Skip to content

Commit 7846b32

Browse files
committed
fix(core): bound auto-compaction when the buffer exceeds the window
1 parent 0c6be86 commit 7846b32

2 files changed

Lines changed: 121 additions & 2 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,12 @@ export const layer = Layer.effect(
237237
return yield* compaction.pipe(
238238
Effect.matchEffect({
239239
onSuccess: (result) => publish(selected, result),
240-
onFailure: (failure) => publish(selected, failure),
240+
onFailure: (failure) =>
241+
// A skip is not a failed compaction: automatic compaction fires again once there is
242+
// something to summarize, and the session keeps working without a user-visible error.
243+
trigger.reason === "auto" && failure.error.type === "compaction.unavailable"
244+
? Effect.succeed({ status: "skipped" } as const)
245+
: publish(selected, failure),
241246
}),
242247
)
243248
})
@@ -281,6 +286,10 @@ export const layer = Layer.effect(
281286
const context = trigger.context
282287
const split = splitConversation(context.messages, keep)
283288
if (split === undefined) return yield* Effect.fail(NOTHING_TO_COMPACT)
289+
// Instructions and the opening prompt are not a conversation to summarize. Automatic compaction
290+
// waits for a completed reply; a manual request still summarizes whatever the history holds.
291+
if (trigger.reason === "auto" && !context.messages.some((message) => message.type === "assistant"))
292+
return yield* Effect.fail(NOTHING_TO_COMPACT)
284293

285294
const previous = previousCompaction(context.messages)
286295
const prompt = buildPrompt(previous !== undefined, previous?.summary.includes(LEGACY_HEADING) ?? false)
@@ -851,7 +860,9 @@ const calculateCeiling = (limit: SessionContext.Loaded["model"]["limit"], buffer
851860
// all, only a provider rejection can limit the request.
852861
const window = limit.input || limit.context
853862
if (window <= 0) return Number.POSITIVE_INFINITY
854-
if (buffer !== undefined) return window - buffer
863+
// A buffer at or above the window cannot leave room for the request; treat it as unset so the default
864+
// reserve applies. Clamping to a fraction instead can put the ceiling below the fixed prompt size.
865+
if (buffer !== undefined && buffer < window) return window - buffer
855866
return window - Math.max(Math.floor(window * RESERVE_SHARE), window >= 2 * RESERVE_MIN ? RESERVE_MIN : 0)
856867
}
857868

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,114 @@ it.effect("auto compaction estimates current content against the buffered prompt
362362
}),
363363
)
364364

365+
it.effect("clamps an oversized compaction buffer and waits for an exchange to summarize", () =>
366+
Effect.gen(function* () {
367+
const compaction = yield* SessionCompaction.Service
368+
const session = yield* insertSession(Session.ID.make("ses_oversized_buffer"))
369+
yield* compaction.transform((editor) => editor.configure({ buffer: 600_000 }))
370+
371+
const assistant = (tokens: number) =>
372+
Schema.decodeUnknownSync(SessionMessage.Assistant)({
373+
id: SessionMessage.ID.create(),
374+
type: "assistant",
375+
agent: Agent.defaultID,
376+
model: { id: "summary-model", providerID: "test" },
377+
content: [{ type: "text", text: "Done" }],
378+
tokens: { input: tokens, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
379+
time: { created: 0, completed: 0 },
380+
})
381+
const user = (text: string) =>
382+
SessionMessage.User.make({
383+
id: SessionMessage.ID.create(),
384+
type: "user",
385+
text,
386+
time: { created: DateTime.makeUnsafe(0) },
387+
})
388+
const context = (messages: readonly SessionMessage.Info[]) => ({
389+
...loaded(session, messages),
390+
model: SessionRunnerModel.resolved(model, {
391+
capabilities: { tools: true, input: ["text"], output: ["text"] },
392+
cost: [],
393+
limit: { context: 100_000, output: 10_000 },
394+
}),
395+
agent: {
396+
id: Agent.defaultID,
397+
info: { ...Agent.Info.default(Agent.defaultID), system: "You are a helpful assistant." },
398+
},
399+
initial: "Project instructions.",
400+
})
401+
const status = (messages: readonly SessionMessage.Info[]) =>
402+
compaction.compact({ reason: "auto", context: context(messages) }).pipe(Effect.map((outcome) => outcome.status))
403+
404+
// 600k free tokens on a 100k window is treated as unset, so the ceiling is the default 16k reserve
405+
// below the window.
406+
expect(yield* status([assistant(83_999)])).toBe("skipped")
407+
expect(yield* status([assistant(84_000)])).toBe("completed")
408+
// The opening prompt alone has no reply to summarize; it must not compact or surface a failure.
409+
expect(yield* status([user("x".repeat(400_000))])).toBe("skipped")
410+
}),
411+
)
412+
413+
// Provider-switch regression suggested by @intrepid37 on the PR.
414+
it.effect("auto compaction stays model-aware when switching between differently sized providers", () =>
415+
Effect.gen(function* () {
416+
const compaction = yield* SessionCompaction.Service
417+
const session = yield* insertSession(Session.ID.make("ses_synthetic_provider_switch"))
418+
// A user may configure this for a large model, then change providers mid-thread.
419+
yield* compaction.transform((editor) => editor.configure({ buffer: 600_000 }))
420+
421+
const models = {
422+
large: LanguageModel.make({ id: "synthetic-large", provider: "synthetic-large", route: OpenAIChat.route }),
423+
small: LanguageModel.make({ id: "synthetic-small", provider: "synthetic-small", route: OpenAIChat.route }),
424+
}
425+
const assistant = (provider: "large" | "small", inputTokens: number) =>
426+
Schema.decodeUnknownSync(SessionMessage.Assistant)({
427+
id: SessionMessage.ID.create(),
428+
type: "assistant",
429+
agent: Agent.defaultID,
430+
model: { id: models[provider].id, providerID: models[provider].provider },
431+
content: [{ type: "text", text: "Synthetic response" }],
432+
tokens: { input: inputTokens, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
433+
time: { created: 0, completed: 0 },
434+
})
435+
const user = (text: string) =>
436+
SessionMessage.User.make({
437+
id: SessionMessage.ID.create(),
438+
type: "user",
439+
text,
440+
time: { created: DateTime.makeUnsafe(0) },
441+
})
442+
const status = (provider: "large" | "small", messages: readonly SessionMessage.Info[]) =>
443+
compaction
444+
.compact({
445+
reason: "auto",
446+
context: {
447+
...loaded(session, messages),
448+
model: SessionRunnerModel.resolved(models[provider], {
449+
capabilities: { tools: true, input: ["text"], output: ["text"] },
450+
cost: [],
451+
limit: { context: provider === "large" ? 1_000_000 : 200_000, output: 32_000 },
452+
}),
453+
},
454+
})
455+
.pipe(Effect.map((outcome) => outcome.status))
456+
457+
const big = assistant("large", 250_000)
458+
const start = [user("Start synthetic task"), big]
459+
expect(yield* status("large", start)).toBe("skipped") // 1M - 600k remains usable.
460+
461+
const switched = [...start, user("Switch to smaller provider")]
462+
expect(yield* status("small", switched)).toBe("skipped") // Ignore large provider's token count.
463+
const small = [...switched, assistant("small", 25_000)]
464+
expect(yield* status("small", small)).toBe("skipped") // Oversized buffer falls back to default.
465+
466+
expect(yield* status("large", [...small, user("Switch back")])).toBe("skipped")
467+
468+
// Real pressure on the smaller provider still triggers normal auto-compaction (200k - 30k reserve).
469+
expect(yield* status("small", [...switched, assistant("small", 180_000)])).toBe("completed")
470+
}),
471+
)
472+
365473
/** Seeds the global project plus one session row, returning the projected session. */
366474
const insertSession = (id: Session.ID, overrides?: Partial<typeof SessionTable.$inferInsert>) =>
367475
Effect.gen(function* () {

0 commit comments

Comments
 (0)