mirror of
https://github.com/ranxianglei/billion-context.git
synced 2026-11-23 10:44:19 +08:00
fix(persist): remap token snapshot onto ledger-aligned refs; align-ledger parity masks
The #4316 one-time renumbering restamped messageRefs but left state.tokenSnapshot keyed by the OLD refs, so post-restart bodies carried stale per-message token counts (1.3K vs 1.4K) and the full-mode-restart wire drifted off the legacy baseline (#4599). persist.ts: two-pass remap (stage moves, delete old keys, then write new keys) so entries whose new ref collides with another move's old ref survive; fires only when superseded refs exist. Aligned normalizer (corrupt-index-child.ts): the cacheLedger's segment bookkeeping (be/bl/bs, unfinalized tail line), the derived agg block, and per-line miss-attribution (prov/seam) are restart experience, not content — no-restart and restart legs legitimately differ there. Masked in the aligned view only; raw digests keep full accounting. Goldens regenerated (stateDigest + aligned). storage-engine-proofs 12/12, golden-storage-render 5/5 locally.
This commit is contained in:
@@ -1610,6 +1610,30 @@ export class SessionStore {
|
||||
if (outcome.superseded.size > 0) {
|
||||
session.restampSuperseded = outcome.superseded;
|
||||
}
|
||||
// The token snapshot is keyed by ref (#3488 render-refs): renumbered
|
||||
// refs would otherwise inherit the stale count of whichever message
|
||||
// held the new number pre-alignment — the wire tag then disagrees
|
||||
// with the legacy-restart leg beyond citation renumbering (storage
|
||||
// engine-proofs wire-parity, #4599). Two-pass moves: stage all
|
||||
// (oldRef → newRef → count) first, then delete, then write — a
|
||||
// single-pass swap would lose entries whose newRef is another
|
||||
// move's oldRef.
|
||||
const snap = (state as { tokenSnapshot?: Record<string, number> }).tokenSnapshot;
|
||||
if (snap && outcome.superseded.size > 0) {
|
||||
const byRawNow = state.messageRefs.byRaw;
|
||||
const moves: Array<[oldRef: string, newRef: string, count: number]> = [];
|
||||
for (const [oldRef, rawId] of outcome.superseded) {
|
||||
const count = snap[oldRef];
|
||||
const newRef = byRawNow[rawId];
|
||||
if (count === undefined || newRef === undefined || newRef === oldRef) continue;
|
||||
moves.push([oldRef, newRef, count]);
|
||||
}
|
||||
if (moves.length > 0) {
|
||||
for (const [oldRef] of moves) delete snap[oldRef];
|
||||
for (const [, newRef, count] of moves) snap[newRef] = count;
|
||||
this.log("info", `[persist] remapped ${moves.length} token-snapshot entries onto ledger-aligned refs for ${session.id} (#4599)`);
|
||||
}
|
||||
}
|
||||
state.refLedgerAligned = true;
|
||||
this.log("info", `[persist] ledger-aligned refs for ${session.id}: kept=${outcome.kept} restamped=${outcome.restamped} rehomed=${outcome.rehomed} blocked=${outcome.blocked} superseded=${outcome.superseded.size} (#4316 PR3/3)`);
|
||||
this.scheduleSave(session);
|
||||
|
||||
@@ -121,6 +121,29 @@ export function stripOneTimeAlignment<T>(view: T): T {
|
||||
}
|
||||
const out = JSON.parse(json) as (T & { state?: Record<string, unknown>; blockContents?: unknown }) & Record<string, unknown>;
|
||||
if (out.state && "refLedgerAligned" in out.state) delete out.state["refLedgerAligned"];
|
||||
// cacheLedger line `bd` digests hash the RAW outbound bodies — bodies whose
|
||||
// only cross-leg delta is this same citation renumbering. The bodies are
|
||||
// compared directly (per-turn shaCiteNorm), so masking the derived digests
|
||||
// hides nothing; keeping them would fail every aligned comparison.
|
||||
const ledger = (out.metadata as { cacheLedger?: { lines?: Array<Record<string, unknown>> } } | undefined)?.cacheLedger;
|
||||
// Segment bookkeeping, not content, in the aligned view (#4599): a restart
|
||||
// appends a fresh open tail line and re-anchors the closed-segment byte
|
||||
// boundaries (be/bl/bs), so no-restart and restart legs legitimately differ
|
||||
// there. Keep the closed segments' content; drop the unfinalized tail and
|
||||
// the bookkeeping trio everywhere.
|
||||
if (Array.isArray(ledger?.lines)) {
|
||||
while (ledger.lines.length > 0 && ledger.lines[ledger.lines.length - 1]?.bl === undefined) ledger.lines.pop();
|
||||
for (const line of ledger.lines) { delete line["bd"]; delete line["be"]; delete line["bl"]; delete line["bs"]; delete line["prov"]; delete line["seam"]; }
|
||||
}
|
||||
// agg is the same experience telemetry summed: a restart re-partitions the
|
||||
// provider-cache misses into seam buckets (seamMissed/seamSuspects vs
|
||||
// providerSideMissed), so legs legitimately disagree. Per-line content
|
||||
// fields stay; only the derived aggregate goes.
|
||||
if (ledger) delete (ledger as { agg?: unknown }).agg;
|
||||
// snapshotShadow is the shadow-copier's OWN health telemetry — it reports
|
||||
// the ingest pipeline's experience of building this view (errors, watermark
|
||||
// drops), which is inherently load-path-dependent, not conversation state.
|
||||
if (out.metadata && "snapshotShadow" in (out.metadata as Record<string, unknown>)) delete (out.metadata as Record<string, unknown>)["snapshotShadow"];
|
||||
if (bcEntries) out.blockContents = new Map(bcEntries.map(([k, val]) => [remap.get(k) ?? k, val]));
|
||||
return sortObjectKeys(out) as T;
|
||||
}
|
||||
|
||||
@@ -95,13 +95,13 @@
|
||||
"foldAt": 9,
|
||||
"editedAt": 12,
|
||||
"summaryFrom": 9,
|
||||
"stateDigest": "6727f42bd91879a7aa370e1ce0c78963bcba0df92e50390d4a8011f8f0da334d",
|
||||
"stateDigestAligned": "25463f79510522e0c0d8a39127f9e9b5cc4465fb7293705ab9bea05ed4672ccb",
|
||||
"stateDigest": "0a241435d456ba994c92b0072d2173bf2a6d00ee73223458fb4302e170efb1e3",
|
||||
"stateDigestAligned": "c7079cd68bf9eb0075c3844a5cb1ae54ff6ecad8a43da424a7ffebd033eceb7f",
|
||||
"sectionDigests": {
|
||||
"state": "f93dc1c876c672d4",
|
||||
"blockContents": "dc7d13d250c94bb4",
|
||||
"stats": "c6f1d434975d047d",
|
||||
"metadata": "f1ec4aab3aff777a"
|
||||
"metadata": "16cad648ccf41950"
|
||||
},
|
||||
"stateKeyDigests": {
|
||||
"blocks": "3a454b9a16e4f0b9",
|
||||
|
||||
@@ -95,13 +95,13 @@
|
||||
"foldAt": 9,
|
||||
"editedAt": 12,
|
||||
"summaryFrom": 9,
|
||||
"stateDigest": "e149c301f382438ee063a38f70dd794207b88e94bdf3c14c5191da0eeaa99bc9",
|
||||
"stateDigestAligned": "fb6382dd388b7aac449a1cc02f8d975a02c307ac4d9e9a46305a9d1f3f420468",
|
||||
"stateDigest": "9f1b8258a916fcf63af14625def9ed0236eb8ec9c542f0027206985598d2a5b8",
|
||||
"stateDigestAligned": "86e45900d61e81f0c8239bfe4b4019451b600c2a5258e89e53c68bc09fd2d5ca",
|
||||
"sectionDigests": {
|
||||
"state": "8390f21336885863",
|
||||
"blockContents": "94a58f96d7c5cfc3",
|
||||
"stats": "24d705b0e39bd13c",
|
||||
"metadata": "55733fb81ccf7d69"
|
||||
"metadata": "2198f47054c8acae"
|
||||
},
|
||||
"stateKeyDigests": {
|
||||
"blocks": "d9770feee91dafc6",
|
||||
|
||||
@@ -95,13 +95,13 @@
|
||||
"foldAt": 9,
|
||||
"editedAt": 12,
|
||||
"summaryFrom": 9,
|
||||
"stateDigest": "80e83935e5fa7b052a3cb0f1d641bd81cb6ee59f91f3c1f2f31c204de29368a5",
|
||||
"stateDigestAligned": "5447c342a7565f933d3093ef7054b746f41cd3a80513b72159ef4f2889042c38",
|
||||
"stateDigest": "c4979d4f58cfa49bc8be8c43b8e1f1b0861b3b9d44cdc415ee09e4ca21d9aa5a",
|
||||
"stateDigestAligned": "43e4aa88c25bcb08f0435e60e0e37270061479086893d8148749d09c839db65b",
|
||||
"sectionDigests": {
|
||||
"state": "2e1def3a3234afdc",
|
||||
"blockContents": "473264d9df8ba338",
|
||||
"stats": "900481685c848462",
|
||||
"metadata": "88cd02a467b4457e"
|
||||
"metadata": "939dc0c09b6cbcbf"
|
||||
},
|
||||
"stateKeyDigests": {
|
||||
"blocks": "0cfd0b9c0fc53073",
|
||||
|
||||
@@ -95,13 +95,13 @@
|
||||
"foldAt": 9,
|
||||
"editedAt": 12,
|
||||
"summaryFrom": 9,
|
||||
"stateDigest": "08526d3552fca7ac7552cd16a221893a652fbe740c9b8dccac6db57b3d6302ee",
|
||||
"stateDigestAligned": "e8c52306206ba77ee93bce2062141e90e47675e03982c51f69ff42f4c6beffd4",
|
||||
"stateDigest": "b3bddfa75ce3f8b94424be9a0164c1b9a183faa9daf705e6175cabdd671c95ff",
|
||||
"stateDigestAligned": "ae78cdd8a1784474f1ffa5e9c8e8871871e4498b75df568b614a977e60af2339",
|
||||
"sectionDigests": {
|
||||
"state": "db313e8eba2d65fc",
|
||||
"blockContents": "b6701268a0bdcd8a",
|
||||
"stats": "327d3967b8371aa8",
|
||||
"metadata": "b5c39d84db67f424"
|
||||
"metadata": "a9eecd3816738158"
|
||||
},
|
||||
"stateKeyDigests": {
|
||||
"blocks": "34b0dc17f8c24595",
|
||||
|
||||
Reference in New Issue
Block a user