@skathio/somi 4.0.1 → 4.1.1
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/.claude/settings.json +1 -1
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.copilot-extension/extension.json +1 -1
- package/.copilot-extension/marketplace.json +1 -1
- package/README.md +5 -4
- package/VERSION +1 -1
- package/agents/somi-coder.md +4 -228
- package/agents/somi-designer.md +4 -160
- package/agents/somi-discovery-analyst.md +4 -276
- package/agents/somi-flow.md +47 -0
- package/agents/somi-general.md +36 -0
- package/agents/somi-planner.md +4 -341
- package/agents/somi-reviewer.md +4 -199
- package/agents/somi.md +81 -253
- package/commands/adopt.md +6 -84
- package/commands/architecture-review.md +11 -86
- package/commands/atlas.md +10 -25
- package/commands/code-loop.md +10 -221
- package/commands/code-parallel.md +10 -151
- package/commands/code.md +9 -157
- package/commands/debug.md +9 -131
- package/commands/design.md +9 -160
- package/commands/discover.md +9 -194
- package/commands/impact.md +9 -25
- package/commands/incident.md +9 -39
- package/commands/plan-loop.md +9 -181
- package/commands/plan.md +9 -244
- package/commands/pr.md +10 -25
- package/commands/refactor-design.md +11 -89
- package/commands/refactor.md +11 -50
- package/commands/release-readiness.md +11 -70
- package/commands/review-panel.md +10 -141
- package/commands/review.md +11 -181
- package/commands/security-review.md +11 -75
- package/commands/ship-loop.md +9 -260
- package/commands/ship.md +9 -115
- package/commands/somi-config.md +11 -130
- package/commands/test-strategy.md +11 -75
- package/commands/upgrade.md +9 -72
- package/hooks/hooks.json +1 -1
- package/hooks/lib/common.mjs +8 -5
- package/hooks/pre-tool/block-dangerous-bash.mjs +1 -1
- package/hooks/pre-tool/block-secret-writes.mjs +5 -7
- package/hooks/pre-tool/guard-protected-paths.mjs +54 -34
- package/package.json +1 -1
- package/scripts/lib/cost-defaults.mjs +2 -0
- package/scripts/lib/escalation-facts.mjs +146 -0
- package/scripts/lib/escalation.mjs +129 -0
- package/scripts/lib/findings-ledger.mjs +241 -0
- package/scripts/lib/install-text.mjs +97 -0
- package/scripts/lib/loop-caps.mjs +140 -0
- package/scripts/lib/loop-diff.mjs +12 -70
- package/scripts/lib/loop-engine.mjs +227 -0
- package/scripts/lib/loop-escalate.mjs +49 -0
- package/scripts/lib/loop-exit.mjs +47 -0
- package/scripts/lib/loop-notes-compact.mjs +72 -0
- package/scripts/lib/loop-notes.mjs +282 -0
- package/scripts/lib/loop-review.mjs +355 -12
- package/scripts/lib/loop-state.mjs +44 -0
- package/scripts/lib/loop-step-close.mjs +75 -0
- package/scripts/lib/loop-step-kit.mjs +179 -0
- package/scripts/lib/loop-step-review.mjs +140 -0
- package/scripts/lib/loop-step-tool.mjs +89 -0
- package/scripts/lib/loop-step.mjs +303 -0
- package/scripts/lib/mcp-project-root.mjs +18 -7
- package/scripts/lib/path-facts.mjs +187 -0
- package/scripts/lib/path-safety.mjs +171 -0
- package/scripts/lib/run-card.mjs +73 -0
- package/scripts/size-budget-overrides.json +5 -13
- package/scripts/somi-card.mjs +39 -0
- package/scripts/somi-findings.mjs +56 -271
- package/scripts/somi-loop.mjs +84 -443
- package/scripts/somi-mcp.mjs +26 -95
- package/scripts/validate.sh +75 -57
- package/skills/flow-adopt/SKILL.md +92 -0
- package/skills/flow-architecture-review/SKILL.md +94 -0
- package/skills/flow-atlas/SKILL.md +34 -0
- package/skills/flow-code/SKILL.md +167 -0
- package/skills/flow-code-loop/SKILL.md +263 -0
- package/skills/flow-code-parallel/SKILL.md +160 -0
- package/skills/flow-debug/SKILL.md +141 -0
- package/skills/flow-design/SKILL.md +170 -0
- package/skills/flow-discover/SKILL.md +204 -0
- package/skills/flow-impact/SKILL.md +35 -0
- package/skills/flow-incident/SKILL.md +49 -0
- package/skills/flow-plan/SKILL.md +254 -0
- package/skills/flow-plan-loop/SKILL.md +254 -0
- package/skills/flow-pr/SKILL.md +34 -0
- package/skills/flow-refactor/SKILL.md +58 -0
- package/skills/flow-refactor-design/SKILL.md +97 -0
- package/skills/flow-release-readiness/SKILL.md +82 -0
- package/skills/flow-review/SKILL.md +193 -0
- package/skills/flow-review-panel/SKILL.md +154 -0
- package/skills/flow-security-review/SKILL.md +83 -0
- package/skills/flow-ship/SKILL.md +125 -0
- package/skills/flow-ship-loop/SKILL.md +274 -0
- package/skills/flow-somi-config/SKILL.md +142 -0
- package/skills/flow-status/SKILL.md +43 -0
- package/skills/flow-test-strategy/SKILL.md +83 -0
- package/skills/flow-upgrade/SKILL.md +82 -0
- package/skills/role-coder/SKILL.md +247 -0
- package/skills/role-designer/SKILL.md +172 -0
- package/skills/role-discovery-analyst/SKILL.md +288 -0
- package/skills/role-planner/SKILL.md +353 -0
- package/skills/role-reviewer/SKILL.md +218 -0
- package/skills/somi-checkpoint/SKILL.md +95 -0
- package/skills/somi-dispatch/SKILL.md +14 -14
- package/skills/somi-routing/SKILL.md +6 -2
- package/tests/hooks/cases/block-secret-writes.json +6 -1
- package/tests/hooks/cases/guard-protected-paths.json +44 -2
- package/tests/hooks/run.sh +21 -0
- package/tests/scripts/checkpoint-gate.sh +2 -2
- package/tests/scripts/cost-defaults.sh +2 -0
- package/tests/scripts/dispatch-line-gate.sh +4 -4
- package/tests/scripts/dispatch-reference-gate.sh +14 -14
- package/tests/scripts/escalation.sh +409 -0
- package/tests/scripts/eval-runner.sh +8 -7
- package/tests/scripts/fact-sheet.sh +296 -0
- package/tests/scripts/goldens/somi-loop.json +2 -2
- package/tests/scripts/lib/dispatch-reference-gate.mjs +14 -5
- package/tests/scripts/lib/escalation-facts-tests.mjs +140 -0
- package/tests/scripts/lib/escalation-tests.mjs +154 -0
- package/tests/scripts/lib/findings-ledger-tests.mjs +118 -0
- package/tests/scripts/lib/loop-engine-tests.mjs +287 -0
- package/tests/scripts/lib/loop-escalate-tests.mjs +356 -0
- package/tests/scripts/lib/loop-flow-gate.mjs +173 -0
- package/tests/scripts/lib/loop-notes-tests.mjs +384 -0
- package/tests/scripts/lib/loop-step-cli-tests.mjs +156 -0
- package/tests/scripts/lib/loop-step-close-tests.mjs +351 -0
- package/tests/scripts/lib/loop-step-lenient-tests.mjs +114 -0
- package/tests/scripts/lib/loop-step-state-tests.mjs +127 -0
- package/tests/scripts/lib/loop-step-tests.mjs +499 -0
- package/tests/scripts/lib/loop-step-unwritten-tests.mjs +55 -0
- package/tests/scripts/lib/path-facts-tests.mjs +198 -0
- package/tests/scripts/lib/path-safety-tests.mjs +201 -0
- package/tests/scripts/lib/run-suites.mjs +139 -0
- package/tests/scripts/lib/sizing-score-tests.mjs +210 -0
- package/tests/scripts/lib/sizing-set-tests.mjs +131 -0
- package/tests/scripts/lib/somi-mcp-loop-tests.mjs +226 -0
- package/tests/scripts/lib/somi-mcp-tests.mjs +21 -1
- package/tests/scripts/loop-engine.sh +22 -0
- package/tests/scripts/loop-flow-gate.sh +11 -0
- package/tests/scripts/review-panel-seats.sh +4 -4
- package/tests/scripts/run-card.sh +57 -0
- package/tests/scripts/run-suites.sh +74 -0
- package/tests/scripts/run.sh +11 -0
- package/tests/scripts/size-tier.sh +3 -3
- package/tests/scripts/somi-agent-dispatch-contract.sh +23 -3
- package/tests/scripts/somi-mcp.sh +1 -0
- package/tests/scripts/suite-plan.json +20 -0
package/scripts/somi-loop.mjs
CHANGED
|
@@ -1,48 +1,16 @@
|
|
|
1
|
-
// somi-loop.mjs — deterministic state engine for the bounded loops
|
|
1
|
+
// somi-loop.mjs — command line of the deterministic state engine for the bounded loops: parses the arguments,
|
|
2
|
+
// calls the handler in lib/loop-engine.mjs and prints its result. No gate logic lives here.
|
|
2
3
|
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
// claim, etc).
|
|
4
|
+
// The gates live in lib/loop-engine.mjs, cap resolution (flag > env var > .somi/config.json > default) in
|
|
5
|
+
// lib/loop-caps.mjs, state in lib/loop-state.mjs, diff measurement in lib/loop-diff.mjs. Exit codes and stdout
|
|
6
|
+
// JSON shapes are frozen; callers branch on them: 0 ok, 2 max passes, 3 diff cap, 4 time budget, 5 circuit breaker
|
|
7
|
+
// (step after-review), 64 usage, 65 record-dispatch's resolver refused the agent, 70 internal error (step only).
|
|
8
8
|
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
// never been told about, so without it a new file costs nothing against the cap.
|
|
15
|
-
//
|
|
16
|
-
// Cap precedence (matches the gate tables): CLI flag > env var > .somi/config.json
|
|
17
|
-
// > default. `init` resolves once from that chain and freezes the result into state.
|
|
18
|
-
// `pass`/`check-diff` re-resolve their own cap on every call (the documented
|
|
19
|
-
// remedy for a fired gate, "adjust the env var and re-run", used to be inert): absent
|
|
20
|
-
// an explicit CLI flag or env var THIS invocation, the cap already in force for the
|
|
21
|
-
// loop stands unchanged (a `.somi/config.json` edit made mid-loop cannot silently
|
|
22
|
-
// reopen an already-resolved gate); see reresolveCap() below. Every cap value, at
|
|
23
|
-
// `init` and on re-resolution alike, is validated as a non-negative integer before
|
|
24
|
-
// use and rejected otherwise — a malformed value must not silently disable a gate.
|
|
25
|
-
// Diff measurement EXCLUDES .somi/ and .claude/ — artifact churn (progress/diary
|
|
26
|
-
// updates every pass) must not eat the code diff budget.
|
|
27
|
-
//
|
|
28
|
-
// Exit codes (callers branch on these — do not repurpose):
|
|
29
|
-
// 0 ok
|
|
30
|
-
// 2 max-passes-exceeded (`pass` would exceed the cap)
|
|
31
|
-
// 3 diff-cap-exceeded (`check-diff`: weighted lines over the cap;
|
|
32
|
-
// out-of-scope lines count double)
|
|
33
|
-
// 4 time-budget-exceeded (`pass`: the /ship-loop run started by `run-start` on this slug is out of time)
|
|
34
|
-
// 64 usage / environment error (65: record-dispatch's resolver refused the agent)
|
|
35
|
-
//
|
|
36
|
-
// Subcommands (each prints one JSON object):
|
|
37
|
-
// run-start record a /ship-loop run's start and optional --max-minutes budget (kept on resume)
|
|
38
|
-
// init new loop state: baseline SHA, resolved caps, review_min from the sizing record
|
|
39
|
-
// resume | stats the stored state
|
|
40
|
-
// pass next pass, or exit 2 (cap) / 4 (time budget); repeats review_min
|
|
41
|
-
// check-diff weighted cumulative diff against the baseline, or exit 3
|
|
42
|
-
// review-scope what the generalist reviewer reads this pass: delta (from/to trees, files) or full
|
|
43
|
-
// record-dispatch append the resolver's answer for --agent [--host] to the state
|
|
44
|
-
// record-pass append the verdict and counts, with the reviewed tree
|
|
45
|
-
// finish terminal status, cap overrides, dispatches
|
|
9
|
+
// Subcommands (each prints one JSON object): run-start, init, resume, stats, pass, check-diff, review-scope,
|
|
10
|
+
// record-dispatch, record-pass, finish, and `step <open|after-code|after-review|close|status>`: the composite steps of
|
|
11
|
+
// lib/loop-step.mjs, arguments as `--flag value` (--findings and --fixed as JSON text or @<file> holding it, --reviewers comma-separated), the
|
|
12
|
+
// response on stdout as one JSON line, its `exit` as the exit code, nothing on stderr: the response carries every refusal
|
|
13
|
+
// (a bad flag included, exit 64), and an internal error (70) goes to <state dir>/step-errors.log, never to the stderr a model reads.
|
|
46
14
|
//
|
|
47
15
|
// Tested by tests/scripts/run.sh (wired into scripts/validate.sh / CI).
|
|
48
16
|
|
|
@@ -50,33 +18,16 @@ import fs from 'node:fs';
|
|
|
50
18
|
import path from 'node:path';
|
|
51
19
|
import { execFileSync } from 'node:child_process';
|
|
52
20
|
import { fileURLToPath } from 'node:url';
|
|
53
|
-
import {
|
|
54
|
-
import {
|
|
55
|
-
import {
|
|
56
|
-
import {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
// Thrown to unwind to the top-level handler, which sets process.exitCode and
|
|
63
|
-
// lets Node exit naturally — avoids process.exit()'s risk of truncating
|
|
64
|
-
// buffered stdout/stderr writes on a pipe.
|
|
65
|
-
class ExitSignal extends Error {
|
|
66
|
-
constructor(code) {
|
|
67
|
-
super(`exit ${code}`);
|
|
68
|
-
this.code = code;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
21
|
+
import { LoopExit, PROG, usageError } from './lib/loop-exit.mjs';
|
|
22
|
+
import { runStep } from './lib/loop-step.mjs';
|
|
23
|
+
import { base, clean, cut, stepLog } from './lib/loop-step-kit.mjs';
|
|
24
|
+
import { checkDiff, finish, init, pass, recordDispatch, recordPass, resume, reviewScope, runStart } from './lib/loop-engine.mjs';
|
|
25
|
+
|
|
26
|
+
// Unwinding with a LoopExit reaches the top-level handler, which writes its message to stderr and sets
|
|
27
|
+
// process.exitCode, letting Node exit naturally — avoids process.exit()'s risk of truncating buffered
|
|
28
|
+
// stdout/stderr writes on a pipe.
|
|
72
29
|
function die(msg) {
|
|
73
|
-
|
|
74
|
-
throw new ExitSignal(64);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function fail(code, msg) {
|
|
78
|
-
process.stderr.write(`${msg}\n`);
|
|
79
|
-
throw new ExitSignal(code);
|
|
30
|
+
throw usageError(msg);
|
|
80
31
|
}
|
|
81
32
|
|
|
82
33
|
function requireGit() {
|
|
@@ -93,404 +44,94 @@ function projectRoot() {
|
|
|
93
44
|
return b;
|
|
94
45
|
}
|
|
95
46
|
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
} catch {
|
|
102
|
-
return {};
|
|
103
|
-
}
|
|
47
|
+
// A handler's result: its one JSON object on stdout, its message on stderr, its code as the exit code.
|
|
48
|
+
function deliver(result, space) {
|
|
49
|
+
if (result.out !== undefined) console.log(JSON.stringify(result.out, null, space));
|
|
50
|
+
if (result.message) process.stderr.write(`${result.message}\n`);
|
|
51
|
+
process.exitCode = result.code;
|
|
104
52
|
}
|
|
105
53
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
54
|
+
// subcommand -> handler, and the JSON indent where it is not the compact default (`stats` is read by people).
|
|
55
|
+
const COMMANDS = {
|
|
56
|
+
'run-start': { handler: runStart }, init: { handler: init }, resume: { handler: resume }, stats: { handler: resume, space: 2 },
|
|
57
|
+
pass: { handler: pass }, 'check-diff': { handler: checkDiff }, 'review-scope': { handler: reviewScope },
|
|
58
|
+
'record-dispatch': { handler: recordDispatch }, 'record-pass': { handler: recordPass }, finish: { handler: finish },
|
|
59
|
+
};
|
|
60
|
+
const STEP_FLAGS = ['slug', 'loop', 'iteration', 'files', 'host', 'max-passes', 'diff-cap', 'agent', 'review-mode', 'review-scope', 'reviewers', 'verdict', 'blockers', 'majors', 'findings', 'review', 'fixed', 'status'];
|
|
61
|
+
// `--findings` and `--fixed` take JSON text, or `@<file>` naming a UTF-8 file that holds it: Windows PowerShell 5.1 strips
|
|
62
|
+
// the double quotes inside an argument it passes to node, so JSON typed on its command line never arrives intact.
|
|
63
|
+
function jsonArg(key, value) {
|
|
64
|
+
let text = value;
|
|
65
|
+
if (value.startsWith('@')) {
|
|
66
|
+
try { text = fs.readFileSync(value.slice(1), 'utf8').replace(/^\uFEFF/, ''); } catch (e) { die(`--${key}: cannot read ${cut(value.slice(1), 80)} (${e.code ?? 'error'})`); }
|
|
111
67
|
}
|
|
112
|
-
return
|
|
68
|
+
try { return JSON.parse(text); } catch { return die(`--${key} expects JSON text, or @<file> holding it (from PowerShell, which strips the quotes in an argument, write the JSON to a file and pass --${key} @<file>)`); }
|
|
113
69
|
}
|
|
114
70
|
|
|
115
|
-
//
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (
|
|
121
|
-
if (
|
|
122
|
-
|
|
71
|
+
// The step's arguments from `--flag value` pairs, as runStep takes them. A flag it does not know, a repeated one or one without a value is a usage error (echoed cut and cleaned).
|
|
72
|
+
function stepArgs(argv) {
|
|
73
|
+
const args = { step: argv[0] };
|
|
74
|
+
for (let i = 1; i < argv.length; i += 2) {
|
|
75
|
+
const key = argv[i].replace(/^--/, '');
|
|
76
|
+
if (!argv[i].startsWith('--') || !STEP_FLAGS.includes(key) || argv[i + 1] === undefined) die(`unknown argument or missing value: ${cut(argv[i], 40)}`);
|
|
77
|
+
if (Object.hasOwn(args, key.replaceAll('-', '_'))) die(`repeated argument: ${cut(argv[i], 40)}`);
|
|
78
|
+
args[key.replaceAll('-', '_')] = argv[i + 1];
|
|
123
79
|
}
|
|
124
|
-
|
|
80
|
+
for (const k of ['findings', 'fixed']) if (args[k] !== undefined) args[k] = jsonArg(k, args[k]);
|
|
81
|
+
if (args.reviewers !== undefined) args.reviewers = args.reviewers.split(',');
|
|
82
|
+
return args;
|
|
125
83
|
}
|
|
126
84
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
// --- mid-loop cap re-resolution --------------------------------------------
|
|
133
|
-
// `init` resolves caps once (CLI flag > env var > .somi/config.json > default) and
|
|
134
|
-
// freezes them into state -- deliberately, so the diff baseline and pass history stay
|
|
135
|
-
// stable across a whole loop. But commands/code-loop.md's Guardrails document a
|
|
136
|
-
// remedy for a gate that's wrong for the work at hand: "the user adjusts the env var
|
|
137
|
-
// explicitly and re-runs -- the loop does not 'decide' to widen its own bounds." That
|
|
138
|
-
// remedy needs a path that re-reads the override on the SAME subcommand (`pass`,
|
|
139
|
-
// `check-diff`), without `init --force` (which discards `pass`/`history`).
|
|
140
|
-
//
|
|
141
|
-
// `pass` and `check-diff` call reresolveCap() below before their gate check.
|
|
142
|
-
// Precedence matches what's documented: CLI flag > env var > .somi/config.json >
|
|
143
|
-
// default -- an explicit CLI flag or env var THIS invocation is the only thing that
|
|
144
|
-
// can move the cap; absent one, the cap already in force for the loop stands (a
|
|
145
|
-
// `.somi/config.json` edit made while a loop is running must NOT silently reopen an
|
|
146
|
-
// already-resolved gate). A resolved value that differs from what's stored is
|
|
147
|
-
// persisted into `caps` immediately, with an entry appended to a `cap_overrides`
|
|
148
|
-
// audit trail (field, from, to, source, after_pass, at) -- the after-the-fact record
|
|
149
|
-
// the documented remedy needs, since the env var itself lives only in a shell that
|
|
150
|
-
// may since have exited. `cap_overrides` is created lazily, only on the first real
|
|
151
|
-
// override, so a loop that never overrides anything keeps exactly today's state
|
|
152
|
-
// shape (no golden-fixture regeneration forced by this fix). `baseline_sha`,
|
|
153
|
-
// `started`, `pass`, and `history` are never touched here.
|
|
154
|
-
//
|
|
155
|
-
// Every value that reaches `Number()` on this path is user input (a CLI flag, an env
|
|
156
|
-
// var, or a config value) and is validated before use: `Number()` on a malformed
|
|
157
|
-
// string (`"1,000"`, `"unlimited"`) yields NaN, which is false against BOTH gate
|
|
158
|
-
// comparisons (`cur + 1 > max`, `weighted > cap`) -- silently switching the gate off
|
|
159
|
-
// -- and `JSON.stringify` then persists that NaN as `null`, which the next bare call
|
|
160
|
-
// falls through past (via `firstDefined`) to re-arm at the default, with no
|
|
161
|
-
// `cap_overrides` entry recording the reversion. `validateCap()` rejects rather than
|
|
162
|
-
// coerces, so a malformed value dies loudly instead of disabling a gate.
|
|
163
|
-
const CAP_ENV = {
|
|
164
|
-
code: { max_passes: 'SOMI_CODE_LOOP_MAX_PASSES', diff_cap_lines: 'SOMI_CODE_LOOP_DIFF_CAP' },
|
|
165
|
-
plan: { max_passes: 'SOMI_PLAN_LOOP_MAX_PASSES' },
|
|
85
|
+
const FLAGS = {
|
|
86
|
+
'--slug': 'slug', '--loop': 'loop', '--iteration': 'iteration', '--files': 'files', '--max-passes': 'maxPasses',
|
|
87
|
+
'--diff-cap': 'diffCap', '--severity-floor': 'severity', '--verdict': 'verdict', '--blockers': 'blockers',
|
|
88
|
+
'--majors': 'majors', '--status': 'status', '--agent': 'agent', '--host': 'host', '--max-minutes': 'maxMinutes',
|
|
166
89
|
};
|
|
167
|
-
const CAP_FLAG = { max_passes: '--max-passes', diff_cap_lines: '--diff-cap' };
|
|
168
|
-
const CAP_CONFIG_SECTION = { code: 'code_loop', plan: 'plan_loop' };
|
|
169
|
-
const CAP_DEFAULT = { max_passes: 3, diff_cap_lines: 400 };
|
|
170
|
-
|
|
171
|
-
// Rejects a malformed cap value rather than letting `Number()` coerce it to NaN.
|
|
172
|
-
// `label` names the source in the error so `die()`'s message points at what to fix
|
|
173
|
-
// (a specific flag, env var, or the generic field name at `init`, where the value
|
|
174
|
-
// may have come from any of CLI/env/config).
|
|
175
|
-
function validateCap(raw, label) {
|
|
176
|
-
const n = Number(raw);
|
|
177
|
-
if (!Number.isInteger(n) || n < 0) {
|
|
178
|
-
die(`${label}: expected a non-negative integer, got "${raw}"`);
|
|
179
|
-
}
|
|
180
|
-
return n;
|
|
181
|
-
}
|
|
182
90
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
function reresolveCap(state, field, argVal) {
|
|
195
|
-
const family = state.loop === 'plan' ? 'plan' : 'code';
|
|
196
|
-
if (field === 'diff_cap_lines' && family === 'plan') {
|
|
197
|
-
return { resolved: 0, override: null }; // plan loops have no diff cap (see `init`)
|
|
198
|
-
}
|
|
199
|
-
const envVar = CAP_ENV[family][field];
|
|
200
|
-
const stored = state.caps[field];
|
|
201
|
-
const explicit = firstDefined(argVal, process.env[envVar]);
|
|
202
|
-
const isCli = argVal !== undefined && argVal !== '';
|
|
203
|
-
const resolved = explicit === undefined
|
|
204
|
-
? stored
|
|
205
|
-
: validateCap(explicit, isCli ? CAP_FLAG[field] : envVar);
|
|
206
|
-
let override = null;
|
|
207
|
-
if (explicit !== undefined && resolved !== stored) {
|
|
208
|
-
override = {
|
|
209
|
-
field,
|
|
210
|
-
from: stored,
|
|
211
|
-
to: resolved,
|
|
212
|
-
source: isCli ? 'cli' : 'env',
|
|
213
|
-
after_pass: state.pass,
|
|
214
|
-
at: nowIso(),
|
|
215
|
-
};
|
|
91
|
+
function step(argv) {
|
|
92
|
+
const root = projectRoot();
|
|
93
|
+
const explicit = Boolean(process.env.SOMI_LOOP_STATE_DIR);
|
|
94
|
+
const stateDir = process.env.SOMI_LOOP_STATE_DIR || path.join(root, '.somi', 'somi-state', 'loop');
|
|
95
|
+
const installRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
96
|
+
let resp;
|
|
97
|
+
try {
|
|
98
|
+
resp = runStep({ root, env: process.env, stateDir, installRoot, via: 'cli', log: stepLog(root, stateDir, explicit) }, stepArgs(argv));
|
|
99
|
+
} catch (e) { // a flag the parser refuses answers like any other refusal of a step: the response on stdout
|
|
100
|
+
if (!(e instanceof LoopExit)) throw e;
|
|
101
|
+
resp = base(clean(argv[0] ?? '', 20) || null, { ok: false, exit: e.code, error: clean(e.message.replace(`${PROG}: `, ''), 300) });
|
|
216
102
|
}
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
// Mutates `state.caps[field]` and appends the audit entry. Caller still owns calling
|
|
221
|
-
// saveState() -- kept separate so `pass`/`check-diff` persist the override BEFORE
|
|
222
|
-
// their gate check can throw, not only on the success path.
|
|
223
|
-
function applyCapOverride(state, override) {
|
|
224
|
-
state.caps[override.field] = override.to;
|
|
225
|
-
if (!Array.isArray(state.cap_overrides)) state.cap_overrides = [];
|
|
226
|
-
state.cap_overrides.push(override);
|
|
103
|
+
deliver({ out: resp, code: resp.exit }, undefined);
|
|
227
104
|
}
|
|
228
105
|
|
|
229
106
|
function main() {
|
|
230
107
|
requireGit();
|
|
231
108
|
|
|
232
|
-
const
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
let SLUG = '';
|
|
241
|
-
let LOOP = 'code';
|
|
242
|
-
let ITERATION = '';
|
|
243
|
-
let FILES = '';
|
|
244
|
-
let VERDICT = '';
|
|
245
|
-
let BLOCKERS = '0';
|
|
246
|
-
let MAJORS = '0';
|
|
247
|
-
let STATUS = '';
|
|
248
|
-
let AGENT = '';
|
|
249
|
-
let HOST;
|
|
250
|
-
let ARG_MAX_MINUTES = '';
|
|
251
|
-
let FORCE = false;
|
|
252
|
-
let ARG_MAX_PASSES = '';
|
|
253
|
-
let ARG_DIFF_CAP = '';
|
|
254
|
-
let ARG_SEVERITY = '';
|
|
255
|
-
|
|
256
|
-
while (rest.length > 0) {
|
|
257
|
-
const a = rest[0];
|
|
258
|
-
switch (a) {
|
|
259
|
-
case '--slug': SLUG = rest[1]; rest = rest.slice(2); break;
|
|
260
|
-
case '--loop': LOOP = rest[1]; rest = rest.slice(2); break;
|
|
261
|
-
case '--iteration': ITERATION = rest[1]; rest = rest.slice(2); break;
|
|
262
|
-
case '--files': FILES = rest[1]; rest = rest.slice(2); break;
|
|
263
|
-
case '--max-passes': ARG_MAX_PASSES = rest[1]; rest = rest.slice(2); break;
|
|
264
|
-
case '--diff-cap': ARG_DIFF_CAP = rest[1]; rest = rest.slice(2); break;
|
|
265
|
-
case '--severity-floor': ARG_SEVERITY = rest[1]; rest = rest.slice(2); break;
|
|
266
|
-
case '--verdict': VERDICT = rest[1]; rest = rest.slice(2); break;
|
|
267
|
-
case '--blockers': BLOCKERS = rest[1]; rest = rest.slice(2); break;
|
|
268
|
-
case '--majors': MAJORS = rest[1]; rest = rest.slice(2); break;
|
|
269
|
-
case '--status': STATUS = rest[1]; rest = rest.slice(2); break;
|
|
270
|
-
case '--agent': AGENT = rest[1]; rest = rest.slice(2); break;
|
|
271
|
-
case '--host': HOST = rest[1]; rest = rest.slice(2); break;
|
|
272
|
-
case '--max-minutes': ARG_MAX_MINUTES = rest[1]; rest = rest.slice(2); break;
|
|
273
|
-
case '--force': FORCE = true; rest = rest.slice(1); break;
|
|
274
|
-
default: die(`unknown argument: ${a}`);
|
|
275
|
-
}
|
|
109
|
+
const cmd = process.argv[2] || '';
|
|
110
|
+
if (cmd === 'step') return step(process.argv.slice(3));
|
|
111
|
+
const args = { slug: '', loop: 'code', iteration: '', files: '', verdict: '', blockers: '0', majors: '0', status: '', agent: '', host: undefined, maxMinutes: '', force: false, maxPasses: '', diffCap: '', severity: '' };
|
|
112
|
+
for (let rest = process.argv.slice(3); rest.length > 0;) {
|
|
113
|
+
if (rest[0] === '--force') { args.force = true; rest = rest.slice(1); continue; }
|
|
114
|
+
if (!Object.hasOwn(FLAGS, rest[0])) die(`unknown argument: ${cut(rest[0], 40)}`);
|
|
115
|
+
args[FLAGS[rest[0]]] = rest[1];
|
|
116
|
+
rest = rest.slice(2);
|
|
276
117
|
}
|
|
277
118
|
|
|
278
|
-
if (!
|
|
279
|
-
if (!
|
|
280
|
-
|
|
281
|
-
function stateFile() {
|
|
282
|
-
let name = SLUG;
|
|
283
|
-
if (ITERATION) name = `${SLUG}.${ITERATION}`;
|
|
284
|
-
return path.join(STATE_DIR, `${name}.json`);
|
|
285
|
-
}
|
|
286
|
-
const SF = stateFile();
|
|
287
|
-
|
|
288
|
-
function requireState() {
|
|
289
|
-
if (!fs.existsSync(SF)) die(`no loop state at ${SF} — run init first`);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
function loadState() {
|
|
293
|
-
return JSON.parse(fs.readFileSync(SF, 'utf8'));
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function saveState(state) {
|
|
297
|
-
fs.writeFileSync(SF, JSON.stringify(state));
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
const RUN_ROOT = path.dirname(STATE_DIR);
|
|
301
|
-
switch (CMD) {
|
|
302
|
-
case 'run-start': {
|
|
303
|
-
const raw = firstDefined(ARG_MAX_MINUTES, process.env.SOMI_SHIP_LOOP_MAX_MINUTES, configVal(readConfig(root), ['ship_loop', 'max_minutes']));
|
|
304
|
-
const max = raw === undefined ? null : validateCap(raw, 'max_minutes');
|
|
305
|
-
if (max === 0) die('max_minutes: 0 would disable the budget; leave it unset instead');
|
|
306
|
-
console.log(JSON.stringify(startRun(RUN_ROOT, SLUG, max, { force: FORCE })));
|
|
307
|
-
break;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
case 'init': {
|
|
311
|
-
fs.mkdirSync(STATE_DIR, { recursive: true });
|
|
312
|
-
if (fs.existsSync(SF) && !FORCE) {
|
|
313
|
-
let status;
|
|
314
|
-
try {
|
|
315
|
-
status = JSON.parse(fs.readFileSync(SF, 'utf8')).status;
|
|
316
|
-
} catch {
|
|
317
|
-
status = undefined;
|
|
318
|
-
}
|
|
319
|
-
if (status === 'running') {
|
|
320
|
-
die(`loop state already running at ${SF} — use 'resume' to continue it, or 'init --force' to discard`);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
// A brief-only work item (a plan directory with no phases/) exists only for the sized, human-
|
|
325
|
-
// approved small tier of /ship-loop: refuse it without the tier record's approval stamp.
|
|
326
|
-
const planDir = path.join(root, '.somi', 'plans', SLUG);
|
|
327
|
-
if (LOOP === 'code' && fs.existsSync(planDir) && !fs.existsSync(path.join(planDir, 'phases'))) {
|
|
328
|
-
const v = verifyBriefOnly(root, SLUG);
|
|
329
|
-
if (!v.ok) die(`brief-only work item refused: ${v.reason}; use /plan or /ship-loop`);
|
|
330
|
-
}
|
|
119
|
+
if (!cmd) die('usage: somi-loop.mjs <run-start|init|resume|pass|check-diff|review-scope|record-dispatch|record-pass|finish|stats> --slug <slug> …, or step <name> …');
|
|
120
|
+
if (!args.slug) die('--slug is required');
|
|
121
|
+
if (!Object.hasOwn(COMMANDS, cmd)) die(`unknown subcommand: ${cut(cmd, 40)}`);
|
|
331
122
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
let maxPasses;
|
|
338
|
-
let diffCap;
|
|
339
|
-
let sevFloor;
|
|
340
|
-
if (LOOP === 'plan') {
|
|
341
|
-
maxPasses = resolveInitCap(config, 'plan', 'max_passes', ARG_MAX_PASSES);
|
|
342
|
-
sevFloor = firstDefined(ARG_SEVERITY, process.env.SOMI_PLAN_LOOP_SEVERITY_FLOOR, configVal(config, ['plan_loop', 'severity_floor']));
|
|
343
|
-
diffCap = 0; // plan loops have no diff cap
|
|
344
|
-
} else {
|
|
345
|
-
maxPasses = resolveInitCap(config, 'code', 'max_passes', ARG_MAX_PASSES);
|
|
346
|
-
sevFloor = firstDefined(ARG_SEVERITY, process.env.SOMI_CODE_LOOP_SEVERITY_FLOOR, configVal(config, ['code_loop', 'severity_floor']));
|
|
347
|
-
diffCap = resolveInitCap(config, 'code', 'diff_cap_lines', ARG_DIFF_CAP);
|
|
348
|
-
}
|
|
349
|
-
sevFloor = firstDefined(sevFloor, 'Major');
|
|
350
|
-
|
|
351
|
-
let baseline;
|
|
352
|
-
try {
|
|
353
|
-
baseline = execFileSync('git', ['-C', root, 'rev-parse', 'HEAD'], { encoding: 'utf8' }).trim();
|
|
354
|
-
} catch {
|
|
355
|
-
die('cannot resolve HEAD');
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const filesJson = FILES.split(' ').filter((s) => s.length > 0);
|
|
359
|
-
|
|
360
|
-
const state = {
|
|
361
|
-
slug: SLUG,
|
|
362
|
-
loop: LOOP,
|
|
363
|
-
iteration: ITERATION,
|
|
364
|
-
baseline_sha: baseline,
|
|
365
|
-
started: nowIso(),
|
|
366
|
-
status: 'running',
|
|
367
|
-
caps: { max_passes: maxPasses, diff_cap_lines: diffCap, severity_floor: sevFloor },
|
|
368
|
-
iteration_files: filesJson,
|
|
369
|
-
pass: 0,
|
|
370
|
-
history: [],
|
|
371
|
-
};
|
|
372
|
-
if (LOOP === 'code') state.review_min = reviewMinFor(root, SLUG) ?? undefined; // undefined: not serialised
|
|
373
|
-
saveState(state);
|
|
374
|
-
console.log(JSON.stringify({ baseline_sha: state.baseline_sha, caps: state.caps, state_file: SF, ...(state.review_min ? { review_min: state.review_min } : {}) }));
|
|
375
|
-
break;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
case 'resume': {
|
|
379
|
-
requireState();
|
|
380
|
-
console.log(JSON.stringify(loadState()));
|
|
381
|
-
break;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
case 'pass': {
|
|
385
|
-
requireState();
|
|
386
|
-
const state = loadState();
|
|
387
|
-
const { resolved: max, override } = reresolveCap(state, 'max_passes', ARG_MAX_PASSES);
|
|
388
|
-
if (override) {
|
|
389
|
-
applyCapOverride(state, override);
|
|
390
|
-
saveState(state); // persist the raise even if the gate below still fails
|
|
391
|
-
}
|
|
392
|
-
const cur = state.pass;
|
|
393
|
-
if (cur + 1 > max) {
|
|
394
|
-
fail(2, `max-passes-exceeded: pass ${cur + 1} > cap ${max}`);
|
|
395
|
-
}
|
|
396
|
-
const run = checkRun(RUN_ROOT, SLUG);
|
|
397
|
-
if (run?.over) fail(4, `time-budget-exceeded: ${run.elapsed_minutes} of ${run.max_minutes} minutes used by this run`);
|
|
398
|
-
state.pass = cur + 1;
|
|
399
|
-
saveState(state);
|
|
400
|
-
console.log(JSON.stringify({ pass: state.pass, max_passes: max, ...(state.review_min ? { review_min: state.review_min } : {}) }));
|
|
401
|
-
break;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
case 'check-diff': {
|
|
405
|
-
requireState();
|
|
406
|
-
const state = loadState();
|
|
407
|
-
const { resolved: cap, override } = reresolveCap(state, 'diff_cap_lines', ARG_DIFF_CAP);
|
|
408
|
-
if (override) {
|
|
409
|
-
applyCapOverride(state, override);
|
|
410
|
-
saveState(state); // persist the raise even if the gate below still fails
|
|
411
|
-
}
|
|
412
|
-
const { total, weighted, outOfScope } = computeDiff(root, state.baseline_sha, state.iteration_files);
|
|
413
|
-
console.log(JSON.stringify({ diff_lines: total, weighted_lines: weighted, cap, out_of_scope: outOfScope }));
|
|
414
|
-
if (cap > 0 && weighted > cap) {
|
|
415
|
-
fail(3, `diff-cap-exceeded: weighted ${weighted} > cap ${cap} (out-of-scope counts double)`);
|
|
416
|
-
}
|
|
417
|
-
break;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
case 'review-scope': {
|
|
421
|
-
requireState();
|
|
422
|
-
const state = loadState();
|
|
423
|
-
const setting = firstDefined(process.env.SOMI_CODE_LOOP_REVIEW_SCOPE, configVal(readConfig(root), ['code_loop', 'review_scope']), 'delta');
|
|
424
|
-
if (!REVIEW_SCOPES.includes(setting)) die(`review scope must be one of ${REVIEW_SCOPES.join('|')}, got ${JSON.stringify(setting)}`);
|
|
425
|
-
console.log(JSON.stringify({ ...reviewScope(root, state, setting), ...(state.review_min ? { review_min: state.review_min } : {}) }));
|
|
426
|
-
break;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
case 'record-dispatch': {
|
|
430
|
-
requireState();
|
|
431
|
-
if (!AGENT) die('record-dispatch requires --agent');
|
|
432
|
-
const state = loadState();
|
|
433
|
-
const installRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
434
|
-
const d = dispatchEntry({ root, installRoot, agent: AGENT, host: HOST, pass: state.pass });
|
|
435
|
-
if (!d.ok) fail(d.code, `${PROG}: ${d.message}`);
|
|
436
|
-
(state.dispatches ??= []).push(d.entry);
|
|
437
|
-
saveState(state);
|
|
438
|
-
console.log(JSON.stringify(d.entry));
|
|
439
|
-
break;
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
case 'record-pass': {
|
|
443
|
-
requireState();
|
|
444
|
-
if (!VERDICT) die('record-pass requires --verdict');
|
|
445
|
-
const state = loadState();
|
|
446
|
-
const { total } = computeDiff(root, state.baseline_sha, state.iteration_files);
|
|
447
|
-
const entry = {
|
|
448
|
-
pass: state.pass,
|
|
449
|
-
verdict: VERDICT,
|
|
450
|
-
blockers: Number(BLOCKERS),
|
|
451
|
-
majors: Number(MAJORS),
|
|
452
|
-
diff_lines: total,
|
|
453
|
-
tree: snapshotTree(root) ?? undefined, // the reviewed tree: the next pass's delta starts here
|
|
454
|
-
at: nowIso(),
|
|
455
|
-
};
|
|
456
|
-
state.history.push(entry);
|
|
457
|
-
saveState(state);
|
|
458
|
-
console.log(JSON.stringify(state.history[state.history.length - 1]));
|
|
459
|
-
break;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
case 'finish': {
|
|
463
|
-
requireState();
|
|
464
|
-
if (!STATUS) die('finish requires --status');
|
|
465
|
-
const state = loadState();
|
|
466
|
-
state.status = STATUS;
|
|
467
|
-
state.finished = nowIso();
|
|
468
|
-
saveState(state);
|
|
469
|
-
console.log(JSON.stringify({
|
|
470
|
-
status: state.status,
|
|
471
|
-
pass: state.pass,
|
|
472
|
-
history: state.history.length,
|
|
473
|
-
cap_overrides: state.cap_overrides || [],
|
|
474
|
-
...(state.dispatches ? { dispatches: state.dispatches } : {}),
|
|
475
|
-
}));
|
|
476
|
-
break;
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
case 'stats': {
|
|
480
|
-
requireState();
|
|
481
|
-
console.log(JSON.stringify(loadState(), null, 2));
|
|
482
|
-
break;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
default:
|
|
486
|
-
die(`unknown subcommand: ${CMD}`);
|
|
487
|
-
}
|
|
123
|
+
const root = projectRoot();
|
|
124
|
+
const stateDir = process.env.SOMI_LOOP_STATE_DIR || path.join(root, '.somi', 'somi-state', 'loop');
|
|
125
|
+
const installRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
126
|
+
const { handler, space } = COMMANDS[cmd];
|
|
127
|
+
deliver(handler({ root, env: process.env, stateDir, installRoot }, args), space);
|
|
488
128
|
}
|
|
489
129
|
|
|
490
130
|
try {
|
|
491
131
|
main();
|
|
492
132
|
} catch (e) {
|
|
493
|
-
if (e instanceof
|
|
133
|
+
if (e instanceof LoopExit) {
|
|
134
|
+
process.stderr.write(`${e.message}\n`);
|
|
494
135
|
process.exitCode = e.code;
|
|
495
136
|
} else {
|
|
496
137
|
throw e;
|