From 37e0354d152384812d7c810b160ecad942fdfd35 Mon Sep 17 00:00:00 2001 From: NolanHo Date: Thu, 15 Oct 2026 01:45:35 -0700 Subject: [PATCH] fix(sidechat): drop a command/done whose citation fell in the dropped span (round 5, F2) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The round-5 final report pinned a real bad-seed path on cc11ce4 and verified my in-flight atomic drops did not cover it: a command/done whose run SURVIVES while its numeric sourceEventSeq cites an earlier NON-command event in the dropped span kept the parent seq — a stale citation the host's commands invariant rejects (the contract: the citation names an earlier non-command event, never the run). Their real-data oracle found three such seeds among the forty genuinely trimming production sessions. The pre-decision now drops the done when either its run's commandId or its cited source seq is not in the emitted set (the run staying behind is legal — an open command), and rebaseEvent's runtime belt returns DROPPED for an unmappable citation instead of leaving the parent value. The spec's both-kept fixture is rewritten against the host contract: unique commandId per run and a citation pointing at the user message, asserting the remapped citation names a non-command event. A mutation that removes the droppedGroups filter reddens the mid-keep-group case, so the atomic-drop chain and the bracket gate now have an in-repo pin. --- ...026-10-13-sidechat-compaction-tail-seed.md | 2 +- src/sidechat-core.ts | 19 +++++++++++++------ tests/sidechat-compaction-seed.spec.ts | 17 ++++++++++------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/docs/plans/2026-10-13-sidechat-compaction-tail-seed.md b/docs/plans/2026-10-13-sidechat-compaction-tail-seed.md index f6f98e58..fc86b3de 100644 --- a/docs/plans/2026-10-13-sidechat-compaction-tail-seed.md +++ b/docs/plans/2026-10-13-sidechat-compaction-tail-seed.md @@ -97,7 +97,7 @@ DSH 的 compaction 机制(`dsh-compaction` / `compaction-basic`): 三审(同日,两位全新 fresh reviewer):两票 **CHANGES_REQUIRED**——(a) 头部骨架 step 号未归一(受保护头被改写到 turn 1 step ≥2 时产出宿主 invariant 拒绝的种子);(b) 宽头存活 `tool/result` 丢配对 `tool/call`;(c) keep/尾保留的 compaction 计量记录 `shadowedRange/shadowedSeqs` 未重映射(违反 dsh-compaction invariant);(d) 关系门对**非 append** `tool/result` 过严(pruner 替换会让 trim 无谓回退);(e) lint 死赋值。→ v4:头部归一 turn 1 step 1(只改写已有数字信封字段——期间发现并修复了「向 user/message 注入 turn/step」的真等价破坏,token 改为镜像宿主投影:user=整份 data、其余=data.message)、关系门补 nextStep/pendingCalls/嵌套开轮/非 append 豁免、compaction 记录引用重映射或整条丢弃、**replace 目标节点不跳过**(pruner 形状由此从回退转为生效)。 -五审(delta 终验 + 真实数据复测,对象 cc11ce4 并对工作树并发改动钉版复核):抓到 v5 两处回归并当场修复。(a) bracket 门在真实多组会话上**全量回退**——e2e 实测主会话 seed=35,993、9,077 ms(全量拷贝旧路径);根因是真实多组会话的 mid-keep 组 summary 引用全指向被后续组 re-shadow 的节点,记录级丢弃拆散成组记录。→ 修法取审查者给的第二选项并经数据背书:**成组原子丢弃**(发射前定 emittedSeqs,引用失配的 compaction 组 start/summary/end 整组丢、失 run 的 command/done 单独丢);fuzz 3000 例 trim 108→129 全过真 session+compaction+commands invariant 与真 inbox 折叠、0 命中,四审级联反例 pG 从回退翻转为干净 trim。(b) v5 把缺失 `removedCount` 当非法——宿主契约它**可选、缺省 0**(宿主写入方对纯插入省略它;生产库 54% splice 如此;89 个真实 compaction 会话 38 个因此回退,含主会话)→ 还原 `?? 0`。五审实证 v5 在 89 个真实会话 trim 41→3 的覆盖率崩塌即此二回归合成。用例 15 夹具按宿主链式 replace 语义重写(吞**当时** surface span),从空测(夹具本身折不动、断言恒真)翻转为真钉子。终版 e2e(ad42628):**953 ms / seed 3,457(10.4×)**、冷上下文回答再命中 exp5 收尾细节(fsr-ge7-top5-5e5-allfix、1.02×→1.18× 分叉消失、2900x 收尾三件事)。 +五审(delta 终验 + 真实数据复测,对象 cc11ce4 并对工作树并发改动钉版复核):抓到 v5 两处回归并当场修复。(a) bracket 门在真实多组会话上**全量回退**——e2e 实测主会话 seed=35,993、9,077 ms(全量拷贝旧路径);根因是真实多组会话的 mid-keep 组 summary 引用全指向被后续组 re-shadow 的节点,记录级丢弃拆散成组记录。→ 修法取审查者给的第二选项并经数据背书:**成组原子丢弃**(发射前定 emittedSeqs,引用失配的 compaction 组 start/summary/end 整组丢、失 run 的 command/done 单独丢);fuzz 3000 例 trim 108→129 全过真 session+compaction+commands invariant 与真 inbox 折叠、0 命中,四审级联反例 pG 从回退翻转为干净 trim。(b) v5 把缺失 `removedCount` 当非法——宿主契约它**可选、缺省 0**(宿主写入方对纯插入省略它;生产库 54% splice 如此;89 个真实 compaction 会话 38 个因此回退,含主会话)→ 还原 `?? 0`。五审实证 v5 在 89 个真实会话 trim 41→3 的覆盖率崩塌即此二回归合成。用例 15 夹具按宿主链式 replace 语义重写(吞**当时** surface span),从空测(夹具本身折不动、断言恒真)翻转为真钉子。终版 e2e(ad42628):**953 ms / seed 3,457(10.4×)**、冷上下文回答再命中 exp5 收尾细节(fsr-ge7-top5-5e5-allfix、1.02×→1.18× 分叉消失、2900x 收尾三件事)。五审终稿另定级 F2(blocking,两审一致):`command/done.sourceEventSeq` 映射不到时留父 seq——宿主契约它指向**更早的非命令事件**(commands invariant 拒陈旧引用),且真实数据 40 个 trim 中 3 个命中 → 修复:done 引用的数值 sourceSeq 落在被遗忘区段时**随 run 一起原子丢弃**(运行时兜底同样 DROPPED),`pM` 探针由「trim + 违反 commands invariant」翻转为「trim + 无陈旧引用」;用例 16 后半夹具按契约重写(唯一 commandId、sourceEventSeq 指向 user 消息)。变异 M6(去掉 droppedGroups 过滤)→ 用例 15 当场红,证明原子丢弃链与 bracket 门在仓内套件有钉子。 四审(同日,两位全新 fresh reviewer,对象 657dcd5 → tip 196fa11 两版钉证):核心等价性**未能证伪**(2100 生成形状、113 次 trim 全过真 session invariant + 真 Session.create + 真持久 inbox 投影 + 真 `deriveEventMessage` wire 比对;非 trim 全与 legacy 逐字节一致),但两票各在同一根因上给 blocking——**宿主「成组记录」被只丢计量记录拆散**:(a) 成功 `compaction/end` 失去同组 `compaction/summary`(同轮双 compaction 形状,随机实测命中 trim 的 18.6%,真 dsh-compaction invariant 拒绝而三道自证全盲);(b) 657dcd5 的 keep 窗口丢掉紧邻 turn/start 前的 inbox INSERT、保留其后的 CLAIM(真实持久 inbox 投影拒绝——**v4 e2e 在生产主会话上当场抓到**:`thread creation failed: invalid persisted inbox splice at session seq 7`)。→ v5:keepStart 回吞前置 splice + `inboxSplicesValid` 门(e2e 复测:**799 ms**、seed 3,457 = 35,992 的 **10.4×**、冷上下文回答再命中压缩摘要细节);`bracketsValid` 门(成组记录原子性:compaction bracket + command 配对);`command/done.sourceEventSeq` 重映射;spec oracle 换宿主 `deriveEventMessage`;补同轮双 compaction 与命令括号两用例(17 例)。Standards 另修:`expectedFirstKeptTurn` 按骨架实际 turn/start 判定(N2)、`inboxSplicesValid` 收紧到宿主严格度(N4)、post-cut 改写头回退形状入披露(S3,六会话三命中)。四轮审查者的全部树外证据脚本在 v5 上全部转绿或按设计安全回退。 diff --git a/src/sidechat-core.ts b/src/sidechat-core.ts index f2bb9e9b..e2b71397 100644 --- a/src/sidechat-core.ts +++ b/src/sidechat-core.ts @@ -422,10 +422,14 @@ function compactionTail(events: readonly SidechatLogEvent[]): SidechatLogEvent[] } } 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) + const sourceSeq = data.sourceEventSeq + // The host contract: sourceEventSeq cites an earlier NON-command + // event, and the commands invariant rejects a stale or out-of-range + // citation. A done whose run or whose cited source falls in the + // dropped span drops ATOMICALLY with it. + const runKept = typeof id === 'string' && keptRunIds.has(id) + const sourceKept = typeof sourceSeq !== 'number' || emittedSeqs.has(sourceSeq) + if (!runKept || !sourceKept) droppedDones.add(event.seq) } } // Turn renumbering: the host's turn counter must run 1, 2, 3… over the @@ -843,10 +847,13 @@ function rebaseEvent( && firstKeptTurn !== undefined && sourceData.turn >= firstKeptTurn) { seed.data = { ...seed.data, turn: sourceData.turn - turnOffset } } - // A settled command cites the seq of the run it settled; remap it alongside. + // A settled command cites an earlier non-command event by seq; remap it + // alongside, and drop the done when the cited event did not survive — a + // stale citation is something the host's commands invariant rejects. if (event.type === 'command/done' && typeof sourceData.sourceEventSeq === 'number') { const mapped = rebase.get(sourceData.sourceEventSeq) - if (mapped !== undefined) seed.data = { ...seed.data, sourceEventSeq: mapped } + if (mapped === undefined) return DROPPED + seed.data = { ...seed.data, sourceEventSeq: mapped } } // Compaction metering records cite the surface span they priced. When the // span survived the rebase, remap the citations; when it did not (the diff --git a/tests/sidechat-compaction-seed.spec.ts b/tests/sidechat-compaction-seed.spec.ts index 0ef434a6..3e1fa8b5 100644 --- a/tests/sidechat-compaction-seed.spec.ts +++ b/tests/sidechat-compaction-seed.spec.ts @@ -735,14 +735,16 @@ describe('buildSidechatInheritance compaction tail', () => { expect(invariantFailures(seed)).toEqual([]) // Together: a command that runs INSIDE the keep window and settles - // there — both records ride, and the done cites the rebased run. + // there — both records ride, and the done's citation remaps onto the + // rebased event it names. The host contract: sourceEventSeq cites an + // earlier NON-command event, and commandIds are unique per run. const together = [ ...base, lo('turn/start', 8, { turn: 2 }), lo('step/start', 9, { turn: 2, step: 1 }), sf('user/message', 10, userMessage('m-q2', 'q2')), - lo('command/run', 11, { commandId: 'cmd1', name: 'compact', source: { kind: 'user' } }), - lo('command/done', 12, { commandId: 'cmd1', kind: 'success', text: 'ok', sourceEventSeq: 11 }), + lo('command/run', 11, { commandId: 'cmd2', name: 'compact', source: { kind: 'user' } }), + lo('command/done', 12, { commandId: 'cmd2', kind: 'success', text: 'ok', sourceEventSeq: 10 }), sf('assistant/message', 13, { turn: 2, step: 1, message: assistantMessage('a2'), stream: [] }), lo('step/end', 14, { turn: 2, step: 1 }), lo('turn/end', 15, { turn: 2, reason: { kind: 'completed' } }), @@ -751,10 +753,11 @@ describe('buildSidechatInheritance compaction tail', () => { together.push(...compactionGroup(together.length, [4, 5])) const trimmed = buildSidechatInheritance(together) expect(trimmed.seed.length).toBeLessThan(together.length) - const done = trimmed.seed.find(event => event.type === 'command/done') as { data?: { sourceEventSeq?: number }; seq?: number } - const runIdx = trimmed.seed.findIndex(event => event.type === 'command/run') - expect(runIdx).toBeGreaterThanOrEqual(0) - expect(done?.data?.sourceEventSeq).toBe(runIdx) + const done = trimmed.seed.find(event => event.type === 'command/done') as { data?: { sourceEventSeq?: number } } + const citedIdx = trimmed.seed.findIndex((event, index) => index === (done?.data?.sourceEventSeq ?? -1)) + expect(citedIdx).toBeGreaterThanOrEqual(0) + // The citation names a non-command event (the user message), not the run. + expect(trimmed.seed[citedIdx]?.type).toBe('user/message') expect(visibleSequence(trimmed.seed)).toEqual(visibleSequence(together)) expect(invariantFailures(trimmed.seed)).toEqual([]) })