import { afterEach, beforeEach, describe, expect, it, vi } from "bun:test"; import * as fs from "node:fs"; import * as path from "node:path"; import { scheduler } from "node:timers/promises"; import { gunzipSync } from "node:zlib"; import { Agent, type AgentMessage, type AgentTool, ThinkingLevel } from "@sayknow-cli/agent-core"; import { type AssistantMessage, getBundledModel, type Model, streamSimple } from "@sayknow-cli/ai"; import { ModelRegistry } from "@sayknow-cli/coding-agent/config/model-registry"; import { Settings } from "@sayknow-cli/coding-agent/config/settings"; import { ExtensionToolWrapper } from "@sayknow-cli/coding-agent/extensibility/extensions/wrapper"; import { runRootCommand } from "@sayknow-cli/coding-agent/main"; import type { InteractiveMode } from "@sayknow-cli/coding-agent/modes/interactive-mode"; import { createAgentSession } from "@sayknow-cli/coding-agent/sdk"; import { AgentSession, type AgentSessionEvent } from "@sayknow-cli/coding-agent/session/agent-session"; import { AuthStorage } from "@sayknow-cli/coding-agent/session/auth-storage"; import { AUTO_RESUME_MARKER_CUSTOM_TYPE, findInterruptedToolCalls, isRestartSafeTool, resolveAutoResumePolicy, } from "@sayknow-cli/coding-agent/session/auto-resume"; import { convertToLlm } from "@sayknow-cli/coding-agent/session/messages"; import { SessionManager } from "@sayknow-cli/coding-agent/session/session-manager"; import type { ToolSession } from "@sayknow-cli/coding-agent/tools"; import { ReadTool } from "@sayknow-cli/coding-agent/tools/read"; import { getProjectDir, logger, setProjectDir, TempDir } from "@sayknow-cli/utils"; import * as z from "zod/v4"; /** * Automatic same-model resume. Every case runs the production AgentSession and the * real openai-completions serializer; only `globalThis.fetch` is replaced, so each * fetch call is one concrete upstream request and its body is the actual wire. */ type Reply = () => Response; interface WireLog { bodies: string[]; } /** Wire body text; first-party Anthropic requests are gzip-compressed on the wire (#6865). */ function wireBodyText(init?: Parameters[1]): string { const body = init?.body; if (typeof body === "string") return body; if (!(body instanceof Uint8Array)) return ""; const gzip = new Headers(init?.headers).get("content-encoding") === "gzip"; return new TextDecoder().decode(gzip ? gunzipSync(body) : body); } function installReplies(replies: readonly Reply[]): WireLog { const log: WireLog = { bodies: [] }; vi.spyOn(globalThis, "fetch").mockImplementation((async ( input: Parameters[0], init?: Parameters[1], ) => { // Startup may refresh model catalogs in the background; only inference is the wire. const url = input instanceof Request ? input.url : String(input); if (!url.endsWith("/chat/completions")) return new Response("{}", { status: 404 }); log.bodies.push(wireBodyText(init)); const reply = replies[log.bodies.length - 1]; if (!reply) throw new Error(`Unexpected outbound request #${log.bodies.length}`); return reply(); }) as typeof fetch); return log; } function status503(): Response { return new Response( JSON.stringify({ error: { message: "Service temporarily unavailable", type: "server_error", code: null } }), { status: 503, headers: { "content-type": "application/json", "retry-after-ms": "1" } }, ); } function sse(frames: readonly string[]): Reply { return () => { const encoder = new TextEncoder(); let next = 0; const body = new ReadableStream( { pull(controller) { if (next < frames.length) controller.enqueue(encoder.encode(frames[next++])); else controller.close(); }, }, { highWaterMark: 0 }, ); return new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } }); }; } function oc(model: string, delta: Record, finishReason: string | null = null): string { return `data: ${JSON.stringify({ id: "chatcmpl-resume", object: "chat.completion.chunk", created: 1, model, choices: [{ index: 0, delta, finish_reason: finishReason }], })}\n\n`; } const OC_DONE = "data: [DONE]\n\n"; function textReply(model: string, text: string): Reply { return sse([oc(model, { role: "assistant" }), oc(model, { content: text }), oc(model, {}, "stop"), OC_DONE]); } function toolCallReply(model: string, id: string, name: string): Reply { return sse([ oc(model, { role: "assistant" }), oc(model, { tool_calls: [{ index: 0, id, type: "function", function: { name, arguments: "{}" } }] }), oc(model, {}, "tool_calls"), OC_DONE, ]); } function toolNames(body: string): string[] { const parsed = JSON.parse(body) as { tools?: Array<{ function?: { name?: string } }> }; return (parsed.tools ?? []).map(tool => tool.function?.name ?? "").sort(); } function toolSession(cwd: string): ToolSession { return { cwd, hasUI: false, getSessionFile: () => null, getSessionSpawns: () => "*", settings: Settings.isolated() }; } describe("automatic same-model resume", () => { // `skc --continue` adopts the session cwd as the project dir (and process cwd); restore it // before the temp dir is removed, or later shell spawns in this process run from a deleted cwd. const originalProjectDir = getProjectDir(); let tempDir: TempDir; let authStorage: AuthStorage; let modelRegistry: ModelRegistry; let session: AgentSession | undefined; beforeEach(async () => { tempDir = TempDir.createSync("@auto-resume-"); authStorage = await AuthStorage.create(path.join(tempDir.path(), "testauth.db")); modelRegistry = new ModelRegistry(authStorage); }); afterEach(async () => { if (session) { await session.dispose(); session = undefined; } authStorage.close(); setProjectDir(originalProjectDir); tempDir.removeSync(); vi.restoreAllMocks(); }); function groq(): Model { const found = getBundledModel("groq", "llama-3.3-70b-versatile"); if (!found) throw new Error("missing bundled model"); return found; } function build(options: { model: Model; tools?: AgentTool[]; sessionManager?: SessionManager; settings?: Parameters[0]; streamFn?: typeof streamSimple; }): AgentSession { const m = options.model; authStorage.setRuntimeApiKey(m.provider, `${m.provider}-test-key`); const agent = new Agent({ getApiKey: provider => `${provider}-test-key`, initialState: { model: m, systemPrompt: ["Test"], tools: options.tools ?? [], messages: [] }, streamFn: options.streamFn ?? streamSimple, convertToLlm, }); const settings = Settings.isolated({ "compaction.enabled": false, "fallback.auto": false, "retry.maxRetries": 3, "retry.baseDelayMs": 1, "retry.maxDelayMs": 10, ...options.settings, }); settings.setModelRole("default", `${m.provider}/${m.id}`); const built = new AgentSession({ agent, sessionManager: options.sessionManager ?? SessionManager.inMemory(), settings, modelRegistry, }); built.subscribe(() => {}); return built; } function counted(name: string): { tool: AgentTool; runs: () => number } { let runs = 0; return { runs: () => runs, tool: { name, label: name, description: `Counts real ${name} executions`, parameters: z.object({ path: z.string().optional() }), execute: async () => { runs++; return { content: [{ type: "text" as const, text: `${name} result` }] }; }, }, }; } function readTool(): AgentTool { return new ReadTool(toolSession(tempDir.path())) as AgentTool; } function events(s: AgentSession): AgentSessionEvent[] { const seen: AgentSessionEvent[] = []; s.subscribe(event => { seen.push(event); }); return seen; } function markerCount(s: AgentSession): number { return s.sessionManager .getBranch() .filter(entry => entry.type === "custom" && entry.customType === AUTO_RESUME_MARKER_CUSTOM_TYPE).length; } async function interruptedSession(options: { messages: AgentMessage[]; checkpoint: Record; }): Promise { const dir = tempDir.path(); const first = SessionManager.create(dir, dir); for (const message of options.messages) first.appendMessage(message as Parameters[0]); first.appendCustomEntry("recovery_checkpoint", { version: 1, state: "in_flight", ...options.checkpoint }); await first.ensureOnDisk(); await first.flush(); const file = first.getSessionFile(); if (!file) throw new Error("expected persisted session file"); return SessionManager.open(file); } function assistantWithCall(m: Model, id: string, name: string): AssistantMessage { return { role: "assistant", content: [{ type: "toolCall", id, name, arguments: {} }], api: m.api, provider: m.provider, model: m.id, stopReason: "toolUse", usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, }, timestamp: Date.now(), }; } const selectorOf = (m: Model) => `${m.provider}/${m.id}`; const restartCheckpoint = (m: Model) => ({ stepId: "lost-step", selector: selectorOf(m), api: m.api, uncertain: [], }); // ------------------------------------------------------------ restart (P-A) it("1) restart resumes an interrupted write once, read-only, without re-running it", async () => { const m = groq(); const write = counted("write"); const reopened = await interruptedSession({ messages: [ { role: "user", content: "update the file", timestamp: Date.now() }, assistantWithCall(m, "w1", "write"), ], checkpoint: restartCheckpoint(m), }); const log = installReplies([textReply(m.id, "verified, the write needs checking")]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, tools: [write.tool, readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); const seen = events(session); await session.continuePersistedHistory(); await session.waitForIdle(); expect(log.bodies).toHaveLength(1); expect(write.runs()).toBe(0); // Restricted mode exposes only the built-in read tool on the wire. expect(toolNames(log.bodies[0])).toEqual(["read"]); expect(log.bodies[0]).toContain("Interrupted before its outcome was observed"); expect(log.bodies[0]).toContain("Only read-only tools are available now"); // The selection itself is untouched and restored after the run. expect(session.getActiveToolNames().sort()).toEqual(["read", "write"]); expect(markerCount(session)).toBe(1); expect( seen.some(event => event.type === "notice" && event.message.includes("automatically (read-only tools)")), ).toBe(true); }); it("6) a second restart after an automatic resume sends nothing", async () => { const m = groq(); const dir = tempDir.path(); const first = SessionManager.create(dir, dir); first.appendMessage({ role: "user", content: "update", timestamp: Date.now() }); first.appendMessage(assistantWithCall(m, "w1", "write")); first.appendCustomEntry("recovery_checkpoint", { version: 1, state: "in_flight", ...restartCheckpoint(m) }); first.appendCustomEntry(AUTO_RESUME_MARKER_CUSTOM_TYPE, { version: 1, trigger: "restart" }); await first.ensureOnDisk(); await first.flush(); const reopened = await SessionManager.open(first.getSessionFile() ?? ""); const log = installReplies([]); session = build({ model: m, tools: [counted("write").tool, readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); await expect(session.continuePersistedHistory()).rejects.toThrow("interrupted during automatic recovery"); expect(log.bodies).toHaveLength(0); }); it("9) a marker that cannot be made durable sends nothing", async () => { const m = groq(); const reopened = await interruptedSession({ messages: [{ role: "user", content: "update", timestamp: Date.now() }, assistantWithCall(m, "w1", "write")], checkpoint: restartCheckpoint(m), }); const log = installReplies([]); session = build({ model: m, tools: [counted("write").tool, readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); vi.spyOn(session.sessionManager, "flush").mockRejectedValue(new Error("disk failure")); await expect(session.continuePersistedHistory()).rejects.toThrow("interrupted during automatic recovery"); expect(log.bodies).toHaveLength(0); }); it("18) a restart never resumes a step with persisted uncertainty or an opaque/legacy checkpoint", async () => { const m = groq(); for (const checkpoint of [ { ...restartCheckpoint(m), uncertain: ["broker forced refresh"] }, { ...restartCheckpoint(m), api: "cursor-agent" }, { ...restartCheckpoint(m), transport: "pi-native" }, { stepId: "legacy-step" }, ]) { const reopened = await interruptedSession({ messages: [{ role: "user", content: "update", timestamp: Date.now() }, assistantWithCall(m, "w1", "write")], checkpoint, }); const log = installReplies([]); session = build({ model: m, tools: [counted("write").tool, readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); await expect(session.continuePersistedHistory()).rejects.toThrow("interrupted during automatic recovery"); expect(log.bodies).toHaveLength(0); await session.dispose(); session = undefined; vi.restoreAllMocks(); } }); it("18b) a restart never resumes a thinking-only step that stopped on a rate limit, quota, auth or terminal error", async () => { const m = groq(); const thinkingFailure = (errorMessage: string, extra: Partial = {}): AssistantMessage => ({ ...assistantWithCall(m, "t1", "read"), content: [{ type: "thinking", thinking: "plan", thinkingSignature: "sig" }], stopReason: "error", errorMessage, ...extra, }); for (const failed of [ thinkingFailure("429 rate limit exceeded", { transportFailure: { kind: "transport", status: 429 } }), thinkingFailure("500 requests per minute exceeded"), thinkingFailure("quota exhausted", { transportFailure: { kind: "transport", status: 429, providerCode: "insufficient_quota" }, }), thinkingFailure("HTTP 401 unauthorized", { transportFailure: { kind: "transport", status: 401 } }), thinkingFailure("HTTP 400 invalid request"), ]) { const reopened = await interruptedSession({ messages: [{ role: "user", content: "think", timestamp: Date.now() }, failed], checkpoint: restartCheckpoint(m), }); const log = installReplies([]); session = build({ model: m, tools: [readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); await expect(session.continuePersistedHistory()).rejects.toThrow("interrupted during automatic recovery"); expect(log.bodies).toHaveLength(0); await session.dispose(); session = undefined; vi.restoreAllMocks(); } }); it("18c) a restart resumes a thinking-only step that stopped on a server overload, without the failed reasoning", async () => { const m = groq(); const reopened = await interruptedSession({ messages: [ { role: "user", content: "think", timestamp: Date.now() }, { ...assistantWithCall(m, "t1", "read"), content: [{ type: "thinking", thinking: "private plan", thinkingSignature: "sig" }], stopReason: "error", errorMessage: "503 service unavailable: overloaded_error", }, ], checkpoint: restartCheckpoint(m), }); const log = installReplies([textReply(m.id, "done")]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, tools: [readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); await session.continuePersistedHistory(); await session.waitForIdle(); expect(log.bodies).toHaveLength(1); expect(log.bodies[0]).not.toContain("private plan"); expect(markerCount(session)).toBe(1); }); it("18d) a restart resumes a step cut off mid tool call without the attempt or re-running the call", async () => { const m = groq(); const write = counted("write"); const reopened = await interruptedSession({ messages: [ { role: "user", content: "update the file", timestamp: Date.now() }, { ...assistantWithCall(m, "cut1", "write"), content: [ { type: "thinking", thinking: "private plan", thinkingSignature: "sig" }, { type: "toolCall", id: "cut1", name: "write", arguments: { path: "partial" } }, ], stopReason: "error", errorMessage: "The socket connection was closed unexpectedly. (transport=ECONNRESET)", }, { role: "toolResult", toolCallId: "cut1", toolName: "write", content: [{ type: "text", text: "Tool execution failed due to an error: socket closed" }], isError: true, timestamp: Date.now(), }, ], checkpoint: restartCheckpoint(m), }); const log = installReplies([textReply(m.id, "done")]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, tools: [write.tool, readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); await session.continuePersistedHistory(); await session.waitForIdle(); expect(log.bodies).toHaveLength(1); expect(write.runs()).toBe(0); // The never-run call is not replayed, so the full tool set stays available. expect(log.bodies[0]).not.toContain("cut1"); expect(log.bodies[0]).not.toContain("private plan"); expect(toolNames(log.bodies[0])).toEqual(["read", "write"]); expect(markerCount(session)).toBe(1); }); it("15) a restart on a different model than the interrupted step pauses", async () => { const m = groq(); const reopened = await interruptedSession({ messages: [{ role: "user", content: "update", timestamp: Date.now() }, assistantWithCall(m, "w1", "write")], checkpoint: { ...restartCheckpoint(m), selector: "openai/gpt-4o-mini" }, }); const log = installReplies([]); session = build({ model: m, tools: [counted("write").tool, readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); await expect(session.continuePersistedHistory()).rejects.toThrow("interrupted during automatic recovery"); expect(log.bodies).toHaveLength(0); }); it("5) a restart resume spends at most two requests", async () => { const m = groq(); const reopened = await interruptedSession({ messages: [{ role: "user", content: "update", timestamp: Date.now() }, assistantWithCall(m, "w1", "write")], checkpoint: restartCheckpoint(m), }); const log = installReplies([status503, status503, status503, status503]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, tools: [counted("write").tool, readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); await session.continuePersistedHistory().catch(() => undefined); await session.waitForIdle(); expect(log.bodies.length).toBeLessThanOrEqual(2); expect(markerCount(session)).toBe(1); }); it("12) a name-alike read from an extension is not restart-safe", async () => { const m = groq(); const fakeRead = counted("read"); const reopened = await interruptedSession({ messages: [{ role: "user", content: "update", timestamp: Date.now() }, assistantWithCall(m, "w1", "write")], checkpoint: restartCheckpoint(m), }); const log = installReplies([]); session = build({ model: m, tools: [counted("write").tool, fakeRead.tool], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); await expect(session.continuePersistedHistory()).rejects.toThrow("interrupted during automatic recovery"); expect(log.bodies).toHaveLength(0); }); it("16) retry.autoResume=false keeps the restart pause", async () => { const m = groq(); const reopened = await interruptedSession({ messages: [{ role: "user", content: "update", timestamp: Date.now() }, assistantWithCall(m, "w1", "write")], checkpoint: restartCheckpoint(m), }); const log = installReplies([]); session = build({ model: m, tools: [counted("write").tool, readTool()], sessionManager: reopened, settings: { "retry.autoResume": false }, }); session.agent.replaceMessages(reopened.buildSessionContext().messages); await expect(session.continuePersistedHistory()).rejects.toThrow("interrupted during automatic recovery"); expect(log.bodies).toHaveLength(0); }); // ------------------------------------------------------------ in-process (P-B) it("2) a transient failure after executed tools resumes once with every tool and replays nothing", async () => { const m = groq(); const write = counted("write"); const log = installReplies([ toolCallReply(m.id, "w1", "write"), status503, status503, status503, status503, textReply(m.id, "done after resume"), ]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, tools: [write.tool, readTool()] }); await session.prompt("write then answer"); for (let i = 0; i < 50 && session.agent.state.messages.at(-1)?.role !== "assistant"; i++) await Bun.sleep(5); await session.waitForIdle(); for (let i = 0; i < 50 && log.bodies.length < 6; i++) await Bun.sleep(5); await session.waitForIdle(); // One tool loop, three retries inside the step, then exactly one automatic resume. expect(log.bodies).toHaveLength(6); expect(log.bodies.length).toBeLessThanOrEqual(1 + 7); expect(write.runs()).toBe(1); expect(toolNames(log.bodies[5])).toEqual(["read", "write"]); expect(markerCount(session)).toBe(1); const last = session.agent.state.messages.findLast(message => message.role === "assistant") as AssistantMessage; expect(last.stopReason).toBe("stop"); }); it("14) an abort right after the resume is scheduled sends nothing and restores the tool view", async () => { const m = groq(); const write = counted("write"); const log = installReplies([toolCallReply(m.id, "w1", "write"), status503, status503, status503, status503]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, tools: [write.tool, readTool()] }); const live = session; live.subscribe(event => { if (event.type === "notice" && event.message.startsWith("Resuming the interrupted step")) live.abort(); }); await live.prompt("write then answer"); await live.waitForIdle(); await Bun.sleep(20); await live.waitForIdle(); expect(log.bodies).toHaveLength(5); expect(write.runs()).toBe(1); expect(markerCount(live)).toBe(1); expect(live.getActiveToolNames().sort()).toEqual(["read", "write"]); }); it("stale resume instructions are dropped from later turns", () => { const llm = convertToLlm([ { role: "user", content: "first", timestamp: 0 }, { role: "custom", customType: "auto-resume", content: "resume instruction", display: false, attribution: "agent", timestamp: 1, }, { role: "user", content: "second", timestamp: 2 }, ]); expect(JSON.stringify(llm)).not.toContain("resume instruction"); }); function managedRun(replies: readonly Reply[]): { urls: string[]; bodies: string[] } { const urls: string[] = []; const bodies: string[] = []; vi.spyOn(globalThis, "fetch").mockImplementation((async ( input: Parameters[0], init?: Parameters[1], ) => { urls.push(input instanceof Request ? input.url : String(input)); bodies.push(wireBodyText(init)); const reply = replies[bodies.length - 1]; return reply ? reply() : status503(); }) as typeof fetch); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); return { urls, bodies }; } it("4) a managed chain resumes once on the interrupted model without advancing the chain", async () => { const m = groq(); const write = counted("write"); const { urls, bodies } = managedRun([ toolCallReply(m.id, "w1", "write"), status503, status503, status503, status503, textReply(m.id, "done after resume"), ]); session = build({ model: m, tools: [write.tool, readTool()] }); session.settings.setModelRole("default", [`${m.provider}/${m.id}`, "openai/gpt-4o-mini"]); const continueSpy = vi.spyOn(session.agent, "continue"); await session.prompt("write then answer"); await session.waitForIdle(); await Bun.sleep(20); await session.waitForIdle(); expect(write.runs()).toBe(1); // The resume runs outside the managed chain: no fallback can switch the model. expect(continueSpy.mock.calls.at(-1)?.[0]?.fallbackManaged).toBeUndefined(); expect(bodies).toHaveLength(6); expect(bodies[5]).toContain("interrupted before it finished"); expect(urls.every(url => url.includes("api.groq.com"))).toBe(true); expect(markerCount(session)).toBe(1); }); it("4b) a transient failure inside the resumed run retries on the same model only", async () => { const m = groq(); const write = counted("write"); const { urls, bodies } = managedRun([ toolCallReply(m.id, "w1", "write"), status503, status503, status503, status503, status503, textReply(m.id, "done after resume"), ]); session = build({ model: m, tools: [write.tool, readTool()] }); authStorage.setRuntimeApiKey("openai", "openai-test-key"); session.settings.setModelRole("default", [`${m.provider}/${m.id}`, "openai/gpt-4o-mini"]); await session.prompt("write then answer"); await session.waitForIdle(); for (let i = 0; i < 50 && bodies.length < 7; i++) await Bun.sleep(5); await session.waitForIdle(); expect(write.runs()).toBe(1); expect(markerCount(session)).toBe(1); expect(bodies).toHaveLength(7); expect(urls.every(url => url.includes("api.groq.com"))).toBe(true); const last = session.agent.state.messages.findLast(message => message.role === "assistant") as AssistantMessage; expect(last.stopReason).toBe("stop"); }); it("4c) a quota failure inside the resumed run stops on the interrupted model", async () => { const m = groq(); const write = counted("write"); const quota: Reply = () => new Response( JSON.stringify({ error: { message: "You exceeded your current quota", type: "insufficient_quota", code: "insufficient_quota", }, }), { status: 429, headers: { "content-type": "application/json" } }, ); const { urls, bodies } = managedRun([ toolCallReply(m.id, "w1", "write"), status503, status503, status503, status503, quota, textReply("gpt-4o-mini", "switched model"), ]); session = build({ model: m, tools: [write.tool, readTool()] }); // The next chain entry is usable, so only the pin keeps the resume on groq. authStorage.setRuntimeApiKey("openai", "openai-test-key"); session.settings.setModelRole("default", [`${m.provider}/${m.id}`, "openai/gpt-4o-mini"]); const seen = events(session); await session.prompt("write then answer"); await session.waitForIdle(); await Bun.sleep(20); await session.waitForIdle(); expect(write.runs()).toBe(1); expect(markerCount(session)).toBe(1); // The resume's quota failure is the last request: no other model is contacted. expect(bodies).toHaveLength(6); expect(urls.every(url => url.includes("api.groq.com"))).toBe(true); expect(seen.some(event => event.type === "model_fallback_switched")).toBe(false); expect(session.model?.id).toBe(m.id); }); it("5b) a step whose budget is spent never resumes (no second budget)", async () => { const m = groq(); const write = counted("write"); const { bodies } = managedRun([toolCallReply(m.id, "w1", "write")]); session = build({ model: m, tools: [write.tool, readTool()], settings: { "retry.maxRetries": 99 } }); session.settings.setModelRole("default", [`${m.provider}/${m.id}`, "openai/gpt-4o-mini"]); await session.prompt("write then answer"); await session.waitForIdle(); await Bun.sleep(20); await session.waitForIdle(); // One tool step plus at most seven requests for the failing step. expect(bodies.length).toBeLessThanOrEqual(1 + 7); expect(markerCount(session)).toBe(0); }); // ------------------------------------------------------------ pure policy it("policy pauses on every guard and resumes restricted only with interrupted calls", () => { const m = groq(); const read = readTool(); const write = counted("write").tool; const base = { enabled: true, trigger: "restart" as const, messages: [ { role: "user" as const, content: "x", timestamp: 0 }, assistantWithCall(m, "w1", "write"), ] as AgentMessage[], resumedThisTurn: false, uncertainUpstream: false, opaqueTransport: false, budgetAdmits: true, userInputPending: false, selectorAvailable: true, prefixReplayable: true, selectedTools: [read, write], }; expect(resolveAutoResumePolicy(base)).toMatchObject({ type: "resume", mode: "restricted" }); expect(resolveAutoResumePolicy({ ...base, enabled: false })).toEqual({ type: "pause", reason: "disabled" }); expect(resolveAutoResumePolicy({ ...base, resumedThisTurn: true }).type).toBe("pause"); expect(resolveAutoResumePolicy({ ...base, uncertainUpstream: true }).type).toBe("pause"); expect(resolveAutoResumePolicy({ ...base, opaqueTransport: true }).type).toBe("pause"); expect(resolveAutoResumePolicy({ ...base, budgetAdmits: false }).type).toBe("pause"); expect(resolveAutoResumePolicy({ ...base, userInputPending: true }).type).toBe("pause"); expect(resolveAutoResumePolicy({ ...base, selectorAvailable: false }).type).toBe("pause"); expect(resolveAutoResumePolicy({ ...base, selectedTools: [write] })).toEqual({ type: "pause", reason: "no_restart_safe_tools", }); // A completed call is never treated as interrupted (11). const completed = [ ...base.messages, { role: "toolResult", toolCallId: "w1", toolName: "write", content: [], isError: false, timestamp: 1 }, ] as AgentMessage[]; expect(findInterruptedToolCalls(completed)).toEqual([]); expect(resolveAutoResumePolicy({ ...base, messages: completed })).toMatchObject({ type: "resume", mode: "full" }); // A failure cut off mid tool call never ran the call (tools run only after a // completed stream): it is dropped with its placeholder results and resumed. const failedWithThinking: AssistantMessage = { ...assistantWithCall(m, "w2", "write"), content: [ { type: "thinking", thinking: "secret", thinkingSignature: "sig" }, { type: "toolCall", id: "w2", name: "write", arguments: {} }, ], stopReason: "error", }; const placeholder = { role: "toolResult", toolCallId: "w2", toolName: "write", content: [{ type: "text", text: "Tool execution failed due to an error: socket closed" }], isError: true, timestamp: 1, }; for (const tail of [[failedWithThinking], [failedWithThinking, placeholder]]) { expect(resolveAutoResumePolicy({ ...base, messages: [base.messages[0], ...tail] as AgentMessage[] })).toEqual({ type: "resume", mode: "full", interruptedCalls: [], }); } // Signed reasoning next to a call in an aborted tail, or next to answer text, never resumes. const abortedWithThinking: AssistantMessage = { ...failedWithThinking, stopReason: "aborted" }; const failedTextAndCall: AssistantMessage = { ...failedWithThinking, content: [{ type: "text", text: "partial" }, ...failedWithThinking.content], }; for (const failed of [abortedWithThinking, failedTextAndCall]) { expect(resolveAutoResumePolicy({ ...base, messages: [base.messages[0], failed] as AgentMessage[] })).toEqual({ type: "pause", reason: "unsafe_tail", }); } // A successful result means the call ran: the tail is not dropped. expect( resolveAutoResumePolicy({ ...base, messages: [base.messages[0], failedWithThinking, { ...placeholder, isError: false }] as AgentMessage[], }), ).toEqual({ type: "pause", reason: "unsafe_tail" }); // A failure that streamed only reasoning holds no answer: it is dropped and resumed. const failedThinkingOnly: AssistantMessage = { ...failedWithThinking, content: [{ type: "thinking", thinking: "secret", thinkingSignature: "sig" }], }; expect( resolveAutoResumePolicy({ ...base, messages: [base.messages[0], failedThinkingOnly] as AgentMessage[] }), ).toEqual({ type: "resume", mode: "full", interruptedCalls: [] }); // A visible text prefix needs a faithful serializer projection. const failedText: AssistantMessage = { ...assistantWithCall(m, "w3", "write"), content: [{ type: "text", text: "partial" }], stopReason: "error", }; expect( resolveAutoResumePolicy({ ...base, prefixReplayable: false, messages: [base.messages[0], failedText] as AgentMessage[], }), ).toEqual({ type: "pause", reason: "unsafe_tail" }); }); it("restart-safe tools are built-in read-only instances with a unique wire name", () => { const read = readTool(); const write = counted("write").tool; expect(isRestartSafeTool(read, [read, write])).toBe(true); expect(isRestartSafeTool(write, [read, write])).toBe(false); const alike = counted("read").tool; expect(isRestartSafeTool(alike, [alike])).toBe(false); expect(isRestartSafeTool(read, [read, alike])).toBe(false); const shadow = { ...counted("other").tool, customWireName: "read" } as AgentTool; expect(isRestartSafeTool(read, [read, shadow])).toBe(false); }); it("restart-safe identification sees through extension and hook interception wrappers", () => { const read = readTool(); const runner = { hasHandlers: () => false } as unknown as ConstructorParameters[1]; const wrapped = new ExtensionToolWrapper(read, runner) as AgentTool; expect(wrapped instanceof ReadTool).toBe(false); expect(isRestartSafeTool(wrapped, [wrapped])).toBe(true); const alike = new ExtensionToolWrapper(counted("read").tool, runner) as AgentTool; expect(isRestartSafeTool(alike, [alike])).toBe(false); }); async function sdkRestart(settings: Record) { const m = groq(); const reopened = await interruptedSession({ messages: [ { role: "user", content: "update the file", timestamp: Date.now() }, assistantWithCall(m, "w1", "write"), ], checkpoint: restartCheckpoint(m), }); authStorage.setRuntimeApiKey(m.provider, `${m.provider}-test-key`); const { session: sdkSession } = await createAgentSession({ cwd: tempDir.path(), agentDir: tempDir.path(), authStorage, modelRegistry, sessionManager: reopened, settings: Settings.isolated({ "compaction.enabled": false, "fallback.auto": false, ...settings }), model: m, disableExtensionDiscovery: true, extensions: [], skills: [], contextFiles: [], promptTemplates: [], slashCommands: [], enableMCP: false, enableLsp: false, toolNames: ["read", "write"], }); session = sdkSession; return { m, sdkSession }; } for (const appendOnly of ["off", "on"] as const) { it(`1b/19) a production SDK session resumes an interrupted write read-only after a restart (append-only ${appendOnly})`, async () => { const log = installReplies([textReply(groq().id, "verified")]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); const { sdkSession } = await sdkRestart({ "provider.appendOnlyContext": appendOnly }); expect(sdkSession.agent.appendOnlyContext !== undefined).toBe(appendOnly === "on"); expect(sdkSession.getActiveToolNames()).toEqual(expect.arrayContaining(["read", "write"])); await sdkSession.continuePersistedHistory(); await sdkSession.waitForIdle(); expect(log.bodies).toHaveLength(1); // The production tool set is wrapped for extensions; only the built-in read reaches the wire. expect(toolNames(log.bodies[0])).toEqual(["read"]); expect(log.bodies[0]).toContain("Interrupted before its outcome was observed"); expect(log.bodies[0]).not.toContain("synthesized on resume"); expect(markerCount(sdkSession)).toBe(1); }); } /** Runs the real `skc --continue` root command against the saved session; only the TUI is faked. */ async function cliContinue(options: { sessionDir: string; settings?: Parameters[0]; }): Promise<{ continued: number; notices: string[]; errors: string[] }> { const m = groq(); const tracked = { continued: 0, notices: [] as string[], errors: [] as string[] }; const stop = new Error("stop interactive harness"); authStorage.setRuntimeApiKey(m.provider, `${m.provider}-test-key`); const root = runRootCommand( { messages: [], fileArgs: [], unknownFlags: new Map(), continue: true, sessionDir: options.sessionDir, model: `${m.provider}/${m.id}`, tools: ["read", "write"], noSkills: true, noRules: true, noExtensions: true, }, [], { suppressProcessExit: true, settings: Settings.isolated({ "compaction.enabled": false, "fallback.auto": false, "startup.checkUpdate": false, "marketplace.autoUpdate": "off", ...options.settings, }), discoverAuthStorage: async () => authStorage, createAgentSession: async sessionOptions => { const created = await createAgentSession({ ...sessionOptions, authStorage, modelRegistry, disableExtensionDiscovery: true, enableMCP: false, enableLsp: false, }); session = created.session; const original = created.session.continuePersistedHistory.bind(created.session); vi.spyOn(created.session, "continuePersistedHistory").mockImplementation(async () => { tracked.continued++; await original(); }); created.session.subscribe(event => { if (event.type === "notice") tracked.notices.push(event.message); }); return created; }, initTheme: async () => {}, readPipedInput: async () => undefined, stdinIsTTY: true, runStartupCredentialAutoImportIfNeeded: async () => undefined, getChangelogForDisplay: async () => undefined, createInteractiveMode: () => ({ init: async () => {}, showNewVersionNotification: () => {}, renderInitialMessages: () => {}, showStatus: () => {}, showWarning: () => {}, showError: (message: string) => tracked.errors.push(message), getUserInput: async () => { await session?.waitForIdle(); throw stop; }, }) as unknown as InteractiveMode, }, ); await expect(root).rejects.toBe(stop); return tracked; } async function interruptedSessionDir(m: Model, settled: boolean): Promise { const sessionDir = path.join(tempDir.path(), "sessions"); const first = SessionManager.create(tempDir.path(), SessionManager.explicitDestination(sessionDir)); first.appendMessage({ role: "user", content: "update the file", timestamp: Date.now() }); first.appendMessage(assistantWithCall(m, "w1", "write")); if (!settled) { first.appendCustomEntry("recovery_checkpoint", { version: 1, state: "in_flight", ...restartCheckpoint(m) }); } await first.ensureOnDisk(); await first.flush(); return sessionDir; } it("CLI `skc --continue` resumes an interrupted step once, read-only, without user input", async () => { const m = groq(); const sessionDir = await interruptedSessionDir(m, false); const log = installReplies([textReply(m.id, "verified")]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); const result = await cliContinue({ sessionDir }); expect(result.continued).toBe(1); expect(result.errors).toEqual([]); expect(log.bodies).toHaveLength(1); expect(toolNames(log.bodies[0])).toEqual(["read"]); expect(log.bodies[0]).toContain("Interrupted before its outcome was observed"); expect(result.notices).toContain("Resuming the interrupted step automatically (read-only tools)."); expect(markerCount(session as AgentSession)).toBe(1); }); it("CLI `skc --continue` leaves a session idle when nothing was interrupted or auto-resume is off", async () => { const m = groq(); for (const testCase of [ { settled: true, settings: {} }, { settled: false, settings: { "retry.autoResume": false } }, ]) { const sessionDir = await interruptedSessionDir(m, testCase.settled); const log = installReplies([]); const result = await cliContinue({ sessionDir, settings: testCase.settings }); expect(result.continued).toBe(0); expect(log.bodies).toHaveLength(0); await session?.dispose(); session = undefined; vi.restoreAllMocks(); fs.rmSync(sessionDir, { recursive: true, force: true }); } }); it("17) the active tool list stays the user's during a restricted resume, and later changes are kept", async () => { const m = groq(); const write = counted("write"); const extra = counted("notes"); const reopened = await interruptedSession({ messages: [{ role: "user", content: "update", timestamp: Date.now() }, assistantWithCall(m, "w1", "write")], checkpoint: restartCheckpoint(m), }); const log = installReplies([ toolCallReply(m.id, "r1", "read"), textReply(m.id, "checked"), textReply(m.id, "next"), ]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, tools: [write.tool, readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); const live = session; let changed = false; live.subscribe(event => { if (event.type === "tool_execution_start" && !changed) { changed = true; // A tool refresh mid-resume (as MCP refresh does) changes the selection itself. live.agent.setTools([...live.agent.state.tools, extra.tool]); expect(live.getActiveToolNames().sort()).toEqual(["notes", "read", "write"]); } }); await live.continuePersistedHistory(); await live.waitForIdle(); expect(toolNames(log.bodies[1])).toEqual(["read"]); await live.prompt("next"); await live.waitForIdle(); expect(toolNames(log.bodies[2])).toEqual(["notes", "read", "write"]); }); it("10) a restart resume stops at its own deadline instead of waiting", async () => { const m = groq(); const reopened = await interruptedSession({ messages: [{ role: "user", content: "update", timestamp: Date.now() }, assistantWithCall(m, "w1", "write")], checkpoint: restartCheckpoint(m), }); let now = 1_000; vi.spyOn(performance, "now").mockImplementation(() => now); const log = installReplies([ () => { // The first attempt fails after the 120 s restart window has passed. now += 121_000; return status503(); }, ]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, tools: [counted("write").tool, readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); await session.continuePersistedHistory(); await session.waitForIdle(); await Bun.sleep(20); await session.waitForIdle(); expect(log.bodies).toHaveLength(1); expect(markerCount(session)).toBe(1); }); it("a restart never resumes an answer that already finished", async () => { const m = groq(); const finished: AssistantMessage = { ...assistantWithCall(m, "unused", "write"), content: [{ type: "text", text: "all done" }], stopReason: "stop", }; const reopened = await interruptedSession({ messages: [{ role: "user", content: "summarize", timestamp: Date.now() }, finished], checkpoint: restartCheckpoint(m), }); const log = installReplies([]); session = build({ model: m, tools: [readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); await expect(session.continuePersistedHistory()).rejects.toThrow("interrupted during automatic recovery"); expect(log.bodies).toHaveLength(0); expect(markerCount(session)).toBe(0); }); it("a queued follow-up keeps the resumed run read-only until the user message reaches the model", async () => { const m = groq(); const write = counted("write"); const reopened = await interruptedSession({ messages: [{ role: "user", content: "update", timestamp: Date.now() }, assistantWithCall(m, "w1", "write")], checkpoint: restartCheckpoint(m), }); const log = installReplies([ toolCallReply(m.id, "r1", "read"), textReply(m.id, "checked"), textReply(m.id, "follow-up answered"), ]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, tools: [write.tool, readTool()], sessionManager: reopened }); session.agent.replaceMessages(reopened.buildSessionContext().messages); const live = session; let queued = false; live.subscribe(event => { if (event.type === "tool_execution_start" && !queued) { queued = true; void live.followUp("now write it"); } }); await live.continuePersistedHistory(); await live.waitForIdle(); for (let i = 0; i < 50 && log.bodies.length < 3; i++) await Bun.sleep(5); await live.waitForIdle(); expect(log.bodies).toHaveLength(3); expect(toolNames(log.bodies[0])).toEqual(["read"]); // Queuing did not lift the restriction for the rest of the resumed run. expect(toolNames(log.bodies[1])).toEqual(["read"]); // Once the user's message is in the model context, the user directs the turn. expect(log.bodies[2]).toContain("now write it"); expect(toolNames(log.bodies[2])).toEqual(["read", "write"]); expect(write.runs()).toBe(0); }); it("a failure after a visible text prefix resumes once, inside the same step budget", async () => { const m = groq(); const partialThenDrop = (text: string): Reply => () => { const encoder = new TextEncoder(); let sent = false; const body = new ReadableStream( { pull(controller) { if (!sent) { sent = true; controller.enqueue( encoder.encode(oc(m.id, { role: "assistant" }) + oc(m.id, { content: text })), ); return; } controller.error(new Error("socket hang up")); }, }, { highWaterMark: 0 }, ); return new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } }); }; const log = installReplies([ partialThenDrop("Three findings: one,"), partialThenDrop(" two,"), textReply(m.id, " three."), ]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); const info = vi.spyOn(logger, "info"); session = build({ model: m, tools: [readTool()], settings: { "retry.maxRetries": 1 } }); const deltas: string[] = []; const notices: string[] = []; session.subscribe(event => { if (event.type === "message_update" && event.assistantMessageEvent.type === "text_delta") { deltas.push(event.assistantMessageEvent.delta); } if (event.type === "notice") notices.push(event.message); }); await session.prompt("report"); await session.waitForIdle(); for (let i = 0; i < 50 && log.bodies.length < 3; i++) await Bun.sleep(5); await session.waitForIdle(); expect(log.bodies).toHaveLength(3); expect(markerCount(session)).toBe(1); // The resume request carries the preserved prefix exactly once; nothing shown is re-emitted. const occurrences = (body: string, text: string) => body.split(text).length - 1; expect(occurrences(log.bodies[1], "Three findings: one,")).toBe(1); expect(occurrences(log.bodies[2], "Three findings: one,")).toBe(1); expect(occurrences(log.bodies[2], " two,")).toBe(1); expect(deltas).toEqual(["Three findings: one,", " two,", " three."]); const last = session.agent.state.messages.findLast(message => message.role === "assistant") as AssistantMessage; expect(last.stopReason).toBe("stop"); // The user is told the step resumed; operators get one start/finish pair. expect(notices).toContain("Resuming the interrupted step automatically."); const lifecycle = info.mock.calls.filter(([message]) => String(message).startsWith("auto_resume_")); expect(lifecycle).toEqual([ ["auto_resume_started", { trigger: "interrupted", mode: "full", interruptedCalls: 0 }], ["auto_resume_finished", { trigger: "interrupted", mode: "full", outcome: "completed" }], ]); }); it("a failed uncertainty write closes only its own step and is reported", async () => { const m = groq(); const log = installReplies([textReply(m.id, "fresh")]); let uncertainOnce = true; vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, tools: [readTool()], streamFn: (requested, context, options) => { if (uncertainOnce) { uncertainOnce = false; options?.onUncertainUpstream?.("Gateway dispatch has unobserved remote requests and effects"); } return streamSimple(requested, context, options); }, }); const manager = session.sessionManager; const originalFlush = manager.flush.bind(manager); let failed = false; vi.spyOn(manager, "flush").mockImplementation(async () => { const checkpoint = manager .getBranch() .findLast(entry => entry.type === "custom" && entry.customType === "recovery_checkpoint"); const uncertain = checkpoint?.type === "custom" ? (checkpoint.data as { uncertain?: unknown[] }).uncertain : []; if (!failed && Array.isArray(uncertain) && uncertain.length > 0) { failed = true; throw new Error("disk full"); } await originalFlush(); }); const seen = events(session); await session.prompt("dispatch remotely"); await session.waitForIdle(); // The write failed before the request was admitted: nothing is sent for this step. expect(failed).toBe(true); expect(log.bodies).toHaveLength(0); expect( seen.some(event => event.type === "notice" && event.message.includes("could not be saved (disk full)")), ).toBe(true); // The next user turn is a new step: admission is open again. await session.prompt("start over"); await session.waitForIdle(); expect(log.bodies).toHaveLength(1); const last = session.agent.state.messages.findLast(message => message.role === "assistant") as AssistantMessage; expect(last.stopReason).toBe("stop"); }); // ------------------------------------------------------- thinking-only failure function claude(): Model { const found = getBundledModel("anthropic", "claude-sonnet-4-5"); if (!found) throw new Error("missing bundled model"); return found; } it("hiding thinking blocks never asks the provider to omit reasoning from the stream", async () => { const m = getBundledModel("anthropic", "claude-opus-5-5"); if (!m) throw new Error("missing bundled model"); const log = installAnthropic([anthropicReply(m, ["thinking", "text"], "stop")]); session = build({ model: m, settings: { hideThinkingBlock: true } }); session.agent.hideThinkingSummary = true; session.setThinkingLevel(ThinkingLevel.High); await session.prompt("think hard"); await session.waitForIdle(); expect(session.getThinkingVisibility()).toBe("hidden"); expect(log.bodies).toHaveLength(1); // Omitted reasoning streams only pings during a long thinking phase and the idle // watchdog would cut the healthy answer as "stalled". const thinking = (JSON.parse(log.bodies[0]) as { thinking?: { display?: string } }).thinking; expect(thinking).toBeDefined(); expect(thinking?.display).not.toBe("omitted"); }); function af(event: string, data: object): string { return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; } function anthropicFrames(m: Model, blocks: ReadonlyArray<"thinking" | "text">, text = "answer"): string[] { const frames = [ af("message_start", { type: "message_start", message: { id: "msg_thinking_only", type: "message", role: "assistant", model: m.id, content: [], stop_reason: null, stop_sequence: null, usage: { input_tokens: 1, output_tokens: 0 }, }, }), ]; blocks.forEach((kind, index) => { frames.push( af("content_block_start", { type: "content_block_start", index, content_block: kind === "thinking" ? { type: "thinking", thinking: "", signature: "" } : { type: "text", text: "" }, }), af("content_block_delta", { type: "content_block_delta", index, delta: kind === "thinking" ? { type: "thinking_delta", thinking: "private plan" } : { type: "text_delta", text }, }), ); }); return frames; } const OVERLOADED = af("error", { type: "error", error: { type: "overloaded_error", message: "Overloaded" } }); function anthropicReply(m: Model, blocks: ReadonlyArray<"thinking" | "text">, end: "overloaded" | "stop"): Reply { const frames = anthropicFrames(m, blocks); if (end === "overloaded") frames.push(OVERLOADED); else frames.push( af("content_block_stop", { type: "content_block_stop", index: blocks.length - 1 }), af("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 1 }, }), af("message_stop", { type: "message_stop" }), ); return sse(frames); } function installAnthropic(replies: readonly Reply[]): WireLog { const log: WireLog = { bodies: [] }; vi.spyOn(globalThis, "fetch").mockImplementation((async ( input: Parameters[0], init?: Parameters[1], ) => { const url = input instanceof Request ? input.url : String(input); if (!url.endsWith("/v1/messages")) return new Response("{}", { status: 404 }); log.bodies.push(wireBodyText(init)); const reply = replies[log.bodies.length - 1]; if (!reply) throw new Error(`Unexpected outbound request #${log.bodies.length}`); return reply(); }) as typeof fetch); return log; } it("an overloaded failure that streamed only thinking retries on the same model without replaying it", async () => { const m = claude(); const log = installAnthropic([ anthropicReply(m, ["thinking"], "overloaded"), anthropicReply(m, ["text"], "stop"), ]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m }); await session.prompt("think hard"); await session.waitForIdle(); expect(log.bodies).toHaveLength(2); // The failed reasoning is never sent back, signed or not. expect(log.bodies[1]).not.toContain("private plan"); const last = session.agent.state.messages.findLast(message => message.role === "assistant") as AssistantMessage; expect(last.stopReason).toBe("stop"); expect(last.model).toBe(m.id); const assistants = session.agent.state.messages.filter(message => message.role === "assistant"); expect(assistants).toHaveLength(1); }); const BARE_RETRY = { "retry.maxRetries": undefined, "retry.baseDelayMs": undefined, "retry.maxDelayMs": undefined, } as const; it("with no retry settings, a thinking-only overload still retries the same model within the step budget", async () => { const m = claude(); const log = installAnthropic(Array.from({ length: 9 }, () => anthropicReply(m, ["thinking"], "overloaded"))); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, settings: BARE_RETRY }); await session.prompt("think hard"); await session.waitForIdle(); // 1 original + 6 recoveries: the shared 7-request step budget, then the real error. expect(log.bodies).toHaveLength(7); for (const body of log.bodies) { expect((JSON.parse(body) as { model: string }).model).toBe(m.id); expect(body).not.toContain("private plan"); } const last = session.agent.state.messages.findLast(message => message.role === "assistant") as AssistantMessage; expect(last.stopReason).toBe("error"); expect(last.errorMessage).toContain("overloaded_error"); }); it("with no retry settings, a thinking-only overload recovers once the provider answers", async () => { const m = claude(); const log = installAnthropic([ anthropicReply(m, ["thinking"], "overloaded"), anthropicReply(m, ["text"], "stop"), ]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, settings: BARE_RETRY }); await session.prompt("think hard"); await session.waitForIdle(); expect(log.bodies).toHaveLength(2); const last = session.agent.state.messages.findLast(message => message.role === "assistant") as AssistantMessage; expect(last.stopReason).toBe("stop"); }); it("with no retry settings, a thinking-only rate limit is not retried", async () => { const m = claude(); const rateLimited = sse([ ...anthropicFrames(m, ["thinking"]), af("error", { type: "error", error: { type: "rate_limit_error", message: "Rate limited" } }), ]); const log = installAnthropic([rateLimited, anthropicReply(m, ["text"], "stop")]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, settings: BARE_RETRY }); await session.prompt("think hard"); await session.waitForIdle(); expect(log.bodies).toHaveLength(1); const last = session.agent.state.messages.findLast(message => message.role === "assistant") as AssistantMessage; expect(last.stopReason).toBe("error"); }); it("with no retry settings, a thinking-only throttle quoting a bare number is not mistaken for a 5xx", async () => { const m = claude(); for (const [message, expected] of [ ["500 requests per minute exceeded", 1], ["HTTP 500 internal failure", 2], ] as const) { const failed = sse([ ...anthropicFrames(m, ["thinking"]), af("error", { type: "error", error: { type: "api_failure", message } }), ]); const log = installAnthropic([failed, anthropicReply(m, ["text"], "stop")]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, settings: BARE_RETRY }); await session.prompt("think hard"); await session.waitForIdle(); expect(log.bodies).toHaveLength(expected); await session.dispose(); session = undefined; vi.restoreAllMocks(); } }); it("an explicit retry after a stopped thinking-only failure drops the reasoning and asks again", async () => { const m = claude(); const log = installAnthropic([ anthropicReply(m, ["thinking"], "overloaded"), anthropicReply(m, ["text"], "stop"), ]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, settings: { "retry.enabled": false } }); await session.prompt("think hard"); await session.waitForIdle(); expect(log.bodies).toHaveLength(1); expect(await session.retry()).toBe(true); await session.waitForIdle(); expect(log.bodies).toHaveLength(2); expect(log.bodies[1]).not.toContain("private plan"); const assistants = session.agent.state.messages.filter(message => message.role === "assistant"); expect(assistants).toHaveLength(1); expect((assistants[0] as AssistantMessage).stopReason).toBe("stop"); }); it("a step that streamed only thinking after tools ran resumes once without the failed reasoning", async () => { const m = claude(); const log = installAnthropic([ anthropicReply(m, ["thinking"], "overloaded"), anthropicReply(m, ["text"], "stop"), ]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); // The same-model retry is spent, so the interrupted step reaches automatic resume. session = build({ model: m, settings: { "retry.maxRetries": 0 } }); session.agent.replaceMessages([ { role: "user", content: "check the file", timestamp: Date.now() }, { ...assistantWithCall(m, "r1", "read"), }, { role: "toolResult", toolCallId: "r1", toolName: "read", content: [{ type: "text", text: "contents" }], isError: false, timestamp: Date.now(), }, ]); const info = vi.spyOn(logger, "info"); await session.agent.continue(); await session.waitForIdle(); for (let i = 0; i < 50 && log.bodies.length < 2; i++) await Bun.sleep(5); await session.waitForIdle(); expect(log.bodies).toHaveLength(2); expect(log.bodies[1]).not.toContain("private plan"); // The failed attempt is dropped, not replayed as an aborted turn. expect(log.bodies[1]).not.toContain("turn-aborted"); for (const body of log.bodies) expect((JSON.parse(body) as { model: string }).model).toBe(m.id); expect(markerCount(session)).toBe(1); expect(info.mock.calls.some(([message]) => message === "auto_resume_started")).toBe(true); const last = session.agent.state.messages.findLast(message => message.role === "assistant") as AssistantMessage; expect(last.stopReason).toBe("stop"); }); it("a long thinking-only overload on a managed fallback chain retries the same model, never the next entry", async () => { const m = claude(); const thinkingThenStall: Reply = () => { const encoder = new TextEncoder(); const frames = anthropicFrames(m, ["thinking"]); let next = 0; const body = new ReadableStream( { async pull(controller) { if (next < frames.length) { controller.enqueue(encoder.encode(frames[next++])); return; } // Past the managed hold window the reasoning is already published to the user. await Bun.sleep(2_100); controller.enqueue(encoder.encode(OVERLOADED)); controller.close(); }, }, { highWaterMark: 0 }, ); return new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } }); }; const log = installAnthropic([thinkingThenStall, anthropicReply(m, ["text"], "stop")]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); authStorage.setRuntimeApiKey("groq", "groq-test-key"); session = build({ model: m, settings: { "fallback.auto": true } }); expect(session.getDefaultFallbackChain().entries.length).toBeGreaterThan(1); await session.prompt("think hard"); await session.waitForIdle(); for (let i = 0; i < 50 && log.bodies.length < 2; i++) await Bun.sleep(5); await session.waitForIdle(); expect(log.bodies).toHaveLength(2); expect(log.bodies[1]).not.toContain("private plan"); for (const body of log.bodies) expect((JSON.parse(body) as { model: string }).model).toBe(m.id); const last = session.agent.state.messages.findLast(message => message.role === "assistant") as AssistantMessage; expect(last.stopReason).toBe("stop"); expect(last.model).toBe(m.id); }, 15_000); it("a failure with thinking and visible text still stops with the text preserved", async () => { const m = claude(); const log = installAnthropic([anthropicReply(m, ["thinking", "text"], "overloaded")]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m }); await session.prompt("think then answer"); await session.waitForIdle(); expect(log.bodies).toHaveLength(1); const last = session.agent.state.messages.findLast(message => message.role === "assistant") as AssistantMessage; expect(last.stopReason).toBe("error"); expect(last.content.some(block => block.type === "text" && block.text === "answer")).toBe(true); }); it("with no retry settings, a blank-text failure on a code-less internal error retries without replaying the blanks", async () => { // grok-4.7 degenerates into ~4K whitespace tokens, then xAI aborts the stream with // "Internal error during token generation" and no error code. const m = claude(); const blanks = "\n \n\n \n".repeat(40); const degenerate = sse([ ...anthropicFrames(m, ["text"], blanks), af("error", { type: "error", error: { message: "Internal error during token generation" } }), ]); const log = installAnthropic([degenerate, anthropicReply(m, ["text"], "stop")]); vi.spyOn(scheduler, "wait").mockResolvedValue(undefined); session = build({ model: m, settings: BARE_RETRY }); await session.prompt("keep going"); await session.waitForIdle(); expect(log.bodies).toHaveLength(2); expect(log.bodies[1]).not.toContain(JSON.stringify(blanks).slice(1, -1)); const assistants = session.agent.state.messages.filter(message => message.role === "assistant"); expect(assistants).toHaveLength(1); expect((assistants[0] as AssistantMessage).stopReason).toBe("stop"); }); });