@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 12,259 lines 475 KB
Raw
1 import {
2 extractCopilotSubagentUpdate,
3 makeCopilotSubagentRouting,
4 normalizeDevinSessionUpdate,
5 normalizeDevinToolCall,
6 extractDevinSubagentUpdate,
7 makeAcpRegistryAdapterV2,
8 } from "@t3tools/provider-acp-registry/testing";
9 import { AcpRegistrySettings } from "@t3tools/provider-acp-registry/settings";
10 import { AcpRegistryCatalog } from "@t3tools/provider-acp-registry/server/AcpRegistrySupport";
11 import { decodeAcpReplayTranscript, makeAcpReplayRuntime } from "./AcpAdapterV2.testkit.ts";
12 // @effect-diagnostics nodeBuiltinImport:off
13 import * as NodeFS from "node:fs";
14 import * as NodeHttp from "node:http";
15 import * as NodePath from "node:path";
16
17 import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
18 import * as NodeServices from "@effect/platform-node/NodeServices";
19 import * as DocumentText from "../../provider/DocumentText.ts";
20 import { assert, beforeAll, describe, it } from "@effect/vitest";
21 import {
22 CheckpointId,
23 EnvironmentId,
24 type ModelSelection,
25 ProviderDriverKind,
26 ProviderInstanceId,
27 ProviderSessionId,
28 ThreadId,
29 type OrchestrationV2ProviderThread,
30 type OrchestrationV2TurnItem,
31 type OrchestrationV2UserInputQuestion,
32 type ProviderOptionDescriptor,
33 type ProviderUserInputAnswers,
34 } from "@t3tools/contracts";
35 import { GrokSettings } from "@t3tools/provider-grok/settings";
36 import * as HostProcess from "@t3tools/shared/HostProcess";
37 import { deriveBlockedOnThreadWait } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
38 import { resolveSelfInvocation } from "@t3tools/shared/nodeRuntime";
39 import * as DateTime from "effect/DateTime";
40 import * as Deferred from "effect/Deferred";
41 import * as Duration from "effect/Duration";
42 import * as Cause from "effect/Cause";
43 import * as Clock from "effect/Clock";
44 import * as Effect from "effect/Effect";
45 import * as Exit from "effect/Exit";
46 import * as FileSystem from "effect/FileSystem";
47 import * as Fiber from "effect/Fiber";
48 import * as Latch from "effect/Latch";
49 import * as Layer from "effect/Layer";
50 import * as Option from "effect/Option";
51 import * as Path from "effect/Path";
52 import * as Queue from "effect/Queue";
53 import * as Schema from "effect/Schema";
54 import * as Stream from "effect/Stream";
55 import * as Scope from "effect/Scope";
56 import * as TestClock from "effect/testing/TestClock";
57 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
58 import * as ChildProcess from "effect/process/ChildProcess";
59 import * as EffectAcpErrors from "effect-acp/errors";
60 import type * as EffectAcpProtocol from "effect-acp/protocol";
61 import type * as EffectAcpSchema from "effect-acp/compat";
62
63 import { writeFakeCli } from "@t3tools/provider-testing/fakeCli";
64 import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
65 import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
66 import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
67 import {
68 ACP_EMPTY_OPTION_VALUE,
69 ACP_SESSION_MODE_OPTION_ID,
70 acpProviderOptionDescriptors,
71 } from "@t3tools/provider-acp/server/sessionConfig";
72 import {
73 extractXAiAcpSubagentUpdate,
74 makeGrokAdapterV2,
75 makeXAiPromptCompletionRuntime,
76 normalizeXAiAcpToolCallState,
77 registerXAiBackgroundTaskTracking,
78 } from "@t3tools/provider-grok/testing";
79 import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
80 import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
81 import { makeTurnInput } from "./AcpAdapterV2.testkit.ts";
82 import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
83 import {
84 AcpProviderCapabilitiesV2,
85 acpPerTurnPromptUsage,
86 acpScopedNativeId,
87 acpPostSettleContinuationOfferEvidence,
88 acpPostSettleWakeEvidence,
89 acpToolCallDiffPatch,
90 acpToolCallSnippetEdit,
91 makeAcpAdapterV2,
92 type AcpAdapterV2ExtensionContext,
93 type AcpAdapterV2Flavor,
94 type AcpAdapterV2RuntimeInput,
95 type AcpAdapterV2SubagentUpdate,
96 } from "@t3tools/provider-acp/server/adapter";
97
98 import { appendUserInputAttachmentPaths } from "../../provider/userInputAttachments.ts";
99
100 import { makeGrokAcpAdapterFlavor } from "@t3tools/provider-grok/testing";
101 import {
102 acpRegistryPromptFailure,
103 registerMistralVibeAcpExtensions,
104 } from "@t3tools/provider-acp-registry/testing";
105
106 const DEFAULT_GROK_SETTINGS = Schema.decodeSync(GrokSettings)({});
107 const DROID_SETTINGS = Schema.decodeSync(AcpRegistrySettings)({ agentId: "factory-droid" });
108 const DEVIN_SETTINGS = Schema.decodeSync(AcpRegistrySettings)({ agentId: "devin" });
109 const OMP_SETTINGS = Schema.decodeSync(AcpRegistrySettings)({
110 source: "local",
111 commandPath: "/usr/local/bin/omp",
112 commandArgs: ["acp"],
113 });
114 const DROID_LOCAL_SETTINGS = Schema.decodeSync(AcpRegistrySettings)({
115 source: "local",
116 commandPath: "/opt/droid/bin/droid",
117 });
118 const OPENCODE_SETTINGS = Schema.decodeSync(AcpRegistrySettings)({
119 source: "local",
120 commandPath: "/usr/local/bin/opencode",
121 commandArgs: ["acp"],
122 });
123
124 const layerHost = TestProviderHost.layer().pipe(
125 Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
126 );
127
128 const layerTest = Layer.mergeAll(
129 DocumentText.layer.pipe(Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
130 NodeServices.layer,
131 NodeCrypto.layer,
132 IdAllocator.layer,
133 McpProviderSessions.layer,
134 layerHost,
135 );
136 // Node spends ~0.5s loading the agent scripts' TypeScript module graph on every spawn;
137 // a bundle built once per file starts in ~0.15s. Tests still talk to a real subprocess.
138 const bundledAgents = Deferred.makeUnsafe<{
139 readonly mock: string;
140 readonly replay: string;
141 readonly compileCache: string;
142 }>();
143 const bundledMockAgentPath = Deferred.await(bundledAgents).pipe(
144 Effect.map((agents) => agents.mock),
145 );
146 const bundledReplayAgentPath = Deferred.await(bundledAgents).pipe(
147 Effect.map((agents) => agents.replay),
148 );
149 const bundleAgents = Effect.gen(function* () {
150 const fileSystem = yield* FileSystem.FileSystem;
151 const path = yield* Path.Path;
152 const outDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-acp-agents-" });
153 const input = {
154 mock: yield* path.fromFileUrl(new URL("../../../scripts/acp-mock-agent.ts", import.meta.url)),
155 replay: yield* path.fromFileUrl(
156 new URL("../../../scripts/acp-replay-agent.ts", import.meta.url),
157 ),
158 };
159 const { build } = yield* Effect.promise(() => import("vite-plus"));
160 yield* Effect.promise(() =>
161 build({
162 configFile: false,
163 logLevel: "silent",
164 root: path.dirname(input.mock),
165 ssr: { noExternal: true },
166 build: {
167 ssr: true,
168 outDir,
169 rollupOptions: { input, output: { format: "es", entryFileNames: "[name].mjs" } },
170 },
171 }),
172 );
173 yield* Deferred.succeed(bundledAgents, {
174 mock: path.join(outDir, "mock.mjs"),
175 replay: path.join(outDir, "replay.mjs"),
176 compileCache: path.join(outDir, "compile-cache"),
177 });
178 }).pipe(Effect.provide(NodeServices.layer));
179
180 const ACP_TEST_DRIVER = ProviderDriverKind.make("acp-test");
181 const decodeUnknownJson = Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Unknown));
182
183 describe("acpPerTurnPromptUsage", () => {
184 it("maps a per-turn sample straight onto TurnTokenUsage", () => {
185 assert.deepEqual(
186 acpPerTurnPromptUsage(
187 {
188 inputTokens: 2453,
189 outputTokens: 4,
190 totalTokens: 2457,
191 cachedReadTokens: 512,
192 cachedWriteTokens: 32,
193 thoughtTokens: 3,
194 },
195 false,
196 "completed",
197 ),
198 {
199 usageScope: "main_agent",
200 usageStatus: "complete",
201 hasSubagents: false,
202 inputTokens: 2453,
203 outputTokens: 4,
204 cachedInputTokens: 512,
205 cacheCreationTokens: 32,
206 reasoningTokens: 3,
207 },
208 );
209 // Antigravity 1.5+: inputTokens excludes cached reads, while outputTokens already includes thinking.
210 assert.deepEqual(
211 acpPerTurnPromptUsage(
212 {
213 inputTokens: 2181,
214 outputTokens: 23,
215 totalTokens: 14323,
216 cachedReadTokens: 12119,
217 cachedWriteTokens: 0,
218 thoughtTokens: 19,
219 },
220 false,
221 "completed",
222 true,
223 ),
224 {
225 usageScope: "main_agent",
226 usageStatus: "complete",
227 hasSubagents: false,
228 inputTokens: 14300,
229 outputTokens: 23,
230 cachedInputTokens: 12119,
231 cacheCreationTokens: 0,
232 reasoningTokens: 19,
233 },
234 );
235 });
236
237 it("clamps subsets that exceed their parents and drops unset counters", () => {
238 assert.deepEqual(
239 acpPerTurnPromptUsage(
240 {
241 inputTokens: 100,
242 outputTokens: 10,
243 totalTokens: 110,
244 cachedReadTokens: 150,
245 cachedWriteTokens: 120,
246 thoughtTokens: 30,
247 },
248 false,
249 "completed",
250 ),
251 {
252 usageScope: "main_agent",
253 usageStatus: "complete",
254 hasSubagents: false,
255 inputTokens: 100,
256 outputTokens: 10,
257 cachedInputTokens: 100,
258 cacheCreationTokens: 0,
259 reasoningTokens: 10,
260 },
261 );
262 assert.deepEqual(
263 acpPerTurnPromptUsage(
264 { inputTokens: 100, outputTokens: 10, totalTokens: 110 },
265 false,
266 "completed",
267 ),
268 {
269 usageScope: "main_agent",
270 usageStatus: "complete",
271 hasSubagents: false,
272 inputTokens: 100,
273 outputTokens: 10,
274 },
275 );
276 });
277
278 it("caps cache writes at the input left over after cache reads", () => {
279 // Each counter fits under input alone, but together they exceed it.
280 assert.deepEqual(
281 acpPerTurnPromptUsage(
282 {
283 inputTokens: 100,
284 outputTokens: 10,
285 totalTokens: 110,
286 cachedReadTokens: 80,
287 cachedWriteTokens: 80,
288 },
289 false,
290 "completed",
291 ),
292 {
293 usageScope: "main_agent",
294 usageStatus: "complete",
295 hasSubagents: false,
296 inputTokens: 100,
297 outputTokens: 10,
298 cachedInputTokens: 80,
299 cacheCreationTokens: 20,
300 },
301 );
302 });
303
304 it("marks present usage partial on a non-completed terminal and missing usage unavailable", () => {
305 assert.deepEqual(
306 acpPerTurnPromptUsage(
307 { inputTokens: 100, outputTokens: 10, totalTokens: 110 },
308 true,
309 "interrupted",
310 ),
311 {
312 usageScope: "main_agent",
313 usageStatus: "partial",
314 hasSubagents: true,
315 inputTokens: 100,
316 outputTokens: 10,
317 },
318 );
319 assert.deepEqual(acpPerTurnPromptUsage(undefined, false, "completed"), {
320 usageScope: "main_agent",
321 usageStatus: "unavailable",
322 hasSubagents: false,
323 });
324 });
325 });
326
327 describe("acpToolCallDiffPatch", () => {
328 it("keeps an unplaced snippet as recorded text instead of numbering it from line 1", () => {
329 const content = [
330 {
331 type: "diff",
332 path: "/repo/a.ts",
333 oldText: "const target = 10;",
334 newText: "const target = 100;",
335 patch: { format: "snippet", text: "@@ -1,1 +1,1 @@ snippet, line unknown\n" },
336 },
337 ];
338 assert.isUndefined(acpToolCallDiffPatch(content));
339 // An omitted preview is not re-diffed from the bare snippets either.
340 assert.isUndefined(acpToolCallDiffPatch([{ ...content[0], patch: { format: "omitted" } }]));
341 // A status block beside the snippet must not hide it.
342 for (const blocks of [
343 content,
344 [{ type: "content", content: { type: "text", text: "Edited." } }, ...content],
345 ]) {
346 assert.isUndefined(acpToolCallDiffPatch(blocks));
347 assert.deepEqual(acpToolCallSnippetEdit(blocks), {
348 oldStr: "const target = 10;",
349 newStr: "const target = 100;",
350 });
351 }
352 // Beside another diff, the snippet is neither a patch nor a placed edit.
353 const twoDiffs = [
354 ...content,
355 {
356 type: "diff",
357 path: "/repo/b.ts",
358 oldText: "b\n",
359 newText: "B\n",
360 patch: { text: "@@ -1 +1 @@\n-b\n+B\n" },
361 },
362 ];
363 assert.isUndefined(acpToolCallDiffPatch(twoDiffs));
364 assert.isUndefined(acpToolCallSnippetEdit(twoDiffs));
365 });
366
367 it("builds per-file patches from ACP hunks and recorded old/new text", () => {
368 for (const operation of ["add", "delete"]) {
369 const text = operation === "add" ? "@@ -0,0 +1 @@\n+content\n" : "@@ -1 +0,0 @@\n-content\n";
370 assert.equal(
371 acpToolCallDiffPatch([
372 {
373 type: "diff",
374 changes: [{ operation, path: "/repo/a.ts" }],
375 patch: { format: "git_patch", text },
376 },
377 ]),
378 [
379 'diff --git "a//repo/a.ts" "b//repo/a.ts"',
380 operation === "add" ? "--- /dev/null" : '--- "a//repo/a.ts"',
381 operation === "delete" ? "+++ /dev/null" : '+++ "b//repo/a.ts"',
382 text,
383 ].join("\n"),
384 );
385 }
386 assert.equal(
387 acpToolCallDiffPatch([
388 { type: "diff", path: "/repo/new.txt", oldText: null, newText: "hello\n" },
389 { type: "diff", path: "/repo/a.ts", oldText: "a\nb\nc\n", newText: "a\nB\nc\n" },
390 { type: "diff", path: "/repo/same.ts", oldText: "x\n", newText: "x\n" },
391 ]),
392 [
393 "--- /dev/null",
394 "+++ /repo/new.txt",
395 "@@ -0,0 +1,1 @@",
396 "+hello",
397 "",
398 "--- /repo/a.ts",
399 "+++ /repo/a.ts",
400 "@@ -1,3 +1,3 @@",
401 " a",
402 "-b",
403 "+B",
404 " c",
405 "",
406 ].join("\n"),
407 );
408 assert.equal(
409 acpToolCallDiffPatch([
410 {
411 type: "diff",
412 changes: [{ operation: "modify", path: "/repo/a.ts" }],
413 patch: {
414 format: "git_patch",
415 text: "diff --git a/repo/a.ts b/repo/a.ts\n@@ -1 +1 @@\n-a\n+A\n",
416 },
417 },
418 {
419 type: "diff",
420 changes: [{ operation: "modify", path: "/repo/b.ts" }],
421 patch: {
422 format: "git_patch",
423 text: "diff --git a/repo/b.ts b/repo/b.ts\n@@ -1 +1 @@\n-b\n+B\n",
424 },
425 },
426 { type: "diff", path: "/repo/c.ts", oldText: "c\n", newText: "C\n" },
427 ]),
428 [
429 "diff --git a/repo/a.ts b/repo/a.ts",
430 "@@ -1 +1 @@",
431 "-a",
432 "+A",
433 "",
434 "diff --git a/repo/b.ts b/repo/b.ts",
435 "@@ -1 +1 @@",
436 "-b",
437 "+B",
438 "",
439 'diff --git "a//repo/c.ts" "b//repo/c.ts"',
440 "--- /repo/c.ts",
441 "+++ /repo/c.ts",
442 "@@ -1,1 +1,1 @@",
443 "-c",
444 "+C",
445 "",
446 ].join("\n"),
447 );
448 assert.equal(
449 acpToolCallDiffPatch([
450 { type: "diff", path: "/repo/new.txt", oldText: null, newText: "hello\n" },
451 {
452 type: "diff",
453 changes: [{ operation: "modify", path: "/repo/a.ts" }],
454 patch: { format: "git_patch", text: "@@ -1 +1 @@\n-a\n+A\n" },
455 },
456 ]),
457 [
458 'diff --git "a//repo/new.txt" "b//repo/new.txt"',
459 "--- /dev/null",
460 "+++ /repo/new.txt",
461 "@@ -0,0 +1,1 @@",
462 "+hello",
463 "",
464 'diff --git "a//repo/a.ts" "b//repo/a.ts"',
465 '--- "a//repo/a.ts"',
466 '+++ "b//repo/a.ts"',
467 "@@ -1 +1 @@",
468 "-a",
469 "+A",
470 "",
471 ].join("\n"),
472 );
473 });
474 });
475
476 function makeMockRuntime(input: {
477 readonly childProcessSpawner: ChildProcessSpawner.ChildProcessSpawner["Service"];
478 readonly mockAgentPath: string;
479 readonly environment?:
480 | Readonly<Record<string, string>>
481 | ((runtimeOrdinal: number) => Readonly<Record<string, string>>);
482 readonly protocolEvents?: Queue.Queue<EffectAcpProtocol.AcpProtocolLogEvent>;
483 readonly cancelBehavior?: AcpSessionRuntime.AcpSessionRuntimeOptions["cancelBehavior"];
484 // Also opts into replay-idle readiness, which runtimes leave off by default.
485 readonly sessionLoadReplayIdleGap?: Duration.Input;
486 readonly ownDescendantProcessGroups?: boolean;
487 readonly ownDetachedProcessGroup?: boolean;
488 readonly processGroupPlatform?: NodeJS.Platform;
489 readonly processGroupTerminationGrace?: Duration.Input;
490 readonly linuxCgroupController?: AcpSessionRuntime.AcpSessionRuntimeOptions["linuxCgroupController"];
491 readonly posixProcessTreeController?: AcpSessionRuntime.AcpSessionRuntimeOptions["posixProcessTreeController"];
492 readonly windowsProcessTreeTerminator?: AcpSessionRuntime.AcpSessionRuntimeOptions["windowsProcessTreeTerminator"];
493 readonly wrapCancel?: (
494 cancel: AcpSessionRuntime.AcpSessionRuntime["Service"]["cancel"],
495 ) => AcpSessionRuntime.AcpSessionRuntime["Service"]["cancel"];
496 readonly wrapOutgoingResponse?: (
497 onOutgoingResponse: NonNullable<AcpAdapterV2RuntimeInput["onOutgoingResponse"]>,
498 ) => NonNullable<AcpAdapterV2RuntimeInput["onOutgoingResponse"]>;
499 readonly wrapRuntime?: (
500 runtime: AcpSessionRuntime.AcpSessionRuntime["Service"],
501 runtimeOrdinal: number,
502 ) => AcpSessionRuntime.AcpSessionRuntime["Service"];
503 }): AcpAdapterV2Flavor["makeRuntime"] {
504 let runtimeOrdinal = 0;
505 return (runtimeInput) =>
506 Effect.gen(function* () {
507 runtimeOrdinal += 1;
508 const protocolEvents = input.protocolEvents;
509 const protocolLogging =
510 protocolEvents === undefined
511 ? runtimeInput.protocolLogging
512 : {
513 ...runtimeInput.protocolLogging,
514 logIncoming: true,
515 logOutgoing: true,
516 logger: (event: EffectAcpProtocol.AcpProtocolLogEvent) =>
517 Queue.offer(protocolEvents, event).pipe(
518 Effect.andThen(runtimeInput.protocolLogging.logger?.(event) ?? Effect.void),
519 Effect.asVoid,
520 ),
521 };
522 const context = yield* Layer.build(
523 AcpSessionRuntime.layer({
524 ...runtimeInput,
525 ...(input.cancelBehavior === undefined ? {} : { cancelBehavior: input.cancelBehavior }),
526 ...(input.sessionLoadReplayIdleGap === undefined
527 ? {}
528 : {
529 sessionLoadReplayIdleGap: input.sessionLoadReplayIdleGap,
530 sessionLoadReplayIdleReadiness: true,
531 }),
532 ...(input.ownDetachedProcessGroup === undefined
533 ? {}
534 : { ownDetachedProcessGroup: input.ownDetachedProcessGroup }),
535 ...(input.ownDescendantProcessGroups === undefined
536 ? {}
537 : { ownDescendantProcessGroups: input.ownDescendantProcessGroups }),
538 ...(input.ownDetachedProcessGroup === true
539 ? { processGroupPlatform: input.processGroupPlatform ?? "linux" }
540 : {}),
541 ...(input.processGroupTerminationGrace === undefined
542 ? {}
543 : { processGroupTerminationGrace: input.processGroupTerminationGrace }),
544 ...(input.linuxCgroupController === undefined
545 ? {}
546 : { linuxCgroupController: input.linuxCgroupController }),
547 ...(input.posixProcessTreeController === undefined
548 ? {}
549 : { posixProcessTreeController: input.posixProcessTreeController }),
550 ...(input.windowsProcessTreeTerminator === undefined
551 ? {}
552 : { windowsProcessTreeTerminator: input.windowsProcessTreeTerminator }),
553 protocolLogging,
554 spawn: {
555 command: process.execPath,
556 args: [input.mockAgentPath],
557 cwd: runtimeInput.cwd,
558 env: {
559 T3_ACP_SESSION_LIFECYCLE: "1",
560 NODE_COMPILE_CACHE: (yield* Deferred.await(bundledAgents)).compileCache,
561 ...(typeof input.environment === "function"
562 ? input.environment(runtimeOrdinal)
563 : input.environment),
564 },
565 },
566 authMethodId: "test",
567 ...(input.wrapOutgoingResponse === undefined ||
568 runtimeInput.onOutgoingResponse === undefined
569 ? {}
570 : {
571 onOutgoingResponse: input.wrapOutgoingResponse(runtimeInput.onOutgoingResponse),
572 }),
573 }).pipe(
574 Layer.provide(
575 Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, input.childProcessSpawner),
576 ),
577 ),
578 );
579 const runtime = yield* Effect.service(AcpSessionRuntime.AcpSessionRuntime).pipe(
580 Effect.provide(context),
581 );
582 const wrapped = {
583 ...runtime,
584 ...(input.wrapCancel === undefined ? {} : { cancel: input.wrapCancel(runtime.cancel) }),
585 };
586 return input.wrapRuntime?.(wrapped, runtimeOrdinal) ?? wrapped;
587 });
588 }
589
590 it.effect("detaches owned ACP processes on POSIX hosts but not on Windows", () =>
591 Effect.gen(function* () {
592 for (const [platform, expected] of [
593 ["win32", false],
594 ["linux", true],
595 ] as const) {
596 let detached: boolean | undefined;
597 yield* AcpSessionRuntime.make({
598 spawn: { command: "acp-agent", args: [] },
599 cwd: process.cwd(),
600 clientInfo: { name: "test", version: "1" },
601 ownDetachedProcessGroup: true,
602 processGroupPlatform: platform,
603 }).pipe(
604 Effect.provideService(HostProcess.Platform, platform),
605 Effect.provideService(
606 ChildProcessSpawner.ChildProcessSpawner,
607 ChildProcessSpawner.make((command) => {
608 if (ChildProcess.isStandardCommand(command)) detached = command.options.detached;
609 return Effect.die("spawn stopped by test");
610 }),
611 ),
612 Effect.scoped,
613 Effect.exit,
614 );
615 assert.equal(detached, expected, platform);
616 }
617 }).pipe(Effect.provide(layerTest)),
618 );
619
620 const isProviderAdapterProtocolError = Schema.is(ProviderAdapter.ProviderAdapterProtocolError);
621
622 const makePromptInactivityHarness = Effect.fnUntraced(function* (input: {
623 readonly environment: (runtimeOrdinal: number) => Readonly<Record<string, string>>;
624 readonly runtimeMode?: "full-access" | "approval-required";
625 readonly wrapPrompt?: (
626 prompt: AcpSessionRuntime.AcpSessionRuntime["Service"]["prompt"],
627 ) => AcpSessionRuntime.AcpSessionRuntime["Service"]["prompt"];
628 readonly cancelled?: Deferred.Deferred<void>;
629 readonly followAgentWorkState?: boolean;
630 readonly sessionScope?: Scope.Scope;
631 }) {
632 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
633 const natives: Array<AcpSessionRuntime.AcpSessionRuntime["Service"]> = [];
634 const instanceId = ProviderInstanceId.make("acp-prompt-inactivity");
635 const adapter = yield* makeAcpAdapterV2({
636 instanceId,
637 flavor: {
638 driver: ACP_TEST_DRIVER,
639 capabilities: AcpProviderCapabilitiesV2,
640 promptInactivityTimeout: "15 minutes",
641 ...(input.followAgentWorkState === true ? { followAgentWorkState: true } : {}),
642 makeRuntime: makeMockRuntime({
643 childProcessSpawner,
644 mockAgentPath: yield* bundledMockAgentPath,
645 environment: input.environment,
646 wrapRuntime: (runtime) => {
647 natives.push(runtime);
648 return {
649 ...runtime,
650 prompt: input.wrapPrompt?.(runtime.prompt) ?? runtime.prompt,
651 cancel:
652 input.cancelled === undefined
653 ? runtime.cancel
654 : Deferred.succeed(input.cancelled, undefined).pipe(Effect.andThen(runtime.cancel)),
655 };
656 },
657 }),
658 },
659 selfInvocation: yield* resolveSelfInvocation(),
660 ...(input.followAgentWorkState === true
661 ? { continuationRequests: { offer: () => Effect.void } }
662 : {}),
663 });
664 const threadId = ThreadId.make("thread-acp-prompt-inactivity");
665 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
666 runtimeMode: input.runtimeMode ?? "full-access",
667 interactionMode: "default",
668 cwd: process.cwd(),
669 });
670 const modelSelection = { instanceId, model: "default" } as const;
671 const opened = adapter.openSession({
672 threadId,
673 providerSessionId: ProviderSessionId.make("provider-session-acp-prompt-inactivity"),
674 modelSelection,
675 runtimePolicy,
676 });
677 const runtime = yield* input.sessionScope === undefined
678 ? opened
679 : opened.pipe(Effect.provideService(Scope.Scope, input.sessionScope));
680 const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
681 const startTurn = (ordinal: number) =>
682 DateTime.now.pipe(
683 Effect.flatMap((now) =>
684 runtime.startTurn(
685 makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal }),
686 ),
687 ),
688 );
689 return {
690 runtime,
691 natives,
692 providerThread,
693 startTurn,
694 /** A turn still running makes the next start fail, without waiting on time. */
695 nextTurnRejected: (ordinal: number) =>
696 startTurn(ordinal).pipe(
697 Effect.flip,
698 Effect.map(
699 (error) =>
700 isProviderAdapterProtocolError(error.cause) &&
701 error.cause.reason === "previous_turn_active",
702 ),
703 Effect.catchCause(() => Effect.succeed(false)),
704 ),
705 emitUpdate: (notification: EffectAcpSchema.SessionNotification) =>
706 natives.at(-1)!.request("_test/emit-update", notification),
707 terminals: (count: number) =>
708 runtime.events.pipe(
709 Stream.filter((event) => event.type === "turn.terminal"),
710 Stream.take(count),
711 Stream.runCollect,
712 Effect.map((events) =>
713 Array.from(events, (event) => {
714 if (event.type !== "turn.terminal") throw new Error("Expected a turn terminal");
715 return event;
716 }),
717 ),
718 ),
719 };
720 });
721
722 function rawProtocolMethod(event: EffectAcpProtocol.AcpProtocolLogEvent): string | undefined {
723 if (event.stage !== "raw" || typeof event.payload !== "string") return undefined;
724 for (const line of event.payload.split("\n")) {
725 const trimmed = line.trim();
726 if (trimmed.length === 0) continue;
727 const decoded = Option.getOrUndefined(decodeUnknownJson(trimmed));
728 if (typeof decoded === "object" && decoded !== null && "method" in decoded) {
729 const method = (decoded as { readonly method?: unknown }).method;
730 if (typeof method === "string") return method;
731 }
732 }
733 return undefined;
734 }
735
736 function rawProtocolRequest(
737 event: EffectAcpProtocol.AcpProtocolLogEvent,
738 ): { readonly method?: unknown; readonly params?: unknown } | undefined {
739 if (event.stage !== "raw" || typeof event.payload !== "string") return undefined;
740 for (const line of event.payload.split("\n")) {
741 const trimmed = line.trim();
742 if (trimmed.length === 0) continue;
743 const decoded = Option.getOrUndefined(decodeUnknownJson(trimmed));
744 if (typeof decoded === "object" && decoded !== null && "method" in decoded) {
745 return decoded;
746 }
747 }
748 return undefined;
749 }
750
751 function rawProtocolRequestParam(
752 event: EffectAcpProtocol.AcpProtocolLogEvent,
753 key: string,
754 ): unknown {
755 const params = rawProtocolRequest(event)?.params;
756 return typeof params === "object" && params !== null ? Reflect.get(params, key) : undefined;
757 }
758
759 function rawProtocolPromptTextBlocks(
760 event: EffectAcpProtocol.AcpProtocolLogEvent,
761 ): Array<{ type: "text"; text: string }> {
762 const prompt = rawProtocolRequestParam(event, "prompt");
763 if (!Array.isArray(prompt)) return [];
764 return prompt.flatMap((block) =>
765 typeof block === "object" &&
766 block !== null &&
767 Reflect.get(block, "type") === "text" &&
768 typeof Reflect.get(block, "text") === "string"
769 ? [{ type: "text" as const, text: Reflect.get(block, "text") as string }]
770 : [],
771 );
772 }
773
774 const pollProtocolMethods = (events: Queue.Queue<EffectAcpProtocol.AcpProtocolLogEvent>) =>
775 Effect.gen(function* () {
776 const methods: string[] = [];
777 let polled = 0;
778 let event = yield* Queue.poll(events);
779 while (Option.isSome(event) && polled < 256) {
780 polled += 1;
781 const method = rawProtocolMethod(event.value);
782 if (method !== undefined) methods.push(method);
783 event = yield* Queue.poll(events);
784 }
785 return methods;
786 });
787
788 const makeInstructionCompactionHarness = Effect.fnUntraced(function* (
789 environment: Readonly<Record<string, string>>,
790 prepareAttachments = false,
791 ) {
792 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
793 const selfInvocation = yield* resolveSelfInvocation();
794 const mockAgentPath = yield* bundledMockAgentPath;
795 const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
796 const instanceId = ProviderInstanceId.make("acp-test-instruction-compaction");
797 const threadId = ThreadId.make("thread-acp-instruction-compaction");
798 yield* (yield* McpProviderSessions.McpProviderSessions).set({
799 environmentId: EnvironmentId.make("environment-acp-instruction-compaction"),
800 threadId,
801 providerSessionId: "mcp-session-acp-instruction-compaction",
802 providerInstanceId: instanceId,
803 endpoint: "http://127.0.0.1:43123/mcp",
804 authorizationHeader: "Bearer instruction-compaction-token",
805 browserToolsAvailable: true,
806 });
807 type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"];
808 let sessionUpdate: Parameters<Runtime["handleSessionUpdate"]>[0] | undefined;
809 let copilotSessionEvent:
810 | ((event: unknown) => Effect.Effect<void, EffectAcpErrors.AcpError>)
811 | undefined;
812 let duringNextPreparation: Effect.Effect<void, EffectAcpErrors.AcpError> = Effect.void;
813 let nextPromptStopReason: "cancelled" | "refusal" | "error" | undefined;
814 const prepare = Effect.suspend(() => {
815 const during = duringNextPreparation;
816 duringNextPreparation = Effect.void;
817 return during.pipe(Effect.orDie);
818 });
819 const adapter = yield* makeAcpAdapterV2({
820 instanceId,
821 flavor: {
822 driver: ACP_TEST_DRIVER,
823 capabilities: AcpProviderCapabilitiesV2,
824 supportsCompaction: true,
825 rewriteUserText: (text) =>
826 prepareAttachments ? Effect.succeed(text) : prepare.pipe(Effect.as(text)),
827 ...(prepareAttachments
828 ? {
829 buildAttachmentPrompt: () => prepare.pipe(Effect.as([])),
830 }
831 : {}),
832 registerExtensions: makeCopilotSubagentRouting().registerExtensions,
833 // Routes updates tagged with a Devin subagent context to that child session.
834 normalizeSessionUpdate: normalizeDevinSessionUpdate,
835 makeRuntime: makeMockRuntime({
836 childProcessSpawner,
837 mockAgentPath,
838 protocolEvents,
839 environment,
840 wrapRuntime: (runtime) => ({
841 ...runtime,
842 // The captured handlers deliver frames between turns, while no prompt is in flight.
843 handleSessionUpdate: (next) =>
844 Effect.sync(() => {
845 sessionUpdate = next;
846 }).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
847 handleExtNotification: (method, payload, next) =>
848 Effect.sync(() => {
849 if (method === "github.com/copilot/sessionEvent") {
850 copilotSessionEvent = next as typeof copilotSessionEvent;
851 }
852 }).pipe(Effect.andThen(runtime.handleExtNotification(method, payload, next))),
853 // Make compaction during prompt completion deterministic at the adapter boundary.
854 prompt: (payload, options) =>
855 runtime.prompt(payload, options).pipe(
856 Effect.tap(() => runtime.drainEvents),
857 // Grok's local cancel reports `cancelled` even when the agent never saw the prompt.
858 Effect.map((result) => {
859 if (nextPromptStopReason === undefined) return result;
860 const stopReason = nextPromptStopReason;
861 nextPromptStopReason = undefined;
862 return { ...result, stopReason };
863 }),
864 ),
865 }),
866 }),
867 },
868 selfInvocation,
869 });
870 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
871 runtimeMode: "full-access",
872 interactionMode: "default",
873 cwd: process.cwd(),
874 });
875 const modelSelection = { instanceId, model: "default" } as const;
876 const runtime = yield* adapter.openSession({
877 threadId,
878 providerSessionId: ProviderSessionId.make("provider-session-acp-instruction-compaction"),
879 modelSelection,
880 runtimePolicy,
881 });
882 const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
883 const now = yield* DateTime.now;
884 let ordinal = 0;
885 const sessionId = "mock-session-1";
886 return {
887 runTurn: Effect.fnUntraced(function* (messageText: string, expectedStatus = "completed") {
888 yield* runtime.startTurn(
889 makeTurnInput({
890 threadId,
891 providerThread,
892 instanceId,
893 runtimePolicy,
894 now,
895 ordinal: ++ordinal,
896 messageText,
897 }),
898 );
899 const terminal = yield* runtime.events.pipe(
900 Stream.filter((event) => event.type === "turn.terminal"),
901 Stream.runHead,
902 );
903 assert.equal(Option.getOrThrow(terminal).status, expectedStatus);
904 while (true) {
905 const event = yield* Queue.take(protocolEvents);
906 if (event.direction === "outgoing" && rawProtocolMethod(event) === "session/prompt") {
907 return rawProtocolPromptTextBlocks(event);
908 }
909 }
910 }),
911 idleSessionUpdate: Effect.fnUntraced(function* (update: EffectAcpSchema.SessionUpdate) {
912 assert.isDefined(sessionUpdate);
913 yield* sessionUpdate({ sessionId, update });
914 }),
915 reportNextPromptStopped: (stopReason: "cancelled" | "refusal" | "error") =>
916 Effect.sync(() => {
917 nextPromptStopReason = stopReason;
918 }),
919 sessionUpdateDuringNextPreparation: (update: EffectAcpSchema.SessionUpdate) =>
920 Effect.sync(() => {
921 duringNextPreparation = Effect.suspend(() => {
922 assert.isDefined(sessionUpdate);
923 return sessionUpdate({ sessionId, update });
924 });
925 }),
926 idleCopilotSessionEvent: Effect.fnUntraced(function* (event: {
927 readonly type: string;
928 readonly agentId?: string;
929 readonly data: Record<string, unknown>;
930 }) {
931 assert.isDefined(copilotSessionEvent);
932 yield* copilotSessionEvent({ sessionId, ...event });
933 }),
934 };
935 });
936
937 function assertReinjectedInstructions(
938 blocks: Array<{ type: "text"; text: string }>,
939 request: string,
940 ) {
941 const text = blocks[0]?.text ?? "";
942 assert.include(text, "T3 Code interaction mode: Default");
943 assert.include(text, "## T3 Code browser");
944 assert.include(text, "T3 Code orchestration");
945 assert.include(text, `<user_request>\n${request}\n</user_request>`);
946 assert.include(blocks[1]?.text ?? "", "<runtime_info>");
947 }
948
949 const makeAdvertisedCompactionHarness = Effect.fnUntraced(function* (
950 environment: Readonly<Record<string, string>>,
951 ) {
952 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
953 const path = yield* Path.Path;
954 const selfInvocation = yield* resolveSelfInvocation();
955 const mockAgentPath = yield* path.fromFileUrl(
956 new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
957 );
958 const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
959 // Resolves after the adapter records what the agent advertised, so the refusal is deterministic.
960 const commandsPublished = yield* Deferred.make<void>();
961 const instanceId = ProviderInstanceId.make("acp-test-advertised-compaction");
962 const threadId = ThreadId.make("thread-acp-advertised-compaction");
963 const adapter = yield* makeAcpAdapterV2({
964 instanceId,
965 flavor: {
966 driver: ACP_TEST_DRIVER,
967 capabilities: AcpProviderCapabilitiesV2,
968 compactViaAdvertisedCommand: true,
969 onAvailableCommandsUpdate: () => Deferred.succeed(commandsPublished, undefined),
970 makeRuntime: makeMockRuntime({
971 childProcessSpawner,
972 mockAgentPath,
973 protocolEvents,
974 environment,
975 }),
976 },
977 selfInvocation,
978 });
979 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
980 runtimeMode: "full-access",
981 interactionMode: "default",
982 cwd: process.cwd(),
983 });
984 const modelSelection = { instanceId, model: "default" } as const;
985 const runtime = yield* adapter.openSession({
986 threadId,
987 providerSessionId: ProviderSessionId.make("provider-session-acp-advertised-compaction"),
988 modelSelection,
989 runtimePolicy,
990 });
991 const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
992 yield* Deferred.await(commandsPublished);
993 const now = yield* DateTime.now;
994 let ordinal = 0;
995 const nextPromptTexts = () =>
996 Effect.gen(function* () {
997 while (true) {
998 const event = yield* Queue.take(protocolEvents);
999 if (event.direction === "outgoing" && rawProtocolMethod(event) === "session/prompt") {
1000 return rawProtocolPromptTextBlocks(event);
1001 }
1002 }
1003 });
1004 return {
1005 runtime,
1006 compact: (otherSession = false) =>
1007 Effect.gen(function* () {
1008 const compactThread = runtime.compactThread;
1009 assert.isDefined(compactThread, "compactThread must be wired for compactCommand");
1010 return yield* Effect.exit(
1011 compactThread!(
1012 makeTurnInput({
1013 threadId,
1014 providerThread: otherSession
1015 ? {
1016 ...providerThread,
1017 nativeThreadRef: {
1018 driver: ACP_TEST_DRIVER,
1019 nativeId: "other-session",
1020 strength: "strong",
1021 },
1022 }
1023 : { ...providerThread, contextUsage: { usedTokens: 1234 } },
1024 instanceId,
1025 runtimePolicy,
1026 now,
1027 ordinal: ++ordinal,
1028 messageText: "Compact the conversation.",
1029 }),
1030 ),
1031 );
1032 }),
1033 nextPromptTexts,
1034 };
1035 });
1036
1037 const runFormElicitation = (input: {
1038 readonly name: string;
1039 readonly requestedSchema: unknown;
1040 readonly answer: (
1041 questions: ReadonlyArray<OrchestrationV2UserInputQuestion>,
1042 ) => ProviderUserInputAnswers | undefined;
1043 }) =>
1044 Effect.gen(function* () {
1045 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
1046 const fileSystem = yield* FileSystem.FileSystem;
1047 const path = yield* Path.Path;
1048 const mockAgentPath = yield* bundledMockAgentPath;
1049 const directory = yield* fileSystem.makeTempDirectoryScoped({
1050 prefix: "t3-acp-form-elicitation-",
1051 });
1052 const responseLog = path.join(directory, "responses.jsonl");
1053 const instanceId = ProviderInstanceId.make(`acp-test-form-${input.name}`);
1054 const adapter = yield* makeAcpAdapterV2({
1055 instanceId,
1056 flavor: {
1057 driver: ACP_TEST_DRIVER,
1058 capabilities: AcpProviderCapabilitiesV2,
1059 makeRuntime: makeMockRuntime({
1060 childProcessSpawner,
1061 mockAgentPath,
1062 environment: {
1063 T3_ACP_EMIT_ELICITATION: "1",
1064 T3_ACP_ELICITATION_SCHEMA: JSON.stringify(input.requestedSchema),
1065 T3_ACP_ELICITATION_RESPONSE_LOG_PATH: responseLog,
1066 },
1067 }),
1068 },
1069 selfInvocation: yield* resolveSelfInvocation(),
1070 });
1071 const threadId = ThreadId.make(`thread-acp-form-${input.name}`);
1072 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1073 runtimeMode: "approval-required",
1074 interactionMode: "default",
1075 cwd: process.cwd(),
1076 });
1077 const modelSelection = { instanceId, model: "default" } as const;
1078 const runtime = yield* adapter.openSession({
1079 threadId,
1080 providerSessionId: ProviderSessionId.make(`provider-session-acp-form-${input.name}`),
1081 modelSelection,
1082 runtimePolicy,
1083 });
1084 const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
1085 yield* runtime.startTurn(
1086 makeTurnInput({
1087 threadId,
1088 providerThread,
1089 instanceId,
1090 runtimePolicy,
1091 now: yield* DateTime.now,
1092 }),
1093 );
1094 const first = Option.getOrThrow(
1095 yield* runtime.events.pipe(
1096 Stream.filter(
1097 (event) =>
1098 event.type === "turn.terminal" ||
1099 (event.type === "turn_item.updated" && event.turnItem.type === "user_input_request"),
1100 ),
1101 Stream.runHead,
1102 ),
1103 );
1104 let questions: ReadonlyArray<OrchestrationV2UserInputQuestion> | undefined;
1105 if (first.type === "turn_item.updated" && first.turnItem.type === "user_input_request") {
1106 questions = first.turnItem.questions;
1107 const answers = input.answer(questions);
1108 yield* runtime.respondToRuntimeRequest({
1109 requestId: first.turnItem.requestId,
1110 ...(answers === undefined ? {} : { answers }),
1111 });
1112 yield* runtime.events.pipe(
1113 Stream.filter((event) => event.type === "turn.terminal"),
1114 Stream.runHead,
1115 );
1116 }
1117 const responses = (yield* fileSystem.readFileString(responseLog))
1118 .split("\n")
1119 .filter((line) => line.length > 0)
1120 .map((line) => Option.getOrThrow(decodeUnknownJson(line)));
1121 assert.lengthOf(responses, 1);
1122 return { questions, response: responses[0] };
1123 });
1124
1125 /** Submits like the web and mobile clients: an option's `value`, else its label. */
1126 const optionValues = (question: OrchestrationV2UserInputQuestion | undefined) =>
1127 question?.options.map((option) => option.value ?? option.label);
1128
1129 describe("AcpAdapterV2", () => {
1130 beforeAll(() => bundleAgents);
1131
1132 it.live("keeps a completed tool's status when its linked ACP terminal streams output", () =>
1133 Effect.gen(function* () {
1134 const instanceId = ProviderInstanceId.make("acp-test-terminal-linked-tool");
1135 const threadId = ThreadId.make("thread-acp-terminal-linked-tool");
1136 let notify: Parameters<
1137 AcpSessionRuntime.AcpSessionRuntime["Service"]["handleSessionUpdate"]
1138 >[0];
1139 const adapter = yield* makeAcpAdapterV2({
1140 instanceId,
1141 selfInvocation: yield* resolveSelfInvocation(),
1142 flavor: {
1143 driver: ACP_TEST_DRIVER,
1144 capabilities: AcpProviderCapabilitiesV2,
1145 makeRuntime: makeMockRuntime({
1146 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
1147 mockAgentPath: yield* bundledMockAgentPath,
1148 wrapRuntime: (runtime) => ({
1149 ...runtime,
1150 handleSessionUpdate: (handler) =>
1151 Effect.sync(() => {
1152 notify = handler;
1153 }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))),
1154 prompt: () =>
1155 Effect.gen(function* () {
1156 const send = (update: EffectAcpSchema.SessionUpdate) =>
1157 notify({ sessionId: "mock-session-1", update });
1158 yield* send({ sessionUpdate: "state_update", state: "running" });
1159 yield* send({
1160 sessionUpdate: "terminal_update",
1161 terminalId: "term",
1162 command: "npm run dev",
1163 });
1164 yield* send({
1165 sessionUpdate: "tool_call_update",
1166 toolCallId: "linked",
1167 kind: "execute",
1168 title: "Start dev server",
1169 status: "completed",
1170 content: [{ type: "terminal", terminalId: "term" }],
1171 });
1172 yield* send({
1173 sessionUpdate: "terminal_output_chunk",
1174 terminalId: "term",
1175 data: Buffer.from("still running").toString("base64"),
1176 });
1177 yield* send({
1178 sessionUpdate: "state_update",
1179 state: "idle",
1180 stopReason: "end_turn",
1181 });
1182 return { stopReason: "end_turn" as const };
1183 }),
1184 }),
1185 }),
1186 },
1187 });
1188 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1189 runtimeMode: "full-access",
1190 interactionMode: "default",
1191 cwd: process.cwd(),
1192 });
1193 const modelSelection = { instanceId, model: "default" } as const;
1194 const runtime = yield* adapter.openSession({
1195 threadId,
1196 providerSessionId: ProviderSessionId.make("provider-session-acp-terminal-linked-tool"),
1197 modelSelection,
1198 runtimePolicy,
1199 });
1200 const providerThread = yield* runtime.ensureThread({
1201 threadId,
1202 modelSelection,
1203 runtimePolicy,
1204 });
1205 yield* runtime.startTurn(
1206 makeTurnInput({
1207 threadId,
1208 providerThread,
1209 instanceId,
1210 runtimePolicy,
1211 now: yield* DateTime.now,
1212 ordinal: 1,
1213 }),
1214 );
1215 const items = Array.from(
1216 yield* runtime.events.pipe(
1217 Stream.takeUntil((event) => event.type === "turn.terminal"),
1218 Stream.runCollect,
1219 ),
1220 ).flatMap((event) =>
1221 event.type === "turn_item.updated" && event.turnItem.nativeItemRef?.nativeId === "linked"
1222 ? [event.turnItem]
1223 : [],
1224 );
1225 const last = items.at(-1);
1226 assert.equal(last?.type === "command_execution" ? last.output : null, "still running");
1227 assert.deepEqual(
1228 items.map((item) => item.status),
1229 items.map(() => "completed"),
1230 "terminal output must not reopen a tool the agent completed",
1231 );
1232 }).pipe(Effect.provide(layerTest)),
1233 );
1234
1235 it.live("reports per-turn usage unavailable on a continuation turn that never prompts", () =>
1236 Effect.gen(function* () {
1237 const instanceId = ProviderInstanceId.make("acp-test-usage-continuation");
1238 const threadId = ThreadId.make("thread-acp-usage-continuation");
1239 let promptOrdinal = 0;
1240 const promptUsages = [
1241 { inputTokens: 60, outputTokens: 15, totalTokens: 75 },
1242 { inputTokens: 130, outputTokens: 30, totalTokens: 160 },
1243 ];
1244 const adapter = yield* makeAcpAdapterV2({
1245 instanceId,
1246 selfInvocation: yield* resolveSelfInvocation(),
1247 continuationRequests: { offer: () => Effect.void },
1248 flavor: {
1249 driver: ACP_TEST_DRIVER,
1250 capabilities: AcpProviderCapabilitiesV2,
1251 reportsPerTurnPromptUsage: true,
1252 enablePostSettleContinuation: true,
1253 makeRuntime: makeMockRuntime({
1254 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
1255 mockAgentPath: yield* bundledMockAgentPath,
1256 wrapRuntime: (runtime) => ({
1257 ...runtime,
1258 prompt: () => {
1259 promptOrdinal += 1;
1260 const usage = promptUsages[promptOrdinal - 1];
1261 if (usage === undefined) return Effect.die("Unexpected prompt");
1262 return Effect.succeed({ stopReason: "end_turn" as const, usage });
1263 },
1264 }),
1265 }),
1266 },
1267 });
1268 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1269 runtimeMode: "full-access",
1270 interactionMode: "default",
1271 cwd: process.cwd(),
1272 });
1273 const modelSelection = { instanceId, model: "default" } as const;
1274 const runtime = yield* adapter.openSession({
1275 threadId,
1276 providerSessionId: ProviderSessionId.make("provider-session-acp-usage-continuation"),
1277 modelSelection,
1278 runtimePolicy,
1279 });
1280 const providerThread = yield* runtime.ensureThread({
1281 threadId,
1282 modelSelection,
1283 runtimePolicy,
1284 });
1285 const now = yield* DateTime.now;
1286 const completedUsage = (ordinal: number) =>
1287 Effect.gen(function* () {
1288 yield* runtime.startTurn(
1289 makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal }),
1290 );
1291 const events = Array.from(
1292 yield* runtime.events.pipe(
1293 Stream.takeUntil((event) => event.type === "turn.terminal"),
1294 Stream.runCollect,
1295 ),
1296 );
1297 const completed = events.find(
1298 (event) =>
1299 event.type === "provider_turn.updated" && event.providerTurn.status === "completed",
1300 );
1301 return completed?.type === "provider_turn.updated"
1302 ? completed.providerTurn.turnTokenUsage
1303 : null;
1304 });
1305 assert.deepEqual(yield* completedUsage(1), {
1306 usageScope: "main_agent",
1307 usageStatus: "complete",
1308 hasSubagents: false,
1309 inputTokens: 60,
1310 outputTokens: 15,
1311 });
1312 // Continuation attach: drains wake traffic and finalizes without ever
1313 // dispatching session/prompt, so it must report unavailable usage.
1314 yield* runtime.startTurn(
1315 makeTurnInput({
1316 threadId,
1317 providerThread,
1318 instanceId,
1319 runtimePolicy,
1320 now,
1321 ordinal: 2,
1322 messageCreatedBy: "agent",
1323 messageCreationSource: "provider",
1324 messageText: "Background task completed.",
1325 }),
1326 );
1327 const continuationEvents = Array.from(
1328 yield* runtime.events.pipe(
1329 Stream.takeUntil((event) => event.type === "turn.terminal"),
1330 Stream.runCollect,
1331 ),
1332 );
1333 const continuation = continuationEvents.find(
1334 (event) =>
1335 event.type === "provider_turn.updated" && event.providerTurn.status === "completed",
1336 );
1337 assert.deepEqual(
1338 continuation?.type === "provider_turn.updated"
1339 ? continuation.providerTurn.turnTokenUsage
1340 : null,
1341 { usageScope: "main_agent", usageStatus: "unavailable", hasSubagents: false },
1342 );
1343 // The continuation must not disturb the next real prompt's sample.
1344 assert.deepEqual(yield* completedUsage(3), {
1345 usageScope: "main_agent",
1346 usageStatus: "complete",
1347 hasSubagents: false,
1348 inputTokens: 130,
1349 outputTokens: 30,
1350 });
1351 assert.equal(promptOrdinal, 2, "continuation turn must not dispatch session/prompt");
1352 }).pipe(Effect.provide(layerTest)),
1353 );
1354 it.live("fails an end_turn the outcome check rejects and restarts before the next turn", () =>
1355 Effect.gen(function* () {
1356 const checking = yield* Deferred.make<void>();
1357 const releaseCheck = yield* Deferred.make<void>();
1358 const cancelled = yield* Deferred.make<void>();
1359 const idAllocator = yield* IdAllocator.IdAllocatorV2;
1360 const instanceId = ProviderInstanceId.make("acp-test-prompt-outcome");
1361 const threadId = ThreadId.make("thread-acp-prompt-outcome");
1362 const quota = {
1363 class: "usage_limit",
1364 message: "Quota reached.",
1365 code: "RESOURCE_EXHAUSTED",
1366 retryable: true,
1367 resetAt: "2030-01-01T00:00:00.000Z",
1368 } as const;
1369 const poisoned = {
1370 class: "provider_error",
1371 message: "Saved history is unusable.",
1372 code: "INVALID_THINKING_SIGNATURE",
1373 retryable: true,
1374 } as const;
1375 // The runtime ordinal of each prompt, and the session each check watched.
1376 const promptRuntimes: Array<number> = [];
1377 const promptTexts: Array<string> = [];
1378 const watchedSessions: Array<string> = [];
1379 const adapter = yield* makeAcpAdapterV2({
1380 instanceId,
1381 selfInvocation: yield* resolveSelfInvocation(),
1382 flavor: {
1383 driver: ACP_TEST_DRIVER,
1384 capabilities: AcpProviderCapabilitiesV2,
1385 watchPromptOutcome: (sessionId) =>
1386 Effect.sync(() => {
1387 watchedSessions.push(sessionId);
1388 const outcome =
1389 watchedSessions.length === 1
1390 ? { failure: quota, threadDisposition: "reusable" as const }
1391 : watchedSessions.length >= 3
1392 ? { failure: poisoned, threadDisposition: "broken" as const }
1393 : undefined;
1394 const interrupted = watchedSessions.length === 4;
1395 return () =>
1396 interrupted
1397 ? Deferred.succeed(checking, undefined).pipe(
1398 Effect.andThen(Deferred.await(releaseCheck)),
1399 Effect.as(outcome),
1400 )
1401 : Effect.succeed(outcome);
1402 }),
1403 makeRuntime: makeMockRuntime({
1404 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
1405 mockAgentPath: yield* bundledMockAgentPath,
1406 wrapRuntime: (runtime, runtimeOrdinal) => ({
1407 ...runtime,
1408 cancel: Deferred.succeed(cancelled, undefined).pipe(Effect.asVoid),
1409 prompt: (payload) =>
1410 Effect.sync(() => {
1411 promptRuntimes.push(runtimeOrdinal);
1412 promptTexts.push(
1413 payload.prompt
1414 .flatMap((block) => (block.type === "text" ? [block.text] : []))
1415 .join("\n"),
1416 );
1417 return { stopReason: "end_turn" as const };
1418 }),
1419 }),
1420 }),
1421 },
1422 });
1423 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1424 runtimeMode: "full-access",
1425 interactionMode: "default",
1426 cwd: process.cwd(),
1427 });
1428 const modelSelection = { instanceId, model: "default" } as const;
1429 const runtime = yield* adapter.openSession({
1430 threadId,
1431 providerSessionId: ProviderSessionId.make("provider-session-acp-prompt-outcome"),
1432 modelSelection,
1433 runtimePolicy,
1434 });
1435 const providerThread = yield* runtime.ensureThread({
1436 threadId,
1437 modelSelection,
1438 runtimePolicy,
1439 });
1440 const runTurn = (ordinal: number) =>
1441 Effect.gen(function* () {
1442 yield* runtime.startTurn(
1443 makeTurnInput({
1444 threadId,
1445 providerThread,
1446 instanceId,
1447 runtimePolicy,
1448 now: yield* DateTime.now,
1449 ordinal,
1450 }),
1451 );
1452 return Array.from(
1453 yield* runtime.events.pipe(
1454 Stream.takeUntil((event) => event.type === "turn.terminal"),
1455 Stream.runCollect,
1456 ),
1457 );
1458 });
1459
1460 const failed = (yield* runTurn(1)).at(-1);
1461 if (failed?.type !== "turn.terminal" || failed.status !== "failed") {
1462 return assert.fail("expected a failed terminal");
1463 }
1464 assert.deepEqual(failed.failure, quota);
1465 const completed = (yield* runTurn(2)).at(-1);
1466 assert.equal(completed?.type === "turn.terminal" ? completed.status : null, "completed");
1467 assert.deepEqual(promptRuntimes, [1, 2], "the next turn runs on a fresh runtime");
1468 // The fresh runtime has no record of what reached the agent, so it sends T3's notes again.
1469 assert.include(promptTexts[1] ?? "", "<runtime_info>");
1470 assert.include(promptTexts[1] ?? "", "T3 Code interaction mode: Default");
1471 assert.equal(watchedSessions.length, 2);
1472 assert.equal(watchedSessions[0], watchedSessions[1]);
1473
1474 // Poisoned native history marks the thread broken; its persisted error row is never resumed.
1475 const poisonedEvents = yield* runTurn(3);
1476 const broken = poisonedEvents.at(-1);
1477 if (broken?.type !== "turn.terminal" || broken.status !== "failed") {
1478 return assert.fail("expected a failed terminal");
1479 }
1480 assert.deepEqual(broken.failure, poisoned);
1481 assert.equal(broken.threadDisposition, "broken");
1482 const brokenThread = poisonedEvents.findLast(
1483 (event) => event.type === "provider_thread.updated",
1484 );
1485 assert.equal(
1486 brokenThread?.type === "provider_thread.updated"
1487 ? brokenThread.providerThread.status
1488 : null,
1489 "error",
1490 );
1491 const resumed = yield* Effect.result(
1492 runtime.resumeThread({
1493 threadId,
1494 providerThread: { ...providerThread, status: "error" },
1495 modelSelection,
1496 runtimePolicy,
1497 }),
1498 );
1499 assert.equal(
1500 resumed._tag === "Failure" ? resumed.failure._tag : null,
1501 "ProviderAdapterResumeThreadError",
1502 );
1503
1504 const turn = yield* Effect.forkChild(runTurn(4));
1505 yield* Deferred.await(checking);
1506 const stop = yield* Effect.forkChild(
1507 runtime.interruptTurn({
1508 providerThread,
1509 providerTurnId: idAllocator.derive.providerTurn({
1510 driver: ACP_TEST_DRIVER,
1511 nativeTurnId: acpScopedNativeId(instanceId, `${watchedSessions[3]}:turn:4`),
1512 }),
1513 }),
1514 );
1515 yield* Deferred.await(cancelled);
1516 yield* Deferred.succeed(releaseCheck, undefined);
1517 const interruptedEvents = yield* Fiber.join(turn);
1518 yield* Fiber.join(stop);
1519 const interrupted = interruptedEvents.at(-1);
1520 assert.equal(
1521 interrupted?.type === "turn.terminal" ? interrupted.status : null,
1522 "interrupted",
1523 );
1524 assert.equal(
1525 interrupted?.type === "turn.terminal" ? interrupted.threadDisposition : null,
1526 "broken",
1527 );
1528 }).pipe(Effect.provide(layerTest)),
1529 );
1530 it.live("resends a resumable outcome failure's prompt once before failing the turn", () =>
1531 Effect.gen(function* () {
1532 const instanceId = ProviderInstanceId.make("acp-test-prompt-resume");
1533 const threadId = ThreadId.make("thread-acp-prompt-resume");
1534 const notFound = {
1535 class: "provider_error",
1536 message: "Requested entity was not found.",
1537 code: "NOT_FOUND",
1538 retryable: true,
1539 } as const;
1540 const promptTexts: Array<string> = [];
1541 // Checks 1, 3 and 4 fail: turn 1 resumes and completes, turn 2 fails after one resume.
1542 let checks = 0;
1543 const adapter = yield* makeAcpAdapterV2({
1544 instanceId,
1545 selfInvocation: yield* resolveSelfInvocation(),
1546 flavor: {
1547 driver: ACP_TEST_DRIVER,
1548 capabilities: AcpProviderCapabilitiesV2,
1549 steersWithPrompt: () => true,
1550 watchPromptOutcome: () =>
1551 Effect.sync(() => {
1552 checks += 1;
1553 const outcome =
1554 checks === 2
1555 ? undefined
1556 : {
1557 failure: notFound,
1558 threadDisposition: "reusable" as const,
1559 resumeWith: "continue",
1560 };
1561 return () => Effect.succeed(outcome);
1562 }),
1563 makeRuntime: makeMockRuntime({
1564 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
1565 mockAgentPath: yield* bundledMockAgentPath,
1566 wrapRuntime: (runtime) => ({
1567 ...runtime,
1568 prompt: (payload) =>
1569 Effect.sync(() => {
1570 promptTexts.push(
1571 payload.prompt
1572 .flatMap((block) => (block.type === "text" ? [block.text] : []))
1573 .join("\n"),
1574 );
1575 return { stopReason: "end_turn" as const };
1576 }),
1577 }),
1578 }),
1579 },
1580 });
1581 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1582 runtimeMode: "full-access",
1583 interactionMode: "default",
1584 cwd: process.cwd(),
1585 });
1586 const modelSelection = { instanceId, model: "default" } as const;
1587 const runtime = yield* adapter.openSession({
1588 threadId,
1589 providerSessionId: ProviderSessionId.make("provider-session-acp-prompt-resume"),
1590 modelSelection,
1591 runtimePolicy,
1592 });
1593 const providerThread = yield* runtime.ensureThread({
1594 threadId,
1595 modelSelection,
1596 runtimePolicy,
1597 });
1598 const runTurn = (ordinal: number) =>
1599 Effect.gen(function* () {
1600 yield* runtime.startTurn(
1601 makeTurnInput({
1602 threadId,
1603 providerThread,
1604 instanceId,
1605 runtimePolicy,
1606 now: yield* DateTime.now,
1607 ordinal,
1608 }),
1609 );
1610 return Array.from(
1611 yield* runtime.events.pipe(
1612 Stream.takeUntil((event) => event.type === "turn.terminal"),
1613 Stream.runCollect,
1614 ),
1615 );
1616 });
1617 const retryTitles = (events: ReadonlyArray<ProviderAdapter.ProviderAdapterV2Event>) =>
1618 events.flatMap((event) =>
1619 event.type === "turn_item.updated" &&
1620 event.turnItem.type === "error" &&
1621 event.turnItem.retry !== undefined
1622 ? [event.turnItem.title]
1623 : [],
1624 );
1625
1626 const resumed = yield* runTurn(1);
1627 const completed = resumed.at(-1);
1628 assert.equal(completed?.type === "turn.terminal" ? completed.status : null, "completed");
1629 assert.deepEqual(promptTexts.slice(1), ["continue"]);
1630 assert.deepEqual(retryTitles(resumed), ["Provider retry", "Provider recovered"]);
1631
1632 const failedEvents = yield* runTurn(2);
1633 const failed = failedEvents.at(-1);
1634 if (failed?.type !== "turn.terminal" || failed.status !== "failed") {
1635 return assert.fail("expected a failed terminal");
1636 }
1637 assert.deepEqual(failed.failure, notFound);
1638 assert.deepEqual(promptTexts.slice(3), ["continue"], "a turn resumes only once");
1639 }).pipe(Effect.provide(layerTest)),
1640 );
1641 it.live("retains returned usage when Stop finalizes before the prompt callback", () =>
1642 Effect.gen(function* () {
1643 const instanceId = ProviderInstanceId.make("acp-test-prompt-usage-stop");
1644 const threadId = ThreadId.make("thread-acp-prompt-usage-stop");
1645 const wireSettled = yield* Deferred.make<void>();
1646 const releaseCompletion = yield* Deferred.make<void>();
1647 const idAllocator = yield* IdAllocator.IdAllocatorV2;
1648 const adapter = yield* makeAcpAdapterV2({
1649 instanceId,
1650 selfInvocation: yield* resolveSelfInvocation(),
1651 testHooks: {
1652 afterPromptWireSettled: () =>
1653 Deferred.succeed(wireSettled, undefined).pipe(
1654 Effect.andThen(Deferred.await(releaseCompletion)),
1655 ),
1656 },
1657 flavor: {
1658 driver: ACP_TEST_DRIVER,
1659 capabilities: AcpProviderCapabilitiesV2,
1660 reportsPerTurnPromptUsage: true,
1661 preserveRuntimeOnSettledInterrupt: true,
1662 makeRuntime: makeMockRuntime({
1663 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
1664 mockAgentPath: yield* bundledMockAgentPath,
1665 wrapRuntime: (runtime) => ({
1666 ...runtime,
1667 prompt: () =>
1668 Effect.succeed({
1669 stopReason: "end_turn" as const,
1670 usage: { inputTokens: 60, outputTokens: 15, totalTokens: 75 },
1671 }),
1672 }),
1673 }),
1674 },
1675 });
1676 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1677 runtimeMode: "full-access",
1678 interactionMode: "default",
1679 cwd: process.cwd(),
1680 });
1681 const modelSelection = { instanceId, model: "default" } as const;
1682 const runtime = yield* adapter.openSession({
1683 threadId,
1684 providerSessionId: ProviderSessionId.make("provider-session-acp-prompt-usage-stop"),
1685 modelSelection,
1686 runtimePolicy,
1687 });
1688 const providerThread = yield* runtime.ensureThread({
1689 threadId,
1690 modelSelection,
1691 runtimePolicy,
1692 });
1693 yield* runtime.startTurn(
1694 makeTurnInput({
1695 threadId,
1696 providerThread,
1697 instanceId,
1698 runtimePolicy,
1699 now: yield* DateTime.now,
1700 }),
1701 );
1702 yield* Deferred.await(wireSettled);
1703 const providerTurnId = idAllocator.derive.providerTurn({
1704 driver: ACP_TEST_DRIVER,
1705 nativeTurnId: acpScopedNativeId(instanceId, "mock-session-1:turn:1"),
1706 });
1707 yield* runtime.interruptTurn({ providerThread, providerTurnId });
1708 const completed = Array.from(
1709 yield* runtime.events.pipe(
1710 Stream.takeUntil((event) => event.type === "turn.terminal"),
1711 Stream.runCollect,
1712 ),
1713 ).find(
1714 (event) =>
1715 event.type === "provider_turn.updated" && event.providerTurn.status === "interrupted",
1716 );
1717 assert.deepEqual(
1718 completed?.type === "provider_turn.updated" ? completed.providerTurn.turnTokenUsage : null,
1719 {
1720 usageScope: "main_agent",
1721 usageStatus: "partial",
1722 hasSubagents: false,
1723 inputTokens: 60,
1724 outputTokens: 15,
1725 },
1726 );
1727 yield* Deferred.succeed(releaseCompletion, undefined);
1728 }).pipe(Effect.provide(layerTest)),
1729 );
1730
1731 it.live("drops a late prompt response that lands after the turn finalized", () =>
1732 Effect.gen(function* () {
1733 const instanceId = ProviderInstanceId.make("acp-test-prompt-usage-late");
1734 const threadId = ThreadId.make("thread-acp-prompt-usage-late");
1735 const dispatched = yield* Deferred.make<void>();
1736 const gate = yield* Deferred.make<void>();
1737 const wireSettled = yield* Deferred.make<void>();
1738 const idAllocator = yield* IdAllocator.IdAllocatorV2;
1739 const adapter = yield* makeAcpAdapterV2({
1740 instanceId,
1741 selfInvocation: yield* resolveSelfInvocation(),
1742 testHooks: {
1743 afterPromptWireSettled: () =>
1744 Deferred.succeed(wireSettled, undefined).pipe(Effect.asVoid),
1745 },
1746 flavor: {
1747 driver: ACP_TEST_DRIVER,
1748 capabilities: AcpProviderCapabilitiesV2,
1749 reportsPerTurnPromptUsage: true,
1750 terminateRuntimeProcessGroupOnInterrupt: true,
1751 makeRuntime: makeMockRuntime({
1752 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
1753 mockAgentPath: yield* bundledMockAgentPath,
1754 ownDetachedProcessGroup: true,
1755 processGroupTerminationGrace: 0,
1756 wrapRuntime: (runtime) => ({
1757 ...runtime,
1758 // The response only leaves the agent after the turn has been
1759 // force stopped and finalized: teardown does not interrupt this fiber.
1760 prompt: () =>
1761 Deferred.succeed(dispatched, undefined).pipe(
1762 Effect.andThen(Deferred.await(gate)),
1763 Effect.andThen(
1764 Effect.succeed({
1765 stopReason: "cancelled" as const,
1766 usage: { inputTokens: 60, outputTokens: 15, totalTokens: 75 },
1767 }),
1768 ),
1769 ),
1770 }),
1771 }),
1772 },
1773 });
1774 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1775 runtimeMode: "full-access",
1776 interactionMode: "default",
1777 cwd: process.cwd(),
1778 });
1779 const modelSelection = { instanceId, model: "default" } as const;
1780 const runtime = yield* adapter.openSession({
1781 threadId,
1782 providerSessionId: ProviderSessionId.make("provider-session-acp-prompt-usage-late"),
1783 modelSelection,
1784 runtimePolicy,
1785 });
1786 const providerThread = yield* runtime.ensureThread({
1787 threadId,
1788 modelSelection,
1789 runtimePolicy,
1790 });
1791 let stopCollector = false;
1792 const events: Array<Record<string, unknown>> = [];
1793 const usageRecorded = yield* Deferred.make<void>();
1794 const collector = yield* Stream.runForEach(
1795 runtime.events.pipe(Stream.takeUntil(() => stopCollector)),
1796 (event) =>
1797 Effect.suspend(() => {
1798 const record = event as Record<string, unknown>;
1799 events.push(record);
1800 return record.type === "provider_turn.updated" &&
1801 (record.providerTurn as Record<string, unknown>).turnTokenUsage !== undefined
1802 ? Deferred.succeed(usageRecorded, undefined)
1803 : Effect.void;
1804 }),
1805 ).pipe(Effect.forkScoped);
1806 yield* runtime.startTurn(
1807 makeTurnInput({
1808 threadId,
1809 providerThread,
1810 instanceId,
1811 runtimePolicy,
1812 now: yield* DateTime.now,
1813 messageText: "Say hi.",
1814 }),
1815 );
1816 yield* Deferred.await(dispatched);
1817 const providerTurnId = idAllocator.derive.providerTurn({
1818 driver: ACP_TEST_DRIVER,
1819 nativeTurnId: acpScopedNativeId(instanceId, "mock-session-1:turn:1"),
1820 });
1821 // Force stop finalizes the turn without waiting for the gated prompt response.
1822 yield* runtime.interruptTurn({
1823 providerThread,
1824 providerTurnId,
1825 requestRuntimeRestart: true,
1826 });
1827 yield* Deferred.await(usageRecorded);
1828 const usageAt = () => {
1829 const turns = events.filter(
1830 (event) =>
1831 event.type === "provider_turn.updated" &&
1832 (event.providerTurn as Record<string, unknown>).turnTokenUsage !== undefined,
1833 );
1834 const last = turns[turns.length - 1] as
1835 | { providerTurn: Record<string, unknown> }
1836 | undefined;
1837 return last?.providerTurn.turnTokenUsage as Record<string, unknown>;
1838 };
1839 assert.deepEqual(usageAt(), {
1840 usageScope: "main_agent",
1841 usageStatus: "unavailable",
1842 hasSubagents: false,
1843 });
1844 // The response lands now; the finalized turn must not adopt its counters.
1845 yield* Deferred.succeed(gate, undefined);
1846 yield* Deferred.await(wireSettled);
1847 assert.deepEqual(usageAt(), {
1848 usageScope: "main_agent",
1849 usageStatus: "unavailable",
1850 hasSubagents: false,
1851 });
1852 stopCollector = true;
1853 yield* Fiber.interrupt(collector);
1854 }).pipe(Effect.provide(layerTest)),
1855 );
1856
1857 it.live("keeps usage unavailable for undeclared agents even when they report usage", () =>
1858 Effect.gen(function* () {
1859 const instanceId = ProviderInstanceId.make("acp-test-usage-undeclared");
1860 const threadId = ThreadId.make("thread-acp-usage-undeclared");
1861 const adapter = yield* makeAcpAdapterV2({
1862 instanceId,
1863 selfInvocation: yield* resolveSelfInvocation(),
1864 flavor: {
1865 driver: ACP_TEST_DRIVER,
1866 capabilities: AcpProviderCapabilitiesV2,
1867 makeRuntime: makeMockRuntime({
1868 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
1869 mockAgentPath: yield* bundledMockAgentPath,
1870 wrapRuntime: (runtime) => ({
1871 ...runtime,
1872 prompt: () =>
1873 Effect.succeed({
1874 stopReason: "end_turn" as const,
1875 usage: { inputTokens: 60, outputTokens: 15, totalTokens: 75 },
1876 }),
1877 }),
1878 }),
1879 },
1880 });
1881 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1882 runtimeMode: "full-access",
1883 interactionMode: "default",
1884 cwd: process.cwd(),
1885 });
1886 const modelSelection = { instanceId, model: "default" } as const;
1887 const runtime = yield* adapter.openSession({
1888 threadId,
1889 providerSessionId: ProviderSessionId.make("provider-session-acp-usage-undeclared"),
1890 modelSelection,
1891 runtimePolicy,
1892 });
1893 const providerThread = yield* runtime.ensureThread({
1894 threadId,
1895 modelSelection,
1896 runtimePolicy,
1897 });
1898 const now = yield* DateTime.now;
1899 for (const ordinal of [1, 2]) {
1900 yield* runtime.startTurn(
1901 makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal }),
1902 );
1903 const events = Array.from(
1904 yield* runtime.events.pipe(
1905 Stream.takeUntil((event) => event.type === "turn.terminal"),
1906 Stream.runCollect,
1907 ),
1908 );
1909 const completed = events.find(
1910 (event) =>
1911 event.type === "provider_turn.updated" && event.providerTurn.status === "completed",
1912 );
1913 // Usage semantics are unverified for an undeclared agent, so its
1914 // counters are never trusted, on any turn.
1915 assert.deepEqual(
1916 completed?.type === "provider_turn.updated"
1917 ? completed.providerTurn.turnTokenUsage
1918 : null,
1919 { usageScope: "main_agent", usageStatus: "unavailable", hasSubagents: false },
1920 );
1921 }
1922 }).pipe(Effect.provide(layerTest)),
1923 );
1924
1925 it.live.each(["details", "none"] as const)(
1926 "fails the turn on an ACP v2 idle error with %s, settles its open tool, and accepts the next prompt",
1927 (details) =>
1928 Effect.gen(function* () {
1929 const instanceId = ProviderInstanceId.make(`idle-error-${details}`);
1930 const threadId = ThreadId.make(`thread-idle-error-${details}`);
1931 const childSessionId = "mock-child-session-idle-error";
1932 const adapter = yield* makeAcpAdapterV2({
1933 instanceId,
1934 selfInvocation: yield* resolveSelfInvocation(),
1935 flavor: {
1936 driver: ACP_TEST_DRIVER,
1937 capabilities: AcpProviderCapabilitiesV2,
1938 extractBackgroundTaskId: (toolCall) =>
1939 toolCall.toolCallId === "tool-call-idle-error-monitor"
1940 ? "idle-error-monitor"
1941 : undefined,
1942 extractSubagentUpdate: (toolCall) =>
1943 toolCall.toolCallId === "tool-call-idle-error-subagent"
1944 ? {
1945 nativeTaskId: "task-idle-error",
1946 prompt: "child work",
1947 title: "child work",
1948 model: null,
1949 status: toolCall.status === "completed" ? "completed" : "running",
1950 childSessionId,
1951 result: null,
1952 suppressNormalTool: true,
1953 }
1954 : undefined,
1955 makeRuntime: makeMockRuntime({
1956 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
1957 mockAgentPath: yield* bundledMockAgentPath,
1958 environment: { T3_ACP_IDLE_ERROR: details },
1959 }),
1960 },
1961 });
1962 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
1963 runtimeMode: "full-access",
1964 interactionMode: "default",
1965 cwd: process.cwd(),
1966 });
1967 const modelSelection = { instanceId, model: "default" } as const;
1968 const runtime = yield* adapter.openSession({
1969 threadId,
1970 providerSessionId: ProviderSessionId.make(`session-idle-error-${details}`),
1971 modelSelection,
1972 runtimePolicy,
1973 });
1974 const providerThread = yield* runtime.ensureThread({
1975 threadId,
1976 modelSelection,
1977 runtimePolicy,
1978 });
1979 const runTurn = (ordinal: number) =>
1980 Effect.gen(function* () {
1981 yield* runtime.startTurn(
1982 makeTurnInput({
1983 threadId,
1984 providerThread,
1985 instanceId,
1986 runtimePolicy,
1987 now: yield* DateTime.now,
1988 ordinal,
1989 }),
1990 );
1991 return yield* runtime.events.pipe(
1992 Stream.takeUntil((event) => event.type === "turn.terminal"),
1993 Stream.runCollect,
1994 );
1995 });
1996
1997 const failedEvents = yield* runTurn(1);
1998 const failed = failedEvents.at(-1);
1999 assert.equal(failed?.type, "turn.terminal");
2000 if (failed?.type !== "turn.terminal" || failed.status !== "failed") {
2001 return assert.fail("expected a failed terminal");
2002 }
2003 assert.isTrue(
2004 failed.childTurnsContinue,
2005 "the run keeps ingesting work that outlives the failed prompt",
2006 );
2007 const lateSubagent = yield* runtime.events.pipe(
2008 Stream.filter(
2009 (event) => event.type === "subagent.updated" && event.subagent.status === "completed",
2010 ),
2011 Stream.runHead,
2012 Effect.timeoutOption("2 seconds"),
2013 );
2014 assert.isTrue(
2015 Option.isSome(lateSubagent) && Option.isSome(lateSubagent.value),
2016 "a subagent finishing after the failed prompt still reports to the run",
2017 );
2018 assert.deepEqual(
2019 failed.failure,
2020 details === "details"
2021 ? {
2022 class: "provider_error",
2023 message: "Missing Authentication header",
2024 code: "-32000",
2025 retryable: null,
2026 }
2027 : {
2028 class: "provider_error",
2029 message: "Provider turn failed.",
2030 code: null,
2031 retryable: null,
2032 },
2033 );
2034 const toolItems = (nativeId: string) =>
2035 failedEvents.flatMap((event) =>
2036 event.type === "turn_item.updated" &&
2037 event.turnItem.nativeItemRef?.nativeId === nativeId
2038 ? [event.turnItem]
2039 : [],
2040 );
2041 const toolStatuses = toolItems("tool-call-idle-error-1").map((item) => item.status);
2042 assert.include(toolStatuses, "running");
2043 assert.equal(toolStatuses.at(-1), "failed");
2044 assert.deepEqual(
2045 toolItems("tool-call-idle-error-monitor").map((item) => item.status),
2046 ["running"],
2047 "a background monitor outlives the failed turn and keeps its running row",
2048 );
2049 const childItems = toolItems("task-idle-error:tool:tool-call-idle-error-child");
2050 assert.deepEqual(
2051 childItems.map((item) => item.status),
2052 ["running"],
2053 "a child-session tool keeps running in its own thread",
2054 );
2055 assert.isTrue(childItems.every((item) => item.threadId !== threadId));
2056 assert.isEmpty(
2057 toolItems("tool-call-idle-error-child"),
2058 "the failed turn must not reproject the child tool as a root tool",
2059 );
2060
2061 const nextEvents = yield* runTurn(2);
2062 const next = nextEvents.at(-1);
2063 assert.equal(next?.type === "turn.terminal" ? next.status : undefined, "completed");
2064 }).pipe(Effect.provide(layerTest)),
2065 );
2066
2067 // Droid 0.243.0 and OpenCode 2.1 pair each choice with an optional free-form companion field.
2068 const droidChoice = {
2069 type: "string",
2070 title: "Format",
2071 description: "Which reply format?",
2072 oneOf: [
2073 { const: "option_0", title: "JSON" },
2074 { const: "option_1", title: "Plain text" },
2075 { const: "other", title: "Other answer" },
2076 ],
2077 };
2078 const droidSchema = {
2079 type: "object",
2080 properties: {
2081 question_1: droidChoice,
2082 question_1_other: { type: "string" },
2083 question_2: { ...droidChoice, title: "Alternative" },
2084 question_2_other: { type: "string" },
2085 },
2086 required: ["question_1", "question_2"],
2087 };
2088 const droidQuestions = ["question_1", "question_2"].map((id, index) => ({
2089 id,
2090 header: index === 0 ? "Format" : "Alternative",
2091 question: "Which reply format?",
2092 options: [
2093 { label: "JSON", description: "JSON", value: "option_0" },
2094 { label: "Plain text", description: "Plain text", value: "option_1" },
2095 ],
2096 }));
2097
2098 // OpenCode 2.1 pairs each qN choice with a qN_custom free-form companion.
2099 const openCodeSchema = {
2100 type: "object",
2101 properties: {
2102 q0: {
2103 type: "string",
2104 title: "Color preference",
2105 description: "Which color do you prefer?",
2106 oneOf: [
2107 { const: "red", title: "Red", description: "Choose the color red" },
2108 { const: "blue", title: "Blue" },
2109 ],
2110 default: "red",
2111 },
2112 q0_custom: {
2113 type: "string",
2114 title: "Color preference (other)",
2115 description: "Type your own answer",
2116 minLength: 1,
2117 },
2118 },
2119 };
2120 const openCodeQuestions = [
2121 {
2122 id: "q0",
2123 header: "Color preference",
2124 question: "Which color do you prefer?",
2125 options: [
2126 { label: "Red", description: "Choose the color red", value: "red" },
2127 { label: "Blue", description: "Blue", value: "blue" },
2128 ],
2129 allowCustomAnswer: true,
2130 },
2131 ];
2132
2133 // regression: #20417 Droid answers were sent as a cancel; #23505 OMP header and question were swapped
2134 it.live.each([
2135 ...[
2136 { scenario: "OMP selected option", answers: { q0: "Approve" }, content: { q0: "Approve" } },
2137 {
2138 scenario: "OMP custom answer",
2139 answers: { q0: "Please wait" },
2140 content: { q0__other: "Please wait" },
2141 },
2142 ].map((fixture) => ({
2143 ...fixture,
2144 settings: OMP_SETTINGS,
2145 schema: {
2146 type: "object",
2147 properties: {
2148 q0: {
2149 type: "string",
2150 title: "Approve this exact code-only commit?",
2151 description: "Safety-hook false positive",
2152 oneOf: [
2153 { const: "Approve", title: "Approve", description: "Commit the verified fix." },
2154 { const: "Hold", title: "Hold" },
2155 ],
2156 default: "Approve",
2157 },
2158 q0__other: { type: "string", title: "Other (type your own)" },
2159 },
2160 },
2161 questions: [
2162 {
2163 id: "q0",
2164 header: "Safety-hook false positive",
2165 question: "Approve this exact code-only commit?",
2166 options: [
2167 { label: "Approve", value: "Approve", description: "Commit the verified fix." },
2168 { label: "Hold", value: "Hold", description: "Hold" },
2169 ],
2170 },
2171 ],
2172 result: { action: "accept", content: fixture.content },
2173 })),
2174 {
2175 scenario: "OMP unrelated form uses generic mapping",
2176 settings: OMP_SETTINGS,
2177 schema: { type: "object", properties: { name: { type: "string" } } },
2178 questions: [
2179 {
2180 id: "name",
2181 header: "Question 1",
2182 question: "Your answer?",
2183 options: [],
2184 required: false,
2185 },
2186 ],
2187 answers: { name: "Ada" },
2188 result: { action: "accept", content: { name: "Ada" } },
2189 },
2190 {
2191 scenario: "preserves selected IDs and encodes custom answers",
2192 schema: droidSchema,
2193 questions: droidQuestions,
2194 answers: { question_1: "option_1", question_2: "Markdown" },
2195 result: {
2196 action: "accept",
2197 content: { question_1: "option_1", question_2: "other", question_2_other: "Markdown" },
2198 },
2199 },
2200 {
2201 scenario: "extracts the paired form for a local droid command",
2202 settings: DROID_LOCAL_SETTINGS,
2203 schema: droidSchema,
2204 questions: droidQuestions,
2205 answers: { question_1: "option_1", question_2: "Markdown" },
2206 result: {
2207 action: "accept",
2208 content: { question_1: "option_1", question_2: "other", question_2_other: "Markdown" },
2209 },
2210 },
2211 {
2212 scenario: "preserves selected labels and attachment paths in custom answers",
2213 schema: droidSchema,
2214 questions: droidQuestions,
2215 withAttachments: true,
2216 answers: {
2217 question_1: "option_1",
2218 question_2: "option_1\n\nAttached notes: keep this custom text",
2219 },
2220 result: (directory: string) => ({
2221 action: "accept",
2222 content: {
2223 question_1: "other",
2224 question_1_other: `Plain text\n\nAttached image "shot.png": ${JSON.stringify(NodePath.join(directory, "droid-shot.png"))}\nAttached file "spec.txt": ${JSON.stringify(NodePath.join(directory, "droid-spec.txt"))}`,
2225 question_2: "other",
2226 question_2_other: "option_1\n\nAttached notes: keep this custom text",
2227 },
2228 }),
2229 },
2230 {
2231 scenario: "cancels without accepting answers",
2232 schema: droidSchema,
2233 questions: droidQuestions,
2234 answers: undefined,
2235 result: { action: "cancel" },
2236 },
2237 {
2238 scenario: "falls back for an unrelated form",
2239 schema: { type: "object", properties: { name: { type: "string" } } },
2240 questions: [
2241 {
2242 id: "name",
2243 header: "Question 1",
2244 question: "Your answer?",
2245 options: [],
2246 required: false,
2247 },
2248 ],
2249 answers: { name: "Ada" },
2250 result: { action: "accept", content: { name: "Ada" } },
2251 },
2252 {
2253 scenario: "OpenCode keeps the selected option and hides the custom companion",
2254 settings: OPENCODE_SETTINGS,
2255 schema: openCodeSchema,
2256 questions: openCodeQuestions,
2257 answers: { q0: "blue" },
2258 result: { action: "accept", content: { q0: "blue" } },
2259 },
2260 {
2261 scenario: "Devin allowOther choice accepts a typed answer",
2262 settings: DEVIN_SETTINGS,
2263 meta: { "cognition.ai/allowOther": true },
2264 schema: {
2265 type: "object",
2266 properties: {
2267 q0: {
2268 type: "string",
2269 title: "Fruit",
2270 description: "Which fruit?",
2271 oneOf: [
2272 { const: "Apple", title: "Apple" },
2273 { const: "Banana", title: "Banana" },
2274 ],
2275 },
2276 },
2277 required: ["q0"],
2278 },
2279 questions: [
2280 {
2281 id: "q0",
2282 header: "Fruit",
2283 question: "Which fruit?",
2284 options: [
2285 { label: "Apple", description: "Apple", value: "Apple" },
2286 { label: "Banana", description: "Banana", value: "Banana" },
2287 ],
2288 },
2289 ],
2290 answers: { q0: "Mango" },
2291 result: { action: "accept", content: { q0: "Mango" } },
2292 },
2293 ])("Registry elicitation $scenario", (fixture) => {
2294 const { schema, questions, answers, result } = fixture;
2295 const withAttachments = "withAttachments" in fixture && fixture.withAttachments;
2296 const settings = "settings" in fixture ? fixture.settings : DROID_SETTINGS;
2297 return Effect.gen(function* () {
2298 const fileSystem = yield* FileSystem.FileSystem;
2299 const path = yield* Path.Path;
2300 const directory = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-registry-form-" });
2301 const statusPath = path.join(directory, "status.json");
2302 const expectedResult = typeof result === "function" ? result(directory) : result;
2303 let preparedAnswers: ProviderUserInputAnswers | undefined = answers;
2304 if (withAttachments && answers !== undefined) {
2305 yield* fileSystem.writeFileString(path.join(directory, "droid-shot.png"), "image");
2306 yield* fileSystem.writeFileString(path.join(directory, "droid-spec.txt"), "spec");
2307 preparedAnswers = yield* appendUserInputAttachmentPaths({
2308 answers,
2309 attachmentsDir: directory,
2310 attachmentsByQuestionId: {
2311 question_1: [
2312 {
2313 type: "image",
2314 id: "droid-shot",
2315 name: "shot.png",
2316 mimeType: "image/png",
2317 sizeBytes: 5,
2318 },
2319 {
2320 type: "file",
2321 id: "droid-spec",
2322 name: "spec.txt",
2323 mimeType: "text/plain",
2324 sizeBytes: 4,
2325 },
2326 ],
2327 },
2328 });
2329 }
2330 const driver = ProviderDriverKind.make("acpRegistry");
2331 const transcript = yield* decodeAcpReplayTranscript(
2332 {
2333 provider: driver,
2334 protocol: "acp.ndjson-jsonrpc",
2335 version: "1",
2336 scenario: "droid-form",
2337 entries: [
2338 {
2339 type: "expect_outbound",
2340 frame: { kind: "request", method: "initialize", params: "<any>" },
2341 },
2342 {
2343 type: "emit_inbound",
2344 frame: {
2345 kind: "response",
2346 method: "initialize",
2347 result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] },
2348 },
2349 },
2350 {
2351 type: "expect_outbound",
2352 frame: { kind: "request", method: "session/new", params: "<any>" },
2353 },
2354 {
2355 type: "emit_inbound",
2356 frame: {
2357 kind: "response",
2358 method: "session/new",
2359 result: { sessionId: "droid-session" },
2360 },
2361 },
2362 {
2363 type: "expect_outbound",
2364 frame: { kind: "request", method: "session/prompt", params: "<any>" },
2365 },
2366 {
2367 type: "emit_inbound",
2368 frame: {
2369 kind: "request",
2370 method: "elicitation/create",
2371 params: {
2372 mode: "form",
2373 sessionId: "droid-session",
2374 message: "Your answer?",
2375 requestedSchema: schema,
2376 ...("meta" in fixture ? { _meta: fixture.meta } : {}),
2377 },
2378 },
2379 },
2380 {
2381 type: "expect_outbound",
2382 frame: {
2383 kind: "response",
2384 method: "elicitation/create",
2385 result: expectedResult,
2386 },
2387 },
2388 {
2389 type: "emit_inbound",
2390 frame: {
2391 kind: "response",
2392 method: "session/prompt",
2393 result: { stopReason: "end_turn" },
2394 },
2395 },
2396 { type: "runtime_exit", status: "success" },
2397 ],
2398 },
2399 driver,
2400 );
2401 const instanceId = ProviderInstanceId.make("droid-form-test");
2402 const adapter = yield* makeAcpRegistryAdapterV2({
2403 instanceId,
2404 selfInvocation: yield* resolveSelfInvocation(),
2405 settings,
2406 environment: {},
2407 makeRuntime: makeAcpReplayRuntime({
2408 transcript,
2409 statusPath,
2410 fileSystem,
2411 scriptPath: yield* bundledReplayAgentPath,
2412 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
2413 }),
2414 }).pipe(
2415 Effect.provide(
2416 Layer.mock(AcpRegistryCatalog)({ resolve: () => Effect.die("runtime is injected") }),
2417 ),
2418 );
2419 const threadId = ThreadId.make("registry-form-thread");
2420 const modelSelection = { instanceId, model: "default" } as const;
2421 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
2422 runtimeMode: "approval-required",
2423 interactionMode: "default",
2424 cwd: directory,
2425 });
2426 const runtime = yield* adapter.openSession({
2427 threadId,
2428 modelSelection,
2429 runtimePolicy,
2430 providerSessionId: ProviderSessionId.make("droid-form-session"),
2431 });
2432 const providerThread = yield* runtime.ensureThread({
2433 threadId,
2434 modelSelection,
2435 runtimePolicy,
2436 });
2437 yield* runtime.startTurn(
2438 makeTurnInput({
2439 threadId,
2440 providerThread,
2441 instanceId,
2442 runtimePolicy,
2443 now: yield* DateTime.now,
2444 }),
2445 );
2446 const item = Option.getOrThrow(
2447 yield* runtime.events.pipe(
2448 Stream.filter(
2449 (event) =>
2450 event.type === "turn_item.updated" && event.turnItem.type === "user_input_request",
2451 ),
2452 Stream.runHead,
2453 ),
2454 );
2455 if (item.type !== "turn_item.updated" || item.turnItem.type !== "user_input_request")
2456 return yield* Effect.die("Expected a user input question");
2457 assert.deepEqual(item.turnItem.questions, questions);
2458 yield* runtime.respondToRuntimeRequest({
2459 requestId: item.turnItem.requestId,
2460 ...(preparedAnswers === undefined ? {} : { answers: preparedAnswers }),
2461 });
2462 const terminal = yield* runtime.events.pipe(
2463 Stream.filter((event) => event.type === "turn.terminal"),
2464 Stream.runHead,
2465 );
2466 assert.equal(Option.getOrThrow(terminal).status, "completed");
2467 }).pipe(Effect.provide(layerTest));
2468 });
2469
2470 it.live.each(["failed", "recovered", "cancelled"] as const)(
2471 "projects Mistral retry notices and their %s outcome",
2472 (outcome) =>
2473 Effect.gen(function* () {
2474 const instanceId = ProviderInstanceId.make(`vibe-retry-${outcome}`);
2475 const threadId = ThreadId.make(`thread-vibe-retry-${outcome}`);
2476 const adapter = yield* makeAcpAdapterV2({
2477 instanceId,
2478 selfInvocation: yield* resolveSelfInvocation(),
2479 flavor: {
2480 driver: ProviderDriverKind.make("acpRegistry"),
2481 capabilities: AcpProviderCapabilitiesV2,
2482 registerExtensions: registerMistralVibeAcpExtensions,
2483 promptFailure: (cause) => acpRegistryPromptFailure("mistral-vibe", cause),
2484 makeRuntime: makeMockRuntime({
2485 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
2486 mockAgentPath: yield* bundledMockAgentPath,
2487 environment: { T3_ACP_VIBE_RETRY_OUTCOME: outcome },
2488 }),
2489 },
2490 });
2491 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
2492 runtimeMode: "full-access",
2493 interactionMode: "default",
2494 cwd: process.cwd(),
2495 });
2496 const modelSelection = { instanceId, model: "default" } as const;
2497 const runtime = yield* adapter.openSession({
2498 threadId,
2499 providerSessionId: ProviderSessionId.make(`session-vibe-retry-${outcome}`),
2500 modelSelection,
2501 runtimePolicy,
2502 });
2503 const providerThread = yield* runtime.ensureThread({
2504 threadId,
2505 modelSelection,
2506 runtimePolicy,
2507 });
2508 yield* runtime.startTurn(
2509 makeTurnInput({
2510 threadId,
2511 providerThread,
2512 instanceId,
2513 runtimePolicy,
2514 now: yield* DateTime.now,
2515 }),
2516 );
2517 const events = yield* runtime.events.pipe(
2518 Stream.takeUntil((event) => event.type === "turn.terminal"),
2519 Stream.runCollect,
2520 );
2521 const retries = events.flatMap((event) =>
2522 event.type === "turn_item.updated" && event.turnItem.type === "error"
2523 ? [event.turnItem]
2524 : [],
2525 );
2526 const running = retries.filter((item) => item.status === "running");
2527 assert.deepEqual(
2528 running.map((item) => item.retry?.attempt),
2529 [1, 2],
2530 );
2531 assert.equal(new Set(retries.map((item) => item.id)).size, 1);
2532 assert.include(running[0]!.failure.message, "Rate limit reached");
2533 assert.notInclude(running[0]!.failure.message, "private-key");
2534 const terminal = events.find((event) => event.type === "turn.terminal");
2535 assert.isDefined(terminal);
2536 if (terminal?.type !== "turn.terminal") return yield* Effect.die("Missing terminal");
2537 if (outcome === "failed") {
2538 assert.equal(terminal.status, "failed");
2539 assert.equal(terminal.failure?.class, "usage_limit");
2540 assert.include(terminal.failure?.message ?? "", "Rate limit exceeded for mistral");
2541 if (terminal.status === "failed") assert.equal(terminal.retry?.attempt, 2);
2542 } else if (outcome === "cancelled") {
2543 assert.equal(terminal.status, "cancelled");
2544 assert.equal(retries.at(-1)?.status, "cancelled");
2545 assert.equal(retries.at(-1)?.title, "Provider retry stopped");
2546 } else {
2547 assert.equal(terminal.status, "completed");
2548 assert.equal(retries.at(-1)?.status, "completed");
2549 assert.equal(retries.at(-1)?.title, "Provider recovered");
2550 }
2551 }).pipe(Effect.provide(layerTest)),
2552 );
2553
2554 it.live.each([
2555 { modeIds: "plain", environment: {}, planModeId: "architect", buildModeId: "ask" },
2556 {
2557 modeIds: "Copilot URL",
2558 environment: { T3_ACP_COPILOT_MODES: "1" },
2559 planModeId: "https://agentclientprotocol.com/protocol/session-modes#plan",
2560 buildModeId: "https://agentclientprotocol.com/protocol/session-modes#agent",
2561 },
2562 {
2563 // Without a `mode` category, T3 derives no mode state and only the config option switches.
2564 modeIds: "Copilot URL collaboration_mode",
2565 environment: { T3_ACP_COPILOT_MODES: "1", T3_ACP_MODE_OPTION_CATEGORY: "collaboration_mode" },
2566 planModeId: "https://agentclientprotocol.com/protocol/session-modes#plan",
2567 buildModeId: "https://agentclientprotocol.com/protocol/session-modes#agent",
2568 },
2569 ])(
2570 "refreshes ACP prompt instructions and native mode when the interaction mode changes ($modeIds mode ids)",
2571 ({ environment, planModeId, buildModeId }) =>
2572 Effect.gen(function* () {
2573 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
2574 const selfInvocation = yield* resolveSelfInvocation();
2575 const mockAgentPath = yield* bundledMockAgentPath;
2576 const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
2577 const instanceId = ProviderInstanceId.make("acp-test-instruction-transitions");
2578 const threadId = ThreadId.make("thread-acp-instruction-transitions");
2579 yield* (yield* McpProviderSessions.McpProviderSessions).set({
2580 environmentId: EnvironmentId.make("environment-acp-instruction-transitions"),
2581 threadId,
2582 providerSessionId: "mcp-session-acp-instruction-transitions",
2583 providerInstanceId: instanceId,
2584 endpoint: "http://127.0.0.1:43123/mcp",
2585 authorizationHeader: "Bearer instruction-transition-token",
2586 browserToolsAvailable: false,
2587 });
2588 const adapter = yield* makeAcpAdapterV2({
2589 instanceId,
2590 flavor: {
2591 driver: ACP_TEST_DRIVER,
2592 capabilities: AcpProviderCapabilitiesV2,
2593 makeRuntime: makeMockRuntime({
2594 childProcessSpawner,
2595 mockAgentPath,
2596 protocolEvents,
2597 environment,
2598 }),
2599 },
2600 selfInvocation,
2601 });
2602 const policy = (interactionMode: "default" | "plan") =>
2603 ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
2604 runtimeMode: "full-access",
2605 interactionMode,
2606 cwd: process.cwd(),
2607 });
2608 const defaultPolicy = policy("default");
2609 const modelSelection = { instanceId, model: "default" } as const;
2610 const runtime = yield* adapter.openSession({
2611 threadId,
2612 providerSessionId: ProviderSessionId.make("provider-session-acp-instruction-transitions"),
2613 modelSelection,
2614 runtimePolicy: defaultPolicy,
2615 });
2616 const providerThread = yield* runtime.ensureThread({
2617 threadId,
2618 modelSelection,
2619 runtimePolicy: defaultPolicy,
2620 });
2621 const now = yield* DateTime.now;
2622 const runTurn = Effect.fnUntraced(function* (
2623 ordinal: number,
2624 runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy,
2625 messageText: string,
2626 turnModelSelection: ModelSelection = modelSelection,
2627 ) {
2628 yield* runtime.startTurn(
2629 makeTurnInput({
2630 threadId,
2631 providerThread,
2632 instanceId,
2633 runtimePolicy,
2634 now,
2635 ordinal,
2636 messageText,
2637 modelSelection: turnModelSelection,
2638 }),
2639 );
2640 yield* runtime.events.pipe(
2641 Stream.takeUntil((event) => event.type === "turn.terminal"),
2642 Stream.runDrain,
2643 );
2644 const methods: Array<string> = [];
2645 const modeSets: Array<unknown> = [];
2646 while (true) {
2647 const event = yield* Queue.take(protocolEvents);
2648 if (event.direction !== "outgoing") continue;
2649 const method = rawProtocolMethod(event);
2650 if (method === undefined) continue;
2651 methods.push(method);
2652 if (
2653 method === "session/set_config_option" &&
2654 rawProtocolRequestParam(event, "configId") === "mode"
2655 ) {
2656 modeSets.push(rawProtocolRequestParam(event, "value"));
2657 }
2658 if (method === "session/prompt") {
2659 const promptBlocks = rawProtocolPromptTextBlocks(event);
2660 return {
2661 methods,
2662 modeSets,
2663 prompt: promptBlocks.map((block) => block.text).join("\n"),
2664 textBlockCount: promptBlocks.length,
2665 };
2666 }
2667 }
2668 });
2669
2670 const command = yield* runTurn(0, defaultPolicy, "/compact");
2671 assert.isTrue(command.prompt.startsWith("/compact"));
2672 assert.notInclude(command.prompt, "<t3_code_instructions>");
2673 assert.equal(command.textBlockCount, 1);
2674 const whitespaceCommand = yield* runTurn(1, defaultPolicy, " /repro-skill");
2675 assert.equal(whitespaceCommand.prompt, " /repro-skill");
2676 assert.equal(whitespaceCommand.textBlockCount, 1);
2677 assert.notInclude(whitespaceCommand.prompt, "<t3_code_instructions>");
2678 const firstDefault = yield* runTurn(2, defaultPolicy, "First default request.");
2679 assert.equal(firstDefault.textBlockCount, 2);
2680 assert.include(firstDefault.prompt, "T3 Code interaction mode: Default");
2681 assert.include(firstDefault.prompt, "## T3 Code browser");
2682 assert.include(firstDefault.prompt, "T3 Code orchestration");
2683 assert.include(firstDefault.prompt, "<runtime_info>");
2684 assert.notInclude(
2685 firstDefault.methods,
2686 "session/set_config_option",
2687 "Build should preserve the agent's advertised mode default",
2688 );
2689 const secondDefault = yield* runTurn(3, defaultPolicy, "Second default request.");
2690 assert.equal(secondDefault.prompt, "Second default request.");
2691 assert.equal(secondDefault.textBlockCount, 1);
2692
2693 const planPolicy = policy("plan");
2694 const firstPlan = yield* runTurn(4, planPolicy, "Plan this change.");
2695 assert.include(firstPlan.prompt, "T3 Code interaction mode: Plan");
2696 assert.deepEqual(firstPlan.modeSets, [planModeId]);
2697 assert.include(
2698 (yield* runTurn(5, planPolicy, "Continue planning.")).prompt,
2699 "Continue planning.",
2700 );
2701 const restoredBuild = yield* runTurn(6, defaultPolicy, "Implement the change.");
2702 assert.include(restoredBuild.prompt, "T3 Code interaction mode: Default");
2703 assert.deepEqual(
2704 restoredBuild.modeSets,
2705 [buildModeId],
2706 "Build should restore the native mode that T3 temporarily replaced for Plan",
2707 );
2708 const switchedModel = yield* runTurn(7, defaultPolicy, "Switch models.", {
2709 instanceId,
2710 model: "composer-2",
2711 });
2712 assert.equal(switchedModel.textBlockCount, 2);
2713 assert.include(switchedModel.prompt, "as composer-2");
2714 }).pipe(Effect.provide(layerTest)),
2715 );
2716
2717 it.live.each(["cancelled", "refusal", "error"] as const)(
2718 "reinjects ACP instructions after protocol compaction, during reinjection, prompt preparation, or while idle (%s)",
2719 (stopReason) =>
2720 Effect.gen(function* () {
2721 const {
2722 runTurn,
2723 idleSessionUpdate,
2724 idleCopilotSessionEvent,
2725 sessionUpdateDuringNextPreparation,
2726 reportNextPromptStopped,
2727 } = yield* makeInstructionCompactionHarness(
2728 {
2729 T3_ACP_COMPACTION_PROMPT_ORDINALS: "2,4",
2730 T3_ACP_OPEN_COMPACTION_PROMPT_ORDINALS: "13",
2731 },
2732 stopReason === "error",
2733 );
2734 const expectedStatus = stopReason === "cancelled" ? "cancelled" : "failed";
2735 yield* reportNextPromptStopped(stopReason);
2736 assertReinjectedInstructions(yield* runTurn("Start work.", expectedStatus), "Start work.");
2737 assertReinjectedInstructions(
2738 yield* runTurn("Continue until context compacts."),
2739 "Continue until context compacts.",
2740 );
2741 assert.deepEqual(yield* runTurn(" /repro-skill"), [
2742 { type: "text", text: stopReason === "error" ? "/repro-skill" : " /repro-skill" },
2743 ]);
2744 assertReinjectedInstructions(
2745 yield* runTurn("Continue after compaction."),
2746 "Continue after compaction.",
2747 );
2748 assertReinjectedInstructions(
2749 yield* runTurn("Continue after the second compaction."),
2750 "Continue after the second compaction.",
2751 );
2752 assert.deepEqual(yield* runTurn("Finish work."), [{ type: "text", text: "Finish work." }]);
2753
2754 yield* idleSessionUpdate({
2755 sessionUpdate: "compaction_update",
2756 compactionId: "compact-idle",
2757 status: "completed",
2758 });
2759 assertReinjectedInstructions(
2760 yield* runTurn("Continue after idle compaction."),
2761 "Continue after idle compaction.",
2762 );
2763 yield* idleSessionUpdate({
2764 sessionUpdate: "compaction_update",
2765 compactionId: "compact-idle",
2766 status: "completed",
2767 });
2768 assert.deepEqual(yield* runTurn("Continue after a repeated compaction update."), [
2769 { type: "text", text: "Continue after a repeated compaction update." },
2770 ]);
2771 const copilotCompaction = { success: true, outcome: "applied", trigger: "threshold" };
2772 yield* idleCopilotSessionEvent({
2773 type: "session.compaction_complete",
2774 agentId: "agent-a",
2775 data: copilotCompaction,
2776 });
2777 assert.deepEqual(yield* runTurn("Continue after subagent compaction."), [
2778 { type: "text", text: "Continue after subagent compaction." },
2779 ]);
2780 yield* idleCopilotSessionEvent({
2781 type: "session.compaction_complete",
2782 data: copilotCompaction,
2783 });
2784 assertReinjectedInstructions(
2785 yield* runTurn("Continue after Copilot compaction."),
2786 "Continue after Copilot compaction.",
2787 );
2788 yield* idleCopilotSessionEvent({
2789 type: "session.compaction_complete",
2790 data: { success: true, trigger: "threshold" },
2791 });
2792 assertReinjectedInstructions(
2793 yield* runTurn("Continue after legacy Copilot compaction."),
2794 "Continue after legacy Copilot compaction.",
2795 );
2796 yield* idleSessionUpdate({
2797 sessionUpdate: "compaction_update",
2798 compactionId: "compact-child",
2799 status: "completed",
2800 _meta: { "cognition.ai/subagent_context": { parentAgentId: "agent-b" } },
2801 });
2802 assert.deepEqual(yield* runTurn("Continue after child compaction."), [
2803 { type: "text", text: "Continue after child compaction." },
2804 ]);
2805 yield* runTurn("End this turn while compaction is still running.");
2806 assertReinjectedInstructions(
2807 yield* runTurn("Continue after the turn ended mid-compaction."),
2808 "Continue after the turn ended mid-compaction.",
2809 );
2810 yield* idleSessionUpdate({
2811 sessionUpdate: "compaction_update",
2812 compactionId: "compact-13",
2813 status: "completed",
2814 });
2815 assertReinjectedInstructions(
2816 yield* runTurn("Continue after the compaction really finished."),
2817 "Continue after the compaction really finished.",
2818 );
2819 yield* sessionUpdateDuringNextPreparation({
2820 sessionUpdate: "compaction_update",
2821 compactionId: "compact-while-preparing",
2822 status: "completed",
2823 });
2824 assertReinjectedInstructions(
2825 yield* runTurn("Continue after a compaction during prompt preparation."),
2826 "Continue after a compaction during prompt preparation.",
2827 );
2828 assert.deepEqual(yield* runTurn("Finish after that reinjection."), [
2829 { type: "text", text: "Finish after that reinjection." },
2830 ]);
2831 yield* idleSessionUpdate({
2832 sessionUpdate: "compaction_update",
2833 compactionId: "compact-before-undelivered-prompt",
2834 status: "completed",
2835 });
2836 yield* reportNextPromptStopped(stopReason);
2837 assertReinjectedInstructions(
2838 yield* runTurn("Do not retain this prompt.", expectedStatus),
2839 "Do not retain this prompt.",
2840 );
2841 assertReinjectedInstructions(
2842 yield* runTurn("Continue after the undelivered prompt."),
2843 "Continue after the undelivered prompt.",
2844 );
2845 }).pipe(Effect.provide(layerTest)),
2846 );
2847
2848 it.live(
2849 "reinjects ACP instructions after successful native compact, but not failed compact",
2850 () =>
2851 Effect.gen(function* () {
2852 const { runTurn } = yield* makeInstructionCompactionHarness({
2853 T3_ACP_FAIL_PROMPT_ORDINAL: "2",
2854 });
2855 yield* runTurn("Start work.");
2856 assert.deepEqual(yield* runTurn("/compact", "failed"), [
2857 { type: "text", text: "/compact" },
2858 ]);
2859 assert.deepEqual(yield* runTurn("Continue after failure."), [
2860 { type: "text", text: "Continue after failure." },
2861 ]);
2862 assert.deepEqual(yield* runTurn("/compact"), [{ type: "text", text: "/compact" }]);
2863 assert.deepEqual(yield* runTurn("/repro-skill"), [{ type: "text", text: "/repro-skill" }]);
2864 assertReinjectedInstructions(yield* runTurn("Continue work."), "Continue work.");
2865 assert.deepEqual(yield* runTurn("Finish work."), [{ type: "text", text: "Finish work." }]);
2866 // Agents such as Grok accept preservation instructions after the command.
2867 assert.deepEqual(yield* runTurn("/compact keep the plan"), [
2868 { type: "text", text: "/compact keep the plan" },
2869 ]);
2870 assertReinjectedInstructions(
2871 yield* runTurn("Continue after the focused compact."),
2872 "Continue after the focused compact.",
2873 );
2874 }).pipe(Effect.provide(layerTest)),
2875 );
2876
2877 it.live.each([false, true])(
2878 "sends a bare /compact prompt with protocol compaction %s",
2879 (structured) =>
2880 Effect.gen(function* () {
2881 const harness = yield* makeAdvertisedCompactionHarness({
2882 T3_ACP_AVAILABLE_COMMANDS: "compact,plan",
2883 T3_ACP_COMPACTION_PROMPT_ORDINALS: structured ? "1" : "",
2884 });
2885 const compacted = yield* harness.compact();
2886 assert.isTrue(Exit.isSuccess(compacted));
2887 assert.deepEqual(yield* harness.nextPromptTexts(), [{ type: "text", text: "/compact" }]);
2888 const events = yield* harness.runtime.events.pipe(
2889 Stream.takeUntil((event) => event.type === "turn.terminal"),
2890 Stream.runCollect,
2891 );
2892 assert.isTrue(
2893 events.some(
2894 (event) =>
2895 event.type === "provider_thread.updated" &&
2896 event.providerThread.contextUsage === null,
2897 ),
2898 );
2899 assert.isTrue(
2900 events.some(
2901 (event) =>
2902 event.type === "turn_item.updated" &&
2903 event.turnItem.type === "compaction" &&
2904 event.turnItem.status === "completed" &&
2905 event.turnItem.title === (structured ? "Compact context" : "Context compacted"),
2906 ),
2907 );
2908 assert.lengthOf(
2909 events.filter(
2910 (event) =>
2911 event.type === "turn_item.updated" &&
2912 event.turnItem.type === "compaction" &&
2913 event.turnItem.status === "completed",
2914 ),
2915 1,
2916 );
2917 }).pipe(Effect.provide(layerTest)),
2918 );
2919
2920 it.live.each([
2921 { active: "plan", target: "compact", allowed: true },
2922 // regression: #39880 omp advertises a loaded session's commands ~50 ms after session/load.
2923 { active: "plan", target: "compact", allowed: true, loadedCommandsDelayMs: 50 },
2924 { active: "compact", target: "plan", allowed: false },
2925 { active: "compact", target: undefined, allowed: false },
2926 ])(
2927 "validates the target session's compact command ($active -> $target)",
2928 ({ active, target, allowed, loadedCommandsDelayMs }) =>
2929 Effect.gen(function* () {
2930 const harness = yield* makeAdvertisedCompactionHarness({
2931 T3_ACP_AVAILABLE_COMMANDS: active,
2932 ...(target === undefined ? {} : { T3_ACP_LOADED_COMMANDS: target }),
2933 ...(loadedCommandsDelayMs === undefined
2934 ? {}
2935 : { T3_ACP_LOADED_COMMANDS_DELAY_MS: String(loadedCommandsDelayMs) }),
2936 });
2937 const result = yield* harness.compact(true);
2938 assert.equal(Exit.isSuccess(result), allowed);
2939 if (allowed) {
2940 assert.deepEqual(yield* harness.nextPromptTexts(), [{ type: "text", text: "/compact" }]);
2941 yield* harness.runtime.events.pipe(
2942 Stream.takeUntil((event) => event.type === "turn.terminal"),
2943 Stream.runDrain,
2944 );
2945 } else {
2946 const error = Exit.isFailure(result) ? Cause.squash(result.cause) : undefined;
2947 assert.instanceOf(error, ProviderAdapter.ProviderAdapterTurnStartError);
2948 assert.instanceOf(
2949 (error as ProviderAdapter.ProviderAdapterTurnStartError).cause,
2950 ProviderAdapter.ProviderCompactionUnsupportedError,
2951 );
2952 }
2953 }).pipe(Effect.provide(layerTest)),
2954 );
2955
2956 it.live("reapplies a selection after a switch away from it failed partway", () =>
2957 Effect.gen(function* () {
2958 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
2959 const mockAgentPath = yield* bundledMockAgentPath;
2960 const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
2961 const instanceId = ProviderInstanceId.make("acp-test-partial-selection-reapply");
2962 const threadId = ThreadId.make("thread-acp-partial-selection-reapply");
2963 const adapter = yield* makeAcpAdapterV2({
2964 instanceId,
2965 flavor: {
2966 driver: ACP_TEST_DRIVER,
2967 capabilities: AcpProviderCapabilitiesV2,
2968 makeRuntime: makeMockRuntime({
2969 childProcessSpawner,
2970 mockAgentPath,
2971 protocolEvents,
2972 environment: { T3_ACP_FAIL_SET_MODE_CONFIG_OPTION: "1" },
2973 }),
2974 },
2975 selfInvocation: yield* resolveSelfInvocation(),
2976 });
2977 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
2978 runtimeMode: "full-access",
2979 interactionMode: "default",
2980 cwd: process.cwd(),
2981 });
2982 const selectionA = { instanceId, model: "composer-2" } as const;
2983 const selectionB = {
2984 instanceId,
2985 model: "composer-2[fast=true]",
2986 options: [{ id: ACP_SESSION_MODE_OPTION_ID, value: "code" }],
2987 } as const;
2988 const runtime = yield* adapter.openSession({
2989 threadId,
2990 providerSessionId: ProviderSessionId.make("provider-session-acp-partial-selection"),
2991 modelSelection: selectionA,
2992 runtimePolicy,
2993 });
2994 const providerThread = yield* runtime.ensureThread({
2995 threadId,
2996 modelSelection: selectionA,
2997 runtimePolicy,
2998 });
2999 const now = yield* DateTime.now;
3000 const turnInput = (ordinal: number, modelSelection: ModelSelection) =>
3001 makeTurnInput({
3002 threadId,
3003 providerThread,
3004 instanceId,
3005 runtimePolicy,
3006 now,
3007 ordinal,
3008 modelSelection,
3009 });
3010
3011 const switchToB = yield* Effect.exit(runtime.startTurn(turnInput(1, selectionB)));
3012 assert.isTrue(Exit.isFailure(switchToB), "the agent rejected B's mode step");
3013 yield* Queue.clear(protocolEvents);
3014
3015 yield* runtime.startTurn(turnInput(2, selectionA));
3016 yield* runtime.events.pipe(
3017 Stream.takeUntil((event) => event.type === "turn.terminal"),
3018 Stream.runDrain,
3019 );
3020 const modelSetsBeforePrompt: Array<unknown> = [];
3021 while (true) {
3022 const event = yield* Queue.take(protocolEvents);
3023 if (event.direction !== "outgoing") continue;
3024 const method = rawProtocolMethod(event);
3025 if (method === "session/prompt") break;
3026 if (
3027 method === "session/set_config_option" &&
3028 rawProtocolRequestParam(event, "configId") === "model"
3029 ) {
3030 modelSetsBeforePrompt.push(rawProtocolRequestParam(event, "value"));
3031 }
3032 }
3033 assert.deepEqual(
3034 modelSetsBeforePrompt,
3035 ["composer-2"],
3036 "the native model was left on B, so the turn on A must set it back first",
3037 );
3038 }).pipe(Effect.provide(layerTest)),
3039 );
3040
3041 // regression: #21499 turns blocked only on T3 thread waits must show as waiting
3042 it.effect.each(["agent_message_chunk"] as const)(
3043 "keeps named ACP %s open while deriving thread wait execution state",
3044 (sessionUpdate) =>
3045 Effect.gen(function* () {
3046 type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"];
3047 let handler: Parameters<RuntimeService["handleSessionUpdate"]>[0] | undefined;
3048 const instanceId = ProviderInstanceId.make("acp-named-stream-wait");
3049 const adapter = yield* makeAcpAdapterV2({
3050 instanceId,
3051 selfInvocation: yield* resolveSelfInvocation(),
3052 flavor: {
3053 driver: ACP_TEST_DRIVER,
3054 capabilities: AcpProviderCapabilitiesV2,
3055 makeRuntime: makeMockRuntime({
3056 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
3057 mockAgentPath: yield* bundledMockAgentPath,
3058 wrapRuntime: (runtime) => ({
3059 ...runtime,
3060 handleSessionUpdate: (next) =>
3061 Effect.sync(() => {
3062 handler = next;
3063 }).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
3064 prompt: () =>
3065 Effect.gen(function* () {
3066 assert.isDefined(handler);
3067 const updates = [
3068 {
3069 sessionUpdate,
3070 messageId: "named-stream",
3071 content: { type: "text", text: "Waiting for the child result" },
3072 },
3073 {
3074 sessionUpdate: "tool_call",
3075 toolCallId: "wait",
3076 title: "Wait for the child",
3077 status: "in_progress",
3078 rawInput: {
3079 server: "t3-code",
3080 tool: "t3_thread_wait",
3081 arguments: { threadId: "child" },
3082 },
3083 _meta: { is_mcp_tool_call: true },
3084 },
3085 {
3086 sessionUpdate: "tool_call",
3087 toolCallId: "command",
3088 kind: "execute",
3089 title: "Run command",
3090 status: "in_progress",
3091 rawInput: { command: "pwd" },
3092 },
3093 {
3094 sessionUpdate: "tool_call_update",
3095 toolCallId: "command",
3096 status: "completed",
3097 },
3098 {
3099 sessionUpdate: "tool_call_update",
3100 toolCallId: "wait",
3101 status: "completed",
3102 },
3103 ] satisfies Array<EffectAcpSchema.SessionUpdate>;
3104 for (const update of updates)
3105 yield* handler!({ sessionId: "mock-session-1", update });
3106 return { stopReason: "end_turn" as const };
3107 }),
3108 }),
3109 }),
3110 },
3111 });
3112 const threadId = ThreadId.make("thread-acp-named-stream-wait");
3113 const modelSelection = { instanceId, model: "default" };
3114 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3115 runtimeMode: "full-access",
3116 interactionMode: "default",
3117 cwd: process.cwd(),
3118 });
3119 const runtime = yield* adapter.openSession({
3120 threadId,
3121 providerSessionId: ProviderSessionId.make("session-acp-named-stream-wait"),
3122 modelSelection,
3123 runtimePolicy,
3124 });
3125 const providerThread = yield* runtime.ensureThread({
3126 threadId,
3127 modelSelection,
3128 runtimePolicy,
3129 });
3130 const input = makeTurnInput({
3131 threadId,
3132 providerThread,
3133 instanceId,
3134 runtimePolicy,
3135 now: yield* DateTime.now,
3136 });
3137 yield* runtime.startTurn(input);
3138 const events = yield* runtime.events.pipe(
3139 Stream.takeUntil((event) => event.type === "turn.terminal"),
3140 Stream.runCollect,
3141 );
3142 const retained = new Map<string, OrchestrationV2TurnItem>();
3143 const states: Array<boolean> = [];
3144 for (const event of events) {
3145 if (event.type !== "turn_item.updated") continue;
3146 const item = event.turnItem;
3147 retained.set(item.id, item);
3148 if (item.type !== "dynamic_tool" && item.type !== "command_execution") continue;
3149 const streams = [...retained.values()].filter(
3150 (entry) => entry.type === "assistant_message" || entry.type === "reasoning",
3151 );
3152 assert.deepEqual(
3153 streams.map((entry) => [entry.type, entry.status]),
3154 [
3155 [
3156 sessionUpdate === "agent_message_chunk" ? "assistant_message" : "reasoning",
3157 "running",
3158 ],
3159 ],
3160 );
3161 states.push(
3162 deriveBlockedOnThreadWait({
3163 activeRun: { id: input.runId, status: "running" },
3164 turnItems: [...retained.values()],
3165 }),
3166 );
3167 }
3168 assert.deepEqual(states, [true, false, true, false]);
3169 }).pipe(Effect.provide(layerTest)),
3170 );
3171
3172 const textChunk = (
3173 sessionUpdate: "agent_message_chunk" | "agent_thought_chunk",
3174 messageId: string | undefined,
3175 text: string,
3176 ) => ({
3177 sessionUpdate,
3178 ...(messageId === undefined ? {} : { messageId }),
3179 content: { type: "text" as const, text },
3180 });
3181 const answer = (messageId: string | undefined, text: string) =>
3182 textChunk("agent_message_chunk", messageId, text);
3183 const thought = (messageId: string | undefined, text: string) =>
3184 textChunk("agent_thought_chunk", messageId, text);
3185 const agentMessage = (messageId: string, text: string) => ({
3186 sessionUpdate: "agent_message" as const,
3187 messageId,
3188 content: [{ type: "text" as const, text }],
3189 });
3190 type ScriptedTextStep = EffectAcpSchema.SessionUpdate | { readonly advance: Duration.Input };
3191
3192 const runScriptedTextTurn = Effect.fnUntraced(function* (steps: ReadonlyArray<ScriptedTextStep>) {
3193 type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"];
3194 let handler: Parameters<RuntimeService["handleSessionUpdate"]>[0] | undefined;
3195 const instanceId = ProviderInstanceId.make("acp-named-text-resume");
3196 const adapter = yield* makeAcpAdapterV2({
3197 instanceId,
3198 selfInvocation: yield* resolveSelfInvocation(),
3199 flavor: {
3200 driver: ACP_TEST_DRIVER,
3201 capabilities: AcpProviderCapabilitiesV2,
3202 makeRuntime: makeMockRuntime({
3203 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
3204 mockAgentPath: yield* bundledMockAgentPath,
3205 wrapRuntime: (runtime) => ({
3206 ...runtime,
3207 handleSessionUpdate: (next) =>
3208 Effect.sync(() => {
3209 handler = next;
3210 }).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
3211 prompt: () =>
3212 Effect.gen(function* () {
3213 assert.isDefined(handler);
3214 for (const step of steps) {
3215 if ("advance" in step) yield* TestClock.adjust(step.advance);
3216 else yield* handler!({ sessionId: "mock-session-1", update: step });
3217 }
3218 return { stopReason: "end_turn" as const };
3219 }),
3220 }),
3221 }),
3222 },
3223 });
3224 const threadId = ThreadId.make("thread-acp-named-text-resume");
3225 const modelSelection = { instanceId, model: "default" };
3226 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3227 runtimeMode: "full-access",
3228 interactionMode: "default",
3229 cwd: process.cwd(),
3230 });
3231 const runtime = yield* adapter.openSession({
3232 threadId,
3233 providerSessionId: ProviderSessionId.make("session-acp-named-text-resume"),
3234 modelSelection,
3235 runtimePolicy,
3236 });
3237 const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy });
3238 yield* runtime.startTurn(
3239 makeTurnInput({
3240 threadId,
3241 providerThread,
3242 instanceId,
3243 runtimePolicy,
3244 now: yield* DateTime.now,
3245 ordinal: 1,
3246 }),
3247 );
3248 return yield* runtime.events.pipe(
3249 Stream.takeUntil((event) => event.type === "turn.terminal"),
3250 Stream.runCollect,
3251 );
3252 });
3253
3254 it.effect.each([
3255 {
3256 label: "a thought with its own id interrupts a named assistant message",
3257 steps: [
3258 answer("answer", "First. "),
3259 { advance: "1 second" },
3260 thought("thought", "Thinking"),
3261 answer("answer", "Second."),
3262 ],
3263 expected: [
3264 ["assistant_message", "First. Second."],
3265 ["reasoning", "Thinking"],
3266 ],
3267 },
3268 {
3269 label: "named assistant messages interleave",
3270 steps: [answer("a", "A1 "), answer("b", "B1"), answer("a", "A2")],
3271 expected: [
3272 ["assistant_message", "A1 A2"],
3273 ["assistant_message", "B1"],
3274 ],
3275 },
3276 {
3277 label: "a resumed message continues from authoritative text",
3278 steps: [
3279 answer("answer", "stale"),
3280 thought("thought", "Thinking"),
3281 agentMessage("answer", "fresh "),
3282 thought("thought", " more"),
3283 answer("answer", "tail"),
3284 ],
3285 expected: [
3286 ["assistant_message", "fresh tail"],
3287 ["reasoning", "Thinking more"],
3288 ],
3289 },
3290 {
3291 label: "opaque ids differ only in surrounding whitespace",
3292 steps: [answer("answer", "A"), answer(" answer ", "B")],
3293 expected: [
3294 ["assistant_message", "A"],
3295 ["assistant_message", "B"],
3296 ],
3297 },
3298 {
3299 label: "a chunk continues a full update with the same whitespace-padded id",
3300 steps: [agentMessage(" answer ", "A"), answer(" answer ", "B")],
3301 expected: [["assistant_message", "AB"]],
3302 },
3303 ] satisfies ReadonlyArray<{
3304 readonly label: string;
3305 readonly steps: ReadonlyArray<ScriptedTextStep>;
3306 readonly expected: ReadonlyArray<readonly [string, string]>;
3307 }>)("resumes ACP text segments when $label", ({ steps, expected }) =>
3308 Effect.gen(function* () {
3309 const events = yield* runScriptedTextTurn(steps);
3310 const history = new Map<string, Array<OrchestrationV2TurnItem>>();
3311 for (const event of events) {
3312 if (event.type !== "turn_item.updated") continue;
3313 const item = event.turnItem;
3314 if (item.nativeItemRef?.nativeId?.includes(":turn:") !== true) continue;
3315 history.set(item.id, [...(history.get(item.id) ?? []), item]);
3316 }
3317 const texts = [...history.values()].flatMap((snapshots) => {
3318 const last = snapshots.at(-1);
3319 return last !== undefined && "text" in last ? [[last.type, last.text]] : [];
3320 });
3321 assert.deepEqual(texts, expected);
3322 for (const snapshots of history.values()) {
3323 assert.equal(new Set(snapshots.map((item) => item.ordinal)).size, 1);
3324 assert.equal(new Set(snapshots.map((item) => DateTime.formatIso(item.startedAt!))).size, 1);
3325 assert.equal(snapshots.at(-1)?.status, "completed");
3326 }
3327 }).pipe(Effect.provide(layerTest)),
3328 );
3329
3330 it.effect("ignores a text flush scheduled before an ACP segment resumed", () =>
3331 Effect.gen(function* () {
3332 const events = yield* runScriptedTextTurn([
3333 answer("answer", "A"),
3334 answer("answer", "B"),
3335 thought("thought", "Thinking"),
3336 { advance: "30 millis" },
3337 answer("answer", "C"),
3338 answer("answer", "D"),
3339 { advance: "20 millis" },
3340 answer("answer", "E"),
3341 { advance: "30 millis" },
3342 ]);
3343 const snapshots = events.flatMap((event) =>
3344 event.type === "message.updated" && event.message.role === "assistant"
3345 ? [[event.message.text, event.message.streaming]]
3346 : [],
3347 );
3348 assert.deepEqual(snapshots, [
3349 ["A", true],
3350 ["AB", false],
3351 ["ABC", true],
3352 ["ABCDE", true],
3353 ["ABCDE", false],
3354 ]);
3355 }).pipe(Effect.provide(layerTest)),
3356 );
3357
3358 it.live("replaces and removes identified ACP plans across turns", () =>
3359 Effect.gen(function* () {
3360 const instanceId = ProviderInstanceId.make("acp-cross-turn-plans");
3361 const threadId = ThreadId.make("acp-cross-turn-plans");
3362 let notify:
3363 | Parameters<AcpSessionRuntime.AcpSessionRuntime["Service"]["handleSessionUpdate"]>[0]
3364 | undefined;
3365 let ordinal = 0;
3366 const adapter = yield* makeAcpAdapterV2({
3367 instanceId,
3368 selfInvocation: yield* resolveSelfInvocation(),
3369 flavor: {
3370 driver: ACP_TEST_DRIVER,
3371 capabilities: AcpProviderCapabilitiesV2,
3372 makeRuntime: makeMockRuntime({
3373 childProcessSpawner: yield* ChildProcessSpawner.ChildProcessSpawner,
3374 mockAgentPath: yield* bundledMockAgentPath,
3375 wrapRuntime: (runtime) => ({
3376 ...runtime,
3377 handleSessionUpdate: (handler) =>
3378 Effect.sync(() => {
3379 notify = handler;
3380 }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))),
3381 prompt: () =>
3382 Effect.gen(function* () {
3383 ordinal++;
3384 const send = (update: EffectAcpSchema.SessionUpdate) =>
3385 notify!({ sessionId: "mock-session-1", update });
3386 const plan = (id: string, text: string) =>
3387 send({
3388 sessionUpdate: "plan_update",
3389 plan: {
3390 type: "items",
3391 planId: id,
3392 entries: [{ content: text, priority: "medium", status: "pending" }],
3393 },
3394 });
3395 yield* send({ sessionUpdate: "state_update", state: "running" });
3396 if (ordinal === 1) {
3397 yield* plan("control", "First");
3398 yield* plan("control", "Replacement");
3399 yield* send({ sessionUpdate: "plan_removed", planId: "control" });
3400 yield* plan("later-remove", "Remove on next turn");
3401 yield* plan("later-replace", "Old contents");
3402 } else {
3403 yield* send({ sessionUpdate: "plan_removed", planId: "later-remove" });
3404 yield* plan("later-replace", "New contents");
3405 }
3406 yield* send({
3407 sessionUpdate: "plan",
3408 entries: [{ content: "Anonymous plan", priority: "medium", status: "pending" }],
3409 });
3410 yield* send({
3411 sessionUpdate: "state_update",
3412 state: "idle",
3413 stopReason: "end_turn",
3414 });
3415 return { stopReason: "end_turn" as const };
3416 }),
3417 }),
3418 }),
3419 },
3420 });
3421 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3422 runtimeMode: "full-access",
3423 interactionMode: "default",
3424 cwd: process.cwd(),
3425 });
3426 const modelSelection = { instanceId, model: "default" } as const;
3427 const runtime = yield* adapter.openSession({
3428 threadId,
3429 providerSessionId: ProviderSessionId.make("acp-cross-turn-plans"),
3430 modelSelection,
3431 runtimePolicy,
3432 });
3433 const providerThread = yield* runtime.ensureThread({
3434 threadId,
3435 modelSelection,
3436 runtimePolicy,
3437 });
3438 const now = yield* DateTime.now;
3439 const run = (ordinal: number) =>
3440 Effect.gen(function* () {
3441 yield* runtime.startTurn(
3442 makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal }),
3443 );
3444 return Array.from(
3445 yield* runtime.events.pipe(
3446 Stream.takeUntil((event) => event.type === "turn.terminal"),
3447 Stream.runCollect,
3448 ),
3449 ).flatMap((event) => (event.type === "plan.updated" ? [event.plan] : []));
3450 });
3451 const first = yield* run(1);
3452 assert.equal(first.length, 6);
3453 assert.equal(first[0]!.id, first[1]!.id);
3454 assert.equal(first[1]!.id, first[2]!.id);
3455 assert.equal(first[1]!.kind, "todo_list");
3456 assert.deepEqual(first[1]!.kind === "todo_list" ? first[1]!.steps.map((s) => s.text) : [], [
3457 "Replacement",
3458 ]);
3459 assert.equal(first[2]!.status, "superseded");
3460 const second = yield* run(2);
3461 assert.equal(second.length, 3);
3462 assert.notEqual(first[5]!.id, second[2]!.id);
3463 const removed = second.find((p) => p.id === first[3]!.id && p.status === "superseded");
3464 const replacement = second.find(
3465 (p) => p.kind === "todo_list" && p.steps.some((s) => s.text === "New contents"),
3466 );
3467 assert.isDefined(
3468 removed,
3469 "plan_removed on a later turn must supersede the existing session plan",
3470 );
3471 assert.equal(removed?.runId, first[3]!.runId);
3472 assert.equal(replacement?.runId, first[4]!.runId);
3473 assert.equal(removed?.nodeId, first[3]!.nodeId);
3474 assert.equal(replacement?.nodeId, first[4]!.nodeId);
3475 assert.equal(
3476 replacement?.id,
3477 first[4]!.id,
3478 "the same session plan must be replaced, not duplicated",
3479 );
3480 }).pipe(Effect.provide(layerTest), Effect.scoped),
3481 );
3482
3483 it.effect("starts a new replay message after ACP v2 plan boundaries", () =>
3484 Effect.gen(function* () {
3485 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3486 const selfInvocation = yield* resolveSelfInvocation();
3487 const mockAgentPath = yield* bundledMockAgentPath;
3488 type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"];
3489 let sessionUpdateHandler: Parameters<RuntimeService["handleSessionUpdate"]>[0] | undefined;
3490 const instanceId = ProviderInstanceId.make("acp-test-v2-plan-replay-boundary");
3491 const adapter = yield* makeAcpAdapterV2({
3492 instanceId,
3493 flavor: {
3494 driver: ACP_TEST_DRIVER,
3495 capabilities: AcpProviderCapabilitiesV2,
3496 makeRuntime: makeMockRuntime({
3497 childProcessSpawner,
3498 mockAgentPath,
3499 wrapRuntime: (runtime) => ({
3500 ...runtime,
3501 handleSessionUpdate: (handler) =>
3502 Effect.sync(() => {
3503 sessionUpdateHandler = handler;
3504 }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))),
3505 }),
3506 }),
3507 },
3508 selfInvocation,
3509 });
3510 const threadId = ThreadId.make("thread-acp-v2-plan-replay-boundary");
3511 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3512 runtimeMode: "full-access",
3513 interactionMode: "default",
3514 cwd: process.cwd(),
3515 });
3516 const modelSelection = { instanceId, model: "default" } as const;
3517 const runtime = yield* adapter.openSession({
3518 threadId,
3519 providerSessionId: ProviderSessionId.make("provider-session-acp-v2-plan-replay-boundary"),
3520 modelSelection,
3521 runtimePolicy,
3522 });
3523 const providerThread = yield* runtime.ensureThread({
3524 threadId,
3525 modelSelection,
3526 runtimePolicy,
3527 });
3528 assert.isDefined(sessionUpdateHandler);
3529
3530 yield* sessionUpdateHandler!({
3531 sessionId: "mock-session-1",
3532 update: {
3533 sessionUpdate: "agent_message_chunk",
3534 content: { type: "text", text: "before plan" },
3535 },
3536 });
3537 yield* sessionUpdateHandler!({
3538 sessionId: "mock-session-1",
3539 update: {
3540 sessionUpdate: "plan_update",
3541 plan: {
3542 type: "items",
3543 planId: "plan-1",
3544 entries: [{ content: "Continue", priority: "medium", status: "in_progress" }],
3545 },
3546 },
3547 });
3548 yield* sessionUpdateHandler!({
3549 sessionId: "mock-session-1",
3550 update: {
3551 sessionUpdate: "agent_message_chunk",
3552 content: { type: "text", text: "after plan" },
3553 },
3554 });
3555 yield* sessionUpdateHandler!({
3556 sessionId: "mock-session-1",
3557 update: {
3558 sessionUpdate: "agent_thought_chunk",
3559 content: { type: "text", text: "weighing it" },
3560 },
3561 });
3562 yield* sessionUpdateHandler!({
3563 sessionId: "mock-session-1",
3564 update: {
3565 sessionUpdate: "agent_thought",
3566 messageId: "whole-thought",
3567 content: [{ type: "text", text: "settled on it" }],
3568 },
3569 });
3570
3571 const snapshot = yield* runtime.readThreadSnapshot({ providerThread });
3572 assert.deepEqual(
3573 snapshot.messages.map((message) => message.text),
3574 ["before plan", "after plan", "weighing it", "settled on it"],
3575 );
3576 assert.deepEqual(
3577 snapshot.reasoningMessageIds,
3578 snapshot.messages.slice(2).map((message) => message.id),
3579 );
3580 }).pipe(Effect.provide(layerTest)),
3581 );
3582
3583 it.effect("replays history in order, with stable earlier snapshots", () =>
3584 Effect.gen(function* () {
3585 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3586 const path = yield* Path.Path;
3587 const selfInvocation = yield* resolveSelfInvocation();
3588 const mockAgentPath = yield* path.fromFileUrl(
3589 new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
3590 );
3591 type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"];
3592 let sessionUpdateHandler: Parameters<RuntimeService["handleSessionUpdate"]>[0] | undefined;
3593 const instanceId = ProviderInstanceId.make("acp-test-v2-replay-order");
3594 const adapter = yield* makeAcpAdapterV2({
3595 instanceId,
3596 flavor: {
3597 driver: ACP_TEST_DRIVER,
3598 capabilities: AcpProviderCapabilitiesV2,
3599 makeRuntime: makeMockRuntime({
3600 childProcessSpawner,
3601 mockAgentPath,
3602 wrapRuntime: (runtime) => ({
3603 ...runtime,
3604 handleSessionUpdate: (handler) =>
3605 Effect.sync(() => {
3606 sessionUpdateHandler = handler;
3607 }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))),
3608 }),
3609 }),
3610 },
3611 selfInvocation,
3612 });
3613 const threadId = ThreadId.make("thread-acp-v2-replay-order");
3614 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3615 runtimeMode: "full-access",
3616 interactionMode: "default",
3617 cwd: process.cwd(),
3618 });
3619 const modelSelection = { instanceId, model: "default" } as const;
3620 const runtime = yield* adapter.openSession({
3621 threadId,
3622 providerSessionId: ProviderSessionId.make("provider-session-acp-v2-replay-order"),
3623 modelSelection,
3624 runtimePolicy,
3625 });
3626 const providerThread = yield* runtime.ensureThread({
3627 threadId,
3628 modelSelection,
3629 runtimePolicy,
3630 });
3631 assert.isDefined(sessionUpdateHandler);
3632 const replay = (
3633 sessionUpdate: "user_message_chunk" | "agent_message_chunk",
3634 messageId: string,
3635 text: string,
3636 ) =>
3637 sessionUpdateHandler!({
3638 sessionId: "mock-session-1",
3639 update: { sessionUpdate, messageId, content: { type: "text", text } },
3640 });
3641
3642 yield* replay("user_message_chunk", "u1", "<monitor-event> is ");
3643 yield* replay("user_message_chunk", "u1", "XML");
3644 yield* replay("agent_message_chunk", "a1", "hi");
3645 yield* replay("user_message_chunk", "u2", "next");
3646 yield* replay("agent_message_chunk", "a1", " ");
3647 yield* replay("agent_message_chunk", "a1", "there");
3648 yield* replay("agent_message_chunk", "a3", "\t");
3649 yield* replay("agent_message_chunk", "a3", "code with <monitor-event>");
3650 yield* replay("agent_message_chunk", "a4", "\n\n");
3651 yield* replay("user_message_chunk", "u3", "Why did Monitor 0123abcd ended appear?");
3652 // Grok's injected monitor wakes replay as user chunks and stay out of history.
3653 yield* replay(
3654 "user_message_chunk",
3655 "u4",
3656 '\n<monitor-event task_id="019f44a5-87d1-7640-8e35-6a4667ffc873">\nline\n</monitor-event>',
3657 );
3658 yield* replay(
3659 "user_message_chunk",
3660 "u5",
3661 'Monitor "019f44a5-87d1-7640-8e35-6a4667ffc873" ended: exit 0',
3662 );
3663 const first = yield* runtime.readThreadSnapshot({ providerThread });
3664 yield* replay("agent_message_chunk", "a2", "done");
3665 yield* replay("user_message_chunk", "u2", "\n");
3666 yield* replay("user_message_chunk", "u2", "\tone");
3667 yield* sessionUpdateHandler({
3668 sessionId: "mock-child-session",
3669 update: {
3670 sessionUpdate: "agent_message_chunk",
3671 content: { type: "text", text: "Child private work" },
3672 },
3673 });
3674 const second = yield* runtime.readThreadSnapshot({ providerThread });
3675
3676 const texts = (snapshot: typeof first) =>
3677 snapshot.messages.map((message) => `${message.role}:${message.text}`);
3678 assert.deepEqual(texts(first), [
3679 "user:<monitor-event> is XML",
3680 "assistant:hi there",
3681 "user:next",
3682 "assistant:\tcode with <monitor-event>",
3683 "user:Why did Monitor 0123abcd ended appear?",
3684 ]);
3685 assert.deepEqual(texts(second), [
3686 "user:<monitor-event> is XML",
3687 "assistant:hi there",
3688 "user:next\n\tone",
3689 "assistant:\tcode with <monitor-event>",
3690 "user:Why did Monitor 0123abcd ended appear?",
3691 "assistant:done",
3692 ]);
3693 }).pipe(Effect.provide(layerTest), Effect.scoped),
3694 );
3695
3696 it.effect("persists a bounded subset of streamed root and child tool updates", () =>
3697 Effect.gen(function* () {
3698 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3699 const mockAgentPath = yield* bundledMockAgentPath;
3700 type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"];
3701 let handler: Parameters<Runtime["handleSessionUpdate"]>[0] | undefined;
3702 // Devin resends the growing file without status on each update.
3703 const file = `${"x = 1\n".repeat(800)}END_OF_FILE\n`;
3704 const chunks = 200;
3705 const streamedWrite = (
3706 toolCallId: string,
3707 parentAgentId?: string,
3708 ): Array<EffectAcpSchema.SessionUpdate> => {
3709 const meta =
3710 parentAgentId === undefined ? {} : { "cognition.ai/subagent_context": { parentAgentId } };
3711 const write = (text: string, status?: "completed") => ({
3712 sessionUpdate: "tool_call_update" as const,
3713 toolCallId,
3714 title: "Writing ./audit.py",
3715 ...(status === undefined ? {} : { status }),
3716 content: [
3717 { type: "diff" as const, path: "/repo/audit.py", oldText: null, newText: text },
3718 ],
3719 rawInput: { file_path: "/repo/audit.py", content: text },
3720 _meta: meta,
3721 });
3722 return [
3723 {
3724 sessionUpdate: "tool_call",
3725 toolCallId,
3726 title: "Writing …",
3727 kind: "edit",
3728 status: "pending",
3729 _meta: meta,
3730 },
3731 ...Array.from({ length: chunks }, (_, index) =>
3732 write(file.slice(0, Math.ceil((file.length * (index + 1)) / chunks))),
3733 ),
3734 write(file, "completed"),
3735 ];
3736 };
3737 const instanceId = ProviderInstanceId.make("devin-streamed-write");
3738 const adapter = yield* makeAcpAdapterV2({
3739 instanceId,
3740 selfInvocation: yield* resolveSelfInvocation(),
3741 flavor: {
3742 driver: ACP_TEST_DRIVER,
3743 capabilities: AcpProviderCapabilitiesV2,
3744 normalizeSessionUpdate: normalizeDevinSessionUpdate,
3745 normalizeToolCall: normalizeDevinToolCall,
3746 extractSubagentUpdate: extractDevinSubagentUpdate,
3747 makeRuntime: makeMockRuntime({
3748 childProcessSpawner,
3749 mockAgentPath,
3750 wrapRuntime: (runtime) => ({
3751 ...runtime,
3752 handleSessionUpdate: (next) =>
3753 Effect.sync(() => {
3754 handler = next;
3755 }).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
3756 prompt: () =>
3757 Effect.gen(function* () {
3758 const updates = [
3759 {
3760 sessionUpdate: "tool_call_update",
3761 toolCallId: "child-a",
3762 status: "in_progress",
3763 _meta: {
3764 "cognition.ai/subagent_started": {
3765 agentId: "child-a",
3766 title: "Write audit script",
3767 task: "Write audit.py.",
3768 },
3769 },
3770 },
3771 ...streamedWrite("root-write"),
3772 ...streamedWrite("child-write", "child-a"),
3773 ...Array.from({ length: 5 }, (_, index) => ({
3774 sessionUpdate: "tool_call_update" as const,
3775 toolCallId: "ticker",
3776 title: "Watch ticks",
3777 kind: "execute" as const,
3778 status: "in_progress" as const,
3779 rawOutput: `tick ${index + 1}`,
3780 })),
3781 ...Array.from({ length: 5 }, (_, index) => ({
3782 sessionUpdate: "tool_call_update" as const,
3783 toolCallId: "text-ticker",
3784 title: "Watch ticks",
3785 kind: "execute" as const,
3786 status: "in_progress" as const,
3787 rawOutput: { type: "Text", text: `tock ${index + 1}` },
3788 })),
3789 {
3790 sessionUpdate: "tool_call_update",
3791 toolCallId: "child-a",
3792 status: "completed",
3793 _meta: {
3794 "cognition.ai/subagent_completed": {
3795 agentId: "child-a",
3796 success: true,
3797 summary: "Wrote audit.py",
3798 },
3799 },
3800 },
3801 ] satisfies Array<EffectAcpSchema.SessionUpdate>;
3802 for (const update of updates)
3803 yield* handler!({ sessionId: "mock-session-1", update });
3804 return { stopReason: "end_turn" as const };
3805 }),
3806 }),
3807 }),
3808 },
3809 });
3810 const threadId = ThreadId.make("devin-streamed-write");
3811 const modelSelection = { instanceId, model: "default" };
3812 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3813 runtimeMode: "full-access",
3814 interactionMode: "default",
3815 cwd: process.cwd(),
3816 });
3817 const runtime = yield* adapter.openSession({
3818 threadId,
3819 providerSessionId: ProviderSessionId.make("devin-streamed-write-session"),
3820 modelSelection,
3821 runtimePolicy,
3822 });
3823 const providerThread = yield* runtime.ensureThread({
3824 threadId,
3825 modelSelection,
3826 runtimePolicy,
3827 });
3828 yield* runtime.startTurn(
3829 makeTurnInput({
3830 threadId,
3831 providerThread,
3832 instanceId,
3833 runtimePolicy,
3834 now: yield* DateTime.now,
3835 }),
3836 );
3837 const items = Array.from(
3838 yield* runtime.events.pipe(
3839 Stream.takeUntil((event) => event.type === "turn.terminal"),
3840 Stream.runCollect,
3841 ),
3842 ).flatMap((event) => (event.type === "turn_item.updated" ? [event.turnItem] : []));
3843 for (const toolCallId of ["root-write", "child-write"]) {
3844 const writes = items.filter((item) => item.nativeItemRef?.nativeId?.endsWith(toolCallId));
3845 assert.isAtLeast(writes.length, chunks / 10, toolCallId);
3846 assert.isAtMost(writes.length, chunks / 5, toolCallId);
3847 assert.equal(writes.at(-1)?.status, "completed", toolCallId);
3848 assert.include(JSON.stringify(writes.at(-1)), "END_OF_FILE", toolCallId);
3849 }
3850 for (const [toolCallId, word] of [
3851 ["ticker", "tick"],
3852 ["text-ticker", "tock"],
3853 ] as const) {
3854 const ticks = items.filter((item) => item.nativeItemRef?.nativeId?.endsWith(toolCallId));
3855 for (let tick = 1; tick <= 5; tick++) {
3856 assert.isTrue(
3857 ticks.some((item) => JSON.stringify(item).includes(`${word} ${tick}`)),
3858 `${word} ${tick} must persist`,
3859 );
3860 }
3861 }
3862 }).pipe(Effect.provide(layerTest)),
3863 );
3864
3865 it.effect("keeps Devin parent paragraphs intact while projecting native child work", () =>
3866 Effect.gen(function* () {
3867 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3868 const mockAgentPath = yield* bundledMockAgentPath;
3869 type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"];
3870 let handler: Parameters<Runtime["handleSessionUpdate"]>[0] | undefined;
3871 let createTerminal: Parameters<Runtime["handleCreateTerminal"]>[0] | undefined;
3872 const instanceId = ProviderInstanceId.make("devin-replay");
3873 const adapter = yield* makeAcpAdapterV2({
3874 instanceId,
3875 clientTerminals: { shellCommands: true },
3876 selfInvocation: yield* resolveSelfInvocation(),
3877 flavor: {
3878 driver: ACP_TEST_DRIVER,
3879 capabilities: AcpProviderCapabilitiesV2,
3880 normalizeSessionUpdate: normalizeDevinSessionUpdate,
3881 normalizeToolCall: normalizeDevinToolCall,
3882 extractSubagentUpdate: extractDevinSubagentUpdate,
3883 makeRuntime: makeMockRuntime({
3884 childProcessSpawner,
3885 mockAgentPath,
3886 wrapRuntime: (runtime) => ({
3887 ...runtime,
3888 handleSessionUpdate: (next) =>
3889 Effect.sync(() => {
3890 handler = next;
3891 }).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
3892 handleCreateTerminal: (next) =>
3893 Effect.sync(() => {
3894 createTerminal = next;
3895 }).pipe(Effect.andThen(runtime.handleCreateTerminal(next))),
3896 prompt: () =>
3897 Effect.gen(function* () {
3898 assert.isDefined(handler);
3899 assert.isDefined(createTerminal);
3900 const fallbackTerminal = yield* createTerminal(
3901 { sessionId: "mock-session-1", command: "true acp-mcp-call task_status {}" },
3902 { requestId: "child-mcp-terminal", method: "terminal/create" },
3903 );
3904 const updates = [
3905 {
3906 sessionUpdate: "tool_call_update",
3907 toolCallId: "child-a",
3908 status: "in_progress",
3909 _meta: {
3910 "cognition.ai/subagent_started": {
3911 agentId: "child-a",
3912 title: "Map orchestration",
3913 task: "Run pwd, then reply ONE.",
3914 model: " \t ",
3915 },
3916 },
3917 },
3918 {
3919 sessionUpdate: "tool_call_update",
3920 toolCallId: "child-a",
3921 status: "in_progress",
3922 _meta: {
3923 "cognition.ai/subagent_started": {
3924 agentId: "child-a",
3925 title: "Map orchestration",
3926 task: "Run pwd, then reply ONE.",
3927 model: "SWE-1.7 Medium",
3928 depth: 1,
3929 isBackground: true,
3930 },
3931 },
3932 },
3933 {
3934 sessionUpdate: "agent_message_chunk",
3935 content: { type: "text", text: "Map orchestration (command" },
3936 _meta: { "cognition.ai/streamingMessageId": "parent-message" },
3937 },
3938 {
3939 sessionUpdate: "tool_call_update",
3940 toolCallId: "parent-tool",
3941 status: "completed",
3942 title: "Parent tool finished",
3943 },
3944 {
3945 sessionUpdate: "tool_call",
3946 toolCallId: "child-pwd",
3947 title: "Tool",
3948 kind: "execute",
3949 status: "in_progress",
3950 rawInput: { command: "pwd" },
3951 _meta: {
3952 "cognition.ai/inferenceToolName": "exec",
3953 "cognition.ai/subagent_context": { parentAgentId: "child-a" },
3954 },
3955 },
3956 {
3957 sessionUpdate: "tool_call",
3958 toolCallId: "child-weather",
3959 title: "Check weather",
3960 status: "completed",
3961 rawInput: { server: "weather", tool: "get_weather", city: "Berlin" },
3962 rawOutput: {
3963 result: {
3964 _meta: {
3965 source: { name: "Weather", logoUrl: "https://example.com/weather.png" },
3966 },
3967 content: [{ type: "text", text: "Sunny" }],
3968 },
3969 },
3970 _meta: {
3971 is_mcp_tool_call: true,
3972 "cognition.ai/subagent_context": { parentAgentId: "child-a" },
3973 },
3974 },
3975 {
3976 sessionUpdate: "tool_call",
3977 toolCallId: "child-mcp-fallback",
3978 title: "Ran command",
3979 kind: "execute",
3980 status: "completed",
3981 content: [{ type: "terminal", terminalId: fallbackTerminal.terminalId }],
3982 _meta: { "cognition.ai/subagent_context": { parentAgentId: "child-a" } },
3983 },
3984 {
3985 sessionUpdate: "tool_call",
3986 toolCallId: "parent-weather",
3987 title: "Check weather",
3988 status: "completed",
3989 rawInput: { server: "weather", tool: "get_weather", city: "Berlin" },
3990 rawOutput: {
3991 _meta: {
3992 source: { name: "Weather", logoUrl: "https://example.com/weather.png" },
3993 },
3994 content: [{ type: "text", text: "Sunny" }],
3995 },
3996 _meta: { is_mcp_tool_call: true },
3997 },
3998 {
3999 sessionUpdate: "agent_message_chunk",
4000 content: { type: "text", text: " → decider → event)." },
4001 _meta: { "cognition.ai/streamingMessageId": "parent-message" },
4002 },
4003 {
4004 sessionUpdate: "tool_call_update",
4005 toolCallId: "child-pwd",
4006 status: "completed",
4007 rawOutput: "probe-workspace",
4008 _meta: { "cognition.ai/subagent_context": { parentAgentId: "child-a" } },
4009 },
4010 {
4011 sessionUpdate: "agent_message_chunk",
4012 content: { type: "text", text: "Checking the code." },
4013 _meta: {
4014 "cognition.ai/streamingMessageId": "child-progress",
4015 "cognition.ai/subagent_context": { parentAgentId: "child-a" },
4016 },
4017 },
4018 ...["O", "N", "E"].map((text) => ({
4019 sessionUpdate: "agent_message_chunk" as const,
4020 content: { type: "text" as const, text },
4021 _meta: {
4022 "cognition.ai/streamingMessageId": "child-result",
4023 "cognition.ai/subagent_context": { parentAgentId: "child-a" },
4024 },
4025 })),
4026 {
4027 sessionUpdate: "tool_call",
4028 toolCallId: "child-later",
4029 title: "Later tool",
4030 status: "completed",
4031 _meta: { "cognition.ai/subagent_context": { parentAgentId: "child-a" } },
4032 },
4033 {
4034 sessionUpdate: "tool_call_update",
4035 toolCallId: "child-a",
4036 status: "completed",
4037 _meta: {
4038 "cognition.ai/subagent_completed": {
4039 agentId: "child-a",
4040 success: true,
4041 summary: "Final report: ONE",
4042 depth: 1,
4043 },
4044 },
4045 },
4046 ] satisfies Array<EffectAcpSchema.SessionUpdate>;
4047 for (const update of updates)
4048 yield* handler!({ sessionId: "mock-session-1", update });
4049 return { stopReason: "end_turn" as const };
4050 }),
4051 }),
4052 }),
4053 },
4054 });
4055 const threadId = ThreadId.make("devin-replay-parent");
4056 const modelSelection = { instanceId, model: "default" };
4057 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
4058 runtimeMode: "full-access",
4059 interactionMode: "default",
4060 cwd: process.cwd(),
4061 });
4062 const runtime = yield* adapter.openSession({
4063 threadId,
4064 providerSessionId: ProviderSessionId.make("devin-replay-session"),
4065 modelSelection,
4066 runtimePolicy,
4067 });
4068 const providerThread = yield* runtime.ensureThread({
4069 threadId,
4070 modelSelection,
4071 runtimePolicy,
4072 });
4073 yield* runtime.startTurn(
4074 makeTurnInput({
4075 threadId,
4076 providerThread,
4077 instanceId,
4078 runtimePolicy,
4079 now: yield* DateTime.now,
4080 }),
4081 );
4082 const events = Array.from(
4083 yield* runtime.events.pipe(
4084 Stream.takeUntil((event) => event.type === "turn.terminal"),
4085 Stream.runCollect,
4086 ),
4087 );
4088 const items = events.flatMap((event) =>
4089 event.type === "turn_item.updated" ? [event.turnItem] : [],
4090 );
4091 const parentMessages = items.filter(
4092 (item) => item.threadId === threadId && item.type === "assistant_message",
4093 );
4094 assert.equal(new Set(parentMessages.map((item) => item.id)).size, 1);
4095 const lastParent = parentMessages.at(-1);
4096 assert.equal(
4097 lastParent?.type === "assistant_message" ? lastParent.text : undefined,
4098 "Map orchestration (command → decider → event).",
4099 );
4100 const tasks = events.flatMap((event) =>
4101 event.type === "subagent.updated" ? [event.subagent] : [],
4102 );
4103 const task = tasks.at(-1);
4104 assert.isNull(tasks[0]?.model);
4105 const childThread = events.find(
4106 (event) =>
4107 event.type === "app_thread.created" && event.appThread.id === task?.childThreadId,
4108 );
4109 assert.equal(
4110 childThread?.type === "app_thread.created"
4111 ? childThread.appThread.modelSelection.model
4112 : undefined,
4113 modelSelection.model,
4114 );
4115 assert.equal(task?.model, "SWE-1.7 Medium");
4116 assert.equal(task?.status, "completed");
4117 assert.equal(task?.result, "Final report: ONE");
4118 const childMessages = new Map(
4119 items.flatMap((item) =>
4120 item.threadId === task?.childThreadId && item.type === "assistant_message"
4121 ? [[item.id, item.text] as const]
4122 : [],
4123 ),
4124 );
4125 assert.deepEqual([...childMessages.values()], ["Checking the code.", "ONE"]);
4126 assert.equal(task?.prompt, "Run pwd, then reply ONE.");
4127 assert.isTrue(
4128 items.some(
4129 (item) =>
4130 item.threadId === task?.childThreadId &&
4131 item.type === "dynamic_tool" &&
4132 item.toolName === "t3-code.task_status",
4133 ),
4134 );
4135 const childMcp = items.find(
4136 (item) =>
4137 item.threadId === task?.childThreadId &&
4138 item.type === "dynamic_tool" &&
4139 item.toolName === "weather.get_weather",
4140 );
4141 const parentMcp = items.find(
4142 (item) =>
4143 item.threadId === threadId &&
4144 item.type === "dynamic_tool" &&
4145 item.toolName === "weather.get_weather",
4146 );
4147 for (const item of [parentMcp, childMcp]) {
4148 assert.equal(item?.title, "get weather");
4149 assert.deepEqual(item?.toolSource, {
4150 key: "mcp:weather",
4151 name: "Weather",
4152 kind: "integration",
4153 icon: { _tag: "themed-logo", logoUrl: "https://example.com/weather.png" },
4154 });
4155 assert.deepEqual(item?.toolIcon, item?.toolSource?.icon);
4156 }
4157 assert.isTrue(
4158 items.some(
4159 (item) =>
4160 item.threadId === task?.childThreadId &&
4161 item.type === "user_message" &&
4162 item.text === task.prompt,
4163 ),
4164 );
4165 assert.isTrue(
4166 items.some(
4167 (item) =>
4168 item.threadId === task?.childThreadId &&
4169 item.type === "dynamic_tool" &&
4170 item.status === "completed" &&
4171 item.output === "probe-workspace",
4172 ),
4173 );
4174 const childAnswers = items.filter(
4175 (item) => item.threadId === task?.childThreadId && item.type === "assistant_message",
4176 );
4177 assert.equal(new Set(childAnswers.map((item) => item.ordinal)).size, 2);
4178 // Later chunks coalesce; the subagent's completion flushes the final text.
4179 assert.deepEqual(
4180 childAnswers.flatMap((item) =>
4181 item.type === "assistant_message" && item.text !== "Checking the code."
4182 ? [item.text]
4183 : [],
4184 ),
4185 ["O", "ONE"],
4186 );
4187 const parentTools = items.filter(
4188 (item) => item.threadId === threadId && item.type === "dynamic_tool",
4189 );
4190 assert.equal(parentTools.length, 2);
4191 assert.equal(parentTools[0]?.title, "Parent tool finished");
4192 }).pipe(Effect.provide(layerTest)),
4193 );
4194
4195 it.effect("emits coalesced subagent text before a failed turn's terminal", () =>
4196 Effect.gen(function* () {
4197 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
4198 const path = yield* Path.Path;
4199 const mockAgentPath = yield* path.fromFileUrl(
4200 new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
4201 );
4202 type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"];
4203 let handler: Parameters<Runtime["handleSessionUpdate"]>[0] | undefined;
4204 const instanceId = ProviderInstanceId.make("devin-failed-flush");
4205 const adapter = yield* makeAcpAdapterV2({
4206 instanceId,
4207 selfInvocation: yield* resolveSelfInvocation(),
4208 flavor: {
4209 driver: ACP_TEST_DRIVER,
4210 capabilities: AcpProviderCapabilitiesV2,
4211 normalizeSessionUpdate: normalizeDevinSessionUpdate,
4212 normalizeToolCall: normalizeDevinToolCall,
4213 extractSubagentUpdate: extractDevinSubagentUpdate,
4214 makeRuntime: makeMockRuntime({
4215 childProcessSpawner,
4216 mockAgentPath,
4217 wrapRuntime: (runtime) => ({
4218 ...runtime,
4219 handleSessionUpdate: (next) =>
4220 Effect.sync(() => {
4221 handler = next;
4222 }).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
4223 prompt: () =>
4224 Effect.gen(function* () {
4225 const updates = [
4226 {
4227 sessionUpdate: "tool_call_update",
4228 toolCallId: "child-a",
4229 status: "in_progress",
4230 _meta: {
4231 "cognition.ai/subagent_started": {
4232 agentId: "child-a",
4233 title: "Answer",
4234 task: "Reply ONE.",
4235 },
4236 },
4237 },
4238 ...["O", "N", "E"].map((text) => ({
4239 sessionUpdate: "agent_message_chunk" as const,
4240 content: { type: "text" as const, text },
4241 _meta: {
4242 "cognition.ai/streamingMessageId": "child-result",
4243 "cognition.ai/subagent_context": { parentAgentId: "child-a" },
4244 },
4245 })),
4246 ] satisfies Array<EffectAcpSchema.SessionUpdate>;
4247 for (const update of updates)
4248 yield* handler!({ sessionId: "mock-session-1", update });
4249 return yield* new EffectAcpErrors.AcpRequestError({
4250 code: -32603,
4251 errorMessage: "prompt failed",
4252 });
4253 }),
4254 }),
4255 }),
4256 },
4257 });
4258 const threadId = ThreadId.make("devin-failed-flush-parent");
4259 const modelSelection = { instanceId, model: "default" };
4260 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
4261 runtimeMode: "full-access",
4262 interactionMode: "default",
4263 cwd: process.cwd(),
4264 });
4265 const runtime = yield* adapter.openSession({
4266 threadId,
4267 providerSessionId: ProviderSessionId.make("devin-failed-flush-session"),
4268 modelSelection,
4269 runtimePolicy,
4270 });
4271 const providerThread = yield* runtime.ensureThread({
4272 threadId,
4273 modelSelection,
4274 runtimePolicy,
4275 });
4276 yield* runtime.startTurn(
4277 makeTurnInput({
4278 threadId,
4279 providerThread,
4280 instanceId,
4281 runtimePolicy,
4282 now: yield* DateTime.now,
4283 }),
4284 );
4285 const events = Array.from(
4286 yield* runtime.events.pipe(
4287 Stream.takeUntil((event) => event.type === "turn.terminal"),
4288 Stream.runCollect,
4289 ),
4290 );
4291 const terminal = events.at(-1);
4292 assert.equal(terminal?.type === "turn.terminal" ? terminal.status : undefined, "failed");
4293 const childText = events.flatMap((event) =>
4294 event.type === "turn_item.updated" &&
4295 event.turnItem.type === "assistant_message" &&
4296 event.turnItem.threadId !== threadId
4297 ? [event.turnItem.text]
4298 : [],
4299 );
4300 assert.equal(childText.at(-1), "ONE");
4301 }).pipe(Effect.provide(layerTest), Effect.scoped),
4302 );
4303
4304 it.effect("routes interleaved Copilot subagent output into subagent threads", () =>
4305 Effect.gen(function* () {
4306 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
4307 const mockAgentPath = yield* bundledMockAgentPath;
4308 type Runtime = AcpSessionRuntime.AcpSessionRuntime["Service"];
4309 let handler: Parameters<Runtime["handleSessionUpdate"]>[0] | undefined;
4310 let sessionEvent:
4311 | ((event: unknown) => Effect.Effect<void, EffectAcpErrors.AcpError>)
4312 | undefined;
4313 const routing = makeCopilotSubagentRouting();
4314 const instanceId = ProviderInstanceId.make("copilot-replay");
4315 const adapter = yield* makeAcpAdapterV2({
4316 instanceId,
4317 selfInvocation: yield* resolveSelfInvocation(),
4318 flavor: {
4319 driver: ACP_TEST_DRIVER,
4320 capabilities: AcpProviderCapabilitiesV2,
4321 registerExtensions: routing.registerExtensions,
4322 normalizeSessionUpdate: routing.normalizeSessionUpdate,
4323 extractSubagentUpdate: extractCopilotSubagentUpdate,
4324 makeRuntime: makeMockRuntime({
4325 childProcessSpawner,
4326 mockAgentPath,
4327 wrapRuntime: (runtime) => ({
4328 ...runtime,
4329 handleSessionUpdate: (next) =>
4330 Effect.sync(() => {
4331 handler = next;
4332 }).pipe(Effect.andThen(runtime.handleSessionUpdate(next))),
4333 handleExtNotification: (method, payload, next) =>
4334 Effect.sync(() => {
4335 if (method === "github.com/copilot/sessionEvent") {
4336 sessionEvent = next as typeof sessionEvent;
4337 }
4338 }).pipe(Effect.andThen(runtime.handleExtNotification(method, payload, next))),
4339 prompt: () =>
4340 Effect.gen(function* () {
4341 assert.isDefined(handler);
4342 assert.isDefined(sessionEvent);
4343 const sessionId = "mock-session-1";
4344 const raw = (type: string, data: object, agentId?: string) =>
4345 sessionEvent!({ sessionId, type, data, ...(agentId ? { agentId } : {}) });
4346 const update = (next: EffectAcpSchema.SessionUpdate) =>
4347 handler!({ sessionId, update: next });
4348 // Copilot sends each chunk's raw delta just before the chunk.
4349 const chunk = (text: string, task?: string) =>
4350 raw("assistant.message_delta", {
4351 deltaContent: text,
4352 ...(task ? { parentToolCallId: task } : {}),
4353 }).pipe(
4354 Effect.andThen(
4355 update({
4356 sessionUpdate: "agent_message_chunk",
4357 content: { type: "text", text },
4358 }),
4359 ),
4360 );
4361 // Copilot 1.0.114 names the subagent only by agentId, on the delta and the chunk.
4362 const agentChunk = (text: string, agentId: string) =>
4363 raw("assistant.message_delta", { deltaContent: text }, agentId).pipe(
4364 Effect.andThen(
4365 update({
4366 sessionUpdate: "agent_message_chunk",
4367 content: { type: "text", text },
4368 _meta: { "github.com/copilot": { agentId } },
4369 }),
4370 ),
4371 );
4372 const launch = (toolCallId: string, description: string, mode: string) =>
4373 update({
4374 sessionUpdate: "tool_call",
4375 toolCallId,
4376 title: description,
4377 kind: "other",
4378 status: "pending",
4379 rawInput: {
4380 description,
4381 prompt: `${description}.`,
4382 agent_type: "explore",
4383 mode,
4384 },
4385 });
4386 yield* chunk("Launching ");
4387 yield* launch("task-a", "Describe a.txt", "sync");
4388 yield* launch("task-b", "Describe b.txt", "sync");
4389 yield* launch("task-bg", "List directory", "background");
4390 yield* raw("subagent.started", { toolCallId: "task-a" }, "agent-a");
4391 yield* raw("subagent.started", { toolCallId: "task-b" }, "agent-b");
4392 yield* raw("subagent.started", { toolCallId: "task-bg" }, "agent-bg");
4393 yield* update({
4394 sessionUpdate: "tool_call_update",
4395 toolCallId: "task-bg",
4396 status: "completed",
4397 rawOutput: { content: "Agent started in background with agent_id: agent-bg." },
4398 });
4399 yield* update({
4400 sessionUpdate: "tool_call",
4401 toolCallId: "view-a",
4402 title: "Viewing a.txt",
4403 kind: "read",
4404 status: "completed",
4405 rawInput: { path: "a.txt" },
4406 _meta: { "github.com/copilot": { agentId: "agent-a" } },
4407 });
4408 yield* chunk("`a.txt` says", "task-a");
4409 yield* agentChunk("`b.txt` has", "agent-b");
4410 yield* chunk(" hello.", "task-a");
4411 yield* chunk("a.txt b.txt", "task-bg");
4412 yield* agentChunk(" three lines.", "agent-b");
4413 for (const [task, agent, text] of [
4414 ["task-a", "agent-a", "`a.txt` says hello."],
4415 ["task-b", "agent-b", "`b.txt` has three lines."],
4416 ] as const) {
4417 yield* update({
4418 sessionUpdate: "tool_call_update",
4419 toolCallId: task,
4420 status: "completed",
4421 rawOutput: { content: text },
4422 });
4423 yield* raw("subagent.completed", { toolCallId: task }, agent);
4424 }
4425 yield* raw("subagent.completed", { toolCallId: "task-bg" }, "agent-bg");
4426 yield* chunk("all done.");
4427 return { stopReason: "end_turn" as const };
4428 }),
4429 }),
4430 }),
4431 },
4432 });
4433 const threadId = ThreadId.make("copilot-replay-parent");
4434 const modelSelection = { instanceId, model: "default" };
4435 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
4436 runtimeMode: "full-access",
4437 interactionMode: "default",
4438 cwd: process.cwd(),
4439 });
4440 const runtime = yield* adapter.openSession({
4441 threadId,
4442 providerSessionId: ProviderSessionId.make("copilot-replay-session"),
4443 modelSelection,
4444 runtimePolicy,
4445 });
4446 const providerThread = yield* runtime.ensureThread({
4447 threadId,
4448 modelSelection,
4449 runtimePolicy,
4450 });
4451 yield* runtime.startTurn(
4452 makeTurnInput({
4453 threadId,
4454 providerThread,
4455 instanceId,
4456 runtimePolicy,
4457 now: yield* DateTime.now,
4458 }),
4459 );
4460 const events = Array.from(
4461 yield* runtime.events.pipe(
4462 Stream.takeUntil((event) => event.type === "turn.terminal"),
4463 Stream.runCollect,
4464 ),
4465 );
4466 const items = events.flatMap((event) =>
4467 event.type === "turn_item.updated" ? [event.turnItem] : [],
4468 );
4469 const lastText = (thread: string | null | undefined) =>
4470 new Map(
4471 items.flatMap((item) =>
4472 item.threadId === thread && item.type === "assistant_message"
4473 ? [[item.id, item.text] as const]
4474 : [],
4475 ),
4476 );
4477 assert.deepEqual([...lastText(threadId).values()].join(""), "Launching all done.");
4478 const tasks = new Map(
4479 events.flatMap((event) =>
4480 event.type === "subagent.updated"
4481 ? [[event.subagent.nativeTaskRef?.nativeId, event.subagent] as const]
4482 : [],
4483 ),
4484 );
4485 const outputs = Object.fromEntries(
4486 [...tasks].map(([id, task]) => [
4487 id,
4488 [task.status, [...lastText(task.childThreadId).values()].join("")],
4489 ]),
4490 );
4491 assert.deepEqual(outputs, {
4492 "task-a": ["completed", "`a.txt` says hello."],
4493 "task-b": ["completed", "`b.txt` has three lines."],
4494 "task-bg": ["completed", "a.txt b.txt"],
4495 });
4496 assert.isTrue(
4497 items.some(
4498 (item) =>
4499 item.threadId === tasks.get("task-a")?.childThreadId &&
4500 item.type === "dynamic_tool" &&
4501 item.title === "Read a.txt",
4502 ),
4503 );
4504 assert.isFalse(
4505 items.some((item) => item.threadId === threadId && item.type === "dynamic_tool"),
4506 );
4507 }).pipe(Effect.provide(layerTest)),
4508 );
4509
4510 it.effect("keeps protocol diagnostics off unless a protocol logger is supplied", () =>
4511 Effect.gen(function* () {
4512 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
4513 const selfInvocation = yield* resolveSelfInvocation();
4514 const mockAgentPath = yield* bundledMockAgentPath;
4515 const runTurn = Effect.fn("AcpAdapterV2.testProtocolLoggingTurn")(function* (
4516 name: string,
4517 nativeLogging?: NonNullable<Parameters<typeof makeAcpAdapterV2>[0]["nativeLogging"]>,
4518 ) {
4519 const events: Array<EffectAcpProtocol.AcpProtocolLogEvent> = [];
4520 const instanceId = ProviderInstanceId.make(`acp-test-${name}`);
4521 const threadId = ThreadId.make(`thread-acp-${name}`);
4522 const makeRuntime = makeMockRuntime({ childProcessSpawner, mockAgentPath });
4523 const adapter = yield* makeAcpAdapterV2({
4524 instanceId,
4525 flavor: {
4526 driver: ACP_TEST_DRIVER,
4527 capabilities: AcpProviderCapabilitiesV2,
4528 makeRuntime: (runtimeInput) =>
4529 makeRuntime({
4530 ...runtimeInput,
4531 protocolLogging: {
4532 ...runtimeInput.protocolLogging,
4533 logger: (event) =>
4534 Effect.sync(() => void events.push(event)).pipe(
4535 Effect.andThen(runtimeInput.protocolLogging.logger?.(event) ?? Effect.void),
4536 ),
4537 },
4538 }),
4539 },
4540 selfInvocation,
4541 ...(nativeLogging === undefined ? {} : { nativeLogging }),
4542 });
4543 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
4544 runtimeMode: "full-access",
4545 interactionMode: "default",
4546 cwd: process.cwd(),
4547 });
4548 const modelSelection = { instanceId, model: "default" } as const;
4549 const runtime = yield* adapter.openSession({
4550 threadId,
4551 providerSessionId: ProviderSessionId.make(`provider-session-acp-${name}`),
4552 modelSelection,
4553 runtimePolicy,
4554 });
4555 const providerThread = yield* runtime.ensureThread({
4556 threadId,
4557 modelSelection,
4558 runtimePolicy,
4559 });
4560 yield* runtime.startTurn(
4561 makeTurnInput({
4562 threadId,
4563 providerThread,
4564 instanceId,
4565 runtimePolicy,
4566 now: yield* DateTime.now,
4567 ordinal: 1,
4568 }),
4569 );
4570 const terminal = yield* runtime.events.pipe(
4571 Stream.filter((event) => event.type === "turn.terminal"),
4572 Stream.runHead,
4573 );
4574 assert.equal(Option.getOrThrow(terminal).status, "completed");
4575 return events;
4576 });
4577
4578 assert.deepEqual(yield* runTurn("protocol-logging-default"), []);
4579
4580 const explicit: Array<EffectAcpProtocol.AcpProtocolLogEvent> = [];
4581 yield* runTurn("protocol-logging-explicit", () => ({
4582 protocolLogging: {
4583 logIncoming: true,
4584 logOutgoing: true,
4585 logger: (event) => Effect.sync(() => void explicit.push(event)),
4586 },
4587 }));
4588 assert.isTrue(explicit.some((e) => e.direction === "incoming" && e.stage === "raw"));
4589 assert.isTrue(explicit.some((e) => e.direction === "outgoing"));
4590 }).pipe(Effect.provide(layerTest)),
4591 );
4592
4593 it.effect("advertises ACP session notices and shows them as system notices", () =>
4594 Effect.gen(function* () {
4595 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
4596 const selfInvocation = yield* resolveSelfInvocation();
4597 const mockAgentPath = yield* bundledMockAgentPath;
4598 const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
4599 const instanceId = ProviderInstanceId.make("acp-test-notice");
4600 const adapter = yield* makeAcpAdapterV2({
4601 instanceId,
4602 flavor: {
4603 driver: ACP_TEST_DRIVER,
4604 capabilities: AcpProviderCapabilitiesV2,
4605 makeRuntime: makeMockRuntime({
4606 childProcessSpawner,
4607 mockAgentPath,
4608 protocolEvents,
4609 environment: { T3_ACP_EMIT_NOTICE: "1" },
4610 }),
4611 },
4612 selfInvocation,
4613 });
4614 const threadId = ThreadId.make("thread-acp-notice");
4615 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
4616 runtimeMode: "full-access",
4617 interactionMode: "default",
4618 cwd: process.cwd(),
4619 });
4620 const modelSelection = { instanceId, model: "default" } as const;
4621 const runtime = yield* adapter.openSession({
4622 threadId,
4623 providerSessionId: ProviderSessionId.make("provider-session-acp-notice"),
4624 modelSelection,
4625 runtimePolicy,
4626 });
4627 const providerThread = yield* runtime.ensureThread({
4628 threadId,
4629 modelSelection,
4630 runtimePolicy,
4631 });
4632 const now = yield* DateTime.now;
4633 yield* runtime.startTurn(
4634 makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal: 1 }),
4635 );
4636 const items = Array.from(
4637 yield* runtime.events.pipe(
4638 Stream.takeUntil((event) => event.type === "turn.terminal"),
4639 Stream.runCollect,
4640 ),
4641 ).flatMap((event) => (event.type === "turn_item.updated" ? [event.turnItem] : []));
4642
4643 const initialize = Array.from(yield* Queue.takeAll(protocolEvents)).find(
4644 (event) => event.direction === "outgoing" && rawProtocolMethod(event) === "initialize",
4645 );
4646 assert.deepEqual(
4647 initialize === undefined
4648 ? undefined
4649 : Reflect.get(
4650 Object(rawProtocolRequestParam(initialize, "clientCapabilities")),
4651 "session",
4652 ),
4653 { notices: {}, compaction: {} },
4654 );
4655 const title =
4656 "skill discovery warning: candidate was skipped because its metadata is invalid";
4657 assert.deepEqual(
4658 items.flatMap((item) => (item.type === "system_notice" ? [item.message] : [])),
4659 [title],
4660 );
4661 const replies = new Map(
4662 items.flatMap((item) => (item.type === "assistant_message" ? [[item.id, item.text]] : [])),
4663 );
4664 assert.deepEqual(
4665 [...replies.values()],
4666 ["before the notice, after the notice"],
4667 "a notice neither joins the reply nor splits it",
4668 );
4669 }).pipe(Effect.provide(layerTest)),
4670 );
4671
4672 it.effect("projects ACP v2 fidelity updates into first-class orchestration items", () =>
4673 Effect.gen(function* () {
4674 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
4675 const selfInvocation = yield* resolveSelfInvocation();
4676 const mockAgentPath = yield* bundledMockAgentPath;
4677 type RuntimeService = AcpSessionRuntime.AcpSessionRuntime["Service"];
4678 let sessionUpdateHandler: Parameters<RuntimeService["handleSessionUpdate"]>[0] | undefined;
4679 const instanceId = ProviderInstanceId.make("acp-test-v2-fidelity");
4680 const adapter = yield* makeAcpAdapterV2({
4681 instanceId,
4682 flavor: {
4683 driver: ACP_TEST_DRIVER,
4684 capabilities: AcpProviderCapabilitiesV2,
4685 makeRuntime: makeMockRuntime({
4686 childProcessSpawner,
4687 mockAgentPath,
4688 environment: { T3_ACP_EMIT_V2_FIDELITY: "1" },
4689 wrapRuntime: (runtime) => ({
4690 ...runtime,
4691 handleSessionUpdate: (handler) =>
4692 Effect.sync(() => {
4693 sessionUpdateHandler = handler;
4694 }).pipe(Effect.andThen(runtime.handleSessionUpdate(handler))),
4695 }),
4696 }),
4697 },
4698 selfInvocation,
4699 });
4700 const threadId = ThreadId.make("thread-acp-v2-fidelity");
4701 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
4702 runtimeMode: "full-access",
4703 interactionMode: "default",
4704 cwd: process.cwd(),
4705 });
4706 const modelSelection = { instanceId, model: "default" } as const;
4707 const runtime = yield* adapter.openSession({
4708 threadId,
4709 providerSessionId: ProviderSessionId.make("provider-session-acp-v2-fidelity"),
4710 modelSelection,
4711 runtimePolicy,
4712 });
4713 const providerThread = yield* runtime.ensureThread({
4714 threadId,
4715 modelSelection,
4716 runtimePolicy,
4717 });
4718 const now = yield* DateTime.now;
4719 yield* runtime.startTurn(
4720 makeTurnInput({
4721 threadId,
4722 providerThread,
4723 instanceId,
4724 runtimePolicy,
4725 now,
4726 ordinal: 1,
4727 }),
4728 );
4729 const events = Array.from(
4730 yield* runtime.events.pipe(
4731 Stream.takeUntil((event) => event.type === "turn.terminal"),
4732 Stream.runCollect,
4733 ),
4734 );
4735 const plans = events.flatMap((event) => (event.type === "plan.updated" ? [event.plan] : []));
4736 const planA = plans.filter((plan) => plan.kind === "proposed_plan");
4737 const planB = plans.find((plan) => plan.kind === "todo_list");
4738 assert.equal(planA.length, 2);
4739 assert.equal(planA[0]?.id, planA[1]?.id);
4740 assert.equal(planA[1]?.status, "superseded");
4741 assert.isDefined(planB);
4742 assert.notEqual(planA[0]?.id, planB?.id);
4743 const planC = plans.find(
4744 (plan) =>
4745 plan.kind === "todo_list" && plan.steps.some((step) => step.text === "Write the fix"),
4746 );
4747 assert.ok(planC?.kind === "todo_list");
4748 assert.deepEqual(
4749 planC.steps.map((step) => ({ id: step.id, text: step.text, status: step.status })),
4750 [
4751 { id: "acp-step-1", text: "Write the fix", status: "completed" },
4752 { id: "acp-step-3", text: "Add a test", status: "completed" },
4753 ],
4754 );
4755 assert.equal(planC.status, "completed");
4756
4757 const items = events.flatMap((event) =>
4758 event.type === "turn_item.updated" ? [event.turnItem] : [],
4759 );
4760 assert.isFalse(
4761 items.some((item) => item.type === "user_message" && item.text === "stale user text"),
4762 "the active ACP prompt echo must not create a second user message",
4763 );
4764 assert.isTrue(items.some((item) => item.type === "user_message" && item.text.length === 0));
4765 assert.equal(
4766 items.filter((item) => item.type === "assistant_message").at(-1)?.text,
4767 "authoritative answer",
4768 );
4769 assert.isTrue(
4770 items.some((item) => item.type === "reasoning" && item.text === "final thought"),
4771 );
4772 assert.deepInclude(
4773 items.flatMap((item) =>
4774 item.type === "command_execution" ? [{ input: item.input, output: item.output }] : [],
4775 ),
4776 { input: "printf proof", output: "proof" },
4777 );
4778 assert.deepInclude(
4779 items.flatMap((item) =>
4780 item.type === "command_execution" ? [{ input: item.input, output: item.output }] : [],
4781 ),
4782 { input: "cat probe.txt", output: "after\n" },
4783 );
4784 assert.isTrue(
4785 items.some((item) => item.title === "Action required" && item.status === "waiting"),
4786 );
4787 assert.isTrue(
4788 items.some(
4789 (item) =>
4790 item.type === "file_change" &&
4791 item.changes?.[0]?.operation === "move" &&
4792 item.changes[0]?.oldPath === "/workspace/old.ts",
4793 ),
4794 );
4795 const readItem = (nativeId: string) => {
4796 const item = items.findLast(
4797 (candidate) =>
4798 candidate.type === "dynamic_tool" &&
4799 candidate.toolName === "Read" &&
4800 candidate.nativeItemRef?.nativeId?.endsWith(nativeId) === true,
4801 );
4802 return item?.type === "dynamic_tool"
4803 ? { title: item.title, input: item.input, viewedImagePath: item.viewedImagePath }
4804 : null;
4805 };
4806 assert.deepEqual(readItem("structured-read"), {
4807 title: "Read src/env.ts",
4808 input: { path: "src/env.ts" },
4809 viewedImagePath: undefined,
4810 });
4811 assert.deepEqual(readItem("structured-image-read"), {
4812 title: "Read /workspace/mockup.png",
4813 input: { file_path: "/workspace/mockup.png", start_line: 0, end_line: 799 },
4814 viewedImagePath: "/workspace/mockup.png",
4815 });
4816 const search = items.find((item) => item.type === "file_search");
4817 assert.deepEqual(
4818 search?.type === "file_search" ? { title: search.title, pattern: search.pattern } : null,
4819 { title: "Searched TODO in web", pattern: "apps/web" },
4820 );
4821 const fileSearch = (nativeId: string) => {
4822 const item = items.find(
4823 (candidate) =>
4824 candidate.type === "file_search" &&
4825 candidate.nativeItemRef?.nativeId?.endsWith(nativeId) === true,
4826 );
4827 return item?.type === "file_search"
4828 ? { pattern: item.pattern, results: item.results }
4829 : null;
4830 };
4831 assert.deepEqual(fileSearch("structured-search-explicit-location"), {
4832 pattern: "README.md",
4833 results: [{ fileName: "README.md", preview: "# Title" }],
4834 });
4835 assert.deepEqual(fileSearch("structured-search-query-only"), {
4836 pattern: "TODO",
4837 results: [{ fileName: "TODO", preview: "src/a.ts: TODO" }],
4838 });
4839 assert.deepEqual(fileSearch("structured-search-query-no-output"), {
4840 pattern: "FIXME",
4841 results: undefined,
4842 });
4843 const webItem = (nativeId: string, status: string) => {
4844 const item = items.findLast(
4845 (candidate) =>
4846 candidate.type === "web_search" &&
4847 candidate.status === status &&
4848 candidate.nativeItemRef?.nativeId?.endsWith(nativeId) === true,
4849 );
4850 return item?.type === "web_search"
4851 ? { title: item.title, patterns: item.patterns, results: item.results }
4852 : null;
4853 };
4854 assert.deepEqual(webItem("grok-x-search", "running"), {
4855 title: "X search",
4856 patterns: undefined,
4857 results: undefined,
4858 });
4859 assert.deepEqual(webItem("grok-x-search", "completed"), {
4860 title: "X search: conversation_id:42",
4861 patterns: ["conversation_id:42"],
4862 results: undefined,
4863 });
4864 assert.deepEqual(webItem("grok-web-search", "completed"), {
4865 title: "Web search: t3 code",
4866 patterns: ["t3 code"],
4867 results: [{ url: "https://t3.codes" }, { url: "https://github.com/pingdotgg/t3code" }],
4868 });
4869 assert.deepEqual(webItem("antigravity-web-search", "completed"), {
4870 title: "Web search: top story on Hacker News today",
4871 patterns: ["top story on Hacker News today"],
4872 results: [
4873 {
4874 url: "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQGYNzfz",
4875 },
4876 ],
4877 });
4878 assert.deepEqual(webItem("antigravity-run-search-web", "completed"), {
4879 title: "Web search: top story on Hacker News today",
4880 patterns: ["top story on Hacker News today"],
4881 results: undefined,
4882 });
4883 assert.deepEqual(webItem("antigravity-web-search-streamed", "completed"), {
4884 title: "Web search: streamed citations",
4885 patterns: ["streamed citations"],
4886 results: [{ url: "https://example.com/streamed_(detail)" }],
4887 });
4888 assert.deepEqual(webItem("grok-web-fetch", "completed")?.results, [
4889 { url: "https://t3.codes", snippet: "T3 Code page" },
4890 ]);
4891 const completedCompaction = items.find(
4892 (item) =>
4893 item.type === "compaction" &&
4894 item.status === "completed" &&
4895 item.summary === "Retained decisions.",
4896 );
4897 assert.isDefined(completedCompaction);
4898 assert.notProperty(completedCompaction!, "afterTokenCount");
4899
4900 assert.isDefined(sessionUpdateHandler);
4901 yield* sessionUpdateHandler!({
4902 sessionId: "mock-session-1",
4903 update: {
4904 sessionUpdate: "agent_message",
4905 messageId: "late-authoritative-message",
4906 content: [{ type: "text", text: "late authoritative text" }],
4907 },
4908 });
4909 yield* sessionUpdateHandler!({
4910 sessionId: "mock-session-1",
4911 update: {
4912 sessionUpdate: "agent_message",
4913 messageId: "late-authoritative-message",
4914 },
4915 });
4916 const afterOmittedPatch = yield* runtime.readThreadSnapshot({ providerThread });
4917 assert.isTrue(
4918 afterOmittedPatch.messages.some((message) => message.text === "late authoritative text"),
4919 "omitted late message content must preserve the authoritative text",
4920 );
4921 }).pipe(Effect.provide(layerTest)),
4922 );
4923
4924 it.effect("projects only the tail of long ACP terminal output", () =>
4925 Effect.gen(function* () {
4926 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
4927 const mockAgentPath = yield* bundledMockAgentPath;
4928 const instanceId = ProviderInstanceId.make("acp-test-long-terminal");
4929 const adapter = yield* makeAcpAdapterV2({
4930 instanceId,
4931 flavor: {
4932 driver: ACP_TEST_DRIVER,
4933 capabilities: AcpProviderCapabilitiesV2,
4934 makeRuntime: makeMockRuntime({
4935 childProcessSpawner,
4936 mockAgentPath,
4937 environment: { T3_ACP_EMIT_TERMINAL_OUTPUT_BYTES: String(1024 * 1024) },
4938 }),
4939 },
4940 selfInvocation: yield* resolveSelfInvocation(),
4941 });
4942 const threadId = ThreadId.make("thread-acp-long-terminal");
4943 const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
4944 runtimeMode: "full-access",
4945 interactionMode: "default",
4946 cwd: process.cwd(),
4947 });
4948 const modelSelection = { instanceId, model: "default" } as const;
4949 const runtime = yield* adapter.openSession({
4950 threadId,
4951 providerSessionId: ProviderSessionId.make("provider-session-acp-long-terminal"),
4952 modelSelection,
4953 runtimePolicy,
4954 });
4955 const providerThread = yield* runtime.ensureThread({
4956 threadId,
4957 modelSelection,
4958 runtimePolicy,
4959 });
4960 const now = yield* DateTime.now;
4961 yield* runtime.startTurn(
4962 makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal: 1 }),
4963 );
4964 const outputs = Array.from(
4965 yield* runtime.events.pipe(
4966 Stream.takeUntil((event) => event.type === "turn.terminal"),
4967 Stream.runCollect,
4968 ),
4969 ).flatMap((event) =>
4970 event.type === "turn_item.updated" && event.turnItem.type === "command_execution"
4971 ? [event.turnItem.output ?? ""]
4972 : [],
4973 );
4974 const fullOutput = Array.from(
4975 { length: (1024 * 1024) / 64 },
4976 (_, index) => `${String(index + 1).padStart(8, "0")} ${"x".repeat(54)}\n`,
4977 ).join("");
4978
4979 assert.isAtLeast(outputs.length, 2);
4980 assert.isAtMost(Math.max(...outputs.map((output) => output.length)), 320 * 1024);
4981 assert.equal(outputs.at(-1), fullOutput.slice(-320 * 1024));
4982 }).pipe(Effect.provide(layerTest)),
4983 );
4984
4985 it.live(
4986 "loads the persisted ACP session during startup without creating a throwaway session",
4987 () =>
4988 Effect.gen(function* () {
4989 const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
4990 const idAllocator = yield* IdAllocator.IdAllocatorV2;
4991 const selfInvocation = yield* resolveSelfInvocation();
4992 const mockAgentPath = yield* bundledMockAgentPath;
4993 const protocolEvents = yield* Queue.unbounded<EffectAcpProtocol.AcpProtocolLogEvent>();
4994 const instanceId = ProviderInstanceId.make("acp-test-eager-resume");
4995 const adapter = yield* makeAcpAdapterV2({
4996 instanceId,
4997 flavor: {
4998 driver: ACP_TEST_DRIVER,
4999 capabilities: AcpProviderCapabilitiesV2,
5000 makeRuntime: makeMockRuntime({
Showing first 5,000 of 12,259 lines. View raw