Skip to content

feat: add input queue & interrupt mechanism deep-dive#29

Merged
wenshao merged 5 commits into
mainfrom
feat/input-queue-deep-dive
Apr 3, 2026
Merged

feat: add input queue & interrupt mechanism deep-dive#29
wenshao merged 5 commits into
mainfrom
feat/input-queue-deep-dive

Conversation

@wenshao

@wenshao wenshao commented Apr 3, 2026

Copy link
Copy Markdown
Owner

Summary

  • New comparison article: docs/comparison/input-queue-deep-dive.md (507 lines, 16 source refs)
  • Deep-dives into how Claude Code and Qwen Code handle user input during agent execution
  • Based on decompiled Claude Code v2.1.89 + Qwen Code open source analysis

Key Findings

Dimension Claude Code Qwen Code
Queue model Priority queue (now/next/later) Simple FIFO
State machine 3-state QueryGuard (idle/dispatching/running) Boolean processing flag
Interrupt granularity Tool-level (interruptBehavior) Round-level (cancelCurrentRound)
Queue visualization Visible below prompt, Esc to edit Not visible
Post-cancel queue Preserved drain() clears all
Prediction Speculation pre-execution None
Early input Startup stdin capture None

Article Structure (8 sections)

  1. Problem definition — 5 input handling strategies
  2. Claude Code architecture — QueryGuard FSM, priority queue, auto-processing
  3. Qwen Code architecture — FIFO queue, runLoop, cancel mechanisms
  4. Dimension-by-dimension comparison (5 tables)
  5. Root cause analysis — why Claude Code "feels" more responsive
  6. 7-agent comparison table (Claude/Qwen/Gemini/Copilot/Aider/Codex/Cursor)
  7. Key source files
  8. Design insights for agent developers

Test plan

  • Verify markdown renders correctly
  • Spot-check source code references against decompiled source
  • Cross-reference with existing comparison docs for consistency

🤖 Generated with Claude Code

wenshao and others added 2 commits April 3, 2026 14:59
New comparison article (507 lines) analyzing how Claude Code and Qwen Code
handle user input during agent execution. Covers priority queue vs FIFO,
QueryGuard state machine vs boolean lock, 3-level interrupt mechanism,
queue visualization, early input capture, and Speculation pre-execution.

Based on decompiled Claude Code v2.1.89 and Qwen Code open source analysis.
16 source code references. Includes 7-agent comparison table.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Fix `drain()` description: prevents new enqueues, doesn't clear queue
  (abort signal exits loop, items abandoned not cleared)
- Add Qwen Code's 3rd cancellation layer: `shutdown()` (graceful)
- Fix agent-interactive.ts line count: ~350 → 512
- Clarify Escape in Qwen Code → cancelCurrentRound() (queue preserved)
- Fix other-agent table: "阻塞" → "无排队" with note about non-blocking
  UI frameworks (Ink/prompt_toolkit/Rust TUI)
- Fix table column mismatch (escaped pipe in backtick code)
- Fix enqueue code snippet: priority is default, not explicit

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds a new deep-dive comparison document analyzing how Claude Code and Qwen Code handle user input queuing and interrupt/cancel behavior during agent execution.

Changes:

  • Introduces a new long-form article explaining input-queue models, state management, and interruption granularity.
  • Adds side-by-side comparison tables across queue model, state machine, interrupt semantics, and UX feedback mechanisms.
  • Summarizes design takeaways for agent developers and practical guidance for users.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

两者都有自动执行下一轮的机制,但触发方式不同:

- **Claude Code**:React `useEffect` 订阅 `useSyncExternalStore`,状态变更**同步**触发 re-render → 自动 dequeue。Turn 间衔接延迟约为一个 React 渲染周期(~16ms)。
- **Qwen Code**:`runLoop()` 内的 `while` 循环,在 `await runOneRound()` resolve 后 dequeue 作为 microtask 继续体执行。Turn 间衔接无额外调度开销。

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.

这里“继续体执行”应为“继续执行”(或“继续执行下一轮”)。当前表述像是笔误,可能影响读者理解。

Suggested change
- **Qwen Code**`runLoop()` 内的 `while` 循环,在 `await runOneRound()` resolve 后 dequeue 作为 microtask 继续体执行。Turn 间衔接无额外调度开销。
- **Qwen Code**`runLoop()` 内的 `while` 循环,在 `await runOneRound()` resolve 后 dequeue 作为 microtask 继续执行。Turn 间衔接无额外调度开销。

Copilot uses AI. Check for mistakes.
CRITICAL correction: the fundamental difference is NOT just about queue
priority or visual feedback, but about WHERE user input is injected:

- Claude Code: query.ts#L1550-L1643 drains the command queue BETWEEN
  tool execution steps within a single turn. User input is converted to
  attachment messages and injected into toolResults, so the model sees
  new user input at the NEXT STEP of the current turn.

- Qwen Code: runReasoningLoop() has NO queue check between tool cycles.
  queue.dequeue() only happens in the outer runLoop, BETWEEN complete
  rounds. User input is only processed as a NEW round after the current
  round fully completes.

Added section 5 "核心差异:Turn 内队列排水 vs Turn 间队列排水" with
source code evidence, comparison diagram, and practical impact scenarios.
Restructured sections 6-9 as supplementary factors.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1 to +3
# 输入队列与中断机制 Deep-Dive

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

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.

PR 描述里提到该文章为“507 lines / 8 sections”,但当前新增文件为 649 行(见仓库文件统计),并且正文包含 1–9 共 9 个顶层章节。建议同步更新 PR 描述中的行数/章节结构,或调整文档结构以保持一致。

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,648 @@
# 输入队列与中断机制 Deep-Dive

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

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.

本文明确基于“Claude Code(…反编译)”并在后文包含较大段疑似从闭源产品反编译得到的源码片段。为降低版权/合规风险,建议将这些代码块改为更抽象的伪代码/流程图,或仅保留必要的极短摘录并用文字概括要点(同时把证据放到仓库内部的 EVIDENCE 类文档/引用清单里,而不是复制源码实现细节)。

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

Copilot uses AI. Check for mistakes.
Comment on lines +70 to +74
```typescript
// 源码: utils/QueryGuard.ts#L29-L121
class QueryGuard {
private _status: 'idle' | 'dispatching' | 'running' = 'idle'
private _generation = 0

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.

这里直接给出了 QueryGuard 的完整类实现(并标注为反编译源码位置)。如果该实现来自闭源反编译产物,建议避免在仓库中逐行复刻完整实现细节;可改为用关键字段/状态转换的伪代码描述(保留核心结论:dispatching 状态与 generation 的用途),以减少后续维护负担与潜在版权争议。

Copilot uses AI. Check for mistakes.
Replace awkward Chinese translation 排水 (literal: drain water) with
English technical terms: Mid-Turn Queue Drain, Between-Round Queue Drain,
drain, etc. Keep standard CS Chinese terms (消费/放弃/抢占).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Round 1 (forward audit, 6 fixes):
- Problem definition table: add "排队 + Mid-Turn Drain" as distinct row
- Priority table: next priority also consumed mid-turn (not only between-turn)
- Architecture diagram: add dual path (Mid-Turn Drain + Between-Turn)
- Practical impact: fix "第2个文件后" → "工具批次完成后" (drain is per-batch)
- Agent comparison table: add "Mid-Turn Drain" column
- Design insights: add mid-turn drain as #1 recommendation

Round 2 (reverse audit): confirmed getAttachmentMessages called once at L1580
Round 3 (markdown): all tables valid, 9 sections sequential
Round 4 (terminology): zero 排水 remaining, Mid-Turn consistent
Round 5 (final): 649 lines, 20 source refs, clean

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -0,0 +1,649 @@
# 输入队列与中断机制 Deep-Dive

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 comparison doc’s H1 header doesn’t follow the established numbering convention used by other files in docs/comparison (e.g., docs/comparison/features.md:1 is # 1. ..., docs/comparison/cli-vs-ide-agents.md:1 is # 41. ...). Consider adding the next available index (e.g., # 42. ...) to keep navigation consistent.

Suggested change
# 输入队列与中断机制 Deep-Dive
# 42. 输入队列与中断机制 Deep-Dive

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +8
# 输入队列与中断机制 Deep-Dive

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

---

## 1. 问题定义

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 PR description says this article has “507 lines” and “8 sections”, but the added file is 649 lines and contains 9 top-level sections (## 1 through ## 9). Please update the PR description so reviewers can accurately validate scope/checklist items.

Copilot uses AI. Check for mistakes.
@wenshao wenshao merged commit cd04345 into main Apr 3, 2026
wenshao added a commit that referenced this pull request Apr 3, 2026
Includes 5 rounds of fixes that were pushed to feat/input-queue-deep-dive
after PR #29 squash-merged (only initial commit was included):

1. Audit fixes: drain() semantics, Qwen Code 3-layer cancel, line counts,
   Escape→cancelCurrentRound (queue preserved), other-agent table precision
2. Replace 排水→drain/queue drain terminology
3. Add Mid-Turn Queue Drain as core architectural difference (query.ts#L1550)
4. Align document with mid-turn drain thesis (architecture diagram, priority
   table, practical impact scenarios, agent comparison table, design insights)
5. Update for Qwen Code v0.15.0: follow-up suggestions + speculation (PR #2525)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
wenshao added a commit that referenced this pull request Apr 3, 2026
Includes 5 rounds of fixes that were pushed to feat/input-queue-deep-dive
after PR #29 squash-merged (only initial commit was included):

1. Audit fixes: drain() semantics, Qwen Code 3-layer cancel, line counts,
   Escape→cancelCurrentRound (queue preserved), other-agent table precision
2. Replace 排水→drain/queue drain terminology
3. Add Mid-Turn Queue Drain as core architectural difference (query.ts#L1550)
4. Align document with mid-turn drain thesis (architecture diagram, priority
   table, practical impact scenarios, agent comparison table, design insights)
5. Update for Qwen Code v0.15.0: follow-up suggestions + speculation (PR #2525)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
wenshao added a commit that referenced this pull request Apr 3, 2026
Includes 5 rounds of fixes that were pushed to feat/input-queue-deep-dive
after PR #29 squash-merged (only initial commit was included):

1. Audit fixes: drain() semantics, Qwen Code 3-layer cancel, line counts,
   Escape→cancelCurrentRound (queue preserved), other-agent table precision
2. Replace 排水→drain/queue drain terminology
3. Add Mid-Turn Queue Drain as core architectural difference (query.ts#L1550)
4. Align document with mid-turn drain thesis (architecture diagram, priority
   table, practical impact scenarios, agent comparison table, design insights)
5. Update for Qwen Code v0.15.0: follow-up suggestions + speculation (PR #2525)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
wenshao added a commit that referenced this pull request Apr 5, 2026
New items from second round of source code comparison:

P0 (2 items):
- #19: Environment variable sanitization (25+ patterns)
- #20: Dangerous command blacklist (rm -rf, find -exec, git -c, etc.)

P1 (4 items):
- #21: Edit fuzzy matching with Levenshtein distance (10% tolerance)
- #22: Omission placeholder detection (prevent "// ... rest")
- #23: JIT context discovery for read/write/edit tools
- #24: OS-level sandbox (Linux bwrap, macOS Seatbelt, Windows)

P2 (6 items):
- #25: Folder trust discovery (pre-execution scanning)
- #26: Web fetch rate limiting + SSRF hardening
- #27: Grep advanced parameters (include/exclude/names_only)
- #28: Advanced vim operations (big words, find, replace, toggle)
- #29: Footer customization dialog
- #30: Write file LLM content correction

P3 (2 items):
- #31: OAuth flow refactoring (shared utils + RFC 9728)
- #32: Conseca safety framework (context-aware policy)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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