Files
myWiki/concepts/watchdog-pattern.md

42 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "Watchdog Pattern看门狗模式"
created: 2026-06-29
updated: 2026-06-29
type: concept
tags: [safety, loop-engineering, reliability, pattern]
sources: [[prompt-to-loop-engineering-2026]]
confidence: high
---
# Watchdog Pattern
> 在 [[loop-engineering|Loop Engineering]] 中,看门狗是专门防御**自旋死循环**的安全机制——独立于主异步线程的外部进程监控 CPU越过应用层直接强杀。
## 为什么需要看门狗
[[circuit-breaker-pattern|熔断器]] 依赖任务级失败计数,但以下场景熔断器无法覆盖:
- 退避策略缺失导致的自旋retry loop without backoff
- 模型进入"空转"状态(不断生成但不产生有效输出)
- I/O 阻塞导致的假性存活(进程存在但无进展)
## 工作机制
1. **独立进程**:与 Agent 主进程完全隔离
2. **CPU 监控**:检测占用率是否长期满载
3. **I/O 检测**:长时间无 I/O 交互 → 判定为死循环
4. **SIGKILL**:越过应用层直接发信号强杀并回收资源
## 与熔断器的分工
| 机制 | 监控对象 | 触发条件 | 响应 |
|------|---------|---------|------|
| [[circuit-breaker-pattern|熔断器]] | 任务级失败 | 连续失败 N 次 | 跳闸 + 回退 |
| [[watchdog-pattern|看门狗]] | 进程级异常 | CPU 满载 + 无 I/O | SIGKILL |
## 相关概念
- [[circuit-breaker-pattern|熔断器]]
- [[loop-contract|Loop Contract]]
- [[loop-engineering|Loop Engineering]]