Skip to content

Commit aa3fbbb

Browse files
kevin-dpclaude
andcommitted
refactor(agents): drop the mid-turn compaction min-tokens floor
The `minTokens` guard (default 2000, env ELECTRIC_AGENTS_COMPACT_MIN_TOKENS) never changed the outcome at any realistic ceiling — 90% of a real context window is always far above 2000 tokens, so the floor only ever mattered when testing with an artificially low ceiling. Codex has no equivalent floor (it triggers on a single token threshold). Remove the knob, its env override, and the now-unused positiveFromEnv helper. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 4be795d commit aa3fbbb

5 files changed

Lines changed: 2 additions & 29 deletions

File tree

.changeset/context-compaction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ into a summary):
2323
"Context compacted" entry in the conversation timeline.
2424

2525
Thresholds are env-tunable (`ELECTRIC_AGENTS_COMPACT_CEILING`,
26-
`ELECTRIC_AGENTS_COMPACT_BG_CEILING`, `ELECTRIC_AGENTS_COMPACT_MIN_TOKENS`).
26+
`ELECTRIC_AGENTS_COMPACT_BG_CEILING`).

packages/agents-runtime/src/compaction-midturn.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ export interface MidTurnCompactorDeps {
1515
writeCheckpoint: (status: CompactionStatus, content: string) => void
1616
/** Compaction fires at/above this fraction of the context window. */
1717
ceiling: number
18-
/** Don't compact unless the context is at least this many tokens. */
19-
minTokens: number
2018
/** Keep this many of the most recent messages verbatim (the live tail). */
2119
keepTail: number
2220
}
@@ -77,9 +75,7 @@ export function createMidTurnCompactor(
7775
: null
7876

7977
return async ({ messages, currentTokens, contextWindow }) => {
80-
const overCeiling =
81-
currentTokens >= deps.ceiling * contextWindow &&
82-
currentTokens > deps.minTokens
78+
const overCeiling = currentTokens >= deps.ceiling * contextWindow
8379

8480
// Under the ceiling: keep the compacted view sticky if we already compacted
8581
// this turn, otherwise leave the context untouched.

packages/agents-runtime/src/context-factory.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ import {
3737
} from './compaction-summarize'
3838
import { CONTEXT_USAGE_BACKGROUND_START } from './token-accountant'
3939

40-
/** Default minimum context size (tokens) before mid-turn compaction fires. */
41-
const DEFAULT_COMPACT_MIN_TOKENS = 2000
4240
/** Recent messages kept verbatim when compacting (the live tool-call tail). */
4341
const COMPACT_KEEP_TAIL = 6
4442
import { createContextTools } from './tools/context-tools'
@@ -101,12 +99,6 @@ function ratioFromEnv(raw: string | undefined, fallback: number): number {
10199
return Number.isFinite(value) && value > 0 && value <= 1 ? value : fallback
102100
}
103101

104-
/** Parse a positive number from an env string, falling back when invalid. */
105-
function positiveFromEnv(raw: string | undefined, fallback: number): number {
106-
const value = Number(raw)
107-
return Number.isFinite(value) && value > 0 ? value : fallback
108-
}
109-
110102
const MAX_HYDRATED_IMAGE_ATTACHMENTS = 4
111103
const MAX_HYDRATED_IMAGE_ATTACHMENT_BYTES = 10 * 1024 * 1024
112104

@@ -843,19 +835,13 @@ export function createHandlerContext<TState extends StateProxy = StateProxy>(
843835
// failed checkpoint that drives the UI. Thresholds overridable via env
844836
// (RFC §12 tunables):
845837
// ELECTRIC_AGENTS_COMPACT_CEILING (0..1, default 0.9)
846-
// ELECTRIC_AGENTS_COMPACT_MIN_TOKENS (default 2000)
847838
const compactCeiling = ratioFromEnv(
848839
process.env.ELECTRIC_AGENTS_COMPACT_CEILING,
849840
CONTEXT_USAGE_HARD_CEILING
850841
)
851-
const compactMinTokens = positiveFromEnv(
852-
process.env.ELECTRIC_AGENTS_COMPACT_MIN_TOKENS,
853-
DEFAULT_COMPACT_MIN_TOKENS
854-
)
855842
const compactProvider = agentModelProvider(activeAgentConfig)
856843
const onCompactContext = createMidTurnCompactor({
857844
ceiling: compactCeiling,
858-
minTokens: compactMinTokens,
859845
keepTail: COMPACT_KEEP_TAIL,
860846
writeCheckpoint: (status, content) => {
861847
contextApi.insertContext(COMPACTION_CHECKPOINT_ID, {

packages/agents-runtime/test/compaction-midturn.test.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ describe(`createMidTurnCompactor`, () => {
1717
summarize,
1818
writeCheckpoint,
1919
ceiling: 0.9,
20-
minTokens: 100,
2120
keepTail: 4,
2221
})
2322
const out = await compact({
@@ -37,7 +36,6 @@ describe(`createMidTurnCompactor`, () => {
3736
summarize,
3837
writeCheckpoint,
3938
ceiling: 0.9,
40-
minTokens: 100,
4139
keepTail: 4,
4240
})
4341
const messages = msgs(10)
@@ -62,7 +60,6 @@ describe(`createMidTurnCompactor`, () => {
6260
summarize,
6361
writeCheckpoint: vi.fn(),
6462
ceiling: 0.9,
65-
minTokens: 100,
6663
keepTail: 4,
6764
})
6865
const messages = msgs(10)
@@ -88,7 +85,6 @@ describe(`createMidTurnCompactor`, () => {
8885
summarize,
8986
writeCheckpoint: vi.fn(),
9087
ceiling: 0.9,
91-
minTokens: 100,
9288
keepTail: 4,
9389
})
9490
await compact({
@@ -114,7 +110,6 @@ describe(`createMidTurnCompactor`, () => {
114110
summarize,
115111
writeCheckpoint: vi.fn(),
116112
ceiling: 0.9,
117-
minTokens: 100,
118113
keepTail: 4,
119114
})
120115
// Boundary would land at index 6 (10 − keepTail 4); make that a tool_result
@@ -139,7 +134,6 @@ describe(`createMidTurnCompactor`, () => {
139134
summarize,
140135
writeCheckpoint: (s: string) => statuses.push(s),
141136
ceiling: 0.9,
142-
minTokens: 100,
143137
keepTail: 4,
144138
})
145139
const out = await compact({

packages/agents-runtime/test/compaction-trigger.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ import type { ChangeEvent } from '@durable-streams/state'
1111
// we drive the trigger with the env override + a high seeded anchor instead.
1212
const savedEnv = {
1313
ceiling: process.env.ELECTRIC_AGENTS_COMPACT_CEILING,
14-
minTokens: process.env.ELECTRIC_AGENTS_COMPACT_MIN_TOKENS,
1514
}
1615
afterEach(() => {
1716
process.env.ELECTRIC_AGENTS_COMPACT_CEILING = savedEnv.ceiling
18-
process.env.ELECTRIC_AGENTS_COMPACT_MIN_TOKENS = savedEnv.minTokens
1917
})
2018

2119
function completedAssistantMessage(): unknown {
@@ -59,7 +57,6 @@ describe(`mid-turn compaction trigger`, () => {
5957
it(`compacts mid-turn: summarizer runs, model sees the summary, checkpoint persisted`, async () => {
6058
// Ceiling tiny so any real model window is crossed by the seeded anchor.
6159
process.env.ELECTRIC_AGENTS_COMPACT_CEILING = `0.0001`
62-
process.env.ELECTRIC_AGENTS_COMPACT_MIN_TOKENS = `10`
6360

6461
// Enough messages that there's real content beyond the kept tail (6).
6562
const db = buildStreamFixture(

0 commit comments

Comments
 (0)