Skip to content

[PR] fix(stream): compress that folds nothing no longer sets the ledger alignment marker | fix(stream): 什么都没折叠的 compress 不再置上账本对齐标记 - #6331

Closed
BBleae wants to merge 1 commit into
ranxianglei:masterfrom
BBleae:2026-11-12_ledger-align-only-after-fold
Closed

BBleae wants to merge 1 commit into
ranxianglei:masterfrom
BBleae:2026-11-12_ledger-align-only-after-fold

Conversation

@BBleae

@BBleae BBleae commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #6330

接着 #5608-C/D(PR #5609)和 #5797 做的。只动宿主侧,一个提交:改 src/stream.ts,另加一个测试文件和一行 CHANGELOG。kernel/** 下面什么都没动。

master(c26c31ace)上发生了什么

applyRanges 在提交 kernel 返回的状态之后马上置 session.pendingLedgerAlign = true,这在后面的 r.blocksCreated === 0 分支之前。所以在那里被拒绝的 compress(gate:too-small、gate:cannot-anchor、gate:already-compressed、gate:none-resolved、gate:summary-length、gate:other)和 no-op(#4521)也会给一次性的 #4316 对齐置上标记。

下一次存盘消费这个标记,跑 restampRefsToLedger。被拒绝时,这次存盘就是它自己的失败记账安排的那一次(recordCompressFailure → markDirty → scheduleSave),所以 applyRanges 返回时对齐已经跑过了。no-op 时是会话的下一次存盘。

于是对齐跑在一次什么都没折叠的调用上:

不管哪种,在一次回执报失败或 no-op 的调用之后,出站字节都从第一条带标签的消息起变了,这些消息的 time= 属性也不会回来。

用新测试的夹具测的(9 轮带标签、开着时间戳、行在统一存储里;调一次 applyRanges、存一次盘,再对同一段历史跑第二次 processTurn)。「偏离」那几行是这个夹具的一个本地变体:第三条消息受保护,于是它占一个存储行、不占引用号;这个变体不在提交的测试里。

会话 compress 调用 对齐 引用 变了的带标签消息 带 time= 的标签
引用在账本位置上 被拒绝(gate:too-small) master kept=9 restamped=0 rehomed=0 不变 9 / 9 9 → 0
本 PR 没跑 不变 0 / 9 9 → 9
引用在账本位置上 no-op(保护区内) master kept=9 restamped=0 rehomed=0 不变 9 / 9 9 → 0
本 PR 没跑 不变 0 / 9 9 → 9
偏离 被拒绝(gate:too-small) master kept=2 restamped=6 rehomed=0 blocked=1 m00003–m00008 → m00004–m00009 8 / 8 8 → 0
本 PR 没跑 不变 0 / 8 8 → 8

在 master 上第一个标签从 <acp tokens="752" type="text" time="…">m00001</acp> 变成 <acp tokens="752" type="text">m00001</acp>。

这和 src/persist.ts 里 consumePendingLedgerAlign 的注释("No fold ⇒ marker never set ⇒ pure legacy no-op")相矛盾。

已发布的 0.1.213 不受影响:它在冷加载时对齐,没有延后的标记。这个标记是 PR #5609 带进来的。

改动

src/stream.ts:标记改在 r.blocksCreated === 0 的那几处返回之后再置,和其他只在成功时做的记账(clearCompressFailures、clearCompressLoopStreak)放在一起。被拒绝或 no-op 的 compress 不再置这个标记。

创建了块的 compress 照旧置上。在旧位置和新位置之间,这条路径上没有任何东西会安排存盘(scrubTurnSeparatorIds、块内容缓存和 CCR adopt 都只设字段和脏标记),所以折叠后的第一次存盘仍然能看到标记。

在提交之前就返回的拒绝(参数解析失败、gate:no-visible-context、gate:coverage-regression、gate:no-net-shrink 等)本来就不置标记,不变。

只有 applyRanges 置这个标记,所以代理模式和插件模式的变化一样。

开着的 PR #6155 把它的折叠钩子插在状态提交和本 PR 挪走的那段标记代码之间。两边的 diff 在那里碰到相邻的行,所以后合的一方要解一处小的文本冲突(只是标记那段挪到了更下面)。

这个 PR 不改的

搭着真折叠跑的对齐不动,包括它抬高水位线的那一步。同一个夹具,把中间四轮真的折叠掉,在 master 上和本 PR 上表现一样:对齐跑一次(引用都在账本位置上的会话是 kept=9 restamped=0 rehomed=0),折叠范围外的那几轮丢掉 time=,虽然没有任何引用被挪动。这次抬高发生在 restampRefsToLedger(kernel/src/refs.ts)里,所以我把它另外提成一个 kernel 授权申请,这里不碰 kernel/**。

测试

tests/compress-refused-ledger-align.test.ts(新文件,3 个测试):

  • 被拒绝的 compress(gate:too-small):对齐不跑,下一次渲染逐字节相同;
  • no-op 的 compress(范围在保护区里):标记不置,下一次渲染逐字节相同;
  • 真折叠仍然置上标记,下一次存盘消费它。

用 master 的 src/stream.ts 时前两个失败、第三个通过;改后三个都通过。

本地跑的(Windows、Node 22、断网、一次性的家目录)。每个文件单独跑,--test-concurrency=1:

  • 76 个测试文件、653 个测试:650 通过、0 失败、3 跳过(issue2204-decompress-to-file-outcome 里两个只在 Windows 跳过的路径用例,storage-unified 里一个要可选依赖 better-sqlite3 的用例)。这一组是所有引用 applyRanges 的测试文件(58 个,含新文件)再加 18 个:其他会走到对齐的文件(seq-ledger-pr3、storage-orphan-recovery、storage-unified)、其余的 compress-* 和 stream-* 文件、其他时间戳相关文件(message-timestamps-default-on、resume-miss-timestamp-seed、issue404-restore-timestamps、attach-invalid-timestamps)、plugin-fork、side-request-isolation、issue4986-digest-drift-ref-order、golden-kernel-dist-guard、golden-storage-render 和 storage-engine-proofs。
  • tsc --noEmit、check:unused-exports、check:test-import-graph:干净。

本地没跑的:完整的主套件(tests/*.test.ts)、kernel 和 pi-subagents 套件(它们的文件都没变)、e2e 车道。完整套件的结论请以 CI 各车道为准。

applyRanges set session.pendingLedgerAlign right after committing the
kernel's returned state, before the blocksCreated === 0 returns. A refused
compress (range too small, cannot anchor, already compressed) and the no-op
class armed the one-time ranxianglei#4316 alignment too, so the next save ran it after
a call that folded nothing.

Set the marker past those returns. A compress that created a block arms it
exactly as before (ranxianglei#5608-D).

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request due to rate limiting. Please try again later. See Usage limits for GitHub Copilot for details.

@ranxianglei ranxianglei changed the title fix(stream): 什么都没折叠的 compress 不再置上账本对齐标记 [PR] fix(stream): compress that folds nothing no longer sets the ledger alignment marker | fix(stream): 什么都没折叠的 compress 不再置上账本对齐标记 Nov 12, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

Landed via batch PR #6455 (merged) — closing as superseded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants