Skip to content

feat: expose compress.priceProfile {w,r,q} for cache-economics reports - #1285

Merged
ranxianglei merged 7 commits into
masterfrom
2026-09-24_compress-price-profile
Sep 27, 2026
Merged

ranxianglei merged 7 commits into
masterfrom
2026-09-24_compress-price-profile

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Sep 24, 2026 •

Copy link
Copy Markdown
Owner

Closes #1279

What changed

#800's approved design made the price profile optional ("价格档 (w,r,q) 可选配置;不配置则只给纯 token 单位"), but the implementation hardcoded it: buildSessionCacheReport emitted profile: { w: 1.0, r: 0.1, q: 4.0 } and called computeFoldEconomics(...) without a price argument, so every fold's P&L verdict was priced at Anthropic-ratio weights regardless of upstream.

This PR ships in two layers: (1) the optional compress.priceProfile config, and (2) — per the #1417 review follow-up — wiring models.dev pricing as the default source when no level configures one.

Layer 1 — compress.priceProfile config

  • Config: new compress.priceProfile: { w?, r?, q? } — normalized multipliers over the input-token unit (p_in = 1): w = cacheWrite/input, r = cacheRead/input, q = output/input. Validated in parseCompressSettings (finite numbers >= 0; unknown subkeys ignored; malformed value rejects the whole block like every sibling). Merged sub-field-wise across the three levels (global -> provider -> model), same pattern as absorb/ccr/reasoningGuard.
  • Passthrough: the last request's effective profile is stamped onto session.metadata.cachePriceProfile where cs already resolves in runPrepare (deleted when unset, latest-wins — mirrors the activePack stamp). buildSessionCacheReport reads it with a small trust-boundary guard (persisted metadata is user-editable JSON), passes it to computeFoldEconomics, and emits the effective profile into the report's profile field. Zero signature changes: every report face (acp_cache tool, /acp-cache command on all hosts, GET /__bili/cache-report) picks up the last request's config automatically.
  • Zero kernel changes: computeFoldEconomics(f, price?) and buildCacheReport(samples, folds, opts.priceProfile) already accept the profile (acp-kernel 0.0.86 pin unchanged).

Layer 2 — models.dev pricing as the default source (#1417 follow-up)

When no level sets compress.priceProfile, the stamp site now falls back to the request model's models.dev price row instead of the bare kernel defaults.

  • Builder: scripts/update-registry-snapshot.mjs now pulls https://models.dev/catalog.json (not models.json) and flattens the nested providers.<host>.models.<id>.cost rows into a top-level costs map keyed ${host}/${id}, written alongside models in the snapshot. The offline floor therefore carries prices. Snapshot rebuilt: 428 models, 7126 cost rows.
  • Registry: peekRegistryPriceProfile(model, host?) resolves a model to a PriceProfile using the same root/variant/suffix mechanics as peekRegistryContext (窗口溢出报错 #736). Known-provider hosts get their own listing (${provider}/${name}); unknown relays take the first matching listing across hosts (one-time warning on conflicts). Derivation: w = cost.input, r = cost.cache_read ?? 0.1 * input, q = cost.output ?? 1.5 * input; missing/non-positive input -> undefined (never half-invent a profile). Derived multipliers are normalized to kill binary float noise (they print verbatim in the report header).
  • Stamp site: runPrepare stamps the registry profile only when cs.priceProfile === undefined; otherwise user config wins wholesale. Unresolvable models leave no stamp (kernel relative defaults apply downstream).
  • Data reality check: models.json itself carries zero pricing — the live probe found 0 cost keys across all 427 entries. Pricing lives exclusively in catalog.json's nested provider sections (7757 cost-bearing rows of 8181). That is why the builder switched endpoints.

Docs

CONFIGURATION.md + CONFIGURATION.zh-CN.md priceProfile section now document both unit conventions (user config = normalized ratios p_in=1; registry default = absolute $/Mtok) plus the derivation, fallbacks, and override semantics. README.md /acp-cache seam paragraph updated.

Behavior change flag (report-only)

The profile never touches compression triggers, cadence, or any wire behavior — pure report surface. But the numbers in the report change, in two distinct ways:

  1. User-configured profiles (new capability). Old: nonexistent. New: when any level sets compress.priceProfile, P&L fields and the report profile field use it; each unset field falls back to the kernel default (w=1, r=0.1, q=4).
  2. Unconfigured sessions (default changed). Old: ALL folds always priced at {w:1, r:0.1, q:4} (Anthropic-ratio guess), byte-identical for every upstream. New: when the request model resolves in the models.dev registry, folds are priced at that model's absolute $/Mtok row (w=cost.input, r=cache_read ?? 0.1*input, q=output ?? 1.5*input); only unresolvable models keep the kernel relative defaults. Consequence: for a listed model the P&L magnitudes and breakeven/PAID BACK verdicts will differ from before — that is the intended fix (out-of-box reports now reflect the upstream's actual economics rather than an Anthropic assumption). The report header prints the active profile (@ w=.. r=.. q=..), so the unit convention in force is always visible.

Note the two conventions coexist by design: a normalized user profile yields P&L in input-token-equivalent units; a registry-derived profile yields P&L in $/Mtok-weighted units. Both flow through the same kernel math; the header disambiguates.

Tests

  • tests/compress-settings.test.ts: three-level sub-field merge (deepest wins), absent-all-levels -> undefined, parse valid/partial/malformed (rejects strings/negatives/NaN/arrays/scalars).
  • tests/cache-ledger.test.ts: unstamped session keeps {1, 0.1, 4} with pinned economics; stamped DeepSeek-class {1, 0.1, 1.5} shifts the verdicts in the rendered ledger header AND matches the kernel batch path field-for-field; partial {q:2} per-field fallback; corrupt persisted stamps fall back to defaults.
  • tests/price-profile-stamp.test.ts: real-server e2e — provider-level config stamps the session, an unstamped lane clears it (latest-wins); plus the registry-fallback cases: listed model + no config -> stamped with the derived absolute profile, unlisted model -> no stamp, user config at any level -> wins wholesale over the registry row.
  • tests/registry-price-profile.test.ts: priceProfileFromRow full/partial/unusable rows; peekRegistryPriceProfile known-host exact match, unknown-relay suffix scan (first-listing-wins, conflict warning), unlisted -> undefined, cold cache -> undefined (no fetch); bundled-snapshot sanity (>=1 anthropic/* key carries a usable cost row).

Pre-flight on rebased head (master @ 4b5c045):

  • npm run typecheck clean
  • npm test: 2850 tests, 2838 pass, 0 fail, 12 skipped
  • npm run build success

Out-of-scope findings (filed separately, see issue thread)

While verifying incremental-vs-batch parity I found two distinct problems, both tracked in their own issues rather than bundled here:

  1. bili's incremental ledger caps turnsToNextFold (k) at 1 — consumedFoldSeq double-duty in recordCacheSample — so paidBack verdicts are systematically wrong for ALL providers (not just non-Anthropic).
  2. billion-context-pi's standalone acp_cache tool has the same hardcoded-profile class (calls buildCacheReport without a profile option; its config surface would need an equivalent key).

@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-24_compress-price-profile (2c1767f)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-1285

Each push to this PR publishes a new version under the pr-1285 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr1285.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 On it — pulling branch 2026-09-24_compress-price-profile, verifying the diff against the claims (config merge, passthrough stamp, report surface), checking CI status, and confirming the two out-of-scope issues were actually filed. Full review conclusion to follow shortly.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — verified, can be merged. I pulled 2026-09-24_compress-price-profile (single commit 05a5a446, 0 behind current master 8e5eb582), re-ran the pre-flight locally, and walked the regression surface. Details below.

① Original issue (#1279) — direction check. Confirmed the reported defect is real and the fix hits the root cause, not a symptom: pre-PR src/cache-ledger.ts emitted profile: { w: 1.0, r: 0.1, q: 4.0 } unconditionally and called computeFoldEconomics(...) with no price argument, so every fold's P&L was priced at Anthropic-ratio weights regardless of upstream. The missing piece was the config surface — exactly what #800's approved design specified ("价格档 (w,r,q) 可选配置"). The PR adds the key, the three-level merge, and the passthrough; it does not paper over the symptom at the report layer. ✅

② Regression surface — existing behaviors the diff touches, each judged:

  • parseCompressSettings whitelist: purely additive (priceProfile joins KNOWN_KEYS). All pre-existing keys parse identically. New semantics only for the new key: malformed value rejects the whole compress block — same contract as every sibling (disclosed in docs).
  • mergeCompress: one new sub-field-wise merge branch (deepest wins, like absorb/ccr/reasoningGuard); untouched fields byte-identical.
  • server.ts runPrepare: new stamp session.metadata.cachePriceProfile (latest-wins, deleted when unset). New metadata key, no collision with existing ones; no wire/trigger/cadence impact. Session.metadata is a required field (src/session.ts:156, init {} at :330), so direct assignment is safe — same pattern as the ~15 existing stamps.
  • buildSessionCacheReport: the one real behavior change — old: P&L fields always computed at {w:1, r:0.1, q:4}, profile field always those constants; new: computed/emitted at the session's effective profile, per-field kernel fallback. I verified the "unconfigured = identical" claim against the kernel source: acp-kernel 0.0.86 dist/index.js:5992-5994 does price?.w ?? 1 / price?.r ?? 0.1 / price?.q ?? 4, so passing the explicit default-filled profile is arithmetically identical to the old no-arg call, and JSON.stringify(1.0) === "1" keeps the profile field byte-identical. Change is flagged in the PR body per policy. ✅
  • config.ts: only incidental change is the outputSteering JSDoc de-indent from resolving the adjacency conflict with feat: output-side compression (verbosity steering + effort routing) #1257 — cosmetic, disclosed in the PR body ("union of both"). Not a semantic change. ✅
  • Report-only invariant holds: grep confirms cachePriceProfile is read solely by buildSessionCacheReport; nothing downstream of triggers/nudge/preflight consumes it. Both compression modes unaffected (report face, not wire). ✅

③ Code. Clean and minimal (+237/-19 across 9 files). The trust-boundary guard on stampedPriceProfile is the right call — persisted session metadata is user-editable JSON, so validating shape (object, finite ≥ 0 per field) before use, falling back to defaults on corruption, prevents a hand-edited session file from poisoning report math or throwing mid-render. resolveCompress returns a fresh merged object per request (src/compress-settings.ts:112, no caching layer), so stamping cs.priceProfile stores no shared reference that later requests could mutate. Zero signature changes means all three report faces (acp_cache tool, /acp-cache command, GET /__bili/cache-report) pick it up automatically — correct seam choice. No as any/console.error/version-bump violations.

④ Tests — real teeth. tests/cache-ledger.test.ts pins concrete numbers rather than shapes: unstamped baseline oneTimeCostUnits 3500 / breakevenTurns 8.75 / paidBack false; DeepSeek-class {w:1, r:0.1, q:1.5} shifts them to 1000 / 2.5 in the rendered ledger header AND matches the kernel batch path (buildCacheReport({priceProfile})) field-for-field (incremental-vs-batch parity — the property most likely to silently rot); partial {q:2} per-field fallback; corrupt persisted stamp → defaults. If the original hardcoded-profile bug regressed, the DeepSeek case fails immediately; if merge precedence broke, the partial-fallback case fails. tests/compress-settings.test.ts covers three-level sub-field merge (deepest wins), absent-all-levels → undefined, and rejects strings/negatives/NaN/arrays/scalars. Minor gap (acceptable): the runPrepare stamp line itself isn't unit-tested directly — it needs the full request pipeline, which the CI e2e lanes (real codex, real pi package-native) do exercise through runPrepare.

⑤ Diff cleanliness. Every line serves the PR; the single cosmetic hunk is disclosed. No lockfile/generated churn, no unrelated files.

Evidence:

  • Local on rebased head: npm run typecheck clean; node --import tsx --test tests/cache-ledger.test.ts tests/compress-settings.test.ts → 55/55 pass, 0 fail.
  • CI on 05a5a446: 9/9 success — test ubuntu 22/24, windows 22/24, version-guard, build-artifact, real-codex lane, codex CODEX_HOME overlay, real-pi package-native lane. PR state: open, mergeable, base = current master.

Out-of-scope findings: both filed correctly with proper 来源 markers, one distinct problem each — #1286 (incremental ledger caps k at 1 via consumedFoldSeq double-duty → paidBack systematically wrong for all providers; note this affects the numbers this PR exposes, so its fix will be what makes the DeepSeek-class examples in the docs fully trustworthy) and billion-context-pi#529 (standalone acp_cache same hardcode class). Neither belongs in this diff.

Auto-merge gate note: this is a feat: adding a config-schema key, so it stays human-merge regardless — all technical gates are green.

合并前唯一待办:人工 merge(agent 不合并)。结论:修复了缓存报告损益判定被硬编码为 Anthropic 价格权重的问题(#1279),新增可选 compress.priceProfile 三级配置并透传到所有报告出口,未配置时输出与之前逐字节一致;已本地复验 typecheck+定向测试全绿、CI 9/9 通过,可以合并。

ework-agent and others added 5 commits September 26, 2026 21:33
…ofile (#1279 follow-up)

When no level sets compress.priceProfile, the session stamp now falls back to the request model's models.dev price row (absolute $\/Mtok: w = cost.input, r = cost.cache_read ?? 0.1*input, q = cost.output ?? 1.5*input). Known-provider hosts get their own listing; unknown relays take the first matching listing with a one-time conflict warning. Live catalog.json wins when reachable, bundled snapshot is the offline floor (snapshot rebuilt to carry 7126 cost rows). User config at any level still wins wholesale; unresolvable models keep the kernel relative defaults.
ework-agent added 2 commits September 26, 2026 22:49
…lback to kernel 4x, legacy-disk price fallthrough, e2e teardown hygiene (review round 2)
@ranxianglei

Copy link
Copy Markdown
Owner Author

Round-2 review of the registry-cost follow-up commits (44f1bf5 / 7175e84 / f1c5933) — complete. One major fixed on-branch, head 2c1767f.

Verified correct

  • Resolution order: user compress.priceProfile wins wholesale (server.ts stamps verbatim; registry only in the else) — pinned by e2e ({q:1.5} beats a registry row for the same model).
  • Free-vs-unknown: missing/zero/invalid input → undefined (no stamp), distinguished at both layers; mutation-verified.
  • Staleness chain: live > fresh disk > (stale disk vs snapshot, newer-wins) > bundled snapshot; stamp path never fetches. Scale-invariance checked against the kernel (breakevenTurns is homogeneous in w/r/q — absolute $/Mtok and normalized ratios give identical verdicts).

Fixed on-branch (0aa9945 + 2c1767f)

  1. MAJOR — w was priced from input, dropping cache_write. Kernel contract: (w−r)·T prices the cache-write re-upload of the fold; the snapshot carries cache_write on 1537 rows (Anthropic-style ≈1.25× input) and the mapping silently discarded it → ~22% understatement of one-time fold cost / optimistic breakeven for every write-premium provider. Now w = valid(cache_write) ? cache_write : input; pinned by a dedicated test (premium / no-premium / broken-write rows).
  2. q-fallback was 1.5×input, labeled "kernel convention" — it isn't. Kernel default ratio is q: 4×w; observed output/input clusters are 4–6×. Fixed to 4×w + comments aligned.
  3. Legacy-disk price blackout: a fresh pre-costs flat disk cache parsed to costs:null and served for the full 24h TTL with no stamps. Now falls through to a fresh fetch when disk.costs === null (offline keeps the legacy net — pre-costs status quo).
  4. e2e teardown hygiene: proxyCfg.close() sat after the assertions — any throw leaked the server and hung the runner (observed as exit-124 instead of a crisp red during mutation). Moved into finally.

Noted for follow-up (not merge-blocking)

  • Mixed-version shared disk cache: an older bili reading the new {models,costs} shape sees a flat registry (windows miss ≤24h). Consider a format-version bump in the cache filename someday.
  • dashscope/comfly HOST_TO_PROVIDER entries match no catalog provider (catalog uses alibaba) → those hosts get no window/price stamp. Pre-existing.
  • Snapshot grew 316KB→1.05MB (inlined, parsed at module load); acceptable for now, watch dist weight.

Gates on-branch: typecheck clean; targeted 43/43 (registry + price-profile suites). Ready to merge after CI.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Independent cross-check of the round-2 state (head 2c1767f) — every claim verified against code/kernel/snapshot, gates re-run locally, CI fully green. Ready for the human merge.

Claim-by-claim verification:

  • MAJOR fix (w = cache-write): confirmed in src/registry.ts priceProfileFromRow — w = valid(row.cache_write) ? row.cache_write : row.input. Cross-checked against the kernel formula (acp-kernel dist, computeFoldEconomics): oneTimeCostUnits = (w−r)·T + q·σ − r·S, where (w−r)·T is exactly the cache-write re-upload of the invalidated fold range — so pricing w from plain input would understate one-time cost for every write-premium provider, precisely as reported. Snapshot carries cache_write on 1537 rows (counted). ✅
  • q/r fallbacks: now q = 4×w, r = 0.1×w when the row lacks those fields — i.e. the kernel default ratios applied relative to w (kernel defaults are r=0.1, q=4 at w=1). Comments aligned in 2c1767f. ✅
  • Scale invariance: verified from the kernel source — breakevenTurns = max(0, oneTimeCostUnits)/perTurnSavingUnits is homogeneous of degree 1 in (w,r,q): scaling all three by any constant leaves the ratio (and thus paidBack) unchanged. Absolute $/Mtok values are therefore verdict-equivalent to normalized ratios. ✅
  • User-config-wins wholesale: server.ts stamps cs.priceProfile verbatim; the registry lookup sits strictly in the else, with delete-on-miss. E2E pins {q:1.5} beating a registry row for the same model. ✅
  • Free-vs-unknown: rows require finite input > 0 (both sanitizeCosts and flattenProviderCosts), so missing/zero/invalid input → no profile → no stamp → kernel defaults. Mutation-verified per the report; consistent with the code. ✅
  • Staleness chain + legacy-disk blackout: live > fresh disk > (stale disk vs bundled snapshot, newer-wins via newerFallback) > bundled floor; a fresh pre-costs flat disk cache (costs === null) now falls through to a fresh fetch instead of serving blank prices for 24h. Stamp path (peekRegistryPriceProfile) is synchronous and cache-only — never fetches. ✅
  • Snapshot: 7126 cost rows, 1537 with cache_write, size 316KB→1025KB — matches the report exactly.
  • e2e teardown: tests/price-profile-stamp.test.ts closes upstream/proxyCfg in finally (lines 185–190) with an explicit comment that assertion throws must not leak the server. ✅

Gates re-run by me on head 2c1767f: npm run typecheck clean; full local suite: 2851 tests, 2839 pass, 0 fail, 12 skipped (a superset of the targeted 43/43). CI: 13/13 success — test ubuntu 22/24, windows 22/24, e2e, hermetic registry e2e, build-artifact, version-guard, and all four real-client lanes (codex, pi package-native, opencode native, dsh headless). PR state: open, mergeable: True.

One note on master drift: the branch base has moved (master picked up release v0.1.158 plus #1399/#1357/#1411 work since the PR's original base). Of those, only src/server.ts overlaps this PR's file set — acp_rule injection and advisory-chain changes, regions disjoint from this PR's runPrepare stamp hunk; registry.ts/config.ts untouched by master. GitHub confirms textual mergeability; semantic-conflict risk is low. Rebase only if the merge itself complains.

Follow-ups dispositioned:

  • dashscope/comfly host mapping — I verified the claim against the bundled snapshot (no dashscope/ or comfly/ provider prefix exists; Alibaba listings sit under alibaba*), so those hosts dead-end on the known-provider path and get neither registry window nor price stamps. Pre-existing for windows, surfaced for prices here, and the right-key choice (plain alibaba vs alibaba-coding-plan-cn for the coding endpoint) needs catalog verification — filed as HOST_TO_PROVIDER maps dashscope/comfly hosts to provider ids absent from the models.dev catalog → no registry window/price stamps for those hosts #1434 with the 来源 marker, not merge-blocking.
  • Mixed-version shared disk cache (format-version bump) and snapshot weight: accepted design notes, agree with deferring both.

结论:第二轮新增的 models.dev 定价接线(含 w=cache_write 重大修正、q=4×w 回退、遗留磁盘缓存价格黑洞修复、e2e 收尾卫生)已逐条对照内核公式与快照数据核实无误,本地全量测试 0 失败、CI 13/13 全绿,可以合并;dashscope/comfly 主机映射缺陷已另行立项 #1434。

@ranxianglei
ranxianglei deleted the 2026-09-24_compress-price-profile branch October 15, 2026 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: expose compress.priceProfile {w,r,q} config (cache-economics report hardcoded to Anthropic weights)

1 participant