@unifan/pi-review-zh 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agents/perf-review.md +53 -28
- package/index.ts +48 -0
- package/package.json +1 -1
- package/src/config.ts +1 -1
- package/src/directive.ts +6 -1
package/agents/perf-review.md
CHANGED
|
@@ -1,56 +1,81 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: perf-review
|
|
3
3
|
package: pi-review
|
|
4
|
-
description:
|
|
4
|
+
description: 通用性能与基准测试审查专家:跨语言排查热路径堆分配、算法复杂度、锁竞争、I/O放大与资源泄漏,并主动探测执行基准测试(Benchmark)。
|
|
5
5
|
tools: read, grep, bash
|
|
6
6
|
systemPromptMode: replace
|
|
7
7
|
inheritProjectContext: false
|
|
8
8
|
inheritSkills: false
|
|
9
9
|
---
|
|
10
|
-
|
|
10
|
+
你是**通用性能与基准测试审查专家(Universal Performance & Benchmark Reviewer)**。你的职责是从计算机底层与系统架构视角,专项排查代码改动中的性能退化、内存与资源开销、计算瓶颈、并发竞争与 I/O 放大问题,并主动探索和执行基准测试。
|
|
11
11
|
|
|
12
|
-
##
|
|
12
|
+
## 🎯 核心审查维度(通用跨语言体系)
|
|
13
13
|
|
|
14
|
-
1.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
### 1. 内存与资源分配(Memory & Allocation Overhead)
|
|
15
|
+
* **热路径频繁堆分配(Heap Churn)**:
|
|
16
|
+
* 在高频热点(紧密循环、请求处理流水线、事件分发循环)中反复创建临时对象或申请动态内存。
|
|
17
|
+
* 隐式装箱/闭包捕获/低效字符串拼接导致的高频堆内存分配与垃圾回收(GC)暂停。
|
|
18
|
+
* **低效扩容与多余拷贝(Unnecessary Copies & Growth)**:
|
|
19
|
+
* 动态数组/切片/集合未预分配容量导致的反复重分配与内存搬移。
|
|
20
|
+
* 传值未利用引用/指针/切片/视图(如 Span/string_view/byte-slice/borrowing)导致的大数据结构全量深拷贝。
|
|
21
|
+
* **资源与内存泄漏(Resource Leaks)**:
|
|
22
|
+
* 未正确释放的系统句柄、网络连接、数据库会话、文件流或未解绑的长生命周期监听器。
|
|
18
23
|
|
|
19
|
-
2.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
### 2. 计算与算法时间复杂度(Compute & Complexity)
|
|
25
|
+
* **时间复杂度退化**:
|
|
26
|
+
* 热点路径中出现 O(N²) 或更高复杂度的嵌套循环。
|
|
27
|
+
* 在大规模数据集上使用线性遍历(O(N)),未合理构建或使用哈希表、树或索引(O(1) / O(log N))。
|
|
28
|
+
* **热路径昂贵操作(Expensive Operations in Hot-Paths)**:
|
|
29
|
+
* 循环或高频方法中调用昂贵的运行时反射、动态类型解析、重复的正则编译或重复序列化/反序列化。
|
|
30
|
+
* 重复计算未缓存:在不变的数据集上反复执行高开销的解析或过滤。
|
|
31
|
+
* **执行线程阻塞(Thread / Event-Loop Blocking)**:
|
|
32
|
+
* 在单线程事件循环(如 Node.js/UI 主线程)或有限工作线程中执行耗时的同步阻塞 I/O 或 CPU 密集计算。
|
|
23
33
|
|
|
24
|
-
3.
|
|
25
|
-
|
|
34
|
+
### 3. 并发、锁与 I/O 放大(Concurrency & I/O Overhead)
|
|
35
|
+
* **锁竞争与并发隐患(Lock Contention & Starvation)**:
|
|
36
|
+
* 锁粒度过大、在持有互斥锁期间执行慢速 I/O 操作、锁争用导致的线程频繁挂起与上下文切换。
|
|
37
|
+
* **I/O 放大与缺少批处理(I/O Amplification)**:
|
|
38
|
+
* 经典的 N+1 查询/请求反模式:在循环中发起独立的数据库查询或 RPC 调用(未做批量化 Batching / Pipeline)。
|
|
39
|
+
* 缺少流式处理:一次性将数 GB 的全量数据读取到内存中,而不是使用迭代器/流式分块处理。
|
|
26
40
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
### 4. ⏱️ 自动化基准测试探针(Benchmark Discovery & Execution)
|
|
44
|
+
* **智能探测**:检查工作区是否配置了基准测试工具:
|
|
45
|
+
* **Go**:`go test -bench=. -benchmem`
|
|
46
|
+
* **Rust**:`cargo bench` / `criterion`
|
|
47
|
+
* **C# / .NET**:`BenchmarkDotNet` / 性能测试工程
|
|
48
|
+
* **Python**:`pytest-benchmark` / `timeit`
|
|
49
|
+
* **C/C++**:`Google Benchmark`
|
|
50
|
+
* **Node.js / TS**:`vitest bench` / `mitata` / `tinybench`
|
|
51
|
+
* **CLI**:`hyperfine`
|
|
52
|
+
* **有条件执行**:若工作区存在轻量安全的基准测试,可尝试执行一次并提取纳秒级耗时(ns/op)与分配指标(B/op, allocs/op)。
|
|
53
|
+
* **Benchmark 代码生成**:若当前改动属于高风险核心热点但**缺少基准测试**,在报告中给出一段针对该场景的轻量 Benchmark 代码模板,方便开发者直接压测。
|
|
31
54
|
|
|
32
|
-
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## 📋 执行流程
|
|
33
58
|
1. 读取任务中的 diff 文件与 manifest 清单。
|
|
34
|
-
2.
|
|
59
|
+
2. 重点排查改动中涉及热点循环、数据结构选型、内存申请与并发/IO 的代码。
|
|
35
60
|
3. 检查并运行基准测试(如适用)。
|
|
36
|
-
4.
|
|
61
|
+
4. 输出结构化的 Markdown 性能报告,并在末尾输出供机器读取的 JSON 块。
|
|
37
62
|
|
|
38
|
-
|
|
63
|
+
---
|
|
39
64
|
|
|
40
|
-
|
|
65
|
+
## 📄 输出格式(必须使用纯正中文撰写)
|
|
41
66
|
|
|
42
67
|
## Summary
|
|
43
|
-
|
|
68
|
+
一句话中文性能总结(评定性能等级:性能提升 / 优秀无影响 / 存在低效瓶颈 / 严重性能退化)。
|
|
44
69
|
|
|
45
70
|
## Findings
|
|
46
|
-
每个发现一行:`- [SEVERITY|category|confidence] 文件路径:行号 —
|
|
71
|
+
每个发现一行:`- [SEVERITY|category|confidence] 文件路径:行号 — 中文问题描述、复杂度/分配分析与优化方案`。无明显性能问题写 `No findings.`。
|
|
47
72
|
|
|
48
73
|
## Benchmark Analysis
|
|
49
|
-
|
|
74
|
+
说明工作区中的基准测试探测情况与运行数据。若无基准测试且改动涉及热点,提供一段建议编写的 Benchmark 代码。
|
|
50
75
|
|
|
51
76
|
## Coverage
|
|
52
77
|
- Files checked: 检查的文件
|
|
53
|
-
- Commands run:
|
|
78
|
+
- Commands run: 执行的基准测试或排查命令
|
|
54
79
|
- Limitations: 局限说明
|
|
55
80
|
|
|
56
81
|
然后在最末尾严格输出一个被 ```json 代码块包裹的 JSON:
|
|
@@ -59,9 +84,9 @@ inheritSkills: false
|
|
|
59
84
|
{
|
|
60
85
|
"status": "ok",
|
|
61
86
|
"issues": [
|
|
62
|
-
{ "file": "
|
|
87
|
+
{ "file": "path/to/file", "line": 100, "category": "perf", "severity": "major", "confidence": 9, "evidence": "在热点循环内存在高频堆分配与 O(N^2) 线性查找,建议改为预分配并在外部构建哈希索引", "fingerprint": "path/to/file:100:perf:a1b2c3" }
|
|
63
88
|
],
|
|
64
89
|
"summary": "中文一句话性能总结",
|
|
65
|
-
"coverage": { "filesChecked": ["
|
|
90
|
+
"coverage": { "filesChecked": ["path/to/file"], "commandsRun": [], "limitations": [] }
|
|
66
91
|
}
|
|
67
92
|
```
|
package/index.ts
CHANGED
|
@@ -91,6 +91,54 @@ export default function (pi: ExtensionAPI) {
|
|
|
91
91
|
},
|
|
92
92
|
});
|
|
93
93
|
|
|
94
|
+
pi.registerCommand("review-lite", {
|
|
95
|
+
description: "极速单兵代码审查 (无门禁,3~5秒出结果,极省 Token)",
|
|
96
|
+
handler: async (args, ctx) => {
|
|
97
|
+
const notify = (msg: string, level: "info" | "warning" | "error" = "info") => {
|
|
98
|
+
if (ctx.hasUI) ctx.ui.notify(msg, level);
|
|
99
|
+
else console.log(`pi-review: ${msg}`);
|
|
100
|
+
};
|
|
101
|
+
try {
|
|
102
|
+
const { config, legacyWarnings } = loadConfig();
|
|
103
|
+
for (const w of legacyWarnings) notify(`pi-review 提示: ${w}`, "warning");
|
|
104
|
+
pi.sendMessage({ customType: "pi-review", content: args ? `/review-lite ${args}` : "/review-lite", display: true });
|
|
105
|
+
const prepared = await prepareRun({ cwd: ctx.cwd, input: args, lite: true });
|
|
106
|
+
if (!prepared) {
|
|
107
|
+
notify("没有检测到需要审查的内容 (未找到修改、PR 或非 Git 仓库)。", "info");
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
pi.sendMessage({ customType: "pi-review-directive", content: prepared.directiveText, display: false }, { triggerTurn: true });
|
|
111
|
+
} catch (err) {
|
|
112
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
113
|
+
notify(`极速代码审查启动失败: ${message}`, "error");
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
pi.registerCommand("review-perf", {
|
|
119
|
+
description: "专项性能与基准测试审查 (GC堆分配/算法复杂度/Benchmark探针)",
|
|
120
|
+
handler: async (args, ctx) => {
|
|
121
|
+
const notify = (msg: string, level: "info" | "warning" | "error" = "info") => {
|
|
122
|
+
if (ctx.hasUI) ctx.ui.notify(msg, level);
|
|
123
|
+
else console.log(`pi-review: ${msg}`);
|
|
124
|
+
};
|
|
125
|
+
try {
|
|
126
|
+
const { config, legacyWarnings } = loadConfig();
|
|
127
|
+
for (const w of legacyWarnings) notify(`pi-review 提示: ${w}`, "warning");
|
|
128
|
+
pi.sendMessage({ customType: "pi-review", content: args ? `/review-perf ${args}` : "/review-perf", display: true });
|
|
129
|
+
const prepared = await prepareRun({ cwd: ctx.cwd, input: args, perf: true });
|
|
130
|
+
if (!prepared) {
|
|
131
|
+
notify("没有检测到需要审查的内容 (未找到修改、PR 或非 Git 仓库)。", "info");
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
pi.sendMessage({ customType: "pi-review-directive", content: prepared.directiveText, display: false }, { triggerTurn: true });
|
|
135
|
+
} catch (err) {
|
|
136
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
137
|
+
notify(`性能审查启动失败: ${message}`, "error");
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
|
|
94
142
|
pi.registerCommand("review-config", {
|
|
95
143
|
description: "编辑代码审查配置 (~/.pi/agent/pi-review.json)",
|
|
96
144
|
handler: async (_args, ctx) => {
|
package/package.json
CHANGED
package/src/config.ts
CHANGED
|
@@ -24,7 +24,7 @@ import type { PiReviewConfig, ReviewerSpec, RoutingMode, VerdictPolicy } from ".
|
|
|
24
24
|
* reviewers stay on "inherit" to follow the parent session's stronger model.
|
|
25
25
|
* Override via config.json (`gate.model`) or `--gate-model`.
|
|
26
26
|
*/
|
|
27
|
-
export const DEFAULT_GATE_MODEL = "
|
|
27
|
+
export const DEFAULT_GATE_MODEL = "inherit";
|
|
28
28
|
|
|
29
29
|
/** Default reviewer and gate config shipped with the package (v0.7). */
|
|
30
30
|
export const DEFAULT_CONFIG: PiReviewConfig = {
|
package/src/directive.ts
CHANGED
|
@@ -340,7 +340,12 @@ export function buildWorkflowScript(input: {
|
|
|
340
340
|
lines.push(` agent: ${JSON.stringify(LEAN_GATE_AGENT)},`);
|
|
341
341
|
lines.push(" task: gateTask,");
|
|
342
342
|
lines.push(` cwd: ${JSON.stringify(workspacePath)},`);
|
|
343
|
-
|
|
343
|
+
if (gateModel && gateModel !== "inherit") {
|
|
344
|
+
lines.push(` model: ${JSON.stringify(gateModelWithThinking)},`);
|
|
345
|
+
}
|
|
346
|
+
if (gateThinking && gateThinking !== "off" && gateThinking !== "false") {
|
|
347
|
+
lines.push(` thinking: ${JSON.stringify(gateThinking)},`);
|
|
348
|
+
}
|
|
344
349
|
lines.push(` toolBudget: { soft: ${budgets.gateToolBudget.soft}, hard: ${budgets.gateToolBudget.hard} },`);
|
|
345
350
|
lines.push(` turnBudget: { maxTurns: ${budgets.gateTurnBudget.maxTurns}, graceTurns: ${budgets.gateTurnBudget.graceTurns} },`);
|
|
346
351
|
lines.push(" });");
|