From cd545487d03e6875ae2311813adf2c5ae6665df9 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 02:16:33 -0800 Subject: [PATCH 01/39] fix: compact instead of failing when the pre-send context check trips - Measure the request like the compaction trigger: provider-reported usage of the last turn plus the estimated tail (`reportedContextTokens`), capped by the character estimate. - A pre-send budget refusal on a compactable session becomes a context overflow, so the session compacts and retries; it stays fatal after a compaction or on a first turn. Closes #1806 Co-Authored-By: Claude Opus 5.5 --- docs/docs/configure/context-management.md | 6 + .../src/provider/output-token-budget.ts | 39 ++ packages/opencode/src/session/compaction.ts | 14 +- packages/opencode/src/session/llm.ts | 21 +- packages/opencode/src/session/processor.ts | 22 +- packages/opencode/src/session/prompt.ts | 25 + .../session/context-guard-compaction.test.ts | 429 ++++++++++++++++++ 7 files changed, 542 insertions(+), 14 deletions(-) create mode 100644 packages/opencode/test/session/context-guard-compaction.test.ts diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index 1d5424683d..e44d8fc66d 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -150,3 +150,9 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- !!! note "Limitations" The heuristic uses JavaScript string length (UTF-16 code units), which over-estimates tokens for emoji (2 code units but ~1-2 tokens) and CJK characters. For precise token counting, a future update will integrate a native tokenizer. + +## Pre-Send Size Check + +Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request fails with a clear message instead of a provider error. The estimator above runs high on SQL- and JSON-heavy context, so once the model has reported token usage for the conversation, the check measures the request the same way auto-compaction does: the reported usage of the last response plus an estimate of what was added since. A session the compaction threshold still considers in budget is sent normally. + +If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" when the request still does not fit after compaction, when there is nothing to compact yet (a first message that is already too large), or when auto-compaction is disabled. diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index 16d736468c..e9cd99328b 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -392,6 +392,45 @@ export function estimateInputTokens(input: { return total } +/** Provider-reported token usage of one finished assistant turn. */ +export type ReportedUsage = { + readonly total?: number + readonly input: number + readonly output: number + readonly cache: { readonly read: number; readonly write: number } +} + +/** + * The context size the compaction trigger compares against its threshold: provider-reported usage + * (exact) plus an estimated uncounted tail inflated by 1/fraction. The pre-send guard uses the same + * measure, so it cannot refuse a request the compaction trigger still considers in budget. + */ +export function reportedContextTokens(input: { + readonly tokens: ReportedUsage + readonly estimatedTokens?: number + readonly fraction: number +}): number { + const count = + input.tokens.total || input.tokens.input + input.tokens.output + input.tokens.cache.read + input.tokens.cache.write + const estimated = input.estimatedTokens ?? 0 + return estimated > 0 ? count + Math.ceil(estimated / input.fraction) : count +} + +/** + * Prefer the provider-reported size of the same conversation when it is smaller than the + * character estimate. The estimate runs high on SQL- and JSON-heavy context; reported usage plus + * an estimated tail is the measure the compaction trigger uses, so the guard and compaction agree. + */ +export function calibratedInputTokens(estimate: number, reported: number | undefined): number { + if (reported === undefined || !Number.isFinite(reported) || reported <= 0) return estimate + return Math.min(estimate, reported) +} + +/** True for the pre-send refusals that compacting the conversation can resolve. */ +export function isContextBudgetError(error: unknown): error is OutputTokenBudgetError | InputTokenBudgetError { + return error instanceof OutputTokenBudgetError || error instanceof InputTokenBudgetError +} + /** Resolve a direct or lazy input estimate after cheap no-op checks have passed. */ function resolveInputTokens(value: number | (() => number)): number { return typeof value === "function" ? value() : value diff --git a/packages/opencode/src/session/compaction.ts b/packages/opencode/src/session/compaction.ts index dc3b0fe277..73b9cbc0f8 100644 --- a/packages/opencode/src/session/compaction.ts +++ b/packages/opencode/src/session/compaction.ts @@ -14,6 +14,9 @@ import { Agent } from "@/agent/agent" import { Plugin } from "@/plugin" import { Config } from "@/config/config" import { ProviderTransform } from "@/provider/transform" +// altimate_change start — the context measure shared with the pre-send budget guard +import { reportedContextTokens } from "@/provider/output-token-budget" +// altimate_change end import { Telemetry } from "@/telemetry" // altimate_change — telemetry for compaction events import { ModelID, ProviderID } from "@/provider/schema" // altimate_change start — summarizer-integrity error @@ -223,17 +226,16 @@ export namespace SessionCompaction { const context = input.model.limit.context if (context === 0) return false - const count = - input.tokens.total || - input.tokens.input + input.tokens.output + input.tokens.cache.read + input.tokens.cache.write - const maxOutput = ProviderTransform.maxOutputTokens(input.model) const reserved = config.compaction?.reserved ?? COMPACTION_BUFFER const headroom = Math.max(reserved, maxOutput) const base = input.model.limit.input ?? context if (base <= headroom) return false - const estimated = input.estimatedTokens ?? 0 - const adjusted = estimated > 0 ? count + Math.ceil(estimated / contextSafetyFraction(config)) : count + const adjusted = reportedContextTokens({ + tokens: input.tokens, + estimatedTokens: input.estimatedTokens, + fraction: contextSafetyFraction(config), + }) const threshold = overflowThreshold({ base, headroom, fraction: 1 }) return adjusted >= threshold } diff --git a/packages/opencode/src/session/llm.ts b/packages/opencode/src/session/llm.ts index b806e6270c..5655f4cad7 100644 --- a/packages/opencode/src/session/llm.ts +++ b/packages/opencode/src/session/llm.ts @@ -15,6 +15,7 @@ import { mergeDeep, pipe } from "remeda" import { ProviderTransform } from "@/provider/transform" // altimate_change start — size and clamp the finalized provider request import { + calibratedInputTokens, clampOutputTokens, clampReasoningBudget, effectiveContextWindow, @@ -81,6 +82,9 @@ export namespace LLM { tools: Record retries?: number toolChoice?: "auto" | "required" | "none" + // altimate_change start — provider-reported size of this conversation (see reportedContextTokens) + reportedInputTokens?: number + // altimate_change end } export type StreamOutput = StreamTextResult @@ -358,13 +362,18 @@ export namespace LLM { // Provider defaults are lower precedence than the exact case-normalized outgoing record. headerSources: [provider.options, requestHeaders], }), + // The character estimate runs high on SQL/JSON; the provider-reported size of the same + // conversation (when known) keeps this guard on the measure the compaction trigger uses. inputTokens: () => - estimateInputTokens({ - system, - messages: ProviderTransform.messagesForInputEstimate(input.messages, input.model), - tools, - instructions: params.options.instructions, - }), + calibratedInputTokens( + estimateInputTokens({ + system, + messages: ProviderTransform.messagesForInputEstimate(input.messages, input.model), + tools, + instructions: params.options.instructions, + }), + input.reportedInputTokens, + ), }) // altimate_change start — deadline effort wins configured variants and plugin overrides const requestOptions = clampReasoningBudget( diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 02687babb4..673440dca8 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -20,6 +20,9 @@ import { Plugin } from "@/plugin" import type { Provider } from "@/provider/provider" import { LLM } from "./llm" import { Config } from "@/config/config" +// altimate_change start — recognize pre-send context budget refusals +import { isContextBudgetError } from "@/provider/output-token-budget" +// altimate_change end import { SessionCompaction } from "./compaction" import { PermissionNext } from "@/permission/next" import { Question } from "@/question" @@ -379,7 +382,15 @@ export namespace SessionProcessor { }, // altimate_change end // altimate_change start - opt-in text buffering for committed-answer echoes - async process({ deferText, ...streamInput }: LLM.StreamInput & { deferText?: MessageV2.TextPart[] }) { + async process({ + deferText, + compactable, + ...streamInput + }: LLM.StreamInput & { + deferText?: MessageV2.TextPart[] + // The prompt loop can compact this conversation (a finished non-summary turn exists). + compactable?: boolean + }) { // altimate_change end log.info("process") needsCompaction = false @@ -1327,7 +1338,14 @@ export namespace SessionProcessor { ...(traceId && { trace: traceId }), // altimate_change end }) - const error = MessageV2.fromError(e, { providerID: input.model.providerID }) + // altimate_change start — a pre-send budget refusal on a compactable conversation is a + // context overflow: compact and retry, as for a provider-reported overflow. Right after + // a compaction (or on a first turn) it stays the fatal budget error, so it cannot loop. + const error = + compactable && isContextBudgetError(e) + ? new MessageV2.ContextOverflowError({ message: e.message }, { cause: e }).toObject() + : MessageV2.fromError(e, { providerID: input.model.providerID }) + // altimate_change end // altimate_change start — committed echoes and summaries retain recoverable causes without a fatal event if ( (deferText || (input.recoverCommittedAnswer && input.assistantMessage.summary)) && diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 93187d0201..17bb5629a7 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -30,6 +30,9 @@ import { familyVendor } from "../provider/family" // altimate_change end import { type Tool as AITool, tool, jsonSchema, type ToolCallOptions, asSchema } from "ai" import { SessionCompaction } from "./compaction" +// altimate_change start — the context measure shared by compaction and the pre-send budget guard +import { reportedContextTokens } from "@/provider/output-token-budget" +// altimate_change end import { NudgeArbiter } from "./nudge" import { SessionTermination } from "./termination" import { Instance } from "../project/instance" @@ -2230,7 +2233,29 @@ export namespace SessionPrompt { // altimate_change start — only a request actually sent to the model counts toward the cap modelSteps++ // altimate_change end + // altimate_change start — the pre-send guard measures this request the way isOverflow does + // (provider-reported usage of the last turn on this model plus the estimated tail), and a + // guard refusal compacts whenever the proactive check above could have compacted. + // A provider that reports no usage leaves only the estimate: a tail alone would undercount. + const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined + const compactable = measured !== undefined + const reportedInputTokens = + measured && + measured.providerID === model.providerID && + measured.modelID === model.id && + reportedContextTokens({ tokens: measured.tokens, fraction: 1 }) > 0 + ? reportedContextTokens({ + tokens: measured.tokens, + estimatedTokens: uncountedTail, + fraction: SessionCompaction.contextSafetyFraction(await Config.get()), + }) + : undefined + // altimate_change end const result = await processor.process({ + // altimate_change start — see reportedInputTokens above + compactable, + reportedInputTokens, + // altimate_change end user: lastUser, // altimate_change start — correlate request-boundary snapshots with this assistant message messageID: processor.message.id, diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts new file mode 100644 index 0000000000..92e2f73872 --- /dev/null +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -0,0 +1,429 @@ +/** + * The pre-send context guard and the compaction trigger must agree. + * + * The guard (`clampOutputTokens`) refuses a request whose character-based size estimate leaves + * no room for a response; compaction (`SessionCompaction.isOverflow`) fires on provider-reported + * usage. The character estimate runs well above real token counts on SQL- and JSON-heavy context, + * so a session below the compaction threshold used to be refused by the guard and end with + * "Context budget exceeded before the request was sent" without compaction ever running. + */ +import { describe, expect, test } from "bun:test" +import path from "node:path" +import { Effect, Layer } from "effect" +import { LayerNode } from "@opencode-ai/core/effect/layer-node" +import { Session } from "@/session/session" +import { SessionPrompt } from "../../src/session/prompt" +import { SessionSummary } from "../../src/session/summary" +import { MessageID, PartID } from "../../src/session/schema" +import { Database } from "@opencode-ai/core/database/database" +import { SessionProjector } from "@opencode-ai/core/session/projector" +import { testEffect } from "../lib/effect" +import { TestLLMServer } from "../lib/llm-server" +import { provideTmpdirServerLegacy } from "./legacy-instance" +import { LSP } from "@/lsp/lsp" +import { MCP } from "../../src/mcp" +import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner" +import { RuntimeFlags } from "@/effect/runtime-flags" +import { + InputTokenBudgetError, + OutputTokenBudgetError, + ReasoningTokenBudgetError, + calibratedInputTokens, + estimateInputTokens, + isContextBudgetError, + reportedContextTokens, +} from "../../src/provider/output-token-budget" + +const mcp = Layer.succeed( + MCP.Service, + MCP.Service.of({ + status: () => Effect.succeed({}), + clients: () => Effect.succeed({}), + tools: () => Effect.succeed({}), + prompts: () => Effect.succeed({}), + resources: () => Effect.succeed({}), + add: () => Effect.succeed({ status: { status: "disabled" as const } }), + connect: () => Effect.void, + disconnect: () => Effect.void, + remove: () => Effect.void, + getPrompt: () => Effect.succeed(undefined), + readResource: () => Effect.succeed(undefined), + startAuth: () => Effect.die("unexpected MCP auth"), + authenticate: () => Effect.die("unexpected MCP auth"), + finishAuth: () => Effect.die("unexpected MCP auth"), + removeAuth: () => Effect.void, + supportsOAuth: () => Effect.succeed(false), + hasStoredTokens: () => Effect.succeed(false), + getAuthStatus: () => Effect.succeed("not_authenticated" as const), + entry: () => Effect.succeed(undefined), + listMeta: () => Effect.succeed(undefined), + snapshot: () => Effect.succeed({ tools: {}, meta: undefined }), + }), +) + +const lsp = Layer.succeed( + LSP.Service, + LSP.Service.of({ + init: () => Effect.void, + status: () => Effect.succeed([]), + hasClients: () => Effect.succeed(false), + touchFile: () => Effect.void, + diagnostics: () => Effect.succeed({}), + hover: () => Effect.succeed(undefined), + definition: () => Effect.succeed([]), + references: () => Effect.succeed([]), + implementation: () => Effect.succeed([]), + documentSymbol: () => Effect.succeed([]), + workspaceSymbol: () => Effect.succeed([]), + prepareCallHierarchy: () => Effect.succeed([]), + incomingCalls: () => Effect.succeed([]), + outgoingCalls: () => Effect.succeed([]), + }), +) + +const root = LayerNode.group([ + SessionPrompt.node, + Session.node, + SessionProjector.node, + SessionSummary.node, + Database.node, + CrossSpawnSpawner.node, + LayerNode.make(TestLLMServer.layer, []), +]) +const it = testEffect( + LayerNode.buildLayer(root, { + replacements: [ + LayerNode.replace(MCP.node, mcp), + LayerNode.replace(LSP.node, lsp), + LayerNode.replace(RuntimeFlags.node, RuntimeFlags.layer({ experimentalEventSystem: true })), + ], + }), +) + +const model = { providerID: "altimate-backend", modelID: "test-model" } as any + +/** A local OpenAI-compatible model with the given window. */ +const providerCfg = (limit: { context: number; output: number }) => (url: string) => ({ + provider: { + "altimate-backend": { + name: "Altimate (test)", + id: "altimate-backend", + env: [], + npm: "@ai-sdk/openai-compatible", + models: { + "test-model": { + id: "test-model", + name: "Test Model", + attachment: false, + reasoning: false, + temperature: false, + tool_call: true, + release_date: "2025-01-01", + limit, + cost: { input: 0, output: 0 }, + options: {}, + }, + }, + options: { apiKey: "test-key", baseURL: url }, + }, + }, + model: "altimate-backend/test-model", +}) + +const LARGE_WINDOW = { context: 131_072, output: 16_384 } +const SMALL_WINDOW = { context: 16_384, output: 4_096 } +// isOverflow's trigger for LARGE_WINDOW: context minus max(20_000 reserved, 16_384 output). +const LARGE_WINDOW_COMPACTION_THRESHOLD = 131_072 - 20_000 + +const SQL = [ + "with paid as (", + " select o.order_id, o.customer_id, o.created_at::date as order_date,", + " sum(li.amount_cents) filter (where li.status = 'paid') as paid_cents", + " from analytics.orders o", + " join analytics.line_items li on li.order_id = o.order_id", + " where o.created_at >= '2026-01-01' and o.region in ('emea', 'apac')", + " group by 1, 2, 3", + ")", + "select customer_id, count(*) as orders, sum(paid_cents) / 100.0 as revenue", + "from paid group by customer_id order by revenue desc limit 50;", +].join("\n") + +/** One query and its JSON result rows, the shape a data agent's history accumulates. */ +function sqlResultBlock(seed: number): string { + const rows = Array.from({ length: 25 }, (_, row) => ({ + customer_id: `cus_${(seed * 131 + row * 17).toString(36).padStart(8, "0")}`, + orders: (seed * 7 + row) % 97, + revenue: Number((((seed + 3) * (row + 11) * 37.13) % 100_000).toFixed(2)), + region: row % 2 === 0 ? "emea" : "apac", + first_order_date: `2026-0${(row % 9) + 1}-${String((seed % 27) + 1).padStart(2, "0")}`, + })) + return `Query ${seed}:\n${SQL}\nResult:\n${JSON.stringify(rows, null, 2)}\n` +} + +/** SQL/JSON history whose character estimate is at least `target` tokens. */ +function sqlJsonHistory(target: number): string { + const blocks: string[] = [] + let estimate = 0 + for (let seed = 0; estimate < target; seed++) { + const block = sqlResultBlock(seed) + blocks.push(block) + estimate += estimateInputTokens({ system: [], messages: [{ role: "user", content: block }] }) + } + return blocks.join("\n") +} + +/** Earlier turns (`user`, answered without usage), then the turn whose answer reported `assistantUsage`. */ +type Seeded = { user: string | string[]; assistantUsage?: number; next?: string } + +/** Seed [user history, finished assistant with provider usage, new user turn] and run the loop. */ +const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) => + Effect.gen(function* () { + const prompt = yield* SessionPrompt.Service + const sessions = yield* Session.Service + const session = yield* sessions.create({ + title: "context guard", + permission: [{ permission: "*", pattern: "*", action: "allow" }], + }) + const sessionID = session.id + const user = Effect.fnUntraced(function* (text: string) { + const msg = yield* sessions.updateMessage({ + id: MessageID.ascending(), + role: "user", + sessionID, + agent: "build", + model, + time: { created: Date.now() }, + } as any) + yield* sessions.updatePart({ id: PartID.ascending(), messageID: msg.id, sessionID, type: "text", text } as any) + return msg + }) + const answer = Effect.fnUntraced(function* (parentID: MessageID, usage: number) { + const msg = yield* sessions.updateMessage({ + id: MessageID.ascending(), + role: "assistant", + sessionID, + mode: "build", + agent: "build", + path: { cwd: dir, root: dir }, + cost: 0, + tokens: { input: usage, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, + modelID: model.modelID, + providerID: model.providerID, + parentID, + finish: "stop", + time: { created: Date.now(), completed: Date.now() }, + } as any) + yield* sessions.updatePart({ + id: PartID.ascending(), + messageID: msg.id, + sessionID, + type: "text", + text: "The top customers by paid revenue are listed above.", + } as any) + }) + const turns = Array.isArray(seeded.user) ? seeded.user : [seeded.user] + for (const [index, text] of turns.entries()) { + const msg = yield* user(text) + const last = index === turns.length - 1 + if (!last) yield* answer(msg.id, 0) + else if (seeded.assistantUsage !== undefined) yield* answer(msg.id, seeded.assistantUsage) + } + if (seeded.next !== undefined) yield* user(seeded.next) + + yield* prompt.loop({ sessionID }) + + const messages = yield* sessions.messages({ sessionID }) + const assistants = messages.filter((m) => m.info.role === "assistant").map((m) => m.info as any) + return { + messages, + hits: yield* llm.hits, + summaries: assistants.filter((info) => info.summary === true), + errors: assistants.filter((info) => info.error).map((info) => JSON.stringify(info.error)), + compactions: messages.filter((m) => m.parts.some((part) => part.type === "compaction")), + } + }) + +it.live( + "a SQL/JSON-heavy session below the compaction threshold is sent, not refused by the guard", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // The provider counted this conversation as 108k tokens, below the 111k compaction trigger. + // The character estimate of the SQL/JSON history plus the build agent's system prompt and + // tool schemas comes to roughly 130k+, past the guard's limit (131,072 less the 1,024-token + // response floor and a 2% margin): the ~20% overestimate seen on SQL/JSON context. + const history = sqlJsonHistory(88_000) + const reported = 108_000 + expect(reported).toBeLessThan(LARGE_WINDOW_COMPACTION_THRESHOLD) + + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { + user: history, + assistantUsage: reported, + next: "Summarize the revenue concentration.", + }) + + expect(result.errors).toEqual([]) + expect(result.hits.length).toBe(1) + expect(result.compactions.length).toBe(0) + // The output reservation was not squeezed by the inflated estimate either. + expect(result.hits[0]!.body.max_tokens).toBe(LARGE_WINDOW.output) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + +it.live( + "when the guard trips on a compactable session it compacts and then sends the request", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // A local server that reports no usage gives compaction nothing to fire on; the guard's + // estimate is then the only measure, and tripping it must compact rather than end the session. + const history = sqlJsonHistory(100_000) + + yield* llm.text("summary of the revenue analysis so far") + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { + user: history, + assistantUsage: 0, + next: "Summarize the revenue concentration.", + }) + + expect(result.errors).toEqual([]) + expect(result.summaries.length).toBe(1) + expect(result.hits.length).toBe(2) + expect(JSON.stringify(result.hits[1]!.body)).toContain("Summarize the revenue concentration.") + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + +it.live( + "a request that is still too big after compaction ends with the budget error, once", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // Project instructions alone exceed the window: compacting the conversation cannot help. + yield* Effect.promise(() => Bun.write(path.join(dir, "AGENTS.md"), sqlJsonHistory(140_000))) + yield* llm.text("summary of the earlier turn") + const result = yield* runSession(dir, llm, { + user: "List the top customers by paid revenue.", + assistantUsage: 0, + next: "Summarize the revenue concentration.", + }) + + // One compaction at most, then the budget error ends the turn instead of compacting again. + expect(result.summaries.length).toBeLessThanOrEqual(1) + expect(result.compactions.length).toBe(1) + expect(result.errors.length).toBeGreaterThanOrEqual(1) + expect(result.errors.at(-1)).toContain("Context budget exceeded before the request was sent") + expect(result.hits.every((hit) => !JSON.stringify(hit.body).includes("Summarize the revenue"))).toBe(true) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + +it.live( + "a first turn with no provider usage is sent when it fits", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { user: sqlJsonHistory(20_000) }) + + expect(result.errors).toEqual([]) + expect(result.hits.length).toBe(1) + expect(result.compactions.length).toBe(0) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + +it.live( + "a first turn too big for the window fails with the budget error without compacting", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + const result = yield* runSession(dir, llm, { user: sqlJsonHistory(100_000) }) + + expect(result.hits.length).toBe(0) + expect(result.compactions.length).toBe(0) + expect(result.errors.length).toBe(1) + expect(result.errors[0]).toContain("Context budget exceeded before the request was sent") + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + +it.live( + "a small-window model whose session fills the window compacts instead of failing", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // 16k context: the default 20k compaction reserve exceeds the window, so isOverflow never + // fires and the guard is the first check to notice the session is full. + yield* llm.text("summary of the revenue analysis so far") + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { + user: Array.from({ length: 6 }, () => sqlJsonHistory(2_500)), + assistantUsage: 15_800, + next: "Summarize the revenue concentration.", + }) + + expect(result.errors).toEqual([]) + expect(result.summaries.length).toBe(1) + expect(result.hits.length).toBe(2) + expect(JSON.stringify(result.hits[1]!.body)).toContain("Summarize the revenue concentration.") + }), + { + git: true, + // A tool-less agent: the build agent's tool schemas alone estimate above a 16k window. + config: (url: string) => ({ + ...providerCfg(SMALL_WINDOW)(url), + agent: { build: { permission: { "*": "deny" } } }, + }), + }, + ), + { timeout: 120_000 }, +) + +describe("guard calibration", () => { + test("uses the smaller provider-reported size, and the estimate when usage is missing or unusable", () => { + expect(calibratedInputTokens(128_000, 108_400)).toBe(108_400) + // Reported usage never raises the estimate: the guard stays at least as permissive as before. + expect(calibratedInputTokens(90_000, 108_400)).toBe(90_000) + for (const reported of [undefined, 0, -5, Number.NaN, Number.POSITIVE_INFINITY]) { + expect(calibratedInputTokens(128_000, reported)).toBe(128_000) + } + }) + + test("the shared measure is reported usage plus the tail inflated by the safety fraction", () => { + const tokens = { input: 100_000, output: 2_000, reasoning: 0, cache: { read: 6_000, write: 0 } } + expect(reportedContextTokens({ tokens, fraction: 0.65 })).toBe(108_000) + expect(reportedContextTokens({ tokens, estimatedTokens: 650, fraction: 0.65 })).toBe(109_000) + expect(reportedContextTokens({ tokens: { ...tokens, total: 50_000 }, fraction: 0.65 })).toBe(50_000) + }) + + test("only context-size refusals are compaction signals", () => { + const info = { modelID: "m", providerID: "p", inputTokens: 1, requested: 1, context: 1, floor: 1 } + expect(isContextBudgetError(new OutputTokenBudgetError(info))).toBe(true) + expect( + isContextBudgetError( + new InputTokenBudgetError({ modelID: "m", providerID: "p", inputTokens: 1, inputLimit: 1, margin: 1 }), + ), + ).toBe(true) + // A reasoning budget the model cannot honor is a configuration conflict: compacting cannot fix it. + expect( + isContextBudgetError( + new ReasoningTokenBudgetError({ path: "thinking.budgetTokens", configured: 1, maxOutputTokens: 1 }), + ), + ).toBe(false) + expect(isContextBudgetError(new Error("Context budget exceeded before the request was sent."))).toBe(false) + }) +}) From 3787e24648126cdd83dd4be36812de1fee399c65 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 02:51:49 -0800 Subject: [PATCH 02/39] fix: compact in place on a pre-send refusal; bound the calibrated measure - Compact the whole conversation (no replay) so a refusal mid-turn keeps that turn's steps. - Trust reported usage only for the same model and agent with input tokens, never below half the estimate. - With auto-compaction off a refusal stays the budget error. - Tests: model switch, auto-compaction off, mid-turn refusal. Co-Authored-By: Claude Opus 5.5 --- .../src/provider/output-token-budget.ts | 6 +- packages/opencode/src/session/processor.ts | 19 +++- packages/opencode/src/session/prompt.ts | 17 ++- .../altimate/learn/compaction-replay.test.ts | 1 + .../session/context-guard-compaction.test.ts | 107 ++++++++++++++++-- .../test/session/tools-result-errors.test.ts | 1 + 6 files changed, 129 insertions(+), 22 deletions(-) diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index e9cd99328b..ba09beded6 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -20,6 +20,8 @@ const VIDEO_TOKEN_ALLOWANCE = 8_192 const SEMANTIC_MEDIA_BYTES_PER_TOKEN = 64 const DATA_URL_HEADER_LIMIT = 1_024 const MIN_REASONING_BUDGET = 1_024 +/** Lowest share of the character estimate that provider-reported usage may calibrate the guard to. */ +const MIN_CALIBRATED_ESTIMATE_FRACTION = 0.5 const EMOJI = /\p{Extended_Pictographic}/u const DENSE_ASCII_CHARACTER = /[A-Za-z0-9+/_=-]/ const DENSE_ASCII_MIN_LENGTH = 32 @@ -423,7 +425,9 @@ export function reportedContextTokens(input: { */ export function calibratedInputTokens(estimate: number, reported: number | undefined): number { if (reported === undefined || !Number.isFinite(reported) || reported <= 0) return estimate - return Math.min(estimate, reported) + // Usage from a request that differed in ways the tail cannot see (new tools, partial reporting) + // may undercount; never let it lower the guard below this share of the estimate. + return Math.min(estimate, Math.max(reported, Math.ceil(estimate * MIN_CALIBRATED_ESTIMATE_FRACTION))) } /** True for the pre-send refusals that compacting the conversation can resolve. */ diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 673440dca8..c7eff1df8e 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -341,6 +341,9 @@ export namespace SessionProcessor { let blocked = false let attempt = 0 let needsCompaction = false + // altimate_change start — the pending compaction comes from a pre-send budget refusal (nothing was sent) + let refusedBeforeSend = false + // altimate_change end // altimate_change start — per-step generation telemetry let stepStartTime = Date.now() // altimate_change end @@ -357,6 +360,11 @@ export namespace SessionProcessor { get message() { return input.assistantMessage }, + // altimate_change start — see refusedBeforeSend above + get refusedBeforeSend() { + return refusedBeforeSend + }, + // altimate_change end // altimate_change start — disambiguate repeated concurrent tool-call ids partFromToolCall(toolCallID: string, identity: ToolCallIdentity = {}) { // Tool metadata may arrive out of order too. Skip an ambiguous update @@ -394,6 +402,9 @@ export namespace SessionProcessor { // altimate_change end log.info("process") needsCompaction = false + // altimate_change start — see refusedBeforeSend above + refusedBeforeSend = false + // altimate_change end // altimate_change start - let the prompt's exit guard publish a recovered compaction answer let recoveredCommittedAnswer = false // altimate_change end @@ -1341,10 +1352,10 @@ export namespace SessionProcessor { // altimate_change start — a pre-send budget refusal on a compactable conversation is a // context overflow: compact and retry, as for a provider-reported overflow. Right after // a compaction (or on a first turn) it stays the fatal budget error, so it cannot loop. - const error = - compactable && isContextBudgetError(e) - ? new MessageV2.ContextOverflowError({ message: e.message }, { cause: e }).toObject() - : MessageV2.fromError(e, { providerID: input.model.providerID }) + refusedBeforeSend = compactable === true && isContextBudgetError(e) + const error = refusedBeforeSend + ? new MessageV2.ContextOverflowError({ message: e.message }, { cause: e }).toObject() + : MessageV2.fromError(e, { providerID: input.model.providerID }) // altimate_change end // altimate_change start — committed echoes and summaries retain recoverable causes without a fatal event if ( diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 17bb5629a7..b514fa7b55 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2236,18 +2236,22 @@ export namespace SessionPrompt { // altimate_change start — the pre-send guard measures this request the way isOverflow does // (provider-reported usage of the last turn on this model plus the estimated tail), and a // guard refusal compacts whenever the proactive check above could have compacted. - // A provider that reports no usage leaves only the estimate: a tail alone would undercount. + // The reported usage applies only to the same model and agent (same tokenizer, system prompt + // and tools), and only when it counts input: a provider reporting none, or only output tokens, + // leaves the estimate in charge. With auto-compaction off a refusal stays the budget error. + const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined - const compactable = measured !== undefined + const compactable = measured !== undefined && loopConfig.compaction?.auto !== false const reportedInputTokens = measured && measured.providerID === model.providerID && measured.modelID === model.id && - reportedContextTokens({ tokens: measured.tokens, fraction: 1 }) > 0 + measured.agent === lastUser.agent && + (measured.tokens.input > 0 || measured.tokens.cache.read > 0 || measured.tokens.cache.write > 0) ? reportedContextTokens({ tokens: measured.tokens, estimatedTokens: uncountedTail, - fraction: SessionCompaction.contextSafetyFraction(await Config.get()), + fraction: SessionCompaction.contextSafetyFraction(loopConfig), }) : undefined // altimate_change end @@ -2667,7 +2671,10 @@ export namespace SessionPrompt { agent: lastUser.agent, model: lastUser.model, auto: true, - overflow: !processor.message.finish, + // altimate_change start — a pre-send refusal sent nothing: compact the whole conversation as the + // proactive check does, instead of replaying the last user message and dropping this turn's steps + overflow: !processor.message.finish && !processor.refusedBeforeSend, + // altimate_change end }) } continue diff --git a/packages/opencode/test/altimate/learn/compaction-replay.test.ts b/packages/opencode/test/altimate/learn/compaction-replay.test.ts index 46b45d0505..0e2289c1ac 100644 --- a/packages/opencode/test/altimate/learn/compaction-replay.test.ts +++ b/packages/opencode/test/altimate/learn/compaction-replay.test.ts @@ -42,6 +42,7 @@ for (const learnDelivery of [true, false]) { const unexpectedTool = () => { throw new Error("Unexpected tool execution during compaction") } const processor = spyOn(SessionProcessor, "create").mockImplementation((input) => ({ message: input.assistantMessage, + refusedBeforeSend: false, partFromToolCall: unexpectedTool, beginToolExecution: unexpectedTool, finishToolExecution: unexpectedTool, diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 92e2f73872..ee687017bd 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -172,8 +172,16 @@ function sqlJsonHistory(target: number): string { return blocks.join("\n") } -/** Earlier turns (`user`, answered without usage), then the turn whose answer reported `assistantUsage`. */ -type Seeded = { user: string | string[]; assistantUsage?: number; next?: string } +/** + * Earlier turns (`user`, answered without usage), then the turn whose answer reported + * `assistantUsage` (optionally from another model, as a mid-turn step, or with its own text). + */ +type Seeded = { + user: string | string[] + assistantUsage?: number + next?: string + last?: { modelID?: string; finish?: string; text?: string } +} /** Seed [user history, finished assistant with provider usage, new user turn] and run the loop. */ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) => @@ -197,7 +205,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) yield* sessions.updatePart({ id: PartID.ascending(), messageID: msg.id, sessionID, type: "text", text } as any) return msg }) - const answer = Effect.fnUntraced(function* (parentID: MessageID, usage: number) { + const answer = Effect.fnUntraced(function* (parentID: MessageID, usage: number, last: Seeded["last"] = {}) { const msg = yield* sessions.updateMessage({ id: MessageID.ascending(), role: "assistant", @@ -207,10 +215,10 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) path: { cwd: dir, root: dir }, cost: 0, tokens: { input: usage, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, - modelID: model.modelID, + modelID: last.modelID ?? model.modelID, providerID: model.providerID, parentID, - finish: "stop", + finish: last.finish ?? "stop", time: { created: Date.now(), completed: Date.now() }, } as any) yield* sessions.updatePart({ @@ -218,7 +226,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) messageID: msg.id, sessionID, type: "text", - text: "The top customers by paid revenue are listed above.", + text: last.text ?? "The top customers by paid revenue are listed above.", } as any) }) const turns = Array.isArray(seeded.user) ? seeded.user : [seeded.user] @@ -226,7 +234,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) const msg = yield* user(text) const last = index === turns.length - 1 if (!last) yield* answer(msg.id, 0) - else if (seeded.assistantUsage !== undefined) yield* answer(msg.id, seeded.assistantUsage) + else if (seeded.assistantUsage !== undefined) yield* answer(msg.id, seeded.assistantUsage, seeded.last) } if (seeded.next !== undefined) yield* user(seeded.next) @@ -315,12 +323,13 @@ it.live( next: "Summarize the revenue concentration.", }) - // One compaction at most, then the budget error ends the turn instead of compacting again. - expect(result.summaries.length).toBeLessThanOrEqual(1) + // One compaction (the summarizer request is the only one sent), then the budget error ends + // the turn instead of compacting again. expect(result.compactions.length).toBe(1) - expect(result.errors.length).toBeGreaterThanOrEqual(1) - expect(result.errors.at(-1)).toContain("Context budget exceeded before the request was sent") - expect(result.hits.every((hit) => !JSON.stringify(hit.body).includes("Summarize the revenue"))).toBe(true) + expect(result.summaries.length).toBe(1) + expect(result.hits.length).toBe(1) + expect(result.errors.length).toBe(1) + expect(result.errors[0]).toContain("Context budget exceeded before the request was sent") }), { git: true, config: providerCfg(LARGE_WINDOW) }, ), @@ -393,11 +402,85 @@ it.live( { timeout: 120_000 }, ) +it.live( + "usage reported by another model is not trusted: the guard trips and the session compacts", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // Same history and usage as the regression above, but counted by a different model's tokenizer. + yield* llm.text("summary of the revenue analysis so far") + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { + user: sqlJsonHistory(88_000), + assistantUsage: 108_000, + last: { modelID: "another-model" }, + next: "Summarize the revenue concentration.", + }) + + expect(result.errors).toEqual([]) + expect(result.summaries.length).toBe(1) + expect(result.hits.length).toBe(2) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + +it.live( + "with auto-compaction off a refusal stays the budget error", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + const result = yield* runSession(dir, llm, { + user: sqlJsonHistory(100_000), + assistantUsage: 0, + next: "Summarize the revenue concentration.", + }) + + expect(result.hits.length).toBe(0) + expect(result.compactions.length).toBe(0) + expect(result.errors.length).toBe(1) + expect(result.errors[0]).toContain("UnknownError") + expect(result.errors[0]).toContain("Context budget exceeded before the request was sent") + }), + { git: true, config: (url: string) => ({ ...providerCfg(LARGE_WINDOW)(url), compaction: { auto: false } }) }, + ), + { timeout: 120_000 }, +) + +it.live( + "a refusal in the middle of a turn summarizes that turn's steps instead of dropping them", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // [earlier turn] [current user turn] [a finished tool step of the current turn] -> refusal. + yield* llm.text("summary of the revenue analysis so far") + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { + user: ["List the top customers by paid revenue.", sqlJsonHistory(100_000)], + assistantUsage: 0, + last: { finish: "tool-calls", text: "Step result of the current turn: 42 customers above threshold." }, + }) + + expect(result.errors).toEqual([]) + expect(result.summaries.length).toBe(1) + // The current turn's step survives compaction (summarized, or kept in the verbatim tail) and + // reaches the retried request: compaction did not cut at the last user message and replay it. + expect(result.hits.length).toBe(2) + expect(JSON.stringify(result.hits[1]!.body)).toContain("Step result of the current turn") + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + describe("guard calibration", () => { test("uses the smaller provider-reported size, and the estimate when usage is missing or unusable", () => { expect(calibratedInputTokens(128_000, 108_400)).toBe(108_400) // Reported usage never raises the estimate: the guard stays at least as permissive as before. expect(calibratedInputTokens(90_000, 108_400)).toBe(90_000) + // Nor lowers it below half the estimate (partial usage, a request that changed since). + expect(calibratedInputTokens(128_000, 2_000)).toBe(64_000) for (const reported of [undefined, 0, -5, Number.NaN, Number.POSITIVE_INFINITY]) { expect(calibratedInputTokens(128_000, reported)).toBe(128_000) } diff --git a/packages/opencode/test/session/tools-result-errors.test.ts b/packages/opencode/test/session/tools-result-errors.test.ts index 61d6291ff4..ab087c8269 100644 --- a/packages/opencode/test/session/tools-result-errors.test.ts +++ b/packages/opencode/test/session/tools-result-errors.test.ts @@ -210,6 +210,7 @@ it.live("session/prompt resolveTools emits an SDK tool-error for a backend error cost: 0, tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, }, + refusedBeforeSend: false, beginToolExecution: (raw) => ({ raw, occurrence: 0 }), finishToolExecution: (execution) => { finished.push(execution) From 6bda81ffb41bf52914605a2163b880e7badf9214 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 03:03:23 -0800 Subject: [PATCH 03/39] fix: recoverable message on a compacting refusal; tighter usage floor - The overflow raised for a refusal that compacts says it is compacting and retrying, not the fatal advice. - Reported usage may lower the guard to no less than three quarters of the estimate. - Test usage reported for another agent; docs note when usage counts. Co-Authored-By: Claude Opus 5.5 --- docs/docs/configure/context-management.md | 2 +- .../src/provider/output-token-budget.ts | 10 +-- packages/opencode/src/session/processor.ts | 5 +- .../session/context-guard-compaction.test.ts | 63 ++++++++++--------- 4 files changed, 46 insertions(+), 34 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index e44d8fc66d..e5f07efc9d 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -153,6 +153,6 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- ## Pre-Send Size Check -Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request fails with a clear message instead of a provider error. The estimator above runs high on SQL- and JSON-heavy context, so once the model has reported token usage for the conversation, the check measures the request the same way auto-compaction does: the reported usage of the last response plus an estimate of what was added since. A session the compaction threshold still considers in budget is sent normally. +Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request fails with a clear message instead of a provider error. The estimator above runs high on SQL- and JSON-heavy context, so once the model has reported token usage for the conversation, the check measures the request the same way auto-compaction does: the reported usage of the last response plus an estimate of what was added since. A session the compaction threshold still considers in budget is sent normally. Usage reported for a different model or agent does not count; the estimate decides until the current model and agent report their own. If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" when the request still does not fit after compaction, when there is nothing to compact yet (a first message that is already too large), or when auto-compaction is disabled. diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index ba09beded6..080694542b 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -20,8 +20,12 @@ const VIDEO_TOKEN_ALLOWANCE = 8_192 const SEMANTIC_MEDIA_BYTES_PER_TOKEN = 64 const DATA_URL_HEADER_LIMIT = 1_024 const MIN_REASONING_BUDGET = 1_024 -/** Lowest share of the character estimate that provider-reported usage may calibrate the guard to. */ -const MIN_CALIBRATED_ESTIMATE_FRACTION = 0.5 +/** + * Lowest share of the character estimate that provider-reported usage may calibrate the guard to. + * The estimate runs ~15-20% high on SQL/JSON; the floor bounds an undercount from usage that missed + * inputs that changed since (per-step tool selection, refreshed rules, partial usage reporting). + */ +const MIN_CALIBRATED_ESTIMATE_FRACTION = 0.75 const EMOJI = /\p{Extended_Pictographic}/u const DENSE_ASCII_CHARACTER = /[A-Za-z0-9+/_=-]/ const DENSE_ASCII_MIN_LENGTH = 32 @@ -425,8 +429,6 @@ export function reportedContextTokens(input: { */ export function calibratedInputTokens(estimate: number, reported: number | undefined): number { if (reported === undefined || !Number.isFinite(reported) || reported <= 0) return estimate - // Usage from a request that differed in ways the tail cannot see (new tools, partial reporting) - // may undercount; never let it lower the guard below this share of the estimate. return Math.min(estimate, Math.max(reported, Math.ceil(estimate * MIN_CALIBRATED_ESTIMATE_FRACTION))) } diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index c7eff1df8e..fbd980d368 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -1354,7 +1354,10 @@ export namespace SessionProcessor { // a compaction (or on a first turn) it stays the fatal budget error, so it cannot loop. refusedBeforeSend = compactable === true && isContextBudgetError(e) const error = refusedBeforeSend - ? new MessageV2.ContextOverflowError({ message: e.message }, { cause: e }).toObject() + ? new MessageV2.ContextOverflowError( + { message: "The request would not fit the model's context window; compacting the conversation and retrying." }, + { cause: e }, + ).toObject() : MessageV2.fromError(e, { providerID: input.model.providerID }) // altimate_change end // altimate_change start — committed echoes and summaries retain recoverable causes without a fatal event diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index ee687017bd..f3426a2850 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -180,7 +180,7 @@ type Seeded = { user: string | string[] assistantUsage?: number next?: string - last?: { modelID?: string; finish?: string; text?: string } + last?: { modelID?: string; agent?: string; finish?: string; text?: string } } /** Seed [user history, finished assistant with provider usage, new user turn] and run the loop. */ @@ -210,8 +210,8 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) id: MessageID.ascending(), role: "assistant", sessionID, - mode: "build", - agent: "build", + mode: last.agent ?? "build", + agent: last.agent ?? "build", path: { cwd: dir, root: dir }, cost: 0, tokens: { input: usage, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, @@ -402,29 +402,36 @@ it.live( { timeout: 120_000 }, ) -it.live( - "usage reported by another model is not trusted: the guard trips and the session compacts", - () => - provideTmpdirServerLegacy( - Effect.fnUntraced(function* ({ dir, llm }) { - // Same history and usage as the regression above, but counted by a different model's tokenizer. - yield* llm.text("summary of the revenue analysis so far") - yield* llm.text("Revenue is concentrated in the top ten customers.") - const result = yield* runSession(dir, llm, { - user: sqlJsonHistory(88_000), - assistantUsage: 108_000, - last: { modelID: "another-model" }, - next: "Summarize the revenue concentration.", - }) - - expect(result.errors).toEqual([]) - expect(result.summaries.length).toBe(1) - expect(result.hits.length).toBe(2) - }), - { git: true, config: providerCfg(LARGE_WINDOW) }, - ), - { timeout: 120_000 }, -) +// Usage counted for another model (different tokenizer) or agent (different system prompt and tools) +// does not describe this request: the estimate decides, trips, and the session compacts. +for (const [name, last] of [ + ["another model", { modelID: "another-model" }], + ["another agent", { agent: "plan" }], +] as const) { + it.live( + `usage reported for ${name} is not trusted: the guard trips and the session compacts`, + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // Same history and usage as the regression above. + yield* llm.text("summary of the revenue analysis so far") + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { + user: sqlJsonHistory(88_000), + assistantUsage: 108_000, + last, + next: "Summarize the revenue concentration.", + }) + + expect(result.errors).toEqual([]) + expect(result.summaries.length).toBe(1) + expect(result.hits.length).toBe(2) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, + ) +} it.live( "with auto-compaction off a refusal stays the budget error", @@ -479,8 +486,8 @@ describe("guard calibration", () => { expect(calibratedInputTokens(128_000, 108_400)).toBe(108_400) // Reported usage never raises the estimate: the guard stays at least as permissive as before. expect(calibratedInputTokens(90_000, 108_400)).toBe(90_000) - // Nor lowers it below half the estimate (partial usage, a request that changed since). - expect(calibratedInputTokens(128_000, 2_000)).toBe(64_000) + // Nor lowers it below three quarters of the estimate (partial usage, a request that changed since). + expect(calibratedInputTokens(128_000, 2_000)).toBe(96_000) for (const reported of [undefined, 0, -5, Number.NaN, Number.POSITIVE_INFINITY]) { expect(calibratedInputTokens(128_000, reported)).toBe(128_000) } From 622f1a7276d70e1c619ded9b0cedddf3e4d82765 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 03:20:03 -0800 Subject: [PATCH 04/39] fix: address review of the context guard calibration - Skip calibration when uncounted attachments exist; floor at 60% of the estimate. - A refused attempt does not consume the agent's step cap. - The uncounted tail follows stored message order. - Resolve the safety fraction only when a tail estimate exists. - Docs: when usage applies, both refusal messages, output clamping. - Tests: attachment, step cap, real tool part in the mid-turn case. Co-Authored-By: Claude Opus 5.5 --- docs/docs/configure/context-management.md | 6 +- .../src/provider/output-token-budget.ts | 6 +- packages/opencode/src/session/compaction.ts | 3 +- packages/opencode/src/session/prompt.ts | 38 +++++-- .../session/context-guard-compaction.test.ts | 99 ++++++++++++++++++- 5 files changed, 135 insertions(+), 17 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index e5f07efc9d..0ad1d83f35 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -153,6 +153,8 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- ## Pre-Send Size Check -Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request fails with a clear message instead of a provider error. The estimator above runs high on SQL- and JSON-heavy context, so once the model has reported token usage for the conversation, the check measures the request the same way auto-compaction does: the reported usage of the last response plus an estimate of what was added since. A session the compaction threshold still considers in budget is sent normally. Usage reported for a different model or agent does not count; the estimate decides until the current model and agent report their own. +Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request is caught before it is sent instead of failing at the provider. When the prompt leaves less room than requested, the response reservation is reduced first; the request is refused only when too little room would remain. -If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" when the request still does not fit after compaction, when there is nothing to compact yet (a first message that is already too large), or when auto-compaction is disabled. +The estimator above runs high on SQL- and JSON-heavy context. When the last response came from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since (the measure auto-compaction uses), but never less than 60% of the full estimate. New attachments, a different model or agent, or the first request after a compaction fall back to the full estimate, so in those cases a request can still be refused, and compacted, below the compaction threshold. + +If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction, when there is nothing to compact yet (a first message that is already too large), or when auto-compaction is disabled. diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index 080694542b..6d299ddc32 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -22,10 +22,10 @@ const DATA_URL_HEADER_LIMIT = 1_024 const MIN_REASONING_BUDGET = 1_024 /** * Lowest share of the character estimate that provider-reported usage may calibrate the guard to. - * The estimate runs ~15-20% high on SQL/JSON; the floor bounds an undercount from usage that missed - * inputs that changed since (per-step tool selection, refreshed rules, partial usage reporting). + * The estimate runs ~15-20% high on SQL/JSON and higher on tool schemas; the floor bounds an + * undercount from usage that missed inputs changed since (per-step tool selection, refreshed rules). */ -const MIN_CALIBRATED_ESTIMATE_FRACTION = 0.75 +const MIN_CALIBRATED_ESTIMATE_FRACTION = 0.6 const EMOJI = /\p{Extended_Pictographic}/u const DENSE_ASCII_CHARACTER = /[A-Za-z0-9+/_=-]/ const DENSE_ASCII_MIN_LENGTH = 32 diff --git a/packages/opencode/src/session/compaction.ts b/packages/opencode/src/session/compaction.ts index 73b9cbc0f8..8ff30d9658 100644 --- a/packages/opencode/src/session/compaction.ts +++ b/packages/opencode/src/session/compaction.ts @@ -234,7 +234,8 @@ export namespace SessionCompaction { const adjusted = reportedContextTokens({ tokens: input.tokens, estimatedTokens: input.estimatedTokens, - fraction: contextSafetyFraction(config), + // Resolve the fraction only when an estimate needs it (an invalid override warns on each read). + fraction: (input.estimatedTokens ?? 0) > 0 ? contextSafetyFraction(config) : 1, }) const threshold = overflowThreshold({ base, headroom, fraction: 1 }) return adjusted >= threshold diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index b514fa7b55..6b2e3ba703 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -1588,7 +1588,14 @@ export namespace SessionPrompt { // counted, and one oversized output can jump the session past the window // between checks (a common failure mode for long headless runs). Estimate the // uncounted tail and include it. - const uncountedTail = estimateUncountedTail(msgs, lastFinished?.id) + // Messages after the last finished turn by stored order (caller-supplied IDs carry no chronology). + const measuredID = lastFinished?.id + const isAfterMeasured = (id: MessageID) => { + if (!measuredID) return false + const order = compareMessageOrder(messageOrder, id, measuredID) + return order !== 0 ? order > 0 : id > measuredID + } + const uncountedTail = estimateUncountedTail(msgs, measuredID, isAfterMeasured) if ( lastFinished && lastFinished.summary !== true && @@ -2242,8 +2249,20 @@ export namespace SessionPrompt { const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined const compactable = measured !== undefined && loopConfig.compaction?.auto !== false + // Attachments the provider has not counted (in the last turn's tool results or later messages) + // are not in the tail estimate: leave those requests to the estimate. + const unmeasuredMedia = msgs.some( + (msg) => + (msg.info.id === measuredID || isAfterMeasured(msg.info.id)) && + msg.parts.some( + (part) => + part.type === "file" || + (part.type === "tool" && part.state.status === "completed" && (part.state.attachments?.length ?? 0) > 0), + ), + ) const reportedInputTokens = measured && + !unmeasuredMedia && measured.providerID === model.providerID && measured.modelID === model.id && measured.agent === lastUser.agent && @@ -2251,7 +2270,7 @@ export namespace SessionPrompt { ? reportedContextTokens({ tokens: measured.tokens, estimatedTokens: uncountedTail, - fraction: SessionCompaction.contextSafetyFraction(loopConfig), + fraction: uncountedTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, }) : undefined // altimate_change end @@ -2313,6 +2332,9 @@ export namespace SessionPrompt { // altimate_change end }) + // altimate_change start — a pre-send refusal sent nothing: it does not consume the agent's step cap + if (processor.refusedBeforeSend) modelSteps-- + // altimate_change end // altimate_change start - finalize buffered echoes before publishing any text to clients const stepParts = MessageV2.parts(processor.message.id) // Capture before post-processing can fail or request compaction. @@ -3782,7 +3804,11 @@ export namespace SessionPrompt { * figure); its own text is not (already inside `tokens.output`). * Everything after it is counted in full. */ - export function estimateUncountedTail(msgs: MessageV2.WithParts[], lastFinishedID: MessageID | undefined): number { + export function estimateUncountedTail( + msgs: MessageV2.WithParts[], + lastFinishedID: MessageID | undefined, + isAfter: (id: MessageID) => boolean = (id) => id > lastFinishedID!, + ): number { if (!lastFinishedID) return 0 const lastFinished = msgs.find((m) => m.info.id === lastFinishedID) if (!lastFinished) return 0 @@ -3803,10 +3829,10 @@ export namespace SessionPrompt { if (part.type === "tool") tokens += Token.estimate(toolText(part)) } // filterCompacted deliberately reorders retained-tail and summary messages, - // so array position is not chronology. IDs are monotonic; select genuinely - // newer messages by ID regardless of their rendered position. + // so array position is not chronology. Select genuinely newer messages by + // the caller's stored order (default: monotonic IDs), not rendered position. for (const m of msgs) { - if (m.info.id <= lastFinishedID) continue + if (m.info.id === lastFinishedID || !isAfter(m.info.id)) continue for (const part of m.parts) { if (part.type === "text") tokens += Token.estimate(part.text ?? "") if (part.type === "tool") tokens += Token.estimate(toolText(part)) diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index f3426a2850..f59c2f7205 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -180,7 +180,9 @@ type Seeded = { user: string | string[] assistantUsage?: number next?: string - last?: { modelID?: string; agent?: string; finish?: string; text?: string } + last?: { modelID?: string; agent?: string; finish?: string; text?: string; toolOutput?: string } + /** A file attached to the `next` user turn. */ + nextFile?: boolean } /** Seed [user history, finished assistant with provider usage, new user turn] and run the loop. */ @@ -228,6 +230,24 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) type: "text", text: last.text ?? "The top customers by paid revenue are listed above.", } as any) + if (last.toolOutput !== undefined) { + yield* sessions.updatePart({ + id: PartID.ascending(), + messageID: msg.id, + sessionID, + type: "tool", + callID: "call_revenue_query", + tool: "bash", + state: { + status: "completed", + input: { command: "run the revenue query" }, + output: last.toolOutput, + title: "revenue query", + metadata: {}, + time: { start: Date.now(), end: Date.now() }, + }, + } as any) + } }) const turns = Array.isArray(seeded.user) ? seeded.user : [seeded.user] for (const [index, text] of turns.entries()) { @@ -236,7 +256,20 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) if (!last) yield* answer(msg.id, 0) else if (seeded.assistantUsage !== undefined) yield* answer(msg.id, seeded.assistantUsage, seeded.last) } - if (seeded.next !== undefined) yield* user(seeded.next) + if (seeded.next !== undefined) { + const next = yield* user(seeded.next) + if (seeded.nextFile) { + yield* sessions.updatePart({ + id: PartID.ascending(), + messageID: next.id, + sessionID, + type: "file", + mime: "text/plain", + filename: "orders.csv", + url: `data:text/plain;base64,${Buffer.from("order_id,amount\n1,10\n").toString("base64")}`, + } as any) + } + } yield* prompt.loop({ sessionID }) @@ -461,12 +494,17 @@ it.live( provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { // [earlier turn] [current user turn] [a finished tool step of the current turn] -> refusal. + // The step's completed tool result must survive compaction. yield* llm.text("summary of the revenue analysis so far") yield* llm.text("Revenue is concentrated in the top ten customers.") const result = yield* runSession(dir, llm, { user: ["List the top customers by paid revenue.", sqlJsonHistory(100_000)], assistantUsage: 0, - last: { finish: "tool-calls", text: "Step result of the current turn: 42 customers above threshold." }, + last: { + finish: "tool-calls", + text: "Running the revenue query.", + toolOutput: "Step result of the current turn: 42 customers above threshold.", + }, }) expect(result.errors).toEqual([]) @@ -481,13 +519,64 @@ it.live( { timeout: 120_000 }, ) +it.live( + "a new attachment keeps the estimate in charge: the guard trips and the session compacts", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // Same as the regression above, but the new turn attaches a file the tail estimate cannot see. + yield* llm.text("summary of the revenue analysis so far") + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { + user: sqlJsonHistory(88_000), + assistantUsage: 108_000, + next: "Summarize the revenue concentration.", + nextFile: true, + }) + + expect(result.errors).toEqual([]) + expect(result.summaries.length).toBe(1) + expect(result.hits.length).toBe(2) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + +it.live( + "a refused attempt does not consume the agent's step cap", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // steps: 2. Counting the refused attempt would make the retry the last step: tools removed + // and a final answer forced before any work. + yield* llm.text("summary of the revenue analysis so far") + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { + user: sqlJsonHistory(100_000), + assistantUsage: 0, + next: "Summarize the revenue concentration.", + }) + + expect(result.errors).toEqual([]) + expect(result.summaries.length).toBe(1) + expect(result.hits.length).toBe(2) + const retry = result.hits[1]!.body as { tools?: unknown[] } + expect(retry.tools?.length ?? 0).toBeGreaterThan(0) + expect(JSON.stringify(retry)).not.toContain("maximum number of steps") + }), + { git: true, config: (url: string) => ({ ...providerCfg(LARGE_WINDOW)(url), agent: { build: { steps: 2 } } }) }, + ), + { timeout: 120_000 }, +) + describe("guard calibration", () => { test("uses the smaller provider-reported size, and the estimate when usage is missing or unusable", () => { expect(calibratedInputTokens(128_000, 108_400)).toBe(108_400) // Reported usage never raises the estimate: the guard stays at least as permissive as before. expect(calibratedInputTokens(90_000, 108_400)).toBe(90_000) - // Nor lowers it below three quarters of the estimate (partial usage, a request that changed since). - expect(calibratedInputTokens(128_000, 2_000)).toBe(96_000) + // Nor lowers it below 60% of the estimate (partial usage, a request that changed since). + expect(calibratedInputTokens(128_000, 2_000)).toBe(76_800) for (const reported of [undefined, 0, -5, Number.NaN, Number.POSITIVE_INFINITY]) { expect(calibratedInputTokens(128_000, reported)).toBe(128_000) } From 4bfda4d7e156e4a5076d71515e508f1c7a080d00 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 03:23:18 -0800 Subject: [PATCH 05/39] fix: replay the prompt on a turn-start refusal; keep echo recovery - A refusal on the turn's first request replays the user's prompt verbatim; only mid-turn refusals compact without replay. - A refusal while echoing a committed answer uses the echo recovery even when the session cannot compact. - Drop a test assertion that compared two constants. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/session/processor.ts | 9 ++++++++- packages/opencode/src/session/prompt.ts | 8 +++++--- .../test/session/context-guard-compaction.test.ts | 12 ++++++------ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index fbd980d368..2344be8570 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -1352,8 +1352,15 @@ export namespace SessionProcessor { // altimate_change start — a pre-send budget refusal on a compactable conversation is a // context overflow: compact and retry, as for a provider-reported overflow. Right after // a compaction (or on a first turn) it stays the fatal budget error, so it cannot loop. + // A committed-answer echo recovers from an overflow without compacting (the arm below), so a + // refusal there converts whether or not the conversation could be compacted. + const echoRecovery = + (deferText !== undefined || (input.recoverCommittedAnswer && input.assistantMessage.summary)) && + !input.abort.aborted && + input.assistantMessage.finish !== "content-filter" refusedBeforeSend = compactable === true && isContextBudgetError(e) - const error = refusedBeforeSend + const error = + refusedBeforeSend || (echoRecovery && isContextBudgetError(e)) ? new MessageV2.ContextOverflowError( { message: "The request would not fit the model's context window; compacting the conversation and retrying." }, { cause: e }, diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 6b2e3ba703..5131dd2642 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2693,9 +2693,11 @@ export namespace SessionPrompt { agent: lastUser.agent, model: lastUser.model, auto: true, - // altimate_change start — a pre-send refusal sent nothing: compact the whole conversation as the - // proactive check does, instead of replaying the last user message and dropping this turn's steps - overflow: !processor.message.finish && !processor.refusedBeforeSend, + // altimate_change start — a pre-send refusal mid-turn compacts the whole conversation, as the + // proactive check does, so this turn's steps are kept; on the turn's first request the user's + // prompt is replayed verbatim, as for a provider overflow + overflow: + !processor.message.finish && (!processor.refusedBeforeSend || lastFinished?.parentID !== lastUser.id), // altimate_change end }) } diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index f59c2f7205..8e0cd49a9d 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -132,8 +132,6 @@ const providerCfg = (limit: { context: number; output: number }) => (url: string const LARGE_WINDOW = { context: 131_072, output: 16_384 } const SMALL_WINDOW = { context: 16_384, output: 4_096 } -// isOverflow's trigger for LARGE_WINDOW: context minus max(20_000 reserved, 16_384 output). -const LARGE_WINDOW_COMPACTION_THRESHOLD = 131_072 - 20_000 const SQL = [ "with paid as (", @@ -289,13 +287,12 @@ it.live( () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { - // The provider counted this conversation as 108k tokens, below the 111k compaction trigger. + // The provider counted this conversation as 108k tokens, below the ~111k compaction trigger. // The character estimate of the SQL/JSON history plus the build agent's system prompt and - // tool schemas comes to roughly 130k+, past the guard's limit (131,072 less the 1,024-token - // response floor and a 2% margin): the ~20% overestimate seen on SQL/JSON context. + // tool schemas is far higher (~150k here), past the guard's limit (131,072 less the 1,024-token + // response floor and a 2% margin). Without calibration this request is refused. const history = sqlJsonHistory(88_000) const reported = 108_000 - expect(reported).toBeLessThan(LARGE_WINDOW_COMPACTION_THRESHOLD) yield* llm.text("Revenue is concentrated in the top ten customers.") const result = yield* runSession(dir, llm, { @@ -335,6 +332,9 @@ it.live( expect(result.errors).toEqual([]) expect(result.summaries.length).toBe(1) expect(result.hits.length).toBe(2) + // Refused on the turn's first request: the prompt is replayed verbatim after the summary, + // not paraphrased into it. + expect(JSON.stringify(result.hits[0]!.body)).not.toContain("Summarize the revenue concentration.") expect(JSON.stringify(result.hits[1]!.body)).toContain("Summarize the revenue concentration.") }), { git: true, config: providerCfg(LARGE_WINDOW) }, From 2f94162c08cd485e487d06c454b1582e312dbf0e Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 03:25:18 -0800 Subject: [PATCH 06/39] fix: size the unmeasured tail with the guard's conservative estimator The calibration tail now counts multilingual and dense text the way the guard does, so content the provider never measured is not discounted. Co-Authored-By: Claude Opus 5.5 --- .../src/provider/output-token-budget.ts | 2 +- packages/opencode/src/session/prompt.ts | 16 ++++++---- .../session/context-guard-compaction.test.ts | 30 +++++++++++++++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index 6d299ddc32..3478246982 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -178,7 +178,7 @@ function denseAsciiCharacters(input: string): number { } /** Estimate heterogeneous text in small chunks and conservatively count dense or non-ASCII text. */ -function estimateTextTokens(input: string): number { +export function estimateTextTokens(input: string): number { let total = 0 for (let offset = 0; offset < input.length; offset += ESTIMATE_CHUNK_SIZE) { const chunk = input.slice(offset, offset + ESTIMATE_CHUNK_SIZE) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 5131dd2642..7ecbe67d39 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -31,7 +31,7 @@ import { familyVendor } from "../provider/family" import { type Tool as AITool, tool, jsonSchema, type ToolCallOptions, asSchema } from "ai" import { SessionCompaction } from "./compaction" // altimate_change start — the context measure shared by compaction and the pre-send budget guard -import { reportedContextTokens } from "@/provider/output-token-budget" +import { estimateTextTokens, reportedContextTokens } from "@/provider/output-token-budget" // altimate_change end import { NudgeArbiter } from "./nudge" import { SessionTermination } from "./termination" @@ -2260,6 +2260,7 @@ export namespace SessionPrompt { (part.type === "tool" && part.state.status === "completed" && (part.state.attachments?.length ?? 0) > 0), ), ) + const guardTail = measured ? estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens) : 0 const reportedInputTokens = measured && !unmeasuredMedia && @@ -2269,8 +2270,10 @@ export namespace SessionPrompt { (measured.tokens.input > 0 || measured.tokens.cache.read > 0 || measured.tokens.cache.write > 0) ? reportedContextTokens({ tokens: measured.tokens, - estimatedTokens: uncountedTail, - fraction: uncountedTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, + // Unmeasured text is sized with the guard's own conservative (multilingual, dense-text) + // estimator, so the discount applies only to what the provider has counted. + estimatedTokens: guardTail, + fraction: guardTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, }) : undefined // altimate_change end @@ -3810,6 +3813,7 @@ export namespace SessionPrompt { msgs: MessageV2.WithParts[], lastFinishedID: MessageID | undefined, isAfter: (id: MessageID) => boolean = (id) => id > lastFinishedID!, + estimate: (text: string) => number = Token.estimate, ): number { if (!lastFinishedID) return 0 const lastFinished = msgs.find((m) => m.info.id === lastFinishedID) @@ -3828,7 +3832,7 @@ export namespace SessionPrompt { } let tokens = 0 for (const part of lastFinished.parts) { - if (part.type === "tool") tokens += Token.estimate(toolText(part)) + if (part.type === "tool") tokens += estimate(toolText(part)) } // filterCompacted deliberately reorders retained-tail and summary messages, // so array position is not chronology. Select genuinely newer messages by @@ -3836,8 +3840,8 @@ export namespace SessionPrompt { for (const m of msgs) { if (m.info.id === lastFinishedID || !isAfter(m.info.id)) continue for (const part of m.parts) { - if (part.type === "text") tokens += Token.estimate(part.text ?? "") - if (part.type === "tool") tokens += Token.estimate(toolText(part)) + if (part.type === "text") tokens += estimate(part.text ?? "") + if (part.type === "tool") tokens += estimate(toolText(part)) } } return tokens diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 8e0cd49a9d..ff53e83d5d 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -13,6 +13,8 @@ import { Effect, Layer } from "effect" import { LayerNode } from "@opencode-ai/core/effect/layer-node" import { Session } from "@/session/session" import { SessionPrompt } from "../../src/session/prompt" +import type { MessageV2 } from "../../src/session/message-v2" +import { Token } from "../../src/util/token" import { SessionSummary } from "../../src/session/summary" import { MessageID, PartID } from "../../src/session/schema" import { Database } from "@opencode-ai/core/database/database" @@ -30,6 +32,7 @@ import { ReasoningTokenBudgetError, calibratedInputTokens, estimateInputTokens, + estimateTextTokens, isContextBudgetError, reportedContextTokens, } from "../../src/provider/output-token-budget" @@ -606,3 +609,30 @@ describe("guard calibration", () => { expect(isContextBudgetError(new Error("Context budget exceeded before the request was sent."))).toBe(false) }) }) + +describe("calibration tail", () => { + const text = (id: string, role: "user" | "assistant", body: string) => + ({ + info: { id, sessionID: "s", role, time: { created: 1 } }, + parts: [{ id: `${id}-p`, sessionID: "s", messageID: id, type: "text", text: body }], + }) as unknown as MessageV2.WithParts + + test("unmeasured multilingual text is sized with the guard's conservative estimator", () => { + const cjk = "\u6536\u5165\u96c6\u4e2d\u5728\u524d\u5341\u540d\u5ba2\u6237".repeat(5_000) + const msgs = [text("m1", "user", "task"), text("m2", "assistant", "done"), text("m3", "user", cjk)] + const plain = SessionPrompt.estimateUncountedTail(msgs, "m2" as any) + const guard = SessionPrompt.estimateUncountedTail(msgs, "m2" as any, undefined, estimateTextTokens) + expect(plain).toBe(Token.estimate(cjk)) + // At least one token per non-ASCII character, where the plain ratio gives ~1 per 3.7. + expect(guard).toBeGreaterThanOrEqual(cjk.length) + }) + + test("follows the caller's stored order, not lexical IDs", () => { + // A later message with a lexically smaller caller-supplied ID. + const msgs = [text("m5", "user", "task"), text("m6", "assistant", "done"), text("a1", "user", "y".repeat(9_000))] + expect(SessionPrompt.estimateUncountedTail(msgs, "m6" as any)).toBe(0) + expect(SessionPrompt.estimateUncountedTail(msgs, "m6" as any, (id) => id === "a1")).toBe( + Token.estimate("y".repeat(9_000)), + ) + }) +}) From a425fe907a60fe21a3c4d618c58d4457798892ad Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 03:29:24 -0800 Subject: [PATCH 07/39] fix: treat an input-plus-reservation provider error as an overflow If calibration undercounts and the provider rejects input plus max_tokens over the window, the session now compacts reactively instead of ending. Log guard refusals that compact. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/provider/error.ts | 4 ++++ packages/opencode/src/session/processor.ts | 6 ++++++ .../session/context-guard-compaction.test.ts | 16 +++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/provider/error.ts b/packages/opencode/src/provider/error.ts index 67bb6237cf..ce8ba2c51a 100644 --- a/packages/opencode/src/provider/error.ts +++ b/packages/opencode/src/provider/error.ts @@ -60,6 +60,10 @@ export namespace ProviderError { /request entity too large/i, // HTTP 413 /the request was too long/i, // Azure OpenAI /maximum tokens for requested operation/i, // Azure OpenAI + // altimate_change start — input plus the output reservation over the window is an overflow too: + // compaction makes room for the reservation + /input length and `?max_tokens`? exceed context limit/i, + // altimate_change end ] function isOpenAiErrorRetryable(e: APICallError) { diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 2344be8570..7e16f82b42 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -1359,6 +1359,12 @@ export namespace SessionProcessor { !input.abort.aborted && input.assistantMessage.finish !== "content-filter" refusedBeforeSend = compactable === true && isContextBudgetError(e) + if (refusedBeforeSend) + log.warn("pre-send context budget refusal; compacting before retrying", { + sessionID: input.sessionID, + providerID: input.model.providerID, + modelID: input.model.id, + }) const error = refusedBeforeSend || (echoRecovery && isContextBudgetError(e)) ? new MessageV2.ContextOverflowError( diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index ff53e83d5d..dabfc5ade7 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -8,12 +8,13 @@ * "Context budget exceeded before the request was sent" without compaction ever running. */ import { describe, expect, test } from "bun:test" +import { APICallError } from "ai" +import { MessageV2 } from "../../src/session/message-v2" import path from "node:path" import { Effect, Layer } from "effect" import { LayerNode } from "@opencode-ai/core/effect/layer-node" import { Session } from "@/session/session" import { SessionPrompt } from "../../src/session/prompt" -import type { MessageV2 } from "../../src/session/message-v2" import { Token } from "../../src/util/token" import { SessionSummary } from "../../src/session/summary" import { MessageID, PartID } from "../../src/session/schema" @@ -636,3 +637,16 @@ describe("calibration tail", () => { ) }) }) + +test("an input-plus-reservation overflow from the provider is a context overflow (reactive compaction)", () => { + const error = new APICallError({ + message: + "input length and `max_tokens` exceed context limit: 188240 + 21333 > 200000, decrease input length or `max_tokens` and try again", + url: "https://example.com", + requestBodyValues: {}, + statusCode: 400, + responseHeaders: { "content-type": "application/json" }, + isRetryable: false, + }) + expect(MessageV2.ContextOverflowError.isInstance(MessageV2.fromError(error, { providerID: "p" as any }))).toBe(true) +}) From 761bd28942eb09e8860c23fdcc159640f0a55def Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 03:41:13 -0800 Subject: [PATCH 08/39] fix: keep media turns explicit; environment-independent guard tests - A refusal on a turn carrying an image or PDF stays the budget error: compaction would drop it. - The compacting message is used only when compacting; echo recovery keeps the guard's text. - A refusal that compacts hands its run-mode directive to the retry. - Tests: tool-less agent and multi-turn histories, real PNG fixture. Co-Authored-By: Claude Opus 5.5 --- docs/docs/configure/context-management.md | 4 +- packages/opencode/src/session/processor.ts | 16 ++- packages/opencode/src/session/prompt.ts | 7 +- .../session/context-guard-compaction.test.ts | 121 ++++++++++-------- 4 files changed, 88 insertions(+), 60 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index 0ad1d83f35..4a8b47ad2d 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -155,6 +155,6 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request is caught before it is sent instead of failing at the provider. When the prompt leaves less room than requested, the response reservation is reduced first; the request is refused only when too little room would remain. -The estimator above runs high on SQL- and JSON-heavy context. When the last response came from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since (the measure auto-compaction uses), but never less than 60% of the full estimate. New attachments, a different model or agent, or the first request after a compaction fall back to the full estimate, so in those cases a request can still be refused, and compacted, below the compaction threshold. +The estimator above runs high on SQL- and JSON-heavy context. When the last response came from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since (the measure auto-compaction uses), but never less than 60% of the full estimate. New attachments or a different model or agent fall back to the full estimate, so in those cases a request can still be refused below the compaction threshold. -If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction, when there is nothing to compact yet (a first message that is already too large), or when auto-compaction is disabled. +If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when the current message carries an image or PDF (compaction would drop it), or when auto-compaction is disabled. diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 7e16f82b42..ef82a8b4e3 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -462,9 +462,13 @@ export namespace SessionProcessor { // produce a summary and cannot act on it, so the real working turn right // after compaction would silently never see the breaker/loop nudge. let effectiveStreamInput = streamInput + // altimate_change start — kept so a pre-send refusal can hand the directive to the retry + let takenDirective: ReturnType + // altimate_change end if (runMode && !input.assistantMessage.summary) { const directive = NudgeArbiter.take(input.sessionID, input.nudgeGeneration) // altimate_change end + takenDirective = directive // altimate_change — see takenDirective above if (directive) { // Attribute the injection to the DIRECTIVE that won arbitration, // not a hardcoded "nudge" — otherwise every injected doom-loop @@ -1359,19 +1363,23 @@ export namespace SessionProcessor { !input.abort.aborted && input.assistantMessage.finish !== "content-filter" refusedBeforeSend = compactable === true && isContextBudgetError(e) - if (refusedBeforeSend) + if (refusedBeforeSend) { log.warn("pre-send context budget refusal; compacting before retrying", { sessionID: input.sessionID, providerID: input.model.providerID, modelID: input.model.id, }) - const error = - refusedBeforeSend || (echoRecovery && isContextBudgetError(e)) + // Nothing was sent: the directive this request carried belongs to the retry. + if (takenDirective) NudgeArbiter.register(input.sessionID, takenDirective, input.nudgeGeneration) + } + const error = refusedBeforeSend ? new MessageV2.ContextOverflowError( { message: "The request would not fit the model's context window; compacting the conversation and retrying." }, { cause: e }, ).toObject() - : MessageV2.fromError(e, { providerID: input.model.providerID }) + : echoRecovery && isContextBudgetError(e) + ? new MessageV2.ContextOverflowError({ message: e.message }, { cause: e }).toObject() + : MessageV2.fromError(e, { providerID: input.model.providerID }) // altimate_change end // altimate_change start — committed echoes and summaries retain recoverable causes without a fatal event if ( diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 7ecbe67d39..12fcaacac1 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2248,7 +2248,12 @@ export namespace SessionPrompt { // leaves the estimate in charge. With auto-compaction off a refusal stays the budget error. const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined - const compactable = measured !== undefined && loopConfig.compaction?.auto !== false + // Compaction drops images and PDFs (replay swaps them for a placeholder, summaries strip them): + // a refusal on a turn that carries one stays the budget error rather than answer without it. + const turnMedia = msgs + .find((msg) => msg.info.id === lastUser.id) + ?.parts.some((part) => part.type === "file" && MessageV2.isMedia(part.mime)) + const compactable = measured !== undefined && loopConfig.compaction?.auto !== false && !turnMedia // Attachments the provider has not counted (in the last turn's tool results or later messages) // are not in the tail estimate: leave those requests to the estimate. const unmeasuredMedia = msgs.some( diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index dabfc5ade7..eee0e69260 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -106,35 +106,44 @@ const it = testEffect( const model = { providerID: "altimate-backend", modelID: "test-model" } as any -/** A local OpenAI-compatible model with the given window. */ -const providerCfg = (limit: { context: number; output: number }) => (url: string) => ({ - provider: { - "altimate-backend": { - name: "Altimate (test)", - id: "altimate-backend", - env: [], - npm: "@ai-sdk/openai-compatible", - models: { - "test-model": { - id: "test-model", - name: "Test Model", - attachment: false, - reasoning: false, - temperature: false, - tool_call: true, - release_date: "2025-01-01", - limit, - cost: { input: 0, output: 0 }, - options: {}, +/** + * A local OpenAI-compatible model with the given window, driven by a tool-less build agent unless + * `tools` is set. The build agent's system prompt and tool schemas vary by environment (tens of + * thousands of estimated tokens); without them each fixture's history alone decides the guard. + */ +const providerCfg = + (limit: { context: number; output: number }, opts: { tools?: boolean; attachment?: boolean } = {}) => + (url: string) => ({ + ...(opts.tools ? {} : { agent: { build: { permission: { "*": "deny" as const } } } }), + provider: { + "altimate-backend": { + name: "Altimate (test)", + id: "altimate-backend", + env: [], + npm: "@ai-sdk/openai-compatible", + models: { + "test-model": { + id: "test-model", + name: "Test Model", + attachment: opts.attachment === true, + reasoning: false, + temperature: false, + tool_call: true, + release_date: "2025-01-01", + limit, + cost: { input: 0, output: 0 }, + options: {}, + }, }, + options: { apiKey: "test-key", baseURL: url }, }, - options: { apiKey: "test-key", baseURL: url }, }, - }, - model: "altimate-backend/test-model", -}) + model: "altimate-backend/test-model", + }) const LARGE_WINDOW = { context: 131_072, output: 16_384 } +/** A 1x1 PNG. */ +const PNG_BASE64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==" const SMALL_WINDOW = { context: 16_384, output: 4_096 } const SQL = [ @@ -162,6 +171,9 @@ function sqlResultBlock(seed: number): string { return `Query ${seed}:\n${SQL}\nResult:\n${JSON.stringify(rows, null, 2)}\n` } +/** Six user turns of SQL/JSON history totalling at least `target` estimated tokens (compaction trims whole turns). */ +const sqlJsonTurns = (target: number) => Array.from({ length: 6 }, () => sqlJsonHistory(Math.ceil(target / 6))) + /** SQL/JSON history whose character estimate is at least `target` tokens. */ function sqlJsonHistory(target: number): string { const blocks: string[] = [] @@ -183,7 +195,7 @@ type Seeded = { assistantUsage?: number next?: string last?: { modelID?: string; agent?: string; finish?: string; text?: string; toolOutput?: string } - /** A file attached to the `next` user turn. */ + /** An image attached to the `next` user turn. */ nextFile?: boolean } @@ -266,9 +278,9 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) messageID: next.id, sessionID, type: "file", - mime: "text/plain", - filename: "orders.csv", - url: `data:text/plain;base64,${Buffer.from("order_id,amount\n1,10\n").toString("base64")}`, + mime: "image/png", + filename: "revenue-chart.png", + url: `data:image/png;base64,${PNG_BASE64}`, } as any) } } @@ -292,10 +304,10 @@ it.live( provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { // The provider counted this conversation as 108k tokens, below the ~111k compaction trigger. - // The character estimate of the SQL/JSON history plus the build agent's system prompt and - // tool schemas is far higher (~150k here), past the guard's limit (131,072 less the 1,024-token - // response floor and a 2% margin). Without calibration this request is refused. - const history = sqlJsonHistory(88_000) + // The character estimate of the SQL/JSON history alone is ~130k (the ~20% overestimate on + // SQL/JSON), past the guard's limit (131,072 less the 1,024-token response floor and a 2% + // margin). Without calibration this request is refused. + const history = sqlJsonHistory(130_000) const reported = 108_000 yield* llm.text("Revenue is concentrated in the top ten customers.") @@ -323,7 +335,7 @@ it.live( Effect.fnUntraced(function* ({ dir, llm }) { // A local server that reports no usage gives compaction nothing to fire on; the guard's // estimate is then the only measure, and tripping it must compact rather than end the session. - const history = sqlJsonHistory(100_000) + const history = sqlJsonTurns(135_000) yield* llm.text("summary of the revenue analysis so far") yield* llm.text("Revenue is concentrated in the top ten customers.") @@ -395,7 +407,7 @@ it.live( () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { - const result = yield* runSession(dir, llm, { user: sqlJsonHistory(100_000) }) + const result = yield* runSession(dir, llm, { user: sqlJsonHistory(135_000) }) expect(result.hits.length).toBe(0) expect(result.compactions.length).toBe(0) @@ -429,11 +441,7 @@ it.live( }), { git: true, - // A tool-less agent: the build agent's tool schemas alone estimate above a 16k window. - config: (url: string) => ({ - ...providerCfg(SMALL_WINDOW)(url), - agent: { build: { permission: { "*": "deny" } } }, - }), + config: providerCfg(SMALL_WINDOW), }, ), { timeout: 120_000 }, @@ -454,7 +462,7 @@ for (const [name, last] of [ yield* llm.text("summary of the revenue analysis so far") yield* llm.text("Revenue is concentrated in the top ten customers.") const result = yield* runSession(dir, llm, { - user: sqlJsonHistory(88_000), + user: sqlJsonTurns(130_000), assistantUsage: 108_000, last, next: "Summarize the revenue concentration.", @@ -476,7 +484,7 @@ it.live( provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { const result = yield* runSession(dir, llm, { - user: sqlJsonHistory(100_000), + user: sqlJsonHistory(135_000), assistantUsage: 0, next: "Summarize the revenue concentration.", }) @@ -502,7 +510,7 @@ it.live( yield* llm.text("summary of the revenue analysis so far") yield* llm.text("Revenue is concentrated in the top ten customers.") const result = yield* runSession(dir, llm, { - user: ["List the top customers by paid revenue.", sqlJsonHistory(100_000)], + user: [...sqlJsonTurns(135_000), "Break the revenue down by region."], assistantUsage: 0, last: { finish: "tool-calls", @@ -524,25 +532,26 @@ it.live( ) it.live( - "a new attachment keeps the estimate in charge: the guard trips and the session compacts", + "a new image keeps the estimate in charge, and a turn carrying it is not compacted away", () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { - // Same as the regression above, but the new turn attaches a file the tail estimate cannot see. - yield* llm.text("summary of the revenue analysis so far") - yield* llm.text("Revenue is concentrated in the top ten customers.") + // Same as the regression above, but the new turn attaches an image the tail estimate cannot + // see, so the estimate decides and refuses. Compacting would drop the image, so the refusal + // is the budget error rather than an answer without it. const result = yield* runSession(dir, llm, { - user: sqlJsonHistory(88_000), + user: sqlJsonTurns(130_000), assistantUsage: 108_000, next: "Summarize the revenue concentration.", nextFile: true, }) - expect(result.errors).toEqual([]) - expect(result.summaries.length).toBe(1) - expect(result.hits.length).toBe(2) + expect(result.hits.length).toBe(0) + expect(result.compactions.length).toBe(0) + expect(result.errors.length).toBe(1) + expect(result.errors[0]).toContain("Context budget exceeded before the request was sent") }), - { git: true, config: providerCfg(LARGE_WINDOW) }, + { git: true, config: providerCfg(LARGE_WINDOW, { attachment: true }) }, ), { timeout: 120_000 }, ) @@ -557,7 +566,7 @@ it.live( yield* llm.text("summary of the revenue analysis so far") yield* llm.text("Revenue is concentrated in the top ten customers.") const result = yield* runSession(dir, llm, { - user: sqlJsonHistory(100_000), + user: sqlJsonTurns(135_000), assistantUsage: 0, next: "Summarize the revenue concentration.", }) @@ -569,7 +578,13 @@ it.live( expect(retry.tools?.length ?? 0).toBeGreaterThan(0) expect(JSON.stringify(retry)).not.toContain("maximum number of steps") }), - { git: true, config: (url: string) => ({ ...providerCfg(LARGE_WINDOW)(url), agent: { build: { steps: 2 } } }) }, + { + git: true, + config: (url: string) => ({ + ...providerCfg(LARGE_WINDOW, { tools: true })(url), + agent: { build: { steps: 2 } }, + }), + }, ), { timeout: 120_000 }, ) From ec8edeb8a51b9e85655dcdfdd25a9a084309853e Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 03:48:14 -0800 Subject: [PATCH 09/39] fix: media check covers the whole turn; echo refusals are not compactions - An image or PDF anywhere in the turn's user messages keeps the refusal explicit, not only on the newest message. - A refusal taken by the committed-answer echo recovery keeps the guard's text and does not count as a compacting refusal. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/session/processor.ts | 2 +- packages/opencode/src/session/prompt.ts | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index ef82a8b4e3..74346b770b 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -1362,7 +1362,7 @@ export namespace SessionProcessor { (deferText !== undefined || (input.recoverCommittedAnswer && input.assistantMessage.summary)) && !input.abort.aborted && input.assistantMessage.finish !== "content-filter" - refusedBeforeSend = compactable === true && isContextBudgetError(e) + refusedBeforeSend = compactable === true && !echoRecovery && isContextBudgetError(e) if (refusedBeforeSend) { log.warn("pre-send context budget refusal; compacting before retrying", { sessionID: input.sessionID, diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 12fcaacac1..6d109233e2 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2250,9 +2250,21 @@ export namespace SessionPrompt { const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined // Compaction drops images and PDFs (replay swaps them for a placeholder, summaries strip them): // a refusal on a turn that carries one stays the budget error rather than answer without it. - const turnMedia = msgs - .find((msg) => msg.info.id === lastUser.id) - ?.parts.some((part) => part.type === "file" && MessageV2.isMedia(part.mime)) + // The turn starts at its last user message with real content (synthetic follow-ups excluded). + const turnStart = msgs.findLastIndex( + (msg) => + msg.info.role === "user" && + msg.parts.some((part) => (part.type === "text" && !part.synthetic) || part.type === "file"), + ) + const turnMedia = + turnStart >= 0 && + msgs + .slice(turnStart) + .some( + (msg) => + msg.info.role === "user" && + msg.parts.some((part) => part.type === "file" && MessageV2.isMedia(part.mime)), + ) const compactable = measured !== undefined && loopConfig.compaction?.auto !== false && !turnMedia // Attachments the provider has not counted (in the last turn's tool results or later messages) // are not in the tail estimate: leave those requests to the estimate. From 5392b42f4bb68829e7ed4c649a2e1960088dd88a Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 04:00:04 -0800 Subject: [PATCH 10/39] fix: protect unseen tool-result media; trust usage only from tool steps - An image or PDF in a tool result the model has not seen keeps a refusal explicit, as for user attachments. - Reported usage calibrates the guard only when that step made tool calls (it carried the full tool set). - Docs: the guard's tail is charged more conservatively than compaction's; media rule covers the turn. Co-Authored-By: Claude Opus 5.5 --- docs/docs/configure/context-management.md | 4 +- packages/opencode/src/session/prompt.ts | 42 ++++++++---- .../session/context-guard-compaction.test.ts | 64 +++++++++++++++++-- 3 files changed, 91 insertions(+), 19 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index 4a8b47ad2d..8a3aad9be0 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -155,6 +155,6 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request is caught before it is sent instead of failing at the provider. When the prompt leaves less room than requested, the response reservation is reduced first; the request is refused only when too little room would remain. -The estimator above runs high on SQL- and JSON-heavy context. When the last response came from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since (the measure auto-compaction uses), but never less than 60% of the full estimate. New attachments or a different model or agent fall back to the full estimate, so in those cases a request can still be refused below the compaction threshold. +The estimator above runs high on SQL- and JSON-heavy context. When the last response came from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since, but never less than 60% of the full estimate. Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, and new attachments or a different model or agent fall back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. -If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when the current message carries an image or PDF (compaction would drop it), or when auto-compaction is disabled. +If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when an image or PDF the model has not seen yet is pending (in any message of the current turn or in a tool result; compaction would drop it), or when auto-compaction is disabled. diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 6d109233e2..7d63b54d93 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2243,13 +2243,14 @@ export namespace SessionPrompt { // altimate_change start — the pre-send guard measures this request the way isOverflow does // (provider-reported usage of the last turn on this model plus the estimated tail), and a // guard refusal compacts whenever the proactive check above could have compacted. - // The reported usage applies only to the same model and agent (same tokenizer, system prompt - // and tools), and only when it counts input: a provider reporting none, or only output tokens, - // leaves the estimate in charge. With auto-compaction off a refusal stays the budget error. + // The reported usage applies only to a tool-calling step of the same model and agent (same + // tokenizer, system prompt and tools), and only when it counts input: a provider reporting none, + // or only output tokens, leaves the estimate in charge. With auto-compaction off a refusal stays the budget error. const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined // Compaction drops images and PDFs (replay swaps them for a placeholder, summaries strip them): - // a refusal on a turn that carries one stays the budget error rather than answer without it. + // a refusal while the model has not yet seen one (in this turn's user messages, or in tool + // results since the last measured turn) stays the budget error rather than continue without it. // The turn starts at its last user message with real content (synthetic follow-ups excluded). const turnStart = msgs.findLastIndex( (msg) => @@ -2265,7 +2266,18 @@ export namespace SessionPrompt { msg.info.role === "user" && msg.parts.some((part) => part.type === "file" && MessageV2.isMedia(part.mime)), ) - const compactable = measured !== undefined && loopConfig.compaction?.auto !== false && !turnMedia + const unseenToolMedia = msgs.some( + (msg) => + (msg.info.id === measuredID || isAfterMeasured(msg.info.id)) && + msg.parts.some( + (part) => + part.type === "tool" && + part.state.status === "completed" && + (part.state.attachments ?? []).some((attachment) => MessageV2.isMedia(attachment.mime)), + ), + ) + const compactable = + measured !== undefined && loopConfig.compaction?.auto !== false && !turnMedia && !unseenToolMedia // Attachments the provider has not counted (in the last turn's tool results or later messages) // are not in the tail estimate: leave those requests to the estimate. const unmeasuredMedia = msgs.some( @@ -2277,21 +2289,29 @@ export namespace SessionPrompt { (part.type === "tool" && part.state.status === "completed" && (part.state.attachments?.length ?? 0) > 0), ), ) - const guardTail = measured ? estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens) : 0 + // A step that made tool calls was sent with the full tool set; a text-only step may have been + // sent without tools (an echo or final step), so its usage can undercount this request. + const measuredWithTools = msgs + .find((msg) => msg.info.id === measuredID) + ?.parts.some((part) => part.type === "tool") const reportedInputTokens = measured && + measuredWithTools && !unmeasuredMedia && measured.providerID === model.providerID && measured.modelID === model.id && measured.agent === lastUser.agent && (measured.tokens.input > 0 || measured.tokens.cache.read > 0 || measured.tokens.cache.write > 0) - ? reportedContextTokens({ - tokens: measured.tokens, + ? (() => { // Unmeasured text is sized with the guard's own conservative (multilingual, dense-text) // estimator, so the discount applies only to what the provider has counted. - estimatedTokens: guardTail, - fraction: guardTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, - }) + const guardTail = estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens) + return reportedContextTokens({ + tokens: measured.tokens, + estimatedTokens: guardTail, + fraction: guardTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, + }) + })() : undefined // altimate_change end const result = await processor.process({ diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index eee0e69260..8da9f1273d 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -142,6 +142,8 @@ const providerCfg = }) const LARGE_WINDOW = { context: 131_072, output: 16_384 } +/** A finished step that made a tool call: its reported usage covered the full tool set. */ +const TOOL_STEP = { toolOutput: "42 customers above the revenue threshold." } /** A 1x1 PNG. */ const PNG_BASE64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==" const SMALL_WINDOW = { context: 16_384, output: 4_096 } @@ -194,7 +196,15 @@ type Seeded = { user: string | string[] assistantUsage?: number next?: string - last?: { modelID?: string; agent?: string; finish?: string; text?: string; toolOutput?: string } + last?: { + modelID?: string + agent?: string + finish?: string + text?: string + toolOutput?: string + /** Attach an image to the step's tool result. */ + toolImage?: boolean + } /** An image attached to the `next` user turn. */ nextFile?: boolean } @@ -259,6 +269,21 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) title: "revenue query", metadata: {}, time: { start: Date.now(), end: Date.now() }, + ...(last.toolImage + ? { + attachments: [ + { + id: PartID.ascending(), + sessionID, + messageID: msg.id, + type: "file", + mime: "image/png", + filename: "revenue-chart.png", + url: `data:image/png;base64,${PNG_BASE64}`, + }, + ], + } + : {}), }, } as any) } @@ -314,6 +339,7 @@ it.live( const result = yield* runSession(dir, llm, { user: history, assistantUsage: reported, + last: TOOL_STEP, next: "Summarize the revenue concentration.", }) @@ -447,14 +473,16 @@ it.live( { timeout: 120_000 }, ) -// Usage counted for another model (different tokenizer) or agent (different system prompt and tools) -// does not describe this request: the estimate decides, trips, and the session compacts. +// Usage counted for another model (different tokenizer), another agent (different system prompt and +// tools) or a text-only step (possibly sent without tools) does not describe this request: the estimate +// decides, trips, and the session compacts. for (const [name, last] of [ - ["another model", { modelID: "another-model" }], - ["another agent", { agent: "plan" }], + ["another model", { ...TOOL_STEP, modelID: "another-model" }], + ["another agent", { ...TOOL_STEP, agent: "plan" }], + ["a text-only step", {}], ] as const) { it.live( - `usage reported for ${name} is not trusted: the guard trips and the session compacts`, + `usage reported by ${name} is not trusted: the guard trips and the session compacts`, () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { @@ -542,6 +570,7 @@ it.live( const result = yield* runSession(dir, llm, { user: sqlJsonTurns(130_000), assistantUsage: 108_000, + last: TOOL_STEP, next: "Summarize the revenue concentration.", nextFile: true, }) @@ -556,6 +585,29 @@ it.live( { timeout: 120_000 }, ) +it.live( + "an image from a tool result the model has not seen yet is not compacted away", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // A mid-turn step returned a chart image; the next request is refused. Compaction would strip + // the image before the model ever saw it, so the refusal stays the budget error. + const result = yield* runSession(dir, llm, { + user: [...sqlJsonTurns(135_000), "Chart the revenue by region."], + assistantUsage: 0, + last: { finish: "tool-calls", text: "Rendering the chart.", toolOutput: "Chart rendered.", toolImage: true }, + }) + + expect(result.hits.length).toBe(0) + expect(result.compactions.length).toBe(0) + expect(result.errors.length).toBe(1) + expect(result.errors[0]).toContain("Context budget exceeded before the request was sent") + }), + { git: true, config: providerCfg(LARGE_WINDOW, { attachment: true }) }, + ), + { timeout: 120_000 }, +) + it.live( "a refused attempt does not consume the agent's step cap", () => From 4454131f312e0641c6d128717759e4c45731dfff Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 04:11:27 -0800 Subject: [PATCH 11/39] fix: block compaction only for media the model has not seen - Only an image or PDF attached or returned since the last measured step keeps a refusal explicit; media already seen no longer blocks compaction. - One pass computes the attachment flags; docs list the tool-call-step condition. Co-Authored-By: Claude Opus 5.5 --- docs/docs/configure/context-management.md | 4 +- packages/opencode/src/session/prompt.ts | 62 +++++++------------ .../session/context-guard-compaction.test.ts | 38 ++++++++++++ 3 files changed, 61 insertions(+), 43 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index 8a3aad9be0..a9db0e414f 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -155,6 +155,6 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request is caught before it is sent instead of failing at the provider. When the prompt leaves less room than requested, the response reservation is reduced first; the request is refused only when too little room would remain. -The estimator above runs high on SQL- and JSON-heavy context. When the last response came from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since, but never less than 60% of the full estimate. Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, and new attachments or a different model or agent fall back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. +The estimator above runs high on SQL- and JSON-heavy context. When the last response was a tool-calling step (so it carried the full tool set) from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since, but never less than 60% of the full estimate. Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, and new attachments or a different model or agent fall back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. -If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when an image or PDF the model has not seen yet is pending (in any message of the current turn or in a tool result; compaction would drop it), or when auto-compaction is disabled. +If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when an image or PDF the model has not seen yet is pending (attached or returned by a tool since the model's last response; compaction would drop it), or when auto-compaction is disabled. diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 7d63b54d93..b80dca1611 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2248,47 +2248,27 @@ export namespace SessionPrompt { // or only output tokens, leaves the estimate in charge. With auto-compaction off a refusal stays the budget error. const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined - // Compaction drops images and PDFs (replay swaps them for a placeholder, summaries strip them): - // a refusal while the model has not yet seen one (in this turn's user messages, or in tool - // results since the last measured turn) stays the budget error rather than continue without it. - // The turn starts at its last user message with real content (synthetic follow-ups excluded). - const turnStart = msgs.findLastIndex( - (msg) => - msg.info.role === "user" && - msg.parts.some((part) => (part.type === "text" && !part.synthetic) || part.type === "file"), - ) - const turnMedia = - turnStart >= 0 && - msgs - .slice(turnStart) - .some( - (msg) => - msg.info.role === "user" && - msg.parts.some((part) => part.type === "file" && MessageV2.isMedia(part.mime)), - ) - const unseenToolMedia = msgs.some( - (msg) => - (msg.info.id === measuredID || isAfterMeasured(msg.info.id)) && - msg.parts.some( - (part) => - part.type === "tool" && - part.state.status === "completed" && - (part.state.attachments ?? []).some((attachment) => MessageV2.isMedia(attachment.mime)), - ), - ) - const compactable = - measured !== undefined && loopConfig.compaction?.auto !== false && !turnMedia && !unseenToolMedia - // Attachments the provider has not counted (in the last turn's tool results or later messages) - // are not in the tail estimate: leave those requests to the estimate. - const unmeasuredMedia = msgs.some( - (msg) => - (msg.info.id === measuredID || isAfterMeasured(msg.info.id)) && - msg.parts.some( - (part) => - part.type === "file" || - (part.type === "tool" && part.state.status === "completed" && (part.state.attachments?.length ?? 0) > 0), - ), - ) + // One pass over what the provider has not counted yet (the last measured step and later): + // any attachment keeps the estimate in charge. Compaction drops images and PDFs (replay swaps + // them for a placeholder, summaries strip them), so one the model has not seen yet (a user + // attachment or tool result since the last measured step) keeps a refusal the budget error + // rather than continue without it. Media the model already saw does not block compaction. + let unmeasuredMedia = false + let unseenMedia = false + for (const msg of msgs) { + if (msg.info.id !== measuredID && !isAfterMeasured(msg.info.id)) continue + for (const part of msg.parts) { + if (part.type === "file") { + unmeasuredMedia = true + if (MessageV2.isMedia(part.mime)) unseenMedia = true + } + if (part.type !== "tool" || part.state.status !== "completed") continue + const attachments = part.state.attachments ?? [] + if (attachments.length > 0) unmeasuredMedia = true + if (attachments.some((attachment) => MessageV2.isMedia(attachment.mime))) unseenMedia = true + } + } + const compactable = measured !== undefined && loopConfig.compaction?.auto !== false && !unseenMedia // A step that made tool calls was sent with the full tool set; a text-only step may have been // sent without tools (an echo or final step), so its usage can undercount this request. const measuredWithTools = msgs diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 8da9f1273d..4e14d703f3 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -207,6 +207,8 @@ type Seeded = { } /** An image attached to the `next` user turn. */ nextFile?: boolean + /** An image attached to the last `user` turn (sent before the measured step). */ + userFile?: boolean } /** Seed [user history, finished assistant with provider usage, new user turn] and run the loop. */ @@ -288,10 +290,21 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) } as any) } }) + const attachImage = (messageID: MessageID) => + sessions.updatePart({ + id: PartID.ascending(), + messageID, + sessionID, + type: "file", + mime: "image/png", + filename: "revenue-chart.png", + url: `data:image/png;base64,${PNG_BASE64}`, + } as any) const turns = Array.isArray(seeded.user) ? seeded.user : [seeded.user] for (const [index, text] of turns.entries()) { const msg = yield* user(text) const last = index === turns.length - 1 + if (last && seeded.userFile) yield* attachImage(msg.id) if (!last) yield* answer(msg.id, 0) else if (seeded.assistantUsage !== undefined) yield* answer(msg.id, seeded.assistantUsage, seeded.last) } @@ -608,6 +621,31 @@ it.live( { timeout: 120_000 }, ) +it.live( + "an image the model already saw does not block compaction", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // The turn opened with a chart image, a measured tool step followed (the model saw it), and + // a later request is refused: that is compactable like any other mid-turn refusal. + yield* llm.text("summary of the revenue analysis so far") + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { + user: [...sqlJsonTurns(135_000), "Explain this revenue chart."], + userFile: true, + assistantUsage: 0, + last: { finish: "tool-calls", text: "Querying the data behind the chart.", toolOutput: "42 rows." }, + }) + + expect(result.errors).toEqual([]) + expect(result.summaries.length).toBe(1) + expect(result.hits.length).toBe(2) + }), + { git: true, config: providerCfg(LARGE_WINDOW, { attachment: true }) }, + ), + { timeout: 120_000 }, +) + it.live( "a refused attempt does not consume the agent's step cap", () => From f108190fa2bb8b393d37af6937e955ea9ff48a8a Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 04:14:11 -0800 Subject: [PATCH 12/39] refactor: explicit directive init; if/else for the refusal error Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/session/processor.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 74346b770b..836affa59f 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -463,7 +463,7 @@ export namespace SessionProcessor { // after compaction would silently never see the breaker/loop nudge. let effectiveStreamInput = streamInput // altimate_change start — kept so a pre-send refusal can hand the directive to the retry - let takenDirective: ReturnType + let takenDirective: ReturnType = undefined // altimate_change end if (runMode && !input.assistantMessage.summary) { const directive = NudgeArbiter.take(input.sessionID, input.nudgeGeneration) @@ -1372,14 +1372,15 @@ export namespace SessionProcessor { // Nothing was sent: the directive this request carried belongs to the retry. if (takenDirective) NudgeArbiter.register(input.sessionID, takenDirective, input.nudgeGeneration) } - const error = refusedBeforeSend - ? new MessageV2.ContextOverflowError( - { message: "The request would not fit the model's context window; compacting the conversation and retrying." }, - { cause: e }, - ).toObject() - : echoRecovery && isContextBudgetError(e) - ? new MessageV2.ContextOverflowError({ message: e.message }, { cause: e }).toObject() - : MessageV2.fromError(e, { providerID: input.model.providerID }) + let error = MessageV2.fromError(e, { providerID: input.model.providerID }) + if (refusedBeforeSend) { + error = new MessageV2.ContextOverflowError( + { message: "The request would not fit the model's context window; compacting the conversation and retrying." }, + { cause: e }, + ).toObject() + } else if (echoRecovery && isContextBudgetError(e)) { + error = new MessageV2.ContextOverflowError({ message: e.message }, { cause: e }).toObject() + } // altimate_change end // altimate_change start — committed echoes and summaries retain recoverable causes without a fatal event if ( From 5a9fe5cef0a47b518685d029b90387b88d9ecd84 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 04:25:20 -0800 Subject: [PATCH 13/39] fix: distrust reported usage far below the estimate Usage below 60% of the estimate more likely missed inputs than measured them; the full estimate then decides, so the response reservation is clamped as before. Co-Authored-By: Claude Opus 5.5 --- docs/docs/configure/context-management.md | 2 +- packages/opencode/src/provider/output-token-budget.ts | 8 ++++++-- .../test/session/context-guard-compaction.test.ts | 6 ++++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index a9db0e414f..0b8811a39a 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -155,6 +155,6 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request is caught before it is sent instead of failing at the provider. When the prompt leaves less room than requested, the response reservation is reduced first; the request is refused only when too little room would remain. -The estimator above runs high on SQL- and JSON-heavy context. When the last response was a tool-calling step (so it carried the full tool set) from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since, but never less than 60% of the full estimate. Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, and new attachments or a different model or agent fall back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. +The estimator above runs high on SQL- and JSON-heavy context. When the last response was a tool-calling step (so it carried the full tool set) from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since, as long as it is at least 60% of the full estimate (lower usage is distrusted and the full estimate decides). Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, and new attachments or a different model or agent fall back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when an image or PDF the model has not seen yet is pending (attached or returned by a tool since the model's last response; compaction would drop it), or when auto-compaction is disabled. diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index 3478246982..507ce1eb36 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -21,7 +21,8 @@ const SEMANTIC_MEDIA_BYTES_PER_TOKEN = 64 const DATA_URL_HEADER_LIMIT = 1_024 const MIN_REASONING_BUDGET = 1_024 /** - * Lowest share of the character estimate that provider-reported usage may calibrate the guard to. + * Lowest share of the character estimate that provider-reported usage is trusted at; below it the + * full estimate decides. * The estimate runs ~15-20% high on SQL/JSON and higher on tool schemas; the floor bounds an * undercount from usage that missed inputs changed since (per-step tool selection, refreshed rules). */ @@ -429,7 +430,10 @@ export function reportedContextTokens(input: { */ export function calibratedInputTokens(estimate: number, reported: number | undefined): number { if (reported === undefined || !Number.isFinite(reported) || reported <= 0) return estimate - return Math.min(estimate, Math.max(reported, Math.ceil(estimate * MIN_CALIBRATED_ESTIMATE_FRACTION))) + // Usage far below the estimate more likely missed inputs (partial reporting, a changed request) + // than measured them: the estimate decides, so the response reservation is clamped as before. + if (reported < estimate * MIN_CALIBRATED_ESTIMATE_FRACTION) return estimate + return Math.min(estimate, reported) } /** True for the pre-send refusals that compacting the conversation can resolve. */ diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 4e14d703f3..627df2da8a 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -684,8 +684,10 @@ describe("guard calibration", () => { expect(calibratedInputTokens(128_000, 108_400)).toBe(108_400) // Reported usage never raises the estimate: the guard stays at least as permissive as before. expect(calibratedInputTokens(90_000, 108_400)).toBe(90_000) - // Nor lowers it below 60% of the estimate (partial usage, a request that changed since). - expect(calibratedInputTokens(128_000, 2_000)).toBe(76_800) + // Usage below 60% of the estimate is distrusted (partial usage, a request that changed since). + expect(calibratedInputTokens(128_000, 2_000)).toBe(128_000) + expect(calibratedInputTokens(128_000, 76_000)).toBe(128_000) + expect(calibratedInputTokens(128_000, 77_000)).toBe(77_000) for (const reported of [undefined, 0, -5, Number.NaN, Number.POSITIVE_INFINITY]) { expect(calibratedInputTokens(128_000, reported)).toBe(128_000) } From 4ddb528a77d4a25d210eb7f0dfb5d687cce4af68 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 04:33:43 -0800 Subject: [PATCH 14/39] fix: no compaction when the new content alone fills the window - Unmeasured content that alone fills the window refuses at once, leaving history uncompacted. - Replay only when the turn has no earlier step, finished or interrupted. - JSDoc and the directive comment describe the actual behavior. Co-Authored-By: Claude Opus 5.5 --- .../src/provider/output-token-budget.ts | 11 ++++--- packages/opencode/src/session/processor.ts | 4 ++- packages/opencode/src/session/prompt.ts | 31 +++++++++++++------ .../session/context-guard-compaction.test.ts | 23 ++++++++++++++ 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index 507ce1eb36..6bb6203025 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -409,8 +409,8 @@ export type ReportedUsage = { /** * The context size the compaction trigger compares against its threshold: provider-reported usage - * (exact) plus an estimated uncounted tail inflated by 1/fraction. The pre-send guard uses the same - * measure, so it cannot refuse a request the compaction trigger still considers in budget. + * (exact) plus an estimated uncounted tail inflated by 1/fraction. The pre-send guard builds on the + * same measure (with a more conservative tail estimator) instead of the bare character estimate. */ export function reportedContextTokens(input: { readonly tokens: ReportedUsage @@ -424,9 +424,10 @@ export function reportedContextTokens(input: { } /** - * Prefer the provider-reported size of the same conversation when it is smaller than the - * character estimate. The estimate runs high on SQL- and JSON-heavy context; reported usage plus - * an estimated tail is the measure the compaction trigger uses, so the guard and compaction agree. + * Prefer the provider-reported size of the same conversation when it is smaller than the character + * estimate, which runs high on SQL- and JSON-heavy context. Never raises the estimate, and distrusts + * usage below MIN_CALIBRATED_ESTIMATE_FRACTION of it; in those cases the estimate decides, so the + * guard can still refuse (and the session compact) below the compaction trigger. */ export function calibratedInputTokens(estimate: number, reported: number | undefined): number { if (reported === undefined || !Number.isFinite(reported) || reported <= 0) return estimate diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 836affa59f..8fc4fd3b6d 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -1369,7 +1369,9 @@ export namespace SessionProcessor { providerID: input.model.providerID, modelID: input.model.id, }) - // Nothing was sent: the directive this request carried belongs to the retry. + // Nothing was sent: hand the directive back for the retry. It reaches the retry on the + // turn-start replay path; a mid-turn continuation registers its own completion nudge, + // which takes precedence, and detectors re-register theirs on later steps. if (takenDirective) NudgeArbiter.register(input.sessionID, takenDirective, input.nudgeGeneration) } let error = MessageV2.fromError(e, { providerID: input.model.providerID }) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index b80dca1611..0c62053a5d 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2268,7 +2268,12 @@ export namespace SessionPrompt { if (attachments.some((attachment) => MessageV2.isMedia(attachment.mime))) unseenMedia = true } } - const compactable = measured !== undefined && loopConfig.compaction?.auto !== false && !unseenMedia + // Unmeasured content (sized like the guard sizes it) that alone fills the window cannot be helped + // by compacting the history before it: that refusal stays immediate and the history untouched. + const guardTail = measured ? estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens) : 0 + const tailFillsWindow = guardTail >= (model.limit.input || model.limit.context || Infinity) + const compactable = + measured !== undefined && loopConfig.compaction?.auto !== false && !unseenMedia && !tailFillsWindow // A step that made tool calls was sent with the full tool set; a text-only step may have been // sent without tools (an echo or final step), so its usage can undercount this request. const measuredWithTools = msgs @@ -2282,16 +2287,13 @@ export namespace SessionPrompt { measured.modelID === model.id && measured.agent === lastUser.agent && (measured.tokens.input > 0 || measured.tokens.cache.read > 0 || measured.tokens.cache.write > 0) - ? (() => { + ? reportedContextTokens({ + tokens: measured.tokens, // Unmeasured text is sized with the guard's own conservative (multilingual, dense-text) // estimator, so the discount applies only to what the provider has counted. - const guardTail = estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens) - return reportedContextTokens({ - tokens: measured.tokens, - estimatedTokens: guardTail, - fraction: guardTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, - }) - })() + estimatedTokens: guardTail, + fraction: guardTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, + }) : undefined // altimate_change end const result = await processor.process({ @@ -2717,7 +2719,16 @@ export namespace SessionPrompt { // proactive check does, so this turn's steps are kept; on the turn's first request the user's // prompt is replayed verbatim, as for a provider overflow overflow: - !processor.message.finish && (!processor.refusedBeforeSend || lastFinished?.parentID !== lastUser.id), + !processor.message.finish && + (!processor.refusedBeforeSend || + // the turn's first request: no earlier step of this turn, finished or interrupted + !msgs.some( + (msg) => + msg.info.role === "assistant" && + msg.info.parentID === lastUser.id && + msg.info.id !== processor.message.id && + msg.parts.length > 0, + )), // altimate_change end }) } diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 627df2da8a..0304002c59 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -424,6 +424,29 @@ it.live( { timeout: 120_000 }, ) +it.live( + "a new message that alone fills the window fails at once, leaving the history uncompacted", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // Compacting the earlier history cannot make room for a message bigger than the window. + const result = yield* runSession(dir, llm, { + user: "List the top customers by paid revenue.", + assistantUsage: 5_000, + last: TOOL_STEP, + next: sqlJsonHistory(140_000), + }) + + expect(result.hits.length).toBe(0) + expect(result.compactions.length).toBe(0) + expect(result.errors.length).toBe(1) + expect(result.errors[0]).toContain("Context budget exceeded before the request was sent") + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + it.live( "a first turn with no provider usage is sent when it fits", () => From 9a84ce8cc21a8cf5931a1938f2c88cfe5a2bf9fa Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 04:37:52 -0800 Subject: [PATCH 15/39] fix: the oversize-tail rule leaves room for the response floor Unmeasured content that leaves no room for a minimal response refuses at once; the test runs on a 16k window where the proactive trigger is inert. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/session/prompt.ts | 9 +++++---- .../test/session/context-guard-compaction.test.ts | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 0c62053a5d..1c085c215c 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -31,7 +31,7 @@ import { familyVendor } from "../provider/family" import { type Tool as AITool, tool, jsonSchema, type ToolCallOptions, asSchema } from "ai" import { SessionCompaction } from "./compaction" // altimate_change start — the context measure shared by compaction and the pre-send budget guard -import { estimateTextTokens, reportedContextTokens } from "@/provider/output-token-budget" +import { OUTPUT_TOKEN_FLOOR, estimateTextTokens, reportedContextTokens } from "@/provider/output-token-budget" // altimate_change end import { NudgeArbiter } from "./nudge" import { SessionTermination } from "./termination" @@ -2268,10 +2268,11 @@ export namespace SessionPrompt { if (attachments.some((attachment) => MessageV2.isMedia(attachment.mime))) unseenMedia = true } } - // Unmeasured content (sized like the guard sizes it) that alone fills the window cannot be helped - // by compacting the history before it: that refusal stays immediate and the history untouched. + // Unmeasured content (sized like the guard sizes it) that alone leaves no room for a minimal + // response cannot be helped by compacting the history before it: that refusal stays immediate + // and the history untouched. const guardTail = measured ? estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens) : 0 - const tailFillsWindow = guardTail >= (model.limit.input || model.limit.context || Infinity) + const tailFillsWindow = guardTail + OUTPUT_TOKEN_FLOOR >= (model.limit.input || model.limit.context || Infinity) const compactable = measured !== undefined && loopConfig.compaction?.auto !== false && !unseenMedia && !tailFillsWindow // A step that made tool calls was sent with the full tool set; a text-only step may have been diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 0304002c59..5d0d03c7bb 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -429,12 +429,13 @@ it.live( () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { - // Compacting the earlier history cannot make room for a message bigger than the window. + // 16k window, where the proactive compaction trigger never fires: compacting the earlier + // history cannot make room for a message bigger than the window, so the guard refuses at once. const result = yield* runSession(dir, llm, { user: "List the top customers by paid revenue.", - assistantUsage: 5_000, + assistantUsage: 1_000, last: TOOL_STEP, - next: sqlJsonHistory(140_000), + next: sqlJsonHistory(18_000), }) expect(result.hits.length).toBe(0) @@ -442,7 +443,7 @@ it.live( expect(result.errors.length).toBe(1) expect(result.errors[0]).toContain("Context budget exceeded before the request was sent") }), - { git: true, config: providerCfg(LARGE_WINDOW) }, + { git: true, config: providerCfg(SMALL_WINDOW) }, ), { timeout: 120_000 }, ) From 89bf33ceca3d1229ba6f3c39a37118d947f12648 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 04:45:47 -0800 Subject: [PATCH 16/39] fix: oversize-tail check uses the input limit as is and counts only new content - A dedicated input limit is compared without the response floor. - Files sent as file parts add a byte-based allowance. - The measured step's own tool results are excluded: compaction can still summarize them. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/session/prompt.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 1c085c215c..a5f2b913ec 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -206,6 +206,9 @@ IMPORTANT: - This tool provides your final answer - no further actions are taken after calling it` const STRUCTURED_OUTPUT_SYSTEM_PROMPT = `IMPORTANT: The user has requested structured output. You MUST use the StructuredOutput tool to provide your final response. Do NOT respond with plain text - you MUST call the StructuredOutput tool with your answer formatted according to the schema.` +// altimate_change start — bytes per token for a file sent as a file part (pre-send oversize check) +const FILE_BYTES_PER_TOKEN = 4 +// altimate_change end // altimate_change start — the step-cap instruction for a json_schema request, whose answer is a tool call const STRUCTURED_MAX_STEPS = `The maximum number of steps allowed for this task has been reached. Call the StructuredOutput tool now with your final answer formatted according to the schema, based on the work done so far. Do NOT call any other tool and do NOT respond with plain text.` // altimate_change end @@ -2255,12 +2258,16 @@ export namespace SessionPrompt { // rather than continue without it. Media the model already saw does not block compaction. let unmeasuredMedia = false let unseenMedia = false + // Files sent as file parts (not converted to text) are outside the text tail estimate. + let unmeasuredFileTokens = 0 for (const msg of msgs) { if (msg.info.id !== measuredID && !isAfterMeasured(msg.info.id)) continue for (const part of msg.parts) { if (part.type === "file") { unmeasuredMedia = true if (MessageV2.isMedia(part.mime)) unseenMedia = true + else if (part.mime !== "text/plain" && part.mime !== "application/x-directory") + unmeasuredFileTokens += Math.ceil((part.url.length * 3) / 4 / FILE_BYTES_PER_TOKEN) } if (part.type !== "tool" || part.state.status !== "completed") continue const attachments = part.state.attachments ?? [] @@ -2272,7 +2279,16 @@ export namespace SessionPrompt { // response cannot be helped by compacting the history before it: that refusal stays immediate // and the history untouched. const guardTail = measured ? estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens) : 0 - const tailFillsWindow = guardTail + OUTPUT_TOKEN_FLOOR >= (model.limit.input || model.limit.context || Infinity) + // A dedicated input limit already excludes the response; a context window must also fit it. + // Only content after the measured step: that step's own tool results can still be summarized. + const measuredMsg = msgs.find((msg) => msg.info.id === measuredID) + const measuredOwnTokens = measuredMsg + ? estimateUncountedTail([measuredMsg], measuredMsg.info.id, () => false, estimateTextTokens) + : 0 + const unmeasuredTokens = guardTail - measuredOwnTokens + unmeasuredFileTokens + const tailFillsWindow = model.limit.input + ? unmeasuredTokens >= model.limit.input + : unmeasuredTokens + OUTPUT_TOKEN_FLOOR >= (model.limit.context || Infinity) const compactable = measured !== undefined && loopConfig.compaction?.auto !== false && !unseenMedia && !tailFillsWindow // A step that made tool calls was sent with the full tool set; a text-only step may have been From 1468a6d6297364c1d760395b845004b5839411d2 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 04:56:21 -0800 Subject: [PATCH 17/39] fix: tag tool-less requests instead of guessing; drop the oversize-tail shortcut - Echo and final-step requests are tagged sent_without_tools; usage from any other step, text-only included, calibrates the guard. - @file text and directory parts no longer disable calibration. - Remove the oversize-tail rule: a message larger than the window compacts once, then fails. Co-Authored-By: Claude Opus 5.5 --- docs/docs/configure/context-management.md | 2 +- packages/opencode/src/session/prompt.ts | 56 +++++++------------ .../session/context-guard-compaction.test.ts | 36 ++++++------ 3 files changed, 41 insertions(+), 53 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index 0b8811a39a..141478dd51 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -155,6 +155,6 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request is caught before it is sent instead of failing at the provider. When the prompt leaves less room than requested, the response reservation is reduced first; the request is refused only when too little room would remain. -The estimator above runs high on SQL- and JSON-heavy context. When the last response was a tool-calling step (so it carried the full tool set) from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since, as long as it is at least 60% of the full estimate (lower usage is distrusted and the full estimate decides). Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, and new attachments or a different model or agent fall back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. +The estimator above runs high on SQL- and JSON-heavy context. When the last response was sent with the full tool set (not a final or answer-echo step sent without tools) from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since, as long as it is at least 60% of the full estimate (lower usage is distrusted and the full estimate decides). Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, and new attachments or a different model or agent fall back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when an image or PDF the model has not seen yet is pending (attached or returned by a tool since the model's last response; compaction would drop it), or when auto-compaction is disabled. diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index a5f2b913ec..fb5de99a18 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -31,7 +31,7 @@ import { familyVendor } from "../provider/family" import { type Tool as AITool, tool, jsonSchema, type ToolCallOptions, asSchema } from "ai" import { SessionCompaction } from "./compaction" // altimate_change start — the context measure shared by compaction and the pre-send budget guard -import { OUTPUT_TOKEN_FLOOR, estimateTextTokens, reportedContextTokens } from "@/provider/output-token-budget" +import { estimateTextTokens, reportedContextTokens } from "@/provider/output-token-budget" // altimate_change end import { NudgeArbiter } from "./nudge" import { SessionTermination } from "./termination" @@ -206,8 +206,8 @@ IMPORTANT: - This tool provides your final answer - no further actions are taken after calling it` const STRUCTURED_OUTPUT_SYSTEM_PROMPT = `IMPORTANT: The user has requested structured output. You MUST use the StructuredOutput tool to provide your final response. Do NOT respond with plain text - you MUST call the StructuredOutput tool with your answer formatted according to the schema.` -// altimate_change start — bytes per token for a file sent as a file part (pre-send oversize check) -const FILE_BYTES_PER_TOKEN = 4 +// altimate_change start — assistant-message metadata key marking a request sent without tools +const SENT_WITHOUT_TOOLS = "sent_without_tools" // altimate_change end // altimate_change start — the step-cap instruction for a json_schema request, whose answer is a tool call const STRUCTURED_MAX_STEPS = `The maximum number of steps allowed for this task has been reached. Call the StructuredOutput tool now with your final answer formatted according to the schema, based on the work done so far. Do NOT call any other tool and do NOT respond with plain text.` @@ -2258,16 +2258,13 @@ export namespace SessionPrompt { // rather than continue without it. Media the model already saw does not block compaction. let unmeasuredMedia = false let unseenMedia = false - // Files sent as file parts (not converted to text) are outside the text tail estimate. - let unmeasuredFileTokens = 0 for (const msg of msgs) { if (msg.info.id !== measuredID && !isAfterMeasured(msg.info.id)) continue for (const part of msg.parts) { - if (part.type === "file") { + // text/plain files and directories arrive as text parts, already in the tail estimate. + if (part.type === "file" && part.mime !== "text/plain" && part.mime !== "application/x-directory") { unmeasuredMedia = true if (MessageV2.isMedia(part.mime)) unseenMedia = true - else if (part.mime !== "text/plain" && part.mime !== "application/x-directory") - unmeasuredFileTokens += Math.ceil((part.url.length * 3) / 4 / FILE_BYTES_PER_TOKEN) } if (part.type !== "tool" || part.state.status !== "completed") continue const attachments = part.state.attachments ?? [] @@ -2275,44 +2272,33 @@ export namespace SessionPrompt { if (attachments.some((attachment) => MessageV2.isMedia(attachment.mime))) unseenMedia = true } } - // Unmeasured content (sized like the guard sizes it) that alone leaves no room for a minimal - // response cannot be helped by compacting the history before it: that refusal stays immediate - // and the history untouched. - const guardTail = measured ? estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens) : 0 - // A dedicated input limit already excludes the response; a context window must also fit it. - // Only content after the measured step: that step's own tool results can still be summarized. - const measuredMsg = msgs.find((msg) => msg.info.id === measuredID) - const measuredOwnTokens = measuredMsg - ? estimateUncountedTail([measuredMsg], measuredMsg.info.id, () => false, estimateTextTokens) - : 0 - const unmeasuredTokens = guardTail - measuredOwnTokens + unmeasuredFileTokens - const tailFillsWindow = model.limit.input - ? unmeasuredTokens >= model.limit.input - : unmeasuredTokens + OUTPUT_TOKEN_FLOOR >= (model.limit.context || Infinity) - const compactable = - measured !== undefined && loopConfig.compaction?.auto !== false && !unseenMedia && !tailFillsWindow - // A step that made tool calls was sent with the full tool set; a text-only step may have been - // sent without tools (an echo or final step), so its usage can undercount this request. - const measuredWithTools = msgs - .find((msg) => msg.info.id === measuredID) - ?.parts.some((part) => part.type === "tool") + const compactable = measured !== undefined && loopConfig.compaction?.auto !== false && !unseenMedia + // A step sent without tools (an echo or final step, tagged below) reported usage that can + // undercount this request by the tool schemas; every other step carried the full tool set. const reportedInputTokens = measured && - measuredWithTools && + measured.metadata?.[SENT_WITHOUT_TOOLS] !== true && !unmeasuredMedia && measured.providerID === model.providerID && measured.modelID === model.id && measured.agent === lastUser.agent && (measured.tokens.input > 0 || measured.tokens.cache.read > 0 || measured.tokens.cache.write > 0) - ? reportedContextTokens({ - tokens: measured.tokens, + ? (() => { // Unmeasured text is sized with the guard's own conservative (multilingual, dense-text) // estimator, so the discount applies only to what the provider has counted. - estimatedTokens: guardTail, - fraction: guardTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, - }) + const guardTail = estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens) + return reportedContextTokens({ + tokens: measured.tokens, + estimatedTokens: guardTail, + fraction: guardTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, + }) + })() : undefined // altimate_change end + // altimate_change start — record a request sent without tools; its usage cannot calibrate the guard + if (echoCommittedAnswer || textFinalStep) + processor.message.metadata = { ...processor.message.metadata, [SENT_WITHOUT_TOOLS]: true } + // altimate_change end const result = await processor.process({ // altimate_change start — see reportedInputTokens above compactable, diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 5d0d03c7bb..0ef3cd9b52 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -204,6 +204,8 @@ type Seeded = { toolOutput?: string /** Attach an image to the step's tool result. */ toolImage?: boolean + /** The step's request was sent without tools (an echo or final step). */ + sentWithoutTools?: boolean } /** An image attached to the `next` user turn. */ nextFile?: boolean @@ -248,6 +250,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) parentID, finish: last.finish ?? "stop", time: { created: Date.now(), completed: Date.now() }, + ...(last.sentWithoutTools ? { metadata: { sent_without_tools: true } } : {}), } as any) yield* sessions.updatePart({ id: PartID.ascending(), @@ -352,7 +355,7 @@ it.live( const result = yield* runSession(dir, llm, { user: history, assistantUsage: reported, - last: TOOL_STEP, + // The previous turn ended with a plain text answer, the usual shape at a new user turn. next: "Summarize the revenue concentration.", }) @@ -425,25 +428,24 @@ it.live( ) it.live( - "a new message that alone fills the window fails at once, leaving the history uncompacted", + "a final step sent without tools is tagged so its usage never calibrates the guard", () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { - // 16k window, where the proactive compaction trigger never fires: compacting the earlier - // history cannot make room for a message bigger than the window, so the guard refuses at once. - const result = yield* runSession(dir, llm, { - user: "List the top customers by paid revenue.", - assistantUsage: 1_000, - last: TOOL_STEP, - next: sqlJsonHistory(18_000), - }) + // steps: 1 makes the first request the final step, sent without tools. + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { user: "Summarize the revenue concentration." }) - expect(result.hits.length).toBe(0) - expect(result.compactions.length).toBe(0) - expect(result.errors.length).toBe(1) - expect(result.errors[0]).toContain("Context budget exceeded before the request was sent") + const answer = result.messages.find((m) => m.info.role === "assistant")?.info as any + expect(answer?.metadata?.sent_without_tools).toBe(true) }), - { git: true, config: providerCfg(SMALL_WINDOW) }, + { + git: true, + config: (url: string) => ({ + ...providerCfg(LARGE_WINDOW, { tools: true })(url), + agent: { build: { steps: 1 } }, + }), + }, ), { timeout: 120_000 }, ) @@ -511,12 +513,12 @@ it.live( ) // Usage counted for another model (different tokenizer), another agent (different system prompt and -// tools) or a text-only step (possibly sent without tools) does not describe this request: the estimate +// tools) or a step sent without tools (an echo or final step) does not describe this request: the estimate // decides, trips, and the session compacts. for (const [name, last] of [ ["another model", { ...TOOL_STEP, modelID: "another-model" }], ["another agent", { ...TOOL_STEP, agent: "plan" }], - ["a text-only step", {}], + ["a step sent without tools", { sentWithoutTools: true }], ] as const) { it.live( `usage reported by ${name} is not trusted: the guard trips and the session compacts`, From e9ac3045fde33c24f1eb951df02c743f6c2b4234 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 05:06:25 -0800 Subject: [PATCH 18/39] fix: add attachment allowances to the calibrated measure instead of dropping it A new attachment no longer discards reported usage: its allowance (the guard's own) is added, so a near-full session with a new image is sent rather than refused. Co-Authored-By: Claude Opus 5.5 --- docs/docs/configure/context-management.md | 2 +- .../src/provider/output-token-budget.ts | 5 +++ packages/opencode/src/session/prompt.ts | 34 ++++++++++--------- .../session/context-guard-compaction.test.ts | 14 ++++---- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index 141478dd51..1c33b32487 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -155,6 +155,6 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request is caught before it is sent instead of failing at the provider. When the prompt leaves less room than requested, the response reservation is reduced first; the request is refused only when too little room would remain. -The estimator above runs high on SQL- and JSON-heavy context. When the last response was sent with the full tool set (not a final or answer-echo step sent without tools) from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since, as long as it is at least 60% of the full estimate (lower usage is distrusted and the full estimate decides). Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, and new attachments or a different model or agent fall back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. +The estimator above runs high on SQL- and JSON-heavy context. When the last response was sent with the full tool set (not a final or answer-echo step sent without tools) from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since, as long as it is at least 60% of the full estimate (lower usage is distrusted and the full estimate decides). Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, new attachments are added at the check's own allowance, and a different model or agent falls back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when an image or PDF the model has not seen yet is pending (attached or returned by a tool since the model's last response; compaction would drop it), or when auto-compaction is disabled. diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index 6bb6203025..b04986514f 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -283,6 +283,11 @@ function mediaTokenAllowance(part: JsonRecord): number { return MEDIA_TOKEN_ALLOWANCE } +/** The allowance the guard charges for one attachment, for callers sizing unmeasured attachments. */ +export function attachmentTokenAllowance(mime: string, url: string): number { + return mediaTokenAllowance({ type: "file", mediaType: mime, data: url }) +} + /** Mark only actual ModelMessage content parts whose payload is provider media. */ function messageMediaAllowances(messages: readonly unknown[]): WeakMap { const result = new WeakMap() diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index fb5de99a18..376f9e0ef7 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -31,7 +31,7 @@ import { familyVendor } from "../provider/family" import { type Tool as AITool, tool, jsonSchema, type ToolCallOptions, asSchema } from "ai" import { SessionCompaction } from "./compaction" // altimate_change start — the context measure shared by compaction and the pre-send budget guard -import { estimateTextTokens, reportedContextTokens } from "@/provider/output-token-budget" +import { attachmentTokenAllowance, estimateTextTokens, reportedContextTokens } from "@/provider/output-token-budget" // altimate_change end import { NudgeArbiter } from "./nudge" import { SessionTermination } from "./termination" @@ -2252,24 +2252,25 @@ export namespace SessionPrompt { const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined // One pass over what the provider has not counted yet (the last measured step and later): - // any attachment keeps the estimate in charge. Compaction drops images and PDFs (replay swaps - // them for a placeholder, summaries strip them), so one the model has not seen yet (a user - // attachment or tool result since the last measured step) keeps a refusal the budget error - // rather than continue without it. Media the model already saw does not block compaction. - let unmeasuredMedia = false + // attachments are added to the reported measure at the guard's own allowance. Compaction drops + // images and PDFs (replay swaps them for a placeholder, summaries strip them), so one the model + // has not seen yet (a user attachment or tool result since the last measured step) keeps a + // refusal the budget error rather than continue without it. Media already seen does not. + let attachmentTokens = 0 let unseenMedia = false for (const msg of msgs) { if (msg.info.id !== measuredID && !isAfterMeasured(msg.info.id)) continue for (const part of msg.parts) { // text/plain files and directories arrive as text parts, already in the tail estimate. if (part.type === "file" && part.mime !== "text/plain" && part.mime !== "application/x-directory") { - unmeasuredMedia = true + attachmentTokens += attachmentTokenAllowance(part.mime, part.url) if (MessageV2.isMedia(part.mime)) unseenMedia = true } if (part.type !== "tool" || part.state.status !== "completed") continue - const attachments = part.state.attachments ?? [] - if (attachments.length > 0) unmeasuredMedia = true - if (attachments.some((attachment) => MessageV2.isMedia(attachment.mime))) unseenMedia = true + for (const attachment of part.state.attachments ?? []) { + attachmentTokens += attachmentTokenAllowance(attachment.mime, attachment.url) + if (MessageV2.isMedia(attachment.mime)) unseenMedia = true + } } } const compactable = measured !== undefined && loopConfig.compaction?.auto !== false && !unseenMedia @@ -2278,7 +2279,6 @@ export namespace SessionPrompt { const reportedInputTokens = measured && measured.metadata?.[SENT_WITHOUT_TOOLS] !== true && - !unmeasuredMedia && measured.providerID === model.providerID && measured.modelID === model.id && measured.agent === lastUser.agent && @@ -2287,11 +2287,13 @@ export namespace SessionPrompt { // Unmeasured text is sized with the guard's own conservative (multilingual, dense-text) // estimator, so the discount applies only to what the provider has counted. const guardTail = estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens) - return reportedContextTokens({ - tokens: measured.tokens, - estimatedTokens: guardTail, - fraction: guardTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, - }) + return ( + reportedContextTokens({ + tokens: measured.tokens, + estimatedTokens: guardTail, + fraction: guardTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, + }) + attachmentTokens + ) })() : undefined // altimate_change end diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 0ef3cd9b52..d7ae70a3dc 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -599,25 +599,23 @@ it.live( ) it.live( - "a new image keeps the estimate in charge, and a turn carrying it is not compacted away", + "a new image in a near-full session is sent: calibration adds its allowance instead of dropping", () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { - // Same as the regression above, but the new turn attaches an image the tail estimate cannot - // see, so the estimate decides and refuses. Compacting would drop the image, so the refusal - // is the budget error rather than an answer without it. + // Same as the regression above, plus an image on the new turn. The estimate alone would refuse + // (and the unseen image makes the refusal fatal); reported usage plus the image's allowance fits. + yield* llm.text("Revenue is concentrated in the top ten customers.") const result = yield* runSession(dir, llm, { user: sqlJsonTurns(130_000), assistantUsage: 108_000, - last: TOOL_STEP, next: "Summarize the revenue concentration.", nextFile: true, }) - expect(result.hits.length).toBe(0) + expect(result.errors).toEqual([]) expect(result.compactions.length).toBe(0) - expect(result.errors.length).toBe(1) - expect(result.errors[0]).toContain("Context budget exceeded before the request was sent") + expect(result.hits.length).toBe(1) }), { git: true, config: providerCfg(LARGE_WINDOW, { attachment: true }) }, ), From 43f522d00151041a8c3502408c34ef40826d3472 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 05:15:53 -0800 Subject: [PATCH 19/39] fix: tag deadline-finalization requests as sent without tools; pin the attachment allowance Co-Authored-By: Claude Opus 5.5 --- docs/docs/configure/context-management.md | 2 +- packages/opencode/src/session/prompt.ts | 3 ++- .../test/session/context-guard-compaction.test.ts | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index 1c33b32487..d4564b3090 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -155,6 +155,6 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request is caught before it is sent instead of failing at the provider. When the prompt leaves less room than requested, the response reservation is reduced first; the request is refused only when too little room would remain. -The estimator above runs high on SQL- and JSON-heavy context. When the last response was sent with the full tool set (not a final or answer-echo step sent without tools) from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since, as long as it is at least 60% of the full estimate (lower usage is distrusted and the full estimate decides). Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, new attachments are added at the check's own allowance, and a different model or agent falls back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. +The estimator above runs high on SQL- and JSON-heavy context. When the last response was sent with the full tool set (not a step sent without tools, such as a final, deadline or answer-echo step) from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since, as long as it is at least 60% of the full estimate (lower usage is distrusted and the full estimate decides). Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, new attachments are added at the check's own allowance, and a different model or agent falls back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when an image or PDF the model has not seen yet is pending (attached or returned by a tool since the model's last response; compaction would drop it), or when auto-compaction is disabled. diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 376f9e0ef7..7029b36ff3 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2298,7 +2298,8 @@ export namespace SessionPrompt { : undefined // altimate_change end // altimate_change start — record a request sent without tools; its usage cannot calibrate the guard - if (echoCommittedAnswer || textFinalStep) + // (an answer echo, the final step, or a deadline finalization) + if (echoCommittedAnswer || textFinalStep || RunBudgetEffort.toolsDisabled(sessionID)) processor.message.metadata = { ...processor.message.metadata, [SENT_WITHOUT_TOOLS]: true } // altimate_change end const result = await processor.process({ diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index d7ae70a3dc..81505a52e5 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -31,6 +31,7 @@ import { InputTokenBudgetError, OutputTokenBudgetError, ReasoningTokenBudgetError, + attachmentTokenAllowance, calibratedInputTokens, estimateInputTokens, estimateTextTokens, @@ -742,6 +743,20 @@ describe("guard calibration", () => { }) }) +describe("attachment allowance", () => { + test("charges an unmeasured attachment what the guard's own estimate charges it", () => { + const png = `data:image/png;base64,${PNG_BASE64}` + const viaEstimate = + estimateInputTokens({ + system: [], + messages: [{ role: "user", content: [{ type: "file", mediaType: "image/png", data: png }] }], + }) - estimateInputTokens({ system: [], messages: [{ role: "user", content: [] }] }) + expect(attachmentTokenAllowance("image/png", png)).toBe(2_048) + expect(viaEstimate).toBeGreaterThanOrEqual(2_048) + expect(attachmentTokenAllowance("application/pdf", "https://example.com/report.pdf")).toBeGreaterThanOrEqual(32_768) + }) +}) + describe("calibration tail", () => { const text = (id: string, role: "user" | "assistant", body: string) => ({ From 3329c5ba3c8ad239e04d28bbb42d65a0da024b7b Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 05:25:33 -0800 Subject: [PATCH 20/39] fix: inherited deadline tagging; narrow the input-plus-reservation overflow - A task child's deadline-finalization step is tagged sent_without_tools via the ancestor run lookup. - The provider's input-plus-max_tokens error is an overflow only when the input is over half the limit, so an oversized max_tokens cannot loop compactions. - Drop a nested marker pair in processor.ts. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/provider/error.ts | 20 ++++++++-- packages/opencode/src/session/processor.ts | 3 +- packages/opencode/src/session/prompt.ts | 15 ++++++- .../session/context-guard-compaction.test.ts | 40 ++++++++++++++----- 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/packages/opencode/src/provider/error.ts b/packages/opencode/src/provider/error.ts index ce8ba2c51a..d05d30339c 100644 --- a/packages/opencode/src/provider/error.ts +++ b/packages/opencode/src/provider/error.ts @@ -60,12 +60,21 @@ export namespace ProviderError { /request entity too large/i, // HTTP 413 /the request was too long/i, // Azure OpenAI /maximum tokens for requested operation/i, // Azure OpenAI - // altimate_change start — input plus the output reservation over the window is an overflow too: - // compaction makes room for the reservation - /input length and `?max_tokens`? exceed context limit/i, - // altimate_change end ] + // altimate_change start — input plus the output reservation over the window is an overflow when the + // input is a large share of the limit (compaction can make room). A small input with an oversized + // max_tokens is a configuration problem compaction cannot fix, so it stays a plain API error. + const INPUT_PLUS_RESERVATION = /input length and `?max_tokens`? exceed context limit:\s*(\d+)\s*\+\s*(\d+)\s*>\s*(\d+)/i + function isInputPlusReservationOverflow(message: string) { + const match = message.match(INPUT_PLUS_RESERVATION) + if (!match) return false + const input = Number(match[1]) + const limit = Number(match[3]) + return input * 2 > limit + } + // altimate_change end + function isOpenAiErrorRetryable(e: APICallError) { const status = e.statusCode if (!status) return e.isRetryable @@ -92,6 +101,9 @@ export namespace ProviderError { // - z.ai: can accept overflow silently (needs token-count/context-window checks) function isOverflow(message: string) { if (OVERFLOW_PATTERNS.some((p) => p.test(message))) return true + // altimate_change start — see isInputPlusReservationOverflow + if (isInputPlusReservationOverflow(message)) return true + // altimate_change end // Providers/status patterns handled outside of regex list: // - Cerebras: often returns "400 (no body)" / "413 (no body)" diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 8fc4fd3b6d..a1a2e63465 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -462,9 +462,8 @@ export namespace SessionProcessor { // produce a summary and cannot act on it, so the real working turn right // after compaction would silently never see the breaker/loop nudge. let effectiveStreamInput = streamInput - // altimate_change start — kept so a pre-send refusal can hand the directive to the retry + // Kept so a pre-send refusal can hand the directive to the retry. let takenDirective: ReturnType = undefined - // altimate_change end if (runMode && !input.assistantMessage.summary) { const directive = NudgeArbiter.take(input.sessionID, input.nudgeGeneration) // altimate_change end diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 7029b36ff3..6e0292ba18 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -208,6 +208,19 @@ IMPORTANT: const STRUCTURED_OUTPUT_SYSTEM_PROMPT = `IMPORTANT: The user has requested structured output. You MUST use the StructuredOutput tool to provide your final response. Do NOT respond with plain text - you MUST call the StructuredOutput tool with your answer formatted according to the schema.` // altimate_change start — assistant-message metadata key marking a request sent without tools const SENT_WITHOUT_TOOLS = "sent_without_tools" + +/** Whether a deadline has disabled tools for this session, inherited from a live ancestor run (as llm.ts applies it). */ +async function runToolsDisabled(sessionID: string): Promise { + if (!RunBudgetEffort.hasActiveRuns()) return false + const visited = new Set() + let current: string | undefined = sessionID + while (current && !visited.has(current)) { + if (RunBudgetEffort.activeRun(current)) return RunBudgetEffort.toolsDisabled(current) + visited.add(current) + current = (await Session.get(SessionID.make(current)).catch(() => undefined))?.parentID + } + return false +} // altimate_change end // altimate_change start — the step-cap instruction for a json_schema request, whose answer is a tool call const STRUCTURED_MAX_STEPS = `The maximum number of steps allowed for this task has been reached. Call the StructuredOutput tool now with your final answer formatted according to the schema, based on the work done so far. Do NOT call any other tool and do NOT respond with plain text.` @@ -2299,7 +2312,7 @@ export namespace SessionPrompt { // altimate_change end // altimate_change start — record a request sent without tools; its usage cannot calibrate the guard // (an answer echo, the final step, or a deadline finalization) - if (echoCommittedAnswer || textFinalStep || RunBudgetEffort.toolsDisabled(sessionID)) + if (echoCommittedAnswer || textFinalStep || (await runToolsDisabled(sessionID))) processor.message.metadata = { ...processor.message.metadata, [SENT_WITHOUT_TOOLS]: true } // altimate_change end const result = await processor.process({ diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 81505a52e5..7f1ab85ba0 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -784,15 +784,33 @@ describe("calibration tail", () => { }) }) -test("an input-plus-reservation overflow from the provider is a context overflow (reactive compaction)", () => { - const error = new APICallError({ - message: - "input length and `max_tokens` exceed context limit: 188240 + 21333 > 200000, decrease input length or `max_tokens` and try again", - url: "https://example.com", - requestBodyValues: {}, - statusCode: 400, - responseHeaders: { "content-type": "application/json" }, - isRetryable: false, - }) - expect(MessageV2.ContextOverflowError.isInstance(MessageV2.fromError(error, { providerID: "p" as any }))).toBe(true) +test("an input-plus-reservation provider error is an overflow only when the input is a large share", () => { + const error = (message: string) => + MessageV2.fromError( + new APICallError({ + message, + url: "https://example.com", + requestBodyValues: {}, + statusCode: 400, + responseHeaders: { "content-type": "application/json" }, + isRetryable: false, + }), + { providerID: "p" as any }, + ) + // A large input: compaction can make room for the reservation. + expect( + MessageV2.ContextOverflowError.isInstance( + error( + "input length and `max_tokens` exceed context limit: 188240 + 21333 > 200000, decrease input length or `max_tokens` and try again", + ), + ), + ).toBe(true) + // A tiny input with an oversized max_tokens: a configuration problem, not compactable. + expect( + MessageV2.ContextOverflowError.isInstance( + error( + "input length and `max_tokens` exceed context limit: 5 + 64000 > 32768, decrease input length or `max_tokens` and try again", + ), + ), + ).toBe(false) }) From ca66c300ff02291c7d92e0134fd02ece948920a5 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 05:28:06 -0800 Subject: [PATCH 21/39] fix: the guard's calibration tail counts tool-call inputs of later steps Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/session/prompt.ts | 5 +++- .../session/context-guard-compaction.test.ts | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 6e0292ba18..dbdff99163 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2299,7 +2299,7 @@ export namespace SessionPrompt { ? (() => { // Unmeasured text is sized with the guard's own conservative (multilingual, dense-text) // estimator, so the discount applies only to what the provider has counted. - const guardTail = estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens) + const guardTail = estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens, true) return ( reportedContextTokens({ tokens: measured.tokens, @@ -3861,6 +3861,8 @@ export namespace SessionPrompt { lastFinishedID: MessageID | undefined, isAfter: (id: MessageID) => boolean = (id) => id > lastFinishedID!, estimate: (text: string) => number = Token.estimate, + // Also count the tool-call inputs of later steps (sent back with the history); the pre-send guard does. + countToolInput = false, ): number { if (!lastFinishedID) return 0 const lastFinished = msgs.find((m) => m.info.id === lastFinishedID) @@ -3889,6 +3891,7 @@ export namespace SessionPrompt { for (const part of m.parts) { if (part.type === "text") tokens += estimate(part.text ?? "") if (part.type === "tool") tokens += estimate(toolText(part)) + if (part.type === "tool" && countToolInput) tokens += estimate(JSON.stringify(part.state.input ?? {})) } } return tokens diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 7f1ab85ba0..9dfdacaded 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -774,6 +774,34 @@ describe("calibration tail", () => { expect(guard).toBeGreaterThanOrEqual(cjk.length) }) + test("the guard's tail also counts tool-call inputs of later steps", () => { + const step = { + info: { id: "m3", sessionID: "s", role: "assistant", time: { created: 1 } }, + parts: [ + { + id: "m3-t", + sessionID: "s", + messageID: "m3", + type: "tool", + tool: "bash", + callID: "c", + state: { + status: "completed", + input: { query: "q".repeat(4_000) }, + output: "ok", + title: "t", + metadata: {}, + time: { start: 1, end: 2 }, + }, + }, + ], + } as unknown as MessageV2.WithParts + const msgs = [text("m1", "user", "task"), text("m2", "assistant", "done"), step] + const plain = SessionPrompt.estimateUncountedTail(msgs, "m2" as any) + const withInput = SessionPrompt.estimateUncountedTail(msgs, "m2" as any, undefined, undefined, true) + expect(withInput - plain).toBeGreaterThanOrEqual(Token.estimate(JSON.stringify({ query: "q".repeat(4_000) }))) + }) + test("follows the caller's stored order, not lexical IDs", () => { // A later message with a lexically smaller caller-supplied ID. const msgs = [text("m5", "user", "task"), text("m6", "assistant", "done"), text("a1", "user", "y".repeat(9_000))] From 5202805f1160dcea0bf4243c0baff79d54ecbf11 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 05:36:46 -0800 Subject: [PATCH 22/39] refactor: drop the provider input-plus-reservation pattern; share the inherited-run lookup - Remove the provider error pattern added for undercount recovery; its compaction could repeat or misfire, and the distrust floor and tool-less tagging bound the undercount. - RunBudgetEffort.inheritedRun resolves a task child's governing run for both llm.ts and the deadline tag; unit-tested. Co-Authored-By: Claude Opus 5.5 --- .../src/altimate/session/run-budget-effort.ts | 16 +++++++ packages/opencode/src/provider/error.ts | 16 ------- packages/opencode/src/session/llm.ts | 16 +++---- packages/opencode/src/session/prompt.ts | 14 +++--- .../session/context-guard-compaction.test.ts | 43 ++++++------------- 5 files changed, 40 insertions(+), 65 deletions(-) diff --git a/packages/opencode/src/altimate/session/run-budget-effort.ts b/packages/opencode/src/altimate/session/run-budget-effort.ts index 5c497ad41d..ca433ad422 100644 --- a/packages/opencode/src/altimate/session/run-budget-effort.ts +++ b/packages/opencode/src/altimate/session/run-budget-effort.ts @@ -40,3 +40,19 @@ export function hasActiveRuns(): boolean { export function activeRun(sessionID: string): boolean { return active.has(sessionID) } + +/** The session (this one or the nearest ancestor) whose live run governs it; task subagents inherit it. */ +export async function inheritedRun( + sessionID: string, + parentOf: (sessionID: string) => Promise, +): Promise { + if (!hasActiveRuns()) return undefined + const visited = new Set() + let current: string | undefined = sessionID + while (current && !visited.has(current)) { + if (activeRun(current)) return current + visited.add(current) + current = await parentOf(current) + } + return undefined +} diff --git a/packages/opencode/src/provider/error.ts b/packages/opencode/src/provider/error.ts index d05d30339c..67bb6237cf 100644 --- a/packages/opencode/src/provider/error.ts +++ b/packages/opencode/src/provider/error.ts @@ -62,19 +62,6 @@ export namespace ProviderError { /maximum tokens for requested operation/i, // Azure OpenAI ] - // altimate_change start — input plus the output reservation over the window is an overflow when the - // input is a large share of the limit (compaction can make room). A small input with an oversized - // max_tokens is a configuration problem compaction cannot fix, so it stays a plain API error. - const INPUT_PLUS_RESERVATION = /input length and `?max_tokens`? exceed context limit:\s*(\d+)\s*\+\s*(\d+)\s*>\s*(\d+)/i - function isInputPlusReservationOverflow(message: string) { - const match = message.match(INPUT_PLUS_RESERVATION) - if (!match) return false - const input = Number(match[1]) - const limit = Number(match[3]) - return input * 2 > limit - } - // altimate_change end - function isOpenAiErrorRetryable(e: APICallError) { const status = e.statusCode if (!status) return e.isRetryable @@ -101,9 +88,6 @@ export namespace ProviderError { // - z.ai: can accept overflow silently (needs token-count/context-window checks) function isOverflow(message: string) { if (OVERFLOW_PATTERNS.some((p) => p.test(message))) return true - // altimate_change start — see isInputPlusReservationOverflow - if (isInputPlusReservationOverflow(message)) return true - // altimate_change end // Providers/status patterns handled outside of regex list: // - Cerebras: often returns "400 (no body)" / "413 (no body)" diff --git a/packages/opencode/src/session/llm.ts b/packages/opencode/src/session/llm.ts index 5655f4cad7..96ba6f1906 100644 --- a/packages/opencode/src/session/llm.ts +++ b/packages/opencode/src/session/llm.ts @@ -51,7 +51,7 @@ import { LLMRequestEvent } from "@/altimate/observability/tracing" // altimate_change end // altimate_change start — run deadlines override reasoning effort for each remaining request import { applyBudgetEffort, budgetMessages } from "@/altimate/session/budget-effort" -import { activeRun, effortOverride, hasActiveRuns, toolsDisabled } from "@/altimate/session/run-budget-effort" +import { effortOverride, inheritedRun, toolsDisabled } from "@/altimate/session/run-budget-effort" import { analystEffortOptions } from "@/altimate/session/analyst-effort" // altimate_change start — Claude Agent provider request routing import { ClaudeAgent } from "@/altimate/provider/claude-agent" @@ -91,15 +91,11 @@ export namespace LLM { // altimate_change start — task subagents inherit a live ancestor's run effort without retaining state async function sessionRunBudget(sessionID: string) { - if (!hasActiveRuns()) return undefined - const visited = new Set() - let current: string | undefined = sessionID - while (current && !visited.has(current)) { - if (activeRun(current)) return { effort: effortOverride(current), toolsDisabled: toolsDisabled(current) } - visited.add(current) - current = (await Session.get(SessionID.make(current)).catch(() => undefined))?.parentID - } - return undefined + const run = await inheritedRun( + sessionID, + async (id) => (await Session.get(SessionID.make(id)).catch(() => undefined))?.parentID, + ) + return run === undefined ? undefined : { effort: effortOverride(run), toolsDisabled: toolsDisabled(run) } } // altimate_change end diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index dbdff99163..a84739cd24 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -211,15 +211,11 @@ const SENT_WITHOUT_TOOLS = "sent_without_tools" /** Whether a deadline has disabled tools for this session, inherited from a live ancestor run (as llm.ts applies it). */ async function runToolsDisabled(sessionID: string): Promise { - if (!RunBudgetEffort.hasActiveRuns()) return false - const visited = new Set() - let current: string | undefined = sessionID - while (current && !visited.has(current)) { - if (RunBudgetEffort.activeRun(current)) return RunBudgetEffort.toolsDisabled(current) - visited.add(current) - current = (await Session.get(SessionID.make(current)).catch(() => undefined))?.parentID - } - return false + const run = await RunBudgetEffort.inheritedRun( + sessionID, + async (id) => (await Session.get(SessionID.make(id)).catch(() => undefined))?.parentID, + ) + return run !== undefined && RunBudgetEffort.toolsDisabled(run) } // altimate_change end // altimate_change start — the step-cap instruction for a json_schema request, whose answer is a tool call diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 9dfdacaded..88d6b9da92 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -8,7 +8,7 @@ * "Context budget exceeded before the request was sent" without compaction ever running. */ import { describe, expect, test } from "bun:test" -import { APICallError } from "ai" +import * as RunBudgetEffort from "../../src/altimate/session/run-budget-effort" import { MessageV2 } from "../../src/session/message-v2" import path from "node:path" import { Effect, Layer } from "effect" @@ -812,33 +812,16 @@ describe("calibration tail", () => { }) }) -test("an input-plus-reservation provider error is an overflow only when the input is a large share", () => { - const error = (message: string) => - MessageV2.fromError( - new APICallError({ - message, - url: "https://example.com", - requestBodyValues: {}, - statusCode: 400, - responseHeaders: { "content-type": "application/json" }, - isRetryable: false, - }), - { providerID: "p" as any }, - ) - // A large input: compaction can make room for the reservation. - expect( - MessageV2.ContextOverflowError.isInstance( - error( - "input length and `max_tokens` exceed context limit: 188240 + 21333 > 200000, decrease input length or `max_tokens` and try again", - ), - ), - ).toBe(true) - // A tiny input with an oversized max_tokens: a configuration problem, not compactable. - expect( - MessageV2.ContextOverflowError.isInstance( - error( - "input length and `max_tokens` exceed context limit: 5 + 64000 > 32768, decrease input length or `max_tokens` and try again", - ), - ), - ).toBe(false) +test("the deadline tag follows the run a task child inherits from its ancestors", async () => { + const parents: Record = { child: "parent", parent: "root", root: undefined } + const parentOf = async (id: string) => parents[id] + const end = RunBudgetEffort.begin("parent", true) + try { + expect(await RunBudgetEffort.inheritedRun("child", parentOf)).toBe("parent") + expect(RunBudgetEffort.toolsDisabled((await RunBudgetEffort.inheritedRun("child", parentOf))!)).toBe(true) + expect(await RunBudgetEffort.inheritedRun("root", parentOf)).toBeUndefined() + } finally { + end() + } + expect(await RunBudgetEffort.inheritedRun("child", parentOf)).toBeUndefined() }) From c1f6985db4b1c3e3a96cb8d633beaa065dc4b873 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 09:49:55 -0800 Subject: [PATCH 23/39] fix: account for request overhead in context calibration --- docs/docs/configure/context-management.md | 2 +- packages/opencode/src/session/prompt.ts | 46 ++++++++++---- .../session/context-guard-compaction.test.ts | 60 ++++++++++++++++++- 3 files changed, 93 insertions(+), 15 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index d4564b3090..c03093d806 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -155,6 +155,6 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request is caught before it is sent instead of failing at the provider. When the prompt leaves less room than requested, the response reservation is reduced first; the request is refused only when too little room would remain. -The estimator above runs high on SQL- and JSON-heavy context. When the last response was sent with the full tool set (not a step sent without tools, such as a final, deadline or answer-echo step) from the same model and agent, is not a compaction summary, and reported its input tokens, the check uses that reported usage plus an estimate of what was added since, as long as it is at least 60% of the full estimate (lower usage is distrusted and the full estimate decides). Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, new attachments are added at the check's own allowance, and a different model or agent falls back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. +The estimator above runs high on SQL- and JSON-heavy context. When the last response was sent with the full tool set (not a step sent without tools, such as a final, deadline or answer-echo step) from the same model and agent, is not a compaction summary, and reported its input tokens and recorded its request overhead, the check uses that reported usage plus an estimate of what was added since, as long as it is at least 60% of the full estimate (lower usage is distrusted and the full estimate decides). Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, new attachments are added at the check's own allowance, growth in the system prompt or tools since that response is added, and a different model or agent falls back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when an image or PDF the model has not seen yet is pending (attached or returned by a tool since the model's last response; compaction would drop it), or when auto-compaction is disabled. diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index a84739cd24..dd13dbd138 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -31,7 +31,12 @@ import { familyVendor } from "../provider/family" import { type Tool as AITool, tool, jsonSchema, type ToolCallOptions, asSchema } from "ai" import { SessionCompaction } from "./compaction" // altimate_change start — the context measure shared by compaction and the pre-send budget guard -import { attachmentTokenAllowance, estimateTextTokens, reportedContextTokens } from "@/provider/output-token-budget" +import { + attachmentTokenAllowance, + estimateInputTokens, + estimateTextTokens, + reportedContextTokens, +} from "@/provider/output-token-budget" // altimate_change end import { NudgeArbiter } from "./nudge" import { SessionTermination } from "./termination" @@ -206,8 +211,9 @@ IMPORTANT: - This tool provides your final answer - no further actions are taken after calling it` const STRUCTURED_OUTPUT_SYSTEM_PROMPT = `IMPORTANT: The user has requested structured output. You MUST use the StructuredOutput tool to provide your final response. Do NOT respond with plain text - you MUST call the StructuredOutput tool with your answer formatted according to the schema.` -// altimate_change start — assistant-message metadata key marking a request sent without tools +// altimate_change start — assistant-message metadata for request calibration const SENT_WITHOUT_TOOLS = "sent_without_tools" +const REQUEST_OVERHEAD_TOKENS = "request_overhead_tokens" /** Whether a deadline has disabled tools for this session, inherited from a live ancestor run (as llm.ts applies it). */ async function runToolsDisabled(sessionID: string): Promise { @@ -2256,10 +2262,22 @@ export namespace SessionPrompt { // (provider-reported usage of the last turn on this model plus the estimated tail), and a // guard refusal compacts whenever the proactive check above could have compacted. // The reported usage applies only to a tool-calling step of the same model and agent (same - // tokenizer, system prompt and tools), and only when it counts input: a provider reporting none, + // tokenizer), and only when it counts input: a provider reporting none, // or only output tokens, leaves the estimate in charge. With auto-compaction off a refusal stays the budget error. const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined + const requestTools = echoCommittedAnswer || textFinalStep ? {} : { ...tools } + // Use the same removal rules as LLM.stream so denied tools do not inflate overhead. + for (const name of LLM.removedTools(Object.keys(requestTools), { agent, user: lastUser })) { + delete requestTools[name] + } + const requestOverheadTokens = estimateInputTokens({ + // LLM.stream appends the per-user system prompt to the supplied system instructions. + system: lastUser.system ? [...system, lastUser.system] : system, + messages: [], + tools: requestTools, + }) + const measuredOverheadTokens = measured?.metadata?.[REQUEST_OVERHEAD_TOKENS] // One pass over what the provider has not counted yet (the last measured step and later): // attachments are added to the reported measure at the guard's own allowance. Compaction drops // images and PDFs (replay swaps them for a placeholder, summaries strip them), so one the model @@ -2283,11 +2301,12 @@ export namespace SessionPrompt { } } const compactable = measured !== undefined && loopConfig.compaction?.auto !== false && !unseenMedia - // A step sent without tools (an echo or final step, tagged below) reported usage that can - // undercount this request by the tool schemas; every other step carried the full tool set. + // Usage from a tool-less final step or a message without recorded overhead cannot calibrate. const reportedInputTokens = measured && measured.metadata?.[SENT_WITHOUT_TOOLS] !== true && + typeof measuredOverheadTokens === "number" && + Number.isFinite(measuredOverheadTokens) && measured.providerID === model.providerID && measured.modelID === model.id && measured.agent === lastUser.agent && @@ -2301,15 +2320,20 @@ export namespace SessionPrompt { tokens: measured.tokens, estimatedTokens: guardTail, fraction: guardTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, - }) + attachmentTokens + }) + + attachmentTokens + + Math.max(0, requestOverheadTokens - measuredOverheadTokens) ) })() : undefined // altimate_change end - // altimate_change start — record a request sent without tools; its usage cannot calibrate the guard - // (an answer echo, the final step, or a deadline finalization) - if (echoCommittedAnswer || textFinalStep || (await runToolsDisabled(sessionID))) - processor.message.metadata = { ...processor.message.metadata, [SENT_WITHOUT_TOOLS]: true } + // altimate_change start — record request overhead and mark echo, final, and deadline steps sent without tools + const sentWithoutTools = echoCommittedAnswer || textFinalStep || (await runToolsDisabled(sessionID)) + processor.message.metadata = { + ...processor.message.metadata, + [REQUEST_OVERHEAD_TOKENS]: requestOverheadTokens, + ...(sentWithoutTools ? { [SENT_WITHOUT_TOOLS]: true } : {}), + } // altimate_change end const result = await processor.process({ // altimate_change start — see reportedInputTokens above @@ -2359,7 +2383,7 @@ export namespace SessionPrompt { // altimate_change start - the final echo, and the step-cap answer, cannot spend another step on tools. // Text finalization uses the same toolless shape as compaction and title generation: no // tools and toolChoice "none", so llm.ts flattens historical tool calls for the gateway. - tools: echoCommittedAnswer || textFinalStep ? {} : tools, + tools: requestTools, deferText: echoCommittedAnswer ? deferredText : undefined, // altimate_change end model, diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 88d6b9da92..ce84525ac6 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -197,6 +197,7 @@ type Seeded = { user: string | string[] assistantUsage?: number next?: string + nextSystem?: string last?: { modelID?: string agent?: string @@ -207,6 +208,8 @@ type Seeded = { toolImage?: boolean /** The step's request was sent without tools (an echo or final step). */ sentWithoutTools?: boolean + /** Null represents a message persisted before overhead was recorded. */ + requestOverheadTokens?: number | null } /** An image attached to the `next` user turn. */ nextFile?: boolean @@ -224,13 +227,14 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) permission: [{ permission: "*", pattern: "*", action: "allow" }], }) const sessionID = session.id - const user = Effect.fnUntraced(function* (text: string) { + const user = Effect.fnUntraced(function* (text: string, system?: string) { const msg = yield* sessions.updateMessage({ id: MessageID.ascending(), role: "user", sessionID, agent: "build", model, + system, time: { created: Date.now() }, } as any) yield* sessions.updatePart({ id: PartID.ascending(), messageID: msg.id, sessionID, type: "text", text } as any) @@ -251,7 +255,10 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) parentID, finish: last.finish ?? "stop", time: { created: Date.now(), completed: Date.now() }, - ...(last.sentWithoutTools ? { metadata: { sent_without_tools: true } } : {}), + metadata: { + ...(last.sentWithoutTools ? { sent_without_tools: true } : {}), + ...(last.requestOverheadTokens === null ? {} : { request_overhead_tokens: last.requestOverheadTokens ?? 0 }), + }, } as any) yield* sessions.updatePart({ id: PartID.ascending(), @@ -313,7 +320,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) else if (seeded.assistantUsage !== undefined) yield* answer(msg.id, seeded.assistantUsage, seeded.last) } if (seeded.next !== undefined) { - const next = yield* user(seeded.next) + const next = yield* user(seeded.next, seeded.nextSystem) if (seeded.nextFile) { yield* sessions.updatePart({ id: PartID.ascending(), @@ -371,6 +378,53 @@ it.live( { timeout: 120_000 }, ) +it.live( + "a new system prompt that exceeds the context budget forces compaction", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + yield* llm.text("Summary of the revenue analysis.") + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(130_000), + assistantUsage: 108_000, + last: { requestOverheadTokens: 10 }, + next: "Summarize the revenue concentration.", + nextSystem: sqlJsonHistory(40_000), + }) + + expect(result.summaries.length).toBe(1) + expect(result.errors).toEqual([]) + expect(result.hits.length).toBe(2) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + +it.live( + "a measured step without recorded request overhead cannot calibrate the guard", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + yield* llm.text("Summary of the revenue analysis.") + yield* llm.text("Revenue is concentrated in the top ten customers.") + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(130_000), + assistantUsage: 108_000, + last: { requestOverheadTokens: null }, + next: "Summarize the revenue concentration.", + }) + + expect(result.summaries.length).toBe(1) + expect(result.errors).toEqual([]) + expect(result.hits.length).toBe(2) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + it.live( "when the guard trips on a compactable session it compacts and then sends the request", () => From 3e84d94e04f91f57a5bd9befefc30b565a66799c Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 09:51:11 -0800 Subject: [PATCH 24/39] fix: use the filtered tool set only for the overhead estimate Pass the request's tools to the processor unchanged; the permission-filtered copy feeds only the calibration estimate. Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/session/prompt.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index dd13dbd138..2a1afbed82 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2266,8 +2266,8 @@ export namespace SessionPrompt { // or only output tokens, leaves the estimate in charge. With auto-compaction off a refusal stays the budget error. const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined + // Estimate-only copy of the tools this request sends, with the removal rules LLM.stream applies. const requestTools = echoCommittedAnswer || textFinalStep ? {} : { ...tools } - // Use the same removal rules as LLM.stream so denied tools do not inflate overhead. for (const name of LLM.removedTools(Object.keys(requestTools), { agent, user: lastUser })) { delete requestTools[name] } @@ -2383,7 +2383,7 @@ export namespace SessionPrompt { // altimate_change start - the final echo, and the step-cap answer, cannot spend another step on tools. // Text finalization uses the same toolless shape as compaction and title generation: no // tools and toolChoice "none", so llm.ts flattens historical tool calls for the gateway. - tools: requestTools, + tools: echoCommittedAnswer || textFinalStep ? {} : tools, deferText: echoCommittedAnswer ? deferredText : undefined, // altimate_change end model, From 5082b82a3bcdac4f2247ce1eed5333a90b240ced Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 09:59:02 -0800 Subject: [PATCH 25/39] fix: calibrate context guards using finalized requests Record finalized request overhead and retry context-induced reasoning budget refusals after compaction. --- docs/docs/configure/context-management.md | 4 +- .../src/provider/output-token-budget.ts | 31 ++++++- packages/opencode/src/session/llm.ts | 23 +++-- packages/opencode/src/session/processor.ts | 14 ++- packages/opencode/src/session/prompt.ts | 19 +--- .../session/context-guard-compaction.test.ts | 93 ++++++++++++++++++- 6 files changed, 152 insertions(+), 32 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index c03093d806..badd5f48c9 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -155,6 +155,6 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request is caught before it is sent instead of failing at the provider. When the prompt leaves less room than requested, the response reservation is reduced first; the request is refused only when too little room would remain. -The estimator above runs high on SQL- and JSON-heavy context. When the last response was sent with the full tool set (not a step sent without tools, such as a final, deadline or answer-echo step) from the same model and agent, is not a compaction summary, and reported its input tokens and recorded its request overhead, the check uses that reported usage plus an estimate of what was added since, as long as it is at least 60% of the full estimate (lower usage is distrusted and the full estimate decides). Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, new attachments are added at the check's own allowance, growth in the system prompt or tools since that response is added, and a different model or agent falls back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. +The estimator above runs high on SQL- and JSON-heavy context. When the last response was sent with the full tool set (not a step sent without tools, such as a final, deadline or answer-echo step) from the same model and agent, is not a compaction summary, and reported its input tokens and recorded its request overhead, the check uses that reported usage plus an estimate of what was added since, as long as it is at least 60% of the full estimate (lower usage is distrusted and the full estimate decides). Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, new attachments are added at the check's own allowance, growth in finalized system text, tool schemas, or provider instructions since that response is added, and a different model or agent falls back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. -If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. It only fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when an image or PDF the model has not seen yet is pending (attached or returned by a tool since the model's last response; compaction would drop it), or when auto-compaction is disabled. +If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. This also applies when context pressure reduces the response reservation so far that the reasoning budget cannot be reconciled with the visible output floor. If the original reservation also fails the reasoning budget check, the refusal is a configuration error. The size check fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when an image or PDF the model has not seen yet is pending (attached or returned by a tool since the model's last response; compaction would drop it), or when auto-compaction is disabled. diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index b04986514f..f0493175bd 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -112,6 +112,7 @@ export class ReasoningTokenBudgetError extends Error { readonly path: string readonly configured: number readonly maxOutputTokens: number + readonly contextInduced?: boolean }, ) { super( @@ -443,8 +444,17 @@ export function calibratedInputTokens(estimate: number, reported: number | undef } /** True for the pre-send refusals that compacting the conversation can resolve. */ -export function isContextBudgetError(error: unknown): error is OutputTokenBudgetError | InputTokenBudgetError { - return error instanceof OutputTokenBudgetError || error instanceof InputTokenBudgetError +export function isContextBudgetError( + error: unknown, +): error is + | OutputTokenBudgetError + | InputTokenBudgetError + | (ReasoningTokenBudgetError & { readonly info: { readonly contextInduced: true } }) { + return ( + error instanceof OutputTokenBudgetError || + error instanceof InputTokenBudgetError || + (error instanceof ReasoningTokenBudgetError && error.info.contextInduced === true) + ) } /** Resolve a direct or lazy input estimate after cheap no-op checks have passed. */ @@ -578,10 +588,25 @@ function transformReasoningBudgets( export function clampReasoningBudget( options: Record, maxOutputTokens: number | undefined, + requested?: number, ): Record { if (maxOutputTokens === undefined) return options const ceiling = Math.floor(maxOutputTokens - OUTPUT_TOKEN_FLOOR) - return transformReasoningBudgets(options, ceiling, maxOutputTokens) + try { + return transformReasoningBudgets(options, ceiling, maxOutputTokens) + } catch (error) { + if ( + error instanceof ReasoningTokenBudgetError && + requested !== undefined && + requested > maxOutputTokens + ) { + // A small explicit budget may fit below the usual minimum. Check the original options + // against the original reservation so only refusals compaction can fix are flagged. + clampReasoningBudget(options, requested) + throw new ReasoningTokenBudgetError({ ...error.info, contextInduced: true }) + } + throw error + } } export * as OutputTokenBudget from "./output-token-budget" diff --git a/packages/opencode/src/session/llm.ts b/packages/opencode/src/session/llm.ts index 96ba6f1906..2f2938e985 100644 --- a/packages/opencode/src/session/llm.ts +++ b/packages/opencode/src/session/llm.ts @@ -82,8 +82,11 @@ export namespace LLM { tools: Record retries?: number toolChoice?: "auto" | "required" | "none" - // altimate_change start — provider-reported size of this conversation (see reportedContextTokens) + // altimate_change start — calibrate from a measured step and the finalized request + /** Prior reported usage plus the unmeasured conversation tail and attachments. */ reportedInputTokens?: number + measuredOverheadTokens?: number + onRequestOverhead?: (tokens: number) => void // altimate_change end } @@ -347,9 +350,15 @@ export namespace LLM { // altimate_change start — clamp after every context-affecting request field is finalized. // Tool schemas and provider instructions consume the shared context window, while encoded - // media bytes do not count as literal text. The estimator runs lazily so providers that omit - // maxOutputTokens pay no serialization cost. Known context beta headers widen the catalog - // limit before the clamp. Fixed reasoning budgets are reconciled with the final reservation. + // media bytes do not count as literal text. The history estimate runs lazily. Known context + // beta headers widen the catalog limit before the clamp. + const overhead = estimateInputTokens({ system, messages: [], tools, instructions: params.options.instructions }) + input.onRequestOverhead?.(overhead) + const measuredOverhead = input.measuredOverheadTokens + const reported = + input.reportedInputTokens !== undefined && measuredOverhead !== undefined && Number.isFinite(measuredOverhead) + ? input.reportedInputTokens + Math.max(0, overhead - measuredOverhead) + : undefined const maxOutputTokens = clampOutputTokens({ model: input.model, requested: params.maxOutputTokens, @@ -368,17 +377,17 @@ export namespace LLM { tools, instructions: params.options.instructions, }), - input.reportedInputTokens, + reported, ), }) - // altimate_change start — deadline effort wins configured variants and plugin overrides + // Deadline effort wins configured variants and plugin overrides. const requestOptions = clampReasoningBudget( runBudget?.effort ? applyBudgetEffort(input.model, params.options) : params.options, maxOutputTokens, + params.maxOutputTokens, ) const requestMessages = budgetMessages(input.messages, input.model, requestOptions) // altimate_change end - // altimate_change end // altimate_change start — detect a toolless request once, for the message flattening below const declaresNoTools = Object.keys(tools).filter((x) => x !== "invalid").length === 0 diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index a1a2e63465..876d3a7246 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -536,7 +536,19 @@ export namespace SessionProcessor { // upstream_fix: cancellation must also end a stalled stream open or read const signal = AbortSignal.any([input.abort, effectiveStreamInput.abort, requestController.signal]) const stream = abortableStream( - async () => (await LLM.stream({ ...effectiveStreamInput, abort: signal })).fullStream, + async () => + ( + await LLM.stream({ + ...effectiveStreamInput, + abort: signal, + onRequestOverhead: (tokens) => { + input.assistantMessage.metadata = { + ...input.assistantMessage.metadata, + request_overhead_tokens: tokens, + } + }, + }) + ).fullStream, signal, ) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 2a1afbed82..e73271084b 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -33,7 +33,6 @@ import { SessionCompaction } from "./compaction" // altimate_change start — the context measure shared by compaction and the pre-send budget guard import { attachmentTokenAllowance, - estimateInputTokens, estimateTextTokens, reportedContextTokens, } from "@/provider/output-token-budget" @@ -2266,17 +2265,6 @@ export namespace SessionPrompt { // or only output tokens, leaves the estimate in charge. With auto-compaction off a refusal stays the budget error. const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined - // Estimate-only copy of the tools this request sends, with the removal rules LLM.stream applies. - const requestTools = echoCommittedAnswer || textFinalStep ? {} : { ...tools } - for (const name of LLM.removedTools(Object.keys(requestTools), { agent, user: lastUser })) { - delete requestTools[name] - } - const requestOverheadTokens = estimateInputTokens({ - // LLM.stream appends the per-user system prompt to the supplied system instructions. - system: lastUser.system ? [...system, lastUser.system] : system, - messages: [], - tools: requestTools, - }) const measuredOverheadTokens = measured?.metadata?.[REQUEST_OVERHEAD_TOKENS] // One pass over what the provider has not counted yet (the last measured step and later): // attachments are added to the reported measure at the guard's own allowance. Compaction drops @@ -2321,17 +2309,15 @@ export namespace SessionPrompt { estimatedTokens: guardTail, fraction: guardTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, }) + - attachmentTokens + - Math.max(0, requestOverheadTokens - measuredOverheadTokens) + attachmentTokens ) })() : undefined // altimate_change end - // altimate_change start — record request overhead and mark echo, final, and deadline steps sent without tools + // altimate_change start — mark echo, final, and deadline steps sent without tools const sentWithoutTools = echoCommittedAnswer || textFinalStep || (await runToolsDisabled(sessionID)) processor.message.metadata = { ...processor.message.metadata, - [REQUEST_OVERHEAD_TOKENS]: requestOverheadTokens, ...(sentWithoutTools ? { [SENT_WITHOUT_TOOLS]: true } : {}), } // altimate_change end @@ -2339,6 +2325,7 @@ export namespace SessionPrompt { // altimate_change start — see reportedInputTokens above compactable, reportedInputTokens, + measuredOverheadTokens: typeof measuredOverheadTokens === "number" ? measuredOverheadTokens : undefined, // altimate_change end user: lastUser, // altimate_change start — correlate request-boundary snapshots with this assistant message diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index ce84525ac6..b7149214b4 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -33,6 +33,7 @@ import { ReasoningTokenBudgetError, attachmentTokenAllowance, calibratedInputTokens, + clampReasoningBudget, estimateInputTokens, estimateTextTokens, isContextBudgetError, @@ -113,7 +114,10 @@ const model = { providerID: "altimate-backend", modelID: "test-model" } as any * thousands of estimated tokens); without them each fixture's history alone decides the guard. */ const providerCfg = - (limit: { context: number; output: number }, opts: { tools?: boolean; attachment?: boolean } = {}) => + ( + limit: { context: number; output: number }, + opts: { tools?: boolean; attachment?: boolean; modelOptions?: Record } = {}, + ) => (url: string) => ({ ...(opts.tools ? {} : { agent: { build: { permission: { "*": "deny" as const } } } }), provider: { @@ -133,7 +137,7 @@ const providerCfg = release_date: "2025-01-01", limit, cost: { input: 0, output: 0 }, - options: {}, + options: opts.modelOptions ?? {}, }, }, options: { apiKey: "test-key", baseURL: url }, @@ -363,7 +367,8 @@ it.live( const result = yield* runSession(dir, llm, { user: history, assistantUsage: reported, - // The previous turn ended with a plain text answer, the usual shape at a new user turn. + // The preceding request included the finalized system and tool overhead of the same agent. + last: { requestOverheadTokens: 5_000 }, next: "Summarize the revenue concentration.", }) @@ -402,6 +407,38 @@ it.live( { timeout: 120_000 }, ) +it.live( + "growth in finalized provider instructions forces compaction and is recorded on the retried request", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + yield* llm.text("Summary of the earlier turns.") + yield* llm.text("Here is the answer.") + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(130_000), + assistantUsage: 108_000, + last: { requestOverheadTokens: 5_000 }, + next: "Answer the follow-up question.", + }) + + expect(result.summaries.length).toBe(1) + expect(result.errors).toEqual([]) + expect(result.hits.length).toBe(2) + expect(JSON.stringify(result.hits[0]!.body)).not.toContain("Answer the follow-up question.") + expect(JSON.stringify(result.hits[1]!.body)).toContain("Answer the follow-up question.") + const answer = result.messages.at(-1)?.info + expect(answer?.role === "assistant" ? answer.metadata?.request_overhead_tokens : undefined).toBeGreaterThan( + 40_000, + ) + }), + { + git: true, + config: providerCfg(LARGE_WINDOW, { modelOptions: { instructions: sqlJsonHistory(40_000) } }), + }, + ), + { timeout: 120_000 }, +) + it.live( "a measured step without recorded request overhead cannot calibrate the guard", () => @@ -539,6 +576,36 @@ it.live( { timeout: 120_000 }, ) +it.live( + "a context clamp that leaves too little room for reasoning compacts and retries", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // The reported input leaves about 1,500 output tokens, above the visible floor but + // below what the enabled reasoning budget needs. The original 4,096-token reservation fits. + yield* llm.text("Summary of the earlier turns.") + yield* llm.text("Here is the answer.") + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(13_000), + assistantUsage: 14_500, + last: { requestOverheadTokens: 6_000 }, + next: "Answer the follow-up question.", + }) + + expect(result.errors).toEqual([]) + expect(result.summaries.length).toBe(1) + expect(result.hits.length).toBe(2) + expect(JSON.stringify(result.hits[0]!.body)).not.toContain("Answer the follow-up question.") + expect(JSON.stringify(result.hits[1]!.body)).toContain("Answer the follow-up question.") + }), + { + git: true, + config: providerCfg(SMALL_WINDOW, { modelOptions: { thinking: { budgetTokens: 2_048 } } }), + }, + ), + { timeout: 120_000 }, +) + it.live( "a small-window model whose session fills the window compacts instead of failing", () => @@ -795,6 +862,26 @@ describe("guard calibration", () => { ).toBe(false) expect(isContextBudgetError(new Error("Context budget exceeded before the request was sent."))).toBe(false) }) + + test("reasoning budget refusals signal compaction only when the original reservation could fit", () => { + const smallBudget = { thinking: { budgetTokens: 512 } } + const largeBudget = { thinking: { budgetTokens: 2_048 } } + for (const [options, clamped, requested, compactable] of [ + [largeBudget, 1_200, 4_096, true], + [smallBudget, 1_400, 1_700, true], + [largeBudget, 1_200, 1_500, false], + [largeBudget, 1_200, 1_200, false], + ] as const) { + let refusal: unknown + try { + clampReasoningBudget(options, clamped, requested) + } catch (error) { + refusal = error + } + expect(refusal).toBeInstanceOf(ReasoningTokenBudgetError) + expect(isContextBudgetError(refusal)).toBe(compactable) + } + }) }) describe("attachment allowance", () => { From aa59ce375c458940b9f3393811fc350dbec2c827 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 10:14:28 -0800 Subject: [PATCH 26/39] fix: calibrate context guard from full request estimate growth --- docs/docs/configure/context-management.md | 2 +- .../src/provider/output-token-budget.ts | 8 +- packages/opencode/src/session/llm.ts | 41 ++-- packages/opencode/src/session/processor.ts | 4 +- packages/opencode/src/session/prompt.ts | 109 +++------- .../session/context-guard-compaction.test.ts | 187 +++++++----------- 6 files changed, 119 insertions(+), 232 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index badd5f48c9..5e228996d5 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -155,6 +155,6 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request is caught before it is sent instead of failing at the provider. When the prompt leaves less room than requested, the response reservation is reduced first; the request is refused only when too little room would remain. -The estimator above runs high on SQL- and JSON-heavy context. When the last response was sent with the full tool set (not a step sent without tools, such as a final, deadline or answer-echo step) from the same model and agent, is not a compaction summary, and reported its input tokens and recorded its request overhead, the check uses that reported usage plus an estimate of what was added since, as long as it is at least 60% of the full estimate (lower usage is distrusted and the full estimate decides). Dense and non-ASCII text the provider has not counted yet is charged more conservatively than auto-compaction charges it, new attachments are added at the check's own allowance, growth in finalized system text, tool schemas, or provider instructions since that response is added, and a different model or agent falls back to the full estimate, so a request can still be refused (and compacted) below the compaction threshold. +For the same model and agent, the check starts with the last response's reported input usage, including cached input, and adds any growth in its own estimate of the full request since that response. The growth accounts for changes such as new messages, rewritten history, tools, system text, provider instructions, and attachments. The check caps the calibrated count at the full estimate and uses the full estimate if the calibrated count falls below 60% of it. It also uses the full estimate after a model or agent change, after a compaction summary, or when the last response lacks reported input usage or a recorded request estimate. If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. This also applies when context pressure reduces the response reservation so far that the reasoning budget cannot be reconciled with the visible output floor. If the original reservation also fails the reasoning budget check, the refusal is a configuration error. The size check fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when an image or PDF the model has not seen yet is pending (attached or returned by a tool since the model's last response; compaction would drop it), or when auto-compaction is disabled. diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index f0493175bd..43f6fc2edb 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -284,11 +284,6 @@ function mediaTokenAllowance(part: JsonRecord): number { return MEDIA_TOKEN_ALLOWANCE } -/** The allowance the guard charges for one attachment, for callers sizing unmeasured attachments. */ -export function attachmentTokenAllowance(mime: string, url: string): number { - return mediaTokenAllowance({ type: "file", mediaType: mime, data: url }) -} - /** Mark only actual ModelMessage content parts whose payload is provider media. */ function messageMediaAllowances(messages: readonly unknown[]): WeakMap { const result = new WeakMap() @@ -415,8 +410,7 @@ export type ReportedUsage = { /** * The context size the compaction trigger compares against its threshold: provider-reported usage - * (exact) plus an estimated uncounted tail inflated by 1/fraction. The pre-send guard builds on the - * same measure (with a more conservative tail estimator) instead of the bare character estimate. + * plus an estimated uncounted tail inflated by 1/fraction. */ export function reportedContextTokens(input: { readonly tokens: ReportedUsage diff --git a/packages/opencode/src/session/llm.ts b/packages/opencode/src/session/llm.ts index 2f2938e985..1476d1517a 100644 --- a/packages/opencode/src/session/llm.ts +++ b/packages/opencode/src/session/llm.ts @@ -83,10 +83,10 @@ export namespace LLM { retries?: number toolChoice?: "auto" | "required" | "none" // altimate_change start — calibrate from a measured step and the finalized request - /** Prior reported usage plus the unmeasured conversation tail and attachments. */ - reportedInputTokens?: number - measuredOverheadTokens?: number - onRequestOverhead?: (tokens: number) => void + /** Provider-reported input usage for the measured request, including cache reads and writes. */ + measuredInputTokens?: number + measuredEstimateTokens?: number + onRequestEstimate?: (tokens: number) => void // altimate_change end } @@ -349,15 +349,17 @@ export namespace LLM { // altimate_change end // altimate_change start — clamp after every context-affecting request field is finalized. - // Tool schemas and provider instructions consume the shared context window, while encoded - // media bytes do not count as literal text. The history estimate runs lazily. Known context - // beta headers widen the catalog limit before the clamp. - const overhead = estimateInputTokens({ system, messages: [], tools, instructions: params.options.instructions }) - input.onRequestOverhead?.(overhead) - const measuredOverhead = input.measuredOverheadTokens - const reported = - input.reportedInputTokens !== undefined && measuredOverhead !== undefined && Number.isFinite(measuredOverhead) - ? input.reportedInputTokens + Math.max(0, overhead - measuredOverhead) + const estimateNow = estimateInputTokens({ + system, + messages: ProviderTransform.messagesForInputEstimate(input.messages, input.model), + tools, + instructions: params.options.instructions, + }) + input.onRequestEstimate?.(estimateNow) + const measuredEstimate = input.measuredEstimateTokens + const calibrated = + input.measuredInputTokens !== undefined && measuredEstimate !== undefined && Number.isFinite(measuredEstimate) + ? input.measuredInputTokens + Math.max(0, estimateNow - measuredEstimate) : undefined const maxOutputTokens = clampOutputTokens({ model: input.model, @@ -367,18 +369,7 @@ export namespace LLM { // Provider defaults are lower precedence than the exact case-normalized outgoing record. headerSources: [provider.options, requestHeaders], }), - // The character estimate runs high on SQL/JSON; the provider-reported size of the same - // conversation (when known) keeps this guard on the measure the compaction trigger uses. - inputTokens: () => - calibratedInputTokens( - estimateInputTokens({ - system, - messages: ProviderTransform.messagesForInputEstimate(input.messages, input.model), - tools, - instructions: params.options.instructions, - }), - reported, - ), + inputTokens: calibratedInputTokens(estimateNow, calibrated), }) // Deadline effort wins configured variants and plugin overrides. const requestOptions = clampReasoningBudget( diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 876d3a7246..4fe75bcfcc 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -541,10 +541,10 @@ export namespace SessionProcessor { await LLM.stream({ ...effectiveStreamInput, abort: signal, - onRequestOverhead: (tokens) => { + onRequestEstimate: (tokens) => { input.assistantMessage.metadata = { ...input.assistantMessage.metadata, - request_overhead_tokens: tokens, + request_estimate_tokens: tokens, } }, }) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index e73271084b..68589ab8d3 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -30,13 +30,6 @@ import { familyVendor } from "../provider/family" // altimate_change end import { type Tool as AITool, tool, jsonSchema, type ToolCallOptions, asSchema } from "ai" import { SessionCompaction } from "./compaction" -// altimate_change start — the context measure shared by compaction and the pre-send budget guard -import { - attachmentTokenAllowance, - estimateTextTokens, - reportedContextTokens, -} from "@/provider/output-token-budget" -// altimate_change end import { NudgeArbiter } from "./nudge" import { SessionTermination } from "./termination" import { Instance } from "../project/instance" @@ -210,19 +203,6 @@ IMPORTANT: - This tool provides your final answer - no further actions are taken after calling it` const STRUCTURED_OUTPUT_SYSTEM_PROMPT = `IMPORTANT: The user has requested structured output. You MUST use the StructuredOutput tool to provide your final response. Do NOT respond with plain text - you MUST call the StructuredOutput tool with your answer formatted according to the schema.` -// altimate_change start — assistant-message metadata for request calibration -const SENT_WITHOUT_TOOLS = "sent_without_tools" -const REQUEST_OVERHEAD_TOKENS = "request_overhead_tokens" - -/** Whether a deadline has disabled tools for this session, inherited from a live ancestor run (as llm.ts applies it). */ -async function runToolsDisabled(sessionID: string): Promise { - const run = await RunBudgetEffort.inheritedRun( - sessionID, - async (id) => (await Session.get(SessionID.make(id)).catch(() => undefined))?.parentID, - ) - return run !== undefined && RunBudgetEffort.toolsDisabled(run) -} -// altimate_change end // altimate_change start — the step-cap instruction for a json_schema request, whose answer is a tool call const STRUCTURED_MAX_STEPS = `The maximum number of steps allowed for this task has been reached. Call the StructuredOutput tool now with your final answer formatted according to the schema, based on the work done so far. Do NOT call any other tool and do NOT respond with plain text.` // altimate_change end @@ -2257,75 +2237,40 @@ export namespace SessionPrompt { // altimate_change start — only a request actually sent to the model counts toward the cap modelSteps++ // altimate_change end - // altimate_change start — the pre-send guard measures this request the way isOverflow does - // (provider-reported usage of the last turn on this model plus the estimated tail), and a - // guard refusal compacts whenever the proactive check above could have compacted. - // The reported usage applies only to a tool-calling step of the same model and agent (same - // tokenizer), and only when it counts input: a provider reporting none, - // or only output tokens, leaves the estimate in charge. With auto-compaction off a refusal stays the budget error. + // altimate_change start — calibrate from the last measured request and protect unseen media const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined - const measuredOverheadTokens = measured?.metadata?.[REQUEST_OVERHEAD_TOKENS] - // One pass over what the provider has not counted yet (the last measured step and later): - // attachments are added to the reported measure at the guard's own allowance. Compaction drops - // images and PDFs (replay swaps them for a placeholder, summaries strip them), so one the model - // has not seen yet (a user attachment or tool result since the last measured step) keeps a - // refusal the budget error rather than continue without it. Media already seen does not. - let attachmentTokens = 0 - let unseenMedia = false - for (const msg of msgs) { - if (msg.info.id !== measuredID && !isAfterMeasured(msg.info.id)) continue - for (const part of msg.parts) { - // text/plain files and directories arrive as text parts, already in the tail estimate. - if (part.type === "file" && part.mime !== "text/plain" && part.mime !== "application/x-directory") { - attachmentTokens += attachmentTokenAllowance(part.mime, part.url) - if (MessageV2.isMedia(part.mime)) unseenMedia = true - } - if (part.type !== "tool" || part.state.status !== "completed") continue - for (const attachment of part.state.attachments ?? []) { - attachmentTokens += attachmentTokenAllowance(attachment.mime, attachment.url) - if (MessageV2.isMedia(attachment.mime)) unseenMedia = true - } - } - } + const recordedEstimate = measured?.metadata?.request_estimate_tokens + // Compaction drops images and PDFs, so keep a refusal as a budget error if the + // current request includes media the model has not seen yet. + const unseenMedia = msgs.some( + (msg) => + (msg.info.id === measuredID || isAfterMeasured(msg.info.id)) && + msg.parts.some( + (part) => + (part.type === "file" && MessageV2.isMedia(part.mime)) || + (part.type === "tool" && + part.state.status === "completed" && + part.state.attachments?.some((attachment) => MessageV2.isMedia(attachment.mime))), + ), + ) const compactable = measured !== undefined && loopConfig.compaction?.auto !== false && !unseenMedia - // Usage from a tool-less final step or a message without recorded overhead cannot calibrate. - const reportedInputTokens = + const measuredInputTokens = measured && - measured.metadata?.[SENT_WITHOUT_TOOLS] !== true && - typeof measuredOverheadTokens === "number" && - Number.isFinite(measuredOverheadTokens) && + typeof recordedEstimate === "number" && + Number.isFinite(recordedEstimate) && measured.providerID === model.providerID && measured.modelID === model.id && measured.agent === lastUser.agent && (measured.tokens.input > 0 || measured.tokens.cache.read > 0 || measured.tokens.cache.write > 0) - ? (() => { - // Unmeasured text is sized with the guard's own conservative (multilingual, dense-text) - // estimator, so the discount applies only to what the provider has counted. - const guardTail = estimateUncountedTail(msgs, measured.id, isAfterMeasured, estimateTextTokens, true) - return ( - reportedContextTokens({ - tokens: measured.tokens, - estimatedTokens: guardTail, - fraction: guardTail > 0 ? SessionCompaction.contextSafetyFraction(loopConfig) : 1, - }) + - attachmentTokens - ) - })() + ? measured.tokens.input + measured.tokens.cache.read + measured.tokens.cache.write : undefined // altimate_change end - // altimate_change start — mark echo, final, and deadline steps sent without tools - const sentWithoutTools = echoCommittedAnswer || textFinalStep || (await runToolsDisabled(sessionID)) - processor.message.metadata = { - ...processor.message.metadata, - ...(sentWithoutTools ? { [SENT_WITHOUT_TOOLS]: true } : {}), - } - // altimate_change end const result = await processor.process({ - // altimate_change start — see reportedInputTokens above + // altimate_change start — pass the measured input usage and its matching estimate compactable, - reportedInputTokens, - measuredOverheadTokens: typeof measuredOverheadTokens === "number" ? measuredOverheadTokens : undefined, + measuredInputTokens, + measuredEstimateTokens: typeof recordedEstimate === "number" ? recordedEstimate : undefined, // altimate_change end user: lastUser, // altimate_change start — correlate request-boundary snapshots with this assistant message @@ -3867,9 +3812,6 @@ export namespace SessionPrompt { msgs: MessageV2.WithParts[], lastFinishedID: MessageID | undefined, isAfter: (id: MessageID) => boolean = (id) => id > lastFinishedID!, - estimate: (text: string) => number = Token.estimate, - // Also count the tool-call inputs of later steps (sent back with the history); the pre-send guard does. - countToolInput = false, ): number { if (!lastFinishedID) return 0 const lastFinished = msgs.find((m) => m.info.id === lastFinishedID) @@ -3888,7 +3830,7 @@ export namespace SessionPrompt { } let tokens = 0 for (const part of lastFinished.parts) { - if (part.type === "tool") tokens += estimate(toolText(part)) + if (part.type === "tool") tokens += Token.estimate(toolText(part)) } // filterCompacted deliberately reorders retained-tail and summary messages, // so array position is not chronology. Select genuinely newer messages by @@ -3896,9 +3838,8 @@ export namespace SessionPrompt { for (const m of msgs) { if (m.info.id === lastFinishedID || !isAfter(m.info.id)) continue for (const part of m.parts) { - if (part.type === "text") tokens += estimate(part.text ?? "") - if (part.type === "tool") tokens += estimate(toolText(part)) - if (part.type === "tool" && countToolInput) tokens += estimate(JSON.stringify(part.state.input ?? {})) + if (part.type === "text") tokens += Token.estimate(part.text ?? "") + if (part.type === "tool") tokens += Token.estimate(toolText(part)) } } return tokens diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index b7149214b4..9704c40c16 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -8,7 +8,6 @@ * "Context budget exceeded before the request was sent" without compaction ever running. */ import { describe, expect, test } from "bun:test" -import * as RunBudgetEffort from "../../src/altimate/session/run-budget-effort" import { MessageV2 } from "../../src/session/message-v2" import path from "node:path" import { Effect, Layer } from "effect" @@ -31,7 +30,6 @@ import { InputTokenBudgetError, OutputTokenBudgetError, ReasoningTokenBudgetError, - attachmentTokenAllowance, calibratedInputTokens, clampReasoningBudget, estimateInputTokens, @@ -210,10 +208,8 @@ type Seeded = { toolOutput?: string /** Attach an image to the step's tool result. */ toolImage?: boolean - /** The step's request was sent without tools (an echo or final step). */ - sentWithoutTools?: boolean - /** Null represents a message persisted before overhead was recorded. */ - requestOverheadTokens?: number | null + /** Override the seeded request estimate. Null represents a message without one. */ + requestEstimateTokens?: number | null } /** An image attached to the `next` user turn. */ nextFile?: boolean @@ -244,7 +240,13 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) yield* sessions.updatePart({ id: PartID.ascending(), messageID: msg.id, sessionID, type: "text", text } as any) return msg }) - const answer = Effect.fnUntraced(function* (parentID: MessageID, usage: number, last: Seeded["last"] = {}) { + const answerText = "The top customers by paid revenue are listed above." + const answer = Effect.fnUntraced(function* ( + parentID: MessageID, + usage: number, + requestEstimateTokens: number | null, + last: Seeded["last"] = {}, + ) { const msg = yield* sessions.updateMessage({ id: MessageID.ascending(), role: "assistant", @@ -259,17 +261,14 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) parentID, finish: last.finish ?? "stop", time: { created: Date.now(), completed: Date.now() }, - metadata: { - ...(last.sentWithoutTools ? { sent_without_tools: true } : {}), - ...(last.requestOverheadTokens === null ? {} : { request_overhead_tokens: last.requestOverheadTokens ?? 0 }), - }, + metadata: requestEstimateTokens === null ? undefined : { request_estimate_tokens: requestEstimateTokens }, } as any) yield* sessions.updatePart({ id: PartID.ascending(), messageID: msg.id, sessionID, type: "text", - text: last.text ?? "The top customers by paid revenue are listed above.", + text: last.text ?? answerText, } as any) if (last.toolOutput !== undefined) { yield* sessions.updatePart({ @@ -316,12 +315,21 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) url: `data:image/png;base64,${PNG_BASE64}`, } as any) const turns = Array.isArray(seeded.user) ? seeded.user : [seeded.user] + // Estimate the seeded history as it was before the measured assistant answered. + const history: { role: "user" | "assistant"; content: { type: "text"; text: string }[] }[] = [] for (const [index, text] of turns.entries()) { const msg = yield* user(text) + history.push({ role: "user", content: [{ type: "text", text }] }) const last = index === turns.length - 1 if (last && seeded.userFile) yield* attachImage(msg.id) - if (!last) yield* answer(msg.id, 0) - else if (seeded.assistantUsage !== undefined) yield* answer(msg.id, seeded.assistantUsage, seeded.last) + if (!last) { + yield* answer(msg.id, 0, null) + history.push({ role: "assistant", content: [{ type: "text", text: answerText }] }) + } else if (seeded.assistantUsage !== undefined) { + const override = seeded.last?.requestEstimateTokens + const estimate = override === undefined ? estimateInputTokens({ system: [], messages: history }) : override + yield* answer(msg.id, seeded.assistantUsage, estimate, seeded.last) + } } if (seeded.next !== undefined) { const next = yield* user(seeded.next, seeded.nextSystem) @@ -367,8 +375,14 @@ it.live( const result = yield* runSession(dir, llm, { user: history, assistantUsage: reported, - // The preceding request included the finalized system and tool overhead of the same agent. - last: { requestOverheadTokens: 5_000 }, + last: { + // Include the stable instructions from the hypothetical preceding request. + requestEstimateTokens: + estimateInputTokens({ + system: [], + messages: [{ role: "user", content: [{ type: "text", text: history }] }], + }) + 7_000, + }, next: "Summarize the revenue concentration.", }) @@ -393,7 +407,6 @@ it.live( const result = yield* runSession(dir, llm, { user: sqlJsonTurns(130_000), assistantUsage: 108_000, - last: { requestOverheadTokens: 10 }, next: "Summarize the revenue concentration.", nextSystem: sqlJsonHistory(40_000), }) @@ -408,7 +421,7 @@ it.live( ) it.live( - "growth in finalized provider instructions forces compaction and is recorded on the retried request", + "a rewrite of already-measured history grows the estimate and forces compaction", () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { @@ -417,7 +430,8 @@ it.live( const result = yield* runSession(dir, llm, { user: sqlJsonTurns(130_000), assistantUsage: 108_000, - last: { requestOverheadTokens: 5_000 }, + // Simulate a history transform expanding content present in the measured request. + last: { requestEstimateTokens: 100_000 }, next: "Answer the follow-up question.", }) @@ -426,21 +440,14 @@ it.live( expect(result.hits.length).toBe(2) expect(JSON.stringify(result.hits[0]!.body)).not.toContain("Answer the follow-up question.") expect(JSON.stringify(result.hits[1]!.body)).toContain("Answer the follow-up question.") - const answer = result.messages.at(-1)?.info - expect(answer?.role === "assistant" ? answer.metadata?.request_overhead_tokens : undefined).toBeGreaterThan( - 40_000, - ) }), - { - git: true, - config: providerCfg(LARGE_WINDOW, { modelOptions: { instructions: sqlJsonHistory(40_000) } }), - }, + { git: true, config: providerCfg(LARGE_WINDOW) }, ), { timeout: 120_000 }, ) it.live( - "a measured step without recorded request overhead cannot calibrate the guard", + "a measured step without a recorded request estimate cannot calibrate the guard", () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { @@ -449,7 +456,7 @@ it.live( const result = yield* runSession(dir, llm, { user: sqlJsonTurns(130_000), assistantUsage: 108_000, - last: { requestOverheadTokens: null }, + last: { requestEstimateTokens: null }, next: "Summarize the revenue concentration.", }) @@ -520,24 +527,47 @@ it.live( ) it.live( - "a final step sent without tools is tagged so its usage never calibrates the guard", + "the processor records the full request estimate on the sent assistant message", () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { - // steps: 1 makes the first request the final step, sent without tools. - yield* llm.text("Revenue is concentrated in the top ten customers.") - const result = yield* runSession(dir, llm, { user: "Summarize the revenue concentration." }) + yield* llm.text("Here is the answer.") + const result = yield* runSession(dir, llm, { user: sqlJsonHistory(20_000) }) - const answer = result.messages.find((m) => m.info.role === "assistant")?.info as any - expect(answer?.metadata?.sent_without_tools).toBe(true) + expect(result.errors).toEqual([]) + expect(result.hits.length).toBe(1) + const answer = result.messages.at(-1)?.info + expect(answer?.role === "assistant" ? answer.metadata?.request_estimate_tokens : undefined).toBeGreaterThan( + 20_000, + ) }), - { - git: true, - config: (url: string) => ({ - ...providerCfg(LARGE_WINDOW, { tools: true })(url), - agent: { build: { steps: 1 } }, - }), - }, + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + +it.live( + "history added since the measured request reduces the output reservation", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + yield* llm.text("Here is the answer.") + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(130_000), + assistantUsage: 108_000, + last: { text: sqlJsonHistory(12_000) }, + next: "Answer the follow-up question.", + }) + + expect(result.errors).toEqual([]) + expect(result.compactions).toHaveLength(0) + expect(result.hits).toHaveLength(1) + // The assistant's last output was absent from the measured request and now + // consumes enough context to squeeze the reservation without forcing compaction. + expect(result.hits[0]!.body.max_tokens).toBeGreaterThan(1_024) + expect(result.hits[0]!.body.max_tokens).toBeLessThan(LARGE_WINDOW.output) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, ), { timeout: 120_000 }, ) @@ -588,7 +618,6 @@ it.live( const result = yield* runSession(dir, llm, { user: sqlJsonTurns(13_000), assistantUsage: 14_500, - last: { requestOverheadTokens: 6_000 }, next: "Answer the follow-up question.", }) @@ -635,12 +664,10 @@ it.live( ) // Usage counted for another model (different tokenizer), another agent (different system prompt and -// tools) or a step sent without tools (an echo or final step) does not describe this request: the estimate -// decides, trips, and the session compacts. +// tools) falls back to the full estimate, which forces compaction. for (const [name, last] of [ ["another model", { ...TOOL_STEP, modelID: "another-model" }], ["another agent", { ...TOOL_STEP, agent: "plan" }], - ["a step sent without tools", { sentWithoutTools: true }], ] as const) { it.live( `usage reported by ${name} is not trusted: the guard trips and the session compacts`, @@ -884,65 +911,13 @@ describe("guard calibration", () => { }) }) -describe("attachment allowance", () => { - test("charges an unmeasured attachment what the guard's own estimate charges it", () => { - const png = `data:image/png;base64,${PNG_BASE64}` - const viaEstimate = - estimateInputTokens({ - system: [], - messages: [{ role: "user", content: [{ type: "file", mediaType: "image/png", data: png }] }], - }) - estimateInputTokens({ system: [], messages: [{ role: "user", content: [] }] }) - expect(attachmentTokenAllowance("image/png", png)).toBe(2_048) - expect(viaEstimate).toBeGreaterThanOrEqual(2_048) - expect(attachmentTokenAllowance("application/pdf", "https://example.com/report.pdf")).toBeGreaterThanOrEqual(32_768) - }) -}) - -describe("calibration tail", () => { +describe("proactive compaction tail", () => { const text = (id: string, role: "user" | "assistant", body: string) => ({ info: { id, sessionID: "s", role, time: { created: 1 } }, parts: [{ id: `${id}-p`, sessionID: "s", messageID: id, type: "text", text: body }], }) as unknown as MessageV2.WithParts - test("unmeasured multilingual text is sized with the guard's conservative estimator", () => { - const cjk = "\u6536\u5165\u96c6\u4e2d\u5728\u524d\u5341\u540d\u5ba2\u6237".repeat(5_000) - const msgs = [text("m1", "user", "task"), text("m2", "assistant", "done"), text("m3", "user", cjk)] - const plain = SessionPrompt.estimateUncountedTail(msgs, "m2" as any) - const guard = SessionPrompt.estimateUncountedTail(msgs, "m2" as any, undefined, estimateTextTokens) - expect(plain).toBe(Token.estimate(cjk)) - // At least one token per non-ASCII character, where the plain ratio gives ~1 per 3.7. - expect(guard).toBeGreaterThanOrEqual(cjk.length) - }) - - test("the guard's tail also counts tool-call inputs of later steps", () => { - const step = { - info: { id: "m3", sessionID: "s", role: "assistant", time: { created: 1 } }, - parts: [ - { - id: "m3-t", - sessionID: "s", - messageID: "m3", - type: "tool", - tool: "bash", - callID: "c", - state: { - status: "completed", - input: { query: "q".repeat(4_000) }, - output: "ok", - title: "t", - metadata: {}, - time: { start: 1, end: 2 }, - }, - }, - ], - } as unknown as MessageV2.WithParts - const msgs = [text("m1", "user", "task"), text("m2", "assistant", "done"), step] - const plain = SessionPrompt.estimateUncountedTail(msgs, "m2" as any) - const withInput = SessionPrompt.estimateUncountedTail(msgs, "m2" as any, undefined, undefined, true) - expect(withInput - plain).toBeGreaterThanOrEqual(Token.estimate(JSON.stringify({ query: "q".repeat(4_000) }))) - }) - test("follows the caller's stored order, not lexical IDs", () => { // A later message with a lexically smaller caller-supplied ID. const msgs = [text("m5", "user", "task"), text("m6", "assistant", "done"), text("a1", "user", "y".repeat(9_000))] @@ -952,17 +927,3 @@ describe("calibration tail", () => { ) }) }) - -test("the deadline tag follows the run a task child inherits from its ancestors", async () => { - const parents: Record = { child: "parent", parent: "root", root: undefined } - const parentOf = async (id: string) => parents[id] - const end = RunBudgetEffort.begin("parent", true) - try { - expect(await RunBudgetEffort.inheritedRun("child", parentOf)).toBe("parent") - expect(RunBudgetEffort.toolsDisabled((await RunBudgetEffort.inheritedRun("child", parentOf))!)).toBe(true) - expect(await RunBudgetEffort.inheritedRun("root", parentOf)).toBeUndefined() - } finally { - end() - } - expect(await RunBudgetEffort.inheritedRun("child", parentOf)).toBeUndefined() -}) From e41271d2c16ae61a1f4eece1ed932f2800aef032 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 10:19:36 -0800 Subject: [PATCH 27/39] fix: keep guard estimates lazy and share the metadata key --- .../src/provider/output-token-budget.ts | 2 ++ packages/opencode/src/session/llm.ts | 31 +++++++++++-------- packages/opencode/src/session/processor.ts | 4 +-- packages/opencode/src/session/prompt.ts | 5 ++- .../session/context-guard-compaction.test.ts | 10 +++--- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index 43f6fc2edb..3090e3662f 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -9,6 +9,8 @@ const log = Log.create({ service: "provider.output-token-budget" }) /** Smallest completion budget worth sending for an agent turn. */ export const OUTPUT_TOKEN_FLOOR = 1_024 +export const REQUEST_ESTIMATE_METADATA_KEY = "request_estimate_tokens" + const CLAMP_MARGIN_FRACTION = 0.02 const CLAMP_MARGIN_MIN = 512 const ESTIMATE_CHUNK_SIZE = 400 diff --git a/packages/opencode/src/session/llm.ts b/packages/opencode/src/session/llm.ts index 1476d1517a..d8496af153 100644 --- a/packages/opencode/src/session/llm.ts +++ b/packages/opencode/src/session/llm.ts @@ -349,18 +349,6 @@ export namespace LLM { // altimate_change end // altimate_change start — clamp after every context-affecting request field is finalized. - const estimateNow = estimateInputTokens({ - system, - messages: ProviderTransform.messagesForInputEstimate(input.messages, input.model), - tools, - instructions: params.options.instructions, - }) - input.onRequestEstimate?.(estimateNow) - const measuredEstimate = input.measuredEstimateTokens - const calibrated = - input.measuredInputTokens !== undefined && measuredEstimate !== undefined && Number.isFinite(measuredEstimate) - ? input.measuredInputTokens + Math.max(0, estimateNow - measuredEstimate) - : undefined const maxOutputTokens = clampOutputTokens({ model: input.model, requested: params.maxOutputTokens, @@ -369,7 +357,24 @@ export namespace LLM { // Provider defaults are lower precedence than the exact case-normalized outgoing record. headerSources: [provider.options, requestHeaders], }), - inputTokens: calibratedInputTokens(estimateNow, calibrated), + inputTokens: () => { + const estimate = estimateInputTokens({ + system, + messages: ProviderTransform.messagesForInputEstimate(input.messages, input.model), + tools, + instructions: params.options.instructions, + }) + input.onRequestEstimate?.(estimate) + const { measuredInputTokens, measuredEstimateTokens } = input + const calibrated = + typeof measuredInputTokens === "number" && + Number.isFinite(measuredInputTokens) && + typeof measuredEstimateTokens === "number" && + Number.isFinite(measuredEstimateTokens) + ? measuredInputTokens + Math.max(0, estimate - measuredEstimateTokens) + : undefined + return calibratedInputTokens(estimate, calibrated) + }, }) // Deadline effort wins configured variants and plugin overrides. const requestOptions = clampReasoningBudget( diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 4fe75bcfcc..ac6ca8fcf8 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -21,7 +21,7 @@ import type { Provider } from "@/provider/provider" import { LLM } from "./llm" import { Config } from "@/config/config" // altimate_change start — recognize pre-send context budget refusals -import { isContextBudgetError } from "@/provider/output-token-budget" +import { isContextBudgetError, REQUEST_ESTIMATE_METADATA_KEY } from "@/provider/output-token-budget" // altimate_change end import { SessionCompaction } from "./compaction" import { PermissionNext } from "@/permission/next" @@ -544,7 +544,7 @@ export namespace SessionProcessor { onRequestEstimate: (tokens) => { input.assistantMessage.metadata = { ...input.assistantMessage.metadata, - request_estimate_tokens: tokens, + [REQUEST_ESTIMATE_METADATA_KEY]: tokens, } }, }) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 68589ab8d3..b3a4a37b33 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -30,6 +30,9 @@ import { familyVendor } from "../provider/family" // altimate_change end import { type Tool as AITool, tool, jsonSchema, type ToolCallOptions, asSchema } from "ai" import { SessionCompaction } from "./compaction" +// altimate_change start — read the persisted request estimate for context calibration +import { REQUEST_ESTIMATE_METADATA_KEY } from "@/provider/output-token-budget" +// altimate_change end import { NudgeArbiter } from "./nudge" import { SessionTermination } from "./termination" import { Instance } from "../project/instance" @@ -2240,7 +2243,7 @@ export namespace SessionPrompt { // altimate_change start — calibrate from the last measured request and protect unseen media const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined - const recordedEstimate = measured?.metadata?.request_estimate_tokens + const recordedEstimate = measured?.metadata?.[REQUEST_ESTIMATE_METADATA_KEY] // Compaction drops images and PDFs, so keep a refusal as a budget error if the // current request includes media the model has not seen yet. const unseenMedia = msgs.some( diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 9704c40c16..da77625f74 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -30,6 +30,7 @@ import { InputTokenBudgetError, OutputTokenBudgetError, ReasoningTokenBudgetError, + REQUEST_ESTIMATE_METADATA_KEY, calibratedInputTokens, clampReasoningBudget, estimateInputTokens, @@ -261,7 +262,8 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) parentID, finish: last.finish ?? "stop", time: { created: Date.now(), completed: Date.now() }, - metadata: requestEstimateTokens === null ? undefined : { request_estimate_tokens: requestEstimateTokens }, + metadata: + requestEstimateTokens === null ? undefined : { [REQUEST_ESTIMATE_METADATA_KEY]: requestEstimateTokens }, } as any) yield* sessions.updatePart({ id: PartID.ascending(), @@ -537,9 +539,9 @@ it.live( expect(result.errors).toEqual([]) expect(result.hits.length).toBe(1) const answer = result.messages.at(-1)?.info - expect(answer?.role === "assistant" ? answer.metadata?.request_estimate_tokens : undefined).toBeGreaterThan( - 20_000, - ) + expect( + answer?.role === "assistant" ? answer.metadata?.[REQUEST_ESTIMATE_METADATA_KEY] : undefined, + ).toBeGreaterThan(20_000) }), { git: true, config: providerCfg(LARGE_WINDOW) }, ), From 2974cdbf18d7f2f3d5b386fad55ca6acefa0bdef Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 10:32:06 -0800 Subject: [PATCH 28/39] fix: remove context guard leftovers and cover recovery paths --- .../src/altimate/session/run-budget-effort.ts | 16 --- .../src/provider/output-token-budget.ts | 4 +- packages/opencode/src/session/llm.ts | 16 ++- .../session/context-guard-compaction.test.ts | 109 +++++++++++++++++- 4 files changed, 117 insertions(+), 28 deletions(-) diff --git a/packages/opencode/src/altimate/session/run-budget-effort.ts b/packages/opencode/src/altimate/session/run-budget-effort.ts index ca433ad422..5c497ad41d 100644 --- a/packages/opencode/src/altimate/session/run-budget-effort.ts +++ b/packages/opencode/src/altimate/session/run-budget-effort.ts @@ -40,19 +40,3 @@ export function hasActiveRuns(): boolean { export function activeRun(sessionID: string): boolean { return active.has(sessionID) } - -/** The session (this one or the nearest ancestor) whose live run governs it; task subagents inherit it. */ -export async function inheritedRun( - sessionID: string, - parentOf: (sessionID: string) => Promise, -): Promise { - if (!hasActiveRuns()) return undefined - const visited = new Set() - let current: string | undefined = sessionID - while (current && !visited.has(current)) { - if (activeRun(current)) return current - visited.add(current) - current = await parentOf(current) - } - return undefined -} diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index 3090e3662f..0d1719bd63 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -182,7 +182,7 @@ function denseAsciiCharacters(input: string): number { } /** Estimate heterogeneous text in small chunks and conservatively count dense or non-ASCII text. */ -export function estimateTextTokens(input: string): number { +function estimateTextTokens(input: string): number { let total = 0 for (let offset = 0; offset < input.length; offset += ESTIMATE_CHUNK_SIZE) { const chunk = input.slice(offset, offset + ESTIMATE_CHUNK_SIZE) @@ -403,7 +403,7 @@ export function estimateInputTokens(input: { } /** Provider-reported token usage of one finished assistant turn. */ -export type ReportedUsage = { +type ReportedUsage = { readonly total?: number readonly input: number readonly output: number diff --git a/packages/opencode/src/session/llm.ts b/packages/opencode/src/session/llm.ts index d8496af153..1ff4da286c 100644 --- a/packages/opencode/src/session/llm.ts +++ b/packages/opencode/src/session/llm.ts @@ -51,7 +51,7 @@ import { LLMRequestEvent } from "@/altimate/observability/tracing" // altimate_change end // altimate_change start — run deadlines override reasoning effort for each remaining request import { applyBudgetEffort, budgetMessages } from "@/altimate/session/budget-effort" -import { effortOverride, inheritedRun, toolsDisabled } from "@/altimate/session/run-budget-effort" +import { activeRun, effortOverride, hasActiveRuns, toolsDisabled } from "@/altimate/session/run-budget-effort" import { analystEffortOptions } from "@/altimate/session/analyst-effort" // altimate_change start — Claude Agent provider request routing import { ClaudeAgent } from "@/altimate/provider/claude-agent" @@ -94,11 +94,15 @@ export namespace LLM { // altimate_change start — task subagents inherit a live ancestor's run effort without retaining state async function sessionRunBudget(sessionID: string) { - const run = await inheritedRun( - sessionID, - async (id) => (await Session.get(SessionID.make(id)).catch(() => undefined))?.parentID, - ) - return run === undefined ? undefined : { effort: effortOverride(run), toolsDisabled: toolsDisabled(run) } + if (!hasActiveRuns()) return undefined + const visited = new Set() + let current: string | undefined = sessionID + while (current && !visited.has(current)) { + if (activeRun(current)) return { effort: effortOverride(current), toolsDisabled: toolsDisabled(current) } + visited.add(current) + current = (await Session.get(SessionID.make(current)).catch(() => undefined))?.parentID + } + return undefined } // altimate_change end diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index da77625f74..31f2f36afc 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -7,12 +7,13 @@ * so a session below the compaction threshold used to be refused by the guard and end with * "Context budget exceeded before the request was sent" without compaction ever running. */ -import { describe, expect, test } from "bun:test" +import { describe, expect, spyOn, test } from "bun:test" import { MessageV2 } from "../../src/session/message-v2" import path from "node:path" import { Effect, Layer } from "effect" import { LayerNode } from "@opencode-ai/core/effect/layer-node" import { Session } from "@/session/session" +import { NudgeArbiter } from "../../src/session/nudge" import { SessionPrompt } from "../../src/session/prompt" import { Token } from "../../src/util/token" import { SessionSummary } from "../../src/session/summary" @@ -25,6 +26,7 @@ import { provideTmpdirServerLegacy } from "./legacy-instance" import { LSP } from "@/lsp/lsp" import { MCP } from "../../src/mcp" import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner" +import { Provider } from "../../src/provider/provider" import { RuntimeFlags } from "@/effect/runtime-flags" import { InputTokenBudgetError, @@ -34,7 +36,6 @@ import { calibratedInputTokens, clampReasoningBudget, estimateInputTokens, - estimateTextTokens, isContextBudgetError, reportedContextTokens, } from "../../src/provider/output-token-budget" @@ -207,6 +208,7 @@ type Seeded = { finish?: string text?: string toolOutput?: string + tool?: "answer_commit" /** Attach an image to the step's tool result. */ toolImage?: boolean /** Override the seeded request estimate. Null represents a message without one. */ @@ -233,7 +235,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) id: MessageID.ascending(), role: "user", sessionID, - agent: "build", + agent: seeded.last?.tool === "answer_commit" ? "analyst" : "build", model, system, time: { created: Date.now() }, @@ -279,7 +281,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) sessionID, type: "tool", callID: "call_revenue_query", - tool: "bash", + tool: last.tool ?? "bash", state: { status: "completed", input: { command: "run the revenue query" }, @@ -637,6 +639,105 @@ it.live( { timeout: 120_000 }, ) +it.live( + "a separate input limit makes the guard compact and retry", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + // The input limit is below the proactive reserve. The history fits the context + // window, so only the pre-send input limit can trigger this compaction. + // The config-to-provider conversion does not carry a declared input limit. + const getModel = Provider.getModel + const modelSpy = spyOn(Provider, "getModel").mockImplementation(async (...args) => { + const resolved = await getModel(...args) + return { ...resolved, limit: { ...resolved.limit, input: 16_384 } } + }) + try { + yield* llm.text("Summary of the earlier turns.") + yield* llm.text("Here is the answer.") + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(24_000), + assistantUsage: 0, + next: "Answer the follow-up question.", + }) + + expect(result.errors).toEqual([]) + expect(result.summaries).toHaveLength(1) + expect(result.hits).toHaveLength(2) + expect(JSON.stringify(result.hits[0]!.body)).not.toContain("Answer the follow-up question.") + expect(JSON.stringify(result.hits[1]!.body)).toContain("Answer the follow-up question.") + } finally { + modelSpy.mockRestore() + } + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + +it.live( + "a refused echo delivers the committed answer through overflow recovery", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + const committed = "This is the committed answer." + const result = yield* runSession(dir, llm, { + user: sqlJsonHistory(135_000), + assistantUsage: 0, + last: { agent: "analyst", finish: "tool-calls", tool: "answer_commit", toolOutput: committed }, + }) + + expect(result.hits).toHaveLength(0) + expect(result.summaries).toHaveLength(0) + expect(result.errors).toEqual([]) + const echo = result.messages.at(-1) + expect(echo?.info).toHaveProperty("metadata.answer_commit_echo_error.name", "ContextOverflowError") + expect(echo?.parts).toContainEqual(expect.objectContaining({ type: "text", text: committed })) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + +it.live( + "a refused request hands its directive back to the retry", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + const directive = "Keep this directive for the next working request." + const begin = NudgeArbiter.begin + const beginSpy = spyOn(NudgeArbiter, "begin").mockImplementation((sessionID) => { + const generation = begin(sessionID) + NudgeArbiter.register(sessionID, { source: "budget_reminder", kind: "budget", text: directive }, generation) + return generation + }) + const runMode = process.env.ALTIMATE_RUN_MODE + process.env.ALTIMATE_RUN_MODE = "1" + try { + yield* llm.text("Summary of the earlier turns.") + yield* llm.text("Here is the answer.") + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(135_000), + assistantUsage: 0, + next: "Answer the follow-up question.", + }) + + expect(result.errors).toEqual([]) + expect(result.summaries).toHaveLength(1) + expect(result.hits).toHaveLength(2) + expect(JSON.stringify(result.hits[0]!.body)).not.toContain(directive) + expect(JSON.stringify(result.hits[1]!.body)).toContain(directive) + } finally { + beginSpy.mockRestore() + if (runMode === undefined) delete process.env.ALTIMATE_RUN_MODE + else process.env.ALTIMATE_RUN_MODE = runMode + } + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + it.live( "a small-window model whose session fills the window compacts instead of failing", () => From 0dac29a78e91132451a9ed4e04a832aec4ecd315 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 10:48:26 -0800 Subject: [PATCH 29/39] fix: clarify attachment refusals and count retried directives once Protect unseen input attachments while allowing model-generated files to compact. Verify that provider, model, and agent changes each disable guard calibration. --- docs/docs/configure/context-management.md | 2 +- packages/opencode/src/session/processor.ts | 34 ++++-- packages/opencode/src/session/prompt.ts | 10 +- .../session/context-guard-compaction.test.ts | 113 ++++++++++++------ 4 files changed, 113 insertions(+), 46 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index 5e228996d5..feee8f0b8a 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -157,4 +157,4 @@ Before each request, altimate checks that the prompt plus the space reserved for For the same model and agent, the check starts with the last response's reported input usage, including cached input, and adds any growth in its own estimate of the full request since that response. The growth accounts for changes such as new messages, rewritten history, tools, system text, provider instructions, and attachments. The check caps the calibrated count at the full estimate and uses the full estimate if the calibrated count falls below 60% of it. It also uses the full estimate after a model or agent change, after a compaction summary, or when the last response lacks reported input usage or a recorded request estimate. -If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. This also applies when context pressure reduces the response reservation so far that the reasoning budget cannot be reconciled with the visible output floor. If the original reservation also fails the reasoning budget check, the refusal is a configuration error. The size check fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), when an image or PDF the model has not seen yet is pending (attached or returned by a tool since the model's last response; compaction would drop it), or when auto-compaction is disabled. +If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. This also applies when context pressure reduces the response reservation so far that the reasoning budget cannot be reconciled with the visible output floor. If the original reservation also fails the reasoning budget check, the refusal is a configuration error. The size check fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), or when auto-compaction is disabled. If an unseen image or PDF blocks auto-compaction, the error suggests running `/compact` and reattaching the file, or removing it. diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index ac6ca8fcf8..1bc30a7f03 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -22,6 +22,7 @@ import { LLM } from "./llm" import { Config } from "@/config/config" // altimate_change start — recognize pre-send context budget refusals import { isContextBudgetError, REQUEST_ESTIMATE_METADATA_KEY } from "@/provider/output-token-budget" +import { NamedError } from "@opencode-ai/util/error" // altimate_change end import { SessionCompaction } from "./compaction" import { PermissionNext } from "@/permission/next" @@ -87,6 +88,10 @@ async function interruptedToolPart(part: MessageV2.ToolPart, capTokens: number): } // altimate_change end +// altimate_change start — track each directive once across a pre-send refusal and retry +const trackedDirectives = new WeakSet() +// altimate_change end + export namespace SessionProcessor { const DOOM_LOOP_THRESHOLD = 3 // altimate_change start — per-tool repeat threshold to catch varied-input loops (e.g. todowrite 2,080x) @@ -393,11 +398,13 @@ export namespace SessionProcessor { async process({ deferText, compactable, + compactBlockedByMedia, ...streamInput }: LLM.StreamInput & { deferText?: MessageV2.TextPart[] // The prompt loop can compact this conversation (a finished non-summary turn exists). compactable?: boolean + compactBlockedByMedia?: boolean }) { // altimate_change end log.info("process") @@ -479,14 +486,18 @@ export namespace SessionProcessor { if (directive.kind === "starvation") return "starvation" as const return "nudge" as const })() - Telemetry.track({ - type: "starvation_breaker", - timestamp: Date.now(), - session_id: input.sessionID, - mode: sbMode, - kind: telemetryKind, - action: "injected", - }) + // A handed-back directive has already been counted. + if (!trackedDirectives.has(directive)) { + trackedDirectives.add(directive) + Telemetry.track({ + type: "starvation_breaker", + timestamp: Date.now(), + session_id: input.sessionID, + mode: sbMode, + kind: telemetryKind, + action: "injected", + }) + } log.info("nudge arbiter directive injected", { sessionID: input.sessionID, source: directive.source, @@ -1393,6 +1404,13 @@ export namespace SessionProcessor { ).toObject() } else if (echoRecovery && isContextBudgetError(e)) { error = new MessageV2.ContextOverflowError({ message: e.message }, { cause: e }).toObject() + } else if (compactBlockedByMedia && isContextBudgetError(e)) { + error = new NamedError.Unknown( + { + message: "The conversation plus the new attachment does not fit the model's context window. Run /compact, then attach it again, or remove the attachment.", + }, + { cause: e }, + ).toObject() } // altimate_change end // altimate_change start — committed echoes and summaries retain recoverable causes without a fatal event diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index b3a4a37b33..151a1cae52 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2251,13 +2251,16 @@ export namespace SessionPrompt { (msg.info.id === measuredID || isAfterMeasured(msg.info.id)) && msg.parts.some( (part) => - (part.type === "file" && MessageV2.isMedia(part.mime)) || + (msg.info.role === "user" && + msg.info.id !== measuredID && + part.type === "file" && + MessageV2.isMedia(part.mime)) || (part.type === "tool" && part.state.status === "completed" && part.state.attachments?.some((attachment) => MessageV2.isMedia(attachment.mime))), ), ) - const compactable = measured !== undefined && loopConfig.compaction?.auto !== false && !unseenMedia + const canAutoCompact = measured !== undefined && loopConfig.compaction?.auto !== false const measuredInputTokens = measured && typeof recordedEstimate === "number" && @@ -2271,7 +2274,8 @@ export namespace SessionPrompt { // altimate_change end const result = await processor.process({ // altimate_change start — pass the measured input usage and its matching estimate - compactable, + compactable: canAutoCompact && !unseenMedia, + compactBlockedByMedia: canAutoCompact && unseenMedia, measuredInputTokens, measuredEstimateTokens: typeof recordedEstimate === "number" ? recordedEstimate : undefined, // altimate_change end diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 31f2f36afc..06cb6bd235 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -13,6 +13,7 @@ import path from "node:path" import { Effect, Layer } from "effect" import { LayerNode } from "@opencode-ai/core/effect/layer-node" import { Session } from "@/session/session" +import { Telemetry } from "@/altimate/telemetry" import { NudgeArbiter } from "../../src/session/nudge" import { SessionPrompt } from "../../src/session/prompt" import { Token } from "../../src/util/token" @@ -147,8 +148,6 @@ const providerCfg = }) const LARGE_WINDOW = { context: 131_072, output: 16_384 } -/** A finished step that made a tool call: its reported usage covered the full tool set. */ -const TOOL_STEP = { toolOutput: "42 customers above the revenue threshold." } /** A 1x1 PNG. */ const PNG_BASE64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==" const SMALL_WINDOW = { context: 16_384, output: 4_096 } @@ -204,9 +203,11 @@ type Seeded = { nextSystem?: string last?: { modelID?: string + providerID?: string agent?: string finish?: string text?: string + file?: boolean toolOutput?: string tool?: "answer_commit" /** Attach an image to the step's tool result. */ @@ -243,6 +244,16 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) yield* sessions.updatePart({ id: PartID.ascending(), messageID: msg.id, sessionID, type: "text", text } as any) return msg }) + const attachImage = (messageID: MessageID) => + sessions.updatePart({ + id: PartID.ascending(), + messageID, + sessionID, + type: "file", + mime: "image/png", + filename: "revenue-chart.png", + url: `data:image/png;base64,${PNG_BASE64}`, + } as any) const answerText = "The top customers by paid revenue are listed above." const answer = Effect.fnUntraced(function* ( parentID: MessageID, @@ -260,7 +271,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) cost: 0, tokens: { input: usage, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, modelID: last.modelID ?? model.modelID, - providerID: model.providerID, + providerID: last.providerID ?? model.providerID, parentID, finish: last.finish ?? "stop", time: { created: Date.now(), completed: Date.now() }, @@ -274,6 +285,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) type: "text", text: last.text ?? answerText, } as any) + if (last.file) yield* attachImage(msg.id) if (last.toolOutput !== undefined) { yield* sessions.updatePart({ id: PartID.ascending(), @@ -308,16 +320,6 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) } as any) } }) - const attachImage = (messageID: MessageID) => - sessions.updatePart({ - id: PartID.ascending(), - messageID, - sessionID, - type: "file", - mime: "image/png", - filename: "revenue-chart.png", - url: `data:image/png;base64,${PNG_BASE64}`, - } as any) const turns = Array.isArray(seeded.user) ? seeded.user : [seeded.user] // Estimate the seeded history as it was before the measured assistant answered. const history: { role: "user" | "assistant"; content: { type: "text"; text: string }[] }[] = [] @@ -337,17 +339,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) } if (seeded.next !== undefined) { const next = yield* user(seeded.next, seeded.nextSystem) - if (seeded.nextFile) { - yield* sessions.updatePart({ - id: PartID.ascending(), - messageID: next.id, - sessionID, - type: "file", - mime: "image/png", - filename: "revenue-chart.png", - url: `data:image/png;base64,${PNG_BASE64}`, - } as any) - } + if (seeded.nextFile) yield* attachImage(next.id) } yield* prompt.loop({ sessionID }) @@ -700,7 +692,7 @@ it.live( ) it.live( - "a refused request hands its directive back to the retry", + "a refused request hands its directive back to the retry and tracks it once", () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { @@ -712,6 +704,7 @@ it.live( return generation }) const runMode = process.env.ALTIMATE_RUN_MODE + const track = spyOn(Telemetry, "track") process.env.ALTIMATE_RUN_MODE = "1" try { yield* llm.text("Summary of the earlier turns.") @@ -727,8 +720,12 @@ it.live( expect(result.hits).toHaveLength(2) expect(JSON.stringify(result.hits[0]!.body)).not.toContain(directive) expect(JSON.stringify(result.hits[1]!.body)).toContain(directive) + expect( + track.mock.calls.filter(([event]) => event.type === "starvation_breaker" && event.action === "injected"), + ).toHaveLength(1) } finally { beginSpy.mockRestore() + track.mockRestore() if (runMode === undefined) delete process.env.ALTIMATE_RUN_MODE else process.env.ALTIMATE_RUN_MODE = runMode } @@ -766,24 +763,23 @@ it.live( { timeout: 120_000 }, ) -// Usage counted for another model (different tokenizer), another agent (different system prompt and -// tools) falls back to the full estimate, which forces compaction. -for (const [name, last] of [ - ["another model", { ...TOOL_STEP, modelID: "another-model" }], - ["another agent", { ...TOOL_STEP, agent: "plan" }], +// With matching identities the reported input would fit. Each mismatch forces the full estimate. +for (const [field, identity] of [ + ["providerID", "other-provider"], + ["modelID", "other-model"], + ["agent", "other-agent"], ] as const) { it.live( - `usage reported by ${name} is not trusted: the guard trips and the session compacts`, + `reported usage with a different ${field} cannot calibrate the guard`, () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { - // Same history and usage as the regression above. yield* llm.text("summary of the revenue analysis so far") yield* llm.text("Revenue is concentrated in the top ten customers.") const result = yield* runSession(dir, llm, { user: sqlJsonTurns(130_000), assistantUsage: 108_000, - last, + last: { [field]: identity, requestEstimateTokens: 137_000 }, next: "Summarize the revenue concentration.", }) @@ -874,6 +870,32 @@ it.live( { timeout: 120_000 }, ) +const MEDIA_GUIDANCE = + "The conversation plus the new attachment does not fit the model's context window. Run /compact, then attach it again, or remove the attachment." + +it.live( + "a refused request with an unseen user image gives manual compaction guidance", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(135_000), + assistantUsage: 0, + next: "Describe this image.", + nextFile: true, + }) + + expect(result.hits).toHaveLength(0) + expect(result.compactions).toHaveLength(0) + expect(result.errors.map((error) => JSON.parse(error))).toEqual([ + { name: "UnknownError", data: { message: MEDIA_GUIDANCE } }, + ]) + }), + { git: true, config: providerCfg(LARGE_WINDOW, { attachment: true }) }, + ), + { timeout: 120_000 }, +) + it.live( "an image from a tool result the model has not seen yet is not compacted away", () => @@ -890,7 +912,30 @@ it.live( expect(result.hits.length).toBe(0) expect(result.compactions.length).toBe(0) expect(result.errors.length).toBe(1) - expect(result.errors[0]).toContain("Context budget exceeded before the request was sent") + expect(result.errors[0]).toContain(MEDIA_GUIDANCE) + }), + { git: true, config: providerCfg(LARGE_WINDOW, { attachment: true }) }, + ), + { timeout: 120_000 }, +) + +it.live( + "an image generated by the measured assistant does not block compaction", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + yield* llm.text("Summary of the earlier turns.") + yield* llm.text("Here is the answer.") + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(135_000), + assistantUsage: 0, + last: { file: true }, + next: "Answer the follow-up question.", + }) + + expect(result.errors).toEqual([]) + expect(result.summaries).toHaveLength(1) + expect(result.hits).toHaveLength(2) }), { git: true, config: providerCfg(LARGE_WINDOW, { attachment: true }) }, ), From 838c2676605e88b55dba58c304033f5aeb2d878e Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 10:59:00 -0800 Subject: [PATCH 30/39] fix: word the media refusal for tool-returned images too Co-Authored-By: Claude Opus 5.5 --- docs/docs/configure/context-management.md | 2 +- packages/opencode/src/session/processor.ts | 2 +- packages/opencode/test/session/context-guard-compaction.test.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index feee8f0b8a..e942763fca 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -157,4 +157,4 @@ Before each request, altimate checks that the prompt plus the space reserved for For the same model and agent, the check starts with the last response's reported input usage, including cached input, and adds any growth in its own estimate of the full request since that response. The growth accounts for changes such as new messages, rewritten history, tools, system text, provider instructions, and attachments. The check caps the calibrated count at the full estimate and uses the full estimate if the calibrated count falls below 60% of it. It also uses the full estimate after a model or agent change, after a compaction summary, or when the last response lacks reported input usage or a recorded request estimate. -If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. This also applies when context pressure reduces the response reservation so far that the reasoning budget cannot be reconciled with the visible output floor. If the original reservation also fails the reasoning budget check, the refusal is a configuration error. The size check fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), or when auto-compaction is disabled. If an unseen image or PDF blocks auto-compaction, the error suggests running `/compact` and reattaching the file, or removing it. +If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. This also applies when context pressure reduces the response reservation so far that the reasoning budget cannot be reconciled with the visible output floor. If the original reservation also fails the reasoning budget check, the refusal is a configuration error. The size check fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), or when auto-compaction is disabled. If an unseen image or PDF (attached, or returned by a tool) blocks auto-compaction, the error suggests running `/compact` and retrying. diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 1bc30a7f03..4c0d68bd69 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -1407,7 +1407,7 @@ export namespace SessionProcessor { } else if (compactBlockedByMedia && isContextBudgetError(e)) { error = new NamedError.Unknown( { - message: "The conversation plus the new attachment does not fit the model's context window. Run /compact, then attach it again, or remove the attachment.", + message: "The conversation plus a new image or PDF (attached or returned by a tool) does not fit the model's context window. Run /compact, then retry; re-attach the file if you attached it.", }, { cause: e }, ).toObject() diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 06cb6bd235..dc8b008588 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -871,7 +871,7 @@ it.live( ) const MEDIA_GUIDANCE = - "The conversation plus the new attachment does not fit the model's context window. Run /compact, then attach it again, or remove the attachment." + "The conversation plus a new image or PDF (attached or returned by a tool) does not fit the model's context window. Run /compact, then retry; re-attach the file if you attached it." it.live( "a refused request with an unseen user image gives manual compaction guidance", From e628fb733e16a25369807b3e104ed0af1ab019dd Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 11:03:04 -0800 Subject: [PATCH 31/39] fix: name the input limit in the media refusal message Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/session/processor.ts | 2 +- packages/opencode/test/session/context-guard-compaction.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 4c0d68bd69..67a6857926 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -1407,7 +1407,7 @@ export namespace SessionProcessor { } else if (compactBlockedByMedia && isContextBudgetError(e)) { error = new NamedError.Unknown( { - message: "The conversation plus a new image or PDF (attached or returned by a tool) does not fit the model's context window. Run /compact, then retry; re-attach the file if you attached it.", + message: "The conversation plus a new image or PDF (attached or returned by a tool) does not fit the model's context window or input limit. Run /compact, then retry; re-attach the file if you attached it.", }, { cause: e }, ).toObject() diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index dc8b008588..8ea7c6212b 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -871,7 +871,7 @@ it.live( ) const MEDIA_GUIDANCE = - "The conversation plus a new image or PDF (attached or returned by a tool) does not fit the model's context window. Run /compact, then retry; re-attach the file if you attached it." + "The conversation plus a new image or PDF (attached or returned by a tool) does not fit the model's context window or input limit. Run /compact, then retry; re-attach the file if you attached it." it.live( "a refused request with an unseen user image gives manual compaction guidance", From d51655214f1e5bf8f81bc9d40ead39b4ff32d03c Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 11:06:17 -0800 Subject: [PATCH 32/39] refactor: drop a redundant id check in the unseen-media scan Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/session/prompt.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 151a1cae52..95b54b7d46 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2251,10 +2251,7 @@ export namespace SessionPrompt { (msg.info.id === measuredID || isAfterMeasured(msg.info.id)) && msg.parts.some( (part) => - (msg.info.role === "user" && - msg.info.id !== measuredID && - part.type === "file" && - MessageV2.isMedia(part.mime)) || + (msg.info.role === "user" && part.type === "file" && MessageV2.isMedia(part.mime)) || (part.type === "tool" && part.state.status === "completed" && part.state.attachments?.some((attachment) => MessageV2.isMedia(attachment.mime))), From c5d2a7edf8d65836ad7c7dcf31d7064ff4f90ad7 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 11:12:32 -0800 Subject: [PATCH 33/39] docs: correct the context-measure comments Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/session/compaction.ts | 2 +- packages/opencode/src/session/llm.ts | 2 ++ packages/opencode/test/session/context-guard-compaction.test.ts | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/session/compaction.ts b/packages/opencode/src/session/compaction.ts index 8ff30d9658..1ab37c4a05 100644 --- a/packages/opencode/src/session/compaction.ts +++ b/packages/opencode/src/session/compaction.ts @@ -14,7 +14,7 @@ import { Agent } from "@/agent/agent" import { Plugin } from "@/plugin" import { Config } from "@/config/config" import { ProviderTransform } from "@/provider/transform" -// altimate_change start — the context measure shared with the pre-send budget guard +// altimate_change start — reported usage plus the inflated uncounted tail import { reportedContextTokens } from "@/provider/output-token-budget" // altimate_change end import { Telemetry } from "@/telemetry" // altimate_change — telemetry for compaction events diff --git a/packages/opencode/src/session/llm.ts b/packages/opencode/src/session/llm.ts index 1ff4da286c..1991d52f43 100644 --- a/packages/opencode/src/session/llm.ts +++ b/packages/opencode/src/session/llm.ts @@ -353,6 +353,8 @@ export namespace LLM { // altimate_change end // altimate_change start — clamp after every context-affecting request field is finalized. + // Tool schemas and provider instructions share the window; encoded media counts at fixed + // allowances. The estimate runs only when the clamp needs it. const maxOutputTokens = clampOutputTokens({ model: input.model, requested: params.maxOutputTokens, diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 8ea7c6212b..5e6106be6f 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -1014,7 +1014,7 @@ describe("guard calibration", () => { } }) - test("the shared measure is reported usage plus the tail inflated by the safety fraction", () => { + test("the compaction measure is reported usage plus the tail inflated by the safety fraction", () => { const tokens = { input: 100_000, output: 2_000, reasoning: 0, cache: { read: 6_000, write: 0 } } expect(reportedContextTokens({ tokens, fraction: 0.65 })).toBe(108_000) expect(reportedContextTokens({ tokens, estimatedTokens: 650, fraction: 0.65 })).toBe(109_000) From 7daeeca9130461f380fb94c370141127f4570e7d Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 11:15:44 -0800 Subject: [PATCH 34/39] docs: describe media allowances accurately in the clamp comment Co-Authored-By: Claude Opus 5.5 --- packages/opencode/src/session/llm.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/opencode/src/session/llm.ts b/packages/opencode/src/session/llm.ts index 1991d52f43..97c1501cf3 100644 --- a/packages/opencode/src/session/llm.ts +++ b/packages/opencode/src/session/llm.ts @@ -353,8 +353,8 @@ export namespace LLM { // altimate_change end // altimate_change start — clamp after every context-affecting request field is finalized. - // Tool schemas and provider instructions share the window; encoded media counts at fixed - // allowances. The estimate runs only when the clamp needs it. + // Tool schemas and provider instructions share the window; encoded media is charged by the + // estimator's media allowances, not as literal text. The estimate runs only when the clamp needs it. const maxOutputTokens = clampOutputTokens({ model: input.model, requested: params.maxOutputTokens, From dc047e515dc6efa27b63cb161cb2d5eb8e7d51d7 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 18:38:50 -0800 Subject: [PATCH 35/39] fix: avoid overestimating recursive file listings --- .../src/provider/output-token-budget.ts | 14 ++++++- .../opencode/test/provider/transform.test.ts | 2 + .../session/context-guard-compaction.test.ts | 39 ++++++++++++++++++- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index 0d1719bd63..0c1c3f4428 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -34,6 +34,11 @@ const DENSE_ASCII_CHARACTER = /[A-Za-z0-9+/_=-]/ const DENSE_ASCII_MIN_LENGTH = 32 const DENSE_ASCII_MIN_UNIQUE = 6 const DENSE_ASCII_EXTRA_FRACTION = 0.75 +// altimate_change start — distinguish recursive path listings from opaque ASCII +// The estimator scans JSON, so a listing line starts after a quote or an escaped newline. +// Require a dot on absolute paths to distinguish them from slash-prefixed encoded data. +const LISTING_PATH = /("|\\n)(\.\/(?:[\w.-]+\/)+[\w.-]+|\/(?:[\w.-]+\/)+[\w.-]+\.[\w-]+)(?="|\\n)/g +// altimate_change end const REASONING_BUDGET_KEYS = new Set(["budgetTokens", "thinkingBudget", "budget_tokens"]) const CONTEXT_WINDOW_BETAS = new Map([["context-1m-2025-08-07", 1_000_000]]) const MEDIA_PART_TYPES = new Set([ @@ -159,7 +164,7 @@ export function mergeRequestHeaders(...sources: readonly unknown[]): Record prefix + path.replaceAll("/", " ")) + for (const character of text) { + // altimate_change end if (!DENSE_ASCII_CHARACTER.test(character)) { flush() continue diff --git a/packages/opencode/test/provider/transform.test.ts b/packages/opencode/test/provider/transform.test.ts index 90e6a8dc0a..48d5da0d5e 100644 --- a/packages/opencode/test/provider/transform.test.ts +++ b/packages/opencode/test/provider/transform.test.ts @@ -4949,6 +4949,8 @@ describe("output token budget", () => { expect(estimate(dense)).toBeGreaterThan(8_000) expect(estimate(dense)).toBeGreaterThan(estimate(repetitive) * 3) + expect(estimate(`./logs/${dense.replaceAll("/", "_")}.txt`)).toBeGreaterThan(8_000) + expect(estimate("/" + "AbCdE123/fGhIj456/".repeat(500) + "kLmNo789")).toBeGreaterThan(8_000) const shortDense = alphabet.slice(0, 32) for (let padding = 0; padding < 400; padding++) { diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 5e6106be6f..b6d6164ffd 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -201,6 +201,7 @@ type Seeded = { assistantUsage?: number next?: string nextSystem?: string + earlierToolOutput?: string last?: { modelID?: string providerID?: string @@ -329,11 +330,13 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) const last = index === turns.length - 1 if (last && seeded.userFile) yield* attachImage(msg.id) if (!last) { - yield* answer(msg.id, 0, null) + yield* answer(msg.id, 0, null, index === 0 ? { toolOutput: seeded.earlierToolOutput } : undefined) history.push({ role: "assistant", content: [{ type: "text", text: answerText }] }) } else if (seeded.assistantUsage !== undefined) { const override = seeded.last?.requestEstimateTokens - const estimate = override === undefined ? estimateInputTokens({ system: [], messages: history }) : override + const toolOutput = seeded.earlierToolOutput + const measuredHistory = toolOutput === undefined ? history : [...history, { role: "tool", content: toolOutput }] + const estimate = override === undefined ? estimateInputTokens({ system: [], messages: measuredHistory }) : override yield* answer(msg.id, seeded.assistantUsage, estimate, seeded.last) } } @@ -393,6 +396,38 @@ it.live( { timeout: 120_000 }, ) +it.live( + "keeps the full output reservation after large recursive file listings", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + const listing = (root: string) => + Array.from( + { length: 540 }, + (_, index) => `${root}/packages/warehouse/src/models/reporting/daily_orders_${index}.sql`, + ).join("\n") + const toolOutput = [listing("./current"), listing("/archive")].join("\n\n") + + yield* llm.text("The files are listed by tree.") + yield* llm.text("The files are listed by tree.") + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(49_000), + earlierToolOutput: toolOutput, + assistantUsage: 49_000, + next: "Which files are present in both trees?", + }) + + expect(result.errors).toEqual([]) + expect(result.compactions.length).toBe(0) + expect(result.hits.length).toBe(1) + expect(result.hits[0]!.body.max_tokens).toBe(LARGE_WINDOW.output) + expect(JSON.stringify(result.hits[0]!.body)).toContain("/archive/packages/warehouse") + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + it.live( "a new system prompt that exceeds the context budget forces compaction", () => From 99049b6008810006dc5b60d34878853af7c7592e Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 18:51:20 -0800 Subject: [PATCH 36/39] fix: verify measured request content before reuse --- docs/docs/configure/context-management.md | 2 +- .../src/provider/output-token-budget.ts | 36 +++++- packages/opencode/src/session/llm.ts | 28 ++-- packages/opencode/src/session/processor.ts | 23 +++- packages/opencode/src/session/prompt.ts | 37 ++++-- .../opencode/test/provider/transform.test.ts | 10 +- .../session/context-guard-compaction.test.ts | 120 ++++++++++++++++-- 7 files changed, 216 insertions(+), 40 deletions(-) diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index e942763fca..143a4f5443 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -155,6 +155,6 @@ These ratios are tuned against the cl100k_base tokenizer used by Claude and GPT- Before each request, altimate checks that the prompt plus the space reserved for the response fits the model's context window, so an oversized request is caught before it is sent instead of failing at the provider. When the prompt leaves less room than requested, the response reservation is reduced first; the request is refused only when too little room would remain. -For the same model and agent, the check starts with the last response's reported input usage, including cached input, and adds any growth in its own estimate of the full request since that response. The growth accounts for changes such as new messages, rewritten history, tools, system text, provider instructions, and attachments. The check caps the calibrated count at the full estimate and uses the full estimate if the calibrated count falls below 60% of it. It also uses the full estimate after a model or agent change, after a compaction summary, or when the last response lacks reported input usage or a recorded request estimate. +For the same model and agent, the check can start with the last response's reported input usage, including cached input, and add the estimated growth from appended messages and attachments. Replacing already-measured history, system text, tools, or provider instructions causes the full request estimate to be used. The check also uses the full estimate when the calibrated count falls below 60% of it, after a model or agent change or compaction summary, or when the last response lacks reported input usage or a recorded request estimate. If the check finds the request too large and the conversation can be compacted, altimate compacts it and retries. This also applies when context pressure reduces the response reservation so far that the reasoning budget cannot be reconciled with the visible output floor. If the original reservation also fails the reasoning budget check, the refusal is a configuration error. The size check fails with "Context budget exceeded before the request was sent" (or "Input budget exceeded" for models with a separate input limit) when the request still does not fit after compaction (the first request after a compaction is sized by the full estimate and is not compacted again), when there is nothing to compact yet (a first message that is already too large), or when auto-compaction is disabled. If an unseen image or PDF (attached, or returned by a tool) blocks auto-compaction, the error suggests running `/compact` and retrying. diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index 0c1c3f4428..b30c9591a3 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -10,6 +10,27 @@ const log = Log.create({ service: "provider.output-token-budget" }) export const OUTPUT_TOKEN_FLOOR = 1_024 export const REQUEST_ESTIMATE_METADATA_KEY = "request_estimate_tokens" +export const REQUEST_SHAPE_METADATA_KEY = "request_estimate_shape" +export const REQUEST_MEDIA_METADATA_KEY = "request_media_fingerprints" + +export type RequestEstimateShape = { + readonly messageCount: number + readonly messageHash: string + readonly overheadHash: string +} + +export function requestEstimateShape(value: unknown): RequestEstimateShape | undefined { + if (!isRecord(value)) return undefined + const { messageCount, messageHash, overheadHash } = value + if (typeof messageCount !== "number" || !Number.isSafeInteger(messageCount) || messageCount < 0) return undefined + if (typeof messageHash !== "string" || typeof overheadHash !== "string") return undefined + return { messageCount, messageHash, overheadHash } +} + +export function requestEstimateHash(value: unknown, mediaMessages?: readonly unknown[]): string { + const media = mediaMessages && messageMediaAllowances(mediaMessages) + return Bun.hash(serializeForEstimate(value, media, true).text).toString(16) +} const CLAMP_MARGIN_FRACTION = 0.02 const CLAMP_MARGIN_MIN = 512 @@ -34,11 +55,9 @@ const DENSE_ASCII_CHARACTER = /[A-Za-z0-9+/_=-]/ const DENSE_ASCII_MIN_LENGTH = 32 const DENSE_ASCII_MIN_UNIQUE = 6 const DENSE_ASCII_EXTRA_FRACTION = 0.75 -// altimate_change start — distinguish recursive path listings from opaque ASCII // The estimator scans JSON, so a listing line starts after a quote or an escaped newline. // Require a dot on absolute paths to distinguish them from slash-prefixed encoded data. -const LISTING_PATH = /("|\\n)(\.\/(?:[\w.-]+\/)+[\w.-]+|\/(?:[\w.-]+\/)+[\w.-]+\.[\w-]+)(?="|\\n)/g -// altimate_change end +const LISTING_PATH = /("|\\n)(\.\/(?:[\w.-]+\/)+[\w.-]+|\/?(?:[\w.-]+\/)+[\w.-]+\.[\w-]+)(?=\\?"|(?:\\r)?\\n)/g const REASONING_BUDGET_KEYS = new Set(["budgetTokens", "thinkingBudget", "budget_tokens"]) const CONTEXT_WINDOW_BETAS = new Map([["context-1m-2025-08-07", 1_000_000]]) const MEDIA_PART_TYPES = new Set([ @@ -174,12 +193,10 @@ function denseAsciiCharacters(input: string): number { length = 0 unique.clear() } - // altimate_change start — split directory components for the dense surcharge // Directory separators in listing lines break dense runs. Long opaque filenames and // directory names still get the same surcharge as other dense text. const text = input.replace(LISTING_PATH, (_, prefix: string, path: string) => prefix + path.replaceAll("/", " ")) for (const character of text) { - // altimate_change end if (!DENSE_ASCII_CHARACTER.test(character)) { flush() continue @@ -324,6 +341,7 @@ function messageMediaAllowances(messages: readonly unknown[]): WeakMap, + fingerprintMedia = false, ): { readonly text: string; readonly mediaTokens: number } { let mediaTokens = 0 const ancestors: object[] = [] @@ -334,6 +352,14 @@ function serializeForEstimate( const mediaField = typeof this === "object" && this !== null && mediaContainers?.has(this) && MEDIA_PAYLOAD_KEYS.has(key) if (mediaField && child !== undefined && child !== null) { + if (fingerprintMedia) { + const original = (this as JsonRecord)[key] + if (ArrayBuffer.isView(original)) { + return Bun.hash(new Uint8Array(original.buffer, original.byteOffset, original.byteLength)).toString(16) + } + if (original instanceof ArrayBuffer) return Bun.hash(original).toString(16) + if (typeof child === "string") return Bun.hash(child).toString(16) + } return "[media omitted]" } if (typeof child === "object" && child !== null) { diff --git a/packages/opencode/src/session/llm.ts b/packages/opencode/src/session/llm.ts index 97c1501cf3..4c6555ec27 100644 --- a/packages/opencode/src/session/llm.ts +++ b/packages/opencode/src/session/llm.ts @@ -21,6 +21,8 @@ import { effectiveContextWindow, estimateInputTokens, mergeRequestHeaders, + requestEstimateHash, + type RequestEstimateShape, } from "@/provider/output-token-budget" // altimate_change end // altimate_change start — tool retrieval @@ -86,7 +88,8 @@ export namespace LLM { /** Provider-reported input usage for the measured request, including cache reads and writes. */ measuredInputTokens?: number measuredEstimateTokens?: number - onRequestEstimate?: (tokens: number) => void + measuredEstimateShape?: RequestEstimateShape + onRequestEstimate?: (tokens: number, shape: RequestEstimateShape) => void // altimate_change end } @@ -364,15 +367,22 @@ export namespace LLM { headerSources: [provider.options, requestHeaders], }), inputTokens: () => { - const estimate = estimateInputTokens({ - system, - messages: ProviderTransform.messagesForInputEstimate(input.messages, input.model), - tools, - instructions: params.options.instructions, - }) - input.onRequestEstimate?.(estimate) - const { measuredInputTokens, measuredEstimateTokens } = input + const messages = ProviderTransform.messagesForInputEstimate(input.messages, input.model) + const overhead = { system, tools, instructions: params.options.instructions } + const estimate = estimateInputTokens({ ...overhead, messages }) + const { measuredInputTokens, measuredEstimateTokens, measuredEstimateShape } = input + const overheadHash = requestEstimateHash(overhead) + const messageHash = requestEstimateHash(messages, messages) + input.onRequestEstimate?.(estimate, { messageCount: messages.length, messageHash, overheadHash }) + // Removed text must not cancel the estimate of new, unmeasured content. + const prefix = measuredEstimateShape && messages.slice(0, measuredEstimateShape.messageCount) + const unchanged = + !measuredEstimateShape || + (overheadHash === measuredEstimateShape.overheadHash && + prefix?.length === measuredEstimateShape.messageCount && + requestEstimateHash(prefix, prefix) === measuredEstimateShape.messageHash) const calibrated = + unchanged && typeof measuredInputTokens === "number" && Number.isFinite(measuredInputTokens) && typeof measuredEstimateTokens === "number" && diff --git a/packages/opencode/src/session/processor.ts b/packages/opencode/src/session/processor.ts index 67a6857926..2cfd10ef00 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -21,7 +21,12 @@ import type { Provider } from "@/provider/provider" import { LLM } from "./llm" import { Config } from "@/config/config" // altimate_change start — recognize pre-send context budget refusals -import { isContextBudgetError, REQUEST_ESTIMATE_METADATA_KEY } from "@/provider/output-token-budget" +import { + isContextBudgetError, + REQUEST_ESTIMATE_METADATA_KEY, + REQUEST_MEDIA_METADATA_KEY, + REQUEST_SHAPE_METADATA_KEY, +} from "@/provider/output-token-budget" import { NamedError } from "@opencode-ai/util/error" // altimate_change end import { SessionCompaction } from "./compaction" @@ -399,12 +404,14 @@ export namespace SessionProcessor { deferText, compactable, compactBlockedByMedia, + requestMedia, ...streamInput }: LLM.StreamInput & { deferText?: MessageV2.TextPart[] // The prompt loop can compact this conversation (a finished non-summary turn exists). compactable?: boolean compactBlockedByMedia?: boolean + requestMedia?: string[] }) { // altimate_change end log.info("process") @@ -523,6 +530,7 @@ export namespace SessionProcessor { let interruptRequested = false let currentText: MessageV2.TextPart | undefined const reasoningMap: Record = {} + let requestEstimate: Record | undefined const interruptRequest = () => { if (!interruptRequested || requestController.signal.aborted || input.abort.aborted) return if (pendingToolExecutions || [...toolcalls.values()].some((part) => part.state.status === "running")) return @@ -552,10 +560,11 @@ export namespace SessionProcessor { await LLM.stream({ ...effectiveStreamInput, abort: signal, - onRequestEstimate: (tokens) => { - input.assistantMessage.metadata = { - ...input.assistantMessage.metadata, + onRequestEstimate: (tokens, shape) => { + requestEstimate = { [REQUEST_ESTIMATE_METADATA_KEY]: tokens, + [REQUEST_SHAPE_METADATA_KEY]: shape, + [REQUEST_MEDIA_METADATA_KEY]: requestMedia, } }, }) @@ -1091,6 +1100,12 @@ export namespace SessionProcessor { input.assistantMessage.finish = value.finishReason input.assistantMessage.cost += usage.cost input.assistantMessage.tokens = usage.tokens + // altimate_change start — persist a request estimate only after the provider responds + // Only a completed provider step can confirm which request and media it saw. + if (requestEstimate) { + input.assistantMessage.metadata = { ...input.assistantMessage.metadata, ...requestEstimate } + } + // altimate_change end if (value.finishReason === "content-filter") { // altimate_change start — upstream_fix: content-filter // finishes are session errors, not successful stops. diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index 95b54b7d46..a776331542 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -31,7 +31,12 @@ import { familyVendor } from "../provider/family" import { type Tool as AITool, tool, jsonSchema, type ToolCallOptions, asSchema } from "ai" import { SessionCompaction } from "./compaction" // altimate_change start — read the persisted request estimate for context calibration -import { REQUEST_ESTIMATE_METADATA_KEY } from "@/provider/output-token-budget" +import { + REQUEST_ESTIMATE_METADATA_KEY, + REQUEST_MEDIA_METADATA_KEY, + REQUEST_SHAPE_METADATA_KEY, + requestEstimateShape, +} from "@/provider/output-token-budget" // altimate_change end import { NudgeArbiter } from "./nudge" import { SessionTermination } from "./termination" @@ -2244,19 +2249,25 @@ export namespace SessionPrompt { const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined const recordedEstimate = measured?.metadata?.[REQUEST_ESTIMATE_METADATA_KEY] + const measuredEstimateShape = requestEstimateShape(measured?.metadata?.[REQUEST_SHAPE_METADATA_KEY]) // Compaction drops images and PDFs, so keep a refusal as a budget error if the - // current request includes media the model has not seen yet. - const unseenMedia = msgs.some( - (msg) => - (msg.info.id === measuredID || isAfterMeasured(msg.info.id)) && - msg.parts.some( - (part) => - (msg.info.role === "user" && part.type === "file" && MessageV2.isMedia(part.mime)) || - (part.type === "tool" && - part.state.status === "completed" && - part.state.attachments?.some((attachment) => MessageV2.isMedia(attachment.mime))), - ), + // current request includes media the model has not seen yet, including an edited attachment. + const requestMedia = msgs.flatMap(({ info, parts }) => + parts.flatMap((part) => { + const files = + info.role === "user" && part.type === "file" + ? [part] + : part.type === "tool" && part.state.status === "completed" && !part.state.time.compacted + ? part.state.attachments ?? [] + : [] + return files + .filter((file) => MessageV2.isMedia(file.mime)) + .map((file) => `${info.id}:${part.id}:${Bun.hash(`${file.mime}\n${file.url}`).toString(16)}`) + }), ) + const recordedMedia = measured?.metadata?.[REQUEST_MEDIA_METADATA_KEY] + const seenMedia = new Set(Array.isArray(recordedMedia) ? recordedMedia : []) + const unseenMedia = requestMedia.some((fingerprint) => !seenMedia.has(fingerprint)) const canAutoCompact = measured !== undefined && loopConfig.compaction?.auto !== false const measuredInputTokens = measured && @@ -2275,6 +2286,8 @@ export namespace SessionPrompt { compactBlockedByMedia: canAutoCompact && unseenMedia, measuredInputTokens, measuredEstimateTokens: typeof recordedEstimate === "number" ? recordedEstimate : undefined, + measuredEstimateShape, + requestMedia, // altimate_change end user: lastUser, // altimate_change start — correlate request-boundary snapshots with this assistant message diff --git a/packages/opencode/test/provider/transform.test.ts b/packages/opencode/test/provider/transform.test.ts index 48d5da0d5e..142f239878 100644 --- a/packages/opencode/test/provider/transform.test.ts +++ b/packages/opencode/test/provider/transform.test.ts @@ -4949,7 +4949,7 @@ describe("output token budget", () => { expect(estimate(dense)).toBeGreaterThan(8_000) expect(estimate(dense)).toBeGreaterThan(estimate(repetitive) * 3) - expect(estimate(`./logs/${dense.replaceAll("/", "_")}.txt`)).toBeGreaterThan(8_000) + expect(estimate(`./logs/${dense.replace(/[+/]/g, "_")}.txt`)).toBeGreaterThan(8_000) expect(estimate("/" + "AbCdE123/fGhIj456/".repeat(500) + "kLmNo789")).toBeGreaterThan(8_000) const shortDense = alphabet.slice(0, 32) @@ -4959,6 +4959,14 @@ describe("output token budget", () => { } }) + test("estimates CRLF and JSON-encoded file listings as path components", () => { + const paths = Array.from({ length: 200 }, (_, index) => "src/packages/warehouse/models/daily-orders_" + index + ".sql") + for (const listing of [paths.join("\r\n"), JSON.stringify(paths)]) { + const estimate = estimateInputTokens({ system: [], messages: [{ role: "user", content: listing }] }) + expect(estimate).toBeLessThan(listing.length / 2) + } + }) + test("does not tokenize encoded media bytes as literal prompt text", () => { const estimated = estimateInputTokens({ system: [], diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index b6d6164ffd..b053a0254f 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -34,6 +34,9 @@ import { OutputTokenBudgetError, ReasoningTokenBudgetError, REQUEST_ESTIMATE_METADATA_KEY, + REQUEST_MEDIA_METADATA_KEY, + REQUEST_SHAPE_METADATA_KEY, + type RequestEstimateShape, calibratedInputTokens, clampReasoningBudget, estimateInputTokens, @@ -201,6 +204,7 @@ type Seeded = { assistantUsage?: number next?: string nextSystem?: string + measureBeforeNext?: boolean earlierToolOutput?: string last?: { modelID?: string @@ -215,11 +219,13 @@ type Seeded = { toolImage?: boolean /** Override the seeded request estimate. Null represents a message without one. */ requestEstimateTokens?: number | null + requestShape?: RequestEstimateShape } /** An image attached to the `next` user turn. */ nextFile?: boolean /** An image attached to the last `user` turn (sent before the measured step). */ userFile?: boolean + olderFile?: "keep" | "replace" } /** Seed [user history, finished assistant with provider usage, new user turn] and run the loop. */ @@ -261,6 +267,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) usage: number, requestEstimateTokens: number | null, last: Seeded["last"] = {}, + media?: string[], ) { const msg = yield* sessions.updateMessage({ id: MessageID.ascending(), @@ -277,7 +284,13 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) finish: last.finish ?? "stop", time: { created: Date.now(), completed: Date.now() }, metadata: - requestEstimateTokens === null ? undefined : { [REQUEST_ESTIMATE_METADATA_KEY]: requestEstimateTokens }, + requestEstimateTokens === null + ? undefined + : { + [REQUEST_ESTIMATE_METADATA_KEY]: requestEstimateTokens, + [REQUEST_SHAPE_METADATA_KEY]: last.requestShape, + [REQUEST_MEDIA_METADATA_KEY]: media, + }, } as any) yield* sessions.updatePart({ id: PartID.ascending(), @@ -324,8 +337,10 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) const turns = Array.isArray(seeded.user) ? seeded.user : [seeded.user] // Estimate the seeded history as it was before the measured assistant answered. const history: { role: "user" | "assistant"; content: { type: "text"; text: string }[] }[] = [] + let olderFile: { id: PartID; messageID: MessageID; mime: string; url: string } | undefined for (const [index, text] of turns.entries()) { const msg = yield* user(text) + if (index === 0 && seeded.olderFile) olderFile = yield* attachImage(msg.id) history.push({ role: "user", content: [{ type: "text", text }] }) const last = index === turns.length - 1 if (last && seeded.userFile) yield* attachImage(msg.id) @@ -337,10 +352,17 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) const toolOutput = seeded.earlierToolOutput const measuredHistory = toolOutput === undefined ? history : [...history, { role: "tool", content: toolOutput }] const estimate = override === undefined ? estimateInputTokens({ system: [], messages: measuredHistory }) : override - yield* answer(msg.id, seeded.assistantUsage, estimate, seeded.last) + const media = olderFile + ? [`${olderFile.messageID}:${olderFile.id}:${Bun.hash(`${olderFile.mime}\n${olderFile.url}`).toString(16)}`] + : undefined + yield* answer(msg.id, seeded.assistantUsage, estimate, seeded.last, media) } } + if (olderFile && seeded.olderFile === "replace") { + yield* sessions.updatePart({ ...olderFile, url: "https://example.test/revised.png" } as any) + } if (seeded.next !== undefined) { + if (seeded.measureBeforeNext) yield* prompt.loop({ sessionID }) const next = yield* user(seeded.next, seeded.nextSystem) if (seeded.nextFile) yield* attachImage(next.id) } @@ -477,6 +499,32 @@ it.live( { timeout: 120_000 }, ) +it.live( + "a changed measured request with no net estimate growth forces compaction", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + yield* llm.text("Summary of the earlier turns.") + yield* llm.text("Here is the answer.") + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(130_000), + assistantUsage: 108_000, + last: { + requestEstimateTokens: 137_000, + requestShape: { messageCount: 2, messageHash: "prior history", overheadHash: "prior system and tools" }, + }, + next: "Answer the follow-up question.", + }) + + expect(result.errors).toEqual([]) + expect(result.summaries).toHaveLength(1) + expect(result.hits).toHaveLength(2) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + it.live( "a measured step without a recorded request estimate cannot calibrate the guard", () => @@ -558,19 +606,31 @@ it.live( ) it.live( - "the processor records the full request estimate on the sent assistant message", + "an append-only follow-up reuses the recorded request estimate", () => provideTmpdirServerLegacy( Effect.fnUntraced(function* ({ dir, llm }) { - yield* llm.text("Here is the answer.") - const result = yield* runSession(dir, llm, { user: sqlJsonHistory(20_000) }) + yield* llm.text("Here is the answer.", { usage: { input: 90_000, output: 5 } }) + yield* llm.text("Here is the follow-up.") + const result = yield* runSession(dir, llm, { + user: sqlJsonHistory(116_000), + next: "Why?", + measureBeforeNext: true, + }) expect(result.errors).toEqual([]) - expect(result.hits.length).toBe(1) - const answer = result.messages.at(-1)?.info + expect(result.hits).toHaveLength(2) + expect(result.hits[0]!.body.max_tokens).toBeLessThan(LARGE_WINDOW.output) + expect(result.hits[1]!.body.max_tokens).toBe(LARGE_WINDOW.output) + const answer = result.messages.find(({ info }) => info.role === "assistant" && info.tokens.input === 90_000)?.info expect( answer?.role === "assistant" ? answer.metadata?.[REQUEST_ESTIMATE_METADATA_KEY] : undefined, - ).toBeGreaterThan(20_000) + ).toBeGreaterThan(116_000) + expect(answer?.role === "assistant" && answer.metadata?.[REQUEST_SHAPE_METADATA_KEY]).toMatchObject({ + messageCount: 1, + messageHash: expect.any(String), + overheadHash: expect.any(String), + }) }), { git: true, config: providerCfg(LARGE_WINDOW) }, ), @@ -954,6 +1014,50 @@ it.live( { timeout: 120_000 }, ) +it.live( + "replacing an image on an older turn keeps the pre-send refusal fatal", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(135_000), + assistantUsage: 0, + olderFile: "replace", + next: "Describe the revised image.", + }) + + expect(result.hits).toHaveLength(0) + expect(result.compactions).toHaveLength(0) + expect(result.errors[0]).toContain(MEDIA_GUIDANCE) + }), + { git: true, config: providerCfg(LARGE_WINDOW, { attachment: true }) }, + ), + { timeout: 120_000 }, +) + +it.live( + "an unchanged older user image does not block compaction", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + yield* llm.text("Summary of the earlier turns.") + yield* llm.text("Here is the answer.") + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(135_000), + assistantUsage: 0, + olderFile: "keep", + next: "Answer the follow-up question.", + }) + + expect(result.errors).toEqual([]) + expect(result.summaries).toHaveLength(1) + expect(result.hits).toHaveLength(2) + }), + { git: true, config: providerCfg(LARGE_WINDOW, { attachment: true }) }, + ), + { timeout: 120_000 }, +) + it.live( "an image generated by the measured assistant does not block compaction", () => From 52555e90d33612f888fa6f5744128ece669bacac Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 18:54:37 -0800 Subject: [PATCH 37/39] test: record all observed fixture attachments --- .../test/session/context-guard-compaction.test.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index b053a0254f..029702af89 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -337,13 +337,13 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) const turns = Array.isArray(seeded.user) ? seeded.user : [seeded.user] // Estimate the seeded history as it was before the measured assistant answered. const history: { role: "user" | "assistant"; content: { type: "text"; text: string }[] }[] = [] - let olderFile: { id: PartID; messageID: MessageID; mime: string; url: string } | undefined + const measuredFiles: { id: PartID; messageID: MessageID; mime: string; url: string }[] = [] for (const [index, text] of turns.entries()) { const msg = yield* user(text) - if (index === 0 && seeded.olderFile) olderFile = yield* attachImage(msg.id) + if (index === 0 && seeded.olderFile) measuredFiles.push(yield* attachImage(msg.id)) history.push({ role: "user", content: [{ type: "text", text }] }) const last = index === turns.length - 1 - if (last && seeded.userFile) yield* attachImage(msg.id) + if (last && seeded.userFile) measuredFiles.push(yield* attachImage(msg.id)) if (!last) { yield* answer(msg.id, 0, null, index === 0 ? { toolOutput: seeded.earlierToolOutput } : undefined) history.push({ role: "assistant", content: [{ type: "text", text: answerText }] }) @@ -352,12 +352,13 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) const toolOutput = seeded.earlierToolOutput const measuredHistory = toolOutput === undefined ? history : [...history, { role: "tool", content: toolOutput }] const estimate = override === undefined ? estimateInputTokens({ system: [], messages: measuredHistory }) : override - const media = olderFile - ? [`${olderFile.messageID}:${olderFile.id}:${Bun.hash(`${olderFile.mime}\n${olderFile.url}`).toString(16)}`] - : undefined + const media = measuredFiles.map( + (file) => `${file.messageID}:${file.id}:${Bun.hash(`${file.mime}\n${file.url}`).toString(16)}`, + ) yield* answer(msg.id, seeded.assistantUsage, estimate, seeded.last, media) } } + const olderFile = measuredFiles[0] if (olderFile && seeded.olderFile === "replace") { yield* sessions.updatePart({ ...olderFile, url: "https://example.test/revised.png" } as any) } From a8450d4539ac557d1211882cc9c2e3aa36fa7015 Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 18:58:59 -0800 Subject: [PATCH 38/39] fix: protect unseen media after a model switch --- packages/opencode/src/session/prompt.ts | 6 ++--- .../session/context-guard-compaction.test.ts | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index a776331542..fa13b2be10 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2248,6 +2248,7 @@ export namespace SessionPrompt { // altimate_change start — calibrate from the last measured request and protect unseen media const loopConfig = await Config.get() const measured = lastFinished && lastFinished.summary !== true ? lastFinished : undefined + const sameModel = measured?.providerID === model.providerID && measured.modelID === model.id const recordedEstimate = measured?.metadata?.[REQUEST_ESTIMATE_METADATA_KEY] const measuredEstimateShape = requestEstimateShape(measured?.metadata?.[REQUEST_SHAPE_METADATA_KEY]) // Compaction drops images and PDFs, so keep a refusal as a budget error if the @@ -2266,15 +2267,14 @@ export namespace SessionPrompt { }), ) const recordedMedia = measured?.metadata?.[REQUEST_MEDIA_METADATA_KEY] - const seenMedia = new Set(Array.isArray(recordedMedia) ? recordedMedia : []) + const seenMedia = new Set(sameModel && Array.isArray(recordedMedia) ? recordedMedia : []) const unseenMedia = requestMedia.some((fingerprint) => !seenMedia.has(fingerprint)) const canAutoCompact = measured !== undefined && loopConfig.compaction?.auto !== false const measuredInputTokens = measured && typeof recordedEstimate === "number" && Number.isFinite(recordedEstimate) && - measured.providerID === model.providerID && - measured.modelID === model.id && + sameModel && measured.agent === lastUser.agent && (measured.tokens.input > 0 || measured.tokens.cache.read > 0 || measured.tokens.cache.write > 0) ? measured.tokens.input + measured.tokens.cache.read + measured.tokens.cache.write diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 029702af89..69d25fbe59 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -1059,6 +1059,28 @@ it.live( { timeout: 120_000 }, ) +it.live( + "an older image seen by another model still blocks pre-send compaction", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(135_000), + assistantUsage: 0, + olderFile: "keep", + last: { modelID: "another-model" }, + next: "Describe the image.", + }) + + expect(result.hits).toHaveLength(0) + expect(result.compactions).toHaveLength(0) + expect(result.errors[0]).toContain(MEDIA_GUIDANCE) + }), + { git: true, config: providerCfg(LARGE_WINDOW, { attachment: true }) }, + ), + { timeout: 120_000 }, +) + it.live( "an image generated by the measured assistant does not block compaction", () => From 966b9a446132cf8b642eba977c04e0654a4b64af Mon Sep 17 00:00:00 2001 From: anandgupta42 Date: Sat, 21 Nov 2026 19:04:45 -0800 Subject: [PATCH 39/39] fix: size the prepared provider request --- .../src/provider/output-token-budget.ts | 17 +---- packages/opencode/src/session/llm.ts | 21 ++---- packages/opencode/src/session/prompt.ts | 13 +++- .../session/context-guard-compaction.test.ts | 73 ++++++++++++++++++- 4 files changed, 91 insertions(+), 33 deletions(-) diff --git a/packages/opencode/src/provider/output-token-budget.ts b/packages/opencode/src/provider/output-token-budget.ts index b30c9591a3..c9b41ed019 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -28,8 +28,8 @@ export function requestEstimateShape(value: unknown): RequestEstimateShape | und } export function requestEstimateHash(value: unknown, mediaMessages?: readonly unknown[]): string { - const media = mediaMessages && messageMediaAllowances(mediaMessages) - return Bun.hash(serializeForEstimate(value, media, true).text).toString(16) + const media = mediaMessages && messageMediaAllowances(mediaMessages, false) + return Bun.hash(serializeForEstimate(value, media).text).toString(16) } const CLAMP_MARGIN_FRACTION = 0.02 @@ -314,7 +314,7 @@ function mediaTokenAllowance(part: JsonRecord): number { } /** Mark only actual ModelMessage content parts whose payload is provider media. */ -function messageMediaAllowances(messages: readonly unknown[]): WeakMap { +function messageMediaAllowances(messages: readonly unknown[], charge = true): WeakMap { const result = new WeakMap() const visited = new WeakSet() @@ -323,7 +323,7 @@ function messageMediaAllowances(messages: readonly unknown[]): WeakMap, - fingerprintMedia = false, ): { readonly text: string; readonly mediaTokens: number } { let mediaTokens = 0 const ancestors: object[] = [] @@ -352,14 +351,6 @@ function serializeForEstimate( const mediaField = typeof this === "object" && this !== null && mediaContainers?.has(this) && MEDIA_PAYLOAD_KEYS.has(key) if (mediaField && child !== undefined && child !== null) { - if (fingerprintMedia) { - const original = (this as JsonRecord)[key] - if (ArrayBuffer.isView(original)) { - return Bun.hash(new Uint8Array(original.buffer, original.byteOffset, original.byteLength)).toString(16) - } - if (original instanceof ArrayBuffer) return Bun.hash(original).toString(16) - if (typeof child === "string") return Bun.hash(child).toString(16) - } return "[media omitted]" } if (typeof child === "object" && child !== null) { diff --git a/packages/opencode/src/session/llm.ts b/packages/opencode/src/session/llm.ts index 4c6555ec27..9603508349 100644 --- a/packages/opencode/src/session/llm.ts +++ b/packages/opencode/src/session/llm.ts @@ -358,6 +358,11 @@ export namespace LLM { // altimate_change start — clamp after every context-affecting request field is finalized. // Tool schemas and provider instructions share the window; encoded media is charged by the // estimator's media allowances, not as literal text. The estimate runs only when the clamp needs it. + const effortOptions = runBudget?.effort ? applyBudgetEffort(input.model, params.options) : params.options + // Numeric reasoning clamps leave the reasoning mode unchanged. + const budgetedMessages = budgetMessages(input.messages, input.model, effortOptions) + const declaresNoTools = Object.keys(tools).filter((x) => x !== "invalid").length === 0 + const requestMessages = declaresNoTools ? ProviderTransform.flattenToolParts(budgetedMessages) : budgetedMessages const maxOutputTokens = clampOutputTokens({ model: input.model, requested: params.maxOutputTokens, @@ -367,7 +372,7 @@ export namespace LLM { headerSources: [provider.options, requestHeaders], }), inputTokens: () => { - const messages = ProviderTransform.messagesForInputEstimate(input.messages, input.model) + const messages = ProviderTransform.messagesForInputEstimate(requestMessages, input.model) const overhead = { system, tools, instructions: params.options.instructions } const estimate = estimateInputTokens({ ...overhead, messages }) const { measuredInputTokens, measuredEstimateTokens, measuredEstimateShape } = input @@ -392,17 +397,7 @@ export namespace LLM { return calibratedInputTokens(estimate, calibrated) }, }) - // Deadline effort wins configured variants and plugin overrides. - const requestOptions = clampReasoningBudget( - runBudget?.effort ? applyBudgetEffort(input.model, params.options) : params.options, - maxOutputTokens, - params.maxOutputTokens, - ) - const requestMessages = budgetMessages(input.messages, input.model, requestOptions) - // altimate_change end - - // altimate_change start — detect a toolless request once, for the message flattening below - const declaresNoTools = Object.keys(tools).filter((x) => x !== "invalid").length === 0 + const requestOptions = clampReasoningBudget(effortOptions, maxOutputTokens, params.maxOutputTokens) // altimate_change end // altimate_change start — record final system text and resolved tools before provider middleware @@ -510,7 +505,7 @@ export namespace LLM { // never declares. The Altimate gateway fails every provider in its fallback chain on // that shape and reports one generic error; see ProviderTransform.flattenToolParts. // altimate_change start — keep interrupted reasoning in storage without sending empty disabled-thinking turns - ...(declaresNoTools ? ProviderTransform.flattenToolParts(requestMessages) : requestMessages), + ...requestMessages, // altimate_change end // altimate_change end ], diff --git a/packages/opencode/src/session/prompt.ts b/packages/opencode/src/session/prompt.ts index fa13b2be10..ba6eeab4b1 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -2253,7 +2253,7 @@ export namespace SessionPrompt { const measuredEstimateShape = requestEstimateShape(measured?.metadata?.[REQUEST_SHAPE_METADATA_KEY]) // Compaction drops images and PDFs, so keep a refusal as a budget error if the // current request includes media the model has not seen yet, including an edited attachment. - const requestMedia = msgs.flatMap(({ info, parts }) => + const media = msgs.flatMap(({ info, parts }) => parts.flatMap((part) => { const files = info.role === "user" && part.type === "file" @@ -2263,18 +2263,25 @@ export namespace SessionPrompt { : [] return files .filter((file) => MessageV2.isMedia(file.mime)) - .map((file) => `${info.id}:${part.id}:${Bun.hash(`${file.mime}\n${file.url}`).toString(16)}`) + .map((file) => ({ + fingerprint: `${info.id}:${part.id}:${file.mime}:${Bun.hash(file.url).toString(16)}`, + supported: model.capabilities.input[file.mime === "application/pdf" ? "pdf" : "image"], + })) }), ) const recordedMedia = measured?.metadata?.[REQUEST_MEDIA_METADATA_KEY] const seenMedia = new Set(sameModel && Array.isArray(recordedMedia) ? recordedMedia : []) - const unseenMedia = requestMedia.some((fingerprint) => !seenMedia.has(fingerprint)) + const unseenMedia = media.some(({ fingerprint }) => !seenMedia.has(fingerprint)) + const requestMedia = media.filter(({ supported }) => supported).map(({ fingerprint }) => fingerprint) + const currentMedia = new Set(requestMedia) + const measuredMediaUnchanged = [...seenMedia].every((fingerprint) => currentMedia.has(fingerprint)) const canAutoCompact = measured !== undefined && loopConfig.compaction?.auto !== false const measuredInputTokens = measured && typeof recordedEstimate === "number" && Number.isFinite(recordedEstimate) && sameModel && + measuredMediaUnchanged && measured.agent === lastUser.agent && (measured.tokens.input > 0 || measured.tokens.cache.read > 0 || measured.tokens.cache.write > 0) ? measured.tokens.input + measured.tokens.cache.read + measured.tokens.cache.write diff --git a/packages/opencode/test/session/context-guard-compaction.test.ts b/packages/opencode/test/session/context-guard-compaction.test.ts index 69d25fbe59..1186871e52 100644 --- a/packages/opencode/test/session/context-guard-compaction.test.ts +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -135,6 +135,9 @@ const providerCfg = id: "test-model", name: "Test Model", attachment: opts.attachment === true, + ...(opts.attachment + ? { modalities: { input: ["text" as const, "image" as const], output: ["text" as const] } } + : {}), reasoning: false, temperature: false, tool_call: true, @@ -142,6 +145,7 @@ const providerCfg = limit, cost: { input: 0, output: 0 }, options: opts.modelOptions ?? {}, + variants: { "no-thinking": { thinking: { type: "disabled" } } }, }, }, options: { apiKey: "test-key", baseURL: url }, @@ -205,6 +209,7 @@ type Seeded = { next?: string nextSystem?: string measureBeforeNext?: boolean + previousReasoning?: string earlierToolOutput?: string last?: { modelID?: string @@ -238,7 +243,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) permission: [{ permission: "*", pattern: "*", action: "allow" }], }) const sessionID = session.id - const user = Effect.fnUntraced(function* (text: string, system?: string) { + const user = Effect.fnUntraced(function* (text: string, system?: string, variant?: string) { const msg = yield* sessions.updateMessage({ id: MessageID.ascending(), role: "user", @@ -246,6 +251,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) agent: seeded.last?.tool === "answer_commit" ? "analyst" : "build", model, system, + variant, time: { created: Date.now() }, } as any) yield* sessions.updatePart({ id: PartID.ascending(), messageID: msg.id, sessionID, type: "text", text } as any) @@ -333,19 +339,34 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) }, } as any) } + return msg }) const turns = Array.isArray(seeded.user) ? seeded.user : [seeded.user] // Estimate the seeded history as it was before the measured assistant answered. const history: { role: "user" | "assistant"; content: { type: "text"; text: string }[] }[] = [] const measuredFiles: { id: PartID; messageID: MessageID; mime: string; url: string }[] = [] for (const [index, text] of turns.entries()) { - const msg = yield* user(text) + const msg = yield* user( + text, + undefined, + index === turns.length - 1 && seeded.previousReasoning !== undefined ? "no-thinking" : undefined, + ) if (index === 0 && seeded.olderFile) measuredFiles.push(yield* attachImage(msg.id)) history.push({ role: "user", content: [{ type: "text", text }] }) const last = index === turns.length - 1 if (last && seeded.userFile) measuredFiles.push(yield* attachImage(msg.id)) if (!last) { - yield* answer(msg.id, 0, null, index === 0 ? { toolOutput: seeded.earlierToolOutput } : undefined) + const response = yield* answer(msg.id, 0, null, index === 0 ? { toolOutput: seeded.earlierToolOutput } : undefined) + if (index === 0 && seeded.previousReasoning !== undefined) { + yield* sessions.updatePart({ + id: PartID.ascending(), + messageID: response.id, + sessionID, + type: "reasoning", + text: seeded.previousReasoning, + time: { start: Date.now(), end: Date.now() }, + } as any) + } history.push({ role: "assistant", content: [{ type: "text", text: answerText }] }) } else if (seeded.assistantUsage !== undefined) { const override = seeded.last?.requestEstimateTokens @@ -353,7 +374,7 @@ const runSession = (dir: string, llm: TestLLMServer["Service"], seeded: Seeded) const measuredHistory = toolOutput === undefined ? history : [...history, { role: "tool", content: toolOutput }] const estimate = override === undefined ? estimateInputTokens({ system: [], messages: measuredHistory }) : override const media = measuredFiles.map( - (file) => `${file.messageID}:${file.id}:${Bun.hash(`${file.mime}\n${file.url}`).toString(16)}`, + (file) => `${file.messageID}:${file.id}:${file.mime}:${Bun.hash(file.url).toString(16)}`, ) yield* answer(msg.id, seeded.assistantUsage, estimate, seeded.last, media) } @@ -638,6 +659,30 @@ it.live( { timeout: 120_000 }, ) +it.live( + "restoring previously omitted reasoning uses the full request estimate", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + yield* llm.text("First answer.", { usage: { input: 85_000, output: 5 } }) + yield* llm.text("Second answer.") + const result = yield* runSession(dir, llm, { + user: sqlJsonTurns(98_000), + previousReasoning: sqlJsonHistory(17_000), + measureBeforeNext: true, + next: "Can you explain further?", + }) + + expect(result.errors).toEqual([]) + expect(result.hits).toHaveLength(2) + expect(result.hits[0]!.body.max_tokens).toBe(LARGE_WINDOW.output) + expect(result.hits[1]!.body.max_tokens).toBeLessThan(LARGE_WINDOW.output) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + it.live( "history added since the measured request reduces the output reservation", () => @@ -960,6 +1005,7 @@ it.live( expect(result.errors).toEqual([]) expect(result.compactions.length).toBe(0) expect(result.hits.length).toBe(1) + expect(JSON.stringify(result.hits[0]!.body)).toContain(PNG_BASE64) }), { git: true, config: providerCfg(LARGE_WINDOW, { attachment: true }) }, ), @@ -1059,6 +1105,25 @@ it.live( { timeout: 120_000 }, ) +it.live( + "a text-only request does not record an unsupported image as seen", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + yield* llm.text("I can read text.") + const result = yield* runSession(dir, llm, { user: "Describe this image.", userFile: true }) + + expect(result.errors).toEqual([]) + expect(result.hits).toHaveLength(1) + expect(JSON.stringify(result.hits[0]!.body)).not.toContain(PNG_BASE64) + const answer = result.messages.at(-1)?.info + expect(answer?.role === "assistant" && answer.metadata?.[REQUEST_MEDIA_METADATA_KEY]).toEqual([]) + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { timeout: 120_000 }, +) + it.live( "an older image seen by another model still blocks pre-send compaction", () =>