cc-viewer 1.6.301 → 1.6.302

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.html CHANGED
@@ -21,7 +21,7 @@
21
21
  // 整体显示大小已弃用 CSS zoom:Electron 改用 webFrame.setZoomFactor(首屏抢占见
22
22
  // electron/tab-content-preload.js),纯浏览器交由用户用浏览器自带快捷键缩放,故此处不再设 zoom。
23
23
  </script>
24
- <script type="module" crossorigin src="/assets/index-DpIkVZv8.js"></script>
24
+ <script type="module" crossorigin src="/assets/index-DSz9bNGm.js"></script>
25
25
  <link rel="modulepreload" crossorigin href="/assets/vendor-antd-Bur5ZxWE.js">
26
26
  <link rel="modulepreload" crossorigin href="/assets/vendor-codemirror-Si44UqBp.js">
27
27
  <link rel="modulepreload" crossorigin href="/assets/vendor-mdxeditor-Cco3AQJS.js">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cc-viewer",
3
- "version": "1.6.301",
3
+ "version": "1.6.302",
4
4
  "description": "Claude Code Logger visualization management tool",
5
5
  "license": "MIT",
6
6
  "main": "server.js",
@@ -0,0 +1,191 @@
1
+ /**
2
+ * PTY → WS 洪泛限流器(Windows ConPTY 洪泛防卡死,server 侧字节率上限)。
3
+ *
4
+ * 背景:pty-manager 的 setImmediate 合帧已把"消息数"压到每 tick 一条,但**字节量
5
+ * 没有上限**——ConPTY 把 TUI 全屏对话框/重绘转译成 macOS forkpty 10~100 倍的字节流
6
+ * (如 /theme 选择器开/关、全屏 TUI 重绘),ws-backpressure gate 只在慢网络
7
+ * (bufferedAmount > 1MB)介入,快 LAN 上洪泛字节全量到达前端,xterm 逐帧解析渲染
8
+ * 稠密 SGR+CJK 重绘把主线程打满。本器在发送侧限流:
9
+ *
10
+ * - 直通态:chunk 立即 send(零延迟,不起 timer)。字节率按固定 flushMs 桶统计,
11
+ * 当前桶累计超 floodThresholdBytesPerWin → 进入限流态。打字回显 / 正常 token
12
+ * 流远低于阈值(≈256KB/s),不受影响。
13
+ * - 限流态:chunk 剥掉自带的 DEC 2026 标记后追加进 pending(pending 内部因此
14
+ * **绝无 2026 标记**,截断永不切坏配对);每 flushMs 把 pending 用单对
15
+ * SYNC_BEGIN/END 重新包裹成**一条** send 发出并清空(无论下游是否跳发,
16
+ * flush 后必清——下游 bpGate 跳发时由其 data-resync 快照对齐,不在这里重试)。
17
+ * 发送前若 pending 超 flushBudgetBytes → findSafeSliceStart 截到尾部预算内:
18
+ * 这是真正的速率上限(≈ flushBudgetBytes / flushMs ≈ 1.9MB/s),与前端
19
+ * TerminalWriteQueue 32KB/帧的消化速率同量级,洪泛期客户端入速 ≈ 出速不积压。
20
+ * pending 超 pendingCap → 同样截到 trimTo(flush 间隔内的内存上界)。中间全屏
21
+ * 重绘帧 last-wins 可丢,ConPTY 重绘流自愈。findSafeSliceStart 只保 ANSI 边界、
22
+ * 不保 2026 配对——配平靠剥标记+重包裹兜底,截断切坏配对会让 xterm 卡在同步
23
+ * 缓冲态(黑屏)。scratch PTY 的 chunk 本就无 2026 标记,剥除为 no-op、重包裹
24
+ * 无害(不支持的终端忽略该序列),两条路径共用同一实现。
25
+ * - 回落:连续 fallbackWins 个桶低于阈值 → flush 残余后回直通态。
26
+ * - reset():清 pending + timer + 回直通态。bpGate onBehind/onResume 时必须调用:
27
+ * data-resync 快照(getOutputBuffer)是唯一真相源,残留 pending 若不清会把早于
28
+ * 快照的旧字节回灌导致画面回退。
29
+ *
30
+ * 纯逻辑、时钟可注入(setTimer/clearTimer/now),便于单测。仿 ws-backpressure.js 惯例。
31
+ */
32
+
33
+ const SYNC_BEGIN = '\x1b[?2026h';
34
+ const SYNC_END = '\x1b[?2026l';
35
+ // 全局替换两种标记。pty-manager flushBatch 只在首尾各加一对,但限流态 pending 由
36
+ // 多个 chunk 拼接而成,内部会出现多对交替——统一剥净再整体包一对。
37
+ const SYNC_MARKS_RE = /\x1b\[\?2026[hl]/g;
38
+
39
+ // 默认常量可经 CCV_FLOOD_* 环境变量覆盖(仿 CCV_FORCE_POLL 先例),便于 Windows
40
+ // 实机排障时调参而不改源码。非法/非正整数值回落默认。
41
+ function envInt(name, fallback) {
42
+ const v = parseInt(process.env[name], 10);
43
+ return Number.isFinite(v) && v > 0 ? v : fallback;
44
+ }
45
+
46
+ const DEFAULT_FLUSH_MS = envInt('CCV_FLOOD_FLUSH_MS', 33); // 限流态合并窗口 = 字节率统计桶宽
47
+ const DEFAULT_FLOOD_THRESHOLD = envInt('CCV_FLOOD_THRESHOLD', 8 * 1024); // 单桶超 8KB(≈256KB/s)判定洪泛
48
+ const DEFAULT_FALLBACK_WINS = envInt('CCV_FLOOD_FALLBACK_WINS', 3); // 连续 N 个低于阈值的桶才回直通(迟滞)
49
+ const DEFAULT_PENDING_CAP = envInt('CCV_FLOOD_PENDING_CAP', 256 * 1024); // 限流态 pending 上限(flush 间隔内的内存上界)
50
+ const DEFAULT_TRIM_TO = envInt('CCV_FLOOD_TRIM_TO', 128 * 1024); // pendingCap 截断后保留的尾部量
51
+ // 单次 flush 发送预算 = 真速率上限:64KB / 33ms ≈ 1.9MB/s,与前端 32KB/帧消化速率同量级
52
+ const DEFAULT_FLUSH_BUDGET = envInt('CCV_FLOOD_FLUSH_BUDGET', 64 * 1024);
53
+
54
+ /**
55
+ * @param {object} opts
56
+ * @param {(data: string) => void} opts.send - 实际发送回调(调用方在内部接 bpGate + ws.send)
57
+ * @param {(buf: string, rawStart: number) => number} opts.findSafeSliceStart - ANSI 安全截断(pty-manager 导出)
58
+ * @param {(buffered: number) => void} [opts.onFloodStart] - 进入限流态(observability 埋点)
59
+ * @param {() => void} [opts.onFloodEnd] - 回落直通态
60
+ * @param {number} [opts.flushMs]
61
+ * @param {number} [opts.floodThresholdBytesPerWin]
62
+ * @param {number} [opts.fallbackWins]
63
+ * @param {number} [opts.pendingCap]
64
+ * @param {number} [opts.trimTo]
65
+ * @param {number} [opts.flushBudgetBytes]
66
+ * @param {(fn: Function, ms: number) => any} [opts.setTimer] - 测试注入
67
+ * @param {(t: any) => void} [opts.clearTimer] - 测试注入
68
+ * @returns {{ offer: (chunk: string) => void, reset: () => void, dispose: () => void, isFlooding: () => boolean }}
69
+ */
70
+ export function createFloodCoalescer({
71
+ send,
72
+ findSafeSliceStart,
73
+ onFloodStart,
74
+ onFloodEnd,
75
+ flushMs = DEFAULT_FLUSH_MS,
76
+ floodThresholdBytesPerWin = DEFAULT_FLOOD_THRESHOLD,
77
+ fallbackWins = DEFAULT_FALLBACK_WINS,
78
+ pendingCap = DEFAULT_PENDING_CAP,
79
+ trimTo = DEFAULT_TRIM_TO,
80
+ flushBudgetBytes = DEFAULT_FLUSH_BUDGET,
81
+ setTimer = setTimeout,
82
+ clearTimer = clearTimeout,
83
+ }) {
84
+ let flooding = false;
85
+ let pending = '';
86
+ let winBytes = 0; // 当前桶累计字节(直通态由 offer 累计,限流态由 flush 结算)
87
+ let calmWins = 0; // 连续低于阈值的桶数
88
+ let flushTimer = null; // 限流态周期 flush;直通态下亦作为桶边界 timer(见 offer)
89
+ let disposed = false;
90
+
91
+ const stopTimer = () => {
92
+ if (flushTimer) {
93
+ clearTimer(flushTimer);
94
+ flushTimer = null;
95
+ }
96
+ };
97
+
98
+ // 直通态的桶边界:到点清零计数。无流量时 timer 不存在,零常驻开销。
99
+ const armPassthroughWindow = () => {
100
+ if (flushTimer) return;
101
+ flushTimer = setTimer(() => {
102
+ flushTimer = null;
103
+ winBytes = 0;
104
+ }, flushMs);
105
+ flushTimer.unref?.();
106
+ };
107
+
108
+ const flushPending = () => {
109
+ if (!pending) return;
110
+ // 单次 flush 发送预算 = 真正的速率上限:超预算截到尾部(last-wins),
111
+ // 保证洪泛期送达客户端的字节率 ≤ flushBudgetBytes/flushMs,与前端消化速率同量级。
112
+ if (pending.length > flushBudgetBytes) {
113
+ const rawStart = pending.length - flushBudgetBytes;
114
+ pending = pending.slice(findSafeSliceStart(pending, rawStart));
115
+ }
116
+ const merged = SYNC_BEGIN + pending + SYNC_END;
117
+ pending = '';
118
+ try { send(merged); } catch { }
119
+ };
120
+
121
+ const onFloodTick = () => {
122
+ flushTimer = null;
123
+ if (disposed || !flooding) return;
124
+ // 本桶结算:低于阈值累计 calm 桶数,连续 fallbackWins 个即回落
125
+ if (winBytes <= floodThresholdBytesPerWin) {
126
+ calmWins++;
127
+ } else {
128
+ calmWins = 0;
129
+ }
130
+ winBytes = 0;
131
+ flushPending();
132
+ if (calmWins >= fallbackWins) {
133
+ flooding = false;
134
+ calmWins = 0;
135
+ try { onFloodEnd?.(); } catch { }
136
+ return; // 不再续约 timer,回直通
137
+ }
138
+ flushTimer = setTimer(onFloodTick, flushMs);
139
+ flushTimer.unref?.();
140
+ };
141
+
142
+ return {
143
+ /** 每条 PTY chunk 调用。直通态立即 send;限流态进 pending 等周期 flush。 */
144
+ offer(chunk) {
145
+ if (disposed || !chunk) return;
146
+ winBytes += chunk.length;
147
+ if (!flooding) {
148
+ if (winBytes > floodThresholdBytesPerWin) {
149
+ // 进入限流态:当前 chunk 是压垮桶的那条,一并纳入 pending(含它之前
150
+ // 本桶已直通的部分不回收——它们已发出,量级在阈值内)。
151
+ flooding = true;
152
+ calmWins = 0;
153
+ stopTimer();
154
+ pending = chunk.replace(SYNC_MARKS_RE, '');
155
+ flushTimer = setTimer(onFloodTick, flushMs);
156
+ flushTimer.unref?.();
157
+ try { onFloodStart?.(winBytes); } catch { }
158
+ return;
159
+ }
160
+ armPassthroughWindow();
161
+ try { send(chunk); } catch { }
162
+ return;
163
+ }
164
+ pending += chunk.replace(SYNC_MARKS_RE, '');
165
+ // 单 chunk 可超 cap:pty-manager 每 tick 合帧,一个 tick(如 /resume 重放)可达数百 KB,
166
+ // 故该分支并非不可达——它是 flush 间隔内的内存上界,与 flushPending 的速率预算各司其职。
167
+ if (pending.length > pendingCap) {
168
+ const rawStart = pending.length - trimTo;
169
+ const safeStart = findSafeSliceStart(pending, rawStart);
170
+ pending = pending.slice(safeStart);
171
+ }
172
+ },
173
+ /** bpGate onBehind/onResume 时调用:resync 快照是唯一真相源,清掉旧 pending 防回灌。 */
174
+ reset() {
175
+ stopTimer();
176
+ pending = '';
177
+ winBytes = 0;
178
+ calmWins = 0;
179
+ flooding = false;
180
+ },
181
+ isFlooding() {
182
+ return flooding;
183
+ },
184
+ /** ws close 时调用,终态。 */
185
+ dispose() {
186
+ disposed = true;
187
+ stopTimer();
188
+ pending = '';
189
+ },
190
+ };
191
+ }
@@ -47,8 +47,11 @@ async function getPty() {
47
47
  * 在 outputBuffer 截断时,找到安全的截断位置,
48
48
  * 避免从 ANSI 转义序列中间开始导致终端状态紊乱。
49
49
  * 策略:从截断点向后扫描,跳过可能被截断的不完整转义序列。
50
+ * 注意:只保 ANSI 序列边界,不保 DEC 2026 同步标记的配对——
51
+ * 跨 2026 块截断的配平由调用方负责(见 lib/pty-flood-coalescer.js)。
52
+ * export 供洪泛限流器复用同一截断语义。
50
53
  */
51
- function findSafeSliceStart(buf, rawStart) {
54
+ export function findSafeSliceStart(buf, rawStart) {
52
55
  // 从 rawStart 开始,向后最多扫描 64 字节寻找安全起点
53
56
  const scanLimit = Math.min(rawStart + 64, buf.length);
54
57
  let i = rawStart;
@@ -560,8 +560,12 @@ function revealFile(req, res, parsedUrl, isLocal, deps) {
560
560
  if (plat === 'darwin') {
561
561
  execFile('open', ['-R', fullPath], () => {});
562
562
  } else if (plat === 'win32') {
563
- // explorer /select,<path> 必须合到一个 arg;分两个会让含空格/中文路径 escape 失败。
564
- spawn('explorer.exe', [`/select,${fullPath}`], { shell: false, windowsHide: true });
563
+ // explorer /select 的规范形式是 /select,"<path>"(仅路径部分加引号、整体一个 arg)。
564
+ // 必须 windowsVerbatimArguments 透传:否则 Node 对含空格 arg 整体加引号生成
565
+ // explorer.exe "/select,C:\My Proj\x.txt",explorer 解析不了 → 功能失效。
566
+ // Windows 文件名不允许 ",且不经 cmd.exe,无元字符注入面。
567
+ const child = spawn('explorer.exe', [`/select,"${fullPath}"`], { windowsVerbatimArguments: true, windowsHide: true });
568
+ child.on('error', () => {}); // 防 async ENOENT 变 uncaughtException 砸进程
565
569
  } else {
566
570
  execFile('xdg-open', [dirname(fullPath)], () => {});
567
571
  }
@@ -18,6 +18,14 @@ function stripImConfigs(obj) {
18
18
  if (obj) for (const id of listPlatforms()) delete obj[id];
19
19
  }
20
20
 
21
+ // /theme 选择器特征:选项文案高特异、不太可能出现在普通生成输出里(ESC 兜底的门控签名)
22
+ const THEME_PICKER_RE = /Auto \(match terminal\)|colorblind-friendly/;
23
+
24
+ // 并发切主题防重入:双端同时 POST themeColor 时只允许一条 /theme 同步链路在途,
25
+ // 防止双监听器 + 双 /theme 注入 + 双 ESC(后到的 POST 仅落盘偏好,跳过 PTY 同步)。
26
+ let _themeSyncInFlight = false;
27
+ export function _resetThemeSyncForTests() { _themeSyncInFlight = false; }
28
+
21
29
  function preferencesGet(req, res, parsedUrl, isLocal, deps) {
22
30
  let prefs = {};
23
31
  try { if (existsSync(deps.getPrefsFile())) prefs = JSON.parse(readFileSync(deps.getPrefsFile(), 'utf-8')); } catch { }
@@ -84,11 +92,18 @@ function preferencesPost(req, res, parsedUrl, isLocal, deps) {
84
92
  // preferences.json 可能携带 auth 的 base64 密码 —— 与 lib/auth.js writePrefs 一致地
85
93
  // 重申 0600,避免该路径(无 mode/不 chmod)把密码文件留成默认 umask 的可读权限。
86
94
  try { chmodSync(prefsFile, 0o600); } catch { /* best-effort; non-POSIX or race */ }
87
- // 主题切换时同步到 Claude Code CLI:发 /theme,监听输出验证结果,不对就再发一次
88
- if (incoming.themeColor && deps.writeToPty && deps.onPtyData) {
95
+ // 主题切换时同步到 Claude Code CLI:发 /theme,监听输出验证结果。
96
+ // 现代 CLI(≥2.x)的 /theme 是交互式选择器(args 被忽略),注入后对话框可能
97
+ // 残留在终端:
98
+ // - mismatch 时**不再重发** /theme —— toggle 语义已不存在,重发只会把选择器
99
+ // 再次打开,把终端困进"确认-重开"循环(Windows ConPTY 下每轮全屏重绘洪泛)。
100
+ // - 5s 超时若 buf 检出选择器特征(选项文案,见 THEME_PICKER_RE),补发一次
101
+ // ESC 关闭残留对话框。无特征绝不发 ESC —— CLI 正在流式生成时 /theme 只是被
102
+ // 排队、对话框未开,误发 ESC 会 interrupt 用户正在跑的任务(宁漏关不误发)。
103
+ if (incoming.themeColor && deps.writeToPty && deps.onPtyData && !_themeSyncInFlight) {
104
+ _themeSyncInFlight = true;
89
105
  const target = incoming.themeColor === 'light' ? 'light' : 'dark';
90
106
  let buf = '';
91
- let retried = false;
92
107
  const removeListener = deps.onPtyData((data) => {
93
108
  buf += data;
94
109
  if (buf.length > 4096) buf = buf.slice(-2048); // 限制 buf 大小
@@ -97,15 +112,20 @@ function preferencesPost(req, res, parsedUrl, isLocal, deps) {
97
112
  if (match) {
98
113
  removeListener();
99
114
  clearTimeout(timeout);
100
- if (match[1] !== target && !retried) {
101
- // 结果与目标不一致,再 toggle 一次
102
- retried = true;
103
- try { deps.writeToPty('/theme\r'); } catch {}
115
+ _themeSyncInFlight = false;
116
+ if (match[1] !== target) {
117
+ console.warn(`[preferences] CLI theme sync mismatch: got ${match[1]}, wanted ${target} (no retry; modern /theme is an interactive picker)`);
104
118
  }
105
119
  }
106
120
  });
107
- // 5 秒超时,避免监听器泄漏
108
- const timeout = setTimeout(() => { removeListener(); }, 5000);
121
+ // 5 秒超时,避免监听器泄漏;检出选择器残留时 ESC 兜底关闭
122
+ const timeout = setTimeout(() => {
123
+ removeListener();
124
+ _themeSyncInFlight = false;
125
+ if (THEME_PICKER_RE.test(buf)) {
126
+ try { deps.writeToPty('\x1b'); } catch {}
127
+ }
128
+ }, 5000);
109
129
  try { deps.writeToPty('/theme\r'); } catch {}
110
130
  }
111
131
  // 回显里也剥离 auth/authByProject(含 base64 密码) —— GET 已剥离,POST 回显同样不能漏给
package/server/server.js CHANGED
@@ -81,6 +81,7 @@ import { backupConfigs } from './lib/config-backup.js';
81
81
  import { normalizeBasePath, validateBasePath, stripBasePath } from './lib/base-path.js';
82
82
  import { createHardenedCleanup } from './lib/term-signals.js';
83
83
  import { createBackpressureGate } from './lib/ws-backpressure.js';
84
+ import { createFloodCoalescer } from './lib/pty-flood-coalescer.js';
84
85
 
85
86
 
86
87
  // 动态获取 getPrefsFile()(LOG_DIR 可能在运行时被 setLogDir 修改)
@@ -1027,7 +1028,7 @@ export async function startViewer() {
1027
1028
  async function setupTerminalWebSocket(httpServer) {
1028
1029
  try {
1029
1030
  const { WebSocketServer } = await import('ws');
1030
- const { writeToPty, writeToPtySequential, resizePty, onPtyData, onPtyExit, getPtyState, getOutputBuffer, getCurrentWorkspace, spawnShell } = await import('./pty-manager.js');
1031
+ const { writeToPty, writeToPtySequential, resizePty, onPtyData, onPtyExit, getPtyState, getOutputBuffer, getCurrentWorkspace, spawnShell, findSafeSliceStart } = await import('./pty-manager.js');
1031
1032
  const {
1032
1033
  spawnScratch,
1033
1034
  writeScratch,
@@ -1132,6 +1133,20 @@ async function setupTerminalWebSocket(httpServer) {
1132
1133
  };
1133
1134
  };
1134
1135
 
1136
+ // 洪泛限流器状态日志(与 makeBpLogger 同款 5s 节流,独立实例不共享计数)。
1137
+ // Windows 实机排"切主题/大流量卡死"时据此确认 ConPTY 洪泛是否触发、几次、量级。
1138
+ const makeFloodLogger = (label, ws) => {
1139
+ let floodCount = 0;
1140
+ let lastLogAt = 0;
1141
+ return (event, bytes) => {
1142
+ if (event === 'start') floodCount++;
1143
+ const now = Date.now();
1144
+ if (now - lastLogAt < 5000) return;
1145
+ lastLogAt = now;
1146
+ console.warn(`[${label}] pty flood ${event}: client=${ws._socket?.remoteAddress || '?'} winBytes=${bytes} floodTotal=${floodCount}`);
1147
+ };
1148
+ };
1149
+
1135
1150
  // scratch 终端 WS:极简版,仅承载 input/resize/data/exit + 显式 kill;不掺杂 hook/SDK/preset
1136
1151
  wssScratch.on('connection', async (ws, req) => {
1137
1152
  const id = req.ccvScratchId;
@@ -1157,11 +1172,18 @@ async function setupTerminalWebSocket(httpServer) {
1157
1172
  // 快照自身有界:scratch outputBuffer 50KB 滚动截断(scratch-pty-manager.js MAX_BUFFER),
1158
1173
  // behind 期间继续灌也不会撑爆 resync 响应。
1159
1174
  const _bpLog = makeBpLogger('scratch-ws', ws);
1175
+ // floodGate 在 bpGate 之后构造(send 闭包依赖 bpGate),onBehind/onResume 经 let 前向引用 reset:
1176
+ // resync 快照是唯一真相源,coalescer 残留 pending 不清会把早于快照的旧字节回灌导致画面回退。
1177
+ let floodGate = null;
1160
1178
  const bpGate = createBackpressureGate({
1161
1179
  getBufferedAmount: () => ws.bufferedAmount,
1162
- onBehind: (buffered) => _bpLog('behind', buffered),
1180
+ onBehind: (buffered) => {
1181
+ _bpLog('behind', buffered);
1182
+ floodGate?.reset();
1183
+ },
1163
1184
  onResume: (buffered) => {
1164
1185
  _bpLog('resume', buffered);
1186
+ floodGate?.reset();
1165
1187
  if (ws.readyState !== 1) return;
1166
1188
  try { ws.send(JSON.stringify({ type: 'data-resync', data: getScratchOutputBuffer(id) })); } catch {}
1167
1189
  },
@@ -1171,10 +1193,22 @@ async function setupTerminalWebSocket(httpServer) {
1171
1193
  },
1172
1194
  });
1173
1195
 
1196
+ // 洪泛限流器:字节率超阈值时按窗口合并 + last-wins 截断(ConPTY 全屏重绘洪泛防卡死,
1197
+ // 与 bpGate 互补——bpGate 管慢网络写缓冲,floodGate 管快 LAN 字节率,详见 lib/pty-flood-coalescer.js)
1198
+ const _floodLog = makeFloodLogger('scratch-ws', ws);
1199
+ floodGate = createFloodCoalescer({
1200
+ send: (data) => {
1201
+ if (ws.readyState === 1 && bpGate.offer()) {
1202
+ try { ws.send(JSON.stringify({ type: 'data', data })); } catch {}
1203
+ }
1204
+ },
1205
+ findSafeSliceStart,
1206
+ onFloodStart: (bytes) => _floodLog('start', bytes),
1207
+ onFloodEnd: () => _floodLog('end', 0),
1208
+ });
1209
+
1174
1210
  const removeDataListener = onScratchData(id, (data) => {
1175
- if (ws.readyState === 1 && bpGate.offer()) {
1176
- try { ws.send(JSON.stringify({ type: 'data', data })); } catch {}
1177
- }
1211
+ floodGate.offer(data);
1178
1212
  });
1179
1213
 
1180
1214
  const removeExitListener = onScratchExit(id, (exitCode) => {
@@ -1203,6 +1237,7 @@ async function setupTerminalWebSocket(httpServer) {
1203
1237
 
1204
1238
  ws.on('close', () => {
1205
1239
  bpGate.dispose();
1240
+ floodGate.dispose();
1206
1241
  removeDataListener();
1207
1242
  removeExitListener();
1208
1243
  // pty 本身**不杀**(保留以支持刷新重连),由 kill 消息或 /api/workspaces/stop 触发;
@@ -1255,11 +1290,18 @@ async function setupTerminalWebSocket(httpServer) {
1255
1290
  // 快照自身有界:outputBuffer 200KB 滚动截断(pty-manager.js MAX_BUFFER + findSafeSliceStart
1256
1291
  // ANSI 安全起点),behind 期间 PTY 继续灌也不会撑爆 resync 响应。
1257
1292
  const _bpLog = makeBpLogger('terminal-ws', ws);
1293
+ // floodGate 前向引用(构造顺序同 scratch 路径):onBehind/onResume 必清 coalescer
1294
+ // pending——resync 快照是唯一真相源,旧 pending 回灌会导致画面回退。
1295
+ let floodGate = null;
1258
1296
  const bpGate = createBackpressureGate({
1259
1297
  getBufferedAmount: () => ws.bufferedAmount,
1260
- onBehind: (buffered) => _bpLog('behind', buffered),
1298
+ onBehind: (buffered) => {
1299
+ _bpLog('behind', buffered);
1300
+ floodGate?.reset();
1301
+ },
1261
1302
  onResume: (buffered) => {
1262
1303
  _bpLog('resume', buffered);
1304
+ floodGate?.reset();
1263
1305
  if (ws.readyState !== 1) return;
1264
1306
  try { ws.send(JSON.stringify({ type: 'data-resync', data: getOutputBuffer() })); } catch {}
1265
1307
  try {
@@ -1288,11 +1330,23 @@ async function setupTerminalWebSocket(httpServer) {
1288
1330
  },
1289
1331
  });
1290
1332
 
1333
+ // 洪泛限流器:字节率超阈值时按窗口合并 + last-wins 截断(ConPTY 全屏重绘洪泛防卡死,
1334
+ // 与 bpGate 互补——bpGate 管慢网络写缓冲,floodGate 管快 LAN 字节率,详见 lib/pty-flood-coalescer.js)
1335
+ const _floodLog = makeFloodLogger('terminal-ws', ws);
1336
+ floodGate = createFloodCoalescer({
1337
+ send: (data) => {
1338
+ if (ws.readyState === 1 && bpGate.offer()) {
1339
+ try { ws.send(JSON.stringify({ type: 'data', data })); } catch {}
1340
+ }
1341
+ },
1342
+ findSafeSliceStart,
1343
+ onFloodStart: (bytes) => _floodLog('start', bytes),
1344
+ onFloodEnd: () => _floodLog('end', 0),
1345
+ });
1346
+
1291
1347
  // PTY 输出 → WebSocket(合并 ws 后客户端自行按 msg.type 分发,server 端不再 role 过滤)
1292
1348
  const removeDataListener = onPtyData((data) => {
1293
- if (ws.readyState === 1 && bpGate.offer()) {
1294
- ws.send(JSON.stringify({ type: 'data', data }));
1295
- }
1349
+ floodGate.offer(data);
1296
1350
  });
1297
1351
 
1298
1352
  // PTY 退出 → WebSocket
@@ -1670,6 +1724,7 @@ async function setupTerminalWebSocket(httpServer) {
1670
1724
 
1671
1725
  ws.on('close', () => {
1672
1726
  bpGate.dispose();
1727
+ floodGate.dispose();
1673
1728
  removeDataListener();
1674
1729
  removeExitListener();
1675
1730
  clientSizes.delete(ws);