Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AcpRegistryProbe.test.ts

536 lines16.9 KB

1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodePath from "node:path";
3import * as NodeURL from "node:url";
4​
5import * as NodeServices from "@effect/platform-node/NodeServices";
6import { AcpRegistrySettings, ProviderInstanceId } from "@t3tools/contracts";
7import { describe, expect, it } from "@effect/vitest";
8import * as Deferred from "effect/Deferred";
9import * as Effect from "effect/Effect";
10import * as Fiber from "effect/Fiber";
11import * as Schema from "effect/Schema";
12import * as TestClock from "effect/testing/TestClock";
13import * as EffectAcpErrors from "effect-acp/errors";
14​
15import type { AcpSessionRuntimeStartResult } from "./AcpSessionRuntime.ts";
16import {
17 acpRegistryProbeFailure,
18 acpRegistryProbeResult,
19 listAcpRegistrySessions,
20 logoutAcpRegistry,
21 normalizeAcpRegistryAuthMethods,
22 normalizeAcpRegistryCommands,
23 probeAcpRegistryConfiguration,
24} from "./AcpRegistryProbe.ts";
25import * as AcpRegistrySupport from "./AcpRegistrySupport.ts";
26​
27const instanceId = ProviderInstanceId.make("acpRegistry_codex");
28const __dirname = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
29const mockAgentPath = NodePath.join(__dirname, "../../../scripts/acp-mock-agent.ts");
30const decodeSettings = Schema.decodeSync(AcpRegistrySettings);
31​
32describe("ACP Registry probe", () => {
33 it("returns advertised auth methods and de-duplicated models", () => {
34 const result = acpRegistryProbeResult(instanceId, {
35 sessionId: "probe-session",
36 initializeResult: {
37 protocolVersion: 1,
38 agentCapabilities: {
39 loadSession: true,
40 auth: { logout: {} },
41 sessionCapabilities: { list: {}, resume: {} },
42 },
43 authMethods: [
44 { id: "chatgpt", name: "ChatGPT" },
45 {
46 id: "api-key",
47 name: "API key",
48 type: "env_var",
49 vars: [{ name: "OPENAI_API_KEY", label: "OpenAI API key" }],
50 },
51 { id: "login", name: "Terminal login", type: "terminal" },
52 ],
53 },
54 sessionSetupResult: {
55 sessionId: "probe-session",
56 configOptions: [
57 {
58 id: "model",
59 name: "Model",
60 category: "model",
61 type: "select",
62 currentValue: "gpt-5.4",
63 options: [
64 { value: "gpt-5.4", name: "GPT-5.4" },
65 { value: "gpt-5.4", name: "Duplicate" },
66 ],
67 },
68 ],
69 },
70 modelConfigId: "model",
71 } satisfies AcpSessionRuntimeStartResult);
72​
73 expect(result).toEqual({
74 instanceId,
75 ready: true,
76 icon: null,
77 authMethods: [
78 { id: "chatgpt", name: "ChatGPT", description: null, type: "agent" },
79 {
80 id: "api-key",
81 name: "API key",
82 description: null,
83 type: "env_var",
84 envVarNames: ["OPENAI_API_KEY"],
85 },
86 { id: "login", name: "Terminal login", description: null, type: "terminal" },
87 ],
88 models: [{ id: "gpt-5.4", name: "GPT-5.4", description: null }],
89 currentModelId: "gpt-5.4",
90 configOptions: [],
91 sessionManagement: {
92 canList: true,
93 canLoad: true,
94 canResume: true,
95 canLogout: true,
96 canDelete: false,
97 canConfigureProviders: false,
98 },
99 });
100 });
101​
102 it("composes runnable terminal auth commands from the resolved spawn recipe", () => {
103 const methods = normalizeAcpRegistryAuthMethods(
104 [
105 {
106 id: "login",
107 name: "Terminal login",
108 type: "terminal",
109 args: ["auth", "log in"],
110 env: { FORCE_TTY: "1" },
111 },
112 ],
113 { command: "/opt/agents/devin", args: ["--acp"] },
114 );
115​
116 expect(methods).toEqual([
117 {
118 id: "login",
119 name: "Terminal login",
120 description: null,
121 type: "terminal",
122 command: "FORCE_TTY=1 /opt/agents/devin --acp auth 'log in'",
123 },
124 ]);
125 });
126​
127 it("omits unsafe truncated auth commands instead of publishing broken shell", () => {
128 const methods = normalizeAcpRegistryAuthMethods(
129 [
130 {
131 id: "login",
132 name: "Terminal login",
133 type: "terminal",
134 args: ["x".repeat(2_048)],
135 },
136 ],
137 { command: "/opt/agents/devin", args: ["--acp"] },
138 );
139​
140 expect(methods[0]).not.toHaveProperty("command");
141 });
142​
143 it("omits overlong opaque auth method and environment variable ids", () => {
144 const methods = normalizeAcpRegistryAuthMethods([
145 { id: "x".repeat(129), name: "Invalid" },
146 {
147 id: "api-key",
148 name: "API key",
149 type: "env_var",
150 vars: [
151 { name: "y".repeat(129), label: "Invalid" },
152 { name: "OPENAI_API_KEY", label: "OpenAI API key" },
153 ],
154 },
155 ]);
156​
157 expect(methods).toEqual([
158 {
159 id: "api-key",
160 name: "API key",
161 description: null,
162 type: "env_var",
163 envVarNames: ["OPENAI_API_KEY"],
164 },
165 ]);
166 });
167​
168 it("falls back to model-category configuration options", () => {
169 const result = acpRegistryProbeResult(instanceId, {
170 sessionId: "probe-session",
171 initializeResult: { protocolVersion: 1 },
172 sessionSetupResult: {
173 sessionId: "probe-session",
174 configOptions: [
175 {
176 id: "model",
177 name: "Model",
178 category: "model",
179 type: "select",
180 currentValue: "sonnet",
181 options: [
182 { value: "sonnet", name: "Sonnet", description: "Balanced" },
183 { value: "haiku", name: "Haiku" },
184 ],
185 },
186 ],
187 },
188 modelConfigId: "model",
189 } satisfies AcpSessionRuntimeStartResult);
190​
191 expect(result.models).toEqual([
192 { id: "sonnet", name: "Sonnet", description: "Balanced" },
193 { id: "haiku", name: "Haiku", description: null },
194 ]);
195 expect(result.currentModelId).toBe("sonnet");
196 });
197​
198 it("omits overlong opaque model ids instead of publishing mutated ids", () => {
199 const result = acpRegistryProbeResult(instanceId, {
200 sessionId: "probe-session",
201 initializeResult: { protocolVersion: 1 },
202 sessionSetupResult: {
203 sessionId: "probe-session",
204 configOptions: [
205 {
206 id: "model",
207 name: "Model",
208 category: "model",
209 type: "select",
210 currentValue: "x".repeat(129),
211 options: [
212 { value: "x".repeat(129), name: "Too long" },
213 { value: "valid", name: "Valid" },
214 ],
215 },
216 ],
217 },
218 modelConfigId: "model",
219 } satisfies AcpSessionRuntimeStartResult);
220​
221 expect(result.models).toEqual([{ id: "valid", name: "Valid", description: null }]);
222 expect(result.currentModelId).toBeNull();
223 });
224​
225 it("omits a current model that falls outside the bounded model catalog", () => {
226 const result = acpRegistryProbeResult(instanceId, {
227 sessionId: "probe-session",
228 initializeResult: { protocolVersion: 1 },
229 sessionSetupResult: {
230 sessionId: "probe-session",
231 configOptions: [
232 {
233 id: "model",
234 name: "Model",
235 category: "model",
236 type: "select",
237 currentValue: "model-256",
238 options: Array.from({ length: 257 }, (_, index) => ({
239 value: `model-${index}`,
240 name: `Model ${index}`,
241 })),
242 },
243 ],
244 },
245 modelConfigId: "model",
246 } satisfies AcpSessionRuntimeStartResult);
247​
248 expect(result.models).toHaveLength(256);
249 expect(result.currentModelId).toBeNull();
250 });
251​
252 it("bounds and de-duplicates advertised commands", () => {
253 const commands = normalizeAcpRegistryCommands([
254 {
255 name: "create_plan",
256 description: " Create a plan ",
257 input: { type: "text", hint: " topic " },
258 },
259 { name: "CREATE_PLAN", description: "duplicate" },
260 ...Array.from({ length: 140 }, (_, index) => ({
261 name: `command_${index}`,
262 description: "",
263 })),
264 ]);
265​
266 expect(commands.slashCommands).toHaveLength(128);
267 expect(commands.slashCommands[0]).toEqual({
268 name: "create_plan",
269 description: "Create a plan",
270 input: { hint: "topic" },
271 });
272 expect(
273 commands.slashCommands.filter((command) => command.name.toLowerCase() === "create_plan"),
274 ).toHaveLength(1);
275 expect(commands.skills).toEqual([]);
276 });
277​
278 it("routes dollar-prefixed ACP commands to the provider skill menu", () => {
279 expect(
280 normalizeAcpRegistryCommands([
281 { name: "$workspace-skill", description: "Run the workspace skill" },
282 { name: "$WORKSPACE-SKILL", description: "duplicate" },
283 { name: "$", description: "empty" },
284 { name: "review", description: "Review changes" },
285 ]),
286 ).toEqual({
287 slashCommands: [{ name: "review", description: "Review changes" }],
288 skills: [
289 {
290 name: "workspace-skill",
291 description: "Run the workspace skill",
292 path: "acp://skill/workspace-skill",
293 scope: "agent",
294 enabled: true,
295 },
296 ],
297 });
298 });
299​
300 it.effect("captures commands advertised just after session creation", () =>
301 Effect.gen(function* () {
302 const result = yield* probeAcpRegistryConfiguration({
303 instanceId,
304 settings: decodeSettings({ agentId: "mock-agent" }),
305 cwd: process.cwd(),
306 environment: process.env,
307 });
308​
309 expect(result.slashCommands).toEqual([
310 {
311 name: "review",
312 description: "Review the current changes",
313 input: { hint: "focus" },
314 },
315 ]);
316 expect(result.skills).toEqual([
317 {
318 name: "workspace-skill",
319 description: "Run the workspace skill",
320 path: "acp://skill/workspace-skill",
321 scope: "agent",
322 enabled: true,
323 },
324 ]);
325 }).pipe(
326 Effect.provideService(
327 AcpRegistrySupport.AcpRegistryCatalog,
328 AcpRegistrySupport.AcpRegistryCatalog.of({
329 search: () => Effect.die("unused search"),
330 prepare: () => Effect.die("unused prepare"),
331 inspect: () => Effect.die("unused inspect"),
332 uninstallManagedBinary: () => Effect.die("unused uninstall"),
333 resolve: () =>
334 Effect.succeed({
335 agent: {
336 id: "mock-agent",
337 name: "Mock Agent",
338 version: "1.0.0",
339 description: "ACP probe test agent",
340 distribution: { npx: { package: "mock-agent@1.0.0" } },
341 },
342 distribution: "npx",
343 spawn: {
344 command: "node",
345 args: [mockAgentPath],
346 env: {
347 ...process.env,
348 T3_ACP_COMMAND_ADVERTISEMENT_DELAY_MS: "25",
349 },
350 },
351 }),
352 }),
353 ),
354 Effect.provide(NodeServices.layer),
355 Effect.scoped,
356 ),
357 );
358​
359 it.effect("includes package resolution in the probe timeout", () =>
360 Effect.gen(function* () {
361 const resolveStarted = yield* Deferred.make<void>();
362 const catalog = AcpRegistrySupport.AcpRegistryCatalog.of({
363 search: () => Effect.die("unused search"),
364 prepare: () => Effect.die("unused prepare"),
365 inspect: () => Effect.die("unused inspect"),
366 uninstallManagedBinary: () => Effect.die("unused uninstall"),
367 resolve: () =>
368 Deferred.succeed(resolveStarted, undefined).pipe(Effect.andThen(Effect.never)),
369 });
370 const probe = yield* probeAcpRegistryConfiguration({
371 instanceId,
372 settings: decodeSettings({ agentId: "slow-agent" }),
373 cwd: process.cwd(),
374 environment: process.env,
375 }).pipe(
376 Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, catalog),
377 Effect.flip,
378 Effect.forkChild({ startImmediately: true }),
379 );
380​
381 yield* Deferred.await(resolveStarted);
382 yield* TestClock.adjust("60 seconds");
383​
384 expect(yield* Fiber.join(probe)).toMatchObject({
385 reason: "probe_failed",
386 message: expect.stringContaining("within 60 seconds"),
387 });
388 }).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
389 );
390​
391 it.effect("lists native sessions and logs out through generic ACP lifecycle methods", () => {
392 const catalog = AcpRegistrySupport.AcpRegistryCatalog.of({
393 search: () => Effect.die("unused search"),
394 prepare: () => Effect.die("unused prepare"),
395 inspect: () => Effect.die("unused inspect"),
396 uninstallManagedBinary: () => Effect.die("unused uninstall"),
397 resolve: () =>
398 Effect.succeed({
399 agent: {
400 id: "mock-agent",
401 name: "Mock Agent",
402 version: "1.0.0",
403 description: "ACP session management test agent",
404 distribution: { npx: { package: "mock-agent@1.0.0" } },
405 },
406 distribution: "npx" as const,
407 spawn: {
408 command: "node",
409 args: [mockAgentPath],
410 env: {
411 ...process.env,
412 T3_ACP_SESSION_LIFECYCLE: "1",
413 T3_ACP_AUTH_METHOD_ID: "mock-login",
414 },
415 },
416 }),
417 });
418 const input = {
419 instanceId,
420 settings: decodeSettings({ agentId: "mock-agent" }),
421 cwd: process.cwd(),
422 environment: process.env,
423 };
424​
425 return Effect.gen(function* () {
426 const listed = yield* listAcpRegistrySessions(input);
427 expect(listed).toMatchObject({
428 canLoad: true,
429 canResume: true,
430 nextCursor: null,
431 sessions: [
432 {
433 sessionId: "mock-session-1",
434 cwd: process.cwd(),
435 importedThreadId: null,
436 },
437 ],
438 });
439 yield* logoutAcpRegistry(input);
440 }).pipe(
441 Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, catalog),
442 Effect.provide(NodeServices.layer),
443 Effect.scoped,
444 );
445 });
446​
447 it.effect("preserves the ACP method error for unsupported session management", () => {
448 const catalog = AcpRegistrySupport.AcpRegistryCatalog.of({
449 search: () => Effect.die("unused search"),
450 prepare: () => Effect.die("unused prepare"),
451 inspect: () => Effect.die("unused inspect"),
452 uninstallManagedBinary: () => Effect.die("unused uninstall"),
453 resolve: () =>
454 Effect.succeed({
455 agent: {
456 id: "mock-agent",
457 name: "Mock Agent",
458 version: "1.0.0",
459 description: "ACP unsupported management test agent",
460 distribution: { npx: { package: "mock-agent@1.0.0" } },
461 },
462 distribution: "npx" as const,
463 spawn: {
464 command: "node",
465 args: [mockAgentPath],
466 env: { ...process.env, T3_ACP_OMIT_SESSION_LIST_HANDLER: "1" },
467 },
468 }),
469 });
470​
471 return Effect.gen(function* () {
472 const failure = yield* listAcpRegistrySessions({
473 instanceId,
474 settings: decodeSettings({ agentId: "mock-agent" }),
475 cwd: process.cwd(),
476 environment: process.env,
477 }).pipe(Effect.flip);
478​
479 expect(failure).toMatchObject({
480 reason: "session_list_unsupported",
481 message: "The ACP agent does not advertise session listing.",
482 cause: { _tag: "AcpRequestError", code: -32601 },
483 });
484 }).pipe(
485 Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, catalog),
486 Effect.provide(NodeServices.layer),
487 Effect.scoped,
488 );
489 });
490​
491 it("distinguishes authentication failures from generic probe failures", () => {
492 const advertised = normalizeAcpRegistryAuthMethods([
493 { id: "grok-login", name: "Log in with Grok" },
494 ]);
495 const cause = new EffectAcpErrors.AcpRequestError({
496 code: -32000,
497 errorMessage: "login required",
498 });
499 const authFailure = acpRegistryProbeFailure(cause, advertised);
500 expect(authFailure.reason).toBe("authentication_failed");
501 expect(authFailure.message).toBe("The ACP agent could not complete authentication.");
502 expect(authFailure.cause).toBe(cause);
503 expect(authFailure.authMethods).toEqual([
504 {
505 id: "grok-login",
506 name: "Log in with Grok",
507 description: null,
508 type: "agent",
509 },
510 ]);
511 expect(
512 acpRegistryProbeFailure(
513 new EffectAcpErrors.AcpTransportError({
514 detail: "Authentication required",
515 cause: "credentials missing",
516 }),
517 ).reason,
518 ).toBe("authentication_failed");
519 const genericCause = new EffectAcpErrors.AcpTransportError({
520 detail: "connection closed",
521 cause: "closed",
522 });
523 const genericFailure = acpRegistryProbeFailure(genericCause);
524 expect(genericFailure.reason).toBe("probe_failed");
525 expect(genericFailure.message).toBe("The ACP agent could not create a test session.");
526 expect(genericFailure.cause).toBe(genericCause);
527 expect(
528 acpRegistryProbeFailure(
529 new EffectAcpErrors.AcpTransportError({
530 detail: "Failed while logging request",
531 cause: "logging failed",
532 }),
533 ).reason,
534 ).toBe("probe_failed");
535 });
536});