Repository navigation
fix(cache): settle non-streaming rewriter turns into the cache ledger (#1547) - #1550
ranxianglei wants to merge 4 commits into
Conversation
…loop/plugin call sites (#1547) recordUsage (loop/core.ts) and applyUsageSample (plugin.ts) each inlined the same input-side stats update + recordCacheSample sequence with subtly drifted guards. Extract settleUsageReport into cache-ledger.ts (next to recordCacheSample, which it calls) so all three response shapes can settle through one path.
…#1547) The non-streaming JSON branch of forward() updated session.stats directly but never recorded a ledger sample, so stream:false sessions produced zero lines in /acp-cache and __bili/cache-report and were blind to every hit-rate / invalidation-attribution feature. Route its usage capture through settleUsageReport with protocol + targetOrigin, and give the branch the [acp-usage] log line and collapse watch the other two shapes already have.
[bot] 🏷 Review started. Pulling the branch ( |
[bot] 🏷 Review complete — fix is correct, minimal, and well-tested. Mergeable once the stack lands (order: #1540 → #1545 → #1550). I added one small direct fix during review (below). Details: ① Direction (issue first). Verified #1547's triage against both master and #1545's head: the root cause is real and correctly located — the non-streaming else arm of ② Regression audit (existing behaviors on touched paths):
③ Code. Helper signature/behavior audited line-by-line (single definition, callers keep their own gates/log/watch/output handling); ④ Test validity. ⑤ Diff cleanliness. 5 files, 2 commits (refactor / fix cleanly separated), every line on-topic. Verification evidence. CI on this head ran the 5 e2e lanes (real opencode/dsh/pi/codex vs deterministic fake upstream + hermetic registry e2e) — all green. The main Direct fix added during review (per review policy, committed straight to the branch): the new non-streaming 中文摘要:修复了非流式(stream:false)请求从不写入缓存账本的缺陷(三个结算点统一走共享 helper),行为变更 (a)–(g) 均已在 diff 中逐项核实为有意对齐、无意外回归,本地补齐了 CI 未跑的 typecheck+全量测试(3047 通过)并顺手补全了日志的 img-saved 字段,待 #1545 合入、rebase 到 master 触发主 CI 后即可合并。 |
…st not eat a pending fold (#1536) Mutation check: dropping the known-gate on the consumedFoldSeq advance leaves this red ('measured sample owns the fold compRepay'); clean tree 48/48.
|
Deep review: ✅ mergeable — cleanly rebased stack head, 48/48 local (incl. my new pin), typecheck clean. What I verified
Stack order: #1540 → #1545 → #1550 (this PR carries the fold-cursor pin test on its head — merge the stack in order and it all lands together). |
[bot] 🏷 Confirmed against the new head
Nothing outstanding from my side — ready to merge in order whenever you are. 中文摘要:已在新头 1e04ba0(含你的 fold-cursor 回归 pin)上独立复核——typecheck 干净、全量 3048 通过、CI 五条 e2e 全绿,pin 测试断言有牙(mutation 会红),无新增问题;按 #1540 → #1545 → #1550 顺序合并即可,合并时留意主 ci.yml 需在落到 master 后的最终头上跑绿。 |
Closes #1547
What
The cache ledger had exactly two entry points:
recordUsage(streaming compress loop,src/loop/core.ts) andapplyUsageSample(plugin pipes,src/plugin.ts). The non-streaming rewriter branch offorward()(src/server.ts, else arm ofif (prepared.stream)— JSON responses with compress injection) updatedsession.statsdirectly but never recorded a ledger sample.stream:falsesessions therefore produced zero lines in/acp-cacheand__bili/cache-report, and every hit-rate / invalidation-attribution feature (including the #1536 P0/P1 work this branch builds on) was blind to them.Changes
50278b82): extractsettleUsageReport(session, { total, reportedCached, output?, protocol?, upstream? })intosrc/cache-ledger.ts— the shared input-side settle path (stats update +recordCacheSample).recordUsageandapplyUsageSamplenow call it; behavior-neutral (typecheck + full suite green before and after extraction; guards audited line-by-line).bb4ef7ac): the non-streaming else arm settles through the same helper, passingprotocol: prepared.protocolandupstream: targetOrigin(the origin computation added by fix(cache): quarantine unmeasured cache reports + attribute invalidation causes (P0+P1, CLI) #1545), so all three response shapes (plugin SSE, loop SSE, loop JSON) feed one ledger. The branch also gets the[acp-usage]log line and thewarnCacheCollapsewatch the other two shapes already have.Behavior changes (flagged per policy)
lastInputTokens→0,lastInputTokensSource, and retired the overflow armcacheSamplesincremented even at total===0[acp-usage]log line for non-streaming turnsoutputTokensonly added when input fields were presentNudge / emergency-truncate triggering is unaffected: it reads
session.stats, whose values are unchanged for every live report shape.Stacking dependency
Builds on #1545's branch: consumes its
targetOrigincomputation and the extendedrecordCacheSample(cached: number | null, protocol, upstream)signature. Please merge after #1545 (or rebase onto master once it lands).Tests
tests/issue1547-nonstream-cache-ledger.test.ts: server-level repro of the issue (a stream:false turn through the built proxy now yields a ledger line and populates__bili/cache-report; a follow-up zero-total turn keepslastInputTokensand records no sample) + 3 unit tests forsettleUsageReport(attribution fields, credit netting + quarantine, zero-total no-clobber).ACP_TEST_E2Erun needs a local upstream at 127.0.0.1:8199 which this sandbox does not have — the base commit fails identically there, so CI (which has the secret upstream) is the authoritative e2e gate for this PR.