@mmerterden/multi-agent-pipeline 20.33.0 → 20.34.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.
Files changed (95) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/README.md +2 -1
  3. package/README.tr.md +2 -1
  4. package/docs/adr/0018-cross-repo-links-not-a-global-graph.md +43 -0
  5. package/docs/adr/README.md +2 -1
  6. package/docs/facts.json +5 -4
  7. package/docs/features.md +1 -1
  8. package/install/_dev-only-files.mjs +1 -0
  9. package/install/catalog-history.json +1 -1
  10. package/install/claude.mjs +9 -2
  11. package/install/templates/claude-hooks.json +12 -1
  12. package/manifest.json +98 -75
  13. package/package.json +3 -1
  14. package/pipeline/agents/code-reviewer.md +9 -0
  15. package/pipeline/agents/explorer.md +4 -0
  16. package/pipeline/agents/plan-critic.md +4 -2
  17. package/pipeline/commands/multi-agent/SKILL.md +17 -136
  18. package/pipeline/commands/multi-agent/channels/SKILL.md +15 -297
  19. package/pipeline/commands/multi-agent/graph/SKILL.md +5 -2
  20. package/pipeline/commands/multi-agent/language/SKILL.md +3 -3
  21. package/pipeline/commands/multi-agent/resume/SKILL.md +8 -22
  22. package/pipeline/commands/multi-agent/review/SKILL.md +7 -131
  23. package/pipeline/commands/multi-agent/setup/SKILL.md +19 -224
  24. package/pipeline/contract/README.md +5 -2
  25. package/pipeline/multi-agent-refs/channels/generator.md +146 -0
  26. package/pipeline/multi-agent-refs/channels/menu.md +55 -0
  27. package/pipeline/multi-agent-refs/channels/multi-repo.md +6 -0
  28. package/pipeline/multi-agent-refs/channels/preview.md +25 -0
  29. package/pipeline/multi-agent-refs/channels/reference.md +98 -0
  30. package/pipeline/multi-agent-refs/features/autopilot-circuit-breaker.md +1 -1
  31. package/pipeline/multi-agent-refs/features/code-graph.md +27 -0
  32. package/pipeline/multi-agent-refs/features/office-threat-model.md +1 -1
  33. package/pipeline/multi-agent-refs/features/plan-critic.md +5 -2
  34. package/pipeline/multi-agent-refs/features/review-delta.md +4 -1
  35. package/pipeline/multi-agent-refs/features/scope-check.md +10 -1
  36. package/pipeline/multi-agent-refs/knowledge.md +9 -0
  37. package/pipeline/multi-agent-refs/orchestrator/dispatcher.md +163 -0
  38. package/pipeline/multi-agent-refs/phases/phase-1-plan.md +4 -4
  39. package/pipeline/multi-agent-refs/phases/phase-2-dev.md +9 -8
  40. package/pipeline/multi-agent-refs/phases/phase-3-review.md +12 -7
  41. package/pipeline/multi-agent-refs/phases/phase-4-commit.md +7 -3
  42. package/pipeline/multi-agent-refs/phases/phase-5-report.md +6 -3
  43. package/pipeline/multi-agent-refs/picker-contract.md +1 -1
  44. package/pipeline/multi-agent-refs/resume/details.md +35 -0
  45. package/pipeline/multi-agent-refs/review/inputs.md +87 -0
  46. package/pipeline/multi-agent-refs/review/module-guides.md +25 -0
  47. package/pipeline/multi-agent-refs/review/valven.md +42 -0
  48. package/pipeline/multi-agent-refs/setup/accounts.md +27 -0
  49. package/pipeline/multi-agent-refs/setup/app-store-connect.md +5 -0
  50. package/pipeline/multi-agent-refs/setup/firebase.md +5 -0
  51. package/pipeline/multi-agent-refs/setup/google-play.md +4 -0
  52. package/pipeline/multi-agent-refs/setup/hooks-and-plugins.md +21 -0
  53. package/pipeline/multi-agent-refs/setup/language.md +46 -0
  54. package/pipeline/multi-agent-refs/setup/multi-repo-group.md +51 -0
  55. package/pipeline/multi-agent-refs/setup/summaries.md +63 -0
  56. package/pipeline/multi-agent-refs/setup/token-save-flow.md +51 -0
  57. package/pipeline/office/index.html +12 -7
  58. package/pipeline/office/toolkit.json +1 -1
  59. package/pipeline/rules/response-shape.md +6 -7
  60. package/pipeline/schemas/agent-state.schema.json +18 -0
  61. package/pipeline/schemas/phases.json +4 -4
  62. package/pipeline/schemas/plan-critique.schema.json +9 -2
  63. package/pipeline/schemas/planning-output.schema.json +25 -2
  64. package/pipeline/schemas/prefs.schema.json +24 -1
  65. package/pipeline/schemas/reviewer-output.schema.json +20 -2
  66. package/pipeline/schemas/token-budget.json +4 -4
  67. package/pipeline/schemas/triage-output.schema.json +20 -2
  68. package/pipeline/scripts/_graph-analysis.mjs +270 -0
  69. package/pipeline/scripts/_memory-policy.mjs +165 -0
  70. package/pipeline/scripts/_secret-scan.mjs +105 -0
  71. package/pipeline/scripts/_unified-diff.mjs +2 -2
  72. package/pipeline/scripts/capture-flush.sh +12 -2
  73. package/pipeline/scripts/check-read-size.sh +9 -0
  74. package/pipeline/scripts/code-graph-rules/android.json +8 -3
  75. package/pipeline/scripts/contract-server.mjs +36 -15
  76. package/pipeline/scripts/debt-ledger.mjs +323 -0
  77. package/pipeline/scripts/file-context.mjs +261 -0
  78. package/pipeline/scripts/file-context.sh +20 -0
  79. package/pipeline/scripts/graph-links.mjs +230 -0
  80. package/pipeline/scripts/graph-query.mjs +133 -1
  81. package/pipeline/scripts/graph-report.mjs +65 -0
  82. package/pipeline/scripts/learn-from-transcripts.mjs +12 -48
  83. package/pipeline/scripts/learnings-ledger.mjs +143 -24
  84. package/pipeline/scripts/plan-critique-gate.mjs +11 -2
  85. package/pipeline/scripts/rework-rulings.mjs +106 -0
  86. package/pipeline/scripts/scope-check-gate.mjs +149 -2
  87. package/pipeline/scripts/triage-memory.mjs +64 -37
  88. package/pipeline/scripts/validate-planning.mjs +68 -4
  89. package/pipeline/scripts/validate-reviewer.mjs +20 -0
  90. package/pipeline/scripts/validate-triage.mjs +107 -2
  91. package/pipeline/skills/.skill-manifest.json +5 -5
  92. package/pipeline/skills/shared/core/multi-agent-graph/SKILL.md +5 -2
  93. package/pipeline/skills/shared/core/multi-agent-language/SKILL.md +4 -4
  94. package/pipeline/skills/shared/core/multi-agent-review/SKILL.md +2 -0
  95. package/pipeline/skills/shared/core/multi-agent-setup/SKILL.md +4 -4
@@ -14,7 +14,10 @@
14
14
  * run re-discovers the same structure and reviewers re-flag preferences the team
15
15
  * already rejected.
16
16
  *
17
- * Each line conforms to pipeline/schemas/learnings-ledger.schema.json.
17
+ * Each line conforms to pipeline/schemas/learnings-ledger.schema.json. Every
18
+ * entry passes the secret scan in _secret-scan.mjs before it is written, and
19
+ * `<private>` spans are removed first; a refused entry is counted in
20
+ * `droppedBySecretScan`.
18
21
  *
19
22
  * Subcommands:
20
23
  * add --kind <fact|convention|rejected-preference> --statement "..."
@@ -43,6 +46,12 @@
43
46
  * severity rejections are NOT distilled - a wrong rejection of a
44
47
  * blocking issue must never permanently suppress that class. Exit 2
45
48
  * when nothing new was distilled (a repeat of the same triage).
49
+ * feedback --id <id> --outcome <useful|dead_end|corrected> [--task <id>] [--repo-slug <slug>]
50
+ * Record how an injected entry fared. An entry marked corrected,
51
+ * or judged a dead end at least twice and more often than useful,
52
+ * leaves brief and profile; one useful at least twice ranks by
53
+ * its strength too (_memory-policy.mjs). Kept beside the ledger
54
+ * in learnings-feedback.jsonl.
46
55
  * forget [--statement "..."] [--kind <kind>] [--repo-slug <slug>]
47
56
  * Remove ledger entries matching the statement substring and/or
48
57
  * kind (rewrites the file). Use to clear a bad/stale learning.
@@ -70,7 +79,15 @@ import {
70
79
  import { homedir } from "node:os";
71
80
  import { execFileSync } from "node:child_process";
72
81
  import { join, basename, dirname } from "node:path";
73
- import { bm25, recencyRank, rrf, tokens, shortId, recallConfig } from "./_retrieval.mjs";
82
+ import { bm25, recencyRank, rrf, tokens, recallConfig } from "./_retrieval.mjs";
83
+ import {
84
+ checkedSlug,
85
+ ledgerRowId,
86
+ memoryExcluded,
87
+ nearDuplicate,
88
+ standing,
89
+ } from "./_memory-policy.mjs";
90
+ import { openScanner, stripPrivate } from "./_secret-scan.mjs";
74
91
 
75
92
  const argv = process.argv.slice(2);
76
93
  const SUB = argv[0];
@@ -110,6 +127,10 @@ function die(msg, code = 1) {
110
127
  // the learning replay always miss. `--git-common-dir` points at the main
111
128
  // repository's .git for every linked worktree.
112
129
  function repoSlug() {
130
+ return checkedSlug(rawRepoSlug());
131
+ }
132
+
133
+ function rawRepoSlug() {
113
134
  if (flags["repo-slug"]) return String(flags["repo-slug"]);
114
135
  if (process.env.MULTI_AGENT_REPO_SLUG) return process.env.MULTI_AGENT_REPO_SLUG;
115
136
  try {
@@ -150,6 +171,36 @@ function ledgerPath(slug) {
150
171
  );
151
172
  }
152
173
 
174
+ function feedbackPath(slug) {
175
+ return join(dirname(ledgerPath(slug)), "learnings-feedback.jsonl");
176
+ }
177
+
178
+ /** id -> outcome rows recorded against it. */
179
+ function readFeedback(slug) {
180
+ const p = feedbackPath(slug);
181
+ const byId = new Map();
182
+ if (!existsSync(p)) return byId;
183
+ for (const line of readFileSync(p, "utf8").split("\n")) {
184
+ if (!line.trim()) continue;
185
+ try {
186
+ const f = JSON.parse(line);
187
+ if (!byId.has(f.id)) byId.set(f.id, []);
188
+ byId.get(f.id).push(f);
189
+ } catch {
190
+ /* skip corrupted line */
191
+ }
192
+ }
193
+ return byId;
194
+ }
195
+
196
+ /** Rows with their standing; suppressed ones dropped. */
197
+ function standingRows(slug, rows) {
198
+ const fb = readFeedback(slug);
199
+ return rows
200
+ .map((r) => ({ r, s: standing(fb.get(rowId(r)) ?? []) }))
201
+ .filter((x) => !x.s.suppressed);
202
+ }
203
+
153
204
  function ensureDir(p) {
154
205
  mkdirSync(dirname(p), { recursive: true });
155
206
  }
@@ -171,9 +222,7 @@ function normStatement(s) {
171
222
  // Stable pointer for a ledger row, from the same fields the dedup key uses.
172
223
  // Not stored on the row: an existing ledger gets ids without a migration, and a
173
224
  // re-added entry keeps the id it already had.
174
- function rowId(row) {
175
- return shortId("L", row.kind, normStatement(row.statement));
176
- }
225
+ const rowId = ledgerRowId;
177
226
 
178
227
  function readLedger(slug) {
179
228
  const p = ledgerPath(slug);
@@ -197,8 +246,22 @@ function writeRow(slug, row) {
197
246
  appendFileSync(p, JSON.stringify(row) + "\n", "utf8");
198
247
  }
199
248
 
249
+ // One scanner per process; every write goes through it (_secret-scan.mjs).
250
+ let scanner = null;
251
+ const secretScan = { dropped: 0 };
252
+ function passesSecretScan(text) {
253
+ if (!scanner) {
254
+ scanner = openScanner();
255
+ process.on("exit", () => scanner.close());
256
+ }
257
+ if (scanner.isClean(text)) return true;
258
+ secretScan.dropped++;
259
+ return false;
260
+ }
261
+
200
262
  /** Append one entry unless an entry with the same kind + normalized statement
201
- * already exists (idempotent). Returns true when written. */
263
+ * already exists (idempotent), or the text fails the secret scan. Returns
264
+ * true when written. */
202
265
  function addEntry(slug, { kind, statement, scope, task, confidence, diagnosis, source }, existing) {
203
266
  // One line, enforced here rather than hoped for. The schema says "the
204
267
  // knowledge itself, in one line", and both blocks render one entry per line -
@@ -206,16 +269,23 @@ function addEntry(slug, { kind, statement, scope, task, confidence, diagnosis, s
206
269
  // `from-triage` builds statements out of model-written finding text, a
207
270
  // newline plus a plausible-looking `</repo-profile>` would break out of the
208
271
  // injected block itself. Collapsing whitespace closes both at the store.
209
- const stmt = oneLine(statement);
272
+ const stmt = oneLine(stripPrivate(statement));
210
273
  if (!stmt) return false;
211
274
  const seenKey = `${kind}::${normStatement(stmt)}`;
212
275
  if (existing.has(seenKey)) return false;
276
+ const sameKind = [...existing]
277
+ .filter((k) => k.startsWith(`${kind}::`))
278
+ .map((k) => k.slice(kind.length + 2));
279
+ if (nearDuplicate(stmt.toLowerCase(), sameKind)) return false;
280
+ const diag = oneLine(stripPrivate(diagnosis));
281
+ const scopeText = scope || "global";
282
+ if (!passesSecretScan(`${stmt}\n${diag}\n${scopeText}`)) return false;
213
283
  const row = {
214
284
  v: "1.0.0",
215
285
  ts: new Date().toISOString(),
216
286
  repo_slug: slug,
217
287
  kind,
218
- scope: scope || "global",
288
+ scope: scopeText,
219
289
  statement: stmt,
220
290
  source_task: task || null,
221
291
  confidence: CONFIDENCES.has(confidence) ? confidence : "medium",
@@ -223,7 +293,6 @@ function addEntry(slug, { kind, statement, scope, task, confidence, diagnosis, s
223
293
  // Optional causal diagnosis (Reflexion P5): the "why it failed" behind a
224
294
  // lesson. Verbal diagnosis is what turns a detected error into a prevented
225
295
  // one; a bare outcome does not. Stored only when supplied.
226
- const diag = oneLine(diagnosis);
227
296
  if (diag) row.diagnosis = diag;
228
297
  // Who found it. Optional so old rows stay valid, but written whenever the
229
298
  // caller knows: a machine-mined path correction and a model's architectural
@@ -235,11 +304,19 @@ function addEntry(slug, { kind, statement, scope, task, confidence, diagnosis, s
235
304
  return true;
236
305
  }
237
306
 
307
+ function refuseExcluded(slug) {
308
+ if (!memoryExcluded(slug)) return false;
309
+ process.stdout.write(JSON.stringify({ ok: true, slug, added: 0, excluded: true }) + "\n");
310
+ process.exitCode = 2;
311
+ return true;
312
+ }
313
+
238
314
  function cmdAdd() {
239
315
  const kind = flags.kind;
240
316
  if (!KINDS.has(kind)) die(`--kind must be one of: ${[...KINDS].join(", ")}`, 1);
241
317
  if (!flags.statement || flags.statement === true) die('add needs --statement "..."', 1);
242
318
  const slug = repoSlug();
319
+ if (refuseExcluded(slug)) return;
243
320
  const existing = new Set(readLedger(slug).map((r) => `${r.kind}::${normStatement(r.statement)}`));
244
321
  const wrote = addEntry(
245
322
  slug,
@@ -256,7 +333,13 @@ function cmdAdd() {
256
333
  existing,
257
334
  );
258
335
  process.stdout.write(
259
- JSON.stringify({ ok: true, slug, added: wrote ? 1 : 0, deduped: wrote ? 0 : 1 }) + "\n",
336
+ JSON.stringify({
337
+ ok: true,
338
+ slug,
339
+ added: wrote ? 1 : 0,
340
+ deduped: wrote || secretScan.dropped ? 0 : 1,
341
+ droppedBySecretScan: secretScan.dropped,
342
+ }) + "\n",
260
343
  );
261
344
  process.exit(wrote ? 0 : 2);
262
345
  }
@@ -267,6 +350,7 @@ function cmdFromTriage() {
267
350
  if (!existsSync(triagePath)) die(`triage file not found: ${triagePath}`, 1);
268
351
  const task = flags.task && flags.task !== true ? String(flags.task) : null;
269
352
  const slug = repoSlug();
353
+ if (refuseExcluded(slug)) return;
270
354
  let triage;
271
355
  try {
272
356
  triage = JSON.parse(readFileSync(triagePath, "utf8"));
@@ -311,7 +395,15 @@ function cmdFromTriage() {
311
395
  written++;
312
396
  }
313
397
  }
314
- process.stdout.write(JSON.stringify({ ok: true, slug, added: written, skippedBlocking }) + "\n");
398
+ process.stdout.write(
399
+ JSON.stringify({
400
+ ok: true,
401
+ slug,
402
+ added: written,
403
+ skippedBlocking,
404
+ droppedBySecretScan: secretScan.dropped,
405
+ }) + "\n",
406
+ );
315
407
  process.exitCode = written ? 0 : 2;
316
408
  return;
317
409
  }
@@ -430,12 +522,14 @@ function compareCodepoints(a, b) {
430
522
  }
431
523
 
432
524
  function cmdBrief() {
433
- const slug = flags["repo-slug"] ? String(flags["repo-slug"]) : repoSlug();
525
+ const slug = repoSlug();
434
526
  const cfg = recallConfig();
435
527
  const max = flags.max ? Math.max(1, Number(flags.max)) : 20;
436
528
  const task = flags.task && flags.task !== true ? String(flags.task) : null;
437
529
  const scope = flags.scope && flags.scope !== true ? String(flags.scope) : null;
438
- const rows = readLedger(slug);
530
+ const scored = standingRows(slug, readLedger(slug));
531
+ const rows = scored.map((x) => x.r);
532
+ const strengthOf = new Map(scored.map((x) => [x.r, x.s.corroborated ? x.s.strength : 0]));
439
533
  if (rows.length === 0) {
440
534
  process.exit(2); // nothing to inject; caller skips silently
441
535
  }
@@ -471,8 +565,13 @@ function cmdBrief() {
471
565
  );
472
566
  const fresh = recencyRank(pool, { halfLifeDays: cfg.halfLifeDays });
473
567
  const matched = new Set(relevance.filter((x) => x.score > 0).map((x) => x.index));
474
- const fused = rrf([relevance, fresh], { k: [10, 60], weights: [3, 1] }).filter((x) =>
475
- matched.has(x.index),
568
+ // Entries recorded useful at least twice rank by their strength as well;
569
+ // with no feedback this list is empty and the fusion is unchanged.
570
+ const earned = pool
571
+ .map((r, index) => ({ index, score: strengthOf.get(r) || 0 }))
572
+ .sort((a, b) => b.score - a.score);
573
+ const fused = rrf([relevance, fresh, earned], { k: [10, 60, 60], weights: [3, 1, 1] }).filter(
574
+ (x) => matched.has(x.index),
476
575
  );
477
576
  ordered = fused.slice(0, max).map((x) => pool[x.index]);
478
577
  // A task whose words match nothing in the ledger must not silently inject an
@@ -527,10 +626,10 @@ function scopeMatches(entryScope, target) {
527
626
  // everything after it. Detail is deliberately absent; each line carries the
528
627
  // pointer that buys it back.
529
628
  function cmdProfile() {
530
- const slug = flags["repo-slug"] ? String(flags["repo-slug"]) : repoSlug();
629
+ const slug = repoSlug();
531
630
  const cfg = recallConfig();
532
631
  const max = flags.max ? Math.max(1, Number(flags.max)) : 60;
533
- const rows = readLedger(slug);
632
+ const rows = standingRows(slug, readLedger(slug)).map((x) => x.r);
534
633
  if (rows.length === 0) process.exit(2);
535
634
 
536
635
  const ordered = rows
@@ -565,7 +664,7 @@ function cmdProfile() {
565
664
  function cmdShow() {
566
665
  const id = flags.id && flags.id !== true ? String(flags.id) : null;
567
666
  if (!id) die("show needs --id <id>", 1);
568
- const slug = flags["repo-slug"] ? String(flags["repo-slug"]) : repoSlug();
667
+ const slug = repoSlug();
569
668
  const want = id.startsWith("L:") ? id : `L:${id}`;
570
669
  const row = readLedger(slug).find((r) => rowId(r) === want);
571
670
  if (!row) {
@@ -592,7 +691,7 @@ function cmdShow() {
592
691
  function cmdTimeline() {
593
692
  const id = flags.anchor && flags.anchor !== true ? String(flags.anchor) : null;
594
693
  if (!id) die("timeline needs --anchor <id>", 1);
595
- const slug = flags["repo-slug"] ? String(flags["repo-slug"]) : repoSlug();
694
+ const slug = repoSlug();
596
695
  const before = flags.before && flags.before !== true ? Math.max(0, Number(flags.before)) : 3;
597
696
  const after = flags.after && flags.after !== true ? Math.max(0, Number(flags.after)) : 3;
598
697
  const want = id.startsWith("L:") ? id : `L:${id}`;
@@ -622,15 +721,32 @@ function cmdTimeline() {
622
721
  }
623
722
 
624
723
  function cmdPath() {
625
- process.stdout.write(
626
- ledgerPath(flags["repo-slug"] ? String(flags["repo-slug"]) : repoSlug()) + "\n",
627
- );
724
+ process.stdout.write(ledgerPath(repoSlug()) + "\n");
628
725
  process.exitCode = 0;
629
726
  return;
630
727
  }
631
728
 
729
+ const OUTCOMES = new Set(["useful", "dead_end", "corrected"]);
730
+
731
+ function cmdFeedback() {
732
+ const slug = repoSlug();
733
+ const raw = flags.id && flags.id !== true ? String(flags.id).replace(/^\[|\]$/g, "") : "";
734
+ const id = raw && !raw.startsWith("L:") ? `L:${raw}` : raw;
735
+ const outcome = String(flags.outcome || "");
736
+ if (!id || !OUTCOMES.has(outcome))
737
+ die("feedback needs --id <id> --outcome <useful|dead_end|corrected>", 1);
738
+ if (!readLedger(slug).some((r) => rowId(r) === id)) die(`no ledger entry ${id}`, 2);
739
+ if (refuseExcluded(slug)) return;
740
+ const row = { ts: new Date().toISOString(), id, outcome };
741
+ if (flags.task && flags.task !== true) row.task = String(flags.task);
742
+ ensureDir(feedbackPath(slug));
743
+ appendFileSync(feedbackPath(slug), JSON.stringify(row) + "\n", "utf8");
744
+ const s = standing(readFeedback(slug).get(id) ?? []);
745
+ process.stdout.write(JSON.stringify({ ok: true, slug, id, ...s }) + "\n");
746
+ }
747
+
632
748
  function cmdStats() {
633
- const slug = flags["repo-slug"] ? String(flags["repo-slug"]) : repoSlug();
749
+ const slug = repoSlug();
634
750
  const rows = readLedger(slug);
635
751
  const counts = { fact: 0, convention: 0, "rejected-preference": 0 };
636
752
  for (const r of rows) if (counts[r.kind] !== undefined) counts[r.kind]++;
@@ -651,6 +767,9 @@ switch (SUB) {
651
767
  case "forget":
652
768
  cmdForget();
653
769
  break;
770
+ case "feedback":
771
+ cmdFeedback();
772
+ break;
654
773
  case "brief":
655
774
  cmdBrief();
656
775
  break;
@@ -671,7 +790,7 @@ switch (SUB) {
671
790
  break;
672
791
  default:
673
792
  process.stderr.write(
674
- "usage: learnings-ledger.mjs <add|from-triage|forget|brief|profile|show|timeline|path|stats> [flags]\n",
793
+ "usage: learnings-ledger.mjs <add|from-triage|feedback|forget|brief|profile|show|timeline|path|stats> [flags]\n",
675
794
  );
676
795
  process.exit(64);
677
796
  }
@@ -4,7 +4,9 @@
4
4
  * answer per objection, and a verdict decided by constitution rule id.
5
5
  *
6
6
  * At the end of Phase 1 the plan-critic persona writes objections to the plan
7
- * under four lenses (scope, feasibility, security, a cheaper alternative), and
7
+ * under six lenses (scope, feasibility, security, a cheaper alternative, an
8
+ * input the spec implies but no test exercises, a decision left to the
9
+ * implementer), and
8
10
  * the planner answers each one exactly once: accept and revise the plan, or
9
11
  * rebut with evidence. Both halves live in one file,
10
12
  * `<worktree>/.pipeline/plan-critique.json` (schemas/plan-critique.schema.json).
@@ -59,7 +61,14 @@ import { requirementIds } from "./spec-consistency-gate.mjs";
59
61
  import { resolveOne } from "./verify-citations.mjs";
60
62
 
61
63
  export const GATE = "plan-critique";
62
- export const LENSES = ["scope", "feasibility", "security", "alternative"];
64
+ export const LENSES = [
65
+ "scope",
66
+ "feasibility",
67
+ "security",
68
+ "alternative",
69
+ "unstated-input",
70
+ "underspecified",
71
+ ];
63
72
  export const ANCHOR_KINDS = ["file", "plan", "analysis", "inference"];
64
73
  export const ACTIONS = ["accept", "rebut"];
65
74
  const OBJ_ID = /^OBJ-\d{2,3}$/;
@@ -0,0 +1,106 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * rework-rulings.mjs - what happens to each open finding when the rework cap
4
+ * is reached.
5
+ *
6
+ * Phase 2 allows a fixed number of rework rounds. At the cap every accepted
7
+ * finding still open gets one ruling, recorded, never dropped:
8
+ *
9
+ * escalate load-bearing: blocking severity, or a security finding
10
+ * (ruleId SEC-* / CON-SEC-*, a security criteriaSource, or the
11
+ * security-auditor's `security` object). The run stops and the
12
+ * user decides.
13
+ * parked anything else. It leaves the loop as a follow-up with its
14
+ * reason, shown before commit (Phase 4 Step 0b), carried in the
15
+ * PR follow-ups and recorded in the debt list, so the run can finish instead of stopping on a
16
+ * finding that is safe to defer.
17
+ *
18
+ * The run escalates when any ruling is `escalate`; otherwise it continues.
19
+ *
20
+ * Usage:
21
+ * rework-rulings.mjs --triage <triage-output.json> [--state <agent-state.json>]
22
+ *
23
+ * --state records the result as reviewIterations[-1].rulings.
24
+ *
25
+ * Output: {escalate, parked, rulings[]} as JSON on stdout.
26
+ * Exit: 0 decided (either way) · 2 usage or unreadable triage · 3 state write failed
27
+ *
28
+ * @module pipeline/scripts/rework-rulings
29
+ */
30
+
31
+ import { invokedDirectly } from "../lib/invoked-directly.mjs";
32
+ import { readRegularJson } from "../lib/regular-file.mjs";
33
+ import { updateState } from "./gate-ledger.mjs";
34
+
35
+ const SECURITY_RULE = /^(CON-)?SEC-/i;
36
+
37
+ /** True when a finding may not leave the loop without the user. */
38
+ export function loadBearing(f) {
39
+ if (f?.severity === "blocking") return true;
40
+ if (typeof f?.ruleId === "string" && SECURITY_RULE.test(f.ruleId)) return true;
41
+ if (f?.security && typeof f.security === "object") return true;
42
+ return typeof f?.criteriaSource === "string" && /security/i.test(f.criteriaSource);
43
+ }
44
+
45
+ /** One ruling per accepted finding of a triage output. */
46
+ export function rule(triage) {
47
+ const rulings = (triage?.accepted ?? []).map((f) => {
48
+ const escalate = loadBearing(f);
49
+ return {
50
+ fingerprint: f.fingerprint ?? null,
51
+ file: f.file ?? null,
52
+ line: Number.isInteger(f.line) ? f.line : null,
53
+ severity: f.severity ?? null,
54
+ ruling: escalate ? "escalate" : "parked",
55
+ reason: escalate
56
+ ? f.severity === "blocking"
57
+ ? "blocking finding open at the rework cap"
58
+ : "security finding open at the rework cap"
59
+ : `rework cap reached; ${f.severity ?? "unrated"} finding parked as a follow-up`,
60
+ issue: f.issue ?? "",
61
+ };
62
+ });
63
+ return {
64
+ escalate: rulings.some((r) => r.ruling === "escalate"),
65
+ parked: rulings.filter((r) => r.ruling === "parked").length,
66
+ rulings,
67
+ };
68
+ }
69
+
70
+ function flag(argv, name) {
71
+ const i = argv.indexOf(`--${name}`);
72
+ return i >= 0 ? argv[i + 1] : undefined;
73
+ }
74
+
75
+ if (invokedDirectly(import.meta.url)) {
76
+ const argv = process.argv.slice(2);
77
+ const triagePath = flag(argv, "triage");
78
+ if (!triagePath) {
79
+ console.error("usage: rework-rulings.mjs --triage <path> [--state <path>]");
80
+ process.exit(2);
81
+ }
82
+ const triage = readRegularJson(triagePath, null);
83
+ if (!triage || !Array.isArray(triage.accepted)) {
84
+ console.error(`rework-rulings: no accepted[] in ${triagePath}`);
85
+ process.exit(2);
86
+ }
87
+ const result = rule(triage);
88
+ const statePath = flag(argv, "state");
89
+ if (statePath) {
90
+ try {
91
+ await updateState(statePath, (s) => {
92
+ const iterations = Array.isArray(s.reviewIterations) ? [...s.reviewIterations] : [];
93
+ if (!iterations.length) iterations.push({ iteration: 1 });
94
+ iterations[iterations.length - 1] = {
95
+ ...iterations[iterations.length - 1],
96
+ rulings: result.rulings,
97
+ };
98
+ return { ...s, reviewIterations: iterations };
99
+ });
100
+ } catch (e) {
101
+ console.error(`rework-rulings: state not written: ${e.message}`);
102
+ process.exitCode = 3;
103
+ }
104
+ }
105
+ process.stdout.write(JSON.stringify(result) + "\n");
106
+ }
@@ -10,21 +10,29 @@
10
10
  * Usage:
11
11
  * git diff --name-only <base>...HEAD | node scope-check-gate.mjs --check .pipeline/scope-check.json --diff-files -
12
12
  * node scope-check-gate.mjs --check scope-check.json --diff-files files.txt [--advisory]
13
+ * [--patch <unified diff> | --repo <dir> --patch-range <a>...<b>]
13
14
  *
14
- * Output (stdout, JSON): {ok, justified, unjustified[], unlisted[], notDone}
15
+ * Output (stdout, JSON): {ok, justified, unjustified[], unlisted[], notDone, formatOnly[]}
15
16
  * unjustified = in the diff, missing from files[] (or with an empty reason)
16
17
  * unlisted = in files[], not in the diff (stale entry; reported, never fatal)
18
+ * formatOnly = with --patch, hunks whose removed and added lines differ only
19
+ * in whitespace: a reformat of lines the task did not need to
20
+ * touch. Reported, never fatal. --patch-range reads that diff
21
+ * with git itself, its output format pinned, so a caller needs
22
+ * no file or process substitution for it.
17
23
  *
18
24
  * Exit codes: 0 every diff file justified (or --advisory), 1 otherwise or on
19
25
  * unreadable input, 64 usage.
20
26
  */
21
27
 
28
+ import { execFileSync } from "node:child_process";
22
29
  import { readFileSync } from "node:fs";
23
30
 
24
31
  import { normalizeFile } from "./_fingerprint.mjs";
32
+ import { parseUnifiedDiff } from "./_unified-diff.mjs";
25
33
 
26
34
  const USAGE =
27
- "usage: scope-check-gate.mjs --check <scope-check.json> --diff-files <path|-> [--advisory]";
35
+ "usage: scope-check-gate.mjs --check <scope-check.json> --diff-files <path|-> [--advisory] [--patch <diff> | --repo <dir> --patch-range <a>...<b>]";
28
36
 
29
37
  function parseArgs(argv) {
30
38
  const opts = {};
@@ -33,15 +41,145 @@ function parseArgs(argv) {
33
41
  if (a === "--check") opts.check = argv[++i];
34
42
  else if (a === "--diff-files") opts.diffFiles = argv[++i];
35
43
  else if (a === "--advisory") opts.advisory = true;
44
+ else if (a === "--patch") opts.patch = argv[++i];
45
+ else if (a === "--patch-range") opts.patchRange = argv[++i];
46
+ else if (a === "--repo") opts.repo = argv[++i];
36
47
  else return null;
37
48
  }
49
+ if (opts.patchRange !== undefined && !/^[A-Za-z0-9_./~^@{}-]+$/.test(opts.patchRange))
50
+ return null;
51
+ if (opts.patchRange?.startsWith("-")) return null;
38
52
  return opts.check && opts.diffFiles ? opts : null;
39
53
  }
40
54
 
55
+ /** The diff of a revision range, in the one shape formatOnlyHunks reads. */
56
+ function rangeDiff(repo, range) {
57
+ return execFileSync(
58
+ "git",
59
+ [
60
+ "-C",
61
+ repo || ".",
62
+ "-c",
63
+ "core.quotePath=false",
64
+ "diff",
65
+ "--no-ext-diff",
66
+ "--no-color",
67
+ "--no-textconv",
68
+ "--src-prefix=a/",
69
+ "--dst-prefix=b/",
70
+ range,
71
+ "--",
72
+ ],
73
+ { encoding: "utf8", maxBuffer: 64 * 1024 * 1024, timeout: 30_000 },
74
+ );
75
+ }
76
+
41
77
  function readText(path) {
42
78
  return path === "-" ? readFileSync(0, "utf-8") : readFileSync(path, "utf-8");
43
79
  }
44
80
 
81
+ const INDENT_SENSITIVE = /(\.(py|pyi|ya?ml|coffee|haml|slim|pug)|(^|\/)(GNU)?[Mm]akefile)$/;
82
+
83
+ const LINE_COMMENT = /(\.(py|pyi|ya?ml|sh|bash|zsh|rb|toml|coffee)|(^|\/)(GNU)?[Mm]akefile)$/;
84
+ // Languages where a space next to a bracket, comma, semicolon or colon never
85
+ // carries meaning. YAML, shell, Makefile and CSS are left out on purpose.
86
+ const TIGHT_LANGS =
87
+ /\.(js|jsx|mjs|cjs|ts|tsx|java|go|cs|swift|kt|kts|json|rs|dart|scala|py|pyi|c|h|cc|cpp|hpp|m|mm)$/;
88
+ const LEADING_OPERATOR_LANGS = /\.(kt|kts|swift|go|scala)$/;
89
+ const BREAK_KEEPERS = /\b(return|throw|yield|break|continue)$/;
90
+ const LEADING_OPERATOR = /^[+\-*/%&|^.?]/;
91
+ const TIGHT = /\s*([(){}[\],;:])\s*/g;
92
+
93
+ /** One line with string literals and comments verbatim and every other whitespace run as one space. */
94
+ function foldLine(line, hashComments) {
95
+ const out = [];
96
+ let quote = null;
97
+ let gap = false;
98
+ for (let i = 0; i < line.length; i++) {
99
+ const c = line[i];
100
+ if (quote) {
101
+ out.push(c);
102
+ if (c === "\\") out.push(line[++i] ?? "");
103
+ else if (c === quote) quote = null;
104
+ continue;
105
+ }
106
+ if (c === " " || c === "\t" || c === "\r") {
107
+ gap = true;
108
+ continue;
109
+ }
110
+ if (gap && out.length) out.push(" ");
111
+ gap = false;
112
+ if ((c === "/" && line[i + 1] === "/") || (hashComments && c === "#")) {
113
+ out.push(line.slice(i));
114
+ break;
115
+ }
116
+ if (c === '"' || c === "'" || c === "`") quote = c;
117
+ out.push(c);
118
+ }
119
+ return out.join("");
120
+ }
121
+
122
+ /**
123
+ * The layout-free form of some lines. Each line is folded on its own (linear
124
+ * in the hunk), then the lines are joined by a space, except that a break
125
+ * after return, throw, yield, break or continue, and in Kotlin, Swift, Go or
126
+ * Scala a break before a leading operator, stays a break. Space next to
127
+ * brackets, commas, semicolons and colons is dropped only in TIGHT_LANGS, and
128
+ * never in a hunk that holds a C preprocessor line.
129
+ */
130
+ function outsideStrings(text, file = "") {
131
+ const hashComments = LINE_COMMENT.test(file);
132
+ const lines = text.split("\n").map((l) => foldLine(l, hashComments));
133
+ let joined = "";
134
+ lines.forEach((line, i) => {
135
+ if (i === 0) {
136
+ joined = line;
137
+ return;
138
+ }
139
+ const keepBreak =
140
+ BREAK_KEEPERS.test(lines[i - 1]) ||
141
+ (LEADING_OPERATOR_LANGS.test(file) && LEADING_OPERATOR.test(line));
142
+ joined += (keepBreak ? "\u23ce" : " ") + line;
143
+ });
144
+ const preprocessor = lines.some((l) => l.startsWith("#")) && !hashComments;
145
+ const tight = TIGHT_LANGS.test(file) && !preprocessor;
146
+ return (tight ? joined.replace(TIGHT, "$1") : joined).trim();
147
+ }
148
+
149
+ const indentOf = (line) => /^[ \t]*/.exec(line)[0];
150
+
151
+ /** True when the removed and added lines differ only in layout. */
152
+ function layoutOnly(file, removed, added) {
153
+ if (!removed.length || !added.length) return false;
154
+ if (INDENT_SENSITIVE.test(file)) {
155
+ if (removed.length !== added.length) return false;
156
+ return removed.every(
157
+ (r, i) =>
158
+ indentOf(r.text) === indentOf(added[i].text) &&
159
+ outsideStrings(r.text, file) === outsideStrings(added[i].text, file),
160
+ );
161
+ }
162
+ const joined = (lines) => outsideStrings(lines.map((d) => d.text).join("\n"), file);
163
+ return joined(removed) === joined(added);
164
+ }
165
+
166
+ /**
167
+ * Hunks of a unified diff whose change is layout only: whitespace outside
168
+ * string literals, and in indentation-sensitive files never a leading indent.
169
+ */
170
+ export function formatOnlyHunks(patch) {
171
+ const found = [];
172
+ for (const [file, diff] of parseUnifiedDiff(patch)) {
173
+ for (const h of diff.hunks) {
174
+ if (!h.removed.length && !h.added.length) continue;
175
+ if (layoutOnly(file, h.removed, h.added)) {
176
+ found.push({ file: normalizeFile(file), hunk: h.header });
177
+ }
178
+ }
179
+ }
180
+ return found;
181
+ }
182
+
45
183
  export function evaluate(check, diffFiles) {
46
184
  const files = Array.isArray(check?.files) ? check.files : [];
47
185
  const reasons = new Map();
@@ -81,6 +219,15 @@ function main() {
81
219
  return fail;
82
220
  }
83
221
  const result = evaluate(check, diffFiles);
222
+ if (opts.patch || opts.patchRange) {
223
+ try {
224
+ const patch = opts.patch ? readText(opts.patch) : rangeDiff(opts.repo, opts.patchRange);
225
+ result.formatOnly = formatOnlyHunks(patch);
226
+ } catch (err) {
227
+ result.formatOnly = [];
228
+ result.patchError = err.message;
229
+ }
230
+ }
84
231
  process.stdout.write(JSON.stringify(result) + "\n");
85
232
  return result.ok ? 0 : fail;
86
233
  }