instance('config', $config); $definition = require $server . '/config/console.php'; $name = 'prescription-ai:dispatch'; $command = new $definition['commands'][$name](); $command->setApp($app); $run = static fn (): int => $command->run(new Input([]), new Output('buffer')); $lanes = ['prepare', 'qwen', 'openai']; try { $config->set(['enabled' => false], 'prescription_analysis'); dispatchExpect($run() === 0, 'Disabled dispatch must exit before accessing the CLI entry point'); $config->set(['enabled' => true], 'prescription_analysis'); try { $run(); throw new LogicException('Missing think entry point was reported as a successful dispatch'); } catch (RuntimeException $expected) { dispatchExpect(str_contains($expected->getMessage(), 'think'), 'Launch failure should identify the entry point'); } file_put_contents($directory . '/think', <<<'PHP' run(new Input(['--check']), $inspect); dispatchExpect(substr_count($inspect->fetch(), 'missing') === 3 && !is_dir($app->getRuntimePath()), 'Check-only inspection must report missing lanes without creating locks or dispatching work'); $held = []; foreach ($lanes as $lane) { $held[$lane] = Guard::acquire($app->getRuntimePath(), $lane, LOCK_SH); } $healthy = new Output('buffer'); dispatchExpect($command->run(new Input([]), $healthy) === 0 && substr_count($healthy->fetch(), 'busy') === 3, 'Three supervised workers must satisfy the inspection without spawning children'); $worker = new $definition['commands']['prescription-ai:work'](); $worker->setApp($app); $skipped = new Output('buffer'); dispatchExpect($worker->run(new Input(['--lane=prepare', '--once', '--scheduled']), $skipped) === 0 && $skipped->fetch() === '', 'Scheduled worker must exit before a database claim or idle sweep when locked'); foreach ($lanes as $lane) { dispatchExpect(!is_file($directory . '/' . $lane), 'A healthy lane was dispatched'); } fclose($held['openai']); dispatchExpect($run() === 0, 'A single missing lane must be dispatched'); $deadline = microtime(true) + 5; while (!is_file($directory . '/openai') && microtime(true) < $deadline) { usleep(20000); } dispatchExpect(is_file($directory . '/openai') && !is_file($directory . '/prepare') && !is_file($directory . '/qwen'), 'Dispatch should only start the missing lane'); touch($directory . '/release'); while (!is_file($directory . '/openai.done') && microtime(true) < $deadline) { usleep(20000); } dispatchExpect(is_file($directory . '/openai.done'), 'Single fallback did not finish'); if (PHP_OS_FAMILY === 'Linux') { while (Guard::linuxWorkers($directory . '/think') && microtime(true) < $deadline) { usleep(20000); } } unlink($directory . '/openai'); unlink($directory . '/openai.done'); unlink($directory . '/release'); fclose($held['prepare']); fclose($held['qwen']); $single = Guard::acquire($app->getRuntimePath(), 'openai', LOCK_EX | LOCK_NB); dispatchExpect(Guard::acquire($app->getRuntimePath(), 'openai', LOCK_EX | LOCK_NB) === null, 'Overlapping scheduled consumers must not process the same lane'); fclose($single); dispatchExpect($run() === 0, 'Enabled dispatch must succeed'); touch($directory . '/release'); $deadline = microtime(true) + 5; foreach ($lanes as $lane) { $result = $directory . '/' . $lane; while (!is_file($result . '.done') && microtime(true) < $deadline) { usleep(20000); } dispatchExpect(is_file($result . '.done') && file_get_contents($result . '.done') === 'released', "Dispatch blocked on the {$lane} worker or did not start it"); dispatchExpect(json_decode(file_get_contents($result), true) === ['prescription-ai:work', '--lane=' . $lane, '--once', '--scheduled'], "Unexpected {$lane} worker arguments"); } // A relative think path must resolve against the observed process, never the inspector's cwd. $proc = $directory . '/proc'; foreach (['self', '101/cwd', '102/cwd'] as $path) { mkdir($proc . '/' . $path, 0700, true); } touch($proc . '/self/cmdline'); foreach (['101', '102'] as $id) { touch($proc . '/' . $id . '/cwd/think'); file_put_contents($proc . '/' . $id . '/comm', 'php8.2'); file_put_contents($proc . '/' . $id . '/cmdline', implode("\0", ['php', 'think', 'prescription-ai:work', '--lane=openai'])); } $target = $proc . '/101/cwd/think'; foreach (['prepare' => [], 'qwen' => ['--lane', 'qwen'], 'openai' => ['--lane=openai']] as $lane => $options) { file_put_contents($proc . '/101/cmdline', implode("\0", array_merge(['php', './think', 'prescription-ai:work'], $options))); dispatchExpect(Guard::linuxWorkers($target, $proc) === [$lane => 'resident'], 'Legacy workers must be scoped to this application and exact lane'); } file_put_contents($proc . '/101/cmdline', implode("\0", ['php', 'think', 'prescription-ai:work', '--once'])); dispatchExpect(Guard::linuxWorkers($target, $proc) === ['prepare' => 'single_run'], 'A running fallback must occupy its lane'); if (PHP_OS_FAMILY === 'Linux') { file_put_contents($proc . '/101/cmdline', implode("\0", ['/www/server/php/82/bin/php', $target, 'prescription-ai:work', '--lane=qwen'])); dispatchExpect(Guard::linuxWorkers($target, $proc) === ['qwen' => 'resident'], 'Absolute think path from a supervised process'); } $db = new PDO('sqlite::memory:', null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); $db->sqliteCreateFunction('UNIX_TIMESTAMP', 'time'); $db->exec('CREATE TABLE zyt_dev_crontab (name TEXT, type INTEGER, system INTEGER, remark TEXT, command TEXT, params TEXT, status INTEGER, expression TEXT, last_time INTEGER, create_time INTEGER, update_time INTEGER, delete_time INTEGER)'); $migration = file_get_contents($server . '/database/migrations/2026_12_02_prescription_ai_crontab.sql'); $upgrade = file_get_contents($server . '/sql/1.9.20261202/01_prescription_ai_crontab.sql'); $db->exec($migration); $db->exec($upgrade); $select = static fn (): array => $db->query('SELECT name,command,params,status,expression FROM zyt_dev_crontab')->fetchAll(PDO::FETCH_ASSOC); $fresh = $select(); dispatchExpect(count($fresh) === 1 && $fresh[0]['command'] === $name && $fresh[0]['params'] === '' && (int) $fresh[0]['status'] === 1 && $fresh[0]['expression'] === '* * * * *', 'Upgrading must enable one per-minute dispatch'); $db->exec("UPDATE zyt_dev_crontab SET status = 2, expression = '*/5 * * * *'"); $edited = $select(); $db->exec($migration); $db->exec($upgrade); dispatchExpect($select() === $edited, 'Repeated upgrades must retain administrator edits'); $db->exec('DELETE FROM zyt_dev_crontab'); $install = file_get_contents($server . '/public/install/db/like.sql'); preg_match('/^INSERT INTO `la_dev_crontab`[^;]+;/m', $install, $match); dispatchExpect(isset($match[0]), 'Installer must seed the schedule with its configurable prefix'); $db->exec(str_replace('`la_', '`zyt_', $match[0])); dispatchExpect($select() === $fresh, 'Installer and upgrade must register the same task'); echo "Prescription AI dispatch, upgrade and installer checks passed\n"; } finally { $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST); foreach ($files as $file) { $file->isDir() ? rmdir($file->getPathname()) : unlink($file->getPathname()); } rmdir($directory); }