fix(compaction): retry oversized summaries with smaller prefixes

This commit is contained in:
Shuhan Lin committed 2026-10-22 22:43:32 +08:00
1 parent d10f417f13
commit cf7229e72a
17 files changed
+698 -23

No files matched your search

@@ -304,7 +304,7 @@ export class BasicCompactionEngine extends CompactionEngine {
const measurement = this.ctx.tokenMeter.measure(agent.session)
const range = selectCompactableRange(agent.session, measurement, 0)
if (range === null) return null
return this.compactRegion(range.start, range.end, agent, signal)
return this.compactAutomaticRegion(range.start, range.end, agent, signal)
}
/* v8 ignore next -- closed-union exhaustiveness guard */
default:
@@ -359,7 +359,7 @@ export class BasicCompactionEngine extends CompactionEngine {
+ `(${measurement.totalTokens} estimated tokens >= threshold ${spec.thresholdTokens})`,
)
}
result = await this.compactRegion(range.start, range.end, agent, signal)
result = await this.compactAutomaticRegion(range.start, range.end, agent, signal)
measurement = meter.measure(agent.session, header)
if (measurement.totalTokens < spec.thresholdTokens) return result
}
@@ -370,8 +370,26 @@ export class BasicCompactionEngine extends CompactionEngine {
)
}
/** Compact an automatically selected prefix, allowing smaller balanced prefixes on summary overflow. */
private compactAutomaticRegion(
start: SessionSeq,
end: SessionSeq,
agent: Agent,
signal: AbortSignal,
): Promise<CompactionResult> {
return compactSurfaceRegion(
this.regionDependencies(),
agent.session,
start,
end,
agent,
{ owner: 'current-turn', stability: 'whole-surface', allowSmallerPrefix: true },
signal,
)
}
/**
* Compact one inclusive positional range from the agent-owned surface using
* Compact one exact inclusive positional range from the agent-owned surface using
* the effective token meter for all retention and shrink pricing.
* @param start - inclusive first surface-node seq.
* @param end - inclusive last surface-node seq.
@@ -430,6 +448,7 @@ export class BasicCompactionEngine extends CompactionEngine {
{
owner: null,
stability: 'selected-span',
allowSmallerPrefix: true,
...sourceCommandId === undefined ? {} : { sourceCommandId },
flush: async () => {
await this.ctx.sessions.flush(agent.session)
@@ -16,7 +16,7 @@ import {
} from '@deepseek-ai/dsh-compaction'
import type { CompactionResult } from '@deepseek-ai/dsh-compaction'
import type { CommandId } from '@deepseek-ai/dsh-commands/brand'
import { createUserMessage, errorChain } from '@deepseek-ai/dsh-llm'
import { CONTEXT_WINDOW_EXCEEDED_CODE, createUserMessage, errorChain, LlmError } from '@deepseek-ai/dsh-llm'
import type { Message, UserMessage } from '@deepseek-ai/dsh-llm'
import type { TokenMeasurement, TokenMeter } from '@deepseek-ai/dsh-token-meter'
import { SessionSeq, type Session, type SessionEvent } from '@deepseek-ai/dsh-session'
@@ -61,6 +61,8 @@ interface CompactionTransactionOptions {
readonly flush?: () => Promise<void>
/** Manual command that initiated this transaction, when present. */
readonly sourceCommandId?: CommandId
/** Allow a self-selected range to shrink after summary overflow; absent keeps the exact range. */
readonly allowSmallerPrefix?: true
}
interface CompactionEntryState {
@@ -227,6 +229,7 @@ export async function compactSurfaceRegion(
compactionId,
options.sourceCommandId,
assertStable,
options.allowSmallerPrefix === true,
signal,
)
if (options.owner === null) signal?.throwIfAborted()
@@ -391,6 +394,7 @@ async function summarizeCompaction(
compactionId: CompactionResult['compactionId'],
sourceCommandId: CommandId | undefined,
assertStable: StabilityCheck,
allowSmallerPrefix: boolean,
signal?: AbortSignal,
): Promise<SummarizedCompaction> {
let summaryResult: SummaryResult
@@ -402,9 +406,16 @@ async function summarizeCompaction(
} catch (error: unknown) {
if (signal?.aborted === true) throw error
assertStable(dependencies, agent.session, prepared)
if (!dependencies.recover(error, agent, prepared.shadowedSeqs, signal)) throw error
prepared = prepareCompaction(dependencies, agent.session,
validateSurfaceRegion(agent.session, prepared.start, prepared.end))
let selection: SurfaceSelection
if (dependencies.recover(error, agent, prepared.shadowedSeqs, signal)) {
selection = validateSurfaceRegion(agent.session, prepared.start, prepared.end)
} else {
if (!allowSmallerPrefix || !(error instanceof LlmError) || error.code !== CONTEXT_WINDOW_EXCEEDED_CODE) throw error
const smaller = smallerBalancedPrefix(agent.session, prepared)
if (smaller === null) throw error
selection = smaller
}
prepared = prepareCompaction(dependencies, agent.session, selection)
}
}
const checkpointMessage = createUserMessage({
@@ -427,6 +438,16 @@ async function summarizeCompaction(
}
}
/** Halve complete tool-balanced units; one remaining unit cannot be shortened safely. */
function smallerBalancedPrefix(session: Session, selection: SurfaceSelection): SurfaceSelection | null {
const ends = selection.shadowedSeqs.filter(seq => toolPairingBalancedAfter(session, seq))
if (ends.length < 2) return null
// A validated span ends balanced; two or more units leave a nonempty proper prefix.
// oxlint-disable-next-line typescript/no-non-null-assertion
const end = ends[Math.ceil(ends.length / 2) - 1]!
return validateSurfaceRegion(session, selection.start, end)
}
/** Reject a summary prepared against any earlier surface generation. */
function assertWholeSurfaceUnchanged(
dependencies: RegionDependencies,