Repository navigation
fix: attribute proxy mode/projection switches distinctly from host prompt rewrites (#2867) - #2873
Conversation
📦 Built Package ArtifactBranch: Option A — Install this exact build (recommended)npm install -g billion-context@0.0.3195-pr.2873.09243ecThis exact version is immutable and reproducible. The rolling Option B — Download artifact
tar xzf billion-context-pr2873.tgz
npm install -g packageInstall instructions are refreshed on each push. |
[bot] 🏷 收到,开始按 PR review 规范处理:拉取分支核对代码、检查 CI 状态、审查方向与回归面,结论将按结构化八问回复。 |
The committed self-heal e2e could never pass as written; CI red confirmed
it. All four defects were in the test itself — product code behaved exactly
as designed (verified via server logs):
1. Admin URL routed through the bili tunnel: `/__bili/cache-report`
hit the mock upstream (any bodyless GET → 200 {}), so rj.reports.find
threw before any attribution assertion ran. Use the loopback proxy port
directly (same form as issue1547-nonstream-cache-ledger).
2. Zombie-window off-by-one: the kernel's first-turn tokenCount is 0
(baseline stamped 0/falsy), so growth reads 0 at t2 and that round is
nudge-idle — injections land t3..t7 and SELF_HEAL_WINDOW=5 arms at end
of t7, making t8 (not t7) the first degraded prepare. Six headerless
rounds now, with the timing documented inline. FILLER also grew
repeat(28)→repeat(230) so mass outside the 5-message recent window
clears minCompressRange/T1 threshold, plus a hermetic
nudge.minGrowthFloor:100 override (issue2432 pattern).
3. Missing stream:true: only the streaming lane settles usage into the
cache ledger; the non-streaming branch JSON-parses the SSE reply,
fails, and passes through unaccounted (#1547) → "0 requests" report.
4. Malformed response.completed frame: data was a bare response object
without the {type, response:{...}} wrapper; dispatch still fired via
the event:-line fallback but data.response.usage read undefined →
empty usage → no settle (plus in-band truncation errors from the pipe
observer).
Verified locally: this suite 2/2 green; report shows PROJECTION SWITCHES
(1 switch · 9980 tok re-billed), projection switch: 9980 (1), prompt
rewrite: 0 (0), no PROMPT SWITCHES section; neighbors
cache-ledger-projection-switch/prompt-switch + responses-plugin-dev-position
21/21 green; typecheck clean.
[bot] 🏷 PR #2873 review 结论(含我在 PR 分支上直接修掉的一处问题) 先说判定:这个 PR 的方向和产品代码是对的,但提交时的 e2e 测试按原样永远过不了——CI 红(ubuntu-22/24、windows-24 的 test job 全挂),本地复现确认。四个缺陷全在测试夹具本身,产品代码一个都没有。我已按「小问题直接修」在 PR 分支补了一个标注 commit( 四个缺陷(均在
修复后的证据(本地实测):e2e 2/2 绿; 结构化八问:
行为变更披露(本 PR 自身,沿用 PR 正文): 收工判定:base 即当前 master,本地 typecheck + 定向套件全绿,diff 收敛到单一测试文件且每行都服务于本次修复——CI 转绿即可合并。合并链接:#2873 |
…ode fields Justification (tests/golden-storage-render.test.ts header requires stated justification for regeneration): PR #2873 intentionally grows the persisted session surface; the committed goldens predate it. Only the metadata-section digests drift — wire bytes, state, blockContents and stats digests are all unchanged: - cacheLedger.agg gains projSwitches/projSwitchMissed zero-initialized on every lane: the same established pattern as promptSwitches/keySwitches/ wireSwitches/... (every switch-family counter is unconditionally zero-initialized); the normalize list (src/cache-ledger.ts:656) covers them, so legacy sessions without the keys load as 0. - responses lane only: cacheLedger.lastKnownCanon (the canonical incoming system fingerprint, required to classify future lines across a proxy restart) and per-line pm (the mode under which each request was prepared); both are undefined-gated at read time (src/cache-ledger.ts: 811-812), so legacy sessions keep the legacy host-side reading. - session.metadata gains canonicalSysFp + lastResponsesMode (responses lane, hash-only values, documented in the PR body). A/B proof: pre-PR master head cd02d2d passes these goldens 5/5; the PR head reproduced the exact CI failure digests before regeneration. Reader paths verified legacy-safe (normalize list :656, lastKnownCanon gating :811-812, readProjSwitchStats fallback :1536). No report-surface change: sections stay byte-identical when counts are zero.
[bot] 🏷 第三轮更新:第二次 CI 红已解决 — golden 快照过期(非产品回归) 上一轮推入 fixture 修复( 八问(本轮增量):
当前状态:分支 |
[bot] 🏷 CI 全绿,收工判定
分支
产品代码零改动(相对原 PR);行为变更披露维持原 PR 正文所列五项不变。本地定向验证(golden 5/5、e2e 2/2、邻接套件 21/21、typecheck)与 CI 全量回归一致。 判定:可合并。 合并由人执行:#2873 |
📦 Merged Build PublishedThis merge to master was published as npm install -g billion-context@0.0.308-master.56063ce # this exact build
npm install -g billion-context@master # always the newest merged build
|
What
Closes #2867
When a Responses session self-heals from plugin mode to proxy mode (#2155), the same mid-history system/developer items are projected differently by the two modes (plugin keeps them in place via
__bili_inplace_sysdev, #1638; proxy hoists them into the front developer block, #1085). The forwarded-system fingerprint therefore flips even though the host changed nothing — and the cache ledger (#2350) narrated every such flip as host rewrote the system prompt. This PR separates the two fingerprints and classifies each flip at settle time.How
src/fold-reconcile.ts,src/server/prepare-responses.ts):systemFp= the FORWARDED projection (what actually went upstream; unchanged semantics);canonicalSysFp= the canonical INCOMING view (role + text + wire order of every sys/dev item, computed before mode-dependent marking, independent of pluginMode). Hash/length/mode/reason only — never the prompt body.src/cache-ledger.ts): projection flipped + canonical stable ⇒ new causeprojection(new sparse line flagspf/pm, aggprojSwitches/projSwitchMissed, own cold window, report section + invalidation bucket); canonical also changed ⇒ host-sidepromptas today; canonical-only flip (e.g. mid-history edit under plugin in-place rules) ⇒ observationalcfflag, no billing bucket; unknown sides never classify (legacy reading preserved for non-responses lanes).prepare-responsesrecords the serving mode (lastResponsesMode) and logs ONE line per transition with the reason (self-heal degrade / restore / header absent).src/web/client.ts,src/web/i18n.tsen+zh), stats fields (src/server/admin.ts).Behavior changes (old → new)
promptSwitches/ cause=prompt/ "PROMPT SWITCHES (host rewrote system prompt)" now fire ONLY when the canonical incoming view also changed. Sessions with pure self-heal mode switches show FEWER prompt events than before — those events move to the newprojectiondimension. Charged-token totals are unchanged (partition only).projection, notprompt. Before this PR the flip line opened BOTH switch windows (promptSwitchedis true whenever the forwarded bytes change, which it does on a mode switch), so the cold tail re-attributed the proxy's own mode change to the host — the same bug class as the headline issue, found by the new test suite.PROJECTION SWITCHESsection,projection switch:invalidation line,report.projSwitches,readProjSwitchStats(),/__bili/statsfieldsprojSwitches/projSwitchMissedTokens.[responses] compression mode switched X→Y (reason).Config surface
N/A — no new config file fields, env vars, or CLI flags.
canonicalSysFpandlastResponsesModeare internalsession.metadatakeys (forensics inputs for the ledger), not user-facing configuration. Persistence format stays v1; new ledger fields are sparse and normalize-in-place on load (same mechanism as prior agg additions).Verification
npm run typecheckclean.tests/cache-ledger-projection-switch.test.ts(9: pure flip, cold-tail continuation, flapping, host-edit-wins, canon-only observation, sparseness/legacy, model-shadowing, wire-outranked, report text),tests/responses-mode-switch-fingerprint.test.ts(unitcanonicalResponsesSystem+ real-server e2e: t1 plugin bind → 5 zombie nudge rounds → degrade arms → first proxy prepare re-hoists the mid-history developer item; captured bodies show the position move and/__bili/cache-reportattributes it to PROJECTION SWITCHES with zero PROMPT SWITCHES), plus regression runs oftests/cache-ledger-prompt-switch.test.ts(8, [cache-ledger] fold-reconcile 检测到宿主侧系统提示词改写但未接入归因:prompt 改写被误归入未归因 TTL 残差 #2350 unchanged) andtests/responses-plugin-dev-position.test.ts(4, [Bug] bili 启动 OMP 后缓存命中率 82.7%,连续请求多次骤降至 4992 cached tokens #1638 position preservation unchanged).kernel/; kernel-guard not triggered.Golden regeneration justification (storage-render, follow-up commit 41cede6)
Per the rule in tests/golden-storage-render.test.ts (regeneration requires stated justification): this PR intentionally grows the persisted session surface, so the four storage-render goldens were regenerated. Only metadata-section digests changed (wire bytes / state / blockContents / stats unchanged):
cacheLedger.agg.projSwitches/.projSwitchMissed— zero-initialized on every lane, same established pattern as the existing switch-family counters (promptSwitches,keySwitches, …); covered by the normalize list so legacy sessions load them as 0.cacheLedger.lastKnownCanon(canonical incoming system fingerprint, needed to classify lines across a proxy restart) and per-linepm(mode under which the request was prepared); both undefined-gated at read time, so legacy sessions keep the legacy host-side reading.session.metadata.canonicalSysFp+session.metadata.lastResponsesMode(responses lane, hash-only values, as documented above).A/B proof: pre-PR master head
cd02d2dc5passes these goldens 5/5; the PR head reproduced the exact CI failure digests before regeneration. Reader paths verified legacy-safe (src/cache-ledger.ts: normalize list :656,lastKnownCanongating :811-812,readProjSwitchStatsfallback :1536). Report surface stays byte-identical when counts are zero.