Skip to content

fix(chat): 自动压缩时显示压缩状态与「停止压缩」,而非「正在等待模型」 - #963

Closed
jokinas wants to merge 1 commit into
agegr:mainfrom
jokinas:fix/compaction-status-and-control
Closed

jokinas wants to merge 1 commit into
agegr:mainfrom
jokinas:fix/compaction-status-and-control

Conversation

@jokinas

@jokinas jokinas commented Sep 24, 2026

Copy link
Copy Markdown

自动压缩会话可能持续一段时间,这期间聊天界面显示「正在等待模型...」,且只提供一个通用的「停止」按钮 —— 用户会以为模型卡住了,而且拿不到正确的控制项。

Bug 原因

agentPhase 在发送 prompt 时被设一次(handleSend 设为 waiting_model),压缩开始时没有任何代码更新它。而 phaseLabel() 只读 phase,于是继续显示过期的 waiting_model。

压缩按钮的问题更严重,它的外层守卫是:

{!isStreaming && onCompact && ( ... )}
  • 手动压缩:从空闲会话发起 → !isStreaming 成立 → 按钮本来就渲染着 → isCompacting 变 true 时切换成「停止压缩」✅
  • 自动压缩:发生在回合进行中 → agentRunning 为 true → sessionBusy 为 true → isStreaming 为 true → 按钮从未渲染,只剩通用「停止」❌

所以手动压缩看起来正常、自动压缩不正常——这也是它一直没被发现的原因。

顺带一提,那个守卫和按钮自己的 disabled={isStreaming && !isCompacting} 是互相矛盾的:后者只有在这个按钮本就该能在 streaming 时出现才讲得通。

修复

1. 状态文字:phaseLabel() 增加 isCompacting 参数,压缩状态优先于 phase。渲染条件改为 (agentPhase || isCompacting),压缩时即使 phase 为空也显示状态。

2. 控制按钮:守卫改为 (!isStreaming || isCompacting),使控制在回合进行中可达。两个动作都保留:

按钮 命令 效果
停止压缩 abort_compaction 跳过压缩,回合继续
停止 abort 中止整个回合

3. 清理死代码:isStreaming && !isCompacting 状态在新守卫下不可达,对应的 disabled / 半透明 opacity / not-allowed 光标 / hover 提前 return 全部移除,而不是留着误导后人。

4. phaseLabel() 移到 lib/chat-phase-label.ts:在测试里 import ChatWindow.tsx 会拖进 next/image 和整个组件树,node 环境加载不了,所以抽成纯模块才能做真正的行为测试。

使用场景

长会话上下文接近上限,模型自动触发压缩:

修复前

状态:正在等待模型...        ← 误导,以为卡死
按钮:[停止]                 ← 点了会中止整个回合,不是只跳过压缩

修复后

状态:正在压缩…              ← 明确告知在做什么
按钮:[停止压缩] [停止]      ← 可只跳过压缩,也可中止整个回合

空闲态和手动压缩的 UI 完全不变。

验证

  • tsc --noEmit:本 PR 0 错(剩余 2 个是 main 上已有的 SDK 类型漂移)
  • npm run lint:干净
  • npm test:1308 pass / 4 fail —— 同样这 4 个在 upstream main 上就失败
  • 5 个新测试:各 phase 的状态文字、压缩优先、控制按钮守卫在回合进行中可达

…to-compaction

Auto-compaction can hold a turn for a while before the next token arrives.
While it runs, the chat UI reported "Waiting for model..." and offered only a
generic Stop button, which reads as a hang and gives the user the wrong control.

## What was wrong

`agentPhase` is set once when the prompt is sent (handleSend sets
`waiting_model`) and nobody updated it when compaction began. `phaseLabel()`
only looked at the phase, so it kept reporting the stale `waiting_model`.

The compaction control was worse. It is guarded by:

    {!isStreaming && onCompact && ( ... )}

Manual compaction starts from an idle session, so the button is already
rendered and simply flips to "Stop compaction" when `isCompacting` turns true.
Auto-compaction starts *mid-turn*, where `sessionBusy` is already true, so the
button never rendered at all and only the generic Stop remained. That guard
also contradicted the button's own `disabled={isStreaming && !isCompacting}`,
which only makes sense if the button is meant to be reachable while streaming.

Manual compaction therefore looked correct and auto-compaction did not, which
is why this went unnoticed.

## What changed

`phaseLabel()` takes `isCompacting` and returns the compaction label ahead of
the phase, so the status names the activity that is actually blocking the turn.
The render condition accepts a missing phase while compacting.

The compaction control guard becomes `(!isStreaming || isCompacting)`, so the
control is reachable mid-turn. Both actions stay available: "Stop compaction"
sends `abort_compaction` and skips the compaction so the turn continues, while
Stop sends `abort` and drops the whole turn. The now-unreachable
"streaming but not compacting" branches (disabled, dimmed opacity, not-allowed
cursor, the hover early-return) are removed rather than left as dead code.

`phaseLabel()` moved to lib/chat-phase-label.ts so it can be unit tested;
importing ChatWindow.tsx in a test pulls in next/image and the whole component
tree, so it cannot be loaded in node.

## Result

| | before | after |
|---|---|---|
| auto-compaction status | Waiting for model... | Compacting… |
| auto-compaction control | Stop | Stop compaction (+ Stop) |
| idle / manual | unchanged | unchanged |

## Verification

- tsc: 0 errors from this change (the 2 remaining are pre-existing SDK drift)
- lint: clean
- tests: 1308 pass / 4 fail — the same 4 fail on upstream main
- 5 new tests: phase label per phase, compaction precedence, and the control
  guard reaching mid-turn

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks @jokinas! The bug is real: auto-compaction starts mid-turn, so the status stayed on "Waiting for model…" and the !isStreaming guard hid Stop compaction.

One change was needed, and I couldn't push to your fork, so it continues in #1008. Your commit is kept as is. The edits to demo/components/* are reverted: the demo resolves @/lib against demo/lib, which has no chat-phase-label module, so they would break the Pages build. The demo copies are synced separately.

Closing in favor of #1008.


Generated by Claude Code

@agegr agegr closed this Sep 30, 2026
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
…#907 agegr#941 agegr#946 agegr#961 agegr#968 agegr#969 agegr#1007 agegr#1008 agegr#1009 agegr#1010)

上游已把我们提前摘取的 agegr#884 / agegr#871 / agegr#996 / agegr#961 原样合并(agegr#961 逐字节相同),
agegr#967 与 agegr#963 由上游的 agegr#1009 / agegr#1008 接管。

冲突解决:
- demo/ 对齐上游 fe288c0,撤销 agegr#963 对 demo 的两处改动(`@/lib/chat-phase-label`
  在 demo/lib 下不存在,会打断 Pages 构建)。
- AGENTS.md:保留我们的 default-project 条目,open-folder 换成上游的
  open-in-explorer。
- MessageView.tsx / SessionSidebar.tsx / FileExplorer.tsx:以上游为准,
  DismissButton 采用上游的独立文件,保留我们的思考卡片与文件管理改动。
- SessionSidebar 移除引用已删函数的旧 agegr#951 工作区按钮(agegr#951 的其他入口另行收尾)。
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
agegr#967 / agegr#963 已由上游的 agegr#1009 / agegr#1008 落地,agegr#961 上游原样合并(逐字节相同),
agegr#951 已收敛到上游的 agegr#907,agegr#960 上下游都拒绝——这些都与上游一致,按台账规则删行。
agegr#899 上游关闭但本地保留,状态注明为永久分叉。
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.

2 participants