Skip to content

fix(floor): skip the provider-usage floor while the anchor predates a successful compress - #259

Merged
ranxianglei merged 1 commit into
masterfrom
2026-08-31_floor-stale-anchor
Aug 31, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-08-31_floor-stale-anchor

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

fix(floor): skip the provider-usage floor while the anchor predates a successful compress

问题(#258 评审发现)

getContextUsage() 锚定最后一条带有效 usage 的 assistant 消息。#258 引入的 floor(tokenCount = max(sentTokens, realUsage.tokens))在压缩成功后的一轮会出问题:

assistant(usage: 175k / 97%)          ← 锚点
  └─ compress toolResult(成功)        ← 上下文刚刚变小
       └─ 下一个 LLM 调用              ← 仍以 175k floor → EMERGENCY

后果(压缩后的一个请求窗口内):

  1. 重跑 emergency nudge — 刚压缩完又注入"请压缩"提示;
  2. 机械截断工具结果 — emergencyTruncateNode 在 ≥95% 时无条件执行,把刚通过压缩保留下来的工具结果截成 2K+2K+marker(数据丢失);
  3. 面板/acp_status 永久显示压缩前的大数字 直到下一条 usage 到达。

下一条 assistant usage 到达即自愈,但窗口内的截断是不可逆的。

pi 自身的 compaction 有同样的检查(agent-session.js:"Verify the usage source is post-compaction"),本 PR 把对齐检查带到扩展的 floor 上。

修复

新增 src/floor-stale.ts:usageAnchorPredatesCompression(entries)

  • 锚点 = 最后一条满足 getAssistantUsage 规则的 assistant(role=assistant、stopReason 非 aborted/error、totalTokens 或分项之和 > 0);
  • 比较对象 = 最后一条成功的 compress toolResult(面板 >0 blocks;失败/isError/0-block no-op 不算 — 它们没有改变上下文);
  • 锚点早于它 → 本轮跳过 floor,用发送视图的 sentTokens。

三个调用点一致接入,保证决策与显示不分裂:

调用点 行为
src/index.ts context transform 跳过 floor(logInfo 一条 stale-anchor-skip),nudge/truncate 仲裁用 sentTokens
src/status-tool.ts acp_status 同上
src/commands.ts /acp 面板 同上 + 面板大数字 sessionTokens 不再取 stale usage

同时修正 src/index.ts 的 floor 注释:原文声称 realUsage "can't cause a false emergency",对不上报 usage 的 provider(tree-sum 回退)不成立 — 现如实记录为已接受取舍。

测试(+9,413 → 422 pass / 0 fail)

  • tests/floor-stale.test.ts(8 个单元用例):fresh anchor / stale anchor / 无 compress / 失败 compress 不算 / 0-block no-op 不算 / error·aborted·全零 usage 不算锚点 / 仅 totalTokens / 非 compress 工具结果忽略;
  • tests/sent-view-arbitration.test.ts(扩展级回归):19 条消息流 + usage: 175k 锚点 + 成功 compress toolResult + fakeCtx(175_000) → 0 nudge(对照组:无 usage → 0;floor 生效组:无 compress → nudge ≥1)。

验证:npm run typecheck ✅ · npm test 422/0 ✅ · npm run build ✅(518.85KB)

版本

0.1.52 → 0.1.53,CHANGELOG 已在 Unreleased 段记录。

已知取舍(评审记录,不在本 PR 范围)

  1. omp fix: inline typebox into dist (ACP tags/nudge silently disappeared) #18:provider 永不上报 usage 时,pi 回退 session 树求和(只增不减)→ floor 永久高位。上游问题,接受。
  2. 显示层 CJK:compress 面板与 kernel countTokens 回到未校准估算(密度系统已随 fix: run nudge/emergency decision on provider-reported prompt size (#257) #258 移除),CJK 低估 2–5×;决策层由 floor 兜底,/status 以 Provider-reported 为准。

关联

… successful compress

pi's getContextUsage() anchors on the last assistant message with valid
provider usage. A successful compress lands AFTER that anchor, so the next
LLM call floors the meter at the pre-compress request size and re-runs the
emergency decision (nudge + mechanical tool-result truncation) on context
that was just shrunk; the panel keeps showing the old large number too.

- new src/floor-stale.ts: usageAnchorPredatesCompression(entries) scans the
  branch for the last valid assistant usage anchor (same validity rule as
  pi's getAssistantUsage) vs the last successful compress toolResult
  (panel >0 blocks; failed/no-op compresses don't count)
- skip the floor while stale at all three processTurn sites (context
  transform, acp_status, /acp panel; the panel's sessionTokens number too)
- mirrors pi's own post-compaction check: "usage source must be
  post-compaction" (agent-session.js)
- fixes the floor comment in src/index.ts that claimed "can't cause a
  false emergency" (not true for usage-less providers' tree-sum fallback —
  now documented as the accepted omp #18 trade-off)
- docs: token-calibration-plan.md banner marks the density system removed
  in #258 and records the current design + accepted trade-offs
- tests: 8 unit cases + extension-level regression (97% usage anchor
  predating a successful compress -> no emergency nudge); 422 pass
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Extension Artifact

Branch: 2026-08-31_floor-stale-anchor (57d8042)

Option A — Install from npm PR tag (recommended)

pi install npm:billion-context-pi@pr-259

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pi-pr259.tgz
pi install ./package

This comment is automatically updated on each push.

@ranxianglei
ranxianglei merged commit 95c79df into master Aug 31, 2026
9 checks passed
HangxiangMa pushed a commit to HangxiangMa/billion-context-pi that referenced this pull request Sep 21, 2026
…stale-anchor

fix(floor): skip the provider-usage floor while the anchor predates a successful compress
HangxiangMa pushed a commit to HangxiangMa/billion-context-pi that referenced this pull request Sep 21, 2026
…acks

Fixes ranxianglei#401

- src/packs/* deleted (229 lines): types/sanitize/lean/builtin/dir all
  live in the kernel now (acp-kernel ranxianglei#259/ranxianglei#260, v0.0.66)
- src/prompt-pack.ts rewritten on kernel exports; external surface
  (packResolver/resolveActivePack/mergeSurface/readToolSurfaceWithPacks)
  unchanged
- pi-specific surface (PiPromptSections tri-state, tool extras,
  delegatePrompt) read from surface.adapters.pi and sanitized adapter-side;
  kernel treats it as opaque data
- lean assertions moved to kernel leanPack.adapters.pi (aligned recall
  rules from ranxianglei#399/ranxianglei#400)
- acp-kernel pinned 0.0.65 -> 0.0.66 (exact)
- 792 tests: 789 pass / 0 fail / 3 skipped (pre-existing); typecheck+build green
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.

1 participant