Repository navigation
fix: plugin fork snapshot O(1) steady-state maintenance + lazy persistence (#2077) - #2088
Conversation
…ist it lazily (#2077) rememberPluginMessages re-cloned and re-serialized the full raw history on every plugin request (structuredClone + stableJson, two O(history) walks), and persist.ts wrote the snapshot into every session record even for sessions that never fork. - copy-on-write snapshot: full-view prefix scan (sameSnapshotView — the exact field set forkMessageIdentityHash digests plus id; ids alone are not enough because adapters attach toolIsError outside the CoreMessage interface); pure extensions clone only the tail and extend an exactly-tracked byte count instead of re-cloning + re-serializing everything - cap enforced from the tracked count on every request (knob shrink still drops the snapshot without history change); legacy records gain their byte count with one-time remeasure - identical-view resends skip clone/serialize/refs/markDirty entirely, keeping the revision cache warm for status polls - wire comparable verdict short-circuited by a native sha256 of the body - lazy persistence: buildRecord writes pluginSnapshot only once it becomes an external contract (fork receipt present, or sticky publicSnapshotRetained set on the parent at fork time, flushed eagerly like publishForkSession); non-forking sessions pay no disk cost and self-heal on the next model request because plugin agents resend full history - stored-refs list re-evaluates when the snapshot changed or the content-store object identity changed (store updates are immutable), skipping the walk otherwise Tests: 5 new regression tests in tests/plugin-fork.test.ts (in-place extension + exact byte accounting, legacy remeasure + O(1) no-op + cap re-evaluation, lazy persistence contract, replacement path, restart self-heal); corruption-loop test pins publicSnapshotRetained so it still exercises the corrupted-store detection path.
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.2125-pr.2088.a45f8cfThis exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr2088.tgz
npm install -g packageInstall instructions are refreshed on each push. |
The old wording claimed len(sj([a, b])) = len(sj(a)) + len(sj(b)) + 1, which is off by two for whole-array serialization (brackets + comma). The invariant the code actually implements is the incremental form: appending element e to array A grows len(sj(A)) by len(sj(e)) + 1.
[bot] 🏷 Review 结论:可合(含我直接推上去的一个注释小修,见文末) 结构化七问1. 是什么问题:plugin fork 快照( 2. 什么原因导致的:#2017/#2058 引入 isolated fork 后快照成为 fork 回放的外部契约,但维护方式仍是「每请求从零重建」的最保守形态:无增量结构、无 dirty 标记、不区分「是否已是外部契约」。 3. 怎么修复的:三个机制都打在根因上(疏而非堵):
关键假设我逐一验证过,全部成立:① stableJson 增量公式正确(测试用独立 oracle 实现按全量序列化断言精确相等);② 4. 是否有回归问题:逐项审过既有行为:
5. 是否破坏缓存:上游 prefix cache 不受影响(wire 内容零变化,快照是代理内部数据)。反而是改善:identical-view 重发不再 bump revision, 6. 是否改了设计方向:没有架构转向。这是既有 fork-snapshot 机制内的性能/持久化优化,边界不变(快照仍是 fork 外部契约,409 契约文本不变,两种压缩模式的分工不变——改动全在 pluginMode 侧的簿记,proxy mode 路径未触碰)。 7. 是否引入了新的配置:无用户可见 config/env/CLI 变更。新增 3 个内部 metadata 字段( 验证证据
顺手小修(已直接提交到 PR 分支)extension 分支注释里的公式写错了:原文称 判定:可合。 |
[bot] 🏷 跟进:注释修复合并后的新 head |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.14-master.2dfb702 # this exact build
npm install -g billion-context@master # always the newest merged build
|
Closes #2077
What
rememberPluginMessagesran two full O(history) walks on every plugin request (structuredClone(original)+stableJson(...).length > cap), andbuildRecordwrotepluginSnapshotinto every session record (up to the 16 MiB cap of extra JSON per session file, even for users who never fork). Introduced with #2017/#2058.How
rememberPluginMessages: a full-view prefix scan (sameSnapshotView— exactly the field setforkMessageIdentityHashdigests plus id). Steady state is now:markDirty(revision cache stays warm);len(sj([a,b])) = len(sj(a))+len(sj(b))+1additivity); measured exact against the full canonical serialization in tests;BILI_PUBLIC_SNAPSHOT_CAP_BYTESis still enforced from the tracked count on every request — a knob shrink drops the snapshot even without history change; over-cap fail-closed (409 SNAPSHOT_UNAVAILABLE) and under-cap recovery unchanged. Legacy records (no byte count) get a one-time remeasure.toolIsErroroutside theCoreMessageinterface (forkToolIsError, src/plugin.ts:846); a resent history can flip that flag under unchanged ids (pinned by "HTTP snapshot normalizes tool error flags..."), so the walk compares every field fork matching can see. A maintenance contract comment ties the comparator toforkMessageIdentityHash's field set.buildRecord):pluginSnapshotis persisted only once it becomes an external contract — a fork receipt exists on the session, or the stickymetadata.publicSnapshotRetainedflag set on the parent at successful fork time (eagerly flushed, mirroring thepublishForkSessionsync-flush precedent; the receipt lives on the child only, so the parent needs its own trigger).storeOriginalreturns new objects), skipping the walk otherwise. The list remains a union (monotone), so skipping can only under-list refs that resolve today; the fork-time check validates listed refs against the current store, and child creation derives its ref list from store keys — both unaffected by under-listing. The save-time sweep inbuildRecordis untouched as backstop.Behavior changes (old → new)
markDirty) → O(history) field scan with no allocation in steady state; O(tail) clone/serialize on growth; zero work on identical-view resend. Intended — this is the fix.GET /__bili/plugin/snapshotwith 409 "raw snapshot unavailable" until its next model request rebuilds it — self-healing because plugin agents resend their full history every turn; the 409 text is the pre-existing designed recovery contract. Intended.sessionRevisionOfcache stays warm (previously every request invalidated it). Consequence only: fewer recomputes; values identical.publicSnapshotBytes(number),publicSnapshotRetained(boolean),publicSnapshotWireHash(string). No config-file/env/CLI changes; legacy files load fine (missing byte count ⇒ one-time remeasure).Verification
npm run typecheckclean;npm run buildclean.tests/plugin-fork.test.ts43/43 (incl. 5 new regression tests: in-place extension + exact byte accounting, legacy remeasure + O(1) no-op + cap re-evaluation, lazy-persistence contract, replacement path, restart self-heal),tests/issue1307-side-snapshot.test.ts2/2, persist roundtrip trio (proxy-persist,affinity-persist,persist-stats-roundtrip) 35/35. Full suite delegated to CI per local-test discipline.publicSnapshotRetainedbefore flushing so it still exercises the corrupted-store detection path under lazy persistence (without the pin it would pass vacuously).