|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +const AGENT_LOOP_PACKAGE = 'voku/agent-loop'; |
| 6 | +const OWNED_AGENT_PACKAGES = [ |
| 7 | + 'voku/agent-kanban', |
| 8 | + 'voku/agent-learning', |
| 9 | + 'voku/agent-map', |
| 10 | + 'voku/agent-recall-compiler', |
| 11 | + 'voku/agent-session', |
| 12 | +]; |
| 13 | +const STALE_SIBLING_RELEASE_FIELDS = [ |
| 14 | + 'agent_kanban_release', |
| 15 | + 'agent_learning_release', |
| 16 | + 'agent_map_release', |
| 17 | + 'agent_recall_compiler_release', |
| 18 | + 'agent_session_release', |
| 19 | +]; |
| 20 | + |
| 21 | +if ($argc < 3 || $argc > 4) { |
| 22 | + fwrite(STDERR, "Usage: php tools/agent-loop/verify-release-set.php <issue.json> <composer.json> [composer.lock]\n"); |
| 23 | + exit(2); |
| 24 | +} |
| 25 | + |
| 26 | +try { |
| 27 | + $issue = readJsonObject($argv[1]); |
| 28 | + $composer = readJsonObject($argv[2]); |
| 29 | + $toolchain = requireArray($issue, 'toolchain', $argv[1]); |
| 30 | + $require = stringRequirements($composer['require'] ?? [], 'require', $argv[2]); |
| 31 | + $requireDev = stringRequirements($composer['require-dev'] ?? [], 'require-dev', $argv[2]); |
| 32 | + $rootRequirements = $require + $requireDev; |
| 33 | + |
| 34 | + $expectedAgentLoop = requireString($toolchain, 'agent_loop_release', $argv[1]); |
| 35 | + $actualAgentLoop = $rootRequirements[AGENT_LOOP_PACKAGE] ?? null; |
| 36 | + if ($actualAgentLoop !== $expectedAgentLoop) { |
| 37 | + throw new \RuntimeException(sprintf( |
| 38 | + '%s must require %s %s; got %s.', |
| 39 | + $argv[2], |
| 40 | + AGENT_LOOP_PACKAGE, |
| 41 | + $expectedAgentLoop, |
| 42 | + $actualAgentLoop ?? '<missing>', |
| 43 | + )); |
| 44 | + } |
| 45 | + |
| 46 | + foreach (OWNED_AGENT_PACKAGES as $package) { |
| 47 | + if (isset($rootRequirements[$package])) { |
| 48 | + throw new \RuntimeException(sprintf( |
| 49 | + '%s must not constrain %s directly; %s owns the first-party release set.', |
| 50 | + $argv[2], |
| 51 | + $package, |
| 52 | + AGENT_LOOP_PACKAGE, |
| 53 | + )); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + foreach (STALE_SIBLING_RELEASE_FIELDS as $field) { |
| 58 | + if (array_key_exists($field, $toolchain)) { |
| 59 | + throw new \RuntimeException(sprintf( |
| 60 | + '%s.toolchain.%s duplicates transitive release-set authority; record the resolved lock instead.', |
| 61 | + $argv[1], |
| 62 | + $field, |
| 63 | + )); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if ($argc === 4) { |
| 68 | + $lock = readJsonObject($argv[3]); |
| 69 | + $resolved = resolvedVersions($lock, $argv[3]); |
| 70 | + $resolvedAgentLoop = $resolved[AGENT_LOOP_PACKAGE] ?? null; |
| 71 | + if ($resolvedAgentLoop !== $expectedAgentLoop) { |
| 72 | + throw new \RuntimeException(sprintf( |
| 73 | + 'Resolved %s must be %s; got %s.', |
| 74 | + AGENT_LOOP_PACKAGE, |
| 75 | + $expectedAgentLoop, |
| 76 | + $resolvedAgentLoop ?? '<missing>', |
| 77 | + )); |
| 78 | + } |
| 79 | + |
| 80 | + $releaseSet = [AGENT_LOOP_PACKAGE => $resolvedAgentLoop]; |
| 81 | + foreach (OWNED_AGENT_PACKAGES as $package) { |
| 82 | + $version = $resolved[$package] ?? null; |
| 83 | + if ($version === null) { |
| 84 | + throw new \RuntimeException('Resolved release set is missing ' . $package . '.'); |
| 85 | + } |
| 86 | + $releaseSet[$package] = $version; |
| 87 | + } |
| 88 | + ksort($releaseSet, SORT_STRING); |
| 89 | + |
| 90 | + fwrite(STDOUT, json_encode( |
| 91 | + ['resolved_agent_release_set' => $releaseSet], |
| 92 | + JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR, |
| 93 | + ) . PHP_EOL); |
| 94 | + } |
| 95 | +} catch (Throwable $exception) { |
| 96 | + fwrite(STDERR, $exception->getMessage() . "\n"); |
| 97 | + exit(1); |
| 98 | +} |
| 99 | + |
| 100 | +/** @return array<string, mixed> */ |
| 101 | +function readJsonObject(string $path): array |
| 102 | +{ |
| 103 | + $content = file_get_contents($path); |
| 104 | + if ($content === false) { |
| 105 | + throw new \RuntimeException('Cannot read ' . $path . '.'); |
| 106 | + } |
| 107 | + |
| 108 | + $decoded = json_decode($content, true, 512, JSON_THROW_ON_ERROR); |
| 109 | + if (!is_array($decoded)) { |
| 110 | + throw new \RuntimeException($path . ' must contain a JSON object.'); |
| 111 | + } |
| 112 | + |
| 113 | + return $decoded; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * @param array<string, mixed> $data |
| 118 | + * @return array<string, mixed> |
| 119 | + */ |
| 120 | +function requireArray(array $data, string $key, string $path): array |
| 121 | +{ |
| 122 | + $value = $data[$key] ?? null; |
| 123 | + if (!is_array($value)) { |
| 124 | + throw new \RuntimeException(sprintf('%s.%s must be an object.', $path, $key)); |
| 125 | + } |
| 126 | + |
| 127 | + return $value; |
| 128 | +} |
| 129 | + |
| 130 | +/** @param array<string, mixed> $data */ |
| 131 | +function requireString(array $data, string $key, string $path): string |
| 132 | +{ |
| 133 | + $value = $data[$key] ?? null; |
| 134 | + if (!is_string($value) || $value === '') { |
| 135 | + throw new \RuntimeException(sprintf('%s.%s must be a non-empty string.', $path, $key)); |
| 136 | + } |
| 137 | + |
| 138 | + return $value; |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * @param mixed $requirements |
| 143 | + * @return array<string, string> |
| 144 | + */ |
| 145 | +function stringRequirements(mixed $requirements, string $section, string $path): array |
| 146 | +{ |
| 147 | + if (!is_array($requirements)) { |
| 148 | + throw new \RuntimeException(sprintf('%s.%s must be an object.', $path, $section)); |
| 149 | + } |
| 150 | + |
| 151 | + $result = []; |
| 152 | + foreach ($requirements as $package => $constraint) { |
| 153 | + if (!is_string($package) || !is_string($constraint)) { |
| 154 | + throw new \RuntimeException(sprintf('%s.%s must contain string package constraints.', $path, $section)); |
| 155 | + } |
| 156 | + $result[$package] = $constraint; |
| 157 | + } |
| 158 | + |
| 159 | + return $result; |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * @param array<string, mixed> $lock |
| 164 | + * @return array<string, string> |
| 165 | + */ |
| 166 | +function resolvedVersions(array $lock, string $path): array |
| 167 | +{ |
| 168 | + $resolved = []; |
| 169 | + foreach (['packages', 'packages-dev'] as $section) { |
| 170 | + $packages = $lock[$section] ?? []; |
| 171 | + if (!is_array($packages)) { |
| 172 | + throw new \RuntimeException(sprintf('%s.%s must be an array.', $path, $section)); |
| 173 | + } |
| 174 | + foreach ($packages as $package) { |
| 175 | + if (!is_array($package)) { |
| 176 | + continue; |
| 177 | + } |
| 178 | + $name = $package['name'] ?? null; |
| 179 | + $version = $package['version'] ?? null; |
| 180 | + if (is_string($name) && is_string($version)) { |
| 181 | + $resolved[$name] = $version; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + return $resolved; |
| 187 | +} |
0 commit comments