Skip to content

feat(nudge): persist nudge injections as display-only session entries + single-line acp.log (closes #326) - #329

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-09-08_persist-nudge-records
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-09-08_persist-nudge-records

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #326.

Triage / verification

Changes

  • Persistence (src/index.ts): on nudge injection, append a compact one-line record (e.g. [ACP nudge] EMERGENCY 95% · T1 · top range m00120–m00168) via pi.appendEntry("acp-nudge", {text}). Deduped per user turn — emergency nudges re-inject on every LLM call until retry-capped and must not flood the session file. markNudgeRecorded runs only after a successful append (a failed append retries on the next fire). Recorded-state cleared at session start alongside nudge-shown tracking.
  • Renderer (src/index.ts): registerEntryRenderer("acp-nudge", ...) renders the record as a dim boxed line (pi-tui Box/Text, customMessageBg background). @earendil-works/pi-tui added as devDependency pinned exactly 0.83.0 (matching the host) and bundled inline by tsup — dist stays zero-runtime-deps (verified: no bare pi-tui import remains in the bundle).
  • Single-line log (src/log.ts): \r/\n in string fields (including Error.stack) are escaped to literals, so every acp.log entry occupies exactly one physical line — grep nudge-injected now returns the full nudge body instead of just the headline.
  • Docs: README Logging section (nudge audit trail + one-entry-per-line guarantee), CHANGELOG.

Tests (4 new; full suite green)

  • formatNudgeRecord unit — exact-string assertions matching the issue example (emergency/non-emergency, tier, missing ranges).
  • Integration — emergency overflow recipe (180K window vs ~270K sent view): renderer registered at factory time; exactly one persisted record per user turn across three context fires (emergency re-injection still reaches the model but must not duplicate the record); a new user message yields the next record.
  • Renderer unit — well-formed record → box with one text child; empty data → undefined (invisible but persisted).
  • log.test.ts — multi-line field values (nudge text + stack trace) stay on one tagged physical line.

Verification

  • npm run typecheck ✅
  • npm test ✅ — 466 pass / 3 skipped / 0 fail (469 total)
  • npm run build ✅ — dist 627 KB, pi-tui fully inlined
  • Smoke-loaded the built dist/index.js in plain node ✅

@ranxianglei

Copy link
Copy Markdown
Owner Author

分诊(队列清理 #484): KEEP-VALID(#326 根因解)。

缺口仍在:master 无 acp-nudge custom type(src/messages.ts:24 只有 ACP_STATUS_CUSTOM_TYPE),src/log.ts 的 fmt()(:22-30)不压多行。设计对路:经 pi.appendEntry 写 type:"custom" 不投影进发送视图,记录跨重启存活且不污染模型上下文。

rebase 风险:中等。 nudge 注入路径(src/index.ts)与 src/runtime.ts 自 9/8 起大变(live-only-tail #471、system-passthrough #478、turn-boundary);必须验证 src/state-rebuild.ts(log 回放重建,晚于本 PR 落地)会跳过 acp-nudge 条目。丢弃 PR 里的内核 pin bump hunk。

@github-actions

github-actions Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Extension Artifact

Branch: 2026-09-08_persist-nudge-records (f514d58)

Option A — Install from npm PR tag (recommended)

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

Each push to this PR publishes a new version under the pr-329 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-pr329.tgz
pi install ./package

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

Rebased onto current master (72d8771, v0.1.74) — new head 5f4fe20. Per triage, the package.json/lock hunks were dropped entirely: @earendil-works/pi-tui@0.83.0 is already a master devDep, and the acp-kernel pin stays at 0.0.81 (untouched).

Adaptation to post-9/8 master changes:

  • src/runtime.ts: nudge-recorded dedup now uses master's per-sid Map style — markNudgeRecorded(sid, turnKey) / nudgeRecordedFor(sid, turnKey), cleared alongside nudgeShownTurns in clearNudgeTracking(sid).
  • src/index.ts: persistence block sits at the current injection site (right after debug.event("nudge-injected")), reusing the existing emergency flag and the viable-filtered compressibleRanges; pi-tui imports merged with master's; wireNudgeRecords(pi) wired into the factory after command registration.

State-rebuild skip verification (the required check): rebuildStateFromLog / hasCompressHistory in src/state-rebuild.ts process only entries with entry.type === "message" (entryMessage() returns undefined for anything else) → acp-nudge custom entries are structurally ignored on log-replay rebuild. Every other entry-scanning module (cache-tool, commands, decompress-tool, export, live-only-tail, messages, search-index, tokens) carries the same guard. Additionally entriesToCoreMessages projects only custom_message entries (with acp-status/acp-export excluded), so plain type:"custom" nudge records never reach model context — no addition to CONTEXT_EXCLUDED_CUSTOM_TYPES needed.

Small fixes applied directly during the rebase:

  • Removed a duplicated let refused = false; introduced by the conflict merge (TS2451).
  • Kept the explicit registerEntryRenderer<AcpNudgeRecord>(...) type argument — pi 0.83.0's signature is generic (registerEntryRenderer<T = unknown>), so this gives typed entry.data.text access instead of unknown.

Evidence (all run on the rebased head):

  • npm run typecheck ✅ (strict)
  • Full suite: 872 tests → 787 pass / 3 skip / 82 fail — the failure set is byte-identical to a pristine-master baseline run (all 82 are pre-existing sandbox failures: those tests hardcode /tmp paths, which are read-only in this environment; they pass in CI). Zero regressions from this PR.
  • Targeted tests/nudge-record.test.ts + tests/log.test.ts: 10/10 pass (incl. the 3 new record-persistence tests and the single-line log test).
  • Build: dist/index.js 862 KB with pi-tui fully inlined (zero bare imports) — zero-runtime-deps property intact.

Ready for CI re-run and review.

中文摘要:已按分诊要求把 PR 变基到最新 master(v0.1.74),丢弃了依赖/pin 的 hunk(pi-tui 本就是 master devDep,acp-kernel 保持 0.0.81 不动),将 nudge 记录持久化适配到 per-sid Map 与 turn-boundary 之后的新注入点,并验证了 state-rebuild 日志回放会跳过 acp-nudge 条目(仅处理 type==="message",全部 10 个条目扫描模块同此);全量测试的失败集与干净 master 基线逐字节一致(82 个均为沙箱 /tmp 只读导致的既有失败,CI 中通过),零回归,typecheck/build/目标测试全绿,可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

这个目的是啥?改了啥 有啥影响 另外冲突

@ranxianglei
ranxianglei force-pushed the 2026-09-08_persist-nudge-records branch from 5f4fe20 to 6d975de Compare September 21, 2026 01:23
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

目的(对应 #326): nudge 注入(上下文超阈值时的压缩提醒)目前只走 ctx.ui.notify() 瞬时显示——pi 每次重启 chatContainer.clear() 后就全没了,无法事后重建"哪些 nudge 在何时触发过";同时 acp.log 里多行 nudge 条目只有第一物理行带标签,grep nudge-injected 只能看到标题、看不到 tier/breakdown/ranges。本 PR 让 nudge 持久化 + 可审计。

改了啥(8 个文件,+226/−7):

  1. src/index.ts:实际注入时(去重检查通过后)用 pi.appendEntry("acp-nudge", {text}) 写一条紧凑记录:[ACP nudge] EMERGENCY 95% · T1 · top range m00120–m00168;并注册一个 dim 盒子的 entry renderer,让它出现在 TUI 滚动历史里。
  2. src/runtime.ts:per-sid 的 nudgeRecordedTurns 集合做"每用户轮次一条"去重(emergency nudge 会重试上限前每次 LLM 调用都重发,不去重会刷屏 session 文件)。
  3. src/messages.ts:新增 ACP_NUDGE_CUSTOM_TYPE = "acp-nudge" 常量 + AcpNudgeRecord 接口。
  4. src/log.ts:字段格式化时转义 \r/\n → acp.log 每条严格占一行物理行,grep 自包含。
  5. 测试:新增 tests/nudge-record.test.ts(3 个:格式单测对照 issue 里的示例、集成测试断言 3 次触发含 emergency 重发也只落 1 条/轮、renderer 单测)+ tests/log.test.ts 单行测试。
  6. CHANGELOG/README 文档。

有啥影响:

  • 模型上下文零污染(构造上保证):appendEntry 写的是 type:"custom" 条目,而 entriesToCoreMessages 只投影 custom_message 进发送视图 → 记录永远到不了 LLM;也验证了 state-rebuild.ts 日志回放只处理 type:"message",会跳过它。正因为零上下文成本,持久化没有挂在 debug 开关后面。
  • 不复用 /acp 的 pi.sendMessage 先例的原因:流式期间 sendMessage 会降级成 steer(),等于劫持正在跑的 turn;appendEntry 是纯同步追加,无此问题。
  • 行为增量仅两处:TUI/session 文件里多一行只读记录、acp.log 变单行。无配置变更、无内核改动。
  • pi-tui 0.83.0 本就是 master 的 devDep,tsup 内联打包,dist 保持零运行时依赖(已验证 bundle 无裸 import)。
  • 验证:typecheck ✅ · 目标测试 10/10 ✅ · 全量 894 条测试失败集与干净 master 基线逐字节一致(88 个均为沙箱 /tmp 只读的既有失败,CI 中通过)· build ✅。

冲突: 上次推完 rebase 后 master 又前进了 28 个提交(#466 fork-ref-stability、#487 enabled-config、#464 terminal-escape 日志等,动了 src/index.ts/src/runtime.ts),所以 GitHub 标了 dirty。已再次变基到最新 master(68bbcd9),只有两处小冲突,均为简单并集合并(pi-tui import 行 ∪ 新增的 user-config import;runtime 返回对象补上 master 新加的 noteTerminalEscape/dropTerminalEscape/noteTruncationSkipped/dropTruncationSkipped + 本 PR 的 markNudgeRecorded/nudgeRecordedFor)。重新跑绿后已 force-push,新 head 6d975de,PR 现在应显示可合并,CI 重跑中。

一句话:把 nudge 注入持久化为只读 session 条目并让 acp.log 单行可 grep(#326),因 master 前进导致的冲突已二次变基解决并全量验证零回归,可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

冲突

@ranxianglei
ranxianglei force-pushed the 2026-09-08_persist-nudge-records branch from 6d975de to f514d58 Compare September 21, 2026 02:04
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

master 又前进了——上次推完(6d975de @ 基 68bbcd9)之后,#324(strip-historical-images,closes #321)合入,新 master 是 c323299;它正好也动了 src/index.ts/src/runtime.ts/CHANGELOG,所以又标了冲突。

已做第三次变基到 c323299:

  • 唯一冲突:src/runtime.ts 的返回对象单行——master 末尾加了 stripImagesFor,我们在 clearNudgeTokenStamps 后插了 markNudgeRecorded, nudgeRecordedFor,按并集合并(两边都保留)。其余文件全部自动合并。
  • 验证(变基后全跑):typecheck ✅ · nudge-record+log 目标测试 10/10 ✅ · 全量测试 902 条(我们的 head)vs 干净新 master 基线 898 条,失败集逐字节一致(88 个均为沙箱 /tmp 只读的既有失败,CI 中通过;多出的 4 条是本 PR 新增且全过)· diff 仍为 8 文件 +226/−7。
  • 新 head:f514d58,已 force-push,现在直接坐在最新 master 上。GitHub 重新计算后应显示可合并,CI 重跑中。

提醒:这个仓库最近 master 合入比较频繁,如果在你点合并前又有新 PR 落地,dirty 标记可能再次出现——再拍我一声即可秒级再变基,或者趁现在窗口期直接合并也行。

一句话:冲突是 master 又前进(#324 动了同一批热文件)所致,已第三次变基到最新 master 并解决唯一的并集型小冲突、全量验证零回归,可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

Superseded by #515.

Line-level comparison of the two PR diffs confirms #515 carries this exact change rebased onto current master (v0.1.76), plus:

  • README.zh-CN.md doc sync (required by AGENTS.md §7.3, missing here)
  • JSDoc-style comment for ACP_NUDGE_CUSTOM_TYPE (was //) in src/messages.ts

Closing to avoid double-merge risk from a stale base. Reopen if anything was lost in the rebase.

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] Persist nudge echoes as session entries (survive restarts, grep-able log)

1 participant