import { withoutAgentListingMessages } from "./test-agent-listing.js"; import { describe, expect, it, vi } from "vitest"; import { createTestModelSelection } from "./test-model-selection.js"; import { createTestRuntimeModel } from "./test-runtime-model.js"; import { AutomationCreateLimitError, CoreErrorType, RewindScope, createFileSystemError, createMessageId, createSessionEvent, createSessionId, createTraceId, createTurnId, GOAL_COMPLETION_VERIFICATION_QUERY_SOURCE, HookEventName, ModelTransportKind, SESSION_ENTRY_TARGET_COMPLETION_VERIFICATION, SessionEventType, STREAM_RECOVERY_DISCARDED_ERROR_NAME, STREAM_RECOVERY_DISCARDED_FINISH, } from "@zcode/contracts"; import type { AutomationPort, CreateSessionInput, BackgroundExecutionSnapshot, ExecutionPort, FileSystemPort, ForkCommitBundle, MessageInfo, MessageId, MessagePart, MessageWithParts, ModelSelection, ModelUsageRecord, PartId, PermissionBrokerPort, PermissionRuleset, ProjectId, SessionId, SessionEntryInfo, SessionInfo, SessionRevert, SessionStorePort, SessionGoal, GoalStatus, ToolArtifactReadRequest, ToolArtifactStorePort, ToolArtifactWriteRequest, TodoItem, UpdateSessionInput, } from "@zcode/contracts"; import { createTestAgentRuntime } from "./test-agent-runtime.js"; import { MessageHistoryImpl } from "../src/agent/message-history.js"; import { hydrateMessageHistoryFromSession } from "../src/agent/session-history-hydrator.js"; import { buildProviderRequestMessages } from "../src/runtime/helpers/provider-request-messages.js"; import { DEFAULT_MODEL_REQUEST_MEDIA_BUDGET_BYTES } from "../src/runtime/helpers/media-budget.js"; import { MAX_TURN_STEER_INPUT_BYTES } from "../src/runtime/helpers/steering.js"; import { projectIdFromDirectory } from "../src/runtime/helpers/project.js"; import { InMemoryRuntimeTaskRegistry } from "../src/runtime-task/registry.js"; import { createToolRegistry } from "../src/tool/registry.js"; import { BackgroundTaskTracker } from "../src/tool/executor/background-tasks.js"; import { formatReadBatchNotice } from "../src/tool/handlers/read-text.js"; import type { ToolExecutionContext } from "../src/tool/types.js"; import { createTestSessionEventStore } from "./test-event-store.js"; import { START_PLAN_BUSY_AUTO_RETRY_EXHAUSTED_MESSAGE } from "../src/runtime/methods/streaming-recovery.js"; import type { StableConversationForkChildMetadata } from "../src/runtime/types.js"; import { formatSubagentMessage } from "../src/runtime/methods/subagent-messages.js"; import type { BashBackgroundLifecycleExecutionPort } from "../src/tool/handlers/bash-background-lifecycle.js"; import { createTestModelFactory, type TestModelExecutionObservation, } from "./test-runtime-model.js"; type TestBashBackgroundLifecycleExecutionPort = BashBackgroundLifecycleExecutionPort & { waitForBackgroundTask?( taskId: string, options?: { signal?: AbortSignal }, ): Promise; }; function providerContentToText(content: unknown): string { if (typeof content === "string") return content; if (Array.isArray(content)) { return content .map((block) => { if (block && typeof block === "object" && "type" in block) { if (block.type === "text" && "text" in block) { return String(block.text); } return JSON.stringify(block); } return String(block ?? ""); }) .join("\n"); } return String(content ?? ""); } function providerMessagesToText(messages: readonly { content?: unknown }[]): string { return messages.map((message) => providerContentToText(message.content)).join("\n"); } async function publishTestModelNetworkStatus( observation: TestModelExecutionObservation, event: Record, ): Promise { await observation.invocationContext?.statusSink?.publish({ attempt: 1, maxAttempts: 1, providerId: String(observation.model.providerId), modelId: String(observation.model.modelId), requestId: event.requestId, timestamp: new Date().toISOString(), traceId: observation.invocationContext?.traceContext?.traceId ?? createTraceId("test-trace"), transport: ModelTransportKind.Sse, ...event, }); } function createProviderBusyError(providerId: string, providerCode = "3010"): Error { const error = new Error("model admission concurrency limit exceeded") as Error & { code?: string; context?: Record; }; error.code = "model_rate_limited"; error.context = { providerCode, providerId, reason: "rate_limited", retryable: false, }; return error; } describe("AgentRuntime tool loop", () => { it("tracks Bash background completion through direct execution wait", async () => { const sessionId = createSessionId("runtime-bash-background-direct-wait"); const turnId = createTurnId("turn-bash-background-direct-wait"); const events: any[] = []; const notifications: any[] = []; const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry(); const tracker = new BackgroundTaskTracker({ emitEvent: async (event: any) => { events.push(event); }, enqueueBackgroundTaskNotification: (notification: any) => { notifications.push(notification); }, executionPort: { async waitForBackgroundTask(taskId: string) { return { completedAt: new Date(), outputPath: "/tmp/zcode-bash-output.log", result: { cancelled: false, completedAt: new Date(), durationMs: 60, exitCode: 0, startedAt: new Date(), status: "completed" as const, stderr: { bytes: 0, text: "", truncated: false, }, stdout: { artifactPath: "/tmp/zcode-bash-output.log", bytes: 4, text: "done", truncated: false, }, timedOut: false, }, startedAt: new Date(), status: "completed" as const, taskId, }; }, } as any, logger: { warn: vi.fn(), }, runtimeTaskRegistry, sessionId, } as any); await tracker.trackBackgroundTask( { id: "toolu_bash_direct_wait", input: { command: "npm test", }, name: "Bash", } as any, { backgroundTaskId: "exec_direct_wait", persistedOutputPath: "/tmp/zcode-bash-output.log", status: "backgrounded", }, { traceId: createTraceId("trace-bash-background-direct-wait"), } as any, turnId, ); await waitForCondition(() => notifications.length === 1); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskStarted); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskCompleted); expect(notifications).toHaveLength(1); expect(notifications[0]).toMatchObject({ originMeta: { backgroundSource: "bash", title: "npm test", workId: "exec_direct_wait", }, taskId: "exec_direct_wait", toolName: "Bash", }); expect(notifications[0].text).toContain(""); expect(notifications[0].text).toContain("exec_direct_wait"); expect(notifications[0].text).toContain( 'Background command "npm test" completed (exit code 0)', ); expect(notifications[0].text).not.toContain("Use Read on the output file"); expect(runtimeTaskRegistry.get("exec_direct_wait")).toMatchObject({ isBackgrounded: true, outputFile: "/tmp/zcode-bash-output.log", status: "completed", taskId: "exec_direct_wait", type: "local_bash", }); }); it("tracks Workflow background tasks in the runtime task registry", async () => { const sessionId = createSessionId("runtime-workflow-background-registry"); const turnId = createTurnId("turn-workflow-background-registry"); const startedAt = new Date(0); const events: any[] = []; const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry(); const tracker = new BackgroundTaskTracker({ emitEvent: async (event: any) => { events.push(event); }, logger: { warn: vi.fn(), }, runtimeTaskRegistry, sessionId, workflowPort: { async getTask(taskId: string) { return { description: "Review changes", name: "Review workflow", runId: taskId, startedAt, status: "running" as const, taskId, }; }, }, } as any); await tracker.trackBackgroundTask( { id: "toolu_workflow_registry", input: { scriptPath: "review.workflow.js", }, name: "Workflow", } as any, { backgroundTaskId: "wf_registry", name: "Review workflow", response: "started", runId: "wf_registry", status: "backgrounded", traceId: "trace_workflow_registry", }, { traceId: createTraceId("trace-workflow-background-registry"), } as any, turnId, ); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskStarted); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskUpdated); expect(runtimeTaskRegistry.get("wf_registry")).toMatchObject({ agentId: "wf_registry", agentType: "local_workflow", description: "Review changes", isBackgrounded: true, parentToolCallId: "toolu_workflow_registry", status: "running", taskId: "wf_registry", taskType: "local_workflow", type: "local_workflow", }); }); it("queues completed Workflow notifications as task-notification commands", async () => { const sessionId = createSessionId("runtime-workflow-background-notification"); const turnId = createTurnId("turn-workflow-background-notification"); const startedAt = new Date(0); const completedAt = new Date(1); const events: any[] = []; const notifications: any[] = []; const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry(); const tracker = new BackgroundTaskTracker({ emitEvent: async (event: any) => { events.push(event); }, enqueueBackgroundTaskNotification: (notification: any) => { notifications.push(notification); }, logger: { warn: vi.fn(), }, runtimeTaskRegistry, sessionId, workflowPort: { async getTask(taskId: string) { return { completedAt, description: "Review changes", name: "Review workflow", output: { backgroundTaskId: taskId, name: "Review workflow", response: "Workflow completed: wf_notify", runId: taskId, scriptPath: "/tmp/review.workflow.js", status: "completed", traceId: "trace_workflow_notify", }, runId: taskId, startedAt, status: "completed" as const, taskId, }; }, }, } as any); await tracker.trackBackgroundTask( { id: "toolu_workflow_notify", input: { scriptPath: "review.workflow.js", }, name: "Workflow", } as any, { backgroundTaskId: "wf_notify", name: "Review workflow", response: "started", runId: "wf_notify", status: "backgrounded", traceId: "trace_workflow_notify", }, { traceId: createTraceId("trace-workflow-background-notification"), } as any, turnId, ); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskStarted); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskCompleted); expect(notifications).toHaveLength(1); expect(notifications[0].text).toContain(""); expect(notifications[0].text).toContain("wf_notify"); expect(notifications[0].text).toContain("toolu_workflow_notify"); expect(notifications[0].text).toContain("completed"); expect(notifications[0].text).toContain( "Workflow "Review changes" completed.", ); expect(notifications[0].text).toContain("Workflow completed: wf_notify"); expect(notifications[0].text).not.toContain("[SYSTEM NOTIFICATION"); expect(runtimeTaskRegistry.get("wf_notify")).toMatchObject({ isBackgrounded: true, notified: true, status: "completed", taskId: "wf_notify", type: "local_workflow", }); }); it("tracks Workflow completion through direct workflow wait", async () => { const sessionId = createSessionId("runtime-workflow-background-direct-wait"); const turnId = createTurnId("turn-workflow-background-direct-wait"); const startedAt = new Date(0); const completedAt = new Date(1); const events: any[] = []; const notifications: any[] = []; const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry(); const tracker = new BackgroundTaskTracker({ emitEvent: async (event: any) => { events.push(event); }, enqueueBackgroundTaskNotification: (notification: any) => { notifications.push(notification); }, logger: { warn: vi.fn(), }, runtimeTaskRegistry, sessionId, workflowPort: { async waitForTask(taskId: string) { return { completedAt, description: "Direct wait workflow", output: { backgroundTaskId: taskId, response: "Workflow completed through direct wait", runId: taskId, status: "completed", traceId: "trace_workflow_direct_wait", }, runId: taskId, startedAt, status: "completed" as const, taskId, }; }, }, } as any); await tracker.trackBackgroundTask( { id: "toolu_workflow_direct_wait", input: { scriptPath: "direct.workflow.js", }, name: "Workflow", } as any, { backgroundTaskId: "wf_direct_wait", response: "started", runId: "wf_direct_wait", status: "backgrounded", traceId: "trace_workflow_direct_wait", }, { traceId: createTraceId("trace-workflow-background-direct-wait"), } as any, turnId, ); await waitForCondition(() => notifications.length === 1); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskStarted); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskCompleted); expect(notifications[0].text).toContain("wf_direct_wait"); expect(notifications[0].text).toContain( "Workflow completed through direct wait", ); expect(runtimeTaskRegistry.get("wf_direct_wait")).toMatchObject({ isBackgrounded: true, notified: true, status: "completed", taskId: "wf_direct_wait", type: "local_workflow", }); }); it("queues Bash completion notifications before emitting terminal events", async () => { const sessionId = createSessionId("runtime-bash-notification-before-terminal-event"); const turnId = createTurnId("turn-bash-notification-before-terminal-event"); const startedAt = new Date(0); const completedAt = new Date(1); const completionEventStarted = deferred(); const completionEventMayFinish = deferred(); const completionEventFinished = deferred(); const notifications: any[] = []; const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry(); const tracker = new BackgroundTaskTracker({ emitEvent: async (event: any) => { if (event.type === SessionEventType.BackgroundTaskCompleted) { completionEventStarted.resolve(); await completionEventMayFinish.promise; completionEventFinished.resolve(); } }, enqueueBackgroundTaskNotification: (notification: any) => { notifications.push(notification); }, executionPort: { async waitForBackgroundTask(taskId: string) { return { completedAt, outputPath: "/tmp/zcode-bash-output.log", result: { cancelled: false, completedAt, durationMs: 60, exitCode: 0, startedAt, status: "completed" as const, stderr: { bytes: 0, text: "", truncated: false, }, stdout: { artifactPath: "/tmp/zcode-bash-output.log", bytes: 4, text: "done", truncated: false, }, timedOut: false, }, startedAt, status: "completed" as const, taskId, }; }, } as any, logger: { warn: vi.fn(), }, runtimeTaskRegistry, sessionId, } as any); await tracker.trackBackgroundTask( { id: "toolu_bash_notification_before_terminal_event", input: { command: "npm test", }, name: "Bash", } as any, { backgroundTaskId: "exec_notify_before_event", persistedOutputPath: "/tmp/zcode-bash-output.log", status: "backgrounded", }, { traceId: createTraceId("trace-bash-notification-before-terminal-event"), } as any, turnId, ); await completionEventStarted.promise; expect(notifications).toHaveLength(1); expect(runtimeTaskRegistry.get("exec_notify_before_event")).toMatchObject({ notified: true, status: "completed", type: "local_bash", }); completionEventMayFinish.resolve(); await completionEventFinished.promise; }); it("queues Workflow completion notifications before emitting terminal events", async () => { const sessionId = createSessionId("runtime-workflow-notification-before-terminal-event"); const turnId = createTurnId("turn-workflow-notification-before-terminal-event"); const startedAt = new Date(0); const completedAt = new Date(1); const completionEventStarted = deferred(); const completionEventMayFinish = deferred(); const completionEventFinished = deferred(); const notifications: any[] = []; const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry(); const tracker = new BackgroundTaskTracker({ emitEvent: async (event: any) => { if (event.type === SessionEventType.BackgroundTaskCompleted) { completionEventStarted.resolve(); await completionEventMayFinish.promise; completionEventFinished.resolve(); } }, enqueueBackgroundTaskNotification: (notification: any) => { notifications.push(notification); }, logger: { warn: vi.fn(), }, runtimeTaskRegistry, sessionId, workflowPort: { async waitForTask(taskId: string) { return { completedAt, description: "Queued workflow", output: { backgroundTaskId: taskId, response: "Workflow completed before event", runId: taskId, status: "completed", traceId: "trace_workflow_notification_before_event", }, runId: taskId, startedAt, status: "completed" as const, taskId, }; }, }, } as any); await tracker.trackBackgroundTask( { id: "toolu_workflow_notification_before_terminal_event", input: { scriptPath: "queued.workflow.js", }, name: "Workflow", } as any, { backgroundTaskId: "wf_notify_before_event", response: "started", runId: "wf_notify_before_event", status: "backgrounded", traceId: "trace_workflow_notification_before_event", }, { traceId: createTraceId("trace-workflow-notification-before-terminal-event"), } as any, turnId, ); await completionEventStarted.promise; expect(notifications).toHaveLength(1); expect(runtimeTaskRegistry.get("wf_notify_before_event")).toMatchObject({ notified: true, status: "completed", type: "local_workflow", }); completionEventMayFinish.resolve(); await completionEventFinished.promise; }); it("marks background registry tasks lost when snapshot provider loses the task", async () => { const sessionId = createSessionId("runtime-bash-background-registry-lost"); const turnId = createTurnId("turn-bash-background-registry-lost"); const events: any[] = []; const notifications: any[] = []; const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry(); const tracker = new BackgroundTaskTracker({ emitEvent: async (event: any) => { events.push(event); }, enqueueBackgroundTaskNotification: (notification: any) => { notifications.push(notification); }, executionPort: { async getBackgroundTask() { return undefined; }, } as any, logger: { warn: vi.fn(), }, runtimeTaskRegistry, sessionId, } as any); await tracker.trackBackgroundTask( { id: "toolu_bash_lost", input: { command: "npm test", }, name: "Bash", } as any, { backgroundTaskId: "exec_lost", persistedOutputPath: "/tmp/zcode-bash-lost.log", status: "backgrounded", }, { traceId: createTraceId("trace-bash-background-registry-lost"), } as any, turnId, ); expect(events).toContainEqual( expect.objectContaining({ payload: expect.objectContaining({ status: "lost", taskId: "exec_lost", }), type: SessionEventType.BackgroundTaskCompleted, }), ); expect(runtimeTaskRegistry.get("exec_lost")).toMatchObject({ isBackgrounded: true, status: "lost", taskId: "exec_lost", type: "local_bash", }); expect(notifications).toHaveLength(1); expect(notifications[0].text).toContain("exec_lost"); expect(notifications[0].text).toContain("failed"); expect(notifications[0].text).not.toContain("lost"); }); it("keeps running Bash background snapshot updates when direct completion wait is available", async () => { const sessionId = createSessionId("runtime-bash-background-direct-wait-updates"); const turnId = createTurnId("turn-bash-background-direct-wait-updates"); const startedAt = new Date(0); const completedAt = new Date(1); const events: any[] = []; const notifications: any[] = []; const completedSnapshot = { completedAt, outputPath: "/tmp/zcode-bash-output.log", result: { cancelled: false, completedAt, durationMs: 60, exitCode: 0, startedAt, status: "completed" as const, stderr: { bytes: 0, text: "", truncated: false, }, stdout: { artifactPath: "/tmp/zcode-bash-output.log", bytes: 5, text: "ready", truncated: false, }, timedOut: false, }, startedAt, status: "completed" as const, taskId: "exec_direct_wait_updates", }; const tracker = new BackgroundTaskTracker({ emitEvent: async (event: any) => { events.push(event); }, enqueueBackgroundTaskNotification: (notification: any) => { notifications.push(notification); }, executionPort: { async getBackgroundTask(taskId: string) { return { outputPath: "/tmp/zcode-bash-output.log", startedAt, status: "running" as const, stdoutBytes: 5, stdoutTail: "ready", taskId, }; }, async waitForBackgroundTask() { await delay(50); return completedSnapshot; }, } as any, logger: { warn: vi.fn(), }, sessionId, } as any); await tracker.trackBackgroundTask( { id: "toolu_bash_direct_wait_updates", input: { command: "npm run dev", }, name: "Bash", } as any, { backgroundTaskId: "exec_direct_wait_updates", persistedOutputPath: "/tmp/zcode-bash-output.log", status: "backgrounded", }, { traceId: createTraceId("trace-bash-background-direct-wait-updates"), } as any, turnId, ); await waitForCondition(() => events.some( (event) => event.type === SessionEventType.BackgroundTaskUpdated && event.payload?.stdoutTail === "ready", ), ); await waitForCondition(() => notifications.length === 1); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskStarted); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskUpdated); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskCompleted); expect(notifications[0].text).toContain("completed"); }); it("normalizes stopped Bash background notifications to task status", async () => { const sessionId = createSessionId("runtime-bash-background-stopped-notification"); const turnId = createTurnId("turn-bash-background-stopped-notification"); const notifications: any[] = []; const tracker = new BackgroundTaskTracker({ emitEvent: async () => {}, enqueueBackgroundTaskNotification: (notification: any) => { notifications.push(notification); }, executionPort: { async waitForBackgroundTask(taskId: string) { return { completedAt: new Date(), outputPath: "/tmp/zcode-bash-output.log", result: { cancelled: true, completedAt: new Date(), durationMs: 60, startedAt: new Date(), status: "cancelled" as const, stderr: { bytes: 0, text: "", truncated: false, }, stdout: { artifactPath: "/tmp/zcode-bash-output.log", bytes: 0, text: "", truncated: false, }, timedOut: false, }, startedAt: new Date(), status: "cancelled" as const, taskId, }; }, } as any, logger: { warn: vi.fn(), }, sessionId, } as any); await tracker.trackBackgroundTask( { id: "toolu_bash_stopped_notification", input: { command: "npm test", }, name: "Bash", } as any, { backgroundTaskId: "exec_stopped_notification", persistedOutputPath: "/tmp/zcode-bash-output.log", status: "backgrounded", }, { traceId: createTraceId("trace-bash-background-stopped-notification"), } as any, turnId, ); await waitForCondition(() => notifications.length === 1); expect(notifications[0].text).toContain("killed"); expect(notifications[0].text).not.toContain("cancelled"); expect(notifications[0].text).toContain( 'Background command "npm test" was stopped', ); }); it("does not keep a stale Bash background tracker when started event emit fails", async () => { const sessionId = createSessionId("runtime-bash-background-start-failure"); const turnId = createTurnId("turn-bash-background-start-failure"); const events: any[] = []; const notifications: any[] = []; let failStartedEmit = true; const tracker = new BackgroundTaskTracker({ emitEvent: async (event: any) => { if (failStartedEmit && event.type === SessionEventType.BackgroundTaskStarted) { throw new Error("event store unavailable"); } events.push(event); }, enqueueBackgroundTaskNotification: (notification: any) => { notifications.push(notification); }, executionPort: { async waitForBackgroundTask(taskId: string) { return { completedAt: new Date(), outputPath: "/tmp/zcode-bash-output.log", result: { cancelled: false, completedAt: new Date(), durationMs: 60, exitCode: 0, startedAt: new Date(), status: "completed" as const, stderr: { bytes: 0, text: "", truncated: false, }, stdout: { artifactPath: "/tmp/zcode-bash-output.log", bytes: 4, text: "done", truncated: false, }, timedOut: false, }, startedAt: new Date(), status: "completed" as const, taskId, }; }, } as any, logger: { warn: vi.fn(), }, sessionId, } as any); const toolCall = { id: "toolu_bash_direct_wait_retry", input: { command: "npm test", }, name: "Bash", } as any; const output = { backgroundTaskId: "exec_direct_wait_retry", persistedOutputPath: "/tmp/zcode-bash-output.log", status: "backgrounded", }; const traceContext = { traceId: createTraceId("trace-bash-background-start-failure"), } as any; await expect( tracker.trackBackgroundTask(toolCall, output, traceContext, turnId), ).rejects.toThrow("event store unavailable"); failStartedEmit = false; await tracker.trackBackgroundTask(toolCall, output, traceContext, turnId); await waitForCondition(() => notifications.length === 1); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskStarted); expect(events.map((event) => event.type)).toContain(SessionEventType.BackgroundTaskCompleted); expect(notifications).toHaveLength(1); }); it("does not wake a sealed subagent runtime for background Bash completion", async () => { const sessionId = createSessionId("sealed-subagent-background-bash"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const startedAt = new Date(0); const completedAt = new Date(1); const backgroundCanFinish = deferred(); let modelCallCount = 0; const executionPort: TestBashBackgroundLifecycleExecutionPort = { async run() { throw new Error("run should not be called for explicit background Bash"); }, async runBashWithBackgroundLifecycle() { return { kind: "backgrounded", task: { taskId: "child_bash_bg", status: "running", startedAt, outputPath: "/tmp/child-bash.log", }, }; }, async getBackgroundTask(taskId) { return { taskId, status: "running", startedAt, outputPath: "/tmp/child-bash.log", }; }, async waitForBackgroundTask(taskId) { await backgroundCanFinish.promise; return { taskId, status: "completed", startedAt, completedAt, outputPath: "/tmp/child-bash.log", result: { status: "completed", exitCode: 0, stdout: { text: "done", bytes: 4, truncated: false }, stderr: { text: "", bytes: 0, truncated: false }, durationMs: 1, timedOut: false, cancelled: false, startedAt, completedAt, }, }; }, }; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", taskType: "subagent_child", workingDirectory: "/tmp/zcode-sealed-subagent-bg-bash", }, { eventStore, executionPort, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount += 1; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "bash-bg", name: "Bash", input: { command: "node long-running.js", description: "child long task", run_in_background: true, }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "child launched background bash", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.executeTurn("start child background bash"); (runtime as any).sealBackgroundTaskNotifications({ reason: "subagent_terminal", }); backgroundCanFinish.resolve(); await waitForCondition(async () => { const projection = await runtime.getProjection(); return projection.backgroundTasks.some( (task) => task.taskId === "child_bash_bg" && task.status === "completed", ); }); expect(modelCallCount).toBe(2); const persisted = await sessionStore.messages({ sessionID: sessionId }); expect(JSON.stringify(persisted)).not.toContain(""); }); it("seals the default child runtime after a subagent turn completes", async () => { const parentSessionId = createSessionId("default-child-runtime-seal-parent"); const childTurnId = createTurnId("default-child-runtime-seal-turn"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const startedAt = new Date(0); const completedAt = new Date(1); const backgroundCanFinish = deferred(); let childModelCallCount = 0; let childSessionId: SessionId | undefined; const executionPort: TestBashBackgroundLifecycleExecutionPort = { async run() { throw new Error("run should not be called for explicit background Bash"); }, async runBashWithBackgroundLifecycle() { return { kind: "backgrounded", task: { taskId: "child_default_bash_bg", status: "running", startedAt, outputPath: "/tmp/child-default-bash.log", }, }; }, async getBackgroundTask(taskId) { return { taskId, status: "running", startedAt, outputPath: "/tmp/child-default-bash.log", }; }, async waitForBackgroundTask(taskId) { await backgroundCanFinish.promise; return { taskId, status: "completed", startedAt, completedAt, outputPath: "/tmp/child-default-bash.log", result: { status: "completed", exitCode: 0, stdout: { text: "done", bytes: 4, truncated: false }, stderr: { text: "", bytes: 0, truncated: false }, durationMs: 1, timedOut: false, cancelled: false, startedAt, completedAt, }, }; }, }; const parentRuntime = createTestAgentRuntime( parentSessionId, { mode: "yolo", workingDirectory: "/tmp/zcode-default-child-runtime-seal", }, { eventStore, executionPort, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { childModelCallCount += 1; childSessionId ??= observation.invocationContext?.traceContext?.sessionId; if (childModelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "bash-bg-default-child", name: "Bash", input: { command: "node long-running.js", description: "child default long task", run_in_background: true, }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "child launched background bash", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); const subagentPort = (parentRuntime as any).subagentPort; const output = await subagentPort.run({ agentType: "general-purpose", description: "child starts background bash", parentToolCallId: "call_default_child_seal", prompt: "start a background bash and finish", sessionId: parentSessionId, trace: { traceId: createTraceId("trace-default-child-runtime-seal"), sessionId: parentSessionId, turnId: childTurnId, }, turnId: childTurnId, workingDirectory: "/tmp/zcode-default-child-runtime-seal", workspaceRoot: "/tmp/zcode-default-child-runtime-seal", }); expect(output).toMatchObject({ content: [{ text: "child launched background bash", type: "text" }], status: "completed", }); backgroundCanFinish.resolve(); await waitForCondition(async () => { if (!childSessionId) return false; const events = await eventStore.getEvents(childSessionId); return events.some((event) => event.type === SessionEventType.BackgroundTaskCompleted); }); await delay(20); expect(childModelCallCount).toBe(2); }); it("cancels subagent background Bash after the subagent max runtime", async () => { vi.useFakeTimers(); try { const sessionId = createSessionId("subagent-background-bash-timeout"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const startedAt = new Date(0); const cancelled: string[] = []; let modelCallCount = 0; const executionPort: TestBashBackgroundLifecycleExecutionPort = { async run() { throw new Error("run should not be called"); }, async runBashWithBackgroundLifecycle() { return { kind: "backgrounded", task: { taskId: "child_bash_timeout", status: "running", startedAt }, }; }, async getBackgroundTask(taskId) { return { taskId, status: "running", startedAt }; }, async cancelBackgroundTask(taskId) { cancelled.push(taskId); return { taskId, status: "cancelled", startedAt, completedAt: new Date(10), }; }, }; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", taskType: "subagent_child", subagents: { backgroundBashMaxMs: 10 }, workingDirectory: "/tmp/zcode-subagent-bash-timeout", }, { eventStore, executionPort, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount += 1; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "bash-bg-timeout", name: "Bash", input: { command: "node long-running.js", description: "child long task", run_in_background: true, }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "child launched background bash", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.executeTurn("launch child bash"); await vi.advanceTimersByTimeAsync(10); expect(cancelled).toEqual(["child_bash_timeout"]); } finally { vi.useRealTimers(); } }); it("cancels running subagent background Bash tasks when the subagent turn is cancelled", async () => { const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry(); const cancelled: string[] = []; const runtime = createTestAgentRuntime( createSessionId("subagent-background-bash-cancel-cleanup"), { mode: "yolo", taskType: "subagent_child", workingDirectory: "/tmp/zcode-child-cancel", }, { eventStore: createTestSessionEventStore(), executionPort: { async run() { throw new Error("not used"); }, async cancelBackgroundTask(taskId) { cancelled.push(taskId); return { taskId, status: "cancelled", startedAt: new Date(0), completedAt: new Date(1), }; }, }, modelFactory: createTestModelFactory({ async generateText(request: any) { return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), runtimeTaskRegistry, sessionStore: new RecordingSessionStore(), }, ); runtimeTaskRegistry.register({ agentId: "child_bash_cancel", agentType: "local_bash", description: "child bash", isBackgrounded: true, startedAt: new Date(0), status: "running", taskId: "child_bash_cancel", taskType: "local_bash", type: "local_bash", }); await (runtime as any).cancelRunningRuntimeBackgroundTasks({ reason: "subagent_cancelled", }); expect(cancelled).toEqual(["child_bash_cancel"]); }); it("queues concurrent prompt turns on the same runtime", async () => { const sessionId = createSessionId("runtime-command-queue-prompt"); const eventStore = createTestSessionEventStore(); const firstModelStarted = deferred(); const firstModelCanFinish = deferred(); const modelInputs: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestMessage = withoutAgentListingMessages(request.messages).at(-1); modelInputs.push(providerContentToText(latestMessage?.content)); if (modelInputs.length === 1) { firstModelStarted.resolve(); await firstModelCanFinish.promise; } return { finishReason: "stop", providerMetadata: undefined, text: `answer ${modelInputs.length}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const firstTurn = runtime.executeTurn("first prompt"); await firstModelStarted.promise; const secondTurn = runtime.executeTurn("second prompt"); await delay(0); expect(modelInputs).toEqual(["first prompt"]); firstModelCanFinish.resolve(); await expect(firstTurn).resolves.toMatchObject({ response: "answer 1" }); await expect(secondTurn).resolves.toMatchObject({ response: "answer 2" }); expect(modelInputs).toEqual(["first prompt", "second prompt"]); }); it("keeps prompt command queues isolated per runtime", async () => { const mainModelStarted = deferred(); const mainModelCanFinish = deferred(); const mainInputs: string[] = []; const childInputs: string[] = []; const mainRuntime = createTestAgentRuntime( createSessionId("runtime-command-queue-main"), { mode: "build" }, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ async generateText(request: any) { mainInputs.push( providerContentToText(withoutAgentListingMessages(request.messages).at(-1)?.content), ); mainModelStarted.resolve(); await mainModelCanFinish.promise; return { finishReason: "stop", providerMetadata: undefined, text: "main done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const childRuntime = createTestAgentRuntime( createSessionId("runtime-command-queue-child"), { mode: "build" }, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ async generateText(request: any) { childInputs.push( providerContentToText(withoutAgentListingMessages(request.messages).at(-1)?.content), ); return { finishReason: "stop", providerMetadata: undefined, text: "child done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const mainTurn = mainRuntime.executeTurn("main prompt"); await mainModelStarted.promise; await expect(childRuntime.executeTurn("child prompt")).resolves.toMatchObject({ response: "child done", }); expect(childInputs).toEqual(["child prompt"]); expect(mainInputs).toEqual(["main prompt"]); mainModelCanFinish.resolve(); await expect(mainTurn).resolves.toMatchObject({ response: "main done" }); }); it("cancels running background tasks through the execution port", async () => { const sessionId = createSessionId("runtime-cancel-background"); const eventStore = createTestSessionEventStore(); const startedAt = new Date(0); let cancelledTaskId: string | undefined; const executionPort: ExecutionPort = { async run() { throw new Error("run should not be called"); }, async cancelBackgroundTask(taskId) { cancelledTaskId = taskId; return { taskId, status: "cancelled", startedAt, completedAt: new Date(1), pid: 1234, stdoutBytes: 5, stdoutTail: "ready", }; }, }; await eventStore.append( createSessionEvent(SessionEventType.BackgroundTaskStarted, sessionId, { cancellable: true, command: "npm run dev", startedAt, status: "running", taskId: "exec_bg", terminalId: "exec_bg", toolCallId: "tool_bash", toolName: "Bash", }), ); const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, executionPort, }, ); const result = await runtime.cancelBackgroundTask("exec_bg"); const events = await eventStore.getEvents(sessionId); const projection = await runtime.getProjection(); expect(cancelledTaskId).toBe("exec_bg"); expect(result).toMatchObject({ cancelled: true, status: "cancelled", taskId: "exec_bg", }); expect(events.some((event) => event.type === SessionEventType.BackgroundTaskUpdated)).toBe( true, ); expect(events.at(-1)).toMatchObject({ payload: { cancellable: false, stdoutBytes: 5, stdoutTail: "ready", status: "cancelled", taskId: "exec_bg", }, type: SessionEventType.BackgroundTaskCompleted, }); expect(projection.backgroundTasks[0]).toMatchObject({ cancellable: false, status: "cancelled", stdoutTail: "ready", taskId: "exec_bg", }); }); it("queues completed background Bash notifications as task-notification commands", async () => { const sessionId = createSessionId("runtime-background-bash-notification"); const eventStore = createTestSessionEventStore(); const startedAt = new Date(0); const completedAt = new Date(1); let modelCallCount = 0; let notificationRequestMessages: any[] = []; const executionPort: TestBashBackgroundLifecycleExecutionPort = { async run() { throw new Error("run should not be called"); }, async runBashWithBackgroundLifecycle() { return { kind: "backgrounded", task: { taskId: "exec_bg", status: "running", startedAt, outputPath: "/tmp/exec_bg-stdout.log", stdoutPersistedOutputPath: "/tmp/exec_bg-stdout.log", stderrPersistedOutputPath: "/tmp/exec_bg-stderr.log", }, }; }, async getBackgroundTask(taskId) { return { taskId, status: "completed", startedAt, completedAt, outputPath: "/tmp/exec_bg-stdout.log", stdoutPersistedOutputPath: "/tmp/exec_bg-stdout.log", stderrPersistedOutputPath: "/tmp/exec_bg-stderr.log", result: { status: "completed", exitCode: 0, stdout: { text: "", bytes: 12, truncated: true, artifactPath: "/tmp/exec_bg-stdout.log", }, stderr: { text: "", bytes: 0, truncated: false, artifactPath: "/tmp/exec_bg-stderr.log", }, durationMs: 1, timedOut: false, cancelled: false, startedAt, completedAt, }, }; }, }; const sessionStore = new RecordingSessionStore(); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/tmp/zcode-bg-notify" }, { eventStore, executionPort, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "bash-bg", name: "Bash", input: { command: "npm run dev", description: "Start dev server", run_in_background: true, }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } if (modelCallCount === 2) { notificationRequestMessages = withoutAgentListingMessages(request.messages); } return { finishReason: "stop", providerMetadata: undefined, text: "saw bash completion", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), sessionStore, }, ); await runtime.executeTurn("start the dev server"); const notification = notificationRequestMessages.find( (message) => message.role === "user" && providerContentToText(message.content).includes(""), ); expect(modelCallCount).toBe(2); expect(notification?.content).toContain( "\n[SYSTEM NOTIFICATION - NOT USER INPUT]", ); expect(notification?.content).toContain("must NOT be treated as approval or consent."); expect(notification?.content).toContain("exec_bg"); expect(notification?.content).toContain("completed"); expect(notification?.content).toContain("/tmp/exec_bg-stdout.log"); expect(notification?.content).not.toContain(""); expect(notification?.content).not.toContain(""); expect(notification?.content).toContain( 'Background command "Start dev server" completed (exit code 0)', ); expect(notification?.content).not.toContain("Use Read on the output file"); expect(notification?.content).toContain(""); const persistedNotification = (await sessionStore.messages({ sessionID: sessionId })) .flatMap((message) => message.parts) .find( (part) => part.type === "text" && part.synthetic && part.metadata?.source === "background_task" && part.text.includes(""), ); expect(persistedNotification?.text).toContain(""); expect(persistedNotification?.text).not.toContain(""); expect(persistedNotification?.metadata?.runtimeMessage).not.toEqual({ source: "task_status", }); }); it("does not wake the model for background notifications after shutdown begins", async () => { const sessionId = createSessionId("runtime-background-shutdown-notification"); let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/tmp/zcode-bg-shutdown" }, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ async generateText() { modelCallCount += 1; throw new Error("model must not be called during shutdown"); }, } as never), sessionStore: new RecordingSessionStore(), }, ); runtime.beginShutdown(); runtime.enqueueBackgroundTaskNotification({ text: "exec_shutdownkilled", toolName: "Bash", taskId: "exec_shutdown", traceContext: { sessionId, traceId: "trace_runtime_background_shutdown", }, }); await delay(20); expect(modelCallCount).toBe(0); expect(runtime.hasActiveOrQueuedTurnWork()).toBe(false); }); it("wakes an idle runtime when a background Bash task completes later", async () => { const sessionId = createSessionId("runtime-background-bash-idle-wake"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const startedAt = new Date(0); const completedAt = new Date(1); let backgroundCompleted = false; let modelCallCount = 0; let wakeRequestMessages: any[] = []; const executionPort: TestBashBackgroundLifecycleExecutionPort = { async run() { throw new Error("run should not be called"); }, async runBashWithBackgroundLifecycle() { return { kind: "backgrounded", task: { taskId: "exec_bg_idle", status: "running", startedAt, outputPath: "/tmp/exec_bg_idle-stdout.log", stdoutPersistedOutputPath: "/tmp/exec_bg_idle-stdout.log", stderrPersistedOutputPath: "/tmp/exec_bg_idle-stderr.log", }, }; }, async getBackgroundTask(taskId) { if (!backgroundCompleted) { return { taskId, status: "running", startedAt, outputPath: "/tmp/exec_bg_idle-stdout.log", stdoutPersistedOutputPath: "/tmp/exec_bg_idle-stdout.log", stderrPersistedOutputPath: "/tmp/exec_bg_idle-stderr.log", }; } return { taskId, status: "completed", startedAt, completedAt, outputPath: "/tmp/exec_bg_idle-stdout.log", stdoutPersistedOutputPath: "/tmp/exec_bg_idle-stdout.log", stderrPersistedOutputPath: "/tmp/exec_bg_idle-stderr.log", result: { status: "completed", exitCode: 0, stdout: { text: "", bytes: 12, truncated: true, artifactPath: "/tmp/exec_bg_idle-stdout.log", }, stderr: { text: "", bytes: 0, truncated: false, artifactPath: "/tmp/exec_bg_idle-stderr.log", }, durationMs: 1, timedOut: false, cancelled: false, startedAt, completedAt, }, }; }, }; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/tmp/zcode-bg-idle-wake" }, { eventStore, executionPort, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "bash-bg-idle", name: "Bash", input: { command: "npm run dev", description: "Start idle dev server", run_in_background: true, }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } if (modelCallCount === 3) { wakeRequestMessages = withoutAgentListingMessages(request.messages); } return { finishReason: "stop", providerMetadata: undefined, text: modelCallCount === 2 ? "background command launched" : "saw bash completion", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), sessionStore, }, ); await runtime.executeTurn("start the dev server"); expect(modelCallCount).toBe(2); backgroundCompleted = true; await waitForCondition(async () => { await delay(25); return modelCallCount === 3; }, 1_500); const notification = wakeRequestMessages.find( (message) => message.role === "user" && providerContentToText(message.content).includes(""), ); expect(notification?.content).toContain("exec_bg_idle"); expect(notification?.content).toContain("completed"); expect(notification?.content).toContain( "/tmp/exec_bg_idle-stdout.log", ); }); it("drains runtime task notifications before the next active tool-loop model request", async () => { const sessionId = createSessionId("runtime-background-notification-active-loop"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); let runtime: AgentRuntime; let modelCallCount = 0; let secondRequestMessages: any[] = []; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "NotifyDuringTool", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { (runtime as any).enqueueBackgroundTaskNotification({ originMeta: { backgroundSource: "subagent", title: "Active subagent", workId: "active-loop-agent", }, text: "\n" + "active-loop-bg\n" + "completed\n" + "Background task completed.\n" + "", traceContext: { traceId: createTraceId("trace-active-loop-bg"), spanId: "span-active-loop-bg", sessionId, }, }); return "tool-ok"; }, }); runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/tmp/zcode-active-loop-bg" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "notify-tool", name: "NotifyDuringTool", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } secondRequestMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "done after notification", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), toolRegistry: registry, }, ); await runtime.executeTurn("run tool then consume background notification"); const secondRequestText = providerMessagesToText(secondRequestMessages); expect(modelCallCount).toBe(2); expect(secondRequestText).toContain("tool-ok"); expect(secondRequestText).toContain(""); expect(secondRequestText).toContain("active-loop-bg"); const completedTurns = (await eventStore.getEvents(sessionId)) .filter((event) => event.type === SessionEventType.TurnComplete) .map( (event) => (event.payload as { backgroundSubagentResultConsumed?: boolean }) .backgroundSubagentResultConsumed, ); expect(completedTurns).toContain(true); }); it("drains a subagent message before the next active tool-loop model request", async () => { const sessionId = createSessionId("runtime-subagent-message-active-loop"); const registry = createToolRegistry(); let runtime: AgentRuntime; let modelCallCount = 0; let activeModelCalls = 0; let maxConcurrentModelCalls = 0; let secondRequestMessages: any[] = []; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "NotifyWithSubagentMessage", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { (runtime as any).enqueueSubagentMessage({ responseId: "response_active_1", agentId: "agent_progress", agentType: "general-purpose", childSessionId: createSessionId("child-active-response"), childToolCallId: "child-response-call-active", summary: "权限链路进度", message: "继续验证异常路径", traceContext: { traceId: createTraceId("trace-subagent-message-active"), sessionId, }, }); return "tool-ok"; }, }); runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ async generateText(request: any) { activeModelCalls++; maxConcurrentModelCalls = Math.max(maxConcurrentModelCalls, activeModelCalls); try { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "notify-subagent-message-tool", name: "NotifyWithSubagentMessage", input: {}, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } secondRequestMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "done after subagent response", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } finally { activeModelCalls--; } }, } as never), toolRegistry: registry, }, ); await runtime.executeTurn("run tool then consume subagent response"); const secondRequestText = providerMessagesToText(secondRequestMessages); expect(modelCallCount).toBe(2); expect(maxConcurrentModelCalls).toBe(1); expect(secondRequestText).toContain("tool-ok"); expect(secondRequestText).toContain(""); expect(secondRequestText).toContain("agent_progress"); expect(secondRequestText).toContain("继续验证异常路径"); }); it("defers a subagent message queued during first-turn setup to the outer command queue", async () => { const sessionId = createSessionId("runtime-subagent-message-first-request-race"); const turnSetupStarted = deferred(); const turnSetupMayFinish = deferred(); const requests: any[][] = []; let activeModelCalls = 0; let maxConcurrentModelCalls = 0; const runtime = createTestAgentRuntime( sessionId, {}, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ async generateText(request: any) { activeModelCalls++; maxConcurrentModelCalls = Math.max(maxConcurrentModelCalls, activeModelCalls); try { requests.push(withoutAgentListingMessages(request.messages)); return { finishReason: "stop", providerMetadata: undefined, text: "first request completed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } finally { activeModelCalls--; } }, } as never), }, ); (runtime as any).runUserPromptSubmitHooks = async () => { turnSetupStarted.resolve(); await turnSetupMayFinish.promise; return { additionalContexts: [] }; }; const turn = runtime.executeTurn("consume a response before the first request"); await turnSetupStarted.promise; (runtime as any).enqueueSubagentMessage({ responseId: "response_first_request_race", agentId: "agent_first_request_race", agentType: "general-purpose", childSessionId: createSessionId("child-first-request-race"), childToolCallId: "child-response-call-first-request-race", summary: "first request progress", message: "arrived before the first provider request", traceContext: { traceId: createTraceId("trace-subagent-message-first-request-race"), sessionId, }, }); (runtime as any).enqueueBackgroundTaskNotification({ text: [ "", "first-request-notification-stays-queued", "completed", "", ].join("\n"), traceContext: { traceId: createTraceId("trace-background-first-request-race"), sessionId, }, }); turnSetupMayFinish.resolve(); await turn; await waitForCondition( () => Promise.resolve(!(runtime as any).runtimeCommandDrainActive), 1_500, ); expect(providerMessagesToText(requests[0] ?? [])).not.toContain( "arrived before the first provider request", ); expect(providerMessagesToText(requests[0] ?? [])).not.toContain( "first-request-notification-stays-queued", ); expect(providerMessagesToText(requests[1] ?? [])).toContain( "arrived before the first provider request", ); expect(providerMessagesToText(requests[1] ?? [])).not.toContain( "first-request-notification-stays-queued", ); expect(providerMessagesToText(requests[2] ?? [])).toContain( "first-request-notification-stays-queued", ); expect(requests).toHaveLength(3); expect(maxConcurrentModelCalls).toBe(1); }); it("defers a subagent message arriving after the active-loop snapshot", async () => { const sessionId = createSessionId("runtime-subagent-message-final-intake-race"); const secondRequestPreparationStarted = deferred(); const secondRequestPreparationMayFinish = deferred(); const registry = createToolRegistry(); const requests: any[][] = []; let initializeMcpCallCount = 0; let activeModelCalls = 0; let maxConcurrentModelCalls = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "FinishRequestPreparation", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "tool-ok", }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ async generateText(request: any) { activeModelCalls++; maxConcurrentModelCalls = Math.max(maxConcurrentModelCalls, activeModelCalls); try { requests.push(withoutAgentListingMessages(request.messages)); if (requests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "finish-request-preparation", name: "FinishRequestPreparation", input: {}, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "request preparation completed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } finally { activeModelCalls--; } }, } as never), toolRegistry: registry, }, ); (runtime as any).initializeMcp = async () => { initializeMcpCallCount++; if (initializeMcpCallCount === 2) { secondRequestPreparationStarted.resolve(); await secondRequestPreparationMayFinish.promise; } }; const turn = runtime.executeTurn("run a tool before the response arrives"); await secondRequestPreparationStarted.promise; (runtime as any).enqueueSubagentMessage({ responseId: "response_final_intake_race", agentId: "agent_final_intake_race", agentType: "Explore", childSessionId: createSessionId("child-final-intake-race"), childToolCallId: "child-response-call-final-intake-race", summary: "late preparation progress", message: "arrived after the early active-loop drain", traceContext: { traceId: createTraceId("trace-subagent-message-final-intake-race"), sessionId, }, }); secondRequestPreparationMayFinish.resolve(); await turn; await waitForCondition( () => Promise.resolve(!(runtime as any).runtimeCommandDrainActive), 1_500, ); expect(providerMessagesToText(requests[1] ?? [])).not.toContain( "arrived after the early active-loop drain", ); expect(providerMessagesToText(requests[2] ?? [])).toContain( "arrived after the early active-loop drain", ); expect(requests).toHaveLength(3); expect(maxConcurrentModelCalls).toBe(1); }); it("wakes an idle parent with one model-only subagent message turn", async () => { const sessionId = createSessionId("runtime-subagent-message-idle"); const sessionStore = new RecordingSessionStore(); const requests: any[][] = []; let modelCallCount = 0; let activeModelCalls = 0; let maxConcurrentModelCalls = 0; const runtime = createTestAgentRuntime( sessionId, {}, { eventStore: createTestSessionEventStore(), sessionStore, modelFactory: createTestModelFactory({ async generateText(request: any) { activeModelCalls++; maxConcurrentModelCalls = Math.max(maxConcurrentModelCalls, activeModelCalls); try { modelCallCount++; requests.push(withoutAgentListingMessages(request.messages)); return { finishReason: "stop", providerMetadata: undefined, text: modelCallCount === 1 ? "initial done" : "response consumed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } finally { activeModelCalls--; } }, } as never), }, ); await runtime.executeTurn("initial prompt"); (runtime as any).enqueueSubagentMessage({ responseId: "response_idle_1", agentId: "agent_idle", agentType: "Explore", childSessionId: createSessionId("child-idle-response"), childToolCallId: "child-response-call-idle", summary: "idle progress", message: "parent should wake", traceContext: { traceId: createTraceId("trace-subagent-message-idle"), sessionId, }, }); await waitForCondition(() => Promise.resolve(modelCallCount === 2), 1_500); expect(modelCallCount).toBe(2); expect(maxConcurrentModelCalls).toBe(1); expect(providerMessagesToText(requests[1] ?? [])).toContain( "parent should wake", ); const persistedMessage = (sessionStore.messagesBySession.get(sessionId) ?? []).find( (message) => message.source === "subagent_message", ); const persistedPart = (sessionStore.partsBySession.get(sessionId) ?? []).find( (part) => part.messageID === persistedMessage?.id && part.type === "text" && part.metadata?.source === "subagent_message", ); expect(persistedMessage).toMatchObject({ role: "user", semantics: { kind: "subagent_notification", origin: "agent_runtime", providerVisibility: "visible", source: "subagent_message", transcriptVisibility: "hidden", uiVisibility: "hidden", }, source: "subagent_message", synthetic: true, visibility: "model-only", }); expect(persistedPart).toMatchObject({ synthetic: true, metadata: expect.objectContaining({ source: "subagent_message", visibility: "model-only", subagentMessage: expect.objectContaining({ agentId: "agent_idle", childToolCallId: "child-response-call-idle", responseId: "response_idle_1", }), }), }); }); it("preserves subagent message FIFO before a task notification in one active drain", async () => { const sessionId = createSessionId("runtime-subagent-message-active-fifo"); const registry = createToolRegistry(); let runtime: AgentRuntime; let modelCallCount = 0; let activeModelCalls = 0; let maxConcurrentModelCalls = 0; let secondRequestMessages: any[] = []; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "EnqueueOrderedRuntimeCommands", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { (runtime as any).enqueueSubagentMessage({ responseId: "response_fifo_first", agentId: "agent_fifo", agentType: "Explore", childSessionId: createSessionId("child-fifo"), childToolCallId: "child-response-call-fifo", summary: "fifo response", message: "subagent response is first", traceContext: { traceId: createTraceId("trace-subagent-fifo"), sessionId, }, }); (runtime as any).enqueueBackgroundTaskNotification({ text: [ "", "task_fifo_second", "completed", "", ].join("\n"), traceContext: { traceId: createTraceId("trace-task-fifo"), sessionId, }, }); return "commands enqueued"; }, }); runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore: createTestSessionEventStore(), toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { activeModelCalls++; maxConcurrentModelCalls = Math.max(maxConcurrentModelCalls, activeModelCalls); try { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "enqueue-runtime-commands", name: "EnqueueOrderedRuntimeCommands", input: {}, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } secondRequestMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "ordered commands consumed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } finally { activeModelCalls--; } }, } as never), }, ); await runtime.executeTurn("enqueue both runtime commands"); const requestText = providerMessagesToText(secondRequestMessages); expect(requestText.indexOf("subagent response is first")).toBeGreaterThanOrEqual(0); expect(requestText.indexOf("subagent response is first")).toBeLessThan( requestText.indexOf(""), ); expect(maxConcurrentModelCalls).toBe(1); }); it("preserves subagent message FIFO across separate idle runtime commands", async () => { const sessionId = createSessionId("runtime-subagent-message-idle-fifo"); const requests: any[][] = []; let modelCallCount = 0; let activeModelCalls = 0; let maxConcurrentModelCalls = 0; const runtime = createTestAgentRuntime( sessionId, {}, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ async generateText(request: any) { activeModelCalls++; maxConcurrentModelCalls = Math.max(maxConcurrentModelCalls, activeModelCalls); try { modelCallCount++; requests.push(withoutAgentListingMessages(request.messages)); return { finishReason: "stop", providerMetadata: undefined, text: `idle command ${modelCallCount}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } finally { activeModelCalls--; } }, } as never), }, ); await runtime.executeTurn("initial idle turn"); (runtime as any).enqueueSubagentMessage({ responseId: "response_idle_fifo", agentId: "agent_idle_fifo", agentType: "general-purpose", childSessionId: createSessionId("child-idle-fifo"), childToolCallId: "child-response-call-idle-fifo", summary: "idle fifo response", message: "idle subagent response first", traceContext: { traceId: createTraceId("trace-idle-response-fifo"), sessionId, }, }); await waitForCondition(() => Promise.resolve(modelCallCount === 2), 1_500); (runtime as any).enqueueBackgroundTaskNotification({ text: [ "", "idle_task_notification_second", "completed", "", ].join("\n"), traceContext: { traceId: createTraceId("trace-idle-task-fifo"), sessionId, }, }); await waitForCondition(() => Promise.resolve(modelCallCount === 3), 1_500); const firstResponseRequest = requests.findIndex((messages) => providerMessagesToText(messages).includes("idle subagent response first"), ); const firstNotificationRequest = requests.findIndex((messages) => providerMessagesToText(messages).includes("idle_task_notification_second"), ); expect(firstResponseRequest).toBe(1); expect(firstNotificationRequest).toBe(2); expect(maxConcurrentModelCalls).toBe(1); }); it("runs a queued subagent message after an in-flight terminal parent request", async () => { const sessionId = createSessionId("runtime-subagent-message-terminal-race"); const firstModelStarted = deferred(); const firstModelMayFinish = deferred(); const requests: any[][] = []; let modelCallCount = 0; let activeModelCalls = 0; let maxConcurrentModelCalls = 0; const runtime = createTestAgentRuntime( sessionId, {}, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ async generateText(request: any) { activeModelCalls++; maxConcurrentModelCalls = Math.max(maxConcurrentModelCalls, activeModelCalls); modelCallCount++; requests.push(withoutAgentListingMessages(request.messages)); try { if (modelCallCount === 1) { firstModelStarted.resolve(); await firstModelMayFinish.promise; } return { finishReason: "stop", providerMetadata: undefined, text: modelCallCount === 1 ? "terminal response" : "queued response consumed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } finally { activeModelCalls--; } }, } as never), }, ); const turn = runtime.executeTurn("finish without another tool roundtrip"); await firstModelStarted.promise; (runtime as any).enqueueSubagentMessage({ responseId: "response_terminal_race", agentId: "agent_terminal_race", agentType: "general-purpose", childSessionId: createSessionId("child-terminal-race"), childToolCallId: "child-response-call-terminal-race", summary: "late progress", message: "arrived during final model request", traceContext: { traceId: createTraceId("trace-subagent-message-terminal-race"), sessionId, }, }); firstModelMayFinish.resolve(); await turn; await waitForCondition(() => Promise.resolve(modelCallCount === 2), 1_500); expect(providerMessagesToText(requests[0] ?? [])).not.toContain( "arrived during final model request", ); expect(providerMessagesToText(requests[1] ?? [])).toContain( "arrived during final model request", ); expect(modelCallCount).toBe(2); expect(maxConcurrentModelCalls).toBe(1); }); it("escapes every subagent message XML field", () => { const injection = `<>&"'`; const text = formatSubagentMessage({ agentId: `agent-${injection}`, agentType: `type-${injection}`, summary: `summary-${injection}`, message: `message-${injection}`, }); expect(text.match(//gu)).toHaveLength(1); expect(text.match(/<\/subagent-message>/gu)).toHaveLength(1); expect(text).not.toContain(injection); expect(text).not.toContain(""); expect(text).toContain("<"); expect(text).toContain(">"); expect(text).toContain("&"); expect(text).toContain("""); expect(text).toContain("'"); }); it("persists successful foreground Bash cwd across tool calls", async () => { const sessionId = createSessionId("runtime-bash-cwd-persistence"); const eventStore = createTestSessionEventStore(); const initialCwd = "/tmp/zcode-runtime-initial"; const nextCwd = "/tmp/zcode-runtime-initial/next"; const requests: Parameters[0][] = []; const rootObservations: string[] = []; const registry = createToolRegistry(); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "RootProbe", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async (_input: unknown, context: ToolExecutionContext) => { rootObservations.push(context.workspaceRoot); return context.workspaceRoot; }, }); const now = new Date(); const executionPort: ExecutionPort = { async run(request) { requests.push(request); return { status: "completed", exitCode: 0, stdout: { text: "", bytes: 0, truncated: false, }, stderr: { text: "", bytes: 0, truncated: false, }, durationMs: 1, timedOut: false, cancelled: false, startedAt: now, completedAt: now, resolvedCwd: requests.length === 1 ? nextCwd : undefined, }; }, }; let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: initialCwd }, { eventStore, executionPort, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "bash-cd", name: "Bash", input: { command: `cd ${nextCwd}` }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (modelCallCount === 2) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "bash-pwd", name: "Bash", input: { command: "pwd" }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (modelCallCount === 3) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "root-probe", name: "RootProbe", input: {}, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), toolRegistry: registry, }, ); await runtime.executeTurn("change directory then show pwd"); expect(requests).toHaveLength(2); expect(requests[0]?.cwd).toBe(initialCwd); expect(requests[0]?.captureCwdAfterSuccess).toBe(true); expect(requests[1]?.cwd).toBe(nextCwd); expect(requests[1]?.captureCwdAfterSuccess).toBe(true); expect(rootObservations).toEqual([initialCwd]); expect(runtime.getProjectId()).toBe(projectIdFromDirectory(initialCwd)); }); it("resets Bash cwd to workspace root after a successful command leaves the workspace", async () => { const sessionId = createSessionId("runtime-bash-cwd-reset-reminder"); const eventStore = createTestSessionEventStore(); const workspaceRoot = "/tmp/zcode-bash-root"; const outsideCwd = "/tmp/zcode-bash-outside"; const requests: Parameters[0][] = []; let secondRequestMessages: Array<{ content?: unknown }> = []; const now = new Date(); const executionPort: ExecutionPort = { async run(request) { requests.push(request); return { status: "completed", exitCode: 0, stdout: { text: requests.length === 1 ? "left\n" : "root\n", bytes: requests.length === 1 ? 5 : 5, truncated: false, }, stderr: { text: "", bytes: 0, truncated: false, }, durationMs: 1, timedOut: false, cancelled: false, startedAt: now, completedAt: now, resolvedCwd: requests.length === 1 ? outsideCwd : undefined, }; }, }; let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: workspaceRoot }, { eventStore, executionPort, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "bash-cd-outside", name: "Bash", input: { command: `cd ${outsideCwd} && echo left` }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (modelCallCount === 2) { secondRequestMessages = withoutAgentListingMessages(request.messages); return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "bash-pwd-after-reset", name: "Bash", input: { command: "pwd" }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("leave workspace and then show pwd"); expect(requests).toHaveLength(2); expect(requests[0]?.cwd).toBe(workspaceRoot); expect(requests[1]?.cwd).toBe(workspaceRoot); expect(providerMessagesToText(secondRequestMessages)).toContain( `Shell cwd was reset to ${workspaceRoot}`, ); expect(providerMessagesToText(secondRequestMessages)).not.toContain(outsideCwd); }); it("keeps persisted message root stable after Bash cwd changes", async () => { const sessionId = createSessionId("runtime-bash-cwd-stable-message-root"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const initialCwd = "/tmp/zcode-runtime-root"; const nextCwd = "/tmp/zcode-runtime-root/subdir"; const now = new Date(); const executionPort: ExecutionPort = { async run() { return { status: "completed", exitCode: 0, stdout: { text: "", bytes: 0, truncated: false, }, stderr: { text: "", bytes: 0, truncated: false, }, durationMs: 1, timedOut: false, cancelled: false, startedAt: now, completedAt: now, resolvedCwd: nextCwd, }; }, }; let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: initialCwd }, { eventStore, executionPort, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "bash-cd", name: "Bash", input: { command: `cd ${nextCwd}` }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.executeTurn("change directory and finish"); const messagePaths = (await sessionStore.messages({ sessionID: sessionId })) .map((message) => message.info.path) .filter((path): path is NonNullable => path !== undefined); expect(messagePaths.length).toBeGreaterThan(0); expect(messagePaths.map((path) => path.root)).toEqual( Array.from({ length: messagePaths.length }, () => initialCwd), ); expect(messagePaths.some((path) => path.cwd === nextCwd)).toBe(true); }); it("fills default runtime metadata when synthetic notice metadata leaves runtimeMessage undefined", async () => { const sessionId = createSessionId("runtime-synthetic-notice-default-runtime-metadata"); const sessionStore = new RecordingSessionStore(); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-synthetic-notice-default-runtime-metadata", }, { sessionStore }, ); const messageID = createMessageId("synthetic-notice-default-runtime-metadata"); await (runtime as any).persistSyntheticUserNoticeForSession({ messageID, metadata: { runtimeMessage: undefined, custom: "kept", }, sessionId, source: "subagent", text: "Subagent finished.", traceContext: { traceId: createTraceId(), sessionId, }, }); const textPart = (await sessionStore.messages({ sessionID: sessionId })) .flatMap((message) => message.parts) .find((part) => part.type === "text" && part.messageID === messageID); expect(textPart).toMatchObject({ metadata: { custom: "kept", source: "subagent", runtimeMessage: { source: "queued_system_notification", }, }, synthetic: true, text: "Subagent finished.", type: "text", }); }); it("exposes AskUserQuestion in model tool contracts", async () => { const sessionId = createSessionId("runtime-ask-user-question-tool"); const eventStore = createTestSessionEventStore(); let toolNames: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { toolNames = request.tools.map((tool: { name: string }) => tool.name); return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); await runtime.executeTurn("ask a clarification if needed"); expect(toolNames).toContain("AskUserQuestion"); }); it("refreshes provider runtime headers before every model request in a tool loop", async () => { const sessionId = createSessionId("runtime-provider-runtime-headers-per-request"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "ReadMarker", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "marker-ok", }); const refreshes: string[] = []; let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelSelection: createTestModelSelection({ providerId: "account:zai-individual-coding-plan", modelId: "glm-5.1", }), }, { eventStore, toolRegistry: registry, providerRuntimeHeadersPort: { shouldRefreshBeforeModelRequest: ({ providerId }) => providerId === "account:zai-individual-coding-plan", async refreshBeforeModelRequest(input) { refreshes.push(`${input.providerId}:${input.reason}`); return { headersApplied: true, }; }, }, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { await observation.invocationContext?.refreshRuntimeHeadersBeforeAttempt?.({ attempt: 1, abortSignal: request.abortSignal, providerId: String(observation.model.providerId), modelId: String(observation.model.modelId), traceContext: observation.invocationContext?.traceContext, }); modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "tool-read-marker", name: "ReadMarker", input: {}, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("read marker then answer"); expect(modelCallCount).toBe(2); expect(refreshes).toEqual([ "account:zai-individual-coding-plan:model-request", "account:zai-individual-coding-plan:model-request", ]); }); it("continues an active target when idle and accounts usage", async () => { const sessionId = createSessionId("runtime-target-continue"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); let modelCallCount = 0; let capturedMessages: Array<{ role: string; content: unknown }> = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; capturedMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "continued target", usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5, }, }; }, } as never), sessionStore, }, ); await runtime.recordExternalUserPrompt("Ship target"); const target = await sessionStore.setTarget({ objective: "Ship target", sessionID: sessionId, }); const result = await runtime.continueActiveTargetIfIdle({ inputId: "client-target-run", }); const stored = await sessionStore.readTarget({ sessionID: sessionId }); const events = await eventStore.getEvents(sessionId); const visibleGoalTurnStarted = events.find( (event) => event.type === SessionEventType.TurnStarted && (event.payload as { input?: string }).input === "Ship target", ); const turnStarted = events.find( (event) => event.type === SessionEventType.TurnStarted && (event.payload as { inputSource?: string }).inputSource === "goal-continuation", ); const turnComplete = events.find( (event) => event.type === SessionEventType.TurnComplete && (event.payload as { inputId?: string }).inputId === "client-target-run", ); const targetChanged = events.find((event) => event.type === SessionEventType.TargetChanged); const persistedMessages = await sessionStore.messages({ sessionID: sessionId, }); const visibleGoalMessage = persistedMessages[0]; const continuationMessage = persistedMessages.find( (message) => message.info.role === "user" && message.info.source === "goal-continuation", ); expect(result?.response).toBe("continued target"); expect(modelCallCount).toBe(1); expect( capturedMessages.some( (message) => message.role === "user" && providerContentToText(message.content) === "Ship target", ), ).toBe(true); expect(stored).toMatchObject({ status: "active", targetID: target.targetID, tokensUsed: 5, }); expect(targetChanged?.payload).toMatchObject({ action: "usage_accounted", source: "runtime", target: { targetID: target.targetID, tokensUsed: 5, }, }); expect(turnStarted?.payload).toMatchObject({ inputId: "client-target-run", inputSource: "goal-continuation", inputVisibility: "model-only", targetId: target.targetID, }); expect(turnComplete?.payload).toMatchObject({ inputId: "client-target-run", }); expect(visibleGoalTurnStarted?.payload).toMatchObject({ input: "Ship target", messageId: visibleGoalMessage?.info.id, }); const targetContinuationInput = capturedMessages .map((message) => providerContentToText(message.content)) .find((content) => content.includes("Continue working toward the active session goal.")); expect(targetContinuationInput).toBeDefined(); expect(targetContinuationInput).toContain(""); expect(targetContinuationInput).toContain(""); expect(targetContinuationInput).not.toContain("source="); expect(visibleGoalMessage?.info).toMatchObject({ role: "user" }); expect(visibleGoalMessage?.info).not.toHaveProperty("source"); expect(visibleGoalMessage?.info).not.toHaveProperty("synthetic"); expect(visibleGoalMessage?.info).not.toHaveProperty("visibility"); expect(visibleGoalMessage).toBeDefined(); expect(messageText(visibleGoalMessage!)).toBe("Ship target"); expect(persistedMessages.indexOf(continuationMessage!)).toBeGreaterThan(0); expect(continuationMessage?.info).toMatchObject({ source: "goal-continuation", synthetic: true, visibility: "model-only", }); expect(continuationMessage?.parts[0]).toMatchObject({ metadata: { source: "goal-continuation", targetId: target.targetID, runtimeMessage: { source: "target_continuation", }, visibility: "model-only", }, synthetic: true, type: "text", }); }); it("runs the runtime-owned active target loop until verification stops it", async () => { const sessionId = createSessionId("runtime-target-loop-runtime-owned"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requestOrder: string[] = []; let verifierCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-loop" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("Verify whether the active session goal")) { verifierCount += 1; if (verifierCount === 1) { requestOrder.push("verifier-fail"); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "Continue once before completing.", passed: false, reason: "More work remains.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } requestOrder.push("verifier-pass"); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "", passed: true, reason: "The goal is complete.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Continue working toward the active session goal.")) { requestOrder.push("continuation"); return { finishReason: "stop", providerMetadata: undefined, text: "continued target", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } throw new Error(`unexpected request: ${latestText}`); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Runtime loop"); await sessionStore.setTarget({ objective: "Finish through runtime-owned loop", sessionID: sessionId, }); const result = await runtime.continueActiveTargetLoop({ trigger: "manual", verifyBeforeFirstContinue: true, }); expect(result?.response).toBe("continued target"); expect(requestOrder).toEqual(["verifier-fail", "continuation", "verifier-pass"]); }); it("prompt command can run post-turn active target loop when requested", async () => { const sessionId = createSessionId("runtime-target-loop-prompt-opt-in"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requestOrder: string[] = []; let verifierCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-loop-prompt" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("finish target")) { requestOrder.push("prompt"); return { finishReason: "stop", providerMetadata: undefined, text: "prompt done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Verify whether the active session goal")) { verifierCount += 1; if (verifierCount === 1) { requestOrder.push("verifier-fail"); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "Continue after the prompt turn.", passed: false, reason: "More work remains.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } requestOrder.push("verifier-pass"); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "", passed: true, reason: "The goal is complete.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Continue working toward the active session goal.")) { requestOrder.push("continuation"); return { finishReason: "stop", providerMetadata: undefined, text: "continued target", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } throw new Error(`unexpected request: ${latestText}`); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Prompt loop"); await sessionStore.setTarget({ objective: "Finish through prompt post-turn loop", sessionID: sessionId, }); const result = await runtime.executeTurn("finish target", undefined, { continueActiveTargetAfterTurn: true, }); expect(result.response).toBe("continued target"); expect(requestOrder).toEqual(["prompt", "verifier-fail", "continuation", "verifier-pass"]); }); it("prompt post-turn active target loop propagates continuation errors", async () => { const sessionId = createSessionId("runtime-target-loop-prompt-error"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requestOrder: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-loop-error" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("finish with continuation error")) { requestOrder.push("prompt"); return { finishReason: "stop", providerMetadata: undefined, text: "prompt done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Verify whether the active session goal")) { requestOrder.push("verifier-fail"); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "Continue after the prompt turn.", passed: false, reason: "More work remains.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Continue working toward the active session goal.")) { requestOrder.push("continuation-error"); throw new Error("continuation failed"); } throw new Error(`unexpected request: ${latestText}`); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Prompt error"); await sessionStore.setTarget({ objective: "Propagate continuation errors", sessionID: sessionId, }); const thrown = await runtime .executeTurn("finish with continuation error", undefined, { continueActiveTargetAfterTurn: true, }) .catch((error: unknown) => error); expect(thrown).toBeInstanceOf(Error); expect((thrown as Error).message).toContain("Turn execution failed"); expect((thrown as { cause?: Error }).cause?.message).toContain("continuation failed"); expect(requestOrder).toEqual(["prompt", "verifier-fail", "continuation-error"]); }); it("plain executeTurn does not run post-turn active target loop without opt-in", async () => { const sessionId = createSessionId("runtime-target-loop-prompt-no-opt-in"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requestOrder: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-loop-plain" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("plain prompt")) { requestOrder.push("prompt"); return { finishReason: "stop", providerMetadata: undefined, text: "plain done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } requestOrder.push("target-loop"); throw new Error("plain executeTurn should not run the target loop"); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Plain prompt"); await sessionStore.setTarget({ objective: "Do not auto continue without opt-in", sessionID: sessionId, }); const result = await runtime.executeTurn("plain prompt"); expect(result.response).toBe("plain done"); expect(requestOrder).toEqual(["prompt"]); }); it("manual active target loop starts without first verification", async () => { const sessionId = createSessionId("runtime-target-loop-manual-no-verify"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requestOrder: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-loop-manual", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("Verify whether the active session goal")) { requestOrder.push("verifier-pass"); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "", passed: true, reason: "Manual continuation finished the goal.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Continue working toward the active session goal.")) { requestOrder.push("continuation"); return { finishReason: "stop", providerMetadata: undefined, text: "manual continuation", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } throw new Error(`unexpected request: ${latestText}`); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Manual"); await sessionStore.setTarget({ objective: "Continue manually without verifier", sessionID: sessionId, }); const result = await runtime.continueActiveTargetLoop({ trigger: "manual", verifyBeforeFirstContinue: false, }); expect(result?.response).toBe("manual continuation"); expect(requestOrder).toEqual(["continuation", "verifier-pass"]); }); it("manual active target loop queues behind an active prompt turn", async () => { const sessionId = createSessionId("runtime-target-loop-manual-queues"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const promptStarted = deferred(); const promptMayFinish = deferred(); const requestOrder: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-loop-manual-queues", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("blocking prompt")) { requestOrder.push("prompt"); promptStarted.resolve(); await promptMayFinish.promise; return { finishReason: "stop", providerMetadata: undefined, text: "prompt done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Continue working toward the active session goal.")) { requestOrder.push("continuation"); return { finishReason: "stop", providerMetadata: undefined, text: "manual continuation", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } throw new Error(`unexpected request: ${latestText}`); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Manual queue"); await sessionStore.setTarget({ objective: "Queue manual continuation behind active prompt", sessionID: sessionId, }); const promptTurn = runtime.executeTurn("blocking prompt"); await promptStarted.promise; const continuation = runtime.continueActiveTargetLoop({ trigger: "manual", verifyBeforeFirstContinue: false, }); let continuationSettled = false; const observedContinuation = continuation.then((result) => { continuationSettled = true; return result; }); await delay(20); expect(continuationSettled).toBe(false); expect(requestOrder).toEqual(["prompt"]); promptMayFinish.resolve(); await expect(promptTurn).resolves.toMatchObject({ response: "prompt done" }); await expect(observedContinuation).resolves.toMatchObject({ response: "manual continuation", }); expect(requestOrder).toEqual(["prompt", "continuation"]); }); it("skips queued target continuation loop when the goal is paused before drain", async () => { const sessionId = createSessionId("runtime-target-loop-queued-pause"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const promptStarted = deferred(); const promptMayFinish = deferred(); const requestOrder: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-loop-queued-pause", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("blocking prompt")) { requestOrder.push("prompt"); promptStarted.resolve(); await promptMayFinish.promise; return { finishReason: "stop", providerMetadata: undefined, text: "prompt done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Continue working toward the active session goal.")) { requestOrder.push("continuation"); return { finishReason: "stop", providerMetadata: undefined, text: "should not continue paused goal", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } throw new Error(`unexpected request: ${latestText}`); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Queue pause"); await sessionStore.setTarget({ objective: "Do not continue after pause", sessionID: sessionId, }); const promptTurn = runtime.executeTurn("blocking prompt"); await promptStarted.promise; const continuation = runtime.continueActiveTargetLoop({ trigger: "manual", verifyBeforeFirstContinue: false, }); await sessionStore.updateTargetStatus({ sessionID: sessionId, status: "paused", }); promptMayFinish.resolve(); await expect(promptTurn).resolves.toMatchObject({ response: "prompt done" }); await expect(continuation).resolves.toBeNull(); expect(requestOrder).toEqual(["prompt"]); }); it("defers goal completion verification while background tasks are still running", async () => { const sessionId = createSessionId("runtime-target-defers-running-background"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry(); const requests: any[] = []; runtimeTaskRegistry.register({ agentId: "bg_goal_running", agentType: "local_bash", description: "npm run test", isBackgrounded: true, outputFile: "/tmp/bg-goal-running.log", startedAt: new Date(0), status: "running", taskId: "bg_goal_running", taskType: "local_bash", type: "local_bash", }); await eventStore.append( createSessionEvent(SessionEventType.BackgroundTaskStarted, sessionId, { cancellable: true, command: "npm run test", startedAt: new Date(0), status: "running", taskId: "bg_goal_running", terminalId: "bg_goal_running", toolCallId: "tool_bg_goal_running", toolName: "Bash", }), ); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-bg-defer" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { requests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "unexpected verifier request", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), runtimeTaskRegistry, sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Wait for background"); await sessionStore.setTarget({ objective: "Wait for background output before verification", sessionID: sessionId, }); await expect( runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true }), ).resolves.toBeNull(); expect(requests).toEqual([]); }); it("defers goal completion verification for registry-only running background Bash tasks", async () => { const sessionId = createSessionId("runtime-target-defers-registry-only-bash"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry(); const requests: any[] = []; runtimeTaskRegistry.register({ agentId: "bash_registry_only_running", agentType: "local_bash", description: "sleep 999", isBackgrounded: true, outputFile: "/tmp/bash-registry-only.log", startedAt: new Date(0), status: "running", taskId: "bash_registry_only_running", taskType: "local_bash", type: "local_bash", }); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-registry-only-bash" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { requests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: "unexpected verifier request", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), runtimeTaskRegistry, sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Wait for registry Bash"); await sessionStore.setTarget({ objective: "Wait for registry-only Bash before verification", sessionID: sessionId, }); await expect( runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true }), ).resolves.toBeNull(); expect(requests).toEqual([]); }); it("uses runtime task registry instead of stale projection for goal verification deferral", async () => { const sessionId = createSessionId("runtime-target-registry-over-stale-projection"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry(); const requests: any[] = []; await eventStore.append( createSessionEvent(SessionEventType.BackgroundTaskStarted, sessionId, { cancellable: true, command: "npm run stale", startedAt: new Date(0), status: "running", taskId: "bg_goal_stale_projection", terminalId: "bg_goal_stale_projection", toolCallId: "tool_bg_goal_stale_projection", toolName: "Bash", }), ); runtimeTaskRegistry.register({ agentId: "bg_goal_stale_projection", agentType: "local_bash", completedAt: new Date(1), description: "npm run stale", isBackgrounded: true, outputFile: "/tmp/bg-goal-stale.log", startedAt: new Date(0), status: "completed", taskId: "bg_goal_stale_projection", taskType: "local_bash", type: "local_bash", }); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-registry-stale" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { requests.push(request); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "", passed: true, reason: "Registry says no running background tasks remain.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), runtimeTaskRegistry, sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Ignore stale projection"); await sessionStore.setTarget({ objective: "Verify once registry has terminal state", sessionID: sessionId, }); await expect( runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true }), ).resolves.toBeNull(); expect(requests).toHaveLength(1); expect(providerMessagesToText(requests[0].messages)).toContain( "Verify whether the active session goal", ); }); it("continues active goals without verification while background tasks are running", async () => { const sessionId = createSessionId("runtime-target-continues-with-running-background"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requests: any[] = []; const observations: TestModelExecutionObservation[] = []; await eventStore.append( createSessionEvent(SessionEventType.BackgroundTaskStarted, sessionId, { cancellable: true, command: "npm run test", startedAt: new Date(0), status: "running", taskId: "bg_goal_continue", terminalId: "bg_goal_continue", toolCallId: "tool_bg_goal_continue", toolName: "Bash", }), ); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-bg-continue" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { requests.push(request); observations.push(observation); return { finishReason: "stop", providerMetadata: undefined, text: "continued target while background runs", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Keep working"); await sessionStore.setTarget({ objective: "Keep working while background output is pending", sessionID: sessionId, }); await expect( runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: false }), ).resolves.toMatchObject({ response: "continued target while background runs", }); expect(requests).toHaveLength(1); const requestText = providerMessagesToText(requests[0].messages); expect(requestText).toContain("Continue working toward the active session goal."); expect(requestText).not.toContain("Verify whether the active session goal"); }); it("queues active target continuation while a prompt command is entering the turn", async () => { const sessionId = createSessionId("runtime-target-queue-gate"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const sessionStartHookEntered = deferred(); const sessionStartHookCanFinish = deferred(); const modelInputs: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-gate" }, { eventStore, hookRunner: { async run(input: any) { if (input.hookEventName === HookEventName.SessionStart) { sessionStartHookEntered.resolve(); await sessionStartHookCanFinish.promise; } return { additionalContexts: [] }; }, }, modelFactory: createTestModelFactory({ async generateText(request: any) { const requestText = providerMessagesToText( withoutAgentListingMessages(request.messages), ); const isContinuation = requestText.includes( "Continue working toward the active session goal.", ); modelInputs.push(isContinuation ? "continuation" : "prompt"); return { finishReason: "stop", providerMetadata: undefined, text: isContinuation ? "continued target" : "user turn done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), sessionStore, }, ); await runtime.recordExternalUserPrompt("Prepare target"); await sessionStore.setTarget({ objective: "Prepare target", sessionID: sessionId, }); const pendingTurn = runtime.executeTurn("user prompt"); await sessionStartHookEntered.promise; const pendingContinuation = runtime.continueActiveTargetIfIdle({ inputId: "client-target-run", }); let continuationSettled = false; const observedContinuation = pendingContinuation.then((result) => { continuationSettled = true; return result; }); await delay(20); expect(continuationSettled).toBe(false); expect(modelInputs).toEqual([]); sessionStartHookCanFinish.resolve(); await expect(pendingTurn).resolves.toMatchObject({ response: "user turn done", }); await expect(observedContinuation).resolves.toMatchObject({ response: "continued target", }); expect(modelInputs).toEqual(["prompt", "continuation"]); }); it("queues background notifications behind target completion verification", async () => { const sessionId = createSessionId("runtime-target-verifier-command-queue"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const verifierStarted = deferred(); const verifierMayFinish = deferred(); const continuationMayFinish = deferred(); const notificationMayFinish = deferred(); const requestOrder: string[] = []; const verifierOutputTokenSettings: Array<{ maxOutputTokens?: number; }> = []; let verifierCount = 0; const runtime = createTestAgentRuntime( sessionId, { maxOutputTokens: 131_072, mode: "build", workingDirectory: "/tmp/zcode-runtime-target-verifier-queue", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); const isVerifier = latestText.includes("Verify whether the active session goal"); if (isVerifier) { verifierOutputTokenSettings.push({ maxOutputTokens: request.options?.maxOutputTokens, }); } const isNotification = latestText.includes(""); const kind = isVerifier ? verifierCount === 0 ? "verifier-fail" : "verifier-pass" : isNotification ? "notification" : requestOrder.length === 0 ? "initial" : "continuation"; requestOrder.push(kind); if (kind === "initial") { return { finishReason: "stop", providerMetadata: undefined, text: "checkpoint finished", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (kind === "verifier-fail") { verifierCount++; verifierStarted.resolve(); await verifierMayFinish.promise; return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "Continue with the remaining checkpoint.", passed: false, reason: "The target still has unfinished work.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (kind === "verifier-pass") { verifierCount++; return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "", passed: true, reason: "The notification result has been processed.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (kind === "continuation") { await continuationMayFinish.promise; return { finishReason: "stop", providerMetadata: undefined, text: "continued target", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } await notificationMayFinish.promise; return { finishReason: "stop", providerMetadata: undefined, text: "processed notification", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Queue verifier"); await sessionStore.setTarget({ objective: "Finish queue-sensitive target", sessionID: sessionId, }); await runtime.executeTurn("finish checkpoint"); const pendingContinuation = runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true, }); await verifierStarted.promise; runtime.enqueueBackgroundTaskNotification({ text: "bg-after-verifiercompleted", traceContext: { sessionId, traceId: "trace_bg_after_verifier", }, }); await delay(20); expect(requestOrder).toEqual(["initial", "verifier-fail"]); verifierMayFinish.resolve(); await waitForCondition(() => requestOrder.includes("continuation")); expect(requestOrder).toEqual(["initial", "verifier-fail", "continuation"]); continuationMayFinish.resolve(); await expect(pendingContinuation).resolves.toMatchObject({ response: "continued target", }); await waitForCondition(() => requestOrder.includes("notification")); notificationMayFinish.resolve(); await waitForCondition(() => requestOrder.at(-1) === "verifier-pass"); expect(requestOrder).toEqual([ "initial", "verifier-fail", "continuation", "notification", "verifier-pass", ]); expect(verifierOutputTokenSettings).toEqual([ { maxOutputTokens: 131_072 }, { maxOutputTokens: 131_072 }, ]); }); it("runs pending background notifications before goal completion verification", async () => { const sessionId = createSessionId("runtime-target-notification-before-verifier"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requestOrder: string[] = []; let verifierCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-notification-order" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("bg-notification-1")) { expect(latestText).toContain("bg-notification-2"); requestOrder.push("notification-batch"); return { finishReason: "stop", providerMetadata: undefined, text: "processed notification batch", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("bg-notification-2")) { throw new Error("second notification must not start an independent model turn"); } if (latestText.includes("Verify whether the active session goal")) { verifierCount++; if (verifierCount === 1) { requestOrder.push("verifier-fail"); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "Continue after both background notifications.", passed: false, reason: "The background results are now available.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } requestOrder.push("verifier-pass"); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "", passed: true, reason: "The goal is complete after the continuation.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } requestOrder.push("continuation"); return { finishReason: "stop", providerMetadata: undefined, text: "continued target after notifications", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Wait for notifications"); await sessionStore.setTarget({ objective: "Use both background notifications before verification", sessionID: sessionId, }); const runtimeQueueControl = runtime as unknown as { drainRuntimeCommandQueue(): Promise; runtimeCommandDrainActive: boolean; }; runtimeQueueControl.runtimeCommandDrainActive = true; runtime.enqueueBackgroundTaskNotification({ text: "bg-notification-1completed", traceContext: { sessionId, traceId: "trace_bg_notification_1" }, }); runtime.enqueueBackgroundTaskNotification({ text: "bg-notification-2completed", traceContext: { sessionId, traceId: "trace_bg_notification_2" }, }); runtimeQueueControl.runtimeCommandDrainActive = false; await runtimeQueueControl.drainRuntimeCommandQueue(); expect(requestOrder).toEqual([ "notification-batch", "verifier-fail", "continuation", "verifier-pass", ]); }); it("does not verify a cleared goal after processing a background notification", async () => { const sessionId = createSessionId("runtime-target-notification-clear-before-verifier"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requestOrder: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-notification-clear", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("bg-notification-clear")) { requestOrder.push("notification"); await sessionStore.clearTarget({ sessionID: sessionId }); return { finishReason: "stop", providerMetadata: undefined, text: "processed notification", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Verify whether the active session goal")) { requestOrder.push("verifier"); throw new Error("cleared goal should not be verified"); } requestOrder.push("continuation"); throw new Error("cleared goal should not continue"); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Clear after notification"); await sessionStore.setTarget({ objective: "Do not verify after clear", sessionID: sessionId, }); runtime.enqueueBackgroundTaskNotification({ text: "bg-notification-clearcompleted", traceContext: { sessionId, traceId: "trace_bg_notification_clear" }, }); await waitForCondition(() => requestOrder.includes("notification")); await delay(20); expect(requestOrder).toEqual(["notification"]); await expect(sessionStore.readTarget({ sessionID: sessionId })).resolves.toBeNull(); }); it("continues the post-notification goal loop until verification stops it", async () => { const sessionId = createSessionId("runtime-target-notification-goal-loop"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requestOrder: string[] = []; let verifierCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-notification-loop" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("bg-notification-loop")) { requestOrder.push("notification"); return { finishReason: "stop", providerMetadata: undefined, text: "processed notification", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Verify whether the active session goal")) { verifierCount++; if (verifierCount === 1) { requestOrder.push("verifier-fail"); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "Continue once after the notification.", passed: false, reason: "More work is needed.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } requestOrder.push("verifier-pass"); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "", passed: true, reason: "The continuation finished the goal.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } requestOrder.push("continuation"); return { finishReason: "stop", providerMetadata: undefined, text: "continued target after notification", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Finish after notification"); await sessionStore.setTarget({ objective: "Finish after background notification", sessionID: sessionId, }); runtime.enqueueBackgroundTaskNotification({ text: "bg-notification-loopcompleted", traceContext: { sessionId, traceId: "trace_bg_notification_loop" }, }); await waitForCondition(() => requestOrder.includes("verifier-pass")); expect(requestOrder).toEqual([ "notification", "verifier-fail", "continuation", "verifier-pass", ]); await expect(sessionStore.readTarget({ sessionID: sessionId })).resolves.toMatchObject({ status: "complete", }); }); it("task notification trigger yields to queued runtime commands before another target iteration", async () => { const sessionId = createSessionId("runtime-target-notification-yields-to-command"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const continuationStarted = deferred(); const continuationMayFinish = deferred(); const requestOrder: string[] = []; let verifierCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-notification-yield" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("bg-notification-yield")) { requestOrder.push("notification"); return { finishReason: "stop", providerMetadata: undefined, text: "processed notification", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Verify whether the active session goal")) { verifierCount += 1; if (verifierCount === 1) { requestOrder.push("verifier-fail"); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "Continue after notification.", passed: false, reason: "More work remains.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } requestOrder.push("verifier-pass"); return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "", passed: true, reason: "Done.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Continue working toward the active session goal.")) { requestOrder.push("continuation"); continuationStarted.resolve(); await continuationMayFinish.promise; return { finishReason: "stop", providerMetadata: undefined, text: "continued target", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("queued user prompt")) { requestOrder.push("prompt"); return { finishReason: "stop", providerMetadata: undefined, text: "queued prompt handled", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } throw new Error(`unexpected request: ${latestText}`); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Yield after notification"); await sessionStore.setTarget({ objective: "Yield queued commands after notification", sessionID: sessionId, }); runtime.enqueueBackgroundTaskNotification({ text: "bg-notification-yieldcompleted", traceContext: { sessionId, traceId: "trace_bg_notification_yield" }, }); await continuationStarted.promise; const queuedPrompt = runtime.executeTurn("queued user prompt"); await delay(20); expect(requestOrder).toEqual(["notification", "verifier-fail", "continuation"]); continuationMayFinish.resolve(); await expect(queuedPrompt).resolves.toMatchObject({ response: "queued prompt handled", }); expect(requestOrder).toEqual(["notification", "verifier-fail", "continuation", "prompt"]); }); it("task notification trigger does not continue active target when verification is disabled", async () => { const sessionId = createSessionId("runtime-target-notification-verifier-disabled"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requestOrder: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", targetCompletionVerification: { enabled: false }, workingDirectory: "/tmp/zcode-runtime-target-notification-disabled", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("bg-notification-disabled")) { requestOrder.push("notification"); return { finishReason: "stop", providerMetadata: undefined, text: "processed notification", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if ( latestText.includes("Verify whether the active session goal") || latestText.includes("Continue working toward the active session goal.") ) { requestOrder.push("target-loop"); throw new Error("notification should not start active target loop"); } throw new Error(`unexpected request: ${latestText}`); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Notification disabled"); await sessionStore.setTarget({ objective: "Do not continue after disabled notification verifier", sessionID: sessionId, }); runtime.enqueueBackgroundTaskNotification({ text: "bg-notification-disabledcompleted", traceContext: { sessionId, traceId: "trace_bg_notification_disabled" }, }); await waitForCondition(() => requestOrder.includes("notification")); await delay(20); expect(requestOrder).toEqual(["notification"]); }); it("task notification without active target only sends the notification turn", async () => { const sessionId = createSessionId("runtime-target-notification-no-goal"); const eventStore = createTestSessionEventStore(); const requests: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-notification-no-goal" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { requests.push( providerContentToText(withoutAgentListingMessages(request.messages).at(-1)?.content), ); return { finishReason: "stop", providerMetadata: undefined, text: "processed notification", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.ensureSessionPersistedForExternalActivity("initial session"); runtime.enqueueBackgroundTaskNotification({ text: "bg-no-goalcompleted", traceContext: { sessionId, traceId: "trace_bg_no_goal" }, }); await waitForCondition(() => requests.length === 1); await delay(20); expect(requests).toHaveLength(1); expect(requests[0]).toContain("bg-no-goal"); }); it("task notification target loop defers verification while another background task is running", async () => { const sessionId = createSessionId("runtime-target-notification-background-still-running"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const runtimeTaskRegistry = new InMemoryRuntimeTaskRegistry(); const requestOrder: string[] = []; runtimeTaskRegistry.register({ agentId: "bg_still_running", agentType: "local_bash", description: "npm run long", isBackgrounded: true, outputFile: "/tmp/bg-still-running.log", startedAt: new Date(0), status: "running", taskId: "bg_still_running", taskType: "local_bash", type: "local_bash", }); await eventStore.append( createSessionEvent(SessionEventType.BackgroundTaskStarted, sessionId, { cancellable: true, command: "npm run long", startedAt: new Date(0), status: "running", taskId: "bg_still_running", terminalId: "bg_still_running", toolCallId: "tool_bg_still_running", toolName: "Bash", }), ); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-bg-still-running" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); requestOrder.push( latestText.includes("") ? "notification" : latestText.includes("Verify whether the active session goal") ? "verifier" : "continuation", ); return { finishReason: "stop", providerMetadata: undefined, text: "processed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), runtimeTaskRegistry, sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Wait"); await sessionStore.setTarget({ objective: "Wait for all background work", sessionID: sessionId, }); runtime.enqueueBackgroundTaskNotification({ text: "bg-completed-onecompleted", traceContext: { sessionId, traceId: "trace_bg_completed_one" }, }); await waitForCondition(() => requestOrder.includes("notification")); await delay(20); expect(requestOrder).toEqual(["notification"]); }); it("post-turn active target loop yields when a prompt is already queued", async () => { const sessionId = createSessionId("runtime-target-prompt-post-turn-yields"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const firstPromptStarted = deferred(); const firstPromptMayFinish = deferred(); const requestOrder: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-prompt-yield" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("first prompt")) { requestOrder.push("first-prompt"); firstPromptStarted.resolve(); await firstPromptMayFinish.promise; return { finishReason: "stop", providerMetadata: undefined, text: "first done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("second prompt")) { requestOrder.push("second-prompt"); return { finishReason: "stop", providerMetadata: undefined, text: "second done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Verify whether the active session goal")) { requestOrder.push("verifier"); throw new Error("queued prompt should run before post-turn verifier"); } requestOrder.push("continuation"); throw new Error("queued prompt should run before post-turn continuation"); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Prompt yield"); await sessionStore.setTarget({ objective: "Yield to queued prompt", sessionID: sessionId, }); const firstTurn = runtime.executeTurn("first prompt", undefined, { continueActiveTargetAfterTurn: true, }); await firstPromptStarted.promise; const secondTurn = runtime.executeTurn("second prompt"); await delay(20); expect(requestOrder).toEqual(["first-prompt"]); firstPromptMayFinish.resolve(); await expect(firstTurn).resolves.toMatchObject({ response: "first done" }); await expect(secondTurn).resolves.toMatchObject({ response: "second done" }); expect(requestOrder).toEqual(["first-prompt", "second-prompt"]); }); it("keeps target continuation history and appends goal state change reminders on following user turns", async () => { const sessionId = createSessionId("runtime-target-stop-reminder-filter"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const capturedRequests: Array> = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { capturedRequests.push(withoutAgentListingMessages(request.messages)); return { finishReason: "stop", providerMetadata: undefined, text: capturedRequests.length === 1 ? "checkpoint 1" : "你好!有什么我可以帮你的吗?", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), sessionStore, }, ); await runtime.recordExternalUserPrompt("Stop target reminder"); await sessionStore.setTarget({ objective: "Stop target reminder", sessionID: sessionId, }); await runtime.continueActiveTargetIfIdle({ inputId: "client-target-run" }); await sessionStore.updateTargetStatus({ sessionID: sessionId, status: "paused", }); await runtime.recordGoalStateChangeReminder({ text: "The active session goal is paused. Do not continue pursuing it unless the user resumes or replaces the goal.", }); await runtime.executeTurn("nihao"); const goalContinuationRequestText = providerMessagesToText(capturedRequests[0] ?? []); const followupRequestText = providerMessagesToText(capturedRequests[1] ?? []); expect(goalContinuationRequestText).toContain( "Continue working toward the active session goal.", ); expect(goalContinuationRequestText).toContain(""); expect(followupRequestText).toContain("nihao"); expect(followupRequestText).toContain("Continue working toward the active session goal."); expect(followupRequestText).toContain( "The active session goal is paused. Do not continue pursuing it unless the user resumes or replaces the goal.", ); expect( followupRequestText.indexOf("Continue working toward the active session goal."), ).toBeLessThan(followupRequestText.indexOf("The active session goal is paused.")); }); it("keeps a paused goal state change after an interrupted target continuation", async () => { const sessionId = createSessionId("runtime-target-stop-reminder-without-assistant-boundary"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const continuationAbortController = new AbortController(); const continuationRequestStarted = deferred(); const capturedRequests: any[][] = []; const pauseReminder = "The active session goal is paused. Do not continue pursuing it unless the user resumes or replaces the goal."; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-stop-order" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { capturedRequests.push(withoutAgentListingMessages(request.messages)); if (capturedRequests.length === 1) { continuationRequestStarted.resolve(); await waitForAbort(request.abortSignal); throw request.abortSignal.reason ?? new Error("target continuation cancelled"); } return { finishReason: "stop", providerMetadata: undefined, text: "follow-up completed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.recordExternalUserPrompt("Interrupted target continuation"); await sessionStore.setTarget({ objective: "Interrupted target continuation", sessionID: sessionId, }); const pendingContinuation = runtime.continueActiveTargetIfIdle({ abortSignal: continuationAbortController.signal, inputId: "client-interrupted-target-run", }); await continuationRequestStarted.promise; await sessionStore.updateTargetStatus({ sessionID: sessionId, status: "paused", }); await runtime.recordGoalStateChangeReminder({ text: pauseReminder }); continuationAbortController.abort(new Error("stop active goal")); await expect(pendingContinuation).rejects.toMatchObject({ type: CoreErrorType.TurnCancelled, }); await expect(runtime.executeTurn("Follow-up after Stop")).resolves.toMatchObject({ response: "follow-up completed", }); const followupMessages = capturedRequests[1] ?? []; const followupMessageTexts = followupMessages.map((message) => providerContentToText(message.content), ); const continuationIndexes = followupMessageTexts .map((text, index) => text.includes("Continue working toward the active session goal.") ? index : -1, ) .filter((index) => index >= 0); const pauseReminderIndex = followupMessageTexts.findIndex((text) => text.includes(pauseReminder), ); const followupQueryIndex = followupMessageTexts.findIndex((text) => text.includes("Follow-up after Stop"), ); expect(capturedRequests).toHaveLength(2); expect(continuationIndexes).toHaveLength(1); expect(continuationIndexes[0]).toBeLessThan(pauseReminderIndex); expect(pauseReminderIndex).toBeLessThan(followupQueryIndex); await expect(sessionStore.readTarget({ sessionID: sessionId })).resolves.toMatchObject({ status: "paused", }); }); it("closes a tool call before materializing a goal pause when Stop lands before scheduling", async () => { const sessionId = createSessionId("runtime-goal-pause-before-tool-scheduling"); const persistenceOrder: string[] = []; const sessionStore = new GoalReminderOrderingSessionStore(persistenceOrder); const pendingToolPartPersisted = deferred(); const releasePendingToolPart = deferred(); const savePart = sessionStore.savePart.bind(sessionStore); sessionStore.savePart = async (input) => { await savePart(input); if ( input.type === "tool" && input.callID === "blocked-before-scheduling" && input.state.status === "pending" ) { pendingToolPartPersisted.resolve(); await releasePendingToolPart.promise; } }; const baseEventStore = createTestSessionEventStore(); const appendEvent = baseEventStore.append.bind(baseEventStore); baseEventStore.append = async (event) => { const stored = await appendEvent(event); if (event.type === SessionEventType.TurnComplete) { persistenceOrder.push("turn_terminal"); } return stored; }; const registry = createToolRegistry(); const abortController = new AbortController(); const capturedRequests: any[][] = []; const pauseReminder = "The active session goal is paused. Do not continue pursuing it unless the user resumes or replaces the goal."; let handlerStarted = false; let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "BlockedBeforeScheduling", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { handlerStarted = true; return "unexpected tool execution"; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-goal-pause-before-scheduling" }, { eventStore: baseEventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { capturedRequests.push(withoutAgentListingMessages(request.messages)); modelCallCount += 1; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "blocked-before-scheduling", name: "BlockedBeforeScheduling", input: {}, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "next request completed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, toolRegistry: registry, }, ); await sessionStore.setTarget({ objective: "Keep tool grammar closed before scheduling", sessionID: sessionId, status: "active", }); const pendingTurn = runtime.executeTurn("start a tool", undefined, { abortSignal: abortController.signal, }); let pendingTurnSettled = false; try { await pendingToolPartPersisted.promise; await sessionStore.updateTargetStatus({ sessionID: sessionId, status: "paused" }); await runtime.recordGoalStateChangeReminder({ text: pauseReminder }); abortController.abort(new Error("stop before tool scheduling")); releasePendingToolPart.resolve(); await expect(pendingTurn).rejects.toMatchObject({ type: CoreErrorType.TurnCancelled, }); pendingTurnSettled = true; } finally { releasePendingToolPart.resolve(); if (!abortController.signal.aborted) { abortController.abort(new Error("test cleanup")); } if (!pendingTurnSettled) { await pendingTurn.catch(() => undefined); } } const stoppedTurnPersistenceOrder = [...persistenceOrder]; await expect(runtime.executeTurn("next query after early stop")).resolves.toMatchObject({ response: "next request completed", }); const followupMessages = capturedRequests[1] ?? []; const toolResultIndex = followupMessages.findIndex( (message) => message.role === "tool" && message.toolCallId === "blocked-before-scheduling", ); const pauseReminderIndex = followupMessages.findIndex((message) => providerContentToText(message.content).includes(pauseReminder), ); expect(handlerStarted).toBe(false); expect(stoppedTurnPersistenceOrder).toEqual([ "tool_result:blocked-before-scheduling", "goal_state_change", "turn_terminal", ]); expect(toolResultIndex).toBeGreaterThanOrEqual(0); expect(pauseReminderIndex).toBeGreaterThan(toolResultIndex); }); it("closes sibling tool results before a goal pause when checkpoint creation is cancelled", async () => { const sessionId = createSessionId("runtime-goal-pause-during-tool-checkpoint"); const persistenceOrder: string[] = []; const sessionStore = new GoalReminderOrderingSessionStore(persistenceOrder); const checkpointStarted = deferred(); const artifactStore: ToolArtifactStorePort = { async writeToolResultArtifact(_request, options) { checkpointStarted.resolve(); await waitForAbort(options?.signal); throw options?.signal?.reason ?? new Error("checkpoint cancelled"); }, async readToolResultArtifact(request) { throw new Error(`unexpected artifact read: ${request.uri}`); }, }; const baseEventStore = createTestSessionEventStore(); const appendEvent = baseEventStore.append.bind(baseEventStore); baseEventStore.append = async (event) => { const stored = await appendEvent(event); if (event.type === SessionEventType.TurnComplete) { persistenceOrder.push("turn_terminal"); } return stored; }; const registry = createToolRegistry(); const abortController = new AbortController(); const capturedRequests: any[][] = []; const pauseReminder = "The active session goal is paused. Do not continue pursuing it unless the user resumes or replaces the goal."; let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "CheckpointMutation", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => ({ content: "export const value = 2;\n", filePath: "/tmp/zcode-goal-pause-checkpoint/src/checkpoint.ts", originalFile: "export const value = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const value = 1;", "+export const value = 2;"], }, ], type: "update", }), }); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "CheckpointSibling", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "sibling completed", }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/tmp/zcode-goal-pause-checkpoint" }, { artifactStore, eventStore: baseEventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { capturedRequests.push(withoutAgentListingMessages(request.messages)); modelCallCount += 1; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "checkpoint-mutation", name: "CheckpointMutation", input: {} }, { id: "checkpoint-sibling", name: "CheckpointSibling", input: {} }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "next request completed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, toolRegistry: registry, }, ); await sessionStore.setTarget({ objective: "Close sibling results around checkpoint cancellation", sessionID: sessionId, status: "active", }); const pendingTurn = runtime.executeTurn("run checkpoint siblings", undefined, { abortSignal: abortController.signal, }); let pendingTurnSettled = false; try { await checkpointStarted.promise; await sessionStore.updateTargetStatus({ sessionID: sessionId, status: "paused" }); await runtime.recordGoalStateChangeReminder({ text: pauseReminder }); abortController.abort(new Error("stop during checkpoint creation")); await expect(pendingTurn).rejects.toMatchObject({ type: CoreErrorType.TurnCancelled, }); pendingTurnSettled = true; } finally { if (!abortController.signal.aborted) { abortController.abort(new Error("test cleanup")); } if (!pendingTurnSettled) { await pendingTurn.catch(() => undefined); } } const stoppedTurnPersistenceOrder = [...persistenceOrder]; await expect(runtime.executeTurn("next query after checkpoint stop")).resolves.toMatchObject({ response: "next request completed", }); const followupMessages = capturedRequests[1] ?? []; const toolResultIndexes = followupMessages .map((message, index) => message.role === "tool" && (message.toolCallId === "checkpoint-mutation" || message.toolCallId === "checkpoint-sibling") ? index : -1, ) .filter((index) => index >= 0); const pauseReminderIndex = followupMessages.findIndex((message) => providerContentToText(message.content).includes(pauseReminder), ); expect(stoppedTurnPersistenceOrder).toEqual([ "tool_result:checkpoint-mutation", "tool_result:checkpoint-sibling", "goal_state_change", "turn_terminal", ]); expect(toolResultIndexes).toHaveLength(2); expect(pauseReminderIndex).toBeGreaterThan(Math.max(...toolResultIndexes)); }); it("keeps the Read truncation notice when Stop interrupts the checkpoint, live and after resume", async () => { const sessionId = createSessionId("runtime-read-notice-during-tool-checkpoint"); const workingDirectory = "/tmp/zcode-read-notice-checkpoint"; const sessionStore = new RecordingSessionStore(); const eventStore = createTestSessionEventStore(); const checkpointStarted = deferred(); const artifactStore: ToolArtifactStorePort = { async writeToolResultArtifact(_request, options) { checkpointStarted.resolve(); await waitForAbort(options?.signal); throw options?.signal?.reason ?? new Error("checkpoint cancelled"); }, async readToolResultArtifact(request) { throw new Error(`unexpected artifact read: ${request.uri}`); }, }; const notice = `[Truncated: PARTIAL view — ${workingDirectory}/big.txt: showing lines 1-1 of 99 total (30000 tokens, cap 25000). Do NOT answer from this page alone if the answer may be further in the file.]`; const registry = createToolRegistry(); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "CheckpointMutation", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => ({ content: "export const value = 2;\n", filePath: `${workingDirectory}/src/checkpoint.ts`, originalFile: "export const value = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const value = 1;", "+export const value = 2;"], }, ], type: "update", }), }); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "PartialRead", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, // 与 Read 相同:工具自己声明批次提示,结算不按输出形状识别。 formatBatchNotice: formatReadBatchNotice, handler: async () => ({ type: "text", filePath: `${workingDirectory}/big.txt`, content: "first line", numLines: 1, startLine: 1, totalLines: 99, truncated: true, truncatedByTokenCap: true, partialViewNotice: notice, }), }); const createRuntime = (requests: any[][]) => { let modelCallCount = 0; return createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory }, { artifactStore, eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { requests.push(withoutAgentListingMessages(request.messages)); modelCallCount += 1; if (modelCallCount === 1 && requests === liveRequests) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "checkpoint-mutation", name: "CheckpointMutation", input: {} }, { id: "partial-read", name: "PartialRead", input: {} }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "next request completed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, toolRegistry: registry, }, ); }; const liveRequests: any[][] = []; const runtime = createRuntime(liveRequests); const abortController = new AbortController(); const pendingTurn = runtime.executeTurn("read and edit", undefined, { abortSignal: abortController.signal, }); await checkpointStarted.promise; abortController.abort(new Error("stop during checkpoint creation")); await expect(pendingTurn).rejects.toMatchObject({ type: CoreErrorType.TurnCancelled }); // Stop 打断 checkpoint 时截断提示也要写入,顺序是 结果 → 提示 → 中断标记; // 否则 tool result 已在历史里,模型之后只看到被截断的正文。 const persistedOrder = (await sessionStore.messages({ sessionID: sessionId })) .flatMap((message) => message.parts) .flatMap((part) => { if (part.type === "tool" && part.state.status === "completed") return [`tool:${part.callID}`]; const source = part.type === "text" ? part.metadata?.runtimeMessage?.source : undefined; return source === "read_truncation_notice" || source === "interrupt_marker" ? [source] : []; }); expect(persistedOrder).toEqual([ "tool:checkpoint-mutation", "tool:partial-read", "read_truncation_notice", "interrupt_marker", ]); const expectNoticeBetweenResultAndMarker = (messages: any[]) => { const text = providerMessagesToText(messages); const resultIndex = text.indexOf("first line"); const noticeIndex = text.indexOf(notice); expect(resultIndex).toBeGreaterThanOrEqual(0); expect(noticeIndex).toBeGreaterThan(resultIndex); // checkpoint 被取消也属于工具阶段停止,标记用工具阶段的文字。 expect(text.indexOf("[Request interrupted by user for tool use]")).toBeGreaterThan( noticeIndex, ); }; await runtime.executeTurn("next query after checkpoint stop"); expectNoticeBetweenResultAndMarker(liveRequests[1]!); const coldRequests: any[][] = []; const cold = createRuntime(coldRequests); await cold.resumeFromStore(); await cold.executeTurn("after resume"); expectNoticeBetweenResultAndMarker(coldRequests[0]!); }); it("materializes a late active-turn goal pause before the terminal event", async () => { const sessionId = createSessionId("runtime-goal-pause-during-terminal-persistence"); const persistenceOrder: string[] = []; const sessionStore = new GoalReminderOrderingSessionStore(persistenceOrder); const terminalPersistenceStarted = deferred(); const releaseTerminalPersistence = deferred(); const baseEventStore = createTestSessionEventStore(); const appendEvent = baseEventStore.append.bind(baseEventStore); let blockNextTerminalEvent = true; baseEventStore.append = async (event) => { if (event.type === SessionEventType.TurnComplete && blockNextTerminalEvent) { blockNextTerminalEvent = false; terminalPersistenceStarted.resolve(); await releaseTerminalPersistence.promise; } const stored = await appendEvent(event); if (event.type === SessionEventType.TurnComplete) { persistenceOrder.push("turn_terminal"); } return stored; }; const abortController = new AbortController(); const capturedRequests: any[][] = []; const pauseReminder = "The active session goal is paused. Do not continue pursuing it unless the user resumes or replaces the goal."; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-goal-pause-during-terminal" }, { eventStore: baseEventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { capturedRequests.push(withoutAgentListingMessages(request.messages)); return { finishReason: "stop", providerMetadata: undefined, text: "request completed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await sessionStore.setTarget({ objective: "Persist late pause before terminal", sessionID: sessionId, status: "active", }); const pendingTurn = runtime.executeTurn("complete this turn", undefined, { abortSignal: abortController.signal, }); let pendingTurnSettled = false; try { await terminalPersistenceStarted.promise; await sessionStore.updateTargetStatus({ sessionID: sessionId, status: "paused" }); await runtime.recordGoalStateChangeReminder({ text: pauseReminder }); abortController.abort(new Error("stop while terminal event is pending")); releaseTerminalPersistence.resolve(); await pendingTurn.catch(() => undefined); pendingTurnSettled = true; } finally { releaseTerminalPersistence.resolve(); if (!abortController.signal.aborted) { abortController.abort(new Error("test cleanup")); } if (!pendingTurnSettled) { await pendingTurn.catch(() => undefined); } } const stoppedTurnPersistenceOrder = [...persistenceOrder]; await expect(runtime.executeTurn("next query after late stop")).resolves.toMatchObject({ response: "request completed", }); const followupMessages = capturedRequests[1] ?? []; const pauseReminderIndex = followupMessages.findIndex((message) => providerContentToText(message.content).includes(pauseReminder), ); expect(stoppedTurnPersistenceOrder).toEqual(["goal_state_change", "turn_terminal"]); expect(pauseReminderIndex).toBeGreaterThanOrEqual(0); }); it("materializes an active-turn goal pause reminder after cancelled sibling tool results", async () => { const sessionId = createSessionId("runtime-goal-pause-after-tool-results"); const persistenceOrder: string[] = []; const sessionStore = new GoalReminderOrderingSessionStore(persistenceOrder); const baseEventStore = createTestSessionEventStore(); const appendEvent = baseEventStore.append.bind(baseEventStore); baseEventStore.append = async (event) => { const stored = await appendEvent(event); if (event.type === SessionEventType.TurnComplete) { persistenceOrder.push("turn_terminal"); } return stored; }; const registry = createToolRegistry(); const waitingToolStarted = deferred(); const abortController = new AbortController(); const capturedRequests: any[][] = []; const pauseReminder = "The active session goal is paused. Do not continue pursuing it unless the user resumes or replaces the goal."; const resumeReminder = "The session goal is active again and will be pursued."; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "FastSibling", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "fast sibling completed", }); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "WaitForGoalPause", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async (_input, context: ToolExecutionContext) => { waitingToolStarted.resolve(); await waitForAbort(context.abortSignal); throw context.abortSignal.reason ?? new Error("tool cancelled"); }, }); let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-goal-pause-order" }, { eventStore: baseEventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { capturedRequests.push(withoutAgentListingMessages(request.messages)); modelCallCount += 1; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "fast-sibling", name: "FastSibling", input: {} }, { id: "waiting-sibling", name: "WaitForGoalPause", input: {} }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "next request completed", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, toolRegistry: registry, }, ); await sessionStore.setTarget({ objective: "Pause only after tool results close", sessionID: sessionId, status: "active", }); const pendingTurn = runtime.executeTurn("run sibling tools", undefined, { abortSignal: abortController.signal, }); let pendingTurnSettled = false; try { await waitingToolStarted.promise; await sessionStore.updateTargetStatus({ sessionID: sessionId, status: "paused" }); await runtime.recordGoalStateChangeReminder({ text: pauseReminder }); expect(persistenceOrder).not.toContain("goal_state_change"); abortController.abort(new Error("stop active goal")); await expect(pendingTurn).rejects.toMatchObject({ type: CoreErrorType.TurnCancelled, }); pendingTurnSettled = true; } finally { if (!abortController.signal.aborted) { abortController.abort(new Error("test cleanup")); } if (!pendingTurnSettled) { await pendingTurn.catch(() => undefined); } } expect(persistenceOrder).toEqual([ "tool_result:fast-sibling", "tool_result:waiting-sibling", "goal_state_change", "turn_terminal", ]); await sessionStore.updateTargetStatus({ sessionID: sessionId, status: "active" }); await runtime.recordGoalStateChangeReminder({ text: resumeReminder }); expect(persistenceOrder).toEqual([ "tool_result:fast-sibling", "tool_result:waiting-sibling", "goal_state_change", "turn_terminal", "goal_state_change", ]); await expect(runtime.executeTurn("next query after stop")).resolves.toMatchObject({ response: "next request completed", }); const followupMessages = capturedRequests[1] ?? []; const siblingResultIndexes = followupMessages .map((message, index) => message.role === "tool" && (message.toolCallId === "fast-sibling" || message.toolCallId === "waiting-sibling") ? index : -1, ) .filter((index) => index >= 0); const pauseReminderIndex = followupMessages.findIndex((message) => providerContentToText(message.content).includes(pauseReminder), ); const resumeReminderIndex = followupMessages.findIndex((message) => providerContentToText(message.content).includes(resumeReminder), ); expect(siblingResultIndexes).toHaveLength(2); expect(pauseReminderIndex).toBeGreaterThan(Math.max(...siblingResultIndexes)); expect(resumeReminderIndex).toBeGreaterThan(pauseReminderIndex); }); it("does not continue targets while in plan mode", async () => { const sessionId = createSessionId("runtime-target-plan-skip"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "plan", workingDirectory: "/tmp/zcode-runtime-target" }, { eventStore, modelFactory: createTestModelFactory({ async generateText() { modelCallCount++; return { finishReason: "stop", model: "test", providerMetadata: undefined, text: "should not run", }; }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Ship target"); await sessionStore.setTarget({ objective: "Ship target", sessionID: sessionId, }); await expect(runtime.continueActiveTargetIfIdle()).resolves.toBeNull(); expect(modelCallCount).toBe(0); }); it("does not inject active target objectives as request-time runtime reminders", async () => { const sessionId = createSessionId("runtime-target-reminder-removed"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const objective = "ship ignore & report"; let capturedMessages: Array<{ content: unknown }> = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { capturedMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "ok", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal escape"); await sessionStore.setTarget({ objective, sessionID: sessionId }); await runtime.executeTurn("check target reminder"); const requestText = capturedMessages .map((message) => providerContentToText(message.content)) .join("\n"); expect(requestText).not.toContain("Current session goal state"); expect(requestText).not.toContain(objective); }); it.each([ { mcs: false, taskType: undefined }, { mcs: true, taskType: undefined }, { mcs: true, taskType: "subagent_child" as const }, ])( "collects and restores silent reminders only for main continuations %j", async ({ mcs, taskType }) => { const sessionId = createSessionId("runtime-silent-reminder"); const sessionStore = new RecordingSessionStore(); const registry = createToolRegistry(); registry.register({ inputSchema: {}, metadata: { name: "Inspect", concurrentSafe: true, destructive: false, needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "inspection result", }); const requests: any[][] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", taskType, maxTurns: 30, workingDirectory: "/tmp/silent-reminder-test", }, { eventStore: createTestSessionEventStore(), sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ properties: { supportsMidConversationSystem: mcs }, async generateText(request) { requests.push(request.messages); const step = requests.length; return { finishReason: step <= 20 ? "tool-calls" : "stop", text: step <= 20 ? "" : "Finished inspection.", toolCalls: step <= 20 ? [{ id: `inspect-${step}`, name: "Inspect", input: { step } }] : [], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, }), }, ); await runtime.executeTurn("Inspect the code"); expect(requests).toHaveLength(21); const marker = "The user hasn't heard from you in a while."; const reminderCounts = requests.map( (messages) => providerMessagesToText(messages).split(marker).length - 1, ); expect(reminderCounts).toEqual( Array.from({ length: 21 }, (_, index) => taskType ? 0 : Math.min(3, Math.floor(index / 5)), ), ); // 静默提醒在 MCS 开启时投影为 system 消息。 expect( requests .flat() .filter((message) => message.role === "system") .some((message) => providerContentToText(message.content).includes(marker)), ).toBe(mcs && !taskType); const persisted = await sessionStore.messages({ sessionID: sessionId }); const notices = persisted .flatMap((message) => message.parts) .filter( (part) => part.type === "text" && part.metadata?.runtimeMessage?.source === "silent_turn_reminder", ); expect(notices).toHaveLength(taskType ? 0 : 3); expect( notices.every( (part) => part.type === "text" && part.synthetic && !part.text.includes(""), ), ).toBe(true); const restored = new MessageHistoryImpl(); await hydrateMessageHistoryFromSession({ history: restored, messages: persisted }); expect( restored .borrowReadOnlyRuntimeEntries() .filter((entry) => entry.metadata?.source === "silent_turn_reminder"), ).toHaveLength(taskType ? 0 : 3); await runtime.executeTurn("New user request"); expect(providerMessagesToText(requests.at(-1)!).split(marker).length - 1).toBe( taskType ? 0 : 3, ); }, ); it("does not inject todo reminders before the cadence threshold", async () => { const sessionId = createSessionId("runtime-todo-current-turn-context"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); let capturedMessages: any[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-todo-current-turn-context", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { capturedMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "todo-aware answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), sessionStore, }, ); await sessionStore.updateTodos({ sessionID: sessionId, todos: [ { content: "Keep current todos visible", priority: "high", status: "in_progress", }, ], }); await runtime.executeTurn("continue with the plan"); const contextText = providerMessagesToText(capturedMessages); const persistedTexts = (await sessionStore.messages({ sessionID: sessionId })) .flatMap((message) => message.parts) .filter((part) => part.type === "text") .map((part) => part.text); expect(contextText).not.toContain("Current session todo state (authoritative):"); expect(contextText).not.toContain("The TodoWrite tool hasn't been used recently."); expect(persistedTexts.join("\n")).not.toContain("Current session todo state (authoritative):"); expect(persistedTexts.join("\n")).not.toContain( "The TodoWrite tool hasn't been used recently.", ); }); it("injects and persists a todo reminder only when cadence is due", async () => { const sessionId = createSessionId("runtime-todo-reminder-cadence-due"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requests: any[][] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-todo-reminder-cadence" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { requests.push(withoutAgentListingMessages(request.messages)); return { finishReason: "stop", providerMetadata: undefined, text: `answer ${requests.length}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), sessionStore, }, ); await sessionStore.updateTodos({ sessionID: sessionId, todos: [ { content: "Review prompt trajectory output", priority: "high", status: "in_progress", }, ], }); for (let index = 0; index < 10; index++) { await runtime.executeTurn(`warmup ${index}`); } await runtime.executeTurn("cadence due"); await runtime.executeTurn("cadence should not repeat"); const dueRequestText = providerMessagesToText(requests[10]!); const nextRequestText = providerMessagesToText(requests[11]!); const persistedTodoReminders = (await sessionStore.messages({ sessionID: sessionId })) .flatMap((message) => message.parts) .filter((part) => part.type === "text" && part.synthetic) .filter((part) => part.metadata?.runtimeMessage?.source === "todo_reminder"); expect(dueRequestText).toContain("The TodoWrite tool hasn't been used recently."); expect(dueRequestText).toContain("1. [in_progress] Review prompt trajectory output"); expect(dueRequestText).not.toContain("Current session todo state (authoritative):"); expect(dueRequestText.match(/The TodoWrite tool hasn't been used recently\./g)).toHaveLength(1); expect(nextRequestText.match(/The TodoWrite tool hasn't been used recently\./g)).toHaveLength( 1, ); expect(persistedTodoReminders).toHaveLength(1); expect(persistedTodoReminders[0]?.text).toContain( "The TodoWrite tool hasn't been used recently.", ); expect(persistedTodoReminders[0]?.text).not.toContain(""); expect(persistedTodoReminders[0]?.text).not.toContain(""); }); it("does not inject todo reminders when TodoWrite is not exposed", async () => { const sessionId = createSessionId("runtime-todo-reminder-tool-unavailable"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requests: any[][] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", toolAllowlist: ["Read"], workingDirectory: "/tmp/zcode-todo-reminder-tool-unavailable", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { requests.push(withoutAgentListingMessages(request.messages)); return { finishReason: "stop", providerMetadata: undefined, text: `answer ${requests.length}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await sessionStore.updateTodos({ sessionID: sessionId, todos: [ { content: "Should not trigger without TodoWrite", priority: "medium", status: "pending", }, ], }); for (let index = 0; index < 11; index++) { await runtime.executeTurn(`turn ${index}`); } const finalRequestText = providerMessagesToText(requests.at(-1)!); const persistedTodoReminders = (await sessionStore.messages({ sessionID: sessionId })) .flatMap((message) => message.parts) .filter((part) => part.type === "text" && part.synthetic) .filter((part) => part.metadata?.runtimeMessage?.source === "todo_reminder"); expect(finalRequestText).not.toContain("The TodoWrite tool hasn't been used recently."); expect(finalRequestText).not.toContain("Current session todo state (authoritative):"); expect(persistedTodoReminders).toHaveLength(0); }); it("persists output style reminders like other input attachments", async () => { const sessionId = createSessionId("runtime-output-style-in-memory-history"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requests: any[][] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", outputStyle: { name: "Learning", prompt: "Explain tradeoffs while solving the task.", }, workingDirectory: "/tmp/zcode-output-style-in-memory-history", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { requests.push(withoutAgentListingMessages(request.messages)); return { finishReason: "stop", providerMetadata: undefined, text: `answer ${requests.length}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.executeTurn("first output style turn"); await runtime.executeTurn("second output style turn"); const firstRequestText = providerMessagesToText(requests[0]!); const secondRequestText = providerMessagesToText(requests[1]!); const persistedTexts = (await sessionStore.messages({ sessionID: sessionId })) .flatMap((message) => message.parts) .filter((part) => part.type === "text") .map((part) => part.text) .join("\n"); expect(firstRequestText.match(/Learning output style is active/g)).toHaveLength(1); expect(secondRequestText.match(/Learning output style is active/g)).toHaveLength(2); // output_style 与其他输入附件一样写入历史,resume 后前缀不变。 expect(persistedTexts.match(/Learning output style is active/g)).toHaveLength(2); }); it("repeats the output style reminder on every input attachment run in a turn", async () => { const sessionId = createSessionId("runtime-output-style-single-per-turn"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "ReadMarker", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "marker-ok", }); const requests: any[][] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build", outputStyle: { name: "Learning", prompt: "Explain tradeoffs while solving the task.", }, workingDirectory: "/tmp/zcode-output-style-single-per-turn", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { requests.push(withoutAgentListingMessages(request.messages)); if (requests.length === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "read-marker", name: "ReadMarker", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore: new RecordingSessionStore(), toolRegistry: registry, }, ); await runtime.executeTurn("read marker with style"); const secondRequestText = providerMessagesToText(requests[1]!); // 每次附件收集都产出 output_style,不去重;工具批次后的请求因此有两条。 expect(secondRequestText.match(/Learning output style is active/g)).toHaveLength(2); }); it("uses hydrated todo reminder metadata to avoid duplicate cadence reminders after resume", async () => { const sessionId = createSessionId("runtime-todo-reminder-resume-marker"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const firstRuntime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-todo-reminder-resume-marker", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { return { finishReason: "stop", providerMetadata: undefined, text: "first runtime answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); for (let index = 0; index < 11; index++) { await firstRuntime.executeTurn(`first runtime ${index}`); } const resumeRequests: any[][] = []; const secondRuntime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-todo-reminder-resume-marker", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { resumeRequests.push(withoutAgentListingMessages(request.messages)); return { finishReason: "stop", providerMetadata: undefined, text: "resume answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await secondRuntime.resumeFromStore(); await secondRuntime.executeTurn("after resume"); const resumeRequestText = providerMessagesToText(resumeRequests[0]!); const persistedTodoReminders = (await sessionStore.messages({ sessionID: sessionId })) .flatMap((message) => message.parts) .filter((part) => part.type === "text" && part.synthetic) .filter((part) => part.metadata?.runtimeMessage?.source === "todo_reminder"); expect(resumeRequestText.match(/The TodoWrite tool hasn't been used recently\./g)).toHaveLength( 1, ); expect(persistedTodoReminders).toHaveLength(1); }); it("marks the active goal complete when the post-turn verifier passes", async () => { const sessionId = createSessionId("runtime-target-verify-pass"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requests: any[] = []; const observations: TestModelExecutionObservation[] = []; let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { requests.push(request); observations.push(observation); modelCallCount++; if (modelCallCount === 1) { return { finishReason: "stop", providerMetadata: undefined, text: "implemented target", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (modelCallCount === 2) { return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ passed: true, reason: "Evidence covers the requested implementation and tests.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } throw new Error("unexpected extra model request"); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Verify pass"); await sessionStore.setTarget({ objective: "Ship target and run tests", sessionID: sessionId, }); const result = await runtime.executeTurn("finish target"); const continuation = await runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true, }); const stored = await sessionStore.readTarget({ sessionID: sessionId }); const events = await eventStore.getEvents(sessionId); const verifierRequest = events.find( (event) => event.type === SessionEventType.ModelRequest && (event.payload as any).querySource === GOAL_COMPLETION_VERIFICATION_QUERY_SOURCE, ); const verifierComplete = events.find( (event) => event.type === SessionEventType.ModelComplete && (event.payload as any).querySource === GOAL_COMPLETION_VERIFICATION_QUERY_SOURCE, ); const targetChanged = events.find( (event) => event.type === SessionEventType.TargetChanged && (event.payload as any).action === "status_updated", ); expect(result.response).toBe("implemented target"); expect(continuation).toBeNull(); expect(stored?.status).toBe("complete"); expect(targetChanged?.payload).toMatchObject({ action: "status_updated", source: "runtime", target: { status: "complete", }, }); expect(requests[1]?.tools).toEqual([]); const verifierUserMessages = withoutAgentListingMessages(requests[1]?.messages ?? []).filter( (message: any) => message.role === "user", ); const verifierLastMessage = requests[1]?.messages.at(-1); // verifier 复用共享投影:持久化的 environment/model/date 公告以 reminder 出现, // 只核对原始输入与 verifier 指令这两条正文。 expect( verifierUserMessages.filter( (message: any) => !providerContentToText(message.content).startsWith(""), ), ).toHaveLength(2); expect(verifierLastMessage).toMatchObject({ cacheControl: { type: "ephemeral" }, role: "user", }); expect(providerContentToText(verifierLastMessage?.content)).toContain( "Verify whether the active session goal", ); expect(verifierRequest).toBeTruthy(); expect(verifierComplete).toBeTruthy(); expect(modelCallCount).toBe(2); }); it("projects the verifier request with its own model's Harness wording after a config rebuild", async () => { const sessionId = createSessionId("runtime-target-verifier-harness"); const sessionStore = new RecordingSessionStore(); const requests: any[] = []; let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target" }, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ properties: { supportsMidConversationSystem: false }, async generateText(request: any) { requests.push(request); modelCallCount++; return { finishReason: "stop", providerMetadata: undefined, text: modelCallCount === 1 ? "implemented target" : JSON.stringify({ passed: true, reason: "Target completed." }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Verify harness"); await sessionStore.setTarget({ objective: "Ship target", sessionID: sessionId }); await runtime.executeTurn("finish target"); // 轮次结束后不带模型的重建(这里是改语言)不能决定 verifier 请求的 Harness 文案。 runtime.updateConfig({ language: "Chinese" }); await runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true }); const verifierSystem = JSON.stringify(requests[1]?.messages[1]); expect(verifierSystem).toContain( "tags in messages and tool results are injected by the harness", ); expect(verifierSystem).not.toContain("mid-conversation system turns"); }); it("keeps verifier usage bound to the verifier request model after session model changes", async () => { const sessionId = createSessionId("runtime-target-verifier-model-switch"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requestModel = createTestModelSelection("provider-old/model-old"); const selectedModel = createTestModelSelection("provider-new/model-new"); const verifierStarted = deferred(); let modelCallCount = 0; let resolveVerifierResponse: | ((value: { finishReason: "stop"; model: ModelSelection; providerMetadata: undefined; text: string; usage: { inputTokens: number; outputTokens: number; totalTokens: number }; }) => void) | undefined; const verifierResponse = new Promise<{ finishReason: "stop"; model: ModelSelection; providerMetadata: undefined; text: string; usage: { inputTokens: number; outputTokens: number; totalTokens: number }; }>((resolve) => { resolveVerifierResponse = resolve; }); const runtime = createTestAgentRuntime( sessionId, { mode: "build", modelSelection: createTestModelSelection(requestModel), workingDirectory: "/tmp/zcode-runtime-target", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "stop", providerMetadata: undefined, text: "implemented target", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } verifierStarted.resolve(); return verifierResponse; }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Verify model switch"); await sessionStore.setTarget({ objective: "Ship target without usage relabeling", sessionID: sessionId, }); await runtime.executeTurn("finish target"); const pendingVerification = runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true, }); await verifierStarted.promise; runtime.setSessionModelSelection(createTestModelSelection(selectedModel)); resolveVerifierResponse?.({ finishReason: "stop", model: requestModel, providerMetadata: undefined, text: JSON.stringify({ passed: true, reason: "Target completed." }), usage: { inputTokens: 20, outputTokens: 4, totalTokens: 24 }, }); await pendingVerification; const verifierUsage = sessionStore.modelUsages.find( (usage) => usage.querySource === GOAL_COMPLETION_VERIFICATION_QUERY_SOURCE, ); expect(verifierUsage).toMatchObject({ modelId: requestModel.modelId, providerId: requestModel.providerId, providerTotalTokens: 24, status: "completed", }); }); it("retries Start Plan 3010 before injecting verifier nextAction", async () => { vi.useFakeTimers(); const sessionId = createSessionId("runtime-target-verify-fail"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requests: any[] = []; const observations: TestModelExecutionObservation[] = []; let modelCallCount = 0; const providerId = "account:bigmodel-start-plan"; const runtime = createTestAgentRuntime( sessionId, { mode: "build", modelSelection: { modelId: "GLM-5.2" as never, providerId: providerId as never, }, workingDirectory: "/tmp/zcode-runtime-target", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { requests.push(request); observations.push(observation); modelCallCount++; if (modelCallCount === 1) { return { finishReason: "stop", providerMetadata: undefined, text: "plan finished", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (modelCallCount === 2) { throw createProviderBusyError(providerId); } if (modelCallCount === 3) { return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "Inspect the implementation and run the requested tests.", passed: false, reason: "A completed plan does not prove the target is complete.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "continuing target", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); try { await runtime.ensureSessionPersistedForExternalActivity("/goal Verify fail"); await sessionStore.setTarget({ objective: "Implement target, not just a plan", sessionID: sessionId, }); await runtime.executeTurn("finish target too early"); const resultPromise = runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true, }); await vi.advanceTimersByTimeAsync(1_000); const result = await resultPromise; const stored = await sessionStore.readTarget({ sessionID: sessionId }); const events = await eventStore.getEvents(sessionId); const nextModelContext = requests[3]?.messages .map((message: any) => message.content) .join("\n"); expect(result?.response).toBe("continuing target"); expect(modelCallCount).toBe(4); expect(observations[1]?.invocationContext?.modelRequestSessionType).toBe("main"); expect(observations[2]?.invocationContext?.modelRequestSessionType).toBe("main"); expect(stored?.status).toBe("active"); expect(nextModelContext).toContain("completion verifier"); expect(nextModelContext).toContain("A completed plan does not prove the target is complete."); expect(nextModelContext).toContain("Inspect the implementation and run the requested tests."); expect(nextModelContext).toContain("Continue working toward the active session goal."); expect(nextModelContext).not.toContain("GoalUpdate"); expect( events.some( (event) => event.type === SessionEventType.ModelComplete && (event.payload as any).querySource === GOAL_COMPLETION_VERIFICATION_QUERY_SOURCE, ), ).toBe(true); } finally { vi.useRealTimers(); } }); it("completes the goal when post-turn completion verification fails open", async () => { const sessionId = createSessionId("runtime-target-verify-error-fail-open"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); let modelCallCount = 0; const providerId = "custom-openai"; const runtime = createTestAgentRuntime( sessionId, { mode: "build", modelSelection: { modelId: "gpt-5" as never, providerId: providerId as never, }, workingDirectory: "/tmp/zcode-runtime-target", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "stop", providerMetadata: undefined, text: "turn done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } throw createProviderBusyError(providerId); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Verify error"); await sessionStore.setTarget({ objective: "Ship target after verifier error", sessionID: sessionId, }); await runtime.executeTurn("finish target before verification"); const result = await runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true, }); const stored = await sessionStore.readTarget({ sessionID: sessionId }); const events = await eventStore.getEvents(sessionId); const failedVerification = events.find( (event) => event.type === SessionEventType.TargetCompletionVerification && (event.payload as any).status === "failed_closed", ); expect(result).toBeNull(); expect(modelCallCount).toBe(2); expect(stored?.status).toBe("complete"); expect(failedVerification?.payload).toMatchObject({ goalIteration: 1, status: "failed_closed", verification: { passed: true, reason: "Completion verifier request failed: model admission concurrency limit exceeded", }, }); expect( ((failedVerification?.payload as any)?.verification as { nextAction?: string } | undefined) ?.nextAction, ).toBeUndefined(); }); it("projects historical media capability and budget before target completion verification", async () => { const sessionId = createSessionId("runtime-target-verify-video-capability"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requests: any[] = []; const generateText = async (request: any) => { requests.push(request); return { finishReason: "stop" as const, providerMetadata: undefined, text: requests.length === 1 ? "turn done" : JSON.stringify({ passed: true, reason: "Target is complete." }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target", }, { eventStore, modelFactory: ({ selection }) => createTestRuntimeModel({ generateText, inputFormat: { supportsVideo: false }, modelId: selection.modelId, providerId: selection.providerId, }), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Verify video projection"); await sessionStore.setTarget({ objective: "Verify a target with historical video", sessionID: sessionId, }); await runtime.executeTurn("finish target before verification"); const historicalImageDataUrl = `data:image/png;base64,${"A".repeat(5 * 1024 * 1024)}`; const retainedImageCount = Math.floor( DEFAULT_MODEL_REQUEST_MEDIA_BUDGET_BYTES / Buffer.byteLength(historicalImageDataUrl, "utf8"), ); runtime["messageHistory"].addUser( [ { type: "text", text: "historical video" }, { type: "video", mediaType: "video/mp4", dataUrl: "data:video/mp4;base64,dmlkZW8=", }, ...Array.from( { length: retainedImageCount + 2 }, () => ({ type: "image", mediaType: "image/png", dataUrl: historicalImageDataUrl, }) as const, ), ], { source: "real_user" }, ); await runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true }); const verificationRequest = requests[1]; expect(verificationRequest.options).toEqual({ maxOutputTokens: 32_000, }); expect( verificationRequest.messages.flatMap((message: any) => Array.isArray(message.content) ? message.content.filter((block: any) => block.type === "video") : [], ), ).toHaveLength(0); expect( verificationRequest.messages.flatMap((message: any) => Array.isArray(message.content) ? message.content.filter((block: any) => block.type === "image") : [], ), ).toHaveLength(retainedImageCount); expect(providerMessagesToText(verificationRequest.messages)).toEqual( expect.stringContaining("does not support video input"), ); expect(providerMessagesToText(verificationRequest.messages)).toEqual( expect.stringContaining("Media omitted from provider request"), ); }); it("skips post-turn verification when goal completion verification is disabled", async () => { const sessionId = createSessionId("runtime-target-verify-disabled"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", targetCompletionVerification: { enabled: false }, workingDirectory: "/tmp/zcode-runtime-target", }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "stop", providerMetadata: undefined, text: "turn done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "direct complete", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Verify disabled"); await sessionStore.setTarget({ objective: "Ship direct target", sessionID: sessionId, }); await runtime.executeTurn("finish target with verifier disabled"); const result = await runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true, }); const stored = await sessionStore.readTarget({ sessionID: sessionId }); const events = await eventStore.getEvents(sessionId); expect(result?.response).toBe("direct complete"); expect(stored?.status).toBe("active"); expect(modelCallCount).toBe(2); expect( events.some( (event) => event.type === SessionEventType.ModelRequest && (event.payload as any).querySource === GOAL_COMPLETION_VERIFICATION_QUERY_SOURCE, ), ).toBe(false); }); it("pauses an active target when its turn is cancelled", async () => { const sessionId = createSessionId("runtime-target-cancel"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const abortController = new AbortController(); const modelStarted = deferred(); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelStarted.resolve(); await waitForAbort(request.abortSignal); throw request.abortSignal.reason ?? new Error("model cancelled"); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Ship target"); await sessionStore.setTarget({ objective: "Ship target", sessionID: sessionId, }); const pending = runtime.executeTurn("work toward target", undefined, { abortSignal: abortController.signal, }); await modelStarted.promise; abortController.abort(new Error("test cancelled")); await expect(pending).rejects.toMatchObject({ type: CoreErrorType.TurnCancelled, }); await expect(sessionStore.readTarget({ sessionID: sessionId })).resolves.toMatchObject({ status: "paused", }); }); it("pauses an active target when the post-turn verifier is cancelled", async () => { const sessionId = createSessionId("runtime-target-verifier-cancel"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const abortController = new AbortController(); const verifierStarted = deferred(); let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "stop", providerMetadata: undefined, text: "checkpoint 1", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } verifierStarted.resolve(); await waitForAbort(request.abortSignal); throw request.abortSignal.reason ?? new Error("verifier cancelled"); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal checkpoint flow"); await sessionStore.setTarget({ objective: "Complete four checkpoints", sessionID: sessionId, }); await runtime.executeTurn("start checkpoint flow"); const pending = runtime.continueActiveTargetIfIdle({ abortSignal: abortController.signal, verifyBeforeContinue: true, }); await verifierStarted.promise; abortController.abort(new Error("test verifier cancelled")); await expect(pending).rejects.toThrow("test verifier cancelled"); await expect(sessionStore.readTarget({ sessionID: sessionId })).resolves.toMatchObject({ status: "paused", }); const events = await eventStore.getEvents(sessionId); expect( events.some( (event) => event.type === SessionEventType.TargetCompletionVerification && (event.payload as any).status === "cancelled", ), ).toBe(true); expect( events.some( (event) => event.type === SessionEventType.TargetChanged && (event.payload as any).target?.status === "paused", ), ).toBe(true); expect(modelCallCount).toBe(2); }); it("stops an active verifier through the runtime-owned foreground execution scope", async () => { const sessionId = createSessionId("runtime-target-verifier-runtime-stop"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const verifierStarted = deferred(); let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "stop", providerMetadata: undefined, text: "checkpoint 1", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (modelCallCount === 2) { verifierStarted.resolve(); await waitForAbort(request.abortSignal); throw request.abortSignal.reason ?? new Error("verifier cancelled"); } return { finishReason: "stop", providerMetadata: undefined, text: "fresh request after runtime-owned verifier stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal checkpoint flow"); await sessionStore.setTarget({ objective: "Complete four checkpoints", sessionID: sessionId, }); await runtime.executeTurn("start checkpoint flow"); const pending = runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true, }); await verifierStarted.promise; const startedEvent = (await eventStore.getEvents(sessionId)).find( (event) => event.type === SessionEventType.TargetCompletionVerification && (event.payload as any).status === "started", ); const foregroundExecutionId = (startedEvent?.payload as any)?.foregroundExecutionId as | string | undefined; expect(foregroundExecutionId).toBeTruthy(); expect(runtime.getActiveForegroundExecutionId()).toBe(foregroundExecutionId); expect( runtime.stopActiveForegroundExecution({ expectedForegroundExecutionId: "stale-foreground", }), ).toMatchObject({ kind: "mismatch", activeForegroundExecutionId: foregroundExecutionId, }); expect( runtime.stopActiveForegroundExecution({ expectedForegroundExecutionId: foregroundExecutionId, preserveQueueAutoDrainOnCancel: true, reason: "test runtime verifier stop", }), ).toEqual({ kind: "stopped", foregroundExecutionId }); // abort 只是发出停止请求;foreground authority 要到 runtime command 的 finally // 才真正释放,Bootstrap 的 Send now barrier 必须能观察这个区间。 expect(runtime.getActiveForegroundExecutionId()).toBe(foregroundExecutionId); await expect(pending).rejects.toThrow("test runtime verifier stop"); expect(runtime.getActiveForegroundExecutionId()).toBeUndefined(); await expect(sessionStore.readTarget({ sessionID: sessionId })).resolves.toMatchObject({ status: "paused", }); const events = await eventStore.getEvents(sessionId); expect( events.find( (event) => event.type === SessionEventType.TargetCompletionVerification && (event.payload as any).status === "cancelled" && (event.payload as any).foregroundExecutionId === foregroundExecutionId, )?.payload, ).toMatchObject({ preserveQueueAutoDrainOnCancel: true, status: "cancelled", }); await expect( runtime.executeTurn("new prompt after runtime-owned verifier stop", undefined, { continueActiveTargetAfterTurn: true, }), ).resolves.toMatchObject({ response: "fresh request after runtime-owned verifier stop", }); expect(modelCallCount).toBe(3); }); it("does not continue a goal when it is paused while the post-turn verifier is running", async () => { const sessionId = createSessionId("runtime-target-verifier-stop-race"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const verifierStarted = deferred(); const verifierMayFinish = deferred(); let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "stop", providerMetadata: undefined, text: "checkpoint done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (modelCallCount === 2) { verifierStarted.resolve(); await verifierMayFinish.promise; return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "Continue with the next checkpoint.", passed: false, reason: "The final checkpoint is not complete yet.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "should not continue after stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Race with stop"); await sessionStore.setTarget({ objective: "Complete checkpoint flow", sessionID: sessionId, }); await runtime.executeTurn("finish checkpoint"); const pending = runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true, }); await verifierStarted.promise; await sessionStore.updateTargetStatus({ sessionID: sessionId, status: "paused", }); verifierMayFinish.resolve(); await expect(pending).resolves.toBeNull(); await expect(sessionStore.readTarget({ sessionID: sessionId })).resolves.toMatchObject({ status: "paused", }); expect(modelCallCount).toBe(2); }); it("does not continue the old goal when it is replaced while the verifier is running", async () => { const sessionId = createSessionId("runtime-target-verifier-replace-race"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const verifierStarted = deferred(); const verifierMayFinish = deferred(); const requestOrder: string[] = []; const initialTarget: SessionGoal = { sessionID: sessionId, targetID: "target_replace_old", objective: "Old objective", summaryTitle: null, status: "active", tokenBudget: null, tokensUsed: 0, timeUsedSeconds: 0, time: { created: 1, updated: 1 }, }; const replacementTarget: SessionGoal = { ...initialTarget, targetID: "target_replace_new", objective: "Replacement objective", time: { created: 2, updated: 2 }, }; const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-runtime-target-replace-race" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const latestText = providerContentToText( withoutAgentListingMessages(request.messages).at(-1)?.content, ); if (latestText.includes("Verify whether the active session goal")) { requestOrder.push("verifier-old"); verifierStarted.resolve(); await verifierMayFinish.promise; return { finishReason: "stop", providerMetadata: undefined, text: JSON.stringify({ nextAction: "Continue the old objective.", passed: false, reason: "The old target is not complete.", }), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (latestText.includes("Continue working toward the active session goal.")) { requestOrder.push( latestText.includes("Replacement objective") ? "continuation-new" : "continuation-old", ); return { finishReason: "stop", providerMetadata: undefined, text: "continued replacement goal", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } throw new Error(`unexpected request: ${latestText}`); }, } as never), sessionStore, }, ); await runtime.ensureSessionPersistedForExternalActivity("/goal Replace during verifier"); sessionStore.targets.set(sessionId, initialTarget); const pendingOldContinuation = runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: true, }); await verifierStarted.promise; sessionStore.targets.set(sessionId, replacementTarget); verifierMayFinish.resolve(); await expect(pendingOldContinuation).resolves.toBeNull(); const replacementContinuation = await runtime.continueActiveTargetIfIdle({ verifyBeforeContinue: false, }); expect(replacementContinuation?.response).toBe("continued replacement goal"); expect(requestOrder).toEqual(["verifier-old", "continuation-new"]); }); it("appends a plan-mode reminder at request time without filtering tool contracts", async () => { const sessionId = createSessionId("runtime-plan-mode-reminder"); const eventStore = createTestSessionEventStore(); let capturedMessages: any[] = []; let toolNames: string[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "plan" }, { eventStore, modelFactory: createTestModelFactory({ properties: { supportsMidConversationSystem: true }, async generateText(request: any) { capturedMessages = withoutAgentListingMessages(request.messages); toolNames = request.tools.map((tool: { name: string }) => tool.name); return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); await runtime.executeTurn("看看项目里是啥"); const events = await eventStore.getEvents(sessionId); const modelRequestEvent = events.find((event) => event.type === SessionEventType.ModelRequest); const lastMessage = capturedMessages.at(-1); const userMessages = capturedMessages.filter((message) => message.role === "user"); const realUserMessage = userMessages.at(-1); const planModeReminder = lastMessage; const realUserContent = realUserMessage?.content; const planModeReminderText = providerContentToText(planModeReminder?.content); expect(toolNames).toContain("Bash"); expect(planModeReminder).toMatchObject({ role: "system" }); expect(userMessages).toHaveLength(1); expect( withoutAgentListingMessages((modelRequestEvent?.payload as { messages: any[] }).messages), ).toEqual(capturedMessages); expect(realUserMessage?.cacheControl).toEqual({ type: "ephemeral" }); expect(planModeReminderText).not.toContain(""); expect(planModeReminderText).toContain("Plan mode is active"); expect(planModeReminderText).toContain("MUST NOT make any edits"); expect(realUserContent).toBe("看看项目里是啥"); }); it("CronCreate 达到 20 个上限后取消后续工具并只进行一次无工具提示", async () => { const sessionId = createSessionId("runtime-automation-create-limit-text-only"); const eventStore = createTestSessionEventStore(); const create = vi.fn(async () => { throw new AutomationCreateLimitError( "[AUTOMATION_CREATE_LIMIT_REACHED] At most 20 automations may be retained. Delete an existing automation before creating another.", ); }); const list = vi.fn(async () => []); const update = vi.fn(); const deleteAutomation = vi.fn(async () => undefined); const automationPort: AutomationPort = { create, list, update, delete: deleteAutomation, pause: vi.fn(async () => false), }; let modelCallCount = 0; let secondRequest: any; const permissionBroker: PermissionBrokerPort = { async requestPermission() { return { decision: "allow" }; }, }; const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { automationPort, eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "正在创建。", toolCalls: [ { id: "create-at-limit", input: { cron: "0 9 * * *", prompt: "生成晨报", title: "每天 9 点晨报", }, name: "CronCreate", }, { id: "delete-after-limit", input: { id: "automation-existing" }, name: "CronDelete", }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } secondRequest = request; return { finishReason: "stop", providerMetadata: undefined, text: "定时任务已达到 20 个上限,本次未创建。请前往“自动化”手动删除一个已有任务后重试。", toolCalls: [ { id: "delete-hallucinated-after-limit", input: { id: "automation-existing" }, name: "CronDelete", }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), permissionBroker, }, ); const result = await runtime.executeTurn("每天 9 点生成晨报"); expect(result.response).toContain("达到 20 个上限"); expect(modelCallCount).toBe(2); expect(create).toHaveBeenCalledTimes(1); expect(list).not.toHaveBeenCalled(); expect(deleteAutomation).not.toHaveBeenCalled(); expect(secondRequest.tools).toEqual([]); const secondRequestText = providerMessagesToText(secondRequest.messages); expect(secondRequestText).toContain("cannot be recovered automatically"); expect(secondRequestText).not.toContain( "Delete an existing automation before creating another", ); }); it("automation turn 的异常 provider 直接提交写工具时仍由 handler 拒绝", async () => { const sessionId = createSessionId("runtime-automation-handler-guard"); const eventStore = createTestSessionEventStore(); const automationPort: AutomationPort = { create: vi.fn(), list: vi.fn(async () => []), update: vi.fn(), delete: vi.fn(), pause: vi.fn(), }; let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { automationPort, eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { expect( request.tools .map((tool: { name: string }) => tool.name) .filter((name: string) => name.startsWith("Cron")), ).toEqual(["CronList", "CronPauseSelf"]); // 模拟 provider 无视声明工具面,直接返回三种 mutation tool call。 return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "blocked-create", input: { cron: "0 9 * * *", delayMinutes: null, prompt: "report", title: "daily report", }, name: "CronCreate", }, { id: "blocked-update", input: { id: "automation-1", prompt: "new report", title: "new report" }, name: "CronUpdate", }, { id: "blocked-delete", input: { id: "automation-1" }, name: "CronDelete", }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), permissionBroker: { async requestPermission() { return { decision: "allow" }; }, }, }, ); await runtime.executeTurn("run scheduled report", undefined, { automationId: "automation-1", toolDisallowlist: ["CronCreate", "CronUpdate", "CronDelete"], }); expect(automationPort.create).not.toHaveBeenCalled(); expect(automationPort.update).not.toHaveBeenCalled(); expect(automationPort.delete).not.toHaveBeenCalled(); const errors = (await eventStore.getEvents(sessionId)).filter( (event) => event.type === SessionEventType.ToolCallError, ); expect(errors).toHaveLength(3); expect(errors.map((event) => event.payload)).toEqual( expect.arrayContaining([ expect.objectContaining({ error: expect.objectContaining({ type: CoreErrorType.PermissionDenied }), }), ]), ); }); it("automation turn 的 CronPauseSelf 只暂停 Host 准入注入的本轮 automationId", async () => { const sessionId = createSessionId("runtime-automation-pause-self"); const eventStore = createTestSessionEventStore(); const automationPort: AutomationPort = { create: vi.fn(), list: vi.fn(async () => []), update: vi.fn(), delete: vi.fn(), pause: vi.fn(async () => true), }; let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { automationPort, eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { expect(request.tools.map((tool: { name: string }) => tool.name)).toContain( "CronPauseSelf", ); return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "pause-self", input: {}, name: "CronPauseSelf" }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "paused", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), permissionBroker: { async requestPermission() { return { decision: "allow" }; }, }, }, ); await runtime.executeTurn("stop when the condition is met", undefined, { automationId: "automation-1", toolDisallowlist: ["CronCreate", "CronUpdate", "CronDelete"], }); // 暂停目标只能是 loop state 的 automationId,模型输入为空对象。 expect(automationPort.pause).toHaveBeenCalledTimes(1); expect(automationPort.pause).toHaveBeenCalledWith({ id: "automation-1" }); const errors = (await eventStore.getEvents(sessionId)).filter( (event) => event.type === SessionEventType.ToolCallError, ); expect(errors).toHaveLength(0); }); it("普通用户轮即使 provider 直接提交 CronPauseSelf 也由 handler 拒绝且不调用端口", async () => { const sessionId = createSessionId("runtime-user-turn-pause-self-guard"); const eventStore = createTestSessionEventStore(); const automationPort: AutomationPort = { create: vi.fn(), list: vi.fn(async () => []), update: vi.fn(), delete: vi.fn(), pause: vi.fn(async () => true), }; let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { automationPort, eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { // 普通用户轮没有本轮 automationId,provider 工具面不得出现 CronPauseSelf。 expect(request.tools.map((tool: { name: string }) => tool.name)).not.toContain( "CronPauseSelf", ); return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "pause-self-blocked", input: {}, name: "CronPauseSelf" }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), permissionBroker: { async requestPermission() { return { decision: "allow" }; }, }, }, ); await runtime.executeTurn("pause my automation"); expect(automationPort.pause).not.toHaveBeenCalled(); const errors = (await eventStore.getEvents(sessionId)).filter( (event) => event.type === SessionEventType.ToolCallError, ); expect(errors).toHaveLength(1); expect(errors[0]!.payload).toEqual( expect.objectContaining({ error: expect.objectContaining({ type: CoreErrorType.PermissionDenied }), }), ); }); it.each(["build", "yolo"] as const)( "stops the turn after ExitPlanMode is denied with %s + Plan", async (mode) => { const sessionId = createSessionId("runtime-plan-exit-denied-stops"); const eventStore = createTestSessionEventStore(); let modelCallCount = 0; let permissionRequestCount = 0; const permissionBroker: PermissionBrokerPort = { async requestPermission(request) { permissionRequestCount++; expect(request.toolName).toBe("ExitPlanMode"); return { decision: "deny" }; }, }; const runtime = createTestAgentRuntime( sessionId, { mode, planEnabled: true }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "Here is the proposed plan.", toolCalls: [ { id: "exit-plan-denied", input: { plan: "1. Change the runtime\n2. Add tests" }, name: "ExitPlanMode", }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "This continuation should not happen.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), permissionBroker, }, ); const result = await runtime.executeTurn("plan first"); const events = await eventStore.getEvents(sessionId); expect(result.response).toBe("Here is the proposed plan."); expect(modelCallCount).toBe(1); expect(permissionRequestCount).toBe(1); expect(runtime.getMode()).toBe(mode); expect(runtime.getPlanEnabled()).toBe(true); expect(events.filter((event) => event.type === SessionEventType.ModelRequest)).toHaveLength( 1, ); expect( events.filter((event) => event.type === SessionEventType.PermissionResolved), ).toHaveLength(1); }, ); it("does not execute later tool groups after ExitPlanMode requests turn stop", async () => { const sessionId = createSessionId("runtime-plan-exit-denied-skips-later-groups"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); let modelCallCount = 0; let permissionRequestCount = 0; let readHandlerRan = false; let followUpRequest: any; const permissionBroker: PermissionBrokerPort = { async requestPermission(request) { permissionRequestCount++; expect(request.toolName).toBe("ExitPlanMode"); return { decision: "deny" }; }, }; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "ReadLike", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { readHandlerRan = true; return "read-ok"; }, } as any); const runtime = createTestAgentRuntime( sessionId, { mode: "plan" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "Here is the proposed plan.", toolCalls: [ { id: "exit-plan-denied-before-read", input: { plan: "1. Change the runtime\n2. Add tests" }, name: "ExitPlanMode", }, { id: "read-like-after-denied-exit", input: {}, name: "ReadLike" }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } followUpRequest = request; return { finishReason: "stop", providerMetadata: undefined, text: "This continuation should not happen.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), permissionBroker, toolRegistry: registry, }, ); const result = await runtime.executeTurn("plan first"); const events = await eventStore.getEvents(sessionId); expect(result.response).toBe("Here is the proposed plan."); expect(modelCallCount).toBe(1); expect(permissionRequestCount).toBe(1); expect(readHandlerRan).toBe(false); expect( events.some( (event) => event.type === SessionEventType.ToolCallStarted && (event.payload as any).toolCallId === "read-like-after-denied-exit", ), ).toBe(false); await runtime.executeTurn("review again"); const toolMessages = followUpRequest.messages.filter((message: any) => message.role === "tool"); expect(toolMessages.map((message: any) => message.toolCallId)).toEqual([ "exit-plan-denied-before-read", "read-like-after-denied-exit", ]); expect(toolMessages[1].content).toContain("turn stop"); }); it("stops the plan turn after ExitPlanMode is denied with a non-feedback reason", async () => { const sessionId = createSessionId("runtime-plan-exit-policy-denied-stops"); const eventStore = createTestSessionEventStore(); let modelCallCount = 0; let permissionRequestCount = 0; const permissionBroker: PermissionBrokerPort = { async requestPermission(request) { permissionRequestCount++; expect(request.toolName).toBe("ExitPlanMode"); return { decision: "deny", reason: "Blocked by project policy.", }; }, }; const runtime = createTestAgentRuntime( sessionId, { mode: "plan" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "Here is the proposed plan.", toolCalls: [ { id: "exit-plan-policy-denied", input: { plan: "1. Change the runtime\n2. Add tests" }, name: "ExitPlanMode", }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "This continuation should not happen.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), permissionBroker, }, ); const result = await runtime.executeTurn("plan first"); const events = await eventStore.getEvents(sessionId); expect(result.response).toBe("Here is the proposed plan."); expect(modelCallCount).toBe(1); expect(permissionRequestCount).toBe(1); expect(runtime.getMode()).toBe("build"); expect(runtime.getPlanEnabled()).toBe(true); expect(events.filter((event) => event.type === SessionEventType.ModelRequest)).toHaveLength(1); expect( events.filter((event) => event.type === SessionEventType.PermissionResolved), ).toHaveLength(1); }); it("continues the plan turn after ExitPlanMode is denied with feedback", async () => { const sessionId = createSessionId("runtime-plan-exit-feedback-continues"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); let modelCallCount = 0; let permissionRequestCount = 0; let secondRequestMessages: any[] = []; const feedback = "Please add tests before implementation."; const permissionBroker: PermissionBrokerPort = { async requestPermission(request) { permissionRequestCount++; expect(request.toolName).toBe("ExitPlanMode"); return { decision: "deny", reason: feedback, reasonSource: "plan_approval_feedback", }; }, }; const runtime = createTestAgentRuntime( sessionId, { mode: "plan" }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "Here is the proposed plan.", toolCalls: [ { id: "exit-plan-feedback", input: { plan: "1. Change the runtime\n2. Add tests" }, name: "ExitPlanMode", }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } secondRequestMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "I will revise the plan with tests first.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), permissionBroker, }, ); const result = await runtime.executeTurn("plan first"); const events = await eventStore.getEvents(sessionId); const persistedMessages = await sessionStore.messages({ sessionID: sessionId }); const secondRequestText = providerMessagesToText(secondRequestMessages); const exitPlanToolResult = secondRequestMessages.find( (message) => message.role === "tool" && message.toolCallId === "exit-plan-feedback", ); const feedbackUserMessage = secondRequestMessages.at(-1); const persistedUserTexts = persistedMessages .filter((message) => message.info.role === "user") .flatMap((message) => message.parts) .filter((part) => part.type === "text") .map((part) => part.text); expect(result.response).toBe("I will revise the plan with tests first."); expect(modelCallCount).toBe(2); expect(permissionRequestCount).toBe(1); expect(runtime.getMode()).toBe("build"); expect(runtime.getPlanEnabled()).toBe(true); expect(providerContentToText(exitPlanToolResult?.content)).toContain( "The plan was not approved by the user.", ); expect(providerContentToText(exitPlanToolResult?.content)).not.toContain(feedback); expect(feedbackUserMessage).toMatchObject({ role: "user" }); expect(providerContentToText(feedbackUserMessage?.content)).toBe(feedback); expect(secondRequestText).toContain(feedback); expect(persistedUserTexts).toContain("plan first"); expect(persistedUserTexts).toContain(feedback); expect(events.filter((event) => event.type === SessionEventType.ModelRequest)).toHaveLength(2); const turnSteerQueued = events.find((event) => event.type === SessionEventType.TurnSteerQueued); const turnSteerDrained = events.find( (event) => event.type === SessionEventType.TurnSteerDrained, ); expect(turnSteerQueued?.payload).toMatchObject({ source: "plan_approval_feedback", }); expect(turnSteerDrained?.payload).not.toHaveProperty("sources"); expect( events.filter((event) => event.type === SessionEventType.PermissionResolved), ).toHaveLength(1); }); it("does not promise denied ExitPlanMode feedback follows when steering rejects it", async () => { const sessionId = createSessionId("runtime-plan-exit-feedback-too-large"); const eventStore = createTestSessionEventStore(); let modelCallCount = 0; let permissionRequestCount = 0; let secondRequestMessages: any[] = []; const feedback = "x".repeat(MAX_TURN_STEER_INPUT_BYTES + 1); const permissionBroker: PermissionBrokerPort = { async requestPermission(request) { permissionRequestCount++; expect(request.toolName).toBe("ExitPlanMode"); return { decision: "deny", reason: feedback, reasonSource: "plan_approval_feedback", }; }, }; const runtime = createTestAgentRuntime( sessionId, { mode: "plan" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "Here is the proposed plan.", toolCalls: [ { id: "exit-plan-feedback-too-large", input: { plan: "1. Change the runtime\n2. Add tests" }, name: "ExitPlanMode", }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } secondRequestMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "I will ask for shorter feedback.", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), permissionBroker, }, ); const result = await runtime.executeTurn("plan first"); const events = await eventStore.getEvents(sessionId); const exitPlanToolResult = secondRequestMessages.find( (message) => message.role === "tool" && message.toolCallId === "exit-plan-feedback-too-large", ); const feedbackUserMessage = secondRequestMessages.find( (message) => message.role === "user" && providerContentToText(message.content) === feedback, ); const rejection = events.find((event) => event.type === SessionEventType.TurnSteerRejected); expect(result.response).toBe("I will ask for shorter feedback."); expect(modelCallCount).toBe(2); expect(permissionRequestCount).toBe(1); expect(providerContentToText(exitPlanToolResult?.content)).toContain( "The plan was not approved by the user.", ); expect(providerContentToText(exitPlanToolResult?.content)).not.toContain("feedback follows"); expect(feedbackUserMessage).toBeUndefined(); expect(rejection?.payload).toMatchObject({ reason: "input_too_large", }); }); it("executes tools through the schedule and emits scheduling events", async () => { const sessionId = createSessionId("runtime-tool-schedule"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); let slowReadCompleted = false; let writeSawSlowReadCompleted = false; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "SlowRead", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { await delay(10); slowReadCompleted = true; return "read-ok"; }, }); registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteLike", needsApproval: true, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { writeSawSlowReadCompleted = slowReadCompleted; return "write-ok"; }, }); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "FastRead", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "fast-ok", }); let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "slow-read", name: "SlowRead", input: {} }, { id: "write", name: "WriteLike", input: {} }, { id: "fast-read", name: "FastRead", input: {} }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("run scheduled tools"); const events = await eventStore.getEvents(sessionId); const eventTypes = events.map((event) => event.type); const scheduledPayloads = events .filter((event) => event.type === SessionEventType.ToolCallScheduled) .map((event) => event.payload as any); const batchPayloads = events .filter((event) => event.type === SessionEventType.ToolBatchComplete) .map((event) => event.payload as any); const turnComplete = events.find((event) => event.type === SessionEventType.TurnComplete); expect(result.response).toBe("done"); expect(modelCallCount).toBe(2); expect(turnComplete?.payload).toMatchObject({ toolCallCount: 3, historyRoundCount: 2, }); expect(writeSawSlowReadCompleted).toBe(true); expect(scheduledPayloads.map((payload) => payload.toolName)).toEqual([ "SlowRead", "WriteLike", "FastRead", ]); expect(scheduledPayloads.map((payload) => payload.canRunParallel)).toEqual([true, false, true]); expect(scheduledPayloads.map((payload) => payload.parallelGroupIndex)).toEqual([0, 1, 2]); expect(new Set(scheduledPayloads.map((payload) => payload.assistantMessageId)).size).toBe(1); expect(scheduledPayloads[0]?.assistantMessageId).toEqual(expect.any(String)); expect(batchPayloads).toHaveLength(3); expect(eventTypes.indexOf(SessionEventType.ToolCallScheduled)).toBeLessThan( eventTypes.indexOf(SessionEventType.ToolCallStarted), ); }); it("keeps structured image tool results in the next model request", async () => { const sessionId = createSessionId("runtime-tool-result-image-content"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const imageContent = [ { type: "image" as const, mediaType: "image/jpeg", dataUrl: "data:image/jpeg;base64,cmVhZC1pbWFnZQ==", source: { id: "read-image", kind: "inline" as const, placeholder: "Read image", }, }, ]; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "ReadImageLike", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => ({ ok: true }), formatModelContent: () => imageContent, }); let modelCallCount = 0; let secondRequest: any; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "read-image", name: "ReadImageLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } secondRequest = request; return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("read image"); const toolMessage = secondRequest.messages.find((message: any) => message.role === "tool"); expect(result.response).toBe("done"); expect(modelCallCount).toBe(2); expect(toolMessage).toMatchObject({ content: imageContent, role: "tool", toolCallId: "read-image", toolName: "ReadImageLike", }); }); it("persists data-backed tool media as provider-neutral artifact attachments", async () => { const sessionId = createSessionId("runtime-tool-result-video-persistence"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const artifactStore = new RecordingArtifactStore(); const registry = createToolRegistry(); const dataUrl = "data:video/mp4;base64,cmVhZC12aWRlbw=="; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "ReadVideoLike", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => ({ ok: true }), formatModelContent: () => [ { type: "video", mediaType: "video/mp4", dataUrl, source: { id: "read-video", kind: "inline", placeholder: "clip.mp4", sizeBytes: 10, }, }, { type: "text", text: "post-tool context" }, ], }); let modelCallCount = 0; const requests: any[] = []; const requestObservations: TestModelExecutionObservation[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { artifactStore, eventStore, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { requests.push(request); requestObservations.push(observation); modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "read-video", name: "ReadVideoLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("read video"); const completedToolPart = sessionStore.partsBySession .get(sessionId) ?.find((part) => part.type === "tool" && part.state.status === "completed"); expect(artifactStore.requests).toHaveLength(1); expect(artifactStore.requests[0]).toMatchObject({ content: dataUrl, contentType: "text/plain", sessionId, toolCallId: "read-video-media-1", toolName: "ReadVideoLike", }); expect(completedToolPart).toMatchObject({ type: "tool", state: { status: "completed", metadata: { modelContentLayout: [ { type: "attachment", attachmentIndex: 0 }, { type: "text", text: "post-tool context" }, ], }, attachments: [ { type: "file", mime: "video/mp4", filename: "clip.mp4", url: "zcode-artifact://test/artifact-1", metadata: { artifactUri: "zcode-artifact://test/artifact-1", recoverability: "provider_ready", sizeBytes: 10, storageKind: "artifact", }, }, ], }, }); expect(JSON.stringify(completedToolPart)).not.toContain("cmVhZC12aWRlbw=="); const switchedModel = createTestModelSelection("third-party/model-b"); runtime.setSessionModelSelection(createTestModelSelection(switchedModel)); await runtime.executeTurn("continue after model switch"); const switchedRequest = requests[2]; expect(requestObservations[2]?.model).toMatchObject(switchedModel); expect( switchedRequest.messages.find( (message: any) => message.role === "tool" && message.toolCallId === "read-video", ), ).toMatchObject({ content: [ { type: "video", mediaType: "video/mp4", dataUrl, }, { type: "text", text: "post-tool context" }, ], }); const coldModel = createTestModelSelection("cold-provider/model-c"); let coldRequest: any; let coldObservation: TestModelExecutionObservation | undefined; const resumedRuntime = createTestAgentRuntime( sessionId, { mode: "yolo", modelSelection: createTestModelSelection(coldModel), }, { artifactStore, eventStore: createTestSessionEventStore(), sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { coldRequest = request; coldObservation = observation; return { finishReason: "stop", providerMetadata: undefined, text: "cold resume done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await resumedRuntime.resumeFromStore(); await resumedRuntime.executeTurn("continue after cold resume"); expect(coldObservation?.model).toMatchObject(coldModel); expect( coldRequest.messages.find( (message: any) => message.role === "tool" && message.toolCallId === "read-video", ), ).toMatchObject({ content: [ { type: "video", mediaType: "video/mp4", dataUrl, }, { type: "text", text: "post-tool context" }, ], }); }); it("textifies structured image tool results in text-only model requests", async () => { const sessionId = createSessionId("runtime-tool-result-image-text-only"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const imageContent = [ { type: "image" as const, mediaType: "image/jpeg", dataUrl: "data:image/jpeg;base64,cmVhZC1pbWFnZQ==", source: { id: "read-image", kind: "inline" as const, placeholder: "Read image", }, }, ]; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "ReadImageLike", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => ({ ok: true }), formatModelContent: () => imageContent, }); let modelCallCount = 0; let secondRequest: any; const generateText = async (request: any) => { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls" as const, providerMetadata: undefined, text: "", toolCalls: [{ id: "read-image", name: "ReadImageLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } secondRequest = request; return { finishReason: "stop" as const, providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", }, { eventStore, toolRegistry: registry, modelFactory: ({ selection }) => createTestRuntimeModel({ generateText, inputFormat: { supportsImage: false }, modelId: selection.modelId, providerId: selection.providerId, }), }, ); const result = await runtime.executeTurn("read image"); const toolMessage = secondRequest.messages.find((message: any) => message.role === "tool"); expect(result.response).toBe("done"); expect(modelCallCount).toBe(2); expect( secondRequest.messages.flatMap((message: any) => Array.isArray(message.content) ? message.content.filter((block: any) => block.type === "image") : [], ), ).toHaveLength(0); expect(providerContentToText(toolMessage.content)).toContain("does not support image input"); }); it("continues long-running turns after more than 100 tool calls", async () => { const sessionId = createSessionId("runtime-long-tool-run"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); let executedToolCount = 0; let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "Noop", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { executedToolCount++; return "ok"; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: Array.from({ length: 101 }, (_, index) => ({ id: `noop-${index}`, name: "Noop", input: { index }, })), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "finished long tool run", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("run a long tool sequence"); const events = await eventStore.getEvents(sessionId); expect(result.response).toBe("finished long tool run"); expect(modelCallCount).toBe(2); expect(executedToolCount).toBe(101); expect( events.filter((event) => event.type === SessionEventType.ToolCallScheduled), ).toHaveLength(101); }); it("queues attachment-only steered input while tools run and drains it after tool results", async () => { const sessionId = createSessionId("runtime-turn-steer-tool-loop"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); let runtime: AgentRuntime; let modelCallCount = 0; let secondRequestMessages: any[] = []; const sessionStore = new RecordingSessionStore(); const queuedIntent = { sourceCommandId: "command-queued", queueItemId: "queue-command-queued", clientId: "mobile-client", kind: "sendText" as const, admissionSeq: 7, admittedAt: 1234, requestedDelivery: "guide" as const, admittedDelivery: "guide" as const, attachmentRefs: [ { ref: "artifact://note", fileName: "note.txt", mime: "text/plain", bytes: 4 }, ], }; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "SteerDuringTool", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { const activeTurn = runtime.getActiveTurnInfo(); expect(activeTurn?.steerable).toBe(true); const steerResult = await runtime.steerTurn({ expectedTurnId: activeTurn?.turnId, input: "", intent: queuedIntent, delivery: "guide", attachments: [ { type: "file", content: "note body", path: "note.txt", filename: "note.txt", mimeType: "text/plain", sizeBytes: 9, }, ], }); expect(steerResult.kind).toBe("queued"); return "tool-ok"; }, }); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "SiblingSteerTool", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "sibling-tool-ok", }); runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "steer-tool", name: "SteerDuringTool", input: {} }, { id: "sibling-steer-tool", name: "SiblingSteerTool", input: {} }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } secondRequestMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "done after queued input", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("run tool then accept steering"); const events = await eventStore.getEvents(sessionId); const eventTypes = events.map((event) => event.type); expect(result.response).toBe("done after queued input"); expect(modelCallCount).toBe(2); // 工具批次之后的 token 预算提醒冒泡到保留 user 形态的输入之前,停在 tool result 后。 expect(secondRequestMessages.slice(-5).map((message) => message.role)).toEqual([ "assistant", "tool", "tool", "user", "user", ]); expect(providerContentToText(secondRequestMessages.at(-2)?.content)).toContain( "", ); expect(providerContentToText(secondRequestMessages.at(-1)?.content)).toContain("note body"); const drainIndex = eventTypes.indexOf(SessionEventType.TurnSteerDrained); const toolResultIndexes = eventTypes.flatMap((type, index) => type === SessionEventType.ToolCallResult ? [index] : [], ); expect(toolResultIndexes).toHaveLength(2); expect(Math.max(...toolResultIndexes)).toBeLessThan(drainIndex); expect(eventTypes).toContain(SessionEventType.TurnSteerQueued); expect(eventTypes).toContain(SessionEventType.TurnSteerDrained); expect(eventTypes).toContain(SessionEventType.SessionInputPromoted); const queued = events.find((event) => event.type === SessionEventType.TurnSteerQueued); const drained = events.find((event) => event.type === SessionEventType.TurnSteerDrained); expect(queued?.payload).toMatchObject({ intent: queuedIntent, delivery: "guide" }); expect(drained?.payload).toMatchObject({ drainedInputs: [{ intent: queuedIntent, delivery: "guide" }], }); const persistedQueuedUser = sessionStore.messagesBySession .get(sessionId) ?.find((message) => message.anchor?.sourceCommandId === "command-queued"); expect(persistedQueuedUser).toMatchObject({ anchor: { sourceCommandId: "command-queued" }, metadata: { inputClientId: "mobile-client", inputIntent: queuedIntent, conversationInputIntent: { sourceCommandId: "command-queued", queueItemId: "queue-command-queued", clientId: "mobile-client", kind: "sendText", text: "", attachments: queuedIntent.attachmentRefs, delivery: { requested: "guide", admitted: "guide" }, order: { admissionSeq: 7, queuePosition: 0 }, steer: { state: "guided" }, dispatch: { state: "drained" }, admittedAt: 1234, }, }, }); expect( sessionStore.partsBySession .get(sessionId) ?.some( (part) => part.messageID === persistedQueuedUser?.id && part.type === "file" && part.filename === "note.txt", ), ).toBe(true); expect(sessionStore.promotedSessionInputIds).toContain("queue-command-queued"); }); it("continues a text-only step with one user-role guide in the same active turn", async () => { const sessionId = createSessionId("runtime-guide-text-only-continuation"); const eventStore = createTestSessionEventStore(); let runtime: AgentRuntime; let modelCallCount = 0; let secondRequestMessages: any[] = []; runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount += 1; if (modelCallCount === 1) { const activeTurn = runtime.getActiveTurnInfo(); expect(activeTurn?.steerable).toBe(true); const queued = await runtime.steerTurn({ expectedTurnId: activeTurn?.turnId, input: "guide without a tool boundary", delivery: "guide", intent: { sourceCommandId: "command-guide-fallback", queueItemId: "queue-guide-fallback", clientId: "desktop", kind: "sendText", admissionSeq: 1, admittedAt: 1, requestedDelivery: "guide", admittedDelivery: "guide", }, }); expect(queued.kind).toBe("queued"); } else { secondRequestMessages = withoutAgentListingMessages(request.messages); } return { finishReason: "stop", providerMetadata: undefined, text: "text-only completion", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("finish without tools"); const events = await eventStore.getEvents(sessionId); expect(modelCallCount).toBe(2); // 纯文本回复后取出 guide 时收集预算提醒,排在这条 user 输入之前。这条 guide 直接调 steerTurn、 // 不带 user_steer,不重新起算:已用 = 上一次响应的 1 + 1。 expect(secondRequestMessages.slice(-3).map((message) => message.role)).toEqual([ "assistant", "user", "user", ]); expect(providerContentToText(secondRequestMessages.at(-2)?.content)).toContain( "14999998 tokens left", ); expect(providerContentToText(secondRequestMessages.at(-1)?.content)).toContain( "guide without a tool boundary", ); expect(events.map((event) => event.type)).toContain(SessionEventType.TurnSteerDrained); expect(events.map((event) => event.type)).not.toContain( SessionEventType.TurnSteerDeliveryChanged, ); expect((await runtime.getProjection()).pendingSteerInputs).toEqual([]); }); it("collects input attachments again for a text-only guide continuation", async () => { const sessionId = createSessionId("runtime-guide-text-only-attachments"); let runtime: AgentRuntime; const requests: unknown[] = []; runtime = createTestAgentRuntime( sessionId, { mode: "build", outputStyle: { name: "Learning", prompt: "Explain tradeoffs while solving the task." }, }, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ async generateText(request: any) { requests.push(request.messages); if (requests.length === 1) { const activeTurn = runtime.getActiveTurnInfo(); await runtime.steerTurn({ expectedTurnId: activeTurn?.turnId, input: "guide without a tool boundary", delivery: "guide", }); } return { finishReason: "stop", providerMetadata: undefined, text: "text-only completion", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("finish without tools"); expect(requests).toHaveLength(2); // 文本回复后续上的 guide 是新输入,和工具批次之后一样收集输入附件。 expect(JSON.stringify(requests[1]).match(/Learning output style is active/g)).toHaveLength(2); }); it("does not inline-drain an ordinary queue item at a text-only model boundary", async () => { const sessionId = createSessionId("runtime-ordinary-queue-not-inline-drained"); const eventStore = createTestSessionEventStore(); let runtime: AgentRuntime; let modelCallCount = 0; runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount += 1; if (modelCallCount === 1) { const activeTurn = runtime.getActiveTurnInfo(); const queued = await runtime.steerTurn({ expectedTurnId: activeTurn?.turnId, input: "future turn", delivery: "queue", }); expect(queued.kind).toBe("queued"); } return { finishReason: "stop", providerMetadata: undefined, text: "current turn done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("current turn"); expect(modelCallCount).toBe(1); expect((await eventStore.getEvents(sessionId)).map((event) => event.type)).not.toContain( SessionEventType.TurnSteerDrained, ); expect((await runtime.getProjection()).pendingSteerInputs).toHaveLength(1); }); it("preserves active steered input when the turn fails and pauses runtime queue drain", async () => { const sessionId = createSessionId("runtime-turn-error-preserves-queue"); const eventStore = createTestSessionEventStore(); let runtime: AgentRuntime; runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, modelFactory: createTestModelFactory({ async generateText() { const activeTurn = runtime.getActiveTurnInfo(); const queued = await runtime.steerTurn({ expectedTurnId: activeTurn?.turnId, input: "保留这条错误后的输入", }); expect(queued.kind).toBe("queued"); throw new Error("synthetic provider failure"); }, } as never), }, ); await expect(runtime.executeTurn("会失败的首轮")).rejects.toMatchObject({ type: CoreErrorType.UnknownError, }); const events = await eventStore.getEvents(sessionId); expect(events.some((event) => event.type === SessionEventType.TurnError)).toBe(true); expect(events.some((event) => event.type === SessionEventType.TurnSteerDiscarded)).toBe(false); expect((await runtime.getProjection()).pendingSteerInputs).toMatchObject([ { input: "保留这条错误后的输入" }, ]); expect((runtime as any).queueAutoDrain).toBe(false); }); it("keeps system-reminder-looking steered input as real user content", async () => { const sessionId = createSessionId("runtime-turn-steer-literal-system-reminder"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const literalQueuedInput = "\nqueued user text\n"; let runtime: AgentRuntime; let modelCallCount = 0; let secondRequestMessages: any[] = []; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "SteerLiteralDuringTool", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { const activeTurn = runtime.getActiveTurnInfo(); expect(activeTurn?.steerable).toBe(true); const steerResult = await runtime.steerTurn({ delivery: "guide", expectedTurnId: activeTurn?.turnId, input: literalQueuedInput, }); expect(steerResult.kind).toBe("queued"); return "tool-ok"; }, }); runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "steer-literal-tool", name: "SteerLiteralDuringTool", input: {}, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } secondRequestMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "done after literal queued input", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); await runtime.executeTurn("run tool then accept literal steering"); const queuedUserMessage = secondRequestMessages.at(-1); expect(modelCallCount).toBe(2); expect(queuedUserMessage).toMatchObject({ role: "user", content: literalQueuedInput, cacheControl: { type: "ephemeral" }, }); }); it("keeps a no-tool ordinary follow-up queued for a future product turn", async () => { const sessionId = createSessionId("runtime-turn-steer-after-answer"); const eventStore = createTestSessionEventStore(); let runtime: AgentRuntime; let modelCallCount = 0; let secondRequestMessages: any[] = []; runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { const activeTurn = runtime.getActiveTurnInfo(); expect(activeTurn?.steerable).toBe(true); const steerResult = await runtime.steerTurn({ expectedTurnId: activeTurn?.turnId, input: "one more thing", }); expect(steerResult.kind).toBe("queued"); return { finishReason: "stop", providerMetadata: undefined, text: "first answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } secondRequestMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "second answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("answer, then get steered"); expect(result.response).toBe("first answer"); expect(modelCallCount).toBe(1); expect(secondRequestMessages).toEqual([]); expect((await runtime.getProjection()).pendingSteerInputs).toEqual([ expect.objectContaining({ input: "one more thing" }), ]); }); it("keeps typed compact in FIFO instead of draining it as a normal user prompt", async () => { const sessionId = createSessionId("runtime-turn-steer-typed-compact"); const eventStore = createTestSessionEventStore(); let runtime: AgentRuntime; let modelCallCount = 0; runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount += 1; const activeTurn = runtime.getActiveTurnInfo(); const queued = await runtime.steerTurn({ commandKind: "compact", delivery: "queue", expectedTurnId: activeTurn?.turnId, input: "/compact", inputId: "command-compact", intent: { sourceCommandId: "command-compact", queueItemId: "queue-compact", clientId: "desktop", kind: "compact", text: "/compact", admissionSeq: 2, admittedAt: 1234, requestedDelivery: "queue", admittedDelivery: "queue", }, }); expect(queued.kind).toBe("queued"); return { finishReason: "stop", providerMetadata: undefined, text: "first answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("answer before compact"); const events = await eventStore.getEvents(sessionId); expect(modelCallCount).toBe(1); expect(events.map((event) => event.type)).toContain(SessionEventType.TurnSteerQueued); expect(events.map((event) => event.type)).not.toContain(SessionEventType.TurnSteerDrained); expect((await runtime.getProjection()).pendingSteerInputs).toEqual([ expect.objectContaining({ commandKind: "compact", input: "/compact", intent: expect.objectContaining({ kind: "compact", sourceCommandId: "command-compact" }), }), ]); }); it("drains all waiting guides together after the next completed tool batch", async () => { const sessionId = createSessionId("runtime-turn-steer-query-queue"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); let runtime: AgentRuntime; let modelCallCount = 0; const requestQueryIds: Array = []; const trailingUserTexts: string[][] = []; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "GuideBoundary", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "tool boundary complete", }); runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any, observation) { modelCallCount++; requestQueryIds.push(observation.invocationContext?.traceContext?.queryId); const messages = withoutAgentListingMessages(request.messages); const lastResult = messages.findLastIndex((message: any) => message.role === "tool"); trailingUserTexts.push( messages .slice(lastResult + 1) .filter((message: any) => message.role === "user") .map((message: any) => String(message.content)), ); if (modelCallCount === 1) { const activeTurn = runtime.getActiveTurnInfo(); expect(activeTurn?.steerable).toBe(true); const firstSteer = await runtime.steerTurn({ delivery: "guide", expectedTurnId: activeTurn?.turnId, input: "first queued follow-up", queryId: "query_first_steer" as never, }); const secondSteer = await runtime.steerTurn({ delivery: "guide", expectedTurnId: activeTurn?.turnId, input: "second queued follow-up", queryId: "query_second_steer" as never, }); expect(firstSteer.kind).toBe("queued"); expect(secondSteer.kind).toBe("queued"); return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "guide-boundary-1", name: "GuideBoundary", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "answer after both queued follow-ups", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("original request", undefined, { queryId: "query_original_request" as never, }); const events = await eventStore.getEvents(sessionId); const queuedEvents = events.filter((event) => event.type === SessionEventType.TurnSteerQueued); const drainedEvents = events.filter( (event) => event.type === SessionEventType.TurnSteerDrained, ); expect(result.response).toBe("answer after both queued follow-ups"); // 同一边界一次取出全部已等待的 guide;下一次请求归因到最后一条的 query。 expect(requestQueryIds).toEqual(["query_original_request", "query_second_steer"]); // 默认测试模型不合并相邻 user 内容,不补换行。这批工具的 token 预算提醒按冒泡规则 // 越过保留 user 形态的 guide,停在 tool result 之后。 expect(trailingUserTexts[1]).toEqual([ "\n14999998 tokens left\n", "first queued follow-up", "second queued follow-up", ]); // 一起取出,但每条各自落库并各发一个 drained 事件。 const drainedPayloads = drainedEvents.map((event) => event.payload as any); expect(drainedPayloads.map((payload) => payload.queryIds)).toEqual([ ["query_first_steer"], ["query_second_steer"], ]); expect(drainedPayloads.map((payload) => payload.injectedMessageIds)).toEqual([ [expect.any(String)], [expect.any(String)], ]); // 普通 active-turn steer 不需要显式 source;缺省即表示普通 guided steer。 expect(queuedEvents.every((event) => !("source" in event.payload))).toBe(true); expect(drainedEvents.every((event) => !("sources" in event.payload))).toBe(true); }); it.each([true, false])( "keeps each guide's page state next to it when guides drain together, live and cold (MCS=%s)", async (mcs) => { const sessionId = createSessionId(`runtime-guide-gen-ui-pairing-${mcs}`); const sessionStore = new RecordingSessionStore(); const registry = createToolRegistry(); let runtime: AgentRuntime; let modelCallCount = 0; let continuationMessages: any[] = []; // guide 和页面状态带 mk: 标记,按出现顺序比较位置。 const guideMarkers = (messages: readonly unknown[]) => [...JSON.stringify(messages).matchAll(/mk:(st[a-z]+-\d)/g)].map((match) => match[1]); const steerWithPageState = (input: string, state: string) => runtime.steerTurn({ delivery: "guide", expectedTurnId: runtime.getActiveTurnInfo()?.turnId, input, // 与 App 发送 guide 的路径相同(input-facade 标为 user_steer)。 inputPresentation: "user_steer", intent: { genUiContext: [{ path: "/workspace/gen-ui/widget.html", state }] }, }); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "GuideBoundary", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "tool boundary complete", }); runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore: createTestSessionEventStore(), sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ properties: { supportsMidConversationSystem: mcs }, async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { // 工具运行期间,用户在两个不同的页面状态下各发一条消息;同一边界一起取出。 await steerWithPageState("mk:steer-1", "mk:state-1"); await steerWithPageState("mk:steer-2", "mk:state-2"); return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "guide-boundary-1", name: "GuideBoundary", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } continuationMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "answered both", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("original request"); // MCS 下两条 guide 合成一条 system,状态都在它之前的 user 侧;MCS 关时每个状态紧跟它的 guide。 const expected = mcs ? ["state-1", "state-2", "steer-1", "steer-2"] : ["steer-1", "state-1", "steer-2", "state-2"]; expect(modelCallCount).toBe(2); expect(guideMarkers(continuationMessages)).toEqual(expected); // 冷恢复从同一份输入快照派生状态,请求里的顺序与 live 相同。 const restored = new MessageHistoryImpl(); await hydrateMessageHistoryFromSession({ history: restored, messages: await sessionStore.messages({ sessionID: sessionId }), }); const coldMessages = buildProviderRequestMessages({ entries: restored.borrowReadOnlyRuntimeEntries(), useMidConversationSystem: mcs, }).messages; expect(guideMarkers(coldMessages)).toEqual(expected); }, ); it.each([true, false])( "keeps the page state of a user_steer input that opens a new turn, live and cold (MCS=%s)", async (mcs) => { const sessionId = createSessionId(`runtime-send-now-gen-ui-${mcs}`); const sessionStore = new RecordingSessionStore(); let lastMessages: any[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore: createTestSessionEventStore(), sessionStore, modelFactory: createTestModelFactory({ properties: { supportsMidConversationSystem: mcs }, async generateText(request: any) { lastMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const markers = (messages: readonly unknown[]) => [...JSON.stringify(messages).matchAll(/mk:([a-z-]+)/g)].map((match) => match[1]); await runtime.executeTurn("mk:original"); // 旧版本的立即发送以 user_steer 开新一轮,已存会话仍是这种形式;页面状态要留在输入旁边。 await runtime.executeTurn("mk:send-now", undefined, { inputPresentation: "user_steer", intent: { genUiContext: [{ path: "/workspace/gen-ui/widget.html", state: "mk:send-now-state" }], }, } as never); // 接在 text-only 回复之后,MCS 下也没有 system 锚点,降级为 user reminder,状态跟在它之后。 const expected = ["original", "send-now", "send-now-state"]; expect(markers(lastMessages)).toEqual(expected); const restored = new MessageHistoryImpl(); await hydrateMessageHistoryFromSession({ history: restored, messages: await sessionStore.messages({ sessionID: sessionId }), }); const coldMessages = buildProviderRequestMessages({ entries: restored.borrowReadOnlyRuntimeEntries(), useMidConversationSystem: mcs, }).messages; expect(markers(coldMessages)).toEqual(expected); }, ); it("stops the guide drain at a guide reserved by send-now", async () => { const sessionId = createSessionId("runtime-turn-steer-reserved-guide"); const eventStore = createTestSessionEventStore(); let runtime: AgentRuntime; const trailingUserTexts: string[][] = []; runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { const messages = withoutAgentListingMessages(request.messages); const lastAssistant = messages.findLastIndex( (message: any) => message.role === "assistant", ); trailingUserTexts.push( messages .slice(lastAssistant + 1) .filter((message: any) => message.role === "user") .map((message: any) => String(message.content)), ); if (trailingUserTexts.length === 1) { const turnId = runtime.getActiveTurnInfo()?.turnId; await runtime.steerTurn({ delivery: "guide", expectedTurnId: turnId, input: "guide a", }); const reserved = await runtime.steerTurn({ delivery: "guide", expectedTurnId: turnId, input: "guide b reserved", }); await runtime.steerTurn({ delivery: "guide", expectedTurnId: turnId, input: "guide c", }); if (reserved.kind !== "queued") throw new Error("expected queued input"); await expect( runtime.reservePendingInputById({ pendingInputId: reserved.pendingInputId, reservationId: "send-now-command", traceContext: { sessionId, traceId: createTraceId("trace-reserved-guide") }, }), ).resolves.toBe(true); } return { finishReason: "stop", providerMetadata: undefined, text: `step ${trailingUserTexts.length}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("original request"); const drainedEvents = (await eventStore.getEvents(sessionId)).filter( (event) => event.type === SessionEventType.TurnSteerDrained, ); // 已被 send-now 预留的 guide 归预留方提升;按 FIFO 取到它为止,后面的 guide 也不越过它。 // 这些 guide 直接调 steerTurn、不带 user_steer,纯文本回复后的预算提醒不重新起算: // 已用 = 上一次响应的 1 + 1。 expect(trailingUserTexts[1]).toEqual([ "\n14999998 tokens left\n", "guide a", ]); expect(drainedEvents).toHaveLength(1); expect( (drainedEvents[0]!.payload as any).drainedInputs.map((input: any) => input.text), ).toEqual(["guide a"]); }); it("settles each drained guide before the next, so a later persistence failure keeps the earlier one and leaves the rest queued", async () => { const sessionId = createSessionId("runtime-turn-steer-partial-drain"); const eventStore = createTestSessionEventStore(); // 第二条 guide 的 user message + session input promote 提交失败。 class FailingGuideBStore extends RecordingSessionStore { override async promoteSessionInput( input: Parameters[0], ): Promise { if (input.parts.some((part) => part.type === "text" && part.text === "guide b")) { throw new Error("injected guide b promote failure"); } await super.promoteSessionInput(input); } } const sessionStore = new FailingGuideBStore(); let runtime: AgentRuntime; let modelCallCount = 0; const requestTexts: string[] = []; runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; requestTexts.push(JSON.stringify(request.messages)); if (modelCallCount === 1) { const turnId = runtime.getActiveTurnInfo()?.turnId; for (const input of ["guide a", "guide b", "guide c"]) { await runtime.steerTurn({ delivery: "guide", expectedTurnId: turnId, input }); } } return { finishReason: "stop", providerMetadata: undefined, text: `step ${modelCallCount}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("original request").catch(() => undefined); const events = await eventStore.getEvents(sessionId); const drainedTexts = events .filter((event) => event.type === SessionEventType.TurnSteerDrained) .map((event) => (event.payload as any).drainedInputs.map((input: any) => input.text)); const queuedIds = events .filter((event) => event.type === SessionEventType.TurnSteerQueued) .map((event) => (event.payload as any).pendingInputId); // A 的记录、移出队列与 drained 事实已按条完成;B 没有提交,不进入 drained,也不再请求模型。 expect(drainedTexts).toEqual([["guide a"]]); expect(sessionStore.promotedSessionInputIds).toEqual([queuedIds[0]]); expect(modelCallCount).toBe(1); // B 与之后的 C 仍在队列里(error-paused),等待用户继续。 expect((await runtime.getProjection()).pendingSteerInputs.map((item) => item.input)).toEqual([ "guide b", "guide c", ]); // B 仍在队列里,下一轮请求不能已经带着它;否则继续队列时模型会看到两次。 await runtime.executeTurn("next request"); expect(requestTexts.at(-1)).toContain("guide a"); expect(requestTexts.at(-1)).not.toContain("guide b"); }); it("finishes a started guide drain when the turn is stopped mid-way, and the next turn sends a valid request", async () => { const sessionId = createSessionId("runtime-turn-steer-stop-during-drain"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); let runtime: AgentRuntime; // 第一条 guide 提交期间用户按停止。 class StopDuringGuideAStore extends RecordingSessionStore { override async promoteSessionInput( input: Parameters[0], ): Promise { if (input.parts.some((part) => part.type === "text" && part.text === "guide a")) { runtime.stopActiveForegroundExecution({ reason: "user stop" }); } await super.promoteSessionInput(input); } } const sessionStore = new StopDuringGuideAStore(); const requests: any[][] = []; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "GuideBoundary", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "tool boundary complete", }); runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { requests.push(withoutAgentListingMessages(request.messages)); if (requests.length === 1) { const turnId = runtime.getActiveTurnInfo()?.turnId; for (const input of ["guide a", "guide b", "guide c"]) { await runtime.steerTurn({ delivery: "guide", expectedTurnId: turnId, input }); } return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "guide-boundary-1", name: "GuideBoundary", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "next answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await expect(runtime.executeTurn("original request")).rejects.toMatchObject({ type: CoreErrorType.TurnCancelled, }); const drainedTexts = (await eventStore.getEvents(sessionId)) .filter((event) => event.type === SessionEventType.TurnSteerDrained) .map((event) => (event.payload as any).drainedInputs.map((input: any) => input.text)); // 已开始的取出做完整批:停止在 a 提交期间到达,b、c 仍按条提交并各有自己的 drained 事件。 expect(drainedTexts).toEqual([["guide a"], ["guide b"], ["guide c"]]); // 停止在本批提交后生效:本轮不再请求模型,也没有 guide 退回队列。 expect(requests).toHaveLength(1); expect((await runtime.getProjection()).pendingSteerInputs).toEqual([]); // 下一轮照常:tool call 后紧跟它的结果,之后依次是三条 guide、中断标记与新输入。 // 新输入轮首的 token 预算提醒按冒泡规则越过这些 user 输入,停在 tool result 之后。 await expect(runtime.executeTurn("next request")).resolves.toMatchObject({ response: "next answer", }); const next = requests.at(-1)!; const toolCallIndex = next.findIndex( (message: any) => message.role === "assistant" && message.toolCalls?.some((call: any) => call.id === "guide-boundary-1"), ); expect(toolCallIndex).toBeGreaterThan(-1); expect(next[toolCallIndex + 1]?.role).toBe("tool"); expect( next .slice(toolCallIndex + 2) .filter((message: any) => message.role === "user") .map((message: any) => providerContentToText(message.content)), ).toEqual([ "\n15000000 tokens left\n", "guide a", "guide b", "guide c", "[Request interrupted by user]", "next request", ]); }); it("rejects steering when the expected turn id is stale", async () => { const sessionId = createSessionId("runtime-turn-steer-stale"); const eventStore = createTestSessionEventStore(); const modelStarted = deferred(); const finishModel = deferred(); const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelStarted.resolve(); await finishModel.promise; return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const pendingTurn = runtime.executeTurn("wait for stale steering"); await modelStarted.promise; const result = await runtime.steerTurn({ expectedTurnId: createTurnId("stale"), input: "stale input", }); finishModel.resolve(); await pendingTurn; const events = await eventStore.getEvents(sessionId); const rejection = events.find((event) => event.type === SessionEventType.TurnSteerRejected); expect(result).toMatchObject({ kind: "rejected", reason: "expected_turn_mismatch", }); expect(rejection?.payload).toMatchObject({ reason: "expected_turn_mismatch", }); }); // 语义变更(2026-07-05 裁决,catalog B02-B04 stopKeepsQueueAndDisablesAutoDrain): // stop/cancel 不再丢弃排队输入——queue 原样保留成 held,由 v4 投影派生 // autoDrain=false + choice 路由,显式消费走 sendQueuedNow/heldQueueDisposition。 // 本测试原断言 cancel 即 TurnSteerDiscarded(旧行为),随裁决反转。 it("keeps queued steering input when the active turn is cancelled (held)", async () => { const sessionId = createSessionId("runtime-turn-steer-discard-cancel"); const eventStore = createTestSessionEventStore(); const abortController = new AbortController(); const modelStarted = deferred(); const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelStarted.resolve(); await waitForAbort(request.abortSignal); return { finishReason: "stop", providerMetadata: undefined, text: "too late", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const pendingTurn = runtime.executeTurn("wait for cancel", undefined, { abortSignal: abortController.signal, }); await modelStarted.promise; const queued = await runtime.steerTurn("discard me"); const guide = await runtime.steerTurn({ delivery: "guide", input: "guide interrupted by stop", intent: { sourceCommandId: "command-guide-cancel", queueItemId: "queue-guide-cancel", clientId: "desktop", kind: "sendText", admissionSeq: 2, admittedAt: 2, requestedDelivery: "guide", admittedDelivery: "guide", }, }); abortController.abort(new Error("test cancel")); await expect(pendingTurn).rejects.toMatchObject({ type: CoreErrorType.TurnCancelled, }); const events = await eventStore.getEvents(sessionId); const projection = await runtime.getProjection(); expect(queued.kind).toBe("queued"); expect(guide.kind).toBe("queued"); expect(events.map((event) => event.type)).not.toContain(SessionEventType.TurnSteerDiscarded); expect(events).toEqual( expect.arrayContaining([ expect.objectContaining({ type: SessionEventType.TurnSteerDeliveryChanged, payload: expect.objectContaining({ pendingInputId: "queue-guide-cancel", fallbackReasonCode: "guide.turnInterrupted", }), }), ]), ); expect(projection.pendingSteerInputs).toHaveLength(2); expect(projection.pendingSteerInputs[0]?.input).toBe("discard me"); expect(projection.pendingSteerInputs[1]?.intent).toMatchObject({ admittedDelivery: "queue", fallbackReasonCode: "guide.turnInterrupted", }); }); it("M07a preserves queue auto-drain when sendQueuedNow preempts the active turn", async () => { const sessionId = createSessionId("runtime-send-now-preempt"); const eventStore = createTestSessionEventStore(); const modelStarted = deferred(); const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelStarted.resolve(); await waitForAbort(request.abortSignal); return { finishReason: "stop", providerMetadata: undefined, text: "too late", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const pendingTurn = runtime.executeTurn("wait for send now"); await modelStarted.promise; const queued = await runtime.steerTurn("send this now"); expect(queued.kind).toBe("queued"); if (queued.kind !== "queued") throw new Error("expected queued input"); await expect( runtime.reservePendingInputById({ pendingInputId: queued.pendingInputId, reservationId: "send-now-command", traceContext: { sessionId, traceId: createTraceId("trace-send-now-preempt"), }, }), ).resolves.toBe(true); expect( runtime.stopActiveForegroundExecution({ preserveQueueAutoDrainOnCancel: true, reason: "v4 sendQueuedNow preempts active turn", }), ).toMatchObject({ kind: "stopped" }); await expect(pendingTurn).rejects.toMatchObject({ type: CoreErrorType.TurnCancelled, }); const events = await eventStore.getEvents(sessionId); expect( events.find((event) => event.type === SessionEventType.TurnComplete)?.payload, ).toMatchObject({ preserveQueueAutoDrainOnCancel: true, resultType: "cancelled", }); expect((await runtime.getProjection()).pendingSteerInputs).toHaveLength(1); }); it("persists queue edit/reorder and settles user deletion before removing the authoritative item", async () => { const sessionId = createSessionId("runtime-turn-steer-durable-edit"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const modelStarted = deferred(); const finishModel = deferred(); let modelCalls = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCalls += 1; if (modelCalls === 1) { modelStarted.resolve(); await finishModel.promise; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const traceContext = { sessionId, traceId: createTraceId("trace-durable-edit"), }; const pendingTurn = runtime.executeTurn("hold queue"); await modelStarted.promise; const first = await runtime.steerTurn({ input: "first", delivery: "queue", toolDisallowlist: ["CronCreate"], intent: { sourceCommandId: "command-first", queueItemId: "queue-first", clientId: "desktop", kind: "sendText", admissionSeq: 1, admittedAt: 1, requestedDelivery: "queue", admittedDelivery: "queue", attachmentRefs: [ { ref: "artifact:first", fileName: "first.txt", mime: "text/plain", bytes: 5, }, ], }, }); const second = await runtime.steerTurn({ input: "second", delivery: "queue", intent: { sourceCommandId: "command-second", queueItemId: "queue-second", clientId: "mobile", kind: "sendText", admissionSeq: 2, admittedAt: 2, requestedDelivery: "queue", admittedDelivery: "queue", }, }); if (first.kind !== "queued" || second.kind !== "queued") { throw new Error("expected queued inputs"); } await expect( runtime.editPendingInputById({ pendingInputId: first.pendingInputId, newText: "first edited", traceContext, }), ).resolves.toBe("edited"); await expect( runtime.reorderPendingInput({ pendingInputId: second.pendingInputId, beforePendingInputId: first.pendingInputId, traceContext, }), ).resolves.toBe(true); await expect( runtime.removePendingInputById({ pendingInputId: first.pendingInputId, reason: "user_removed", traceContext, }), ).resolves.toBe(true); expect(sessionStore.sessionInputUpdates).toEqual([ { sessionID: sessionId, updates: [{ id: first.pendingInputId, text: "first edited" }], }, { sessionID: sessionId, updates: [ { id: second.pendingInputId, queuePosition: 0 }, { id: first.pendingInputId, queuePosition: 1 }, ], }, ]); expect(sessionStore.settledSessionInputs).toContainEqual({ id: first.pendingInputId, sessionID: sessionId, status: "cancelled", reason: "user_removed", }); const projection = await runtime.getProjection(); expect(projection.pendingSteerInputs.map((item) => item.pendingInputId)).toEqual([ second.pendingInputId, ]); const queuedEvents = (await eventStore.getEvents(sessionId)).filter( (event) => event.type === SessionEventType.TurnSteerQueued, ); expect(queuedEvents).toEqual( expect.arrayContaining([ expect.objectContaining({ payload: expect.objectContaining({ pendingInputId: first.pendingInputId, input: "first edited", toolDisallowlist: ["CronCreate"], }), }), ]), ); finishModel.resolve(); await pendingTurn; }); it("reserves a queued input so roundtrip drain and competing promotion cannot consume it", async () => { const sessionId = createSessionId("runtime-turn-steer-reservation"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const modelStarted = deferred(); const finishModel = deferred(); const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelStarted.resolve(); await finishModel.promise; return { finishReason: "stop", providerMetadata: undefined, text: "first answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const traceContext = { sessionId, traceId: createTraceId("trace-reservation") }; const pendingTurn = runtime.executeTurn("run before promotion"); await modelStarted.promise; const queued = await runtime.steerTurn("promote me"); expect(queued.kind).toBe("queued"); if (queued.kind !== "queued") throw new Error("expected queued input"); await expect( runtime.reservePendingInputById({ pendingInputId: queued.pendingInputId, reservationId: "send-now-a", traceContext, }), ).resolves.toBe(true); await expect( runtime.reservePendingInputById({ pendingInputId: queued.pendingInputId, reservationId: "send-now-b", traceContext, }), ).resolves.toBe(false); await expect( runtime.removePendingInputById({ pendingInputId: queued.pendingInputId, reason: "user_removed", traceContext, }), ).resolves.toBe(false); finishModel.resolve(); await pendingTurn; const eventsBeforeRelease = await eventStore.getEvents(sessionId); expect(eventsBeforeRelease.map((event) => event.type)).not.toContain( SessionEventType.TurnSteerDrained, ); expect((await runtime.getProjection()).pendingSteerInputs).toHaveLength(1); await expect( runtime.releasePendingInputReservation({ pendingInputId: queued.pendingInputId, reservationId: "send-now-a", traceContext, }), ).resolves.toBe(true); const dispatchStates = (await eventStore.getEvents(sessionId)) .filter((event) => event.type === SessionEventType.TurnSteerDispatchChanged) .map((event) => (event.payload as { state: string }).state); expect(dispatchStates).toEqual(["reserved", "queued"]); await expect( runtime.reservePendingInputById({ pendingInputId: queued.pendingInputId, reservationId: "send-now-b", traceContext, }), ).resolves.toBe(true); await expect( runtime.markPendingInputPromoting({ pendingInputId: queued.pendingInputId, reservationId: "send-now-b", traceContext, }), ).resolves.toBe(true); await expect( runtime.removePendingInputById({ pendingInputId: queued.pendingInputId, reason: "promoted", reservationId: "send-now-b", traceContext, }), ).resolves.toBe(true); expect((await runtime.getProjection()).pendingSteerInputs).toHaveLength(0); expect(sessionStore.admittedSessionInputIds).toContain(queued.pendingInputId); // send-now 从 queue 摘除后仍保持 admitted;真正的 user message 会在另一条 // startPromptTurn 写路径中原子 promotion,崩溃则由恢复清扫置 discarded。 expect(sessionStore.settledSessionInputIds).not.toContain(queued.pendingInputId); }); it("freezes a queued input for composer edit until the owning editor submits or releases it", async () => { // docs/ui/composer-edit-mode.md:编辑锁与 reservation 互斥;冻结期间不能被删除、重排、 // 提升,只有持锁编辑器的 editPendingInputById 能写回并同时释放锁。 const sessionId = createSessionId("runtime-turn-steer-edit-lock"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const modelStarted = deferred(); const finishModel = deferred(); const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText() { modelStarted.resolve(); await finishModel.promise; return { finishReason: "stop", providerMetadata: undefined, text: "first answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const traceContext = { sessionId, traceId: createTraceId("trace-edit-lock") }; const pendingTurn = runtime.executeTurn("run while editing"); await modelStarted.promise; const queued = await runtime.steerTurn({ input: "edit me", delivery: "queue", intent: { sourceCommandId: "command-edit", queueItemId: "queue-edit", clientId: "desktop", kind: "sendText", admissionSeq: 1, admittedAt: 1, requestedDelivery: "queue", admittedDelivery: "queue", mode: "build", }, }); const other = await runtime.steerTurn({ input: "other", delivery: "queue" }); if (queued.kind !== "queued" || other.kind !== "queued") { throw new Error("expected queued inputs"); } const lock = { editorId: "editor-a", clientId: "desktop", inputOrigin: "desktop" as const }; await expect( runtime.lockPendingInputEdit({ pendingInputId: queued.pendingInputId, editLock: lock, traceContext, }), ).resolves.toBe("locked"); await expect( runtime.lockPendingInputEdit({ pendingInputId: queued.pendingInputId, editLock: { ...lock, editorId: "editor-b" }, traceContext, }), ).resolves.toBe("lockedByOther"); await expect( runtime.lockPendingInputEdit({ pendingInputId: "missing-input", editLock: lock, traceContext, }), ).resolves.toBe("missing"); expect(runtime.getPendingInputEditLock(queued.pendingInputId)).toEqual(lock); // 冻结项:reserve / 删除 / 重排都被拒绝;其他项不受影响。 await expect( runtime.reservePendingInputById({ pendingInputId: queued.pendingInputId, reservationId: "send-now", traceContext, }), ).resolves.toBe(false); await expect( runtime.removePendingInputById({ pendingInputId: queued.pendingInputId, reason: "user_removed", traceContext, }), ).resolves.toBe(false); await expect( runtime.reorderPendingInput({ pendingInputId: queued.pendingInputId, beforePendingInputId: null, traceContext, }), ).resolves.toBe(false); await expect( runtime.reorderPendingInput({ pendingInputId: other.pendingInputId, beforePendingInputId: queued.pendingInputId, traceContext, }), ).resolves.toBe(true); // 未持锁的写回不能覆盖冻结项;声明错误编辑器视为锁已丢失。 await expect( runtime.editPendingInputById({ pendingInputId: queued.pendingInputId, newText: "overwrite", traceContext, }), ).resolves.toBe("editLocked"); await expect( runtime.editPendingInputById({ pendingInputId: queued.pendingInputId, newText: "overwrite", editorId: "editor-b", traceContext, }), ).resolves.toBe("lockLost"); const attachmentRefs = [ { ref: "artifact:new", fileName: "new.txt", mime: "text/plain", bytes: 3 }, ]; const modelSelection = { providerId: "provider-edit", modelId: "model-edit" }; await expect( runtime.editPendingInputById({ pendingInputId: queued.pendingInputId, newText: "edited text", editorId: "editor-a", attachmentRefs, modelSelection, mode: "yolo", planEnabled: true, traceContext, }), ).resolves.toBe("edited"); expect(runtime.getPendingInputEditLock(queued.pendingInputId)).toBeUndefined(); const events = await eventStore.getEvents(sessionId); const lockEvents = events .filter((event) => event.type === SessionEventType.TurnSteerEditLockChanged) .map((event) => (event.payload as { editLock?: unknown }).editLock ?? null); expect(lockEvents).toEqual([lock, null]); const editedEvent = events .filter((event) => event.type === SessionEventType.TurnSteerQueued) .at(-1); expect(editedEvent?.payload).toMatchObject({ pendingInputId: queued.pendingInputId, input: "edited text", intent: { text: "edited text", attachmentRefs, modelSelection, mode: "yolo", planEnabled: true, }, }); // 只改配置(applyQueueItemsConfig):缺省 newText 保留原输入,不写正文更新。 const inputUpdatesBefore = sessionStore.sessionInputUpdates.length; await expect( runtime.editPendingInputById({ pendingInputId: queued.pendingInputId, mode: "guarded", traceContext, }), ).resolves.toBe("edited"); expect(sessionStore.sessionInputUpdates).toHaveLength(inputUpdatesBefore); const configEdited = (await eventStore.getEvents(sessionId)) .filter((event) => event.type === SessionEventType.TurnSteerQueued) .at(-1); expect(configEdited?.payload).toMatchObject({ pendingInputId: queued.pendingInputId, input: "edited text", intent: { text: "edited text", attachmentRefs, modelSelection, mode: "guarded" }, }); // 解锁后恢复正常:可再次加锁并由任意端解除。 await expect( runtime.lockPendingInputEdit({ pendingInputId: queued.pendingInputId, editLock: lock, traceContext, }), ).resolves.toBe("locked"); await expect( runtime.releasePendingInputEdit({ pendingInputId: queued.pendingInputId, traceContext }), ).resolves.toBe(true); await expect( runtime.releasePendingInputEdit({ pendingInputId: queued.pendingInputId, traceContext }), ).resolves.toBe(false); await expect( runtime.reservePendingInputById({ pendingInputId: queued.pendingInputId, reservationId: "send-now", traceContext, }), ).resolves.toBe(true); // reserve 后不能再被冻结,与 sendQueuedNow 互斥。 await expect( runtime.lockPendingInputEdit({ pendingInputId: queued.pendingInputId, editLock: lock, traceContext, }), ).resolves.toBe("reserved"); finishModel.resolve(); await pendingTurn; }); it("drops a queued input together with its composer edit lock when a rebuilt runtime resumes", async () => { // 编辑锁只属于当前 runtime 实例(docs/ui/composer-edit-mode.md「锁的释放」)。新 runtime // 必须经 resumeFromStore 丢弃残留队列项(session_resumed),投影与空锁表才保持一致; // 否则会出现「投影显示已锁、runtime 查不到锁」而无法解除、阻塞续跑。 const sessionId = createSessionId("runtime-edit-lock-resume"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const modelStarted = deferred(); const finishModel = deferred(); const createRuntime = () => createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-edit-lock-resume" }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText() { modelStarted.resolve(); await finishModel.promise; return { finishReason: "stop", providerMetadata: undefined, text: "answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const traceContext = { sessionId, traceId: createTraceId("trace-edit-lock-resume") }; const firstRuntime = createRuntime(); const pendingTurn = firstRuntime.executeTurn("run while editing"); await modelStarted.promise; const queued = await firstRuntime.steerTurn({ input: "edit me", delivery: "queue" }); if (queued.kind !== "queued") throw new Error("expected queued input"); await expect( firstRuntime.lockPendingInputEdit({ pendingInputId: queued.pendingInputId, editLock: { editorId: "editor-a", clientId: "desktop" }, traceContext, }), ).resolves.toBe("locked"); const resumedRuntime = createRuntime(); await resumedRuntime.resumeFromStore(); expect((await resumedRuntime.getProjection()).pendingSteerInputs).toHaveLength(0); expect(resumedRuntime.getPendingInputEditLock(queued.pendingInputId)).toBeUndefined(); const discarded = (await eventStore.getEvents(sessionId)).find( (event) => event.type === SessionEventType.TurnSteerDiscarded, ); expect(discarded?.payload).toMatchObject({ pendingInputIds: [queued.pendingInputId], reason: "session_resumed", }); finishModel.resolve(); await pendingTurn; }); it("keeps a frozen item's edit lock when clearing the queue fails to record the discard", async () => { // docs/ui/composer-edit-mode.md:锁与队列项同生同灭。清空队列写丢弃事件失败时, // 项仍在投影里,锁也必须仍在 runtime 里,才能被解除且继续拦住删除。 const sessionId = createSessionId("runtime-edit-lock-clear-failure"); const baseStore = createTestSessionEventStore(); let failNextDiscard = true; const eventStore = new Proxy(baseStore, { get(target, property, receiver) { if (property === "append") { return async (event: SessionEvent) => { if (failNextDiscard && event.type === SessionEventType.TurnSteerDiscarded) { failNextDiscard = false; throw new Error("discard append failed"); } return target.append(event); }; } const value = Reflect.get(target, property, receiver); return typeof value === "function" ? value.bind(target) : value; }, }); const sessionStore = new RecordingSessionStore(); const modelStarted = deferred(); const finishModel = deferred(); const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText() { modelStarted.resolve(); await finishModel.promise; return { finishReason: "stop", providerMetadata: undefined, text: "answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const traceContext = { sessionId, traceId: createTraceId("trace-edit-lock-clear-failure") }; const pendingTurn = runtime.executeTurn("run while editing"); await modelStarted.promise; const queued = await runtime.steerTurn({ input: "edit me", delivery: "queue" }); if (queued.kind !== "queued") throw new Error("expected queued input"); const lock = { editorId: "editor-a", clientId: "desktop" }; await runtime.lockPendingInputEdit({ pendingInputId: queued.pendingInputId, editLock: lock, traceContext, }); await expect(runtime.clearAllPendingInputs(traceContext)).rejects.toThrow( "discard append failed", ); expect((await runtime.getProjection()).pendingSteerInputs).toMatchObject([ { pendingInputId: queued.pendingInputId }, ]); expect(runtime.getPendingInputEditLock(queued.pendingInputId)).toEqual(lock); await expect( runtime.removePendingInputById({ pendingInputId: queued.pendingInputId, reason: "user_removed", traceContext, }), ).resolves.toBe(false); await expect( runtime.releasePendingInputEdit({ pendingInputId: queued.pendingInputId, traceContext }), ).resolves.toBe(true); // 重试清空成功后,项与锁一起消失。 await runtime.lockPendingInputEdit({ pendingInputId: queued.pendingInputId, editLock: lock, traceContext, }); await expect(runtime.clearAllPendingInputs(traceContext)).resolves.toBe(1); expect((await runtime.getProjection()).pendingSteerInputs).toHaveLength(0); expect(runtime.getPendingInputEditLock(queued.pendingInputId)).toBeUndefined(); finishModel.resolve(); await pendingTurn; }); it("keeps the edit lock when the unlock event after an edit or release fails to persist", async () => { // docs/ui/composer-edit-mode.md:锁的内存状态与锁事件同生同灭。解锁事件写入失败时, // 投影仍显示锁,runtime 也必须仍持锁:可重试解除,且删除继续被拒绝。 const sessionId = createSessionId("runtime-edit-lock-unlock-failure"); const baseStore = createTestSessionEventStore(); let failNextUnlock = false; const eventStore = new Proxy(baseStore, { get(target, property, receiver) { if (property === "append") { return async (event: SessionEvent) => { const unlocking = event.type === SessionEventType.TurnSteerEditLockChanged && !(event.payload as { editLock?: unknown }).editLock; if (failNextUnlock && unlocking) { failNextUnlock = false; throw new Error("unlock append failed"); } return target.append(event); }; } const value = Reflect.get(target, property, receiver); return typeof value === "function" ? value.bind(target) : value; }, }); const sessionStore = new RecordingSessionStore(); const modelStarted = deferred(); const finishModel = deferred(); const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText() { modelStarted.resolve(); await finishModel.promise; return { finishReason: "stop", providerMetadata: undefined, text: "answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const traceContext = { sessionId, traceId: createTraceId("trace-edit-lock-unlock-failure") }; const pendingTurn = runtime.executeTurn("run while editing"); await modelStarted.promise; const queued = await runtime.steerTurn({ input: "edit me", delivery: "queue" }); if (queued.kind !== "queued") throw new Error("expected queued input"); const lock = { editorId: "editor-a", clientId: "desktop" }; const expectStillLocked = async () => { expect(runtime.getPendingInputEditLock(queued.pendingInputId)).toEqual(lock); await expect( runtime.removePendingInputById({ pendingInputId: queued.pendingInputId, reason: "user_removed", traceContext, }), ).resolves.toBe(false); }; await runtime.lockPendingInputEdit({ pendingInputId: queued.pendingInputId, editLock: lock, traceContext, }); // 编辑提交:内容已写入,解锁事件失败。 failNextUnlock = true; await expect( runtime.editPendingInputById({ pendingInputId: queued.pendingInputId, newText: "edited", editorId: "editor-a", traceContext, }), ).rejects.toThrow("unlock append failed"); await expectStillLocked(); // 手动解除:解锁事件失败,锁保留;重试成功后才删除。 failNextUnlock = true; await expect( runtime.releasePendingInputEdit({ pendingInputId: queued.pendingInputId, traceContext }), ).rejects.toThrow("unlock append failed"); await expectStillLocked(); await expect( runtime.releasePendingInputEdit({ pendingInputId: queued.pendingInputId, traceContext }), ).resolves.toBe(true); expect(runtime.getPendingInputEditLock(queued.pendingInputId)).toBeUndefined(); const lockEvents = (await baseStore.getEvents(sessionId)) .filter((event) => event.type === SessionEventType.TurnSteerEditLockChanged) .map((event) => Boolean((event.payload as { editLock?: unknown }).editLock)); expect(lockEvents).toEqual([true, false]); finishModel.resolve(); await pendingTurn; }); it("discards queued-but-not-drained steering input during resume", async () => { const sessionId = createSessionId("runtime-turn-steer-recover"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const firstRuntime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-steer-recover" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { return { finishReason: "stop", providerMetadata: undefined, text: "first answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), sessionStore, }, ); const first = await firstRuntime.executeTurn("first prompt"); await eventStore.append( createSessionEvent( SessionEventType.TurnSteerQueued, sessionId, { pendingInputId: "pending_recover_1", input: "queued before crash", inputPreview: "queued before crash", inputSize: 19, queueLength: 1, targetTurnId: first.turnId, }, { traceId: first.traceId, turnId: first.turnId, }, ), ); let capturedMessages: any[] = []; const secondRuntime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-steer-recover" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { capturedMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "second answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), sessionStore, }, ); await secondRuntime.resumeFromStore(); const projection = await secondRuntime.getProjection(); const result = await secondRuntime.executeTurn("after resume"); expect(projection.pendingSteerInputs).toHaveLength(0); expect(result.response).toBe("second answer"); expect(capturedMessages.map((message) => providerContentToText(message.content))).not.toContain( "queued before crash", ); expect(capturedMessages.map((message) => providerContentToText(message.content))).toContain( "after resume", ); const eventTypes = (await eventStore.getEvents(sessionId)).map((event) => event.type); expect(eventTypes).toContain(SessionEventType.TurnSteerDiscarded); expect(eventTypes).not.toContain(SessionEventType.TurnSteerDrained); }); it("does not inject persisted todos into model context during resume", async () => { const sessionId = createSessionId("runtime-todo-resume-context"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const firstRuntime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-todo-resume-context" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { return { finishReason: "stop", providerMetadata: undefined, text: "first answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), sessionStore, }, ); await firstRuntime.executeTurn("first prompt"); await sessionStore.updateTodos({ sessionID: sessionId, todos: [ { content: "Restore ACP plan projection", priority: "high", status: "in_progress", }, { content: "Keep TodoRead available", priority: "medium", status: "pending", }, ], }); let capturedMessages: any[] = []; const secondRuntime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/tmp/zcode-todo-resume-context" }, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { capturedMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: "second answer", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), sessionStore, }, ); await secondRuntime.resumeFromStore(); const result = await secondRuntime.executeTurn("after resume"); const contextText = providerMessagesToText(capturedMessages); const resumedEvent = (await eventStore.getEvents(sessionId)).find( (event) => event.type === SessionEventType.SessionResumed, ); expect(result.response).toBe("second answer"); expect(contextText).not.toContain("Current session todo state (authoritative):"); expect(contextText).not.toContain("1. [in_progress][high] Restore ACP plan projection"); expect(contextText).not.toContain("2. [pending][medium] Keep TodoRead available"); expect(resumedEvent?.payload).toMatchObject({ resumedTodoCount: 2, }); }); it("executes streamed tool calls through the existing tool executor", async () => { const sessionId = createSessionId("runtime-stream-tool-call"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); let modelCallCount = 0; let secondRequest: any; let firstFinishEmitted = false; let toolStartedBeforeFirstFinish = false; let toolHandlerCallCount = 0; const toolStarted = deferred(); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "StreamRead", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { toolHandlerCallCount++; toolStartedBeforeFirstFinish = !firstFinishEmitted; toolStarted.resolve(); return "stream-read-ok"; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelStreaming: "on" }, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText() { throw new Error("generateText should not be used"); }, async *streamText(request: any) { modelCallCount++; if (modelCallCount === 1) { yield { type: "reasoning_start", id: "reasoning-1" }; yield { type: "reasoning_delta", id: "reasoning-1", text: "thinking before tool", }; yield { type: "reasoning_end", id: "reasoning-1" }; yield { type: "tool_call", toolCall: { id: "stream-read", name: "StreamRead", input: {}, }, }; yield { type: "tool_call", toolCall: { id: "stream-read", name: "StreamRead", input: {}, }, }; await Promise.race([toolStarted.promise, delay(100)]); firstFinishEmitted = true; yield { type: "finish", finishReason: "tool-calls", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; return; } secondRequest = request; yield { type: "text_delta", text: "stream done" }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("run streamed tool"); const events = await eventStore.getEvents(sessionId); expect(result.response).toBe("stream done"); expect(modelCallCount).toBe(2); expect(toolHandlerCallCount).toBe(1); expect(toolStartedBeforeFirstFinish).toBe(true); expect( secondRequest.messages.find((message: any) => message.role === "assistant"), ).toMatchObject({ content: [{ type: "reasoning", text: "thinking before tool" }], toolCalls: [{ id: "stream-read", name: "StreamRead", input: {} }], }); expect(secondRequest.messages.at(-2)).toMatchObject({ content: "stream-read-ok", role: "tool", toolCallId: "stream-read", toolName: "StreamRead", }); // 工具批次之后的 token 预算提醒:首轮从 0 计到上一次响应的 usage(1 + 1)。 expect(secondRequest.messages.at(-1)).toMatchObject({ content: "\n14999998 tokens left\n", role: "user", }); expect(events.map((event) => event.type)).toContain(SessionEventType.ModelStreaming); expect(events.map((event) => event.type)).toContain(SessionEventType.ToolCallScheduled); expect(events.map((event) => event.type)).toContain(SessionEventType.ToolCallResult); const ledgerPayloads = events .filter((event) => event.type === SessionEventType.StreamingToolLedgerUpdated) .map((event) => event.payload as any); const anchorPayload = events.find( (event) => event.type === SessionEventType.StreamRecoveryAnchorCreated, )?.payload as any; expect(ledgerPayloads.map((payload) => payload.status)).toEqual([ "tool_call_closed", "tool_queued", "tool_started", "tool_result_committed", ]); expect(anchorPayload).toMatchObject({ kind: "tool_result", toolCallId: "stream-read", toolName: "StreamRead", }); expect(ledgerPayloads.at(-1)).toMatchObject({ executionTiming: "during_stream", readOnly: true, recoveryAnchorId: anchorPayload.anchorId, sideEffectScope: "none", }); }); it("persists MCP presentation on streaming pending and running parts before turn stop", async () => { const sessionId = createSessionId("runtime-stream-mcp-presentation-stop"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const registry = createToolRegistry(); const toolStarted = deferred(); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "mcp__firebase__firebase_get_environment", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", mcpPresentation: { serverName: "firebase", toolName: "firebase_get_environment", description: "Read Firebase environment details.", }, }, handler: async (_input: unknown, context: ToolExecutionContext) => { toolStarted.resolve(); await waitForAbort(context.abortSignal); throw context.abortSignal.reason ?? new Error("streamed MCP tool stopped"); }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelStreaming: "on" }, { eventStore, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText() { throw new Error("generateText should not be used"); }, async *streamText(request: any) { yield { type: "tool_call", toolCall: { id: "stream-mcp-stop", name: "mcp__firebase__firebase_get_environment", input: {}, }, }; await waitForAbort(request.abortSignal); throw request.abortSignal.reason ?? new Error("streamed model stopped"); }, } as never), }, ); const pendingTurn = runtime.executeTurn("run streamed MCP tool"); await toolStarted.promise; const mcpWrites = sessionStore.partWriteHistory.filter( (part): part is Extract => part.type === "tool" && part.callID === "stream-mcp-stop", ); expect(mcpWrites.map((part) => part.state.status)).toEqual(["pending", "running"]); expect(mcpWrites[0]?.metadata).toMatchObject({ display: { kind: "mcp_tool", serverName: "firebase", toolName: "firebase_get_environment", }, }); expect(mcpWrites[1]?.state).toMatchObject({ metadata: { display: { kind: "mcp_tool", serverName: "firebase", toolName: "firebase_get_environment", }, }, status: "running", }); runtime.stopActiveForegroundExecution({ reason: "test streamed MCP stop" }); await expect(pendingTurn).rejects.toMatchObject({ type: CoreErrorType.TurnCancelled }); }); it("returns a failed tool result for sanitized malformed input and continues the run", async () => { const sessionId = createSessionId("runtime-malformed-tool-input-recovery"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const registry = createToolRegistry(); let handlerRan = false; let emptyInputHandlerRan = false; let modelCallCount = 0; let recoveryRequest: any; const emptyInputHandlerStarted = deferred(); const releaseEmptyInputHandler = deferred(); registry.register({ inputSchema: { type: "object", properties: { questions: { type: "array", minItems: 1, }, }, required: ["questions"], additionalProperties: false, }, metadata: { concurrentSafe: true, destructive: false, name: "AskUserQuestionFixture", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { handlerRan = true; return "should-not-run"; }, }); registry.register({ inputSchema: { type: "object", properties: {}, additionalProperties: false, }, metadata: { concurrentSafe: true, destructive: false, name: "EmptyInputFixture", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { emptyInputHandlerRan = true; emptyInputHandlerStarted.resolve(); await releaseEmptyInputHandler.promise; return "empty-input-ok"; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelStreaming: "on" }, { eventStore, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText() { throw new Error("generateText should not be used"); }, async *streamText(request: any) { modelCallCount++; if (modelCallCount === 1) { // adapter 已把 final tool-call 的 malformed JSON 严格降级为普通 {} input。 yield { type: "tool_call", toolCall: { id: "malformed-ask-call", input: {}, name: "AskUserQuestionFixture", }, }; yield { type: "tool_call", toolCall: { id: "empty-input-call", input: {}, name: "EmptyInputFixture", }, }; yield { type: "tool_call", toolCall: { id: "unknown-tool-call", input: {}, name: "UnknownFixture", }, }; yield { type: "tool_call", toolCall: { id: "empty-name-call", input: {}, name: "", }, }; yield { type: "finish", finishReason: "tool-calls", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; return; } recoveryRequest = request; yield { type: "text_delta", text: "recovered after malformed tool input" }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const turnPromise = runtime.executeTurn("ask a question"); await emptyInputHandlerStarted.promise; expect(modelCallCount).toBe(1); releaseEmptyInputHandler.resolve(); const result = await turnPromise; const assistantToolUse = recoveryRequest.messages.find( (message: any) => message.role === "assistant" && message.toolCalls?.length > 0, ); const failedToolResult = recoveryRequest.messages.find( (message: any) => message.role === "tool" && message.toolCallId === "malformed-ask-call", ); const successfulToolResult = recoveryRequest.messages.find( (message: any) => message.role === "tool" && message.toolCallId === "empty-input-call", ); const unknownToolResult = recoveryRequest.messages.find( (message: any) => message.role === "tool" && message.toolCallId === "unknown-tool-call", ); const emptyNameToolResult = recoveryRequest.messages.find( (message: any) => message.role === "tool" && message.toolCallId === "empty-name-call", ); const persistedEmptyNamePart = (await sessionStore.messages({ sessionID: sessionId })) .flatMap((message) => message.parts) .find( (part) => part.type === "tool" && part.callID === "empty-name-call" && part.state.status === "error", ); const scheduledToolCallIds = (await eventStore.getEvents(sessionId)) .filter((event) => event.type === SessionEventType.ToolCallScheduled) .map((event) => (event.payload as any).toolCallId); expect(modelCallCount).toBe(2); expect(handlerRan).toBe(false); expect(emptyInputHandlerRan).toBe(true); expect(result.response).toBe("recovered after malformed tool input"); expect(assistantToolUse?.toolCalls).toEqual([ { blockIndex: 0, id: "malformed-ask-call", input: {}, name: "AskUserQuestionFixture", }, { blockIndex: 1, id: "empty-input-call", input: {}, name: "EmptyInputFixture", }, { blockIndex: 2, id: "unknown-tool-call", input: {}, name: "UnknownFixture", }, { blockIndex: 3, id: "empty-name-call", input: {}, name: "", }, ]); expect(failedToolResult).toMatchObject({ isError: true, role: "tool", toolCallId: "malformed-ask-call", toolName: "AskUserQuestionFixture", }); expect(providerContentToText(failedToolResult?.content)).toBe( [ "InputValidationError: AskUserQuestionFixture failed due to the following issue:", "The required parameter `questions` is missing", ].join("\n"), ); expect(successfulToolResult).toMatchObject({ isError: false, role: "tool", toolCallId: "empty-input-call", toolName: "EmptyInputFixture", }); expect(providerContentToText(successfulToolResult?.content)).toContain("empty-input-ok"); expect(unknownToolResult).toMatchObject({ isError: true, role: "tool", toolCallId: "unknown-tool-call", toolName: "UnknownFixture", }); expect(providerContentToText(unknownToolResult?.content)).toBe( "Error: No such tool available: UnknownFixture", ); expect(emptyNameToolResult).toMatchObject({ isError: true, role: "tool", toolCallId: "empty-name-call", toolName: "", }); expect(providerContentToText(emptyNameToolResult?.content)).toBe( "Error: No such tool available: ", ); expect(scheduledToolCallIds).not.toContain("empty-name-call"); expect(persistedEmptyNamePart).toMatchObject({ metadata: { providerToolName: "" }, state: { error: "Model returned an invalid tool call: tool name is empty.", metadata: { modelContent: "Error: No such tool available: ", }, status: "error", }, tool: "empty_tool_name", }); }); it("keeps side-effecting streamed tool calls behind the end-of-stream boundary", async () => { const sessionId = createSessionId("runtime-stream-side-effect-tool-call"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); let modelCallCount = 0; let firstFinishEmitted = false; let toolStartedBeforeFirstFinish = false; let toolHandlerCallCount = 0; const toolStarted = deferred(); registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "StreamWrite", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { toolHandlerCallCount++; toolStartedBeforeFirstFinish = !firstFinishEmitted; toolStarted.resolve(); return "stream-write-ok"; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelStreaming: "on" }, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText() { throw new Error("generateText should not be used"); }, async *streamText() { modelCallCount++; if (modelCallCount > 1) { yield { type: "text_delta", text: "write done" }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; return; } yield { type: "tool_call", toolCall: { id: "stream-write", name: "StreamWrite", input: {}, }, }; await Promise.race([toolStarted.promise, delay(100)]); firstFinishEmitted = true; yield { type: "finish", finishReason: "tool-calls", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("run streamed write tool"); const events = await eventStore.getEvents(sessionId); const ledgerPayloads = events .filter((event) => event.type === SessionEventType.StreamingToolLedgerUpdated) .map((event) => event.payload as any); expect(result.response).toBe("write done"); expect(modelCallCount).toBe(2); expect(toolHandlerCallCount).toBe(1); expect(toolStartedBeforeFirstFinish).toBe(false); expect(ledgerPayloads.map((payload) => payload.executionTiming)).toEqual([ "end_of_stream", "end_of_stream", "end_of_stream", "end_of_stream", ]); }); it("recovers a stream failure from a completed streaming tool result", async () => { const sessionId = createSessionId("runtime-stream-tool-recovery"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requestModel = createTestModelSelection("provider-old/model-old"); const selectedModel = createTestModelSelection("provider-new/model-new"); const registry = createToolRegistry(); const toolCompleted = deferred(); let modelCallCount = 0; let secondRequest: any; let toolHandlerCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "RecoverRead", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { toolHandlerCallCount++; await delay(5); toolCompleted.resolve(); return "recover-read-ok"; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelSelection: createTestModelSelection(requestModel), modelStreaming: "on", }, { eventStore, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText() { throw new Error("generateText should not be used"); }, async *streamText(request: any) { modelCallCount++; if (modelCallCount > 1) { secondRequest = request; yield { type: "text_delta", text: "recovered done" }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; return; } yield { type: "tool_call", toolCall: { id: "recover-read", name: "RecoverRead", input: {}, }, }; await toolCompleted.promise; yield { type: "text_delta", text: "partial tail" }; runtime.setSessionModelSelection(createTestModelSelection(selectedModel)); throw new Error("Model request timed out."); }, } as never), }, ); const result = await runtime.executeTurn("recover streamed tool"); const events = await eventStore.getEvents(sessionId); const recoveryEventTypes = events .map((event) => event.type) .filter((type) => [ SessionEventType.StreamRecoveryStarted, SessionEventType.StreamRecoveryAnchorSelected, SessionEventType.StreamRecoveryTailDiscarded, SessionEventType.StreamRecoveryRetryStarted, ].includes(type as any), ); const tailDiscardedPayload = events.find( (event) => event.type === SessionEventType.StreamRecoveryTailDiscarded, )?.payload as any; expect(result.response).toBe("recovered done"); expect(modelCallCount).toBe(2); expect(toolHandlerCallCount).toBe(1); expect( secondRequest.messages.find((message: any) => message.role === "assistant"), ).toMatchObject({ providerId: requestModel.providerId, modelId: requestModel.modelId, toolCalls: [{ id: "recover-read", name: "RecoverRead", input: {} }], }); expect(secondRequest.messages.at(-2)).toMatchObject({ content: "recover-read-ok", role: "tool", toolCallId: "recover-read", toolName: "RecoverRead", }); // 流恢复结算的工具结果也算完整工具批次;失败的流没有 usage,预算提醒保持满额。 expect(secondRequest.messages.at(-1)).toMatchObject({ content: "\n15000000 tokens left\n", role: "user", }); expect(recoveryEventTypes).toEqual([ SessionEventType.StreamRecoveryStarted, SessionEventType.StreamRecoveryAnchorSelected, SessionEventType.StreamRecoveryTailDiscarded, SessionEventType.StreamRecoveryRetryStarted, ]); expect(tailDiscardedPayload.discardedTextBytes).toBeGreaterThan(0); expect( sessionStore.messagesBySession .get(sessionId) ?.find((message) => message.role === "assistant" && message.finish === "tool-calls"), ).toMatchObject({ modelId: requestModel.modelId, providerId: requestModel.providerId, }); }); it.each([ { expectedPartStatus: "completed", executionState: "completed" as const }, { expectedPartStatus: "error", executionState: "running" as const }, ])( "settles a $executionState streaming tool before execution-model fallback", async ({ executionState, expectedPartStatus }) => { const sessionId = createSessionId(`runtime-execution-fallback-tool-${executionState}`); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const registry = createToolRegistry(); const toolStarted = deferred(); const toolCompleted = deferred(); const highspeedProviderId = "account:zai-highspeed-card"; let toolHandlerCallCount = 0; let fallbackRequest: any; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "FallbackRead", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async (_input: unknown, _context: ToolExecutionContext) => { toolHandlerCallCount++; toolStarted.resolve(); if (executionState === "running") { // 模拟 handler 尚未返回;executor 在 fallback abort 后生成明确的 cancellation result。 await new Promise(() => {}); } toolCompleted.resolve(); return "fallback-read-ok"; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelSelection: createTestModelSelection("user-provider/user-model"), modelStreaming: "on", titleGeneration: { enabled: false }, }, { eventStore, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText() { throw new Error("streaming runtime must not call generateText"); }, async *streamText(request: any, observation) { const providerId = String(observation.model.providerId); if (providerId !== highspeedProviderId) { fallbackRequest = request; yield { type: "text_delta", text: "fallback completed" }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; return; } yield { type: "tool_call", toolCall: { id: "fallback-read", name: "FallbackRead", input: {} }, }; await toolStarted.promise; if (executionState === "completed") await toolCompleted.promise; yield { type: "text_delta", text: "discarded accelerated tail" }; throw Object.assign(new Error("highspeed stream disconnected"), { code: "model_request_failed", context: { providerId, modelId: "GLM-5.3", reason: "network_error", retryable: true, source: "network", }, name: "AiSdkModelAdapterError", }); }, } as never), }, ); const result = await runtime.executeTurn("fallback after streamed tool", undefined, { inputId: `command-fallback-tool-${executionState}`, intent: { sourceCommandId: `command-fallback-tool-${executionState}`, kind: "sendText", requestedDelivery: "start-now", modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" }, }, modelExecution: { selectionScope: "execution", selectionFallback: { providerId: highspeedProviderId, rules: [{ reason: "highspeed_request_failed" }], }, }, }); expect(result.response).toBe("fallback completed"); expect(toolHandlerCallCount).toBe(1); const fallbackAssistant = fallbackRequest.messages.find( (message: any) => message.role === "assistant" && message.toolCalls?.length > 0, ); expect(fallbackAssistant).toMatchObject({ providerId: highspeedProviderId, modelId: "GLM-5.3", toolCalls: [{ id: "fallback-read", name: "FallbackRead", input: {} }], }); const fallbackToolResult = fallbackRequest.messages.find( (message: any) => message.role === "tool" && message.toolCallId === "fallback-read", ); expect(fallbackToolResult).toMatchObject({ role: "tool", toolCallId: "fallback-read", toolName: "FallbackRead", }); if (executionState === "completed") { expect(fallbackToolResult.content).toBe("fallback-read-ok"); } else { expect(String(fallbackToolResult.content)).toContain("Tool execution cancelled"); } const persistedParts = sessionStore.partsBySession.get(sessionId) ?? []; const persistedToolParts = persistedParts.filter( (part): part is Extract => part.type === "tool", ); expect(persistedToolParts).toHaveLength(1); expect(persistedToolParts[0]?.state.status).toBe(expectedPartStatus); const discardedAssistant = sessionStore.messagesBySession .get(sessionId) ?.find( (message) => message.role === "assistant" && message.providerId === highspeedProviderId && message.error?.name === STREAM_RECOVERY_DISCARDED_ERROR_NAME, ); expect(discardedAssistant).toMatchObject({ finish: STREAM_RECOVERY_DISCARDED_FINISH, error: { name: STREAM_RECOVERY_DISCARDED_ERROR_NAME }, }); const coldHistory = new MessageHistoryImpl(); await hydrateMessageHistoryFromSession({ history: coldHistory, messages: await sessionStore.messages({ sessionID: sessionId }), }); const coldEntries = coldHistory.toRuntimeEntries(); expect(coldEntries).toContainEqual( expect.objectContaining({ message: expect.objectContaining({ role: "assistant", toolCalls: [{ id: "fallback-read", name: "FallbackRead", input: {}, blockIndex: 0 }], }), }), ); expect(coldEntries).toContainEqual( expect.objectContaining({ message: expect.objectContaining({ role: "tool", toolCallId: "fallback-read", }), }), ); const serializedColdEntries = JSON.stringify(coldEntries); expect(serializedColdEntries).not.toContain("[Tool execution was interrupted before resume]"); expect(serializedColdEntries).not.toContain("discarded accelerated tail"); const turnComplete = (await eventStore.getEvents(sessionId)).findLast( (event) => event.type === SessionEventType.TurnComplete, ); expect(turnComplete?.payload).toMatchObject({ historyRoundCount: 2 }); }, ); it.each([ { expectedPartStatus: "completed", executionState: "completed" as const }, { expectedPartStatus: "error", executionState: "running" as const }, ])( "settles a $executionState streaming tool when execution fallback has no resolvable target", async ({ executionState, expectedPartStatus }) => { const sessionId = createSessionId( `runtime-execution-fallback-unavailable-tool-${executionState}`, ); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const registry = createToolRegistry(); const toolStarted = deferred(); const toolCompleted = deferred(); const highspeedProviderId = "account:zai-highspeed-card"; const unavailableFallbackProviderId = "ghost-provider"; const streamedProviders: string[] = []; let toolHandlerCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "UnavailableFallbackRead", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { toolHandlerCallCount++; toolStarted.resolve(); if (executionState === "running") { await new Promise(() => {}); } toolCompleted.resolve(); return "unavailable-fallback-read-ok"; }, }); const highspeedModelFactory = createTestModelFactory({ async generateText() { throw new Error("streaming runtime must not call generateText"); }, async *streamText(_request: any, observation) { const providerId = String(observation.model.providerId); streamedProviders.push(providerId); yield { type: "tool_call", toolCall: { id: "unavailable-fallback-read", name: "UnavailableFallbackRead", input: {}, }, }; await toolStarted.promise; if (executionState === "completed") await toolCompleted.promise; throw Object.assign(new Error("highspeed unavailable-target stream failed"), { code: "model_request_failed", context: { providerId, modelId: "GLM-5.3", reason: "network_error", retryable: true, source: "network", }, name: "AiSdkModelAdapterError", }); }, } as never); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelSelection: createTestModelSelection("user-provider/user-model"), modelStreaming: "on", titleGeneration: { enabled: false }, }, { eventStore, sessionStore, toolRegistry: registry, modelFactory: (factoryInput) => { if (factoryInput.selection.providerId === unavailableFallbackProviderId) { throw new Error("fallback provider is unavailable"); } return highspeedModelFactory(factoryInput); }, }, ); runtime.setSessionModelSelection(undefined); const failure = (await runtime .executeTurn("fail after streamed tool without fallback target", undefined, { inputId: `command-unavailable-fallback-tool-${executionState}`, intent: { sourceCommandId: `command-unavailable-fallback-tool-${executionState}`, kind: "sendText", requestedDelivery: "start-now", modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" }, }, modelExecution: { selectionScope: "execution", selectionFallback: { providerId: highspeedProviderId, rules: [{ reason: "highspeed_request_failed" }], target: { providerId: unavailableFallbackProviderId, modelId: "ghost-model" }, }, }, }) .then( () => undefined, (error: unknown) => error, )) as (Error & { cause?: unknown }) | undefined; expect(failure).toBeInstanceOf(Error); const causeMessage = failure?.cause instanceof Error ? failure.cause.message : undefined; expect([failure?.message, causeMessage]).toContain( "highspeed unavailable-target stream failed", ); expect(streamedProviders).toEqual([highspeedProviderId]); expect(toolHandlerCallCount).toBe(1); const persistedToolParts = (sessionStore.partsBySession.get(sessionId) ?? []).filter( (part): part is Extract => part.type === "tool", ); expect(persistedToolParts).toHaveLength(1); expect(persistedToolParts[0]?.state.status).toBe(expectedPartStatus); const projectToolHistory = (entries: ReturnType) => entries.flatMap((entry) => { const message = (entry as any).message; if (!message) return []; if ( message.role === "assistant" && message.toolCalls?.some( (toolCall: { id: string }) => toolCall.id === "unavailable-fallback-read", ) ) { return [ { role: message.role, toolCalls: message.toolCalls, }, ]; } if (message.role === "tool" && message.toolCallId === "unavailable-fallback-read") { return [ { content: message.content, role: message.role, toolCallId: message.toolCallId, toolName: message.toolName, }, ]; } return []; }); const liveToolHistory = projectToolHistory( (runtime as any).messageHistory.toRuntimeEntries(), ); const coldHistory = new MessageHistoryImpl(); await hydrateMessageHistoryFromSession({ history: coldHistory, messages: await sessionStore.messages({ sessionID: sessionId }), }); const coldToolHistory = projectToolHistory(coldHistory.toRuntimeEntries()); expect(coldToolHistory).toEqual(liveToolHistory); expect(coldToolHistory).toHaveLength(2); if (executionState === "completed") { expect(JSON.stringify(coldToolHistory)).toContain("unavailable-fallback-read-ok"); } expect(JSON.stringify(coldToolHistory)).not.toContain( "[Tool execution was interrupted before resume]", ); const events = await eventStore.getEvents(sessionId); expect( events.some((event) => event.type === SessionEventType.TurnExecutionModelFallback), ).toBe(false); expect( events.some( (event) => event.type === SessionEventType.StreamingToolLedgerUpdated && (event.payload as any).status === "tool_abandoned", ), ).toBe(false); }, ); it("resets output continuation after fallback settles a streaming tool anchor", async () => { const sessionId = createSessionId("runtime-execution-fallback-tool-resets-continuation"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const toolCompleted = deferred(); const highspeedProviderId = "account:zai-highspeed-card"; let highspeedCallCount = 0; let fallbackCallCount = 0; let toolHandlerCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "ContinuationResetRead", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { toolHandlerCallCount++; toolCompleted.resolve(); return "continuation-reset-read-ok"; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelSelection: createTestModelSelection("user-provider/user-model"), modelStreaming: "on", titleGeneration: { enabled: false }, }, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText() { throw new Error("streaming runtime must not call generateText"); }, async *streamText(_request: any, observation) { const providerId = String(observation.model.providerId); if (providerId === highspeedProviderId) { highspeedCallCount++; if (highspeedCallCount <= 3) { yield { type: "text_delta", text: `accelerated part ${highspeedCallCount}` }; yield { type: "finish", finishReason: "length", usage: {} }; return; } yield { type: "tool_call", toolCall: { id: "continuation-reset-read", name: "ContinuationResetRead", input: {}, }, }; await toolCompleted.promise; throw Object.assign(new Error("highspeed failed after continuation tool"), { code: "model_request_failed", context: { providerId, modelId: "GLM-5.3", reason: "network_error", retryable: true, source: "network", }, name: "AiSdkModelAdapterError", }); } fallbackCallCount++; if (fallbackCallCount === 1) { yield { type: "text_delta", text: "fallback partial" }; yield { type: "finish", finishReason: "length", usage: {} }; return; } yield { type: "text_delta", text: "fallback completed" }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const result = await runtime.executeTurn("continue through fallback tool anchor", undefined, { inputId: "command-fallback-tool-continuation-reset", intent: { sourceCommandId: "command-fallback-tool-continuation-reset", kind: "sendText", requestedDelivery: "start-now", modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" }, }, modelExecution: { selectionScope: "execution", selectionFallback: { providerId: highspeedProviderId, rules: [{ reason: "highspeed_request_failed" }], }, }, }); expect(result.response).toBe("fallback completed"); expect(highspeedCallCount).toBe(4); expect(fallbackCallCount).toBe(2); expect(toolHandlerCallCount).toBe(1); }); it("recovers a retryable stream failure after partial text by starting a new stream", async () => { const sessionId = createSessionId("runtime-stream-partial-text-recovery"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const requestModel = createTestModelSelection("provider-old/model-old"); const selectedModel = createTestModelSelection("provider-new/model-new"); let modelCallCount = 0; let secondRequest: any; let secondRequestObservation: TestModelExecutionObservation | undefined; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelSelection: createTestModelSelection(requestModel), modelStreaming: "on", }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText() { throw new Error("generateText should not be used"); }, async *streamText(request: any, observation) { modelCallCount++; if (modelCallCount > 1) { secondRequest = request; secondRequestObservation = observation; await publishTestModelNetworkStatus(observation, { requestId: "model_req_partial_recovery_2", type: "model_request_started", }); yield { type: "text_delta", text: "recovered answer" }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; await publishTestModelNetworkStatus(observation, { durationMs: 10, finishReason: "stop", requestId: "model_req_partial_recovery_2", type: "model_request_completed", }); return; } await publishTestModelNetworkStatus(observation, { requestId: "model_req_partial_recovery_1", type: "model_request_started", }); yield { type: "text_delta", text: "partial answer" }; await publishTestModelNetworkStatus(observation, { idleMs: 60000, message: "Model stream stalled: no event received for 60000ms.", requestId: "model_req_partial_recovery_1", timeoutMs: 60000, type: "model_stream_stalled", }); const error = new Error( "Model stream stalled: no event received for 60000ms.", ) as Error & { code?: string; context?: Record }; error.code = "model_request_timeout"; error.context = { reason: "stream_idle_timeout", retryable: true, }; await publishTestModelNetworkStatus(observation, { durationMs: 60000, message: error.message, reason: "stream_idle_timeout", requestId: "model_req_partial_recovery_1", retryable: true, type: "model_request_failed", }); runtime.setSessionModelSelection(createTestModelSelection(selectedModel)); throw error; }, } as never), }, ); const result = await runtime.executeTurn("recover partial text"); const events = await eventStore.getEvents(sessionId); const recoveryEventTypes = events .map((event) => event.type) .filter((type) => [ SessionEventType.StreamRecoveryStarted, SessionEventType.StreamRecoveryAnchorSelected, SessionEventType.StreamRecoveryTailDiscarded, SessionEventType.StreamRecoveryRetryStarted, ].includes(type as any), ); const tailDiscardedPayload = events.find( (event) => event.type === SessionEventType.StreamRecoveryTailDiscarded, )?.payload as any; const anchorSelectedPayload = events.find( (event) => event.type === SessionEventType.StreamRecoveryAnchorSelected, )?.payload as any; const recoveryStartedPayload = events.find( (event) => event.type === SessionEventType.StreamRecoveryStarted, )?.payload as any; const recoveryRetryStartedPayload = events.find( (event) => event.type === SessionEventType.StreamRecoveryRetryStarted, )?.payload as any; const recoveredRequestStartedPayload = events .filter((event) => event.type === SessionEventType.ModelNetworkStatus) .map((event) => event.payload as any) .find((payload) => payload.requestId === "model_req_partial_recovery_2"); const textDeltaAssistantIds = events .filter((event) => event.type === SessionEventType.ModelStreaming) .map((event) => event.payload as any) .filter((payload) => payload.kind === "text_delta") .map((payload) => payload.assistantMessageId); expect(result.response).toBe("recovered answer"); expect(modelCallCount).toBe(2); expect(secondRequestObservation?.invocationContext?.streamIdleTimeoutRetryNumber).toBe(1); expect(secondRequest.messages.filter((message: any) => message.role === "assistant")).toEqual( [], ); expect(recoveryEventTypes).toEqual([ SessionEventType.StreamRecoveryStarted, SessionEventType.StreamRecoveryAnchorSelected, SessionEventType.StreamRecoveryTailDiscarded, SessionEventType.StreamRecoveryRetryStarted, ]); expect(anchorSelectedPayload).toMatchObject({ committedToolCallIds: [], reason: "no_tool_committed", }); expect(recoveryStartedPayload).toMatchObject({ failedRequestId: "model_req_partial_recovery_1", retryNumber: 1, maxRetries: 10, }); expect(recoveryRetryStartedPayload).toMatchObject({ failedRequestId: "model_req_partial_recovery_1", retryNumber: 1, maxRetries: 10, }); expect(recoveredRequestStartedPayload).toMatchObject({ streamRecovery: { recoveredFromRequestId: "model_req_partial_recovery_1", retryNumber: 1, maxRetries: 10, }, type: "model_request_started", }); expect(tailDiscardedPayload).toMatchObject({ discardedToolCallIds: [], }); expect(tailDiscardedPayload.discardedTextBytes).toBeGreaterThan(0); expect(new Set(textDeltaAssistantIds).size).toBe(2); expect( sessionStore.messagesBySession .get(sessionId) ?.find( (message) => message.role === "assistant" && message.finish === "stream_recovery_discarded", ), ).toMatchObject({ modelId: requestModel.modelId, providerId: requestModel.providerId, }); }); it("retries stream recovery up to ten times after partial text stalls", async () => { const sessionId = createSessionId("runtime-stream-partial-text-recovery-budget"); const eventStore = createTestSessionEventStore(); let modelCallCount = 0; const streamIdleTimeoutRetryNumbers: unknown[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelStreaming: "on" }, { eventStore, modelFactory: createTestModelFactory({ async generateText() { throw new Error("generateText should not be used"); }, async *streamText(_request: any, observation) { modelCallCount++; streamIdleTimeoutRetryNumbers.push( observation.invocationContext?.streamIdleTimeoutRetryNumber, ); if (modelCallCount <= 10) { yield { type: "text_delta", text: `partial ${modelCallCount}` }; const error = new Error( "Model stream stalled: no event received for 60000ms.", ) as Error & { code?: string; context?: Record }; error.code = "model_request_timeout"; error.context = { reason: "stream_idle_timeout", retryable: true, }; throw error; } yield { type: "text_delta", text: "recovered after budget" }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("recover partial text within budget"); const events = await eventStore.getEvents(sessionId); const retryStartedPayloads = events .filter((event) => event.type === SessionEventType.StreamRecoveryRetryStarted) .map((event) => event.payload as any); expect(result.response).toBe("recovered after budget"); expect(streamIdleTimeoutRetryNumbers).toEqual([undefined, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); expect(retryStartedPayloads.map((payload) => payload.retryNumber)).toEqual([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ]); expect(retryStartedPayloads.every((payload) => payload.maxRetries === 10)).toBe(true); }); it("marks Start Plan busy stream recovery exhaustion in the final turn error", async () => { const sessionId = createSessionId("runtime-start-plan-busy-recovery-exhausted"); const eventStore = createTestSessionEventStore(); let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelStreaming: "on" }, { eventStore, modelFactory: createTestModelFactory({ async generateText() { throw new Error("generateText should not be used"); }, async *streamText() { modelCallCount++; yield { type: "text_delta", text: `partial ${modelCallCount}` }; const error = new Error("当前系统繁忙,请稍后再试或升级账户。") as Error & { code?: string; context?: Record; }; error.code = "model_rate_limited"; error.context = { providerCode: "3010", reason: "rate_limited", retryable: true, }; throw error; }, } as never), }, ); await expect(runtime.executeTurn("recover busy until exhausted")).rejects.toMatchObject({ type: CoreErrorType.UnknownError, }); const events = await eventStore.getEvents(sessionId); const turnErrorPayload = events.find((event) => event.type === SessionEventType.TurnError) ?.payload as any; expect(modelCallCount).toBe(11); expect( events.filter((event) => event.type === SessionEventType.StreamRecoveryRetryStarted), ).toHaveLength(10); expect(turnErrorPayload.error).toMatchObject({ code: "3010", message: START_PLAN_BUSY_AUTO_RETRY_EXHAUSTED_MESSAGE, }); expect(turnErrorPayload.error.detail).toContain("provider_code=3010"); }); it("does not retry Start Plan busy admission failures on the first turn", async () => { const sessionId = createSessionId("runtime-start-plan-first-admission-busy-no-retry"); const eventStore = createTestSessionEventStore(); const providerId = "account:zai-start-plan"; let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelSelection: { modelId: "GLM-5.2" as never, providerId: providerId as never, }, modelStreaming: "on", }, { eventStore, modelFactory: createTestModelFactory({ async generateText() { throw new Error("generateText should not be used"); }, async *streamText() { modelCallCount++; yield { type: "error", error: createProviderBusyError(providerId) }; }, } as never), }, ); await expect(runtime.executeTurn("first prompt")).rejects.toMatchObject({ type: CoreErrorType.ModelError, }); const events = await eventStore.getEvents(sessionId); expect(modelCallCount).toBe(1); expect( events.filter((event) => event.type === SessionEventType.StreamRecoveryRetryStarted), ).toHaveLength(0); }); it("retries Start Plan busy admission failures after a visible turn", async () => { vi.useFakeTimers(); const sessionId = createSessionId("runtime-start-plan-second-admission-busy-retry"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const providerId = "account:zai-start-plan"; const requestModel = { modelId: "GLM-5.2" as never, providerId: providerId as never, }; const selectedModel = createTestModelSelection("provider-new/model-new"); let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelSelection: createTestModelSelection(requestModel), modelStreaming: "on", }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText() { throw new Error("generateText should not be used"); }, async *streamText() { modelCallCount++; if (modelCallCount === 1) { yield { type: "text_delta", text: "first done" }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; return; } if (modelCallCount === 2) { runtime.setSessionModelSelection(createTestModelSelection(selectedModel)); yield { type: "error", error: createProviderBusyError(providerId), }; return; } yield { type: "text_delta", text: "second done" }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); try { const first = await runtime.executeTurn("first prompt"); const secondPromise = runtime.executeTurn("second prompt"); await vi.advanceTimersByTimeAsync(1_000); const second = await secondPromise; const events = await eventStore.getEvents(sessionId); const retryStartedPayloads = events .filter((event) => event.type === SessionEventType.StreamRecoveryRetryStarted) .map((event) => event.payload as any); expect(first.response).toBe("first done"); expect(second.response).toBe("second done"); expect(modelCallCount).toBe(3); expect(retryStartedPayloads).toHaveLength(1); expect(retryStartedPayloads[0]).toMatchObject({ retryNumber: 1, maxRetries: 2, }); expect( sessionStore.messagesBySession .get(sessionId) ?.find( (message) => message.role === "assistant" && message.finish === "start_plan_admission_retry_discarded", ), ).toMatchObject({ modelId: requestModel.modelId, providerId: requestModel.providerId, }); } finally { vi.useRealTimers(); } }); it("continues stream recovery with a synthetic failed result for side-effecting tools", async () => { const sessionId = createSessionId("runtime-stream-side-effect-recovery"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); let modelCallCount = 0; let secondRequest: any; let toolHandlerCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "StreamWrite", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { toolHandlerCallCount++; return "stream-write-should-not-run"; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelStreaming: "on" }, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText() { throw new Error("generateText should not be used"); }, async *streamText(request: any) { modelCallCount++; if (modelCallCount > 1) { secondRequest = request; yield { type: "text_delta", text: "continued after synthetic failure", }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; return; } yield { type: "tool_call", toolCall: { id: "stream-write-recovery", name: "StreamWrite", input: { path: "demo.txt" }, }, }; yield { type: "text_delta", text: "partial write tail" }; throw new Error("Turn failed"); }, } as never), }, ); const result = await runtime.executeTurn("recover streamed write tool"); const events = await eventStore.getEvents(sessionId); // 最后一条是这批工具之后的 token 预算提醒,工具结果在它之前。 const toolMessage = secondRequest.messages.at(-2); expect(providerContentToText(secondRequest.messages.at(-1)?.content)).toContain( "", ); const ledgerPayloads = events .filter((event) => event.type === SessionEventType.StreamingToolLedgerUpdated) .map((event) => event.payload as any); const anchorPayload = events.find( (event) => event.type === SessionEventType.StreamRecoveryAnchorCreated && (event.payload as any).toolCallId === "stream-write-recovery", )?.payload as any; const toolErrorPayload = events.find((event) => event.type === SessionEventType.ToolCallError) ?.payload as any; expect(result.response).toBe("continued after synthetic failure"); expect(modelCallCount).toBe(2); expect(toolHandlerCallCount).toBe(0); expect( secondRequest.messages.find((message: any) => message.role === "assistant"), ).toMatchObject({ toolCalls: [ { id: "stream-write-recovery", name: "StreamWrite", input: { path: "demo.txt" }, }, ], }); expect(toolMessage).toMatchObject({ role: "tool", toolCallId: "stream-write-recovery", toolName: "StreamWrite", }); expect(toolMessage.content).toContain("before this tool was executed"); expect(toolErrorPayload).toMatchObject({ error: { type: "stream_recovery_interrupted_tool" }, toolCallId: "stream-write-recovery", }); expect(ledgerPayloads.map((payload) => payload.status)).toEqual([ "tool_call_closed", "tool_result_committed", ]); expect(ledgerPayloads.map((payload) => payload.executionTiming)).toEqual([ "during_stream", "during_stream", ]); expect(anchorPayload).toMatchObject({ kind: "tool_error", toolCallId: "stream-write-recovery", toolName: "StreamWrite", }); }); it("keeps the interrupted-stream error when an empty-name final call arrives before failure", async () => { const sessionId = createSessionId("runtime-stream-empty-name-recovery"); const eventStore = createTestSessionEventStore(); let modelCallCount = 0; let secondRequest: any; const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", modelStreaming: "on" }, { eventStore, toolRegistry: createToolRegistry(), modelFactory: createTestModelFactory({ async generateText() { throw new Error("generateText should not be used"); }, async *streamText(request: any) { modelCallCount++; if (modelCallCount > 1) { secondRequest = request; yield { type: "text_delta", text: "continued after empty-name interruption" }; yield { type: "finish", finishReason: "stop", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; return; } yield { type: "tool_call", toolCall: { id: "stream-empty-name-recovery", name: "", input: {}, }, }; yield { type: "text_delta", text: "partial tail" }; throw new Error("Turn failed"); }, } as never), }, ); const result = await runtime.executeTurn("recover empty-name streamed tool"); const events = await eventStore.getEvents(sessionId); const assistantMessage = secondRequest.messages.find( (message: any) => message.role === "assistant", ); const toolMessage = secondRequest.messages.find( (message: any) => message.role === "tool" && message.toolCallId === "stream-empty-name-recovery", ); const toolErrorPayload = events.find( (event) => event.type === SessionEventType.ToolCallError && (event.payload as any).toolCallId === "stream-empty-name-recovery", )?.payload as any; expect(result.response).toBe("continued after empty-name interruption"); expect(modelCallCount).toBe(2); expect(assistantMessage).toMatchObject({ toolCalls: [ { id: "stream-empty-name-recovery", name: "", input: {}, }, ], }); expect(toolMessage).toMatchObject({ isError: true, role: "tool", toolCallId: "stream-empty-name-recovery", toolName: "", }); expect(providerContentToText(toolMessage?.content)).toContain("before this tool was executed"); expect(providerContentToText(toolMessage?.content)).not.toContain("No such tool available"); expect(toolErrorPayload).toMatchObject({ error: { type: "stream_recovery_interrupted_tool" }, toolCallId: "stream-empty-name-recovery", }); }); it("emits workspace checkpoint events for file mutation tool results", async () => { const sessionId = createSessionId("runtime-file-mutation-checkpoint"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteLike", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => ({ content: "export const value = 2;\n", filePath: "/work/src/demo.ts", originalFile: "export const value = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const value = 1;", "+export const value = 2;"], }, ], type: "update", }), }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work" }, { artifactStore, eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "write-like", name: "WriteLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("write with checkpoint"); const events = await eventStore.getEvents(sessionId); const checkpointEvents = events.filter( (event) => event.type === SessionEventType.CheckpointCreated, ); const checkpoints = await runtime.listWorkspaceCheckpoints(); const artifact = JSON.parse(artifactStore.requests[0]?.content ?? "{}"); expect(result.response).toBe("done"); expect(checkpointEvents).toHaveLength(1); expect(checkpoints).toHaveLength(1); expect(checkpoints[0]).toMatchObject({ checkpointId: expect.stringMatching(/^checkpoint_/), fileCount: 1, messageId: expect.stringMatching(/^msg_/), scope: RewindScope.Workspace, }); expect(checkpointEvents[0]?.payload).toMatchObject({ diffRef: "zcode-artifact://test/artifact-1", fileCount: 1, scope: RewindScope.Workspace, snapshotRef: "zcode-artifact://test/artifact-1", }); const checkpointPayload = checkpointEvents[0]!.payload as { messageId?: string; }; expect(checkpointPayload.messageId).toMatch(/^msg_/); expect(artifactStore.requests).toHaveLength(1); expect(artifactStore.requests[0]).toMatchObject({ contentType: "application/vnd.zcode.workspace-checkpoint+json", toolCallId: "write-like", toolName: "WriteLike", }); expect(artifact).toMatchObject({ files: [ { beforeContent: "export const value = 1;\n", existedBefore: true, path: "/work/src/demo.ts", }, ], kind: "workspace_file_before_change", toolCallId: "write-like", toolName: "WriteLike", version: 1, }); }); it("keeps checkpoint and file summary coverage for approved files outside the workspace", async () => { const sessionId = createSessionId("runtime-external-memory-file"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteLike", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => ({ content: "- 喜欢吃炸鸡\n", filePath: "C:\\Users\\dev\\.zcode\\cli\\memories\\projects\\z-code\\memory\\MEMORY.md", originalFile: null, structuredPatch: [ { oldStart: 0, oldLines: 0, newStart: 1, newLines: 1, lines: ["+- 喜欢吃炸鸡"], }, ], type: "create", }), }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "C:\\Users\\dev\\Projects\\z-code" }, { artifactStore, eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "write-memory", name: "WriteLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("remember preference"); const events = await eventStore.getEvents(sessionId); expect( events.filter((event) => event.type === SessionEventType.CheckpointCreated), ).toHaveLength(1); expect(artifactStore.requests).toHaveLength(1); expect( events.filter((event) => event.type === SessionEventType.ModelComplete).at(-1)?.payload, ).toHaveProperty("fileChanges"); }); it("adds per-turn file change summary to the final main model complete", async () => { const sessionId = createSessionId("runtime-file-change-summary"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteLike", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async (input: any) => ({ content: input.after, filePath: "/work/src/demo.ts", originalFile: input.before, structuredPatch: [ { oldStart: 1, oldLines: input.before.split("\n").filter(Boolean).length, newStart: 1, newLines: input.after.split("\n").filter(Boolean).length, lines: [], }, ], type: "update", }), }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work" }, { artifactStore, eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [ { id: "write-like-1", name: "WriteLike", input: { before: "a\nb\n", after: "a\nc\n" }, }, { id: "write-like-2", name: "WriteLike", input: { before: "a\nc\n", after: "a\nc\nd\n" }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); await runtime.executeTurn("write twice"); const events = await eventStore.getEvents(sessionId); const finalModelComplete = events .filter((event) => event.type === SessionEventType.ModelComplete) .at(-1); expect(finalModelComplete?.payload).toMatchObject({ fileChanges: { files: 1, additions: 2, deletions: 1, items: [ { path: "/work/src/demo.ts", additions: 2, deletions: 1, writeCount: 2, }, ], }, querySource: "main_turn", toolCallCount: 0, }); }); it("adds per-turn file change summary to the final subagent model complete", async () => { const sessionId = createSessionId("runtime-subagent-file-change-summary"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); const fileSystemPort = new MemoryFileSystem({}); const sessionStore = new RecordingSessionStore(); let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteLike", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { fileSystemPort.files.set("/work/src/child.ts", "child\n"); return { content: "child\n", filePath: "/work/src/child.ts", originalFile: null, structuredPatch: [ { oldStart: 0, oldLines: 0, newStart: 1, newLines: 1, lines: ["+child"], }, ], type: "create", }; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", taskType: "subagent_child", workingDirectory: "/work" }, { artifactStore, eventStore, fileSystemPort, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "child-write", name: "WriteLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("write from child"); const events = await eventStore.getEvents(sessionId); const finalModelComplete = events .filter((event) => event.type === SessionEventType.ModelComplete) .at(-1); expect(finalModelComplete?.payload).toMatchObject({ fileChanges: { additions: 1, deletions: 0, files: 1, items: [ { additions: 1, deletions: 0, path: "/work/src/child.ts", writeCount: 1, }, ], }, querySource: "subagent", toolCallCount: 0, }); expect(sessionStore.sessionEntryRecords.get(sessionId)).toEqual( expect.arrayContaining([ expect.objectContaining({ type: "runtime/workspace_checkpoint", data: expect.objectContaining({ payload: expect.objectContaining({ scope: RewindScope.Workspace, snapshotRef: "zcode-artifact://test/artifact-1", }), }), }), ]), ); const resumedEventStore = createTestSessionEventStore(); const resumedRuntime = createTestAgentRuntime( sessionId, { mode: "yolo", taskType: "subagent_child", workingDirectory: "/work" }, { artifactStore, eventStore: resumedEventStore, fileSystemPort, sessionStore, toolRegistry: createToolRegistry(), modelFactory: createTestModelFactory({ async generateText() { throw new Error("resumed preview must not call the model"); }, } as never), }, ); await resumedRuntime.resumeFromStore(); const preview = await resumedRuntime.previewWorkspaceFileRewind({ targetTurnId: finalModelComplete?.turnId, }); expect(preview).toMatchObject({ canApply: true, safeFiles: [ { action: "delete", operationCount: 1, path: "/work/src/child.ts", }, ], unsafeFiles: [], }); const applied = await resumedRuntime.applyWorkspaceFileRewind({ targetTurnId: finalModelComplete?.turnId, }); expect(applied.applied).toBe(true); expect(fileSystemPort.files.has("/work/src/child.ts")).toBe(false); expect(sessionStore.sessionEntryRecords.get(sessionId)).toEqual( expect.arrayContaining([ expect.objectContaining({ type: "runtime/workspace_file_rewind", data: expect.objectContaining({ payload: expect.objectContaining({ reason: "file_summary_rewind", scope: RewindScope.Workspace, }), }), }), ]), ); const reopenedEventStore = createTestSessionEventStore(); const reopenedRuntime = createTestAgentRuntime( sessionId, { mode: "yolo", taskType: "subagent_child", workingDirectory: "/work" }, { artifactStore, eventStore: reopenedEventStore, fileSystemPort, sessionStore, toolRegistry: createToolRegistry(), modelFactory: createTestModelFactory({ async generateText() { throw new Error("reopened session must not call the model"); }, } as never), }, ); await reopenedRuntime.resumeFromStore(); expect(await reopenedEventStore.getEvents(sessionId)).toEqual( expect.arrayContaining([ expect.objectContaining({ type: SessionEventType.RewindTriggered, payload: expect.objectContaining({ reason: "file_summary_rewind" }), }), ]), ); }); it("restores the latest workspace checkpoint through /rewind latest", async () => { const sessionId = createSessionId("runtime-file-rewind"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); const sessionStore = new RecordingSessionStore(); const fileSystemPort = new MemoryFileSystem({ "/work/src/demo.ts": "export const value = 1;\n", }); let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteLike", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { fileSystemPort.files.set("/work/src/demo.ts", "export const value = 2;\n"); return { content: "export const value = 2;\n", filePath: "/work/src/demo.ts", originalFile: "export const value = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const value = 1;", "+export const value = 2;"], }, ], type: "update", }; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work" }, { artifactStore, eventStore, fileSystemPort, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "write-like", name: "WriteLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); await runtime.executeTurn("write then rewind"); expect(fileSystemPort.files.get("/work/src/demo.ts")).toBe("export const value = 2;\n"); const rewind = await runtime.executeTurn("/rewind latest"); const events = await eventStore.getEvents(sessionId); const rewindEvent = events.find((event) => event.type === SessionEventType.RewindTriggered); expect(rewind.response).toContain("Rewound workspace to checkpoint"); expect(fileSystemPort.files.get("/work/src/demo.ts")).toBe("export const value = 1;\n"); expect(rewindEvent?.payload).toMatchObject({ scope: RewindScope.Workspace, strategy: "active_chain", targetCheckpointId: expect.stringMatching(/^checkpoint_/), }); const rewindNotice = (await sessionStore.messages({ sessionID: sessionId })).find( (message) => message.info.source === "rewind", ); expect(rewindNotice).toBeDefined(); expect(messageText(rewindNotice!)).toContain("Workspace rewind applied."); expect(messageText(rewindNotice!)).not.toContain(""); }); it("applies workspace file rewind for every target message in one product turn", async () => { const sessionId = createSessionId("runtime-file-rewind-target-message-ids"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); const sessionStore = new RecordingSessionStore(); const fileSystemPort = new MemoryFileSystem({ "/work/src/a.ts": "a1\n", "/work/src/b.ts": "b1\n", }); let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteLike", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async (input: any) => { fileSystemPort.files.set(input.path, input.after); return { content: input.after, filePath: input.path.replace("/work/", ""), originalFile: input.before, structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: [`-${input.before.trimEnd()}`, `+${input.after.trimEnd()}`], }, ], type: "update", }; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work" }, { artifactStore, eventStore, fileSystemPort, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "write a", toolCalls: [ { id: "write-a", name: "WriteLike", input: { path: "/work/src/a.ts", before: "a1\n", after: "a2\n" }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (modelCallCount === 2) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "write b", toolCalls: [ { id: "write-b", name: "WriteLike", input: { path: "/work/src/b.ts", before: "b1\n", after: "b2\n" }, }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("write a and b"); const assistants = (await sessionStore.messages({ sessionID: sessionId })) .filter((message) => message.info.role === "assistant") .map((message) => message.info.id as MessageId); expect(assistants.length).toBeGreaterThanOrEqual(2); expect(fileSystemPort.files.get("/work/src/a.ts")).toBe("a2\n"); expect(fileSystemPort.files.get("/work/src/b.ts")).toBe("b2\n"); const preview = await runtime.previewWorkspaceFileRewind({ targetMessageIds: assistants.slice(0, 2), }); const applied = await runtime.applyWorkspaceFileRewind({ targetMessageIds: assistants.slice(0, 2), }); expect(preview.safeFiles.map((file) => file.path).sort()).toEqual([ "/work/src/a.ts", "/work/src/b.ts", ]); expect(applied.applied).toBe(true); expect(fileSystemPort.files.get("/work/src/a.ts")).toBe("a1\n"); expect(fileSystemPort.files.get("/work/src/b.ts")).toBe("b1\n"); fileSystemPort.files.set("/work/src/a.ts", "a2\n"); fileSystemPort.files.set("/work/src/b.ts", "b2\n"); const rejected = await runtime.applyWorkspaceFileRewind({ targetMessageIds: assistants.slice(0, 2), commitAfterApply: async () => { throw new Error("conversation admission rejected"); }, }); expect(rejected.applied).toBe(false); expect(rejected.preview.canApply).toBe(false); expect(rejected.preview.unsafeFiles).toEqual( expect.arrayContaining([ expect.objectContaining({ message: "conversation admission rejected" }), ]), ); expect(fileSystemPort.files.get("/work/src/a.ts")).toBe("a2\n"); expect(fileSystemPort.files.get("/work/src/b.ts")).toBe("b2\n"); }); // Bugfix 回归:message 目标 fork 曾恢复目标回合自身 checkpoint 的 beforeContent, // 在最新回复上分叉会把该回复刚新建的文件从共享工作区里删掉(父会话产物一并丢失)。 function createForkFixture(initialFiles: Record) { const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); const sessionStore = new RecordingSessionStore(); const fileSystemPort = new MemoryFileSystem(initialFiles); const turnScript: Array< Array<{ id: string; path: string; before: string | null; after: string }> > = []; let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteFile", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async (input: any) => { fileSystemPort.files.set(input.path, input.after); return { content: input.after, filePath: input.path, originalFile: input.before ?? null, structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: [`+${input.after}`], }, ], type: input.before === null ? "create" : "update", }; }, }); const modelImplementation = { async generateText(request: any) { modelCallCount++; const writes = turnScript.shift(); if (writes && writes.length > 0) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: writes.map((write) => ({ id: write.id, name: "WriteFile", input: { path: write.path, before: write.before, after: write.after, }, })), usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: `done ${modelCallCount}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never; return { artifactStore, fileSystemPort, modelImplementation, registry, sessionStore, runTurn: async ( runtime: AgentRuntime, prompt: string, writes: Array<{ id: string; path: string; before: string | null; after: string; }>, ) => { turnScript.push(writes, []); await runtime.executeTurn(prompt); }, lastAssistantMessageId: async (sessionId: SessionId) => { const messages = await sessionStore.messages({ sessionID: sessionId }); const assistant = messages.filter((message) => message.info.role === "assistant").at(-1); expect(assistant).toBeDefined(); return assistant!.info.id as MessageId; }, }; } it("persists tool→final assistant raw segment and explicit no-goal fork boundary", async () => { const sessionId = createSessionId("runtime-stable-boundary-tool-final"); const fixture = createForkFixture({ "/work/a.ts": "a1" }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { artifactStore: fixture.artifactStore, eventStore: createTestSessionEventStore(), fileSystemPort: fixture.fileSystemPort, sessionStore: fixture.sessionStore, toolRegistry: fixture.registry, modelFactory: createTestModelFactory(fixture.modelImplementation), }, ); await fixture.runTurn(runtime, "write then finish", [ { id: "write-stable", path: "/work/a.ts", before: "a1", after: "a2" }, ]); const messages = await fixture.sessionStore.messages({ sessionID: sessionId }); const boundary = messages.filter((message) => message.info.role === "assistant").at(-1)!; const startIndex = messages.findIndex((message) => message.info.role === "user"); const boundaryIndex = messages.findIndex((message) => message.info.id === boundary.info.id); expect(boundary.info.anchor).toMatchObject({ boundaryMessageId: boundary.info.id, goalBoundary: { kind: "none" }, orderedMessageIds: messages .slice(startIndex, boundaryIndex + 1) .map((message) => message.info.id), }); }); it("persists fallback assistant stable boundary", async () => { const sessionId = createSessionId("runtime-stable-boundary-fallback"); const sessionStore = new RecordingSessionStore(); const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore: createTestSessionEventStore(), modelFactory: createTestModelFactory({ async generateText() { return { finishReason: "stop", text: "fallback response", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, }), sessionStore, }, ); await runtime.executeTurn("fallback response"); const messages = await sessionStore.messages({ sessionID: sessionId }); const boundary = messages.find((message) => message.info.role === "assistant")!; expect(boundary.info.anchor).toMatchObject({ boundaryMessageId: boundary.info.id, goalBoundary: { kind: "none" }, orderedMessageIds: messages.map((message) => message.info.id), }); }); it("captures accounted goal and only pre-boundary verifier entry ids", async () => { const sessionId = createSessionId("runtime-stable-boundary-goal"); const sessionStore = new RecordingSessionStore(); const target = await sessionStore.setTarget({ objective: "finish goal", sessionID: sessionId }); await sessionStore.saveSessionEntry({ id: "verify-before", sessionID: sessionId, type: SESSION_ENTRY_TARGET_COMPLETION_VERIFICATION, time: { created: 1, updated: 1 }, data: { payload: { targetId: target.targetID, status: "completed" } }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore: createTestSessionEventStore(), sessionStore, modelFactory: createTestModelFactory({ async generateText(request: any) { return { finishReason: "stop", providerMetadata: undefined, text: "goal result", usage: { inputTokens: 2, outputTokens: 3, totalTokens: 5 }, }; }, } as never), }, ); await runtime.executeTurn("goal input"); const messages = await sessionStore.messages({ sessionID: sessionId }); const boundary = messages.filter((message) => message.info.role === "assistant").at(-1)!; expect(boundary.info.anchor?.goalBoundary).toMatchObject({ kind: "snapshot", target: { targetID: target.targetID, tokensUsed: 5 }, verificationEntryIds: ["verify-before"], }); }); it("persists the completed product boundary while ordinary Q2 waits for external promotion", async () => { const sessionId = createSessionId("runtime-stable-boundary-auto-drain"); const sessionStore = new RecordingSessionStore(); const firstStarted = deferred(); const finishFirst = deferred(); let calls = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore: createTestSessionEventStore(), sessionStore, modelFactory: createTestModelFactory({ async generateText(request: any) { calls += 1; if (calls === 1) { firstStarted.resolve(); await finishFirst.promise; return { finishReason: "stop", text: "A1", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", text: "A2", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); const running = runtime.executeTurn("Q1"); await firstStarted.promise; await runtime.steerTurn({ input: "Q2", delivery: "queue" }); finishFirst.resolve(); await running; const runningMessages = await sessionStore.messages({ sessionID: sessionId }); const firstAssistant = runningMessages.find( (message) => message.info.role === "assistant" && messageText(message) === "A1", ); expect(firstAssistant?.info.anchor).toMatchObject({ boundaryMessageId: firstAssistant?.info.id, goalBoundary: { kind: "none" }, }); expect(firstAssistant?.info.anchor?.orderedMessageIds?.at(-1)).toBe(firstAssistant?.info.id); expect(calls).toBe(1); expect((await runtime.getProjection()).pendingSteerInputs).toEqual([ expect.objectContaining({ input: "Q2" }), ]); }); it("keeps files created by the target turn when forking at the latest message", async () => { const sessionId = createSessionId("runtime-fork-latest-keeps-files"); const eventStore = createTestSessionEventStore(); const fixture = createForkFixture({}); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work" }, { artifactStore: fixture.artifactStore, eventStore, fileSystemPort: fixture.fileSystemPort, sessionStore: fixture.sessionStore, toolRegistry: fixture.registry, modelFactory: createTestModelFactory(fixture.modelImplementation), }, ); await fixture.runTurn(runtime, "create the handoff file", [ { id: "w1", path: "/work/tmp/handoff.md", before: null, after: "handoff\n" }, ]); expect(fixture.fileSystemPort.files.get("/work/tmp/handoff.md")).toBe("handoff\n"); const targetMessageId = await fixture.lastAssistantMessageId(sessionId); const fork = await runtime.forkWorkspaceFromCheckpoint({ targetMessageId }); expect(fork.restoredFiles).toEqual([]); expect(fixture.fileSystemPort.files.get("/work/tmp/handoff.md")).toBe("handoff\n"); const forkNotice = ( await fixture.sessionStore.messages({ sessionID: fork.forkedSessionId }) ).find((message) => message.info.source === "fork"); expect(forkNotice).toBeDefined(); expect(messageText(forkNotice!)).toContain( "No workspace checkpoint was restored for this fork.", ); }); it("restores only post-fork-point changes when forking at an earlier message", async () => { const sessionId = createSessionId("runtime-fork-undoes-later-changes"); const eventStore = createTestSessionEventStore(); const fixture = createForkFixture({ "/work/a.ts": "a1" }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work" }, { artifactStore: fixture.artifactStore, eventStore, fileSystemPort: fixture.fileSystemPort, sessionStore: fixture.sessionStore, toolRegistry: fixture.registry, modelFactory: createTestModelFactory(fixture.modelImplementation), }, ); await fixture.runTurn(runtime, "edit a", [ { id: "w1", path: "/work/a.ts", before: "a1", after: "a2" }, ]); const targetMessageId = await fixture.lastAssistantMessageId(sessionId); await fixture.runTurn(runtime, "edit a again and create b", [ { id: "w2", path: "/work/a.ts", before: "a2", after: "a3" }, { id: "w3", path: "/work/b.ts", before: null, after: "b1" }, ]); expect(fixture.fileSystemPort.files.get("/work/a.ts")).toBe("a3"); expect(fixture.fileSystemPort.files.get("/work/b.ts")).toBe("b1"); const fork = await runtime.forkWorkspaceFromCheckpoint({ targetMessageId }); // 目标回合自己的变更(a1→a2)必须保留;fork 点之后的变更(a2→a3、新建 b)被撤销。 expect(fixture.fileSystemPort.files.get("/work/a.ts")).toBe("a2"); expect(fixture.fileSystemPort.files.has("/work/b.ts")).toBe(false); expect(fork.restoredFiles).toEqual( expect.arrayContaining([ expect.objectContaining({ action: "restore", path: "/work/a.ts" }), expect.objectContaining({ action: "delete", path: "/work/b.ts" }), ]), ); expect(fork.restoredFiles).toHaveLength(2); const forkNotice = ( await fixture.sessionStore.messages({ sessionID: fork.forkedSessionId }) ).find((message) => message.info.source === "fork"); expect(forkNotice).toBeDefined(); expect(messageText(forkNotice!)).toContain("undoneCheckpointCount: 2"); const forkedEvent = (await eventStore.getEvents(sessionId)).find( (event) => event.type === SessionEventType.SessionForked, ); expect(forkedEvent?.payload).toMatchObject({ restoredFileCount: 2, targetMessageId, }); }); it("PV4-08 running stable fork 只复制对话前缀,不回滚 shared workspace", async () => { const sessionId = createSessionId("runtime-stable-conversation-fork"); const eventStore = createTestSessionEventStore(); const fixture = createForkFixture({ "/work/a.ts": "a1" }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { artifactStore: fixture.artifactStore, eventStore, fileSystemPort: fixture.fileSystemPort, sessionStore: fixture.sessionStore, toolRegistry: fixture.registry, modelFactory: createTestModelFactory(fixture.modelImplementation), }, ); await fixture.runTurn(runtime, "first", [ { id: "w1", path: "/work/a.ts", before: "a1", after: "a2" }, ]); const targetMessageId = await fixture.lastAssistantMessageId(sessionId); const stableMessageIds = (await fixture.sessionStore.messages({ sessionID: sessionId })).map( (message) => String(message.info.id), ); await fixture.runTurn(runtime, "second", [ { id: "w2", path: "/work/a.ts", before: "a2", after: "a3" }, ]); const fork = await runtime.forkStableConversationAtMessage({ goalBoundary: { kind: "none" }, sourceCommandId: "command-stable-fork", target: { productTurnId: "product-first", transcriptTurnId: "runtime-first", orderedMessageIds: stableMessageIds, boundaryMessageId: String(targetMessageId), }, }); expect(fork.restoredFiles).toEqual([]); expect(fixture.fileSystemPort.files.get("/work/a.ts")).toBe("a3"); const childMessages = await fixture.sessionStore.messages({ sessionID: fork.forkedSessionId, }); expect(childMessages.some((message) => messageText(message).includes("second"))).toBe(false); expect( childMessages .map((message) => message.info.metadata?.forkOrigin?.messageId) .filter((messageId): messageId is string => typeof messageId === "string"), ).toEqual(stableMessageIds); expect(fixture.sessionStore.stableForkChildMetadata.get(fork.forkedSessionId)).toMatchObject({ sourceCommandId: "command-stable-fork", forkTarget: { orderedMessageIds: stableMessageIds }, }); }); it("restores the earliest post-fork beforeContent when a file changed repeatedly", async () => { const sessionId = createSessionId("runtime-fork-earliest-before-wins"); const eventStore = createTestSessionEventStore(); const fixture = createForkFixture({ "/work/a.ts": "a1" }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work" }, { artifactStore: fixture.artifactStore, eventStore, fileSystemPort: fixture.fileSystemPort, sessionStore: fixture.sessionStore, toolRegistry: fixture.registry, modelFactory: createTestModelFactory(fixture.modelImplementation), }, ); await fixture.runTurn(runtime, "edit one", [ { id: "w1", path: "/work/a.ts", before: "a1", after: "a2" }, ]); const targetMessageId = await fixture.lastAssistantMessageId(sessionId); await fixture.runTurn(runtime, "edit two", [ { id: "w2", path: "/work/a.ts", before: "a2", after: "a3" }, ]); await fixture.runTurn(runtime, "edit three", [ { id: "w3", path: "/work/a.ts", before: "a3", after: "a4" }, ]); const fork = await runtime.forkWorkspaceFromCheckpoint({ targetMessageId }); expect(fixture.fileSystemPort.files.get("/work/a.ts")).toBe("a2"); expect(fork.restoredFiles).toEqual([ expect.objectContaining({ action: "restore", path: "/work/a.ts" }), ]); }); it("fails the fork without touching the workspace when a later checkpoint artifact is missing", async () => { const sessionId = createSessionId("runtime-fork-missing-artifact"); const eventStore = createTestSessionEventStore(); const fixture = createForkFixture({ "/work/a.ts": "a1" }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work" }, { artifactStore: fixture.artifactStore, eventStore, fileSystemPort: fixture.fileSystemPort, sessionStore: fixture.sessionStore, toolRegistry: fixture.registry, modelFactory: createTestModelFactory(fixture.modelImplementation), }, ); await fixture.runTurn(runtime, "edit a", [ { id: "w1", path: "/work/a.ts", before: "a1", after: "a2" }, ]); const targetMessageId = await fixture.lastAssistantMessageId(sessionId); await fixture.runTurn(runtime, "edit a again", [ { id: "w2", path: "/work/a.ts", before: "a2", after: "a3" }, ]); const laterCheckpointUri = fixture.artifactStore.requests .map((_, index) => `zcode-artifact://test/artifact-${index + 1}`) .at(-1)!; fixture.artifactStore.contents.delete(laterCheckpointUri); const sessionCountBefore = fixture.sessionStore.sessions.size; await expect(runtime.forkWorkspaceFromCheckpoint({ targetMessageId })).rejects.toThrow( /missing artifact/, ); expect(fixture.fileSystemPort.files.get("/work/a.ts")).toBe("a3"); expect(fixture.sessionStore.sessions.size).toBe(sessionCountBefore); }); it("keeps rewind semantics for explicit checkpoint-target forks", async () => { const sessionId = createSessionId("runtime-fork-explicit-checkpoint"); const eventStore = createTestSessionEventStore(); const fixture = createForkFixture({}); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work" }, { artifactStore: fixture.artifactStore, eventStore, fileSystemPort: fixture.fileSystemPort, sessionStore: fixture.sessionStore, toolRegistry: fixture.registry, modelFactory: createTestModelFactory(fixture.modelImplementation), }, ); await fixture.runTurn(runtime, "create c", [ { id: "w1", path: "/work/c.ts", before: null, after: "c1" }, ]); const checkpoints = await runtime.listWorkspaceCheckpoints(); expect(checkpoints).toHaveLength(1); const fork = await runtime.forkWorkspaceFromCheckpoint({ targetCheckpointId: checkpoints[0]!.checkpointId, }); expect(fork.targetCheckpointId).toBe(checkpoints[0]!.checkpointId); expect(fork.restoredFiles).toEqual([ expect.objectContaining({ action: "delete", path: "/work/c.ts" }), ]); expect(fixture.fileSystemPort.files.has("/work/c.ts")).toBe(false); expect(fork.response).toContain("from checkpoint"); }); it("restores every suffix workspace checkpoint through message-level /rewind cascade both", async () => { const sessionId = createSessionId("runtime-message-rewind-multi-checkpoint"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); const sessionStore = new RecordingSessionStore(); const fileSystemPort = new MemoryFileSystem({ "/work/src/a.ts": "export const a = 1;\n", "/work/src/b.ts": "export const b = 1;\n", }); let modelCallCount = 0; let latestModelMessages: any[] = []; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteA", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { fileSystemPort.files.set("/work/src/a.ts", "export const a = 2;\n"); return { content: "export const a = 2;\n", filePath: "/work/src/a.ts", originalFile: "export const a = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const a = 1;", "+export const a = 2;"], }, ], type: "update", }; }, }); registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteB", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { fileSystemPort.files.set("/work/src/b.ts", "export const b = 2;\n"); return { content: "export const b = 2;\n", filePath: "/work/src/b.ts", originalFile: "export const b = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const b = 1;", "+export const b = 2;"], }, ], type: "update", }; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work" }, { artifactStore, eventStore, fileSystemPort, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; latestModelMessages = withoutAgentListingMessages(request.messages); if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "write-a", name: "WriteA", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (modelCallCount === 3) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "write-b", name: "WriteB", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: `done ${modelCallCount}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("first prompt"); await runtime.executeTurn("second prompt"); expect(fileSystemPort.files.get("/work/src/a.ts")).toBe("export const a = 2;\n"); expect(fileSystemPort.files.get("/work/src/b.ts")).toBe("export const b = 2;\n"); const messages = await sessionStore.messages({ sessionID: sessionId }); const target = messages.find( (message) => message.info.role === "user" && messageText(message) === "first prompt", ); expect(target).toBeDefined(); const rewind = await runtime.executeTurn(`/rewind cascade both ${target!.info.id}`); await runtime.executeTurn("after rewind"); const allModelText = providerMessagesToText(latestModelMessages); const events = await eventStore.getEvents(sessionId); const checkpointEvents = events.filter( (event) => event.type === SessionEventType.CheckpointCreated, ); const rewindEvents = events.filter((event) => event.type === SessionEventType.RewindTriggered); expect(checkpointEvents).toHaveLength(2); expect(rewind.response).toContain("Rewound workspace through 2 checkpoints"); expect(fileSystemPort.files.get("/work/src/a.ts")).toBe("export const a = 1;\n"); expect(fileSystemPort.files.get("/work/src/b.ts")).toBe("export const b = 1;\n"); expect(allModelText).toContain("after rewind"); expect(allModelText).not.toContain("rewoundPromptPreview"); expect(allModelText).not.toMatch(/\nfirst prompt(?:\n|$)/); expect(allModelText).not.toContain("second prompt"); expect(rewindEvents).toHaveLength(2); expect(rewindEvents[0]?.payload).toMatchObject({ scope: RewindScope.Workspace, strategy: "active_chain", targetMessageId: target!.info.id, }); expect(rewindEvents[1]?.payload).toMatchObject({ scope: RewindScope.Conversation, strategy: "active_chain", targetMessageId: target!.info.id, }); }); it("does not partially restore workspace when a cascade checkpoint artifact is missing", async () => { const sessionId = createSessionId("runtime-message-rewind-missing-artifact"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); const sessionStore = new RecordingSessionStore(); const fileSystemPort = new MemoryFileSystem({ "/work/src/a.ts": "export const a = 1;\n", "/work/src/b.ts": "export const b = 1;\n", }); let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteA", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { fileSystemPort.files.set("/work/src/a.ts", "export const a = 2;\n"); return { content: "export const a = 2;\n", filePath: "/work/src/a.ts", originalFile: "export const a = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const a = 1;", "+export const a = 2;"], }, ], type: "update", }; }, }); registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteB", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { fileSystemPort.files.set("/work/src/b.ts", "export const b = 2;\n"); return { content: "export const b = 2;\n", filePath: "/work/src/b.ts", originalFile: "export const b = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const b = 1;", "+export const b = 2;"], }, ], type: "update", }; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work" }, { artifactStore, eventStore, fileSystemPort, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "write-a", name: "WriteA", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (modelCallCount === 3) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "write-b", name: "WriteB", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: `done ${modelCallCount}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("first prompt"); await runtime.executeTurn("second prompt"); const messages = await sessionStore.messages({ sessionID: sessionId }); const target = messages.find( (message) => message.info.role === "user" && messageText(message) === "first prompt", ); expect(target).toBeDefined(); const checkpointEvents = (await eventStore.getEvents(sessionId)).filter( (event) => event.type === SessionEventType.CheckpointCreated, ); expect(checkpointEvents).toHaveLength(2); // Bugfix 回归:旧实现边读 artifact 边写 workspace;删除较早 checkpoint 的 artifact 后, // 会先把最新 checkpoint 对应的 b.ts 回退,再在读取 a.ts 时失败,留下半回退工作区。 artifactStore.contents.delete(checkpointEvents[0]!.payload.snapshotRef as string); const rewind = await runtime.executeTurn(`/rewind cascade both ${target!.info.id}`); const rewindEvents = (await eventStore.getEvents(sessionId)).filter( (event) => event.type === SessionEventType.RewindTriggered, ); expect(rewind.response).toContain( "cannot be restored because its snapshot artifact is unavailable", ); expect(fileSystemPort.files.get("/work/src/a.ts")).toBe("export const a = 2;\n"); expect(fileSystemPort.files.get("/work/src/b.ts")).toBe("export const b = 2;\n"); expect(rewindEvents).toHaveLength(1); expect(rewindEvents[0]?.payload).toMatchObject({ reason: "checkpoint_snapshot_unavailable", scope: RewindScope.Workspace, }); }); it("does not restore workspace when cascade both conversation precheck rejects target", async () => { const sessionId = createSessionId("runtime-message-rewind-conversation-precheck"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); const sessionStore = new RecordingSessionStore(); const fileSystemPort = new MemoryFileSystem({ "/work/src/a.ts": "export const a = 1;\n", }); let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteA", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { fileSystemPort.files.set("/work/src/a.ts", "export const a = 2;\n"); return { content: "export const a = 2;\n", filePath: "/work/src/a.ts", originalFile: "export const a = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const a = 1;", "+export const a = 2;"], }, ], type: "update", }; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { artifactStore, eventStore, fileSystemPort, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "write-a", name: "WriteA", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: `done ${modelCallCount}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("first prompt"); const messages = await sessionStore.messages({ sessionID: sessionId }); const assistantTarget = messages.find((message) => message.info.role === "assistant"); expect(assistantTarget).toBeDefined(); const rewind = await runtime.executeTurn(`/rewind cascade both ${assistantTarget!.info.id}`); const rewindEvents = (await eventStore.getEvents(sessionId)).filter( (event) => event.type === SessionEventType.RewindTriggered, ); expect(rewind.response).toContain("is not a rewindable user prompt"); expect(fileSystemPort.files.get("/work/src/a.ts")).toBe("export const a = 2;\n"); expect(rewindEvents).toHaveLength(0); }); it("keeps message-level /rewind both as a single-checkpoint primitive", async () => { const sessionId = createSessionId("runtime-message-rewind-single-checkpoint"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); const sessionStore = new RecordingSessionStore(); const fileSystemPort = new MemoryFileSystem({ "/work/src/a.ts": "export const a = 1;\n", "/work/src/b.ts": "export const b = 1;\n", }); let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteA", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { fileSystemPort.files.set("/work/src/a.ts", "export const a = 2;\n"); return { content: "export const a = 2;\n", filePath: "/work/src/a.ts", originalFile: "export const a = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const a = 1;", "+export const a = 2;"], }, ], type: "update", }; }, }); registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteB", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { fileSystemPort.files.set("/work/src/b.ts", "export const b = 2;\n"); return { content: "export const b = 2;\n", filePath: "/work/src/b.ts", originalFile: "export const b = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const b = 1;", "+export const b = 2;"], }, ], type: "update", }; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work" }, { artifactStore, eventStore, fileSystemPort, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "write-a", name: "WriteA", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } if (modelCallCount === 3) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "write-b", name: "WriteB", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; } return { finishReason: "stop", providerMetadata: undefined, text: `done ${modelCallCount}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 }, }; }, } as never), }, ); await runtime.executeTurn("first prompt"); await runtime.executeTurn("second prompt"); const messages = await sessionStore.messages({ sessionID: sessionId }); const target = messages.find( (message) => message.info.role === "user" && messageText(message) === "first prompt", ); expect(target).toBeDefined(); const rewind = await runtime.executeTurn(`/rewind both ${target!.info.id}`); expect(rewind.response).not.toContain("through 2 checkpoints"); expect(fileSystemPort.files.get("/work/src/a.ts")).toBe("export const a = 1;\n"); expect(fileSystemPort.files.get("/work/src/b.ts")).toBe("export const b = 2;\n"); }); it("rewinds active conversation before a target user message", async () => { const sessionId = createSessionId("runtime-conversation-rewind"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); let modelCallCount = 0; let latestModelMessages: any[] = []; const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, sessionStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; latestModelMessages = withoutAgentListingMessages(request.messages); return { finishReason: "stop", providerMetadata: undefined, text: `reply ${modelCallCount}`, usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); await runtime.executeTurn("first prompt"); await runtime.executeTurn("second prompt"); const messages = await sessionStore.messages({ sessionID: sessionId }); const target = messages.find( (message) => message.info.role === "user" && messageText(message) === "second prompt", ); expect(target).toBeDefined(); const rewind = await runtime.executeTurn(`/rewind message ${target!.info.id}`); await runtime.executeTurn("after rewind"); const allModelText = providerMessagesToText(latestModelMessages); const events = await eventStore.getEvents(sessionId); const rewindEvent = events.find((event) => event.type === SessionEventType.RewindTriggered); const session = await sessionStore.getSession(sessionId); const rewindNotice = (await sessionStore.messages({ sessionID: sessionId })).find( (message) => message.info.source === "rewind", ); expect(rewind.response).toContain(`Rewound conversation to before message ${target!.info.id}`); expect(allModelText).toContain("first prompt"); expect(allModelText).toContain("after rewind"); expect(allModelText).not.toContain("rewoundPromptPreview"); expect(allModelText).not.toMatch(/\nsecond prompt(?:\n|$)/); expect(allModelText).not.toContain("reply 2"); expect(rewindEvent?.payload).toMatchObject({ scope: RewindScope.Conversation, strategy: "active_chain", targetMessageId: target!.info.id, }); expect(session?.revert?.targetMessageID).toBe(target!.info.id); expect(session?.revert?.branchCutAfterMessageID).toBeDefined(); expect(session?.revert?.branchGeneration).toBe(1); expect(rewindNotice).toBeUndefined(); }); it("forks a child session from the latest checkpoint", async () => { const sessionId = createSessionId("runtime-file-fork"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); const fileSystemPort = new MemoryFileSystem({ "/work/src/demo.ts": "export const value = 1;\n", }); const sessionStore = new RecordingSessionStore(); let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "CheckLike", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "checked", }); registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteLike", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { fileSystemPort.files.set("/work/src/demo.ts", "export const value = 2;\n"); return { content: "export const value = 2;\n", filePath: "/work/src/demo.ts", originalFile: "export const value = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const value = 1;", "+export const value = 2;"], }, ], type: "update", }; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work", }, { artifactStore, eventStore, fileSystemPort, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "write-like", name: "WriteLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); await runtime.executeTurn("write then fork"); const fork = await runtime.executeTurn("/fork latest"); const events = await eventStore.getEvents(sessionId); const forkEvent = events.find((event) => event.type === SessionEventType.SessionForked); const child = sessionStore.createdSessions.find((session) => session.parentID === sessionId); const childMessages = child ? await sessionStore.messages({ sessionID: child.id }) : []; expect(fork.response).toContain("Forked session"); expect(fork.response).toContain("copied 1 messages"); expect(fileSystemPort.files.get("/work/src/demo.ts")).toBe("export const value = 1;\n"); expect(child).toBeDefined(); expect(forkEvent?.payload).toMatchObject({ forkPoint: 1, forkedSessionId: child?.id, originalSessionId: sessionId, restoredFileCount: 1, strategy: "fork_required", targetCheckpointId: expect.stringMatching(/^checkpoint_/), }); expect(childMessages.map((message) => message.info.sessionID)).toEqual([ child?.id, child?.id, child?.id, ]); expect(childMessages.map((message) => message.info.role)).toEqual([ "user", "assistant", "user", ]); expect(messageText(childMessages[0]!)).toBe("write then fork"); expect( childMessages[1]?.parts.some( (part) => part.type === "timeline" && part.timelineType === "session_fork", ), ).toBe(true); const forkNotice = forkNoticeTextPart(childMessages.at(-1)); expect(forkNotice).toMatchObject({ metadata: { source: "fork", runtimeMessage: { source: "rewind_notice", }, }, synthetic: true, type: "text", }); expect(forkNotice?.type === "text" ? forkNotice.text : "").not.toContain(""); }); it("forks edited first-turn checkpoints from the active assistant turn", async () => { const sessionId = createSessionId("runtime-file-fork-edited-first-turn"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const artifactStore = new RecordingArtifactStore(); const fileSystemPort = new MemoryFileSystem({ "/work/src/demo.ts": "export const value = 1;\n", }); const sessionStore = new RecordingSessionStore(); let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteLike", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { fileSystemPort.files.set("/work/src/demo.ts", "export const value = 2;\n"); return { content: "export const value = 2;\n", filePath: "/work/src/demo.ts", originalFile: "export const value = 1;\n", structuredPatch: [ { oldStart: 1, oldLines: 1, newStart: 1, newLines: 1, lines: ["-export const value = 1;", "+export const value = 2;"], }, ], type: "update", }; }, }); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo", workingDirectory: "/work", }, { artifactStore, eventStore, fileSystemPort, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "stop", providerMetadata: undefined, text: "old reply", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } if (modelCallCount === 2) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "I will inspect edited.", toolCalls: [{ id: "check-like-edited", name: "CheckLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } if (modelCallCount === 3) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "I will write edited.", toolCalls: [{ id: "write-like-edited", name: "WriteLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "edited final reply", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); await runtime.executeTurn("old prompt"); const oldMessages = await sessionStore.messages({ sessionID: sessionId }); const oldUser = oldMessages.find( (message) => message.info.role === "user" && messageText(message) === "old prompt", ); expect(oldUser).toBeDefined(); await runtime.executeTurn(`/rewind message ${oldUser!.info.id}`); await runtime.executeTurn("edited prompt"); const parentMessages = await sessionStore.messages({ sessionID: sessionId, }); const editedUser = parentMessages.find( (message) => message.info.role === "user" && messageText(message) === "edited prompt", ); expect(editedUser).toBeDefined(); const editedAssistants = parentMessages.filter( (message) => message.info.role === "assistant" && message.info.parentID === editedUser!.info.id, ); expect(editedAssistants).toHaveLength(3); const fork = await runtime.forkWorkspaceFromCheckpoint({ targetMessageId: editedAssistants[0]!.info.id as MessageId, }); const events = await eventStore.getEvents(sessionId); const forkEvent = events.find((event) => event.type === SessionEventType.SessionForked); const child = sessionStore.createdSessions.find((session) => session.parentID === sessionId); // 模型专用公告不属于可见对话结构,与 agent listing 一样排除。 const childMessages = child ? (await sessionStore.messages({ sessionID: child.id })).filter( (message) => message.info.source !== "agent_listing_delta" && message.info.semantics?.kind !== "system_reminder", ) : []; const visibleChildText = childMessages .filter((message) => message.info.source !== "rewind") .map(messageText) .join("\n"); const childAssistantText = childMessages .filter((message) => message.info.role === "assistant") .map(messageText) .join("\n"); // 复制数包含首轮持久化的 environment、model、date 公告,fork 后无需重新宣告; // 还包含轮首与每个工具批次后的 token 预算提醒。整个 assistant 回合都被复制, // 回合内的隐藏提醒不会截断回合。 expect(fork.response).toContain("copied 11 messages"); // Bugfix 回归:fork 历史包含目标回合,工作区必须保留该回合的产出, // 不能恢复目标回合自身 checkpoint 的 beforeContent。 expect(fileSystemPort.files.get("/work/src/demo.ts")).toBe("export const value = 2;\n"); expect(fork.targetMessageId).toBe(editedAssistants[0]!.info.id); expect(forkEvent?.payload).toMatchObject({ // 与复制数一致,含 environment、model、date 公告与 token 预算提醒。 forkPoint: 11, targetMessageId: editedAssistants[0]!.info.id, restoredFileCount: 0, }); expect(childMessages.map((message) => [message.info.role, message.info.source])).toEqual([ ["user", undefined], ["assistant", undefined], ["assistant", undefined], ["assistant", undefined], ["assistant", undefined], ["user", "fork"], ]); expect( childMessages.some((message) => message.parts.some( (part) => part.type === "timeline" && part.timelineType === "session_fork", ), ), ).toBe(true); expect(visibleChildText).toContain("edited prompt"); expect(visibleChildText).not.toContain("old prompt"); expect(visibleChildText).not.toContain("old reply"); expect(childAssistantText).toContain("I will inspect edited."); expect(childAssistantText).toContain("I will write edited."); expect(childAssistantText).toContain("edited final reply"); }); it("forks a child session from a completed assistant message without a workspace checkpoint", async () => { const sessionId = createSessionId("runtime-conversation-fork"); const eventStore = createTestSessionEventStore(); const sessionStore = new RecordingSessionStore(); const registry = createToolRegistry(); let modelCallCount = 0; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "CheckLike", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "checked", }); const runtime = createTestAgentRuntime( sessionId, { mode: "build", workingDirectory: "/work", }, { eventStore, sessionStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "我先检查。", toolCalls: [{ id: "check-like", name: "CheckLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "检查完了。", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); await runtime.executeTurn("你好"); const parentMessages = await sessionStore.messages({ sessionID: sessionId, }); const target = parentMessages.find((message) => message.info.role === "assistant"); expect(target).toBeDefined(); const fork = await runtime.forkWorkspaceFromCheckpoint({ targetMessageId: target!.info.id as MessageId, }); const events = await eventStore.getEvents(sessionId); const forkEvent = events.find((event) => event.type === SessionEventType.SessionForked); const child = sessionStore.createdSessions.find((session) => session.parentID === sessionId); const allChildMessages = child ? await sessionStore.messages({ sessionID: child.id }) : []; const listings = allChildMessages.filter( (message) => message.info.source === "agent_listing_delta", ); expect(listings).toHaveLength(1); expect(listings[0]?.parts[0]?.metadata?.runtimeMessage).toMatchObject({ source: "agent_listing_delta", agentListingDelta: { addedTypes: ["Explore", "general-purpose"] }, }); // 模型专用公告不属于可见对话结构,与 agent listing 一样排除。 const childMessages = allChildMessages.filter( (message) => message.info.source !== "agent_listing_delta" && message.info.semantics?.kind !== "system_reminder", ); expect(fork).toMatchObject({ // 用户输入、environment、model、agent listing、轮首 token 预算提醒、date 公告、 // 两条 assistant 消息,以及它们之间工具批次后的 token 预算提醒。 copiedMessageCount: 9, parentSessionId: sessionId, targetMessageId: target!.info.id, restoredFiles: [], }); expect(fork.targetCheckpointId).toBeUndefined(); expect(child).toBeDefined(); expect(childMessages.map((message) => message.info.role)).toEqual([ "user", "assistant", "assistant", "assistant", "user", ]); expect(childMessages.slice(0, 3).map(messageText)).toEqual([ "你好", "我先检查。", "检查完了。", ]); expect( childMessages[3]?.parts.some( (part) => part.type === "timeline" && part.timelineType === "session_fork", ), ).toBe(true); expect(childMessages[1]?.info.parentID).toBe(childMessages[0]?.info.id); const forkNotice = forkNoticeTextPart(childMessages.at(-1)); expect(forkNotice).toMatchObject({ metadata: { forkContext: { kind: "session_fork", parentSessionId: sessionId, restoredFileCount: 0, targetMessageId: target!.info.id, }, source: "fork", }, synthetic: true, type: "text", }); expect(forkNotice?.type === "text" ? forkNotice.text : "").not.toContain(""); expect(forkEvent?.payload).toMatchObject({ forkedSessionId: child?.id, originalSessionId: sessionId, restoredFileCount: 0, strategy: "fork_required", targetMessageId: target!.info.id, }); expect( (forkEvent?.payload as { targetCheckpointId?: string } | undefined)?.targetCheckpointId, ).toBeUndefined(); }); it("injects budgeted tool model content instead of raw output", async () => { const sessionId = createSessionId("runtime-tool-result-budget"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const rawOutput = "0123456789".repeat(20); let modelCallCount = 0; let secondRequest: any; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "BigRead", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, resultBudget: { maxInlineBytes: 64, maxModelBytes: 64, strategy: "truncate", preview: { direction: "head", }, }, handler: async () => rawOutput, } as any); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "big-read", name: "BigRead", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } secondRequest = request; return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("run a big tool"); const toolMessage = secondRequest.messages.find((message: any) => message.role === "tool"); expect(result.response).toBe("done"); expect(toolMessage.content).toContain("Tool output truncated by resultBudget"); expect(toolMessage.content).not.toBe(rawOutput); expect(Buffer.byteLength(toolMessage.content, "utf8")).toBeLessThanOrEqual(64); }); it("preserves provider tool call ids when injecting tool results", async () => { const sessionId = createSessionId("runtime-tool-id-preservation"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const providerToolCallId = "call_-7666958008559069619"; let modelCallCount = 0; let secondRequest: any; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "ReadLike", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => "read-ok", } as any); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: providerToolCallId, name: "ReadLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } secondRequest = request; return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("preserve tool call ids"); const assistantMessage = secondRequest.messages.find( (message: any) => message.role === "assistant", ); const toolMessage = secondRequest.messages.find((message: any) => message.role === "tool"); expect(result.response).toBe("done"); expect(assistantMessage.toolCalls[0].id).toBe(providerToolCallId); expect(toolMessage.toolCallId).toBe(providerToolCallId); }); it("continues later tool calls after a blocking permission denial", async () => { const sessionId = createSessionId("runtime-tool-blocking-skip-results"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); let modelCallCount = 0; let secondRequest: any; let unsafeHandlerRan = false; let readHandlerRan = false; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "UnsafeWrite", needsApproval: false, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { unsafeHandlerRan = true; return "should-not-run"; }, } as any); registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "ReadLike", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { readHandlerRan = true; return "read-ok"; }, } as any); const runtime = createTestAgentRuntime( sessionId, { mode: "plan" }, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "I will inspect the workspace.", toolCalls: [ { id: "unsafe-write", name: "UnsafeWrite", input: {} }, { id: "read-like", name: "ReadLike", input: {} }, ], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } secondRequest = request; return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("inspect this project"); const toolMessages = secondRequest.messages.filter((message: any) => message.role === "tool"); expect(result.response).toBe("done"); expect(unsafeHandlerRan).toBe(false); expect(readHandlerRan).toBe(true); expect(toolMessages.map((message: any) => message.toolCallId)).toEqual([ "unsafe-write", "read-like", ]); expect(toolMessages[0].content).toContain( "Plan mode only allows read-only, non-destructive tools", ); expect(toolMessages[1].content).toContain("read-ok"); }); it("uses permission side effect declarations when scheduling tools", async () => { const sessionId = createSessionId("runtime-tool-contract-schedule"); const registry = createToolRegistry(); registry.register({ inputSchema: {}, metadata: { destructive: false, name: "DeclaredSideEffect", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, permission: { permission: "declared-side-effect", reason: "test override", riskLevel: "low", sideEffectScope: "workspace", needsApproval: false, patternSources: ["none"], denyPriority: "beforeAsk", }, handler: async () => "ok", } as any); const runtime = createTestAgentRuntime( sessionId, { mode: "yolo" }, { eventStore: createTestSessionEventStore(), toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText() { throw new Error("model should not be called"); }, } as never), }, ); const schedule = await runtime.scheduleTools([ { id: "declared-side-effect", name: "DeclaredSideEffect", input: {}, } as any, ]); expect(schedule.items[0]).toMatchObject({ canRunParallel: false, readOnly: false, sideEffectScope: "workspace", }); }); it("links the turn abort signal to model requests and tool execution", async () => { const sessionId = createSessionId("runtime-tool-abort-signal"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const abortController = new AbortController(); const modelSignals: Array = []; let toolSignal: AbortSignal | undefined; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "AbortAware", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async (_input, context: ToolExecutionContext) => { toolSignal = context.abortSignal; return "signal-ok"; }, }); let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, {}, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelSignals.push(request.abortSignal); modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "abort-aware", name: "AbortAware", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); await runtime.executeTurn("check cancellation wiring", undefined, { abortSignal: abortController.signal, }); expect(modelSignals).toHaveLength(2); expect(modelSignals[0]).toBeDefined(); expect(modelSignals[0]).toBe(modelSignals[1]); expect(modelSignals[0]).not.toBe(abortController.signal); expect(toolSignal).toBeDefined(); expect(toolSignal).not.toBe(abortController.signal); expect(toolSignal?.aborted).toBe(false); }); it("reports turn cancellation when the model request observes abort", async () => { const sessionId = createSessionId("runtime-model-cancel"); const eventStore = createTestSessionEventStore(); const abortController = new AbortController(); const modelStarted = deferred(); let modelSignal: AbortSignal | undefined; const runtime = createTestAgentRuntime( sessionId, {}, { eventStore, modelFactory: createTestModelFactory({ async generateText(request: any) { modelSignal = request.abortSignal; modelStarted.resolve(); await waitForAbort(request.abortSignal); throw request.abortSignal.reason ?? new Error("model cancelled"); }, } as never), }, ); const pending = runtime.executeTurn("cancel model request", undefined, { abortSignal: abortController.signal, }); await modelStarted.promise; abortController.abort(new Error("test cancelled")); await expect(pending).rejects.toMatchObject({ message: "Turn was cancelled.", type: CoreErrorType.TurnCancelled, }); expect(modelSignal?.aborted).toBe(true); const events = await eventStore.getEvents(sessionId); // 用户取消属于正常结束:不再 emit TurnError,而是发 TurnComplete(resultType:"cancelled")。 expect(events.some((event) => event.type === SessionEventType.TurnError)).toBe(false); const cancelComplete = events.find((event) => event.type === SessionEventType.TurnComplete); expect(cancelComplete?.payload).toMatchObject({ resultType: "cancelled" }); }); it("does not schedule tools when cancellation lands after a model step", async () => { const sessionId = createSessionId("runtime-cancel-before-tools"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); const abortController = new AbortController(); let handlerRan = false; registry.register({ inputSchema: {}, metadata: { concurrentSafe: true, destructive: false, name: "ShouldNotRun", needsApproval: false, readOnly: true, riskLevel: "low", sideEffectScope: "none", }, handler: async () => { handlerRan = true; return "unexpected"; }, }); const runtime = createTestAgentRuntime( sessionId, {}, { eventStore, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { abortController.abort(new Error("cancel before tool scheduling")); return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "should-not-run", name: "ShouldNotRun", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); await expect( runtime.executeTurn("cancel before tools", undefined, { abortSignal: abortController.signal, }), ).rejects.toMatchObject({ type: CoreErrorType.TurnCancelled, }); const eventTypes = (await eventStore.getEvents(sessionId)).map((event) => event.type); expect(handlerRan).toBe(false); expect(eventTypes).not.toContain(SessionEventType.ToolCallScheduled); expect(eventTypes).not.toContain(SessionEventType.ToolCallStarted); }); it("waits for permission broker approval before executing side-effecting tools", async () => { const sessionId = createSessionId("runtime-permission-broker"); const eventStore = createTestSessionEventStore(); const registry = createToolRegistry(); let handlerRan = false; let brokerSawRequest = false; const permissionBroker: PermissionBrokerPort = { async requestPermission(request) { brokerSawRequest = true; expect(request.toolName).toBe("WriteLike"); return { decision: "allow", reason: "approved" }; }, }; registry.register({ inputSchema: {}, metadata: { concurrentSafe: false, destructive: false, name: "WriteLike", needsApproval: true, readOnly: false, riskLevel: "medium", sideEffectScope: "workspace", }, handler: async () => { handlerRan = true; return "write-ok"; }, }); let modelCallCount = 0; const runtime = createTestAgentRuntime( sessionId, { mode: "build" }, { eventStore, permissionBroker, toolRegistry: registry, modelFactory: createTestModelFactory({ async generateText(request: any) { modelCallCount++; if (modelCallCount === 1) { return { finishReason: "tool-calls", providerMetadata: undefined, text: "", toolCalls: [{ id: "write", name: "WriteLike", input: {} }], usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; } return { finishReason: "stop", providerMetadata: undefined, text: "done", usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2, }, }; }, } as never), }, ); const result = await runtime.executeTurn("write after approval"); const eventTypes = (await eventStore.getEvents(sessionId)).map((event) => event.type); expect(result.response).toBe("done"); expect(handlerRan).toBe(true); expect(brokerSawRequest).toBe(true); expect(eventTypes).toContain(SessionEventType.PermissionRequested); expect(eventTypes).toContain(SessionEventType.PermissionResolved); expect(eventTypes.indexOf(SessionEventType.PermissionResolved)).toBeLessThan( eventTypes.indexOf(SessionEventType.ToolCallStarted), ); }); }); class RecordingArtifactStore implements ToolArtifactStorePort { readonly requests: ToolArtifactWriteRequest[] = []; readonly contents = new Map(); async writeToolResultArtifact(request: ToolArtifactWriteRequest) { this.requests.push(request); const id = `artifact-${this.requests.length}`; const uri = `zcode-artifact://test/${id}`; this.contents.set(uri, { content: request.content, contentType: request.contentType ?? "application/json", }); return { bytes: Buffer.byteLength(request.content, "utf8"), contentType: request.contentType ?? "application/json", createdAt: new Date(), id, uri, }; } async readToolResultArtifact(request: ToolArtifactReadRequest) { const artifact = this.contents.get(request.uri); if (!artifact) throw new Error(`missing artifact: ${request.uri}`); return { bytes: Buffer.byteLength(artifact.content, "utf8"), content: artifact.content, contentType: artifact.contentType, uri: request.uri, }; } } class MemoryFileSystem implements FileSystemPort { readonly files: Map; constructor(initial: Record) { this.files = new Map(Object.entries(initial)); } async createDirectory(request: { path: string }) { return { path: request.path }; } async stat(request: { path: string }) { const content = this.files.get(request.path); if (content === undefined) { throw createFileSystemError({ code: "not_found", path: request.path, message: `missing: ${request.path}`, }); } return { path: request.path, kind: "file" as const, sizeBytes: content.length, }; } async readTextFile(request: { path: string; encoding?: BufferEncoding }) { const content = this.files.get(request.path); if (content === undefined) { throw createFileSystemError({ code: "not_found", path: request.path, message: `missing: ${request.path}`, }); } return { path: request.path, content, encoding: request.encoding ?? "utf8", bytesRead: content.length, sizeBytes: content.length, truncated: false, }; } async readBinaryFile(request: { path: string; maxBytes?: number }) { const content = this.files.get(request.path); if (content === undefined) { throw createFileSystemError({ code: "not_found", path: request.path, message: `missing: ${request.path}`, }); } const buffer = Buffer.from(content, "utf8"); if (request.maxBytes !== undefined && buffer.byteLength > request.maxBytes) { throw createFileSystemError({ code: "too_large", path: request.path, message: `File content (${buffer.byteLength}B) exceeds maximum allowed size (${request.maxBytes}B).`, }); } return { path: request.path, content: buffer, bytesRead: buffer.byteLength, sizeBytes: buffer.byteLength, }; } async readTextFileRange(request: { path: string; encoding?: BufferEncoding; offsetLine?: number; limitLines?: number; maxBytes?: number; }) { const content = this.files.get(request.path); if (content === undefined) { throw createFileSystemError({ code: "not_found", path: request.path, message: `missing: ${request.path}`, }); } const sizeBytes = Buffer.byteLength(content, "utf8"); if (request.maxBytes !== undefined && sizeBytes > request.maxBytes) { throw createFileSystemError({ code: "too_large", path: request.path, message: `File content (${sizeBytes}B) exceeds maximum allowed size (${request.maxBytes}B). Use offset and limit parameters to read specific portions of the file, or search for specific content instead of reading the whole file.`, }); } const lines = content.length === 0 ? [] : content.split(/\r?\n/); const offsetLine = Math.max(0, Math.trunc(request.offsetLine ?? 0)); const limitLines = request.limitLines === undefined ? undefined : Math.max(0, Math.trunc(request.limitLines)); const selected = limitLines === undefined ? lines.slice(offsetLine) : lines.slice(offsetLine, offsetLine + limitLines); return { path: request.path, content: selected.join("\n"), encoding: request.encoding ?? "utf8", bytesRead: sizeBytes, sizeBytes, truncated: false, startLine: offsetLine + 1, lineCount: selected.length, totalLines: lines.length, }; } async writeTextFile(request: { path: string; content: string }) { this.files.set(request.path, request.content); return { path: request.path, bytesWritten: Buffer.byteLength(request.content, "utf8"), }; } async removeFile(request: { path: string; missingOk?: boolean }) { const removed = this.files.delete(request.path); if (!removed && request.missingOk !== true) { throw createFileSystemError({ code: "not_found", path: request.path, message: `missing: ${request.path}`, }); } return { path: request.path, removed, }; } async listDirectory(request: { path: string }) { const prefix = `${request.path.replace(/\/+$/, "")}/`; const seen = new Set(); const entries = Array.from(this.files.keys()).flatMap((path) => { if (!path.startsWith(prefix)) return []; const rest = path.slice(prefix.length); const [name] = rest.split("/"); if (!name || seen.has(name)) return []; seen.add(name); return [ { kind: rest.includes("/") ? ("directory" as const) : ("file" as const), name, path: `${prefix}${name}`, }, ]; }); return { path: request.path, durationMs: 0, entries, numEntries: entries.length, }; } async searchFiles(request: { path: string; pattern: string }) { return { path: request.path, pattern: request.pattern, durationMs: 0, files: [], numFiles: 0, truncated: false, }; } async searchText(request: { path: string; pattern: string; outputMode?: "content" }) { return { path: request.path, pattern: request.pattern, mode: request.outputMode ?? "files_with_matches", durationMs: 0, files: [], entries: [], numMatches: 0, truncated: false, }; } } class RecordingSessionStore implements SessionStorePort { readonly createdSessions: CreateSessionInput[] = []; readonly messagesBySession = new Map(); readonly modelUsages: ModelUsageRecord[] = []; readonly partsBySession = new Map(); readonly partWriteHistory: MessagePart[] = []; readonly projectPermissions = new Map(); readonly sessions = new Map(); readonly todos = new Map(); readonly targets = new Map(); readonly sessionEntryRecords = new Map(); readonly promotedSessionInputIds: string[] = []; readonly admittedSessionInputIds: string[] = []; readonly settledSessionInputIds: string[] = []; readonly stableForkChildMetadata = new Map(); readonly settledSessionInputs: Array< Parameters>[0] > = []; readonly sessionInputUpdates: Array< Parameters>[0] > = []; async createSession(input: CreateSessionInput): Promise { this.createdSessions.push(input); const now = Date.now(); const session: SessionInfo = { ...input, taskType: input.taskType ?? "interactive", time: { created: input.time?.created ?? now, updated: input.time?.updated ?? now, }, }; this.sessions.set(input.id, session); return session; } async createForkedSessionWithMetadata( input: CreateSessionInput, metadata: StableConversationForkChildMetadata, ): Promise { const session = await this.createSession(input); this.stableForkChildMetadata.set(input.id, metadata); return session; } async commitForkBundle(bundle: ForkCommitBundle): Promise { const session = await this.createSession(bundle.child); for (const message of bundle.messages) { await this.saveMessage(message.info); for (const part of message.parts) await this.savePart(part); } if (bundle.goal) this.targets.set(String(bundle.child.id), bundle.goal.source); if (bundle.entries.length > 0) { this.sessionEntryRecords.set(String(bundle.child.id), [...bundle.entries]); } const forkTarget = bundle.commandFact.metadata.forkTarget; if (forkTarget && typeof forkTarget === "object") { this.stableForkChildMetadata.set(bundle.child.id, { parentSessionId: bundle.commandFact.parentSessionId, sourceCommandId: bundle.commandFact.sourceCommandId, forkTarget: forkTarget as StableConversationForkChildMetadata["forkTarget"], }); } return session; } async updateSession(input: UpdateSessionInput): Promise { const session = this.sessions.get(input.id); if (!session) throw new Error(`missing session: ${input.id}`); const next: SessionInfo = { ...session, title: input.title ?? session.title, shareURL: input.shareURL === null ? undefined : (input.shareURL ?? session.shareURL), summaryAdditions: input.summary === null ? undefined : (input.summary?.additions ?? session.summaryAdditions), summaryDeletions: input.summary === null ? undefined : (input.summary?.deletions ?? session.summaryDeletions), summaryFiles: input.summary === null ? undefined : (input.summary?.files ?? session.summaryFiles), summaryDiffs: input.summary === null ? undefined : (input.summary?.diffs ?? session.summaryDiffs), revert: input.revert === null ? undefined : (input.revert ?? session.revert), permission: input.permission === null ? undefined : (input.permission ?? session.permission), time: { ...session.time, updated: Date.now(), compacting: input.timeCompacting === null ? undefined : (input.timeCompacting ?? session.time.compacting), }, }; this.sessions.set(input.id, next); return next; } async recordModelUsage(input: ModelUsageRecord): Promise { this.modelUsages.push(input); } async upsertTurnUsage(): Promise {} async upsertToolUsage(): Promise {} async getSession(sessionID: SessionId): Promise { return this.sessions.get(sessionID) ?? null; } async listSessions(): Promise { return Array.from(this.sessions.values()); } async saveMessage(input: MessageInfo): Promise { const messages = this.messagesBySession.get(input.sessionID) ?? []; const index = messages.findIndex((message) => message.id === input.id); if (index >= 0) { messages[index] = input; } else { messages.push(input); } this.messagesBySession.set(input.sessionID, messages); } async removeMessage(input: { sessionID: SessionId; messageID: MessageId }): Promise { const messages = this.messagesBySession.get(input.sessionID) ?? []; this.messagesBySession.set( input.sessionID, messages.filter((message) => message.id !== input.messageID), ); } async savePart(input: MessagePart): Promise { this.partWriteHistory.push(input); const parts = this.partsBySession.get(input.sessionID) ?? []; const index = parts.findIndex((part) => part.id === input.id); if (index >= 0) { parts[index] = input; } else { parts.push(input); } this.partsBySession.set(input.sessionID, parts); } async saveSessionInput( input: Parameters>[0], ): Promise { this.admittedSessionInputIds.push(input.id); } async settleSessionInput( input: Parameters>[0], ): Promise { this.settledSessionInputIds.push(input.id); this.settledSessionInputs.push(input); } async updateSessionInputs( input: Parameters>[0], ): Promise { this.sessionInputUpdates.push(input); } async promoteSessionInput(input: { id: string; sessionID: SessionId; message: MessageInfo; parts: MessagePart[]; }): Promise { this.promotedSessionInputIds.push(input.id); await this.saveMessage(input.message); for (const part of input.parts) await this.savePart(part); } async removePart(input: { sessionID: SessionId; messageID: MessageId; partID: PartId; }): Promise { const parts = this.partsBySession.get(input.sessionID) ?? []; this.partsBySession.set( input.sessionID, parts.filter((part) => part.id !== input.partID), ); } async messages(input: { sessionID: SessionId }): Promise { const messages = this.messagesBySession.get(input.sessionID) ?? []; const parts = this.partsBySession.get(input.sessionID) ?? []; return messages.map((info) => ({ info, parts: parts.filter((part) => part.messageID === info.id), })); } async saveSessionEntry(input: SessionEntryInfo): Promise { const entries = this.sessionEntryRecords.get(input.sessionID) ?? []; const index = entries.findIndex((entry) => entry.id === input.id); if (index >= 0) entries[index] = input; else entries.push(input); this.sessionEntryRecords.set(input.sessionID, entries); } async sessionEntries(input: { sessionID: SessionId; type?: string; }): Promise { return (this.sessionEntryRecords.get(input.sessionID) ?? []).filter( (entry) => input.type === undefined || entry.type === input.type, ); } async readTodos(input: { sessionID: SessionId }): Promise { return [...(this.todos.get(input.sessionID) ?? [])]; } async updateTodos(input: { sessionID: SessionId; todos: TodoItem[] }): Promise { this.todos.set( input.sessionID, input.todos.map((todo) => ({ ...todo })), ); } async readTarget(input: { sessionID: SessionId }): Promise { return this.targets.get(input.sessionID) ?? null; } async setTarget(input: { objective: string; sessionID: SessionId; status?: GoalStatus; tokenBudget?: number | null; }): Promise { const target = createTestTarget( input.sessionID, input.objective, input.status ?? "active", input.tokenBudget ?? null, ); this.targets.set(input.sessionID, target); return target; } async createTarget(input: { objective: string; sessionID: SessionId; tokenBudget?: number | null; }): Promise { if (this.targets.has(input.sessionID)) return null; const target = createTestTarget( input.sessionID, input.objective, "active", input.tokenBudget ?? null, ); this.targets.set(input.sessionID, target); return target; } async updateTargetStatus(input: { sessionID: SessionId; status: GoalStatus; }): Promise { const current = this.targets.get(input.sessionID); if (!current) return null; const next = { ...current, status: input.status, time: { ...current.time, updated: 2 }, }; this.targets.set(input.sessionID, next); return next; } async accountTargetUsage(input: { sessionID: SessionId; targetID: string; tokensUsedDelta?: number; timeUsedSecondsDelta?: number; }): Promise { const current = this.targets.get(input.sessionID); if (!current || current.targetID !== input.targetID) return current ?? null; const tokensUsed = current.tokensUsed + Math.max(0, input.tokensUsedDelta ?? 0); const timeUsedSeconds = current.timeUsedSeconds + Math.max(0, input.timeUsedSecondsDelta ?? 0); const status = current.status === "active" && current.tokenBudget !== null && tokensUsed >= current.tokenBudget ? "budget_limited" : current.status; const next = { ...current, status, tokensUsed, timeUsedSeconds, time: { ...current.time, updated: 2 }, }; this.targets.set(input.sessionID, next); return next; } async updateTargetSummaryTitle(input: { sessionID: SessionId; targetID: string; summaryTitle: string; }): Promise { const current = this.targets.get(input.sessionID); if (!current || current.targetID !== input.targetID) return current ?? null; const next = { ...current, summaryTitle: input.summaryTitle, time: { ...current.time, updated: 2 }, }; this.targets.set(input.sessionID, next); return next; } async clearTarget(input: { sessionID: SessionId }): Promise { return this.targets.delete(input.sessionID); } async getProjectPermission(projectID: ProjectId): Promise { return this.projectPermissions.get(projectID) ?? null; } async saveProjectPermission(input: { projectID: ProjectId; permission: PermissionRuleset; }): Promise { this.projectPermissions.set(input.projectID, input.permission); return input.permission; } async setRevert(input: { sessionID: SessionId; revert: SessionRevert }): Promise { await this.updateSession({ id: input.sessionID, revert: input.revert, }); } async clearRevert(sessionID: SessionId): Promise { await this.updateSession({ id: sessionID, revert: null, summary: null, }); } } class GoalReminderOrderingSessionStore extends RecordingSessionStore { constructor(private readonly persistenceOrder: string[]) { super(); } override async savePart(input: MessagePart): Promise { await super.savePart(input); if ( input.type === "tool" && (input.state.status === "completed" || input.state.status === "error") ) { this.persistenceOrder.push(`tool_result:${input.callID}`); return; } if ( input.type === "text" && input.synthetic === true && input.metadata?.source === "goal_state_change" ) { this.persistenceOrder.push("goal_state_change"); } } } function messageText(message: MessageWithParts): string { return message.parts .filter((part) => part.type === "text") .map((part) => (part.type === "text" ? part.text : "")) .join("\n"); } function forkNoticeTextPart(message: MessageWithParts | undefined): MessagePart | undefined { return message?.parts.find((part) => { if (part.type !== "text") return false; const metadata = part.metadata; return Boolean( metadata && typeof metadata === "object" && "source" in metadata && metadata.source === "fork", ); }); } function createTestTarget( sessionID: SessionId, objective: string, status: GoalStatus, tokenBudget: number | null = null, ): SessionGoal { return { sessionID, targetID: "target-runtime-tool-test", objective, summaryTitle: null, status, tokenBudget, tokensUsed: 0, timeUsedSeconds: 0, time: { created: 1, updated: 1, }, }; } function delay(ms: number): Promise { return new Promise((resolve) => { setTimeout(resolve, ms); }); } async function waitForCondition( check: () => boolean | Promise, timeoutMs = 1_000, ): Promise { const startedAt = Date.now(); while (!(await check())) { if (Date.now() - startedAt > timeoutMs) { throw new Error("Timed out waiting for condition"); } await delay(5); } } function deferred(): { promise: Promise; resolve(): void } { let resolveDeferred: () => void = () => {}; const promise = new Promise((resolve) => { resolveDeferred = resolve; }); return { promise, resolve: resolveDeferred, }; } async function waitForAbort(signal?: AbortSignal): Promise { if (!signal || signal.aborted) return; await new Promise((resolve) => { signal.addEventListener("abort", () => resolve(), { once: true }); }); }