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 Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
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: "<any>" } },
{
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: "<any>" } },
{
type: "emit_inbound",
frame: {
kind: "response",
method: "session/new",
result: { sessionId: CLAUDE_ROOT_SESSION },
},
},
],
update: (sessionId: string, sessionUpdate: Record<string, unknown>) => ({
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: "<any>" },
},
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<unknown>,
replayGate?: ProviderReplayGate,
continuationEnabled = true,
) {
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<ProviderContinuationRequest> = [];
const offered = yield* Queue.unbounded<void>();
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");
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({ agentId: "claude-acp" }),
environment: {},
...(continuationEnabled ? { continuationRequests } : {}),
makeRuntime: (runtimeInput: Parameters<typeof replayRuntime>[0]) => {
onTermination = runtimeInput.onTermination;
return replayRuntime(runtimeInput);
},
};
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) {
assert.isDefined(
runtime.hasPendingBackgroundWork,
"Claude ACP v2 must report background work after foreground idle",
);
}
const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
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<ProviderAdapter.ProviderAdapterV2Event> = [];
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,
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<string, unknown>;
const outbound = (method: string, params: unknown = "<any>"): 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<Frame>;
readonly customModels?: ReadonlyArray<string>;
readonly storedModel?: string;
readonly storedModePick?: string;
readonly turnModels?: ReadonlyArray<string>;
readonly rejectedModel?: string;
}) {
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<Effect.Effect<void>> = [];
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: "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,
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 }] }),
};
const configurations: Array<AcpRegistryLiveConfiguration | undefined> = [];
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) => {
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),
}),
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), Effect.scoped),
);
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), Effect.scoped),
);
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<unknown>): Frame => ({
type: "emit_inbound",
frame: {
kind: "notification",
method: "session/update",
params: {
sessionId: "agent-session",
update: { sessionUpdate: "config_option_update", configOptions },
},
},
});
const prompt = (...updates: ReadonlyArray<Frame>): ReadonlyArray<Frame> => [
outbound("session/prompt"),
...updates,
answer("session/prompt", { stopReason: "end_turn" }),
];
const setModel = (modelId: string): ReadonlyArray<Frame> => [
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), Effect.scoped),
);
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), Effect.scoped);
},
);
// 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), Effect.scoped),
);
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), Effect.scoped),
);
});
// 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<string, unknown>,
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), Effect.scoped),
);
// 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<string, unknown>) {
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), Effect.scoped),
);
it.effect("gives Copilot T3's MCP server over HTTP when it advertises 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<string, unknown>,
agentEnvironment: Record<string, string> = {},
) {
const requests: Array<AcpSessionRuntime.AcpSessionRequestLogEvent> = [];
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 http = (authorization: string) => [
{
type: "http",
name: "t3-code",
url: endpoint,
headers: [{ name: "Authorization", value: authorization }],
},
];
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"),
});
}).pipe(Effect.provide(layerTest), Effect.scoped),
);
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<Record<string, unknown> | 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 = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
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), Effect.scoped),
);
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",
},
runtimeInstructions,
];
const echo = (messageId: string, content: ReadonlyArray<unknown>) =>
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),
},
]),
...(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), Effect.scoped),
);
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), Effect.scoped),
);
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" } },
},
},