Skip to content

fix: acp_status derives ranges/nudge from live state, not the prepare-time snapshot (#389) - #397

Merged
ranxianglei merged 2 commits into
masterfrom
2026-08-31_acp-status-live-ranges
Sep 6, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-08-31_acp-status-live-ranges

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Aug 31, 2026 •

Copy link
Copy Markdown
Owner

Problem (#389)

acp_status stitched two freshness levels into one report: the base report read live ctx.session.state, while the appended "Compressible ranges" + "Nudge:" lines read ctx.nudge — a snapshot frozen at request-prepare time and never re-assigned inside the loop. After a successful compress, the same-turn acp_status still listed the just-compressed range as compressible with the stale nudge value, inviting duplicate/mis-targeted compress calls.

Fix (issue option 1)

  • New src/acp-status.ts — shared handleAcpStatus(args, ctx). After the base report (and the existing scope= short-circuit, which already carried no stale data), it re-runs the kernel's pure processTurn over ctx.session.state + ctx.messages (verified: every pipeline node returns new objects; syncBlocks deep-copies state) with tokenCount = session.stats.lastInputTokens — the same estimate the next prepare uses — and derives compressibleRanges (through the same one-shot viableRanges filter the prepare path applies) + protectedRanges + the Nudge line from the fresh decision. turn.state is intentionally NOT adopted, so nudge cadence bookkeeping is untouched; a recompute failure degrades to the base-only report rather than serving stale data.
  • Converged all three dispatcher copies (issue acceptance Use kernel renderTags:"text-only" + forbid tag echoing in prompt #3): src/stream.ts (executeAnthropicProxyTool) and src/compress-loop-responses.ts (executeProxyTool) previously called bare buildStatusReport and ignored every option — they now delegate to the shared handler, so all protocols emit the same output shape and honor scope/view/tool/sort/limit.
  • Removed LoopCtx.nudge and its setters (src/server.ts, src/plugin.ts) — the frozen snapshot can no longer leak into the report, and the plugin tool path gets the live behavior for free.

Tests (tests/acp-status.test.ts, 3 new)

  1. Acceptance fix: address all code review findings (condense, routing, session-id, dead code) #2: state change (real applyRanges compress) → immediate handleAcpStatus with NO new prepare → compressed refs absent from "Compressible ranges", b1 active, Nudge line state-derived.
  2. scope= short-circuit: base-only, no ranges/Nudge section.
  3. Acceptance fix: keep system prompt byte-stable for prefix cache #1 end-to-end through runCompressLoop + responses adapter: compress + acp_status in the same round — the acp_status marker no longer lists the just-compressed range.

Pre-flight

  • npm run typecheck — clean
  • npm test — 871/871 pass (868 existing + 3 new)
  • npm run build — success
  • E2E (tests/e2e/e2e-codex.test.ts) could NOT run in this environment (no codex binary / no local upstream); CI manual-dispatch e2e is available. The loop-level same-turn test above covers the changed pipeline path.

Also fixed (commit 1a1178d): the plugin /acp panel path

handlePluginStatus (src/plugin.ts) built the panel from the nudge remembered at the last model request (prepare-time snapshot). A compress executed through the plugin tool API mutates session.state without re-running prepare, so the panel mixed live blocks with stale, already-consumed ranges. The panel now recomputes the nudge on every status request (processTurn over the remembered pre-fold view against the CURRENT session.state; on failure the nudge/ranges sections are omitted instead of falling back to the stale snapshot). server.ts passes { core, config, log } as deps. New e2e in tests/plugin-protocol.test.ts ("plugin status panel recomputes the nudge after a mid-turn compress") fails pre-fix.

Pre-flight after the panel fix: typecheck clean · 1069 tests, 1067 pass (2 known env fails in plugin-agent.test.ts, unrelated) · build success.

@github-actions

github-actions Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-08-31_acp-status-live-ranges (1a1178d)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-397

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

Option B — Download artifact

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

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · vllm-qwen/qwen3.8-27b

[bot] 🏷 Reviewed PR #397 against master — single commit d498f89, 7 files, +191/−38, no version bump (content commit, correct). Pre-flight all green: typecheck clean, npm test 871/871, build success.

Root cause & fix — verified correct. acp_status mixed two freshness levels: the base report read live ctx.session.state, but the appended "Compressible ranges" + "Nudge:" lines read ctx.nudge, a snapshot frozen at prepare time and never refreshed inside the loop. After a same-turn compress, the snapshot still listed the just-compressed range. The fix re-runs the kernel's pure processTurn over live session.state + messages and derives ranges/nudge from the fresh decision.

Key claims I independently verified:

  • processTurn purity (the load-bearing assumption). Ran an empirical probe: seeded a state with a block + nudge anchors + stats, snapshotted it, called processTurn, and confirmed the input state is byte-identical afterward (no in-place mutation) and that turn.state, turn.state.blocks, turn.state.nudge are all fresh objects. So intentionally NOT adopting turn.state in handleAcpStatus is safe — no state corruption. syncBlocks deep-copies blocks/refs/nudge/stats and every pipeline node returns a new io. The stale comment at server.ts:1154 ("processTurn mutates state") is inaccurate; the prepare path itself proves purity by doing session.state = turn.state (server.ts:1318) — a no-op if it mutated in place.
  • tokenCount matches prepare. handleAcpStatus uses session.stats.lastInputTokens, same as prepare (server.ts:1316) — the recompute sees the same pressure the next prepare would.
  • viableRanges filter consistency. The recompute applies viableRanges(nudge.compressibleRanges), identical to the prepare path (server.ts:1325/1419/1572) and preflight (preflight.ts:263).
  • Causal chain. applyRanges adopts the compression via ctx.session.state = res.state (stream.ts:251), so the same-turn re-run sees the new block and drops the compressed refs from the compressible set.
  • Dispatcher convergence (acceptance Use kernel renderTags:"text-only" + forbid tag echoing in prompt #3). All three — stream.ts executeAnthropicProxyTool, compress-loop-responses.ts executeProxyTool, loop/core.ts executeProxyTool — now delegate to the shared handleAcpStatus, so every protocol emits the same shape and honors scope/view/tool/sort/limit. (Previously the stream + responses copies called bare buildStatusReport and ignored all options.)
  • LoopCtx.nudge removal is complete. Gone from the interface + both construction sites (server.ts, plugin.ts). TS excess-property checking on those object literals means any leftover nudge: would have failed typecheck — it didn't. The only remaining .nudge in the loop path is the fresh turn.nudge in acp-status.ts (the intended one).

Tests — the 3 new cases in tests/acp-status.test.ts are well-targeted: (1) real applyRanges compress → same-turn handleAcpStatus with no new prepare → compressed refs gone, b1 active, nudge state-derived; (2) scope= short-circuit → base-only, no ranges/nudge; (3) end-to-end through runCompressLoop + responses adapter, compress + acp_status in one round. Good coverage of the changed pipeline path.

Minor (non-blocking):

  • The recompute catch in handleAcpStatus is silent. Graceful degradation to base-only is the right call, but a loggerLog("warn", …) would help debug a systematic recompute failure. AcpStatusCtx has no log field, so it'd need the logger import — optional, and the codebase already has other silent catches (e.g. the nudge-render catch in server.ts).

Follow-up (correctly scoped out): buildStatusPanel (plugin.ts) still reads the prepare-time nudge snapshot for the UI panel — same staleness class, different surface. Agree a separate issue is the right call.

Verdict: Correct, minimal, well-tested, and addresses the root cause rather than the symptom. Ready to merge. I can't merge PRs (human-only) — please merge when happy: #397

…-time snapshot (#389)

# Conflicts:
#	src/compress-loop-responses.ts
#	src/loop/core.ts
#	src/stream.ts
@ranxianglei
ranxianglei force-pushed the 2026-08-31_acp-status-live-ranges branch from 48f503b to 31d24e0 Compare September 6, 2026 06:42
@ranxianglei

Copy link
Copy Markdown
Owner Author

已 rebase 到当前 master(v0.1.87 6820e7d)并消解冲突。

冲突消解说明

base 25e441e 之后 master 有两处演进与此 PR 相撞,均已合并保留:

  1. acp_status 会计缺陷:tool 结果全计为 text + 同一报告两套互斥的 tool 定义 + 双估计器混印不标尺 + pct 地板 #386 估计器统一(v0.1.87 / PR#567):master 把 acp_status 基础报告从 estimateTokensFast 换成了 kernel 的 defaultCountTokens(CJK-aware)。本 PR 抽出的 src/acp-status.ts 沿用了当时的 estimateTokensFast,rebase 时已同步改为 defaultCountTokens,与 master 口径一致。
  2. handleAcpStatus 函数体演进:master 在函数里加了 preCompactionArchiveOf 的 PRE-COMPACTION ARCHIVE 区段(v0.1.75 / PR#421)——本 PR 抽出的 src/acp-status.ts 基于 25e441e,已包含该区段,直接保留抽出版本;master 侧 src/loop/core.ts 的原函数删除(移入 src/acp-status.ts),三处 dispatcher(loop/core.ts / stream.ts / compress-loop-responses.ts)统一改走 handleAcpStatus(args, ctx)。

Pre-flight

  • typecheck ✓
  • tests/acp-status.test.ts 3/3 ✓
  • 全量 1066/1068(2 个已知 plugin-agent 环境失败,与 master 基线一致)✓
  • build ✓

显示恢复真实规模:7 files, +204/-48(此前 GitHub 锚在老 base 上显示为 dirty)。

Closes #387

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] 🏷 已对 rebase 后的分支重新审核(31d24e0,基于 v0.1.87 6820e7d;merge-base = 当前 master HEAD,单提交、无 version 改动)。结论:两处冲突消解均正确,可合并。

冲突消解核验(逐行对照 master 原版 loop/core.ts::handleAcpStatus)

把抽取版 src/acp-status.ts 与 master 原函数逐行比对,除预期改动外无遗漏:

  1. acp_status 会计缺陷:tool 结果全计为 text + 同一报告两套互斥的 tool 定义 + 双估计器混印不标尺 + pct 地板 #386 估计器统一:base 报告用 buildStatusReport(..., defaultCountTokens, ...)(src/acp-status.ts:31),与 master 口径一致——master 原函数同样用 defaultCountTokens,rebase 时同步到位 ✓
  2. PRE-COMPACTION ARCHIVE 区段:preCompactionArchiveOf(ctx.session) 区段(src/acp-status.ts:56-64)从 master 原函数逐字保留,且放在 try/catch 之外——现算失败降级 base-only 时该信息仍会附加,比塞进 try 内更稳 ✓
  3. 区块顺序 Nudge → formatRanges → Archive,与 master 完全一致,输出形状不变 ✓
  4. 一处必要的额外改动:新代码对现算出的 nudge.compressibleRanges 套了 viableRanges()(:46)。这是必须的而非多余——master 里读的是 prepare 阶段就已过滤好的快照(server.ts 在 prepare 时过滤),而这里走的是全新 processTurn 输出、未经预过滤,所以必须自己过一遍,才能在 live 数据上保持与 prepare 相同的有效语义。判断正确。

收敛 & 清理(rebase 后仍完整)

  • 三处 dispatcher 全部委托共享 handler:stream.ts:47 / compress-loop-responses.ts:104 / loop/core.ts:151 均 return handleAcpStatus(args, ctx);定义唯一在 src/acp-status.ts:25。
  • buildStatusReport 现在只有 src/acp-status.ts import(证明三处不再裸调);loop/core.ts 已无任何 nudge 残留。

Pre-flight(独立复跑)

  • typecheck ✓
  • build ✓
  • 全量 1067/1068:唯一失败是 resolveClientCommand: codex/claude resolve to themselves(launcher 环境测试,依赖真实 CLI 二进制)。我在 master 上复跑同一条同样失败(master 1064/1065),确认是基线既有失败、与本 PR 无关;本 PR 新增 3 条 acp-status 测试全绿(tests/acp-status.test.ts 3/3),未引入任何新失败。(你那边 1066/1068 的差异是多一条 launcher 环境测试在你机器上没满足二进制条件,属环境方差,非代码问题。)

一个小提醒(非阻塞)

这条写的是 Closes #387,但本 PR 实际修的是 #389(已 CLOSED,其 floor 3 明确指向本 PR);而 #387 是另一个独立 bug(compress 批量门伪造 "a prior compress renumbered" 文案,已在 acp-kernel PR #175 修掉),本 PR 并不触碰它。建议把合并关键字改成 #389(或删掉该行),避免合并时误关无关的 #387。

综上,代码层面 LGTM;按流程合并由人工执行。

handlePluginStatus fed buildStatusPanel the prepare-time snapshot
(mem.nudge) remembered at the last model request. A compress executed
through the plugin tool API mutates session.state without re-running
prepare, so between compress and the next request the panel mixed live
blocks with stale, already-consumed ranges (#387).

Recompute the nudge on every panel request via processTurn over the
remembered pre-fold view against the CURRENT session.state; on failure
omit the nudge/ranges sections instead of falling back to the stale
snapshot.

server.ts passes { core, config, log } to handlePluginStatus; callers
in tests updated for the new deps argument. New e2e in
plugin-protocol.test.ts fails pre-fix (panel kept recommending the
just-compressed m00001..m00002 range).
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