fix(compact-basic): fence surface changes only

This commit is contained in:
Tianyi Cui
2026-07-21 16:34:28 +08:00
parent 6475e51825
commit 5261ebab38
11 changed files with 39 additions and 17 deletions

View File

@@ -4,6 +4,7 @@
* @module @deepseek-ai/dsh-compact-basic/region
*/
import { isDeepStrictEqual } from 'node:util'
import {
renderTranscript,
toolPairingBalancedAfter,
@@ -113,8 +114,8 @@ export async function compactSurfaceRegion(
const shadowedSeqs = nodes.slice(startIdx, endIdx + 1)
const startEvent = session.append('compact/start', { turn: tail.turn })
try {
// Capture after the lock event so any later durable append, including a
// log-only one, invalidates the async selection before replacement.
// Capture after the lock event so a later surface mutation invalidates the
// async selection before replacement. Unrelated log-only facts may append.
const lockedMeasurement = dependencies.meter.measure(session)
const selected = lockedMeasurement.nodes.slice(startIdx, endIdx + 1)
if (selected.length !== shadowedSeqs.length
@@ -126,8 +127,8 @@ export async function compactSurfaceRegion(
const { summary, provider, model, maxTokens } = await dependencies.summarize(text, agent, signal)
const currentMeasurement = dependencies.meter.measure(session)
if (currentMeasurement.logRevision !== lockedMeasurement.logRevision) {
throw new Error('compaction: session log changed during summarization')
if (!isDeepStrictEqual(currentMeasurement.nodes, lockedMeasurement.nodes)) {
throw new Error('compaction: session surface changed during summarization')
}
const framedSummary = frameSummary(summary)
const framedSummaryTokenCount = dependencies.meter.estimateMessage({