diff --git a/packages/core/src/session/compaction.ts b/packages/core/src/session/compaction.ts index 7f91fe9e8f5..91a4e0a19b7 100644 --- a/packages/core/src/session/compaction.ts +++ b/packages/core/src/session/compaction.ts @@ -52,9 +52,12 @@ export type Editor = { configure: (settings: Partial) => void } +/** A prompt's size: `measured` is what the provider reported at the latest response, `estimated` is the text since. */ +export type Estimate = { readonly measured: number; readonly estimated: number } + export type Trigger = - /** `overflow`: the provider just rejected this context as too long. */ - | { readonly reason: "auto" | "overflow"; readonly context: SessionContext.Loaded } + /** `overflow`: the provider just rejected this context as too long. `estimate` is `estimatePrompt(context)`. */ + | { readonly reason: "auto" | "overflow"; readonly context: SessionContext.Loaded; readonly estimate?: Estimate } /** `inputID` is the `/compact` inbox item, whose message shows the outcome. */ | { readonly reason: "manual"; readonly context: SessionContext.Loaded; readonly inputID: SessionMessage.ID } @@ -68,9 +71,12 @@ export interface Interface extends State.Transformable { readonly compact: (trigger: Trigger) => Effect.Effect /** * Starts a background summary when the context nears the threshold. Never waits for it. Loads `context` only when - * background summaries are on. + * background summaries are on. `estimate`, when given, is `estimatePrompt` of the loaded context. */ - readonly precompute: (context: Effect.Effect) => Effect.Effect + readonly precompute: ( + context: Effect.Effect, + estimate?: Estimate, + ) => Effect.Effect } export class Service extends Context.Service()("@opencode/SessionCompaction") {} @@ -287,7 +293,10 @@ export const layer = Layer.effect( // Only the user compacts when automatic compaction is off, overflow included. if (trigger.reason !== "manual" && !settings.auto) return { status: "skipped" } - if (trigger.reason === "auto" && !due(context, calculateCeiling(context.model.limit, settings.buffer))) + if ( + trigger.reason === "auto" && + !due(context, calculateCeiling(context.model.limit, settings.buffer), trigger.estimate) + ) return { status: "skipped" } const held = precomputed.get(context.session.id) precomputed.delete(context.session.id) @@ -296,7 +305,7 @@ export const layer = Layer.effect( // The provider just rejected this context, so the estimate ran low; the first attempt already aims below it. const budget = trigger.reason === "overflow" - ? Math.min(model.cap, Math.floor(estimateContext(context) * SHRINK_STEPS[0])) + ? Math.min(model.cap, Math.floor(total(trigger.estimate ?? estimatePrompt(context)) * SHRINK_STEPS[0])) : model.cap const selected = model.context === context ? trigger : { ...trigger, context: model.context } @@ -327,14 +336,17 @@ export const layer = Layer.effect( */ const precompute = Effect.fn("SessionCompaction.precompute")(function* ( load: Effect.Effect, + given?: Estimate, ) { const settings = state.get() if (!settings.precompute || !settings.auto) return const context = yield* load if (context.model.compaction?.type === "native") return const ceiling = calculateCeiling(context.model.limit, settings.buffer) - if (!Number.isFinite(ceiling) || due(context, ceiling)) return - if (estimateContext(context) < ceiling * PRECOMPUTE_RATIO) return + if (!Number.isFinite(ceiling)) return + const estimate = given ?? estimatePrompt(context) + if (due(context, ceiling, estimate)) return + if (total(estimate) < ceiling * PRECOMPUTE_RATIO) return const anchorIndex = context.messages.findLastIndex((message) => hasMeasuredPrompt(message, context.model.ref)) const anchor = context.messages[anchorIndex] @@ -406,7 +418,7 @@ export const layer = Layer.effect( return { ...exit.value, metadata: { keepID: held.keepID, ...exit.value.metadata } } }) - const due = (context: SessionContext.Loaded, ceiling: number) => { + const due = (context: SessionContext.Loaded, ceiling: number, estimate?: Estimate) => { const messages = context.messages // A compaction just completed; let the runner rebuild the request from it first. const last = messages.at(-1) @@ -414,7 +426,7 @@ export const layer = Layer.effect( // An encrypted native window estimates as nothing, so wait for a response to measure it. const measured = messages.findLastIndex((message) => hasMeasuredPrompt(message, context.model.ref)) if (measured < messages.findLastIndex(SessionHistory.isNativeCompaction)) return false - return estimateContext(context) >= ceiling + return total(estimate ?? estimatePrompt(context)) >= ceiling } /** @@ -1064,13 +1076,12 @@ export const recentUserMessages = ( return sendable.slice(oldestToDrop(sendable, estimateMessage, keep)) } -export const estimateContext = (context: SessionContext.Loaded) => { - const prompt = estimatePrompt(context) - return prompt.measured + prompt.estimated -} +const total = (estimate: Estimate) => estimate.measured + estimate.estimated -/** The prompt size: `measured` is what the provider reported at the latest response, `estimated` is the text since. */ -export const estimatePrompt = (context: SessionContext.Loaded) => { +export const estimateContext = (context: SessionContext.Loaded) => total(estimatePrompt(context)) + +/** The size of the prompt `context` sends. */ +export const estimatePrompt = (context: SessionContext.Loaded): Estimate => { const anchorIndex = context.messages.findLastIndex((message) => hasMeasuredPrompt(message, context.model.ref)) const anchor = context.messages[anchorIndex] const base = transcript(context, context.messages.slice(Math.max(0, anchorIndex))) diff --git a/packages/core/src/session/history.ts b/packages/core/src/session/history.ts index 19f0ce71658..0327d82d4ae 100644 --- a/packages/core/src/session/history.ts +++ b/packages/core/src/session/history.ts @@ -275,6 +275,31 @@ export const preview = Effect.fn("SessionHistory.preview")(function* ( .pipe(Effect.catch((error) => (error instanceof Instructions.InitializationBlocked ? error : Effect.die(error)))) }) +/** + * The synthetic messages `load(db, sessionID, "latest")` would return, read without the rest of the transcript. Under + * that boundary every row from the latest completed compaction on is kept, and synthetic rows carry no media. + */ +export const synthetics = Effect.fn("SessionHistory.synthetics")(function* ( + db: DatabaseService, + sessionID: SessionSchema.ID, +) { + const compaction = yield* latestCompaction(db, sessionID, "latest") + const rows = yield* db + .select() + .from(SessionMessageTable) + .where( + and( + eq(SessionMessageTable.session_id, sessionID), + eq(SessionMessageTable.type, "synthetic"), + compaction ? gte(SessionMessageTable.seq, compaction.seq) : undefined, + ), + ) + .orderBy(asc(SessionMessageTable.seq)) + .all() + .pipe(Effect.orDie) + return yield* Effect.forEach(rows, decodeMessageRow, { concurrency: 1 }) +}) + /** The id and text of the session's first user message. */ export const firstUserMessage = Effect.fn("SessionHistory.firstUserMessage")(function* ( db: DatabaseService, diff --git a/packages/core/src/session/instructions.ts b/packages/core/src/session/instructions.ts index 1165ea6e6d1..9eb121939e3 100644 --- a/packages/core/src/session/instructions.ts +++ b/packages/core/src/session/instructions.ts @@ -4,13 +4,14 @@ import { relative } from "path" import { Context, Effect, Layer, Option, Ref, Schema } from "effect" import { makeLocationNode } from "@opencode/util/effect/app-node" import { Bus } from "../bus.js" +import { Database } from "../database/database.js" import { FSUtil } from "@opencode/util/fs-util" import { Location } from "../location.js" import { SessionEvent } from "./event.js" import { MessageDecodeError } from "./error.js" +import { SessionHistory } from "./history.js" import { SessionMessage } from "./message.js" import { SessionSchema } from "./schema.js" -import { SessionStore } from "./store.js" const InjectedMetadata = Schema.Struct({ instruction: Schema.Struct({ paths: Schema.Array(Schema.String) }), @@ -30,7 +31,7 @@ const layer = Layer.effect( Effect.gen(function* () { const bus = yield* Bus.Service const fs = yield* FSUtil.Service - const store = yield* SessionStore.Service + const db = (yield* Database.Service).db const location = yield* Location.Service // Resolved once for the Location layer; the synthetic text and dedup ledger keep // absolute paths, but the human-facing description shows paths relative to the project @@ -54,7 +55,7 @@ const layer = Layer.effect( }) if (claimed.length === 0) return yield* Effect.gen(function* () { - const alreadyInjected = yield* previouslyInjected(store, input.sessionID) + const alreadyInjected = yield* previouslyInjected(db, input.sessionID) const toInject = claimed.filter((path) => !alreadyInjected.has(path)) if (toInject.length === 0) return const files = yield* Effect.forEach( @@ -96,9 +97,9 @@ const layer = Layer.effect( }), ) -function previouslyInjected(store: SessionStore.Interface, sessionID: SessionSchema.ID) { +function previouslyInjected(db: Database.Interface["db"], sessionID: SessionSchema.ID) { return Effect.gen(function* () { - const history = yield* store.context(sessionID) + const history = yield* SessionHistory.synthetics(db, sessionID) return new Set( history .filter((message): message is SessionMessage.Synthetic => message.type === "synthetic") @@ -121,5 +122,5 @@ function describePath(root: string, path: string) { export const node = makeLocationNode({ name: "session-instructions", layer, - deps: [Bus.node, FSUtil.node, Location.node, SessionStore.node], + deps: [Bus.node, Database.node, FSUtil.node, Location.node], }) diff --git a/packages/core/src/session/model-request.ts b/packages/core/src/session/model-request.ts index 0a4853a1b21..ac27e4111bc 100644 --- a/packages/core/src/session/model-request.ts +++ b/packages/core/src/session/model-request.ts @@ -138,9 +138,10 @@ const unsupportedMedia = ( return `ERROR: Cannot read ${name ? `"${name}"` : modality} (this model does not support ${unsupported} input). Inform the user.` } +// The inline budget counts base64 characters, which are ASCII, so a string's length is its size without scanning it. // Remote and provider-referenced media carry no local payload and never count toward the inline budget. const mediaBytes = (media: Media.Asset) => { - if (media.source.type === "base64") return Buffer.byteLength(media.source.data) + if (media.source.type === "base64") return media.source.data.length if (media.source.type === "bytes") return Math.ceil(media.source.data.byteLength / 3) * 4 return 0 } @@ -160,7 +161,7 @@ const replaceMedia = ( const result = part.result const value = result.value.map((item): Content => { if (item.type !== "file") return item - const text = replace({ mime: item.mime, name: item.name, bytes: () => Buffer.byteLength(item.uri) }) + const text = replace({ mime: item.mime, name: item.name, bytes: () => item.uri.length }) return text === undefined ? item : { type: "text", text } }) return value.every((item, index) => item === result.value[index]) @@ -185,7 +186,7 @@ export const boundImages = (messages: LLMRequest["messages"]) => { if (part.type === "media") return isImage(part.media.mediaType) ? [mediaBytes(part.media)] : [] if (part.type !== "tool-result" || part.result.type !== "content") return [] return part.result.value.flatMap((item: Content) => - item.type === "file" && isImage(item.mime) ? [Buffer.byteLength(item.uri)] : [], + item.type === "file" && isImage(item.mime) ? [item.uri.length] : [], ) }), ) diff --git a/packages/core/src/session/runner/llm.ts b/packages/core/src/session/runner/llm.ts index d04489a5732..344ae4c076f 100644 --- a/packages/core/src/session/runner/llm.ts +++ b/packages/core/src/session/runner/llm.ts @@ -214,13 +214,14 @@ const layer = Layer.effect( // Reuse boundary preparation once; retries refresh context without delivering more input. const loaded = initial ?? (yield* prepareContext(sessionID).pipe(Effect.flatMap(context.load))) initial = undefined - const compacted = yield* compaction.compact({ reason: "auto", context: loaded }) + const estimate = SessionCompaction.estimatePrompt(loaded) + const compacted = yield* compaction.compact({ reason: "auto", context: loaded, estimate }) if (compacted.status === "failed") return yield* new StepFailedError({ error: compacted.error }) if (compacted.status === "completed") { assistantMessageID = SessionMessage.ID.create() continue } - yield* compaction.precompute(Effect.succeed(loaded)) + yield* compaction.precompute(Effect.succeed(loaded), estimate) const stepLimitReached = loaded.agent.info.steps !== undefined && step >= loaded.agent.info.steps const transcript = SessionModelRequest.baseTranscript({ agent: loaded.agent.info, @@ -242,7 +243,7 @@ const layer = Layer.effect( // Keep tool definitions on the final Step to preserve the provider's cached prefix. toolChoice: stepLimitReached ? "none" : undefined, webSocket: "session", - inputTokens: SessionCompaction.estimatePrompt(loaded), + inputTokens: estimate, }) .pipe( // The hook event holds every message from before images are bounded, screenshots included, and is a separate @@ -279,7 +280,7 @@ const layer = Layer.effect( recoverOverflow: Effect.suspend(() => recoverOverflow ? compaction - .compact({ reason: "overflow", context: loaded }) + .compact({ reason: "overflow", context: loaded, estimate }) .pipe(Effect.map((result) => result.status === "completed")) : Effect.succeed(false), ), diff --git a/packages/core/test/session-compaction.test.ts b/packages/core/test/session-compaction.test.ts index c0714298ecb..0dd9666d4b1 100644 --- a/packages/core/test/session-compaction.test.ts +++ b/packages/core/test/session-compaction.test.ts @@ -406,8 +406,10 @@ it.effect("auto compaction estimates current content against the buffered prompt }, }) // An automatic compaction that is not due is skipped. - const due = (context: SessionContext.Loaded) => - compaction.compact({ reason: "auto", context }).pipe(Effect.map((outcome) => outcome.status !== "skipped")) + const due = (context: SessionContext.Loaded, estimate?: SessionCompaction.Estimate) => + compaction + .compact({ reason: "auto", context, estimate }) + .pipe(Effect.map((outcome) => outcome.status !== "skipped")) // 85% of the input limit, which takes precedence over the context window. const inputLimited = { context: 400_000, input: 272_000, output: 128_000 } @@ -451,6 +453,9 @@ it.effect("auto compaction estimates current content against the buffered prompt const grown = { ...input(89_000, contextLimited), messages: [{ ...assistant, content: [tool] }] } expect(SessionCompaction.estimateContext(grown)).toBe(90_000) expect(yield* due(grown)).toBe(true) + // A step estimates its prompt once and passes it; compaction decides on that instead of estimating again. + expect(yield* due(grown, { measured: 0, estimated: 0 })).toBe(false) + expect(yield* due(input(1_000, contextLimited), { measured: 84_000, estimated: 0 })).toBe(true) const interrupted = { ...assistant, id: SessionMessage.ID.create(), tokens: undefined } expect(SessionCompaction.estimateContext({ ...grown, messages: [...grown.messages, interrupted] })).toBe(90_001) diff --git a/packages/core/test/session-instructions.test.ts b/packages/core/test/session-instructions.test.ts index 8fe9ad86990..0298983c72f 100644 --- a/packages/core/test/session-instructions.test.ts +++ b/packages/core/test/session-instructions.test.ts @@ -21,6 +21,7 @@ import { ReadTool } from "@opencode/core/tool/plugin/read" import { ReadToolFileSystem } from "@opencode/core/tool/read-filesystem" import { SessionEvent } from "@opencode/core/session/event" import { SessionExecution } from "@opencode/core/session/execution" +import { SessionHistory } from "@opencode/core/session/history" import { SessionInstructions } from "@opencode/core/session/instructions" import { SessionMessage } from "@opencode/core/session/message" import { SessionProjector } from "@opencode/core/session/projector" @@ -103,10 +104,14 @@ const readCall = ( const writeAgents = (file: string, content: string) => Effect.promise(() => fs.writeFile(file, content)) const mkdir = (dir: string) => Effect.promise(() => fs.mkdir(dir, { recursive: true })) +// The instruction ledger reads only synthetic rows; they must match what full history shows the model. const synthetics = (sessionID: Session.ID) => Effect.gen(function* () { const store = yield* SessionStore.Service - return (yield* store.context(sessionID)).filter((message) => message.type === "synthetic") + const { db } = yield* Database.Service + const visible = (yield* store.context(sessionID)).filter((message) => message.type === "synthetic") + expect(yield* SessionHistory.synthetics(db, sessionID)).toEqual(visible) + return visible }) // Seed a prior synthetic message with an instruction dedup ledger, simulating a prior turn