Bug hunter

From findscripter/everything-skills · View interactive version

Bug hunter is an AI agent skill from the GitHub repository findscripter/everything-skills. 当报告 bug、出现报错或行为异常、需要排查间歇性故障与线上问题时使用;做证据驱动的系统化调试(复现→取证→假设→验证→定位根因→改根因→回归测试),产出根因结论、最小修复与防回归用例;不适用于纯新功能开发、需求设计或仅靠掩盖症状(如随手加可选链兜底)即收工的场景;触发词:修 bug、调试、报错排查

How to use this skill

Skill file

何时使用

适用:

不该用(负边界):

核心原则:不靠猜,跟着证据走;先复现再修;修根因而非症状;改完务必回归并补测试。

步骤

  1. 复现:拿到精确复现步骤,本地复现,记录触发条件、错误信息/行为,确认是必现还是偶现。无法复现就补环境信息(dev/staging/prod、浏览器/设备、前置操作、错误日志)。
  2. 取证:查日志(tail -f logs/app.log、journalctl -u myapp -f、浏览器 DevTools Console),看完整堆栈(错误类型、消息、行号、时间戳),看状态(处理的数据、DB、localStorage/cookie)。
  3. 立假设:基于证据给出可证伪的猜测,例如「登录超时是因为 session cookie 在鉴权完成前就过期」。
  4. 验假设:加日志(console.log('before:', userData))、下断点(debugger;)、隔离问题(注释代码、换 mock 数据缩小范围)。
  5. 定根因:从症状回溯,常见根因 —— null/undefined、类型错误、竞态、缺错误处理、逻辑错误、差一错误、async/await 漏用、缺校验。沿「症状→在哪→为什么…」逐层下钻到真正起点。
  6. 改根因:修起点而非掩盖表象。
// 坏修复(掩盖症状)
const name = user?.profile?.name || 'Unknown';

// 好修复(修根因:登录时确保写入 user ID)
const login = async (credentials) => {
  const user = await authenticate(credentials);
  if (user) { session.userId = user.id; return user; }
  throw new Error('Invalid credentials');
};
  1. 验修复:复现原 bug → 应用修复 → 再复现应失败 → 测边界 → 测相关功能 → 跑现有测试。
  2. 防回归:补一条测试锁住行为。
test('login sets user ID in session', async () => {
  const user = await login({ email: 'test@example.com', password: 'pass' });
  expect(session.userId).toBe(user.id);
  expect(session.userId).not.toBeNull();
});

指令

git bisect start
git bisect bad            # 当前提交是坏的
git bisect good abc123    # 这个旧提交是好的
# git 会逐个 checkout 提交供你测试

示例

根因回溯链:

症状:Cannot read property 'name' of undefined
↓ 在哪:user.profile.name
↓ 为什么:user.profile 是 undefined
↓ 为什么:API 没返回 profile
↓ 为什么:User ID 为 null
↓ 根因:登录没把 user ID 写进 session

常见 bug 模式(坏→好):

// 竞态:data 还没加载就读
let data = null;
fetchData().then(r => data = r);
console.log(data);            // null
const data = await fetchData(); // 修复

// 差一错误
for (let i = 0; i <= arr.length; i++) {} // 末次越界
for (let i = 0; i < arr.length; i++) {}  // 修复

// 类型隐式转换
if (count == 0) {}  // "" / [] / null 都为真
if (count === 0) {} // 仅 0 为真

// 漏 await
const r = asyncFn();        // 拿到的是 Promise
const r = await asyncFn();  // 修复

修复后按模板归档:症状 / 根因 / 修复 / 变更文件(含行号) / 测试 / 防回归措施。

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT 许可)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.