Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions docs/comparison/input-queue-deep-dive.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 输入队列与中断机制 Deep-Dive

> 当 AI Agent 正在执行工具调用时,用户能否继续输入?输入会被丢弃、阻塞,还是排队等待下一轮?本文基于 Claude Code(v2.1.89 反编译)和 Qwen Code(Gemini CLI fork,开源)的源码分析,深度对比两者在输入队列、中断机制和交互流畅性方面的设计差异。
> 当 AI Agent 正在执行工具调用时,用户能否继续输入?输入会被丢弃、阻塞,还是排队等待下一轮?本文基于 Claude Code(v2.1.89 反编译)和 Qwen Code(v0.15.0,Gemini CLI fork,开源)的源码分析,深度对比两者在输入队列、中断机制和交互流畅性方面的设计差异。

---

Expand Down Expand Up @@ -430,12 +430,19 @@ abort(): void { // 3. 立即终止

### 4.6 预测与预执行

| 能力 | Claude Code | Qwen Code |
| 能力 | Claude Code | Qwen Code(v0.15.0+) |
|------|------------|-----------|
| Prompt Suggestion | ✅ 预测下一步输入 | ❌ |
| Speculation | ✅ 预执行预测结果 | ❌ |
| Early Input | ✅ 启动阶段捕获键入 | ❌ |
| Tab 接受 | ✅ 预执行结果直接注入 | — |
| Prompt Suggestion | ✅ 默认开启 | ✅ 默认开启(`followupSuggestionsEnabled`) |
| Speculation | ✅(ant-only,`USER_TYPE === 'ant'`) | ✅ opt-in(`enableSpeculation: false` 默认关闭) |
| Overlay FS 隔离 | ✅ Copy-on-Write | ✅ Copy-on-Write(`/tmp/qwen-speculation/{pid}/`) |

Copilot AI Apr 3, 2026

Copy link

Choose a reason for hiding this comment

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

The Qwen Code Overlay FS path here uses /tmp/qwen-speculation/{pid}/, but later in the doc the path is /tmp/qwen-speculation/{pid}/{uuid}/. Please make these consistent (or explain the directory structure) to avoid confusing readers.

Suggested change
| Overlay FS 隔离 | ✅ Copy-on-Write | ✅ Copy-on-Write(`/tmp/qwen-speculation/{pid}/`|
| Overlay FS 隔离 | ✅ Copy-on-Write | ✅ Copy-on-Write(基目录:`/tmp/qwen-speculation/{pid}/`;每次 speculation 实例目录:`/tmp/qwen-speculation/{pid}/{uuid}/`|

Copilot uses AI. Check for mistakes.
| Tab 接受 → 结果注入 | ✅ 直接注入对话 | ✅ `acceptSpeculation()` → `addHistory()` |
| 工具安全分类 | ✅ `interruptBehavior` | ✅ `speculationToolGate.ts`(safe/write/boundary/unknown 4 类) |
| Pipelined Suggestion | ✅ speculation 完成后预生成下一个 | ✅ `generatePipelinedSuggestion()` |
| Early Input | ✅ 启动阶段 stdin 捕获 | ❌ |

> **重要变化**:Qwen Code v0.15.0(2026-04-03 合入 `#2525`)新增了完整的 follow-up suggestions + speculation 系统,架构与 Claude Code 高度相似。但 speculation 默认关闭,需手动启用。

Copilot AI Apr 3, 2026

Copy link

Choose a reason for hiding this comment

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

#2525 is wrapped in inline code, so GitHub won’t auto-link it, and it’s also ambiguous which repository it refers to (this repo vs qwen-code). Consider using a normal #2525/full link and explicitly naming the repo (e.g., qwen-code PR #2525).

Suggested change
> **重要变化**:Qwen Code v0.15.0(2026-04-03 合入 `#2525`)新增了完整的 follow-up suggestions + speculation 系统,架构与 Claude Code 高度相似。但 speculation 默认关闭,需手动启用。
> **重要变化**:Qwen Code v0.15.0(2026-04-03 合入 qwen-code PR #2525)新增了完整的 follow-up suggestions + speculation 系统,架构与 Claude Code 高度相似。但 speculation 默认关闭,需手动启用。

Copilot uses AI. Check for mistakes.
>
> 源码: `qwen-code/packages/core/src/followup/`(6 个文件,~1,900 行)、`qwen-code/packages/cli/src/config/settingsSchema.ts#L520-L544`

---

Expand Down Expand Up @@ -581,9 +588,22 @@ Claude Code 中断后队列**始终保留**(`messageQueueManager` 无 drain

> 源码: Qwen Code `AgentComposer.tsx#L88`(Escape → `cancelCurrentRound()`)

### 6.3 Speculation 零等待
### 6.3 Speculation 预执行

Claude Code 的 Speculation 系统在用户还未输入时就预测并预执行下一步。当用户按 Tab 接受建议时,结果已经准备好——体感上是**零延迟**。Qwen Code 无此机制。
两者现在都支持 Speculation 预执行,但启用状态不同:

| 维度 | Claude Code | Qwen Code |
|------|------------|-----------|
| 默认状态 | ant-only 自动启用 | **默认关闭**(`enableSpeculation: false`) |
| 启用方式 | `USER_TYPE === 'ant'` | 用户手动在 settings 中开启 |
| Overlay FS | 内存级覆盖层 | `/tmp/qwen-speculation/{pid}/{uuid}/` |
| 工具限制 | `interruptBehavior` 区分 | `speculationToolGate` 分 4 类(safe/write/boundary/unknown) |
| 最大 turn 数 | 无硬编码上限 | `MAX_SPECULATION_TURNS = 20` |
| Tab 接受路径 | 注入对话历史 | `acceptSpeculation()` → `addHistory()` 绕过队列 |
| Cache 共享 | prompt cache breakpoint 机制 | `saveCacheSafeParams()` 捕获 cache 参数 |

> Qwen Code 的 speculation 实现于 2026-04-03 合入(PR #2525),与 Claude Code 架构高度相似。

Copilot AI Apr 3, 2026

Copy link

Choose a reason for hiding this comment

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

This references “PR #2525” without specifying which repository. Since this doc lives in a different repo, please disambiguate (e.g., “qwen-code PR #2525”) and/or link to the PR for traceability.

Suggested change
> Qwen Code 的 speculation 实现于 2026-04-03 合入(PR #2525),与 Claude Code 架构高度相似。
> Qwen Code 的 speculation 实现于 2026-04-03 合入(qwen-code PR #2525),与 Claude Code 架构高度相似。

Copilot uses AI. Check for mistakes.
> 源码: `qwen-code/packages/core/src/followup/speculation.ts`(563 行)

---

Expand All @@ -592,7 +612,7 @@ Claude Code 的 Speculation 系统在用户还未输入时就预测并预执行
| Agent | 队列模型 | 执行中可输入 | Mid-Turn Drain | 优先级 | 中断粒度 | 预测/预执行 |
|-------|----------|:-----------:|:--------------:|:------:|----------|:----------:|
| **Claude Code** | 优先级队列 | ✅ | ✅ | 3 级 | 工具级 | ✅ |
| **Qwen Code** | FIFO 队列 | ✅ | ❌ | 无 | Round 级 | |
| **Qwen Code** | FIFO 队列 | ✅ | ❌ | 无 | Round 级 | ✅ opt-in |
| **Gemini CLI** | FIFO 队列 | ✅ | ❌ | 无 | Round 级 | ❌ |
| **Copilot CLI** | 无队列 | ⚠️ 无排队 | ❌ | — | 全局级 | ❌ |
| **Aider** | 无队列 | ⚠️ 无排队 | ❌ | — | 全局级 | ❌ |
Expand Down Expand Up @@ -626,6 +646,10 @@ Claude Code 的 Speculation 系统在用户还未输入时就预测并预执行
| `packages/core/src/utils/asyncMessageQueue.ts` | 54 | 通用 FIFO 队列 |
| `packages/core/src/agents/runtime/agent-core.ts` | ~540 | 推理循环(`runReasoningLoop`,无 mid-turn drain) |
| `packages/core/src/agents/runtime/agent-interactive.ts` | 512 | 交互代理(消息循环 + 三层取消) |
| `packages/core/src/followup/speculation.ts` | 563 | Speculation 引擎(v0.15.0 新增) |
| `packages/core/src/followup/suggestionGenerator.ts` | 367 | 建议生成 + 12 条过滤规则 |
| `packages/core/src/followup/overlayFs.ts` | 140 | Copy-on-Write overlay 文件系统 |
| `packages/core/src/followup/speculationToolGate.ts` | 146 | 工具安全分类(safe/write/boundary) |
| `packages/cli/src/ui/contexts/KeypressContext.tsx` | ~170 | Ink 键盘输入捕获 |

---
Expand All @@ -643,7 +667,7 @@ Claude Code 的 Speculation 系统在用户还未输入时就预测并预执行
### 对用户

- Claude Code 用户可以在 Agent 执行时**放心输入**——输入不会丢失,会自动成为下一轮
- Qwen Code 用户同样可以输入,但 `abort()` 后队列会被清空——避免在取消后依赖已排队的输入
- Speculation Claude Code 独有的"零等待"体验——但仅限 Anthropic 内部用户(`USER_TYPE === 'ant'`)
- Qwen Code 用户同样可以输入,但 `abort()` 后已排队输入被放弃(`drain()` 阻止新入队 + abort 信号使循环退出)——Escape 取消当前轮则队列保留
- Speculation 预执行:Claude Code 仅限 Anthropic 内部用户(`USER_TYPE === 'ant'`);Qwen Code v0.15.0 支持但默认关闭(需在 settings 中开启 `enableSpeculation`)

> **免责声明**: 以上分析基于 2026 年 Q1 源码,后续版本可能已变更。Qwen Code 为 Gemini CLI fork,其队列模型继承自 Gemini CLI。
> **免责声明**: 以上分析基于 2026 年 Q1 源码(Claude Code v2.1.89、Qwen Code v0.15.0 commit `3bce84d`),后续版本可能已变更。Qwen Code 为 Gemini CLI fork,其队列模型继承自 Gemini CLI,follow-up suggestions + speculation 为 Qwen Code 独立实现