The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import { describe, expect, it } from "@effect/vitest"; |
| 4 | import { |
| 5 | DEFAULT_SERVER_SETTINGS, |
| 6 | ProviderDriverKind, |
| 7 | ProviderInstanceId, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import { AcpRegistrySettings } from "../settings.ts"; |
| 10 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 11 | import { SpawnExecutableResolution } from "@t3tools/shared/shell"; |
| 12 | import * as Deferred from "effect/Deferred"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as FileSystem from "effect/FileSystem"; |
| 15 | import * as Path from "effect/Path"; |
| 16 | import * as Fiber from "effect/Fiber"; |
| 17 | import * as Layer from "effect/Layer"; |
| 18 | import * as Schema from "effect/Schema"; |
| 19 | import * as TestClock from "effect/testing/TestClock"; |
| 20 | import * as HttpClient from "effect/http/HttpClient"; |
| 21 | import * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 22 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 23 | import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 24 | |
| 25 | import * as AcpRegistrySupport from "./AcpRegistrySupport.ts"; |
| 26 | import { prefersHttpMcpTransport } from "./mcpTransport.ts"; |
| 27 | import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost"; |
| 28 | |
| 29 | const registryUrl = "https://registry.test/registry.json"; |
| 30 | const archiveUrl = "https://registry.test/example-agent.bin"; |
| 31 | const decodeAcpRegistrySettings = Schema.decodeSync(AcpRegistrySettings); |
| 32 | const encodeUnknownJson = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown)); |
| 33 | |
| 34 | function makeAgent( |
| 35 | distribution: AcpRegistrySupport.AcpRegistryAgent["distribution"], |
| 36 | ): AcpRegistrySupport.AcpRegistryAgent { |
| 37 | return { |
| 38 | id: "example-agent", |
| 39 | name: "Example Agent", |
| 40 | version: "1.2.3", |
| 41 | description: "ACP Registry test agent", |
| 42 | distribution, |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | function makeRegistry(agent: AcpRegistrySupport.AcpRegistryAgent): string { |
| 47 | return JSON.stringify({ version: "1.0.0", agents: [agent] }); |
| 48 | } |
| 49 | |
| 50 | function settings(input: Partial<AcpRegistrySettings> = {}): AcpRegistrySettings { |
| 51 | return decodeAcpRegistrySettings({ |
| 52 | agentId: "example-agent", |
| 53 | ...input, |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | function layerResolver( |
| 58 | execute: Parameters<typeof HttpClient.make>[0], |
| 59 | environment: NodeJS.ProcessEnv = process.env, |
| 60 | ) { |
| 61 | return Layer.mergeAll( |
| 62 | NodeServices.layer, |
| 63 | NodeCrypto.layer, |
| 64 | TestProviderHost.layer().pipe( |
| 65 | Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 66 | ), |
| 67 | Layer.succeed(HostProcess.Platform, "linux"), |
| 68 | Layer.succeed(HostProcess.Architecture, "x64"), |
| 69 | Layer.succeed(HostProcess.Environment, environment), |
| 70 | Layer.succeed(HttpClient.HttpClient, HttpClient.make(execute)), |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | const makeFakeNpmToolchain = Effect.fn("AcpRegistrySupport.test.makeFakeNpmToolchain")(function* ( |
| 75 | rootDirectory: string, |
| 76 | ) { |
| 77 | const fileSystem = yield* FileSystem.FileSystem; |
| 78 | const path = yield* Path.Path; |
| 79 | const toolchainBin = path.join(rootDirectory, "fake-node", "bin"); |
| 80 | const globalPrefix = path.join(rootDirectory, "tools", "example-agent", "1.2.3", "npm"); |
| 81 | const globalBin = path.join(globalPrefix, "bin"); |
| 82 | const executablePath = path.join(globalBin, "example-agent"); |
| 83 | const npmPath = path.join(toolchainBin, "npm"); |
| 84 | const logPath = path.join(rootDirectory, "npm.log"); |
| 85 | yield* fileSystem.makeDirectory(toolchainBin, { recursive: true }); |
| 86 | yield* fileSystem.makeDirectory(globalPrefix, { recursive: true }); |
| 87 | yield* fileSystem.writeFileString(logPath, ""); |
| 88 | yield* fileSystem.copyFile( |
| 89 | yield* path.fromFileUrl( |
| 90 | new URL("./testing/AcpRegistrySupport.npm.fixture.sh", import.meta.url), |
| 91 | ), |
| 92 | npmPath, |
| 93 | ); |
| 94 | yield* fileSystem.chmod(npmPath, 0o755); |
| 95 | // The fake toolchain runs on the real host, not the simulated provider environment. |
| 96 | const hostEnv = HostProcess.Environment.defaultValue(); |
| 97 | return { |
| 98 | environment: { |
| 99 | ...hostEnv, |
| 100 | PATH: `${toolchainBin}:${hostEnv.PATH ?? ""}`, |
| 101 | FAKE_NPM_LOG: logPath, |
| 102 | FAKE_NPM_PREFIX: path.join(rootDirectory, "external-npm-global"), |
| 103 | FAKE_NPM_MANIFEST: encodeUnknownJson({ |
| 104 | name: "@example/acp", |
| 105 | version: "1.2.3", |
| 106 | bin: { "example-agent": "dist/cli.js" }, |
| 107 | }), |
| 108 | } satisfies NodeJS.ProcessEnv, |
| 109 | executablePath, |
| 110 | globalBin, |
| 111 | logPath, |
| 112 | npmPath, |
| 113 | }; |
| 114 | }); |
| 115 | |
| 116 | const makeFakeUvToolchain = Effect.fn("AcpRegistrySupport.test.makeFakeUvToolchain")(function* ( |
| 117 | rootDirectory: string, |
| 118 | ) { |
| 119 | const fileSystem = yield* FileSystem.FileSystem; |
| 120 | const path = yield* Path.Path; |
| 121 | const toolchainBin = path.join(rootDirectory, "fake-uv", "bin"); |
| 122 | const globalBin = path.join(rootDirectory, "tools", "example-agent", "1.2.3", "python", "bin"); |
| 123 | const executablePath = path.join(globalBin, "fast-agent"); |
| 124 | const uvPath = path.join(toolchainBin, "uv"); |
| 125 | const logPath = path.join(rootDirectory, "uv.log"); |
| 126 | yield* fileSystem.makeDirectory(toolchainBin, { recursive: true }); |
| 127 | yield* fileSystem.writeFileString(logPath, ""); |
| 128 | yield* fileSystem.copyFile( |
| 129 | yield* path.fromFileUrl(new URL("./testing/AcpRegistrySupport.uv.fixture.sh", import.meta.url)), |
| 130 | uvPath, |
| 131 | ); |
| 132 | yield* fileSystem.chmod(uvPath, 0o755); |
| 133 | // The fake toolchain runs on the real host, not the simulated provider environment. |
| 134 | const hostEnv = HostProcess.Environment.defaultValue(); |
| 135 | return { |
| 136 | environment: { |
| 137 | ...hostEnv, |
| 138 | PATH: `${toolchainBin}:${hostEnv.PATH ?? ""}`, |
| 139 | FAKE_UV_LOG: logPath, |
| 140 | FAKE_UV_BIN: path.join(rootDirectory, "external-uv-bin"), |
| 141 | FAKE_UV_EXECUTABLE: executablePath, |
| 142 | } satisfies NodeJS.ProcessEnv, |
| 143 | executablePath, |
| 144 | globalBin, |
| 145 | logPath, |
| 146 | uvPath, |
| 147 | }; |
| 148 | }); |
| 149 | |
| 150 | describe("AcpRegistrySupport", () => { |
| 151 | it("selects the preferred compatible distribution", () => { |
| 152 | const agent = makeAgent({ |
| 153 | binary: { |
| 154 | "linux-x86_64": { |
| 155 | archive: archiveUrl, |
| 156 | cmd: "./bin/example-agent", |
| 157 | args: ["acp"], |
| 158 | }, |
| 159 | }, |
| 160 | npx: { |
| 161 | package: "@example/acp@1.2.3", |
| 162 | args: ["--stdio"], |
| 163 | }, |
| 164 | }); |
| 165 | |
| 166 | expect( |
| 167 | AcpRegistrySupport.resolveAcpRegistryDistribution({ |
| 168 | agent, |
| 169 | preference: "auto", |
| 170 | platformTarget: "linux-x86_64", |
| 171 | }), |
| 172 | ).toMatchObject({ kind: "binary", args: ["acp"] }); |
| 173 | expect( |
| 174 | AcpRegistrySupport.resolveAcpRegistryDistribution({ |
| 175 | agent, |
| 176 | preference: "npx", |
| 177 | platformTarget: "linux-x86_64", |
| 178 | }), |
| 179 | ).toEqual({ |
| 180 | kind: "npx", |
| 181 | packageName: "@example/acp@1.2.3", |
| 182 | args: ["--stdio"], |
| 183 | env: {}, |
| 184 | }); |
| 185 | expect( |
| 186 | AcpRegistrySupport.resolveAcpRegistryDistribution({ |
| 187 | agent, |
| 188 | preference: "binary", |
| 189 | platformTarget: "darwin-aarch64", |
| 190 | }), |
| 191 | ).toBeUndefined(); |
| 192 | expect( |
| 193 | AcpRegistrySupport.resolveAcpRegistryDistribution({ |
| 194 | agent: makeAgent({ |
| 195 | binary: { |
| 196 | "windows-x86_64": { |
| 197 | archive: archiveUrl, |
| 198 | cmd: "./bin/example-agent-x64.exe", |
| 199 | args: ["acp"], |
| 200 | }, |
| 201 | }, |
| 202 | }), |
| 203 | preference: "auto", |
| 204 | platformTarget: "windows-aarch64", |
| 205 | }), |
| 206 | ).toMatchObject({ |
| 207 | kind: "binary", |
| 208 | binaryTarget: { cmd: "./bin/example-agent-x64.exe" }, |
| 209 | }); |
| 210 | expect( |
| 211 | AcpRegistrySupport.resolveAcpRegistryDistribution({ |
| 212 | agent: makeAgent({ |
| 213 | binary: { |
| 214 | "windows-aarch64": { |
| 215 | archive: archiveUrl, |
| 216 | cmd: "./bin/example-agent-arm64.exe", |
| 217 | args: ["acp"], |
| 218 | }, |
| 219 | "windows-x86_64": { |
| 220 | archive: archiveUrl, |
| 221 | cmd: "./bin/example-agent-x64.exe", |
| 222 | args: ["acp"], |
| 223 | }, |
| 224 | }, |
| 225 | }), |
| 226 | preference: "auto", |
| 227 | platformTarget: "windows-aarch64", |
| 228 | }), |
| 229 | ).toMatchObject({ |
| 230 | kind: "binary", |
| 231 | binaryTarget: { cmd: "./bin/example-agent-arm64.exe" }, |
| 232 | }); |
| 233 | }); |
| 234 | |
| 235 | it.effect("resolves a local command on the selected environment without the registry", () => { |
| 236 | const requests: Array<string> = []; |
| 237 | return Effect.gen(function* () { |
| 238 | const fileSystem = yield* FileSystem.FileSystem; |
| 239 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-acp-local-" }); |
| 240 | const commandPath = `${cacheDir}/dsh`; |
| 241 | yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n"); |
| 242 | yield* fileSystem.chmod(commandPath, 0o755); |
| 243 | const hostEnvironment = { PATH: "/missing-host-bin", INHERITED: "host", OVERRIDE: "host" }; |
| 244 | const environment = { ...hostEnvironment, PATH: cacheDir, OVERRIDE: "provider" }; |
| 245 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 246 | cacheDir, |
| 247 | toolsDir: `${cacheDir}/tools`, |
| 248 | registryUrl, |
| 249 | }).pipe(Effect.provideService(HostProcess.Environment, hostEnvironment)); |
| 250 | const localSettings = decodeAcpRegistrySettings({ |
| 251 | source: "local", |
| 252 | commandPath: "dsh", |
| 253 | commandArgs: ["--profile", "acp", "", " spaced ", "$(touch injected); $VALUE"], |
| 254 | }); |
| 255 | |
| 256 | expect(yield* resolver.inspect(localSettings)).toMatchObject({ status: "missing_runner" }); |
| 257 | expect(yield* resolver.inspect(localSettings, environment)).toEqual({ |
| 258 | status: "ready", |
| 259 | agentId: "dsh", |
| 260 | version: null, |
| 261 | distribution: "local", |
| 262 | }); |
| 263 | const resolved = yield* resolver.resolve(localSettings, "/workspace", environment); |
| 264 | expect(resolved).toEqual({ |
| 265 | distribution: "local", |
| 266 | spawn: { |
| 267 | command: commandPath, |
| 268 | args: ["--profile", "acp", "", " spaced ", "$(touch injected); $VALUE"], |
| 269 | cwd: "/workspace", |
| 270 | env: environment, |
| 271 | shell: false, |
| 272 | }, |
| 273 | }); |
| 274 | expect(requests).toEqual([]); |
| 275 | expect(yield* fileSystem.exists(`${cacheDir}/tools`)).toBe(false); |
| 276 | }).pipe( |
| 277 | Effect.scoped, |
| 278 | Effect.provide( |
| 279 | layerResolver((request) => { |
| 280 | requests.push(request.url); |
| 281 | return Effect.die("unexpected registry request for a local provider"); |
| 282 | }), |
| 283 | ), |
| 284 | ); |
| 285 | }); |
| 286 | |
| 287 | it.effect("resolves a local executable path and inherits the host environment", () => |
| 288 | Effect.gen(function* () { |
| 289 | const fileSystem = yield* FileSystem.FileSystem; |
| 290 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-acp-local-path-" }); |
| 291 | const commandPath = `${cacheDir}/dsh wrapper`; |
| 292 | yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n"); |
| 293 | yield* fileSystem.chmod(commandPath, 0o755); |
| 294 | const environment = { PATH: "/unused", INHERITED: "host" }; |
| 295 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 296 | cacheDir, |
| 297 | toolsDir: `${cacheDir}/tools`, |
| 298 | registryUrl, |
| 299 | }).pipe(Effect.provideService(HostProcess.Environment, environment)); |
| 300 | const resolved = yield* resolver.resolve( |
| 301 | decodeAcpRegistrySettings({ source: "local", commandPath }), |
| 302 | "/workspace", |
| 303 | ); |
| 304 | |
| 305 | expect(resolved.spawn).toEqual({ |
| 306 | command: commandPath, |
| 307 | args: [], |
| 308 | cwd: "/workspace", |
| 309 | env: environment, |
| 310 | shell: false, |
| 311 | }); |
| 312 | expect(resolved.agent).toBeUndefined(); |
| 313 | |
| 314 | for (const extension of [".cmd", ".BAT"]) { |
| 315 | const windowsResolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 316 | cacheDir, |
| 317 | toolsDir: `${cacheDir}/tools`, |
| 318 | registryUrl, |
| 319 | }).pipe( |
| 320 | Effect.provideService(HostProcess.Platform, "win32"), |
| 321 | Effect.provideService(SpawnExecutableResolution, () => `C:\\bin\\dsh${extension}`), |
| 322 | ); |
| 323 | const localSettings = decodeAcpRegistrySettings({ source: "local", commandPath: "dsh" }); |
| 324 | for (const failure of [ |
| 325 | yield* windowsResolver.inspect(localSettings).pipe(Effect.flip), |
| 326 | yield* windowsResolver.resolve(localSettings, "/workspace").pipe(Effect.flip), |
| 327 | ]) { |
| 328 | expect(failure).toMatchObject({ |
| 329 | reason: "runner_unavailable", |
| 330 | detail: expect.stringContaining("node.exe"), |
| 331 | }); |
| 332 | } |
| 333 | } |
| 334 | }).pipe( |
| 335 | Effect.scoped, |
| 336 | Effect.provide(layerResolver(() => Effect.die("unexpected registry request"))), |
| 337 | ), |
| 338 | ); |
| 339 | |
| 340 | it.effect("resolves the local Command Code configurations the HTTP selector matches", () => |
| 341 | Effect.gen(function* () { |
| 342 | const fileSystem = yield* FileSystem.FileSystem; |
| 343 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 344 | prefix: "t3-acp-command-code-", |
| 345 | }); |
| 346 | const catalog = (executable: string) => |
| 347 | AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 348 | cacheDir, |
| 349 | toolsDir: `${cacheDir}/tools`, |
| 350 | registryUrl, |
| 351 | }).pipe( |
| 352 | Effect.provideService(HostProcess.Platform, "win32"), |
| 353 | Effect.provideService(SpawnExecutableResolution, () => executable), |
| 354 | ); |
| 355 | |
| 356 | const bare = decodeAcpRegistrySettings({ source: "local", commandPath: "cmdc" }); |
| 357 | expect(prefersHttpMcpTransport(bare)).toBe(true); |
| 358 | expect( |
| 359 | yield* (yield* catalog("C:\\bin\\cmdc.exe")).resolve(bare, "/workspace"), |
| 360 | ).toMatchObject({ spawn: { command: "C:\\bin\\cmdc.exe", shell: false } }); |
| 361 | // A bare name that PATHEXT resolves to a batch shim never launches, so the transport it |
| 362 | // selects cannot matter: there is no session to attach MCP servers to. |
| 363 | expect( |
| 364 | yield* (yield* catalog("C:\\bin\\cmdc.cmd")).resolve(bare, "/workspace").pipe(Effect.flip), |
| 365 | ).toMatchObject({ reason: "runner_unavailable" }); |
| 366 | |
| 367 | const vendored = decodeAcpRegistrySettings({ |
| 368 | source: "local", |
| 369 | commandPath: "node.exe", |
| 370 | commandArgs: [ |
| 371 | "C:\\Users\\me\\.t3\\bin\\cmdcode\\node_modules\\command-code\\dist\\index.mjs", |
| 372 | "acp", |
| 373 | ], |
| 374 | }); |
| 375 | expect(prefersHttpMcpTransport(vendored)).toBe(true); |
| 376 | expect( |
| 377 | yield* (yield* catalog("C:\\Program Files\\nodejs\\node.exe")).resolve( |
| 378 | vendored, |
| 379 | "/workspace", |
| 380 | ), |
| 381 | ).toMatchObject({ spawn: { command: "C:\\Program Files\\nodejs\\node.exe" } }); |
| 382 | }).pipe( |
| 383 | Effect.scoped, |
| 384 | Effect.provide(layerResolver(() => Effect.die("unexpected registry request"))), |
| 385 | ), |
| 386 | ); |
| 387 | |
| 388 | it.effect("reports missing or non-executable local commands without registry fallback", () => |
| 389 | Effect.gen(function* () { |
| 390 | const fileSystem = yield* FileSystem.FileSystem; |
| 391 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 392 | prefix: "t3-acp-local-missing-", |
| 393 | }); |
| 394 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 395 | cacheDir, |
| 396 | toolsDir: `${cacheDir}/tools`, |
| 397 | registryUrl, |
| 398 | }); |
| 399 | const environment = { PATH: cacheDir }; |
| 400 | const unconfigured = decodeAcpRegistrySettings({ source: "local" }); |
| 401 | expect(yield* resolver.inspect(unconfigured, environment)).toEqual({ |
| 402 | status: "unconfigured", |
| 403 | }); |
| 404 | expect( |
| 405 | yield* resolver.resolve(unconfigured, "/workspace", environment).pipe(Effect.flip), |
| 406 | ).toMatchObject({ reason: "agent_not_configured" }); |
| 407 | |
| 408 | const nonExecutable = `${cacheDir}/non-executable`; |
| 409 | yield* fileSystem.writeFileString(nonExecutable, "#!/bin/sh\n"); |
| 410 | yield* fileSystem.chmod(nonExecutable, 0o644); |
| 411 | for (const commandPath of ["dsh", `${cacheDir}/missing`, cacheDir, nonExecutable]) { |
| 412 | const localSettings = decodeAcpRegistrySettings({ source: "local", commandPath }); |
| 413 | expect(yield* resolver.inspect(localSettings, environment)).toEqual({ |
| 414 | status: "missing_runner", |
| 415 | version: null, |
| 416 | distribution: "local", |
| 417 | }); |
| 418 | expect( |
| 419 | yield* resolver.resolve(localSettings, "/workspace", environment).pipe(Effect.flip), |
| 420 | ).toMatchObject({ |
| 421 | reason: "runner_unavailable", |
| 422 | detail: "Local ACP executable is not available on this environment's PATH.", |
| 423 | }); |
| 424 | } |
| 425 | }).pipe( |
| 426 | Effect.scoped, |
| 427 | Effect.provide(layerResolver(() => Effect.die("unexpected registry request"))), |
| 428 | ), |
| 429 | ); |
| 430 | |
| 431 | it.effect("resolves command overrides while preserving registry args and environment", () => { |
| 432 | const agent = makeAgent({ |
| 433 | binary: { |
| 434 | "linux-x86_64": { |
| 435 | archive: archiveUrl, |
| 436 | cmd: "./bin/example-agent", |
| 437 | args: ["acp", "--stdio"], |
| 438 | env: { REGISTRY_VALUE: "registry", OVERRIDE_ME: "registry" }, |
| 439 | }, |
| 440 | }, |
| 441 | }); |
| 442 | const requests: Array<string> = []; |
| 443 | return Effect.gen(function* () { |
| 444 | const fileSystem = yield* FileSystem.FileSystem; |
| 445 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 446 | prefix: "t3-acp-registry-override-", |
| 447 | }); |
| 448 | const commandPath = `${cacheDir}/example-agent`; |
| 449 | yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n"); |
| 450 | yield* fileSystem.chmod(commandPath, 0o755); |
| 451 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 452 | cacheDir, |
| 453 | toolsDir: `${cacheDir}/tools`, |
| 454 | registryUrl, |
| 455 | }); |
| 456 | const resolved = yield* resolver.resolve(settings({ commandPath }), "/workspace", { |
| 457 | HOST_VALUE: "host", |
| 458 | OVERRIDE_ME: "host", |
| 459 | }); |
| 460 | |
| 461 | expect(resolved.distribution).toBe("binary"); |
| 462 | expect(resolved.spawn).toEqual({ |
| 463 | command: commandPath, |
| 464 | args: ["acp", "--stdio"], |
| 465 | cwd: "/workspace", |
| 466 | env: { |
| 467 | HOST_VALUE: "host", |
| 468 | OVERRIDE_ME: "registry", |
| 469 | REGISTRY_VALUE: "registry", |
| 470 | }, |
| 471 | }); |
| 472 | expect(requests).toEqual([registryUrl]); |
| 473 | }).pipe( |
| 474 | Effect.scoped, |
| 475 | Effect.provide( |
| 476 | layerResolver((request) => { |
| 477 | requests.push(request.url); |
| 478 | return Effect.succeed( |
| 479 | HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent))), |
| 480 | ); |
| 481 | }), |
| 482 | ), |
| 483 | ); |
| 484 | }); |
| 485 | |
| 486 | it.effect("installs into T3 home a package and launches its exposed command", () => { |
| 487 | const agent = makeAgent({ |
| 488 | npx: { package: "@example/acp@V1.2.3", args: ["--stdio"] }, |
| 489 | }); |
| 490 | return Effect.gen(function* () { |
| 491 | const fileSystem = yield* FileSystem.FileSystem; |
| 492 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 493 | prefix: "t3-acp-registry-global-package-", |
| 494 | }); |
| 495 | const toolchain = yield* makeFakeNpmToolchain(cacheDir); |
| 496 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 497 | cacheDir, |
| 498 | toolsDir: `${cacheDir}/tools`, |
| 499 | registryUrl, |
| 500 | }).pipe(Effect.provideService(HostProcess.Environment, toolchain.environment)); |
| 501 | |
| 502 | const first = yield* resolver.resolve(settings(), "/workspace", toolchain.environment); |
| 503 | const second = yield* resolver.resolve(settings(), "/workspace", toolchain.environment); |
| 504 | |
| 505 | expect(first.spawn).toMatchObject({ |
| 506 | command: toolchain.executablePath, |
| 507 | args: ["--stdio"], |
| 508 | env: { PATH: expect.stringMatching(new RegExp(`^${toolchain.globalBin}:`, "u")) }, |
| 509 | }); |
| 510 | expect(second.spawn.command).toBe(toolchain.executablePath); |
| 511 | const npmCommands = yield* fileSystem.readFileString(toolchain.logPath); |
| 512 | expect(npmCommands.match(/^install --global /gmu)).toHaveLength(1); |
| 513 | expect(npmCommands).toContain("install --global @example/acp@V1.2.3"); |
| 514 | |
| 515 | // A missing npm bin link must not select an unrelated command from the user's PATH. |
| 516 | const userBin = `${cacheDir}/user-bin`; |
| 517 | yield* fileSystem.makeDirectory(userBin); |
| 518 | yield* fileSystem.writeFileString(`${userBin}/example-agent`, "#!/bin/sh\n"); |
| 519 | yield* fileSystem.chmod(`${userBin}/example-agent`, 0o755); |
| 520 | yield* fileSystem.remove(toolchain.executablePath); |
| 521 | const userEnvironment = { |
| 522 | ...toolchain.environment, |
| 523 | PATH: `${userBin}:${toolchain.environment.PATH}`, |
| 524 | }; |
| 525 | const repaired = yield* resolver.resolve(settings(), "/workspace", userEnvironment); |
| 526 | expect(repaired.spawn.command).toBe(toolchain.executablePath); |
| 527 | const repairCommands = yield* fileSystem.readFileString(toolchain.logPath); |
| 528 | expect(repairCommands.match(/^install --global /gmu)).toHaveLength(2); |
| 529 | |
| 530 | yield* fileSystem.remove(toolchain.executablePath); |
| 531 | const failedRepair = yield* resolver |
| 532 | .resolve(settings(), "/workspace", { ...userEnvironment, npm_config_bin_links: "false" }) |
| 533 | .pipe(Effect.flip); |
| 534 | expect(failedRepair.reason).toBe("install_failed"); |
| 535 | }).pipe( |
| 536 | Effect.scoped, |
| 537 | Effect.provide( |
| 538 | layerResolver((request) => |
| 539 | Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))), |
| 540 | ), |
| 541 | ), |
| 542 | ); |
| 543 | }); |
| 544 | |
| 545 | it.effect("uses its managed prefix despite a system-owned npm prefix", () => { |
| 546 | const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } }); |
| 547 | return Effect.gen(function* () { |
| 548 | const fileSystem = yield* FileSystem.FileSystem; |
| 549 | const path = yield* Path.Path; |
| 550 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 551 | prefix: "t3-acp-registry-user-global-package-", |
| 552 | }); |
| 553 | const toolchain = yield* makeFakeNpmToolchain(cacheDir); |
| 554 | const systemPrefix = path.join(cacheDir, "system-global"); |
| 555 | yield* fileSystem.makeDirectory(systemPrefix); |
| 556 | yield* fileSystem.chmod(systemPrefix, 0o555); |
| 557 | const userHome = path.join(cacheDir, "home"); |
| 558 | const environment = { |
| 559 | ...toolchain.environment, |
| 560 | HOME: userHome, |
| 561 | FAKE_NPM_PREFIX: systemPrefix, |
| 562 | }; |
| 563 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 564 | cacheDir, |
| 565 | toolsDir: `${cacheDir}/tools`, |
| 566 | registryUrl, |
| 567 | }).pipe(Effect.provideService(HostProcess.Environment, environment)); |
| 568 | |
| 569 | const resolved = yield* resolver.resolve(settings(), "/workspace", environment); |
| 570 | const userGlobalBin = toolchain.globalBin; |
| 571 | expect(resolved.spawn).toMatchObject({ |
| 572 | command: path.join(userGlobalBin, "example-agent"), |
| 573 | env: { PATH: expect.stringMatching(new RegExp(`^${userGlobalBin}:`, "u")) }, |
| 574 | }); |
| 575 | }).pipe( |
| 576 | Effect.scoped, |
| 577 | Effect.provide( |
| 578 | layerResolver((request) => |
| 579 | Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))), |
| 580 | ), |
| 581 | ), |
| 582 | ); |
| 583 | }); |
| 584 | |
| 585 | it.effect("gives Windows npm commands only the managed prefix spelling", () => { |
| 586 | const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } }); |
| 587 | return Effect.gen(function* () { |
| 588 | const fileSystem = yield* FileSystem.FileSystem; |
| 589 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 590 | prefix: "t3-acp-registry-windows-npm-prefix-", |
| 591 | }); |
| 592 | const toolchain = yield* makeFakeNpmToolchain(cacheDir); |
| 593 | const environment = { ...toolchain.environment, NPM_CONFIG_PREFIX: "C:\\inherited-npm" }; |
| 594 | const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 595 | const npmEnvironments: Array<Record<string, unknown>> = []; |
| 596 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 597 | cacheDir, |
| 598 | toolsDir: `${cacheDir}/tools`, |
| 599 | registryUrl, |
| 600 | }).pipe( |
| 601 | Effect.provideService(HostProcess.Environment, environment), |
| 602 | Effect.provideService(HostProcess.Platform, "win32"), |
| 603 | Effect.provideService(SpawnExecutableResolution, (command) => |
| 604 | command === "npm" ? toolchain.npmPath : undefined, |
| 605 | ), |
| 606 | Effect.provideService( |
| 607 | ChildProcessSpawner.ChildProcessSpawner, |
| 608 | ChildProcessSpawner.make((command) => { |
| 609 | if (command._tag === "StandardCommand" && command.command === toolchain.npmPath) { |
| 610 | npmEnvironments.push({ ...command.options.env }); |
| 611 | } |
| 612 | return realSpawner.spawn(command); |
| 613 | }), |
| 614 | ), |
| 615 | ); |
| 616 | |
| 617 | yield* resolver.resolve(settings(), "C:\\workspace", environment).pipe(Effect.ignore); |
| 618 | |
| 619 | expect(npmEnvironments.length).toBeGreaterThan(0); |
| 620 | for (const env of npmEnvironments) { |
| 621 | expect(Object.keys(env).filter((key) => key.toLowerCase() === "npm_config_prefix")).toEqual( |
| 622 | ["npm_config_prefix"], |
| 623 | ); |
| 624 | } |
| 625 | }).pipe( |
| 626 | Effect.scoped, |
| 627 | Effect.provide( |
| 628 | layerResolver((request) => |
| 629 | Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))), |
| 630 | ), |
| 631 | ), |
| 632 | ); |
| 633 | }); |
| 634 | |
| 635 | it.effect("keeps only the end of a failed install's output in its error", () => { |
| 636 | const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } }); |
| 637 | return Effect.gen(function* () { |
| 638 | const fileSystem = yield* FileSystem.FileSystem; |
| 639 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 640 | prefix: "t3-acp-registry-failed-install-", |
| 641 | }); |
| 642 | const toolchain = yield* makeFakeNpmToolchain(cacheDir); |
| 643 | const environment = { ...toolchain.environment, FAKE_NPM_INSTALL_FAILURE_LINES: "40000" }; |
| 644 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 645 | cacheDir, |
| 646 | toolsDir: `${cacheDir}/tools`, |
| 647 | registryUrl, |
| 648 | }).pipe(Effect.provideService(HostProcess.Environment, environment)); |
| 649 | |
| 650 | const error = yield* resolver |
| 651 | .resolve(settings(), "/workspace", environment) |
| 652 | .pipe(Effect.flip); |
| 653 | |
| 654 | expect(error.reason).toBe("install_failed"); |
| 655 | expect(error.detail).toContain("exited with code 1"); |
| 656 | expect(error.detail).toMatch(/npm error see the complete log for details$/u); |
| 657 | expect(error.detail.split("exited with code 1: ")[1]?.length).toBeLessThanOrEqual(1_000); |
| 658 | }).pipe( |
| 659 | Effect.scoped, |
| 660 | Effect.provide( |
| 661 | layerResolver((request) => |
| 662 | Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))), |
| 663 | ), |
| 664 | ), |
| 665 | ); |
| 666 | }); |
| 667 | |
| 668 | it.effect("launches the managed uv command over a PATH command named after the agent", () => { |
| 669 | const agent = makeAgent({ |
| 670 | uvx: { package: "fast-agent-acp==0.10.1", args: ["--acp"] }, |
| 671 | }); |
| 672 | return Effect.gen(function* () { |
| 673 | const fileSystem = yield* FileSystem.FileSystem; |
| 674 | const path = yield* Path.Path; |
| 675 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 676 | prefix: "t3-acp-registry-uv-path-collision-", |
| 677 | }); |
| 678 | const toolchain = yield* makeFakeUvToolchain(cacheDir); |
| 679 | const userBin = path.join(cacheDir, "user-bin"); |
| 680 | const userCommand = path.join(userBin, "example-agent"); |
| 681 | yield* fileSystem.makeDirectory(userBin, { recursive: true }); |
| 682 | yield* fileSystem.writeFileString(userCommand, "#!/bin/sh\n"); |
| 683 | yield* fileSystem.chmod(userCommand, 0o755); |
| 684 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 685 | cacheDir, |
| 686 | toolsDir: `${cacheDir}/tools`, |
| 687 | registryUrl, |
| 688 | }); |
| 689 | |
| 690 | const resolved = yield* resolver.resolve(settings(), "/workspace", { |
| 691 | ...toolchain.environment, |
| 692 | PATH: `${userBin}:${toolchain.environment.PATH}`, |
| 693 | }); |
| 694 | |
| 695 | expect(resolved.spawn.command).toBe(toolchain.executablePath); |
| 696 | }).pipe( |
| 697 | Effect.scoped, |
| 698 | Effect.provide( |
| 699 | layerResolver((request) => |
| 700 | Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))), |
| 701 | ), |
| 702 | ), |
| 703 | ); |
| 704 | }); |
| 705 | |
| 706 | it.effect("honors an exact Windows PATH override when launching a global package", () => { |
| 707 | const agent = makeAgent({ |
| 708 | uvx: { package: "fast-agent-acp==0.10.1", args: ["--acp"] }, |
| 709 | }); |
| 710 | return Effect.gen(function* () { |
| 711 | const fileSystem = yield* FileSystem.FileSystem; |
| 712 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 713 | prefix: "t3-acp-registry-windows-package-path-", |
| 714 | }); |
| 715 | const toolchain = yield* makeFakeUvToolchain(cacheDir); |
| 716 | const linuxResolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 717 | cacheDir, |
| 718 | toolsDir: `${cacheDir}/tools`, |
| 719 | registryUrl, |
| 720 | }); |
| 721 | yield* linuxResolver.resolve(settings(), "/workspace", toolchain.environment); |
| 722 | |
| 723 | const windowsEnvironment = { |
| 724 | ...toolchain.environment, |
| 725 | Path: "C:\\host\\bin", |
| 726 | PATH: "C:\\provider\\bin", |
| 727 | }; |
| 728 | const windowsResolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 729 | cacheDir, |
| 730 | toolsDir: `${cacheDir}/tools`, |
| 731 | registryUrl, |
| 732 | }).pipe( |
| 733 | Effect.provideService(HostProcess.Platform, "win32"), |
| 734 | Effect.provideService(SpawnExecutableResolution, (command) => { |
| 735 | if (command === "uv") return toolchain.uvPath; |
| 736 | if (command === "fast-agent") return toolchain.executablePath; |
| 737 | return undefined; |
| 738 | }), |
| 739 | ); |
| 740 | const resolved = yield* windowsResolver.resolve( |
| 741 | settings(), |
| 742 | "C:\\workspace", |
| 743 | windowsEnvironment, |
| 744 | ); |
| 745 | |
| 746 | expect(resolved.spawn).toMatchObject({ |
| 747 | env: { PATH: `${toolchain.globalBin};C:\\provider\\bin` }, |
| 748 | }); |
| 749 | }).pipe( |
| 750 | Effect.scoped, |
| 751 | Effect.provide( |
| 752 | layerResolver((request) => |
| 753 | Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))), |
| 754 | ), |
| 755 | ), |
| 756 | ); |
| 757 | }); |
| 758 | |
| 759 | it.effect("installs and reuses a registry binary in the managed cache", () => { |
| 760 | const binaryBytes = new TextEncoder().encode("#!/bin/sh\necho example\n"); |
| 761 | const agent = makeAgent({ |
| 762 | binary: { |
| 763 | "linux-x86_64": { |
| 764 | archive: archiveUrl, |
| 765 | cmd: "./bin/example-agent", |
| 766 | args: ["acp"], |
| 767 | sha256: "97b29a636d7ddf7bf3567ae4d48c0f2a9b03943fd808b6efbf74f1dd3db131b7", |
| 768 | }, |
| 769 | }, |
| 770 | }); |
| 771 | const requests: Array<string> = []; |
| 772 | return Effect.gen(function* () { |
| 773 | const fileSystem = yield* FileSystem.FileSystem; |
| 774 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 775 | prefix: "t3-acp-registry-install-", |
| 776 | }); |
| 777 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 778 | cacheDir, |
| 779 | toolsDir: `${cacheDir}/tools`, |
| 780 | registryUrl, |
| 781 | }); |
| 782 | const first = yield* resolver.resolve(settings(), "/workspace"); |
| 783 | const second = yield* resolver.resolve(settings(), "/workspace"); |
| 784 | |
| 785 | expect(first.spawn.command).toBe(second.spawn.command); |
| 786 | expect(first.spawn.command).toContain( |
| 787 | "/tools/example-agent/1.2.3/linux-x86_64/bin/example-agent", |
| 788 | ); |
| 789 | expect(yield* fileSystem.readFileString(first.spawn.command)).toBe( |
| 790 | "#!/bin/sh\necho example\n", |
| 791 | ); |
| 792 | expect(requests).toEqual([registryUrl, archiveUrl]); |
| 793 | }).pipe( |
| 794 | Effect.scoped, |
| 795 | Effect.provide( |
| 796 | layerResolver((request) => { |
| 797 | requests.push(request.url); |
| 798 | const response = |
| 799 | request.url === registryUrl |
| 800 | ? new Response(makeRegistry(agent)) |
| 801 | : new Response( |
| 802 | new ReadableStream<Uint8Array>({ |
| 803 | start(controller) { |
| 804 | controller.enqueue(binaryBytes.slice(0, 8)); |
| 805 | controller.enqueue(binaryBytes.slice(8)); |
| 806 | controller.close(); |
| 807 | }, |
| 808 | }), |
| 809 | ); |
| 810 | return Effect.succeed(HttpClientResponse.fromWeb(request, response)); |
| 811 | }), |
| 812 | ), |
| 813 | ); |
| 814 | }); |
| 815 | |
| 816 | it.effect("gives Antigravity a T3-owned temp directory that is removed with the process", () => { |
| 817 | const agent = { |
| 818 | ...makeAgent({ binary: { "linux-x86_64": { archive: archiveUrl, cmd: "./agy" } } }), |
| 819 | id: "antigravity-acp", |
| 820 | }; |
| 821 | return Effect.gen(function* () { |
| 822 | const fileSystem = yield* FileSystem.FileSystem; |
| 823 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 824 | prefix: "t3-acp-registry-runtime-temp-", |
| 825 | }); |
| 826 | const runtimeTempDir = `${cacheDir}/antigravity-tmp/acp-registry`; |
| 827 | yield* fileSystem.makeDirectory(`${runtimeTempDir}/run-killed/_MEI123`, { recursive: true }); |
| 828 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 829 | cacheDir, |
| 830 | toolsDir: `${cacheDir}/tools`, |
| 831 | runtimeTempDir, |
| 832 | registryUrl, |
| 833 | }); |
| 834 | expect(yield* fileSystem.exists(`${runtimeTempDir}/run-killed`)).toBe(false); |
| 835 | |
| 836 | const temp = yield* Effect.scoped( |
| 837 | Effect.gen(function* () { |
| 838 | const resolved = yield* resolver.resolve( |
| 839 | settings({ agentId: "antigravity-acp" }), |
| 840 | "/workspace", |
| 841 | { TMPDIR: "/tmp" }, |
| 842 | ); |
| 843 | const directory = resolved.spawn.env?.TMPDIR ?? ""; |
| 844 | expect(directory.startsWith(`${runtimeTempDir}/run-`)).toBe(true); |
| 845 | expect(yield* fileSystem.exists(directory)).toBe(true); |
| 846 | return directory; |
| 847 | }), |
| 848 | ); |
| 849 | expect(yield* fileSystem.exists(temp)).toBe(false); |
| 850 | }).pipe( |
| 851 | Effect.scoped, |
| 852 | Effect.provide( |
| 853 | layerResolver((request) => |
| 854 | Effect.succeed( |
| 855 | HttpClientResponse.fromWeb( |
| 856 | request, |
| 857 | new Response(request.url === registryUrl ? makeRegistry(agent) : "#!/bin/sh\n"), |
| 858 | ), |
| 859 | ), |
| 860 | ), |
| 861 | ), |
| 862 | ); |
| 863 | }); |
| 864 | |
| 865 | it.effect("installs a contained binary whose name starts with two dots", () => { |
| 866 | const agent = makeAgent({ |
| 867 | binary: { "linux-x86_64": { archive: archiveUrl, cmd: "..example-agent" } }, |
| 868 | }); |
| 869 | return Effect.gen(function* () { |
| 870 | const fileSystem = yield* FileSystem.FileSystem; |
| 871 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 872 | prefix: "t3-acp-registry-dot-name-", |
| 873 | }); |
| 874 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 875 | cacheDir, |
| 876 | toolsDir: `${cacheDir}/tools`, |
| 877 | registryUrl, |
| 878 | }); |
| 879 | const resolved = yield* resolver.resolve(settings(), "/workspace"); |
| 880 | |
| 881 | expect(resolved.spawn.command).toContain( |
| 882 | "/tools/example-agent/1.2.3/linux-x86_64/..example-agent", |
| 883 | ); |
| 884 | }).pipe( |
| 885 | Effect.scoped, |
| 886 | Effect.provide( |
| 887 | layerResolver((request) => |
| 888 | Effect.succeed( |
| 889 | HttpClientResponse.fromWeb( |
| 890 | request, |
| 891 | new Response(request.url === registryUrl ? makeRegistry(agent) : "#!/bin/sh\n"), |
| 892 | ), |
| 893 | ), |
| 894 | ), |
| 895 | ), |
| 896 | ); |
| 897 | }); |
| 898 | |
| 899 | it.effect("keeps a prepared binary ready after the registry advances its version", () => { |
| 900 | const makeBinaryAgent = (version: string) => ({ |
| 901 | ...makeAgent({ |
| 902 | binary: { |
| 903 | "linux-x86_64": { |
| 904 | archive: `https://registry.test/example-agent-${version}.bin`, |
| 905 | cmd: "./bin/example-agent", |
| 906 | }, |
| 907 | }, |
| 908 | }), |
| 909 | version, |
| 910 | }); |
| 911 | let agent = makeBinaryAgent("1.2.3"); |
| 912 | return Effect.gen(function* () { |
| 913 | const fileSystem = yield* FileSystem.FileSystem; |
| 914 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 915 | prefix: "t3-acp-registry-advance-", |
| 916 | }); |
| 917 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 918 | cacheDir, |
| 919 | toolsDir: `${cacheDir}/tools`, |
| 920 | registryUrl, |
| 921 | }); |
| 922 | yield* resolver.prepare({ agentId: agent.id }); |
| 923 | |
| 924 | agent = makeBinaryAgent("1.2.4"); |
| 925 | yield* TestClock.adjust("6 minutes"); |
| 926 | yield* resolver.search({ query: "example" }); |
| 927 | |
| 928 | expect(yield* resolver.inspect(settings())).toMatchObject({ |
| 929 | status: "ready", |
| 930 | distribution: "binary", |
| 931 | }); |
| 932 | const resolved = yield* resolver.resolve(settings(), "/workspace", {}); |
| 933 | expect(resolved.spawn.command).toContain( |
| 934 | "/tools/example-agent/1.2.4/linux-x86_64/bin/example-agent", |
| 935 | ); |
| 936 | expect(yield* fileSystem.readFileString(resolved.spawn.command)).toBe( |
| 937 | "#!/bin/sh\necho 1.2.4\n", |
| 938 | ); |
| 939 | |
| 940 | expect(yield* resolver.uninstallManagedBinary({ agentId: agent.id })).toMatchObject({ |
| 941 | removed: true, |
| 942 | }); |
| 943 | expect(yield* resolver.inspect(settings())).toMatchObject({ status: "unprepared" }); |
| 944 | }).pipe( |
| 945 | Effect.scoped, |
| 946 | Effect.provide( |
| 947 | layerResolver((request) => |
| 948 | Effect.succeed( |
| 949 | HttpClientResponse.fromWeb( |
| 950 | request, |
| 951 | new Response( |
| 952 | request.url === registryUrl |
| 953 | ? makeRegistry(agent) |
| 954 | : `#!/bin/sh\necho ${agent.version}\n`, |
| 955 | ), |
| 956 | ), |
| 957 | ), |
| 958 | ), |
| 959 | ), |
| 960 | ); |
| 961 | }); |
| 962 | |
| 963 | it.effect( |
| 964 | "prepares the registry version despite an older PATH binary and permits explicit overrides", |
| 965 | () => { |
| 966 | const binaryBytes = new TextEncoder().encode("#!/bin/sh\necho managed\n"); |
| 967 | const agent = makeAgent({ |
| 968 | binary: { |
| 969 | "linux-x86_64": { |
| 970 | archive: archiveUrl, |
| 971 | cmd: "./bin/example-agent", |
| 972 | args: ["acp"], |
| 973 | sha256: "e1eb7c905b91c400af2ec858628aec22866bfda1383b93ceb73ae01d9769fb31", |
| 974 | }, |
| 975 | }, |
| 976 | }); |
| 977 | const requests: Array<string> = []; |
| 978 | return Effect.gen(function* () { |
| 979 | const fileSystem = yield* FileSystem.FileSystem; |
| 980 | const path = yield* Path.Path; |
| 981 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 982 | prefix: "t3-acp-registry-system-", |
| 983 | }); |
| 984 | const systemBinDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 985 | prefix: "t3-acp-registry-system-bin-", |
| 986 | }); |
| 987 | const systemBinary = path.join(systemBinDir, "example-agent"); |
| 988 | yield* fileSystem.writeFileString(systemBinary, "#!/bin/sh\necho system\n"); |
| 989 | yield* fileSystem.chmod(systemBinary, 0o755); |
| 990 | const environment = { PATH: systemBinDir }; |
| 991 | |
| 992 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 993 | cacheDir, |
| 994 | toolsDir: `${cacheDir}/tools`, |
| 995 | registryUrl, |
| 996 | }).pipe(Effect.provideService(HostProcess.Environment, environment)); |
| 997 | yield* resolver.search({ query: "example" }); |
| 998 | expect(yield* resolver.inspect(settings(), environment)).toMatchObject({ |
| 999 | status: "unprepared", |
| 1000 | }); |
| 1001 | |
| 1002 | const overridden = settings({ commandPath: systemBinary }); |
| 1003 | expect(yield* resolver.inspect(overridden, environment)).toMatchObject({ |
| 1004 | status: "ready", |
| 1005 | version: null, |
| 1006 | }); |
| 1007 | const custom = yield* resolver.resolve(overridden, "/workspace", environment); |
| 1008 | expect(custom.spawn.command).toBe(systemBinary); |
| 1009 | expect(custom.spawn.args).toEqual(["acp"]); |
| 1010 | expect(requests).toEqual([registryUrl]); |
| 1011 | |
| 1012 | yield* resolver.prepare({ agentId: "example-agent" }); |
| 1013 | const resolved = yield* resolver.resolve(settings(), "/workspace", environment); |
| 1014 | expect(resolved.spawn.command).not.toBe(systemBinary); |
| 1015 | expect(yield* fileSystem.readFileString(resolved.spawn.command)).toBe( |
| 1016 | "#!/bin/sh\necho managed\n", |
| 1017 | ); |
| 1018 | expect(yield* fileSystem.readFileString(systemBinary)).toBe("#!/bin/sh\necho system\n"); |
| 1019 | |
| 1020 | const inspection = yield* resolver.inspect(settings(), environment); |
| 1021 | expect(inspection).toMatchObject({ |
| 1022 | status: "ready", |
| 1023 | distribution: "binary", |
| 1024 | version: "1.2.3", |
| 1025 | }); |
| 1026 | |
| 1027 | expect(requests).toEqual([registryUrl, registryUrl, archiveUrl]); |
| 1028 | }).pipe( |
| 1029 | Effect.scoped, |
| 1030 | Effect.provide( |
| 1031 | layerResolver((request) => { |
| 1032 | requests.push(request.url); |
| 1033 | return Effect.succeed( |
| 1034 | HttpClientResponse.fromWeb( |
| 1035 | request, |
| 1036 | request.url === registryUrl |
| 1037 | ? new Response(makeRegistry(agent)) |
| 1038 | : new Response(binaryBytes.buffer as ArrayBuffer), |
| 1039 | ), |
| 1040 | ); |
| 1041 | }), |
| 1042 | ), |
| 1043 | ); |
| 1044 | }, |
| 1045 | ); |
| 1046 | |
| 1047 | it.effect("installs a tar archive with a root entry and a listing larger than 1 MiB", () => { |
| 1048 | const downloadUrl = "https://registry.test/agent.tar.gz"; |
| 1049 | const agent = makeAgent({ |
| 1050 | binary: { "linux-x86_64": { archive: downloadUrl, cmd: "./agent" } }, |
| 1051 | }); |
| 1052 | return Effect.gen(function* () { |
| 1053 | const fileSystem = yield* FileSystem.FileSystem; |
| 1054 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-acp-root-entry-" }); |
| 1055 | const source = `${cacheDir}/source`; |
| 1056 | yield* fileSystem.makeDirectory(source); |
| 1057 | yield* fileSystem.writeFileString(`${source}/agent`, "#!/bin/sh\necho managed\n"); |
| 1058 | // 5,000 long names list past the generic 1 MiB command output cap. |
| 1059 | const entries = `${source}/entries`; |
| 1060 | yield* fileSystem.makeDirectory(entries); |
| 1061 | yield* Effect.forEach( |
| 1062 | Array.from({ length: 5_000 }, (_, index) => index), |
| 1063 | (index) => fileSystem.writeFileString(`${entries}/${index}-${"e".repeat(240)}`, ""), |
| 1064 | { concurrency: 16 }, |
| 1065 | ); |
| 1066 | const archivePath = `${cacheDir}/agent.tar.gz`; |
| 1067 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 1068 | const tar = yield* spawner.spawn( |
| 1069 | ChildProcess.make("tar", ["-czf", archivePath, "-C", source, "."]), |
| 1070 | ); |
| 1071 | expect(yield* tar.exitCode).toBe(0); |
| 1072 | const archive = yield* fileSystem.readFile(archivePath); |
| 1073 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1074 | cacheDir, |
| 1075 | toolsDir: `${cacheDir}/tools`, |
| 1076 | registryUrl, |
| 1077 | }).pipe( |
| 1078 | Effect.provideService( |
| 1079 | HttpClient.HttpClient, |
| 1080 | HttpClient.make((request) => |
| 1081 | Effect.succeed( |
| 1082 | HttpClientResponse.fromWeb( |
| 1083 | request, |
| 1084 | request.url === registryUrl |
| 1085 | ? new Response(makeRegistry(agent)) |
| 1086 | : new Response(archive), |
| 1087 | ), |
| 1088 | ), |
| 1089 | ), |
| 1090 | ), |
| 1091 | ); |
| 1092 | yield* resolver.prepare({ agentId: agent.id }); |
| 1093 | const resolved = yield* resolver.resolve(settings(), "/workspace", {}); |
| 1094 | expect(resolved.spawn.command).toBe( |
| 1095 | yield* fileSystem.realPath(`${cacheDir}/tools/example-agent/1.2.3/linux-x86_64/agent`), |
| 1096 | ); |
| 1097 | expect(yield* fileSystem.readFileString(resolved.spawn.command)).toBe( |
| 1098 | "#!/bin/sh\necho managed\n", |
| 1099 | ); |
| 1100 | }).pipe( |
| 1101 | Effect.scoped, |
| 1102 | Effect.provide( |
| 1103 | Layer.mergeAll( |
| 1104 | NodeServices.layer, |
| 1105 | NodeCrypto.layer, |
| 1106 | TestProviderHost.layer().pipe( |
| 1107 | Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 1108 | ), |
| 1109 | ), |
| 1110 | ), |
| 1111 | Effect.provideService(HostProcess.Platform, "linux"), |
| 1112 | Effect.provideService(HostProcess.Architecture, "x64"), |
| 1113 | ); |
| 1114 | }); |
| 1115 | |
| 1116 | it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
| 1117 | "leaves the mode of a file outside the install root when the command links to it", |
| 1118 | () => { |
| 1119 | const downloadUrl = "https://registry.test/agent.tar.gz"; |
| 1120 | const agent = makeAgent({ |
| 1121 | binary: { "linux-x86_64": { archive: downloadUrl, cmd: "./agent" } }, |
| 1122 | }); |
| 1123 | return Effect.gen(function* () { |
| 1124 | const fileSystem = yield* FileSystem.FileSystem; |
| 1125 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1126 | prefix: "t3-acp-outside-link-", |
| 1127 | }); |
| 1128 | const outsideFile = `${cacheDir}/outside`; |
| 1129 | yield* fileSystem.writeFileString(outsideFile, "private\n"); |
| 1130 | yield* fileSystem.chmod(outsideFile, 0o600); |
| 1131 | const source = `${cacheDir}/source`; |
| 1132 | yield* fileSystem.makeDirectory(source); |
| 1133 | yield* fileSystem.symlink(outsideFile, `${source}/agent`); |
| 1134 | const archivePath = `${cacheDir}/agent.tar.gz`; |
| 1135 | const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 1136 | const tar = yield* spawner.spawn( |
| 1137 | ChildProcess.make("tar", ["-czf", archivePath, "-C", source, "agent"]), |
| 1138 | ); |
| 1139 | expect(yield* tar.exitCode).toBe(0); |
| 1140 | const archive = yield* fileSystem.readFile(archivePath); |
| 1141 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1142 | cacheDir, |
| 1143 | toolsDir: `${cacheDir}/tools`, |
| 1144 | registryUrl, |
| 1145 | }).pipe( |
| 1146 | Effect.provideService( |
| 1147 | HttpClient.HttpClient, |
| 1148 | HttpClient.make((request) => |
| 1149 | Effect.succeed( |
| 1150 | HttpClientResponse.fromWeb( |
| 1151 | request, |
| 1152 | request.url === registryUrl |
| 1153 | ? new Response(makeRegistry(agent)) |
| 1154 | : new Response(archive), |
| 1155 | ), |
| 1156 | ), |
| 1157 | ), |
| 1158 | ), |
| 1159 | ); |
| 1160 | |
| 1161 | const error = yield* resolver.prepare({ agentId: agent.id }).pipe(Effect.flip); |
| 1162 | |
| 1163 | expect(error.reason).toBe("archive_invalid"); |
| 1164 | expect((yield* fileSystem.stat(outsideFile)).mode & 0o777).toBe(0o600); |
| 1165 | }).pipe( |
| 1166 | Effect.scoped, |
| 1167 | Effect.provide( |
| 1168 | Layer.mergeAll( |
| 1169 | NodeServices.layer, |
| 1170 | NodeCrypto.layer, |
| 1171 | TestProviderHost.layer().pipe( |
| 1172 | Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 1173 | ), |
| 1174 | ), |
| 1175 | ), |
| 1176 | Effect.provideService(HostProcess.Platform, "linux"), |
| 1177 | Effect.provideService(HostProcess.Architecture, "x64"), |
| 1178 | ); |
| 1179 | }, |
| 1180 | ); |
| 1181 | |
| 1182 | it.effect("searches compatible agents with deterministic ranking and bounded metadata", () => { |
| 1183 | const exact = { |
| 1184 | ...makeAgent({ npx: { package: "@example/acp@1.2.3" } }), |
| 1185 | id: "codex-acp", |
| 1186 | name: "Codex", |
| 1187 | authors: ["OpenAI", "Zed Industries"], |
| 1188 | license: "Apache-2.0", |
| 1189 | website: "https://example.test/codex", |
| 1190 | repository: "https://example.test/codex/source", |
| 1191 | icon: "https://example.test/codex.svg", |
| 1192 | } satisfies AcpRegistrySupport.AcpRegistryAgent; |
| 1193 | const descriptionMatch = { |
| 1194 | ...makeAgent({ npx: { package: "other-agent@1.2.3" } }), |
| 1195 | id: "other-agent", |
| 1196 | name: "Other Agent", |
| 1197 | description: "An adapter for Codex workflows", |
| 1198 | } satisfies AcpRegistrySupport.AcpRegistryAgent; |
| 1199 | const incompatible = { |
| 1200 | ...makeAgent({ |
| 1201 | binary: { |
| 1202 | "darwin-aarch64": { archive: archiveUrl, cmd: "agent" }, |
| 1203 | }, |
| 1204 | }), |
| 1205 | id: "darwin-only", |
| 1206 | name: "Codex Darwin", |
| 1207 | } satisfies AcpRegistrySupport.AcpRegistryAgent; |
| 1208 | |
| 1209 | return Effect.gen(function* () { |
| 1210 | const fileSystem = yield* FileSystem.FileSystem; |
| 1211 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1212 | prefix: "t3-acp-registry-search-", |
| 1213 | }); |
| 1214 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1215 | cacheDir, |
| 1216 | toolsDir: `${cacheDir}/tools`, |
| 1217 | registryUrl, |
| 1218 | }); |
| 1219 | const result = yield* resolver.search({ query: "codex" }); |
| 1220 | |
| 1221 | expect(result.agents.map((agent) => agent.id)).toEqual(["codex-acp", "other-agent"]); |
| 1222 | expect(result.agents[0]).toMatchObject({ |
| 1223 | authors: ["OpenAI", "Zed Industries"], |
| 1224 | distribution: "npx", |
| 1225 | integrity: "registry", |
| 1226 | license: "Apache-2.0", |
| 1227 | }); |
| 1228 | }).pipe( |
| 1229 | Effect.scoped, |
| 1230 | Effect.provide( |
| 1231 | layerResolver((request) => |
| 1232 | Effect.succeed( |
| 1233 | HttpClientResponse.fromWeb( |
| 1234 | request, |
| 1235 | new Response( |
| 1236 | JSON.stringify({ |
| 1237 | version: "1.0.0", |
| 1238 | agents: [descriptionMatch, incompatible, exact], |
| 1239 | }), |
| 1240 | ), |
| 1241 | ), |
| 1242 | ), |
| 1243 | ), |
| 1244 | ), |
| 1245 | ); |
| 1246 | }); |
| 1247 | |
| 1248 | it.effect("searches reuse an index fetched in the last five minutes", () => { |
| 1249 | const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } }); |
| 1250 | let requests = 0; |
| 1251 | return Effect.gen(function* () { |
| 1252 | const fileSystem = yield* FileSystem.FileSystem; |
| 1253 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1254 | prefix: "t3-acp-registry-refresh-", |
| 1255 | }); |
| 1256 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1257 | cacheDir, |
| 1258 | toolsDir: `${cacheDir}/tools`, |
| 1259 | registryUrl, |
| 1260 | }); |
| 1261 | yield* Effect.all( |
| 1262 | [resolver.search({ query: "example" }), resolver.search({ query: "example" })], |
| 1263 | { concurrency: "unbounded" }, |
| 1264 | ); |
| 1265 | expect(requests).toBe(1); |
| 1266 | |
| 1267 | yield* TestClock.adjust("4 minutes"); |
| 1268 | yield* resolver.search({ query: "other" }); |
| 1269 | expect(requests).toBe(1); |
| 1270 | |
| 1271 | yield* TestClock.adjust("1 minute"); |
| 1272 | yield* resolver.search({ query: "example" }); |
| 1273 | expect(requests).toBe(2); |
| 1274 | }).pipe( |
| 1275 | Effect.scoped, |
| 1276 | Effect.provide( |
| 1277 | layerResolver((request) => { |
| 1278 | requests += 1; |
| 1279 | return Effect.yieldNow.pipe( |
| 1280 | Effect.as(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))), |
| 1281 | ); |
| 1282 | }), |
| 1283 | ), |
| 1284 | ); |
| 1285 | }); |
| 1286 | |
| 1287 | it.effect("searches retry the network after falling back to the cached index", () => { |
| 1288 | const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } }); |
| 1289 | let requests = 0; |
| 1290 | return Effect.gen(function* () { |
| 1291 | const fileSystem = yield* FileSystem.FileSystem; |
| 1292 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1293 | prefix: "t3-acp-registry-retry-", |
| 1294 | }); |
| 1295 | yield* fileSystem.makeDirectory(`${cacheDir}/acp-registry`, { recursive: true }); |
| 1296 | yield* fileSystem.writeFileString( |
| 1297 | `${cacheDir}/acp-registry/registry.json`, |
| 1298 | makeRegistry(agent), |
| 1299 | ); |
| 1300 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1301 | cacheDir, |
| 1302 | toolsDir: `${cacheDir}/tools`, |
| 1303 | registryUrl, |
| 1304 | }); |
| 1305 | const offline = yield* resolver.search({ query: "example" }); |
| 1306 | expect(offline.agents.map((result) => result.id)).toEqual([agent.id]); |
| 1307 | expect(requests).toBe(1); |
| 1308 | |
| 1309 | yield* resolver.search({ query: "example" }); |
| 1310 | expect(requests).toBe(2); |
| 1311 | yield* resolver.search({ query: "example" }); |
| 1312 | expect(requests).toBe(2); |
| 1313 | }).pipe( |
| 1314 | Effect.scoped, |
| 1315 | Effect.provide( |
| 1316 | layerResolver((request) => { |
| 1317 | requests += 1; |
| 1318 | return Effect.succeed( |
| 1319 | HttpClientResponse.fromWeb( |
| 1320 | request, |
| 1321 | requests === 1 |
| 1322 | ? new Response("unavailable", { status: 503 }) |
| 1323 | : new Response(makeRegistry(agent)), |
| 1324 | ), |
| 1325 | ); |
| 1326 | }), |
| 1327 | ), |
| 1328 | ); |
| 1329 | }); |
| 1330 | |
| 1331 | it.effect("searches retry the network after a refresh falls back within the reuse window", () => { |
| 1332 | const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } }); |
| 1333 | let requests = 0; |
| 1334 | let online = true; |
| 1335 | return Effect.gen(function* () { |
| 1336 | const fileSystem = yield* FileSystem.FileSystem; |
| 1337 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1338 | prefix: "t3-acp-registry-refresh-fallback-", |
| 1339 | }); |
| 1340 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1341 | cacheDir, |
| 1342 | toolsDir: `${cacheDir}/tools`, |
| 1343 | registryUrl, |
| 1344 | }); |
| 1345 | yield* resolver.search({ query: "example" }); |
| 1346 | expect(requests).toBe(1); |
| 1347 | |
| 1348 | // Prepare always refreshes; an unknown agent stops it right after the refresh. |
| 1349 | online = false; |
| 1350 | const missing = yield* resolver.prepare({ agentId: "missing-agent" }).pipe(Effect.flip); |
| 1351 | expect(missing.reason).toBe("agent_not_found"); |
| 1352 | expect(requests).toBe(2); |
| 1353 | |
| 1354 | online = true; |
| 1355 | yield* resolver.search({ query: "example" }); |
| 1356 | expect(requests).toBe(3); |
| 1357 | }).pipe( |
| 1358 | Effect.scoped, |
| 1359 | Effect.provide( |
| 1360 | layerResolver((request) => { |
| 1361 | requests += 1; |
| 1362 | return Effect.succeed( |
| 1363 | HttpClientResponse.fromWeb( |
| 1364 | request, |
| 1365 | online |
| 1366 | ? new Response(makeRegistry(agent)) |
| 1367 | : new Response("unavailable", { status: 503 }), |
| 1368 | ), |
| 1369 | ); |
| 1370 | }), |
| 1371 | ), |
| 1372 | ); |
| 1373 | }); |
| 1374 | |
| 1375 | it.effect("filters runner recipes that the environment cannot prepare", () => { |
| 1376 | const runnerAgent = makeAgent({ npx: { package: "@example/acp@1.2.3" } }); |
| 1377 | const binaryAgent = { |
| 1378 | ...makeAgent({ |
| 1379 | binary: { |
| 1380 | "linux-x86_64": { archive: archiveUrl, cmd: "example-agent" }, |
| 1381 | }, |
| 1382 | }), |
| 1383 | id: "binary-agent", |
| 1384 | name: "Binary Agent", |
| 1385 | } satisfies AcpRegistrySupport.AcpRegistryAgent; |
| 1386 | return Effect.gen(function* () { |
| 1387 | const fileSystem = yield* FileSystem.FileSystem; |
| 1388 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1389 | prefix: "t3-acp-registry-runner-filter-", |
| 1390 | }); |
| 1391 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1392 | cacheDir, |
| 1393 | toolsDir: `${cacheDir}/tools`, |
| 1394 | registryUrl, |
| 1395 | }); |
| 1396 | const result = yield* resolver.search({ query: "" }); |
| 1397 | |
| 1398 | expect(result.agents.map((agent) => agent.id)).toEqual(["binary-agent"]); |
| 1399 | }).pipe( |
| 1400 | Effect.scoped, |
| 1401 | Effect.provide( |
| 1402 | layerResolver( |
| 1403 | (request) => |
| 1404 | Effect.succeed( |
| 1405 | HttpClientResponse.fromWeb( |
| 1406 | request, |
| 1407 | new Response( |
| 1408 | JSON.stringify({ version: "1.0.0", agents: [runnerAgent, binaryAgent] }), |
| 1409 | ), |
| 1410 | ), |
| 1411 | ), |
| 1412 | { PATH: "" }, |
| 1413 | ), |
| 1414 | ), |
| 1415 | ); |
| 1416 | }); |
| 1417 | |
| 1418 | it.effect("discards registry agents with blank names or unsafe versions", () => { |
| 1419 | const distribution = { |
| 1420 | binary: { |
| 1421 | "linux-x86_64": { archive: archiveUrl, cmd: "example-agent" }, |
| 1422 | }, |
| 1423 | } satisfies AcpRegistrySupport.AcpRegistryAgent["distribution"]; |
| 1424 | const valid = makeAgent(distribution); |
| 1425 | const blankName = { ...valid, id: "blank-name", name: " " }; |
| 1426 | const blankVersion = { ...valid, id: "blank-version", version: "\t" }; |
| 1427 | const parentVersion = { ...valid, id: "parent-version", version: ".." }; |
| 1428 | return Effect.gen(function* () { |
| 1429 | const fileSystem = yield* FileSystem.FileSystem; |
| 1430 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1431 | prefix: "t3-acp-registry-blank-fields-", |
| 1432 | }); |
| 1433 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1434 | cacheDir, |
| 1435 | toolsDir: `${cacheDir}/tools`, |
| 1436 | registryUrl, |
| 1437 | }); |
| 1438 | const result = yield* resolver.search({ query: "" }); |
| 1439 | |
| 1440 | expect(result.agents.map((agent) => agent.id)).toEqual([valid.id]); |
| 1441 | }).pipe( |
| 1442 | Effect.scoped, |
| 1443 | Effect.provide( |
| 1444 | layerResolver((request) => |
| 1445 | Effect.succeed( |
| 1446 | HttpClientResponse.fromWeb( |
| 1447 | request, |
| 1448 | new Response( |
| 1449 | JSON.stringify({ |
| 1450 | version: "1.0.0", |
| 1451 | agents: [blankName, blankVersion, parentVersion, valid], |
| 1452 | }), |
| 1453 | ), |
| 1454 | ), |
| 1455 | ), |
| 1456 | ), |
| 1457 | ), |
| 1458 | ); |
| 1459 | }); |
| 1460 | |
| 1461 | it.effect("ignores unpinned runner recipes from the registry", () => { |
| 1462 | const agent = makeAgent({ npx: { package: "@example/acp@latest" } }); |
| 1463 | return Effect.gen(function* () { |
| 1464 | const fileSystem = yield* FileSystem.FileSystem; |
| 1465 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1466 | prefix: "t3-acp-registry-unpinned-", |
| 1467 | }); |
| 1468 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1469 | cacheDir, |
| 1470 | toolsDir: `${cacheDir}/tools`, |
| 1471 | registryUrl, |
| 1472 | }); |
| 1473 | const result = yield* resolver.search({ query: "" }); |
| 1474 | |
| 1475 | expect(result.agents).toEqual([]); |
| 1476 | }).pipe( |
| 1477 | Effect.scoped, |
| 1478 | Effect.provide( |
| 1479 | layerResolver((request) => |
| 1480 | Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))), |
| 1481 | ), |
| 1482 | ), |
| 1483 | ); |
| 1484 | }); |
| 1485 | |
| 1486 | it.effect("rejects package syntax for the wrong runner", () => { |
| 1487 | const invalidAgents = [ |
| 1488 | { ...makeAgent({ npx: { package: "example-agent==1.2.3" } }), id: "bad-npx" }, |
| 1489 | ]; |
| 1490 | const validAgents = [ |
| 1491 | { ...makeAgent({ uvx: { package: "minion-code@0.1.44" } }), id: "valid-at" }, |
| 1492 | { ...makeAgent({ uvx: { package: "fast-agent-acp==0.9.30" } }), id: "valid-equals" }, |
| 1493 | ]; |
| 1494 | return Effect.gen(function* () { |
| 1495 | const fileSystem = yield* FileSystem.FileSystem; |
| 1496 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1497 | prefix: "t3-acp-registry-runner-syntax-", |
| 1498 | }); |
| 1499 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1500 | cacheDir, |
| 1501 | toolsDir: `${cacheDir}/tools`, |
| 1502 | registryUrl, |
| 1503 | }); |
| 1504 | const invalid = yield* resolver.prepare({ agentId: "bad-npx" }).pipe(Effect.flip); |
| 1505 | const validAt = yield* resolver.prepare({ agentId: "valid-at" }).pipe(Effect.flip); |
| 1506 | const validEquals = yield* resolver.prepare({ agentId: "valid-equals" }).pipe(Effect.flip); |
| 1507 | |
| 1508 | expect(invalid.reason).toBe("agent_not_found"); |
| 1509 | expect(validAt.reason).toBe("runner_unavailable"); |
| 1510 | expect(validEquals.reason).toBe("runner_unavailable"); |
| 1511 | }).pipe( |
| 1512 | Effect.scoped, |
| 1513 | Effect.provide( |
| 1514 | layerResolver( |
| 1515 | (request) => |
| 1516 | Effect.succeed( |
| 1517 | HttpClientResponse.fromWeb( |
| 1518 | request, |
| 1519 | new Response( |
| 1520 | JSON.stringify({ version: "1.0.0", agents: [...invalidAgents, ...validAgents] }), |
| 1521 | ), |
| 1522 | ), |
| 1523 | ), |
| 1524 | { PATH: "" }, |
| 1525 | ), |
| 1526 | ), |
| 1527 | ); |
| 1528 | }); |
| 1529 | |
| 1530 | it.effect("verifies declared SHA-256 before installing a binary", () => { |
| 1531 | const agent = makeAgent({ |
| 1532 | binary: { |
| 1533 | "linux-x86_64": { |
| 1534 | archive: archiveUrl, |
| 1535 | cmd: "example-agent", |
| 1536 | sha256: "0".repeat(64), |
| 1537 | }, |
| 1538 | }, |
| 1539 | }); |
| 1540 | const binaryBytes = new TextEncoder().encode("not the declared binary"); |
| 1541 | |
| 1542 | return Effect.gen(function* () { |
| 1543 | const fileSystem = yield* FileSystem.FileSystem; |
| 1544 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1545 | prefix: "t3-acp-registry-checksum-", |
| 1546 | }); |
| 1547 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1548 | cacheDir, |
| 1549 | toolsDir: `${cacheDir}/tools`, |
| 1550 | registryUrl, |
| 1551 | }); |
| 1552 | const error = yield* resolver.prepare({ agentId: agent.id }).pipe(Effect.flip); |
| 1553 | |
| 1554 | expect(error.reason).toBe("checksum_mismatch"); |
| 1555 | }).pipe( |
| 1556 | Effect.scoped, |
| 1557 | Effect.provide( |
| 1558 | layerResolver((request) => |
| 1559 | Effect.succeed( |
| 1560 | HttpClientResponse.fromWeb( |
| 1561 | request, |
| 1562 | request.url === registryUrl |
| 1563 | ? new Response(makeRegistry(agent)) |
| 1564 | : new Response(binaryBytes.buffer as ArrayBuffer), |
| 1565 | ), |
| 1566 | ), |
| 1567 | ), |
| 1568 | ), |
| 1569 | ); |
| 1570 | }); |
| 1571 | |
| 1572 | it.effect("falls back to a valid cached registry index when refresh fails", () => { |
| 1573 | const agent = makeAgent({ |
| 1574 | npx: { |
| 1575 | package: "@example/acp@1.2.3", |
| 1576 | args: ["--stdio"], |
| 1577 | }, |
| 1578 | }); |
| 1579 | return Effect.gen(function* () { |
| 1580 | const fileSystem = yield* FileSystem.FileSystem; |
| 1581 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1582 | prefix: "t3-acp-registry-cache-", |
| 1583 | }); |
| 1584 | const registryDirectory = `${cacheDir}/acp-registry`; |
| 1585 | yield* fileSystem.makeDirectory(registryDirectory, { recursive: true }); |
| 1586 | yield* fileSystem.writeFileString(`${registryDirectory}/registry.json`, makeRegistry(agent)); |
| 1587 | const toolchain = yield* makeFakeNpmToolchain(cacheDir); |
| 1588 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1589 | cacheDir, |
| 1590 | toolsDir: `${cacheDir}/tools`, |
| 1591 | registryUrl, |
| 1592 | }); |
| 1593 | const resolved = yield* resolver.resolve(settings(), "/workspace", toolchain.environment); |
| 1594 | |
| 1595 | expect(resolved.spawn).toMatchObject({ |
| 1596 | command: toolchain.executablePath, |
| 1597 | args: ["--stdio"], |
| 1598 | }); |
| 1599 | }).pipe( |
| 1600 | Effect.scoped, |
| 1601 | Effect.provide( |
| 1602 | layerResolver((request) => |
| 1603 | Effect.succeed( |
| 1604 | HttpClientResponse.fromWeb(request, new Response("unavailable", { status: 503 })), |
| 1605 | ), |
| 1606 | ), |
| 1607 | ), |
| 1608 | ); |
| 1609 | }); |
| 1610 | |
| 1611 | it.effect("rejects unsafe command paths before downloading an archive", () => { |
| 1612 | const agent = makeAgent({ |
| 1613 | binary: { |
| 1614 | "linux-x86_64": { |
| 1615 | archive: archiveUrl, |
| 1616 | cmd: "../outside", |
| 1617 | }, |
| 1618 | }, |
| 1619 | }); |
| 1620 | const requests: Array<string> = []; |
| 1621 | return Effect.gen(function* () { |
| 1622 | const fileSystem = yield* FileSystem.FileSystem; |
| 1623 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1624 | prefix: "t3-acp-registry-invalid-", |
| 1625 | }); |
| 1626 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1627 | cacheDir, |
| 1628 | toolsDir: `${cacheDir}/tools`, |
| 1629 | registryUrl, |
| 1630 | }); |
| 1631 | const error = yield* resolver.resolve(settings(), "/workspace").pipe(Effect.flip); |
| 1632 | |
| 1633 | expect(error.reason).toBe("archive_invalid"); |
| 1634 | expect(requests).toEqual([registryUrl]); |
| 1635 | }).pipe( |
| 1636 | Effect.scoped, |
| 1637 | Effect.provide( |
| 1638 | layerResolver((request) => { |
| 1639 | requests.push(request.url); |
| 1640 | return Effect.succeed( |
| 1641 | HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent))), |
| 1642 | ); |
| 1643 | }), |
| 1644 | ), |
| 1645 | ); |
| 1646 | }); |
| 1647 | |
| 1648 | it.effect("inspects from disk without waiting for a registry refresh", () => { |
| 1649 | const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } }); |
| 1650 | let requests = 0; |
| 1651 | return Effect.gen(function* () { |
| 1652 | const fileSystem = yield* FileSystem.FileSystem; |
| 1653 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1654 | prefix: "t3-acp-registry-cold-inspect-", |
| 1655 | }); |
| 1656 | const registryDirectory = `${cacheDir}/acp-registry`; |
| 1657 | yield* fileSystem.makeDirectory(registryDirectory, { recursive: true }); |
| 1658 | yield* fileSystem.writeFileString(`${registryDirectory}/registry.json`, makeRegistry(agent)); |
| 1659 | |
| 1660 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1661 | cacheDir, |
| 1662 | toolsDir: `${cacheDir}/tools`, |
| 1663 | registryUrl, |
| 1664 | }); |
| 1665 | const inspection = yield* resolver.inspect(settings()); |
| 1666 | |
| 1667 | expect(inspection).toMatchObject({ status: "ready", agentId: agent.id }); |
| 1668 | expect(requests).toBe(0); |
| 1669 | }).pipe( |
| 1670 | Effect.scoped, |
| 1671 | Effect.provide( |
| 1672 | layerResolver((request) => { |
| 1673 | requests += 1; |
| 1674 | return Effect.succeed( |
| 1675 | HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent))), |
| 1676 | ); |
| 1677 | }), |
| 1678 | ), |
| 1679 | ); |
| 1680 | }); |
| 1681 | |
| 1682 | it.effect("recovers inspection from a corrupt cache once the registry is reachable", () => { |
| 1683 | const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } }); |
| 1684 | let online = false; |
| 1685 | let requests = 0; |
| 1686 | return Effect.gen(function* () { |
| 1687 | const fileSystem = yield* FileSystem.FileSystem; |
| 1688 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1689 | prefix: "t3-acp-registry-offline-", |
| 1690 | }); |
| 1691 | yield* fileSystem.makeDirectory(`${cacheDir}/acp-registry`, { recursive: true }); |
| 1692 | yield* fileSystem.writeFileString(`${cacheDir}/acp-registry/registry.json`, "{not json"); |
| 1693 | const options = { cacheDir, toolsDir: `${cacheDir}/tools`, registryUrl }; |
| 1694 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog(options); |
| 1695 | |
| 1696 | const offline = yield* resolver.inspect(settings()).pipe(Effect.flip); |
| 1697 | expect(offline.reason).toBe("registry_unavailable"); |
| 1698 | |
| 1699 | online = true; |
| 1700 | const concurrent = yield* Effect.all( |
| 1701 | [resolver.inspect(settings()), resolver.inspect(settings())], |
| 1702 | { concurrency: "unbounded" }, |
| 1703 | ); |
| 1704 | expect(concurrent).toMatchObject([{ status: "ready" }, { status: "ready" }]); |
| 1705 | expect(requests).toBe(2); |
| 1706 | |
| 1707 | const restarted = yield* AcpRegistrySupport.makeAcpRegistryCatalog(options); |
| 1708 | expect(yield* restarted.inspect(settings())).toMatchObject({ status: "ready" }); |
| 1709 | expect(requests).toBe(2); |
| 1710 | }).pipe( |
| 1711 | Effect.scoped, |
| 1712 | Effect.provide( |
| 1713 | layerResolver((request) => { |
| 1714 | requests += 1; |
| 1715 | return Effect.succeed( |
| 1716 | HttpClientResponse.fromWeb( |
| 1717 | request, |
| 1718 | online |
| 1719 | ? new Response(makeRegistry(agent)) |
| 1720 | : new Response("unavailable", { status: 503 }), |
| 1721 | ), |
| 1722 | ); |
| 1723 | }), |
| 1724 | ), |
| 1725 | ); |
| 1726 | }); |
| 1727 | |
| 1728 | it.effect("uses the effective provider environment for inspection and resolution", () => { |
| 1729 | const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } }); |
| 1730 | return Effect.gen(function* () { |
| 1731 | const fileSystem = yield* FileSystem.FileSystem; |
| 1732 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1733 | prefix: "t3-acp-registry-effective-env-", |
| 1734 | }); |
| 1735 | const registryDirectory = `${cacheDir}/acp-registry`; |
| 1736 | yield* fileSystem.makeDirectory(registryDirectory, { recursive: true }); |
| 1737 | yield* fileSystem.writeFileString(`${registryDirectory}/registry.json`, makeRegistry(agent)); |
| 1738 | const toolchain = yield* makeFakeNpmToolchain(cacheDir); |
| 1739 | |
| 1740 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1741 | cacheDir, |
| 1742 | toolsDir: `${cacheDir}/tools`, |
| 1743 | registryUrl, |
| 1744 | }); |
| 1745 | const hostInspection = yield* resolver.inspect(settings()); |
| 1746 | const providerEnvironment = toolchain.environment; |
| 1747 | const instanceInspection = yield* resolver.inspect(settings(), providerEnvironment); |
| 1748 | const resolved = yield* resolver.resolve(settings(), "/workspace", providerEnvironment); |
| 1749 | |
| 1750 | expect(hostInspection).toMatchObject({ status: "missing_runner", runner: "npm" }); |
| 1751 | expect(instanceInspection).toMatchObject({ status: "ready", distribution: "npx" }); |
| 1752 | expect(resolved.spawn).toMatchObject({ |
| 1753 | command: toolchain.executablePath, |
| 1754 | env: { PATH: expect.stringMatching(new RegExp(`^${toolchain.globalBin}:`, "u")) }, |
| 1755 | }); |
| 1756 | }).pipe( |
| 1757 | Effect.scoped, |
| 1758 | Effect.provide( |
| 1759 | layerResolver( |
| 1760 | (request) => |
| 1761 | Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))), |
| 1762 | { PATH: "" }, |
| 1763 | ), |
| 1764 | ), |
| 1765 | ); |
| 1766 | }); |
| 1767 | |
| 1768 | it.effect("requires a regular executable file in the managed binary cache", () => { |
| 1769 | const agent = makeAgent({ |
| 1770 | binary: { |
| 1771 | "linux-x86_64": { |
| 1772 | archive: archiveUrl, |
| 1773 | cmd: "bin/example-agent", |
| 1774 | }, |
| 1775 | }, |
| 1776 | }); |
| 1777 | return Effect.gen(function* () { |
| 1778 | const fileSystem = yield* FileSystem.FileSystem; |
| 1779 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1780 | prefix: "t3-acp-registry-non-file-", |
| 1781 | }); |
| 1782 | const registryDirectory = `${cacheDir}/acp-registry`; |
| 1783 | const fakeExecutable = `${cacheDir}/tools/example-agent/1.2.3/linux-x86_64/bin/example-agent`; |
| 1784 | yield* fileSystem.makeDirectory(fakeExecutable, { recursive: true }); |
| 1785 | yield* fileSystem.makeDirectory(registryDirectory, { recursive: true }); |
| 1786 | yield* fileSystem.writeFileString(`${registryDirectory}/registry.json`, makeRegistry(agent)); |
| 1787 | |
| 1788 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1789 | cacheDir, |
| 1790 | toolsDir: `${cacheDir}/tools`, |
| 1791 | registryUrl, |
| 1792 | }); |
| 1793 | const directoryError = yield* resolver.inspect(settings()).pipe(Effect.flip); |
| 1794 | |
| 1795 | expect(directoryError.reason).toBe("archive_invalid"); |
| 1796 | |
| 1797 | yield* fileSystem.remove(fakeExecutable, { recursive: true }); |
| 1798 | yield* fileSystem.writeFileString(fakeExecutable, "#!/bin/sh\n"); |
| 1799 | yield* fileSystem.chmod(fakeExecutable, 0o644); |
| 1800 | const modeError = yield* resolver.inspect(settings()).pipe(Effect.flip); |
| 1801 | |
| 1802 | expect(modeError.reason).toBe("archive_invalid"); |
| 1803 | |
| 1804 | yield* fileSystem.chmod(fakeExecutable, 0o755); |
| 1805 | expect(yield* resolver.inspect(settings())).toMatchObject({ status: "ready" }); |
| 1806 | }).pipe( |
| 1807 | Effect.scoped, |
| 1808 | Effect.provide( |
| 1809 | layerResolver((request) => |
| 1810 | Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))), |
| 1811 | ), |
| 1812 | ), |
| 1813 | ); |
| 1814 | }); |
| 1815 | |
| 1816 | it.effect.each([ |
| 1817 | { name: "legacy registry", source: undefined, agentId: "example-agent", referenced: true }, |
| 1818 | { name: "registry", source: "registry", agentId: "example-agent", referenced: true }, |
| 1819 | { name: "padded registry", source: "registry", agentId: " example-agent ", referenced: true }, |
| 1820 | { name: "local command", source: "local", agentId: "example-agent", referenced: false }, |
| 1821 | ] as const)( |
| 1822 | "checks $name references before uninstalling only managed binaries", |
| 1823 | ({ source, agentId, referenced }) => { |
| 1824 | return Effect.gen(function* () { |
| 1825 | const fileSystem = yield* FileSystem.FileSystem; |
| 1826 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1827 | prefix: "t3-acp-registry-uninstall-", |
| 1828 | }); |
| 1829 | const agentRoot = `${cacheDir}/tools/example-agent`; |
| 1830 | const runnerCache = `${cacheDir}/external-npx-cache/example-agent/package.json`; |
| 1831 | yield* fileSystem.makeDirectory(`${agentRoot}/1.2.3/linux-x86_64`, { recursive: true }); |
| 1832 | yield* fileSystem.writeFileString(`${agentRoot}/1.2.3/linux-x86_64/agent`, "binary"); |
| 1833 | yield* fileSystem.makeDirectory(`${cacheDir}/external-npx-cache/example-agent`, { |
| 1834 | recursive: true, |
| 1835 | }); |
| 1836 | yield* fileSystem.writeFileString(runnerCache, "{}"); |
| 1837 | |
| 1838 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1839 | cacheDir, |
| 1840 | toolsDir: `${cacheDir}/tools`, |
| 1841 | registryUrl, |
| 1842 | }); |
| 1843 | const hostSettings = yield* TestProviderHost.TestProviderHostSettings; |
| 1844 | const instanceId = ProviderInstanceId.make("uninstall-reference"); |
| 1845 | yield* hostSettings.set({ |
| 1846 | ...DEFAULT_SERVER_SETTINGS, |
| 1847 | providerInstances: { |
| 1848 | [instanceId]: { |
| 1849 | driver: ProviderDriverKind.make("acpRegistry"), |
| 1850 | displayName: "Uninstall reference", |
| 1851 | enabled: true, |
| 1852 | config: { |
| 1853 | agentId, |
| 1854 | commandPath: "/local/agent", |
| 1855 | ...(source ? { source } : {}), |
| 1856 | }, |
| 1857 | }, |
| 1858 | }, |
| 1859 | }); |
| 1860 | const first = yield* resolver.uninstallManagedBinary({ agentId: "example-agent" }); |
| 1861 | expect(first).toEqual({ agentId: "example-agent", removed: !referenced }); |
| 1862 | expect(yield* fileSystem.exists(agentRoot)).toBe(referenced); |
| 1863 | yield* hostSettings.set(DEFAULT_SERVER_SETTINGS); |
| 1864 | const second = yield* resolver.uninstallManagedBinary({ agentId: "example-agent" }); |
| 1865 | |
| 1866 | expect(second).toEqual({ agentId: "example-agent", removed: referenced }); |
| 1867 | expect(yield* resolver.uninstallManagedBinary({ agentId: "example-agent" })).toEqual({ |
| 1868 | agentId: "example-agent", |
| 1869 | removed: false, |
| 1870 | }); |
| 1871 | expect(yield* fileSystem.exists(agentRoot)).toBe(false); |
| 1872 | expect(yield* fileSystem.exists(runnerCache)).toBe(true); |
| 1873 | }).pipe( |
| 1874 | Effect.scoped, |
| 1875 | Effect.provide( |
| 1876 | layerResolver((request) => |
| 1877 | Effect.succeed(HttpClientResponse.fromWeb(request, new Response("unused"))), |
| 1878 | ), |
| 1879 | ), |
| 1880 | ); |
| 1881 | }, |
| 1882 | ); |
| 1883 | |
| 1884 | it.effect("keeps managed package installs when removing the same agent's binaries", () => |
| 1885 | Effect.gen(function* () { |
| 1886 | const fileSystem = yield* FileSystem.FileSystem; |
| 1887 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1888 | prefix: "t3-acp-uninstall-packages-", |
| 1889 | }); |
| 1890 | const versionRoot = `${cacheDir}/tools/example-agent/1.2.3`; |
| 1891 | yield* fileSystem.makeDirectory(`${versionRoot}/linux-x86_64`, { recursive: true }); |
| 1892 | yield* fileSystem.writeFileString(`${versionRoot}/linux-x86_64/agent`, "binary"); |
| 1893 | yield* fileSystem.makeDirectory(`${versionRoot}/npm/bin`, { recursive: true }); |
| 1894 | yield* fileSystem.writeFileString(`${versionRoot}/npm/bin/agent`, "package command"); |
| 1895 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1896 | cacheDir, |
| 1897 | toolsDir: `${cacheDir}/tools`, |
| 1898 | registryUrl, |
| 1899 | }); |
| 1900 | expect(yield* resolver.uninstallManagedBinary({ agentId: "example-agent" })).toEqual({ |
| 1901 | agentId: "example-agent", |
| 1902 | removed: true, |
| 1903 | }); |
| 1904 | expect(yield* fileSystem.exists(`${versionRoot}/linux-x86_64`)).toBe(false); |
| 1905 | expect(yield* fileSystem.readFileString(`${versionRoot}/npm/bin/agent`)).toBe( |
| 1906 | "package command", |
| 1907 | ); |
| 1908 | expect(yield* resolver.uninstallManagedBinary({ agentId: "example-agent" })).toEqual({ |
| 1909 | agentId: "example-agent", |
| 1910 | removed: false, |
| 1911 | }); |
| 1912 | }).pipe( |
| 1913 | Effect.scoped, |
| 1914 | Effect.provide(layerResolver(() => Effect.die("unexpected HTTP request"))), |
| 1915 | ), |
| 1916 | ); |
| 1917 | |
| 1918 | it.effect("keeps a binary prepared by another client while an uninstall is waiting", () => { |
| 1919 | const agent = makeAgent({ |
| 1920 | binary: { |
| 1921 | "linux-x86_64": { |
| 1922 | archive: archiveUrl, |
| 1923 | cmd: "./bin/example-agent", |
| 1924 | }, |
| 1925 | }, |
| 1926 | }); |
| 1927 | const binaryBytes = new TextEncoder().encode("#!/bin/sh\necho example\n"); |
| 1928 | return Effect.gen(function* () { |
| 1929 | const downloadStarted = yield* Deferred.make<void>(); |
| 1930 | const releaseDownload = yield* Deferred.make<void>(); |
| 1931 | return yield* Effect.gen(function* () { |
| 1932 | const fileSystem = yield* FileSystem.FileSystem; |
| 1933 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 1934 | prefix: "t3-acp-registry-uninstall-race-", |
| 1935 | }); |
| 1936 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 1937 | cacheDir, |
| 1938 | toolsDir: `${cacheDir}/tools`, |
| 1939 | registryUrl, |
| 1940 | }); |
| 1941 | |
| 1942 | const prepareFiber = yield* resolver |
| 1943 | .prepare({ agentId: agent.id }) |
| 1944 | .pipe(Effect.forkChild({ startImmediately: true })); |
| 1945 | yield* Deferred.await(downloadStarted); |
| 1946 | const uninstallFiber = yield* resolver |
| 1947 | .uninstallManagedBinary({ agentId: agent.id }) |
| 1948 | .pipe(Effect.forkChild({ startImmediately: true })); |
| 1949 | |
| 1950 | expect(uninstallFiber.pollUnsafe()).toBeUndefined(); |
| 1951 | yield* Deferred.succeed(releaseDownload, undefined); |
| 1952 | |
| 1953 | expect(yield* Fiber.join(prepareFiber)).toMatchObject({ prepared: true }); |
| 1954 | expect(yield* Fiber.join(uninstallFiber)).toEqual({ |
| 1955 | agentId: agent.id, |
| 1956 | removed: false, |
| 1957 | }); |
| 1958 | const agentRoot = `${cacheDir}/tools/${agent.id}`; |
| 1959 | expect(yield* fileSystem.exists(agentRoot)).toBe(true); |
| 1960 | |
| 1961 | const hostSettings = yield* TestProviderHost.TestProviderHostSettings; |
| 1962 | const instanceId = ProviderInstanceId.make("registry-reference"); |
| 1963 | yield* hostSettings.set({ |
| 1964 | ...DEFAULT_SERVER_SETTINGS, |
| 1965 | providerInstances: { |
| 1966 | [instanceId]: { |
| 1967 | driver: ProviderDriverKind.make("acpRegistry"), |
| 1968 | displayName: "Registry reference", |
| 1969 | enabled: true, |
| 1970 | config: { agentId: agent.id }, |
| 1971 | }, |
| 1972 | }, |
| 1973 | }); |
| 1974 | expect(yield* resolver.uninstallManagedBinary({ agentId: agent.id })).toEqual({ |
| 1975 | agentId: agent.id, |
| 1976 | removed: false, |
| 1977 | }); |
| 1978 | yield* hostSettings.set(DEFAULT_SERVER_SETTINGS); |
| 1979 | expect(yield* resolver.uninstallManagedBinary({ agentId: agent.id })).toEqual({ |
| 1980 | agentId: agent.id, |
| 1981 | removed: true, |
| 1982 | }); |
| 1983 | expect(yield* resolver.uninstallManagedBinary({ agentId: agent.id })).toEqual({ |
| 1984 | agentId: agent.id, |
| 1985 | removed: false, |
| 1986 | }); |
| 1987 | }).pipe( |
| 1988 | Effect.scoped, |
| 1989 | Effect.provide( |
| 1990 | layerResolver((request) => { |
| 1991 | if (request.url === registryUrl) { |
| 1992 | return Effect.succeed( |
| 1993 | HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent))), |
| 1994 | ); |
| 1995 | } |
| 1996 | return Deferred.succeed(downloadStarted, undefined).pipe( |
| 1997 | Effect.andThen(Deferred.await(releaseDownload)), |
| 1998 | Effect.as( |
| 1999 | HttpClientResponse.fromWeb( |
| 2000 | request, |
| 2001 | new Response(binaryBytes.buffer as ArrayBuffer), |
| 2002 | ), |
| 2003 | ), |
| 2004 | ); |
| 2005 | }), |
| 2006 | ), |
| 2007 | ); |
| 2008 | }); |
| 2009 | }); |
| 2010 | |
| 2011 | it.effect("resolves an installed agent while another agent's download is still running", () => { |
| 2012 | const slowArchiveUrl = "https://registry.test/slow-agent.bin"; |
| 2013 | const agent = makeAgent({ |
| 2014 | binary: { "linux-x86_64": { archive: archiveUrl, cmd: "./bin/example-agent" } }, |
| 2015 | }); |
| 2016 | const slowAgent = { |
| 2017 | ...agent, |
| 2018 | distribution: { |
| 2019 | binary: { "linux-x86_64": { archive: slowArchiveUrl, cmd: "./bin/slow-agent" } }, |
| 2020 | }, |
| 2021 | id: "slow-agent", |
| 2022 | name: "Slow Agent", |
| 2023 | }; |
| 2024 | const binaryBytes = new TextEncoder().encode("#!/bin/sh\necho example\n"); |
| 2025 | return Effect.gen(function* () { |
| 2026 | const downloadStarted = yield* Deferred.make<void>(); |
| 2027 | const releaseDownload = yield* Deferred.make<void>(); |
| 2028 | return yield* Effect.gen(function* () { |
| 2029 | const fileSystem = yield* FileSystem.FileSystem; |
| 2030 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 2031 | prefix: "t3-acp-registry-parallel-install-", |
| 2032 | }); |
| 2033 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 2034 | cacheDir, |
| 2035 | toolsDir: `${cacheDir}/tools`, |
| 2036 | registryUrl, |
| 2037 | }); |
| 2038 | |
| 2039 | yield* resolver.resolve(settings(), "/workspace"); |
| 2040 | |
| 2041 | const prepareFiber = yield* resolver |
| 2042 | .prepare({ agentId: slowAgent.id }) |
| 2043 | .pipe(Effect.forkChild({ startImmediately: true })); |
| 2044 | yield* Deferred.await(downloadStarted); |
| 2045 | |
| 2046 | const resolved = yield* resolver.resolve(settings(), "/workspace"); |
| 2047 | expect(resolved.spawn.command).toContain( |
| 2048 | "/tools/example-agent/1.2.3/linux-x86_64/bin/example-agent", |
| 2049 | ); |
| 2050 | expect(prepareFiber.pollUnsafe()).toBeUndefined(); |
| 2051 | |
| 2052 | yield* Deferred.succeed(releaseDownload, undefined); |
| 2053 | expect(yield* Fiber.join(prepareFiber)).toMatchObject({ prepared: true }); |
| 2054 | }).pipe( |
| 2055 | Effect.scoped, |
| 2056 | Effect.provide( |
| 2057 | layerResolver((request) => { |
| 2058 | if (request.url === registryUrl) { |
| 2059 | return Effect.succeed( |
| 2060 | HttpClientResponse.fromWeb( |
| 2061 | request, |
| 2062 | new Response(JSON.stringify({ version: "1.0.0", agents: [slowAgent, agent] })), |
| 2063 | ), |
| 2064 | ); |
| 2065 | } |
| 2066 | const response = HttpClientResponse.fromWeb( |
| 2067 | request, |
| 2068 | new Response(binaryBytes.buffer as ArrayBuffer), |
| 2069 | ); |
| 2070 | if (request.url !== slowArchiveUrl) return Effect.succeed(response); |
| 2071 | return Deferred.succeed(downloadStarted, undefined).pipe( |
| 2072 | Effect.andThen(Deferred.await(releaseDownload)), |
| 2073 | Effect.as(response), |
| 2074 | ); |
| 2075 | }), |
| 2076 | ), |
| 2077 | ); |
| 2078 | }); |
| 2079 | }); |
| 2080 | |
| 2081 | it.effect("reserves a prepared binary until a configured instance inspects it", () => { |
| 2082 | const agent = makeAgent({ |
| 2083 | binary: { |
| 2084 | "linux-x86_64": { |
| 2085 | archive: archiveUrl, |
| 2086 | cmd: "./bin/example-agent", |
| 2087 | }, |
| 2088 | }, |
| 2089 | }); |
| 2090 | const binaryBytes = new TextEncoder().encode("#!/bin/sh\necho example\n"); |
| 2091 | return Effect.gen(function* () { |
| 2092 | const fileSystem = yield* FileSystem.FileSystem; |
| 2093 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 2094 | prefix: "t3-acp-registry-uninstall-reservation-", |
| 2095 | }); |
| 2096 | const resolver = yield* AcpRegistrySupport.makeAcpRegistryCatalog({ |
| 2097 | cacheDir, |
| 2098 | toolsDir: `${cacheDir}/tools`, |
| 2099 | registryUrl, |
| 2100 | }); |
| 2101 | |
| 2102 | yield* resolver.prepare({ agentId: agent.id }); |
| 2103 | expect(yield* resolver.uninstallManagedBinary({ agentId: agent.id })).toEqual({ |
| 2104 | agentId: agent.id, |
| 2105 | removed: false, |
| 2106 | }); |
| 2107 | expect(yield* resolver.inspect(settings())).toMatchObject({ status: "ready" }); |
| 2108 | expect(yield* resolver.uninstallManagedBinary({ agentId: agent.id })).toEqual({ |
| 2109 | agentId: agent.id, |
| 2110 | removed: true, |
| 2111 | }); |
| 2112 | }).pipe( |
| 2113 | Effect.scoped, |
| 2114 | Effect.provide( |
| 2115 | layerResolver((request) => |
| 2116 | Effect.succeed( |
| 2117 | HttpClientResponse.fromWeb( |
| 2118 | request, |
| 2119 | request.url === registryUrl |
| 2120 | ? new Response(makeRegistry(agent)) |
| 2121 | : new Response(binaryBytes.buffer as ArrayBuffer), |
| 2122 | ), |
| 2123 | ), |
| 2124 | ), |
| 2125 | ), |
| 2126 | ); |
| 2127 | }); |
| 2128 | }); |
| 2129 | |
| 2130 | describe("acpRegistryManagedBinaryDirectories", () => { |
| 2131 | it.effect("lists managed package and binary command directories", () => |
| 2132 | Effect.gen(function* () { |
| 2133 | const fileSystem = yield* FileSystem.FileSystem; |
| 2134 | const path = yield* Path.Path; |
| 2135 | const cacheDir = yield* fileSystem.makeTempDirectoryScoped({ |
| 2136 | prefix: "t3-acp-registry-bins-", |
| 2137 | }); |
| 2138 | const install = (agent: string, version: string, target: string, commandDirectory = "") => |
| 2139 | fileSystem.makeDirectory( |
| 2140 | path.join(cacheDir, "tools", agent, version, target, commandDirectory), |
| 2141 | { recursive: true }, |
| 2142 | ); |
| 2143 | yield* install("kimi", "1.49.0", "linux-x86_64", "bin"); |
| 2144 | yield* install("kimi", "1.50.0", "linux-x86_64", "cmd"); |
| 2145 | yield* install("kimi", "1.50.0", "darwin-aarch64"); |
| 2146 | yield* install("other-agent", "0.2.0", "darwin-aarch64"); |
| 2147 | const globalBin = path.join(cacheDir, "tools", "gemini", "0.56.0", "npm", "bin"); |
| 2148 | const geminiCommand = path.join(globalBin, "gemini"); |
| 2149 | const receiptsDirectory = path.join(cacheDir, "acp-registry", "package-installs"); |
| 2150 | yield* fileSystem.makeDirectory(globalBin, { recursive: true }); |
| 2151 | yield* fileSystem.makeDirectory(receiptsDirectory, { recursive: true }); |
| 2152 | yield* fileSystem.writeFileString(geminiCommand, "#!/bin/sh\n"); |
| 2153 | yield* fileSystem.chmod(geminiCommand, 0o755); |
| 2154 | yield* fileSystem.writeFileString( |
| 2155 | path.join(receiptsDirectory, "gemini.json"), |
| 2156 | encodeUnknownJson({ |
| 2157 | agentId: "gemini", |
| 2158 | agentVersion: "0.56.0", |
| 2159 | distribution: "npx", |
| 2160 | packageSpec: "@google/gemini-cli@0.56.0", |
| 2161 | managerPath: "/usr/bin/npm", |
| 2162 | binDirectory: globalBin, |
| 2163 | executablePath: geminiCommand, |
| 2164 | packageRoot: path.join( |
| 2165 | cacheDir, |
| 2166 | "tools", |
| 2167 | "gemini", |
| 2168 | "0.56.0", |
| 2169 | "npm", |
| 2170 | "lib", |
| 2171 | "node_modules", |
| 2172 | "@google", |
| 2173 | "gemini-cli", |
| 2174 | ), |
| 2175 | packageVersion: "0.56.0", |
| 2176 | }), |
| 2177 | ); |
| 2178 | yield* fileSystem.writeFileString( |
| 2179 | path.join(cacheDir, "acp-registry", "registry.json"), |
| 2180 | encodeUnknownJson({ |
| 2181 | version: "1.0.0", |
| 2182 | agents: [ |
| 2183 | { |
| 2184 | ...makeAgent({ |
| 2185 | binary: { |
| 2186 | "linux-x86_64": { archive: archiveUrl, cmd: "bin/kimi" }, |
| 2187 | }, |
| 2188 | }), |
| 2189 | id: "kimi", |
| 2190 | name: "Kimi", |
| 2191 | version: "1.49.0", |
| 2192 | }, |
| 2193 | { |
| 2194 | ...makeAgent({ |
| 2195 | binary: { |
| 2196 | "linux-x86_64": { archive: archiveUrl, cmd: "cmd/kimi" }, |
| 2197 | }, |
| 2198 | }), |
| 2199 | id: "kimi", |
| 2200 | name: "Kimi", |
| 2201 | version: "1.50.0", |
| 2202 | }, |
| 2203 | ], |
| 2204 | }), |
| 2205 | ); |
| 2206 | |
| 2207 | const directories = yield* AcpRegistrySupport.acpRegistryManagedBinaryDirectories({ |
| 2208 | cacheDir, |
| 2209 | toolsDir: path.join(cacheDir, "tools"), |
| 2210 | platform: "linux", |
| 2211 | architecture: "x64", |
| 2212 | }); |
| 2213 | expect(directories).toEqual([ |
| 2214 | globalBin, |
| 2215 | path.join(cacheDir, "tools", "kimi", "1.50.0", "linux-x86_64", "cmd"), |
| 2216 | path.join(cacheDir, "tools", "kimi", "1.49.0", "linux-x86_64", "bin"), |
| 2217 | ]); |
| 2218 | |
| 2219 | const missing = yield* AcpRegistrySupport.acpRegistryManagedBinaryDirectories({ |
| 2220 | cacheDir: path.join(cacheDir, "does-not-exist"), |
| 2221 | toolsDir: path.join(cacheDir, "does-not-exist", "tools"), |
| 2222 | platform: "linux", |
| 2223 | architecture: "x64", |
| 2224 | }); |
| 2225 | expect(missing).toEqual([]); |
| 2226 | }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), |
| 2227 | ); |
| 2228 | }); |