root-cause-tracing by secondsky/claude-skills
npx skills add https://github.com/secondsky/claude-skills --skill root-cause-tracingBug 通常深藏在调用栈中显现(在错误目录中执行 git init、文件创建在错误位置、使用错误路径打开数据库)。你的本能反应是在错误出现的地方修复,但这只是治标不治本。
核心原则: 沿着调用链向后追溯,直到找到原始触发点,然后在源头进行修复。
在以下情况使用:
Error: git init failed in ~/project/packages/core
什么代码直接导致了这个问题?
await execFileAsync('git', ['init'], { cwd: projectDir });
WorktreeManager.createSessionWorktree(projectDir, sessionId)
→ 由 Session.initializeWorkspace() 调用
→ 由 Session.create() 调用
→ 由 Project.create() 处的测试调用
传递了什么值?
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
projectDir = ''(空字符串!)cwd 会解析为 process.cwd()空字符串从哪里来?
const context = setupCoreTest(); // 返回 { tempDir: '' }
Project.create('name', context.tempDir); // 在 beforeEach 之前访问!
当无法手动追溯时,添加检测代码:
// 在有问题的操作之前
async function gitInit(directory: string) {
const stack = new Error().stack;
console.error('DEBUG git init:', {
directory,
cwd: process.cwd(),
nodeEnv: process.env.NODE_ENV,
stack,
});
await execFileAsync('git', ['init'], { cwd: directory });
}
关键点: 在测试中使用 console.error()(而不是 logger - 可能不会显示)
运行并捕获:
bun test 2>&1 | grep 'DEBUG git init'
分析堆栈跟踪:
如果某些东西在测试期间出现,但你不知道是哪个测试:
使用二分查找脚本逐个运行测试:
# 示例:查找哪个测试在错误位置创建 .git
bun test --run --bail 2>&1 | tee test-output.log
逐个运行测试,在第一个污染者处停止。
症状: .git 创建在 packages/core/(源代码目录)
追溯链:
git init 在 process.cwd() 中运行 ← 空的 cwd 参数context.tempDir{ tempDir: '' }根本原因: 顶层变量初始化时访问了空值
修复: 将 tempDir 改为 getter,如果在 beforeEach 之前访问则抛出错误
同时添加纵深防御:
永远不要仅仅修复错误出现的地方。 追溯回去找到原始触发点。
在测试中: 使用 console.error() 而不是 logger - logger 可能被抑制
在操作之前: 在危险操作之前记录日志,而不是在它失败之后
包含上下文: 目录、cwd、环境变量、时间戳
捕获堆栈: new Error().stack 显示完整的调用链
从调试会话中:
每周安装量
79
代码仓库
GitHub 星标
93
首次出现
2026年1月25日
安全审计
安装于
claude-code67
codex64
gemini-cli63
cursor63
opencode62
github-copilot61
Bugs often manifest deep in the call stack (git init in wrong directory, file created in wrong location, database opened with wrong path). Your instinct is to fix where the error appears, but that's treating a symptom.
Core principle: Trace backward through the call chain until you find the original trigger, then fix at the source.
Use when:
Error: git init failed in ~/project/packages/core
What code directly causes this?
await execFileAsync('git', ['init'], { cwd: projectDir });
WorktreeManager.createSessionWorktree(projectDir, sessionId)
→ called by Session.initializeWorkspace()
→ called by Session.create()
→ called by test at Project.create()
What value was passed?
projectDir = '' (empty string!)cwd resolves to process.cwd()Where did empty string come from?
const context = setupCoreTest(); // Returns { tempDir: '' }
Project.create('name', context.tempDir); // Accessed before beforeEach!
When you can't trace manually, add instrumentation:
// Before the problematic operation
async function gitInit(directory: string) {
const stack = new Error().stack;
console.error('DEBUG git init:', {
directory,
cwd: process.cwd(),
nodeEnv: process.env.NODE_ENV,
stack,
});
await execFileAsync('git', ['init'], { cwd: directory });
}
Critical: Use console.error() in tests (not logger - may not show)
Run and capture:
bun test 2>&1 | grep 'DEBUG git init'
Analyze stack traces:
If something appears during tests but you don't know which test:
Use the bisection script to run tests one-by-one:
# Example: find which test creates .git in wrong place
bun test --run --bail 2>&1 | tee test-output.log
Runs tests one-by-one, stops at first polluter.
Symptom: .git created in packages/core/ (source code)
Trace chain:
git init runs in process.cwd() ← empty cwd parametercontext.tempDir before beforeEach{ tempDir: '' } initiallyRoot cause: Top-level variable initialization accessing empty value
Fix: Made tempDir a getter that throws if accessed before beforeEach
Also added defense-in-depth:
NEVER fix just where the error appears. Trace back to find the original trigger.
In tests: Use console.error() not logger - logger may be suppressed Before operation: Log before the dangerous operation, not after it fails Include context: Directory, cwd, environment variables, timestamps Capture stack: new Error().stack shows complete call chain
From debugging session:
Weekly Installs
79
Repository
GitHub Stars
93
First Seen
Jan 25, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
claude-code67
codex64
gemini-cli63
cursor63
opencode62
github-copilot61
后端测试指南:API端点、业务逻辑与数据库测试最佳实践
11,800 周安装