Skip to content

Commit 9afac06

Browse files
wenshaoclaude
andauthored
feat: add MCP auto-reconnect + expand terminal rendering optimization (#40)
* feat: add MCP Auto-Reconnect as item 70 (from PR #2866 analysis) New P2 item from QwenLM/qwen-code#2866 upstream backports analysis: - Claude Code: MAX_ERRORS_BEFORE_RECONNECT = 3, SSE auto-reconnect, session expired (404) auto-refresh - Qwen Code: no MCP reconnect mechanism - Source: services/mcp/client.ts L1225-L1357 Also confirmed PR #2866 items already covered: - Compress orphan funcCall → item 1 (compression) enhancement - API-round grouping → item 1 grouping.ts (63 lines) - Follow-up suggestions → item 3 (Speculation) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: expand terminal rendering item + add MCP auto-reconnect Item 54 expanded from "Fullscreen Rendering" to full "终端渲染优化": - DEC 2026 synchronized output (BSU/ESU atomic rendering) - Cell-level differential rendering (ink/log-update.ts) - Double buffering (frontFrame/backFrame swap) - DECSTBM hardware scroll - CharCache/StylePool/CharPool caching (16K cap) - Damage tracking (dirty rectangle) - Render throttling (~60fps via queueMicrotask) - Alt-screen fullscreen mode Qwen Code has only message-splitting (useGeminiStream.ts L632-634). Claude Code has 8-layer anti-flicker in customized Ink engine (~7000 LOC). Item 70: MCP Auto-Reconnect (from PR #2866 analysis) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0c6c4f0 commit 9afac06

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

docs/comparison/qwen-code-improvement-report-p2.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -673,21 +673,29 @@
673673

674674
<a id="item-54"></a>
675675

676-
### 54. Fullscreen Rendering(P2)
676+
### 54. 终端渲染优化(P2)
677677

678-
**思路**Alt-screen 渲染 + 虚拟滚动缓冲区——完全消除终端闪烁。通过 `CLAUDE_CODE_NO_FLICKER=1` 启用
678+
**思路**Claude Code 定制了 Ink 渲染引擎(`ink/` 目录 ~7,000 行),实现 8 层防闪烁机制。Qwen Code 使用标准 Ink 库仅有消息拆分一种防闪烁手段。核心技术:DEC 2026 同步输出(BSU/ESU 包裹所有输出,终端原子渲染)+ cell-level 差分(仅写变化的 cell)+ 双缓冲(frontFrame/backFrame swap)
679679

680680
**Claude Code 源码索引**
681681

682682
| 文件 | 关键函数/常量 |
683683
|------|-------------|
684-
| `utils/fullscreen.ts` | alt-screen 切换 + 虚拟化 |
684+
| `ink/terminal.ts` (248行) | DEC 2026 检测(`CSI ?2026h/l`)、`writeDiffToTerminal()` |
685+
| `ink/log-update.ts` (773行) | cell-level diff 引擎、DECSTBM 硬件滚动 |
686+
| `ink/renderer.ts` (178行) | `frontFrame`/`backFrame` 双缓冲、`prevFrameContaminated` |
687+
| `ink/output.ts` (797行) | Damage Tracking(dirty rectangle)、CharCache(16K cap) |
688+
| `ink/screen.ts` (1486行) | StylePool、CharPool、HyperlinkPool |
689+
| `ink/ink.tsx` (1722行) | 渲染节流(~60fps via `queueMicrotask`)、pool 管理 |
690+
| `utils/fullscreen.ts` | alt-screen 切换(`CLAUDE_CODE_NO_FLICKER=1`|
685691

686-
**Qwen Code 修改方向**`AppContainer.tsx` 新增 fullscreen 模式;通过 ANSI alt-screen sequences 切换;Ink `<ScrollBox>` 虚拟化长内容
692+
**Qwen Code 修改方向**短期——对 Ink 的 `render()` 包裹 BSU/ESU 序列实现同步输出(最高性价比);中期——引入 cell-level diff(参考 `ink/log-update.ts`)替代 Ink 默认的全量 rewrite
687693

688-
**意义**:终端闪烁是低性能终端上的常见 UX 问题。
689-
**缺失后果**:长输出时终端闪烁——视觉体验差。
690-
**改进收益**:alt-screen 无闪烁渲染——视觉稳定。
694+
**意义**:终端渲染质量直接决定用户对工具的第一感受——闪烁 = 不专业。
695+
**缺失后果**:流式输出和工具执行时终端闪烁——尤其在 tmux/低性能终端上明显。
696+
**改进收益**:8 层防闪烁机制——从"能用"到"丝滑"的 UX 跨越。
697+
698+
**相关文章**[终端渲染与防闪烁](../tools/claude-code/11-terminal-rendering.md)
691699

692700
---
693701

@@ -770,3 +778,24 @@
770778
**意义**:与外部服务(GitHub/Slack/Linear)的集成需要 OAuth 管理。
771779
**缺失后果**:手动配置 token + 手动刷新——容易过期。
772780
**改进收益**:托管式 OAuth——一键连接,自动刷新,401 自动重试。
781+
782+
---
783+
784+
<a id="item-70"></a>
785+
786+
### 70. MCP Auto-Reconnect(P2)
787+
788+
**思路**:MCP 服务器连接不稳定(网络抖动、服务重启)时自动重连——连续 3 次错误后关闭连接并重建。SSE 传输层内置重连(maxRetries: 2),session 过期(404)时自动刷新。
789+
790+
**Claude Code 源码索引**
791+
792+
| 文件 | 关键函数/常量 |
793+
|------|-------------|
794+
| `services/mcp/client.ts` (L1225-L1357) | `MAX_ERRORS_BEFORE_RECONNECT = 3``consecutiveConnectionErrors` 计数、SSE reconnection exhausted 检测 |
795+
| `services/mcp/types.ts` (L211) | `reconnectAttempt?: number` |
796+
797+
**Qwen Code 修改方向**`mcp-client.ts``McpClient` 类新增 `consecutiveErrors` 计数;`onError` 回调中累计错误数,达到 3 次时 `close()` + 重新 `connect()`
798+
799+
**意义**:MCP 工具是 Agent 扩展能力的核心——连接中断会导致 Agent 丧失关键工具能力。
800+
**缺失后果**:MCP 服务器短暂不可用 → Agent 整个 session 的 MCP 工具失效——需手动重启。
801+
**改进收益**:瞬态故障自动恢复——用户无感知,Agent 持续使用 MCP 工具。

docs/comparison/qwen-code-improvement-report.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
| **P2** | Bash File Watcher — 检测 formatter/linter 修改已读文件,防止 stale-edit [](./qwen-code-improvement-report-p2.md#item-49) | 缺失 |||
8383
| **P2** | /batch 并行操作 — 编排大规模并行变更(多文件/多任务)[](./qwen-code-improvement-report-p2.md#item-50) | 缺失 |||
8484
| **P2** | Chrome Extension — 调试 live web 应用(读 DOM/Console/Network)[](./qwen-code-improvement-report-p2.md#item-51) | 缺失 |||
85+
| **P2** | MCP Auto-Reconnect — 连续 3 次错误自动重连 + SSE 断线恢复 [](./qwen-code-improvement-report-p2.md#item-70) | 缺失 |||
8586
| **P1** | Structured Output — `--json-schema` 强制 JSON Schema 验证输出 [](./qwen-code-improvement-report-p0-p1.md#item-17) | 缺失 |||
8687
| **P1** | Agent SDK 增强 — Python SDK + 流式回调 + 工具审批回调(Qwen 仅 TS SDK)[](./qwen-code-improvement-report-p0-p1.md#item-18) | 仅 TypeScript SDK |||
8788
| **P1** | Bare Mode — `--bare` 跳过所有自动发现,CI/脚本最快启动 [](./qwen-code-improvement-report-p0-p1.md#item-19) | 缺失 |||
@@ -90,7 +91,7 @@
9091
| **P1** | GitLab CI/CD — 官方 GitLab pipeline 集成 [](./qwen-code-improvement-report-p0-p1.md#item-22) | 缺失 |||
9192
| **P2** | /effort — 设置模型 effort 级别(○ 低 / ◐ 中 / ● 高)[](./qwen-code-improvement-report-p2.md#item-52) | 缺失 |||
9293
| **P2** | Status Line 自定义 — shell 脚本在状态栏展示自定义信息 [](./qwen-code-improvement-report-p2.md#item-53) | 缺失 |||
93-
| **P2** | Fullscreen Rendering — alt-screen 无闪烁渲染 + 虚拟滚动缓冲 [](./qwen-code-improvement-report-p2.md#item-54) | 缺失 | ||
94+
| **P2** | 终端渲染优化 — DEC 2026 同步输出 + 差分渲染 + 双缓冲 + DECSTBM 硬件滚动 + 缓存池化 + alt-screen [](./qwen-code-improvement-report-p2.md#item-54) | 仅消息拆分防闪烁 | ||
9495
| **P2** | Image [Image #N] Chips — 粘贴图片后生成位置引用标记 [](./qwen-code-improvement-report-p2.md#item-55) | 缺失 |||
9596
| **P2** | --max-turns — headless 模式最大 turn 数限制 [](./qwen-code-improvement-report-p2.md#item-56) | 缺失 |||
9697
| **P2** | --max-budget-usd — headless 模式 USD 花费上限 [](./qwen-code-improvement-report-p2.md#item-57) | 缺失 |||

0 commit comments

Comments
 (0)