@skathio/somi 3.1.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/.claude/settings.json +13 -0
  2. package/.claude-plugin/marketplace.json +2 -2
  3. package/.claude-plugin/plugin.json +1 -1
  4. package/.copilot-extension/extension.json +1 -1
  5. package/.copilot-extension/marketplace.json +2 -2
  6. package/README.md +28 -23
  7. package/VERSION +1 -1
  8. package/agents/{architecture-reviewer.md → somi-architecture-reviewer.md} +10 -11
  9. package/agents/{atlas.md → somi-atlas.md} +3 -5
  10. package/agents/{coder.md → somi-coder.md} +26 -23
  11. package/agents/{designer.md → somi-designer.md} +9 -11
  12. package/agents/{discovery-analyst.md → somi-discovery-analyst.md} +7 -9
  13. package/agents/{impact.md → somi-impact.md} +6 -8
  14. package/agents/{incident.md → somi-incident.md} +2 -4
  15. package/agents/{plan-reviewer.md → somi-plan-reviewer.md} +9 -11
  16. package/agents/{planner.md → somi-planner.md} +16 -24
  17. package/agents/{pr.md → somi-pr.md} +2 -4
  18. package/agents/{refactor-designer.md → somi-refactor-designer.md} +14 -15
  19. package/agents/{refactorer.md → somi-refactorer.md} +11 -12
  20. package/agents/{reviewer.md → somi-reviewer.md} +8 -7
  21. package/agents/{sdlc-reviewer.md → somi-sdlc-reviewer.md} +8 -10
  22. package/agents/{security-reviewer.md → somi-security-reviewer.md} +8 -8
  23. package/agents/{test-strategist.md → somi-test-strategist.md} +4 -6
  24. package/agents/somi.md +25 -12
  25. package/commands/adopt.md +9 -10
  26. package/commands/architecture-review.md +4 -5
  27. package/commands/atlas.md +3 -3
  28. package/commands/code-loop.md +38 -14
  29. package/commands/code-parallel.md +4 -4
  30. package/commands/code.md +24 -15
  31. package/commands/debug.md +8 -9
  32. package/commands/design.md +14 -15
  33. package/commands/discover.md +11 -12
  34. package/commands/impact.md +2 -2
  35. package/commands/incident.md +3 -4
  36. package/commands/plan-loop.md +16 -12
  37. package/commands/plan.md +15 -16
  38. package/commands/pr.md +2 -2
  39. package/commands/refactor-design.md +10 -11
  40. package/commands/refactor.md +7 -8
  41. package/commands/release-readiness.md +6 -7
  42. package/commands/review-panel.md +24 -23
  43. package/commands/review.md +14 -14
  44. package/commands/security-review.md +8 -7
  45. package/commands/ship-loop.md +107 -28
  46. package/commands/ship.md +6 -6
  47. package/commands/somi-config.md +131 -0
  48. package/commands/test-strategy.md +3 -4
  49. package/commands/upgrade.md +4 -5
  50. package/examples/code-review-example.md +1 -1
  51. package/examples/discovery-example.md +1 -1
  52. package/examples/feature-plan-example.md +2 -2
  53. package/hooks/hooks.json +14 -1
  54. package/hooks/lib/common.mjs +64 -1
  55. package/hooks/post-tool/audit-log.mjs +3 -2
  56. package/hooks/pre-tool/block-secret-writes.mjs +12 -11
  57. package/hooks/pre-tool/guard-cost-lowering.mjs +177 -0
  58. package/hooks/pre-tool/guard-protected-paths.mjs +13 -3
  59. package/hooks/session-start/detect-repo-instructions.mjs +27 -2
  60. package/hooks/session-start/ensure-default-config.mjs +121 -0
  61. package/package.json +1 -1
  62. package/rules/00-priorities.md +1 -1
  63. package/rules/30-security-owasp.md +1 -1
  64. package/rules/50-collaboration.md +9 -6
  65. package/rules/CLAUDE.md +11 -10
  66. package/scripts/lib/agent-info.mjs +85 -0
  67. package/scripts/lib/agent-registry.mjs +92 -0
  68. package/scripts/lib/cost-ceiling.mjs +175 -171
  69. package/scripts/lib/cost-config-edit.mjs +102 -0
  70. package/scripts/lib/cost-config.mjs +64 -0
  71. package/scripts/lib/cost-defaults.mjs +51 -0
  72. package/scripts/lib/cost-lowering.mjs +89 -0
  73. package/scripts/lib/cost-model.mjs +37 -22
  74. package/scripts/lib/dispatch-resolver.mjs +59 -50
  75. package/scripts/lib/loop-diff.mjs +96 -0
  76. package/scripts/lib/loop-dispatch.mjs +19 -0
  77. package/scripts/lib/loop-review.mjs +52 -0
  78. package/scripts/lib/run-budget.mjs +28 -0
  79. package/scripts/lib/size-tier.mjs +184 -0
  80. package/scripts/lib/tier-record.mjs +82 -0
  81. package/scripts/size-budget-overrides.json +7 -15
  82. package/scripts/somi-config.mjs +358 -0
  83. package/scripts/somi-dispatch.mjs +1 -1
  84. package/scripts/somi-loop.mjs +71 -96
  85. package/scripts/somi-mcp.mjs +135 -14
  86. package/scripts/somi-tier.mjs +393 -0
  87. package/scripts/validate.sh +37 -215
  88. package/skills/api-design/SKILL.md +1 -1
  89. package/skills/clean-code/SKILL.md +1 -1
  90. package/skills/market-research/SKILL.md +2 -2
  91. package/skills/owasp-defense/SKILL.md +2 -2
  92. package/skills/plan-review/SKILL.md +5 -1
  93. package/skills/reasoning-craft/SKILL.md +1 -1
  94. package/skills/requirements-engineering/SKILL.md +3 -3
  95. package/skills/solid-principles/SKILL.md +1 -1
  96. package/skills/somi-dispatch/SKILL.md +104 -109
  97. package/skills/somi-routing/SKILL.md +88 -1
  98. package/skills/testing-playbook/SKILL.md +2 -2
  99. package/skills/threat-modeling/SKILL.md +3 -3
  100. package/templates/BRIEF.md.tmpl +2 -2
  101. package/templates/DESIGN.md.tmpl +1 -1
  102. package/templates/DOD.md.tmpl +2 -2
  103. package/templates/PHASE.md.tmpl +2 -2
  104. package/templates/RCA.md.tmpl +1 -1
  105. package/templates/REVIEW.md.tmpl +3 -2
  106. package/templates/SDD.md.tmpl +1 -1
  107. package/templates/SPEC.md.tmpl +1 -1
  108. package/templates/TDD.md.tmpl +1 -1
  109. package/tests/hooks/cases/block-secret-writes.json +130 -17
  110. package/tests/hooks/cases/detect-repo-instructions.json +22 -1
  111. package/tests/hooks/cases/guard-protected-paths.json +175 -25
  112. package/tests/scripts/agent-model-frontmatter.sh +38 -0
  113. package/tests/scripts/agent-names.sh +77 -0
  114. package/tests/scripts/check-frontmatter.sh +1 -1
  115. package/tests/scripts/checkpoint-gate.sh +44 -3
  116. package/tests/scripts/convergence-runner.sh +10 -6
  117. package/tests/scripts/cost-ceiling.sh +127 -215
  118. package/tests/scripts/cost-defaults.sh +128 -0
  119. package/tests/scripts/cost-hardcap.sh +329 -0
  120. package/tests/scripts/cost-lowering.sh +261 -0
  121. package/tests/scripts/cost-model.sh +18 -14
  122. package/tests/scripts/dispatch-guard.sh +7 -7
  123. package/tests/scripts/dispatch-line-gate.sh +61 -0
  124. package/tests/scripts/dispatch-reference-gate.sh +13 -13
  125. package/tests/scripts/ensure-default-config.sh +162 -0
  126. package/tests/scripts/eval-runner.sh +9 -9
  127. package/tests/scripts/evals-packaging.sh +127 -20
  128. package/tests/scripts/fixtures/size-tier-cases.json +87 -0
  129. package/tests/scripts/goldens/loop-state-done.json +1 -1
  130. package/tests/scripts/goldens/somi-loop.json +3 -3
  131. package/tests/scripts/lib/agent-registry-tests.mjs +95 -0
  132. package/tests/scripts/lib/brief-only-guard.mjs +27 -0
  133. package/tests/scripts/lib/checkpoint-gate.mjs +40 -3
  134. package/tests/scripts/lib/dispatch-reference-gate.mjs +5 -5
  135. package/tests/scripts/lib/retirement-gate.mjs +7 -2
  136. package/tests/scripts/lib/ship-loop-sizing-guard.mjs +52 -0
  137. package/tests/scripts/lib/size-tier-drift.mjs +74 -0
  138. package/tests/scripts/lib/somi-mcp-config-tests.mjs +92 -0
  139. package/tests/scripts/lib/somi-mcp-tests.mjs +66 -28
  140. package/tests/scripts/lib/somi-mcp-tier-tests.mjs +64 -0
  141. package/tests/scripts/retirement-gate.sh +19 -0
  142. package/tests/scripts/review-panel-seats.sh +2 -2
  143. package/tests/scripts/run.sh +95 -0
  144. package/tests/scripts/size-tier.sh +562 -0
  145. package/tests/scripts/somi-agent-dispatch-contract.sh +20 -11
  146. package/tests/scripts/somi-config.sh +281 -0
  147. package/tests/scripts/somi-dispatch.sh +150 -98
  148. package/tests/scripts/somi-mcp.sh +2 -0
@@ -0,0 +1,52 @@
1
+ // loop-review.mjs -- what /code-loop's reviewer must see (which lens, which scope), decided from recorded state
2
+ // rather than from the reviewer's reading of evidence. Shared by scripts/somi-loop.mjs; no output of its own.
3
+
4
+ import fs from 'node:fs';
5
+ import os from 'node:os';
6
+ import path from 'node:path';
7
+ import { execFileSync } from 'node:child_process';
8
+ import { readRecord } from './tier-record.mjs';
9
+
10
+ // The sizing record decides the review lens: a work item that may handle untrusted input or touch auth, crypto,
11
+ // secrets or PII (security_lens in scripts/somi-tier.mjs's record) is reviewed with the security lens on every
12
+ // pass. No record, or a record without the flag, asks for nothing beyond the loop's configured mode.
13
+ export function reviewMinFor(root, slug) {
14
+ return readRecord(root, slug)?.security_lens === true ? 'security' : null;
15
+ }
16
+
17
+ // The working tree as a git tree object: tracked and untracked files, .somi/ and .claude/ excluded, the same
18
+ // surface the diff cap measures. Built through a throwaway copy of the index so the caller's index is never
19
+ // touched (a copy rather than an empty index, so unchanged files are not re-hashed). -> tree id, or null.
20
+ export function snapshotTree(root) {
21
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'somi-tree-'));
22
+ const index = path.join(dir, 'index');
23
+ try {
24
+ const real = execFileSync('git', ['-C', root, 'rev-parse', '--path-format=absolute', '--git-path', 'index'], { encoding: 'utf8' }).trim();
25
+ if (fs.existsSync(real)) fs.copyFileSync(real, index);
26
+ const env = { ...process.env, GIT_INDEX_FILE: index };
27
+ execFileSync('git', ['-C', root, 'add', '-A', '--', '.', ':(exclude).somi', ':(exclude).claude'], { env, stdio: 'ignore' });
28
+ return execFileSync('git', ['-C', root, 'write-tree'], { env, encoding: 'utf8' }).trim();
29
+ } catch {
30
+ return null;
31
+ } finally {
32
+ fs.rmSync(dir, { recursive: true, force: true });
33
+ }
34
+ }
35
+
36
+ export const REVIEW_SCOPES = Object.freeze(['delta', 'full']);
37
+
38
+ // What the generalist reviewer reads on this pass. Pass 1, the last allowed pass, a `full` setting, or a missing
39
+ // snapshot of the previous review all mean the whole iteration; otherwise only what changed since the last
40
+ // reviewed tree, plus the open findings it must re-check. The security lens is not narrowed by this.
41
+ export function reviewScope(root, state, setting) {
42
+ const full = (why) => ({ scope: 'full', why });
43
+ if (setting === 'full') return full('setting');
44
+ if (state.pass <= 1) return full('first-pass');
45
+ if (state.pass >= state.caps.max_passes) return full('last-pass');
46
+ const prev = state.history.at(-1);
47
+ if (!prev?.tree) return full('no-snapshot');
48
+ const to = snapshotTree(root);
49
+ if (!to) return full('no-snapshot');
50
+ const names = execFileSync('git', ['-C', root, 'diff', '--name-only', prev.tree, to], { encoding: 'utf8' });
51
+ return { scope: 'delta', since_pass: prev.pass, from: prev.tree, to, files: names.split('\n').filter(Boolean) };
52
+ }
@@ -0,0 +1,28 @@
1
+ // run-budget.mjs -- a /ship-loop run's wall-time budget. The run records its start once (run-start); every later
2
+ // `pass` of any plan or code loop on the same slug checks it, so the stop does not depend on an orchestrator
3
+ // remembering to look at the clock. State: .somi/somi-state/run/<slug>.json { started, max_minutes }.
4
+
5
+ import fs from 'node:fs';
6
+ import path from 'node:path';
7
+
8
+ const file = (stateRoot, slug) => path.join(stateRoot, 'run', `${slug}.json`);
9
+
10
+ // -> the recorded run; an existing one is kept unless `force` (a resumed run keeps its clock).
11
+ export function startRun(stateRoot, slug, maxMinutes, { force = false, now = Date.now() } = {}) {
12
+ const f = file(stateRoot, slug);
13
+ if (!force && fs.existsSync(f)) return { ...JSON.parse(fs.readFileSync(f, 'utf8')), resumed: true };
14
+ const run = { started: new Date(now).toISOString(), max_minutes: maxMinutes };
15
+ fs.mkdirSync(path.dirname(f), { recursive: true });
16
+ fs.writeFileSync(f, JSON.stringify(run));
17
+ return run;
18
+ }
19
+
20
+ // -> null when the slug has no run or no time budget, else { elapsed_minutes, max_minutes, over }.
21
+ export function checkRun(stateRoot, slug, now = Date.now()) {
22
+ let run;
23
+ try { run = JSON.parse(fs.readFileSync(file(stateRoot, slug), 'utf8')); } catch { return null; }
24
+ if (!Number.isSafeInteger(run?.max_minutes) || run.max_minutes < 1) return null;
25
+ const elapsed = Math.floor((now - Date.parse(run.started)) / 60000);
26
+ if (!Number.isFinite(elapsed)) return { elapsed_minutes: null, max_minutes: run.max_minutes, over: true }; // unreadable start: fail closed
27
+ return { elapsed_minutes: elapsed, max_minutes: run.max_minutes, over: elapsed >= run.max_minutes };
28
+ }
@@ -0,0 +1,184 @@
1
+ // size-tier.mjs — decideTier(signals): how much pipeline does this request deserve?
2
+ //
3
+ // The model extracts observable signals (each with an evidence string); this pure function decides
4
+ // the tier, so the same signals always give the same answer and the reasons come from signal ids,
5
+ // never from model prose. No I/O, no clock, no imports. Unsure goes up: a signal that is unknown,
6
+ // missing, lacks evidence, is malformed, or is contradicted by the counts counts as its worst case
7
+ // and is listed in `failed`.
8
+ //
9
+ // Tiers: trivial (-> /code), small (-> inline brief -> /code-loop), full (-> today's flow).
10
+ //
11
+ // Signals T1..T5 are the five numbered signals of the trivial threshold, in the same order. Their
12
+ // meaning, and how to extract every other signal (what the count signals measure, what
13
+ // hard_surface covers), lives only in skills/somi-routing/SKILL.md, section "Size tiers"; this
14
+ // file holds ids and thresholds, never the wording.
15
+ //
16
+ // Input: { <id>: { value, evidence } }; evidence is 1..MAX_EVIDENCE_CHARS characters with no
17
+ // control characters. Output, always the same keys:
18
+ // tier 'trivial' | 'small' | 'full'
19
+ // floor 'full' | null — 'full' whenever sensitive_core is not a proven false, whichever rule fired
20
+ // first (a doubt about auth, crypto, secrets or PII must survive an existing-work-item
21
+ // match); no override may go below it
22
+ // securityLens true unless T3 is a proven true and sensitive_core a proven false: the change may
23
+ // handle untrusted input or touch the core, so the security lens reviews it at any tier
24
+ // failed ids that were read as their worst case = unknown + invalid, in SIGNAL_IDS order
25
+ // unknown ids the extractor explicitly marked null / 'unknown'
26
+ // invalid ids that were missing, malformed, over the evidence cap, or contradicted by the counts
27
+ // reasons signal ids that fired the deciding rule (`<id>_unknown` for an unknown work-item or
28
+ // brief signal); for small, the signals that kept it from trivial; for trivial, empty
29
+ // basis { id: evidence } for T3 and sensitive_core and, when the tier is below full, every signal read cleanly,
30
+ // so a human can see why security and size were ruled out
31
+ //
32
+ // Hostile input: the function runs no user code of its own making. It reads own data properties
33
+ // only (an accessor is never invoked) once each into a plain copy. That guarantee covers
34
+ // JSON-derived input, which is all a caller should pass (the CLI hands over JSON.parse output
35
+ // only). A Proxy can still fire its own traps, which the function survives (any throw becomes the
36
+ // refusal result), but it cannot claim more than plain JSON could.
37
+
38
+ export const MAX_MODULES = 2;
39
+ export const MAX_FILES = 6;
40
+ // Provisional: production lines only, tests not counted. To be revisited once the code loop's
41
+ // diff cap no longer counts test lines.
42
+ export const MAX_DIFF_LINES = 100;
43
+ export const TRIVIAL_MAX_FILES = 1;
44
+ export const TRIVIAL_MAX_MODULES = 1;
45
+ export const TRIVIAL_MAX_DIFF_LINES = 50;
46
+ export const MAX_EVIDENCE_CHARS = 300;
47
+
48
+ export const SIGNAL_IDS = Object.freeze([
49
+ 'T1', 'T2', 'T3', 'T4', 'T5', 'sensitive_core',
50
+ 'hard_surface', 'design_open', 'open_questions', 'acceptance_testable',
51
+ 'modules_touched', 'est_files', 'est_diff_lines',
52
+ 'existing_work_item', 'brief_exists',
53
+ ]);
54
+
55
+ const MIN_COUNT = { open_questions: 0, modules_touched: 1, est_files: 1, est_diff_lines: 1 };
56
+ const TRIVIAL_IDS = SIGNAL_IDS.slice(0, 5);
57
+ // C0/C1 controls plus the invisible and direction-changing characters that can disguise text in a terminal.
58
+ const CONTROL_CHARS = /[\u0000-\u001f\u007f-\u009f\u061c\u200b-\u200f\u2028-\u202e\u2060-\u2069\ufeff]/;
59
+
60
+ // What an unreadable signal is taken to be: the answer that sends the tier up.
61
+ const WORST = Object.freeze({
62
+ T1: false, T2: false, T3: false, T4: false, T5: false, sensitive_core: true,
63
+ hard_surface: true, design_open: true, acceptance_testable: false,
64
+ existing_work_item: true, brief_exists: true,
65
+ open_questions: Infinity, modules_touched: Infinity, est_files: Infinity, est_diff_lines: Infinity,
66
+ });
67
+
68
+ const REFUSED = () => ({
69
+ tier: 'full', floor: 'full', securityLens: true, failed: [...SIGNAL_IDS], unknown: [], invalid: [...SIGNAL_IDS],
70
+ reasons: ['signals_unreadable'], basis: {},
71
+ });
72
+
73
+ function plain(o) {
74
+ if (o === null || typeof o !== 'object' || Array.isArray(o)) return false;
75
+ const p = Object.getPrototypeOf(o);
76
+ return p === Object.prototype || p === null;
77
+ }
78
+
79
+ // Own data property only: an accessor is never invoked, an inherited key is never seen.
80
+ function own(o, key) {
81
+ const d = Object.getOwnPropertyDescriptor(o, key);
82
+ return d && 'value' in d ? d.value : undefined;
83
+ }
84
+
85
+ const validEvidence = (e) => typeof e === 'string' && e.trim() !== ''
86
+ && e.length <= MAX_EVIDENCE_CHARS && !CONTROL_CHARS.test(e);
87
+
88
+ // -> { state: 'ok', value, evidence } | { state: 'unknown' } | { state: 'invalid' }
89
+ function readEntry(entry, id) {
90
+ if (!plain(entry)) return { state: 'invalid' };
91
+ const value = own(entry, 'value');
92
+ const evidence = own(entry, 'evidence');
93
+ if (!validEvidence(evidence)) return { state: 'invalid' };
94
+ if (value === null || value === 'unknown') return { state: 'unknown' };
95
+ const isCount = id in MIN_COUNT;
96
+ const good = isCount ? Number.isSafeInteger(value) && value >= MIN_COUNT[id] : typeof value === 'boolean';
97
+ return good ? { state: 'ok', value, evidence } : { state: 'invalid' };
98
+ }
99
+
100
+ // One pass, each field read once into a plain copy; everything after works on the copy.
101
+ function readSignals(input) {
102
+ if (!plain(input)) return null;
103
+ const v = {};
104
+ const read = {};
105
+ for (const id of SIGNAL_IDS) read[id] = readEntry(own(input, id), id);
106
+ // A footprint claim the counts contradict is not believed.
107
+ if (read.T1.state === 'ok' && read.T1.value) {
108
+ const files = read.est_files, mods = read.modules_touched;
109
+ if ((files.state === 'ok' && files.value > TRIVIAL_MAX_FILES) || (mods.state === 'ok' && mods.value > TRIVIAL_MAX_MODULES)) {
110
+ read.T1 = { state: 'invalid' };
111
+ }
112
+ }
113
+ // "Not security-sensitive" contradicts "touches auth, crypto, secrets or PII": the T3 claim is not believed.
114
+ if (read.T3.state === 'ok' && read.T3.value && read.sensitive_core.state === 'ok' && read.sensitive_core.value) {
115
+ read.T3 = { state: 'invalid' };
116
+ }
117
+ for (const id of SIGNAL_IDS) v[id] = read[id].state === 'ok' ? read[id].value : WORST[id];
118
+ const unknown = SIGNAL_IDS.filter((id) => read[id].state === 'unknown');
119
+ const invalid = SIGNAL_IDS.filter((id) => read[id].state === 'invalid');
120
+ return { v, read, unknown, invalid };
121
+ }
122
+
123
+ // Each check is [signal id, does this value send the tier up?].
124
+ const fired = (v, checks) => checks.filter(([id, up]) => up(v[id])).map(([id]) => id);
125
+
126
+ // First match wins. Order is the contract; each rule names the signals that fired it.
127
+ const RULES = [
128
+ (v) => {
129
+ const hit = fired(v, [['existing_work_item', (x) => x], ['brief_exists', (x) => x]]);
130
+ return hit.length ? { tier: 'full', reasons: hit } : null;
131
+ },
132
+ // Auth, crypto, secrets or PII: a security floor no override can lower. Untrusted input alone (a false T3) is not
133
+ // trivial and gets the security lens, but does not by itself need the planning pipeline.
134
+ (v) => (v.sensitive_core === true ? { tier: 'full', reasons: ['sensitive_core'] } : null),
135
+ (v) => {
136
+ const hit = fired(v, [
137
+ ['hard_surface', (x) => x], ['design_open', (x) => x], ['open_questions', (x) => x > 0],
138
+ ['acceptance_testable', (x) => !x], ['T4', (x) => !x],
139
+ ]);
140
+ return hit.length ? { tier: 'full', reasons: hit } : null;
141
+ },
142
+ (v) => {
143
+ const hit = fired(v, [
144
+ ['modules_touched', (x) => x > MAX_MODULES], ['est_files', (x) => x > MAX_FILES],
145
+ ['est_diff_lines', (x) => x > MAX_DIFF_LINES],
146
+ ]);
147
+ return hit.length ? { tier: 'full', reasons: hit } : null;
148
+ },
149
+ (v) => (TRIVIAL_IDS.every((id) => v[id] === true) && v.est_files <= TRIVIAL_MAX_FILES
150
+ && v.modules_touched <= TRIVIAL_MAX_MODULES && v.est_diff_lines <= TRIVIAL_MAX_DIFF_LINES
151
+ ? { tier: 'trivial', reasons: [] } : null),
152
+ ];
153
+
154
+ const SMALL_BECAUSE = [
155
+ ['est_files', (x) => x > TRIVIAL_MAX_FILES],
156
+ ['modules_touched', (x) => x > TRIVIAL_MAX_MODULES],
157
+ ['est_diff_lines', (x) => x > TRIVIAL_MAX_DIFF_LINES],
158
+ ];
159
+
160
+ export function decideTier(signals, { fullBasis = false } = {}) {
161
+ let r;
162
+ try {
163
+ r = readSignals(signals);
164
+ } catch {
165
+ return REFUSED();
166
+ }
167
+ if (!r) return REFUSED();
168
+ const { v, read, unknown, invalid } = r;
169
+ let decided = null;
170
+ for (const rule of RULES) {
171
+ decided = rule(v);
172
+ if (decided) break;
173
+ }
174
+ if (!decided) {
175
+ decided = { tier: 'small', reasons: [...TRIVIAL_IDS.filter((id) => v[id] !== true), ...fired(v, SMALL_BECAUSE)] };
176
+ }
177
+ const failed = SIGNAL_IDS.filter((id) => read[id].state !== 'ok');
178
+ const reasons = decided.reasons.map((id) => ((id === 'existing_work_item' || id === 'brief_exists') && failed.includes(id) ? `${id}_unknown` : id));
179
+ const basis = {};
180
+ for (const id of SIGNAL_IDS) {
181
+ if (read[id].state === 'ok' && (fullBasis || (id === 'T3' || id === 'sensitive_core' || decided.tier !== 'full'))) basis[id] = read[id].evidence;
182
+ }
183
+ return { tier: decided.tier, floor: v.sensitive_core === false ? null : 'full', securityLens: v.T3 !== true || v.sensitive_core !== false, failed, unknown, invalid, reasons, basis };
184
+ }
@@ -0,0 +1,82 @@
1
+ // tier-record.mjs -- read side of the sizing record written by scripts/somi-tier.mjs, shared by
2
+ // that CLI (approve) and scripts/somi-loop.mjs (the brief-only entry check).
3
+ //
4
+ // A brief-only work item (.somi/plans/<slug>/ holding brief.md but no phases/) may start a code loop
5
+ // only when the record for the slug says: tier small, a real signals hash (the CLI computed it), an
6
+ // approval stamp, and a brief hash equal to the brief as it is now. Links are never followed.
7
+ // Limit, accepted: an agent that deliberately writes both files by hand defeats this; the protected
8
+ // path hook and .somi/audit.log are what make that visible.
9
+ import crypto from 'node:crypto';
10
+ import fs from 'node:fs';
11
+ import path from 'node:path';
12
+
13
+ export const SLUG_RE = /^[a-z0-9][a-z0-9-]{0,63}$/;
14
+ const MAX_BYTES = 64 * 1024;
15
+ const HASH_RE = /^[0-9a-f]{64}$/;
16
+
17
+ // -> Buffer of a regular, non-link file of at most MAX_BYTES, or null when absent / not usable.
18
+ function readRegular(file) {
19
+ let fd;
20
+ try {
21
+ fd = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK);
22
+ const st = fs.fstatSync(fd);
23
+ if (!st.isFile() || st.size > MAX_BYTES) return null;
24
+ return fs.readFileSync(fd);
25
+ } catch {
26
+ return null;
27
+ } finally {
28
+ if (fd !== undefined) fs.closeSync(fd);
29
+ }
30
+ }
31
+
32
+ const noLinks = (root, parts) => {
33
+ let dir = root;
34
+ for (const part of parts) {
35
+ dir = path.join(dir, part);
36
+ try {
37
+ if (!fs.lstatSync(dir).isDirectory()) return false;
38
+ } catch {
39
+ return false;
40
+ }
41
+ }
42
+ return true;
43
+ };
44
+
45
+ export const briefPath = (root, slug) => path.join(root, '.somi', 'plans', slug, 'brief.md');
46
+
47
+ export function briefSha256(root, slug) {
48
+ const bytes = readRegular(briefPath(root, slug));
49
+ return bytes ? crypto.createHash('sha256').update(bytes).digest('hex') : null;
50
+ }
51
+
52
+ // -> the parsed record, or null.
53
+ export function readRecord(root, slug) {
54
+ if (!SLUG_RE.test(slug) || !noLinks(root, ['.somi', 'somi-state', 'tier'])) return null;
55
+ const bytes = readRegular(path.join(root, '.somi', 'somi-state', 'tier', `${slug}.json`));
56
+ if (!bytes) return null;
57
+ try {
58
+ const rec = JSON.parse(bytes.toString('utf8'));
59
+ return rec !== null && typeof rec === 'object' && !Array.isArray(rec) ? rec : null;
60
+ } catch {
61
+ return null;
62
+ }
63
+ }
64
+
65
+ // -> { ok: true } or { ok: false, reason } for a brief-only work item.
66
+ export function verifyBriefOnly(root, slug) {
67
+ const rec = readRecord(root, slug);
68
+ if (!rec) return { ok: false, reason: 'no readable tier record from scripts/somi-tier.mjs' };
69
+ if (rec.slug !== slug) return { ok: false, reason: 'the tier record names another slug' };
70
+ if (rec.tier !== 'small') return { ok: false, reason: 'the tier record is not small' };
71
+ if (typeof rec.signals_sha256 !== 'string' || !HASH_RE.test(rec.signals_sha256)) {
72
+ return { ok: false, reason: 'the tier record has no signals hash' };
73
+ }
74
+ if (typeof rec.approved_at !== 'string' || rec.approved_at === '') {
75
+ return { ok: false, reason: 'the tier record carries no approval (run somi-tier.mjs approve after the human replies)' };
76
+ }
77
+ const now = briefSha256(root, slug);
78
+ if (now === null || rec.brief_sha256 !== now) {
79
+ return { ok: false, reason: 'brief.md is missing or changed since it was approved' };
80
+ }
81
+ return { ok: true };
82
+ }
@@ -1,6 +1,6 @@
1
1
  [
2
2
  {
3
- "file": "agents/planner.md",
3
+ "file": "agents/somi-planner.md",
4
4
  "budget": 300,
5
5
  "current_size": 402,
6
6
  "source": "ecbad71",
@@ -8,7 +8,7 @@
8
8
  "reason": "Prompt file over the context-cost budget; split or trim to drive to zero."
9
9
  },
10
10
  {
11
- "file": "agents/discovery-analyst.md",
11
+ "file": "agents/somi-discovery-analyst.md",
12
12
  "budget": 300,
13
13
  "current_size": 331,
14
14
  "source": "e1701df",
@@ -23,14 +23,6 @@
23
23
  "at": "2026-10-01",
24
24
  "reason": "Over budget at the time the gate was introduced; split to drive the ledger to zero."
25
25
  },
26
- {
27
- "file": "scripts/somi-loop.mjs",
28
- "budget": 500,
29
- "current_size": 522,
30
- "source": "df8cf8b",
31
- "at": "2026-10-01",
32
- "reason": "Over budget at the time the gate was introduced; split to drive the ledger to zero."
33
- },
34
26
  {
35
27
  "file": "tests/evals/convergence.mjs",
36
28
  "budget": 500,
@@ -42,7 +34,7 @@
42
34
  {
43
35
  "file": "tests/evals/run.mjs",
44
36
  "budget": 500,
45
- "current_size": 1433,
37
+ "current_size": 1380,
46
38
  "source": "e3243d6",
47
39
  "at": "2026-10-01",
48
40
  "reason": "Over budget at the time the gate was introduced; split to drive the ledger to zero."
@@ -58,10 +50,10 @@
58
50
  {
59
51
  "file": "tests/scripts/convergence-runner.sh",
60
52
  "budget": 800,
61
- "current_size": 1510,
62
- "source": "6d9dec9",
63
- "at": "2026-10-01",
64
- "reason": "Over budget at the time the gate was introduced; split to drive the ledger to zero."
53
+ "current_size": 1514,
54
+ "source": "release/4.0.0 (#39)",
55
+ "at": "2026-10-25",
56
+ "reason": "Over budget at the time the gate was introduced; +4 for #39 (set-based worktree-leak helper and its positive control). Split to drive the ledger to zero."
65
57
  },
66
58
  {
67
59
  "file": "tests/scripts/evals-fixtures.sh",