@koreanhole/kandev 0.0.0-stage → 0.1.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 +367 -2
- package/bin/codex-acp.js +16 -0
- package/bin/kandev-auto-sync.mjs +101 -0
- package/bin/kandev-config.js +53 -0
- package/bin/kandev.js +35 -0
- package/config/config.yaml +6 -0
- package/config/docker/projects/opencodex-cli.json +79 -0
- package/config/docker/projects/samsung-bus-tracker.json +91 -0
- package/config/gateways.json +14 -0
- package/config/pipeline.json +150 -0
- package/config/workflows/sdlc.yaml +1019 -0
- package/docker/Dockerfile +68 -0
- package/docker/Dockerfile.opencodex-cli +44 -0
- package/docker/Dockerfile.samsung-bus-tracker +75 -0
- package/package.json +61 -4
- package/scripts/agent-tools-docker.mjs +223 -0
- package/scripts/baseline.mjs +20 -0
- package/scripts/codex-medium +2 -0
- package/scripts/codex-stage +22 -0
- package/scripts/gate.mjs +909 -0
- package/scripts/gateways/docker.mjs +64 -0
- package/scripts/gateways/speckit.mjs +78 -0
- package/scripts/install-prerequisites.mjs +48 -0
- package/scripts/lib/artifacts.mjs +990 -0
- package/scripts/lib/bugfix.mjs +325 -0
- package/scripts/lib/checksum.mjs +166 -0
- package/scripts/lib/docker-workspace.mjs +1209 -0
- package/scripts/lib/gateways.mjs +79 -0
- package/scripts/lib/kandev.mjs +273 -0
- package/scripts/lib/mcp.mjs +111 -0
- package/scripts/lib/pipeline.mjs +548 -0
- package/scripts/lib/planning-replan.mjs +103 -0
- package/scripts/lib/resume.mjs +207 -0
- package/scripts/lib/review-history.mjs +260 -0
- package/scripts/lib/stage-boundary.mjs +146 -0
- package/scripts/lib/stage-model.mjs +23 -0
- package/scripts/lib/task-worktree.mjs +40 -0
- package/scripts/measure-tokens.mjs +121 -0
- package/scripts/planning-tools.mjs +166 -0
- package/scripts/replan-planning.mjs +82 -0
- package/scripts/review-digest.mjs +240 -0
- package/scripts/run-android-e2e.mjs +116 -0
- package/scripts/run-code-review.mjs +281 -0
- package/scripts/run-detached.mjs +163 -0
- package/scripts/run-doc-review.mjs +155 -0
- package/scripts/run-gateways.mjs +138 -0
- package/scripts/sync.mjs +588 -0
- package/scripts/verify-change.mjs +252 -0
- package/scripts/verify-node.mjs +288 -0
- package/scripts/verify.mjs +442 -0
- package/scripts/wait-status.mjs +88 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/** Print read-only Kandev token usage by task and profile. */
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { DatabaseSync } from "node:sqlite";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
|
|
9
|
+
export function defaultDbPath() {
|
|
10
|
+
return path.join(os.homedir(), ".kandev", "data", "kandev.db");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function fail(message) {
|
|
14
|
+
process.stderr.write(`${message}\n`);
|
|
15
|
+
process.exit(2);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function measureDatabase(dbPath) {
|
|
19
|
+
const file = path.resolve(dbPath);
|
|
20
|
+
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
|
|
21
|
+
throw new Error(`database does not exist: ${file}`);
|
|
22
|
+
}
|
|
23
|
+
const db = new DatabaseSync(file, { readOnly: true });
|
|
24
|
+
try {
|
|
25
|
+
const tables = new Set(
|
|
26
|
+
db.prepare("SELECT name FROM sqlite_master WHERE type = 'table'").all().map((row) => row.name),
|
|
27
|
+
);
|
|
28
|
+
for (const table of ["tasks", "task_usage_events", "agent_profiles"]) {
|
|
29
|
+
if (!tables.has(table)) throw new Error(`database is missing table: ${table}`);
|
|
30
|
+
}
|
|
31
|
+
const tasks = db.prepare(`
|
|
32
|
+
SELECT t.id, t.title, t.state,
|
|
33
|
+
COUNT(u.id) AS events,
|
|
34
|
+
COALESCE(SUM(u.tokens_in), 0) AS tokens_in,
|
|
35
|
+
COALESCE(SUM(u.tokens_cached_read), 0) AS cached_read,
|
|
36
|
+
COALESCE(SUM(u.tokens_out), 0) AS output,
|
|
37
|
+
COALESCE(SUM(u.tokens_thought), 0) AS thinking,
|
|
38
|
+
COALESCE(SUM(u.tokens_total), 0) AS total
|
|
39
|
+
FROM tasks t
|
|
40
|
+
JOIN task_usage_events u ON u.task_id = t.id
|
|
41
|
+
GROUP BY t.id, t.title, t.state
|
|
42
|
+
ORDER BY total DESC, t.id ASC
|
|
43
|
+
`).all().map((row) => ({ ...row, profiles: [] }));
|
|
44
|
+
const byTask = new Map(tasks.map((task) => [task.id, task]));
|
|
45
|
+
const profiles = db.prepare(`
|
|
46
|
+
SELECT u.task_id,
|
|
47
|
+
COALESCE(NULLIF(p.name, ''), NULLIF(u.agent_profile_id, ''), '(unknown profile)') AS profile,
|
|
48
|
+
COUNT(u.id) AS events,
|
|
49
|
+
COALESCE(SUM(u.tokens_in), 0) AS tokens_in,
|
|
50
|
+
COALESCE(SUM(u.tokens_cached_read), 0) AS cached_read,
|
|
51
|
+
COALESCE(SUM(u.tokens_out), 0) AS output,
|
|
52
|
+
COALESCE(SUM(u.tokens_thought), 0) AS thinking,
|
|
53
|
+
COALESCE(SUM(u.tokens_total), 0) AS total
|
|
54
|
+
FROM task_usage_events u
|
|
55
|
+
LEFT JOIN agent_profiles p ON p.id = u.agent_profile_id
|
|
56
|
+
GROUP BY u.task_id, u.agent_profile_id, p.name
|
|
57
|
+
ORDER BY total DESC, profile ASC
|
|
58
|
+
`).all();
|
|
59
|
+
for (const row of profiles) byTask.get(row.task_id)?.profiles.push(row);
|
|
60
|
+
return { dbPath: file, tasks };
|
|
61
|
+
} finally {
|
|
62
|
+
db.close();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function formatMeasurement(report) {
|
|
67
|
+
const lines = [`Database: ${report.dbPath}`];
|
|
68
|
+
for (const task of report.tasks) {
|
|
69
|
+
lines.push("");
|
|
70
|
+
lines.push(`Task ${task.title} [${task.id}] ${task.state}`);
|
|
71
|
+
lines.push(
|
|
72
|
+
` events ${task.events} | tokens_in ${task.tokens_in} | cached_read ${task.cached_read} | output ${task.output} | thinking ${task.thinking} | total ${task.total}`,
|
|
73
|
+
);
|
|
74
|
+
for (const profile of task.profiles) {
|
|
75
|
+
lines.push(
|
|
76
|
+
` profile ${profile.profile} | events ${profile.events} | tokens_in ${profile.tokens_in} | cached_read ${profile.cached_read} | output ${profile.output} | thinking ${profile.thinking} | total ${profile.total}`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
if (task.profiles.reduce((events, profile) => events + Number(profile.events), 0) !== Number(task.events)) {
|
|
80
|
+
lines.push(" warning: profile event counts do not sum to task events");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (report.tasks.length === 0) lines.push("No tasks with usage events.");
|
|
84
|
+
return lines.join("\n");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function parse(argv) {
|
|
88
|
+
const options = { db: defaultDbPath() };
|
|
89
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
90
|
+
const arg = argv[index];
|
|
91
|
+
if (arg === "--help") return { help: true };
|
|
92
|
+
if (arg !== "--db") throw new Error(`unknown option ${arg}`);
|
|
93
|
+
const value = argv[index + 1];
|
|
94
|
+
if (!value) throw new Error("--db requires a path");
|
|
95
|
+
options.db = value;
|
|
96
|
+
index += 1;
|
|
97
|
+
}
|
|
98
|
+
return options;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function main() {
|
|
102
|
+
let options;
|
|
103
|
+
try {
|
|
104
|
+
options = parse(process.argv.slice(2));
|
|
105
|
+
} catch (error) {
|
|
106
|
+
fail(error.message);
|
|
107
|
+
}
|
|
108
|
+
if (options.help) {
|
|
109
|
+
console.log("Usage: node scripts/measure-tokens.mjs [--db <path>]");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
console.log(formatMeasurement(measureDatabase(options.db)));
|
|
114
|
+
} catch (error) {
|
|
115
|
+
fail(error.message);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
120
|
+
main();
|
|
121
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/** Finalize planning review hashes and validate the complete planning contract. */
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { PLANNING_STAGE, readStageArtifact } from "./lib/artifacts.mjs";
|
|
7
|
+
import { readReviewRun, readReviewRuns, reviewScopeProblems, reviewSnapshotProblems, snapshotHashes } from "./lib/review-history.mjs";
|
|
8
|
+
import { maybeReexecInTaskContainer } from "./lib/docker-workspace.mjs";
|
|
9
|
+
|
|
10
|
+
const SELF = fileURLToPath(import.meta.url);
|
|
11
|
+
const ROOT = path.resolve(path.dirname(SELF), "..");
|
|
12
|
+
|
|
13
|
+
function fail(message) {
|
|
14
|
+
process.stderr.write(`${message}\n`);
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function readJson(file) {
|
|
19
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function writeJson(file, value) {
|
|
23
|
+
const temp = `${file}.tmp-${process.pid}`;
|
|
24
|
+
fs.writeFileSync(temp, `${JSON.stringify(value, null, 2)}\n`);
|
|
25
|
+
fs.renameSync(temp, file);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function finalizePlanning(cwd) {
|
|
29
|
+
const root = path.resolve(cwd);
|
|
30
|
+
const agentic = path.join(root, ".agentic");
|
|
31
|
+
const specFile = path.join(agentic, "spec.json");
|
|
32
|
+
const reviewFile = path.join(agentic, "spec-review.json");
|
|
33
|
+
if (!fs.existsSync(specFile)) throw new Error(".agentic/spec.json is missing");
|
|
34
|
+
if (!fs.existsSync(reviewFile)) throw new Error(".agentic/spec-review.json is missing");
|
|
35
|
+
const spec = readJson(specFile);
|
|
36
|
+
const review = readJson(reviewFile);
|
|
37
|
+
if (!Array.isArray(spec.documents) || spec.documents.length === 0) {
|
|
38
|
+
throw new Error("spec.json.documents must be a non-empty array");
|
|
39
|
+
}
|
|
40
|
+
const feature = path.resolve(root, String(spec.feature_dir || ""));
|
|
41
|
+
if (!feature.startsWith(`${root}${path.sep}`)) throw new Error("spec.json.feature_dir escapes the worktree");
|
|
42
|
+
|
|
43
|
+
if (spec.change_type === "bugfix") {
|
|
44
|
+
const reproPath = path.join(agentic, "bugfix-repro.mjs");
|
|
45
|
+
if (fs.existsSync(reproPath)) {
|
|
46
|
+
const reproContent = fs.readFileSync(reproPath, "utf8");
|
|
47
|
+
if (/(?:bugfix-runs|\bplanning-before\b|\bimplementation-before\b)/.test(reproContent)) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
".agentic/bugfix-repro.mjs must only write to .agentic/bugfix-result.json; it must not reference or write to .agentic/bugfix-runs/ or historical phase files."
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let run;
|
|
56
|
+
try {
|
|
57
|
+
run = readReviewRun(root, review?.reviewers?.requirement_agent?.run_id);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
review.reviewed_documents = [];
|
|
60
|
+
writeJson(reviewFile, review);
|
|
61
|
+
throw new Error(`review snapshot cannot be loaded: ${error.message}`);
|
|
62
|
+
}
|
|
63
|
+
const runs = readReviewRuns(root);
|
|
64
|
+
if (runs.length > 0 && runs.at(-1).id === run.id) {
|
|
65
|
+
review.rounds = runs.length;
|
|
66
|
+
review.major_history = runs.map((entry) => entry.major);
|
|
67
|
+
const counts = run.run.counts?.findingsBySeverity;
|
|
68
|
+
if (counts && typeof counts === "object") {
|
|
69
|
+
review.blockers = counts.blocker ?? 0;
|
|
70
|
+
review.majors = counts.major ?? 0;
|
|
71
|
+
review.minors = counts.minor ?? 0;
|
|
72
|
+
if (review.reviewers?.requirement_agent && typeof review.reviewers.requirement_agent === "object") {
|
|
73
|
+
review.reviewers.requirement_agent.blockers = counts.blocker ?? 0;
|
|
74
|
+
review.reviewers.requirement_agent.majors = counts.major ?? 0;
|
|
75
|
+
review.reviewers.requirement_agent.minors = counts.minor ?? 0;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
const findings = JSON.parse(fs.readFileSync(path.join(run.dir, "findings.json"), "utf8"));
|
|
80
|
+
if (Array.isArray(findings)) {
|
|
81
|
+
review.major_ids = findings
|
|
82
|
+
.filter((entry) => entry?.severity === "major")
|
|
83
|
+
.map((entry) => entry.stableId ?? entry.id);
|
|
84
|
+
if ((review.blockers ?? 0) > 0 || Array.isArray(review.accepted_blockers)) {
|
|
85
|
+
review.blocker_ids = findings
|
|
86
|
+
.filter((entry) => entry?.severity === "blocker")
|
|
87
|
+
.map((entry) => entry.stableId ?? entry.id);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
} catch {
|
|
91
|
+
// findings.json is optional if unreadable
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const snapshot = snapshotHashes(run.snapshot);
|
|
95
|
+
review.reviewed_documents = spec.documents
|
|
96
|
+
.filter((file) => snapshot.has(file))
|
|
97
|
+
.map((file) => ({ path: file, sha256: snapshot.get(file) }));
|
|
98
|
+
writeJson(reviewFile, review);
|
|
99
|
+
|
|
100
|
+
const scope = reviewScopeProblems(root, feature);
|
|
101
|
+
if (scope.length) throw new Error(scope.join("; "));
|
|
102
|
+
const binding = reviewSnapshotProblems(root, feature, spec.documents, review);
|
|
103
|
+
if (binding.length) throw new Error(binding.join("; "));
|
|
104
|
+
|
|
105
|
+
const isBlocked = Boolean(fs.existsSync(path.join(agentic, "planning-blocked.json")) || review.verdict === "FAIL");
|
|
106
|
+
const result = readStageArtifact(root, PLANNING_STAGE);
|
|
107
|
+
if (!result.ok) {
|
|
108
|
+
if (isBlocked) {
|
|
109
|
+
const nonVerdictProblems = result.problems.filter(
|
|
110
|
+
(problem) => !problem.includes("expected PASS") && !problem.includes("blocker findings remain"),
|
|
111
|
+
);
|
|
112
|
+
if (nonVerdictProblems.length > 0) {
|
|
113
|
+
throw new Error(nonVerdictProblems.join("; "));
|
|
114
|
+
}
|
|
115
|
+
return { ok: true, documents: review.reviewed_documents.length, blocked: true };
|
|
116
|
+
}
|
|
117
|
+
throw new Error(result.problems.join("; "));
|
|
118
|
+
}
|
|
119
|
+
if (isBlocked) {
|
|
120
|
+
return { ok: true, documents: review.reviewed_documents.length, blocked: true };
|
|
121
|
+
}
|
|
122
|
+
return { ok: true, documents: review.reviewed_documents.length };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function parse(argv) {
|
|
126
|
+
const options = { cwd: process.cwd(), mode: null };
|
|
127
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
128
|
+
const key = argv[index];
|
|
129
|
+
if (key === "--help") return { ...options, help: true };
|
|
130
|
+
if (key === "finalize") { options.mode = key; continue; }
|
|
131
|
+
if (key !== "--cwd") throw new Error(`unknown option ${key}`);
|
|
132
|
+
const value = argv[index + 1];
|
|
133
|
+
if (value === undefined) throw new Error("missing value for --cwd");
|
|
134
|
+
options.cwd = value;
|
|
135
|
+
index += 1;
|
|
136
|
+
}
|
|
137
|
+
if (!options.mode) throw new Error("expected finalize");
|
|
138
|
+
return options;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function main() {
|
|
142
|
+
try {
|
|
143
|
+
const options = parse(process.argv.slice(2));
|
|
144
|
+
if (options.help) {
|
|
145
|
+
console.log("Usage: node scripts/planning-tools.mjs finalize --cwd <abs>");
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const reexec = await maybeReexecInTaskContainer({
|
|
149
|
+
cwd: options.cwd,
|
|
150
|
+
scriptPath: SELF,
|
|
151
|
+
root: ROOT,
|
|
152
|
+
});
|
|
153
|
+
if (reexec) {
|
|
154
|
+
process.exitCode = reexec.status;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const result = finalizePlanning(options.cwd);
|
|
158
|
+
process.stdout.write(`${JSON.stringify(result)}\n`);
|
|
159
|
+
} catch (error) {
|
|
160
|
+
fail(error.message);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
165
|
+
main();
|
|
166
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/** Archive a blocked Planning review and let the gate restart from Stage 1. */
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { findTaskRepo, kandevHome } from "./lib/task-worktree.mjs";
|
|
6
|
+
import { resetPlanningAttempt } from "./lib/planning-replan.mjs";
|
|
7
|
+
|
|
8
|
+
function usage() {
|
|
9
|
+
console.log("Usage: node scripts/replan-planning.mjs --task <id> [--reason <text>] [--json] [--force]");
|
|
10
|
+
console.log(" node scripts/replan-planning.mjs --cwd <worktree> [--reason <text>] [--json] [--force]");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function parse(argv) {
|
|
14
|
+
const options = { task: null, cwd: null, reason: "manual planning replan", force: false, json: false };
|
|
15
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
16
|
+
const key = argv[i];
|
|
17
|
+
if (key === "--help") return { help: true };
|
|
18
|
+
if (key === "--force") { options.force = true; continue; }
|
|
19
|
+
if (key === "--json") { options.json = true; continue; }
|
|
20
|
+
if (!["--task", "--cwd", "--reason"].includes(key)) throw new Error(`unknown option ${key}`);
|
|
21
|
+
const value = argv[i + 1];
|
|
22
|
+
if (value === undefined) throw new Error(`missing value for ${key}`);
|
|
23
|
+
options[key.slice(2)] = value;
|
|
24
|
+
i += 1;
|
|
25
|
+
}
|
|
26
|
+
return options;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isBlocked(repoDir) {
|
|
30
|
+
const agentic = path.join(repoDir, ".agentic");
|
|
31
|
+
if (fs.existsSync(path.join(agentic, "planning-blocked.json"))) return true;
|
|
32
|
+
try {
|
|
33
|
+
const review = JSON.parse(fs.readFileSync(path.join(agentic, "spec-review.json"), "utf8"));
|
|
34
|
+
return review?.verdict === "FAIL";
|
|
35
|
+
} catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function main() {
|
|
41
|
+
let options;
|
|
42
|
+
try {
|
|
43
|
+
options = parse(process.argv.slice(2));
|
|
44
|
+
} catch (err) {
|
|
45
|
+
console.error(`FAIL replan: ${err.message}`);
|
|
46
|
+
process.exit(2);
|
|
47
|
+
}
|
|
48
|
+
if (options.help) { usage(); return; }
|
|
49
|
+
let repoDir = options.cwd ? path.resolve(options.cwd) : null;
|
|
50
|
+
if (!repoDir && options.task) {
|
|
51
|
+
const found = findTaskRepo(options.task, kandevHome());
|
|
52
|
+
if (!found) {
|
|
53
|
+
console.error(`FAIL replan: task worktree not found for ${options.task}`);
|
|
54
|
+
process.exit(1);
|
|
55
|
+
}
|
|
56
|
+
repoDir = found.repoDir;
|
|
57
|
+
}
|
|
58
|
+
if (!repoDir) {
|
|
59
|
+
console.error("FAIL replan: pass --task <id> or --cwd <worktree>");
|
|
60
|
+
process.exit(2);
|
|
61
|
+
}
|
|
62
|
+
if (!fs.existsSync(path.join(repoDir, ".agentic"))) {
|
|
63
|
+
console.error(`FAIL replan: .agentic is missing in ${repoDir}`);
|
|
64
|
+
process.exit(1);
|
|
65
|
+
}
|
|
66
|
+
if (!options.force && !isBlocked(repoDir)) {
|
|
67
|
+
console.error("FAIL replan: task is not planning-blocked; use --force to reset anyway");
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
let marker;
|
|
71
|
+
try {
|
|
72
|
+
marker = resetPlanningAttempt(repoDir, { task: options.task, reason: options.reason });
|
|
73
|
+
} catch (err) {
|
|
74
|
+
console.error(`FAIL replan: ${err.message}`);
|
|
75
|
+
process.exit(1);
|
|
76
|
+
}
|
|
77
|
+
const out = { ok: true, repoDir, ...marker };
|
|
78
|
+
if (options.json) console.log(JSON.stringify(out, null, 2));
|
|
79
|
+
else console.log(`replan requested: ${marker.archivedTo}\nthe gate will move the task to 1. Agent Gateway and re-enter Planning with a clean review budget`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
main();
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/** Extract a compact, bounded review summary from the code-review wrapper log. */
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
|
|
7
|
+
const MAX_FINDINGS = 20;
|
|
8
|
+
const MAX_MESSAGE = 240;
|
|
9
|
+
const LOCATION_RE = /([A-Za-z0-9_./-]+\.[A-Za-z0-9]+):(\d+)/;
|
|
10
|
+
const SECTION_SEVERITY_RE = /^\s*(?:#{1,4}\s*)?(?:\*\*)?\[?(CRITICAL|HIGH|MEDIUM|LOW|INFO)\]?(?:\*\*)?\s*:?\s*$/i;
|
|
11
|
+
const INLINE_SEVERITY_RE = /^\s*(?:(?:[-*]|\d+\.)\s*)?(?:#{1,4}\s*)?(?:\*\*)?\[?(CRITICAL|HIGH|MEDIUM|LOW|INFO)\]?(?:\*\*)?(?:\s+|\s*[:—–-]\s*|\s*\])/i;
|
|
12
|
+
const STANDALONE_CONFIDENCE_RE = /^(?:신뢰도|confidence)\s*[::]?\s*(?:high|medium|low)\b.*$/i;
|
|
13
|
+
const SUB_BULLET_RE = /^\s*[-*]\s*(?:\*\*)?(?:위치|근거|조치|설명|원인|영향|신뢰도|confidence)(?:\*\*)?(?:\b|[::\s]|$)/i;
|
|
14
|
+
const LOCATION_LABEL_RE = /^\s*[-*]?\s*(?:\*\*)?(?:위치|파일|file|location)(?:\*\*)?(?:\b|[::\s]|$)/i;
|
|
15
|
+
|
|
16
|
+
function clean(value) {
|
|
17
|
+
return String(value || "")
|
|
18
|
+
.replace(/^\s*[-*]\s*/, "")
|
|
19
|
+
.replace(/\*\*/g, "")
|
|
20
|
+
.replace(/^\*+/, "")
|
|
21
|
+
.replace(/\x60/g, "")
|
|
22
|
+
.trim();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function extractInlineMessage(line, hasInlineLocation = false) {
|
|
26
|
+
let text = String(line || "")
|
|
27
|
+
.replace(/^\s*(?:[-*]|\d+\.)\s*/, "")
|
|
28
|
+
.replace(/^(?:#{1,4}\s*)?/, "")
|
|
29
|
+
.replace(/^(?:\*\*)?\[?(?:CRITICAL|HIGH|MEDIUM|LOW|INFO)\]?(?:\*\*)?\s*(?:[:—–-]\s*)?/i, "");
|
|
30
|
+
if (hasInlineLocation) {
|
|
31
|
+
text = text.replace(
|
|
32
|
+
/^(?:.*?[A-Za-z0-9_./-]+\.[A-Za-z0-9]+:\d+(?:-\d+)?[`*)\s,]*(?:\([^)]*\)\s*)?(?:[—–-]|:\s+)\s*)/,
|
|
33
|
+
"",
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
text = text.replace(/\s*(?:신뢰도|confidence)\s*[::]?\s*(?:high|medium|low)[^.]*\.?\s*$/i, "");
|
|
37
|
+
return clean(text);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function extractLastJson(text) {
|
|
41
|
+
const source = String(text || "");
|
|
42
|
+
for (let start = source.lastIndexOf("{"); start >= 0; start = source.lastIndexOf("{", start - 1)) {
|
|
43
|
+
try {
|
|
44
|
+
const value = JSON.parse(source.slice(start));
|
|
45
|
+
if (value && typeof value === "object") return value;
|
|
46
|
+
} catch {
|
|
47
|
+
// Keep scanning for the last complete object.
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
throw new Error("review log does not contain a JSON RunSummary");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function parseFindings(markdown) {
|
|
54
|
+
const lines = String(markdown || "").split("\n");
|
|
55
|
+
const findings = [];
|
|
56
|
+
let current = null;
|
|
57
|
+
let sectionSeverity = null;
|
|
58
|
+
const push = () => {
|
|
59
|
+
if (!current) return;
|
|
60
|
+
if (current.message || current.file) findings.push(current);
|
|
61
|
+
current = null;
|
|
62
|
+
};
|
|
63
|
+
for (const line of lines) {
|
|
64
|
+
if (line.includes("CRITICAL/HIGH") || /^\s*(?:\*\*)?판정\b/.test(line)) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const sectionMatch = SECTION_SEVERITY_RE.exec(line);
|
|
68
|
+
if (sectionMatch) {
|
|
69
|
+
push();
|
|
70
|
+
sectionSeverity = sectionMatch[1].toUpperCase();
|
|
71
|
+
current = { severity: sectionSeverity, file: null, line: null, message: "" };
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const inlineMatch = INLINE_SEVERITY_RE.exec(line);
|
|
75
|
+
if (inlineMatch) {
|
|
76
|
+
push();
|
|
77
|
+
const sev = inlineMatch[1].toUpperCase();
|
|
78
|
+
sectionSeverity = sev;
|
|
79
|
+
current = { severity: sev, file: null, line: null, message: "" };
|
|
80
|
+
const location = LOCATION_RE.exec(line);
|
|
81
|
+
if (location) {
|
|
82
|
+
current.file = location[1];
|
|
83
|
+
current.line = Number(location[2]);
|
|
84
|
+
}
|
|
85
|
+
const msg = extractInlineMessage(line, Boolean(location));
|
|
86
|
+
if (msg && !/^[#*]+$/.test(msg) && !STANDALONE_CONFIDENCE_RE.test(msg)) {
|
|
87
|
+
current.message = msg.slice(0, MAX_MESSAGE);
|
|
88
|
+
}
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (
|
|
92
|
+
sectionSeverity &&
|
|
93
|
+
current &&
|
|
94
|
+
(current.file || current.message) &&
|
|
95
|
+
/^\s*(?:[-*]|\d+\.)\s+/.test(line) &&
|
|
96
|
+
!SUB_BULLET_RE.test(line) &&
|
|
97
|
+
LOCATION_RE.test(line)
|
|
98
|
+
) {
|
|
99
|
+
push();
|
|
100
|
+
const location = LOCATION_RE.exec(line);
|
|
101
|
+
const msg = extractInlineMessage(line, true);
|
|
102
|
+
current = {
|
|
103
|
+
severity: sectionSeverity,
|
|
104
|
+
file: location[1],
|
|
105
|
+
line: Number(location[2]),
|
|
106
|
+
message: msg && !STANDALONE_CONFIDENCE_RE.test(msg) ? msg.slice(0, MAX_MESSAGE) : "",
|
|
107
|
+
};
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (!current) continue;
|
|
111
|
+
if (!current.file) {
|
|
112
|
+
const location = LOCATION_RE.exec(line);
|
|
113
|
+
if (location) {
|
|
114
|
+
current.file = location[1];
|
|
115
|
+
current.line = Number(location[2]);
|
|
116
|
+
if (!LOCATION_LABEL_RE.test(line) && !current.message) {
|
|
117
|
+
const msg = extractInlineMessage(line, true);
|
|
118
|
+
if (msg && !/^[#*]+$/.test(msg) && !STANDALONE_CONFIDENCE_RE.test(msg)) {
|
|
119
|
+
current.message = msg.slice(0, MAX_MESSAGE);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (!current.message) {
|
|
126
|
+
const message = clean(line)
|
|
127
|
+
.replace(/\s*(?:신뢰도|confidence)\s*[::]?\s*(?:high|medium|low)[^.]*\.?\s*$/i, "")
|
|
128
|
+
.trim();
|
|
129
|
+
if (message && !/^[#*]+$/.test(message) && !STANDALONE_CONFIDENCE_RE.test(message)) {
|
|
130
|
+
current.message = message.slice(0, MAX_MESSAGE);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (findings.length >= MAX_FINDINGS) break;
|
|
134
|
+
}
|
|
135
|
+
push();
|
|
136
|
+
const seen = new Set();
|
|
137
|
+
return findings
|
|
138
|
+
.filter((finding) => {
|
|
139
|
+
const key = finding.severity + "|" + (finding.file || "") + "|" + (finding.line || "") + "|" + finding.message;
|
|
140
|
+
if (seen.has(key)) return false;
|
|
141
|
+
seen.add(key);
|
|
142
|
+
return true;
|
|
143
|
+
})
|
|
144
|
+
.slice(0, MAX_FINDINGS);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function buildReviewDigest(run) {
|
|
148
|
+
const context = run.context || run.target || {};
|
|
149
|
+
const reviewers = Array.isArray(run.reviewers) ? run.reviewers : [];
|
|
150
|
+
const findings = [];
|
|
151
|
+
const unparsed = [];
|
|
152
|
+
const mappedReviewers = reviewers.map((reviewer) => {
|
|
153
|
+
const parsed = parseFindings(reviewer.findings);
|
|
154
|
+
const role = reviewer.roleId || reviewer.role || "";
|
|
155
|
+
for (const finding of parsed) findings.push({ ...finding, reviewer: role });
|
|
156
|
+
const text = String(reviewer.findings || "").trim();
|
|
157
|
+
if (
|
|
158
|
+
text &&
|
|
159
|
+
!/^(?:발견\s*사항\s*없음|지적\s*사항\s*없음|결함\s*없음|no\s+findings\.?)$/i.test(text) &&
|
|
160
|
+
parsed.length === 0
|
|
161
|
+
) {
|
|
162
|
+
unparsed.push(role);
|
|
163
|
+
}
|
|
164
|
+
return {
|
|
165
|
+
role,
|
|
166
|
+
status: reviewer.status || "",
|
|
167
|
+
duration_ms: Number(reviewer.durationMs || 0),
|
|
168
|
+
model: reviewer.model || "",
|
|
169
|
+
finding_count: parsed.length,
|
|
170
|
+
findings: parsed,
|
|
171
|
+
};
|
|
172
|
+
});
|
|
173
|
+
const seen = new Set();
|
|
174
|
+
const compactFindings = findings.filter((finding) => {
|
|
175
|
+
const key = finding.severity + "|" + (finding.file || "") + "|" + (finding.line || "") + "|" + finding.message;
|
|
176
|
+
if (seen.has(key)) return false;
|
|
177
|
+
seen.add(key);
|
|
178
|
+
return true;
|
|
179
|
+
}).slice(0, MAX_FINDINGS);
|
|
180
|
+
return {
|
|
181
|
+
run_id: run.id || run.runId || "",
|
|
182
|
+
status: run.status || "",
|
|
183
|
+
mode: context.mode || run.mode || "",
|
|
184
|
+
changed_files: Array.isArray(context.changedFiles)
|
|
185
|
+
? context.changedFiles
|
|
186
|
+
: Array.isArray(run.changedFiles)
|
|
187
|
+
? run.changedFiles
|
|
188
|
+
: [],
|
|
189
|
+
diff_stat: context.diffStat || run.diffStat || "",
|
|
190
|
+
diff_bytes: context.diffBytes ?? run.diffBytes ?? null,
|
|
191
|
+
prompt_chars: context.promptChars ?? run.promptChars ?? null,
|
|
192
|
+
reviewers: mappedReviewers,
|
|
193
|
+
findings: compactFindings,
|
|
194
|
+
finding_count: compactFindings.length,
|
|
195
|
+
unparsed_reviewers: unparsed,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function parse(argv) {
|
|
200
|
+
const options = { cwd: process.cwd(), log: ".agentic/code-review.log" };
|
|
201
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
202
|
+
const key = argv[index];
|
|
203
|
+
if (key === "--help") return { help: true };
|
|
204
|
+
if (!["--cwd", "--log"].includes(key)) throw new Error("unknown option " + key);
|
|
205
|
+
const value = argv[index + 1];
|
|
206
|
+
if (value === undefined) throw new Error("missing value for " + key);
|
|
207
|
+
if (key === "--cwd") options.cwd = value;
|
|
208
|
+
if (key === "--log") options.log = value;
|
|
209
|
+
index += 1;
|
|
210
|
+
}
|
|
211
|
+
return options;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function main() {
|
|
215
|
+
let options;
|
|
216
|
+
try {
|
|
217
|
+
options = parse(process.argv.slice(2));
|
|
218
|
+
} catch (error) {
|
|
219
|
+
process.stderr.write(error.message + "\n");
|
|
220
|
+
process.exitCode = 2;
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (options.help) {
|
|
224
|
+
process.stdout.write("Usage: node scripts/review-digest.mjs [--cwd <dir>] [--log <file>]\n");
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
const log = path.resolve(options.cwd, options.log);
|
|
229
|
+
const digest = buildReviewDigest(extractLastJson(fs.readFileSync(log, "utf8")));
|
|
230
|
+
process.stdout.write(JSON.stringify(digest) + "\n");
|
|
231
|
+
} catch (error) {
|
|
232
|
+
process.stderr.write(error.message + "\n");
|
|
233
|
+
process.exitCode = 2;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
238
|
+
main();
|
|
239
|
+
}
|
|
240
|
+
|