Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import { AcpRegistrySettings } from "@t3tools/contracts";
4import {
5 HostProcessArchitecture,
6 HostProcessEnvironment,
7 HostProcessPlatform,
8} from "@t3tools/shared/hostProcess";
9import { SpawnExecutableResolution } from "@t3tools/shared/shell";
10import * as Deferred from "effect/Deferred";
11import * as Effect from "effect/Effect";
12import * as FileSystem from "effect/FileSystem";
13import * as Path from "effect/Path";
14import * as Fiber from "effect/Fiber";
15import * as Layer from "effect/Layer";
16import * as Option from "effect/Option";
17import * as Ref from "effect/Ref";
18import * as Schema from "effect/Schema";
19import { HttpClient, HttpClientResponse } from "effect/unstable/http";
20import * as TestClock from "effect/testing/TestClock";
21import * as NodeCrypto from "node:crypto";
22
23import {
24 AcpRegistryError,
25 acpRegistryManagedBinaryDirectories,
26 makeAcpRegistryCatalog,
27 resolveAcpRegistryDistribution,
28 resolveAcpRegistryPlatformTarget,
29 toAcpRegistryOperationError,
30 type AcpRegistryAgent,
31} from "./AcpRegistrySupport.ts";
32
33const registryUrl = "https://registry.test/registry.json";
34const archiveUrl = "https://registry.test/example-agent.bin";
35const decodeAcpRegistrySettings = Schema.decodeSync(AcpRegistrySettings);
36const encodeUnknownJson = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown));
37
38function makeAgent(distribution: AcpRegistryAgent["distribution"]): AcpRegistryAgent {
39 return {
40 id: "example-agent",
41 name: "Example Agent",
42 version: "1.2.3",
43 description: "ACP Registry test agent",
44 distribution,
45 };
46}
47
48function makeRegistry(agent: AcpRegistryAgent): string {
49 return JSON.stringify({ version: "1.0.0", agents: [agent] });
50}
51
52function settings(input: Partial<AcpRegistrySettings> = {}): AcpRegistrySettings {
53 return decodeAcpRegistrySettings({
54 agentId: "example-agent",
55 ...input,
56 });
57}
58
59function resolverLayer(
60 execute: Parameters<typeof HttpClient.make>[0],
61 environment: NodeJS.ProcessEnv = process.env,
62) {
63 return Layer.mergeAll(
64 NodeServices.layer,
65 Layer.succeed(HostProcessPlatform, "linux"),
66 Layer.succeed(HostProcessArchitecture, "x64"),
67 Layer.succeed(HostProcessEnvironment, environment),
68 Layer.succeed(HttpClient.HttpClient, HttpClient.make(execute)),
69 );
70}
71
72const makeFakeNpmToolchain = Effect.fn("AcpRegistrySupport.test.makeFakeNpmToolchain")(function* (
73 rootDirectory: string,
74) {
75 const fileSystem = yield* FileSystem.FileSystem;
76 const path = yield* Path.Path;
77 const toolchainBin = path.join(rootDirectory, "fake-node", "bin");
78 const globalPrefix = path.join(rootDirectory, "global");
79 const globalBin = path.join(globalPrefix, "bin");
80 const executablePath = path.join(globalBin, "example-agent");
81 const npmPath = path.join(toolchainBin, "npm");
82 const logPath = path.join(rootDirectory, "npm.log");
83 yield* fileSystem.makeDirectory(toolchainBin, { recursive: true });
84 yield* fileSystem.makeDirectory(globalPrefix, { recursive: true });
85 yield* fileSystem.writeFileString(logPath, "");
86 yield* fileSystem.writeFileString(
87 npmPath,
88 [
89 "#!/bin/sh",
90 'printf \'%s\\n\' "$*" >> "$FAKE_NPM_LOG"',
91 'prefix="${npm_config_prefix:-$FAKE_NPM_PREFIX}"',
92 'if [ "$1" = "root" ] && [ "$2" = "--global" ]; then',
93 " printf '%s\\n' \"$prefix/lib/node_modules\"",
94 " exit 0",
95 "fi",
96 'if [ "$1" = "prefix" ] && [ "$2" = "--global" ]; then',
97 " printf '%s\\n' \"$prefix\"",
98 " exit 0",
99 "fi",
100 'if [ "$1" = "install" ] && [ "$2" = "--global" ]; then',
101 ' package_root="$prefix/lib/node_modules/@example/acp"',
102 ' executable="$prefix/bin/example-agent"',
103 ' mkdir -p "$package_root" "$prefix/bin"',
104 ' printf \'%s\' "$FAKE_NPM_MANIFEST" > "$package_root/package.json"',
105 " printf '#!/bin/sh\\n' > \"$executable\"",
106 ' chmod 755 "$executable"',
107 " exit 0",
108 "fi",
109 "exit 64",
110 "",
111 ].join("\n"),
112 );
113 yield* fileSystem.chmod(npmPath, 0o755);
114 return {
115 environment: {
116 ...process.env,
117 PATH: `${toolchainBin}:${process.env.PATH ?? ""}`,
118 FAKE_NPM_LOG: logPath,
119 FAKE_NPM_PREFIX: globalPrefix,
120 FAKE_NPM_MANIFEST: encodeUnknownJson({
121 name: "@example/acp",
122 version: "1.2.3",
123 bin: { "example-agent": "dist/cli.js" },
124 }),
125 } satisfies NodeJS.ProcessEnv,
126 executablePath,
127 globalBin,
128 logPath,
129 npmPath,
130 };
131});
132
133const makeFakeUvToolchain = Effect.fn("AcpRegistrySupport.test.makeFakeUvToolchain")(function* (
134 rootDirectory: string,
135) {
136 const fileSystem = yield* FileSystem.FileSystem;
137 const path = yield* Path.Path;
138 const toolchainBin = path.join(rootDirectory, "fake-uv", "bin");
139 const globalBin = path.join(rootDirectory, "uv-tools", "bin");
140 const executablePath = path.join(globalBin, "fast-agent");
141 const uvPath = path.join(toolchainBin, "uv");
142 const logPath = path.join(rootDirectory, "uv.log");
143 yield* fileSystem.makeDirectory(toolchainBin, { recursive: true });
144 yield* fileSystem.writeFileString(logPath, "");
145 yield* fileSystem.writeFileString(
146 uvPath,
147 [
148 "#!/bin/sh",
149 'printf \'%s\\n\' "$*" >> "$FAKE_UV_LOG"',
150 'if [ "$1" = "tool" ] && [ "$2" = "dir" ] && [ "$3" = "--bin" ]; then',
151 " printf '%s\\n' \"$FAKE_UV_BIN\"",
152 " exit 0",
153 "fi",
154 'if [ "$1" = "tool" ] && [ "$2" = "list" ]; then',
155 ' if [ -x "$FAKE_UV_EXECUTABLE" ]; then',
156 " printf 'fast-agent-acp v0.10.1\\n- fast-agent\\n'",
157 " fi",
158 " exit 0",
159 "fi",
160 'if [ "$1" = "tool" ] && [ "$2" = "install" ] && [ "$3" = "--force" ]; then',
161 ' mkdir -p "$FAKE_UV_BIN"',
162 " printf '#!/bin/sh\\n' > \"$FAKE_UV_EXECUTABLE\"",
163 ' chmod 755 "$FAKE_UV_EXECUTABLE"',
164 " exit 0",
165 "fi",
166 "exit 64",
167 "",
168 ].join("\n"),
169 );
170 yield* fileSystem.chmod(uvPath, 0o755);
171 return {
172 environment: {
173 ...process.env,
174 PATH: `${toolchainBin}:${process.env.PATH ?? ""}`,
175 FAKE_UV_LOG: logPath,
176 FAKE_UV_BIN: globalBin,
177 FAKE_UV_EXECUTABLE: executablePath,
178 } satisfies NodeJS.ProcessEnv,
179 executablePath,
180 globalBin,
181 logPath,
182 uvPath,
183 };
184});
185
186describe("AcpRegistrySupport", () => {
187 it("preserves the registry failure when translating it for clients", () => {
188 const cause = new Error("registry unavailable");
189 const failure = new AcpRegistryError({
190 reason: "registry_unavailable",
191 detail: "Could not load the ACP Registry.",
192 cause,
193 });
194
195 expect(toAcpRegistryOperationError(failure)).toMatchObject({
196 reason: "registry_unavailable",
197 message: "Could not load the ACP Registry.",
198 cause: failure,
199 });
200 });
201
202 it("maps supported Node platforms to ACP Registry target keys", () => {
203 expect(resolveAcpRegistryPlatformTarget("darwin", "arm64")).toBe("darwin-aarch64");
204 expect(resolveAcpRegistryPlatformTarget("linux", "x64")).toBe("linux-x86_64");
205 expect(resolveAcpRegistryPlatformTarget("win32", "arm64")).toBe("windows-aarch64");
206 expect(resolveAcpRegistryPlatformTarget("freebsd", "x64")).toBeUndefined();
207 expect(resolveAcpRegistryPlatformTarget("linux", "ia32")).toBeUndefined();
208 });
209
210 it("selects the preferred compatible distribution", () => {
211 const agent = makeAgent({
212 binary: {
213 "linux-x86_64": {
214 archive: archiveUrl,
215 cmd: "./bin/example-agent",
216 args: ["acp"],
217 },
218 },
219 npx: {
220 package: "@example/acp@1.2.3",
221 args: ["--stdio"],
222 },
223 });
224
225 expect(
226 resolveAcpRegistryDistribution({
227 agent,
228 preference: "auto",
229 platformTarget: "linux-x86_64",
230 }),
231 ).toMatchObject({ kind: "binary", args: ["acp"] });
232 expect(
233 resolveAcpRegistryDistribution({
234 agent,
235 preference: "npx",
236 platformTarget: "linux-x86_64",
237 }),
238 ).toEqual({
239 kind: "npx",
240 packageName: "@example/acp@1.2.3",
241 args: ["--stdio"],
242 env: {},
243 });
244 expect(
245 resolveAcpRegistryDistribution({
246 agent,
247 preference: "binary",
248 platformTarget: "darwin-aarch64",
249 }),
250 ).toBeUndefined();
251 });
252
253 it.effect("resolves command overrides while preserving registry args and environment", () => {
254 const agent = makeAgent({
255 binary: {
256 "linux-x86_64": {
257 archive: archiveUrl,
258 cmd: "./bin/example-agent",
259 args: ["acp", "--stdio"],
260 env: { REGISTRY_VALUE: "registry", OVERRIDE_ME: "registry" },
261 },
262 },
263 });
264 const requests: Array<string> = [];
265 return Effect.gen(function* () {
266 const fileSystem = yield* FileSystem.FileSystem;
267 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
268 prefix: "t3-acp-registry-override-",
269 });
270 const commandPath = `${cacheDir}/example-agent`;
271 yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n");
272 yield* fileSystem.chmod(commandPath, 0o755);
273 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
274 const resolved = yield* resolver.resolve(settings({ commandPath }), "/workspace", {
275 HOST_VALUE: "host",
276 OVERRIDE_ME: "host",
277 });
278
279 expect(resolved.distribution).toBe("binary");
280 expect(resolved.spawn).toEqual({
281 command: commandPath,
282 args: ["acp", "--stdio"],
283 cwd: "/workspace",
284 env: {
285 HOST_VALUE: "host",
286 OVERRIDE_ME: "registry",
287 REGISTRY_VALUE: "registry",
288 },
289 });
290 expect(requests).toEqual([registryUrl]);
291 }).pipe(
292 Effect.scoped,
293 Effect.provide(
294 resolverLayer((request) => {
295 requests.push(request.url);
296 return Effect.succeed(
297 HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent))),
298 );
299 }),
300 ),
301 );
302 });
303
304 it.effect("globally installs a package and launches its exposed command", () => {
305 const agent = makeAgent({
306 npx: { package: "@example/acp@V1.2.3", args: ["--stdio"] },
307 });
308 return Effect.gen(function* () {
309 const fileSystem = yield* FileSystem.FileSystem;
310 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
311 prefix: "t3-acp-registry-global-package-",
312 });
313 const toolchain = yield* makeFakeNpmToolchain(cacheDir);
314 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl }).pipe(
315 Effect.provideService(HostProcessEnvironment, toolchain.environment),
316 );
317
318 const first = yield* resolver.resolve(settings(), "/workspace", toolchain.environment);
319 const second = yield* resolver.resolve(settings(), "/workspace", toolchain.environment);
320
321 expect(first.spawn).toMatchObject({
322 command: toolchain.executablePath,
323 args: ["--stdio"],
324 env: { PATH: expect.stringMatching(new RegExp(`^${toolchain.globalBin}:`, "u")) },
325 });
326 expect(second.spawn.command).toBe(toolchain.executablePath);
327 const npmCommands = yield* fileSystem.readFileString(toolchain.logPath);
328 expect(npmCommands.match(/^install --global /gmu)).toHaveLength(1);
329 expect(npmCommands).toContain("install --global @example/acp@V1.2.3");
330 }).pipe(
331 Effect.scoped,
332 Effect.provide(
333 resolverLayer((request) =>
334 Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))),
335 ),
336 ),
337 );
338 });
339
340 it.effect("falls back to a writable user prefix for a system-owned npm", () => {
341 const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } });
342 return Effect.gen(function* () {
343 const fileSystem = yield* FileSystem.FileSystem;
344 const path = yield* Path.Path;
345 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
346 prefix: "t3-acp-registry-user-global-package-",
347 });
348 const toolchain = yield* makeFakeNpmToolchain(cacheDir);
349 yield* fileSystem.chmod(path.dirname(toolchain.globalBin), 0o555);
350 const userHome = path.join(cacheDir, "home");
351 const environment = { ...toolchain.environment, HOME: userHome };
352 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl }).pipe(
353 Effect.provideService(HostProcessEnvironment, environment),
354 );
355
356 const resolved = yield* resolver.resolve(settings(), "/workspace", environment);
357 const userGlobalBin = path.join(userHome, ".local", "bin");
358 expect(resolved.spawn).toMatchObject({
359 command: path.join(userGlobalBin, "example-agent"),
360 env: { PATH: expect.stringMatching(new RegExp(`^${userGlobalBin}:`, "u")) },
361 });
362 }).pipe(
363 Effect.scoped,
364 Effect.provide(
365 resolverLayer((request) =>
366 Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))),
367 ),
368 ),
369 );
370 });
371
372 it.effect("globally installs a uv tool and launches its exposed command", () => {
373 const agent = makeAgent({
374 uvx: { package: "fast-agent-acp==V0.10.1", args: ["--acp"] },
375 });
376 return Effect.gen(function* () {
377 const fileSystem = yield* FileSystem.FileSystem;
378 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
379 prefix: "t3-acp-registry-global-uv-tool-",
380 });
381 const toolchain = yield* makeFakeUvToolchain(cacheDir);
382 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
383
384 const resolved = yield* resolver.resolve(settings(), "/workspace", toolchain.environment);
385
386 expect(resolved.spawn).toMatchObject({
387 command: toolchain.executablePath,
388 args: ["--acp"],
389 env: { PATH: expect.stringMatching(new RegExp(`^${toolchain.globalBin}:`, "u")) },
390 });
391 expect(yield* fileSystem.readFileString(toolchain.logPath)).toContain(
392 "tool install --force fast-agent-acp==V0.10.1",
393 );
394 }).pipe(
395 Effect.scoped,
396 Effect.provide(
397 resolverLayer((request) =>
398 Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))),
399 ),
400 ),
401 );
402 });
403
404 it.effect("honors an exact Windows PATH override when launching a global package", () => {
405 const agent = makeAgent({
406 uvx: { package: "fast-agent-acp==0.10.1", args: ["--acp"] },
407 });
408 return Effect.gen(function* () {
409 const fileSystem = yield* FileSystem.FileSystem;
410 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
411 prefix: "t3-acp-registry-windows-package-path-",
412 });
413 const toolchain = yield* makeFakeUvToolchain(cacheDir);
414 const linuxResolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
415 yield* linuxResolver.resolve(settings(), "/workspace", toolchain.environment);
416
417 const windowsEnvironment = {
418 ...toolchain.environment,
419 Path: "C:\\host\\bin",
420 PATH: "C:\\provider\\bin",
421 };
422 const windowsResolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl }).pipe(
423 Effect.provideService(HostProcessPlatform, "win32"),
424 Effect.provideService(SpawnExecutableResolution, (command) => {
425 if (command === "uv") return toolchain.uvPath;
426 if (command === "fast-agent") return toolchain.executablePath;
427 return undefined;
428 }),
429 );
430 const resolved = yield* windowsResolver.resolve(
431 settings(),
432 "C:\\workspace",
433 windowsEnvironment,
434 );
435
436 expect(resolved.spawn).toMatchObject({
437 env: { PATH: `${toolchain.globalBin};C:\\provider\\bin` },
438 });
439 }).pipe(
440 Effect.scoped,
441 Effect.provide(
442 resolverLayer((request) =>
443 Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))),
444 ),
445 ),
446 );
447 });
448
449 it.effect("installs and reuses a registry binary in the managed cache", () => {
450 const binaryBytes = new TextEncoder().encode("#!/bin/sh\necho example\n");
451 const agent = makeAgent({
452 binary: {
453 "linux-x86_64": {
454 archive: archiveUrl,
455 cmd: "./bin/example-agent",
456 args: ["acp"],
457 sha256: NodeCrypto.createHash("sha256").update(binaryBytes).digest("hex"),
458 },
459 },
460 });
461 const requests: Array<string> = [];
462 return Effect.gen(function* () {
463 const fileSystem = yield* FileSystem.FileSystem;
464 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
465 prefix: "t3-acp-registry-install-",
466 });
467 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
468 const first = yield* resolver.resolve(settings(), "/workspace");
469 const second = yield* resolver.resolve(settings(), "/workspace");
470
471 expect(first.spawn.command).toBe(second.spawn.command);
472 expect(first.spawn.command).toContain(
473 "/acp-registry/agents/example-agent/1.2.3/linux-x86_64/bin/example-agent",
474 );
475 expect(yield* fileSystem.readFileString(first.spawn.command)).toBe(
476 "#!/bin/sh\necho example\n",
477 );
478 expect(requests).toEqual([registryUrl, archiveUrl]);
479 }).pipe(
480 Effect.scoped,
481 Effect.provide(
482 resolverLayer((request) => {
483 requests.push(request.url);
484 const response =
485 request.url === registryUrl
486 ? new Response(makeRegistry(agent))
487 : new Response(
488 new ReadableStream<Uint8Array>({
489 start(controller) {
490 controller.enqueue(binaryBytes.slice(0, 8));
491 controller.enqueue(binaryBytes.slice(8));
492 controller.close();
493 },
494 }),
495 );
496 return Effect.succeed(HttpClientResponse.fromWeb(request, response));
497 }),
498 ),
499 );
500 });
501
502 it.effect("detects an already-installed system binary instead of downloading", () => {
503 const agent = makeAgent({
504 binary: {
505 "linux-x86_64": {
506 archive: archiveUrl,
507 cmd: "./bin/example-agent",
508 args: ["acp"],
509 },
510 },
511 });
512 const requests: Array<string> = [];
513 return Effect.gen(function* () {
514 const fileSystem = yield* FileSystem.FileSystem;
515 const path = yield* Path.Path;
516 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
517 prefix: "t3-acp-registry-system-",
518 });
519 const systemBinDir = yield* fileSystem.makeTempDirectoryScoped({
520 prefix: "t3-acp-registry-system-bin-",
521 });
522 const systemBinary = path.join(systemBinDir, "example-agent");
523 yield* fileSystem.writeFileString(systemBinary, "#!/bin/sh\necho system\n");
524 yield* fileSystem.chmod(systemBinary, 0o755);
525 const environment = { PATH: systemBinDir };
526
527 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
528 const resolved = yield* resolver.resolve(settings(), "/workspace", environment);
529 expect(resolved.spawn.command).toBe(systemBinary);
530 expect(resolved.spawn.args).toEqual(["acp"]);
531
532 const inspection = yield* resolver.inspect(settings(), environment);
533 expect(inspection).toMatchObject({ status: "ready", distribution: "binary" });
534
535 // Only the registry index was fetched; no archive download happened.
536 expect(requests).toEqual([registryUrl]);
537 }).pipe(
538 Effect.scoped,
539 Effect.provide(
540 resolverLayer((request) => {
541 requests.push(request.url);
542 return Effect.succeed(
543 HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent))),
544 );
545 }),
546 ),
547 );
548 });
549
550 it.effect("searches compatible agents with deterministic ranking and bounded metadata", () => {
551 const exact = {
552 ...makeAgent({ npx: { package: "@example/acp@1.2.3" } }),
553 id: "codex-acp",
554 name: "Codex",
555 authors: ["OpenAI", "Zed Industries"],
556 license: "Apache-2.0",
557 website: "https://example.test/codex",
558 repository: "https://example.test/codex/source",
559 icon: "https://example.test/codex.svg",
560 } satisfies AcpRegistryAgent;
561 const descriptionMatch = {
562 ...makeAgent({ npx: { package: "other-agent@1.2.3" } }),
563 id: "other-agent",
564 name: "Other Agent",
565 description: "An adapter for Codex workflows",
566 } satisfies AcpRegistryAgent;
567 const incompatible = {
568 ...makeAgent({
569 binary: {
570 "darwin-aarch64": { archive: archiveUrl, cmd: "agent" },
571 },
572 }),
573 id: "darwin-only",
574 name: "Codex Darwin",
575 } satisfies AcpRegistryAgent;
576
577 return Effect.gen(function* () {
578 const fileSystem = yield* FileSystem.FileSystem;
579 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
580 prefix: "t3-acp-registry-search-",
581 });
582 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
583 const result = yield* resolver.search({ query: "codex" });
584
585 expect(result.agents.map((agent) => agent.id)).toEqual(["codex-acp", "other-agent"]);
586 expect(result.agents[0]).toMatchObject({
587 authors: ["OpenAI", "Zed Industries"],
588 distribution: "npx",
589 integrity: "registry",
590 license: "Apache-2.0",
591 });
592 }).pipe(
593 Effect.scoped,
594 Effect.provide(
595 resolverLayer((request) =>
596 Effect.succeed(
597 HttpClientResponse.fromWeb(
598 request,
599 new Response(
600 JSON.stringify({
601 version: "1.0.0",
602 agents: [descriptionMatch, incompatible, exact],
603 }),
604 ),
605 ),
606 ),
607 ),
608 ),
609 );
610 });
611
612 it.effect("refreshes explicit searches and coalesces concurrent refreshes", () => {
613 const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } });
614 let requests = 0;
615 return Effect.gen(function* () {
616 const fileSystem = yield* FileSystem.FileSystem;
617 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
618 prefix: "t3-acp-registry-refresh-",
619 });
620 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
621 yield* Effect.all(
622 [resolver.search({ query: "example" }), resolver.search({ query: "example" })],
623 { concurrency: "unbounded" },
624 );
625 expect(requests).toBe(1);
626
627 yield* resolver.search({ query: "example" });
628 expect(requests).toBe(2);
629 }).pipe(
630 Effect.scoped,
631 Effect.provide(
632 resolverLayer((request) => {
633 requests += 1;
634 return Effect.yieldNow.pipe(
635 Effect.as(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))),
636 );
637 }),
638 ),
639 );
640 });
641
642 it.effect("filters runner recipes that the environment cannot prepare", () => {
643 const runnerAgent = makeAgent({ npx: { package: "@example/acp@1.2.3" } });
644 const binaryAgent = {
645 ...makeAgent({
646 binary: {
647 "linux-x86_64": { archive: archiveUrl, cmd: "example-agent" },
648 },
649 }),
650 id: "binary-agent",
651 name: "Binary Agent",
652 } satisfies AcpRegistryAgent;
653 return Effect.gen(function* () {
654 const fileSystem = yield* FileSystem.FileSystem;
655 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
656 prefix: "t3-acp-registry-runner-filter-",
657 });
658 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
659 const result = yield* resolver.search({ query: "" });
660
661 expect(result.agents.map((agent) => agent.id)).toEqual(["binary-agent"]);
662 }).pipe(
663 Effect.scoped,
664 Effect.provide(
665 resolverLayer(
666 (request) =>
667 Effect.succeed(
668 HttpClientResponse.fromWeb(
669 request,
670 new Response(
671 JSON.stringify({ version: "1.0.0", agents: [runnerAgent, binaryAgent] }),
672 ),
673 ),
674 ),
675 { PATH: "" },
676 ),
677 ),
678 );
679 });
680
681 it.effect("discards registry agents with blank names or versions", () => {
682 const distribution = {
683 binary: {
684 "linux-x86_64": { archive: archiveUrl, cmd: "example-agent" },
685 },
686 } satisfies AcpRegistryAgent["distribution"];
687 const valid = makeAgent(distribution);
688 const blankName = { ...valid, id: "blank-name", name: " " };
689 const blankVersion = { ...valid, id: "blank-version", version: "\t" };
690 return Effect.gen(function* () {
691 const fileSystem = yield* FileSystem.FileSystem;
692 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
693 prefix: "t3-acp-registry-blank-fields-",
694 });
695 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
696 const result = yield* resolver.search({ query: "" });
697
698 expect(result.agents.map((agent) => agent.id)).toEqual([valid.id]);
699 }).pipe(
700 Effect.scoped,
701 Effect.provide(
702 resolverLayer((request) =>
703 Effect.succeed(
704 HttpClientResponse.fromWeb(
705 request,
706 new Response(
707 JSON.stringify({
708 version: "1.0.0",
709 agents: [blankName, blankVersion, valid],
710 }),
711 ),
712 ),
713 ),
714 ),
715 ),
716 );
717 });
718
719 it.effect("ignores unpinned runner recipes from the registry", () => {
720 const agent = makeAgent({ npx: { package: "@example/acp@latest" } });
721 return Effect.gen(function* () {
722 const fileSystem = yield* FileSystem.FileSystem;
723 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
724 prefix: "t3-acp-registry-unpinned-",
725 });
726 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
727 const result = yield* resolver.search({ query: "" });
728
729 expect(result.agents).toEqual([]);
730 }).pipe(
731 Effect.scoped,
732 Effect.provide(
733 resolverLayer((request) =>
734 Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))),
735 ),
736 ),
737 );
738 });
739
740 it.effect("rejects package syntax for the wrong runner", () => {
741 const invalidAgents = [
742 { ...makeAgent({ npx: { package: "example-agent==1.2.3" } }), id: "bad-npx" },
743 ];
744 const validAgents = [
745 { ...makeAgent({ uvx: { package: "minion-code@0.1.44" } }), id: "valid-at" },
746 { ...makeAgent({ uvx: { package: "fast-agent-acp==0.9.30" } }), id: "valid-equals" },
747 ];
748 return Effect.gen(function* () {
749 const fileSystem = yield* FileSystem.FileSystem;
750 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
751 prefix: "t3-acp-registry-runner-syntax-",
752 });
753 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
754 const invalid = yield* resolver.prepare({ agentId: "bad-npx" }).pipe(Effect.flip);
755 const validAt = yield* resolver.prepare({ agentId: "valid-at" }).pipe(Effect.flip);
756 const validEquals = yield* resolver.prepare({ agentId: "valid-equals" }).pipe(Effect.flip);
757
758 expect(invalid.reason).toBe("agent_not_found");
759 expect(validAt.reason).toBe("runner_unavailable");
760 expect(validEquals.reason).toBe("runner_unavailable");
761 }).pipe(
762 Effect.scoped,
763 Effect.provide(
764 resolverLayer(
765 (request) =>
766 Effect.succeed(
767 HttpClientResponse.fromWeb(
768 request,
769 new Response(
770 JSON.stringify({ version: "1.0.0", agents: [...invalidAgents, ...validAgents] }),
771 ),
772 ),
773 ),
774 { PATH: "" },
775 ),
776 ),
777 );
778 });
779
780 it.effect("verifies declared SHA-256 before installing a binary", () => {
781 const agent = makeAgent({
782 binary: {
783 "linux-x86_64": {
784 archive: archiveUrl,
785 cmd: "example-agent",
786 sha256: "0".repeat(64),
787 },
788 },
789 });
790 const binaryBytes = new TextEncoder().encode("not the declared binary");
791
792 return Effect.gen(function* () {
793 const fileSystem = yield* FileSystem.FileSystem;
794 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
795 prefix: "t3-acp-registry-checksum-",
796 });
797 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
798 const error = yield* resolver.prepare({ agentId: agent.id }).pipe(Effect.flip);
799
800 expect(error.reason).toBe("checksum_mismatch");
801 }).pipe(
802 Effect.scoped,
803 Effect.provide(
804 resolverLayer((request) =>
805 Effect.succeed(
806 HttpClientResponse.fromWeb(
807 request,
808 request.url === registryUrl
809 ? new Response(makeRegistry(agent))
810 : new Response(binaryBytes.buffer as ArrayBuffer),
811 ),
812 ),
813 ),
814 ),
815 );
816 });
817
818 it.effect("globally installs package recipes during preparation", () => {
819 const agent = makeAgent({ npx: { package: "@example/acp@1.2.3", args: ["--stdio"] } });
820 const requests: string[] = [];
821
822 return Effect.gen(function* () {
823 const fileSystem = yield* FileSystem.FileSystem;
824 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
825 prefix: "t3-acp-registry-runner-",
826 });
827 const toolchain = yield* makeFakeNpmToolchain(cacheDir);
828 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl }).pipe(
829 Effect.provideService(HostProcessEnvironment, toolchain.environment),
830 );
831 const prepared = yield* resolver.prepare({ agentId: agent.id });
832 expect(prepared).toEqual({
833 agentId: "example-agent",
834 version: "1.2.3",
835 distribution: "npx",
836 prepared: true,
837 });
838 expect(yield* fileSystem.exists(toolchain.executablePath)).toBe(true);
839 expect(yield* fileSystem.readFileString(toolchain.logPath)).toContain(
840 "install --global @example/acp@1.2.3",
841 );
842 expect(requests).toEqual([registryUrl]);
843 }).pipe(
844 Effect.scoped,
845 Effect.provide(
846 resolverLayer((request) => {
847 requests.push(request.url);
848 return Effect.succeed(
849 HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent))),
850 );
851 }, process.env),
852 ),
853 );
854 });
855
856 it.effect("falls back to a valid cached registry index when refresh fails", () => {
857 const agent = makeAgent({
858 npx: {
859 package: "@example/acp@1.2.3",
860 args: ["--stdio"],
861 },
862 });
863 return Effect.gen(function* () {
864 const fileSystem = yield* FileSystem.FileSystem;
865 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
866 prefix: "t3-acp-registry-cache-",
867 });
868 const registryDirectory = `${cacheDir}/acp-registry`;
869 yield* fileSystem.makeDirectory(registryDirectory, { recursive: true });
870 yield* fileSystem.writeFileString(`${registryDirectory}/registry.json`, makeRegistry(agent));
871 const toolchain = yield* makeFakeNpmToolchain(cacheDir);
872 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
873 const resolved = yield* resolver.resolve(settings(), "/workspace", toolchain.environment);
874
875 expect(resolved.spawn).toMatchObject({
876 command: toolchain.executablePath,
877 args: ["--stdio"],
878 });
879 }).pipe(
880 Effect.scoped,
881 Effect.provide(
882 resolverLayer((request) =>
883 Effect.succeed(
884 HttpClientResponse.fromWeb(request, new Response("unavailable", { status: 503 })),
885 ),
886 ),
887 ),
888 );
889 });
890
891 it.effect("rejects unsafe command paths before downloading an archive", () => {
892 const agent = makeAgent({
893 binary: {
894 "linux-x86_64": {
895 archive: archiveUrl,
896 cmd: "../outside",
897 },
898 },
899 });
900 const requests: Array<string> = [];
901 return Effect.gen(function* () {
902 const fileSystem = yield* FileSystem.FileSystem;
903 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
904 prefix: "t3-acp-registry-invalid-",
905 });
906 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
907 const error = yield* resolver.resolve(settings(), "/workspace").pipe(Effect.flip);
908
909 expect(error.reason).toBe("archive_invalid");
910 expect(requests).toEqual([registryUrl]);
911 }).pipe(
912 Effect.scoped,
913 Effect.provide(
914 resolverLayer((request) => {
915 requests.push(request.url);
916 return Effect.succeed(
917 HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent))),
918 );
919 }),
920 ),
921 );
922 });
923
924 it.effect("inspects from disk without waiting for a registry refresh", () => {
925 const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } });
926 let requests = 0;
927 return Effect.gen(function* () {
928 const fileSystem = yield* FileSystem.FileSystem;
929 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
930 prefix: "t3-acp-registry-cold-inspect-",
931 });
932 const registryDirectory = `${cacheDir}/acp-registry`;
933 yield* fileSystem.makeDirectory(registryDirectory, { recursive: true });
934 yield* fileSystem.writeFileString(`${registryDirectory}/registry.json`, makeRegistry(agent));
935
936 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
937 const inspection = yield* resolver.inspect(settings());
938
939 expect(inspection).toMatchObject({ status: "ready", agentId: agent.id });
940 expect(requests).toBe(0);
941 }).pipe(
942 Effect.scoped,
943 Effect.provide(
944 resolverLayer((request) => {
945 requests += 1;
946 return Effect.succeed(
947 HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent))),
948 );
949 }),
950 ),
951 );
952 });
953
954 it.effect("fails a cold inspection immediately when no local registry is available", () => {
955 let requests = 0;
956 return Effect.gen(function* () {
957 const fileSystem = yield* FileSystem.FileSystem;
958 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
959 prefix: "t3-acp-registry-empty-inspect-",
960 });
961 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
962 const error = yield* resolver.inspect(settings()).pipe(Effect.flip);
963
964 expect(error.reason).toBe("registry_unavailable");
965 expect(requests).toBe(0);
966 }).pipe(
967 Effect.scoped,
968 Effect.provide(
969 resolverLayer((request) => {
970 requests += 1;
971 return Effect.succeed(HttpClientResponse.fromWeb(request, new Response("unused")));
972 }),
973 ),
974 );
975 });
976
977 it.effect("uses the effective provider environment for inspection and resolution", () => {
978 const agent = makeAgent({ npx: { package: "@example/acp@1.2.3" } });
979 return Effect.gen(function* () {
980 const fileSystem = yield* FileSystem.FileSystem;
981 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
982 prefix: "t3-acp-registry-effective-env-",
983 });
984 const registryDirectory = `${cacheDir}/acp-registry`;
985 yield* fileSystem.makeDirectory(registryDirectory, { recursive: true });
986 yield* fileSystem.writeFileString(`${registryDirectory}/registry.json`, makeRegistry(agent));
987 const toolchain = yield* makeFakeNpmToolchain(cacheDir);
988
989 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
990 const hostInspection = yield* resolver.inspect(settings());
991 const providerEnvironment = toolchain.environment;
992 const instanceInspection = yield* resolver.inspect(settings(), providerEnvironment);
993 const resolved = yield* resolver.resolve(settings(), "/workspace", providerEnvironment);
994
995 expect(hostInspection).toMatchObject({ status: "missing_runner", runner: "npm" });
996 expect(instanceInspection).toMatchObject({ status: "ready", distribution: "npx" });
997 expect(resolved.spawn).toMatchObject({
998 command: toolchain.executablePath,
999 env: { PATH: expect.stringMatching(new RegExp(`^${toolchain.globalBin}:`, "u")) },
1000 });
1001 }).pipe(
1002 Effect.scoped,
1003 Effect.provide(
1004 resolverLayer(
1005 (request) =>
1006 Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))),
1007 { PATH: "" },
1008 ),
1009 ),
1010 );
1011 });
1012
1013 it.effect("requires a regular executable file in the managed binary cache", () => {
1014 const agent = makeAgent({
1015 binary: {
1016 "linux-x86_64": {
1017 archive: archiveUrl,
1018 cmd: "bin/example-agent",
1019 },
1020 },
1021 });
1022 return Effect.gen(function* () {
1023 const fileSystem = yield* FileSystem.FileSystem;
1024 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
1025 prefix: "t3-acp-registry-non-file-",
1026 });
1027 const registryDirectory = `${cacheDir}/acp-registry`;
1028 const fakeExecutable = `${registryDirectory}/agents/example-agent/1.2.3/linux-x86_64/bin/example-agent`;
1029 yield* fileSystem.makeDirectory(fakeExecutable, { recursive: true });
1030 yield* fileSystem.writeFileString(`${registryDirectory}/registry.json`, makeRegistry(agent));
1031
1032 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
1033 const directoryError = yield* resolver.inspect(settings()).pipe(Effect.flip);
1034
1035 expect(directoryError.reason).toBe("archive_invalid");
1036
1037 yield* fileSystem.remove(fakeExecutable, { recursive: true });
1038 yield* fileSystem.writeFileString(fakeExecutable, "#!/bin/sh\n");
1039 yield* fileSystem.chmod(fakeExecutable, 0o644);
1040 const modeError = yield* resolver.inspect(settings()).pipe(Effect.flip);
1041
1042 expect(modeError.reason).toBe("archive_invalid");
1043
1044 yield* fileSystem.chmod(fakeExecutable, 0o755);
1045 expect(yield* resolver.inspect(settings())).toMatchObject({ status: "ready" });
1046 }).pipe(
1047 Effect.scoped,
1048 Effect.provide(
1049 resolverLayer((request) =>
1050 Effect.succeed(HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent)))),
1051 ),
1052 ),
1053 );
1054 });
1055
1056 it.effect("uninstalls only the T3-managed binary tree and is idempotent", () => {
1057 return Effect.gen(function* () {
1058 const fileSystem = yield* FileSystem.FileSystem;
1059 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
1060 prefix: "t3-acp-registry-uninstall-",
1061 });
1062 const agentRoot = `${cacheDir}/acp-registry/agents/example-agent`;
1063 const runnerCache = `${cacheDir}/external-npx-cache/example-agent/package.json`;
1064 yield* fileSystem.makeDirectory(`${agentRoot}/1.2.3/linux-x86_64`, { recursive: true });
1065 yield* fileSystem.writeFileString(`${agentRoot}/1.2.3/linux-x86_64/agent`, "binary");
1066 yield* fileSystem.makeDirectory(`${cacheDir}/external-npx-cache/example-agent`, {
1067 recursive: true,
1068 });
1069 yield* fileSystem.writeFileString(runnerCache, "{}");
1070
1071 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
1072 const first = yield* resolver.uninstallManagedBinary({ agentId: "example-agent" });
1073 const second = yield* resolver.uninstallManagedBinary({ agentId: "example-agent" });
1074
1075 expect(first).toEqual({ agentId: "example-agent", removed: true });
1076 expect(second).toEqual({ agentId: "example-agent", removed: false });
1077 expect(yield* fileSystem.exists(agentRoot)).toBe(false);
1078 expect(yield* fileSystem.exists(runnerCache)).toBe(true);
1079 }).pipe(
1080 Effect.scoped,
1081 Effect.provide(
1082 resolverLayer((request) =>
1083 Effect.succeed(HttpClientResponse.fromWeb(request, new Response("unused"))),
1084 ),
1085 ),
1086 );
1087 });
1088
1089 it.effect("keeps a binary prepared by another client while an uninstall is waiting", () => {
1090 const agent = makeAgent({
1091 binary: {
1092 "linux-x86_64": {
1093 archive: archiveUrl,
1094 cmd: "./bin/example-agent",
1095 },
1096 },
1097 });
1098 const binaryBytes = new TextEncoder().encode("#!/bin/sh\necho example\n");
1099 return Effect.gen(function* () {
1100 const downloadStarted = yield* Deferred.make<void>();
1101 const releaseDownload = yield* Deferred.make<void>();
1102 const referenceChecked = yield* Deferred.make<void>();
1103 const isReferenced = yield* Ref.make(false);
1104 return yield* Effect.gen(function* () {
1105 const fileSystem = yield* FileSystem.FileSystem;
1106 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
1107 prefix: "t3-acp-registry-uninstall-race-",
1108 });
1109 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
1110
1111 const prepareFiber = yield* resolver
1112 .prepare({ agentId: agent.id })
1113 .pipe(Effect.forkChild({ startImmediately: true }));
1114 yield* Deferred.await(downloadStarted);
1115 const uninstallFiber = yield* resolver
1116 .uninstallManagedBinary(
1117 { agentId: agent.id },
1118 Deferred.succeed(referenceChecked, undefined).pipe(
1119 Effect.andThen(Ref.get(isReferenced)),
1120 ),
1121 )
1122 .pipe(Effect.forkChild({ startImmediately: true }));
1123
1124 expect(Option.isNone(yield* Deferred.poll(referenceChecked))).toBe(true);
1125 yield* Ref.set(isReferenced, true);
1126 yield* Deferred.succeed(releaseDownload, undefined);
1127
1128 expect(yield* Fiber.join(prepareFiber)).toMatchObject({ prepared: true });
1129 expect(yield* Fiber.join(uninstallFiber)).toEqual({
1130 agentId: agent.id,
1131 removed: false,
1132 });
1133 const agentRoot = `${cacheDir}/acp-registry/agents/${agent.id}`;
1134 expect(yield* fileSystem.exists(agentRoot)).toBe(true);
1135
1136 yield* Ref.set(isReferenced, false);
1137 expect(
1138 yield* resolver.uninstallManagedBinary({ agentId: agent.id }, Ref.get(isReferenced)),
1139 ).toEqual({ agentId: agent.id, removed: true });
1140 expect(
1141 yield* resolver.uninstallManagedBinary({ agentId: agent.id }, Ref.get(isReferenced)),
1142 ).toEqual({ agentId: agent.id, removed: false });
1143 }).pipe(
1144 Effect.scoped,
1145 Effect.provide(
1146 resolverLayer((request) => {
1147 if (request.url === registryUrl) {
1148 return Effect.succeed(
1149 HttpClientResponse.fromWeb(request, new Response(makeRegistry(agent))),
1150 );
1151 }
1152 return Deferred.succeed(downloadStarted, undefined).pipe(
1153 Effect.andThen(Deferred.await(releaseDownload)),
1154 Effect.as(
1155 HttpClientResponse.fromWeb(
1156 request,
1157 new Response(binaryBytes.buffer as ArrayBuffer),
1158 ),
1159 ),
1160 );
1161 }),
1162 ),
1163 );
1164 });
1165 });
1166
1167 it.effect("reserves a prepared binary until a configured instance inspects it", () => {
1168 const agent = makeAgent({
1169 binary: {
1170 "linux-x86_64": {
1171 archive: archiveUrl,
1172 cmd: "./bin/example-agent",
1173 },
1174 },
1175 });
1176 const binaryBytes = new TextEncoder().encode("#!/bin/sh\necho example\n");
1177 return Effect.gen(function* () {
1178 const fileSystem = yield* FileSystem.FileSystem;
1179 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
1180 prefix: "t3-acp-registry-uninstall-reservation-",
1181 });
1182 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
1183
1184 yield* resolver.prepare({ agentId: agent.id });
1185 expect(yield* resolver.uninstallManagedBinary({ agentId: agent.id })).toEqual({
1186 agentId: agent.id,
1187 removed: false,
1188 });
1189 expect(yield* resolver.inspect(settings())).toMatchObject({ status: "ready" });
1190 expect(yield* resolver.uninstallManagedBinary({ agentId: agent.id })).toEqual({
1191 agentId: agent.id,
1192 removed: true,
1193 });
1194 }).pipe(
1195 Effect.scoped,
1196 Effect.provide(
1197 resolverLayer((request) =>
1198 Effect.succeed(
1199 HttpClientResponse.fromWeb(
1200 request,
1201 request.url === registryUrl
1202 ? new Response(makeRegistry(agent))
1203 : new Response(binaryBytes.buffer as ArrayBuffer),
1204 ),
1205 ),
1206 ),
1207 ),
1208 );
1209 });
1210
1211 it.effect("expires an abandoned prepared-binary reservation", () => {
1212 const agent = makeAgent({
1213 binary: {
1214 "linux-x86_64": {
1215 archive: archiveUrl,
1216 cmd: "./bin/example-agent",
1217 },
1218 },
1219 });
1220 const binaryBytes = new TextEncoder().encode("#!/bin/sh\necho example\n");
1221 return Effect.gen(function* () {
1222 const fileSystem = yield* FileSystem.FileSystem;
1223 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
1224 prefix: "t3-acp-registry-uninstall-expiry-",
1225 });
1226 const resolver = yield* makeAcpRegistryCatalog({ cacheDir, registryUrl });
1227
1228 yield* resolver.prepare({ agentId: agent.id });
1229 yield* TestClock.adjust("31 seconds");
1230 expect(yield* resolver.uninstallManagedBinary({ agentId: agent.id })).toEqual({
1231 agentId: agent.id,
1232 removed: true,
1233 });
1234 }).pipe(
1235 Effect.scoped,
1236 Effect.provide(
1237 resolverLayer((request) =>
1238 Effect.succeed(
1239 HttpClientResponse.fromWeb(
1240 request,
1241 request.url === registryUrl
1242 ? new Response(makeRegistry(agent))
1243 : new Response(binaryBytes.buffer as ArrayBuffer),
1244 ),
1245 ),
1246 ),
1247 ),
1248 );
1249 });
1250});
1251
1252describe("acpRegistryManagedBinaryDirectories", () => {
1253 it.effect("lists global package and cached binary command directories", () =>
1254 Effect.gen(function* () {
1255 const fileSystem = yield* FileSystem.FileSystem;
1256 const path = yield* Path.Path;
1257 const cacheDir = yield* fileSystem.makeTempDirectoryScoped({
1258 prefix: "t3-acp-registry-bins-",
1259 });
1260 const install = (agent: string, version: string, target: string, commandDirectory = "") =>
1261 fileSystem.makeDirectory(
1262 path.join(cacheDir, "acp-registry", "agents", agent, version, target, commandDirectory),
1263 { recursive: true },
1264 );
1265 yield* install("kimi", "1.49.0", "linux-x86_64", "bin");
1266 yield* install("kimi", "1.50.0", "linux-x86_64", "cmd");
1267 yield* install("kimi", "1.50.0", "darwin-aarch64");
1268 yield* install("other-agent", "0.2.0", "darwin-aarch64");
1269 const globalBin = path.join(cacheDir, "global", "bin");
1270 const geminiCommand = path.join(globalBin, "gemini");
1271 const receiptsDirectory = path.join(cacheDir, "acp-registry", "package-installs");
1272 yield* fileSystem.makeDirectory(globalBin, { recursive: true });
1273 yield* fileSystem.makeDirectory(receiptsDirectory, { recursive: true });
1274 yield* fileSystem.writeFileString(geminiCommand, "#!/bin/sh\n");
1275 yield* fileSystem.chmod(geminiCommand, 0o755);
1276 yield* fileSystem.writeFileString(
1277 path.join(receiptsDirectory, "gemini.json"),
1278 encodeUnknownJson({
1279 agentId: "gemini",
1280 agentVersion: "0.56.0",
1281 distribution: "npx",
1282 packageSpec: "@google/gemini-cli@0.56.0",
1283 managerPath: "/usr/bin/npm",
1284 binDirectory: globalBin,
1285 executablePath: geminiCommand,
1286 packageRoot: path.join(
1287 cacheDir,
1288 "global",
1289 "lib",
1290 "node_modules",
1291 "@google",
1292 "gemini-cli",
1293 ),
1294 packageVersion: "0.56.0",
1295 }),
1296 );
1297 yield* fileSystem.writeFileString(
1298 path.join(cacheDir, "acp-registry", "registry.json"),
1299 encodeUnknownJson({
1300 version: "1.0.0",
1301 agents: [
1302 {
1303 ...makeAgent({
1304 binary: {
1305 "linux-x86_64": { archive: archiveUrl, cmd: "bin/kimi" },
1306 },
1307 }),
1308 id: "kimi",
1309 name: "Kimi",
1310 version: "1.49.0",
1311 },
1312 {
1313 ...makeAgent({
1314 binary: {
1315 "linux-x86_64": { archive: archiveUrl, cmd: "cmd/kimi" },
1316 },
1317 }),
1318 id: "kimi",
1319 name: "Kimi",
1320 version: "1.50.0",
1321 },
1322 ],
1323 }),
1324 );
1325
1326 const directories = yield* acpRegistryManagedBinaryDirectories({
1327 fileSystem,
1328 path,
1329 cacheDir,
1330 platform: "linux",
1331 architecture: "x64",
1332 });
1333 expect(directories).toEqual([
1334 globalBin,
1335 path.join(cacheDir, "acp-registry", "agents", "kimi", "1.50.0", "linux-x86_64", "cmd"),
1336 path.join(cacheDir, "acp-registry", "agents", "kimi", "1.49.0", "linux-x86_64", "bin"),
1337 ]);
1338
1339 const missing = yield* acpRegistryManagedBinaryDirectories({
1340 fileSystem,
1341 path,
1342 cacheDir: path.join(cacheDir, "does-not-exist"),
1343 platform: "linux",
1344 architecture: "x64",
1345 });
1346 expect(missing).toEqual([]);
1347 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
1348 );
1349});