@skathio/somi 4.0.1 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.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 +226 -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 +278 -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 +301 -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 +77 -447
- 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 +371 -0
- package/tests/scripts/lib/loop-step-cli-tests.mjs +141 -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,82 +1,32 @@
|
|
|
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, --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
|
|
|
49
|
-
import fs from 'node:fs';
|
|
50
17
|
import path from 'node:path';
|
|
51
18
|
import { execFileSync } from 'node:child_process';
|
|
52
19
|
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
|
-
|
|
20
|
+
import { LoopExit, PROG, usageError } from './lib/loop-exit.mjs';
|
|
21
|
+
import { runStep } from './lib/loop-step.mjs';
|
|
22
|
+
import { base, clean, cut, stepLog } from './lib/loop-step-kit.mjs';
|
|
23
|
+
import { checkDiff, finish, init, pass, recordDispatch, recordPass, resume, reviewScope, runStart } from './lib/loop-engine.mjs';
|
|
24
|
+
|
|
25
|
+
// Unwinding with a LoopExit reaches the top-level handler, which writes its message to stderr and sets
|
|
26
|
+
// process.exitCode, letting Node exit naturally — avoids process.exit()'s risk of truncating buffered
|
|
27
|
+
// stdout/stderr writes on a pipe.
|
|
72
28
|
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);
|
|
29
|
+
throw usageError(msg);
|
|
80
30
|
}
|
|
81
31
|
|
|
82
32
|
function requireGit() {
|
|
@@ -93,404 +43,84 @@ function projectRoot() {
|
|
|
93
43
|
return b;
|
|
94
44
|
}
|
|
95
45
|
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
} catch {
|
|
102
|
-
return {};
|
|
103
|
-
}
|
|
46
|
+
// A handler's result: its one JSON object on stdout, its message on stderr, its code as the exit code.
|
|
47
|
+
function deliver(result, space) {
|
|
48
|
+
if (result.out !== undefined) console.log(JSON.stringify(result.out, null, space));
|
|
49
|
+
if (result.message) process.stderr.write(`${result.message}\n`);
|
|
50
|
+
process.exitCode = result.code;
|
|
104
51
|
}
|
|
105
52
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
return cur === null ? undefined : cur;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// Mirrors bash's `${a:-${b:-$c}}` — first arg that is neither unset nor an
|
|
116
|
-
// empty string wins (a numeric 0 counts as "set", matching bash treating the
|
|
117
|
-
// non-empty string "0" as set).
|
|
118
|
-
function firstDefined(...vals) {
|
|
119
|
-
for (const v of vals) {
|
|
120
|
-
if (v === undefined || v === null) continue;
|
|
121
|
-
if (typeof v === 'string' && v === '') continue;
|
|
122
|
-
return v;
|
|
123
|
-
}
|
|
124
|
-
return undefined;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function nowIso() {
|
|
128
|
-
// `date -u +%Y-%m-%dT%H:%M:%SZ` — no milliseconds.
|
|
129
|
-
return new Date().toISOString().replace(/\.\d{3}Z$/, 'Z');
|
|
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' },
|
|
53
|
+
// subcommand -> handler, and the JSON indent where it is not the compact default (`stats` is read by people).
|
|
54
|
+
const COMMANDS = {
|
|
55
|
+
'run-start': { handler: runStart }, init: { handler: init }, resume: { handler: resume }, stats: { handler: resume, space: 2 },
|
|
56
|
+
pass: { handler: pass }, 'check-diff': { handler: checkDiff }, 'review-scope': { handler: reviewScope },
|
|
57
|
+
'record-dispatch': { handler: recordDispatch }, 'record-pass': { handler: recordPass }, finish: { handler: finish },
|
|
166
58
|
};
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
const n = Number(raw);
|
|
177
|
-
if (!Number.isInteger(n) || n < 0) {
|
|
178
|
-
die(`${label}: expected a non-negative integer, got "${raw}"`);
|
|
59
|
+
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'];
|
|
60
|
+
// 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).
|
|
61
|
+
function stepArgs(argv) {
|
|
62
|
+
const args = { step: argv[0] };
|
|
63
|
+
for (let i = 1; i < argv.length; i += 2) {
|
|
64
|
+
const key = argv[i].replace(/^--/, '');
|
|
65
|
+
if (!argv[i].startsWith('--') || !STEP_FLAGS.includes(key) || argv[i + 1] === undefined) die(`unknown argument or missing value: ${cut(argv[i], 40)}`);
|
|
66
|
+
if (Object.hasOwn(args, key.replaceAll('-', '_'))) die(`repeated argument: ${cut(argv[i], 40)}`);
|
|
67
|
+
args[key.replaceAll('-', '_')] = argv[i + 1];
|
|
179
68
|
}
|
|
180
|
-
|
|
69
|
+
for (const k of ['findings', 'fixed']) if (args[k] !== undefined) { try { args[k] = JSON.parse(args[k]); } catch { die(`--${k} expects JSON text`); } }
|
|
70
|
+
if (args.reviewers !== undefined) args.reviewers = args.reviewers.split(',');
|
|
71
|
+
return args;
|
|
181
72
|
}
|
|
182
73
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const envVar = CAP_ENV[family][field];
|
|
189
|
-
const fromConfig = configVal(config, [CAP_CONFIG_SECTION[family], field]);
|
|
190
|
-
const picked = firstDefined(argVal, process.env[envVar], fromConfig, CAP_DEFAULT[field]);
|
|
191
|
-
return validateCap(picked, field);
|
|
192
|
-
}
|
|
74
|
+
const FLAGS = {
|
|
75
|
+
'--slug': 'slug', '--loop': 'loop', '--iteration': 'iteration', '--files': 'files', '--max-passes': 'maxPasses',
|
|
76
|
+
'--diff-cap': 'diffCap', '--severity-floor': 'severity', '--verdict': 'verdict', '--blockers': 'blockers',
|
|
77
|
+
'--majors': 'majors', '--status': 'status', '--agent': 'agent', '--host': 'host', '--max-minutes': 'maxMinutes',
|
|
78
|
+
};
|
|
193
79
|
|
|
194
|
-
function
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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
|
-
};
|
|
80
|
+
function step(argv) {
|
|
81
|
+
const root = projectRoot();
|
|
82
|
+
const explicit = Boolean(process.env.SOMI_LOOP_STATE_DIR);
|
|
83
|
+
const stateDir = process.env.SOMI_LOOP_STATE_DIR || path.join(root, '.somi', 'somi-state', 'loop');
|
|
84
|
+
const installRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
85
|
+
let resp;
|
|
86
|
+
try {
|
|
87
|
+
resp = runStep({ root, env: process.env, stateDir, installRoot, via: 'cli', log: stepLog(root, stateDir, explicit) }, stepArgs(argv));
|
|
88
|
+
} catch (e) { // a flag the parser refuses answers like any other refusal of a step: the response on stdout
|
|
89
|
+
if (!(e instanceof LoopExit)) throw e;
|
|
90
|
+
resp = base(clean(argv[0] ?? '', 20) || null, { ok: false, exit: e.code, error: clean(e.message.replace(`${PROG}: `, ''), 300) });
|
|
216
91
|
}
|
|
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);
|
|
92
|
+
deliver({ out: resp, code: resp.exit }, undefined);
|
|
227
93
|
}
|
|
228
94
|
|
|
229
95
|
function main() {
|
|
230
96
|
requireGit();
|
|
231
97
|
|
|
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
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
if (!CMD) die('usage: somi-loop.mjs <run-start|init|resume|pass|check-diff|review-scope|record-dispatch|record-pass|finish|stats> --slug <slug> …');
|
|
279
|
-
if (!SLUG) die('--slug is required');
|
|
280
|
-
|
|
281
|
-
function stateFile() {
|
|
282
|
-
let name = SLUG;
|
|
283
|
-
if (ITERATION) name = `${SLUG}.${ITERATION}`;
|
|
284
|
-
return path.join(STATE_DIR, `${name}.json`);
|
|
98
|
+
const cmd = process.argv[2] || '';
|
|
99
|
+
if (cmd === 'step') return step(process.argv.slice(3));
|
|
100
|
+
const args = { slug: '', loop: 'code', iteration: '', files: '', verdict: '', blockers: '0', majors: '0', status: '', agent: '', host: undefined, maxMinutes: '', force: false, maxPasses: '', diffCap: '', severity: '' };
|
|
101
|
+
for (let rest = process.argv.slice(3); rest.length > 0;) {
|
|
102
|
+
if (rest[0] === '--force') { args.force = true; rest = rest.slice(1); continue; }
|
|
103
|
+
if (!Object.hasOwn(FLAGS, rest[0])) die(`unknown argument: ${cut(rest[0], 40)}`);
|
|
104
|
+
args[FLAGS[rest[0]]] = rest[1];
|
|
105
|
+
rest = rest.slice(2);
|
|
285
106
|
}
|
|
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
107
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
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
|
-
}
|
|
108
|
+
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> …');
|
|
109
|
+
if (!args.slug) die('--slug is required');
|
|
110
|
+
if (!Object.hasOwn(COMMANDS, cmd)) die(`unknown subcommand: ${cut(cmd, 40)}`);
|
|
323
111
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
if (!v.ok) die(`brief-only work item refused: ${v.reason}; use /plan or /ship-loop`);
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
// Cap resolution: CLI > env > config > default (per loop type), through the
|
|
333
|
-
// same CAP_ENV / CAP_CONFIG_SECTION / CAP_DEFAULT tables reresolveCap() uses,
|
|
334
|
-
// and validated the same way -- a malformed value dies here rather than
|
|
335
|
-
// silently resolving to a fail-open NaN/null.
|
|
336
|
-
const config = readConfig(root);
|
|
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
|
-
}
|
|
112
|
+
const root = projectRoot();
|
|
113
|
+
const stateDir = process.env.SOMI_LOOP_STATE_DIR || path.join(root, '.somi', 'somi-state', 'loop');
|
|
114
|
+
const installRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
115
|
+
const { handler, space } = COMMANDS[cmd];
|
|
116
|
+
deliver(handler({ root, env: process.env, stateDir, installRoot }, args), space);
|
|
488
117
|
}
|
|
489
118
|
|
|
490
119
|
try {
|
|
491
120
|
main();
|
|
492
121
|
} catch (e) {
|
|
493
|
-
if (e instanceof
|
|
122
|
+
if (e instanceof LoopExit) {
|
|
123
|
+
process.stderr.write(`${e.message}\n`);
|
|
494
124
|
process.exitCode = e.code;
|
|
495
125
|
} else {
|
|
496
126
|
throw e;
|