<?php
declare(strict_types=1);

// Read-only Composer class-path probe. No App init, DB, token, or HTTP.
$lane = (string)($argv[1] ?? '');
if (!in_array($lane, ['B', 'C'], true)) {
    fwrite(STDERR, "DENIED: B or C required\n");
    exit(78);
}
$root = $lane === 'B' ? '/private/tmp/zhct-http-ai-macos.x2CCZX'
    : '/tmp/zhct-ai-http-20260929.3XDj3w';
$pid = $lane === 'B' ? 13127 : 53772;
$expected = $lane === 'B' ? 'mysql' : 'dm';
if (!is_file($root . '/vendor/autoload.php')) {
    throw new RuntimeException('Worker Composer autoload absent');
}
if ($lane === 'B') {
    $ps = (string)shell_exec('/bin/ps eww -p ' . $pid . ' -o command=');
    if (strpos($ps, 'POC_DIALECT=' . $expected) === false
        || strpos($ps, 'POC_EXTERNAL_STUBS=1') === false) {
        throw new RuntimeException('B worker identity drift');
    }
} else {
    $env = [];
    foreach (explode("\0", (string)file_get_contents('/proc/' . $pid . '/environ')) as $item) {
        $pair = explode('=', $item, 2);
        if (count($pair) === 2) $env[$pair[0]] = $pair[1];
    }
    if (($env['POC_TARGET_ROOT'] ?? '') !== $root
        || ($env['POC_DIALECT'] ?? '') !== $expected
        || ($env['POC_EXTERNAL_STUBS'] ?? '') !== '1') {
        throw new RuntimeException('C worker identity drift');
    }
}
$loader = require $root . '/vendor/autoload.php';
if (!is_object($loader) || !method_exists($loader, 'findFile')) {
    throw new RuntimeException('Composer class loader unavailable');
}
$classes = [
    'app\\common\\CozeApi',
    'app\\api\\model\\User',
    'app\\api\\service\\meal\\Meal',
    'app\\api\\controller\\meal\\Meal',
];
$resolved = [];
foreach ($classes as $class) {
    $file = $loader->findFile($class);
    $real = is_string($file) ? realpath($file) : false;
    if (!is_string($real) || !is_file($real)) {
        throw new RuntimeException('Actual Composer class source unresolved');
    }
    $resolved[$class] = ['path' => $real, 'sha256' => hash_file('sha256', $real)];
}
echo json_encode(['lane' => $lane, 'worker_pid' => $pid,
    'composer_autoload_realpath' => realpath($root . '/vendor/autoload.php'),
    'classes' => $resolved, 'business_http' => 0,
    'business_dml' => 0], JSON_UNESCAPED_SLASHES), "\n";
