fix(sidechat): drop grouped records atomically and honor the host's optional removedCount (round 5)

The round-5 delta review and the real-data re-run exposed two v5
regressions that disabled the optimization exactly where it matters.

First, the new bracket gate fell back on real multi-group sessions: a
mid-keep compaction group's summary cites nodes a later group
re-shadowed, so record-level dropping stranded its successful
compaction/end and the gate rejected the whole tail — the deployment's
main session forked at 9,077 ms with seed 35,993 (the full copy). The
fix is the reviewers' other option, now data-backed: decide the
emitted-seq set BEFORE emission and drop unmatched groups ATOMICALLY —
a compaction bracket goes as a whole, a command/done losing its run
goes alone. The fuzz suite recovers from 108 to 129 clean trims per
3000 cases (every one passing the real session, compaction, and
commands invariants plus the real inbox projection), and the round-4
cascade counterexample flips from fallback to a clean trim.

Second, v5 treated a missing removedCount as invalid, but the host
contract makes it optional and defaults it to 0 — the host's own writer
omits it for pure inserts, 54% of production splices are like that,
and 38 of 89 real compaction sessions (including the e2e main session)
fell back purely on this. Restored the ?? 0 default while keeping the
rest of the host-fold strictness.

The round-4 spec case is rewritten against the host's chained-replace
semantics (the consumed group swallows the surface span as it exists
at ITS fold moment), which turned it from an empty test — the fixture
could not fold at all, so it pinned nothing — into a real pin: the
atomic drop trims with the earlier group's records gone entirely.
Final second-instance e2e on the real main session: 953 ms, seed
3,457 (10.4x), and a cold-context answer that only the compaction
summaries could contain.
This commit is contained in:
NolanHo authored and Atlas Zeng committed 2026-10-16 16:14:41 +08:00
1 parent a106e1964e
commit f320cdee83
2 files changed
+94 -19

No files matched your search

+64 -5
View File
@@ -374,6 +374,60 @@ function compactionTail(events: readonly SidechatLogEvent[]): SidechatLogEvent[]
for (let seq = Math.max(0, start); seq <= Math.min(end, events.length - 1); seq++) replaceCovered.add(seq)
}
}
// The set of parent seqs the seed will carry, decided BEFORE emission: a
// grouped record whose citations cannot remap drops ATOMICALLY — its
// compaction bracket (start/summary/end) or its command pair goes
// together, never a lone partner that the host's invariant companions
// reject. Real multi-compaction sessions hit this on every mid-keep group
// (its summary cites nodes later groups re-shadowed), so record-level
// dropping would strand brackets on every real parent.
const emittedSeqs = new Set<number>()
for (const event of headEvents) emittedSeqs.add(event.seq)
emittedSeqs.add(checkpoint.seq)
const keepSkip = (event: SidechatLogEvent): boolean =>
(isSurfaceEligibleType(event.type) && !surviving.has(event.seq) && !replaceCovered.has(event.seq))
|| (event.type === 'tool/call' && !survivingCallIds.has(String(dataOf(event).callId)))
for (const [from, to] of [[keepStart, cut], [cut + 1, events.length]] as const) {
for (let index = from; index < to; index++) {
const event = events[index]
if (event === undefined) continue
if (isCompactionGroupEvent(event, groupId)) continue
if (index < cut && keepSkip(event)) continue
emittedSeqs.add(event.seq)
}
}
const droppedGroups = new Set<string>()
const keptRunIds = new Set<string>()
for (const seq of emittedSeqs) {
const event = events[seq]
if (event === undefined || event.type !== 'command/run') continue
const id = dataOf(event).commandId
if (typeof id === 'string') keptRunIds.add(id)
}
const droppedDones = new Set<number>()
for (const seq of emittedSeqs) {
const event = events[seq]
if (event === undefined) continue
const data = dataOf(event)
if (event.type === 'compaction/summary' || event.type === 'compaction/prune') {
const range = data.shadowedRange as { start?: unknown; end?: unknown } | undefined
const cited = data.shadowedSeqs
if (range === null || typeof range !== 'object' || !Array.isArray(cited)) continue
const start = typeof range.start === 'number' ? emittedSeqs.has(range.start) : true
const end = typeof range.end === 'number' ? emittedSeqs.has(range.end) : true
const citedAll = cited.every(item => typeof item !== 'number' || emittedSeqs.has(item))
if (!start || !end || !citedAll) {
const id = data.compactionId
if (typeof id === 'string') droppedGroups.add(id)
}
} else if (event.type === 'command/done') {
const id = data.commandId
const runSeq = data.sourceEventSeq
const runKept = (typeof id === 'string' && keptRunIds.has(id))
|| (typeof runSeq === 'number' && emittedSeqs.has(runSeq) && events[runSeq]?.type === 'command/run')
if (!runKept) droppedDones.add(event.seq)
}
}
// Turn renumbering: the host's turn counter must run 1, 2, 3… over the
// seed, but the dropped span leaves a gap between the head skeleton's
// turn and the keep window's first turn/start. Every kept event carrying
@@ -421,13 +475,17 @@ function compactionTail(events: readonly SidechatLogEvent[]): SidechatLogEvent[]
for (let index = keepStart; index < cut; index++) {
const event = events[index]
if (event === undefined || isCompactionGroupEvent(event, groupId)) continue
if (isSurfaceEligibleType(event.type) && !surviving.has(event.seq) && !replaceCovered.has(event.seq)) continue
if (event.type === 'tool/call' && !survivingCallIds.has(String(dataOf(event).callId))) continue
if (keepSkip(event)) continue
if (droppedGroups.has(String(dataOf(event).compactionId))) continue
if (droppedDones.has(event.seq)) continue
emit(event)
}
for (let index = cut + 1; index < events.length; index++) {
const event = events[index]
if (event !== undefined && !isCompactionGroupEvent(event, groupId)) emit(event)
if (event === undefined || isCompactionGroupEvent(event, groupId)) continue
if (droppedGroups.has(String(dataOf(event).compactionId))) continue
if (droppedDones.has(event.seq)) continue
emit(event)
}
if (aborted) return undefined
// Prove equivalence before the caller relies on it: the rebased sequence
@@ -496,8 +554,9 @@ function inboxSplicesValid(seed: readonly SeedEvent[]): boolean {
if (data.target !== 'next-turn' && data.target !== 'next-step') return false
const inbox = data.target === 'next-step' ? nextStep : nextTurn
const rawStart = data.start
const rawRemoved = data.removedCount
const removedCount = typeof rawRemoved === 'number' ? rawRemoved : Number.NaN
// The host contract makes removedCount optional and defaults it to 0
// (its own writer omits it for pure inserts — 54% of real splices).
const removedCount = typeof data.removedCount === 'number' ? data.removedCount : 0
const inserted = Array.isArray(data.inserted) ? data.inserted as Array<Record<string, unknown>> : undefined
const start = typeof rawStart === 'number' ? rawStart : Number.NaN
if (inserted === undefined