Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodePath from "node:path";
3import * as NodeURL from "node:url";
4
5import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
6import * as NodeServices from "@effect/platform-node/NodeServices";
7import * as Layer from "effect/Layer";
8import { ProviderInstanceId } from "@t3tools/contracts";
9import { AcpRegistrySettings } from "@t3tools/provider-acp-registry/settings";
10import { describe, expect, it } from "@effect/vitest";
11import * as Effect from "effect/Effect";
12import * as Schema from "effect/Schema";
13import * as EffectAcpErrors from "effect-acp/errors";
14
15import type * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
16import {
17 acpRegistryProbeFailure,
18 acpRegistryProbeResult,
19 listAcpRegistrySessions,
20 logoutAcpRegistry,
21 normalizeAcpRegistryAuthMethods,
22 normalizeAcpRegistryCommands,
23} from "@t3tools/provider-acp-registry/testing";
24import * as AcpRegistrySupport from "@t3tools/provider-acp-registry/server/AcpRegistrySupport";
25
26const instanceId = ProviderInstanceId.make("acpRegistry_codex");
27const __dirname = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
28const mockAgentPath = NodePath.join(__dirname, "../../scripts/acp-mock-agent.ts");
29const decodeSettings = Schema.decodeSync(AcpRegistrySettings);
30
31describe("ACP Registry probe", () => {
32 it("returns advertised auth methods and de-duplicated models", () => {
33 const result = acpRegistryProbeResult(instanceId, {
34 sessionId: "probe-session",
35 initializeResult: {
36 protocolVersion: 1,
37 agentCapabilities: {
38 loadSession: true,
39 auth: { logout: {} },
40 sessionCapabilities: { list: {}, resume: {} },
41 },
42 authMethods: [
43 { id: "chatgpt", name: "ChatGPT" },
44 {
45 id: "api-key",
46 name: "API key",
47 type: "env_var",
48 vars: [{ name: "OPENAI_API_KEY", label: "OpenAI API key" }],
49 },
50 { id: "login", name: "Terminal login", type: "terminal" },
51 ],
52 },
53 sessionSetupResult: {
54 sessionId: "probe-session",
55 configOptions: [
56 {
57 id: "model",
58 name: "Model",
59 category: "model",
60 type: "select",
61 currentValue: "gpt-5.4",
62 options: [
63 { value: "gpt-5.4", name: "GPT-5.4" },
64 { value: "gpt-5.4", name: "Duplicate" },
65 ],
66 },
67 ],
68 },
69 modelConfigId: "model",
70 } satisfies AcpSessionRuntime.AcpSessionRuntimeStartResult);
71
72 expect(result).toEqual({
73 instanceId,
74 ready: true,
75 icon: null,
76 authMethods: [
77 { id: "chatgpt", name: "ChatGPT", description: null, type: "agent" },
78 {
79 id: "api-key",
80 name: "API key",
81 description: null,
82 type: "env_var",
83 envVarNames: ["OPENAI_API_KEY"],
84 },
85 { id: "login", name: "Terminal login", description: null, type: "terminal" },
86 ],
87 models: [{ id: "gpt-5.4", name: "GPT-5.4", description: null }],
88 currentModelId: "gpt-5.4",
89 configOptions: [],
90 sessionManagement: {
91 canList: true,
92 canLoad: true,
93 canResume: true,
94 canLogout: true,
95 canDelete: false,
96 canConfigureProviders: false,
97 },
98 });
99 });
100
101 it("composes runnable terminal auth commands from the resolved spawn recipe", () => {
102 const methods = normalizeAcpRegistryAuthMethods(
103 [
104 {
105 id: "login",
106 name: "Terminal login",
107 type: "terminal",
108 args: ["auth", "log in"],
109 env: { FORCE_TTY: "1" },
110 },
111 ],
112 { command: "/opt/agents/devin", args: ["--acp"] },
113 );
114
115 expect(methods).toEqual([
116 {
117 id: "login",
118 name: "Terminal login",
119 description: null,
120 type: "terminal",
121 command: "FORCE_TTY=1 /opt/agents/devin --acp auth 'log in'",
122 },
123 ]);
124 });
125
126 it("omits unsafe truncated auth commands instead of publishing broken shell", () => {
127 const methods = normalizeAcpRegistryAuthMethods(
128 [
129 {
130 id: "login",
131 name: "Terminal login",
132 type: "terminal",
133 args: ["x".repeat(2_048)],
134 },
135 ],
136 { command: "/opt/agents/devin", args: ["--acp"] },
137 );
138
139 expect(methods[0]).not.toHaveProperty("command");
140 });
141
142 it("omits overlong opaque model ids instead of publishing mutated ids", () => {
143 const result = acpRegistryProbeResult(instanceId, {
144 sessionId: "probe-session",
145 initializeResult: { protocolVersion: 1 },
146 sessionSetupResult: {
147 sessionId: "probe-session",
148 configOptions: [
149 {
150 id: "model",
151 name: "Model",
152 category: "model",
153 type: "select",
154 currentValue: "x".repeat(129),
155 options: [
156 { value: "x".repeat(129), name: "Too long" },
157 { value: "valid", name: "Valid" },
158 ],
159 },
160 ],
161 },
162 modelConfigId: "model",
163 } satisfies AcpSessionRuntime.AcpSessionRuntimeStartResult);
164
165 expect(result.models).toEqual([{ id: "valid", name: "Valid", description: null }]);
166 expect(result.currentModelId).toBeNull();
167 });
168
169 it("preserves grouped model values and advertises reasoning choices", () => {
170 const codexModel = '["codex","gpt-6.1-sol"]';
171 const goModel = '["opencode-go","deepseek-v4.1-flash"]';
172 const result = acpRegistryProbeResult(instanceId, {
173 sessionId: "probe-session",
174 initializeResult: { protocolVersion: 1 },
175 sessionSetupResult: {
176 sessionId: "probe-session",
177 configOptions: [
178 {
179 id: "model",
180 name: "Model",
181 category: "model",
182 type: "select",
183 currentValue: goModel,
184 options: [
185 {
186 groupId: "codex",
187 name: "Codex",
188 options: [{ value: codexModel, name: "GPT-6.1 Sol" }],
189 },
190 {
191 groupId: "opencode-go",
192 name: "OpenCode Go",
193 options: [{ value: goModel, name: "DeepSeek V4.1 Flash" }],
194 },
195 ],
196 },
197 {
198 id: "reasoning",
199 name: "Reasoning",
200 category: "thought_level",
201 type: "select",
202 currentValue: "high",
203 options: [
204 {
205 groupId: "effort",
206 name: "Effort",
207 options: [
208 { value: "medium", name: "Medium" },
209 { value: "high", name: "High" },
210 ],
211 },
212 ],
213 },
214 ],
215 },
216 modelConfigId: "model",
217 } satisfies AcpSessionRuntime.AcpSessionRuntimeStartResult);
218
219 expect(result.models).toEqual([
220 { id: codexModel, name: "GPT-6.1 Sol", description: null },
221 { id: goModel, name: "DeepSeek V4.1 Flash", description: null },
222 ]);
223 expect(result.currentModelId).toBe(goModel);
224 expect(result.configOptions).toEqual([
225 {
226 id: "reasoning",
227 label: "Reasoning",
228 type: "select",
229 currentValue: "high",
230 options: [
231 { id: "medium", label: "Medium" },
232 { id: "high", label: "High" },
233 ],
234 role: "reasoning",
235 },
236 ]);
237 });
238
239 it.each(["config option", "session model state"] as const)(
240 "retains the current model beyond the catalog cap from %s",
241 (source) => {
242 const models = Array.from({ length: 257 }, (_, index) => ({
243 modelId: `model-${index}`,
244 name: `Model ${index}`,
245 }));
246 const result = acpRegistryProbeResult(instanceId, {
247 sessionId: "probe-session",
248 initializeResult: { protocolVersion: 1 },
249 sessionSetupResult: {
250 sessionId: "probe-session",
251 ...(source === "config option"
252 ? {
253 configOptions: [
254 {
255 id: "model",
256 name: "Model",
257 category: "model",
258 type: "select",
259 currentValue: "model-256",
260 options: models.map(({ modelId, name }) => ({ value: modelId, name })),
261 },
262 ],
263 }
264 : { models: { currentModelId: "model-256", availableModels: models } }),
265 },
266 modelConfigId: source === "config option" ? "model" : undefined,
267 } satisfies AcpSessionRuntime.AcpSessionRuntimeStartResult);
268
269 expect(result.models).toHaveLength(256);
270 expect(result.models[0]?.id).toBe("model-0");
271 expect(result.models.at(-1)?.id).toBe("model-256");
272 expect(result.currentModelId).toBe("model-256");
273 },
274 );
275
276 it("keeps every distinct advertised command and de-duplicates the rest", () => {
277 const commands = normalizeAcpRegistryCommands([
278 {
279 name: "create_plan",
280 description: " Create a plan ",
281 input: { type: "text", hint: " topic " },
282 },
283 { name: "CREATE_PLAN", description: "duplicate" },
284 ...Array.from({ length: 140 }, (_, index) => ({
285 name: `command_${index}`,
286 description: "",
287 })),
288 { name: "$late-skill", description: "Advertised after 128 commands" },
289 ]);
290
291 expect(commands.slashCommands).toHaveLength(141);
292 expect(commands.slashCommands.at(-1)).toEqual({ name: "command_139" });
293 expect(commands.slashCommands[0]).toEqual({
294 name: "create_plan",
295 description: "Create a plan",
296 input: { hint: "topic" },
297 });
298 expect(
299 commands.slashCommands.filter((command) => command.name.toLowerCase() === "create_plan"),
300 ).toHaveLength(1);
301 expect(commands.skills.map((skill) => skill.name)).toEqual(["late-skill"]);
302 });
303
304 it("routes dollar-prefixed ACP commands to the provider skill menu", () => {
305 expect(
306 normalizeAcpRegistryCommands([
307 { name: "$workspace-skill", description: "Run the workspace skill" },
308 { name: "$WORKSPACE-SKILL", description: "duplicate" },
309 { name: "$", description: "empty" },
310 { name: "review", description: "Review changes" },
311 ]),
312 ).toEqual({
313 slashCommands: [{ name: "review", description: "Review changes" }],
314 skills: [
315 {
316 name: "workspace-skill",
317 description: "Run the workspace skill",
318 path: "acp://skill/workspace-skill",
319 scope: "agent",
320 enabled: true,
321 },
322 ],
323 });
324 });
325
326 it.effect("lists native sessions and logs out through generic ACP lifecycle methods", () => {
327 const catalog = AcpRegistrySupport.AcpRegistryCatalog.of({
328 search: () => Effect.die("unused search"),
329 prepare: () => Effect.die("unused prepare"),
330 inspect: () => Effect.die("unused inspect"),
331 uninstallManagedBinary: () => Effect.die("unused uninstall"),
332 resolve: () =>
333 Effect.succeed({
334 agent: {
335 id: "mock-agent",
336 name: "Mock Agent",
337 version: "1.0.0",
338 description: "ACP session management test agent",
339 distribution: { npx: { package: "mock-agent@1.0.0" } },
340 },
341 distribution: "npx" as const,
342 spawn: {
343 command: "node",
344 args: [mockAgentPath],
345 env: {
346 ...process.env,
347 T3_ACP_SESSION_LIFECYCLE: "1",
348 T3_ACP_AUTH_METHOD_ID: "mock-login",
349 },
350 },
351 }),
352 });
353 const input = {
354 instanceId,
355 settings: decodeSettings({ agentId: "mock-agent" }),
356 cwd: process.cwd(),
357 environment: process.env,
358 };
359
360 return Effect.gen(function* () {
361 const listed = yield* listAcpRegistrySessions(input);
362 expect(listed).toMatchObject({
363 canLoad: true,
364 canResume: true,
365 nextCursor: null,
366 sessions: [
367 {
368 sessionId: "mock-session-1",
369 cwd: process.cwd(),
370 importedThreadId: null,
371 },
372 ],
373 });
374 yield* logoutAcpRegistry(input);
375 }).pipe(
376 Effect.provideService(AcpRegistrySupport.AcpRegistryCatalog, catalog),
377 Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
378 Effect.scoped,
379 );
380 });
381
382 it("distinguishes authentication failures from generic probe failures", () => {
383 const advertised = normalizeAcpRegistryAuthMethods([
384 { id: "grok-login", name: "Log in with Grok" },
385 ]);
386 const cause = new EffectAcpErrors.AcpRequestError({
387 code: -32000,
388 errorMessage: "login required",
389 });
390 const authFailure = acpRegistryProbeFailure(cause, advertised);
391 expect(authFailure.reason).toBe("authentication_failed");
392 expect(authFailure.message).toBe("The ACP agent could not complete authentication.");
393 expect(authFailure.cause).toBe(cause);
394 expect(authFailure.authMethods).toEqual([
395 {
396 id: "grok-login",
397 name: "Log in with Grok",
398 description: null,
399 type: "agent",
400 },
401 ]);
402 expect(
403 acpRegistryProbeFailure(
404 new EffectAcpErrors.AcpTransportError({
405 detail: "Authentication required",
406 cause: "credentials missing",
407 }),
408 ).reason,
409 ).toBe("authentication_failed");
410 const genericCause = new EffectAcpErrors.AcpTransportError({
411 detail: "connection closed",
412 cause: "closed",
413 });
414 const genericFailure = acpRegistryProbeFailure(genericCause);
415 expect(genericFailure.reason).toBe("probe_failed");
416 expect(genericFailure.message).toBe("The ACP agent could not create a test session.");
417 expect(genericFailure.cause).toBe(genericCause);
418 expect(
419 acpRegistryProbeFailure(
420 new EffectAcpErrors.AcpTransportError({
421 detail: "Failed while logging request",
422 cause: "logging failed",
423 }),
424 ).reason,
425 ).toBe("probe_failed");
426 });
427});