← 返回 Skills 市场
534422530

系统化调试

作者 534422530 · GitHub ↗ · v1.1.0 · MIT-0
cross-platform ✓ 安全检测通过
57
总下载
0
收藏
0
当前安装
2
版本数
在 OpenClaw 中安装
/install laosi-systematic-debug
功能描述
系统化调试 - 假设驱动调试:观察→假设→隔离→验证→修复。附带Binary Search调试树、Minimal Reproducer生成
使用说明 (SKILL.md)

Systematic Debug - 系统化调试

激活词: 调试 / debug / 排查问题

Debug流水线

观察问题 → 形成假设 → 隔离范围 → 验证假设 → 修复并回归
   ↓          ↓          ↓          ↓          ↓
 收集日志    3+候选   二分查找    最小复现   修复+测试

核心原则:永远不要不带着假设就去改代码。

Python 实现

from dataclasses import dataclass, field
from typing import List, Optional, Callable
from datetime import datetime

@dataclass
class Hypothesis:
    description: str
    evidence_for: List[str] = field(default_factory=list)
    evidence_against: List[str] = field(default_factory=list)
    confidence: float = 0.0  # 0-1
    confirmed: Optional[bool] = None
    
    def evaluate(self):
        """根据正反证据更新置信度"""
        total = len(self.evidence_for) + len(self.evidence_against)
        if total == 0:
            self.confidence = 0.0
        else:
            self.confidence = len(self.evidence_for) / total
        return self.confidence

@dataclass
class DebugSession:
    title: str
    symptoms: List[str] = field(default_factory=list)
    hypotheses: List[Hypothesis] = field(default_factory=list)
    log: List[str] = field(default_factory=list)
    created: str = ""
    
    def __post_init__(self):
        if not self.created:
            self.created = datetime.now().isoformat()
    
    def add_symptom(self, symptom: str):
        self.symptoms.append(symptom)
        self.log.append(f"[OBSERVE] 症状: {symptom}")
    
    def add_hypothesis(self, desc: str) -> Hypothesis:
        h = Hypothesis(description=desc)
        self.hypotheses.append(h)
        self.log.append(f"[HYPOTHESIZE] 假设: {desc}")
        return h
    
    def add_evidence(self, hypothesis_index: int, evidence: str, supports: bool):
        h = self.hypotheses[hypothesis_index]
        if supports:
            h.evidence_for.append(evidence)
        else:
            h.evidence_against.append(evidence)
        tag = "FOR" if supports else "AGAINST"
        self.log.append(f"[EVIDENCE] {tag}: {evidence}")
        h.evaluate()
    
    def best_hypothesis(self) -> Optional[Hypothesis]:
        if not self.hypotheses:
            return None
        return max(self.hypotheses, key=lambda h: h.confidence)
    
    def report(self) -> str:
        lines = [f"# Debug Report: {self.title}", f"Created: {self.created}\
"]
        lines.append("## 症状")
        for s in self.symptoms:
            lines.append(f"- {s}")
        
        lines.append("\
## 假设 (按置信度排序)")
        sorted_h = sorted(self.hypotheses, key=lambda h: -h.confidence)
        for i, h in enumerate(sorted_h):
            status = "✅ 确认" if h.confirmed else "❌ 排除" if h.confirmed is False else "⏳ 待验证"
            lines.append(f"\
### {status} H{i}: {h.description} (置信度: {h.confidence:.0%})")
            if h.evidence_for:
                lines.append("支持证据:")
                for e in h.evidence_for:
                    lines.append(f"  ✅ {e}")
            if h.evidence_against:
                lines.append("反对证据:")
                for e in h.evidence_against:
                    lines.append(f"  ❌ {e}")
        
        lines.append(f"\
## 根因")
        best = self.best_hypothesis()
        if best and best.confirmed:
            lines.append(f"✅ {best.description}")
        else:
            lines.append("⏳ 尚未确认根因")
        
        lines.append("\
## 调试日志")
        for entry in self.log:
            lines.append(f"- {entry}")
        
        return "\
".join(lines)

# 二分查找定位Bug
def binary_search_isolation(items: list, test_fn: Callable[[list], bool]) -> int:
    """在列表中用二分法找到第一个触发错误的元素"""
    lo, hi = 0, len(items)
    steps = 0
    while lo \x3C hi:
        mid = (lo + hi) // 2
        steps += 1
        if test_fn(items[lo:mid]):
            lo = mid + 1
        else:
            hi = mid
    print(f"二分查找: {steps} 步定位到 index {lo}")
    return lo

# 使用示例
session = DebugSession(title="API超时排查")

# 观察阶段
session.add_symptom("用户列表API响应时间>10s(正常应\x3C200ms)")
session.add_symptom("只在数据量>10000条时出现")
session.add_symptom("CPU使用率正常,内存正常")

# 形成假设
h1 = session.add_hypothesis("数据库缺少索引导致全表扫描")
h2 = session.add_hypothesis("N+1查询问题:每次循环都发SQL")
h3 = session.add_hypothesis("ORM懒加载触发过多查询")

# 收集证据
session.add_evidence(0, "EXPLAIN显示全表扫描 type=ALL", True)
session.add_evidence(0, "查询数据量100万只返回10条", True)
session.add_evidence(1, "代码中for循环内调用了query()", True)
session.add_evidence(1, "使用select_related可减少查询数", True)
session.add_evidence(2, "DB总查询数=用户数+1", True)

# 确认根因
session.hypotheses[2].confirmed = True

print(session.report())

# 定位到具体代码行
code_lines = [
    "def get_users():",
    "    users = User.objects.all()",
    "    result = []",
    "    for u in users:           # ← 懒加载,这里才发SQL",
    "        result.append({",
    "            'name': u.name,",
    "            'posts': u.posts.count()  # ← 每次循环一条SQL",
    "        })",
    "    return result",
]

# 用二分法定位问题代码段
def test_has_nplus1(lines_slice):
    return not any("count()" in l for l in lines_slice)

idx = binary_search_isolation(code_lines, test_has_nplus1)
print(f"问题代码在第 {idx+1} 行: {code_lines[idx]}")

反模式(不要这样做)

❌ 坏做法 ✅ 好做法
随便改一行看看 先形成假设再改
同时改多个地方 一次只改一个变量
跳过最小复现 先写最小复现再改
只修症状 修根因加回归测试
不复盘 记录根因和修复过程

使用场景

  1. 线上故障: 服务宕机时按流水线快速定位根因
  2. 性能问题: 慢查询/高延迟,二分法定位瓶颈
  3. 回归Bug: 之前能用的功能坏了,从最近的变更开始查
  4. 偶发Bug: 记录所有条件,找交集缩小范围

依赖

  • Python 3.8+
  • 无第三方依赖
安全使用建议
Install this if you want the agent to slow down and investigate bugs before changing code. Be aware it may activate on many ordinary bug or error reports, so use a more specialized skill explicitly when a task needs a different workflow.
能力评估
Purpose & Capability
The stated purpose is systematic root-cause debugging, and the artifact content stays aligned with that purpose: reproduce, inspect logs, review recent diffs, form hypotheses, write a regression test, and implement a minimal fix.
Instruction Scope
The activation language is broad and forceful for bug-fix requests, including generic English and Chinese debug phrases, but it remains scoped to broken behavior and explicitly excludes lint-only fixes, dependency updates, feature work, and refactoring.
Install Mechanism
No installer, dependency bootstrap, script payload, package install step, or automatic runtime hook was present in the reviewed artifact evidence.
Credentials
Suggested actions such as reading errors, running git history commands, adding diagnostic logging, running tests, and sanitized web searches are proportionate to debugging work.
Persistence & Privilege
The skill does not request credentials, privileged access, background execution, external posting, destructive actions, or persistence; the only local reference is reading prior debug learnings if present.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install laosi-systematic-debug
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /laosi-systematic-debug 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.1.0
- Expanded documentation to include Chinese descriptions, debug workflows, and anti-patterns. - Added a full Python implementation for systematic debugging, with hypothesis tracking, evidence gathering, and bug isolation utilities. - Included sample usage: simulating API timeout investigation, root cause reporting, and binary search for faulty code lines. - Provided clear best practices and anti-patterns in a comparative table. - Specified usage scenarios and Python version requirements (no third-party dependencies).
v1.0.0
Initial release introducing a systematic, hypothesis-driven debugging workflow. - Outlines a clear 5-step process: Observe, Hypothesize, Isolate, Verify, Fix. - Emphasizes evidence gathering, multiple hypotheses, and root cause analysis. - Discourages common debugging anti-patterns like guesswork and symptom-fixing. - Includes a regression test requirement after fixes.
元数据
Slug laosi-systematic-debug
版本 1.1.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 2
常见问题

系统化调试 是什么?

系统化调试 - 假设驱动调试:观察→假设→隔离→验证→修复。附带Binary Search调试树、Minimal Reproducer生成. 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 57 次。

如何安装 系统化调试?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install laosi-systematic-debug」即可一键安装,无需额外配置。

系统化调试 是免费的吗?

是的,系统化调试 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

系统化调试 支持哪些平台?

系统化调试 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 系统化调试?

由 534422530(@534422530)开发并维护,当前版本 v1.1.0。

💬 留言讨论