Skip to content

Commit e1d3f81

Browse files
committed
fix(config): scope agent compaction window to agents.<id>.compaction.window and update docs
1 parent 30b487b commit e1d3f81

10 files changed

Lines changed: 68 additions & 30 deletions

File tree

‎packages/client/src/promise/generated/types.ts‎

Lines changed: 2 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/core/src/config/plugin/agent.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { ConfigAgent } from "@opencode/schema/config/agent"
66
import path from "path"
77
import { Effect, Option, Schema } from "effect"
88
import { Agent } from "../../agent.js"
9-
import { Provider } from "../../provider.js"
109
import { Config } from "../../config.js"
1110
import { ConfigMarkdown } from "../markdown.js"
1211
import { ConfigEntryObserver } from "./entry-observer.js"
@@ -32,7 +31,7 @@ type PathAction = FileAccess.ExternalDirectoryAuthorization["action"] | typeof R
3231
const pathActions = ["external_directory", "read", "edit"] as const satisfies readonly PathAction[]
3332
const agentKeys = new Set(["variant", ...Object.keys(ConfigAgent.Info.fields)])
3433
// Fields only V2 understands. A file that uses any of them is read as V2, whatever else it contains.
35-
const nativeKeys = ["permissions", "request", "system", "disabled"]
34+
const nativeKeys = ["compaction", "permissions", "request", "system", "disabled"]
3635

3736
export const Plugin = define({
3837
id: "opencode.config.agent",
@@ -89,9 +88,13 @@ export const Plugin = define({
8988
providerID: item.model.providerID,
9089
...(item.model.variant === undefined ? {} : { variant: item.model.variant }),
9190
}
91+
if (item.compaction?.window !== undefined) {
92+
agent.request.settings.compaction = {
93+
...agent.request.settings.compaction,
94+
window: item.compaction.window,
95+
}
96+
}
9297
if (item.request !== undefined) {
93-
if (item.request.settings !== undefined)
94-
agent.request.settings = Provider.mergeOverlay(agent.request.settings, item.request.settings) ?? {}
9598
Object.assign(agent.request.headers, item.request.headers ?? {})
9699
Object.assign(agent.request.body, item.request.body ?? {})
97100
}

‎packages/core/src/session/compaction.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ const calculateCeiling = (limit: SessionContext.Loaded["model"]["limit"], buffer
11461146

11471147
/**
11481148
* The limits automatic compaction plans for: the model's, narrowed to the effective compaction window when smaller.
1149-
* An agent's `request.settings.compaction.window` (or `request.settings.compactionWindow`) overrides the model's or
1149+
* An agent's configured `compaction.window` (`request.settings.compaction.window`) overrides the model's or
11501150
* provider's `settings.compaction.window`, which overrides the top-level `compaction.window`. A larger override on a
11511151
* model, variant, or agent can therefore opt into the model's full window even when a lower global `compaction.window`
11521152
* is set. It never raises the model's own window, and it leaves the summary request's size to the model's limits.
@@ -1155,10 +1155,8 @@ const autoLimit = (
11551155
context: SessionContext.Loaded,
11561156
configured?: number,
11571157
): SessionContext.Loaded["model"]["limit"] => {
1158-
const settings = context.agent.info.request?.settings
11591158
const window =
1160-
settings?.compaction?.window ??
1161-
settings?.["compactionWindow"] ??
1159+
context.agent.info.request?.settings?.compaction?.window ??
11621160
context.model.compaction?.window ??
11631161
configured
11641162
const limit = context.model.limit

‎packages/core/test/config/agent.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ permissions:
362362
agents: {
363363
reviewer: {
364364
model: "anthropic/claude-sonnet",
365+
compaction: { window: 60_000 },
365366
system: "Review carefully.",
366367
description: "Reviews changes",
367368
mode: "subagent",
@@ -381,6 +382,7 @@ permissions:
381382
info: decode({
382383
agents: {
383384
reviewer: {
385+
compaction: { window: 80_000 },
384386
request: {
385387
headers: { shared: "last", second: "two" },
386388
body: { retries: 2, effort: "high" },
@@ -407,7 +409,7 @@ permissions:
407409
model: { providerID: "anthropic", id: "claude-sonnet" },
408410
})
409411
expect(reviewer.request).toEqual({
410-
settings: {},
412+
settings: { compaction: { window: 80_000 } },
411413
headers: { first: "one", shared: "last", second: "two" },
412414
body: { enabled: true, profile: "review", retries: 2, effort: "high" },
413415
})
@@ -458,6 +460,8 @@ Review carefully.`,
458460
path.join(tmp.path, "agents", "native.md"),
459461
`---
460462
variant: high
463+
compaction:
464+
window: 50000
461465
request:
462466
headers:
463467
x-agent: native
@@ -501,7 +505,7 @@ Use native v2 fields.`,
501505
expect(yield* agents.get(Agent.ID.make("team/helper"))).toMatchObject({ system: "Help the team." })
502506
expect(yield* agents.get(Agent.ID.make("native"))).toMatchObject({
503507
system: "Use native v2 fields.",
504-
request: { headers: { "x-agent": "native" }, body: { effort: "high" } },
508+
request: { settings: { compaction: { window: 50000 } }, headers: { "x-agent": "native" }, body: { effort: "high" } },
505509
permissions: [...defaultPermissions, { action: "edit", resource: "*", effect: "deny" }],
506510
})
507511
const themed = yield* agents.get(Agent.ID.make("themed"))

‎packages/core/test/session-compaction.test.ts‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ it.effect("compaction window overrides and caps the automatic compaction ceiling
666666
time: { created: 0, completed: 0 },
667667
})
668668

669-
const agent = (settings?: { compaction?: { window?: number }; compactionWindow?: number }) => ({
669+
const agent = (settings?: { compaction?: { window?: number } }) => ({
670670
id: Agent.defaultID,
671671
info: {
672672
...Agent.Info.default(Agent.defaultID),
@@ -680,7 +680,7 @@ it.effect("compaction window overrides and caps the automatic compaction ceiling
680680

681681
const context = (input: number, overrides?: {
682682
model?: { compaction?: { window?: number } }
683-
agent?: { settings?: { compaction?: { window?: number }; compactionWindow?: number } }
683+
agent?: { settings?: { compaction?: { window?: number } } }
684684
}) => ({
685685
...loaded(session, [message(input)]),
686686
model: overrides?.model ? { ...resolved, compaction: overrides.model.compaction } : resolved,
@@ -706,14 +706,6 @@ it.effect("compaction window overrides and caps the automatic compaction ceiling
706706
yield* due(context(170_000, { model: { compaction: { window: 50_000 } }, agent: { settings: { compaction: { window: 200_000 } } } })),
707707
).toBe(true)
708708

709-
// The same override through request.settings.compactionWindow.
710-
expect(
711-
yield* due(context(169_999, { model: { compaction: { window: 50_000 } }, agent: { settings: { compactionWindow: 200_000 } } })),
712-
).toBe(false)
713-
expect(
714-
yield* due(context(170_000, { model: { compaction: { window: 50_000 } }, agent: { settings: { compactionWindow: 200_000 } } })),
715-
).toBe(true)
716-
717709
// Invalid (-1) or larger-than-limit (1_000_000) windows never raise the ceiling past the model's own limit.
718710
expect(yield* due(context(169_999, { model: { compaction: { window: -1 } } }))).toBe(false)
719711
expect(yield* due(context(170_000, { model: { compaction: { window: -1 } } }))).toBe(true)

‎packages/protocol/openapi.json‎

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/schema/src/config/agent.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import { ConfigProvider } from "./provider.js"
88

99
export const Color = Schema.String.check(Schema.isPattern(/^#[0-9a-fA-F]{6}$/u))
1010

11+
export class Compaction extends Schema.Class<Compaction>("Config.Agent.Compaction")({
12+
window: PositiveInt.pipe(optional),
13+
}) {}
14+
1115
export class Info extends Schema.Class<Info>("Config.Agent")({
1216
model: ConfigModel.Selection.pipe(optional),
17+
compaction: Compaction.pipe(optional),
1318
request: ConfigProvider.Request.pipe(optional),
1419
system: Schema.String.pipe(optional),
1520
description: Schema.String.pipe(optional),

‎packages/schema/src/config/provider.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const ModelOverlays = {
3232
}
3333

3434
export class Request extends Schema.Class<Request>("Config.Provider.Request")({
35-
settings: Overlays.settings,
3635
headers: Overlays.headers,
3736
body: Overlays.body,
3837
}) {}

‎services/www/src/docs/content/compaction.mdx‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Add `compaction` to any [OpenCode configuration file](/config):
8585
| `auto` | `true` | Compact automatically near the context limit, and recover once when a provider rejects a request as too long. Manual compaction always works. |
8686
| `keep.tokens` | `15000` | Approximate recent conversation kept beside the summary. Larger values preserve more detail but leave less room for new work. |
8787
| `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. |
88+
| `window` | model limit | Optional token cap on the context window used to schedule automatic compaction. Never raises a model's own limit. |
8889

8990
`keep.tokens` accepts a non-negative integer.
9091

@@ -102,6 +103,36 @@ Add `compaction` to any [OpenCode configuration file](/config):
102103
- A token count such as `20000` also works. The same count is a large share of a small window and a small share of a large one.
103104
- A token count at least as large as the model's window is ignored and the default applies.
104105

106+
### Window
107+
108+
Set `compaction.window` to compact earlier on large-context models while keeping the full window available when a single turn or summary needs it. You can override the window per provider, model, variant, or agent:
109+
110+
```jsonc title="opencode.jsonc"
111+
{
112+
"$schema": "https://opencode.ai/config.json",
113+
"compaction": {
114+
"window": 60000,
115+
},
116+
"providers": {
117+
"anthropic": {
118+
"settings": { "compaction": { "window": 80000 } },
119+
"models": {
120+
"claude-opus-4-7": {
121+
"settings": { "compaction": { "window": 120000 } },
122+
},
123+
},
124+
},
125+
},
126+
"agents": {
127+
"compaction": {
128+
"compaction": { "window": 200000 },
129+
},
130+
},
131+
}
132+
```
133+
134+
More specific settings win (`agents.<id>.compaction.window` > variant/model/provider `settings.compaction.window` > top-level `compaction.window`), so a model or agent can opt back into a larger window up to the model's own limit.
135+
105136
## Model
106137

107138
By default OpenCode writes the summary itself, using the session's model. Set

‎services/www/src/docs/content/config.mdx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,12 @@ Control automatic context compaction and how much recent context it preserves.
364364
"tokens": 15000,
365365
},
366366
"buffer": "15%",
367+
"window": 80000,
367368
},
368369
}
369370
```
370371

371-
`buffer` is the share of the model's context window kept free before automatic compaction starts.
372+
`buffer` is the share of the effective context window kept free before automatic compaction starts. `window` optionally caps the context window used to schedule automatic compaction without raising a smaller model limit; override it per provider, model, or variant with `settings.compaction.window`, or per agent with `compaction.window`.
372373

373374
Local summaries remain the default. Opt into native provider compaction for both
374375
automatic and manual requests with a provider or model policy:

0 commit comments

Comments
 (0)