-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplan.txt
More file actions
108 lines (91 loc) · 4.5 KB
/
Copy pathplan.txt
File metadata and controls
108 lines (91 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
D1:CLI + 日志解析 + AST 提取
你是一个资深 Python 工具链工程师。请实现 `log2repro` 的 D1 核心模块:
1. `cli.py`: 使用 typer 实现 `log2repro run <input_path_or_text> [--model gpt-4o] [--dry-run]`
- 支持 stdin、文件路径、字符串直接传入
- 输出结构化 JSON 到 stdout 或保存为 `repro_context.json`
2. `parsers/base.py` & `parsers/stacktrace.py`:
- 正则提取 traceback 核心段:文件名、行号、异常类型、关键变量名、调用链
- 返回 Pydantic 模型 `ParsedTrace(file: str, line: int, error: str, context_vars: list[str], chain: list[str])`
3. `extractors/ast_parser.py`:
- 使用 Python 内置 `ast` 模块解析指定源码文件
- 提取:函数签名、import 语句、全局变量、类型注解
- 过滤掉无关代码,只保留与 traceback 调用链相关的 AST 节点
- 返回 `ASTContext(signature: str, imports: list[str], known_vars: dict)`
约束:
- 全量类型注解 + docstring
- 每个模块附带 pytest 单元测试(使用 `tests/fixtures/trace_sample.log`)
- 禁用网络请求,纯本地解析
- 输出完整文件内容,不要省略
D2:LLM Prompt 工程 + 代码生成
继续实现 D2:LLM 驱动的代码生成模块。
1. `generators/prompts.py`: 定义 3 个严格约束的 System Prompt:
- 角色:Python 调试专家
- 输入格式:ParsedTrace + ASTContext
- 输出要求:必须包含 ① reproduce.py ② requirements.txt ③ mock_data.json
- 强约束:禁止编造不存在的库/变量;使用标准库 mock;所有外部调用必须打桩;代码必须可独立运行
2. `generators/code_gen.py`:
- 使用 litellm.completion() 调用模型
- 支持 temperature=0.2, max_tokens=2000
- 使用 Jinja2 渲染 prompt 模板,注入 AST 提取结果作为硬约束
- 解析 LLM 返回的 Markdown 代码块,拆分为独立文件
- 失败重试 2 次,记录 retry log
约束:
- 实现 `generate_repro_script(context: ReproContext) -> dict[str, str]`
- 附带 mock LLM 响应的单元测试
- 输出完整文件
D3:沙箱验证 + 自动修复 + 输出打包
实现 D3:沙箱执行验证与自动修复链路。
1. `validators/sandbox.py`:
- 创建临时 venv,写入 requirements.txt 并 pip install
- 使用 subprocess 运行 reproduce.py,设置 timeout=10s
- 捕获 stdout/stderr/exit_code
- 验证是否成功触发原始异常(或按预期 mock 返回)
2. `validators/auto_fix.py`:
- 若沙箱报错(SyntaxError/ModuleNotFound/ImportError):
* 提取新报错信息
* 将原代码+新报错喂回 LLM 要求局部修复
* 最多重试 3 次,每次缩小修复范围
- 若 3 次失败,降级为 --dry-run 模式并输出人工修复建议
3. CLI 整合:
- `log2repro run` 最终输出:reproduce.py, requirements.txt, mock_data.json, README_repro.md
- 支持 `--output-dir ./repro_out` 自动创建目录
约束:
- 沙箱禁用网络(通过环境变量/pytest-mock 模拟)
- 全链路结构化日志(使用 rich 或 logging)
- 输出完整文件,含测试用例
三、Sentry Webhook 自动化对接示例
让 log2repro 能无缝嵌入现有监控流,大幅提升项目实用性:
# .github/workflows/sentry-auto-repro.yml
name: Sentry Issue → Auto Repro
on:
workflow_dispatch:
schedule: [{cron: '*/30 * * * *'}] # 实际可用 Sentry Webhook 触发
jobs:
generate-repro:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install log2repro
run: pip install log2repro
- name: Fetch New Sentry Issues
uses: getsentry/action-fetch-issues@v1
with:
org: your-org
project: your-project
token: ${{ secrets.SENTRY_TOKEN }}
output: sentry_issues.json
- name: Generate Reproduction Scripts
run: |
cat sentry_issues.json | jq -c '.[]' | while read -r issue; do
id=$(echo $issue | jq -r .id)
echo "$issue" | log2repro run --model gpt-4o --output-dir repros/$id
done
- name: Commit & Create PR
run: |
git add repros/
git commit -m "chore: auto-generate repro scripts for ${{ github.run_id }}"
gh pr create --title "🔍 Auto Repro Scripts" --body "Generated by log2repro"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
定位话术(写入 README):
log2repro 不替代 Sentry/Langfuse,而是它们的 Debug 加速器。Sentry 告诉你“哪里错了”,log2repro 直接给你“怎么在本地跑出来