iterate-plugin 2.5.0 → 2.7.0
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/README.md +83 -15
- package/dist/config-loader.js +171 -0
- package/dist/config-write.js +174 -0
- package/dist/index.js +58 -0
- package/dist/meta-review.js +181 -0
- package/dist/paths.js +32 -0
- package/dist/review.js +328 -0
- package/dist/skill-prompt.js +337 -0
- package/dist/tools/checkpoint.js +260 -0
- package/dist/tools/config.js +134 -0
- package/dist/tools/context.js +160 -0
- package/dist/tools/decision-log.js +162 -0
- package/dist/tools/fix.js +553 -0
- package/dist/tools/history.js +138 -0
- package/dist/tools/prune.js +268 -0
- package/dist/tools/review.js +159 -0
- package/dist/tools/triage.js +333 -0
- package/dist/tools/validate.js +164 -0
- package/dist/types.js +1 -0
- package/lib/client.js +84 -4
- package/lib/parse.js +68 -0
- package/package.json +7 -3
- package/src/index.ts +4 -0
- package/src/meta-review.ts +14 -1
- package/src/review.ts +33 -5
- package/src/tools/fix.ts +11 -0
- package/src/tools/history.ts +162 -0
- package/src/tools/prune.ts +313 -0
- package/src/tools/triage.ts +7 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Meta-review engine: review a ReviewReport and produce a final review report.
|
|
3
|
+
*
|
|
4
|
+
* This is the "纯反复审查" closing step: after the review loop converges on
|
|
5
|
+
* zero new findings, we don't just trust the aggregated report — we audit the
|
|
6
|
+
* report itself for internal consistency (counts, severity buckets, dimension
|
|
7
|
+
* sums, sort order, convergence math). The result is a deterministic
|
|
8
|
+
* `MetaReviewResult` plus a `FinalReviewReport` that pairs the source report
|
|
9
|
+
* with a verdict.
|
|
10
|
+
*
|
|
11
|
+
* Like `review.ts`, this module contains NO I/O and NO agent spawning — it is
|
|
12
|
+
* the pure, testable core. The workflow script (skill-prompt.ts) orchestrates
|
|
13
|
+
* the actual subagent-driven meta-review critique; all deterministic math
|
|
14
|
+
* lives here.
|
|
15
|
+
*/
|
|
16
|
+
import { sortFindings } from "./review.js";
|
|
17
|
+
/** Number of distinct consistency checks performed by `metaReviewReport`. */
|
|
18
|
+
export const META_REVIEW_CHECKS = 6;
|
|
19
|
+
/**
|
|
20
|
+
* Audit a ReviewReport for internal consistency.
|
|
21
|
+
*
|
|
22
|
+
* Checks (all deterministic, no I/O):
|
|
23
|
+
* 1. COUNT_MATCH: summary.totalFindings === findings.length
|
|
24
|
+
* 2. SEVERITY_SUM: summary severity buckets (critical+high+medium+low) total
|
|
25
|
+
* to summary.totalFindings AND match the actual per-severity counts.
|
|
26
|
+
* 3. DIMENSION_SUM: summary.byDimension values sum to totalFindings and every
|
|
27
|
+
* finding's dimension is present in report.dimensions.
|
|
28
|
+
* 4. SORT_ORDER: findings are severity-sorted (most severe first).
|
|
29
|
+
* 5. CONVERGENCE: findingsByRound sums to totalFindings and the `converged`
|
|
30
|
+
* flag is consistent with the last round's new-finding count.
|
|
31
|
+
* 6. ROUND_SHAPE: every round has a positive round number; no round is
|
|
32
|
+
* missing from the sequence. A round with zero findings is only flagged
|
|
33
|
+
* when it is NOT the last round — an empty FINAL round means the review
|
|
34
|
+
* converged (the last pass found nothing new), which is the expected,
|
|
35
|
+
* successful termination of a dry-run, not a defect.
|
|
36
|
+
*
|
|
37
|
+
* Returns a MetaReviewResult; `passed` is true only when all checks pass.
|
|
38
|
+
*/
|
|
39
|
+
export function metaReviewReport(report) {
|
|
40
|
+
const issues = [];
|
|
41
|
+
const add = (code, severity, summary, detail) => {
|
|
42
|
+
issues.push({ code, severity, summary, detail });
|
|
43
|
+
};
|
|
44
|
+
// Guard: a null/undefined report is a hard failure, not a crash.
|
|
45
|
+
if (!report || typeof report !== 'object') {
|
|
46
|
+
return {
|
|
47
|
+
passed: false,
|
|
48
|
+
verdict: 'revise',
|
|
49
|
+
checksRun: META_REVIEW_CHECKS,
|
|
50
|
+
issues: [
|
|
51
|
+
{
|
|
52
|
+
code: 'REPORT_UNDEFINED',
|
|
53
|
+
severity: 'critical',
|
|
54
|
+
summary: 'Report is missing or not an object',
|
|
55
|
+
detail: 'metaReviewReport received no valid ReviewReport to audit.',
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
const findings = Array.isArray(report.findings) ? report.findings : [];
|
|
61
|
+
const summary = report.summary ?? {};
|
|
62
|
+
const total = Number(summary.totalFindings ?? 0);
|
|
63
|
+
const dimensions = Array.isArray(report.dimensions) ? report.dimensions : [];
|
|
64
|
+
// 1. COUNT_MATCH
|
|
65
|
+
if (total !== findings.length) {
|
|
66
|
+
add('COUNT_MATCH', 'high', `summary.totalFindings (${total}) does not match findings.length (${findings.length})`, `The report claims ${total} findings but lists ${findings.length}.`);
|
|
67
|
+
}
|
|
68
|
+
// 2. SEVERITY_SUM
|
|
69
|
+
const sevCounts = { critical: 0, high: 0, medium: 0, low: 0 };
|
|
70
|
+
for (const f of findings) {
|
|
71
|
+
const s = f?.severity;
|
|
72
|
+
if (s && s in sevCounts)
|
|
73
|
+
sevCounts[s]++;
|
|
74
|
+
}
|
|
75
|
+
const bucketSum = sevCounts.critical + sevCounts.high + sevCounts.medium + sevCounts.low;
|
|
76
|
+
const declaredSeveritySum = Number(summary.critical ?? 0) +
|
|
77
|
+
Number(summary.high ?? 0) +
|
|
78
|
+
Number(summary.medium ?? 0) +
|
|
79
|
+
Number(summary.low ?? 0);
|
|
80
|
+
if (declaredSeveritySum !== total || bucketSum !== total) {
|
|
81
|
+
add('SEVERITY_SUM', 'high', 'Severity bucket counts are inconsistent with totalFindings', `declared buckets sum to ${declaredSeveritySum}, actual buckets sum to ${bucketSum}, ` +
|
|
82
|
+
`but totalFindings is ${total}.`);
|
|
83
|
+
}
|
|
84
|
+
// 3. DIMENSION_SUM
|
|
85
|
+
const byDim = summary.byDimension ?? {};
|
|
86
|
+
let dimSum = 0;
|
|
87
|
+
for (const v of Object.values(byDim))
|
|
88
|
+
dimSum += Number(v) || 0;
|
|
89
|
+
if (dimSum !== total) {
|
|
90
|
+
add('DIMENSION_SUM', 'high', 'byDimension counts do not sum to totalFindings', `byDimension sums to ${dimSum}, but totalFindings is ${total}.`);
|
|
91
|
+
}
|
|
92
|
+
const invalidDim = findings.find((f) => !dimensions.includes(f?.dimension));
|
|
93
|
+
if (invalidDim) {
|
|
94
|
+
add('DIMENSION_UNKNOWN', 'medium', `Finding references unknown dimension "${invalidDim.dimension}"`, `dimension "${invalidDim.dimension}" is not in report.dimensions ` +
|
|
95
|
+
`(${dimensions.join(', ') || 'none'}).`);
|
|
96
|
+
}
|
|
97
|
+
// 4. SORT_ORDER
|
|
98
|
+
const sorted = sortFindings(findings);
|
|
99
|
+
const isSorted = sorted.every((f, i) => f === findings[i]);
|
|
100
|
+
if (!isSorted) {
|
|
101
|
+
add('SORT_ORDER', 'low', 'Findings are not severity-sorted', 'findings should be ordered most-severe first (critical > high > medium > low).');
|
|
102
|
+
}
|
|
103
|
+
// 5. CONVERGENCE
|
|
104
|
+
const findingsByRound = Array.isArray(report.convergence?.findingsByRound)
|
|
105
|
+
? report.convergence.findingsByRound
|
|
106
|
+
: [];
|
|
107
|
+
const convSum = findingsByRound.reduce((a, b) => a + Number(b) || 0, 0);
|
|
108
|
+
if (convSum !== total) {
|
|
109
|
+
add('CONVERGENCE_SUM', 'high', 'convergence.findingsByRound does not sum to totalFindings', `findingsByRound ${JSON.stringify(findingsByRound)} sums to ${convSum}, ` +
|
|
110
|
+
`but totalFindings is ${total}.`);
|
|
111
|
+
}
|
|
112
|
+
// `findingsByRound` is indexed by the actual round number (round r → index
|
|
113
|
+
// r-1), so the "last round" is the LAST RECORDED round's reported number, not
|
|
114
|
+
// the array's last index (the array is sized to the highest round, which only
|
|
115
|
+
// equals the record count for contiguous 1..N round numbers). Read the flag
|
|
116
|
+
// consistency the same way buildReviewReport/computeConvergence set it.
|
|
117
|
+
const reportRounds = Array.isArray(report.rounds) ? report.rounds : [];
|
|
118
|
+
const lastRecordedRound = reportRounds.length > 0 && typeof reportRounds[reportRounds.length - 1]?.round === 'number'
|
|
119
|
+
? reportRounds[reportRounds.length - 1].round
|
|
120
|
+
: null;
|
|
121
|
+
const lastRoundNew = lastRecordedRound !== null && lastRecordedRound > 0
|
|
122
|
+
? Number(findingsByRound[lastRecordedRound - 1] ?? 0)
|
|
123
|
+
: null;
|
|
124
|
+
const expectedConverged = lastRoundNew === 0;
|
|
125
|
+
if (report.convergence?.converged !== expectedConverged) {
|
|
126
|
+
add('CONVERGENCE_FLAG', 'medium', 'convergence.converged flag is inconsistent with the last round', `last round reported ${lastRoundNew} new findings, so converged should be ` +
|
|
127
|
+
`${expectedConverged}, but it is ${report.convergence?.converged}.`);
|
|
128
|
+
}
|
|
129
|
+
// 6. ROUND_SHAPE
|
|
130
|
+
const rounds = Array.isArray(report.rounds) ? report.rounds : [];
|
|
131
|
+
const seenRounds = new Set();
|
|
132
|
+
for (const [index, r] of rounds.entries()) {
|
|
133
|
+
if (!r || typeof r.round !== 'number' || r.round < 1) {
|
|
134
|
+
add('ROUND_NUMBER', 'medium', 'A round has a missing or non-positive round number', `round: ${JSON.stringify(r)}`);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
seenRounds.add(r.round);
|
|
138
|
+
const isLastRound = index === rounds.length - 1;
|
|
139
|
+
if (!Array.isArray(r.findings) || (r.findings.length === 0 && !isLastRound)) {
|
|
140
|
+
add('ROUND_EMPTY', 'low', `Round ${r.round} has no findings`, 'A recorded round should contain at least one finding — except a final converged round, ' +
|
|
141
|
+
'which finding nothing new is the expected success signal.');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
for (let i = 1; i <= rounds.length; i++) {
|
|
145
|
+
if (!seenRounds.has(i)) {
|
|
146
|
+
add('ROUND_GAP', 'medium', `Round ${i} is missing from the round sequence`, `rounds present: ${[...seenRounds].sort((a, b) => a - b).join(', ') || 'none'}.`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const passed = issues.length === 0;
|
|
150
|
+
return {
|
|
151
|
+
passed,
|
|
152
|
+
verdict: passed ? 'approved' : 'revise',
|
|
153
|
+
checksRun: META_REVIEW_CHECKS,
|
|
154
|
+
issues,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Build the final review report: pair the source report with its meta-review
|
|
159
|
+
* verdict and a rolled-up summary. Pure and deterministic.
|
|
160
|
+
*/
|
|
161
|
+
export function buildFinalReviewReport(report) {
|
|
162
|
+
const meta = metaReviewReport(report);
|
|
163
|
+
const summary = report?.summary ?? {};
|
|
164
|
+
const verdict = meta.passed ? 'approved' : 'needs_revision';
|
|
165
|
+
return {
|
|
166
|
+
verdict,
|
|
167
|
+
source: report,
|
|
168
|
+
metaReview: meta,
|
|
169
|
+
summary: {
|
|
170
|
+
totalFindings: Number(summary.totalFindings ?? 0),
|
|
171
|
+
critical: Number(summary.critical ?? 0),
|
|
172
|
+
high: Number(summary.high ?? 0),
|
|
173
|
+
medium: Number(summary.medium ?? 0),
|
|
174
|
+
low: Number(summary.low ?? 0),
|
|
175
|
+
converged: Boolean(report?.convergence?.converged),
|
|
176
|
+
totalRounds: Number(report?.convergence?.totalRounds ?? 0),
|
|
177
|
+
reportIssues: meta.issues.length,
|
|
178
|
+
verdict,
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
}
|
package/dist/paths.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared filesystem layout for the iterate plugin's runtime state.
|
|
3
|
+
*
|
|
4
|
+
* All runtime artifacts live under `<projectRoot>/.iterate/`:
|
|
5
|
+
* .iterate/decision-log.jsonl — append-only decision log
|
|
6
|
+
* .iterate/fixes/ — fix system: backups + fix registry
|
|
7
|
+
* .iterate/checkpoint.json — iteration checkpoint (resume support)
|
|
8
|
+
*
|
|
9
|
+
* Kept separate from config-loader so every tool points at the same dirs.
|
|
10
|
+
*/
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
/** Runtime state root for a project (e.g. `<projectRoot>/.iterate`). */
|
|
13
|
+
export function iterateDir(projectRoot) {
|
|
14
|
+
return join(projectRoot, '.iterate');
|
|
15
|
+
}
|
|
16
|
+
/** Fix-system directory (backups + registry). */
|
|
17
|
+
export function fixesDir(projectRoot) {
|
|
18
|
+
return join(iterateDir(projectRoot), 'fixes');
|
|
19
|
+
}
|
|
20
|
+
/** Fix-registry file (JSON). */
|
|
21
|
+
export function fixRegistryPath(projectRoot) {
|
|
22
|
+
return join(fixesDir(projectRoot), 'registry.json');
|
|
23
|
+
}
|
|
24
|
+
/** Fix-backup file for one fix id + timestamp. */
|
|
25
|
+
export function fixBackupPath(projectRoot, id, timestamp) {
|
|
26
|
+
const safe = id.replace(/[^a-zA-Z0-9_-]/g, '_');
|
|
27
|
+
return join(fixesDir(projectRoot), `${safe}_${timestamp.replace(/[:.]/g, '-')}.bak`);
|
|
28
|
+
}
|
|
29
|
+
/** Iteration checkpoint file (JSON). */
|
|
30
|
+
export function checkpointPath(projectRoot) {
|
|
31
|
+
return join(iterateDir(projectRoot), 'checkpoint.json');
|
|
32
|
+
}
|
package/dist/review.js
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic review engine for the iterate review loop (dry-run and normal).
|
|
3
|
+
*
|
|
4
|
+
* This module contains NO I/O and NO agent spawning — it is the pure,
|
|
5
|
+
* testable core of the multi-round convergence loop:
|
|
6
|
+
*
|
|
7
|
+
* 1. dedupe findings across rounds (file + dimension + normalized summary)
|
|
8
|
+
* 2. filter out `known_intentional` entries from personalization
|
|
9
|
+
* 3. sort by severity (critical > high > medium > low)
|
|
10
|
+
* 4. compute multi-round convergence stats ("纯反复审查" 收敛统计)
|
|
11
|
+
* 5. assemble the ReviewReport
|
|
12
|
+
* 6. build reviewer task prompts + structured-output schema for subagents
|
|
13
|
+
*
|
|
14
|
+
* The workflow script (see skill-prompt.ts) does the orchestration:
|
|
15
|
+
* spawn parallel reviewers, feed back already-known findings each round,
|
|
16
|
+
* and stop when a round yields 0 new findings or the round cap is reached.
|
|
17
|
+
* All deterministic math lives here so it can be unit-tested.
|
|
18
|
+
*/
|
|
19
|
+
/** Severity ordering: lower rank = more severe. */
|
|
20
|
+
export const SEVERITY_RANK = {
|
|
21
|
+
critical: 0,
|
|
22
|
+
high: 1,
|
|
23
|
+
medium: 2,
|
|
24
|
+
low: 3,
|
|
25
|
+
};
|
|
26
|
+
/** Sort findings by severity (most severe first), then by file path. */
|
|
27
|
+
export function sortFindings(findings) {
|
|
28
|
+
return [...findings].sort((a, b) => {
|
|
29
|
+
// Guard against an out-of-spec severity string (e.g. from a model that
|
|
30
|
+
// bypassed the schema): treat it as the least severe so NaN never enters
|
|
31
|
+
// the comparator and ordering stays deterministic.
|
|
32
|
+
const rankA = SEVERITY_RANK[a.severity] ?? SEVERITY_RANK.low;
|
|
33
|
+
const rankB = SEVERITY_RANK[b.severity] ?? SEVERITY_RANK.low;
|
|
34
|
+
const bySeverity = rankA - rankB;
|
|
35
|
+
if (bySeverity !== 0)
|
|
36
|
+
return bySeverity;
|
|
37
|
+
const byFile = a.file.localeCompare(b.file);
|
|
38
|
+
if (byFile !== 0)
|
|
39
|
+
return byFile;
|
|
40
|
+
return (a.line ?? 0) - (b.line ?? 0);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/** Normalize a summary so near-identical duplicates collapse to one key. */
|
|
44
|
+
export function normalizeSummary(summary) {
|
|
45
|
+
return summary
|
|
46
|
+
.trim()
|
|
47
|
+
.toLowerCase()
|
|
48
|
+
.replace(/[\s\n\t]+/g, ' ');
|
|
49
|
+
}
|
|
50
|
+
/** Dedupe key: same file + same dimension + similar summary. */
|
|
51
|
+
export function findingKey(f) {
|
|
52
|
+
return `${f.file}|${f.dimension}|${normalizeSummary(f.summary)}`;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Remove duplicate findings within a list.
|
|
56
|
+
* Keeps the first occurrence of each dedupe key.
|
|
57
|
+
*/
|
|
58
|
+
export function dedupeFindings(findings) {
|
|
59
|
+
const seen = new Set();
|
|
60
|
+
const out = [];
|
|
61
|
+
for (const f of findings) {
|
|
62
|
+
const key = findingKey(f);
|
|
63
|
+
if (seen.has(key))
|
|
64
|
+
continue;
|
|
65
|
+
seen.add(key);
|
|
66
|
+
out.push(f);
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Filter out findings that match a `known_intentional` entry.
|
|
72
|
+
* Match rule (mirrors SKILL.md Phase 1 FILTER):
|
|
73
|
+
* - same `file` AND same `dimension`, AND
|
|
74
|
+
* - entry `line` is 0/undefined (whole file) OR equals the finding's line.
|
|
75
|
+
*/
|
|
76
|
+
export function filterKnownIntentional(findings, known) {
|
|
77
|
+
if (!known || known.length === 0)
|
|
78
|
+
return findings;
|
|
79
|
+
return findings.filter((f) => {
|
|
80
|
+
const matched = known.some((k) => {
|
|
81
|
+
const sameFile = k.file === f.file;
|
|
82
|
+
const sameDim = k.dimension === f.dimension;
|
|
83
|
+
if (!sameFile || !sameDim)
|
|
84
|
+
return false;
|
|
85
|
+
const wholeFile = k.line === undefined || k.line === 0;
|
|
86
|
+
if (wholeFile)
|
|
87
|
+
return true;
|
|
88
|
+
return k.line === f.line;
|
|
89
|
+
});
|
|
90
|
+
return !matched;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Merge per-round findings into one globally-deduped stream while tracking
|
|
95
|
+
* which round first surfaced each finding. This is the deterministic core of
|
|
96
|
+
* "反复多轮审查直至收敛":
|
|
97
|
+
* - `findingsByRound` = number of GLOBALLY new findings first seen in round r,
|
|
98
|
+
* indexed by the actual `round` number (round r → index r-1). The array is
|
|
99
|
+
* sized to the highest round number encountered, so non-contiguous round
|
|
100
|
+
* numbers (e.g. a resumed run that starts at round 5, or a caller that only
|
|
101
|
+
* passes `[{round: 3}]`) still yield correct counts instead of being
|
|
102
|
+
* collapsed onto wrong indices.
|
|
103
|
+
* - `converged` = the last executed round produced 0 new findings
|
|
104
|
+
* - `stoppedReason` = 'converged' | 'max_rounds_reached'
|
|
105
|
+
*/
|
|
106
|
+
export function aggregateRounds(rounds, maxReviewRounds) {
|
|
107
|
+
const seen = new Set();
|
|
108
|
+
const firstRoundByKey = new Map();
|
|
109
|
+
const merged = [];
|
|
110
|
+
// Guard: round numbers are expected to be positive integers. Skip malformed
|
|
111
|
+
// entries defensively rather than letting `firstRoundByKey` key on NaN/0.
|
|
112
|
+
let maxRound = 0;
|
|
113
|
+
for (const round of rounds) {
|
|
114
|
+
if (typeof round.round !== 'number' || !Number.isInteger(round.round) || round.round < 1)
|
|
115
|
+
continue;
|
|
116
|
+
if (round.round > maxRound)
|
|
117
|
+
maxRound = round.round;
|
|
118
|
+
for (const f of round.findings) {
|
|
119
|
+
const key = findingKey(f);
|
|
120
|
+
if (seen.has(key))
|
|
121
|
+
continue;
|
|
122
|
+
seen.add(key);
|
|
123
|
+
firstRoundByKey.set(key, round.round);
|
|
124
|
+
merged.push(f);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const findingsByRound = [];
|
|
128
|
+
for (let r = 1; r <= maxRound; r++) {
|
|
129
|
+
let count = 0;
|
|
130
|
+
for (const key of firstRoundByKey.keys()) {
|
|
131
|
+
if (firstRoundByKey.get(key) === r)
|
|
132
|
+
count++;
|
|
133
|
+
}
|
|
134
|
+
findingsByRound.push(count);
|
|
135
|
+
}
|
|
136
|
+
return { findings: dedupeFindings(merged), findingsByRound, firstRoundByKey };
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Compute convergence statistics for a dry-run review.
|
|
140
|
+
*/
|
|
141
|
+
export function computeConvergence(rounds, maxReviewRounds) {
|
|
142
|
+
const { findingsByRound } = aggregateRounds(rounds, maxReviewRounds);
|
|
143
|
+
const totalRounds = rounds.length;
|
|
144
|
+
// `findingsByRound` is indexed by the actual round number (round r → index
|
|
145
|
+
// r-1), so convergence must read the LAST PRESENT round's count using its
|
|
146
|
+
// reported round number — not `totalRounds - 1`, which is only valid for
|
|
147
|
+
// contiguous 1..N round numbers.
|
|
148
|
+
const lastRound = totalRounds > 0 ? rounds[totalRounds - 1].round : 0;
|
|
149
|
+
const lastRoundCount = lastRound > 0 ? (findingsByRound[lastRound - 1] ?? 0) : 0;
|
|
150
|
+
const converged = totalRounds > 0 && lastRoundCount === 0;
|
|
151
|
+
return {
|
|
152
|
+
totalRounds,
|
|
153
|
+
findingsByRound,
|
|
154
|
+
converged,
|
|
155
|
+
stoppedReason: totalRounds === 0
|
|
156
|
+
? 'max_rounds_reached'
|
|
157
|
+
: converged
|
|
158
|
+
? 'converged'
|
|
159
|
+
: 'max_rounds_reached',
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/** Build a severity/summary breakdown map for the report. */
|
|
163
|
+
function summarize(findings) {
|
|
164
|
+
const summary = {
|
|
165
|
+
totalFindings: findings.length,
|
|
166
|
+
critical: 0,
|
|
167
|
+
high: 0,
|
|
168
|
+
medium: 0,
|
|
169
|
+
low: 0,
|
|
170
|
+
byDimension: {},
|
|
171
|
+
};
|
|
172
|
+
for (const f of findings) {
|
|
173
|
+
if (f.severity === 'critical')
|
|
174
|
+
summary.critical++;
|
|
175
|
+
else if (f.severity === 'high')
|
|
176
|
+
summary.high++;
|
|
177
|
+
else if (f.severity === 'medium')
|
|
178
|
+
summary.medium++;
|
|
179
|
+
else
|
|
180
|
+
summary.low++;
|
|
181
|
+
summary.byDimension[f.dimension] = (summary.byDimension[f.dimension] ?? 0) + 1;
|
|
182
|
+
}
|
|
183
|
+
return summary;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Assemble the final ReviewReport from raw per-round findings.
|
|
187
|
+
* Applies known_intentional filtering, cross-round dedupe, severity sort,
|
|
188
|
+
* and convergence stats in one deterministic pass. Shared by dry-run (pure
|
|
189
|
+
* review) and normal (autonomous loop) modes — the mode only records intent;
|
|
190
|
+
* the math is identical.
|
|
191
|
+
*/
|
|
192
|
+
export function buildReviewReport(input) {
|
|
193
|
+
// 1. Filter known-intentional per round (before cross-round dedupe).
|
|
194
|
+
const filteredRounds = input.rounds.map((r) => ({
|
|
195
|
+
round: r.round,
|
|
196
|
+
findings: filterKnownIntentional(r.findings, input.knownIntentional),
|
|
197
|
+
}));
|
|
198
|
+
// 2. Cross-round dedupe + per-round "first seen" tracking.
|
|
199
|
+
const { findings, findingsByRound } = aggregateRounds(filteredRounds, input.maxReviewRounds);
|
|
200
|
+
// 3. Severity sort the global result.
|
|
201
|
+
const sorted = sortFindings(findings);
|
|
202
|
+
// 4. Convergence. Must be identical to `computeConvergence`: `findingsByRound`
|
|
203
|
+
// is indexed by the actual round number (round r → index r-1) and sized to
|
|
204
|
+
// the highest round, so convergence reads the LAST PRESENT round's count
|
|
205
|
+
// using its reported round number — NOT `filteredRounds.length - 1`, which
|
|
206
|
+
// is only valid for contiguous 1..N round numbers (resumed iterations and
|
|
207
|
+
// non-contiguous round sets would otherwise read the wrong count).
|
|
208
|
+
const lastRound = filteredRounds.length > 0 ? filteredRounds[filteredRounds.length - 1].round : 0;
|
|
209
|
+
const lastRoundCount = lastRound > 0 ? (findingsByRound[lastRound - 1] ?? 0) : 0;
|
|
210
|
+
const converged = filteredRounds.length > 0 && lastRoundCount === 0;
|
|
211
|
+
return {
|
|
212
|
+
mode: input.mode,
|
|
213
|
+
goal: input.goal,
|
|
214
|
+
dimensions: input.dimensions,
|
|
215
|
+
maxReviewRounds: input.maxReviewRounds,
|
|
216
|
+
rounds: filteredRounds,
|
|
217
|
+
findings: sorted,
|
|
218
|
+
convergence: {
|
|
219
|
+
totalRounds: filteredRounds.length,
|
|
220
|
+
findingsByRound,
|
|
221
|
+
converged,
|
|
222
|
+
stoppedReason: filteredRounds.length === 0
|
|
223
|
+
? 'max_rounds_reached'
|
|
224
|
+
: converged
|
|
225
|
+
? 'converged'
|
|
226
|
+
: 'max_rounds_reached',
|
|
227
|
+
},
|
|
228
|
+
summary: summarize(sorted),
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* JSON Schema for reviewer subagent structured output.
|
|
233
|
+
* Object-rooted (dsh `agent` opts.schema requires object-rooted schemas with
|
|
234
|
+
* only type/properties/required/additionalProperties/items/enum/const/oneOf).
|
|
235
|
+
*/
|
|
236
|
+
export function findingsSchema() {
|
|
237
|
+
return {
|
|
238
|
+
type: 'object',
|
|
239
|
+
additionalProperties: false,
|
|
240
|
+
properties: {
|
|
241
|
+
findings: {
|
|
242
|
+
type: 'array',
|
|
243
|
+
items: {
|
|
244
|
+
type: 'object',
|
|
245
|
+
additionalProperties: false,
|
|
246
|
+
properties: {
|
|
247
|
+
dimension: { type: 'string' },
|
|
248
|
+
file: { type: 'string' },
|
|
249
|
+
line: { type: 'integer' },
|
|
250
|
+
severity: { type: 'string', enum: ['critical', 'high', 'medium', 'low'] },
|
|
251
|
+
summary: { type: 'string' },
|
|
252
|
+
failure_scenario: { type: 'string' },
|
|
253
|
+
suggested_fix: { type: 'string' },
|
|
254
|
+
is_atomic: { type: 'boolean' },
|
|
255
|
+
},
|
|
256
|
+
required: [
|
|
257
|
+
'dimension',
|
|
258
|
+
'file',
|
|
259
|
+
'severity',
|
|
260
|
+
'summary',
|
|
261
|
+
'failure_scenario',
|
|
262
|
+
'suggested_fix',
|
|
263
|
+
'is_atomic',
|
|
264
|
+
],
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
required: ['findings'],
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Build the task prompt for one dimension's reviewer subagent.
|
|
273
|
+
* In dry-run mode, pass `alreadyKnown` (the findings from earlier rounds) so the
|
|
274
|
+
* reviewer hunts for NEW issues only — that is what makes "反复审查" converge.
|
|
275
|
+
*/
|
|
276
|
+
export function reviewerTaskPrompt(input) {
|
|
277
|
+
const parts = [];
|
|
278
|
+
parts.push(`You are the "${input.dimension}" reviewer for the iterate review.`, `Goal: ${input.goal}`, `Scope: ${input.scope === 'full' ? 'entire codebase' : 'changed files only'}.`);
|
|
279
|
+
if (input.mode === 'dry-run') {
|
|
280
|
+
parts.push('MODE: dry-run / pure review. You MUST NOT modify, create, or delete ANY file. Read-only analysis only.');
|
|
281
|
+
}
|
|
282
|
+
if (input.alreadyKnown && input.alreadyKnown.length > 0) {
|
|
283
|
+
parts.push('Already-known findings from earlier rounds (do NOT re-report these; find NEW issues only):', JSON.stringify(input.alreadyKnown, null, 2));
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
parts.push('This is round 1 — report every issue you find in this dimension.');
|
|
287
|
+
}
|
|
288
|
+
parts.push(`Return a JSON object: {"findings": [...]}.`, `Each finding: dimension (must be "${input.dimension}"), file (relative path), ` +
|
|
289
|
+
'line (optional integer), severity (critical/high/medium/low), summary (one line), ' +
|
|
290
|
+
'failure_scenario (how/when it fails, specific evidence), suggested_fix (the concrete fix), ' +
|
|
291
|
+
`is_atomic (true if the fix is <= ${input.maxLines} lines within a SINGLE file/function, else false).`, `Write summaries and details in ${input.outputLanguage}.`);
|
|
292
|
+
return parts.join('\n');
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Build a review plan: how many rounds, which dimensions, and the reviewer
|
|
296
|
+
* prompt template for each dimension. Used by the `iterate_review` tool's
|
|
297
|
+
* `plan` operation to give the orchestrator a canonical spec.
|
|
298
|
+
*/
|
|
299
|
+
export function buildReviewPlan(input) {
|
|
300
|
+
// Defensive reads: a malformed config (e.g. `dimensions` as a non-array, or
|
|
301
|
+
// `review`/`atomic` missing) must degrade to sane defaults instead of
|
|
302
|
+
// throwing an uncaught TypeError inside the tool's `execute`.
|
|
303
|
+
const language = input.config.language === 'zh' ? 'Chinese (中文)' : 'English';
|
|
304
|
+
const goal = input.config.goal ?? '';
|
|
305
|
+
const scope = input.config.review?.scope ?? 'full';
|
|
306
|
+
const dimensions = Array.isArray(input.config.dimensions) ? input.config.dimensions : [];
|
|
307
|
+
const maxLines = input.config.atomic?.max_lines ?? 20;
|
|
308
|
+
return {
|
|
309
|
+
mode: input.mode,
|
|
310
|
+
goal,
|
|
311
|
+
scope,
|
|
312
|
+
dimensions: dimensions.map((d) => ({
|
|
313
|
+
id: d,
|
|
314
|
+
reviewerPrompt: reviewerTaskPrompt({
|
|
315
|
+
dimension: d,
|
|
316
|
+
goal,
|
|
317
|
+
scope,
|
|
318
|
+
mode: input.mode,
|
|
319
|
+
alreadyKnown: [],
|
|
320
|
+
outputLanguage: language,
|
|
321
|
+
maxLines,
|
|
322
|
+
}),
|
|
323
|
+
findingsSchema: findingsSchema(),
|
|
324
|
+
})),
|
|
325
|
+
maxReviewRounds: input.maxReviewRounds,
|
|
326
|
+
knownIntentional: input.knownIntentional ?? [],
|
|
327
|
+
};
|
|
328
|
+
}
|