08 — TUI:自适应流分块、退出与 Shutdown
自适应流分块(Smooth / CatchUp)
实现位于 codex-rs/tui/src/streaming/chunking.rs,模块级注释完整描述了 策略:
- Smooth:每个 baseline tick 只消费一行队列,打字机感稳定。
- CatchUp:队列积压时一次性 drain,降低 尾延迟。
- 滞回:进入阈值高、退出阈值低 +
EXIT_HOLD/REENTER_CATCH_UP_HOLD,防止模式抖动;严重积压可绕过 re-entry hold。
1:27:codex-rs/tui/src/streaming/chunking.rs
//! Adaptive stream chunking policy for commit animation ticks.
//!
//! This policy preserves the baseline user experience while adapting to bursty
//! stream input. In [`ChunkingMode::Smooth`], one queued line is drained per
//! baseline commit tick. When queue pressure rises, it switches to
//! [`ChunkingMode::CatchUp`] and drains queued backlog immediately so display
//! lag converges as quickly as possible.
//!
//! The policy is source-agnostic: it depends only on queue depth and queue
//! age from [`QueueSnapshot`]. It does not branch on source identity or explicit
//! throughput targets.
//!
//! # Mental model
//!
//! Think of this as a two-gear system:
//!
//! - [`ChunkingMode::Smooth`]: steady baseline display pacing.
//! - [`ChunkingMode::CatchUp`]: full queue draining while backlog exists.
//!
//! The transition logic intentionally uses hysteresis:
//!
//! - enter catch-up on higher-pressure thresholds
//! - exit catch-up on lower-pressure thresholds, held for [`EXIT_HOLD`]
//! - after exit, suppress immediate re-entry for [`REENTER_CATCH_UP_HOLD`]
//! unless backlog is severe
//!
//! This avoids rapid gear-flapping near threshold boundaries.与前端类比:类似视频播放的 jank vs catch-up、或聊天列表在 高流量时关闭动画直接跳到最新。
调参文档:docs/tui-stream-chunking-tuning.md 给出症状 → 参数调整表;docs/tui-stream-chunking-validation.md 描述测量流程。
退出流:Shutdown-first 统一模型
docs/exit-confirmation-prompt-design.md 描述 TUI 退出语义,并指向 PR #8936 的历史与动机摘要:
AppEvent::Exit(ShutdownFirst):用户退出时先Op::Shutdown,等待清理完成。Exit(Immediate):紧急出口,可能丢弃在途任务与子进程清理。- Ctrl+C 优先级:活动 modal 先消费;有 cancellable 工作时先
Interrupt;空闲时双击才 quit。 /quit等 slash:明确意图,不二次确认 但仍走 shutdown-first。/new:只要 shutdown 抑制ShutdownComplete,不退出进程,仅换新会话。
设计原因:历史上混用「立即退出」与「先 shutdown」导致 状态泄漏与回归;统一模型后 可测试、可推理(文档列了最小测试期望)。
配置中的用户提示
docs/config.md 末尾:Ctrl+C/Ctrl+D 使用约 1 秒内的二次按键提示,与上述退出流一致。