import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, describe, it } from "@effect/vitest"; import { EnvironmentId, MessageId, NodeId, ProjectId, RunAttemptId, RunId, type ModelSelection, ProviderInstanceId, ProviderSessionId, ThreadId, } from "@t3tools/contracts"; import { pendingBackgroundTurnItems } from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; import { resolveSelfInvocation, selfInvocationArgs } from "@t3tools/shared/nodeRuntime"; import * as Exit from "effect/Exit"; import * as Fiber from "effect/Fiber"; import * as DateTime from "effect/DateTime"; import * as Queue from "effect/Queue"; import * as Stream from "effect/Stream"; import * as TestClock from "effect/testing/TestClock"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; import * as HttpClient from "effect/http/HttpClient"; import * as HttpClientResponse from "effect/http/HttpClientResponse"; import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import * as EffectAcpErrors from "effect-acp/errors"; import { makeProviderFailure } from "@t3tools/provider-core/server/failure"; import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost"; import { makeProviderReplayGate, type ProviderReplayGate, } from "@t3tools/provider-testing/replayGate"; import type { ProviderContinuationRequest } from "@t3tools/provider-core/server/ProviderContinuationRequests"; import * as ServerSettings from "../../serverSettings.ts"; import type { AcpRegistryLiveConfiguration } from "@t3tools/provider-acp-registry/testing"; import * as AcpRegistrySupport from "@t3tools/provider-acp-registry/server/AcpRegistrySupport"; import * as AcpRegistryRuntimeCoordinator from "@t3tools/provider-acp-registry/server/AcpRegistryRuntimeCoordinator"; import { ACP_SESSION_MODE_OPTION_ID } from "@t3tools/provider-acp/server/sessionConfig"; import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime"; import { t3AcpPromptWithInstructions } from "@t3tools/provider-core/server/orchestrationInstructions"; import { buildRuntimeInstructions } from "@t3tools/provider-core/server/runtimeInstructions"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import { decodeAcpReplayTranscript, makeAcpReplayCompletenessAssertion, makeAcpReplayRuntime, makeTurnInput, } from "./AcpAdapterV2.testkit.ts"; import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; import { AcpRegistryAdapterV2Driver } from "@t3tools/provider-acp-registry/server"; import { ACP_REGISTRY_PROVIDER, makeAcpRegistryAdapterV2, acpRegistryPromptFailure, acpRegistryProbeResult, } from "@t3tools/provider-acp-registry/testing"; const decodeAcpRegistryAdapterSettings = Schema.decodeUnknownEffect( AcpRegistryAdapterV2Driver.configSchema, ); const layerHost = TestProviderHost.layer().pipe( Layer.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), ); const layerRegistry = Layer.succeed( HttpClient.HttpClient, HttpClient.make((request) => Effect.succeed( HttpClientResponse.fromWeb( request, Response.json({ version: "1.0.0", agents: [ { id: "fixture-agent", name: "Fixture Agent", version: "1.0.0", description: "ACP V2 adapter fixture", distribution: { binary: { "darwin-aarch64": { archive: "https://registry.test/unused", cmd: "fixture-agent", args: [], }, "linux-x86_64": { archive: "https://registry.test/unused", cmd: "fixture-agent", args: [], }, }, }, }, ], }), ), ), ), ); const layerInjectedRuntimeCatalog = Layer.mock(AcpRegistrySupport.AcpRegistryCatalog)({ resolve: () => Effect.die("the runtime is injected"), }); const layerTest = Layer.mergeAll( NodeServices.layer, NodeCrypto.layer, IdAllocator.layer, McpProviderSessions.layer, layerHost, layerRegistry, ServerSettings.layerTest(), ); const CLAUDE_ROOT_SESSION = "claude-session"; /** Replay frames of Claude Agent (claude-agent-acp 0.90.0) on ACP v2. */ const claudeFrames = { open: [ { type: "expect_outbound", frame: { kind: "request", method: "initialize", params: "" } }, { type: "emit_inbound", frame: { kind: "response", method: "initialize", result: { protocolVersion: 2, info: { name: "@agentclientprotocol/claude-agent-acp", version: "0.90.0" }, capabilities: { session: {} }, authMethods: [], }, }, }, { type: "expect_outbound", frame: { kind: "request", method: "session/new", params: "" } }, { type: "emit_inbound", frame: { kind: "response", method: "session/new", result: { sessionId: CLAUDE_ROOT_SESSION }, }, }, ], update: (sessionId: string, sessionUpdate: Record) => ({ type: "emit_inbound", frame: { kind: "notification", method: "session/update", params: { sessionId, update: sessionUpdate }, }, }), childState: (parent: string, child: string, state: string) => claudeFrames.update(parent, { sessionUpdate: "subagent_update", sessionId: child, state: { state }, }), /** A prompt Claude takes in and works on. */ prompt: (ordinal: number) => [ { type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: "" }, }, claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "user_message", messageId: `user-${ordinal}`, content: [{ type: "text", text: "test prompt" }], }), { type: "emit_inbound", frame: { kind: "response", method: "session/prompt", result: { messageId: `user-${ordinal}` }, }, }, claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "state_update", state: "running" }), ], idle: (stopReason: string) => claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "state_update", state: "idle", stopReason, }), closeRequest: { type: "expect_outbound", frame: { kind: "request", method: "session/close", params: { sessionId: CLAUDE_ROOT_SESSION }, }, }, closed: { type: "emit_inbound", frame: { kind: "response", method: "session/close", result: {} }, }, }; const openClaudeReplaySession = Effect.fn("openClaudeReplaySession")(function* ( entries: ReadonlyArray, replayGate?: ProviderReplayGate, continuationEnabled = true, agentId: string | Record = "claude-acp", ) { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; const replayDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-lifecycle-" }); const transcript = yield* decodeAcpReplayTranscript( { provider: ACP_REGISTRY_PROVIDER, protocol: "acp.ndjson-jsonrpc", version: "1", scenario: "claude-lifecycle", entries: entries as never, }, ACP_REGISTRY_PROVIDER, ); const offers: Array = []; const offered = yield* Queue.unbounded(); const continuationRequests = { offer: (request: ProviderContinuationRequest) => Effect.sync(() => { offers.push(request); }).pipe(Effect.andThen(Queue.offer(offered, undefined)), Effect.asVoid), }; const instanceId = ProviderInstanceId.make("claude-lifecycle"); const outgoing = yield* Queue.unbounded(); let onTermination: NonNullable< AcpSessionRuntime.AcpSessionRuntimeOptions["onTermination"] > = () => Effect.die("Runtime has not started"); const replayRuntime = makeAcpReplayRuntime({ transcript, statusPath: path.join(replayDir, "status.json"), scriptPath: yield* path.fromFileUrl( new URL("../../../scripts/acp-replay-agent.ts", import.meta.url), ), childProcessSpawner, fileSystem, ...(replayGate === undefined ? {} : { replayGate }), }); const adapterOptions = { selfInvocation: yield* resolveSelfInvocation(), instanceId, settings: yield* decodeAcpRegistryAdapterSettings( typeof agentId === "string" ? { agentId } : agentId, ), environment: {}, ...(continuationEnabled ? { continuationRequests } : {}), makeRuntime: (runtimeInput: Parameters[0]) => { onTermination = runtimeInput.onTermination; return replayRuntime({ ...runtimeInput, protocolLogging: { logIncoming: false, logOutgoing: true, logger: (event) => event.stage === "raw" && typeof event.payload === "string" ? Queue.offer(outgoing, JSON.parse(event.payload)).pipe(Effect.asVoid) : Effect.void, }, }); }, }; const adapter = yield* makeAcpRegistryAdapterV2(adapterOptions).pipe( Effect.provide(layerInjectedRuntimeCatalog), ); const threadId = ThreadId.make("thread-claude-lifecycle"); const modelSelection = { instanceId, model: "default" } as const; const runtimeMode = "approval-required"; const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ runtimeMode, interactionMode: "default", cwd: replayDir, }); const runtime = yield* adapter.openSession({ threadId, providerSessionId: ProviderSessionId.make("session-claude-lifecycle"), modelSelection, runtimePolicy, }); yield* Effect.addFinalizer(() => Effect.sync(() => replayGate?.releaseAll())); if (continuationEnabled && agentId === "claude-acp") { assert.isDefined( runtime.hasPendingBackgroundWork, "Claude ACP v2 must report background work after foreground idle", ); } const events = yield* Queue.unbounded(); yield* runtime.events.pipe( Stream.runForEach((event) => Queue.offer(events, event)), Effect.forkScoped, ); const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy }); const now = yield* DateTime.now; const turnInput = ( ordinal: number, options: { readonly continuation?: boolean } = {}, ): ProviderAdapter.ProviderAdapterV2TurnInput => ({ appThread: { createdBy: "user", creationSource: "web", id: threadId, projectId: ProjectId.make(`project-claude-lifecycle`), title: "Claude Agent", providerInstanceId: instanceId, modelSelection, runtimeMode, interactionMode: "default", branch: null, worktreePath: null, activeProviderThreadId: providerThread.id, lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId }, forkedFrom: null, createdAt: now, updatedAt: now, archivedAt: null, settledOverride: null, settledAt: null, lastVisitedAt: null, deletedAt: null, }, threadId, runId: RunId.make(`run-claude-lifecycle-${ordinal}`), runOrdinal: ordinal, providerTurnOrdinal: ordinal, attemptId: RunAttemptId.make(`attempt-claude-lifecycle-${ordinal}`), rootNodeId: NodeId.make(`node-claude-lifecycle-${ordinal}`), providerThread, message: { ...(options.continuation === true ? { createdBy: "agent" as const, creationSource: "provider" as const } : { createdBy: "user" as const, creationSource: "web" as const }), messageId: MessageId.make(`message-claude-lifecycle-${ordinal}`), text: "test prompt", attachments: [], }, modelSelection, runtimePolicy, }); const until = (predicate: (event: ProviderAdapter.ProviderAdapterV2Event) => boolean) => Effect.gen(function* () { const seen: Array = []; while (true) { const event = yield* Queue.take(events); seen.push(event); if (predicate(event)) return seen; } }); const throughMarker = (title: string) => until( (event) => event.type === "provider_thread.updated" && event.providerThread.nativeMetadata?.title === title, ); const terminal = until((event) => event.type === "turn.terminal").pipe( Effect.map((seen) => { const event = seen.at(-1)!; if (event.type !== "turn.terminal") throw new Error("Missing terminal"); return event; }), ); return { runtime, providerThread, turnInput, until, throughMarker, terminal, offers, offered, outgoing, terminate: () => onTermination( new EffectAcpErrors.AcpTransportError({ detail: "Injected fixture process exit", cause: "test", }), ), }; }); const claudeMarker = (title: string) => claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "session_info_update", title, }); const claudeChild = [ claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "agent-1", name: "Agent", title: "Review", kind: "think", status: "in_progress", rawInput: { prompt: "Review the code", description: "Review", run_in_background: true }, }), claudeFrames.childState(CLAUDE_ROOT_SESSION, "child-1", "running"), claudeFrames.update("child-1", { sessionUpdate: "session_message", messageId: "agent-1", senderSessionId: CLAUDE_ROOT_SESSION, recipientSessionId: "child-1", content: [{ type: "text", text: "Review the code" }], }), claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "agent-1", status: "completed", _meta: { claudeCode: { toolResponse: { isAsync: true } } }, }), ]; const claudeUnrelatedChildren = [ claudeFrames.childState("unknown-parent", "unrelated-child", "running"), claudeFrames.update("unrelated-child", { sessionUpdate: "session_message", messageId: "agent-1", senderSessionId: "unknown-parent", content: [{ type: "text", text: "Unrelated delegation" }], }), claudeFrames.update("unlinked-child", { sessionUpdate: "agent_message_chunk", messageId: "unlinked-reply", content: { type: "text", text: "Unlinked output" }, }), ]; describe("AcpRegistryAdapterV2", () => { it("preserves and sanitizes structured ACP errors without exposing arbitrary defects", () => { const limit = new EffectAcpErrors.AcpRequestError({ code: -31001, errorMessage: "Rate limit exceeded for mistral (model: mistral-vibe-cli-latest).", }); assert.deepEqual(acpRegistryPromptFailure("mistral-vibe", limit), { class: "usage_limit", message: limit.errorMessage, code: "-31001", retryable: null, }); assert.equal(acpRegistryPromptFailure("other-agent", limit).class, "provider_error"); assert.equal( acpRegistryPromptFailure("mistral-vibe", new Error("private defect")).message, "Provider turn failed.", ); const rejected = acpRegistryPromptFailure( "any-agent", new EffectAcpErrors.AcpRequestError({ code: -32603, errorMessage: "Request rejected. api_key=private-key https://example.test/?token=secret", }), ); assert.include(rejected.message, "Request rejected."); assert.notInclude(rejected.message, "private-key"); assert.notInclude(rejected.message, "token=secret"); }); describe("the agent's own pickers", () => { type Frame = Record; const outbound = (method: string, params: unknown = ""): Frame => ({ type: "expect_outbound", frame: { kind: "request", method, params }, }); const answer = (method: string, result: unknown): Frame => ({ type: "emit_inbound", frame: { kind: "response", method, result }, }); const permissionModeOption = (currentValue: string) => ({ id: "permission-mode", name: "Permission mode", category: "mode", type: "select", currentValue, options: ["ask", "auto"].map((value) => ({ value, name: value })), }); const openWithStoredPicks = Effect.fn("openWithStoredPicks")(function* (input: { readonly setup: unknown; readonly pickFrames: ReadonlyArray; readonly agentId?: string; readonly customModels?: ReadonlyArray; readonly storedModel?: string; readonly storedModePick?: string; readonly turnModels?: ReadonlyArray; readonly rejectedModel?: string; readonly storedOptions?: ReadonlyArray<{ readonly id: string; readonly value: string }>; readonly failsConfiguration?: boolean; readonly liveConfiguration?: AcpRegistryLiveConfiguration; }) { const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; // Scope release gives the replay agent at most a second to exit, so the status // read below waits for the agent itself instead of racing its last write. const agentExits: Array> = []; const childProcessSpawner = ChildProcessSpawner.make((command) => spawner .spawn(command) .pipe( Effect.tap((handle) => Effect.sync(() => agentExits.push(Effect.ignore(handle.exitCode))), ), ), ); const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const replayDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-acp-registry-stored-picks-", }); const statusPath = path.join(replayDir, "status.json"); const transcript = yield* decodeAcpReplayTranscript( { provider: ACP_REGISTRY_PROVIDER, protocol: "acp.ndjson-jsonrpc", version: "1", scenario: "stored-picks", entries: [ outbound("initialize"), answer("initialize", { protocolVersion: 1, agentCapabilities: { loadSession: false }, authMethods: [{ id: "test", name: "Test" }], }), outbound("session/new"), answer("session/new", { sessionId: "agent-session", ...(input.setup as object) }), ...input.pickFrames, ] as never, }, ACP_REGISTRY_PROVIDER, ); const instanceId = ProviderInstanceId.make("acp-registry-stored-picks"); let published: AcpRegistryLiveConfiguration | undefined; const adapter = yield* makeAcpRegistryAdapterV2({ selfInvocation: yield* resolveSelfInvocation(), instanceId, settings: yield* decodeAcpRegistryAdapterSettings({ agentId: input.agentId ?? "fixture-agent", authMethodId: "test", customModels: input.customModels ?? [], }), environment: {}, makeRuntime: makeAcpReplayRuntime({ customModels: input.customModels, transcript, statusPath, scriptPath: yield* path.fromFileUrl( new URL("../../../scripts/acp-replay-agent.ts", import.meta.url), ), childProcessSpawner, fileSystem, }), }).pipe( Effect.provide( Layer.mergeAll( layerInjectedRuntimeCatalog, Layer.mock(AcpRegistryRuntimeCoordinator.AcpRegistryRuntimeCoordinator)({ withForegroundStartup: (_startupKey, effect) => effect, getLiveConfiguration: () => Effect.sync(() => Option.fromUndefinedOr(published ?? input.liveConfiguration)), publishLiveConfiguration: (_instanceId, configuration) => Effect.sync(() => { published = configuration; }), }), ), ), ); const threadId = ThreadId.make("thread-acp-registry-stored-picks"); const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ runtimeMode: "approval-required", interactionMode: "default", cwd: replayDir, }); const stored: ModelSelection = { instanceId, model: input.storedModel ?? "default", ...(input.storedModePick === undefined ? {} : { options: [{ id: ACP_SESSION_MODE_OPTION_ID, value: input.storedModePick }] }), ...(input.storedOptions === undefined ? {} : { options: input.storedOptions }), }; const configurations: Array = []; yield* Effect.gen(function* () { const runtime = yield* adapter.openSession({ threadId, providerSessionId: ProviderSessionId.make("provider-session-stored-picks"), modelSelection: stored, runtimePolicy, }); configurations.push(published); if (input.turnModels === undefined) return; const providerThread = yield* runtime.ensureThread({ threadId, modelSelection: stored, runtimePolicy, }); const now = yield* DateTime.now; let current = stored; for (const [index, model] of input.turnModels.entries()) { const target: ModelSelection = { instanceId, model }; const plan = yield* adapter.planSelectionTransition({ current, target, sessionCapabilities: runtime.providerSession.capabilities, }); assert.equal(plan.type, "apply_on_next_turn"); yield* runtime.startTurn( makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal: index + 1, modelSelection: target, }), ); yield* runtime.events.pipe( Stream.takeUntil((event) => event.type === "turn.terminal"), Stream.runForEach((event) => Effect.sync(() => { if (event.type === "turn.terminal") { assert.equal(event.status, "completed", event.failure?.message); } }), ), ); configurations.push(published); current = target; } }).pipe( Effect.match({ onFailure: (error) => { if (input.failsConfiguration === true) return; assert.isDefined(input.rejectedModel); assert.equal( makeProviderFailure({ cause: error }).message, `The agent did not accept the model "${input.rejectedModel}". Choose another model for this provider, then retry.`, ); }, onSuccess: () => { assert.isUndefined(input.rejectedModel); assert.notEqual(input.failsConfiguration, true, "configuration should have failed"); }, }), Effect.scoped, ); yield* Effect.all(agentExits, { discard: true }); yield* makeAcpReplayCompletenessAssertion(fileSystem, statusPath, transcript); return { configurations }; }); it.effect("switches an agent that only advertises modes with session/set_mode", () => openWithStoredPicks({ setup: { modes: { currentModeId: "default", availableModes: ["default", "autoEdit"].map((id) => ({ id, name: id })), }, }, pickFrames: [ outbound("session/set_mode", { sessionId: "agent-session", modeId: "autoEdit" }), answer("session/set_mode", {}), ], storedModePick: "autoEdit", }).pipe(Effect.provide(layerTest)), ); it.effect("switches a mode config option under its own id", () => openWithStoredPicks({ setup: { configOptions: [permissionModeOption("ask")] }, pickFrames: [ outbound("session/set_config_option", { sessionId: "agent-session", configId: "permission-mode", value: "auto", }), answer("session/set_config_option", { configOptions: [permissionModeOption("auto")] }), ], storedModePick: "auto", }).pipe(Effect.provide(layerTest)), ); const modelOption = (id: string, currentValue: string, grouped = false) => ({ id, name: "Model", category: "model", type: "select", currentValue, options: grouped ? [{ group: "models", name: "Models", options: [{ value: "default", name: "Default" }] }] : [{ value: "auto", name: "Auto" }], }); const configOptionUpdate = (configOptions: ReadonlyArray): Frame => ({ type: "emit_inbound", frame: { kind: "notification", method: "session/update", params: { sessionId: "agent-session", update: { sessionUpdate: "config_option_update", configOptions }, }, }, }); const prompt = (...updates: ReadonlyArray): ReadonlyArray => [ outbound("session/prompt"), ...updates, answer("session/prompt", { stopReason: "end_turn" }), ]; const setModel = (modelId: string): ReadonlyArray => [ outbound("session/set_model", { sessionId: "agent-session", modelId }), answer("session/set_model", {}), ]; it.effect( "discovers session models and switches through live model configuration changes", () => Effect.gen(function* () { const flatOption = (current: string) => modelOption("live-model", current); const groupedOption = (current: string) => modelOption("replacement-model", current, true); const { configurations } = yield* openWithStoredPicks({ setup: { models: { currentModelId: "hermes", availableModels: ["hermes", "auto", "default"].map((modelId) => ({ modelId, name: modelId, })), }, }, customModels: ["custom-model"], storedModel: "default", turnModels: [ "hermes", "custom-model", "auto", "default", "hermes", "custom-model", "auto", "custom-model", "default", "hermes", ], pickFrames: [ ...setModel("default"), ...setModel("hermes"), ...prompt(), ...setModel("custom-model"), ...prompt(), ...setModel("auto"), ...prompt(), ...setModel("default"), ...prompt(), ...setModel("hermes"), ...prompt(configOptionUpdate([flatOption("hermes")])), outbound("session/set_config_option", { sessionId: "agent-session", configId: "live-model", value: "custom-model", }), answer("session/set_config_option", { configOptions: [flatOption("custom-model")] }), ...prompt(), outbound("session/set_config_option", { sessionId: "agent-session", configId: "live-model", value: "auto", }), answer("session/set_config_option", { configOptions: [flatOption("auto")] }), ...prompt(configOptionUpdate([groupedOption("auto")])), outbound("session/set_config_option", { sessionId: "agent-session", configId: "replacement-model", value: "custom-model", }), answer("session/set_config_option", { configOptions: [groupedOption("custom-model")], }), ...prompt(), outbound("session/set_config_option", { sessionId: "agent-session", configId: "replacement-model", value: "default", }), answer("session/set_config_option", { configOptions: [groupedOption("default")] }), ...prompt(configOptionUpdate([])), ...setModel("hermes"), ...prompt(), ], }); assert.deepEqual( configurations.map((configuration) => ({ models: configuration?.models.map((model) => model.id), current: configuration?.currentModelId, })), [ { models: ["hermes", "auto", "default"], current: "default" }, { models: ["hermes", "auto", "default"], current: "hermes" }, { models: ["hermes", "auto", "default"], current: null }, { models: ["hermes", "auto", "default"], current: "auto" }, { models: ["hermes", "auto", "default"], current: "default" }, { models: ["auto"], current: null }, { models: ["auto"], current: null }, { models: ["default"], current: null }, { models: ["default"], current: null }, { models: ["hermes", "auto", "default"], current: "default" }, { models: ["hermes", "auto", "default"], current: "hermes" }, ], ); }).pipe(Effect.provide(layerTest)), ); // Copilot's session/new reports its built-in default, not the model it will run // (github/copilot-cli#5363), so the selected model must be written even when it matches, // and an unset effort gets the value the composer shows. it.effect( "writes the selected Copilot model and the shown effort when session/new reports defaults", () => { const copilotModel = (currentValue: string) => ({ ...modelOption("model", currentValue), options: ["claude-sonnet-5.5", "claude-haiku-4.5"].map((value) => ({ value, name: value, })), }); const effort = (currentValue: string) => ({ id: "reasoning_effort", name: "Reasoning effort", category: "thought_level", type: "select", currentValue, options: ["medium", "xhigh"].map((value) => ({ value, name: value })), }); return openWithStoredPicks({ agentId: "github-copilot-cli", setup: { configOptions: [copilotModel("claude-sonnet-5.5"), effort("medium")] }, storedModel: "claude-sonnet-5.5", liveConfiguration: { models: [], currentModelId: null, configOptions: [ { id: "reasoning_effort", label: "Reasoning effort", type: "select", role: "reasoning", currentValue: "xhigh", options: [ { id: "medium", label: "medium" }, { id: "xhigh", label: "xhigh" }, ], }, ], }, pickFrames: [ // The new session's own update replaces the shared live configuration. { type: "emit_inbound", frame: { kind: "notification", method: "session/update", params: { sessionId: "agent-session", update: { sessionUpdate: "config_option_update", configOptions: [copilotModel("claude-sonnet-5.5"), effort("medium")], }, }, }, }, outbound("session/set_config_option", { sessionId: "agent-session", configId: "model", value: "claude-sonnet-5.5", }), answer("session/set_config_option", { configOptions: [copilotModel("claude-sonnet-5.5"), effort("medium")], }), outbound("session/set_config_option", { sessionId: "agent-session", configId: "reasoning_effort", value: "xhigh", }), answer("session/set_config_option", { configOptions: [copilotModel("claude-sonnet-5.5"), effort("xhigh")], }), ], }).pipe(Effect.provide(layerTest)); }, ); it.effect.each([ "config", "legacy", "updated-config", "removed-config", "configuration-update", "unavailable-before-configuration", ] as const)( "rejects an unavailable explicit model in a %s catalog before inference", (catalog) => { const duringTurn = catalog === "updated-config" || catalog === "removed-config"; const duringConfiguration = catalog === "configuration-update"; const changesOptions = duringConfiguration || catalog === "unavailable-before-configuration"; const model = duringTurn || duringConfiguration ? "hermes" : "missing"; const restrictedOption = { ...modelOption("model", "other"), options: [{ value: "other", name: "Other" }], }; return openWithStoredPicks({ setup: { ...(catalog === "legacy" || catalog === "removed-config" ? { models: { currentModelId: "other", availableModels: [{ modelId: "other", name: "Other" }], }, } : {}), configOptions: catalog === "legacy" ? [] : [ { ...modelOption("model", "hermes"), options: [{ value: "hermes", name: "Hermes" }], }, ...(changesOptions ? [permissionModeOption("ask")] : []), ], }, storedModel: model, ...(duringTurn ? { turnModels: [model, model] } : {}), ...(changesOptions ? { storedModePick: "auto" } : {}), rejectedModel: model, pickFrames: duringTurn ? prompt(configOptionUpdate(catalog === "removed-config" ? [] : [restrictedOption])) : duringConfiguration ? [ outbound("session/set_config_option", { sessionId: "agent-session", configId: "permission-mode", value: "auto", }), answer("session/set_config_option", { configOptions: [restrictedOption, permissionModeOption("auto")], }), ] : [], }).pipe(Effect.provide(layerTest)); }, ); // regression: #19519 models come from session model state, not a hardcoded list it.effect("keeps unadvertised default and auto selections agent-controlled", () => Effect.gen(function* () { const { configurations } = yield* openWithStoredPicks({ setup: { models: { currentModelId: "hermes", availableModels: ["hermes", "other"].map((modelId) => ({ modelId, name: modelId })), }, }, storedModel: "default", turnModels: ["auto", "default", "other", "default", "auto", "default", "auto"], pickFrames: [ ...prompt(), ...prompt(), ...setModel("other"), ...prompt( configOptionUpdate([ { ...modelOption("live-model", "other"), options: [{ value: "other", name: "Other" }], }, ]), ), ...prompt(), ...prompt(), ...prompt( configOptionUpdate([ { ...modelOption("replacement-model", "other", true), options: [ { group: "models", name: "Models", options: [{ value: "other", name: "Other" }], }, ], }, ]), ), ...prompt(), ], }); assert.deepEqual( configurations.map((configuration) => configuration?.currentModelId), ["hermes", "hermes", "hermes", "other", "other", "other", "other", "other"], ); }).pipe(Effect.provide(layerTest)), ); it.effect( "fails configuration when an option write errors for a reason other than refusal", () => { const sessionModel = (currentValue: string) => ({ ...modelOption("model", currentValue), options: ["default", "named"].map((value) => ({ value, name: value })), }); const thinking = { id: "thinking", name: "Thinking", category: "thought_level", type: "select", currentValue: "low", options: ["low", "high"].map((value) => ({ value, name: value })), }; return openWithStoredPicks({ setup: { configOptions: [sessionModel("default")] }, storedModel: "named", storedOptions: [ { id: "thinking", value: "high" }, { id: "unavailable", value: "x" }, ], failsConfiguration: true, pickFrames: [ outbound("session/set_config_option", { sessionId: "agent-session", configId: "model", value: "named", }), answer("session/set_config_option", { configOptions: [sessionModel("named"), thinking], }), outbound("session/set_config_option", { sessionId: "agent-session", configId: "thinking", value: "high", }), { type: "emit_inbound", frame: { kind: "response", method: "session/set_config_option", error: { code: -32603, message: "Internal error" }, }, }, ], }).pipe(Effect.provide(layerTest)); }, ); const modelOptions = [ { id: "fast", name: "Fast", category: "model", type: "boolean", currentValue: false }, { id: " ", name: "Unnamed", category: "model", type: "select", currentValue: "unaddressable", options: [{ value: "unaddressable", name: "Unaddressable" }], }, { id: "provider", name: "Provider", category: "model", type: "select", currentValue: "gateway", options: [ { group: "hosted", name: "Hosted", options: [ { value: "gateway", name: "Gateway" }, { value: "codex", name: "Codex" }, ], }, ], }, { id: "model", name: "Model", category: "model", type: "select", currentValue: "later-model", options: [{ value: "later-model", name: "Later model" }], }, ] as const; it.effect("opens a model from the catalog by writing the first usable model select", () => Effect.gen(function* () { const setup = { sessionId: "agent-session", configOptions: modelOptions }; const catalog = acpRegistryProbeResult(ProviderInstanceId.make("acp-registry-mode-pick"), { sessionId: setup.sessionId, initializeResult: { protocolVersion: 1 }, sessionSetupResult: { ...setup, // The catalog uses the normalized groupId; ACP v1 sends group on the wire. configOptions: modelOptions.map((option) => option.id === "provider" ? { ...option, options: [{ ...option.options[0], groupId: option.options[0].group }], } : option, ), }, modelConfigId: "provider", }); assert.deepEqual(catalog.models, [ { id: "gateway", name: "Gateway", description: null }, { id: "codex", name: "Codex", description: null }, ]); assert.equal(catalog.currentModelId, "gateway"); const model = catalog.models[1]?.id; assert.isDefined(model); const opened = yield* openWithStoredPicks({ setup, pickFrames: [ outbound("session/set_config_option", { sessionId: "agent-session", configId: "provider", value: "codex", }), answer("session/set_config_option", { configOptions: modelOptions.map((option) => option.id === "provider" ? { ...option, currentValue: "codex" } : option, ), }), ], storedModel: model, }); assert.equal(opened.configurations.at(-1)?.currentModelId, "codex"); }).pipe(Effect.provide(layerTest)), ); }); // regression: #21618 Claude Agent needs ACP v2 for native background lifecycle it.effect("launches Claude Agent on ACP v2 unless its instance environment decides", () => Effect.gen(function* () { const launchSwitch = Effect.fn("launchSwitch")(function* ( config: Record, environment: NodeJS.ProcessEnv = {}, ) { let launched: NodeJS.ProcessEnv | undefined; const instanceId = ProviderInstanceId.make("acp-registry-launch-environment"); const adapter = yield* makeAcpRegistryAdapterV2({ selfInvocation: yield* resolveSelfInvocation(), instanceId, settings: yield* decodeAcpRegistryAdapterSettings(config), environment, }).pipe( Effect.provide( Layer.mock(AcpRegistrySupport.AcpRegistryCatalog)({ resolve: (_settings, _cwd, launchEnvironment) => { launched = launchEnvironment; return Effect.fail( new AcpRegistrySupport.AcpRegistryError({ reason: "runner_unavailable", detail: "Recorded the launch environment.", }), ); }, }), ), ); yield* adapter .openSession({ threadId: ThreadId.make("thread-acp-registry-launch-environment"), providerSessionId: ProviderSessionId.make("provider-session-launch-environment"), modelSelection: { instanceId, model: "default" }, runtimePolicy: ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ runtimeMode: "full-access", interactionMode: "default", cwd: process.cwd(), }), }) .pipe(Effect.flip); return launched?.CLAUDE_AGENT_ACP_EXPERIMENTAL_V2; }); assert.equal(yield* launchSwitch({ agentId: "claude-acp" }), "1"); assert.equal( yield* launchSwitch( { source: "local", commandPath: "/opt/bin/claude-agent-acp" }, { PATH: "/opt/bin" }, ), "1", ); assert.equal( yield* launchSwitch({ agentId: "claude-acp" }, { CLAUDE_AGENT_ACP_EXPERIMENTAL_V2: "0" }), "0", ); assert.isUndefined(yield* launchSwitch({ agentId: "gemini" })); const fallback = yield* openClaudeReplaySession( [ ...claudeFrames.open, claudeFrames.prompt(1)[0], claudeChild[0], claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "agent-1", status: "completed", }), ...claudeFrames.prompt(1).slice(1), claudeFrames.idle("end_turn"), claudeFrames.closeRequest, claudeFrames.closed, ], undefined, false, ); assert.isUndefined( fallback.runtime.hasPendingBackgroundWork, "an adapter without a continuation dispatcher must use ordinary prompt completion", ); yield* fallback.runtime.startTurn(fallback.turnInput(1)); const completed = yield* fallback.until((event) => event.type === "turn.terminal"); assert.isTrue( completed.some( (event) => event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool" && event.turnItem.nativeItemRef?.nativeId === "agent-1", ), ); const terminal = completed.at(-1)!; if (terminal.type !== "turn.terminal") throw new Error("Missing fallback terminal"); assert.equal(terminal.status, "completed"); }).pipe(Effect.provide(layerTest)), ); // regression: #26695 cancelling a Copilot prompt kills its background agents it.effect("never steers Copilot by cancelling its prompt", () => Effect.gen(function* () { const steering = Effect.fn("steering")(function* (config: Record) { const instanceId = ProviderInstanceId.make("acp-registry-steering"); const adapter = yield* makeAcpRegistryAdapterV2({ selfInvocation: yield* resolveSelfInvocation(), instanceId, settings: yield* decodeAcpRegistryAdapterSettings(config), environment: {}, }).pipe(Effect.provide(layerInjectedRuntimeCatalog)); const { turns } = yield* adapter.getCapabilities(); return { supportsInterrupt: turns.supportsInterrupt, supportsActiveSteering: turns.supportsActiveSteering, supportsSteeringByInterruptRestart: turns.supportsSteeringByInterruptRestart, }; }); const copilot = { supportsInterrupt: true, supportsActiveSteering: false, supportsSteeringByInterruptRestart: false, }; assert.deepEqual(yield* steering({ agentId: "github-copilot-cli" }), copilot); assert.deepEqual( yield* steering({ source: "local", commandPath: "C:\\tools\\copilot.cmd" }), copilot, ); assert.deepEqual(yield* steering({ agentId: "gemini" }), { ...copilot, supportsSteeringByInterruptRestart: true, }); }).pipe(Effect.provide(layerTest)), ); it.effect("gives HTTP-capable agents T3's MCP server over HTTP", () => Effect.gen(function* () { const selfInvocation = yield* resolveSelfInvocation(); const mockAgentPath = yield* (yield* Path.Path).fromFileUrl( new URL("../../../scripts/acp-mock-agent.ts", import.meta.url), ); const mcpSessions = yield* McpProviderSessions.McpProviderSessions; const endpoint = "http://127.0.0.1:43123/mcp"; const sessionMcpServers = Effect.fn("sessionMcpServers")(function* ( config: Record, agentEnvironment: Record = {}, ) { const requests: Array = []; const instanceId = ProviderInstanceId.make("acp-registry-mcp-transport"); const threadId = ThreadId.make("thread-acp-registry-mcp-transport"); const mcpSession = { environmentId: EnvironmentId.make("environment-acp-registry-mcp-transport"), threadId, providerSessionId: "mcp-session-acp-registry-mcp-transport", providerInstanceId: instanceId, endpoint, browserToolsAvailable: false, }; yield* mcpSessions.set({ ...mcpSession, authorizationHeader: "Bearer opened-token" }); const adapter = yield* makeAcpRegistryAdapterV2({ selfInvocation, instanceId, settings: yield* decodeAcpRegistryAdapterSettings(config), environment: {}, nativeLogging: () => ({ requestLogger: (event) => Effect.sync(() => { if (event.status === "started") requests.push(event); }), }), }).pipe( Effect.provide( Layer.mock(AcpRegistrySupport.AcpRegistryCatalog)({ resolve: () => Effect.succeed({ distribution: "local" as const, spawn: { command: process.execPath, args: [mockAgentPath], env: { T3_ACP_SESSION_LIFECYCLE: "1", ...agentEnvironment }, }, }), }), ), ); const modelSelection = { instanceId, model: "default" } as const; const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ runtimeMode: "full-access", interactionMode: "default", cwd: process.cwd(), }); const runtime = yield* adapter.openSession({ threadId, providerSessionId: ProviderSessionId.make("provider-session-acp-registry-mcp-transport"), modelSelection, runtimePolicy, }); const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy, }); yield* mcpSessions.set({ ...mcpSession, authorizationHeader: "Bearer rotated-token" }); yield* runtime.resumeThread({ providerThread: { ...providerThread, nativeThreadRef: { driver: ACP_REGISTRY_PROVIDER, nativeId: "mock-session-2", strength: "strong", }, }, }); const sent = (method: string) => { const payload = requests.find((event) => event.method === method)?.payload; return typeof payload === "object" && payload !== null ? Reflect.get(payload, "mcpServers") : undefined; }; return { new: sent("session/new"), load: sent("session/load") }; }); const plainHttp = (authorization: string) => [ { type: "http", name: "t3-code", url: endpoint, headers: [{ name: "Authorization", value: authorization }], }, ]; const http = (authorization: string) => [ { type: "http", name: "t3-code", url: endpoint, headers: [ { name: "Authorization", value: authorization }, { name: "t3-tool-exposure", value: "deferred" }, ], }, ]; const stdio = (authorization: string) => [ { name: "t3-code", command: selfInvocation.command, args: [...selfInvocationArgs(selfInvocation, ["acp-mcp-bridge"])], env: [ { name: "ELECTRON_RUN_AS_NODE", value: "1" }, { name: "T3_ACP_MCP_ENDPOINT", value: endpoint }, { name: "T3_ACP_MCP_AUTHORIZATION", value: authorization }, ], }, ]; assert.deepEqual( yield* sessionMcpServers({ agentId: "github-copilot-cli" }, { T3_ACP_MCP_HTTP: "1" }), { new: http("Bearer opened-token"), load: http("Bearer rotated-token") }, ); const acp = [{ type: "acp", name: "t3-code", serverId: "t3-code" }]; assert.deepEqual( yield* sessionMcpServers( { agentId: "github-copilot-cli" }, { T3_ACP_MCP_HTTP: "1", T3_ACP_MCP_ACP: "1" }, ), { new: acp, load: acp }, ); assert.deepEqual(yield* sessionMcpServers({ agentId: "github-copilot-cli" }), { new: stdio("Bearer opened-token"), load: stdio("Bearer rotated-token"), }); assert.deepEqual(yield* sessionMcpServers({ agentId: "gemini" }, { T3_ACP_MCP_HTTP: "1" }), { new: stdio("Bearer opened-token"), load: stdio("Bearer rotated-token"), }); // Command Code cannot start the stdio bridge on Windows: it spawns MCP commands through // cmd.exe, which splits the unquoted T3 executable path at the space. const commandCode = { source: "local", commandPath: "C:\\tools\\cmdc.exe", commandArgs: ["acp"], }; // Command Code is not Copilot: no deferred tool-exposure header. assert.deepEqual(yield* sessionMcpServers(commandCode, { T3_ACP_MCP_HTTP: "1" }), { new: plainHttp("Bearer opened-token"), load: plainHttp("Bearer rotated-token"), }); assert.deepEqual(yield* sessionMcpServers(commandCode), { new: stdio("Bearer opened-token"), load: stdio("Bearer rotated-token"), }); // Any other local agent keeps the stdio bridge. assert.deepEqual( yield* sessionMcpServers( { source: "local", commandPath: "C:\\tools\\node.exe", commandArgs: ["agent.mjs"] }, { T3_ACP_MCP_HTTP: "1" }, ), { new: stdio("Bearer opened-token"), load: stdio("Bearer rotated-token") }, ); }).pipe(Effect.provide(layerTest)), ); it.effect("the agent process env names the thread it serves", () => Effect.gen(function* () { const selfInvocation = yield* resolveSelfInvocation(); const mockAgentPath = yield* (yield* Path.Path).fromFileUrl( new URL("../../../scripts/acp-mock-agent.ts", import.meta.url), ); const endpoint = "http://127.0.0.1:43123/mcp"; const instanceId = ProviderInstanceId.make("acp-registry-thread-env"); const threadId = ThreadId.make("thread-acp-registry-thread-env"); yield* (yield* McpProviderSessions.McpProviderSessions).set({ environmentId: EnvironmentId.make("environment-acp-registry-thread-env"), threadId, providerSessionId: "mcp-session-acp-registry-thread-env", providerInstanceId: instanceId, endpoint, authorizationHeader: "Bearer thread-env-token", browserToolsAvailable: false, }); // Several agents can run from one directory at once; the spawn env is how // a process the agent starts (an MCP server, a shell tool) finds its own // T3 thread, the same way Claude and Codex sessions do. const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const spawnEnvironments: Array | undefined> = []; const capturingSpawner = ChildProcessSpawner.make((command) => Effect.sync(() => { if (command._tag === "StandardCommand") spawnEnvironments.push(command.options.env); }).pipe(Effect.andThen(spawner.spawn(command))), ); const withCapturedSpawns = (effect: Effect.Effect) => effect.pipe( Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, capturingSpawner), ); const adapter = yield* withCapturedSpawns( makeAcpRegistryAdapterV2({ selfInvocation, instanceId, settings: yield* decodeAcpRegistryAdapterSettings({ agentId: "claude-acp" }), environment: {}, }).pipe( Effect.provide( Layer.mock(AcpRegistrySupport.AcpRegistryCatalog)({ resolve: () => Effect.succeed({ distribution: "local" as const, spawn: { command: process.execPath, args: [mockAgentPath], env: { T3_ACP_SESSION_LIFECYCLE: "1" }, }, }), }), ), ), ); const modelSelection = { instanceId, model: "default" } as const; const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ runtimeMode: "full-access", interactionMode: "default", cwd: process.cwd(), }); const runtime = yield* withCapturedSpawns( adapter.openSession({ threadId, providerSessionId: ProviderSessionId.make("provider-session-acp-registry-thread-env"), modelSelection, runtimePolicy, }), ); yield* withCapturedSpawns(runtime.ensureThread({ threadId, modelSelection, runtimePolicy })); const agentEnvironment = spawnEnvironments.find( (environment) => environment?.T3_ACP_SESSION_LIFECYCLE === "1", ); assert.isDefined(agentEnvironment, "the agent itself must have been spawned"); assert.equal(agentEnvironment?.T3CODE_THREAD_ID, "thread-acp-registry-thread-env"); assert.equal(agentEnvironment?.T3_ACP_MCP_ENDPOINT, endpoint); assert.equal(agentEnvironment?.T3_ACP_MCP_AUTHORIZATION, "Bearer thread-env-token"); }).pipe(Effect.provide(layerTest)), ); it.effect.each(["before", "after"] as const)( "reconciles a full prompt echo %s acknowledgment", (ordering) => Effect.gen(function* () { const runtimeInstructions = { type: "text", text: buildRuntimeInstructions({ harness: "acpRegistry", model: "default" }), }; const promptBlocks = (ordinal: number) => [ { type: "text", text: ordinal === 1 ? t3AcpPromptWithInstructions({ prompt: "test prompt", state: { interactionMode: "default", hasT3Mcp: false, hasDeviceTools: false }, }) : "test prompt", }, ...(ordinal === 1 ? [runtimeInstructions] : []), ]; const echo = (messageId: string, content: ReadonlyArray) => claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "user_message", messageId, content, }); const promptText = (ordinal: number) => promptBlocks(ordinal) .map((block) => block.text) .join("\n"); const frames = (ordinal: number) => { const [request, , acknowledgment] = claudeFrames.prompt(ordinal); const matching = echo(`echo-${ordinal}`, promptBlocks(ordinal)); return [ request, echo("different-blocks", [ { type: "text", text: promptText(ordinal), annotations: { audience: ["user"] }, }, ]), ...(ordering === "before" ? [matching, acknowledgment] : [acknowledgment, matching]), claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "user_message_chunk", messageId: `echo-${ordinal}`, content: { type: "text", text: "updated echo" }, }), echo(`echo-${ordinal}`, [{ type: "text", text: "updated echo" }]), echo("second-id", promptBlocks(ordinal)), echo("unmatched", [{ type: "text", text: "Continue the review." }]), claudeFrames.idle("end_turn"), ]; }; const session = yield* openClaudeReplaySession( [ ...claudeFrames.open, ...frames(1), ...frames(2), claudeFrames.closeRequest, claudeFrames.closed, ], undefined, false, ); for (const ordinal of [1, 2]) { yield* session.runtime.startTurn(session.turnInput(ordinal)); const events = yield* session.until((event) => event.type === "turn.terminal"); const messages = events.flatMap((event) => event.type === "message.updated" && event.message.role === "user" ? [event.message] : [], ); const items = events.flatMap((event) => event.type === "turn_item.updated" && event.turnItem.type === "user_message" ? [event.turnItem] : [], ); const expected = new Set([promptText(ordinal), "Continue the review."]); assert.deepEqual(new Set(messages.map((message) => message.text)), expected); assert.equal(new Set(messages.map((message) => message.id)).size, 3); assert.equal(new Set(items.map((item) => item.id)).size, 3); for (const id of ["different-blocks", "second-id", "unmatched"]) { assert.isTrue( items.some((item) => item.nativeItemRef?.nativeId?.endsWith(`:user:message:${id}`)), `${id} remains visible`, ); } } }).pipe(Effect.provide(layerTest)), ); it.effect("settles Claude's accepted foreground reply while retaining child output", () => Effect.gen(function* () { const gate = makeProviderReplayGate(["accepted", "child-output", "wake"]); const { update, idle, prompt } = claudeFrames; const session = yield* openClaudeReplaySession( [ ...claudeFrames.open, prompt(1)[0], idle("end_turn"), claudeMarker("before acceptance"), { ...prompt(1)[1], label: "accepted" }, ...prompt(1).slice(2), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "agent-1", name: "Agent", title: "Review", kind: "think", status: "pending", rawInput: { description: "Review" }, }), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "malformed-agent", name: "Agent", title: "Incomplete agent", kind: "think", status: "pending", rawInput: { description: "Never receives a prompt" }, }), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "failed-task", name: "Task", title: "Malformed task", kind: "think", status: "failed", rawInput: { description: "Missing prompt" }, }), ...claudeChild.slice(0, 2), update("child-1", { sessionUpdate: "agent_message_chunk", messageId: "early-reply", content: { type: "text", text: "First review result." }, }), claudeFrames.childState(CLAUDE_ROOT_SESSION, "child-1", "idle"), ...claudeChild.slice(2), update("child-1", { sessionUpdate: "state_update", state: "running" }), ...claudeUnrelatedChildren, update(CLAUDE_ROOT_SESSION, { sessionUpdate: "agent_message_chunk", messageId: "reply", content: { type: "text", text: "The review is running." }, }), idle("end_turn"), { ...update("child-1", { sessionUpdate: "agent_message_chunk", messageId: "child-reply", content: { type: "text", text: "Found the regression." }, }), label: "child-output", }, update("child-1", { sessionUpdate: "usage_update", used: 2_500, size: 10_000, cost: { amount: 0.42, currency: "USD" }, }), update("child-1", { sessionUpdate: "session_info_update", title: "Child review", updatedAt: "2026-10-19T00:00:00Z", }), update("child-1", { sessionUpdate: "state_update", state: "idle", stopReason: "end_turn", usage: { totalTokens: 3_000, inputTokens: 2_500, outputTokens: 500, thoughtTokens: 0 }, }), claudeMarker("child output"), { ...update(CLAUDE_ROOT_SESSION, { sessionUpdate: "user_message", messageId: "wake-user", content: [{ type: "text", text: "Continue the review." }], }), label: "wake", }, idle("end_turn"), claudeMarker("continuation idle"), claudeFrames.closeRequest, claudeFrames.closed, ], gate, ); yield* session.runtime.startTurn(session.turnInput(1)); const beforeAcceptance = yield* session.throughMarker("before acceptance"); assert.isFalse(beforeAcceptance.some((event) => event.type === "turn.terminal")); gate.release("accepted"); const foreground = yield* session.until((event) => event.type === "turn.terminal"); const foregroundTerminal = foreground.at(-1)!; if (foregroundTerminal.type !== "turn.terminal") throw new Error("Missing foreground terminal"); assert.equal(foregroundTerminal.status, "completed"); const earlyResult = foreground.findLast( (event) => event.type === "subagent.updated" && event.subagent.status === "completed", ); assert.isDefined(earlyResult, "a child can finish before its spawn acknowledgement"); if (earlyResult?.type !== "subagent.updated") throw new Error("Missing early result"); assert.equal(earlyResult.subagent.result, "First review result."); const earlyItem = foreground.findLast( (event) => event.type === "turn_item.updated" && event.turnItem.type === "subagent" && event.turnItem.status === "completed", ); if (earlyItem?.type !== "turn_item.updated" || earlyItem.turnItem.type !== "subagent") throw new Error("Missing early parent item"); assert.equal(earlyItem.turnItem.result, "First review result."); assert.isTrue(yield* session.runtime.hasPendingBackgroundWork!); gate.release("child-output"); const childOutput = yield* session.throughMarker("child output"); const message = childOutput.find( (event) => event.type === "message.updated" && event.message.text === "Found the regression.", ); assert.isDefined(message, "child output survives foreground completion"); if (message?.type !== "message.updated") throw new Error("Missing child output"); assert.notEqual(message.message.threadId, session.turnInput(1).threadId); const childThreads = childOutput.flatMap((event) => event.type === "provider_thread.updated" && event.providerThread.appThreadId === message.message.threadId ? [event.providerThread] : [], ); assert.deepEqual( childThreads.map((thread) => thread.contextUsage), [ { usedTokens: 2_500, maxTokens: 10_000, cost: { amount: 0.42, currency: "USD" } }, { usedTokens: 2_500, maxTokens: 10_000, cost: { amount: 0.42, currency: "USD" } }, ], ); assert.deepEqual(childThreads.at(-1)?.nativeMetadata, { title: "Child review", updatedAt: "2026-10-19T00:00:00Z", }); assert.isFalse(childOutput.some((event) => event.type === "turn.terminal")); assert.isTrue( childOutput.some( (event) => event.type === "subagent.updated" && event.subagent.status === "completed", ), ); const latestChild = childOutput.findLast((event) => event.type === "subagent.updated"); if (latestChild?.type !== "subagent.updated") throw new Error("Missing current child result"); assert.equal(latestChild.subagent.result, "Found the regression."); const rootItems = new Map( [...foreground, ...childOutput].flatMap((event) => event.type === "turn_item.updated" && event.turnItem.threadId === session.turnInput(1).threadId ? [[event.turnItem.id, event.turnItem] as const] : [], ), ); const generic = [...rootItems.values()].filter((item) => item.type === "dynamic_tool"); assert.isFalse( generic.some((item) => item.nativeItemRef?.nativeId === "agent-1"), "native subagents never project a duplicate generic tool", ); const malformed = generic.find((item) => item.nativeItemRef?.nativeId === "malformed-agent"); assert.isDefined(malformed, "unclassified tools still project on foreground completion"); assert.equal(malformed?.status, "completed"); const failed = generic.find((item) => item.nativeItemRef?.nativeId === "failed-task"); assert.isDefined(failed, "unclassified terminal tools retain ordinary failures"); assert.equal(failed?.status, "failed"); assert.isFalse( [...rootItems.values()].some((item) => item.type === "user_message"), "the submitted root prompt is already owned by the app", ); assert.deepEqual(pendingBackgroundTurnItems({ turnItems: [...rootItems.values()] }), []); assert.isFalse(yield* session.runtime.hasPendingBackgroundWork!); gate.release("wake"); yield* Queue.take(session.offered); yield* session.throughMarker("continuation idle"); const offer = session.offers[0]!; yield* offer.dispatchIfCurrent!(Effect.void); yield* session.runtime.startTurn(session.turnInput(2, { continuation: true })); const continuation = yield* session.until((event) => event.type === "turn.terminal"); const terminal = continuation.at(-1)!; if (terminal.type !== "turn.terminal") throw new Error("Missing continuation terminal"); assert.equal( terminal.status, "completed", "buffered acceptance preserves the latest live idle", ); assert.isFalse( continuation.some( (event) => event.type === "turn_item.updated" && event.turnItem.type === "user_message", ), "autonomous root echoes only signal acceptance", ); }).pipe(Effect.provide(layerTest)), ); // Copilot holds session/prompt while an attached async shell runs (github/copilot-cli#5293); // its documented foreground-done signal is the opt-in assistant.idle session event. it.effect("settles Copilot's reply on assistant.idle while an async shell holds the prompt", () => Effect.gen(function* () { const sessionId = "copilot-session"; const sessionEvent = (type: string) => ({ type: "emit_inbound", frame: { kind: "notification", method: "github.com/copilot/sessionEvent", params: { sessionId, type, data: {} }, }, }); const update = (sessionUpdate: Record) => claudeFrames.update(sessionId, sessionUpdate); const session = yield* openClaudeReplaySession( [ { type: "expect_outbound", frame: { kind: "request", method: "initialize", params: "" }, }, { type: "emit_inbound", frame: { kind: "response", method: "initialize", result: { protocolVersion: 1, agentInfo: { name: "Copilot", version: "1.0.110" }, agentCapabilities: { loadSession: true }, authMethods: [], }, }, }, { type: "expect_outbound", frame: { kind: "request", method: "session/new", params: "" }, }, { type: "emit_inbound", frame: { kind: "response", method: "session/new", result: { sessionId } }, }, { type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: "" }, }, // A previous prompt's idle can land before this prompt's turn starts. sessionEvent("assistant.idle"), update({ sessionUpdate: "session_info_update", title: "after stale idle" }), sessionEvent("assistant.turn_start"), update({ sessionUpdate: "tool_call", toolCallId: "shell-1", title: "sleep 120", kind: "execute", status: "completed", rawInput: { command: "sleep 120", mode: "async", detach: false }, }), update({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "ok" } }), sessionEvent("assistant.idle"), update({ sessionUpdate: "session_info_update", title: "after foreground idle" }), // The shell never exits, so the first session/prompt is never answered. { type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: "" }, }, sessionEvent("assistant.turn_start"), update({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "two" } }), sessionEvent("assistant.idle"), update({ sessionUpdate: "session_info_update", title: "after second idle" }), ], undefined, false, "github-copilot-cli", ); yield* session.runtime.startTurn(session.turnInput(1)); const stale = yield* session.throughMarker("after stale idle"); assert.isFalse(stale.some((event) => event.type === "turn.terminal")); const foreground = yield* session.throughMarker("after foreground idle"); const terminal = foreground.find((event) => event.type === "turn.terminal"); assert.isDefined(terminal, "the run settles once Copilot's foreground work is idle"); assert.equal(terminal?.type === "turn.terminal" ? terminal.status : null, "completed"); yield* session.runtime.startTurn(session.turnInput(2)); const second = yield* session.throughMarker("after second idle"); const secondTerminal = second.find((event) => event.type === "turn.terminal"); assert.equal(secondTerminal?.type === "turn.terminal" ? secondTerminal.runOrdinal : null, 2); }).pipe(Effect.provide(layerTest)), ); // Copilot wakes its root agent when a background subagent finishes, after the run settled. it.effect("does not drop Copilot's root reply when a finished background agent wakes it", () => Effect.gen(function* () { const sessionId = "copilot-session"; const sessionEvent = (type: string) => ({ type: "emit_inbound", frame: { kind: "notification", method: "github.com/copilot/sessionEvent", params: { sessionId, type, data: {} }, }, }); const update = (sessionUpdate: Record) => claudeFrames.update(sessionId, sessionUpdate); const session = yield* openClaudeReplaySession( [ { type: "expect_outbound", frame: { kind: "request", method: "initialize", params: "" }, }, { type: "emit_inbound", frame: { kind: "response", method: "initialize", result: { protocolVersion: 1, agentInfo: { name: "Copilot", version: "1.0.112" }, agentCapabilities: { loadSession: true }, authMethods: [], }, }, }, { type: "expect_outbound", frame: { kind: "request", method: "session/new", params: "" }, }, { type: "emit_inbound", frame: { kind: "response", method: "session/new", result: { sessionId } }, }, { type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: "" }, }, sessionEvent("assistant.turn_start"), update({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Agents are still working." }, }), sessionEvent("assistant.idle"), update({ sessionUpdate: "session_info_update", title: "after foreground idle" }), // The background agent finishes; Copilot reads it and starts a new root turn. sessionEvent("assistant.turn_start"), update({ sessionUpdate: "tool_call", toolCallId: "read-agent-1", title: "read_agent", kind: "other", status: "completed", rawInput: { agent_id: "agent-1" }, }), update({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Agent 1 finished" }, }), sessionEvent("assistant.idle"), { type: "emit_inbound", frame: { kind: "response", method: "session/prompt", result: { stopReason: "end_turn" }, }, }, update({ sessionUpdate: "session_info_update", title: "after wake idle" }), ], undefined, true, "github-copilot-cli", ); yield* session.runtime.startTurn(session.turnInput(1)); const foreground = yield* session.throughMarker("after foreground idle"); assert.isDefined(foreground.find((event) => event.type === "turn.terminal")); const wake = yield* session.throughMarker("after wake idle"); assert.isTrue( session.offers.length > 0 || JSON.stringify(wake).includes("Agent 1 finished"), "the woken root reply is shown or opens a continuation run", ); yield* session.runtime.startTurn(session.turnInput(2, { continuation: true })); const continuation = yield* session.until((event) => event.type === "turn.terminal"); assert.include(JSON.stringify(continuation), "Agent 1 finished"); assert.include(JSON.stringify(continuation.at(-1)), '"status":"completed"'); }).pipe(Effect.provide(layerTest)), ); // regression: #38599 Hermes replies on its own when a background process it started exits it.effect("does not drop Hermes' reply after a background process finishes", () => Effect.gen(function* () { const gate = makeProviderReplayGate(["process-exit"]); const sessionId = "hermes-session"; const update = (sessionUpdate: Record) => claudeFrames.update(sessionId, sessionUpdate); const session = yield* openClaudeReplaySession( [ { type: "expect_outbound", frame: { kind: "request", method: "initialize", params: "" }, }, { type: "emit_inbound", frame: { kind: "response", method: "initialize", result: { protocolVersion: 1, agentInfo: { name: "hermes-agent", version: "0.23.1" }, agentCapabilities: { loadSession: true }, authMethods: [], }, }, }, { type: "expect_outbound", frame: { kind: "request", method: "session/new", params: "" }, }, { type: "emit_inbound", frame: { kind: "response", method: "session/new", result: { sessionId } }, }, { type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: "" }, }, update({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Started it in the background." }, }), { type: "emit_inbound", frame: { kind: "response", method: "session/prompt", result: { stopReason: "end_turn" }, }, }, { ...update({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "The background command printed DONE" }, }), label: "process-exit", }, update({ sessionUpdate: "session_info_update", title: "after completion reply" }), ], gate, true, { source: "local", commandPath: "hermes", commandArgs: ["acp"] }, ); yield* session.runtime.startTurn(session.turnInput(1)); const foreground = yield* session.until((event) => event.type === "turn.terminal"); assert.include(JSON.stringify(foreground.at(-1)), '"status":"completed"'); gate.release("process-exit"); yield* Queue.take(session.offered); yield* session.throughMarker("after completion reply"); yield* session.offers[0]!.dispatchIfCurrent!(Effect.void); yield* session.runtime.startTurn(session.turnInput(2, { continuation: true })); yield* TestClock.adjust("10 seconds"); const continuation = yield* session.until((event) => event.type === "turn.terminal"); assert.include(JSON.stringify(continuation), "printed DONE"); assert.include(JSON.stringify(continuation.at(-1)), '"status":"completed"'); }).pipe(Effect.provide(layerTest)), ); // Copilot flattens MCP titles and reports kind "read" for MCP calls and glob ("other" for rg); // its native tool.execution_start event carries the real identity. it.effect.each(["glob", "rg"])("keeps Copilot's MCP and %s identity", (toolName) => Effect.gen(function* () { const sessionId = "copilot-session"; const executionStart = (data: Record) => ({ type: "emit_inbound", frame: { kind: "notification", method: "github.com/copilot/sessionEvent", params: { sessionId, type: "tool.execution_start", data }, }, }); const update = (sessionUpdate: Record) => claudeFrames.update(sessionId, sessionUpdate); const session = yield* openClaudeReplaySession( [ { type: "expect_outbound", frame: { kind: "request", method: "initialize", params: "" }, }, { type: "emit_inbound", frame: { kind: "response", method: "initialize", result: { protocolVersion: 1, agentInfo: { name: "Copilot", version: "1.0.112" }, agentCapabilities: { loadSession: true }, authMethods: [], }, }, }, { type: "expect_outbound", frame: { kind: "request", method: "session/new", params: "" }, }, { type: "emit_inbound", frame: { kind: "response", method: "session/new", result: { sessionId } }, }, { type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: "" }, }, executionStart({ toolCallId: "mcp-call", mcpServerName: "demo-library", mcpToolName: "search_books", }), update({ sessionUpdate: "tool_call", toolCallId: "mcp-call", title: "demo-library-search_books", kind: "read", status: "completed", rawInput: { arguments: { query: "paper airplanes" }, limit: 2 }, }), executionStart({ toolCallId: "native-search", toolName, toolTitle: "Finding files", arguments: { pattern: "*.ts", paths: ["/workspace"] }, }), update({ sessionUpdate: "tool_call", toolCallId: "native-search", title: "Finding files matching *.ts", kind: toolName === "rg" ? "other" : "read", status: "completed", rawInput: { pattern: "*.ts", paths: ["/workspace"] }, locations: [{ path: "/workspace" }], }), update({ sessionUpdate: "session_info_update", title: "after tools" }), ], undefined, false, "github-copilot-cli", ); yield* session.runtime.startTurn(session.turnInput(1)); const events = yield* session.throughMarker("after tools"); const lastItem = (nativeId: string) => events .flatMap((event) => event.type === "turn_item.updated" && event.turnItem.nativeItemRef?.nativeId === nativeId ? [event.turnItem] : [], ) .at(-1); const mcp = lastItem("mcp-call"); const search = lastItem("native-search"); assert.deepEqual( { mcp: mcp?.type === "dynamic_tool" ? mcp.toolName : mcp?.type, search: search?.type === "dynamic_tool" ? search.toolName : search?.type, }, { mcp: "demo-library.search_books", search: "file_search" }, ); }).pipe(Effect.provide(layerTest)), ); it.effect.each([ { kind: "approval", stopReason: "end_turn", status: "completed" }, { kind: "question", stopReason: "error", status: "failed" }, ] as const)("continues Claude's background $kind and preserves $status idle", (testCase) => Effect.gen(function* () { const gate = makeProviderReplayGate(["wake", "stop-request", "refused-response"]); const method = testCase.kind === "approval" ? "session/request_permission" : "elicitation/create"; const params = testCase.kind === "approval" ? { sessionId: "child-1", title: "Run tests?", subject: { type: "tool_call", toolCall: { toolCallId: "test-command", name: "Bash", title: "Run tests", kind: "execute", status: "pending", rawInput: { command: "vp test run" }, }, }, options: [{ optionId: "allow", name: "Allow", kind: "allow_once" }], } : { sessionId: "child-1", mode: "form", message: "Which file?", requestedSchema: { type: "object", properties: { file: { type: "string", title: "File", enum: ["a.ts", "b.ts"] } }, required: ["file"], }, }; const session = yield* openClaudeReplaySession( [ ...claudeFrames.open, ...claudeFrames.prompt(1), ...claudeChild, claudeFrames.idle("end_turn"), { ...claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "state_update", state: "running", }), label: "wake", }, claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "user_message", messageId: "wake-user", content: [{ type: "text", text: "Continue the review." }], }), claudeFrames.closeRequest, { type: "emit_inbound", frame: { kind: "request", method, params } }, { type: "emit_inbound", label: "refused-response", frame: { kind: "response", method: "session/close", error: { code: -32603, message: "Close refused" }, }, }, { type: "expect_outbound", frame: { kind: "response", method, result: testCase.kind === "approval" ? { outcome: { outcome: "selected", optionId: "allow" } } : { action: "accept", content: { file: "a.ts" } }, }, }, claudeFrames.childState(CLAUDE_ROOT_SESSION, "child-1", "idle"), ...claudeUnrelatedChildren, claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "state_update", state: "idle", stopReason: testCase.stopReason, ...(testCase.status === "failed" ? { error: { code: -32603, message: "API Error: 529 overloaded" } } : {}), }), { type: "emit_inbound", label: "stop-request", frame: { kind: "request", method, params }, }, claudeFrames.closeRequest, claudeFrames.closed, { type: "expect_outbound", frame: { kind: "response", method, result: testCase.kind === "approval" ? { outcome: { outcome: "cancelled" } } : { action: "cancel" }, }, }, claudeMarker("detached request cancelled"), ], gate, ); yield* session.runtime.startTurn(session.turnInput(1)); assert.equal((yield* session.terminal).status, "completed"); assert.isTrue(yield* session.runtime.hasPendingBackgroundWork!); gate.release("wake"); yield* Queue.take(session.offered); const offer = session.offers[0]!; assert.isDefined(offer.dispatchIfCurrent); yield* offer.dispatchIfCurrent!(Effect.void); yield* session.runtime.startTurn(session.turnInput(2, { continuation: true })); const started = yield* session.until( (event) => event.type === "provider_turn.updated" && event.providerTurn.status === "running", ); const running = started.at(-1)!; if (running.type !== "provider_turn.updated") throw new Error("Missing active continuation"); const closing = yield* session.runtime .interruptTurn({ providerThread: session.providerThread, providerTurnId: running.providerTurn.id, requestRuntimeRestart: true, }) .pipe(Effect.exit, Effect.forkScoped); yield* Effect.addFinalizer(() => Effect.sync(() => gate.releaseAll())); const seen = yield* session.until( (event) => event.type === "runtime_request.updated" && event.runtimeRequest.status === "pending", ); assert.isFalse( seen.some( (event) => event.type === "turn_item.updated" && event.turnItem.type === "user_message", ), ); const request = seen.at(-1)!; if (request.type !== "runtime_request.updated") throw new Error("Missing request"); assert.isTrue( seen.some( (event) => event.type === "node.updated" && event.node.id === request.runtimeRequest.nodeId && event.node.runId === session.turnInput(2).runId, ), ); yield* Effect.promise(() => gate.waitForReached("refused-response")); gate.release("refused-response"); assert.isTrue(Exit.isFailure(yield* Fiber.join(closing))); assert.isTrue(yield* session.runtime.hasPendingBackgroundWork!); yield* session.runtime.respondToRuntimeRequest({ requestId: request.runtimeRequest.id, ...(testCase.kind === "approval" ? { decision: "accept" as const } : { answers: { file: "a.ts" } }), }); const terminal = yield* session.terminal; assert.equal(terminal.status, testCase.status); if (terminal.status === "failed") { assert.include(terminal.failure?.message ?? "", "529 overloaded"); assert.isUndefined(terminal.childTurnsContinue); assert.isFalse(yield* session.runtime.hasPendingBackgroundWork!); } gate.release("stop-request"); yield* Queue.take(session.offered); yield* session.runtime.interruptTurn({ providerThread: session.providerThread, providerTurnId: terminal.providerTurnId, requestRuntimeRestart: true, }); const cancelled = yield* session.throughMarker("detached request cancelled"); assert.isFalse( cancelled.some( (event) => event.type === "runtime_request.updated" && event.runtimeRequest.status === "pending", ), "detached requests are cancelled before turn admission", ); }).pipe(Effect.provide(layerTest)), ); it.effect("retains streamed child tool output through the generic Devin route", () => Effect.gen(function* () { const { update } = claudeFrames; const session = yield* openClaudeReplaySession( [ ...claudeFrames.open, ...claudeFrames.prompt(1), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "early-tool", status: "in_progress", rawInput: { command: "echo early" }, }), update("child-1", { sessionUpdate: "tool_call_content_chunk", toolCallId: "early-tool", content: { type: "content", content: { type: "text", text: "early" } }, }), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "early-tool", status: "completed", }), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "spawn", status: "in_progress", _meta: { "cognition.ai/subagent_started": { agentId: "child-1", task: "Review" } }, }), ...[CLAUDE_ROOT_SESSION, "child-1"].map((sessionId) => update(sessionId, { sessionUpdate: "tool_call_update", toolCallId: "shared-tool", title: "Read package", kind: "execute", status: "in_progress", rawInput: { command: "cat package.json" }, ...(sessionId === "child-1" ? { content: [{ type: "content", content: { type: "text", text: "one" } }] } : {}), }), ), update("child-1", { sessionUpdate: "tool_call_content_chunk", toolCallId: "shared-tool", content: { type: "content", content: { type: "text", text: "two" } }, }), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "shared-tool", status: "completed", }), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "shared-tool", content: [{ type: "content", content: { type: "text", text: "replacement" } }], }), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "shared-tool", content: [], }), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "shared-tool", rawOutput: { stdout: "structured", stderr: "" }, }), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "shared-tool", content: [{ type: "content", content: { type: "text", text: "display" } }], }), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "shared-tool", rawOutput: { type: "Text", text: "vendor" }, }), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "shared-tool", content: [{ type: "content", content: { type: "text", text: "display" } }], }), claudeMarker("tools complete"), claudeFrames.idle("end_turn"), claudeFrames.closeRequest, claudeFrames.closed, ], undefined, false, "devin", ); yield* session.runtime.startTurn(session.turnInput(1)); const events = yield* session.throughMarker("tools complete"); const child = events.find((event) => event.type === "subagent.updated"); if (child?.type !== "subagent.updated") throw new Error("Missing child"); const tools = events.flatMap((event) => event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool" ? [event.turnItem] : [], ); const childTools = tools.filter((item) => item.threadId === child.subagent.childThreadId); assert.deepEqual( childTools.map((item) => item.output), [ [{ type: "content", content: { type: "text", text: "one" } }], [{ type: "content", content: { type: "text", text: "onetwo" } }], [{ type: "content", content: { type: "text", text: "onetwo" } }], [{ type: "content", content: { type: "text", text: "replacement" } }], [], { stdout: "structured", stderr: "" }, { stdout: "structured", stderr: "" }, { type: "Text", text: "vendor" }, { type: "Text", text: "vendor" }, ], ); assert.equal(new Set(childTools.map((item) => item.id)).size, 1); assert.equal(childTools.at(-1)?.nativeItemRef?.nativeId, "child-1:tool:shared-tool"); assert.equal(childTools.at(-1)?.status, "completed"); assert.deepEqual(childTools.at(-1)?.input, { command: "cat package.json" }); const parentTools = events.flatMap((event) => event.type === "turn_item.updated" && event.turnItem.type === "command_execution" && event.turnItem.threadId === session.turnInput(1).threadId && event.turnItem.nativeItemRef?.nativeId === "shared-tool" ? [event.turnItem] : [], ); assert.lengthOf(parentTools, 1); assert.isUndefined(parentTools[0]?.output); assert.isFalse( events.some( (event) => event.type === "message.updated" && event.message.text.includes("onetwo"), ), ); }).pipe(Effect.provide(layerTest)), ); it.effect("replaces streamed root tool output with a later content list", () => Effect.gen(function* () { const { update } = claudeFrames; const text = (value: string) => [{ type: "content", content: { type: "text", text: value } }]; const session = yield* openClaudeReplaySession([ ...claudeFrames.open, ...claudeFrames.prompt(1), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "root-tool", title: "Stream", status: "in_progress", content: text("one"), }), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_content_chunk", toolCallId: "root-tool", content: { type: "content", content: { type: "text", text: "two" } }, }), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "root-tool", status: "completed", content: text("replacement"), }), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_content_chunk", toolCallId: "root-tool", content: { type: "content", content: { type: "text", text: "-more" } }, }), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_content_chunk", toolCallId: "root-tool", content: { type: "diff", changes: [{ operation: "add", path: "/a.txt" }], patch: { format: "git_patch", text: "PATCH" }, }, }), claudeMarker("tools complete"), claudeFrames.idle("end_turn"), claudeFrames.closeRequest, claudeFrames.closed, ]); yield* session.runtime.startTurn(session.turnInput(1)); const events = yield* session.throughMarker("tools complete"); const outputs = events.flatMap((event) => event.type === "turn_item.updated" && event.turnItem.nativeItemRef?.nativeId === "root-tool" && "output" in event.turnItem ? [event.turnItem.output] : [], ); assert.deepEqual(outputs, [ text("one"), text("onetwo"), text("replacement"), text("replacement-more"), { type: "Text", text: "replacement-morePATCH" }, ]); }).pipe(Effect.provide(layerTest)), ); it.effect("approves Devin's plan without choosing a bypass option it lists first", () => Effect.gen(function* () { const session = yield* openClaudeReplaySession( [ ...claudeFrames.open.map((entry) => entry.frame.method === "initialize" && entry.type === "emit_inbound" ? { ...entry, frame: { ...entry.frame, result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] }, }, } : entry, ), { type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: "" }, }, claudeFrames.update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call", toolCallId: "exit-plan", title: "Exit plan mode", kind: "switch_mode", status: "pending", }), { type: "emit_inbound", frame: { kind: "request", method: "session/request_permission", params: { sessionId: CLAUDE_ROOT_SESSION, toolCall: { toolCallId: "exit-plan" }, // Devin gives both plan approvals allow_once and does not promise their order. options: [ { optionId: "plan_bypass", name: "Yes, implement plan and bypass permissions", kind: "allow_once", }, { optionId: "plan_accept_edits", name: "Yes, implement plan and accept edits", kind: "allow_once", }, { optionId: "reject_once", name: "No, plan needs changes", kind: "reject_once" }, ], }, }, }, { type: "expect_outbound", frame: { kind: "response", method: "session/request_permission", result: { outcome: { outcome: "selected", optionId: "" } }, }, }, { type: "emit_inbound", frame: { kind: "response", method: "session/prompt", result: { stopReason: "end_turn" }, }, }, claudeFrames.closeRequest, claudeFrames.closed, ], undefined, false, "devin", ); yield* session.runtime.startTurn(session.turnInput(1)).pipe(Effect.forkScoped); const seen = yield* session.until( (event) => event.type === "runtime_request.updated" && event.runtimeRequest.status === "pending", ); const request = seen.at(-1)!; if (request.type !== "runtime_request.updated") throw new Error("Missing request"); yield* session.runtime.respondToRuntimeRequest({ requestId: request.runtimeRequest.id, decision: "accept", }); let answer: unknown; while (answer === undefined) { const frame = (yield* Queue.take(session.outgoing)) as { readonly result?: { readonly outcome?: { readonly optionId?: string } }; }; answer = frame.result?.outcome; } assert.deepEqual(answer, { outcome: "selected", optionId: "plan_accept_edits" }); assert.equal((yield* session.terminal).status, "completed"); }).pipe(Effect.provide(layerTest), Effect.scoped), ); it.effect("keeps stopped child tools interrupted when a late output chunk arrives", () => Effect.gen(function* () { const { update } = claudeFrames; const session = yield* openClaudeReplaySession( [ ...claudeFrames.open.map((entry) => entry.frame.method === "initialize" && entry.type === "emit_inbound" ? { ...entry, frame: { ...entry.frame, result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] }, }, } : entry, ), { type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: "" }, }, update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "spawn", status: "in_progress", _meta: { "cognition.ai/subagent_started": { agentId: "child-1", task: "Review" } }, }), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "stopped-tool", status: "in_progress", }), claudeMarker("before cancel"), { type: "expect_outbound", frame: { kind: "notification", method: "session/cancel", params: { sessionId: CLAUDE_ROOT_SESSION }, }, }, update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "stopped-tool", status: "failed", }), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "stopped-tool", content: [{ type: "content", content: { type: "text", text: "stopping" } }], }), update("child-1", { sessionUpdate: "tool_call_content_chunk", toolCallId: "stopped-tool", content: { type: "content", content: { type: "text", text: "cancelled" } }, }), claudeMarker("late output"), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "spawn", status: "failed", _meta: { "cognition.ai/subagent_completed": { agentId: "child-1", success: false } }, }), { type: "emit_inbound", frame: { kind: "response", method: "session/prompt", result: { stopReason: "cancelled" }, }, }, claudeFrames.closeRequest, claudeFrames.closed, ], undefined, false, "devin", ); const starting = yield* session.runtime .startTurn(session.turnInput(1)) .pipe(Effect.forkScoped); const started = yield* session.throughMarker("before cancel"); const running = started.findLast((event) => event.type === "provider_turn.updated"); if (running?.type !== "provider_turn.updated") throw new Error("Missing active turn"); const closing = yield* session.runtime .interruptTurn({ providerThread: session.providerThread, providerTurnId: running.providerTurn.id, }) .pipe(Effect.forkScoped); const events = yield* session.throughMarker("late output"); yield* Fiber.join(closing); yield* Fiber.join(starting); const tools = events.flatMap((event) => event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool" ? [event.turnItem] : [], ); assert.deepEqual( tools.map((item) => item.status), ["interrupted", "interrupted", "interrupted"], ); assert.deepEqual(tools.at(-1)?.output, [ { type: "content", content: { type: "text", text: "stoppingcancelled" } }, ]); }).pipe(Effect.provide(layerTest)), ); it.effect("keeps native child tools interrupted after idle and late output", () => Effect.gen(function* () { const { update, childState } = claudeFrames; const session = yield* openClaudeReplaySession([ ...claudeFrames.open, ...claudeFrames.prompt(1), ...claudeChild.slice(0, -1), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "unfinished", status: "in_progress", }), childState(CLAUDE_ROOT_SESSION, "child-1", "idle"), update("child-1", { sessionUpdate: "tool_call_content_chunk", toolCallId: "unfinished", content: { type: "content", content: { type: "text", text: "late" } }, }), claudeMarker("late native output"), claudeFrames.idle("end_turn"), claudeFrames.closeRequest, claudeFrames.closed, ]); yield* session.runtime.startTurn(session.turnInput(1)); const events = yield* session.throughMarker("late native output"); const tools = events.flatMap((event) => event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool" ? [event.turnItem] : [], ); assert.deepEqual( tools.map((tool) => tool.status), ["running", "interrupted", "interrupted"], ); assert.deepEqual(tools.at(-1)?.output, [ { type: "content", content: { type: "text", text: "late" } }, ]); }).pipe(Effect.provide(layerTest)), ); it.effect.each(["error", "end_turn"])( "hands live Claude child tools to the next run after %s and keeps nested output in the child", (stopReason) => Effect.gen(function* () { const { update, childState, idle, prompt } = claudeFrames; const gate = makeProviderReplayGate(["nested-output", "nested-idle"]); const session = yield* openClaudeReplaySession( [ ...claudeFrames.open, ...prompt(1), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "agent-1", name: "Agent", title: "Review", kind: "think", status: "in_progress", rawInput: { prompt: "Review the code", description: "Review" }, }), ...claudeChild.slice(1, -1), update("child-1", { sessionUpdate: "tool_call_update", toolCallId: "child-tool", name: "Bash", title: "Read package", kind: "execute", status: "in_progress", rawInput: { command: "cat package.json" }, }), update("child-1", { sessionUpdate: "agent_message_chunk", messageId: "progress", content: { type: "text", text: "Review in progress." }, }), idle(stopReason), ...prompt(2), childState(CLAUDE_ROOT_SESSION, "child-1", "running"), update("child-1", { sessionUpdate: "session_message", messageId: "resume-child", senderSessionId: CLAUDE_ROOT_SESSION, recipientSessionId: "child-1", content: [{ type: "text", text: "Continue the review." }], }), claudeMarker("resumed"), childState("child-1", "grandchild", "running"), childState(CLAUDE_ROOT_SESSION, "child-1", "idle"), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "agent-1", name: "Agent", status: "completed", rawInput: { prompt: "Review the code", description: "Review" }, }), idle("end_turn"), { ...update("grandchild", { sessionUpdate: "terminal_update", terminalId: "nested-terminal", command: "/usr/bin/node /srv/t3/bin.ts acp-mcp-call orchestrator_capabilities {}", }), label: "nested-output", }, update("grandchild", { sessionUpdate: "terminal_output_chunk", terminalId: "nested-terminal", data: Buffer.from("{}\n").toString("base64"), }), update("grandchild", { sessionUpdate: "terminal_update", terminalId: "nested-terminal", exitStatus: { exitCode: 0 }, }), update("grandchild", { sessionUpdate: "tool_call_update", toolCallId: "terminal-tool", name: "Bash", title: "Run nested command", kind: "execute", status: "in_progress", content: [{ type: "terminal", terminalId: "nested-terminal" }], }), update("grandchild", { sessionUpdate: "tool_call_update", toolCallId: "nested-tool", name: "Bash", title: "Read nested package", kind: "execute", status: "in_progress", rawInput: { command: "cat nested/package.json" }, }), ...["one", "two"].map((text) => update("grandchild", { sessionUpdate: "tool_call_content_chunk", toolCallId: "nested-tool", content: { type: "content", content: { type: "text", text } }, }), ), update("grandchild", { sessionUpdate: "tool_call_update", toolCallId: "nested-tool", status: "completed", }), update("grandchild", { sessionUpdate: "agent_message_chunk", messageId: "nested-reply", content: { type: "text", text: "Nested output" }, }), update(CLAUDE_ROOT_SESSION, { sessionUpdate: "user_message", messageId: "nested-wake", content: [{ type: "text", text: "Continue the review." }], }), idle("end_turn"), claudeMarker("nested"), { ...childState("child-1", "grandchild", "idle"), label: "nested-idle", }, claudeMarker("nested idle"), ...prompt(4), update("child-1", { sessionUpdate: "session_message", messageId: "second-review", senderSessionId: CLAUDE_ROOT_SESSION, recipientSessionId: "child-1", content: [{ type: "text", text: "Review the fix." }], }), childState(CLAUDE_ROOT_SESSION, "child-1", "running"), update("child-1", { sessionUpdate: "agent_message_chunk", messageId: "nested-reply", content: { type: "text", text: "The fix is correct." }, }), childState(CLAUDE_ROOT_SESSION, "child-1", "idle"), idle("end_turn"), claudeFrames.closeRequest, claudeFrames.closed, ], gate, ); yield* session.runtime.startTurn(session.turnInput(1)); const settled = yield* session.terminal; assert.equal(settled.status, stopReason === "error" ? "failed" : "completed"); if (settled.status === "failed") assert.isTrue(settled.childTurnsContinue); yield* session.runtime.startTurn(session.turnInput(2)); const resumed = yield* session.throughMarker("resumed"); const row = resumed.find((event) => event.type === "subagent.updated"); assert.isDefined(row); if (row?.type !== "subagent.updated") throw new Error("Missing resumed child"); assert.equal(row.subagent.runId, session.turnInput(2).runId); assert.equal( row.subagent.result, "Review in progress.", "live handoff retains current execution progress", ); assert.lengthOf( resumed.filter( (event) => event.type === "message.updated" && event.message.threadId === row.subagent.childThreadId && event.message.role === "user" && event.message.text === "Continue the review.", ), 1, "child resume prompts remain visible once", ); const openTools = resumed.filter( (event) => event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool" && event.turnItem.status === "running", ); assert.lengthOf(openTools, 1, "handoff announces each open child tool once"); const foreground = yield* session.until((event) => event.type === "turn.terminal"); assert.isTrue( foreground.some( (event) => event.type === "subagent.updated" && event.subagent.status === "completed", ), "the foreground Agent tool terminalizes its visible owner", ); assert.isTrue(yield* session.runtime.hasPendingBackgroundWork!); gate.release("nested-output"); const childOutput = yield* session.throughMarker("nested"); yield* Queue.take(session.offered); assert.lengthOf(session.offers, 1); yield* session.offers[0]!.dispatchIfCurrent!(Effect.void); yield* session.runtime.startTurn(session.turnInput(3, { continuation: true })); const continued = yield* session.until((event) => event.type === "turn.terminal"); const terminal = continued.at(-1)!; if (terminal.type !== "turn.terminal") throw new Error("Missing continuation terminal"); assert.equal(terminal.status, "completed"); const nested = [...childOutput, ...continued]; const messages = nested.filter( (event) => event.type === "message.updated" && event.message.text === "Nested output", ); assert.lengthOf(messages, 1); if (messages[0]?.type === "message.updated") { assert.equal(messages[0].message.threadId, row.subagent.childThreadId); } const nestedTool = nested.findLast( (event) => event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool" && event.turnItem.nativeItemRef?.nativeId === "agent-1:session:grandchild:tool:nested-tool", ); assert.isDefined(nestedTool); if (nestedTool?.type === "turn_item.updated") { assert.equal(nestedTool.turnItem.threadId, row.subagent.childThreadId); if (nestedTool.turnItem.type === "dynamic_tool") { assert.deepEqual(nestedTool.turnItem.input, { command: "cat nested/package.json" }); assert.equal(nestedTool.turnItem.status, "completed"); } const toolUpdates = nested.flatMap((event) => event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool" && event.turnItem.id === nestedTool.turnItem.id ? [event.turnItem] : [], ); assert.deepEqual( toolUpdates.map((item) => item.output), [ null, [{ type: "content", content: { type: "text", text: "one" } }], [{ type: "content", content: { type: "text", text: "onetwo" } }], [{ type: "content", content: { type: "text", text: "onetwo" } }], ], ); } assert.isFalse( nested.some( (event) => event.type === "turn_item.updated" && event.turnItem.threadId === session.turnInput(1).threadId && event.turnItem.type === "dynamic_tool" && JSON.stringify(event.turnItem.output).includes("onetwo"), ), ); assert.isTrue( nested.some( (event) => event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool" && event.turnItem.nativeItemRef?.nativeId === "agent-1:session:grandchild:tool:terminal-tool" && event.turnItem.threadId === row.subagent.childThreadId && event.turnItem.toolName === "t3-code.orchestrator_capabilities", ), "embedded agent-terminal commands preserve nested MCP identity", ); assert.isTrue(yield* session.runtime.hasPendingBackgroundWork!); gate.release("nested-idle"); const finished = yield* session.throughMarker("nested idle"); assert.isTrue( finished.some( (event) => event.type === "subagent.updated" && event.subagent.status === "completed", ), ); assert.isFalse(yield* session.runtime.hasPendingBackgroundWork!); assert.lengthOf(session.offers, 1, "nested frames make progress in one continuation"); const firstResult = finished.findLast((event) => event.type === "subagent.updated"); if (firstResult?.type !== "subagent.updated") throw new Error("Missing first review result"); assert.equal( firstResult.subagent.result, "Review in progress.", "reopening the visible owner for a running descendant keeps its execution result", ); yield* session.runtime.startTurn(session.turnInput(4)); const reviewed = yield* session.until((event) => event.type === "turn.terminal"); const currentResult = reviewed.findLast((event) => event.type === "subagent.updated"); if (currentResult?.type !== "subagent.updated") throw new Error("Missing second review result"); assert.equal(currentResult.subagent.id, row.subagent.id); assert.equal(currentResult.subagent.childThreadId, row.subagent.childThreadId); assert.equal(currentResult.subagent.result, "The fix is correct."); const currentItem = reviewed.findLast( (event) => event.type === "turn_item.updated" && event.turnItem.type === "subagent", ); if (currentItem?.type !== "turn_item.updated" || currentItem.turnItem.type !== "subagent") throw new Error("Missing current parent item"); assert.equal(currentItem.turnItem.result, "The fix is correct."); assert.lengthOf( reviewed.filter( (event) => event.type === "message.updated" && event.message.text === "The fix is correct.", ), 1, "a new execution does not append the previous execution's text", ); assert.isFalse(yield* session.runtime.hasPendingBackgroundWork!); }).pipe(Effect.provide(layerTest)), ); it.effect("writes a child's coalesced text before a restart resets it", () => Effect.gen(function* () { const { update, childState, idle, prompt } = claudeFrames; const reply = (text: string) => update("child-1", { sessionUpdate: "agent_message_chunk", messageId: "reply", content: { type: "text", text }, }); const session = yield* openClaudeReplaySession([ ...claudeFrames.open, ...prompt(1), ...claudeChild.slice(0, -1), reply("Review "), reply("done."), // A working grandchild keeps the idle child from publishing (and flushing) its status. childState("child-1", "grandchild", "running"), childState(CLAUDE_ROOT_SESSION, "child-1", "idle"), childState(CLAUDE_ROOT_SESSION, "child-1", "running"), idle("end_turn"), claudeFrames.closeRequest, claudeFrames.closed, ]); yield* session.runtime.startTurn(session.turnInput(1)); const events = yield* session.until((event) => event.type === "turn.terminal"); assert.isTrue( events.some( (event) => event.type === "message.updated" && event.message.role === "assistant" && event.message.text === "Review done.", ), ); }).pipe(Effect.provide(layerTest), Effect.scoped), ); it.effect("fails active Claude work and releases its children when the process exits", () => Effect.gen(function* () { const session = yield* openClaudeReplaySession([ ...claudeFrames.open, ...claudeFrames.prompt(1), ...claudeChild, claudeMarker("child ready"), ]); yield* session.runtime.startTurn(session.turnInput(1)); yield* session.throughMarker("child ready"); yield* session.terminate(); const terminal = yield* session.terminal; assert.equal(terminal.status, "failed"); assert.equal(terminal.failure?.class, "transport_error"); assert.isFalse(yield* session.runtime.hasPendingBackgroundWork!); const settled = yield* openClaudeReplaySession([ ...claudeFrames.open, ...claudeFrames.prompt(1), ...claudeChild, claudeFrames.childState("child-1", "grandchild", "running"), claudeFrames.update("grandchild", { sessionUpdate: "tool_call_update", toolCallId: "nested-tool", name: "Bash", kind: "execute", status: "in_progress", rawInput: { command: "cat package.json" }, }), claudeFrames.childState(CLAUDE_ROOT_SESSION, "child-1", "idle"), claudeFrames.idle("end_turn"), ]); yield* settled.runtime.startTurn(settled.turnInput(1)); const foreground = yield* settled.until((event) => event.type === "turn.terminal"); const completed = foreground.at(-1)!; if (completed.type !== "turn.terminal") throw new Error("Missing settled root terminal"); assert.equal(completed.status, "completed"); assert.isTrue(yield* settled.runtime.hasPendingBackgroundWork!); yield* settled.terminate(); assert.isFalse( yield* settled.runtime.hasPendingBackgroundWork!, "exit releases visible owners held open by nested work", ); const exited = yield* settled.until( (event) => event.type === "turn_item.updated" && event.turnItem.type === "subagent" && event.turnItem.status === "failed", ); assert.isTrue( exited.some( (event) => event.type === "subagent.updated" && event.subagent.status === "failed", ), ); const items = new Map( [...foreground, ...exited].flatMap((event) => event.type === "turn_item.updated" ? [[event.turnItem.id, event.turnItem] as const] : [], ), ); assert.deepEqual(pendingBackgroundTurnItems({ turnItems: [...items.values()] }), []); assert.isTrue( exited.some( (event) => event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool" && event.turnItem.status === "interrupted", ), ); }).pipe(Effect.provide(layerTest)), ); it.effect.each([ { stopReason: "refusal", childUpdate: "state_update" }, { stopReason: "max_tokens", childUpdate: "subagent_update" }, ])( "fails Claude's turn and child and closes its running tool when they stop with $stopReason via $childUpdate", ({ stopReason, childUpdate }) => Effect.gen(function* () { const { update, idle, prompt } = claudeFrames; const session = yield* openClaudeReplaySession([ ...claudeFrames.open, ...prompt(1), ...claudeChild, update(CLAUDE_ROOT_SESSION, { sessionUpdate: "tool_call_update", toolCallId: "root-tool", name: "Bash", title: "Read package", kind: "execute", status: "in_progress", rawInput: { command: "cat package.json" }, }), update("child-1", { sessionUpdate: "state_update", state: "running" }), childUpdate === "state_update" ? update("child-1", { sessionUpdate: "state_update", state: "idle", stopReason }) : update(CLAUDE_ROOT_SESSION, { sessionUpdate: "subagent_update", sessionId: "child-1", state: { state: "idle", stopReason }, }), idle(stopReason), ]); yield* session.runtime.startTurn(session.turnInput(1)); const events = yield* session.until((event) => event.type === "turn.terminal"); const terminal = events.at(-1)!; if (terminal.type !== "turn.terminal") throw new Error("Missing root terminal"); assert.equal(terminal.status, "failed"); assert.equal(terminal.failure?.code, stopReason); const child = events.findLast((event) => event.type === "subagent.updated"); if (child?.type !== "subagent.updated") throw new Error("Missing child update"); assert.equal(child.subagent.status, "failed"); assert.isNotNull(child.subagent.result, "the child shows why it stopped"); assert.isFalse( events.some( (event) => event.type === "message.updated" && event.message.threadId === child.subagent.childThreadId && event.message.role === "assistant", ), "the child's conversation has no reply it didn't send", ); const tool = events.findLast( (event) => event.type === "turn_item.updated" && event.turnItem.type === "command_execution", ); if (tool?.type !== "turn_item.updated") throw new Error("Missing tool update"); assert.equal(tool.turnItem.status, "failed"); }).pipe(Effect.provide(layerTest), Effect.scoped), ); it.effect("keeps Claude activity after a refused Stop and closes it on retry", () => Effect.gen(function* () { const gate = makeProviderReplayGate(["close-response", "queued-close-response"]); const session = yield* openClaudeReplaySession( [ ...claudeFrames.open, ...claudeFrames.prompt(1), ...claudeChild, claudeMarker("before Stop"), claudeFrames.closeRequest, claudeFrames.update("child-1", { sessionUpdate: "agent_message_chunk", messageId: "closing-reply", content: { type: "text", text: "Finished during close." }, }), claudeFrames.childState(CLAUDE_ROOT_SESSION, "child-1", "idle"), claudeFrames.idle("end_turn"), claudeMarker("idle during close"), { type: "emit_inbound", label: "close-response", frame: { kind: "response", method: "session/close", error: { code: -32603, message: "Close refused" }, }, }, ...claudeFrames.prompt(2), claudeMarker("before retry"), claudeFrames.closeRequest, claudeFrames.idle("cancelled"), claudeFrames.closed, ], gate, ); yield* session.runtime.startTurn(session.turnInput(1)); const started = yield* session.throughMarker("before Stop"); const running = started.findLast((event) => event.type === "provider_turn.updated"); if (running?.type !== "provider_turn.updated") throw new Error("Missing active turn"); const stop = { providerThread: session.providerThread, providerTurnId: running.providerTurn.id, requestRuntimeRestart: true, }; const closing = yield* session.runtime .interruptTurn(stop) .pipe(Effect.exit, Effect.forkScoped); yield* Effect.addFinalizer(() => Effect.sync(() => gate.releaseAll())); const idle = yield* session.throughMarker("idle during close"); gate.release("close-response"); const refused = yield* Fiber.join(closing); assert.isTrue(Exit.isFailure(refused), "a refused close must not report a successful Stop"); const terminal = idle.findLast((event) => event.type === "turn.terminal"); assert.equal( terminal?.type === "turn.terminal" ? terminal.status : null, "completed", "idle during a refused close must settle the foreground turn", ); const result = idle.findLast((event) => event.type === "subagent.updated"); if (result?.type !== "subagent.updated") throw new Error("Missing child close-time result"); assert.equal(result.subagent.status, "completed"); assert.equal(result.subagent.result, "Finished during close."); assert.isFalse(yield* session.runtime.hasPendingBackgroundWork!); yield* session.runtime.startTurn(session.turnInput(2)); const retry = yield* session.throughMarker("before retry"); const retryRunning = retry.findLast((event) => event.type === "provider_turn.updated"); if (retryRunning?.type !== "provider_turn.updated") throw new Error("Missing active retry turn"); yield* session.runtime.interruptTurn({ ...stop, providerTurnId: retryRunning.providerTurn.id, }); assert.equal((yield* session.terminal).status, "interrupted"); assert.isFalse(yield* session.runtime.hasPendingBackgroundWork!); const queued = yield* openClaudeReplaySession( [ ...claudeFrames.open, { type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: "" }, }, claudeMarker("before queued Stop"), claudeFrames.closeRequest, { type: "emit_inbound", frame: { kind: "response", method: "session/prompt", error: { code: -32800, message: "Request cancelled" }, }, }, { ...claudeFrames.closed, label: "queued-close-response" }, ], gate, ); yield* queued.runtime.startTurn(queued.turnInput(3)); const pending = yield* queued.throughMarker("before queued Stop"); const queuedRunning = pending.findLast((event) => event.type === "provider_turn.updated"); if (queuedRunning?.type !== "provider_turn.updated") throw new Error("Missing queued provider turn"); const queuedClosing = yield* queued.runtime .interruptTurn({ providerThread: queued.providerThread, providerTurnId: queuedRunning.providerTurn.id, requestRuntimeRestart: true, }) .pipe(Effect.forkScoped); const queuedTerminal = yield* queued.terminal; gate.release("queued-close-response"); yield* Fiber.join(queuedClosing); assert.equal(queuedTerminal.providerTurnId, queuedRunning.providerTurn.id); assert.equal(queuedTerminal.status, "interrupted"); assert.isNull(queuedTerminal.failure); assert.isFalse(yield* queued.runtime.hasPendingBackgroundWork!); }).pipe(Effect.provide(layerTest)), ); it.effect.each([ { failure: "a lost session", error: { code: -32603, message: "Internal error", data: { details: "Request session.send failed with message: session event delivery failed: session not found: copilot-session", }, }, restarts: true, }, { failure: "a lost session reported in the message", error: { code: -32603, message: "Request session.send failed with message: session event delivery failed: session not found: copilot-session", }, restarts: true, }, { failure: "a session-lock timeout", error: { code: -32603, message: "Internal error", data: { details: "session lock unavailable during is_session_generation_current: acquisition timed out", }, }, restarts: true, }, { failure: "a session-lock timeout reported in the message", error: { code: -32603, message: "Request session.send failed with message: session lock unavailable during is_session_generation_current: acquisition timed out", }, restarts: true, }, { failure: "an unrelated internal error", error: { code: -32603, message: "Request session.send failed with message: Session directory no longer exists", }, restarts: false, }, ] as const)("runs Copilot's next message after $failure", ({ error, restarts }) => Effect.gen(function* () { const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner; const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const replayDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-copilot-lost-" }); const scriptPath = yield* path.fromFileUrl( new URL("../../../scripts/acp-replay-agent.ts", import.meta.url), ); const request = (method: string, params: unknown = "") => ({ type: "expect_outbound", frame: { kind: "request", method, params }, }); const respond = (method: string, response: object) => ({ type: "emit_inbound", frame: { kind: "response", method, ...response }, }); const start = (sessionId: string) => [ request("initialize"), respond("initialize", { result: { protocolVersion: 1, agentCapabilities: { loadSession: true } }, }), request("session/new"), respond("session/new", { result: { sessionId } }), ]; const failedPrompt = [ ...start("copilot-session"), request("session/prompt"), respond("session/prompt", { error }), ]; const completedPrompt = [ request("session/prompt"), respond("session/prompt", { result: { stopReason: "end_turn" } }), ]; const processes = restarts ? [ failedPrompt, [ ...start("replacement-session"), request("session/load", { sessionId: "copilot-session", cwd: "", mcpServers: "", }), respond("session/load", { result: {} }), ...completedPrompt, ], ] : [[...failedPrompt, ...completedPrompt]]; let spawned = 0; const instanceId = ProviderInstanceId.make("copilot-lost-session"); const adapter = yield* makeAcpRegistryAdapterV2({ selfInvocation: yield* resolveSelfInvocation(), instanceId, settings: yield* decodeAcpRegistryAdapterSettings({ agentId: "github-copilot-cli" }), environment: {}, makeRuntime: (runtimeInput) => Effect.gen(function* () { const index = spawned++; const transcript = yield* decodeAcpReplayTranscript( { provider: ACP_REGISTRY_PROVIDER, protocol: "acp.ndjson-jsonrpc", version: "1", scenario: `copilot-lost-session-${index}`, entries: processes[index] as never, }, ACP_REGISTRY_PROVIDER, ).pipe(Effect.orDie); return yield* makeAcpReplayRuntime({ transcript, statusPath: path.join(replayDir, `status-${index}.json`), scriptPath, childProcessSpawner, fileSystem, })(runtimeInput); }), }).pipe(Effect.provide(layerInjectedRuntimeCatalog)); const threadId = ThreadId.make("thread-copilot-lost-session"); const modelSelection = { instanceId, model: "default" } as const; const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ runtimeMode: "approval-required", interactionMode: "default", cwd: replayDir, }); const runtime = yield* adapter.openSession({ threadId, providerSessionId: ProviderSessionId.make("session-copilot-lost-session"), modelSelection, runtimePolicy, }); const providerThread = yield* runtime.ensureThread({ threadId, modelSelection, runtimePolicy, }); const now = yield* DateTime.now; const runTurn = (ordinal: number) => runtime .startTurn( makeTurnInput({ threadId, providerThread, instanceId, runtimePolicy, now, ordinal }), ) .pipe( Effect.andThen( runtime.events.pipe( Stream.filter((event) => event.type === "turn.terminal"), Stream.take(1), Stream.runCollect, ), ), Effect.map(([terminal]) => terminal?.status), ); assert.equal(yield* runTurn(1), "failed"); assert.equal(yield* runTurn(2), "completed"); assert.equal(spawned, restarts ? 2 : 1); }).pipe(Effect.provide(layerTest)), ); });