diff --git a/packages/app/e2e/regression/timeline-stream.spec.ts b/packages/app/e2e/regression/timeline-stream.spec.ts index 38b128fb430..324ede83bce 100644 --- a/packages/app/e2e/regression/timeline-stream.spec.ts +++ b/packages/app/e2e/regression/timeline-stream.spec.ts @@ -1566,17 +1566,12 @@ test.describe("compaction", () => { await expect(page.locator('[data-component="session-working"]')).toBeVisible() }) - test("shows an automatic compaction as one quiet divider with background compaction on", async ({ page }) => { - const config = page.waitForResponse((response) => new URL(response.url()).pathname === "/api/config") - + test("shows an automatic compaction as one quiet divider", async ({ page }) => { const timeline = await setupTimeline(page, { sessionMessages: [user, completed], sessionStatus: { [sessionID]: { type: "busy" } }, - configEntries: [{ type: "document", info: { experimental: { background_compaction: true } } }], }) - await config - await timeline.send(compactionStarted({ sessionID, reason: "auto" })) await timeline.send(compactionDelta({ sessionID, text: "Partial automatic summary." })) const compaction = page.locator('[data-component="session-compaction-message"]') @@ -1640,11 +1635,17 @@ test.describe("compaction", () => { await timeline.send(compactionDelta({ sessionID, text: outcome.partial })) await expect(compactions).toHaveCount(index + 1) const compaction = compactions.nth(index) - await expect(compaction).toContainText(outcome.partial) + + // An automatic compaction runs as one quiet divider; a manual one streams its summary. + if (outcome.reason === "auto") + await expect(compaction.locator('[data-slot="session-compaction-divider"]')).toHaveAttribute( + "data-state", + "running", + ) + + if (outcome.reason === "manual") await expect(compaction).toContainText(outcome.partial) if (name === "interrupted") { - await expect(compaction.getByRole("status").getByLabel("Compacting", { exact: true })).toBeVisible() - const request = page.waitForRequest( (request) => request.method() === "POST" && new URL(request.url()).pathname === `/api/session/${sessionID}/interrupt`, diff --git a/packages/app/src/session/timeline/cache.ts b/packages/app/src/session/timeline/cache.ts index 63f6c8da8f6..d73922409da 100644 --- a/packages/app/src/session/timeline/cache.ts +++ b/packages/app/src/session/timeline/cache.ts @@ -84,18 +84,6 @@ export function createTimelineCache( const sessionMenu = select(() => markdown?.sessionMenu) const turnDiff = select(() => data.turnDiff) - // The Location's config is a query: read it in the reader and only while selected, so a pending config - // suspends that reader as the live provider would rather than this view's owner, the whole screen. - let compaction = false - - const backgroundCompaction = () => { - if (!active()) return compaction - - compaction = data.backgroundCompaction - - return compaction - } - const view = () => render( { @@ -135,9 +123,6 @@ export function createTimelineCache( get copyAlwaysVisible() { return data.copyAlwaysVisible }, - get backgroundCompaction() { - return backgroundCompaction() - }, navigateToSession: data.navigateToSession, sessionHref: data.sessionHref, readMedia: data.readMedia, diff --git a/packages/app/src/shell/routes/session-ui-provider.tsx b/packages/app/src/shell/routes/session-ui-provider.tsx index 71fad9ef94b..8df23d77b0b 100644 --- a/packages/app/src/shell/routes/session-ui-provider.tsx +++ b/packages/app/src/shell/routes/session-ui-provider.tsx @@ -1,7 +1,6 @@ import { DataProvider, type TurnDiffInput } from "@opencode/session-ui/context" import { MarkdownProvider, type ReadMarkdownImage } from "@opencode/session-ui/context/markdown" import { useNavigate, useParams } from "@solidjs/router" -import { useQuery } from "@tanstack/solid-query" import { createEffect, createMemo, createSignal, on, onCleanup, untrack, type ParentProps } from "solid-js" import { useProviders } from "@/providers/catalog/providers" import { LocalProvider } from "@/providers/models/selection" @@ -197,25 +196,6 @@ export function SessionUIProvider( const providers = useProviders(directory) - const config = useQuery(() => ({ - queryKey: [serverSDK.scope, "session-ui-config", directory()], - enabled: serverSDK.connection.status() === "connected", - queryFn: () => serverSDK.api.config.get({ location: { directory: directory() } }), - })) - - onCleanup( - serverSDK.event.on("config.updated", (event) => { - if (event.location && event.location.directory !== directory()) return - void config.refetch() - }), - ) - - // Matches the server: the highest-priority document that sets `experimental` decides. - const backgroundCompaction = () => - config.data - ?.flatMap((entry) => (entry.type === "document" && entry.info.experimental ? [entry.info.experimental] : [])) - .at(-1)?.background_compaction === true - // Turn diffs live in the routed tab's in-memory state, so they outlast timeline remounts and go when the tab closes. // A cached timeline keeps the source it had while routed, so a child keeps its tab's diffs after the tab returns to // the parent. A route without an open tab, such as one whose tab is closing, loads into a loose cache rather than @@ -268,7 +248,6 @@ export function SessionUIProvider( turnDiff={turnDiff()} onNavigateToSession={navigateToSession} onSessionHref={href} - backgroundCompaction={backgroundCompaction()} > { @@ -477,8 +473,8 @@ test("a view first reading its providers while another Location is selected read input.cache() input.setState("deferred", true) expect(input.deferredReads).toEqual({ - "ses_a in /repo": ["/repo", "/repo", "/repo", "/repo"], - "ses_b in /other": ["/other", "/other", "/other", "/other"], + "ses_a in /repo": ["/repo", "/repo", "/repo"], + "ses_b in /other": ["/other", "/other", "/other"], }) } finally { input.dispose() diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index f0748c53651..d502eb3bd6e 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -2513,7 +2513,12 @@ export type ConfigEntry = } } } - compaction?: { auto?: boolean; keep?: { tokens?: number }; buffer?: ConfigCompactionPercent | number } + compaction?: { + auto?: boolean + background?: boolean + keep?: { tokens?: number } + buffer?: ConfigCompactionPercent | number + } skills?: Array commands?: { [x: string]: { @@ -2589,7 +2594,6 @@ export type ConfigEntry = codemode_vm?: boolean all_flags?: boolean todo?: boolean - background_compaction?: boolean media_blob?: boolean schedules?: boolean artifacts?: boolean diff --git a/packages/core/src/config/normalize.ts b/packages/core/src/config/normalize.ts index 891e6418f66..a3138bd5da9 100644 --- a/packages/core/src/config/normalize.ts +++ b/packages/core/src/config/normalize.ts @@ -353,14 +353,15 @@ function normalizeCompaction( unsupportedIfPresent(input.compaction, "tail_turns", ["compaction", "tail_turns"], diagnostics) unsupportedIfPresent(input.compaction, "prune", ["compaction", "prune"], diagnostics) const result: Record = {} - if (own(input.compaction, "auto")) { + for (const key of ["auto", "background"] as const) { + if (!own(input.compaction, key)) continue const value = decodeEncoded( - ConfigCompaction.Info.fields.auto, - input.compaction.auto, - ["compaction", "auto"], + ConfigCompaction.Info.fields[key], + input.compaction[key], + ["compaction", key], diagnostics, ) - if (value !== undefined) result.auto = value + if (value !== undefined) result[key] = value } const legacyTokens = own(input.compaction, "preserve_recent_tokens") ? decodeEncoded( diff --git a/packages/core/src/config/plugin/compaction.ts b/packages/core/src/config/plugin/compaction.ts index 28a4cc49652..cc7d0dfbda9 100644 --- a/packages/core/src/config/plugin/compaction.ts +++ b/packages/core/src/config/plugin/compaction.ts @@ -3,7 +3,6 @@ export * as ConfigCompactionPlugin from "./compaction.js" import { define } from "@opencode/plugin/effect/plugin" import { Effect } from "effect" import { Config } from "../../config.js" -import { Flag } from "../../flag.js" import { SessionCompaction } from "../../session/compaction.js" import { ConfigEntryObserver } from "./entry-observer.js" @@ -12,14 +11,13 @@ export const Plugin = define({ effect: Effect.fn(function* (ctx) { const config = yield* Config.Service const compaction = yield* SessionCompaction.Service - const flags = yield* Flag.Service const loaded = yield* ConfigEntryObserver.observe(config, ctx.event, compaction.reload()) yield* compaction.transform((editor) => { - editor.configure({ precompute: flags.resolve(loaded.entries).background_compaction.enabled }) for (const entry of loaded.entries) { if (entry.type !== "document" || !entry.info.compaction) continue editor.configure({ ...(entry.info.compaction.auto === undefined ? {} : { auto: entry.info.compaction.auto }), + ...(entry.info.compaction.background === undefined ? {} : { background: entry.info.compaction.background }), ...(entry.info.compaction.buffer === undefined ? {} : { buffer: entry.info.compaction.buffer }), ...(entry.info.compaction.keep?.tokens === undefined ? {} : { keep: entry.info.compaction.keep.tokens }), }) diff --git a/packages/core/src/session/compaction.ts b/packages/core/src/session/compaction.ts index e1df47e7eaf..62ff2dfd84f 100644 --- a/packages/core/src/session/compaction.ts +++ b/packages/core/src/session/compaction.ts @@ -23,7 +23,6 @@ import { Agent } from "../agent.js" import { Bus } from "../bus.js" import { Database } from "../database/database.js" import { llmClient } from "../effect/app-node-platform.js" -import { Instructions } from "../instructions/index.js" import { Model } from "../model.js" import { State } from "../state.js" import { Token } from "../util/token.js" @@ -46,7 +45,7 @@ export type Settings = { /** Tokens of recent conversation kept verbatim beside the summary. */ keep: number /** Summarize in the background as the context nears the threshold, so automatic compaction can swap it in. */ - precompute: boolean + background: boolean } export type Editor = { @@ -112,8 +111,6 @@ type Background = { type Precomputed = { readonly firstID: SessionMessage.ID | undefined readonly keepID: SessionMessage.ID - readonly model: SessionContext.Loaded["model"]["ref"] - readonly initial: string /** The longest tail since `keepID` that compaction still accepts beside this summary. */ readonly tailLimit: number /** Background summaries that failed since the last compaction, this one included once it has. */ @@ -266,7 +263,7 @@ export const layer = Layer.effect( const state = State.create({ name: "session-compaction", - initial: () => ({ auto: true, keep: 15_000, precompute: false }), + initial: () => ({ auto: true, keep: 15_000, background: true }), editor: (settings) => ({ configure: (update) => { Object.assign(settings, update) @@ -324,7 +321,7 @@ export const layer = Layer.effect( // opens here, before any wait, so a background summary still being written shows as a running compaction. if (trigger.reason !== "manual") yield* bus.publish(SessionEvent.Compaction.Started, { sessionID: context.session.id, reason: "auto" }) - const swapped = held ? yield* usePrecomputed(selected, held, budget, settings) : undefined + const swapped = held ? yield* usePrecomputed(selected, held, budget, native) : undefined const compaction = swapped ? Effect.succeed(swapped) : native @@ -353,13 +350,13 @@ export const layer = Layer.effect( given?: Estimate, ) { const settings = state.get() - if (!settings.precompute || !settings.auto) return + if (!settings.background || !settings.auto) return const context = yield* load if (context.model.compaction?.type === "native") return + // An unknown window has an infinite ceiling, which this never reaches. Past the ceiling it still starts: at the + // end of a turn, the next prompt then swaps it in instead of waiting for a summary. const ceiling = calculateCeiling(context.model.limit, settings.buffer) - if (!Number.isFinite(ceiling)) return const estimate = given ?? estimatePrompt(context) - if (due(context, ceiling, estimate)) return const first = context.messages.find((message) => hasMeasuredPrompt(message, context.model.ref)) const cycleStart = first?.type === "assistant" && first.tokens ? promptTokens(first.tokens) : 0 if (total(estimate) < ceiling - Math.min(PRECOMPUTE_LEAD, (ceiling - cycleStart) / 2)) return @@ -397,8 +394,6 @@ export const layer = Layer.effect( precomputed.set(sessionID, { firstID: context.messages[0]?.id, keepID: anchor.id, - model: context.model.ref, - initial: Instructions.join(context.initial), tailLimit: tailLimit(model.cap), failures, usage, @@ -409,23 +404,18 @@ export const layer = Layer.effect( /** * The held summary plus everything since it, kept verbatim, when it still describes this conversation and that * tail is short. Waits for a summary still being written. Undefined, after stopping the background work, when it - * cannot be used; compaction then summarizes as usual. + * cannot be used; compaction then summarizes as usual. A summary is plain text, so it serves any model, but a + * session that switched to native compaction compacts natively. */ const usePrecomputed = Effect.fnUntraced(function* ( trigger: Trigger, held: Precomputed, budget: number, - settings: Settings, + native: boolean, ): Effect.fn.Return { const context = trigger.context const index = heldIndex(held, context) - const usable = - settings.precompute && - trigger.reason !== "overflow" && - context.model.compaction?.type !== "native" && - index >= 0 && - tailTokens(context, index) <= tailLimit(budget) - if (!usable) { + if (native || index < 0 || tailTokens(context, index) > tailLimit(budget)) { yield* Fiber.interrupt(held.fiber) return undefined } @@ -966,12 +956,10 @@ export const node = makeLocationNode({ /** * Where the messages kept beside a held summary start, or -1 when the summary no longer describes this - * conversation: a compaction or revert since replaced its start, or the model or instructions changed. + * conversation: a compaction or revert since replaced its start. */ const heldIndex = (held: Precomputed, context: SessionContext.Loaded) => { - if (held.firstID !== context.messages[0]?.id || held.initial !== Instructions.join(context.initial)) return -1 - if (held.model.providerID !== context.model.ref.providerID || held.model.id !== context.model.ref.id) return -1 - if (held.model.variant !== context.model.ref.variant) return -1 + if (held.firstID !== context.messages[0]?.id) return -1 return context.messages.findIndex((message) => message.id === held.keepID) } diff --git a/packages/core/test/config/normalization.test.ts b/packages/core/test/config/normalization.test.ts index aa32eea379f..59ee7b46d0f 100644 --- a/packages/core/test/config/normalization.test.ts +++ b/packages/core/test/config/normalization.test.ts @@ -567,7 +567,6 @@ describe("ConfigNormalize", () => { experimental: { portable_shell_scanner: true, todo: true, - background_compaction: true, media_blob: true, part_rows: true, image_generate: { model: "openai/gpt-image-2" }, @@ -580,7 +579,6 @@ describe("ConfigNormalize", () => { experimental: { portable_shell_scanner: true, todo: true, - background_compaction: true, media_blob: true, part_rows: true, image_generate: { model: { providerID: "openai", model: "gpt-image-2" } }, diff --git a/packages/core/test/session-compaction.test.ts b/packages/core/test/session-compaction.test.ts index 1b541314993..400768df67c 100644 --- a/packages/core/test/session-compaction.test.ts +++ b/packages/core/test/session-compaction.test.ts @@ -1025,12 +1025,6 @@ const precomputeHistory = () => { return { near: [...near, anchor], due, anchor } } -const enablePrecompute = Effect.gen(function* () { - const compaction = yield* SessionCompaction.Service - yield* compaction.transform((editor) => editor.configure({ precompute: true })) - return compaction -}) - const summaryWindow = resolved.limit.context const summaryBuffer = summaryWindow / 8 const summaryCeiling = summaryWindow - summaryBuffer @@ -1050,7 +1044,7 @@ const summaryFixture = Effect.fnUntraced(function* (messages: readonly SessionMe }, } const compaction = yield* SessionCompaction.Service - yield* compaction.transform((editor) => editor.configure({ buffer: summaryBuffer, precompute: true })) + yield* compaction.transform((editor) => editor.configure({ buffer: summaryBuffer })) return { context, compaction, @@ -1211,10 +1205,11 @@ it.effect("summary measured recovery reuses usage only for the matching agent", }), ) -it.effect("background summaries stay off unless enabled", () => +it.effect("background summaries stay off when disabled", () => Effect.gen(function* () { requests = [] const compaction = yield* SessionCompaction.Service + yield* compaction.transform((editor) => editor.configure({ background: false })) const session = yield* insertSession(Session.ID.make("ses_precompute_off")) const history = precomputeHistory() @@ -1232,7 +1227,7 @@ it.effect("automatic compaction swaps in the background summary without another requests = [] const db = (yield* Database.Service).db const store = yield* SessionStore.Service - const compaction = yield* enablePrecompute + const compaction = yield* SessionCompaction.Service const sessionID = Session.ID.make("ses_precompute_swap") const session = yield* insertSession(sessionID) const history = precomputeHistory() @@ -1284,7 +1279,7 @@ it.effect("automatic compaction waits for a background summary still being writt const release = yield* Deferred.make() beforeResponse = Deferred.await(release) const bus = yield* Bus.Service - const compaction = yield* enablePrecompute + const compaction = yield* SessionCompaction.Service const session = yield* insertSession(Session.ID.make("ses_precompute_join")) const history = precomputeHistory() const started = yield* bus @@ -1309,7 +1304,7 @@ it.effect("automatic compaction waits for a background summary still being writt it.effect("a model switch waits for the new model's first response before summarizing in the background", () => Effect.gen(function* () { requests = [] - const compaction = yield* enablePrecompute + const compaction = yield* SessionCompaction.Service const session = yield* insertSession(Session.ID.make("ses_precompute_switch")) const history = precomputeHistory() const switched = (messages: readonly SessionMessage.Info[]) => ({ @@ -1330,7 +1325,7 @@ it.effect("a model switch waits for the new model's first response before summar it.effect("a background summary that fell too far behind is replaced from a newer response", () => Effect.gen(function* () { requests = [] - const compaction = yield* enablePrecompute + const compaction = yield* SessionCompaction.Service const session = yield* insertSession(Session.ID.make("ses_precompute_behind")) const history = precomputeHistory() // About 75k tokens arrived after the summarized prefix, past the 68k (40% of the 170k budget) that may stay @@ -1354,7 +1349,7 @@ it.effect("a background summary that fell too far behind is replaced from a newe it.effect("a failed background summary is retried from a newer response, then left until compaction", () => Effect.gen(function* () { requests = [] - const compaction = yield* enablePrecompute + const compaction = yield* SessionCompaction.Service const session = yield* insertSession(Session.ID.make("ses_precompute_failed")) const history = precomputeHistory() const later = [...history.near, textUser("Third question"), measured("Third answer", 160_000)] @@ -1386,35 +1381,87 @@ it.effect("a failed background summary is retried from a newer response, then le it.effect("compaction summarizes as usual when the background summary no longer fits the conversation", () => Effect.gen(function* () { - const compaction = yield* enablePrecompute + requests = [] + const compaction = yield* SessionCompaction.Service const session = yield* insertSession(Session.ID.make("ses_precompute_stale")) const history = precomputeHistory() - const compactAfterPrecompute = (due: readonly SessionMessage.Info[], context = loaded(session, due)) => - Effect.gen(function* () { - requests = [] - yield* compaction.precompute(Effect.succeed(loaded(session, history.near))) - expect(yield* compaction.compact({ reason: "auto", context })).toEqual({ status: "completed" }) - // The background summary is stopped; the summary that is kept covers the whole conversation. - return JSON.stringify(requests.at(-1)?.messages) - }) - // About 75k tokens arrived after the summarized prefix, past the 68k that may stay verbatim beside it. const burst = [...history.near, textUser("x".repeat(300_000)), measured("Burst handled", 182_000)] - expect(yield* compactAfterPrecompute(burst)).toContain("Burst handled") - // A different model would replay a summary written for the previous one. + yield* compaction.precompute(Effect.succeed(loaded(session, history.near))) + expect(yield* compaction.compact({ reason: "auto", context: loaded(session, burst) })).toEqual({ + status: "completed", + }) + // The background summary is stopped; the summary that is kept covers the whole conversation. + expect(JSON.stringify(requests.at(-1)?.messages)).toContain("Burst handled") + }), +) + +// The summary is plain text, so it still serves after a model switch, and after the compaction agent's own model +// stands in for the session's. +it.effect("a background summary is swapped in for a different model", () => + Effect.gen(function* () { + requests = [] + const compaction = yield* SessionCompaction.Service + const session = yield* insertSession(Session.ID.make("ses_precompute_other_model")) + const history = precomputeHistory() + + yield* compaction.precompute(Effect.succeed(loaded(session, history.near))) const switched = { ...loaded(session, history.due), model: { ...resolved, ref: { ...resolved.ref, id: Model.ID.make("other") } }, } - expect(yield* compactAfterPrecompute(history.due, switched)).toContain("Third answer") + expect(yield* compaction.compact({ reason: "auto", context: switched })).toEqual({ status: "completed" }) + + // The one request ended before the response the summary started from. + expect(requests).toHaveLength(1) + expect(JSON.stringify(requests[0].messages)).not.toContain("Second answer") + }), +) + +it.effect("an overflow compaction swaps in the background summary", () => + Effect.gen(function* () { + requests = [] + const compaction = yield* SessionCompaction.Service + const session = yield* insertSession(Session.ID.make("ses_precompute_overflow")) + const history = precomputeHistory() + + yield* compaction.precompute(Effect.succeed(loaded(session, history.near))) + expect(yield* compaction.compact({ reason: "overflow", context: loaded(session, history.due) })).toEqual({ + status: "completed", + }) + + // The one request ended before the response the summary started from. + expect(requests).toHaveLength(1) + expect(JSON.stringify(requests[0].messages)).not.toContain("Second answer") + }), +) + +it.effect("a background summary starts when the latest response already passed the threshold", () => + Effect.gen(function* () { + requests = [] + const compaction = yield* SessionCompaction.Service + const session = yield* insertSession(Session.ID.make("ses_precompute_past")) + const history = precomputeHistory() + + // The turn ended past the threshold; the summary is written while the session is idle. + yield* compaction.precompute(Effect.succeed(loaded(session, history.due))) + const next = [...history.due, textUser("Fourth question")] + expect(yield* compaction.compact({ reason: "auto", context: loaded(session, next) })).toEqual({ + status: "completed", + }) + + expect(requests).toHaveLength(1) + const summarized = JSON.stringify(requests[0].messages) + expect(summarized).toContain("Third question") + expect(summarized).not.toContain("Third answer") }), ) it.effect("a background summary keeps a tail longer than twice keep while it stays under 40% of the budget", () => Effect.gen(function* () { requests = [] - const compaction = yield* enablePrecompute + const compaction = yield* SessionCompaction.Service const session = yield* insertSession(Session.ID.make("ses_precompute_long_tail")) const history = precomputeHistory() // About 35k tokens arrived after the summarized prefix: more than twice keep, less than 68k. @@ -1438,7 +1485,7 @@ it.effect("a background summary keeps a tail longer than twice keep while it sta it.effect("a background summary starts a fixed lead before the threshold, not at a share of it", () => Effect.gen(function* () { requests = [] - const compaction = yield* enablePrecompute + const compaction = yield* SessionCompaction.Service const session = yield* insertSession(Session.ID.make("ses_precompute_lead")) // A 1M window compacts at 850k, so a background summary starts 60k before that, at 790k. const large = (messages: readonly SessionMessage.Info[]) => ({ @@ -1465,7 +1512,7 @@ it.effect("a background summary starts a fixed lead before the threshold, not at it.effect("a background summary starts on a window whose threshold is under 64k", () => Effect.gen(function* () { requests = [] - const compaction = yield* enablePrecompute + const compaction = yield* SessionCompaction.Service const session = yield* insertSession(Session.ID.make("ses_precompute_small")) // A 64k window compacts at 48k. The first response measured 20k, so half that room, 14k, sets the start at 34k. const small = (messages: readonly SessionMessage.Info[]) => ({ @@ -1490,7 +1537,7 @@ it.effect("a background summary starts on a window whose threshold is under 64k" it.effect("a background summary waits for a response whose prompt is still cached", () => Effect.gen(function* () { - const compaction = yield* enablePrecompute + const compaction = yield* SessionCompaction.Service const history = precomputeHistory() const later = (created: number) => [ ...history.near, diff --git a/packages/core/test/session-runner.test.ts b/packages/core/test/session-runner.test.ts index be3f89a8ff3..e39df55267f 100644 --- a/packages/core/test/session-runner.test.ts +++ b/packages/core/test/session-runner.test.ts @@ -2923,6 +2923,10 @@ describe("SessionRunnerLLM", () => { scenario(`preserves the session request prefix during ${reason} compaction (${route.id})`, function* (s) { const agents = yield* Agent.Service const hooks = yield* PluginHooks.Service + const compaction = yield* SessionCompaction.Service + // The idle step past the threshold would start a background summary, which keeps the next prompt verbatim + // instead of summarizing it; this covers the summary written when compaction is due. + yield* compaction.transform((editor) => editor.configure({ background: false })) const agentID = Agent.ID.make("reviewer") const variant = Model.VariantID.make("test-variant") s.currentModel = LanguageModel.make({ diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index 98161135b21..92b8f8fe379 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -16541,6 +16541,9 @@ "auto": { "type": "boolean" }, + "background": { + "type": "boolean" + }, "keep": { "type": "object", "properties": { @@ -16659,9 +16662,6 @@ "todo": { "type": "boolean" }, - "background_compaction": { - "type": "boolean" - }, "media_blob": { "type": "boolean" }, diff --git a/packages/schema/src/config/compaction.ts b/packages/schema/src/config/compaction.ts index e1d4f650a43..9af68bd1c6d 100644 --- a/packages/schema/src/config/compaction.ts +++ b/packages/schema/src/config/compaction.ts @@ -15,6 +15,10 @@ export type Percent = typeof Percent.Type export class Info extends Schema.Class("Config.Compaction")({ auto: Schema.Boolean.pipe(optional), + background: Schema.Boolean.pipe(optional).annotate({ + description: + "Write the summary in the background as the session nears automatic compaction, so it does not wait for one (default: true). A summary that is never used still costs tokens.", + }), keep: Keep.pipe(optional), buffer: Schema.Union([Percent, NonNegativeInt]).pipe(optional).annotate({ description: diff --git a/packages/schema/src/config/flag.ts b/packages/schema/src/config/flag.ts index 22001a1ae53..0628599901a 100644 --- a/packages/schema/src/config/flag.ts +++ b/packages/schema/src/config/flag.ts @@ -9,8 +9,6 @@ export const wildcard = "all" */ export const registry = { todo: "Enable the experimental todowrite tool.", - background_compaction: - "Enable experimental background compaction: summarize the conversation in the background as it nears the compaction threshold, so automatic compaction does not wait for the summary. Background summaries that are never used still cost tokens.", media_blob: "Store new session media once in a deduplicated blob table instead of inline in every message row.", schedules: "Enable experimental scheduled tasks that wake a session with a prompt.", artifacts: "Enable experimental artifacts backed by pluggable providers.", diff --git a/packages/session-ui/component-tests/session-compaction.spec.ts b/packages/session-ui/component-tests/session-compaction.spec.ts index 946d96b1824..52524f986d4 100644 --- a/packages/session-ui/component-tests/session-compaction.spec.ts +++ b/packages/session-ui/component-tests/session-compaction.spec.ts @@ -127,27 +127,6 @@ story("warms the label only once the gathering light reaches it", async ({ mount expect(cooled).toEqual([0, 0]) }) -story("keeps the full record for a running automatic compaction with background compaction off", async ({ mount }) => { - const timeline = await mount(`${rows}--compaction-in-progress`, { args: { backgroundCompaction: false } }) - const message = timeline.locator('[data-component="session-compaction-message"]') - - await expect(message).not.toHaveAttribute("data-quiet") - await expect(message.locator('[data-slot="session-compaction-divider"]')).toHaveCount(0) - await expect(message).toContainText("Session compaction started") - await expect(message.getByRole("status")).toBeVisible() -}) - -story("keeps the full record for a finished automatic compaction with background compaction off", async ({ mount }) => { - const timeline = await mount(`${rows}--compaction-and-continuation`, { args: { backgroundCompaction: false } }) - const message = timeline.locator('[data-component="session-compaction-message"]') - - await expect(message).not.toHaveAttribute("data-quiet") - await expect(message.locator('[data-slot="session-compaction-divider"]')).toHaveCount(0) - await expect(message).toContainText("Session compaction started") - await expect(message).toContainText("The Session timeline now consumes current nested assistant content.") - await expect(message).toContainText("Session compacted · 11.9K in · 508 out") -}) - story("keeps the full record for a manual compaction", async ({ mount }) => { const timeline = await mount(`${rows}--compaction-manual`) const message = timeline.locator('[data-component="session-compaction-message"]') diff --git a/packages/session-ui/src/context/data.tsx b/packages/session-ui/src/context/data.tsx index c1ba20b9097..a35f082710e 100644 --- a/packages/session-ui/src/context/data.tsx +++ b/packages/session-ui/src/context/data.tsx @@ -81,8 +81,6 @@ export const { peekMedia?: PeekMediaUrlFn onNavigateToSession?: NavigateToSessionFn onSessionHref?: SessionHrefFn - /** Whether `experimental.background_compaction` is on for this location; automatic compactions then show quietly. */ - backgroundCompaction?: boolean }) => { return { get store() { @@ -100,9 +98,6 @@ export const { get copyAlwaysVisible() { return props.copyAlwaysVisible === true }, - get backgroundCompaction() { - return props.backgroundCompaction === true - }, navigateToSession: props.onNavigateToSession, sessionHref: props.onSessionHref, shellRunning: props.shellRunning, diff --git a/packages/session-ui/src/message/message-content.tsx b/packages/session-ui/src/message/message-content.tsx index 0db7c12e580..4096aab8aaf 100644 --- a/packages/session-ui/src/message/message-content.tsx +++ b/packages/session-ui/src/message/message-content.tsx @@ -935,7 +935,6 @@ function CompactionDivider(props: { label: string }) { export function SessionCompactionMessage(props: { message: SessionMessageCompaction; error: string }) { const i18n = useI18n() - const data = useData() const summary = () => (props.message.status === "failed" ? "" : props.message.summary) const error = () => { @@ -982,7 +981,7 @@ export function SessionCompactionMessage(props: { message: SessionMessageCompact return ( {(revision) => (
- + (props.message.status === "failed" ? "" : props.message.summary) const error = () => { @@ -81,7 +79,7 @@ export function CompactionMessage(props: { message: SessionMessageCompaction; er return (
- + @@ -444,7 +444,7 @@ function CompactSessionStory() { Cancel next compaction - + @@ -453,34 +453,24 @@ function CompactSessionStory() { const CompactSession = { render: () => } -// Automatic compactions only show quietly with experimental.background_compaction on; turn it off to see today's record. -const backgroundCompaction = { - args: { backgroundCompaction: true }, - argTypes: { backgroundCompaction: { control: "boolean" } }, -} - export const CompactionInProgress = { - ...backgroundCompaction, - render: (args: { backgroundCompaction: boolean }) => ( + render: () => ( ), } export const CompactionAndContinuation = { - ...backgroundCompaction, - render: (args: { backgroundCompaction: boolean }) => ( + render: () => ( ), } @@ -492,7 +482,6 @@ export const CompactionManual = { description="A /compact keeps the full record: started divider, summary and usage." document={compactionManualDocument} width="600px" - backgroundCompaction /> ), } @@ -542,7 +531,7 @@ function CompactionReplayStory(props: { duration: number }) { Replay - + @@ -568,7 +557,6 @@ export const CompactionFailed = { description="A failed compaction keeps the production divider and shows the provider error." document={compactionFailedDocument} width="600px" - backgroundCompaction /> ), } @@ -580,7 +568,6 @@ export const CompactionCancelled = { description="An interrupted compaction keeps the production divider without an empty error block." document={compactionCancelledDocument} width="600px" - backgroundCompaction /> ), } diff --git a/services/site/src/docs/content/compaction.mdx b/services/site/src/docs/content/compaction.mdx index 47a373834cd..4f73e220780 100644 --- a/services/site/src/docs/content/compaction.mdx +++ b/services/site/src/docs/content/compaction.mdx @@ -83,6 +83,7 @@ Add `compaction` to any [OpenCode configuration file](/config): | Field | Default | Behavior | | --- | ---: | --- | | `auto` | `true` | Compact automatically near the context limit, and recover once when a provider rejects a request as too long. Manual compaction always works. | +| `background` | `true` | Write the summary ahead of time so automatic compaction doesn't wait for it. See [Background compaction](#background-compaction). | | `keep.tokens` | `15000` | Approximate recent conversation kept beside the summary. Larger values preserve more detail but leave less room for new work. | | `buffer` | `"15%"`, at least 16k | Share of the model's context window kept free. Automatic compaction starts once the conversation reaches the rest. Larger values compact earlier. | @@ -120,21 +121,8 @@ If that model cannot be resolved, OpenCode falls back to the session's model. ## Background compaction - - Experimental. This may change, break, or be removed in any release without notice. - - -Automatic compaction normally pauses the session while the summary is written. With background compaction, OpenCode -writes the summary ahead of time and swaps it in when compaction is due: - -```jsonc title="opencode.jsonc" -{ - "$schema": "https://opencode.ai/config.json", - "experimental": { - "background_compaction": true, - }, -} -``` +OpenCode writes the summary ahead of time, while the session keeps going, and swaps it in when compaction is due. +Automatic compaction then doesn't pause the session to write one: ```text 60k tokens before the threshold summary of the conversation so far starts in the background @@ -149,10 +137,21 @@ and the threshold. | --- | --- | | Cost | The summary request reuses the prompt cache. A summary that is never used, for example because the session ends before the threshold, still costs tokens. | | Waits | If the summary is still being written when compaction is due, the session waits for it to finish. | -| Falls back | Compaction summarizes as usual if the conversation since the held summary grew past about 40% of the threshold, or if the model, instructions, or start of the conversation changed. | -| Skipped | Not used for native compaction, right after a model switch, or when a provider rejects a request as too long. After a break longer than the prompt cache lifetime, it starts from the next response instead. | +| Falls back | Compaction summarizes as usual if the conversation since the held summary grew past about 40% of the threshold, or if the start of the conversation changed. | +| Skipped | Not used for native compaction. Right after a model switch, or after a break longer than the prompt cache lifetime, it starts from the next response instead. | | Storage | Held summaries live in memory and are lost on restart. | +Turn it off to write the summary only when compaction is due: + +```jsonc title="opencode.jsonc" +{ + "$schema": "https://opencode.ai/config.json", + "compaction": { + "background": false, + }, +} +``` + ## Providers Some providers can compact the conversation on their side instead. Enable it per diff --git a/services/site/src/docs/content/config.mdx b/services/site/src/docs/content/config.mdx index 0cded3ea2da..3a6cf47e547 100644 --- a/services/site/src/docs/content/config.mdx +++ b/services/site/src/docs/content/config.mdx @@ -629,7 +629,6 @@ All experimental settings live under the `experimental` key: "experimental": { "todo": true, "subagent_depth": 2, - "background_compaction": true, "image_generate": { "model": "openai/gpt-image-2" }, "location_idle_timeout": "4 hours", }, @@ -645,20 +644,6 @@ Set `todo` to `true` to give agents a `todowrite` tool for tracking multi-step w `subagent_depth` sets how deeply subagents can start their own subagents. Defaults to `1`, so a subagent cannot start another subagent. -### Background compaction - -Set `background_compaction` to `true` to write the compaction summary in the background before it is needed, so -automatic compaction doesn't pause the session. Defaults to `false`. See -[Compaction](/compaction#background-compaction) for how it works. - -```jsonc -{ - "experimental": { - "background_compaction": true, - }, -} -``` - ### Image generation `image_generate` sets the default model for the [`experimental_image_generate`](/tools#image-generate) tool, as