Skip to content

feat: push/pop 上下文栈 + /digest 回溯蒸馏#1310

Open
universe-hcy wants to merge 5 commits into
claude-code-best:mainfrom
universe-hcy:feat/push-pop-digest
Open

feat: push/pop 上下文栈 + /digest 回溯蒸馏#1310
universe-hcy wants to merge 5 commits into
claude-code-best:mainfrom
universe-hcy:feat/push-pop-digest

Conversation

@universe-hcy

@universe-hcy universe-hcy commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

背景

使用agent进行代码开发,本质上是从模型已有的训练分布中,去泛化新任务的分布。在每个状态节点,agent会结合现有的上下文、网络搜索、以及开发者提供的上下文进行下一步的状态转移。agent与用户的协作的整个流程看作一棵启发式的搜索树,由于并非每次决策都是正确的,遇到问题时需要小范围的试错搜索。但目前现有的顺序结构的上下文方式只能追加、无法回溯,对于失败的搜索尝试会一直记录在上下文中,导致一次跑偏的讨论会永久留在上下文里稀释后续推理。
新增的/Push/Pop、/digest 为这棵树补上「回溯」能力:弹栈时只有蒸馏物穿过节点边界,展开过程中的中间态被丢弃,从而可以支持多分叉的讨论以及尝试。
由于回溯过程是采用上下文信息 对 回溯范围内容进行蒸馏,整个回溯流程以及回溯前后都是cache友好的(只有蒸馏出来的结果小部分内容cache不命中),相较/compact能够更加自主的进行上下文管理,并且成本更低。
同时,当前功能为额外的新增功能,在不使用时和以往完全没有影响。

总结

  • /push + /pop:继承完整上下文的讨论旁支,pop 时蒸馏成四栏 digest 回卷主线,
    支持嵌套、--to #N 跨层弹出、--list 零成本列栈、栈感知 auto-compact、跨会话持久化
  • /digest:与 /pop 平级的入口,事后在消息选择器里回溯选点,把选中消息到对话底部
    蒸馏成 digest(复用同一底层压缩原语 + DIGEST_PROMPT),不 resubmit
  • feature flag:PUSH_POP
  • 与rewind-summarize的对比:rewind-summarize本质上是"回到那点重来",产出的是泛化摘要,/digest和/pop 走四栏(Decisions / Rejected / Open questions / Action items),专门为"我刚探索完一条岔路或者子树,要把结论和被否掉的路留给主线"定制的。

Summary by CodeRabbit

  • New Features
    • Added /push and /pop for nested discussion branches, including --list, --to #N``, --discard, and `--keep-code`.
    • Added /digest to distill a rolled-back branch into Decisions, Rejected, Open questions, and Action items.
    • Persisted push/pop stacks across resume, restoring and projecting them into the active view.
    • Introduced a compaction strategy picker and improved spinner/feedback for these flows.
  • Documentation
    • Added detailed guides for push/pop context stacks and digest behavior.
  • Tests
    • Added automated coverage for parsing, projection, and command/digest push-pop interactions.

universe-hcy and others added 3 commits July 24, 2026 04:45
/push 开一个继承完整上下文的讨论旁支,/pop 把讨论蒸馏成结构化 digest
回卷主线——讨论噪音不污染主线、只留结论。支持嵌套(≤3)、--to #N 跨层弹出、
--list 零成本列栈,以及栈感知 auto-compact 三选确认(压到最近/指定 push 点/全量)。
纯增量、opt-in,feature flag PUSH_POP。

Co-Authored-By: claude-opus-4-8[1m] <noreply@anthropic.com>
- 栈随 transcript 持久化(last-wins push-stack entry),--resume 后仍能 /pop
- resume 时 projectPushStackOntoMessages 重新投影,失效标记透明丢弃并通知
- /push --list 预览改取用户最近指令,避免显示空/合成占位消息(No response requested.)
- 更新 push-pop-context-stack.md:跨会话持久化由非目标转为目标,同步预览语义
- /pop 时显示进度条以及前后上下文token数变化

Co-Authored-By: claude-opus-4-8[1m] <noreply@anthropic.com>
/digest 像 /rewind 一样弹消息选择器,让用户事后选一条较早消息,
把它到对话底部的上下文蒸馏成四栏 digest(复用 pop 的 DIGEST_PROMPT +
summaryFraming:'digest' + 局部压缩),不 resubmit。与 /pop 平级、
共享同一底层蒸馏原语。feature flag 复用 PUSH_POP。

Co-Authored-By: claude-opus-4-8[1m] <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds feature-gated /push, /pop, and /digest commands with nested stack state, digest-based partial compaction, stack-aware auto-compaction, interactive dialogs, and JSONL transcript persistence across resume flows.

Changes

Push/pop and digest workflows

Layer / File(s) Summary
Commands and stack contracts
scripts/defines.ts, src/commands/*, src/services/pushStack/state.ts, src/Tool.ts
Registers feature-gated commands, defines stack contracts, implements command parsing and validation, and adds related callbacks and tests.
Digest selector and partial compaction
src/components/MessageSelector.tsx, src/services/compact/*, src/services/pushStack/digestPrompt.ts, src/screens/REPL.tsx, docs/features/digest.md
Adds digest-mode selection, digest prompts and framing, partial-compaction options, token metrics, and non-resubmitting digest execution.
Push/pop orchestration and stack-aware compaction
src/screens/REPL.tsx, src/components/pushStack/*, src/services/compact/autoCompact.ts, docs/features/push-pop-context-stack.md
Adds push/pop callbacks, marker application and retention, compaction strategy selection, spinner state, stack-depth UI, and stack-aware automatic compaction.
Transcript persistence and resume projection
src/state/AppStateStore.ts, src/types/logs.ts, src/utils/sessionStorage.ts, src/utils/forkedAgent.ts, src/utils/__tests__/sessionStorage.test.ts
Stores push-stack state in application state and JSONL transcripts, restores last-wins stack entries, projects valid anchors on resume, and tests persistence behavior.

Estimated code review effort: 4 (Complex) | ~60 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 64.52% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main additions: push/pop context stack support and /digest distillation.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
src/services/pushStack/state.ts (1)

20-20: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Wrap the changed TypeScript lines to 80 columns.

  • src/services/pushStack/state.ts#L20-L20: wrap the marker-field documentation.
  • src/services/pushStack/state.ts#L53-L53: wrap the depth-limit documentation.
  • src/state/AppStateStore.ts#L227-L227: wrap the push-stack documentation.
  • src/commands.ts#L173-L173: wrap the push command dynamic import assertion.
  • src/commands.ts#L178-L178: wrap the pop command dynamic import assertion.
  • src/commands.ts#L183-L183: wrap the digest command dynamic import assertion.
  • src/commands/digest/index.ts#L5-L5: wrap the command description.
  • src/commands/pop/pop.ts#L53-L53: wrap the no-stack message.
  • src/commands/push/push.ts#L22-L22: wrap the preview documentation.

As per coding guidelines, use 80-character width for other files.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/services/pushStack/state.ts` at line 20, Wrap the changed TypeScript
lines to 80 columns without changing behavior: update the marker and depth-limit
documentation in src/services/pushStack/state.ts (lines 20 and 53), push-stack
documentation in src/state/AppStateStore.ts (line 227), dynamic import
assertions in src/commands.ts (lines 173, 178, and 183), the digest command
description in src/commands/digest/index.ts (line 5), the no-stack message in
src/commands/pop/pop.ts (line 53), and preview documentation in
src/commands/push/push.ts (line 22).

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/commands/pop/pop.ts`:
- Around line 12-31: Update parsePopArgs handling for both --to and --to= forms
to validate the entire marker value before conversion, rejecting inputs such as
2junk and 1.5 while continuing to accept valid optional-# positive integers.
Preserve the existing invalid-marker error messages and assignment to opts.to
for valid values.

In `@src/screens/REPL.tsx`:
- Around line 2376-2382: Update the resume-time notification in the push-stack
recovery flow, identified by the key “push-stack-resume-dropped,” to use English
text consistent with the sibling push/pop notifications in REPL. Preserve the
existing droppedCount interpolation, priority, timeout, and notification
behavior.

In `@src/services/compact/autoCompact.ts`:
- Around line 449-465: Update the fallback notification logic after partial
compaction failure or an unavailable chosen marker so non-interactive
fallback-to-full paths also notify through toolUseContext.addNotification.
Preserve the existing distinct messages for interactive partial failure and
explicit full compaction, while ensuring every path that removes all push
markers emits a notification.

---

Nitpick comments:
In `@src/services/pushStack/state.ts`:
- Line 20: Wrap the changed TypeScript lines to 80 columns without changing
behavior: update the marker and depth-limit documentation in
src/services/pushStack/state.ts (lines 20 and 53), push-stack documentation in
src/state/AppStateStore.ts (line 227), dynamic import assertions in
src/commands.ts (lines 173, 178, and 183), the digest command description in
src/commands/digest/index.ts (line 5), the no-stack message in
src/commands/pop/pop.ts (line 53), and preview documentation in
src/commands/push/push.ts (line 22).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: d64daa1c-9804-4797-983e-51bf1a6a3335

📥 Commits

Reviewing files that changed from the base of the PR and between 34b3dc9 and 5b1a67c.

📒 Files selected for processing (27)
  • README.md
  • docs/features/digest.md
  • docs/features/push-pop-context-stack.md
  • scripts/defines.ts
  • src/Tool.ts
  • src/commands.ts
  • src/commands/__tests__/pushpop.test.ts
  • src/commands/digest/digest.ts
  • src/commands/digest/index.ts
  • src/commands/pop/index.ts
  • src/commands/pop/pop.ts
  • src/commands/push/index.ts
  • src/commands/push/push.ts
  • src/components/MessageSelector.tsx
  • src/components/pushStack/CompactStrategyDialog.tsx
  • src/main.tsx
  • src/screens/REPL.tsx
  • src/services/compact/autoCompact.ts
  • src/services/compact/compact.ts
  • src/services/compact/prompt.ts
  • src/services/pushStack/digestPrompt.ts
  • src/services/pushStack/state.ts
  • src/state/AppStateStore.ts
  • src/types/logs.ts
  • src/utils/__tests__/sessionStorage.test.ts
  • src/utils/forkedAgent.ts
  • src/utils/sessionStorage.ts

Comment thread src/commands/pop/pop.ts
Comment thread src/screens/REPL.tsx
Comment thread src/services/compact/autoCompact.ts Outdated
universe-hcy and others added 2 commits July 24, 2026 06:55
- parsePopArgs 用正则严格校验整数,拒绝 2junk/1.5 等被 parseInt 静默截断的非法序号
- 非交互会话 partial 压缩失败回退 full 时补发通知,避免 push 标记被静默丢弃
- resume 时 push 点失效通知统一为英文,与兄弟通知一致
- 补充 2junk/1.5 回归测试

Co-Authored-By: claude-opus-4-8[1m] <noreply@anthropic.com>
给 CodeRabbit docstring 覆盖率检查涉及的顶层 handler 补齐 JSDoc:
push/pop/digest 三个命令的 call、REPL 的 openMessageSelector、
askCompactStrategy(行注释转 JSDoc)、onAbort。

Co-Authored-By: claude-opus-4-8[1m] <noreply@anthropic.com>
@universe-hcy

Copy link
Copy Markdown
Contributor Author

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 9420d6e3-f545-4215-acd0-89c4a98fb53c

📥 Commits
Reviewing files that changed from the base of the PR and between a322dc7 and a143a40.

📒 Files selected for processing (4)

  • src/commands/digest/digest.ts
  • src/commands/pop/pop.ts
  • src/commands/push/push.ts
  • src/screens/REPL.tsx

🚧 Files skipped from review as they are similar to previous changes (4)

  • src/commands/digest/digest.ts
  • src/commands/push/push.ts
  • src/commands/pop/pop.ts
  • src/screens/REPL.tsx

📝 Walkthrough

Walkthrough

Adds feature-gated /push, /pop, and /digest commands with nested stack state, digest-based partial compaction, stack-aware auto-compaction, interactive dialogs, and JSONL transcript persistence across resume flows.

Changes

Push/pop and digest workflows

Layer / File(s) Summary
Commands and stack contracts
scripts/defines.ts, src/commands/*, src/services/pushStack/state.ts, src/Tool.ts Registers feature-gated commands, defines stack contracts, implements command parsing and validation, and adds related callbacks and tests.
Digest selector and partial compaction
src/components/MessageSelector.tsx, src/services/compact/*, src/services/pushStack/digestPrompt.ts, src/screens/REPL.tsx, docs/features/digest.md Adds digest-mode selection, digest prompts and framing, partial-compaction options, token metrics, and non-resubmitting digest execution.
Push/pop orchestration and stack-aware compaction
src/screens/REPL.tsx, src/components/pushStack/*, src/services/compact/autoCompact.ts, docs/features/push-pop-context-stack.md Adds push/pop callbacks, marker application and retention, compaction strategy selection, spinner state, stack-depth UI, and stack-aware automatic compaction.
Transcript persistence and resume projection
src/state/AppStateStore.ts, src/types/logs.ts, src/utils/sessionStorage.ts, src/utils/forkedAgent.ts, src/utils/__tests__/sessionStorage.test.ts Stores push-stack state in application state and JSONL transcripts, restores last-wins stack entries, projects valid anchors on resume, and tests persistence behavior.
Estimated code review effort: 4 (Complex) | ~60 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 64.52% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main changes: a push/pop context stack plus /digest distillation support.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)

  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

看起来CodeRabbit 的 tree-sitter 解析器把每一个箭头函数都识别成一个独立的"function 节点",这里的箭头函数和c++的lambda函数差不太多。从代码简洁度来说,一般不会给lambda函数添加注释。因为lambda函数简洁同时语意也非常明确,添加注释就类似于给都能辨认出来是猫的图片再注释一个猫。

不过这里如果需要做添加注释来补齐整个的覆盖率的话,我这里再额外添加一下。

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

@universe-hcy 理解并同意。简短且语义清晰的箭头函数/lambda 不应为了覆盖率而添加重复性注释;这种指标更适合衡量公开 API、复杂控制流和关键业务逻辑的文档完整性。

如果补齐阈值是合并要求,建议优先为 /push/pop/digest 的入口、状态投影/持久化以及压缩策略等非显而易见的逻辑补充 JSDoc,而不必逐个给简单回调添加“显而易见”的注释。已记录这一上下文,后续会将该类覆盖率告警视为质量信号而非绝对要求。

🐇💭


✏️ Learnings added
Learnt from: universe-hcy
URL: https://github.com/claude-code-best/claude-code/pull/1310

Timestamp: 2026-07-25T03:06:37.694Z
Learning: In this TypeScript repository, docstring coverage tooling may count each arrow function as an individual function node. Concise, self-evident arrow functions/lambdas generally should not receive redundant comments solely to satisfy coverage metrics; prioritize documentation for public APIs, complex control flow, and non-obvious business logic.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

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