moflo 4.9.13 → 4.9.14

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.
@@ -88,7 +88,11 @@ var TASK_RE = /\b(fix|bug|error|implement|add|create|build|write|refactor|debug|
88
88
  var TEST_RUNNER_RE = /(?:^|[^a-z])(?:npm|yarn|pnpm|bun)\s+(?:run\s+)?(?:test|t)(?:[:\s]|$)|\b(?:npx|pnpx)\s+(?:vitest|jest|mocha|ava|tap|jasmine|pytest)\b|(?:^|;|&&|\|\|)\s*(?:vitest|jest|pytest|mocha|jasmine|tap|ava)\s|\b(?:cargo|go|deno|dotnet|mvn)\s+test\b|\bgradle\w*\s+test\b/i;
89
89
  // Edits to these don't change runtime behaviour, so they don't invalidate prior test/simplify runs.
90
90
  // Lock files and .gitignore are tracked but inert; package.json/*.yaml ARE source — they reset.
91
- var EDIT_RESET_SKIP_RE = /\.(md|markdown|txt|rst|adoc|lock|gitignore)$|(?:^|[\\\/])(CHANGELOG(?:\.md)?|\.env\.example|package-lock\.json|pnpm-lock\.yaml|yarn\.lock|bun\.lockb)$/i;
91
+ var EDIT_RESET_SKIP_BOTH_RE = /\.(md|markdown|txt|rst|adoc|lock|gitignore)$|(?:^|[\\\/])(CHANGELOG(?:\.md)?|\.env\.example|package-lock\.json|pnpm-lock\.yaml|yarn\.lock|bun\.lockb)$/i;
92
+ // Test files: invalidate the testing gate (tests are stale once test code changes)
93
+ // but NOT the simplify gate — /simplify already reviewed the production code; touching
94
+ // a test file or fixture doesn't expose new untested surface for code review (#908).
95
+ var EDIT_RESET_SKIP_SIMPLIFY_ONLY_RE = /(?:^|[\\\/])(__tests__|__mocks__|tests?|spec|specs|cypress|e2e|fixtures?)[\\\/]|\.(test|spec)\.[mc]?[jt]sx?$|\.fixture\.[mc]?[jt]sx?$/i;
92
96
 
93
97
  switch (command) {
94
98
  case 'check-before-agent': {
@@ -180,11 +184,20 @@ switch (command) {
180
184
  }
181
185
  case 'reset-edit-gates': {
182
186
  var fp = process.env.TOOL_INPUT_file_path || '';
183
- if (fp && EDIT_RESET_SKIP_RE.test(fp)) break;
187
+ // Inert files (markdown, lockfiles, CHANGELOG, .env.example): no gate reset.
188
+ if (fp && EDIT_RESET_SKIP_BOTH_RE.test(fp)) break;
184
189
  var s = readState();
185
- if (!s.testsRun && !s.simplifyRun) break;
186
- s.testsRun = false;
187
- s.simplifyRun = false;
190
+ // Test-only edits invalidate testsRun but preserve simplifyRun (#908).
191
+ var isTestOnly = fp && EDIT_RESET_SKIP_SIMPLIFY_ONLY_RE.test(fp);
192
+ var resetTests = s.testsRun;
193
+ var resetSimplify = s.simplifyRun && !isTestOnly;
194
+ if (!resetTests && !resetSimplify) break;
195
+ var gates = [];
196
+ if (resetTests) { s.testsRun = false; gates.push('tests'); }
197
+ if (resetSimplify) { s.simplifyRun = false; gates.push('simplify'); }
198
+ if (fp) {
199
+ s.lastResetBy = { file: fp, at: new Date().toISOString(), gates: gates };
200
+ }
188
201
  writeState(s);
189
202
  break;
190
203
  }
@@ -205,6 +218,9 @@ switch (command) {
205
218
  for (var i = 0; i < missing.length; i++) {
206
219
  process.stderr.write(' - ' + missing[i] + '\n');
207
220
  }
221
+ if (s.lastResetBy && s.lastResetBy.file) {
222
+ process.stderr.write('Last gate reset: ' + s.lastResetBy.file + ' (' + (s.lastResetBy.gates || []).join(', ') + ')\n');
223
+ }
208
224
  process.stderr.write('Disable per-gate via moflo.yaml:\n');
209
225
  process.stderr.write(' gates:\n testing_gate: false\n simplify_gate: false\n learnings_gate: false\n');
210
226
  process.exit(2);
@@ -4,11 +4,27 @@ Phase-by-phase notes for the full `/flo <issue>` run. Phase 2 (Ticket) lives in
4
4
 
5
5
  ## Phase 1: Research (also `-r`)
6
6
 
7
- ### 1.1 Fetch the issue
7
+ ### 1.1 Fetch the issue + history (cheap, before any file exploration)
8
+
9
+ Run these BEFORE any `Glob` / `Grep` / `Read` of source files. The goal is to catch "this is already (partially) fixed" in two commands rather than 10K tokens of file scanning.
10
+
8
11
  ```bash
9
- gh issue view <issue-number> --json number,title,body,labels,state,assignees,comments,milestone
12
+ # Issue + closing PRs (one call, one new field vs. before).
13
+ gh issue view <issue-number> --json number,title,body,labels,state,assignees,comments,milestone,closedByPullRequestsReferences
14
+
15
+ # Commits that reference the issue. Silently no-ops outside a git work tree —
16
+ # consumers without git, fresh `npx moflo` shells, non-git VCS all skip cleanly.
17
+ if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
18
+ git log --all --grep="\b<issue-number>\b\|#<issue-number>" --oneline -30 || true
19
+ fi
10
20
  ```
11
21
 
22
+ **Surface what you find and proceed — never pause to ask.** `/flo` is fire-and-forget; a prompt that blocks for 30 minutes waiting on a yes/no is a worse failure than re-doing already-shipped work. Specifically:
23
+
24
+ - **Issue is CLOSED with non-empty `closedByPullRequestsReferences`** → read the closing PR body and merge commit as primary context. Treat the run as "look for any remaining work or follow-up" and continue. Do not stop.
25
+ - **Commits reference the issue but it's still open** → those are partial fixes. Summarise them in one line (`partial fix already shipped: <sha> <subject>`), then `git show <sha>` if you need the diff, scope the implementation around what's still missing, and continue. Do not stop.
26
+ - **No history found / scan skipped** → proceed silently to memory + code exploration as before.
27
+
12
28
  ### 1.2 Check ticket status
13
29
  Look for the `## Acceptance Criteria` heading in the body.
14
30
  - Present → ticket already enhanced; skip ahead to execute.
@@ -15,7 +15,30 @@ Treat the union of staged + unstaged + committed-since-base as the diff to revie
15
15
 
16
16
  Also note: was `/simplify` already run on this branch in this session? If yes, you're in a **validation pass** (Phase 2.5 below) — most of the heavy lifting is done.
17
17
 
18
- ## Phase 2: Classify the diff
18
+ ## Phase 2: Classify the diff (deterministic — call the classifier)
19
+
20
+ **Call the classifier first, follow its decision.** Do not eyeball the diff and pick a tier in prose — that's the failure mode that costs ~230K tokens per run on mechanical decompositions (issue #908). The classifier reads the same diff Claude would, applies the rules below, and returns a JSON dispatch decision:
21
+
22
+ ```bash
23
+ node .claude/helpers/simplify-classify.cjs --base main
24
+ ```
25
+
26
+ (In the moflo source repo, equivalent is `node bin/simplify-classify.cjs --base main`. The launcher syncs `bin/simplify-classify.cjs` → `.claude/helpers/simplify-classify.cjs` in consumer projects.)
27
+
28
+ Output:
29
+ ```json
30
+ {
31
+ "tier": "TRIVIAL" | "SMALL" | "NORMAL",
32
+ "model": "sonnet",
33
+ "agentCount": 0 | 1 | 3,
34
+ "reasoning": ["..."],
35
+ "stats": { "added": ..., "deleted": ..., "declAdded": ..., "declRemoved": ..., "netDecls": ..., "fileCount": ..., "securityHit": ... }
36
+ }
37
+ ```
38
+
39
+ If `bin/simplify-classify.cjs` is missing (older moflo install), fall back to the prose rules below — but on a current install the classifier IS the source of truth. Default behavior: **single Sonnet agent** unless the diff signals genuinely warrant escalation.
40
+
41
+ Tier definitions the classifier encodes (for reference, not for re-derivation):
19
42
 
20
43
  Pick the **smallest tier** the diff genuinely fits. When in doubt, escalate one step (not two).
21
44
 
@@ -40,13 +63,14 @@ This is the default tier for **most real diffs**, including changes to critical
40
63
 
41
64
  Examples that qualify: extracting a constant, inlining a one-liner, swapping a `for` for a `forEach`, adding one early-return, refactoring a single function within a file, adding a cache fast-path inside an existing block.
42
65
 
43
- ### NORMAL — three parallel agents
44
- Reserved for **genuinely cross-cutting** changes. ANY of these triggers NORMAL:
45
- - 3+ files changed
46
- - >200 net LOC changed
47
- - Adds/removes/renames a public API
48
- - Introduces or removes a dependency
49
- - Cross-cutting refactor (touches the same pattern in multiple modules)
66
+ ### NORMAL — three parallel agents (high bar)
67
+ Reserved for **genuinely cross-cutting** changes that single-agent review can't cover. The classifier escalates to NORMAL only when ANY of:
68
+ - `>500 LOC changed` (real volume, not just "more than 200")
69
+ - `5+ files AND ≥3 net new declarations` (broad new surface, not relocation)
70
+ - `security-sensitive path AND netDecls > 0` (aidefence/, swarm/consensus/, hooks gate, daemon-lock, launcher — only when adding logic, not on a 1-line touch)
71
+ - `3+ new files AND ≥5 new declarations` (genuinely new subsystem)
72
+
73
+ **Mechanical relocation is NOT NORMAL** even with many files / many lines. If `declAdded` and `declRemoved` are both ≥2 and `netDecls` is small (within 30% of total declarations touched), it's a structural move — SMALL, single agent. This is the #906/#908 case: ~330 LOC across 6 files of pure decomposition was costing 230K tokens via three-agent fan-out when it needed one Sonnet agent.
50
74
 
51
75
  Three agents exist to cover orthogonal axes (Reuse / Quality / Efficiency) when the change is broad enough that one agent's tool-call budget can't survey it all. For single-file edits, one focused agent always covers all three axes — three is duplication, not coverage.
52
76
 
@@ -62,48 +86,11 @@ Escalate one tier (self-review → SMALL agent) only if the fix introduced any o
62
86
 
63
87
  Do **not** escalate to NORMAL on a validation pass. If the fix is so structural that NORMAL is warranted, treat it as a fresh diff and start over from Phase 1.
64
88
 
65
- ## Phase 2.7: Route the model (before any Agent spawn)
66
-
67
- For every tier that spawns an Agent (SMALL / NORMAL — TRIVIAL self-review skips this), call the moflo router to pick the cheapest model that fits the task **before** invoking Agent:
68
-
69
- ```
70
- mcp__moflo__hooks_model-route — {
71
- task: "<diff summary — see wording rules below>",
72
- preferCost: true
73
- }
74
- ```
89
+ ## Phase 2.7: Model selection
75
90
 
76
- ### Wording the task description
77
-
78
- The router's complexity score is keyword-sensitive. Words like `refactor`, `architect`, `audit`, `system`, `redesign`, `migrate` flip a high-complexity flag and force opus *even when scoring suggests sonnet*. For `/simplify` you are **always doing code review**, never genuine architecture, so frame the task accordingly:
79
-
80
- - ✅ Good: `"Review 110-line single-file change in bin/session-start-launcher.mjs for reuse, quality, efficiency."`
81
- - ❌ Bad: `"Review refactor that adds mtime-cache fast-path and architects new caching layer."`
82
-
83
- Drop the trigger words. State LOC count, file count, and "review for reuse, quality, efficiency". That's enough signal.
84
-
85
- ### Applying the result
86
-
87
- The router returns `{ model: 'haiku' | 'sonnet' | 'opus', complexity, reasoning, alternatives, ... }`.
88
-
89
- **Hard rule for `/simplify`: opus is never correct.** Code review does not require Opus-tier reasoning even on critical surface. If the router returns `opus`:
90
-
91
- 1. Look at `alternatives` — if `sonnet` scores higher than the selected model's confidence, downgrade to sonnet.
92
- 2. Otherwise, downgrade to sonnet anyway (treat opus as "router was uncertain — pick the safer middle").
93
-
94
- Pass the final model verbatim to the Agent's `model` parameter (Agent accepts `'haiku' | 'sonnet' | 'opus'`). On router failure (MCP call errors), default to `'sonnet'`.
95
-
96
- In practice: comment trims and pure formatting → haiku; everything else for `/simplify` → sonnet.
97
-
98
- ### Feed back the outcome
99
-
100
- After the agent completes, record the outcome so the router learns:
101
-
102
- ```
103
- mcp__moflo__hooks_model-outcome — { task: "<same wording as route call>", model: "<chosen>", outcome: "success" | "failure" | "escalated" }
104
- ```
91
+ **Use the model the classifier returned** — always `sonnet` for `/simplify`. Opus is never correct here; the classifier enforces this. No router call needed; the classifier IS the router for this skill.
105
92
 
106
- `escalated` = the agent missed something a higher-tier pass would have caught. That signal teaches the router to bias similar tasks upward next time. Don't fake `escalated` to retroactively justify opus — only record it when a *real* miss happened.
93
+ If you fell back to prose rules in Phase 2 (no classifier available), use `sonnet` unconditionally. Pass the model verbatim to Agent's `model` parameter.
107
94
 
108
95
  ## Phase 3: Run the appropriate review
109
96
 
package/bin/gate.cjs CHANGED
@@ -88,7 +88,11 @@ var TASK_RE = /\b(fix|bug|error|implement|add|create|build|write|refactor|debug|
88
88
  var TEST_RUNNER_RE = /(?:^|[^a-z])(?:npm|yarn|pnpm|bun)\s+(?:run\s+)?(?:test|t)(?:[:\s]|$)|\b(?:npx|pnpx)\s+(?:vitest|jest|mocha|ava|tap|jasmine|pytest)\b|(?:^|;|&&|\|\|)\s*(?:vitest|jest|pytest|mocha|jasmine|tap|ava)\s|\b(?:cargo|go|deno|dotnet|mvn)\s+test\b|\bgradle\w*\s+test\b/i;
89
89
  // Edits to these don't change runtime behaviour, so they don't invalidate prior test/simplify runs.
90
90
  // Lock files and .gitignore are tracked but inert; package.json/*.yaml ARE source — they reset.
91
- var EDIT_RESET_SKIP_RE = /\.(md|markdown|txt|rst|adoc|lock|gitignore)$|(?:^|[\\\/])(CHANGELOG(?:\.md)?|\.env\.example|package-lock\.json|pnpm-lock\.yaml|yarn\.lock|bun\.lockb)$/i;
91
+ var EDIT_RESET_SKIP_BOTH_RE = /\.(md|markdown|txt|rst|adoc|lock|gitignore)$|(?:^|[\\\/])(CHANGELOG(?:\.md)?|\.env\.example|package-lock\.json|pnpm-lock\.yaml|yarn\.lock|bun\.lockb)$/i;
92
+ // Test files: invalidate the testing gate (tests are stale once test code changes)
93
+ // but NOT the simplify gate — /simplify already reviewed the production code; touching
94
+ // a test file or fixture doesn't expose new untested surface for code review (#908).
95
+ var EDIT_RESET_SKIP_SIMPLIFY_ONLY_RE = /(?:^|[\\\/])(__tests__|__mocks__|tests?|spec|specs|cypress|e2e|fixtures?)[\\\/]|\.(test|spec)\.[mc]?[jt]sx?$|\.fixture\.[mc]?[jt]sx?$/i;
92
96
 
93
97
  switch (command) {
94
98
  case 'check-before-agent': {
@@ -180,11 +184,20 @@ switch (command) {
180
184
  }
181
185
  case 'reset-edit-gates': {
182
186
  var fp = process.env.TOOL_INPUT_file_path || '';
183
- if (fp && EDIT_RESET_SKIP_RE.test(fp)) break;
187
+ // Inert files (markdown, lockfiles, CHANGELOG, .env.example): no gate reset.
188
+ if (fp && EDIT_RESET_SKIP_BOTH_RE.test(fp)) break;
184
189
  var s = readState();
185
- if (!s.testsRun && !s.simplifyRun) break;
186
- s.testsRun = false;
187
- s.simplifyRun = false;
190
+ // Test-only edits invalidate testsRun but preserve simplifyRun (#908).
191
+ var isTestOnly = fp && EDIT_RESET_SKIP_SIMPLIFY_ONLY_RE.test(fp);
192
+ var resetTests = s.testsRun;
193
+ var resetSimplify = s.simplifyRun && !isTestOnly;
194
+ if (!resetTests && !resetSimplify) break;
195
+ var gates = [];
196
+ if (resetTests) { s.testsRun = false; gates.push('tests'); }
197
+ if (resetSimplify) { s.simplifyRun = false; gates.push('simplify'); }
198
+ if (fp) {
199
+ s.lastResetBy = { file: fp, at: new Date().toISOString(), gates: gates };
200
+ }
188
201
  writeState(s);
189
202
  break;
190
203
  }
@@ -205,6 +218,9 @@ switch (command) {
205
218
  for (var i = 0; i < missing.length; i++) {
206
219
  process.stderr.write(' - ' + missing[i] + '\n');
207
220
  }
221
+ if (s.lastResetBy && s.lastResetBy.file) {
222
+ process.stderr.write('Last gate reset: ' + s.lastResetBy.file + ' (' + (s.lastResetBy.gates || []).join(', ') + ')\n');
223
+ }
208
224
  process.stderr.write('Disable per-gate via moflo.yaml:\n');
209
225
  process.stderr.write(' gates:\n testing_gate: false\n simplify_gate: false\n learnings_gate: false\n');
210
226
  process.exit(2);
@@ -595,7 +595,7 @@ try {
595
595
 
596
596
  // Gate and hook helpers — shipped as static files in bin/
597
597
  const binHelperFiles = [
598
- 'gate.cjs', 'gate-hook.mjs', 'prompt-hook.mjs', 'hook-handler.cjs',
598
+ 'gate.cjs', 'gate-hook.mjs', 'prompt-hook.mjs', 'hook-handler.cjs', 'simplify-classify.cjs',
599
599
  ];
600
600
  for (const file of binHelperFiles) {
601
601
  await syncFile(resolve(binDir, file), resolve(helpersDir, file), `.claude/helpers/${file}`);
@@ -0,0 +1,211 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * /simplify diff classifier — issue #908.
4
+ *
5
+ * Decides which review tier the current diff warrants and returns a JSON
6
+ * dispatch decision. The /simplify skill MUST call this first so routing is
7
+ * deterministic and unit-testable instead of a prose decision Claude makes
8
+ * over and over per run.
9
+ *
10
+ * Rule (per user direction): default to single-agent Sonnet review. Only
11
+ * escalate to a 3-agent fan-out when diff signals genuinely warrant it.
12
+ * Opus is never selected — the existing skill already documents that.
13
+ *
14
+ * Outputs JSON:
15
+ * {
16
+ * "tier": "TRIVIAL" | "SMALL" | "NORMAL",
17
+ * "model": "sonnet",
18
+ * "agentCount": 0 | 1 | 3,
19
+ * "reasoning": [string, ...],
20
+ * "stats": { added, deleted, fileCount, declAdded, declRemoved, ... }
21
+ * }
22
+ *
23
+ * Usage:
24
+ * node bin/simplify-classify.cjs [--base main]
25
+ * node bin/simplify-classify.cjs --diff <unified-diff-on-stdin>
26
+ *
27
+ * The --diff stdin form exists so unit tests can drive the classifier
28
+ * with synthetic diffs (no git repo required).
29
+ */
30
+ 'use strict';
31
+
32
+ const { execSync } = require('child_process');
33
+
34
+ // Paths where new logic warrants the 3-agent fan-out (issue #908).
35
+ // Mechanical edits inside these paths are still SMALL; only adding/removing
36
+ // declarations triggers escalation.
37
+ const SECURITY_PATHS = [
38
+ /(?:^|[\\\/])aidefence[\\\/]/i,
39
+ /(?:^|[\\\/])swarm[\\\/]consensus[\\\/]/i,
40
+ /(?:^|[\\\/])hooks?[\\\/](?:handlers?|gate|wiring)/i,
41
+ /(?:^|[\\\/])services[\\\/]daemon-lock\.ts$/i,
42
+ /(?:^|[\\\/])bin[\\\/]gate\./i,
43
+ /(?:^|[\\\/])bin[\\\/]session-start-launcher\./i,
44
+ /(?:^|[\\\/])\.claude[\\\/]helpers[\\\/]gate/i,
45
+ ];
46
+
47
+ function safeExec(cmd) {
48
+ try { return execSync(cmd, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }); }
49
+ catch { return ''; }
50
+ }
51
+
52
+ function readDiffFromGit(base) {
53
+ // Combined diff: committed-since-base + working-tree
54
+ const committed = safeExec(`git diff ${base}...HEAD`);
55
+ const working = safeExec('git diff HEAD');
56
+ return committed + (working ? '\n' + working : '');
57
+ }
58
+
59
+ /**
60
+ * Parse a unified-diff string into per-file stats and aggregate signals.
61
+ * No git/I/O — pure function over the diff text. Test-friendly.
62
+ */
63
+ function parseDiff(diff) {
64
+ const lines = diff.split('\n');
65
+ const files = new Map(); // filename → { added, deleted, declAdded, declRemoved, isNew, isRenamed }
66
+ let current = null;
67
+
68
+ // Match function/class/export-const-arrow/method declarations being
69
+ // added or removed. Conservative — biased toward false negatives so we
70
+ // don't over-escalate.
71
+ const DECL_RE = /^(?:export\s+)?(?:default\s+)?(?:async\s+)?(?:function|class|interface|type)\s+\w/;
72
+ const ARROW_DECL_RE = /^(?:export\s+)?(?:const|let|var)\s+\w+\s*[:=].*=>\s*\{?$/;
73
+
74
+ for (let i = 0; i < lines.length; i++) {
75
+ const ln = lines[i];
76
+
77
+ // File header: `diff --git a/path b/path`
78
+ let m = ln.match(/^diff --git (?:a\/)?(.+?) (?:b\/)?(.+)$/);
79
+ if (m) {
80
+ const filename = m[2];
81
+ current = { filename, added: 0, deleted: 0, declAdded: 0, declRemoved: 0, isNew: false, isRenamed: false };
82
+ files.set(filename, current);
83
+ continue;
84
+ }
85
+ if (!current) continue;
86
+
87
+ if (ln.startsWith('new file mode')) current.isNew = true;
88
+ if (ln.startsWith('rename from') || ln.startsWith('rename to') || ln.startsWith('similarity index')) current.isRenamed = true;
89
+
90
+ // Skip diff headers
91
+ if (ln.startsWith('+++') || ln.startsWith('---') || ln.startsWith('@@') || ln.startsWith('index ')) continue;
92
+
93
+ if (ln.startsWith('+') && !ln.startsWith('+++')) {
94
+ current.added++;
95
+ const body = ln.slice(1).trim();
96
+ if (DECL_RE.test(body) || ARROW_DECL_RE.test(body)) current.declAdded++;
97
+ } else if (ln.startsWith('-') && !ln.startsWith('---')) {
98
+ current.deleted++;
99
+ const body = ln.slice(1).trim();
100
+ if (DECL_RE.test(body) || ARROW_DECL_RE.test(body)) current.declRemoved++;
101
+ }
102
+ }
103
+
104
+ // Aggregate
105
+ let added = 0, deleted = 0, declAdded = 0, declRemoved = 0;
106
+ let newFiles = 0, renamedFiles = 0;
107
+ let securityHit = false;
108
+ for (const f of files.values()) {
109
+ added += f.added;
110
+ deleted += f.deleted;
111
+ declAdded += f.declAdded;
112
+ declRemoved += f.declRemoved;
113
+ if (f.isNew) newFiles++;
114
+ if (f.isRenamed) renamedFiles++;
115
+ if (SECURITY_PATHS.some(rx => rx.test(f.filename))) securityHit = true;
116
+ }
117
+
118
+ return {
119
+ added, deleted, declAdded, declRemoved,
120
+ netDecls: declAdded - declRemoved,
121
+ fileCount: files.size,
122
+ newFiles, renamedFiles,
123
+ securityHit,
124
+ files: [...files.keys()],
125
+ };
126
+ }
127
+
128
+ /**
129
+ * Pure decision function. Takes parsed stats, returns dispatch decision.
130
+ * No I/O. Easy to unit-test with synthetic stats.
131
+ */
132
+ function decide(stats) {
133
+ const reasoning = [];
134
+ const totalChange = stats.added + stats.deleted;
135
+
136
+ if (totalChange === 0) {
137
+ return { tier: 'TRIVIAL', model: 'sonnet', agentCount: 0, reasoning: ['empty diff — nothing to review'], stats };
138
+ }
139
+
140
+ // TRIVIAL: tiny diff, no declarations changed
141
+ if (totalChange <= 10 && stats.fileCount <= 1 && stats.netDecls === 0 && stats.declAdded === 0 && stats.declRemoved === 0) {
142
+ reasoning.push(`≤10 LOC in 1 file with no declaration changes`);
143
+ return { tier: 'TRIVIAL', model: 'sonnet', agentCount: 0, reasoning, stats };
144
+ }
145
+
146
+ // Mechanical relocation detection — the #906 case.
147
+ // If declarations were both ADDED and REMOVED at roughly matching rates,
148
+ // it's a structural move, not net-new logic. Judge by declaration balance,
149
+ // not raw LOC balance — formatting/blank-line differences between source
150
+ // and destination files easily push raw LOC out of balance even when the
151
+ // semantic change is purely "moved 5 functions across 5 new files".
152
+ // Mechanical relocations are SMALL even when many files / many lines.
153
+ const declTouched = stats.declAdded + stats.declRemoved;
154
+ const isMostlyRelocation = stats.declAdded >= 2
155
+ && stats.declRemoved >= 2
156
+ && Math.abs(stats.netDecls) <= Math.max(2, Math.floor(declTouched * 0.30));
157
+
158
+ if (isMostlyRelocation) {
159
+ reasoning.push(
160
+ `mostly relocation: ${stats.declAdded} decls added, ${stats.declRemoved} removed, net ${stats.netDecls >= 0 ? '+' : ''}${stats.netDecls}`,
161
+ );
162
+ return { tier: 'SMALL', model: 'sonnet', agentCount: 1, reasoning, stats };
163
+ }
164
+
165
+ // Escalation triggers — any one trips NORMAL (3 agents).
166
+ // Always Sonnet — Opus is never the right model for /simplify per skill rule.
167
+ const triggers = [];
168
+ if (totalChange > 500) triggers.push(`>500 LOC changed (${totalChange})`);
169
+ if (stats.fileCount >= 5 && stats.netDecls >= 3) triggers.push(`${stats.fileCount} files with ${stats.netDecls} net new declarations`);
170
+ if (stats.securityHit && stats.netDecls > 0) triggers.push('security-sensitive path with new logic');
171
+ if (stats.newFiles >= 3 && stats.declAdded >= 5) triggers.push(`${stats.newFiles} new files with ${stats.declAdded} new declarations`);
172
+
173
+ if (triggers.length > 0) {
174
+ return { tier: 'NORMAL', model: 'sonnet', agentCount: 3, reasoning: triggers, stats };
175
+ }
176
+
177
+ // Default: SMALL — single sonnet agent
178
+ reasoning.push(`small/medium diff: ${totalChange} LOC across ${stats.fileCount} file(s), +${stats.declAdded}/-${stats.declRemoved} decls`);
179
+ return { tier: 'SMALL', model: 'sonnet', agentCount: 1, reasoning, stats };
180
+ }
181
+
182
+ function classifyDiff(diffText) {
183
+ return decide(parseDiff(diffText));
184
+ }
185
+
186
+ function classifyFromGit(base = 'main') {
187
+ return classifyDiff(readDiffFromGit(base));
188
+ }
189
+
190
+ if (require.main === module) {
191
+ const args = process.argv.slice(2);
192
+ const baseIdx = args.indexOf('--base');
193
+ const base = baseIdx >= 0 ? args[baseIdx + 1] : 'main';
194
+ const stdinDiff = args.includes('--diff') || args.includes('--stdin');
195
+
196
+ let result;
197
+ if (stdinDiff) {
198
+ let buf = '';
199
+ process.stdin.setEncoding('utf-8');
200
+ process.stdin.on('data', (d) => { buf += d; });
201
+ process.stdin.on('end', () => {
202
+ result = classifyDiff(buf);
203
+ process.stdout.write(JSON.stringify(result, null, 2) + '\n');
204
+ });
205
+ } else {
206
+ result = classifyFromGit(base);
207
+ process.stdout.write(JSON.stringify(result, null, 2) + '\n');
208
+ }
209
+ }
210
+
211
+ module.exports = { parseDiff, decide, classifyDiff, classifyFromGit };