dsh-project-flow 0.10.4 → 0.12.0

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/watchdog.mjs CHANGED
@@ -26,9 +26,19 @@ import {
26
26
  export const FAULT = {
27
27
  REPEAT: 'FAULT_HUMAN_INTERVENTION_REQUIRED',
28
28
  SILENT: 'FAULT_SILENT_HANG',
29
+ STALL: 'FAULT_TOOL_STALLED',
29
30
  BUDGET: 'FAULT_BUDGET_EXCEEDED',
30
31
  }
31
32
 
33
+ /**
34
+ * 一个工具最多允许跑多久(毫秒)。超了才算卡住。
35
+ *
36
+ * 这个数是给「渲染、编译、跑测试」这类活留的:30 秒竖屏成片渲 5 条,
37
+ * ffmpeg 老老实实跑九分钟,中途一个字都不吐——它不是在挂死,它是在干活。
38
+ * 默认 30 分钟,够渲一部长片;真要跑更久的活,配置里调 busyCapMs。
39
+ */
40
+ const DEFAULT_BUSY_CAP_MS = 1800000
41
+
32
42
  export class WatchdogFault extends Error {
33
43
  constructor(code, detail) {
34
44
  super(`[${code}] ${detail}`)
@@ -160,11 +170,12 @@ export class RepeatBreaker {
160
170
  }
161
171
 
162
172
  /**
163
- * 活性巡检:三条信号,取较新的那条算「活着」。
173
+ * 活性巡检:四条信号,取较新的那条算「活着」。
164
174
  *
165
175
  * 文件写入 —— 扫工作区 mtime
166
176
  * 子会话输出 —— 流式收到一段就 touch 一次
167
177
  * API 返回 —— 每阶结束 beat 一次
178
+ * 工具在跑 —— tool/call 与 tool/result 之间的那一段
168
179
  *
169
180
  * 第二条(touch)是补的一个洞:以前只有「每阶结束」才 beat,一阶跑一小时,
170
181
  * 中间那一个小时在探针眼里跟卡死没区别。实测踩过——实施子会话一直在吐 token、
@@ -172,6 +183,16 @@ export class RepeatBreaker {
172
183
  * FAULT_SILENT_HANG 掐掉。真正的静默是「连一个 token 都不再吐」,
173
184
  * 只有接到了流上才分得出来。
174
185
  *
186
+ * 第四条(busy)是再补的一个洞,而且比第二条更隐蔽:**光有心跳救不了长命令**。
187
+ * 子会话调 bash 跑 ffmpeg,发出 tool/call 之后就没有下一个事件了——命令跑多久,
188
+ * 事件流就断多久。2026-12-03 那个「五个 30 秒成片」的活就是这么被掐掉的:
189
+ * 五条 1080×1920 竖屏渲染花了九分钟,期间不吐 token、不写文件、没有 API 往返,
190
+ * 三条信号全断,探针判定静默挂死,而产物十二条要求逐条全对。
191
+ *
192
+ * 所以「有一个工具还没返回」本身就得算活着。但不能算免死金牌:一个命令卡住
193
+ * 三小时和真挂死没有区别,所以给了 busyCapMs 上限——超了照样判,只是判的
194
+ * 理由和控制码跟静默挂死分开(FAULT_TOOL_STALLED),重试策略也应当不同。
195
+ *
175
196
  * 跳掉 .project-flow 是关键:那里面 run.log 一直在写,
176
197
  * 算进来等于心跳永远新鲜,这个熔断就废了。
177
198
  */
@@ -184,11 +205,29 @@ export class LivenessProbe {
184
205
  #timer
185
206
  #onSilent
186
207
  #fired = false
208
+ /** 还没返回的工具个数。>0 表示子会话卡在一条命令里,不是在挂死。 */
209
+ #busy = 0
210
+ #busySince = 0
211
+ #busyCapMs
212
+ #busyLabel = ''
213
+ /**
214
+ * 子会话本体,用来问它忙不忙。
215
+ *
216
+ * 这是第二条「它在干活」的路,而且比事件可靠得多。事件通道会因各种原因
217
+ * 接不上——回调签名变了、activation 不是 local、归属过滤误杀——一断就
218
+ * 整条保护静默失效,而且看不出来。`agent.status` 是宿主自己的状态机,
219
+ * 读一个字段就行,不需要任何通道。
220
+ */
221
+ #agent
222
+ #agentSince = 0
223
+ #agentSeen = 0
224
+ #agentBusy = 0
187
225
 
188
- constructor({ cwd, windowMs, onSilent }) {
226
+ constructor({ cwd, windowMs, onSilent, busyCapMs }) {
189
227
  this.#cwd = cwd
190
228
  this.#windowMs = windowMs
191
229
  this.#onSilent = onSilent
230
+ this.#busyCapMs = Number.isFinite(busyCapMs) && busyCapMs > 0 ? busyCapMs : DEFAULT_BUSY_CAP_MS
192
231
  // 间隔跟着窗口走:窗口小就查得勤,窗口大就不折腾。
193
232
  // 下限 200ms 是为了测试能验到判定本身,不是为了让线上跑这么快。
194
233
  const interval = Math.min(15000, Math.max(200, Math.floor(windowMs / 4)))
@@ -200,6 +239,10 @@ export class LivenessProbe {
200
239
  if (typeof ms === 'number' && ms > 0) this.#windowMs = ms
201
240
  }
202
241
 
242
+ setBusyCap(ms) {
243
+ if (typeof ms === 'number' && ms > 0) this.#busyCapMs = ms
244
+ }
245
+
203
246
  beat() {
204
247
  this.#lastBeat = Date.now()
205
248
  this.#fired = false
@@ -214,12 +257,121 @@ export class LivenessProbe {
214
257
  if (this.#fired === true) this.#fired = false
215
258
  }
216
259
 
260
+ /**
261
+ * 一个工具开始跑。
262
+ *
263
+ * 从这里到对应的 toolEnd 之间,探针当它活着——不管中间多久没别的动静。
264
+ * 只记个数不记 id:并行工具调用、报错不返回都会让配对错位,
265
+ * 与其猜得精细,不如按个数加总,再加两道兜底(busyCap 与 resetBusy)。
266
+ */
267
+ toolStart(label) {
268
+ if (this.#busy === 0) {
269
+ this.#busySince = Date.now()
270
+ this.#busyLabel = typeof label === 'string' ? label : ''
271
+ }
272
+ this.#busy += 1
273
+ this.#lastTouch = Date.now()
274
+ }
275
+
276
+ /** 一个工具返回了。计数减到 0 就把计时重置,别让上一段的时长记到下一段头上。 */
277
+ toolEnd() {
278
+ this.#busy = Math.max(0, this.#busy - 1)
279
+ this.#lastTouch = Date.now()
280
+ if (this.#busy === 0) {
281
+ this.#busySince = 0
282
+ this.#busyLabel = ''
283
+ }
284
+ }
285
+
286
+ /**
287
+ * 强制把「有工具在跑」清零。
288
+ *
289
+ * 子会话被取消时,正在跑的工具不会再有 tool/result——不同步的话计数会泄漏,
290
+ * 下一阶一上来就在 busy 状态里,静默熔断整个失效。所以每阶开始和结束各清一次。
291
+ */
292
+ resetBusy() {
293
+ this.#busy = 0
294
+ this.#busySince = 0
295
+ this.#busyLabel = ''
296
+ }
297
+
298
+ /**
299
+ * 盯住这个子会话,之后每次 tick 直接问它忙不忙。
300
+ *
301
+ * @param agent 带 `status` 字段的宿主 agent 对象;传 undefined 表示不知道
302
+ */
303
+ watchAgent(agent) {
304
+ this.#agent = agent
305
+ this.#agentSince = 0
306
+ // 统计在挂上新 agent 时清零:留着上一阶的数只会让人读错。
307
+ this.#agentSeen = 0
308
+ this.#agentBusy = 0
309
+ }
310
+
311
+ /** 松手。只清 agent 本身,统计留着给调用方读——那是诊断用的证据。 */
312
+ clearAgent() {
313
+ this.#agent = undefined
314
+ this.#agentSince = 0
315
+ }
316
+
317
+ /** 观测统计:问了几次、其中几次它不闲。用来判断这条路有没有真的接通。 */
318
+ get agentStats() {
319
+ return { seen: this.#agentSeen, busy: this.#agentBusy }
320
+ }
321
+
322
+ get busyCount() {
323
+ return this.#busy
324
+ }
325
+
217
326
  get idleMs() {
218
327
  return Date.now() - Math.max(this.#lastFileWrite, this.#lastBeat, this.#lastTouch)
219
328
  }
220
329
 
221
330
  async #tick() {
222
331
  if (this.#fired) return
332
+
333
+ // 先问子会话自己:它说没闲下来,就是在干活,不管有没有事件、有没有写文件。
334
+ //
335
+ // 这一条不依赖事件通道,是长命令保护的最后一道保险。事件那条路挂过——
336
+ // 真机跑出来 events=0,一个事件都没接到,整条保护静默失效,而代码看着全对。
337
+ // 读 status 不需要通道,也就没有「接不上」这回事。
338
+ if (this.#agent !== undefined) {
339
+ this.#agentSeen += 1
340
+ const status = this.#agent.status
341
+ if (typeof status === 'string' && status !== 'idle') {
342
+ this.#agentBusy += 1
343
+ this.#lastTouch = Date.now()
344
+ if (this.#agentSince === 0) this.#agentSince = Date.now()
345
+ const held = Date.now() - this.#agentSince
346
+ // 跟「有工具在跑」一样的上限:忙也不是免死金牌,忙过一个上限照样判。
347
+ if (held <= this.#busyCapMs) return
348
+ this.#fired = true
349
+ this.#onSilent(Math.max(1, Math.round(held / 1000)), {
350
+ stalledTool: true,
351
+ label: `agent.status=${status}`,
352
+ busyCount: 1,
353
+ })
354
+ return
355
+ }
356
+ this.#agentSince = 0
357
+ }
358
+
359
+ // 有工具在跑:它可能九分钟不吭声,那是正常的,不是挂死。
360
+ // 但不能无限等——超过上限一个都不返回,那就是真卡住了。
361
+ if (this.#busy > 0) {
362
+ const held = Date.now() - this.#busySince
363
+ if (held <= this.#busyCapMs) return
364
+ this.#fired = true
365
+ // 至少报 1 秒:配置调得很小时会算出 0,而「已经跑了 0 秒还没返回」
366
+ // 这句话读起来像 bug,不像诊断。
367
+ this.#onSilent(Math.max(1, Math.round(held / 1000)), {
368
+ stalledTool: true,
369
+ label: this.#busyLabel,
370
+ busyCount: this.#busy,
371
+ })
372
+ return
373
+ }
374
+
223
375
  const newest = await newestMtime(this.#cwd, 0)
224
376
  if (newest > this.#lastFileWrite) this.#lastFileWrite = newest
225
377
  if (this.idleMs > this.#windowMs) {
@@ -417,18 +569,41 @@ export class Watchdog {
417
569
  #controller = new AbortController()
418
570
  #fault
419
571
  #onTrip
572
+ #cwd
573
+ #busyCapMs
574
+
575
+ /**
576
+ * 探针报静默时的处理。
577
+ *
578
+ * 提成字段而不是写在构造里,是为了 pardon() 能重建探针——重建时必须把同一个
579
+ * 回调再递一次,否则恢复出来的探针永远不报警,比不恢复还糟。
580
+ */
581
+ #onSilent = (seconds, info) => {
582
+ // 两种「没动静」要分开报:一种是真挂死,一种是一条命令跑了太久。
583
+ // 它们的处置完全不同——挂死值得重试,卡住的命令重试只会再卡一次,
584
+ // 而且第二次可能带着副作用。控制码分开,调用方才分得清。
585
+ if (info?.stalledTool === true) {
586
+ const which = info.label === '' ? '' : `(${info.label})`
587
+ this.#trip(
588
+ FAULT.STALL,
589
+ `一个工具${which}已经跑了 ${seconds} 秒还没返回,判定卡住。`
590
+ + '它在干活、只是太久了,所以静默窗口管不到它;上限是 busyCapMs。'
591
+ + '接着跑之前先确认那个命令是不是真该跑这么久。',
592
+ )
593
+ return
594
+ }
595
+ this.#trip(FAULT.SILENT, `连续 ${seconds} 秒既没写文件也没有 API 返回,判定静默挂死`)
596
+ }
420
597
 
421
598
  constructor({ cwd, logPath, settings, onTrip }) {
422
599
  this.#onTrip = onTrip
600
+ this.#cwd = cwd
601
+ this.#busyCapMs = settings.busyCapMs
423
602
  this.#repeat = new RepeatBreaker({
424
603
  logPath,
425
604
  limit: settings.maxRepeatErrors ?? 3,
426
605
  })
427
- this.#liveness = new LivenessProbe({
428
- cwd,
429
- windowMs: settings.silentMsBuild ?? 900000,
430
- onSilent: seconds => this.#trip(FAULT.SILENT, `连续 ${seconds} 秒既没写文件也没有 API 返回,判定静默挂死`),
431
- })
606
+ this.#liveness = this.#makeProbe(settings.silentMsBuild ?? 900000)
432
607
  this.#budget = new BudgetBreaker({
433
608
  limitUsd: settings.budgetUsd ?? 10,
434
609
  priceTable: parsePriceTable(settings.priceTable),
@@ -436,6 +611,15 @@ export class Watchdog {
436
611
  })
437
612
  }
438
613
 
614
+ #makeProbe(windowMs) {
615
+ return new LivenessProbe({
616
+ cwd: this.#cwd,
617
+ windowMs,
618
+ busyCapMs: this.#busyCapMs,
619
+ onSilent: this.#onSilent,
620
+ })
621
+ }
622
+
439
623
  /** 传给 runStage 的 signal。熔断时它被 abort,子会话当场收工。 */
440
624
  get signal() {
441
625
  return this.#controller.signal
@@ -466,6 +650,9 @@ export class Watchdog {
466
650
 
467
651
  /** 进某一阶时切静默窗口:规划短、实施和质检长。 */
468
652
  beginStage(stage, model) {
653
+ // 每阶开始先把「有工具在跑」清零:上一阶被取消时,正在跑的命令不会再有
654
+ // tool/result,计数不同步就会漏到这一阶,静默熔断整个失效。
655
+ this.#liveness.resetBusy()
469
656
  this.#liveness.beat()
470
657
  this.#currentStage = stage
471
658
  const settings = this.#windowByStage(stage)
@@ -482,6 +669,84 @@ export class Watchdog {
482
669
  this.#liveness.touch()
483
670
  }
484
671
 
672
+ /**
673
+ * 子会话刚发出一个工具调用。从这里到 toolEnd 之间不算静默。
674
+ *
675
+ * 这一对是修「长命令被误判挂死」的落点:跑渲染、编译、跑测试的时候,
676
+ * 事件流是真的会断上十几分钟,光靠 touch 救不了。
677
+ */
678
+ toolStart(label) {
679
+ if (this.#fault !== undefined) return
680
+ this.#liveness.toolStart(label)
681
+ }
682
+
683
+ /** 工具返回了,或者这一阶结束了(收尾时统一清,别让计数泄漏)。 */
684
+ toolEnd() {
685
+ if (this.#fault !== undefined) return
686
+ this.#liveness.toolEnd()
687
+ }
688
+
689
+ /**
690
+ * 把「有工具在跑」全部作废。
691
+ *
692
+ * 用在一次尝试收尾的时候:被取消或被打断的命令永远不会有 tool/result,
693
+ * 计数留着的话,下一次尝试、甚至下一阶一开场就处于「在干活」状态,
694
+ * 那个阶段的静默熔断等于没有。
695
+ */
696
+ resetTools() {
697
+ if (this.#fault !== undefined) return
698
+ this.#liveness.resetBusy()
699
+ }
700
+
701
+ /** 当前有没有工具在跑。给状态栏和阶段超时用。 */
702
+ get busy() {
703
+ return this.#liveness.busyCount > 0
704
+ }
705
+
706
+ /**
707
+ * 把子会话本体交给探针,之后它每次 tick 都问一句「你还忙吗」。
708
+ *
709
+ * 这条路和 tool/call 事件是并行的两道保险:事件到得快但可能接不上,
710
+ * status 一定能读到但粒度粗(只能区分 idle 和非 idle)。两道都在,
711
+ * 才不会因为其中一道静默坏掉就整条保护失效。
712
+ */
713
+ watchAgent(agent) {
714
+ this.#liveness.watchAgent(agent)
715
+ }
716
+
717
+ /** 这一阶收工,松手。 */
718
+ clearAgent() {
719
+ this.#liveness.clearAgent()
720
+ }
721
+
722
+ /** 观测统计:问了几次、几次不闲。这是判断这条路通没通的唯一证据。 */
723
+ get agentStats() {
724
+ return this.#liveness.agentStats
725
+ }
726
+
727
+ /**
728
+ * 撤销这次熔断,把守护器恢复成能用的状态。
729
+ *
730
+ * 只在一种情况下调用:熔断之后复核发现产物其实全对(Requirement.md 里的 check
731
+ * 全过)。那时候熔断本身就是误判,留着它会让后面每一步 cancelled() 都抛,
732
+ * 复核等于白做;而且段 4 还要接着跑,没有静默保护的话质检卡死也没人管。
733
+ *
734
+ * 重建 controller 是必须的:原来那个已经 abort 过,abort 过的 signal 永远
735
+ * 是 aborted,复用等于让下一阶一进去就被取消。
736
+ *
737
+ * @returns 是否真的撤销了一次熔断
738
+ */
739
+ pardon() {
740
+ if (this.#fault === undefined) return false
741
+ this.#fault = undefined
742
+ this.#controller = new AbortController()
743
+ this.#liveness.dispose()
744
+ // 窗口先用「质检」那档:撤销之后通常就是往下走质检。
745
+ // 真进了别的阶段,beginStage 会按阶段再切一次。
746
+ this.#liveness = this.#makeProbe(this.#silentCheck)
747
+ return true
748
+ }
749
+
485
750
  #windowByStage(stage) {
486
751
  if (stage === 2) return this.#silentPlan
487
752
  if (stage === 3) return this.#silentBuild
@@ -523,6 +788,9 @@ export class Watchdog {
523
788
  // 已经熔断过了就不再累加:探针 dispose 之后 beat() 没有意义,
524
789
  // 再往上抛错只会盖掉真正的熔断原因。
525
790
  if (this.#fault !== undefined) return
791
+ // 这一阶收工了,工具计数一律归零。泄漏的计数会让下一阶一开场就处于
792
+ // 「在干活」状态,那个阶段的静默熔断等于没有。
793
+ this.#liveness.resetBusy()
526
794
  this.#liveness.beat()
527
795
  this.#budget.countOutput(typeof outcome?.text === 'string' ? outcome.text.length : 0)
528
796