@zvada/agent-server 0.3.23 → 0.3.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (246) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +14 -5
  3. package/dist/accounts/chatgpt.js +144 -0
  4. package/dist/accounts/check.js +71 -0
  5. package/dist/accounts/claude.js +80 -0
  6. package/dist/accounts/endpoint.js +104 -0
  7. package/dist/accounts/http.js +58 -0
  8. package/dist/accounts/index.js +9 -0
  9. package/dist/client/client.js +686 -0
  10. package/dist/client/connection.js +338 -0
  11. package/dist/client/history.js +49 -0
  12. package/dist/client/index.js +6 -0
  13. package/dist/client/listeners.js +19 -0
  14. package/dist/client/logins.js +92 -0
  15. package/dist/client/rpc.js +105 -0
  16. package/dist/client/streams.js +432 -0
  17. package/dist/client/transports.js +118 -0
  18. package/dist/client/turns.js +122 -0
  19. package/dist/core/agents/acp/acp-agent.js +425 -0
  20. package/dist/core/agents/acp/adapter.js +231 -0
  21. package/dist/core/agents/acp/client.js +222 -0
  22. package/dist/core/agents/acp/known-agents.js +57 -0
  23. package/dist/core/agents/acp/mappings.js +164 -0
  24. package/dist/core/agents/acp/requests.js +79 -0
  25. package/dist/core/agents/attachments.js +14 -0
  26. package/dist/core/agents/base.js +64 -0
  27. package/dist/core/agents/claude-code/adapter.js +474 -0
  28. package/dist/core/agents/claude-code/approvals.js +245 -0
  29. package/dist/core/agents/claude-code/claude-agent.js +319 -0
  30. package/dist/core/agents/claude-code/credentials.js +142 -0
  31. package/dist/core/agents/claude-code/events.js +11 -0
  32. package/dist/core/agents/claude-code/login.js +217 -0
  33. package/dist/core/agents/claude-code/mcp.js +181 -0
  34. package/dist/core/agents/claude-code/models.js +74 -0
  35. package/dist/core/agents/claude-code/options.js +240 -0
  36. package/dist/core/agents/claude-code/outcome.js +204 -0
  37. package/dist/core/agents/claude-code/results.js +58 -0
  38. package/dist/core/agents/claude-code/sdk-pair.js +59 -0
  39. package/dist/core/agents/claude-code/session.js +593 -0
  40. package/dist/core/agents/claude-code/spawn.js +125 -0
  41. package/dist/core/agents/claude-code/spend.js +118 -0
  42. package/dist/core/agents/claude-code/tools.js +131 -0
  43. package/dist/core/agents/claude-code/turn-usage.js +121 -0
  44. package/dist/core/agents/claude-code/turn.js +199 -0
  45. package/dist/core/agents/codex-app-server/adapter.js +463 -0
  46. package/dist/core/agents/codex-app-server/client.js +338 -0
  47. package/dist/core/agents/codex-app-server/codex-agent.js +294 -0
  48. package/dist/core/agents/codex-app-server/commands.js +184 -0
  49. package/dist/core/agents/codex-app-server/credentials.js +92 -0
  50. package/dist/core/agents/codex-app-server/effort.js +13 -0
  51. package/dist/core/agents/codex-app-server/events.js +18 -0
  52. package/dist/core/agents/codex-app-server/homes.js +118 -0
  53. package/dist/core/agents/codex-app-server/input.js +52 -0
  54. package/dist/core/agents/codex-app-server/items.js +307 -0
  55. package/dist/core/agents/codex-app-server/login.js +183 -0
  56. package/dist/core/agents/codex-app-server/mcp.js +323 -0
  57. package/dist/core/agents/codex-app-server/models.js +160 -0
  58. package/dist/core/agents/codex-app-server/options.js +155 -0
  59. package/dist/core/agents/codex-app-server/plan-approval.js +124 -0
  60. package/dist/core/agents/codex-app-server/protocol.js +38 -0
  61. package/dist/core/agents/codex-app-server/requests.js +250 -0
  62. package/dist/core/agents/codex-app-server/session.js +278 -0
  63. package/dist/core/agents/codex-app-server/subagents.js +260 -0
  64. package/dist/core/agents/codex-app-server/thread-settings.js +214 -0
  65. package/dist/core/agents/codex-app-server/turn-items.js +69 -0
  66. package/dist/core/agents/codex-app-server/turn.js +448 -0
  67. package/dist/core/agents/codex-app-server/user-login.js +78 -0
  68. package/dist/core/agents/config-fingerprint.js +10 -0
  69. package/dist/core/agents/credentials.js +71 -0
  70. package/dist/core/agents/diffs.js +27 -0
  71. package/dist/core/agents/elicitation.js +42 -0
  72. package/dist/core/agents/inherit-config.js +5 -0
  73. package/dist/core/agents/login.js +56 -0
  74. package/dist/core/agents/mcp-config.js +109 -0
  75. package/dist/core/agents/plan.js +6 -0
  76. package/dist/core/agents/process-pool.js +307 -0
  77. package/dist/core/agents/registry.js +49 -0
  78. package/dist/core/agents/scripted/adapter.js +86 -0
  79. package/dist/core/agents/scripted/mcp-call.js +74 -0
  80. package/dist/core/agents/scripted/scripted-agent.js +330 -0
  81. package/dist/core/agents/scripted/scripts.js +154 -0
  82. package/dist/core/agents/scripted/steps.js +87 -0
  83. package/dist/core/agents/session-agent.js +209 -0
  84. package/dist/core/agents/stderr-tail.js +71 -0
  85. package/dist/core/agents/thinking-levels.js +9 -0
  86. package/dist/core/agents/types.js +1 -0
  87. package/dist/core/diagnostics.js +23 -0
  88. package/dist/core/index.js +31 -0
  89. package/dist/core/presets.js +114 -0
  90. package/dist/core/processes/identity.js +91 -0
  91. package/dist/core/processes/lifeline.js +210 -0
  92. package/dist/core/processes/native-processes.js +299 -0
  93. package/dist/core/processes/process-table.js +163 -0
  94. package/dist/core/provision/cache-trust.js +121 -0
  95. package/dist/core/provision/cache-usage.js +61 -0
  96. package/dist/core/provision/checksums.js +22 -0
  97. package/dist/core/provision/extract.js +28 -0
  98. package/dist/core/provision/index.js +4 -0
  99. package/dist/core/provision/install-build.js +99 -0
  100. package/dist/core/provision/npm.js +316 -0
  101. package/dist/core/provision/pins.js +35 -0
  102. package/dist/core/provision/platform.js +65 -0
  103. package/dist/core/provision/provisioner.js +372 -0
  104. package/dist/core/provision/releases.js +68 -0
  105. package/dist/core/provision/tools.js +279 -0
  106. package/dist/core/proxy/anthropic-proxy.js +163 -0
  107. package/dist/core/proxy/api-key-store.js +30 -0
  108. package/dist/core/proxy/index.js +3 -0
  109. package/dist/core/proxy/loopback.js +86 -0
  110. package/dist/core/runtime/agent-runtime.js +434 -0
  111. package/dist/core/runtime/event-processor.js +379 -0
  112. package/dist/core/runtime/event-sink.js +65 -0
  113. package/dist/core/runtime/harness-clis.js +83 -0
  114. package/dist/core/runtime/harness-logins.js +313 -0
  115. package/dist/core/runtime/login-reads.js +71 -0
  116. package/dist/core/runtime/model-lists.js +160 -0
  117. package/dist/core/runtime/permission-policy.js +84 -0
  118. package/dist/core/runtime/request-broker.js +139 -0
  119. package/dist/core/runtime/steering.js +79 -0
  120. package/dist/core/runtime/turn-outcome.js +16 -0
  121. package/dist/core/runtime/turn.js +329 -0
  122. package/dist/core/utils/bounded.js +13 -0
  123. package/dist/core/utils/deadline.js +57 -0
  124. package/dist/core/utils/errors.js +176 -0
  125. package/dist/core/utils/is-record.js +4 -0
  126. package/dist/core/utils/loopback.js +11 -0
  127. package/dist/index.js +4 -0
  128. package/dist/operator/index.js +10 -0
  129. package/dist/operator/mcp.js +139 -0
  130. package/dist/operator/operator.js +499 -0
  131. package/dist/operator/render.js +408 -0
  132. package/dist/operator/view.js +35 -0
  133. package/dist/protocol/account.js +96 -0
  134. package/dist/protocol/async-queue.js +65 -0
  135. package/dist/protocol/auth.js +169 -0
  136. package/dist/protocol/canonical.js +18 -0
  137. package/dist/protocol/clis.js +63 -0
  138. package/dist/protocol/config.js +137 -0
  139. package/dist/protocol/conversation.js +231 -0
  140. package/dist/protocol/credentials.js +141 -0
  141. package/dist/protocol/errors.js +181 -0
  142. package/dist/protocol/execution.js +28 -0
  143. package/dist/protocol/factories.js +231 -0
  144. package/dist/protocol/guards.js +71 -0
  145. package/dist/protocol/harness.js +93 -0
  146. package/dist/protocol/history.js +201 -0
  147. package/dist/protocol/ids.js +37 -0
  148. package/dist/protocol/index.js +36 -0
  149. package/dist/protocol/lifecycle.js +562 -0
  150. package/dist/protocol/meta-keys.js +17 -0
  151. package/dist/protocol/meta.js +13 -0
  152. package/dist/protocol/model-catalog.js +223 -0
  153. package/dist/protocol/models.js +84 -0
  154. package/dist/protocol/part-input.js +94 -0
  155. package/dist/protocol/parts.js +166 -0
  156. package/dist/protocol/plan.js +31 -0
  157. package/dist/protocol/reduce.js +484 -0
  158. package/dist/protocol/selectors.js +136 -0
  159. package/dist/protocol/seq-cursor.js +37 -0
  160. package/dist/protocol/stop-reasons.js +41 -0
  161. package/dist/protocol/thinking.js +31 -0
  162. package/dist/protocol/time.js +21 -0
  163. package/dist/protocol/tokens.js +57 -0
  164. package/dist/protocol/tool-closure.js +70 -0
  165. package/dist/protocol/tool-state.js +153 -0
  166. package/dist/protocol/verify.js +467 -0
  167. package/dist/protocol/vocabulary.js +27 -0
  168. package/dist/protocol/wire-api.js +94 -0
  169. package/dist/protocol/wire.js +669 -0
  170. package/dist/server/acp/binding.js +251 -0
  171. package/dist/server/acp/session-records.js +53 -0
  172. package/dist/server/acp/translate.js +401 -0
  173. package/dist/server/agent-server.js +338 -0
  174. package/dist/server/bin.js +492 -0
  175. package/dist/server/feedback.js +216 -0
  176. package/dist/server/history.js +123 -0
  177. package/dist/server/index.js +8 -0
  178. package/dist/server/install.js +171 -0
  179. package/dist/server/open-parts.js +87 -0
  180. package/dist/server/routes.js +291 -0
  181. package/dist/server/secrets.js +19 -0
  182. package/dist/server/session-gate.js +54 -0
  183. package/dist/server/session-log.js +66 -0
  184. package/dist/server/session-store.js +243 -0
  185. package/dist/server/subscriptions.js +189 -0
  186. package/dist/server/transports.js +214 -0
  187. package/dist/server/wire-error.js +26 -0
  188. package/dist/sync/event-log.js +89 -0
  189. package/dist/sync/fit.js +202 -0
  190. package/dist/sync/history-reader.js +33 -0
  191. package/dist/sync/history-sqlite.js +281 -0
  192. package/dist/sync/history.js +163 -0
  193. package/dist/sync/index.js +5 -0
  194. package/dist/sync/journal-sqlite.js +316 -0
  195. package/dist/sync/journal.js +10 -0
  196. package/dist/sync/live-tails.js +85 -0
  197. package/dist/sync/node-sqlite.js +27 -0
  198. package/dist/sync/projection.js +32 -0
  199. package/dist/sync/recovery.js +117 -0
  200. package/dist/sync/retry.js +45 -0
  201. package/dist/sync/schema.js +73 -0
  202. package/dist/sync/sql.js +94 -0
  203. package/dist/sync/sqlite-file.js +31 -0
  204. package/dist/sync/sqlite.js +18 -0
  205. package/dist/sync/worker.js +212 -0
  206. package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
  207. package/dist/types/core/agents/claude-code/claude-agent.d.ts +4 -1
  208. package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
  209. package/dist/types/core/agents/claude-code/options.d.ts +24 -11
  210. package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
  211. package/dist/types/core/agents/claude-code/session.d.ts +8 -0
  212. package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
  213. package/dist/types/core/agents/codex-app-server/homes.d.ts +6 -0
  214. package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
  215. package/dist/types/core/agents/codex-app-server/login.d.ts +12 -0
  216. package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
  217. package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
  218. package/dist/types/core/agents/codex-app-server/session.d.ts +4 -1
  219. package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
  220. package/dist/types/core/agents/process-pool.d.ts +8 -0
  221. package/dist/types/core/agents/process-pool.d.ts.map +1 -1
  222. package/dist/types/core/presets.d.ts +2 -2
  223. package/dist/types/core/presets.d.ts.map +1 -1
  224. package/dist/types/server/bin.d.ts +3 -0
  225. package/dist/types/server/bin.d.ts.map +1 -0
  226. package/dist/types/server/transports.d.ts +2 -1
  227. package/dist/types/server/transports.d.ts.map +1 -1
  228. package/dist/types/sync/node-sqlite.d.ts +4 -1
  229. package/dist/types/sync/node-sqlite.d.ts.map +1 -1
  230. package/docs/consuming.md +30 -4
  231. package/docs/deploy.md +28 -0
  232. package/docs/harnesses.md +13 -1
  233. package/package.json +43 -18
  234. package/src/core/agents/claude-code/approvals.ts +5 -1
  235. package/src/core/agents/claude-code/claude-agent.ts +32 -5
  236. package/src/core/agents/claude-code/options.ts +24 -11
  237. package/src/core/agents/claude-code/session.ts +13 -0
  238. package/src/core/agents/codex-app-server/homes.ts +11 -2
  239. package/src/core/agents/codex-app-server/login.ts +30 -0
  240. package/src/core/agents/codex-app-server/requests.ts +7 -6
  241. package/src/core/agents/codex-app-server/session.ts +11 -2
  242. package/src/core/agents/process-pool.ts +30 -4
  243. package/src/core/presets.ts +3 -2
  244. package/src/server/bin.ts +25 -10
  245. package/src/server/transports.ts +6 -1
  246. package/src/sync/node-sqlite.ts +19 -3
@@ -0,0 +1,35 @@
1
+ /**
2
+ * The CLI versions this engine release is tested against — the versions
3
+ * `agent-server install` (and on-demand provisioning) downloads.
4
+ *
5
+ * - `claude` is the version of the `@anthropic-ai/claude-agent-sdk-<platform>`
6
+ * packages, which carry the native CLI matched to the SDK JS we bundle. It
7
+ * MUST track the lockfile's claude-agent-sdk version (a test enforces this);
8
+ * bump both together. When the SDK is resolvable on disk, its real installed
9
+ * version wins over this pin so the pair can never skew.
10
+ * - `codex` is the `@openai/codex` platform-package version the
11
+ * codex-app-server adapter's protocol snapshot was verified against (see
12
+ * AGENTS.md "Verifying harness protocols"). Bump after re-verifying. The pin
13
+ * governs downloads (compiled binaries, bare sandboxes, `pinned` mode); in
14
+ * `auto` an installed `@openai/codex` package (a lockfile's) runs instead,
15
+ * and a `codex` on PATH only in `system` mode.
16
+ *
17
+ * After any bump, run `bun run releases:refresh` (each platform's build and
18
+ * its checksums, `releases.ts`) and `bun run models:refresh` (the model
19
+ * catalog, which records the versions that listed it); tests fail until both
20
+ * match these pins.
21
+ */
22
+ export const DEFAULT_PINS = {
23
+ // 0.3.288 verified 2026-10-03: the paired CLI honors `--thinking-display
24
+ // summarized`, so reasoning parts carry text (2.1.233 ignored the flag and
25
+ // streamed token-estimate placeholders). Re-check thinking summaries on
26
+ // every bump (see options.ts extraArgs).
27
+ claude: "0.3.288",
28
+ // 0.160.0 re-verified 2026-10-03 against generate-ts: thread/turn/item
29
+ // methods, token usage, approvals, and host-managed ChatGPT auth changed
30
+ // only additively since 0.153.4. Of the GPT-6 family, 0.153.4 lists only
31
+ // gpt-6-astra; the rest needs 0.159 or newer.
32
+ // Since 0.160 a thread has one writer: resuming it from a second process
33
+ // fails until the first releases it.
34
+ codex: "0.160.0",
35
+ };
@@ -0,0 +1,65 @@
1
+ import { constants, accessSync, existsSync } from "node:fs";
2
+ import { delimiter, join } from "node:path";
3
+ /**
4
+ * Musl detection mirrors the Claude SDK's own check: a Node process report
5
+ * without `glibcVersionRuntime` means musl. Runtimes without process.report
6
+ * (or partial implementations) fall back to the Alpine marker file.
7
+ */
8
+ function isMusl() {
9
+ if (process.platform !== "linux")
10
+ return false;
11
+ try {
12
+ const report = process.report?.getReport?.();
13
+ if (report?.header)
14
+ return !report.header.glibcVersionRuntime;
15
+ }
16
+ catch {
17
+ // fall through to the filesystem heuristic
18
+ }
19
+ return existsSync("/etc/alpine-release");
20
+ }
21
+ export function detectPlatform() {
22
+ return { platform: process.platform, arch: process.arch, musl: isMusl() };
23
+ }
24
+ /** A platform as the cache and `install --platform` name it: `darwin-arm64`, `linux-x64-musl`. */
25
+ export function platformName(p) {
26
+ return `${p.platform}-${p.arch}${p.musl ? "-musl" : ""}`;
27
+ }
28
+ /** The platform a name from `platformName` stands for (one a vendor publishes: `CliToolSpec.platforms`). */
29
+ export function parsePlatform(name) {
30
+ const [platform = "", arch = "", musl] = name.split("-");
31
+ return { platform, arch, musl: musl === "musl" };
32
+ }
33
+ /**
34
+ * Suffix of `@anthropic-ai/claude-agent-sdk-<suffix>` — the platform package
35
+ * carrying the native `claude` binary. Claude ships dedicated musl builds.
36
+ */
37
+ export function claudePackageSuffix(p) {
38
+ return platformName(p);
39
+ }
40
+ /**
41
+ * Suffix of `@openai/codex@<version>-<suffix>` — codex publishes platform
42
+ * builds as version suffixes of the main package. Its linux builds are static
43
+ * (musl) already, so there is no separate musl variant.
44
+ */
45
+ export function codexPackageSuffix(p) {
46
+ return `${p.platform}-${p.arch}`;
47
+ }
48
+ /** Locate an executable on PATH (the `which` we can unit-test and run anywhere). */
49
+ export function findOnPath(name, env = process.env, platform = process.platform) {
50
+ const paths = (env.PATH ?? "").split(delimiter).filter(Boolean);
51
+ const names = platform === "win32" ? [`${name}.exe`, `${name}.cmd`, name] : [name];
52
+ for (const dir of paths) {
53
+ for (const file of names) {
54
+ const candidate = join(dir, file);
55
+ try {
56
+ accessSync(candidate, constants.X_OK);
57
+ return candidate;
58
+ }
59
+ catch {
60
+ // not here — keep walking
61
+ }
62
+ }
63
+ }
64
+ return undefined;
65
+ }
@@ -0,0 +1,372 @@
1
+ import { existsSync, lstatSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { delimiter, dirname, join, resolve } from "node:path";
4
+ import { classifyError } from "../../protocol/index.js";
5
+ import { CliProvisionError } from "../utils/errors.js";
6
+ import { managedDirectory, trustedBinary, trustedBinaryIn, trustedCacheDirectory, } from "./cache-trust.js";
7
+ import { markUsed, pruneCache } from "./cache-usage.js";
8
+ import { expectedBuild } from "./checksums.js";
9
+ import { extractTarGz } from "./extract.js";
10
+ import { installBuild, noBinary } from "./install-build.js";
11
+ import { DEFAULT_DOWNLOAD_TIMEOUTS, DEFAULT_NPM_REGISTRY, withoutSecrets, } from "./npm.js";
12
+ import { detectPlatform } from "./platform.js";
13
+ import { CLI_RELEASES } from "./releases.js";
14
+ import { CLI_TOOLS, CLI_TOOL_SPECS, binaryIn } from "./tools.js";
15
+ function failureOf(error) {
16
+ return { error: messageOf(error), category: classifyError(error) };
17
+ }
18
+ const MODES = ["auto", "pinned", "system"];
19
+ /** A build this process keeps resolving is recorded as used again at most this often. */
20
+ const MARK_USED_EVERY_MS = 60 * 60 * 1000;
21
+ function defaultLog(line) {
22
+ console.error(`[agent-server] ${line}`);
23
+ }
24
+ function messageOf(error) {
25
+ return error instanceof Error ? error.message : String(error);
26
+ }
27
+ /**
28
+ * Resolves the native CLIs the harnesses spawn (`claude`, `codex`) and — in
29
+ * managed modes — downloads the pinned builds from the npm registry into a
30
+ * local cache, checked against this release's table of them (`releases.ts`)
31
+ * and installed atomically (`install-build.ts`), so concurrent processes
32
+ * converge on one copy. What differs per tool is its `CLI_TOOL_SPECS` entry.
33
+ *
34
+ * One instance per runtime; `resolve` is memoized per tool (a failure clears
35
+ * the memo so a transient network error doesn't poison the process).
36
+ */
37
+ export class CliProvisioner {
38
+ mode;
39
+ cacheDir;
40
+ registry;
41
+ /** Explicit pins only; a tool's spec says its default. */
42
+ pins;
43
+ overrides;
44
+ bundleDirs;
45
+ log;
46
+ fetch;
47
+ platform;
48
+ env;
49
+ extract;
50
+ hostResolve;
51
+ releases;
52
+ timeouts;
53
+ memo = new Map();
54
+ /** The tools a resolve or install is working on now, by how many. */
55
+ resolving = new Map();
56
+ /** Each tool's download while it runs. */
57
+ downloads = new Map();
58
+ /** Each tool's last failure to resolve, until one succeeds. */
59
+ failures = new Map();
60
+ /** The cache entries this process runs (`<tool>/<entry>`), and when each use was last recorded. */
61
+ inUse = new Map();
62
+ /** Lines said once per process, however often a status asks. */
63
+ said = new Set();
64
+ observers = new Set();
65
+ constructor(options = {}, deps = {}) {
66
+ this.env = deps.env ?? process.env;
67
+ this.mode = options.mode ?? this.modeFromEnv();
68
+ this.cacheDir = resolve(options.cacheDir ??
69
+ this.env.AGENT_SERVER_CACHE_DIR ??
70
+ join(this.env.XDG_CACHE_HOME ?? join(homedir(), ".cache"), "agent-server", "cli"));
71
+ this.registry = options.registry ?? this.env.AGENT_SERVER_NPM_REGISTRY ?? DEFAULT_NPM_REGISTRY;
72
+ this.pins = options.pins ?? {};
73
+ this.overrides = options.overrides ?? {};
74
+ this.bundleDirs = (options.bundleDirs ??
75
+ this.env.AGENT_SERVER_BUNDLE_DIR?.split(delimiter) ??
76
+ [])
77
+ .filter(Boolean)
78
+ .map((dir) => resolve(dir));
79
+ this.log = options.log ?? defaultLog;
80
+ this.fetch = deps.fetch ?? fetch;
81
+ this.platform = deps.platform ?? detectPlatform();
82
+ this.extract = deps.extract ?? extractTarGz;
83
+ this.releases = deps.releases ?? CLI_RELEASES;
84
+ this.timeouts = deps.timeouts ?? DEFAULT_DOWNLOAD_TIMEOUTS;
85
+ this.hostResolve =
86
+ deps.hostResolve ??
87
+ ((tool) => {
88
+ const found = CLI_TOOL_SPECS[tool].hostInstall({
89
+ platform: this.platform,
90
+ env: this.env,
91
+ searchPath: this.mode === "system",
92
+ });
93
+ return found && { ...found, source: "host" };
94
+ });
95
+ }
96
+ /**
97
+ * Resolve a CLI per the provisioning mode. `undefined` (claude in `system`
98
+ * mode only) means "let the harness's own resolution speak" — the Claude SDK
99
+ * finds its sibling platform package and has the clearest failure message.
100
+ */
101
+ resolve(tool) {
102
+ return this.resolveMemo(tool, false);
103
+ }
104
+ /** Warm the managed cache with the pinned build (the `install` command). */
105
+ install(tool) {
106
+ return this.working(tool, () => this.ensureInstalled(tool));
107
+ }
108
+ /** Every install's progress; returns an unsubscribe. */
109
+ observe(observer) {
110
+ this.observers.add(observer);
111
+ return () => this.observers.delete(observer);
112
+ }
113
+ /**
114
+ * Where `tool` is now, without installing it: ready, being installed,
115
+ * failed the last time it was resolved, or missing (with the version an
116
+ * install would fetch).
117
+ */
118
+ async status(tool) {
119
+ try {
120
+ const found = this.locate(tool);
121
+ if (found)
122
+ return { state: "ready", ...found };
123
+ }
124
+ catch (error) {
125
+ return { state: "failed", ...failureOf(error) };
126
+ }
127
+ const { version } = this.build(tool);
128
+ const download = this.downloads.get(tool);
129
+ if (download || this.resolving.has(tool)) {
130
+ return {
131
+ state: "installing",
132
+ version,
133
+ receivedBytes: download?.receivedBytes ?? 0,
134
+ ...(download?.totalBytes !== undefined && { totalBytes: download.totalBytes }),
135
+ };
136
+ }
137
+ const failure = this.failures.get(tool);
138
+ // Where nothing installs (`system`), there is no version to name.
139
+ if (this.mode === "system")
140
+ return failure ? { state: "failed", ...failure } : { state: "missing" };
141
+ return failure ? { state: "failed", version, ...failure } : { state: "missing", version };
142
+ }
143
+ /**
144
+ * Remove cached builds no process used for 14 days, except what this one
145
+ * runs, and what dead installs left — and, as `install --prune` asks,
146
+ * builds written by an agent-server that records no use, by their age.
147
+ * Returns what it removed.
148
+ */
149
+ prune() {
150
+ return pruneCache(this.ensureTrustedCacheRoot(), CLI_TOOLS, new Set(this.inUse.keys()), true);
151
+ }
152
+ resolveMemo(tool, again) {
153
+ let pending = this.memo.get(tool);
154
+ if (!pending) {
155
+ pending = this.working(tool, () => this.resolveUncached(tool)).then((resolved) => {
156
+ if (resolved)
157
+ this.log(`${tool} CLI: ${resolved.path} (${resolved.source}${withVersion(resolved)})`);
158
+ return resolved;
159
+ });
160
+ this.memo.set(tool, pending);
161
+ }
162
+ const memoized = pending;
163
+ return memoized
164
+ .then((resolved) => {
165
+ if (resolved?.source === "cache" || resolved?.source === "downloaded") {
166
+ const root = this.ensureTrustedCacheRoot();
167
+ if (!managedDirectory(root, dirname(resolved.path), false) ||
168
+ !existsSync(resolved.path)) {
169
+ // Removed under this process (by hand, or a prune that took it
170
+ // for unused): resolved again, once, which installs it again.
171
+ if (this.memo.get(tool) === memoized)
172
+ this.memo.delete(tool);
173
+ if (again)
174
+ throw new CliProvisionError(`Cached CLI disappeared: ${resolved.path}`);
175
+ return this.resolveMemo(tool, true);
176
+ }
177
+ trustedBinary(resolved.path, lstatSync(resolved.path));
178
+ // A process that keeps running its build keeps it from a prune (hourly at most).
179
+ void this.used(tool, this.build(tool).entry, false).catch(() => { });
180
+ }
181
+ return resolved;
182
+ })
183
+ .catch((error) => {
184
+ if (this.memo.get(tool) === memoized)
185
+ this.memo.delete(tool);
186
+ throw error;
187
+ });
188
+ }
189
+ /** Run a resolve or install of `tool`, saying meanwhile that it is installing, and recording how it ended. */
190
+ async working(tool, work) {
191
+ this.resolving.set(tool, (this.resolving.get(tool) ?? 0) + 1);
192
+ try {
193
+ const result = await work();
194
+ this.failures.delete(tool);
195
+ return result;
196
+ }
197
+ catch (error) {
198
+ this.failures.set(tool, failureOf(error));
199
+ throw error;
200
+ }
201
+ finally {
202
+ const left = (this.resolving.get(tool) ?? 1) - 1;
203
+ if (left)
204
+ this.resolving.set(tool, left);
205
+ else
206
+ this.resolving.delete(tool);
207
+ }
208
+ }
209
+ async resolveUncached(tool) {
210
+ const found = this.locate(tool);
211
+ if (found?.source === "cache")
212
+ await this.used(tool, this.build(tool).entry, true);
213
+ if (found)
214
+ return found;
215
+ if (this.mode === "system") {
216
+ const missing = CLI_TOOL_SPECS[tool].notOnHost?.();
217
+ if (missing)
218
+ throw missing;
219
+ return undefined;
220
+ }
221
+ return this.ensureInstalled(tool);
222
+ }
223
+ /** The build `tool` resolves to without a download, per the mode. */
224
+ locate(tool) {
225
+ const spec = CLI_TOOL_SPECS[tool];
226
+ const override = this.overrides[tool] ?? this.env[spec.overrideEnv];
227
+ if (override) {
228
+ if (!existsSync(override)) {
229
+ throw new CliProvisionError(`${tool} CLI override points to a missing file: ${override} ` +
230
+ `(from ${this.overrides[tool] ? "options.overrides" : `$${spec.overrideEnv}`})`, { category: "invalid_request" });
231
+ }
232
+ return { path: override, source: "override" };
233
+ }
234
+ const build = this.build(tool);
235
+ // @ref LLP 0010#implementation-2026-10-31 — what an app or image ships, before the host's; the operator vouches for it
236
+ for (const dir of this.bundleDirs) {
237
+ // A regular file: a symlink could redirect the spawn.
238
+ const folder = join(dir, tool, build.entry);
239
+ const path = binaryIn(folder, spec.binaries);
240
+ if (path)
241
+ return { path, source: "bundled", version: build.version };
242
+ if (existsSync(folder))
243
+ this.say(`${folder} holds no regular ${spec.binaries[0]}: passed over`);
244
+ }
245
+ if (this.mode !== "pinned") {
246
+ const host = this.hostResolve(tool);
247
+ if (host)
248
+ return { ...host, source: "host" };
249
+ }
250
+ if (this.mode === "system")
251
+ return undefined;
252
+ const root = this.ensureTrustedCacheRoot();
253
+ const cached = trustedBinaryIn(root, join(root, tool, build.entry), spec.binaries);
254
+ return cached ? { path: cached, source: "cache", version: build.version } : undefined;
255
+ }
256
+ /** The build of `tool` this engine runs: an explicit pin, else the tool's default. */
257
+ build(tool) {
258
+ const spec = CLI_TOOL_SPECS[tool];
259
+ let version = this.pins[tool];
260
+ if (!version) {
261
+ const note = spec.defaultVersionNote?.();
262
+ if (note)
263
+ this.say(note);
264
+ version = spec.defaultVersion();
265
+ }
266
+ const pkg = spec.package(this.platform, version);
267
+ return { version, pkg, entry: `${version}-${pkg.suffix}` };
268
+ }
269
+ /** Pin trusted aliases to their physical location and recheck on every use. */
270
+ ensureTrustedCacheRoot() {
271
+ this.cacheDir = trustedCacheDirectory(this.cacheDir, true);
272
+ return this.cacheDir;
273
+ }
274
+ async ensureInstalled(tool) {
275
+ const spec = CLI_TOOL_SPECS[tool];
276
+ const root = this.ensureTrustedCacheRoot();
277
+ const build = this.build(tool);
278
+ const dir = join(root, tool, build.entry);
279
+ let path = trustedBinaryIn(root, dir, spec.binaries);
280
+ const downloaded = !path;
281
+ if (!path) {
282
+ await this.download(tool, build, root, dir);
283
+ path = trustedBinaryIn(root, dir, spec.binaries);
284
+ if (!path)
285
+ throw noBinary(build.pkg);
286
+ }
287
+ await this.used(tool, build.entry, true);
288
+ // The cache grew: what no process used for a while goes. Best effort.
289
+ if (downloaded)
290
+ await pruneCache(root, CLI_TOOLS, new Set(this.inUse.keys()), false).catch(() => { });
291
+ return { path, source: downloaded ? "downloaded" : "cache", version: build.version };
292
+ }
293
+ /** Download and install one build, reporting its progress to the observers and the log. */
294
+ async download(tool, build, root, dir) {
295
+ const spec = CLI_TOOL_SPECS[tool];
296
+ const { version, pkg } = build;
297
+ let progress = { tool, version, phase: "downloading", receivedBytes: 0 };
298
+ const report = (next) => {
299
+ progress = { ...progress, ...next };
300
+ this.downloads.set(tool, progress);
301
+ for (const observer of this.observers) {
302
+ try {
303
+ observer(progress);
304
+ }
305
+ catch {
306
+ // A host's progress display must not fail the download.
307
+ }
308
+ }
309
+ };
310
+ const startedAt = Date.now();
311
+ try {
312
+ report({});
313
+ const expected = expectedBuild(tool, version, pkg.suffix, this.releases, spec.localBuild);
314
+ if (expected.source !== "release")
315
+ this.say(`${tool} ${version} (${pkg.suffix}) is not pinned by this agent-server release: checked against ${expected.source === "local" ? expected.vouchedBy : "the registry's checksum only"}`);
316
+ this.log(`downloading ${tool} ${version} (${pkg.suffix}) from ${withoutSecrets(this.registry)}…`);
317
+ const bytes = await installBuild({
318
+ spec,
319
+ pkg,
320
+ expected,
321
+ root,
322
+ dir,
323
+ registry: this.registry,
324
+ fetch: this.fetch,
325
+ timeouts: this.timeouts,
326
+ extract: this.extract,
327
+ onProgress: (phase, receivedBytes, totalBytes) => report({ phase, receivedBytes, totalBytes }),
328
+ });
329
+ const seconds = ((Date.now() - startedAt) / 1000).toFixed(1);
330
+ this.log(`installed ${tool} ${version} → ${dir} (${(bytes / 1024 / 1024).toFixed(0)} MB in ${seconds}s)`);
331
+ report({ phase: "ready" });
332
+ }
333
+ catch (error) {
334
+ report({ phase: "failed", ...failureOf(error) });
335
+ throw error;
336
+ }
337
+ finally {
338
+ this.downloads.delete(tool);
339
+ }
340
+ }
341
+ /**
342
+ * Record that this process runs a cached build, so no prune removes it:
343
+ * now when `now` (it was just found or installed, its record perhaps
344
+ * pruned with it), else at most hourly as it keeps resolving it.
345
+ */
346
+ async used(tool, entry, now) {
347
+ const key = `${tool}/${entry}`;
348
+ const recorded = this.inUse.get(key);
349
+ if (!now && recorded !== undefined && Date.now() - recorded < MARK_USED_EVERY_MS)
350
+ return;
351
+ this.inUse.set(key, Date.now());
352
+ await markUsed(this.ensureTrustedCacheRoot(), tool, entry);
353
+ }
354
+ /** A line said once per process. */
355
+ say(line) {
356
+ if (this.said.has(line))
357
+ return;
358
+ this.said.add(line);
359
+ this.log(line);
360
+ }
361
+ modeFromEnv() {
362
+ const raw = this.env.AGENT_SERVER_PROVISION;
363
+ if (raw === undefined || raw === "")
364
+ return "auto";
365
+ if (MODES.includes(raw))
366
+ return raw;
367
+ throw new CliProvisionError(`Invalid $AGENT_SERVER_PROVISION: ${raw} (expected ${MODES.join(" | ")})`, { category: "invalid_request" });
368
+ }
369
+ }
370
+ function withVersion(resolved) {
371
+ return resolved.version ? ` ${resolved.version}` : "";
372
+ }
@@ -0,0 +1,68 @@
1
+ /**
2
+ * The pinned CLIs' builds, by platform: each npm tarball's integrity, and for
3
+ * Claude the binary's sha256 from Anthropic's signed release manifest (its
4
+ * signature verified when this file was generated).
5
+ */
6
+ export const CLI_RELEASES = {
7
+ claude: {
8
+ version: "0.3.288",
9
+ cli: "2.1.288",
10
+ builds: {
11
+ "darwin-arm64": {
12
+ integrity: "sha512-85niGScWj8sr7yqOjSfqINaKv/zB47T45o4NBq1BmpaKRYFK7grtK24LA6C7cjx1Pznp+WT68mo373fbbwFW0A==",
13
+ sha256: "bbe93063f7a0879a1021b2891e5c9354e5b3b98433e32efe6750f7710afed750",
14
+ },
15
+ "darwin-x64": {
16
+ integrity: "sha512-hY8MQtgAi+cRedFb+qSqZzAYXIfjC+ZiXtCxKCs4OqWbqrdVZ0GKcEv4nB6MfQGs/Qr4U24cDNuO2lskBbZAWA==",
17
+ sha256: "946acab03a55b60e2016e30964c48b96ef6673cb5da684383f4835da410eede0",
18
+ },
19
+ "linux-arm64": {
20
+ integrity: "sha512-CCj0xau64T+Ia+X2wZgjYFSUIWfNJRWIGFbT8+jKiCzQggaBo6OSZ8hAYyME3h13JYrgpyrUTzB5YiwKaw8ZFg==",
21
+ sha256: "359ab6a058fcde9741dff54979a212fd134cdf8e8cfc2f8de02bc350b9e2b9d5",
22
+ },
23
+ "linux-x64": {
24
+ integrity: "sha512-Y5M496UZKttScbzptA45NI+CMHYONnskw0aq3rJ6s+gE6L6Jy8F8UoKt0lQO11R5xTY65TIeCasjQf/bLavuNg==",
25
+ sha256: "0298068b686e7fdbaf9402a7a587bb7f49c0b0e084de09f69145a0719207640c",
26
+ },
27
+ "linux-arm64-musl": {
28
+ integrity: "sha512-66MD4hzNTqrTL5MN21bSfxm/fhbnM+ERsmWHGEsJojxAWY5DXCUlDkaAYM0rxDpdvLMVt9PpRVIiWDptaiFPxA==",
29
+ sha256: "6b9816b225b303dacc17acc9d83016055274498effdbce6227e62d5dbaa762b9",
30
+ },
31
+ "linux-x64-musl": {
32
+ integrity: "sha512-g3HDERLfEamirfp+MJDVl1fT8YYQMILUH65jPzl+SzKLeBu26WmAXAoZFmU4dasEmKZkEnBdL0ZQy3qnDy5EXg==",
33
+ sha256: "b4334268a2ce06bf984dab2d6925cc5afe9d2344fa427fc362e266da0d57a894",
34
+ },
35
+ "win32-arm64": {
36
+ integrity: "sha512-ijVSU5Vl1L1SlEm1djPNEekxHm9FS5GOitz8kL8KLdYDspMA97bgFM4gy/+riA3qn4ERepJkjdDLE+oYMTO+UA==",
37
+ sha256: "5015d2b92866232b85384ac6e5eef4ff155418a92123503e52714c29186af8c6",
38
+ },
39
+ "win32-x64": {
40
+ integrity: "sha512-zh4yfGduDEBKUqb3BPtq/cLYwHoScqm7X5fJhmkSs0BJiSJGlb38j6FXWDvoPfe+OXNjE5onNx5HlAnDzsQzrA==",
41
+ sha256: "84304f7d4b0cd0ebcbe8318695a260151b48991a6659c3366fdd5da290c0ab91",
42
+ },
43
+ },
44
+ },
45
+ codex: {
46
+ version: "0.160.0",
47
+ builds: {
48
+ "darwin-arm64": {
49
+ integrity: "sha512-aefV6cqZA2REZgR//4McyXlp7zLcTti4CI2v3j9IVgNndPBv2kCeNEcz07qeelXcwOdSFPUKb6roA48vZmDgrQ==",
50
+ },
51
+ "darwin-x64": {
52
+ integrity: "sha512-ir7cdsPrb9VkqNqpTECoTiomv04xws/FQ968mHQzuL+2J5Da0Dkh2Qlxp7sguDDZJab4YpYmNjQvBVMXbTeQaQ==",
53
+ },
54
+ "linux-arm64": {
55
+ integrity: "sha512-VnVdsS06YlDsL8OwaJjQ3xdqdJNG4i+eBJBV3LytddknzSaF/urijLzVg3ptkwvoj8C7Gn7iWpVE+y8WEaPiCg==",
56
+ },
57
+ "linux-x64": {
58
+ integrity: "sha512-KI/73OqGrHmR18s7ya7E1NqV6rT0y3lxr0s8S1qR2m6zU6QRF/HlR529jALLh5vjdUnsRT4Ahoxt0axb4kY99g==",
59
+ },
60
+ "win32-arm64": {
61
+ integrity: "sha512-tTvK9ZIGuj9WnYaYOAnfjJVSfPV7dKqWtDHqUPLf0Hadp6SgynTxx7S7iMXRieFeXVOI2XH76KxEHSOTOnz3zw==",
62
+ },
63
+ "win32-x64": {
64
+ integrity: "sha512-/gCFcuOmGlQkgGivWCtY8BNEDixh70Pue0HZOk9S8bj2vUIE1GLD9zcNB/KqqDJmn0S+lIttauLpzQnw6HOCHA==",
65
+ },
66
+ },
67
+ },
68
+ };