Repository navigation
fix(#325): floor meter at provider anchor minus tokens reclaimed since it - #328
Conversation
[bot] 🏷 Reviewing now — pulling the PR diff, re-verifying the root-cause chain independently, and running the full suite locally before giving a verdict. Will report back shortly. |
Reviewed end-to-end — the fix logic is correct (root cause independently verified, floor math proven in both quadrants), but the PR cannot land as-is: it is What I verified
Design verdictApproving the approach over the alternative: subtracting genuinely-freed tokens from the provider's real number is exact and needs no magic constant; "add the fixed component back to the estimate" would require measuring tool schemas that are unmeasurable/unstable inside the extension. Right call. Blocker: dirty merge vs masterBranch point is I resolved it locally and validated: full suite 633 / 630 pass / 0 fail / 3 skip, typecheck + build green. Recipe for the rebase:
Minor finding (report-only, no separate issue per policy)
Recommendation: rebase onto current master with the recipe above (happy to share the exact hunks if useful). Fix code itself: LGTM. |
|
分诊(队列清理 #484): KEEP-VALID(#325 的两极缺陷是真问题),rebase 中等。 master 现行是二值跳过: rebase 风险:中等。 |
577a7ac to
55ca193
Compare
📦 Built Extension ArtifactBranch: Option A — Install from npm PR tag (recommended)pi install npm:billion-context-pi@pr-328Each push to this PR publishes a new version under the Option B — Download artifact
tar xzf billion-context-pi-pr328.tgz
pi install ./packageThis comment is automatically updated on each push. |
已按分诊完成 rebase 到当前 master(v0.1.74 / acp-kernel 0.0.81),单 commit 保留。head 现为 冲突解决: 关键交互(分诊点名的"floor 与校准上限的交互"):provider floor 从二值跳过改为连续值 测试适配(新基座强制,已在 PR 描述补记):
验证:typecheck ✅ / build ✅(acp-kernel 内联,零运行时依赖)/ 三个受影响测试文件全绿(floor-stale 14、sent-view 5、growth-scale 2)。本地全量套件里非绿的仅为环境噪声:临时文件写入受限导致的 EACCES(CI 有可写 /tmp)+ 2 个 未改 PR: #328 |
aede448 to
261f611
Compare
…e it After a successful compress, usageAnchorPredatesCompression() skipped the provider-usage floor entirely, so tokenCount fell back to the raw sent-view estimate — which structurally omits tool-definition schemas, ref-tag overhead, and the injected nudge (a fixed ~70–80K for this host). The meter read false-idle; on the next turn a fresh provider usage re-engaged the floor at the full real prompt size (~84–95% of limit) and fired a spurious EMERGENCY, which the model answered by compressing again → loop. Fix: when the anchor predates a successful compress, floor at (lastProviderUsage − netReclaimedSinceAnchor) instead of skipping. netReclaimed sums max(0, compressedTokens − summaryTokens) over active blocks whose creating compress toolResult lands after the last valid usage anchor. The provider's real number already counts all fixed overhead accurately, so subtracting only the genuinely-freed tokens yields an exact post-compression size without needing to measure tool schemas (unmeasurable/unstable in the extension). New work added after the compress still grows tokenCount via sentTokens as before. No regression on the non-stale path (floor unchanged); a stale-anchor compress that reclaimed little still correctly trips the nudge.
261f611 to
71c1529
Compare
Fixes #325.
Problem
After a successful compress, ACP's own token accounting dropped ~70–80K below the provider-reported prompt size, so the nudge meter flipped to
idle. On the next turn the host-floor anchor (#257) snappedtokenCountback up to the real prompt size (~84–95% of the effective limit) and re-injected an EMERGENCY "compress now" nudge. The model compressed again → loop. The agent stopped working every few turns just to compress.Root cause
src/index.tssetstokenCount = estimateTokens(coreMessages, coveredIds, imageTokens) + systemPromptTokens, then floors it at the provider-reported prompt size — unlessusageAnchorPredatesCompression()is true (a successful compress landed after the last valid assistant usage), in which case the floor was skipped entirely.estimateTokens(src/tokens.ts) sums message text only. It structurally omits tool-definition schemas, ref-tag overhead, and the injected nudge — a fixed ~70–80K on this host. So the raw sent-view is always that far below the real request.tokenCountto the provider number — which is why pre-compression estimates "matched the provider exactly."tokenCountback to the undercounting raw estimate → falseidle. Next turn, fresh provider usage re-engaged the floor at full real size → spurious EMERGENCY.The #289 view-recount inherited the same blind spot because it compares against the post-processTurn view.
Fix
When the anchor predates a successful compress, floor at
(lastProviderUsage − netReclaimedSinceAnchor)instead of skipping.netReclaimedSinceAnchor= Σ over active blocks whose creatingcompresstoolResult lands after the last valid usage anchor ofmax(0, compressedTokens − summaryTokens).The provider's real number already counts all fixed overhead (tool schemas, etc.) accurately; subtracting only the genuinely-freed tokens yields an exact post-compression size with no need to measure tool schemas — which are unmeasurable/unstable inside the extension (no API enumerates Pi's full tool set). This is why the issue's suggested direction ("add the fixed component to the estimate") was not taken: it would need an unstable magic constant.
New work added after the compress still grows
tokenCountthroughsentTokensas before; the non-stale path is unchanged; a stale-anchor compress that reclaimed little still correctly trips the nudge.Tests
tests/floor-stale.test.ts: unit tests forcompressionAnchorStaleness(fresh / stale / pre-anchor exclusion / inactive+unattributable skip / negative-savings clamp / multi-block sum).tests/sent-view-arbitration.test.ts: rewrote the stale test to seed a real reclaiming block (no false emergency), plus a guard test asserting a tiny-reclaim + near-limit context still fires.Notes
Rebase onto v0.1.74 (acp-kernel 0.0.81)
Rebased onto current
master; the single commit is preserved.src/floor-stale.tsandtests/floor-stale.test.tsapplied cleanly (unchanged upstream). Two regions had moved upstream and were merged by hand:src/index.tsmeter block — rewritten since this PR branched ([Bug] Context size accounting diverges from provider usage 1.5-2x and never re-anchors: first-sight-mass stuck on, emergency state persists after context recovered below limit #455 provider-calibration cap, [Bug] Token 计数与压缩后状态不同步,导致低收益及零收益的连续压缩 #289 sent-view recount). The fix was re-integrated against that structure rather than cherry-picked verbatim.tests/sent-view-arbitration.test.ts— migrated upstream for the acp-kernel 0.0.55 kernel API; the [bug] Post-compression sent-view undercounts prompt by ~70–80K → false idle → host-floor emergency re-nudge loop #325 rewrite was reconciled onto it.Meter integration
The provider floor is now continuous instead of a binary skip:
Fresh turns floor at the full provider number; stale turns floor at
anchor − netReclaimed. It feeds the sameapplyFloorsas the #455 calibration cap and the #289 view-recount, and the #455 calibration stays fresh-only (gated on!predates).Growth-scale flip signal (#267)
The baseline re-anchor trigger changes from the binary stale flag to
estScaleWins = hostFloor <= sentTokens— i.e. it fires when the effective ruler actually switches between estimate-dominated and provider-floor-dominated. Under the adjusted floor a stale turn can still floor near the full anchor (small reclaim); treating "stale ⇒ estimate scale" would spuriously reset the growth baseline. Keying off which value actually wins avoids that while still catching genuine switches.Test adaptations forced by the new base
tests/sent-view-arbitration.test.ts— the "no false emergency" case now seeds 120K reclaimed (was 60K). On acp-kernel 0.0.81 a 60K reclaim leaves the adjusted floor at 63.9% of the window — still inside the ≥45% nudge band, so a nudge correctly fires; 120K → ~30.6% clears the band (the verified idle quadrant). The tiny-reclaim guard test (a genuinely full context must still trip the nudge) is unchanged.tests/growth-scale-flip.test.ts(Growth 基线混用两套刻度 #267) — the stale→fresh flip case now seeds a real ~150K-reclaim block so the [bug] Post-compression sent-view undercounts prompt by ~70–80K → false idle → host-floor emergency re-nudge loop #325 adjusted floor keeps turn 1 on the estimate scale. Without it, a stale turn with no reclaim reports provider scale — precisely the behavior [bug] Post-compression sent-view undercounts prompt by ~70–80K → false idle → host-floor emergency re-nudge loop #325 removes — so the old "stale ⇒ estimate baseline" assumption no longer holds. The stable-scale case is unchanged.Verification on the rebased head
tsc --noEmit✅ · tsup build ✅ (acp-kernel inlined, zero runtime deps). All three affected test files pass locally (floor-stale 14, sent-view-arbitration 5, growth-scale-flip 2). No version bump (release-branch territory per AGENTS.md).