From db553c820e4e297ea553d90ff112e19ee2380541 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 07:42:35 -0800 Subject: [PATCH 01/20] fix: `run --max-turns` charges completed steps, not retried attempts A turn was charged on every step start, so each retried attempt of a step that failed mid-stream cost a turn. Charge on step-finish instead; a step may start only while fewer than `--max-turns` steps completed. Compaction stays excluded; harness-injected steps still count. Closes #1808 Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/cli/cmd/run-accounting.ts | 31 ++- packages/opencode/src/cli/cmd/run.ts | 10 +- .../__snapshots__/help-snapshots.test.ts.snap | 5 +- .../opencode/test/cli/run-accounting.test.ts | 48 +++- .../test/cli/run/budget-finalization.test.ts | 24 +- .../test/cli/run/turn-budget-retry.test.ts | 239 ++++++++++++++++++ 6 files changed, 337 insertions(+), 20 deletions(-) create mode 100644 packages/opencode/test/cli/run/turn-budget-retry.test.ts diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index ecd915bcb3..f758bffc10 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -1,8 +1,10 @@ // Fork-only helpers for the `run` command: // - honest turn accounting: compaction-machinery steps must not consume the -// --max-turns budget. `step-start` parts carry only messageID/sessionID, so -// the owning message's summary flag is resolved via a lookup populated from -// `message.updated` events (the assistant message row is persisted — and its +// --max-turns budget, and a turn is charged when a step completes, so a +// failed or retried attempt costs nothing. Step parts carry only +// messageID/sessionID, so the owning message's summary flag is resolved via +// a lookup populated from `message.updated` events (the assistant message +// row is persisted — and its // event published — before its first step-start part streams). // - dual-attribution termination logging: every run records TWO independent // fields instead of one rc: `why_model_stopped` and `why_harness_stopped`, @@ -166,23 +168,36 @@ export function create() { else summaryMessages.delete(info.id) }, isCompactionStep, + // altimate_change start — charge the turn budget on step-finish, not step-start + // A step-start opens an attempt; the processor retries a failed attempt + // (up to SessionRetry.RETRY_MAX_ATTEMPTS per step), and each retry streams + // its own step-start but only the attempt that completes streams a + // step-finish. Charging on start billed every failed attempt as a turn. + // `turnCount` is therefore the number of COMPLETED non-compaction steps; + // harness-injected generations (idle-done challenge, continuation, + // silent-turn reply) are real assistant steps and are charged the same way. /** - * Count a step-start toward the turn budget unless it belongs to a - * compaction-machinery message. Returns true when the step was counted. + * Whether a starting step is subject to the turn budget (false for + * compaction machinery). The step is charged only when it finishes. */ onStepStart(messageID: string): boolean { - if (isCompactionStep(messageID)) return false - turnCount++ - return true + return !isCompactionStep(messageID) }, + /** Completed non-compaction assistant steps. */ get turnCount() { return turnCount }, + /** True when `maxTurns` completed steps already ran, so no further step may start. */ + turnBudgetReached(maxTurns: number | undefined): boolean { + return maxTurns !== undefined && maxTurns > 0 && turnCount >= maxTurns + }, onStepFinish(messageID: string, reason: string | undefined) { if (isCompactionStep(messageID)) return + turnCount++ lastFinishReason = reason lastFinishMessageID = messageID }, + // altimate_change end onText(messageID: string, text: string, synthetic = false) { if (isCompactionStep(messageID)) return if (synthetic) return diff --git a/packages/opencode/src/cli/cmd/run.ts b/packages/opencode/src/cli/cmd/run.ts index 39ec739b81..4b10069527 100644 --- a/packages/opencode/src/cli/cmd/run.ts +++ b/packages/opencode/src/cli/cmd/run.ts @@ -416,7 +416,8 @@ export const RunCommand = cmd({ // altimate_change start — budget limits for CI/enterprise governance .option("max-turns", { type: "number", - describe: "maximum assistant work turns; allows one committed-answer echo, or one best-effort reply after a budget abort", + describe: + "maximum completed assistant work turns (a failed or retried attempt is not counted); allows one committed-answer echo, or one best-effort reply after a budget abort", }) // altimate_change start — wall-clock work budget, with a separate finalization allowance .option("time-budget", { @@ -1149,12 +1150,14 @@ You are speaking to a non-technical business executive. Follow these rules stric // compaction-machinery steps are excluded from turn accounting — // the owning message's agent is resolved via the message.updated lookup // above, so compacting models are not differentially charged turns. + // Turns are charged on step-finish: a step may start only while fewer + // than maxTurns steps have completed, so a retried attempt is free. const counted = accounting.onStepStart(part.messageID) const requestID = requestOwnership.ownerOf(part.messageID) const committedAnswer = requestID !== undefined && committedRequests.has(requestID) // altimate_change start — the time-budget abort already owns the interrupted work phase if ( - counted && maxTurns && accounting.turnCount > maxTurns && !committedAnswer && !budgetAborted && + counted && accounting.turnBudgetReached(maxTurns) && !committedAnswer && !budgetAborted && (!timeExhausted() || options?.turnLimit !== undefined) ) { stopTimeBudget() @@ -2049,7 +2052,8 @@ You are speaking to a non-technical business executive. Follow these rules stric // A silent stop exactly at the work limit has no in-flight generation to // abort. Enter finalization directly instead of spending a reply just to // discover that it exceeds the same budget. - if (!answered() && assistantStarted && !accounting.fatal && args.maxTurns && accounting.turnCount >= args.maxTurns) { + // The same completed-step count the in-loop budget check uses. + if (!answered() && assistantStarted && !accounting.fatal && accounting.turnBudgetReached(args.maxTurns)) { accounting.onBudgetExhausted() error = `Budget exceeded: reached ${args.maxTurns} assistant turn${args.maxTurns !== 1 ? "s" : ""} limit` if (!emit("budget_exhausted", { limit: args.maxTurns })) { diff --git a/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap b/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap index 020fee6fb1..01f6283f32 100644 --- a/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap +++ b/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap @@ -135,8 +135,9 @@ Options: statement (1-indexed) [number] --trace enable session tracing (default: true, disable with --no-trace) [boolean] [default: true] - --max-turns maximum assistant work turns; allows one committed-answer - echo, or one best-effort reply after a budget abort [number] + --max-turns maximum completed assistant work turns (a failed or retried + attempt is not counted); allows one committed-answer echo, or + one best-effort reply after a budget abort [number] --time-budget maximum time in seconds including startup (env: ALTIMATE_RUN_TIME_BUDGET_S); bounded finalization may follow [number] diff --git a/packages/opencode/test/cli/run-accounting.test.ts b/packages/opencode/test/cli/run-accounting.test.ts index 0338839614..02773d3247 100644 --- a/packages/opencode/test/cli/run-accounting.test.ts +++ b/packages/opencode/test/cli/run-accounting.test.ts @@ -22,22 +22,66 @@ describe("RunAccounting recoverable overflow trace errors", () => { }) describe("RunAccounting turn accounting", () => { - test("counts ordinary assistant steps", () => { + test("counts ordinary assistant steps when they finish", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) expect(acc.onStepStart("msg_1")).toBe(true) + acc.onStepFinish("msg_1", "tool-calls") expect(acc.onStepStart("msg_1")).toBe(true) + acc.onStepFinish("msg_1", "stop") expect(acc.turnCount).toBe(2) }) + test("a step that fails mid-stream and is retried costs exactly one turn", () => { + const acc = RunAccounting.create() + acc.onAssistantMessage({ id: "msg_1", agent: "build" }) + // attempt 1 streams its step-start, then the provider stream breaks: no step-finish + acc.onStepStart("msg_1") + // attempt 2 (the processor's retry) starts the same step again + acc.onStepStart("msg_1") + expect(acc.turnCount).toBe(0) + acc.onStepFinish("msg_1", "stop") + expect(acc.turnCount).toBe(1) + }) + + test("a step whose every attempt fails costs no turn and keeps its error", () => { + const acc = RunAccounting.create() + acc.onAssistantMessage({ id: "msg_1", agent: "build" }) + // the first attempt plus every retry the processor allows, none completing + for (let attempt = 0; attempt <= 5; attempt++) acc.onStepStart("msg_1") + acc.onSessionError("APIError", "upstream stream reset") + expect(acc.turnCount).toBe(0) + expect(acc.turnBudgetReached(1)).toBe(false) + expect(acc.fatal).toBe(true) + expect(acc.termination().why_harness_stopped).toBe("error") + }) + + test("the budget is reached only after maxTurns steps complete", () => { + const acc = RunAccounting.create() + acc.onAssistantMessage({ id: "msg_1", agent: "build" }) + expect(acc.turnBudgetReached(1)).toBe(false) + acc.onStepStart("msg_1") + expect(acc.turnBudgetReached(1)).toBe(false) + acc.onStepStart("msg_1") // a retried attempt does not trip a one-turn budget + expect(acc.turnBudgetReached(1)).toBe(false) + acc.onStepFinish("msg_1", "tool-calls") + expect(acc.turnBudgetReached(1)).toBe(true) + expect(acc.turnBudgetReached(2)).toBe(false) + expect(acc.turnBudgetReached(undefined)).toBe(false) + }) + test("excludes compaction-machinery steps from turnCount", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_work", agent: "build" }) acc.onAssistantMessage({ id: "msg_compact", agent: "compaction", summary: true }) expect(acc.onStepStart("msg_work")).toBe(true) + acc.onStepFinish("msg_work", "tool-calls") expect(acc.onStepStart("msg_compact")).toBe(false) + acc.onStepFinish("msg_compact", "stop") expect(acc.onStepStart("msg_compact")).toBe(false) + acc.onStepFinish("msg_compact", "stop") expect(acc.onStepStart("msg_work")).toBe(true) + acc.onStepFinish("msg_work", "stop") expect(acc.turnCount).toBe(2) }) @@ -45,12 +89,14 @@ describe("RunAccounting turn accounting", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_user_agent", agent: "compaction", summary: false }) expect(acc.onStepStart("msg_user_agent")).toBe(true) + acc.onStepFinish("msg_user_agent", "stop") expect(acc.turnCount).toBe(1) }) test("a step whose owning message is unknown is counted (conservative default)", () => { const acc = RunAccounting.create() expect(acc.onStepStart("msg_unknown")).toBe(true) + acc.onStepFinish("msg_unknown", "stop") expect(acc.turnCount).toBe(1) }) diff --git a/packages/opencode/test/cli/run/budget-finalization.test.ts b/packages/opencode/test/cli/run/budget-finalization.test.ts index 90ddbec384..6f315726fe 100644 --- a/packages/opencode/test/cli/run/budget-finalization.test.ts +++ b/packages/opencode/test/cli/run/budget-finalization.test.ts @@ -13,6 +13,7 @@ function exhausted() { const accounting = RunAccounting.create() accounting.onStepStart("work") accounting.onStepFinish("work", "tool-calls") + // the over-budget step is aborted at its start, so it never completes accounting.onStepStart("interrupted") accounting.onBudgetExhausted() return accounting @@ -45,7 +46,7 @@ describe("budget finalization accounting", () => { accounting.onSessionError("MessageAbortedError", "intentional time-budget abort") accounting.onPromptResult({ error: { name: "MessageAbortedError" } }) expect(accounting.fatal).toBe(true) - expect(accounting.takeBudgetFinalizationAllowance()).toBe(2) + expect(accounting.takeBudgetFinalizationAllowance()).toBe(1) accounting.onStepStart("finalization") accounting.onText("finalization", "The evidence supports a limited answer.") accounting.onStepFinish("finalization", "stop") @@ -79,7 +80,7 @@ describe("budget finalization accounting", () => { test("successful turn-budget finalization preserves the existing nonzero exit contract", () => { const accounting = exhausted() - expect(accounting.takeBudgetFinalizationAllowance()).toBe(3) + expect(accounting.takeBudgetFinalizationAllowance()).toBe(2) expect(accounting.completeBudgetFinalization(true)).toBe(false) expect(accounting.fatal).toBe(true) }) @@ -111,15 +112,17 @@ describe("budget finalization accounting", () => { accounting.onSessionError("MessageAbortedError", "aborted by the turn limit") accounting.onPromptResult({ error: { name: "MessageAbortedError" } }) - expect(accounting.takeBudgetFinalizationAllowance()).toBe(3) + expect(accounting.takeBudgetFinalizationAllowance()).toBe(2) expect(accounting.takeBudgetFinalizationAllowance()).toBeUndefined() - expect(accounting.turnCount).toBe(2) + expect(accounting.turnCount).toBe(1) expect(accounting.fatal).toBe(true) accounting.onStepStart("finalization") + expect(accounting.turnBudgetReached(2)).toBe(false) accounting.onText("finalization", "The supported result is available; verification is incomplete.") accounting.onStepFinish("finalization", "stop") - expect(accounting.turnCount).toBe(3) + expect(accounting.turnCount).toBe(2) + expect(accounting.turnBudgetReached(2)).toBe(true) expect(accounting.termination()).toEqual({ why_model_stopped: "stop", why_harness_stopped: "budget-exhausted", @@ -139,7 +142,7 @@ describe("budget finalization accounting", () => { const accounting = exhausted() accounting.onSessionError("MessageAbortedError", "aborted") accounting.onPromptResult({ finish }) - expect(accounting.takeBudgetFinalizationAllowance()).toBe(3) + expect(accounting.takeBudgetFinalizationAllowance()).toBe(2) }) } @@ -192,6 +195,8 @@ describe("budget finalization accounting", () => { accounting.onIdleDoneChallengeReplySent() accounting.onIdleDoneChallengeCompleted() accounting.onStepStart("continuation") + accounting.onStepFinish("continuation", "tool-calls") + accounting.onStepStart("over-budget") accounting.onBudgetExhausted() accounting.onSessionError("MessageAbortedError", "budget interruption") accounting.onPromptResult({ error: { name: "MessageAbortedError" } }) @@ -357,6 +362,10 @@ describe("opencode run: budget finalization subprocess", () => { }, }) } + // The work step completes; only completed steps are charged to the budget. + publish("message.part.updated", { + part: { id: `${messageID}_finish`, messageID, sessionID, type: "step-finish", reason: "tool-calls" }, + }) if (sharedRequest) { // The original echo remains exempt, including synthetic compaction continuation rows. const continuationID = `${parentID}_continuation` @@ -568,6 +577,7 @@ describe("opencode run: budget finalization subprocess", () => { ownership.beginRequest("original") ownership.onAssistantMessage({ id: "commit", parentID: "original" }) accounting.onStepStart("commit") + accounting.onStepFinish("commit", "tool-calls") committedRequests.add(ownership.ownerOf("commit")!) const queue = () => { ownership.onUserMessage("queued") @@ -579,6 +589,7 @@ describe("opencode run: budget finalization subprocess", () => { ownership.onAssistantMessage({ id: "summary", parentID: "marker" }) accounting.onAssistantMessage({ id: "summary", summary: true }) expect(accounting.onStepStart("summary")).toBe(false) + accounting.onStepFinish("summary", "stop") if (queued === "during the summary") queue() // SessionCompaction persists this synthetic user before reporting its origin. @@ -590,6 +601,7 @@ describe("opencode run: budget finalization subprocess", () => { ownership.onAssistantMessage({ id: "echo", parentID: "second-continuation" }) expect(ownership.ownerOf("echo")).toBe("original") expect(accounting.onStepStart("echo")).toBe(true) + accounting.onStepFinish("echo", "stop") expect(accounting.turnCount).toBe(2) expect(committedRequests.has(ownership.ownerOf("echo")!)).toBe(true) diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts new file mode 100644 index 0000000000..9672ba59cf --- /dev/null +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -0,0 +1,239 @@ +// altimate_change start — `--max-turns` charges completed steps, not retried attempts +import { describe, expect } from "bun:test" +import { Effect } from "effect" +import { SessionRetry } from "../../../src/session/retry" +import { cliIt } from "../../lib/cli-process" +import { raw } from "../../lib/llm-server" + +const chunk = (delta: Record) => ({ + id: "chatcmpl-test", + object: "chat.completion.chunk", + choices: [{ delta }], +}) + +// A provider stream that starts a step, streams some text, then reports an error +// in-band. The error matches the processor's retryable rate-limit detection, so +// the session retries the step. +const failsMidStream = () => + raw({ + head: [ + chunk({ role: "assistant" }), + chunk({ content: "Partial work" }), + { error: { message: "rate limit exceeded", type: "rate_limit_error", code: "rate_limit_exceeded" } }, + ], + }) + +describe("opencode run: --max-turns charges completed steps only", () => { + cliIt.concurrent( + "a step that fails mid-stream and is retried costs one turn", + ({ llm, opencode }) => + Effect.gen(function* () { + yield* llm.push(failsMidStream()) + yield* llm.text("The answer after one retry.") + const result = yield* opencode.run("Answer once.", { + format: "json", + extraArgs: ["--max-turns", "1", "--no-trace"], + timeoutMs: 45_000, + bunRun: true, + }) + const events = opencode.parseJsonEvents(result.stdout) + expect(events.filter((event) => event.type === "budget_exhausted")).toHaveLength(0) + expect(events.filter((event) => event.type === "error")).toHaveLength(0) + expect( + events.filter((event) => event.type === "text").map((event) => (event.part as { text: string }).text), + ).toEqual(["The answer after one retry."]) + // One step-finish for the one completed step, whatever the number of attempts. + expect(events.filter((event) => event.type === "step_finish")).toHaveLength(1) + opencode.expectExit(result, 0) + }), + 60_000, + ) + // Attached-session fake: the CLI sees exactly the events the session publishes for + // a step whose attempts start and fail, without waiting out real retry backoff. + const attached = (behavior: "all-attempts-fail" | "endless-attempts") => + Effect.gen(function* () { + const sessionID = "ses_turn_budget_retry" + let stream: ReadableStreamDefaultController | undefined + let subscriptionReady = Promise.withResolvers() + let finishPrompt: (value: Response) => void = () => {} + let attemptTimer: ReturnType | undefined + const counts = { prompts: 0, aborts: 0, attempts: 0 } + const encoder = new TextEncoder() + const publish = (type: string, properties: object) => + stream?.enqueue(encoder.encode(`data: ${JSON.stringify({ type, properties })}\n\n`)) + const startAttempt = (messageID: string) => { + counts.attempts++ + publish("message.part.updated", { + part: { id: `${messageID}_start_${counts.attempts}`, messageID, sessionID, type: "step-start" }, + }) + publish("session.status", { + sessionID, + status: { type: "retry", attempt: counts.attempts, message: "rate limit exceeded", next: Date.now() + 10 }, + }) + } + const server = yield* Effect.acquireRelease( + Effect.sync(() => + Bun.serve({ + hostname: "127.0.0.1", + port: 0, + idleTimeout: 0, + async fetch(request) { + const route = new URL(request.url).pathname + if (route === "/provider") + return Response.json({ all: [{ id: "test", models: { "test-model": {} } }], connected: ["test"] }) + if (route === "/config") return Response.json({ share: "disabled", tracing: { enabled: false } }) + if (route === "/event") + return new Response( + new ReadableStream({ + start(controller) { + stream = controller + subscriptionReady.resolve() + publish("server.connected", {}) + }, + }), + { headers: { "content-type": "text/event-stream" } }, + ) + if (route === `/session/${sessionID}/abort`) { + counts.aborts++ + clearInterval(attemptTimer) + const error = { name: "MessageAbortedError", data: { message: "aborted" } } + publish("session.error", { sessionID, error }) + publish("session.status", { sessionID, status: { type: "idle" } }) + finishPrompt(Response.json({ info: { error } })) + return Response.json(true) + } + if (route === `/session/${sessionID}/message` && request.method === "POST") { + const body = (await request.json()) as { messageID: string } + const ready = subscriptionReady + await ready.promise + subscriptionReady = Promise.withResolvers() + counts.prompts++ + const parentID = body.messageID + const messageID = `${parentID}zz_assistant_${counts.prompts}` + publish("session.status", { sessionID, status: { type: "busy" } }) + publish("message.updated", { + info: { + id: messageID, + sessionID, + role: "assistant", + parentID, + agent: "build", + modelID: "test-model", + providerID: "test", + time: { created: Date.now() }, + }, + }) + if (counts.prompts > 1) { + // The time-budget finalization reply completes normally. + startAttempt(messageID) + publish("message.part.updated", { + part: { + id: `${messageID}_text`, + messageID, + sessionID, + type: "text", + text: "Best-effort answer.", + time: { start: 1, end: 2 }, + }, + }) + publish("message.part.updated", { + part: { id: `${messageID}_finish`, messageID, sessionID, type: "step-finish", reason: "stop" }, + }) + publish("session.status", { sessionID, status: { type: "idle" } }) + return Response.json({ info: { finish: "stop" } }) + } + if (behavior === "all-attempts-fail") { + // The first attempt plus every retry the processor allows, none completing. + for (let attempt = 0; attempt <= SessionRetry.RETRY_MAX_ATTEMPTS; attempt++) startAttempt(messageID) + const error = { name: "APIError", data: { message: "rate limit exceeded", isRetryable: true } } + publish("session.error", { sessionID, error }) + publish("session.status", { sessionID, status: { type: "idle" } }) + return Response.json({ info: { error } }) + } + // A step that keeps failing and restarting without ever completing: + // more attempts than one step's retry cap at once, then more until aborted. + for (let attempt = 0; attempt <= SessionRetry.RETRY_MAX_ATTEMPTS + 1; attempt++) startAttempt(messageID) + attemptTimer = setInterval(() => startAttempt(messageID), 20) + return new Promise((resolve) => { + finishPrompt = resolve + }) + } + return Response.json({ error: `Unexpected route: ${request.method} ${route}` }, { status: 404 }) + }, + }), + ), + (server) => + Effect.sync(() => { + clearInterval(attemptTimer) + server.stop(true) + }), + ) + return { server, sessionID, counts } + }) + + cliIt.concurrent( + "a step whose every attempt fails costs no turn and surfaces the error", + ({ opencode }) => + Effect.gen(function* () { + const { server, sessionID, counts } = yield* attached("all-attempts-fail") + const result = yield* opencode.run("Answer once.", { + format: "json", + extraArgs: ["--attach", server.url.toString(), "--session", sessionID, "--max-turns", "1", "--no-trace"], + timeoutMs: 20_000, + bunRun: true, + }) + opencode.expectExit(result, 1) + const events = opencode.parseJsonEvents(result.stdout) + expect(events.filter((event) => event.type === "step_start")).toHaveLength(SessionRetry.RETRY_MAX_ATTEMPTS + 1) + // Attempts that never completed did not exhaust the one-turn budget. + expect(events.filter((event) => event.type === "budget_exhausted")).toHaveLength(0) + expect(counts.aborts).toBe(0) + // The provider failure is what the run reports, and nothing re-prompts after it. + expect(events.filter((event) => event.type === "error")).toContainEqual( + expect.objectContaining({ error: expect.objectContaining({ name: "APIError" }) }), + ) + expect(counts.prompts).toBe(1) + expect(events.filter((event) => event.type === "termination")).toMatchObject([ + { why_harness_stopped: "error" }, + ]) + }), + 30_000, + ) + + cliIt.concurrent( + "attempts that keep restarting without completing are still bounded by --time-budget", + ({ opencode }) => + Effect.gen(function* () { + const { server, sessionID, counts } = yield* attached("endless-attempts") + const result = yield* opencode.run("Answer once.", { + format: "json", + extraArgs: [ + "--attach", + server.url.toString(), + "--session", + sessionID, + "--max-turns", + "1", + "--time-budget", + "5", + "--no-trace", + ], + timeoutMs: 25_000, + bunRun: true, + }) + const events = opencode.parseJsonEvents(result.stdout) + // The turn budget never fires (no attempt completed); the wall-clock budget ends the work. + expect(events.filter((event) => event.type === "budget_exhausted")).toEqual([ + expect.objectContaining({ limit_seconds: 5 }), + ]) + expect(counts.aborts).toBeGreaterThanOrEqual(1) + expect(counts.attempts).toBeGreaterThan(SessionRetry.RETRY_MAX_ATTEMPTS + 1) + expect(result.durationMs).toBeLessThan(20_000) + expect( + events.filter((event) => event.type === "text").map((event) => (event.part as { text: string }).text), + ).toEqual(["Best-effort answer."]) + }), + 40_000, + ) +}) +// altimate_change end From b2aa0bc838523a92f338ebe95212307c5eb12339 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 07:58:38 -0800 Subject: [PATCH 02/20] test: real-processor retry-cap test; fix header comment wrap The processor retry cap is what bounds a step whose every attempt fails mid-stream now that failed attempts cost no turn; test it end to end with backoff stubbed. Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/cli/cmd/run-accounting.ts | 4 +- .../test/session/processor-effect.test.ts | 56 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index f758bffc10..29ce8d36f8 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -4,8 +4,8 @@ // failed or retried attempt costs nothing. Step parts carry only // messageID/sessionID, so the owning message's summary flag is resolved via // a lookup populated from `message.updated` events (the assistant message -// row is persisted — and its -// event published — before its first step-start part streams). +// row is persisted — and its event published — before its first +// step-start part streams). // - dual-attribution termination logging: every run records TWO independent // fields instead of one rc: `why_model_stopped` and `why_harness_stopped`, // so model-looping, tight budgets, and harness errors stop being conflated diff --git a/packages/opencode/test/session/processor-effect.test.ts b/packages/opencode/test/session/processor-effect.test.ts index 3af5cf283a..44c3a4385e 100644 --- a/packages/opencode/test/session/processor-effect.test.ts +++ b/packages/opencode/test/session/processor-effect.test.ts @@ -17,6 +17,7 @@ import { SessionProcessor } from "../../src/session/processor" import { MessageID, PartID, SessionID } from "../../src/session/schema" import { ProviderID, ModelID } from "@/provider/schema" import { SessionStatus } from "../../src/session/status" +import { SessionRetry } from "../../src/session/retry" import { SessionSummary } from "../../src/session/summary" import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner" import { testEffect } from "../lib/effect" @@ -1121,6 +1122,61 @@ it.live("session.processor effect tests retry recognized structured json errors" { config: (url) => providerCfg(url) }, ), ) +// altimate_change start — the retry cap bounds a step whose every attempt fails mid-stream. +// `run --max-turns` charges only completed steps, so this cap (not the turn budget) is what +// stops a provider that breaks every stream after the step started. +it.live("session.processor effect tests stop retrying a mid-stream failure at the retry cap", () => + provideTmpdirServerLegacy( + ({ dir, llm }) => + Effect.gen(function* () { + const { processors, session, provider } = yield* boot() + const failsMidStream = () => + raw({ + head: [ + { id: "chatcmpl-test", object: "chat.completion.chunk", choices: [{ delta: { role: "assistant" } }] }, + { id: "chatcmpl-test", object: "chat.completion.chunk", choices: [{ delta: { content: "partial" } }] }, + { error: { message: "rate limit exceeded", type: "rate_limit_error", code: "rate_limit_exceeded" } }, + ], + }) + // One more failing stream than the cap allows; it must never be requested. + for (let attempt = 0; attempt <= SessionRetry.RETRY_MAX_ATTEMPTS + 1; attempt++) yield* llm.push(failsMidStream()) + // Skip the real exponential backoff; the cap, not the delay, is under test. + const delay = spyOn(SessionRetry, "delay").mockReturnValue(1) + yield* Effect.addFinalizer(() => Effect.sync(() => delay.mockRestore())) + + const chat = yield* session.create({}) + const parent = yield* user(chat.id, "retry cap") + const msg = yield* assistant(chat.id, parent.id, path.resolve(dir)) + const mdl = yield* refModel(provider) + const controller = new AbortController() + const handle = yield* processors.create({ + assistantMessage: msg as unknown as MessageV2.Assistant, + sessionID: chat.id, + model: mdl, + abort: controller.signal, + }) + + yield* runProcess(handle, controller, { + user: userInput(parent, chat.id), + sessionID: chat.id, + model: mdl, + agent: agent(), + system: [], + messages: [{ role: "user", content: "retry cap" }], + tools: {}, + }) + + const parts = MessageV2.parts(msg.id) + expect(yield* llm.calls).toBe(SessionRetry.RETRY_MAX_ATTEMPTS + 1) + // Every attempt started a step; none finished, so none would be charged a turn. + expect(parts.filter((part) => part.type === "step-start")).toHaveLength(SessionRetry.RETRY_MAX_ATTEMPTS + 1) + expect(parts.filter((part) => part.type === "step-finish")).toHaveLength(0) + expect(handle.message.error).toBeDefined() + }), + { config: (url) => providerCfg(url) }, + ), +) +// altimate_change end // BUG: REGRESSION: retry attempts no longer reach the EventV2 status listener used by the Effect processor path. // Minimal src fix: packages/opencode/src/session/processor.ts:540 should publish retry through the same // EventV2Bridge/SessionStatus service instance provided to the processor Effect runtime, not an isolated facade. From f3d86f718d5f6f5a456e858ad003de6eb58c9ce3 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 08:04:46 -0800 Subject: [PATCH 03/20] test: the attached fake answers the soft time-budget reminder without treating it as a prompt Co-Authored-By: Claude Opus 5.5 --- packages/opencode/test/cli/run/turn-budget-retry.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index 9672ba59cf..4a3fc9786b 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -103,7 +103,9 @@ describe("opencode run: --max-turns charges completed steps only", () => { return Response.json(true) } if (route === `/session/${sessionID}/message` && request.method === "POST") { - const body = (await request.json()) as { messageID: string } + const body = (await request.json()) as { messageID: string; noReply?: boolean } + // The soft time-budget reminder is saved without a reply; it is not a prompt. + if (body.noReply) return Response.json({ info: { id: body.messageID, role: "user" } }) const ready = subscriptionReady await ready.promise subscriptionReady = Promise.withResolvers() From e44eab208a018c2d7a78c541f9e6ed04fcaf9775 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 08:07:31 -0800 Subject: [PATCH 04/20] refactor: drop the side-effect-free step-start wrapper; one compaction lookup per step-start Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/cli/cmd/run-accounting.ts | 7 ---- packages/opencode/src/cli/cmd/run.ts | 10 +++-- .../opencode/test/cli/run-accounting.test.ts | 40 ++++++------------- .../test/cli/run/budget-finalization.test.ts | 14 +------ 4 files changed, 20 insertions(+), 51 deletions(-) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index 29ce8d36f8..2ff41aff62 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -176,13 +176,6 @@ export function create() { // `turnCount` is therefore the number of COMPLETED non-compaction steps; // harness-injected generations (idle-done challenge, continuation, // silent-turn reply) are real assistant steps and are charged the same way. - /** - * Whether a starting step is subject to the turn budget (false for - * compaction machinery). The step is charged only when it finishes. - */ - onStepStart(messageID: string): boolean { - return !isCompactionStep(messageID) - }, /** Completed non-compaction assistant steps. */ get turnCount() { return turnCount diff --git a/packages/opencode/src/cli/cmd/run.ts b/packages/opencode/src/cli/cmd/run.ts index 4b10069527..2e36393333 100644 --- a/packages/opencode/src/cli/cmd/run.ts +++ b/packages/opencode/src/cli/cmd/run.ts @@ -1137,14 +1137,17 @@ You are speaking to a non-technical business executive. Follow these rules stric } if (part.type === "step-start") { + // altimate_change start — one compaction lookup serves output, `step` and the turn budget + const compactionStep = accounting.isCompactionStep(part.messageID) + // altimate_change end // altimate_change start — `--output` keeps only the last step's text (see run-output.ts) - outputParts.stepStart(accounting.isCompactionStep(part.messageID)) + outputParts.stepStart(compactionStep) // altimate_change end // altimate_change start — see `step` (#1334). Compaction steps do not count, // as for the turn budget below: one running after the final answer would // otherwise move `step` past `lastTextStep` and ask for a reply the model // already gave. (release review) - if (!accounting.isCompactionStep(part.messageID)) step++ + if (!compactionStep) step++ // altimate_change end // altimate_change start — enforce max-turns budget // compaction-machinery steps are excluded from turn accounting — @@ -1152,12 +1155,11 @@ You are speaking to a non-technical business executive. Follow these rules stric // above, so compacting models are not differentially charged turns. // Turns are charged on step-finish: a step may start only while fewer // than maxTurns steps have completed, so a retried attempt is free. - const counted = accounting.onStepStart(part.messageID) const requestID = requestOwnership.ownerOf(part.messageID) const committedAnswer = requestID !== undefined && committedRequests.has(requestID) // altimate_change start — the time-budget abort already owns the interrupted work phase if ( - counted && accounting.turnBudgetReached(maxTurns) && !committedAnswer && !budgetAborted && + !compactionStep && accounting.turnBudgetReached(maxTurns) && !committedAnswer && !budgetAborted && (!timeExhausted() || options?.turnLimit !== undefined) ) { stopTimeBudget() diff --git a/packages/opencode/test/cli/run-accounting.test.ts b/packages/opencode/test/cli/run-accounting.test.ts index 02773d3247..aed593eb29 100644 --- a/packages/opencode/test/cli/run-accounting.test.ts +++ b/packages/opencode/test/cli/run-accounting.test.ts @@ -25,9 +25,7 @@ describe("RunAccounting turn accounting", () => { test("counts ordinary assistant steps when they finish", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) - expect(acc.onStepStart("msg_1")).toBe(true) acc.onStepFinish("msg_1", "tool-calls") - expect(acc.onStepStart("msg_1")).toBe(true) acc.onStepFinish("msg_1", "stop") expect(acc.turnCount).toBe(2) }) @@ -35,20 +33,20 @@ describe("RunAccounting turn accounting", () => { test("a step that fails mid-stream and is retried costs exactly one turn", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) - // attempt 1 streams its step-start, then the provider stream breaks: no step-finish - acc.onStepStart("msg_1") - // attempt 2 (the processor's retry) starts the same step again - acc.onStepStart("msg_1") + // Attempt 1 started and its stream broke, so it wrote no step-finish. The + // retry's start is checked against completed steps only, so a one-turn + // budget still lets it run. expect(acc.turnCount).toBe(0) + expect(acc.turnBudgetReached(1)).toBe(false) acc.onStepFinish("msg_1", "stop") expect(acc.turnCount).toBe(1) + expect(acc.turnBudgetReached(1)).toBe(true) }) test("a step whose every attempt fails costs no turn and keeps its error", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) - // the first attempt plus every retry the processor allows, none completing - for (let attempt = 0; attempt <= 5; attempt++) acc.onStepStart("msg_1") + // Every attempt the processor allows failed before finishing. acc.onSessionError("APIError", "upstream stream reset") expect(acc.turnCount).toBe(0) expect(acc.turnBudgetReached(1)).toBe(false) @@ -60,10 +58,6 @@ describe("RunAccounting turn accounting", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) expect(acc.turnBudgetReached(1)).toBe(false) - acc.onStepStart("msg_1") - expect(acc.turnBudgetReached(1)).toBe(false) - acc.onStepStart("msg_1") // a retried attempt does not trip a one-turn budget - expect(acc.turnBudgetReached(1)).toBe(false) acc.onStepFinish("msg_1", "tool-calls") expect(acc.turnBudgetReached(1)).toBe(true) expect(acc.turnBudgetReached(2)).toBe(false) @@ -74,13 +68,13 @@ describe("RunAccounting turn accounting", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_work", agent: "build" }) acc.onAssistantMessage({ id: "msg_compact", agent: "compaction", summary: true }) - expect(acc.onStepStart("msg_work")).toBe(true) + expect(acc.isCompactionStep("msg_work")).toBe(false) acc.onStepFinish("msg_work", "tool-calls") - expect(acc.onStepStart("msg_compact")).toBe(false) + expect(acc.isCompactionStep("msg_compact")).toBe(true) acc.onStepFinish("msg_compact", "stop") - expect(acc.onStepStart("msg_compact")).toBe(false) + expect(acc.isCompactionStep("msg_compact")).toBe(true) acc.onStepFinish("msg_compact", "stop") - expect(acc.onStepStart("msg_work")).toBe(true) + expect(acc.isCompactionStep("msg_work")).toBe(false) acc.onStepFinish("msg_work", "stop") expect(acc.turnCount).toBe(2) }) @@ -88,14 +82,14 @@ describe("RunAccounting turn accounting", () => { test("an explicitly selected agent named compaction still consumes the turn budget", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_user_agent", agent: "compaction", summary: false }) - expect(acc.onStepStart("msg_user_agent")).toBe(true) + expect(acc.isCompactionStep("msg_user_agent")).toBe(false) acc.onStepFinish("msg_user_agent", "stop") expect(acc.turnCount).toBe(1) }) test("a step whose owning message is unknown is counted (conservative default)", () => { const acc = RunAccounting.create() - expect(acc.onStepStart("msg_unknown")).toBe(true) + expect(acc.isCompactionStep("msg_unknown")).toBe(false) acc.onStepFinish("msg_unknown", "stop") expect(acc.turnCount).toBe(1) }) @@ -123,7 +117,6 @@ describe("RunAccounting termination attribution (E4)", () => { test("natural finish: model=stop, harness=none", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "m1", agent: "build" }) - acc.onStepStart("m1") acc.onStepFinish("m1", "stop") expect(acc.termination()).toEqual({ why_model_stopped: "stop", why_harness_stopped: "none", done_reason: "none" }) expect(acc.fatal).toBe(false) @@ -132,7 +125,6 @@ describe("RunAccounting termination attribution (E4)", () => { test("model still tool-calling when harness exhausts the budget", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "m1", agent: "build" }) - acc.onStepStart("m1") acc.onStepFinish("m1", "tool-calls") acc.onBudgetExhausted() expect(acc.termination()).toEqual({ @@ -349,7 +341,6 @@ describe("RunAccounting done_reason + idle-done bookkeeping", () => { test("synthetic text appended after DONE does not clear explicit-DONE attribution", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "m1", agent: "plan" }) - acc.onStepStart("m1") acc.onText("m1", "Plan is complete.\nDONE") acc.onText("m1", "altimate-code: plan agent stopped without writing a plan", true) acc.onStepFinish("m1", "stop") @@ -419,17 +410,14 @@ describe("RunAccounting done_reason + idle-done bookkeeping", () => { // the heuristic's doing. const acc = RunAccounting.create() acc.onAssistantMessage({ id: "m1", agent: "build" }) - acc.onStepStart("m1") acc.onIdleDoneChallengeIssued() acc.onIdleDoneChallengeReplySent() acc.onText("m1", "Remaining: wire the config flag. Continuing.") acc.onStepFinish("m1", "stop") acc.onIdleDoneChallengeCompleted() acc.onAssistantMessage({ id: "m2", agent: "build" }) - acc.onStepStart("m2") acc.onStepFinish("m2", "tool-calls") acc.onAssistantMessage({ id: "m3", agent: "build" }) - acc.onStepStart("m3") acc.onText("m3", "All checks green.\nDONE") acc.onStepFinish("m3", "stop") const t = acc.termination() @@ -442,10 +430,8 @@ describe("RunAccounting done_reason + idle-done bookkeeping", () => { acc.onIdleDoneChallengeIssued() acc.onIdleDoneChallengeReplySent() acc.onAssistantMessage({ id: "m1", agent: "build" }) - acc.onStepStart("m1") acc.onStepFinish("m1", "tool-calls") acc.onAssistantMessage({ id: "m2", agent: "build" }) - acc.onStepStart("m2") acc.onText("m2", "Verified.\nDONE") acc.onStepFinish("m2", "stop") acc.onIdleDoneChallengeCompleted() @@ -464,11 +450,9 @@ describe("RunAccounting done_reason + idle-done bookkeeping", () => { test("stale DONE from a tool-calls message is not paired with a later textless stop", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "m1", agent: "build" }) - acc.onStepStart("m1") acc.onText("m1", "Wrapping up.\nDONE") acc.onStepFinish("m1", "tool-calls") acc.onAssistantMessage({ id: "m2", agent: "build" }) - acc.onStepStart("m2") acc.onStepFinish("m2", "stop") // no onText for m2 — no text part at all const t = acc.termination() expect(t.done_reason).toBe("none") diff --git a/packages/opencode/test/cli/run/budget-finalization.test.ts b/packages/opencode/test/cli/run/budget-finalization.test.ts index 6f315726fe..eac6e3983d 100644 --- a/packages/opencode/test/cli/run/budget-finalization.test.ts +++ b/packages/opencode/test/cli/run/budget-finalization.test.ts @@ -11,10 +11,8 @@ import { testProviderConfig } from "../../lib/test-provider" function exhausted() { const accounting = RunAccounting.create() - accounting.onStepStart("work") accounting.onStepFinish("work", "tool-calls") // the over-budget step is aborted at its start, so it never completes - accounting.onStepStart("interrupted") accounting.onBudgetExhausted() return accounting } @@ -41,13 +39,11 @@ describe("budget finalization accounting", () => { // altimate_change start — distinguish recovered wall-clock limits from turn limits and real failures test("a successful time-budget finalization clears fatal status but keeps budget attribution", () => { const accounting = RunAccounting.create() - accounting.onStepStart("interrupted") accounting.onBudgetExhausted("time") accounting.onSessionError("MessageAbortedError", "intentional time-budget abort") accounting.onPromptResult({ error: { name: "MessageAbortedError" } }) expect(accounting.fatal).toBe(true) expect(accounting.takeBudgetFinalizationAllowance()).toBe(1) - accounting.onStepStart("finalization") accounting.onText("finalization", "The evidence supports a limited answer.") accounting.onStepFinish("finalization", "stop") accounting.onPromptResult({ finish: "stop" }) @@ -117,7 +113,6 @@ describe("budget finalization accounting", () => { expect(accounting.turnCount).toBe(1) expect(accounting.fatal).toBe(true) - accounting.onStepStart("finalization") expect(accounting.turnBudgetReached(2)).toBe(false) accounting.onText("finalization", "The supported result is available; verification is incomplete.") accounting.onStepFinish("finalization", "stop") @@ -132,7 +127,6 @@ describe("budget finalization accounting", () => { test("does not grant an allowance before a budget abort", () => { const accounting = RunAccounting.create() - accounting.onStepStart("work") expect(accounting.takeBudgetFinalizationAllowance()).toBeUndefined() expect(accounting.fatal).toBe(false) }) @@ -150,7 +144,6 @@ describe("budget finalization accounting", () => { for (const order of ["before", "after"] as const) { test(`${name} ${order} budget exhaustion prevents a finalization despite reason precedence`, () => { const accounting = RunAccounting.create() - accounting.onStepStart("work") if (order === "before") accounting.onSessionError(name, "genuine failure") accounting.onBudgetExhausted() if (order === "after") accounting.onSessionError(name, "genuine failure") @@ -194,9 +187,7 @@ describe("budget finalization accounting", () => { accounting.onPromptResult({ finish: "other" }) accounting.onIdleDoneChallengeReplySent() accounting.onIdleDoneChallengeCompleted() - accounting.onStepStart("continuation") accounting.onStepFinish("continuation", "tool-calls") - accounting.onStepStart("over-budget") accounting.onBudgetExhausted() accounting.onSessionError("MessageAbortedError", "budget interruption") accounting.onPromptResult({ error: { name: "MessageAbortedError" } }) @@ -576,7 +567,6 @@ describe("opencode run: budget finalization subprocess", () => { const committedRequests = new Set() ownership.beginRequest("original") ownership.onAssistantMessage({ id: "commit", parentID: "original" }) - accounting.onStepStart("commit") accounting.onStepFinish("commit", "tool-calls") committedRequests.add(ownership.ownerOf("commit")!) const queue = () => { @@ -588,7 +578,7 @@ describe("opencode run: budget finalization subprocess", () => { ownership.onUserMessage("marker", "original") ownership.onAssistantMessage({ id: "summary", parentID: "marker" }) accounting.onAssistantMessage({ id: "summary", summary: true }) - expect(accounting.onStepStart("summary")).toBe(false) + expect(accounting.isCompactionStep("summary")).toBe(true) accounting.onStepFinish("summary", "stop") if (queued === "during the summary") queue() @@ -600,7 +590,7 @@ describe("opencode run: budget finalization subprocess", () => { ownership.onUserMessage("second-continuation", "second-marker") ownership.onAssistantMessage({ id: "echo", parentID: "second-continuation" }) expect(ownership.ownerOf("echo")).toBe("original") - expect(accounting.onStepStart("echo")).toBe(true) + expect(accounting.isCompactionStep("echo")).toBe(false) accounting.onStepFinish("echo", "stop") expect(accounting.turnCount).toBe(2) expect(committedRequests.has(ownership.ownerOf("echo")!)).toBe(true) From 60ee54426230aed2fb25094ea67ee0e7e473e042 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 08:08:48 -0800 Subject: [PATCH 05/20] test: drop the wall-clock assertion from the time-budget bound test The budget_exhausted event and the harness timeout already prove the run ends. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/test/cli/run/turn-budget-retry.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index 4a3fc9786b..7ad0398bf7 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -230,7 +230,6 @@ describe("opencode run: --max-turns charges completed steps only", () => { ]) expect(counts.aborts).toBeGreaterThanOrEqual(1) expect(counts.attempts).toBeGreaterThan(SessionRetry.RETRY_MAX_ATTEMPTS + 1) - expect(result.durationMs).toBeLessThan(20_000) expect( events.filter((event) => event.type === "text").map((event) => (event.part as { text: string }).text), ).toEqual(["Best-effort answer."]) From a1b6100faa08db8635b85a9d9543877d03c0953c Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 08:12:28 -0800 Subject: [PATCH 06/20] test: a post-budget compaction step is never charged or stopped; name every free non-completing step Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/cli/cmd/run-accounting.ts | 4 ++ .../test/cli/run/turn-budget-retry.test.ts | 62 ++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index 2ff41aff62..62486bf5b4 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -173,6 +173,10 @@ export function create() { // (up to SessionRetry.RETRY_MAX_ATTEMPTS per step), and each retry streams // its own step-start but only the attempt that completes streams a // step-finish. Charging on start billed every failed attempt as a turn. + // Any step that ends without a step-finish is free: a failed or retried + // attempt, a context overflow that hands off to compaction, or a + // soft-deadline interrupt. The processor's retry cap, the compaction + // circuit breaker and --time-budget bound those paths. // `turnCount` is therefore the number of COMPLETED non-compaction steps; // harness-injected generations (idle-done challenge, continuation, // silent-turn reply) are real assistant steps and are charged the same way. diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index 7ad0398bf7..ff4336fda8 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -50,7 +50,7 @@ describe("opencode run: --max-turns charges completed steps only", () => { ) // Attached-session fake: the CLI sees exactly the events the session publishes for // a step whose attempts start and fail, without waiting out real retry backoff. - const attached = (behavior: "all-attempts-fail" | "endless-attempts") => + const attached = (behavior: "all-attempts-fail" | "endless-attempts" | "compaction-after-budget") => Effect.gen(function* () { const sessionID = "ses_turn_budget_retry" let stream: ReadableStreamDefaultController | undefined @@ -144,6 +144,46 @@ describe("opencode run: --max-turns charges completed steps only", () => { publish("session.status", { sessionID, status: { type: "idle" } }) return Response.json({ info: { finish: "stop" } }) } + if (behavior === "compaction-after-budget") { + // The work step answers and completes, using the whole one-turn budget. + startAttempt(messageID) + publish("message.part.updated", { + part: { + id: `${messageID}_text`, + messageID, + sessionID, + type: "text", + text: "The answer.", + time: { start: 1, end: 2 }, + }, + }) + publish("message.part.updated", { + part: { id: `${messageID}_finish`, messageID, sessionID, type: "step-finish", reason: "stop" }, + }) + // Compaction machinery then runs a step of its own; it is never charged or stopped. + const summaryID = `${messageID}zz_summary` + publish("message.updated", { + info: { + id: summaryID, + sessionID, + role: "assistant", + parentID, + agent: "compaction", + summary: true, + modelID: "test-model", + providerID: "test", + time: { created: Date.now() }, + }, + }) + publish("message.part.updated", { + part: { id: `${summaryID}_start`, messageID: summaryID, sessionID, type: "step-start" }, + }) + publish("message.part.updated", { + part: { id: `${summaryID}_finish`, messageID: summaryID, sessionID, type: "step-finish", reason: "stop" }, + }) + publish("session.status", { sessionID, status: { type: "idle" } }) + return Response.json({ info: { finish: "stop" } }) + } if (behavior === "all-attempts-fail") { // The first attempt plus every retry the processor allows, none completing. for (let attempt = 0; attempt <= SessionRetry.RETRY_MAX_ATTEMPTS; attempt++) startAttempt(messageID) @@ -202,6 +242,26 @@ describe("opencode run: --max-turns charges completed steps only", () => { 30_000, ) + cliIt.concurrent( + "a compaction step after the budget is used is neither charged nor stopped", + ({ opencode }) => + Effect.gen(function* () { + const { server, sessionID, counts } = yield* attached("compaction-after-budget") + const result = yield* opencode.run("Answer once.", { + format: "json", + extraArgs: ["--attach", server.url.toString(), "--session", sessionID, "--max-turns", "1", "--no-trace"], + timeoutMs: 20_000, + bunRun: true, + }) + const events = opencode.parseJsonEvents(result.stdout) + expect(events.filter((event) => event.type === "budget_exhausted")).toHaveLength(0) + expect(counts.aborts).toBe(0) + expect(counts.prompts).toBe(1) + opencode.expectExit(result, 0) + }), + 30_000, + ) + cliIt.concurrent( "attempts that keep restarting without completing are still bounded by --time-budget", ({ opencode }) => From e70fc3e988deb06401275ce34e828ca1f5d3f3d1 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 08:17:21 -0800 Subject: [PATCH 07/20] docs: name --time-budget as the bound for repeated overflow-and-compact cycles; rename a unit test to what it proves Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/cli/cmd/run-accounting.ts | 5 +++-- packages/opencode/test/cli/run-accounting.test.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index 62486bf5b4..98f34232fc 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -175,8 +175,9 @@ export function create() { // step-finish. Charging on start billed every failed attempt as a turn. // Any step that ends without a step-finish is free: a failed or retried // attempt, a context overflow that hands off to compaction, or a - // soft-deadline interrupt. The processor's retry cap, the compaction - // circuit breaker and --time-budget bound those paths. + // soft-deadline interrupt. The processor's retry cap bounds retried + // attempts; repeated overflow-and-compact cycles are bounded only by + // --time-budget (the compaction breaker resets on each success). // `turnCount` is therefore the number of COMPLETED non-compaction steps; // harness-injected generations (idle-done challenge, continuation, // silent-turn reply) are real assistant steps and are charged the same way. diff --git a/packages/opencode/test/cli/run-accounting.test.ts b/packages/opencode/test/cli/run-accounting.test.ts index aed593eb29..115a3a9ee4 100644 --- a/packages/opencode/test/cli/run-accounting.test.ts +++ b/packages/opencode/test/cli/run-accounting.test.ts @@ -43,7 +43,7 @@ describe("RunAccounting turn accounting", () => { expect(acc.turnBudgetReached(1)).toBe(true) }) - test("a step whose every attempt fails costs no turn and keeps its error", () => { + test("a session error with no completed step charges no turn and stays fatal", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) // Every attempt the processor allows failed before finishing. From dd4841718537a8e3d9ccf733a7ef1220784267df Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 08:24:14 -0800 Subject: [PATCH 08/20] fix: `--max-turns` also stops runs whose step attempts never finish A step that never completes is free, so repeated overflow-and-compact cycles could run forever without --time-budget. The budget now also stops once N x (1 + retry cap) attempts started; the finalization step keeps a full step of attempts. The message says which bound fired. Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/cli/cmd/run-accounting.ts | 46 ++++++++++++++++--- packages/opencode/src/cli/cmd/run.ts | 13 ++++-- .../opencode/test/cli/run-accounting.test.ts | 30 ++++++++++++ .../test/cli/run/turn-budget-retry.test.ts | 28 +++++++++-- 4 files changed, 102 insertions(+), 15 deletions(-) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index 98f34232fc..dee9d5aefa 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -17,6 +17,9 @@ // idle-done challenge bookkeeping; DONE detection delegates to the // SessionTermination completion-token contract. import { SessionTermination } from "../../session/termination" +// altimate_change start — the retry cap sizes the attempt backstop +import { SessionRetry } from "../../session/retry" +// altimate_change end export type WhyModelStopped = "stop" | "tool-call" | "explicit-done" | "unknown" export type WhyHarnessStopped = "budget-exhausted" | "timeout" | "error" | "idle-done" | "none" @@ -91,9 +94,25 @@ export function createRecoverableOverflowTraceErrors() { // the single detector shared with the processor stop-path and the idle-done // challenge, so instruction and detection can never drift apart. +// altimate_change start — the turn budget's backstop for steps that never finish +/** The most attempts one step can start: the first try plus every processor retry. */ +export const ATTEMPTS_PER_TURN = SessionRetry.RETRY_MAX_ATTEMPTS + 1 +export type TurnBudgetStop = "completed" | "attempts" + +/** The user-facing reason a `--max-turns` budget stopped the run. */ +export function turnBudgetMessage(maxTurns: number, stop: TurnBudgetStop): string { + const limit = `Budget exceeded: reached ${maxTurns} assistant turn${maxTurns !== 1 ? "s" : ""} limit` + if (stop === "completed") return limit + return `${limit} (${maxTurns * ATTEMPTS_PER_TURN} step attempts started without enough of them completing)` +} +// altimate_change end + export function create() { const summaryMessages = new Set() let turnCount = 0 + // altimate_change start — started non-compaction step attempts, finished or not + let attemptCount = 0 + // altimate_change end // altimate_change start — track the active request's committed echo turn allowance let committedAnswer = false // altimate_change end @@ -173,21 +192,31 @@ export function create() { // (up to SessionRetry.RETRY_MAX_ATTEMPTS per step), and each retry streams // its own step-start but only the attempt that completes streams a // step-finish. Charging on start billed every failed attempt as a turn. - // Any step that ends without a step-finish is free: a failed or retried - // attempt, a context overflow that hands off to compaction, or a - // soft-deadline interrupt. The processor's retry cap bounds retried - // attempts; repeated overflow-and-compact cycles are bounded only by - // --time-budget (the compaction breaker resets on each success). // `turnCount` is therefore the number of COMPLETED non-compaction steps; // harness-injected generations (idle-done challenge, continuation, // silent-turn reply) are real assistant steps and are charged the same way. + // Any step that ends without a step-finish is free: a failed or retried + // attempt, a context overflow that hands off to compaction, or a + // soft-deadline interrupt. So that such steps cannot repeat forever, the + // budget also stops a run once `maxTurns * ATTEMPTS_PER_TURN` attempts + // started, the most N steps can use when every one exhausts its retries. + /** Record a started non-compaction step attempt (it is charged a turn only if it finishes). */ + onStepAttempt(messageID: string) { + if (!isCompactionStep(messageID)) attemptCount++ + }, /** Completed non-compaction assistant steps. */ get turnCount() { return turnCount }, - /** True when `maxTurns` completed steps already ran, so no further step may start. */ + /** Why no further step may start under `maxTurns`, or undefined while one may. */ + turnBudgetStop(maxTurns: number | undefined): TurnBudgetStop | undefined { + if (maxTurns === undefined || maxTurns <= 0) return undefined + if (turnCount >= maxTurns) return "completed" + if (attemptCount >= maxTurns * ATTEMPTS_PER_TURN) return "attempts" + return undefined + }, turnBudgetReached(maxTurns: number | undefined): boolean { - return maxTurns !== undefined && maxTurns > 0 && turnCount >= maxTurns + return this.turnBudgetStop(maxTurns) !== undefined }, onStepFinish(messageID: string, reason: string | undefined) { if (isCompactionStep(messageID)) return @@ -280,6 +309,9 @@ export function create() { takeBudgetFinalizationAllowance(): number | undefined { if (!budgetExhausted || fatalError || pendingContextOverflow || budgetFinalizationSent) return undefined budgetFinalizationSent = true + // altimate_change start — the finalization step also gets one step's worth of attempts + attemptCount = Math.min(attemptCount, turnCount * ATTEMPTS_PER_TURN) + // altimate_change end return turnCount + 1 }, // altimate_change end diff --git a/packages/opencode/src/cli/cmd/run.ts b/packages/opencode/src/cli/cmd/run.ts index 2e36393333..a8f583f104 100644 --- a/packages/opencode/src/cli/cmd/run.ts +++ b/packages/opencode/src/cli/cmd/run.ts @@ -1157,16 +1157,17 @@ You are speaking to a non-technical business executive. Follow these rules stric // than maxTurns steps have completed, so a retried attempt is free. const requestID = requestOwnership.ownerOf(part.messageID) const committedAnswer = requestID !== undefined && committedRequests.has(requestID) + const budgetStop = compactionStep ? undefined : accounting.turnBudgetStop(maxTurns) // altimate_change start — the time-budget abort already owns the interrupted work phase if ( - !compactionStep && accounting.turnBudgetReached(maxTurns) && !committedAnswer && !budgetAborted && + maxTurns && budgetStop && !committedAnswer && !budgetAborted && (!timeExhausted() || options?.turnLimit !== undefined) ) { stopTimeBudget() // altimate_change end budgetAborted = true accounting.onBudgetExhausted() - error = `Budget exceeded: reached ${maxTurns} assistant turn${maxTurns !== 1 ? "s" : ""} limit` + error = RunAccounting.turnBudgetMessage(maxTurns, budgetStop) if (!emit("budget_exhausted", { limit: maxTurns })) { UI.println(UI.Style.TEXT_DANGER_BOLD + "!", UI.Style.TEXT_NORMAL + ` ${error}. Aborting session.`) } @@ -1174,6 +1175,7 @@ You are speaking to a non-technical business executive. Follow these rules stric // Drain the ordered abort/error/idle tail before starting another phase. continue } + accounting.onStepAttempt(part.messageID) // altimate_change end if (emit("step_start", { part })) continue } @@ -2054,10 +2056,11 @@ You are speaking to a non-technical business executive. Follow these rules stric // A silent stop exactly at the work limit has no in-flight generation to // abort. Enter finalization directly instead of spending a reply just to // discover that it exceeds the same budget. - // The same completed-step count the in-loop budget check uses. - if (!answered() && assistantStarted && !accounting.fatal && accounting.turnBudgetReached(args.maxTurns)) { + // The same budget check the event loop uses. + const finalBudgetStop = accounting.turnBudgetStop(args.maxTurns) + if (!answered() && assistantStarted && !accounting.fatal && args.maxTurns && finalBudgetStop) { accounting.onBudgetExhausted() - error = `Budget exceeded: reached ${args.maxTurns} assistant turn${args.maxTurns !== 1 ? "s" : ""} limit` + error = RunAccounting.turnBudgetMessage(args.maxTurns, finalBudgetStop) if (!emit("budget_exhausted", { limit: args.maxTurns })) { UI.println(UI.Style.TEXT_WARNING_BOLD + "!", UI.Style.TEXT_NORMAL + ` ${error}. Requesting a final reply.`) } diff --git a/packages/opencode/test/cli/run-accounting.test.ts b/packages/opencode/test/cli/run-accounting.test.ts index 115a3a9ee4..eb81d2c1f4 100644 --- a/packages/opencode/test/cli/run-accounting.test.ts +++ b/packages/opencode/test/cli/run-accounting.test.ts @@ -64,6 +64,36 @@ describe("RunAccounting turn accounting", () => { expect(acc.turnBudgetReached(undefined)).toBe(false) }) + test("attempts that never finish stop the budget once they exceed what N steps' retries allow", () => { + const acc = RunAccounting.create() + acc.onAssistantMessage({ id: "msg_1", agent: "build" }) + acc.onAssistantMessage({ id: "msg_compact", agent: "compaction", summary: true }) + // Compaction attempts never count toward the backstop. + for (let i = 0; i < 20; i++) acc.onStepAttempt("msg_compact") + for (let i = 0; i < RunAccounting.ATTEMPTS_PER_TURN - 1; i++) acc.onStepAttempt("msg_1") + expect(acc.turnBudgetStop(1)).toBeUndefined() + acc.onStepAttempt("msg_1") + expect(acc.turnCount).toBe(0) + expect(acc.turnBudgetStop(1)).toBe("attempts") + expect(acc.turnBudgetStop(2)).toBeUndefined() + expect(acc.turnBudgetStop(undefined)).toBeUndefined() + // A budget abort's one finalization step still gets a full step's attempts. + acc.onBudgetExhausted() + expect(acc.takeBudgetFinalizationAllowance()).toBe(1) + expect(acc.turnBudgetStop(1)).toBeUndefined() + // Completed steps take precedence in the reported reason. + acc.onStepFinish("msg_1", "stop") + expect(acc.turnBudgetStop(1)).toBe("completed") + }) + + test("the budget message names the limit, and the attempt backstop says why", () => { + expect(RunAccounting.turnBudgetMessage(3, "completed")).toBe("Budget exceeded: reached 3 assistant turns limit") + expect(RunAccounting.turnBudgetMessage(1, "completed")).toBe("Budget exceeded: reached 1 assistant turn limit") + expect(RunAccounting.turnBudgetMessage(1, "attempts")).toBe( + `Budget exceeded: reached 1 assistant turn limit (${RunAccounting.ATTEMPTS_PER_TURN} step attempts started without enough of them completing)`, + ) + }) + test("excludes compaction-machinery steps from turnCount", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_work", agent: "build" }) diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index ff4336fda8..844d419406 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -262,6 +262,30 @@ describe("opencode run: --max-turns charges completed steps only", () => { 30_000, ) + cliIt.concurrent( + "attempts that keep restarting without completing are stopped by --max-turns alone", + ({ opencode }) => + Effect.gen(function* () { + const { server, sessionID, counts } = yield* attached("endless-attempts") + const result = yield* opencode.run("Answer once.", { + format: "json", + extraArgs: ["--attach", server.url.toString(), "--session", sessionID, "--max-turns", "1", "--no-trace"], + timeoutMs: 20_000, + bunRun: true, + }) + opencode.expectExit(result, 1) + const events = opencode.parseJsonEvents(result.stdout) + const budgetIndex = events.findIndex((event) => event.type === "budget_exhausted") + expect(events[budgetIndex]).toMatchObject({ limit: 1 }) + // Exactly one step's worth of attempts ran before the backstop stopped the next. + expect(events.slice(0, budgetIndex).filter((event) => event.type === "step_start")).toHaveLength( + SessionRetry.RETRY_MAX_ATTEMPTS + 1, + ) + expect(counts.aborts).toBe(1) + }), + 30_000, + ) + cliIt.concurrent( "attempts that keep restarting without completing are still bounded by --time-budget", ({ opencode }) => @@ -274,8 +298,6 @@ describe("opencode run: --max-turns charges completed steps only", () => { server.url.toString(), "--session", sessionID, - "--max-turns", - "1", "--time-budget", "5", "--no-trace", @@ -284,7 +306,7 @@ describe("opencode run: --max-turns charges completed steps only", () => { bunRun: true, }) const events = opencode.parseJsonEvents(result.stdout) - // The turn budget never fires (no attempt completed); the wall-clock budget ends the work. + // Without a turn budget, the wall-clock budget ends the work. expect(events.filter((event) => event.type === "budget_exhausted")).toEqual([ expect.objectContaining({ limit_seconds: 5 }), ]) From c46884167c47f176fa9f26f22f4e5df8db656ba6 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 08:29:29 -0800 Subject: [PATCH 09/20] fix: the attempt-backstop message no longer claims the turn limit was reached; unit tests record attempts Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/cli/cmd/run-accounting.ts | 6 +++--- packages/opencode/test/cli/run-accounting.test.ts | 11 +++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index dee9d5aefa..e1f174870c 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -101,9 +101,9 @@ export type TurnBudgetStop = "completed" | "attempts" /** The user-facing reason a `--max-turns` budget stopped the run. */ export function turnBudgetMessage(maxTurns: number, stop: TurnBudgetStop): string { - const limit = `Budget exceeded: reached ${maxTurns} assistant turn${maxTurns !== 1 ? "s" : ""} limit` - if (stop === "completed") return limit - return `${limit} (${maxTurns * ATTEMPTS_PER_TURN} step attempts started without enough of them completing)` + const turns = `${maxTurns} assistant turn${maxTurns !== 1 ? "s" : ""}` + if (stop === "completed") return `Budget exceeded: reached ${turns} limit` + return `Budget exceeded: ${maxTurns * ATTEMPTS_PER_TURN} step attempts started under the ${turns} limit without enough of them completing` } // altimate_change end diff --git a/packages/opencode/test/cli/run-accounting.test.ts b/packages/opencode/test/cli/run-accounting.test.ts index eb81d2c1f4..54815b56d6 100644 --- a/packages/opencode/test/cli/run-accounting.test.ts +++ b/packages/opencode/test/cli/run-accounting.test.ts @@ -36,8 +36,11 @@ describe("RunAccounting turn accounting", () => { // Attempt 1 started and its stream broke, so it wrote no step-finish. The // retry's start is checked against completed steps only, so a one-turn // budget still lets it run. + acc.onStepAttempt("msg_1") expect(acc.turnCount).toBe(0) expect(acc.turnBudgetReached(1)).toBe(false) + acc.onStepAttempt("msg_1") + expect(acc.turnBudgetReached(1)).toBe(false) acc.onStepFinish("msg_1", "stop") expect(acc.turnCount).toBe(1) expect(acc.turnBudgetReached(1)).toBe(true) @@ -47,9 +50,13 @@ describe("RunAccounting turn accounting", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) // Every attempt the processor allows failed before finishing. + for (let i = 0; i < RunAccounting.ATTEMPTS_PER_TURN; i++) acc.onStepAttempt("msg_1") acc.onSessionError("APIError", "upstream stream reset") expect(acc.turnCount).toBe(0) - expect(acc.turnBudgetReached(1)).toBe(false) + // No turn was charged. The processor makes no seventh attempt; if anything + // did start another step, the attempt backstop would stop it. + expect(acc.turnBudgetStop(1)).toBe("attempts") + expect(acc.turnBudgetStop(2)).toBeUndefined() expect(acc.fatal).toBe(true) expect(acc.termination().why_harness_stopped).toBe("error") }) @@ -90,7 +97,7 @@ describe("RunAccounting turn accounting", () => { expect(RunAccounting.turnBudgetMessage(3, "completed")).toBe("Budget exceeded: reached 3 assistant turns limit") expect(RunAccounting.turnBudgetMessage(1, "completed")).toBe("Budget exceeded: reached 1 assistant turn limit") expect(RunAccounting.turnBudgetMessage(1, "attempts")).toBe( - `Budget exceeded: reached 1 assistant turn limit (${RunAccounting.ATTEMPTS_PER_TURN} step attempts started without enough of them completing)`, + `Budget exceeded: ${RunAccounting.ATTEMPTS_PER_TURN} step attempts started under the 1 assistant turn limit without enough of them completing`, ) }) From 47c6c8afa9ff41d1e36bc7e595c43b976b973d13 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 08:36:39 -0800 Subject: [PATCH 10/20] fix: a step cut short by context overflow still costs its turn Keeps overflow-and-compact cycles bounded by --max-turns as before; the attempt backstop remains for other unfinished steps. Drops the test-only turnBudgetReached helper and a redundant nested marker. Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/cli/cmd/run-accounting.ts | 32 ++++++++++++------- .../opencode/test/cli/run-accounting.test.ts | 28 ++++++++++++---- .../test/cli/run/budget-finalization.test.ts | 4 +-- .../test/cli/run/turn-budget-retry.test.ts | 3 +- 4 files changed, 46 insertions(+), 21 deletions(-) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index e1f174870c..e25611a017 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -112,6 +112,8 @@ export function create() { let turnCount = 0 // altimate_change start — started non-compaction step attempts, finished or not let attemptCount = 0 + // A started non-compaction step that has not finished yet. + let openAttempt = false // altimate_change end // altimate_change start — track the active request's committed echo turn allowance let committedAnswer = false @@ -195,14 +197,17 @@ export function create() { // `turnCount` is therefore the number of COMPLETED non-compaction steps; // harness-injected generations (idle-done challenge, continuation, // silent-turn reply) are real assistant steps and are charged the same way. - // Any step that ends without a step-finish is free: a failed or retried - // attempt, a context overflow that hands off to compaction, or a - // soft-deadline interrupt. So that such steps cannot repeat forever, the - // budget also stops a run once `maxTurns * ATTEMPTS_PER_TURN` attempts - // started, the most N steps can use when every one exhausts its retries. + // A step that ends without a step-finish is free (a failed or retried + // attempt, a soft-deadline interrupt), except one cut short by context + // overflow, which is still charged (see onSessionError). So that free + // steps cannot repeat forever, the budget also stops a run once + // `maxTurns * ATTEMPTS_PER_TURN` attempts started, the most N steps can + // use when every one exhausts its retries. /** Record a started non-compaction step attempt (it is charged a turn only if it finishes). */ onStepAttempt(messageID: string) { - if (!isCompactionStep(messageID)) attemptCount++ + if (isCompactionStep(messageID)) return + attemptCount++ + openAttempt = true }, /** Completed non-compaction assistant steps. */ get turnCount() { @@ -215,12 +220,10 @@ export function create() { if (attemptCount >= maxTurns * ATTEMPTS_PER_TURN) return "attempts" return undefined }, - turnBudgetReached(maxTurns: number | undefined): boolean { - return this.turnBudgetStop(maxTurns) !== undefined - }, onStepFinish(messageID: string, reason: string | undefined) { if (isCompactionStep(messageID)) return turnCount++ + openAttempt = false lastFinishReason = reason lastFinishMessageID = messageID }, @@ -266,6 +269,14 @@ export function create() { } if (errorName === "ContextOverflowError") { pendingContextOverflow = true + // altimate_change start — a step cut short by context overflow still costs its turn + // The overflow hands off to compaction and a fresh step, so charging it keeps + // repeated overflow-and-compact cycles bounded by --max-turns, as before. + if (openAttempt) { + turnCount++ + openAttempt = false + } + // altimate_change end return } // the idle-done challenge is delivered by aborting the in-flight @@ -309,9 +320,8 @@ export function create() { takeBudgetFinalizationAllowance(): number | undefined { if (!budgetExhausted || fatalError || pendingContextOverflow || budgetFinalizationSent) return undefined budgetFinalizationSent = true - // altimate_change start — the finalization step also gets one step's worth of attempts + // The finalization step also gets one step's worth of attempts. attemptCount = Math.min(attemptCount, turnCount * ATTEMPTS_PER_TURN) - // altimate_change end return turnCount + 1 }, // altimate_change end diff --git a/packages/opencode/test/cli/run-accounting.test.ts b/packages/opencode/test/cli/run-accounting.test.ts index 54815b56d6..98cc9996e9 100644 --- a/packages/opencode/test/cli/run-accounting.test.ts +++ b/packages/opencode/test/cli/run-accounting.test.ts @@ -38,12 +38,12 @@ describe("RunAccounting turn accounting", () => { // budget still lets it run. acc.onStepAttempt("msg_1") expect(acc.turnCount).toBe(0) - expect(acc.turnBudgetReached(1)).toBe(false) + expect(acc.turnBudgetStop(1)).toBeUndefined() acc.onStepAttempt("msg_1") - expect(acc.turnBudgetReached(1)).toBe(false) + expect(acc.turnBudgetStop(1)).toBeUndefined() acc.onStepFinish("msg_1", "stop") expect(acc.turnCount).toBe(1) - expect(acc.turnBudgetReached(1)).toBe(true) + expect(acc.turnBudgetStop(1)).toBeDefined() }) test("a session error with no completed step charges no turn and stays fatal", () => { @@ -64,11 +64,11 @@ describe("RunAccounting turn accounting", () => { test("the budget is reached only after maxTurns steps complete", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) - expect(acc.turnBudgetReached(1)).toBe(false) + expect(acc.turnBudgetStop(1)).toBeUndefined() acc.onStepFinish("msg_1", "tool-calls") - expect(acc.turnBudgetReached(1)).toBe(true) - expect(acc.turnBudgetReached(2)).toBe(false) - expect(acc.turnBudgetReached(undefined)).toBe(false) + expect(acc.turnBudgetStop(1)).toBeDefined() + expect(acc.turnBudgetStop(2)).toBeUndefined() + expect(acc.turnBudgetStop(undefined)).toBeUndefined() }) test("attempts that never finish stop the budget once they exceed what N steps' retries allow", () => { @@ -101,6 +101,20 @@ describe("RunAccounting turn accounting", () => { ) }) + test("a step cut short by context overflow still costs its turn, once", () => { + const acc = RunAccounting.create() + acc.onAssistantMessage({ id: "msg_1", agent: "build" }) + // An overflow reported before any step started is not charged. + acc.onSessionError("ContextOverflowError", "too long") + expect(acc.turnCount).toBe(0) + acc.onStepAttempt("msg_1") + acc.onSessionError("ContextOverflowError", "too long") + // The same overflow reported again on the prompt channel is not charged twice. + acc.onPromptResult({ error: { name: "ContextOverflowError" } }) + expect(acc.turnCount).toBe(1) + expect(acc.turnBudgetStop(1)).toBe("completed") + }) + test("excludes compaction-machinery steps from turnCount", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_work", agent: "build" }) diff --git a/packages/opencode/test/cli/run/budget-finalization.test.ts b/packages/opencode/test/cli/run/budget-finalization.test.ts index eac6e3983d..8db9ac1f79 100644 --- a/packages/opencode/test/cli/run/budget-finalization.test.ts +++ b/packages/opencode/test/cli/run/budget-finalization.test.ts @@ -113,11 +113,11 @@ describe("budget finalization accounting", () => { expect(accounting.turnCount).toBe(1) expect(accounting.fatal).toBe(true) - expect(accounting.turnBudgetReached(2)).toBe(false) + expect(accounting.turnBudgetStop(2)).toBeUndefined() accounting.onText("finalization", "The supported result is available; verification is incomplete.") accounting.onStepFinish("finalization", "stop") expect(accounting.turnCount).toBe(2) - expect(accounting.turnBudgetReached(2)).toBe(true) + expect(accounting.turnBudgetStop(2)).toBeDefined() expect(accounting.termination()).toEqual({ why_model_stopped: "stop", why_harness_stopped: "budget-exhausted", diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index 844d419406..7d10d1e6b5 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -311,7 +311,8 @@ describe("opencode run: --max-turns charges completed steps only", () => { expect.objectContaining({ limit_seconds: 5 }), ]) expect(counts.aborts).toBeGreaterThanOrEqual(1) - expect(counts.attempts).toBeGreaterThan(SessionRetry.RETRY_MAX_ATTEMPTS + 1) + // More than the initial burst: attempts kept coming until the deadline stopped them. + expect(counts.attempts).toBeGreaterThan(SessionRetry.RETRY_MAX_ATTEMPTS + 2) expect( events.filter((event) => event.type === "text").map((event) => (event.part as { text: string }).text), ).toEqual(["Best-effort answer."]) From d1f452422be3acb418b4e4499850488595510c87 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 08:43:17 -0800 Subject: [PATCH 11/20] fix: overflows rejected before a step starts count toward the attempt backstop Also: a compaction step closes an unfinished attempt, finalization gets exactly one step of attempts, and budget_exhausted carries a reason. Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/cli/cmd/run-accounting.ts | 14 +++- packages/opencode/src/cli/cmd/run.ts | 22 ++++++- .../opencode/test/cli/run-accounting.test.ts | 23 ++++++- .../test/cli/run/turn-budget-retry.test.ts | 66 ++++++++++++++++++- 4 files changed, 117 insertions(+), 8 deletions(-) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index e25611a017..e0cabafc53 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -205,7 +205,11 @@ export function create() { // use when every one exhausts its retries. /** Record a started non-compaction step attempt (it is charged a turn only if it finishes). */ onStepAttempt(messageID: string) { - if (isCompactionStep(messageID)) return + // A compaction step supersedes any unfinished step; its overflow is not that step's. + if (isCompactionStep(messageID)) { + openAttempt = false + return + } attemptCount++ openAttempt = true }, @@ -275,6 +279,10 @@ export function create() { if (openAttempt) { turnCount++ openAttempt = false + } else if (source === "event") { + // Rejected before any step started: no step-start will ever check the + // budget, so the attempt is counted here and the caller enforces it. + attemptCount++ } // altimate_change end return @@ -320,8 +328,8 @@ export function create() { takeBudgetFinalizationAllowance(): number | undefined { if (!budgetExhausted || fatalError || pendingContextOverflow || budgetFinalizationSent) return undefined budgetFinalizationSent = true - // The finalization step also gets one step's worth of attempts. - attemptCount = Math.min(attemptCount, turnCount * ATTEMPTS_PER_TURN) + // The finalization step gets exactly one step's worth of attempts. + attemptCount = turnCount * ATTEMPTS_PER_TURN return turnCount + 1 }, // altimate_change end diff --git a/packages/opencode/src/cli/cmd/run.ts b/packages/opencode/src/cli/cmd/run.ts index a8f583f104..28b220f108 100644 --- a/packages/opencode/src/cli/cmd/run.ts +++ b/packages/opencode/src/cli/cmd/run.ts @@ -1168,7 +1168,7 @@ You are speaking to a non-technical business executive. Follow these rules stric budgetAborted = true accounting.onBudgetExhausted() error = RunAccounting.turnBudgetMessage(maxTurns, budgetStop) - if (!emit("budget_exhausted", { limit: maxTurns })) { + if (!emit("budget_exhausted", { limit: maxTurns, reason: budgetStop })) { UI.println(UI.Style.TEXT_DANGER_BOLD + "!", UI.Style.TEXT_NORMAL + ` ${error}. Aborting session.`) } await sdk.session.abort({ sessionID }, { signal: options?.signal }) @@ -1298,6 +1298,24 @@ You are speaking to a non-technical business executive. Follow these rules stric continue } // altimate_change end + // altimate_change start — overflows rejected before a step starts still hit the attempt backstop + // Such an overflow hands off to compaction (never charged) without a + // step-start, so the step-start budget check never sees the cycle. + if ( + props.error.name === "ContextOverflowError" && sessionID && maxTurns && !budgetAborted && + accounting.turnBudgetStop(maxTurns) === "attempts" && + (!timeExhausted() || options?.turnLimit !== undefined) + ) { + stopTimeBudget() + budgetAborted = true + accounting.onBudgetExhausted() + error = RunAccounting.turnBudgetMessage(maxTurns, "attempts") + if (!emit("budget_exhausted", { limit: maxTurns, reason: "attempts" })) { + UI.println(UI.Style.TEXT_DANGER_BOLD + "!", UI.Style.TEXT_NORMAL + ` ${error}. Aborting session.`) + } + await sdk.session.abort({ sessionID }, { signal: options?.signal }) + } + // altimate_change end // altimate_change end // altimate_change start — keep overflow trace failures recoverable only through compaction if (props.error.name === "ContextOverflowError") recoverableOverflowTraceErrors.add(err) @@ -2061,7 +2079,7 @@ You are speaking to a non-technical business executive. Follow these rules stric if (!answered() && assistantStarted && !accounting.fatal && args.maxTurns && finalBudgetStop) { accounting.onBudgetExhausted() error = RunAccounting.turnBudgetMessage(args.maxTurns, finalBudgetStop) - if (!emit("budget_exhausted", { limit: args.maxTurns })) { + if (!emit("budget_exhausted", { limit: args.maxTurns, reason: finalBudgetStop })) { UI.println(UI.Style.TEXT_WARNING_BOLD + "!", UI.Style.TEXT_NORMAL + ` ${error}. Requesting a final reply.`) } } diff --git a/packages/opencode/test/cli/run-accounting.test.ts b/packages/opencode/test/cli/run-accounting.test.ts index 98cc9996e9..9cac082bb6 100644 --- a/packages/opencode/test/cli/run-accounting.test.ts +++ b/packages/opencode/test/cli/run-accounting.test.ts @@ -104,7 +104,7 @@ describe("RunAccounting turn accounting", () => { test("a step cut short by context overflow still costs its turn, once", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) - // An overflow reported before any step started is not charged. + // An overflow reported before any step started is not charged a turn. acc.onSessionError("ContextOverflowError", "too long") expect(acc.turnCount).toBe(0) acc.onStepAttempt("msg_1") @@ -115,6 +115,27 @@ describe("RunAccounting turn accounting", () => { expect(acc.turnBudgetStop(1)).toBe("completed") }) + test("overflows rejected before any step started count toward the attempt backstop", () => { + const acc = RunAccounting.create() + for (let i = 0; i < RunAccounting.ATTEMPTS_PER_TURN - 1; i++) acc.onSessionError("ContextOverflowError", "too long") + // The prompt channel repeating an overflow does not count it again. + acc.onPromptResult({ error: { name: "ContextOverflowError" } }) + expect(acc.turnBudgetStop(1)).toBeUndefined() + acc.onSessionError("ContextOverflowError", "too long") + expect(acc.turnCount).toBe(0) + expect(acc.turnBudgetStop(1)).toBe("attempts") + }) + + test("a compaction step's overflow is not charged to an earlier unfinished step", () => { + const acc = RunAccounting.create() + acc.onAssistantMessage({ id: "msg_1", agent: "build" }) + acc.onAssistantMessage({ id: "msg_compact", agent: "compaction", summary: true }) + acc.onStepAttempt("msg_1") // interrupted, never finishes + acc.onStepAttempt("msg_compact") + acc.onSessionError("ContextOverflowError", "summary too long") + expect(acc.turnCount).toBe(0) + }) + test("excludes compaction-machinery steps from turnCount", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_work", agent: "build" }) diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index 7d10d1e6b5..d1e4a8a682 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -50,7 +50,9 @@ describe("opencode run: --max-turns charges completed steps only", () => { ) // Attached-session fake: the CLI sees exactly the events the session publishes for // a step whose attempts start and fail, without waiting out real retry backoff. - const attached = (behavior: "all-attempts-fail" | "endless-attempts" | "compaction-after-budget") => + const attached = ( + behavior: "all-attempts-fail" | "endless-attempts" | "compaction-after-budget" | "overflow-before-step", + ) => Effect.gen(function* () { const sessionID = "ses_turn_budget_retry" let stream: ReadableStreamDefaultController | undefined @@ -144,6 +146,44 @@ describe("opencode run: --max-turns charges completed steps only", () => { publish("session.status", { sessionID, status: { type: "idle" } }) return Response.json({ info: { finish: "stop" } }) } + if (behavior === "overflow-before-step") { + // Every request overflows before streaming a step; each overflow hands + // off to a (never charged) compaction step, then the cycle repeats. + let cycle = 0 + const overflowCycle = () => { + cycle++ + counts.attempts++ + publish("session.error", { + sessionID, + error: { name: "ContextOverflowError", data: { message: "prompt is too long" } }, + }) + const summaryID = `${messageID}zz_summary_${cycle}` + publish("message.updated", { + info: { + id: summaryID, + sessionID, + role: "assistant", + parentID, + agent: "compaction", + summary: true, + modelID: "test-model", + providerID: "test", + time: { created: Date.now() }, + }, + }) + publish("message.part.updated", { + part: { id: `${summaryID}_start`, messageID: summaryID, sessionID, type: "step-start" }, + }) + publish("message.part.updated", { + part: { id: `${summaryID}_finish`, messageID: summaryID, sessionID, type: "step-finish", reason: "stop" }, + }) + publish("session.compacted", { sessionID }) + } + attemptTimer = setInterval(overflowCycle, 20) + return new Promise((resolve) => { + finishPrompt = resolve + }) + } if (behavior === "compaction-after-budget") { // The work step answers and completes, using the whole one-turn budget. startAttempt(messageID) @@ -276,7 +316,7 @@ describe("opencode run: --max-turns charges completed steps only", () => { opencode.expectExit(result, 1) const events = opencode.parseJsonEvents(result.stdout) const budgetIndex = events.findIndex((event) => event.type === "budget_exhausted") - expect(events[budgetIndex]).toMatchObject({ limit: 1 }) + expect(events[budgetIndex]).toMatchObject({ limit: 1, reason: "attempts" }) // Exactly one step's worth of attempts ran before the backstop stopped the next. expect(events.slice(0, budgetIndex).filter((event) => event.type === "step_start")).toHaveLength( SessionRetry.RETRY_MAX_ATTEMPTS + 1, @@ -286,6 +326,28 @@ describe("opencode run: --max-turns charges completed steps only", () => { 30_000, ) + cliIt.concurrent( + "overflows rejected before any step starts are stopped by --max-turns alone", + ({ opencode }) => + Effect.gen(function* () { + const { server, sessionID, counts } = yield* attached("overflow-before-step") + const result = yield* opencode.run("Answer once.", { + format: "json", + extraArgs: ["--attach", server.url.toString(), "--session", sessionID, "--max-turns", "1", "--no-trace"], + timeoutMs: 20_000, + bunRun: true, + }) + opencode.expectExit(result, 1) + const events = opencode.parseJsonEvents(result.stdout) + expect(events.filter((event) => event.type === "budget_exhausted")).toEqual([ + expect.objectContaining({ limit: 1, reason: "attempts" }), + ]) + expect(counts.aborts).toBe(1) + expect(counts.attempts).toBeGreaterThanOrEqual(SessionRetry.RETRY_MAX_ATTEMPTS + 1) + }), + 30_000, + ) + cliIt.concurrent( "attempts that keep restarting without completing are still bounded by --time-budget", ({ opencode }) => From e8f89b6c2b5ec8842f7dd0d74a509f90260a7034 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 08:44:59 -0800 Subject: [PATCH 12/20] fix: the attempt backstop also stops a committed echo, and skips the timed finalization Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/cli/cmd/run-accounting.ts | 5 ++--- packages/opencode/src/cli/cmd/run.ts | 11 +++++++---- packages/opencode/test/cli/run-accounting.test.ts | 4 ---- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index e0cabafc53..d0127cff09 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -202,7 +202,8 @@ export function create() { // overflow, which is still charged (see onSessionError). So that free // steps cannot repeat forever, the budget also stops a run once // `maxTurns * ATTEMPTS_PER_TURN` attempts started, the most N steps can - // use when every one exhausts its retries. + // use when every one exhausts its retries. The one budget finalization is + // exempt from that backstop (the caller skips it); its own timeout bounds it. /** Record a started non-compaction step attempt (it is charged a turn only if it finishes). */ onStepAttempt(messageID: string) { // A compaction step supersedes any unfinished step; its overflow is not that step's. @@ -328,8 +329,6 @@ export function create() { takeBudgetFinalizationAllowance(): number | undefined { if (!budgetExhausted || fatalError || pendingContextOverflow || budgetFinalizationSent) return undefined budgetFinalizationSent = true - // The finalization step gets exactly one step's worth of attempts. - attemptCount = turnCount * ATTEMPTS_PER_TURN return turnCount + 1 }, // altimate_change end diff --git a/packages/opencode/src/cli/cmd/run.ts b/packages/opencode/src/cli/cmd/run.ts index 28b220f108..e7e2525dae 100644 --- a/packages/opencode/src/cli/cmd/run.ts +++ b/packages/opencode/src/cli/cmd/run.ts @@ -1157,10 +1157,13 @@ You are speaking to a non-technical business executive. Follow these rules stric // than maxTurns steps have completed, so a retried attempt is free. const requestID = requestOwnership.ownerOf(part.messageID) const committedAnswer = requestID !== undefined && committedRequests.has(requestID) - const budgetStop = compactionStep ? undefined : accounting.turnBudgetStop(maxTurns) + // The attempt backstop does not apply to the one budget finalization + // (options.turnLimit), which its own timeout bounds. + const stop = compactionStep ? undefined : accounting.turnBudgetStop(maxTurns) + const budgetStop = stop === "attempts" && options?.turnLimit !== undefined ? undefined : stop // altimate_change start — the time-budget abort already owns the interrupted work phase if ( - maxTurns && budgetStop && !committedAnswer && !budgetAborted && + maxTurns && budgetStop && (budgetStop === "attempts" || !committedAnswer) && !budgetAborted && (!timeExhausted() || options?.turnLimit !== undefined) ) { stopTimeBudget() @@ -1303,8 +1306,8 @@ You are speaking to a non-technical business executive. Follow these rules stric // step-start, so the step-start budget check never sees the cycle. if ( props.error.name === "ContextOverflowError" && sessionID && maxTurns && !budgetAborted && - accounting.turnBudgetStop(maxTurns) === "attempts" && - (!timeExhausted() || options?.turnLimit !== undefined) + options?.turnLimit === undefined && !timeExhausted() && + accounting.turnBudgetStop(maxTurns) === "attempts" ) { stopTimeBudget() budgetAborted = true diff --git a/packages/opencode/test/cli/run-accounting.test.ts b/packages/opencode/test/cli/run-accounting.test.ts index 9cac082bb6..ac9de7b8b1 100644 --- a/packages/opencode/test/cli/run-accounting.test.ts +++ b/packages/opencode/test/cli/run-accounting.test.ts @@ -84,10 +84,6 @@ describe("RunAccounting turn accounting", () => { expect(acc.turnBudgetStop(1)).toBe("attempts") expect(acc.turnBudgetStop(2)).toBeUndefined() expect(acc.turnBudgetStop(undefined)).toBeUndefined() - // A budget abort's one finalization step still gets a full step's attempts. - acc.onBudgetExhausted() - expect(acc.takeBudgetFinalizationAllowance()).toBe(1) - expect(acc.turnBudgetStop(1)).toBeUndefined() // Completed steps take precedence in the reported reason. acc.onStepFinish("msg_1", "stop") expect(acc.turnBudgetStop(1)).toBe("completed") From 134e8aab5dee9346a213a359c23ecf7b1f37ac67 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 08:55:55 -0800 Subject: [PATCH 13/20] fix: a pre-step overflow after the turn limit stops the run; one abort helper; help names the backstop Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/cli/cmd/run.ts | 49 ++++++++++--------- .../__snapshots__/help-snapshots.test.ts.snap | 5 +- .../test/cli/run/turn-budget-retry.test.ts | 37 +++++++++++++- 3 files changed, 63 insertions(+), 28 deletions(-) diff --git a/packages/opencode/src/cli/cmd/run.ts b/packages/opencode/src/cli/cmd/run.ts index e7e2525dae..7e8c935ef8 100644 --- a/packages/opencode/src/cli/cmd/run.ts +++ b/packages/opencode/src/cli/cmd/run.ts @@ -417,7 +417,7 @@ export const RunCommand = cmd({ .option("max-turns", { type: "number", describe: - "maximum completed assistant work turns (a failed or retried attempt is not counted); allows one committed-answer echo, or one best-effort reply after a budget abort", + "maximum completed assistant work turns (a failed or retried attempt is not counted, but the run also stops after N x 6 step attempts); allows one committed-answer echo, or one best-effort reply after a budget abort", }) // altimate_change start — wall-clock work budget, with a separate finalization allowance .option("time-budget", { @@ -988,6 +988,17 @@ You are speaking to a non-technical business executive. Follow these rules stric // altimate_change start — max-turns budget enforcement (count kept in accounting) const maxTurns = options?.turnLimit ?? args.maxTurns let budgetAborted = false + // One abort sequence for the step-start and overflow budget checks. + const abortForTurnBudget = async (limit: number, stop: RunAccounting.TurnBudgetStop, id: string) => { + stopTimeBudget() + budgetAborted = true + accounting.onBudgetExhausted() + error = RunAccounting.turnBudgetMessage(limit, stop) + if (!emit("budget_exhausted", { limit, reason: stop })) { + UI.println(UI.Style.TEXT_DANGER_BOLD + "!", UI.Style.TEXT_NORMAL + ` ${error}. Aborting session.`) + } + await sdk.session.abort({ sessionID: id }, { signal: options?.signal }) + } // altimate_change end // altimate_change start — parameterized stream @@ -1166,15 +1177,8 @@ You are speaking to a non-technical business executive. Follow these rules stric maxTurns && budgetStop && (budgetStop === "attempts" || !committedAnswer) && !budgetAborted && (!timeExhausted() || options?.turnLimit !== undefined) ) { - stopTimeBudget() // altimate_change end - budgetAborted = true - accounting.onBudgetExhausted() - error = RunAccounting.turnBudgetMessage(maxTurns, budgetStop) - if (!emit("budget_exhausted", { limit: maxTurns, reason: budgetStop })) { - UI.println(UI.Style.TEXT_DANGER_BOLD + "!", UI.Style.TEXT_NORMAL + ` ${error}. Aborting session.`) - } - await sdk.session.abort({ sessionID }, { signal: options?.signal }) + await abortForTurnBudget(maxTurns, budgetStop, part.sessionID) // Drain the ordered abort/error/idle tail before starting another phase. continue } @@ -1301,22 +1305,19 @@ You are speaking to a non-technical business executive. Follow these rules stric continue } // altimate_change end - // altimate_change start — overflows rejected before a step starts still hit the attempt backstop + // altimate_change start — overflows rejected before a step starts still hit the turn budget // Such an overflow hands off to compaction (never charged) without a - // step-start, so the step-start budget check never sees the cycle. - if ( - props.error.name === "ContextOverflowError" && sessionID && maxTurns && !budgetAborted && - options?.turnLimit === undefined && !timeExhausted() && - accounting.turnBudgetStop(maxTurns) === "attempts" - ) { - stopTimeBudget() - budgetAborted = true - accounting.onBudgetExhausted() - error = RunAccounting.turnBudgetMessage(maxTurns, "attempts") - if (!emit("budget_exhausted", { limit: maxTurns, reason: "attempts" })) { - UI.println(UI.Style.TEXT_DANGER_BOLD + "!", UI.Style.TEXT_NORMAL + ` ${error}. Aborting session.`) - } - await sdk.session.abort({ sessionID }, { signal: options?.signal }) + // step-start, so the step-start budget check never sees the cycle. A + // committed answer's echo keeps its completed-turn allowance here too. + const overflowStop = + props.error.name === "ContextOverflowError" && maxTurns && !budgetAborted && + options?.turnLimit === undefined && !timeExhausted() + ? accounting.turnBudgetStop(maxTurns) + : undefined + const activeRequest = requestOwnership.activeRequestID + const echoAllowed = activeRequest !== undefined && committedRequests.has(activeRequest) + if (sessionID && maxTurns && overflowStop && (overflowStop === "attempts" || !echoAllowed)) { + await abortForTurnBudget(maxTurns, overflowStop, sessionID) } // altimate_change end // altimate_change end diff --git a/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap b/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap index 01f6283f32..d4e4f1acec 100644 --- a/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap +++ b/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap @@ -136,8 +136,9 @@ Options: --trace enable session tracing (default: true, disable with --no-trace) [boolean] [default: true] --max-turns maximum completed assistant work turns (a failed or retried - attempt is not counted); allows one committed-answer echo, or - one best-effort reply after a budget abort [number] + attempt is not counted, but the run also stops after N x 6 + step attempts); allows one committed-answer echo, or one + best-effort reply after a budget abort [number] --time-budget maximum time in seconds including startup (env: ALTIMATE_RUN_TIME_BUDGET_S); bounded finalization may follow [number] diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index d1e4a8a682..ff2abb9bc8 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -51,7 +51,12 @@ describe("opencode run: --max-turns charges completed steps only", () => { // Attached-session fake: the CLI sees exactly the events the session publishes for // a step whose attempts start and fail, without waiting out real retry backoff. const attached = ( - behavior: "all-attempts-fail" | "endless-attempts" | "compaction-after-budget" | "overflow-before-step", + behavior: + | "all-attempts-fail" + | "endless-attempts" + | "compaction-after-budget" + | "overflow-before-step" + | "complete-then-overflow", ) => Effect.gen(function* () { const sessionID = "ses_turn_budget_retry" @@ -146,7 +151,14 @@ describe("opencode run: --max-turns charges completed steps only", () => { publish("session.status", { sessionID, status: { type: "idle" } }) return Response.json({ info: { finish: "stop" } }) } - if (behavior === "overflow-before-step") { + if (behavior === "overflow-before-step" || behavior === "complete-then-overflow") { + if (behavior === "complete-then-overflow") { + // One work step completes, using the whole one-turn budget. + startAttempt(messageID) + publish("message.part.updated", { + part: { id: `${messageID}_finish`, messageID, sessionID, type: "step-finish", reason: "tool-calls" }, + }) + } // Every request overflows before streaming a step; each overflow hands // off to a (never charged) compaction step, then the cycle repeats. let cycle = 0 @@ -282,6 +294,27 @@ describe("opencode run: --max-turns charges completed steps only", () => { 30_000, ) + cliIt.concurrent( + "an overflow before any step starts, after the budget is used, stops the run", + ({ opencode }) => + Effect.gen(function* () { + const { server, sessionID, counts } = yield* attached("complete-then-overflow") + const result = yield* opencode.run("Answer once.", { + format: "json", + extraArgs: ["--attach", server.url.toString(), "--session", sessionID, "--max-turns", "1", "--no-trace"], + timeoutMs: 20_000, + bunRun: true, + }) + opencode.expectExit(result, 1) + const events = opencode.parseJsonEvents(result.stdout) + expect(events.filter((event) => event.type === "budget_exhausted")).toEqual([ + expect.objectContaining({ limit: 1, reason: "completed" }), + ]) + expect(counts.aborts).toBe(1) + }), + 30_000, + ) + cliIt.concurrent( "a compaction step after the budget is used is neither charged nor stopped", ({ opencode }) => From d22b2d7854cb343d5f9bb29af0502887ea371468 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 08:59:47 -0800 Subject: [PATCH 14/20] refactor: drop the attempt backstop; keep charging a step cut short by context overflow The backstop drew new edge cases each review round. The approved change is charge-on-finish; an overflowed step stays charged (event channel only, never a compaction step), matching main. Pre-step overflow cycles are unbounded on main too; tracked separately. Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/cli/cmd/run-accounting.ts | 61 ++----- packages/opencode/src/cli/cmd/run.ts | 52 ++---- .../__snapshots__/help-snapshots.test.ts.snap | 5 +- .../opencode/test/cli/run-accounting.test.ts | 66 ++------ .../test/cli/run/budget-finalization.test.ts | 4 +- .../test/cli/run/turn-budget-retry.test.ts | 152 ++++++------------ 6 files changed, 95 insertions(+), 245 deletions(-) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index d0127cff09..c027f2dc39 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -17,9 +17,6 @@ // idle-done challenge bookkeeping; DONE detection delegates to the // SessionTermination completion-token contract. import { SessionTermination } from "../../session/termination" -// altimate_change start — the retry cap sizes the attempt backstop -import { SessionRetry } from "../../session/retry" -// altimate_change end export type WhyModelStopped = "stop" | "tool-call" | "explicit-done" | "unknown" export type WhyHarnessStopped = "budget-exhausted" | "timeout" | "error" | "idle-done" | "none" @@ -94,25 +91,10 @@ export function createRecoverableOverflowTraceErrors() { // the single detector shared with the processor stop-path and the idle-done // challenge, so instruction and detection can never drift apart. -// altimate_change start — the turn budget's backstop for steps that never finish -/** The most attempts one step can start: the first try plus every processor retry. */ -export const ATTEMPTS_PER_TURN = SessionRetry.RETRY_MAX_ATTEMPTS + 1 -export type TurnBudgetStop = "completed" | "attempts" - -/** The user-facing reason a `--max-turns` budget stopped the run. */ -export function turnBudgetMessage(maxTurns: number, stop: TurnBudgetStop): string { - const turns = `${maxTurns} assistant turn${maxTurns !== 1 ? "s" : ""}` - if (stop === "completed") return `Budget exceeded: reached ${turns} limit` - return `Budget exceeded: ${maxTurns * ATTEMPTS_PER_TURN} step attempts started under the ${turns} limit without enough of them completing` -} -// altimate_change end - export function create() { const summaryMessages = new Set() let turnCount = 0 - // altimate_change start — started non-compaction step attempts, finished or not - let attemptCount = 0 - // A started non-compaction step that has not finished yet. + // altimate_change start — the non-compaction step streaming now, until it finishes let openAttempt = false // altimate_change end // altimate_change start — track the active request's committed echo turn allowance @@ -194,36 +176,25 @@ export function create() { // (up to SessionRetry.RETRY_MAX_ATTEMPTS per step), and each retry streams // its own step-start but only the attempt that completes streams a // step-finish. Charging on start billed every failed attempt as a turn. + // A step that ends without a step-finish is free (a failed or retried + // attempt, a soft-deadline interrupt), except one cut short by context + // overflow: it hands off to compaction and is still charged, as before, so + // overflow-and-compact cycles stay bounded by --max-turns. The processor's + // retry cap bounds retried attempts. // `turnCount` is therefore the number of COMPLETED non-compaction steps; // harness-injected generations (idle-done challenge, continuation, // silent-turn reply) are real assistant steps and are charged the same way. - // A step that ends without a step-finish is free (a failed or retried - // attempt, a soft-deadline interrupt), except one cut short by context - // overflow, which is still charged (see onSessionError). So that free - // steps cannot repeat forever, the budget also stops a run once - // `maxTurns * ATTEMPTS_PER_TURN` attempts started, the most N steps can - // use when every one exhausts its retries. The one budget finalization is - // exempt from that backstop (the caller skips it); its own timeout bounds it. - /** Record a started non-compaction step attempt (it is charged a turn only if it finishes). */ + /** Track the step now streaming; a compaction step closes any unfinished one. */ onStepAttempt(messageID: string) { - // A compaction step supersedes any unfinished step; its overflow is not that step's. - if (isCompactionStep(messageID)) { - openAttempt = false - return - } - attemptCount++ - openAttempt = true + openAttempt = !isCompactionStep(messageID) }, /** Completed non-compaction assistant steps. */ get turnCount() { return turnCount }, - /** Why no further step may start under `maxTurns`, or undefined while one may. */ - turnBudgetStop(maxTurns: number | undefined): TurnBudgetStop | undefined { - if (maxTurns === undefined || maxTurns <= 0) return undefined - if (turnCount >= maxTurns) return "completed" - if (attemptCount >= maxTurns * ATTEMPTS_PER_TURN) return "attempts" - return undefined + /** True when `maxTurns` completed steps already ran, so no further step may start. */ + turnBudgetReached(maxTurns: number | undefined): boolean { + return maxTurns !== undefined && maxTurns > 0 && turnCount >= maxTurns }, onStepFinish(messageID: string, reason: string | undefined) { if (isCompactionStep(messageID)) return @@ -275,15 +246,11 @@ export function create() { if (errorName === "ContextOverflowError") { pendingContextOverflow = true // altimate_change start — a step cut short by context overflow still costs its turn - // The overflow hands off to compaction and a fresh step, so charging it keeps - // repeated overflow-and-compact cycles bounded by --max-turns, as before. - if (openAttempt) { + // Counted from the session event only, so the prompt result repeating the + // same overflow cannot charge it again. + if (openAttempt && source === "event") { turnCount++ openAttempt = false - } else if (source === "event") { - // Rejected before any step started: no step-start will ever check the - // budget, so the attempt is counted here and the caller enforces it. - attemptCount++ } // altimate_change end return diff --git a/packages/opencode/src/cli/cmd/run.ts b/packages/opencode/src/cli/cmd/run.ts index 7e8c935ef8..29d3e7910a 100644 --- a/packages/opencode/src/cli/cmd/run.ts +++ b/packages/opencode/src/cli/cmd/run.ts @@ -417,7 +417,7 @@ export const RunCommand = cmd({ .option("max-turns", { type: "number", describe: - "maximum completed assistant work turns (a failed or retried attempt is not counted, but the run also stops after N x 6 step attempts); allows one committed-answer echo, or one best-effort reply after a budget abort", + "maximum completed assistant work turns (a failed or retried attempt is not counted); allows one committed-answer echo, or one best-effort reply after a budget abort", }) // altimate_change start — wall-clock work budget, with a separate finalization allowance .option("time-budget", { @@ -988,17 +988,6 @@ You are speaking to a non-technical business executive. Follow these rules stric // altimate_change start — max-turns budget enforcement (count kept in accounting) const maxTurns = options?.turnLimit ?? args.maxTurns let budgetAborted = false - // One abort sequence for the step-start and overflow budget checks. - const abortForTurnBudget = async (limit: number, stop: RunAccounting.TurnBudgetStop, id: string) => { - stopTimeBudget() - budgetAborted = true - accounting.onBudgetExhausted() - error = RunAccounting.turnBudgetMessage(limit, stop) - if (!emit("budget_exhausted", { limit, reason: stop })) { - UI.println(UI.Style.TEXT_DANGER_BOLD + "!", UI.Style.TEXT_NORMAL + ` ${error}. Aborting session.`) - } - await sdk.session.abort({ sessionID: id }, { signal: options?.signal }) - } // altimate_change end // altimate_change start — parameterized stream @@ -1168,17 +1157,20 @@ You are speaking to a non-technical business executive. Follow these rules stric // than maxTurns steps have completed, so a retried attempt is free. const requestID = requestOwnership.ownerOf(part.messageID) const committedAnswer = requestID !== undefined && committedRequests.has(requestID) - // The attempt backstop does not apply to the one budget finalization - // (options.turnLimit), which its own timeout bounds. - const stop = compactionStep ? undefined : accounting.turnBudgetStop(maxTurns) - const budgetStop = stop === "attempts" && options?.turnLimit !== undefined ? undefined : stop // altimate_change start — the time-budget abort already owns the interrupted work phase if ( - maxTurns && budgetStop && (budgetStop === "attempts" || !committedAnswer) && !budgetAborted && + !compactionStep && accounting.turnBudgetReached(maxTurns) && !committedAnswer && !budgetAborted && (!timeExhausted() || options?.turnLimit !== undefined) ) { + stopTimeBudget() // altimate_change end - await abortForTurnBudget(maxTurns, budgetStop, part.sessionID) + budgetAborted = true + accounting.onBudgetExhausted() + error = `Budget exceeded: reached ${maxTurns} assistant turn${maxTurns !== 1 ? "s" : ""} limit` + if (!emit("budget_exhausted", { limit: maxTurns })) { + UI.println(UI.Style.TEXT_DANGER_BOLD + "!", UI.Style.TEXT_NORMAL + ` ${error}. Aborting session.`) + } + await sdk.session.abort({ sessionID }, { signal: options?.signal }) // Drain the ordered abort/error/idle tail before starting another phase. continue } @@ -1305,21 +1297,6 @@ You are speaking to a non-technical business executive. Follow these rules stric continue } // altimate_change end - // altimate_change start — overflows rejected before a step starts still hit the turn budget - // Such an overflow hands off to compaction (never charged) without a - // step-start, so the step-start budget check never sees the cycle. A - // committed answer's echo keeps its completed-turn allowance here too. - const overflowStop = - props.error.name === "ContextOverflowError" && maxTurns && !budgetAborted && - options?.turnLimit === undefined && !timeExhausted() - ? accounting.turnBudgetStop(maxTurns) - : undefined - const activeRequest = requestOwnership.activeRequestID - const echoAllowed = activeRequest !== undefined && committedRequests.has(activeRequest) - if (sessionID && maxTurns && overflowStop && (overflowStop === "attempts" || !echoAllowed)) { - await abortForTurnBudget(maxTurns, overflowStop, sessionID) - } - // altimate_change end // altimate_change end // altimate_change start — keep overflow trace failures recoverable only through compaction if (props.error.name === "ContextOverflowError") recoverableOverflowTraceErrors.add(err) @@ -2078,12 +2055,11 @@ You are speaking to a non-technical business executive. Follow these rules stric // A silent stop exactly at the work limit has no in-flight generation to // abort. Enter finalization directly instead of spending a reply just to // discover that it exceeds the same budget. - // The same budget check the event loop uses. - const finalBudgetStop = accounting.turnBudgetStop(args.maxTurns) - if (!answered() && assistantStarted && !accounting.fatal && args.maxTurns && finalBudgetStop) { + // The same completed-step count the in-loop budget check uses. + if (!answered() && assistantStarted && !accounting.fatal && accounting.turnBudgetReached(args.maxTurns)) { accounting.onBudgetExhausted() - error = RunAccounting.turnBudgetMessage(args.maxTurns, finalBudgetStop) - if (!emit("budget_exhausted", { limit: args.maxTurns, reason: finalBudgetStop })) { + error = `Budget exceeded: reached ${args.maxTurns} assistant turn${args.maxTurns !== 1 ? "s" : ""} limit` + if (!emit("budget_exhausted", { limit: args.maxTurns })) { UI.println(UI.Style.TEXT_WARNING_BOLD + "!", UI.Style.TEXT_NORMAL + ` ${error}. Requesting a final reply.`) } } diff --git a/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap b/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap index d4e4f1acec..01f6283f32 100644 --- a/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap +++ b/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap @@ -136,9 +136,8 @@ Options: --trace enable session tracing (default: true, disable with --no-trace) [boolean] [default: true] --max-turns maximum completed assistant work turns (a failed or retried - attempt is not counted, but the run also stops after N x 6 - step attempts); allows one committed-answer echo, or one - best-effort reply after a budget abort [number] + attempt is not counted); allows one committed-answer echo, or + one best-effort reply after a budget abort [number] --time-budget maximum time in seconds including startup (env: ALTIMATE_RUN_TIME_BUDGET_S); bounded finalization may follow [number] diff --git a/packages/opencode/test/cli/run-accounting.test.ts b/packages/opencode/test/cli/run-accounting.test.ts index ac9de7b8b1..676ed0e62b 100644 --- a/packages/opencode/test/cli/run-accounting.test.ts +++ b/packages/opencode/test/cli/run-accounting.test.ts @@ -36,27 +36,20 @@ describe("RunAccounting turn accounting", () => { // Attempt 1 started and its stream broke, so it wrote no step-finish. The // retry's start is checked against completed steps only, so a one-turn // budget still lets it run. - acc.onStepAttempt("msg_1") expect(acc.turnCount).toBe(0) - expect(acc.turnBudgetStop(1)).toBeUndefined() - acc.onStepAttempt("msg_1") - expect(acc.turnBudgetStop(1)).toBeUndefined() + expect(acc.turnBudgetReached(1)).toBe(false) acc.onStepFinish("msg_1", "stop") expect(acc.turnCount).toBe(1) - expect(acc.turnBudgetStop(1)).toBeDefined() + expect(acc.turnBudgetReached(1)).toBe(true) }) test("a session error with no completed step charges no turn and stays fatal", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) // Every attempt the processor allows failed before finishing. - for (let i = 0; i < RunAccounting.ATTEMPTS_PER_TURN; i++) acc.onStepAttempt("msg_1") acc.onSessionError("APIError", "upstream stream reset") expect(acc.turnCount).toBe(0) - // No turn was charged. The processor makes no seventh attempt; if anything - // did start another step, the attempt backstop would stop it. - expect(acc.turnBudgetStop(1)).toBe("attempts") - expect(acc.turnBudgetStop(2)).toBeUndefined() + expect(acc.turnBudgetReached(1)).toBe(false) expect(acc.fatal).toBe(true) expect(acc.termination().why_harness_stopped).toBe("error") }) @@ -64,62 +57,25 @@ describe("RunAccounting turn accounting", () => { test("the budget is reached only after maxTurns steps complete", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) - expect(acc.turnBudgetStop(1)).toBeUndefined() + expect(acc.turnBudgetReached(1)).toBe(false) acc.onStepFinish("msg_1", "tool-calls") - expect(acc.turnBudgetStop(1)).toBeDefined() - expect(acc.turnBudgetStop(2)).toBeUndefined() - expect(acc.turnBudgetStop(undefined)).toBeUndefined() - }) - - test("attempts that never finish stop the budget once they exceed what N steps' retries allow", () => { - const acc = RunAccounting.create() - acc.onAssistantMessage({ id: "msg_1", agent: "build" }) - acc.onAssistantMessage({ id: "msg_compact", agent: "compaction", summary: true }) - // Compaction attempts never count toward the backstop. - for (let i = 0; i < 20; i++) acc.onStepAttempt("msg_compact") - for (let i = 0; i < RunAccounting.ATTEMPTS_PER_TURN - 1; i++) acc.onStepAttempt("msg_1") - expect(acc.turnBudgetStop(1)).toBeUndefined() - acc.onStepAttempt("msg_1") - expect(acc.turnCount).toBe(0) - expect(acc.turnBudgetStop(1)).toBe("attempts") - expect(acc.turnBudgetStop(2)).toBeUndefined() - expect(acc.turnBudgetStop(undefined)).toBeUndefined() - // Completed steps take precedence in the reported reason. - acc.onStepFinish("msg_1", "stop") - expect(acc.turnBudgetStop(1)).toBe("completed") - }) - - test("the budget message names the limit, and the attempt backstop says why", () => { - expect(RunAccounting.turnBudgetMessage(3, "completed")).toBe("Budget exceeded: reached 3 assistant turns limit") - expect(RunAccounting.turnBudgetMessage(1, "completed")).toBe("Budget exceeded: reached 1 assistant turn limit") - expect(RunAccounting.turnBudgetMessage(1, "attempts")).toBe( - `Budget exceeded: ${RunAccounting.ATTEMPTS_PER_TURN} step attempts started under the 1 assistant turn limit without enough of them completing`, - ) + expect(acc.turnBudgetReached(1)).toBe(true) + expect(acc.turnBudgetReached(2)).toBe(false) + expect(acc.turnBudgetReached(undefined)).toBe(false) }) test("a step cut short by context overflow still costs its turn, once", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) - // An overflow reported before any step started is not charged a turn. + // An overflow reported before any step started is not charged. acc.onSessionError("ContextOverflowError", "too long") expect(acc.turnCount).toBe(0) acc.onStepAttempt("msg_1") - acc.onSessionError("ContextOverflowError", "too long") - // The same overflow reported again on the prompt channel is not charged twice. + // The prompt result may report it before the session event; only the event charges. acc.onPromptResult({ error: { name: "ContextOverflowError" } }) - expect(acc.turnCount).toBe(1) - expect(acc.turnBudgetStop(1)).toBe("completed") - }) - - test("overflows rejected before any step started count toward the attempt backstop", () => { - const acc = RunAccounting.create() - for (let i = 0; i < RunAccounting.ATTEMPTS_PER_TURN - 1; i++) acc.onSessionError("ContextOverflowError", "too long") - // The prompt channel repeating an overflow does not count it again. - acc.onPromptResult({ error: { name: "ContextOverflowError" } }) - expect(acc.turnBudgetStop(1)).toBeUndefined() acc.onSessionError("ContextOverflowError", "too long") - expect(acc.turnCount).toBe(0) - expect(acc.turnBudgetStop(1)).toBe("attempts") + acc.onSessionError("ContextOverflowError", "too long") + expect(acc.turnCount).toBe(1) }) test("a compaction step's overflow is not charged to an earlier unfinished step", () => { diff --git a/packages/opencode/test/cli/run/budget-finalization.test.ts b/packages/opencode/test/cli/run/budget-finalization.test.ts index 8db9ac1f79..eac6e3983d 100644 --- a/packages/opencode/test/cli/run/budget-finalization.test.ts +++ b/packages/opencode/test/cli/run/budget-finalization.test.ts @@ -113,11 +113,11 @@ describe("budget finalization accounting", () => { expect(accounting.turnCount).toBe(1) expect(accounting.fatal).toBe(true) - expect(accounting.turnBudgetStop(2)).toBeUndefined() + expect(accounting.turnBudgetReached(2)).toBe(false) accounting.onText("finalization", "The supported result is available; verification is incomplete.") accounting.onStepFinish("finalization", "stop") expect(accounting.turnCount).toBe(2) - expect(accounting.turnBudgetStop(2)).toBeDefined() + expect(accounting.turnBudgetReached(2)).toBe(true) expect(accounting.termination()).toEqual({ why_model_stopped: "stop", why_harness_stopped: "budget-exhausted", diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index ff2abb9bc8..5c18cbae7b 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -50,14 +50,7 @@ describe("opencode run: --max-turns charges completed steps only", () => { ) // Attached-session fake: the CLI sees exactly the events the session publishes for // a step whose attempts start and fail, without waiting out real retry backoff. - const attached = ( - behavior: - | "all-attempts-fail" - | "endless-attempts" - | "compaction-after-budget" - | "overflow-before-step" - | "complete-then-overflow", - ) => + const attached = (behavior: "all-attempts-fail" | "endless-attempts" | "compaction-after-budget" | "overflow-mid-step") => Effect.gen(function* () { const sessionID = "ses_turn_budget_retry" let stream: ReadableStreamDefaultController | undefined @@ -151,47 +144,50 @@ describe("opencode run: --max-turns charges completed steps only", () => { publish("session.status", { sessionID, status: { type: "idle" } }) return Response.json({ info: { finish: "stop" } }) } - if (behavior === "overflow-before-step" || behavior === "complete-then-overflow") { - if (behavior === "complete-then-overflow") { - // One work step completes, using the whole one-turn budget. - startAttempt(messageID) - publish("message.part.updated", { - part: { id: `${messageID}_finish`, messageID, sessionID, type: "step-finish", reason: "tool-calls" }, - }) - } - // Every request overflows before streaming a step; each overflow hands - // off to a (never charged) compaction step, then the cycle repeats. - let cycle = 0 - const overflowCycle = () => { - cycle++ - counts.attempts++ - publish("session.error", { + if (behavior === "overflow-mid-step") { + // A step starts, then overflows; compaction runs and the work continues. + startAttempt(messageID) + publish("session.error", { + sessionID, + error: { name: "ContextOverflowError", data: { message: "prompt is too long" } }, + }) + const summaryID = `${messageID}zz_summary` + publish("message.updated", { + info: { + id: summaryID, sessionID, - error: { name: "ContextOverflowError", data: { message: "prompt is too long" } }, - }) - const summaryID = `${messageID}zz_summary_${cycle}` - publish("message.updated", { - info: { - id: summaryID, - sessionID, - role: "assistant", - parentID, - agent: "compaction", - summary: true, - modelID: "test-model", - providerID: "test", - time: { created: Date.now() }, - }, - }) - publish("message.part.updated", { - part: { id: `${summaryID}_start`, messageID: summaryID, sessionID, type: "step-start" }, - }) - publish("message.part.updated", { - part: { id: `${summaryID}_finish`, messageID: summaryID, sessionID, type: "step-finish", reason: "stop" }, - }) - publish("session.compacted", { sessionID }) - } - attemptTimer = setInterval(overflowCycle, 20) + role: "assistant", + parentID, + agent: "compaction", + summary: true, + modelID: "test-model", + providerID: "test", + time: { created: Date.now() }, + }, + }) + publish("message.part.updated", { + part: { id: `${summaryID}_start`, messageID: summaryID, sessionID, type: "step-start" }, + }) + publish("message.part.updated", { + part: { id: `${summaryID}_finish`, messageID: summaryID, sessionID, type: "step-finish", reason: "stop" }, + }) + publish("session.compacted", { sessionID }) + const nextID = `${messageID}zzz_next` + publish("message.updated", { + info: { + id: nextID, + sessionID, + role: "assistant", + parentID, + agent: "build", + modelID: "test-model", + providerID: "test", + time: { created: Date.now() }, + }, + }) + publish("message.part.updated", { + part: { id: `${nextID}_start`, messageID: nextID, sessionID, type: "step-start" }, + }) return new Promise((resolve) => { finishPrompt = resolve }) @@ -295,10 +291,10 @@ describe("opencode run: --max-turns charges completed steps only", () => { ) cliIt.concurrent( - "an overflow before any step starts, after the budget is used, stops the run", + "a step cut short by context overflow still costs its turn", ({ opencode }) => Effect.gen(function* () { - const { server, sessionID, counts } = yield* attached("complete-then-overflow") + const { server, sessionID, counts } = yield* attached("overflow-mid-step") const result = yield* opencode.run("Answer once.", { format: "json", extraArgs: ["--attach", server.url.toString(), "--session", sessionID, "--max-turns", "1", "--no-trace"], @@ -307,8 +303,9 @@ describe("opencode run: --max-turns charges completed steps only", () => { }) opencode.expectExit(result, 1) const events = opencode.parseJsonEvents(result.stdout) + // The overflowed step used the one turn, so the continuation's step is stopped. expect(events.filter((event) => event.type === "budget_exhausted")).toEqual([ - expect.objectContaining({ limit: 1, reason: "completed" }), + expect.objectContaining({ limit: 1 }), ]) expect(counts.aborts).toBe(1) }), @@ -335,52 +332,6 @@ describe("opencode run: --max-turns charges completed steps only", () => { 30_000, ) - cliIt.concurrent( - "attempts that keep restarting without completing are stopped by --max-turns alone", - ({ opencode }) => - Effect.gen(function* () { - const { server, sessionID, counts } = yield* attached("endless-attempts") - const result = yield* opencode.run("Answer once.", { - format: "json", - extraArgs: ["--attach", server.url.toString(), "--session", sessionID, "--max-turns", "1", "--no-trace"], - timeoutMs: 20_000, - bunRun: true, - }) - opencode.expectExit(result, 1) - const events = opencode.parseJsonEvents(result.stdout) - const budgetIndex = events.findIndex((event) => event.type === "budget_exhausted") - expect(events[budgetIndex]).toMatchObject({ limit: 1, reason: "attempts" }) - // Exactly one step's worth of attempts ran before the backstop stopped the next. - expect(events.slice(0, budgetIndex).filter((event) => event.type === "step_start")).toHaveLength( - SessionRetry.RETRY_MAX_ATTEMPTS + 1, - ) - expect(counts.aborts).toBe(1) - }), - 30_000, - ) - - cliIt.concurrent( - "overflows rejected before any step starts are stopped by --max-turns alone", - ({ opencode }) => - Effect.gen(function* () { - const { server, sessionID, counts } = yield* attached("overflow-before-step") - const result = yield* opencode.run("Answer once.", { - format: "json", - extraArgs: ["--attach", server.url.toString(), "--session", sessionID, "--max-turns", "1", "--no-trace"], - timeoutMs: 20_000, - bunRun: true, - }) - opencode.expectExit(result, 1) - const events = opencode.parseJsonEvents(result.stdout) - expect(events.filter((event) => event.type === "budget_exhausted")).toEqual([ - expect.objectContaining({ limit: 1, reason: "attempts" }), - ]) - expect(counts.aborts).toBe(1) - expect(counts.attempts).toBeGreaterThanOrEqual(SessionRetry.RETRY_MAX_ATTEMPTS + 1) - }), - 30_000, - ) - cliIt.concurrent( "attempts that keep restarting without completing are still bounded by --time-budget", ({ opencode }) => @@ -393,6 +344,8 @@ describe("opencode run: --max-turns charges completed steps only", () => { server.url.toString(), "--session", sessionID, + "--max-turns", + "1", "--time-budget", "5", "--no-trace", @@ -401,13 +354,12 @@ describe("opencode run: --max-turns charges completed steps only", () => { bunRun: true, }) const events = opencode.parseJsonEvents(result.stdout) - // Without a turn budget, the wall-clock budget ends the work. + // The turn budget never fires (no attempt completed); the wall-clock budget ends the work. expect(events.filter((event) => event.type === "budget_exhausted")).toEqual([ expect.objectContaining({ limit_seconds: 5 }), ]) expect(counts.aborts).toBeGreaterThanOrEqual(1) - // More than the initial burst: attempts kept coming until the deadline stopped them. - expect(counts.attempts).toBeGreaterThan(SessionRetry.RETRY_MAX_ATTEMPTS + 2) + expect(counts.attempts).toBeGreaterThan(SessionRetry.RETRY_MAX_ATTEMPTS + 1) expect( events.filter((event) => event.type === "text").map((event) => (event.part as { text: string }).text), ).toEqual(["Best-effort answer."]) From 3065db00c033d241da420ae52f43d1597f7cebe1 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 09:07:46 -0800 Subject: [PATCH 15/20] fix: an idle session closes the open step; unit tests record each attempt Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/cli/cmd/run-accounting.ts | 4 ++++ packages/opencode/src/cli/cmd/run.ts | 1 + packages/opencode/test/cli/run-accounting.test.ts | 12 ++++++++++++ 3 files changed, 17 insertions(+) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index c027f2dc39..da6db3e89a 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -188,6 +188,10 @@ export function create() { onStepAttempt(messageID: string) { openAttempt = !isCompactionStep(messageID) }, + /** The session went idle: no step is streaming any more. */ + onIdle() { + openAttempt = false + }, /** Completed non-compaction assistant steps. */ get turnCount() { return turnCount diff --git a/packages/opencode/src/cli/cmd/run.ts b/packages/opencode/src/cli/cmd/run.ts index 29d3e7910a..c24c3fa300 100644 --- a/packages/opencode/src/cli/cmd/run.ts +++ b/packages/opencode/src/cli/cmd/run.ts @@ -1369,6 +1369,7 @@ You are speaking to a non-technical business executive. Follow these rules stric // altimate_change end // altimate_change start — idle gaps are not active work; continuations retain the same start time workActive = false + accounting.onIdle() // altimate_change end break } diff --git a/packages/opencode/test/cli/run-accounting.test.ts b/packages/opencode/test/cli/run-accounting.test.ts index 676ed0e62b..d8de83517b 100644 --- a/packages/opencode/test/cli/run-accounting.test.ts +++ b/packages/opencode/test/cli/run-accounting.test.ts @@ -36,6 +36,8 @@ describe("RunAccounting turn accounting", () => { // Attempt 1 started and its stream broke, so it wrote no step-finish. The // retry's start is checked against completed steps only, so a one-turn // budget still lets it run. + acc.onStepAttempt("msg_1") + acc.onStepAttempt("msg_1") expect(acc.turnCount).toBe(0) expect(acc.turnBudgetReached(1)).toBe(false) acc.onStepFinish("msg_1", "stop") @@ -47,6 +49,7 @@ describe("RunAccounting turn accounting", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) // Every attempt the processor allows failed before finishing. + for (let i = 0; i < 6; i++) acc.onStepAttempt("msg_1") acc.onSessionError("APIError", "upstream stream reset") expect(acc.turnCount).toBe(0) expect(acc.turnBudgetReached(1)).toBe(false) @@ -88,6 +91,15 @@ describe("RunAccounting turn accounting", () => { expect(acc.turnCount).toBe(0) }) + test("after the session goes idle, a later pre-step overflow is not charged to an interrupted step", () => { + const acc = RunAccounting.create() + acc.onAssistantMessage({ id: "msg_1", agent: "build" }) + acc.onStepAttempt("msg_1") // soft-deadline interrupt: no step-finish + acc.onIdle() + acc.onSessionError("ContextOverflowError", "too long") + expect(acc.turnCount).toBe(0) + }) + test("excludes compaction-machinery steps from turnCount", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_work", agent: "build" }) From 0cbcee07f6ea627416a4069a67a2871955a48bf5 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 09:12:45 -0800 Subject: [PATCH 16/20] test: the endless-attempts test requires attempts after the initial burst Co-Authored-By: Claude Opus 5.5 --- .../opencode/test/cli/run/turn-budget-retry.test.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index 5c18cbae7b..334a2d1872 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -57,7 +57,7 @@ describe("opencode run: --max-turns charges completed steps only", () => { let subscriptionReady = Promise.withResolvers() let finishPrompt: (value: Response) => void = () => {} let attemptTimer: ReturnType | undefined - const counts = { prompts: 0, aborts: 0, attempts: 0 } + const counts = { prompts: 0, aborts: 0, attempts: 0, timerAttempts: 0 } const encoder = new TextEncoder() const publish = (type: string, properties: object) => stream?.enqueue(encoder.encode(`data: ${JSON.stringify({ type, properties })}\n\n`)) @@ -243,7 +243,10 @@ describe("opencode run: --max-turns charges completed steps only", () => { // A step that keeps failing and restarting without ever completing: // more attempts than one step's retry cap at once, then more until aborted. for (let attempt = 0; attempt <= SessionRetry.RETRY_MAX_ATTEMPTS + 1; attempt++) startAttempt(messageID) - attemptTimer = setInterval(() => startAttempt(messageID), 20) + attemptTimer = setInterval(() => { + counts.timerAttempts++ + startAttempt(messageID) + }, 20) return new Promise((resolve) => { finishPrompt = resolve }) @@ -359,7 +362,8 @@ describe("opencode run: --max-turns charges completed steps only", () => { expect.objectContaining({ limit_seconds: 5 }), ]) expect(counts.aborts).toBeGreaterThanOrEqual(1) - expect(counts.attempts).toBeGreaterThan(SessionRetry.RETRY_MAX_ATTEMPTS + 1) + // Attempts kept restarting after the initial burst until the deadline stopped them. + expect(counts.timerAttempts).toBeGreaterThan(0) expect( events.filter((event) => event.type === "text").map((event) => (event.part as { text: string }).text), ).toEqual(["Best-effort answer."]) From 72137c395aab68a2b430ccd3b4fd659e4d7f3575 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 09:24:17 -0800 Subject: [PATCH 17/20] fix: a retry closes the failed attempt; help says harness turns count; overflow tests Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/cli/cmd/run-accounting.ts | 4 +-- packages/opencode/src/cli/cmd/run.ts | 12 ++++++-- .../__snapshots__/help-snapshots.test.ts.snap | 7 +++-- .../opencode/test/cli/run-accounting.test.ts | 28 ++++++++++++++++++- .../test/cli/run/turn-budget-retry.test.ts | 10 ++++--- 5 files changed, 49 insertions(+), 12 deletions(-) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index da6db3e89a..25db6ed095 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -188,8 +188,8 @@ export function create() { onStepAttempt(messageID: string) { openAttempt = !isCompactionStep(messageID) }, - /** The session went idle: no step is streaming any more. */ - onIdle() { + /** No step is streaming any more (the session went idle, or a retry began). */ + closeOpenStep() { openAttempt = false }, /** Completed non-compaction assistant steps. */ diff --git a/packages/opencode/src/cli/cmd/run.ts b/packages/opencode/src/cli/cmd/run.ts index c24c3fa300..6a2fac90b5 100644 --- a/packages/opencode/src/cli/cmd/run.ts +++ b/packages/opencode/src/cli/cmd/run.ts @@ -417,7 +417,7 @@ export const RunCommand = cmd({ .option("max-turns", { type: "number", describe: - "maximum completed assistant work turns (a failed or retried attempt is not counted); allows one committed-answer echo, or one best-effort reply after a budget abort", + "maximum completed assistant work turns, including harness follow-up turns (a failed or retried attempt is not counted); allows one committed-answer echo, or one best-effort reply after a budget abort", }) // altimate_change start — wall-clock work budget, with a separate finalization allowance .option("time-budget", { @@ -1359,6 +1359,14 @@ You are speaking to a non-technical business executive. Follow these rules stric } // altimate_change end + // altimate_change start — a retry begins: the failed attempt is over and stays uncharged + if ( + event.type === "session.status" && + event.properties.sessionID === sessionID && + event.properties.status.type === "retry" + ) + accounting.closeOpenStep() + // altimate_change end if ( event.type === "session.status" && event.properties.sessionID === sessionID && @@ -1369,7 +1377,7 @@ You are speaking to a non-technical business executive. Follow these rules stric // altimate_change end // altimate_change start — idle gaps are not active work; continuations retain the same start time workActive = false - accounting.onIdle() + accounting.closeOpenStep() // altimate_change end break } diff --git a/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap b/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap index 01f6283f32..36f831897d 100644 --- a/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap +++ b/packages/opencode/test/cli/help/__snapshots__/help-snapshots.test.ts.snap @@ -135,9 +135,10 @@ Options: statement (1-indexed) [number] --trace enable session tracing (default: true, disable with --no-trace) [boolean] [default: true] - --max-turns maximum completed assistant work turns (a failed or retried - attempt is not counted); allows one committed-answer echo, or - one best-effort reply after a budget abort [number] + --max-turns maximum completed assistant work turns, including harness + follow-up turns (a failed or retried attempt is not counted); + allows one committed-answer echo, or one best-effort reply + after a budget abort [number] --time-budget maximum time in seconds including startup (env: ALTIMATE_RUN_TIME_BUDGET_S); bounded finalization may follow [number] diff --git a/packages/opencode/test/cli/run-accounting.test.ts b/packages/opencode/test/cli/run-accounting.test.ts index d8de83517b..d71d1df01b 100644 --- a/packages/opencode/test/cli/run-accounting.test.ts +++ b/packages/opencode/test/cli/run-accounting.test.ts @@ -95,11 +95,37 @@ describe("RunAccounting turn accounting", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_1", agent: "build" }) acc.onStepAttempt("msg_1") // soft-deadline interrupt: no step-finish - acc.onIdle() + acc.closeOpenStep() acc.onSessionError("ContextOverflowError", "too long") expect(acc.turnCount).toBe(0) }) + test("an overflow after a finished step is not charged again", () => { + const acc = RunAccounting.create() + acc.onAssistantMessage({ id: "msg_1", agent: "build" }) + acc.onStepAttempt("msg_1") + acc.onStepFinish("msg_1", "tool-calls") + acc.onSessionError("ContextOverflowError", "too long") + expect(acc.turnCount).toBe(1) + }) + + test("an overflow reported only on the prompt result charges nothing", () => { + const acc = RunAccounting.create() + acc.onAssistantMessage({ id: "msg_1", agent: "build" }) + acc.onStepAttempt("msg_1") + acc.onPromptResult({ error: { name: "ContextOverflowError" } }) + expect(acc.turnCount).toBe(0) + }) + + test("once a retry begins, a later pre-step overflow is not charged to the failed attempt", () => { + const acc = RunAccounting.create() + acc.onAssistantMessage({ id: "msg_1", agent: "build" }) + acc.onStepAttempt("msg_1") // fails mid-stream + acc.closeOpenStep() // retry status + acc.onSessionError("ContextOverflowError", "too long") // retry rejected before streaming + expect(acc.turnCount).toBe(0) + }) + test("excludes compaction-machinery steps from turnCount", () => { const acc = RunAccounting.create() acc.onAssistantMessage({ id: "msg_work", agent: "build" }) diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index 334a2d1872..2a1ee5d848 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -63,13 +63,15 @@ describe("opencode run: --max-turns charges completed steps only", () => { stream?.enqueue(encoder.encode(`data: ${JSON.stringify({ type, properties })}\n\n`)) const startAttempt = (messageID: string) => { counts.attempts++ + // As in the processor, a retry status precedes every attempt after the first. + if (counts.attempts > 1) + publish("session.status", { + sessionID, + status: { type: "retry", attempt: counts.attempts - 1, message: "rate limit exceeded", next: Date.now() + 10 }, + }) publish("message.part.updated", { part: { id: `${messageID}_start_${counts.attempts}`, messageID, sessionID, type: "step-start" }, }) - publish("session.status", { - sessionID, - status: { type: "retry", attempt: counts.attempts, message: "rate limit exceeded", next: Date.now() + 10 }, - }) } const server = yield* Effect.acquireRelease( Effect.sync(() => From 60bb2b98e69a4ac0f10abe56f9c47b78f851e684 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 09:27:34 -0800 Subject: [PATCH 18/20] test: the attached fake counts retries per assistant step, like the processor Co-Authored-By: Claude Opus 5.5 --- .../opencode/test/cli/run/turn-budget-retry.test.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index 2a1ee5d848..daccc32dac 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -61,13 +61,17 @@ describe("opencode run: --max-turns charges completed steps only", () => { const encoder = new TextEncoder() const publish = (type: string, properties: object) => stream?.enqueue(encoder.encode(`data: ${JSON.stringify({ type, properties })}\n\n`)) + // The processor counts retries per assistant step, so the fake does too. + const stepAttempts = new Map() const startAttempt = (messageID: string) => { counts.attempts++ - // As in the processor, a retry status precedes every attempt after the first. - if (counts.attempts > 1) + const attempt = (stepAttempts.get(messageID) ?? 0) + 1 + stepAttempts.set(messageID, attempt) + // As in the processor, a retry status precedes every attempt of a step after its first. + if (attempt > 1) publish("session.status", { sessionID, - status: { type: "retry", attempt: counts.attempts - 1, message: "rate limit exceeded", next: Date.now() + 10 }, + status: { type: "retry", attempt: attempt - 1, message: "rate limit exceeded", next: Date.now() + 10 }, }) publish("message.part.updated", { part: { id: `${messageID}_start_${counts.attempts}`, messageID, sessionID, type: "step-start" }, From efb97522da5361d6385d8aecf927dcd3ced2ee9a Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 09:39:35 -0800 Subject: [PATCH 19/20] fix: a step that compaction replaces is charged regardless of event order; retry-then-overflow test Co-Authored-By: Claude Opus 5.5 --- .../opencode/src/cli/cmd/run-accounting.ts | 19 ++-- .../opencode/test/cli/run-accounting.test.ts | 21 +++-- .../test/cli/run/turn-budget-retry.test.ts | 87 ++++++++++++++++++- 3 files changed, 112 insertions(+), 15 deletions(-) diff --git a/packages/opencode/src/cli/cmd/run-accounting.ts b/packages/opencode/src/cli/cmd/run-accounting.ts index 25db6ed095..f4cc226d34 100644 --- a/packages/opencode/src/cli/cmd/run-accounting.ts +++ b/packages/opencode/src/cli/cmd/run-accounting.ts @@ -176,17 +176,24 @@ export function create() { // (up to SessionRetry.RETRY_MAX_ATTEMPTS per step), and each retry streams // its own step-start but only the attempt that completes streams a // step-finish. Charging on start billed every failed attempt as a turn. - // A step that ends without a step-finish is free (a failed or retried - // attempt, a soft-deadline interrupt), except one cut short by context - // overflow: it hands off to compaction and is still charged, as before, so - // overflow-and-compact cycles stay bounded by --max-turns. The processor's - // retry cap bounds retried attempts. + // A step that ends without a step-finish is free when a retry or idle + // closes it (a failed or retried attempt). A step still open when compaction + // replaces it, or cut short by a context-overflow error, is charged as + // before, so overflow-and-compact cycles stay bounded by --max-turns + // whichever of the overflow error and the compaction start arrives first. + // The processor's retry cap bounds retried attempts. // `turnCount` is therefore the number of COMPLETED non-compaction steps; // harness-injected generations (idle-done challenge, continuation, // silent-turn reply) are real assistant steps and are charged the same way. /** Track the step now streaming; a compaction step closes any unfinished one. */ onStepAttempt(messageID: string) { - openAttempt = !isCompactionStep(messageID) + if (!isCompactionStep(messageID)) { + openAttempt = true + return + } + // Compaction replacing a step that never finished: that step was cut short. + if (openAttempt) turnCount++ + openAttempt = false }, /** No step is streaming any more (the session went idle, or a retry began). */ closeOpenStep() { diff --git a/packages/opencode/test/cli/run-accounting.test.ts b/packages/opencode/test/cli/run-accounting.test.ts index d71d1df01b..70fe948de5 100644 --- a/packages/opencode/test/cli/run-accounting.test.ts +++ b/packages/opencode/test/cli/run-accounting.test.ts @@ -81,14 +81,19 @@ describe("RunAccounting turn accounting", () => { expect(acc.turnCount).toBe(1) }) - test("a compaction step's overflow is not charged to an earlier unfinished step", () => { - const acc = RunAccounting.create() - acc.onAssistantMessage({ id: "msg_1", agent: "build" }) - acc.onAssistantMessage({ id: "msg_compact", agent: "compaction", summary: true }) - acc.onStepAttempt("msg_1") // interrupted, never finishes - acc.onStepAttempt("msg_compact") - acc.onSessionError("ContextOverflowError", "summary too long") - expect(acc.turnCount).toBe(0) + test("a step that compaction replaces is charged once, whichever event arrives first", () => { + for (const overflowFirst of [true, false]) { + const acc = RunAccounting.create() + acc.onAssistantMessage({ id: "msg_1", agent: "build" }) + acc.onAssistantMessage({ id: "msg_compact", agent: "compaction", summary: true }) + acc.onStepAttempt("msg_1") + if (overflowFirst) acc.onSessionError("ContextOverflowError", "too long") + acc.onStepAttempt("msg_compact") + if (!overflowFirst) acc.onSessionError("ContextOverflowError", "too long") + // The summary's own overflow is never charged. + acc.onSessionError("ContextOverflowError", "summary too long") + expect(acc.turnCount).toBe(1) + } }) test("after the session goes idle, a later pre-step overflow is not charged to an interrupted step", () => { diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index daccc32dac..19e4a36b34 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -50,7 +50,7 @@ describe("opencode run: --max-turns charges completed steps only", () => { ) // Attached-session fake: the CLI sees exactly the events the session publishes for // a step whose attempts start and fail, without waiting out real retry backoff. - const attached = (behavior: "all-attempts-fail" | "endless-attempts" | "compaction-after-budget" | "overflow-mid-step") => + const attached = (behavior: "all-attempts-fail" | "endless-attempts" | "compaction-after-budget" | "overflow-mid-step" | "retry-then-overflow") => Effect.gen(function* () { const sessionID = "ses_turn_budget_retry" let stream: ReadableStreamDefaultController | undefined @@ -150,6 +150,69 @@ describe("opencode run: --max-turns charges completed steps only", () => { publish("session.status", { sessionID, status: { type: "idle" } }) return Response.json({ info: { finish: "stop" } }) } + if (behavior === "retry-then-overflow") { + // Attempt 1 fails mid-stream; the retry is rejected for overflow before it + // streams; compaction runs; the continuation step answers. + startAttempt(messageID) + publish("session.status", { + sessionID, + status: { type: "retry", attempt: 1, message: "rate limit exceeded", next: Date.now() + 10 }, + }) + publish("session.error", { + sessionID, + error: { name: "ContextOverflowError", data: { message: "prompt is too long" } }, + }) + const summaryID = `${messageID}zz_summary` + publish("message.updated", { + info: { + id: summaryID, + sessionID, + role: "assistant", + parentID, + agent: "compaction", + summary: true, + modelID: "test-model", + providerID: "test", + time: { created: Date.now() }, + }, + }) + publish("message.part.updated", { + part: { id: `${summaryID}_start`, messageID: summaryID, sessionID, type: "step-start" }, + }) + publish("message.part.updated", { + part: { id: `${summaryID}_finish`, messageID: summaryID, sessionID, type: "step-finish", reason: "stop" }, + }) + publish("session.compacted", { sessionID }) + const nextID = `${messageID}zzz_next` + publish("message.updated", { + info: { + id: nextID, + sessionID, + role: "assistant", + parentID, + agent: "build", + modelID: "test-model", + providerID: "test", + time: { created: Date.now() }, + }, + }) + startAttempt(nextID) + publish("message.part.updated", { + part: { + id: `${nextID}_text`, + messageID: nextID, + sessionID, + type: "text", + text: "The answer after compaction.", + time: { start: 1, end: 2 }, + }, + }) + publish("message.part.updated", { + part: { id: `${nextID}_finish`, messageID: nextID, sessionID, type: "step-finish", reason: "stop" }, + }) + publish("session.status", { sessionID, status: { type: "idle" } }) + return Response.json({ info: { finish: "stop" } }) + } if (behavior === "overflow-mid-step") { // A step starts, then overflows; compaction runs and the work continues. startAttempt(messageID) @@ -299,6 +362,28 @@ describe("opencode run: --max-turns charges completed steps only", () => { 30_000, ) + cliIt.concurrent( + "a failed attempt closed by its retry is not charged for a later overflow", + ({ opencode }) => + Effect.gen(function* () { + const { server, sessionID, counts } = yield* attached("retry-then-overflow") + const result = yield* opencode.run("Answer once.", { + format: "json", + extraArgs: ["--attach", server.url.toString(), "--session", sessionID, "--max-turns", "1", "--no-trace"], + timeoutMs: 20_000, + bunRun: true, + }) + const events = opencode.parseJsonEvents(result.stdout) + expect(events.filter((event) => event.type === "budget_exhausted")).toHaveLength(0) + expect(counts.aborts).toBe(0) + expect( + events.filter((event) => event.type === "text").map((event) => (event.part as { text: string }).text), + ).toEqual(["The answer after compaction."]) + opencode.expectExit(result, 0) + }), + 30_000, + ) + cliIt.concurrent( "a step cut short by context overflow still costs its turn", ({ opencode }) => From db16e073ef65e598ab66f2530dc739a7bd85ea0a Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 09:45:45 -0800 Subject: [PATCH 20/20] test: share the fake's compaction-step and assistant-message helpers Co-Authored-By: Claude Opus 5.5 --- .../test/cli/run/turn-budget-retry.test.ts | 133 ++++++------------ 1 file changed, 44 insertions(+), 89 deletions(-) diff --git a/packages/opencode/test/cli/run/turn-budget-retry.test.ts b/packages/opencode/test/cli/run/turn-budget-retry.test.ts index 19e4a36b34..c19de2958f 100644 --- a/packages/opencode/test/cli/run/turn-budget-retry.test.ts +++ b/packages/opencode/test/cli/run/turn-budget-retry.test.ts @@ -61,6 +61,44 @@ describe("opencode run: --max-turns charges completed steps only", () => { const encoder = new TextEncoder() const publish = (type: string, properties: object) => stream?.enqueue(encoder.encode(`data: ${JSON.stringify({ type, properties })}\n\n`)) + const assistantMessage = (id: string, parentID: string) => { + publish("message.updated", { + info: { + id, + sessionID, + role: "assistant", + parentID, + agent: "build", + modelID: "test-model", + providerID: "test", + time: { created: Date.now() }, + }, + }) + return id + } + // A compaction summary message runs one completed step of its own. + const compactionStep = (messageID: string, parentID: string) => { + const summaryID = `${messageID}zz_summary` + publish("message.updated", { + info: { + id: summaryID, + sessionID, + role: "assistant", + parentID, + agent: "compaction", + summary: true, + modelID: "test-model", + providerID: "test", + time: { created: Date.now() }, + }, + }) + publish("message.part.updated", { + part: { id: `${summaryID}_start`, messageID: summaryID, sessionID, type: "step-start" }, + }) + publish("message.part.updated", { + part: { id: `${summaryID}_finish`, messageID: summaryID, sessionID, type: "step-finish", reason: "stop" }, + }) + } // The processor counts retries per assistant step, so the fake does too. const stepAttempts = new Map() const startAttempt = (messageID: string) => { @@ -162,40 +200,9 @@ describe("opencode run: --max-turns charges completed steps only", () => { sessionID, error: { name: "ContextOverflowError", data: { message: "prompt is too long" } }, }) - const summaryID = `${messageID}zz_summary` - publish("message.updated", { - info: { - id: summaryID, - sessionID, - role: "assistant", - parentID, - agent: "compaction", - summary: true, - modelID: "test-model", - providerID: "test", - time: { created: Date.now() }, - }, - }) - publish("message.part.updated", { - part: { id: `${summaryID}_start`, messageID: summaryID, sessionID, type: "step-start" }, - }) - publish("message.part.updated", { - part: { id: `${summaryID}_finish`, messageID: summaryID, sessionID, type: "step-finish", reason: "stop" }, - }) + compactionStep(messageID, parentID) publish("session.compacted", { sessionID }) - const nextID = `${messageID}zzz_next` - publish("message.updated", { - info: { - id: nextID, - sessionID, - role: "assistant", - parentID, - agent: "build", - modelID: "test-model", - providerID: "test", - time: { created: Date.now() }, - }, - }) + const nextID = assistantMessage(`${messageID}zzz_next`, parentID) startAttempt(nextID) publish("message.part.updated", { part: { @@ -220,43 +227,10 @@ describe("opencode run: --max-turns charges completed steps only", () => { sessionID, error: { name: "ContextOverflowError", data: { message: "prompt is too long" } }, }) - const summaryID = `${messageID}zz_summary` - publish("message.updated", { - info: { - id: summaryID, - sessionID, - role: "assistant", - parentID, - agent: "compaction", - summary: true, - modelID: "test-model", - providerID: "test", - time: { created: Date.now() }, - }, - }) - publish("message.part.updated", { - part: { id: `${summaryID}_start`, messageID: summaryID, sessionID, type: "step-start" }, - }) - publish("message.part.updated", { - part: { id: `${summaryID}_finish`, messageID: summaryID, sessionID, type: "step-finish", reason: "stop" }, - }) + compactionStep(messageID, parentID) publish("session.compacted", { sessionID }) - const nextID = `${messageID}zzz_next` - publish("message.updated", { - info: { - id: nextID, - sessionID, - role: "assistant", - parentID, - agent: "build", - modelID: "test-model", - providerID: "test", - time: { created: Date.now() }, - }, - }) - publish("message.part.updated", { - part: { id: `${nextID}_start`, messageID: nextID, sessionID, type: "step-start" }, - }) + const nextID = assistantMessage(`${messageID}zzz_next`, parentID) + startAttempt(nextID) return new Promise((resolve) => { finishPrompt = resolve }) @@ -278,26 +252,7 @@ describe("opencode run: --max-turns charges completed steps only", () => { part: { id: `${messageID}_finish`, messageID, sessionID, type: "step-finish", reason: "stop" }, }) // Compaction machinery then runs a step of its own; it is never charged or stopped. - const summaryID = `${messageID}zz_summary` - publish("message.updated", { - info: { - id: summaryID, - sessionID, - role: "assistant", - parentID, - agent: "compaction", - summary: true, - modelID: "test-model", - providerID: "test", - time: { created: Date.now() }, - }, - }) - publish("message.part.updated", { - part: { id: `${summaryID}_start`, messageID: summaryID, sessionID, type: "step-start" }, - }) - publish("message.part.updated", { - part: { id: `${summaryID}_finish`, messageID: summaryID, sessionID, type: "step-finish", reason: "stop" }, - }) + compactionStep(messageID, parentID) publish("session.status", { sessionID, status: { type: "idle" } }) return Response.json({ info: { finish: "stop" } }) }