diff --git a/docs/docs/configure/context-management.md b/docs/docs/configure/context-management.md index 1d5424683d..143a4f5443 100644 --- a/docs/docs/configure/context-management.md +++ b/docs/docs/configure/context-management.md @@ -150,3 +150,11 @@ 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 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 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 16d736468c..c9b41ed019 100644 --- a/packages/opencode/src/provider/output-token-budget.ts +++ b/packages/opencode/src/provider/output-token-budget.ts @@ -9,6 +9,29 @@ 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" +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, false) + return Bun.hash(serializeForEstimate(value, media).text).toString(16) +} + const CLAMP_MARGIN_FRACTION = 0.02 const CLAMP_MARGIN_MIN = 512 const ESTIMATE_CHUNK_SIZE = 400 @@ -20,11 +43,21 @@ 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 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). + */ +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 const DENSE_ASCII_MIN_UNIQUE = 6 const DENSE_ASCII_EXTRA_FRACTION = 0.75 +// 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-]+)(?=\\?"|(?:\\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([ @@ -105,6 +138,7 @@ export class ReasoningTokenBudgetError extends Error { readonly path: string readonly configured: number readonly maxOutputTokens: number + readonly contextInduced?: boolean }, ) { super( @@ -149,7 +183,7 @@ export function mergeRequestHeaders(...sources: readonly unknown[]): Record prefix + path.replaceAll("/", " ")) + for (const character of text) { if (!DENSE_ASCII_CHARACTER.test(character)) { flush() continue @@ -277,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() @@ -286,7 +323,7 @@ function messageMediaAllowances(messages: readonly unknown[]): WeakMap 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, 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 + // 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. */ +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. */ function resolveInputTokens(value: number | (() => number)): number { return typeof value === "function" ? value() : value @@ -523,10 +611,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/compaction.ts b/packages/opencode/src/session/compaction.ts index dc3b0fe277..1ab37c4a05 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 — 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 import { ModelID, ProviderID } from "@/provider/schema" // altimate_change start — summarizer-integrity error @@ -223,17 +226,17 @@ 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, + // 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/llm.ts b/packages/opencode/src/session/llm.ts index b806e6270c..9603508349 100644 --- a/packages/opencode/src/session/llm.ts +++ b/packages/opencode/src/session/llm.ts @@ -15,11 +15,14 @@ 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, estimateInputTokens, mergeRequestHeaders, + requestEstimateHash, + type RequestEstimateShape, } from "@/provider/output-token-budget" // altimate_change end // altimate_change start — tool retrieval @@ -81,6 +84,13 @@ export namespace LLM { tools: Record retries?: number toolChoice?: "auto" | "required" | "none" + // altimate_change start — calibrate from a measured step and the finalized request + /** Provider-reported input usage for the measured request, including cache reads and writes. */ + measuredInputTokens?: number + measuredEstimateTokens?: number + measuredEstimateShape?: RequestEstimateShape + onRequestEstimate?: (tokens: number, shape: RequestEstimateShape) => void + // altimate_change end } export type StreamOutput = StreamTextResult @@ -346,10 +356,13 @@ 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 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. + // 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, @@ -358,25 +371,33 @@ export namespace LLM { // Provider defaults are lower precedence than the exact case-normalized outgoing record. headerSources: [provider.options, requestHeaders], }), - inputTokens: () => - estimateInputTokens({ - system, - messages: ProviderTransform.messagesForInputEstimate(input.messages, input.model), - tools, - instructions: params.options.instructions, - }), + inputTokens: () => { + 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 + 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" && + Number.isFinite(measuredEstimateTokens) + ? measuredInputTokens + Math.max(0, estimate - measuredEstimateTokens) + : undefined + return calibratedInputTokens(estimate, calibrated) + }, }) - // altimate_change start — deadline effort wins configured variants and plugin overrides - const requestOptions = clampReasoningBudget( - runBudget?.effort ? applyBudgetEffort(input.model, params.options) : params.options, - 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 + const requestOptions = clampReasoningBudget(effortOptions, maxOutputTokens, params.maxOutputTokens) // altimate_change end // altimate_change start — record final system text and resolved tools before provider middleware @@ -484,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/processor.ts b/packages/opencode/src/session/processor.ts index 02687babb4..2cfd10ef00 100644 --- a/packages/opencode/src/session/processor.ts +++ b/packages/opencode/src/session/processor.ts @@ -20,6 +20,15 @@ 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, + 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" import { PermissionNext } from "@/permission/next" import { Question } from "@/question" @@ -84,6 +93,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) @@ -338,6 +351,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 @@ -354,6 +370,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 @@ -379,10 +400,25 @@ 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, + 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") 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 @@ -440,9 +476,12 @@ 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 + // Kept so a pre-send refusal can hand the directive to the retry. + let takenDirective: ReturnType = undefined 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 @@ -454,14 +493,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, @@ -487,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 @@ -511,7 +555,20 @@ 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, + onRequestEstimate: (tokens, shape) => { + requestEstimate = { + [REQUEST_ESTIMATE_METADATA_KEY]: tokens, + [REQUEST_SHAPE_METADATA_KEY]: shape, + [REQUEST_MEDIA_METADATA_KEY]: requestMedia, + } + }, + }) + ).fullStream, signal, ) @@ -1043,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. @@ -1327,7 +1390,44 @@ 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. + // 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 && !echoRecovery && 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, + }) + // 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 }) + 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() + } 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 or input limit. Run /compact, then retry; re-attach the file if you attached it.", + }, + { cause: e }, + ).toObject() + } + // 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..ba6eeab4b1 100644 --- a/packages/opencode/src/session/prompt.ts +++ b/packages/opencode/src/session/prompt.ts @@ -30,6 +30,14 @@ 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, + 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" import { Instance } from "../project/instance" @@ -1585,7 +1593,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 && @@ -2230,7 +2245,57 @@ 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 — 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 + // current request includes media the model has not seen yet, including an edited attachment. + const media = 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) => ({ + 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 = 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 + : undefined + // altimate_change end const result = await processor.process({ + // altimate_change start — pass the measured input usage and its matching estimate + compactable: canAutoCompact && !unseenMedia, + 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 messageID: processor.message.id, @@ -2284,6 +2349,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. @@ -2642,7 +2710,21 @@ export namespace SessionPrompt { agent: lastUser.agent, model: lastUser.model, auto: true, - overflow: !processor.message.finish, + // 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 || + // 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 }) } continue @@ -3750,7 +3832,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 @@ -3771,10 +3857,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/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/provider/transform.test.ts b/packages/opencode/test/provider/transform.test.ts index 90e6a8dc0a..142f239878 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.replace(/[+/]/g, "_")}.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++) { @@ -4957,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 new file mode 100644 index 0000000000..1186871e52 --- /dev/null +++ b/packages/opencode/test/session/context-guard-compaction.test.ts @@ -0,0 +1,1304 @@ +/** + * 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, 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 { Telemetry } from "@/altimate/telemetry" +import { NudgeArbiter } from "../../src/session/nudge" +import { SessionPrompt } from "../../src/session/prompt" +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" +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 { Provider } from "../../src/provider/provider" +import { RuntimeFlags } from "@/effect/runtime-flags" +import { + InputTokenBudgetError, + OutputTokenBudgetError, + ReasoningTokenBudgetError, + REQUEST_ESTIMATE_METADATA_KEY, + REQUEST_MEDIA_METADATA_KEY, + REQUEST_SHAPE_METADATA_KEY, + type RequestEstimateShape, + calibratedInputTokens, + clampReasoningBudget, + 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, 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; modelOptions?: Record } = {}, + ) => + (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, + ...(opts.attachment + ? { modalities: { input: ["text" as const, "image" as const], output: ["text" as const] } } + : {}), + reasoning: false, + temperature: false, + tool_call: true, + release_date: "2025-01-01", + limit, + cost: { input: 0, output: 0 }, + options: opts.modelOptions ?? {}, + variants: { "no-thinking": { thinking: { type: "disabled" } } }, + }, + }, + options: { apiKey: "test-key", baseURL: url }, + }, + }, + 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 = [ + "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` +} + +/** 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[] = [] + 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` (optionally from another model, as a mid-turn step, or with its own text). + */ +type Seeded = { + user: string | string[] + assistantUsage?: number + next?: string + nextSystem?: string + measureBeforeNext?: boolean + previousReasoning?: string + earlierToolOutput?: 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. */ + 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. */ +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, system?: string, variant?: string) { + const msg = yield* sessions.updateMessage({ + id: MessageID.ascending(), + role: "user", + sessionID, + 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) + 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, + usage: number, + requestEstimateTokens: number | null, + last: Seeded["last"] = {}, + media?: string[], + ) { + const msg = yield* sessions.updateMessage({ + id: MessageID.ascending(), + role: "assistant", + sessionID, + 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 } }, + modelID: last.modelID ?? model.modelID, + providerID: last.providerID ?? model.providerID, + parentID, + finish: last.finish ?? "stop", + time: { created: Date.now(), completed: Date.now() }, + metadata: + 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(), + messageID: msg.id, + sessionID, + 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(), + messageID: msg.id, + sessionID, + type: "tool", + callID: "call_revenue_query", + tool: last.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() }, + ...(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) + } + 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, + 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) { + 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 + 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 = measuredFiles.map( + (file) => `${file.messageID}:${file.id}:${file.mime}:${Bun.hash(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) + } + 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) + } + + 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 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.") + const result = yield* runSession(dir, llm, { + user: history, + assistantUsage: reported, + 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.", + }) + + 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( + "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", + () => + 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, + 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 rewrite of already-measured history grows the estimate and 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, + // Simulate a history transform expanding content present in the measured request. + last: { requestEstimateTokens: 100_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.") + }), + { git: true, config: providerCfg(LARGE_WINDOW) }, + ), + { 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", + () => + 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: { requestEstimateTokens: 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", + () => + 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 = sqlJsonTurns(135_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) + // 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) }, + ), + { 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 (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.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) }, + ), + { timeout: 120_000 }, +) + +it.live( + "an append-only follow-up reuses the recorded request estimate", + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + 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).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(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) }, + ), + { 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", + () => + 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 }, +) + +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(135_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 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, + 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 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 and tracks it once", + () => + 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 + const track = spyOn(Telemetry, "track") + 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) + 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 + } + }), + { 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, + config: providerCfg(SMALL_WINDOW), + }, + ), + { timeout: 120_000 }, +) + +// 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( + `reported usage with a different ${field} cannot calibrate the guard`, + () => + provideTmpdirServerLegacy( + Effect.fnUntraced(function* ({ dir, llm }) { + 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: { [field]: identity, requestEstimateTokens: 137_000 }, + 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(135_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. + // 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: [...sqlJsonTurns(135_000), "Break the revenue down by region."], + assistantUsage: 0, + last: { + finish: "tool-calls", + text: "Running the revenue query.", + toolOutput: "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 }, +) + +it.live( + "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, 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, + next: "Summarize the revenue concentration.", + nextFile: true, + }) + + 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 }) }, + ), + { timeout: 120_000 }, +) + +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 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", + () => + 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", + () => + 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(MEDIA_GUIDANCE) + }), + { git: true, config: providerCfg(LARGE_WINDOW, { attachment: true }) }, + ), + { 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( + "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", + () => + 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", + () => + 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 }) }, + ), + { 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", + () => + 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: sqlJsonTurns(135_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, { tools: true })(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) + // 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) + } + }) + + 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) + 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) + }) + + 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("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("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)), + ) + }) +}) 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)