@skathio/somi 2.2.2 → 3.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.
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +8 -2
- package/.copilot-extension/extension.json +9 -8
- package/.copilot-extension/marketplace.json +2 -2
- package/README.md +45 -33
- package/VERSION +1 -1
- package/agents/architecture-reviewer.md +16 -0
- package/agents/atlas.md +102 -0
- package/agents/coder.md +66 -22
- package/agents/designer.md +20 -16
- package/agents/discovery-analyst.md +6 -5
- package/agents/impact.md +97 -0
- package/agents/incident.md +96 -0
- package/agents/plan-reviewer.md +70 -0
- package/agents/planner.md +77 -20
- package/agents/pr.md +101 -0
- package/agents/refactor-designer.md +82 -0
- package/agents/refactorer.md +30 -24
- package/agents/reviewer.md +28 -25
- package/agents/sdlc-reviewer.md +69 -0
- package/agents/security-reviewer.md +17 -0
- package/agents/somi.md +204 -153
- package/agents/test-strategist.md +16 -1
- package/commands/adopt.md +21 -7
- package/commands/architecture-review.md +8 -5
- package/commands/atlas.md +24 -72
- package/commands/code-loop.md +46 -15
- package/commands/code-parallel.md +6 -4
- package/commands/code.md +29 -21
- package/commands/debug.md +17 -11
- package/commands/design.md +48 -31
- package/commands/discover.md +26 -16
- package/commands/impact.md +16 -54
- package/commands/incident.md +22 -55
- package/commands/plan-loop.md +43 -17
- package/commands/plan.md +64 -26
- package/commands/pr.md +19 -66
- package/commands/refactor-design.md +98 -0
- package/commands/refactor.md +22 -23
- package/commands/release-readiness.md +16 -7
- package/commands/review-panel.md +31 -10
- package/commands/review.md +21 -14
- package/commands/security-review.md +12 -3
- package/commands/ship-loop.md +71 -54
- package/commands/ship.md +17 -13
- package/commands/test-strategy.md +6 -4
- package/commands/upgrade.md +10 -8
- package/examples/discovery-example.md +2 -2
- package/examples/sample-consumer/CLAUDE.md +41 -7
- package/examples/sample-consumer/README.md +36 -22
- package/hooks/lib/common.mjs +66 -20
- package/hooks/post-tool/audit-log.mjs +7 -11
- package/hooks/post-tool/lint-changed-files.mjs +8 -11
- package/hooks/pre-tool/block-dangerous-bash.mjs +32 -44
- package/hooks/pre-tool/block-secret-writes.mjs +6 -9
- package/hooks/pre-tool/gate-dep-install.mjs +18 -27
- package/hooks/pre-tool/guard-protected-paths.mjs +9 -11
- package/hooks/session-start/detect-repo-instructions.mjs +21 -23
- package/hooks/user-prompt-submit/inject-workflow-context.mjs +190 -42
- package/mcp.json +10 -0
- package/package.json +5 -2
- package/rules/CLAUDE.md +22 -19
- package/scripts/check-links.mjs +218 -0
- package/scripts/check-session-artefacts.mjs +149 -0
- package/scripts/check-size-budget.mjs +137 -0
- package/scripts/generate-digest.mjs +176 -0
- package/scripts/lib/cost-ceiling.mjs +317 -0
- package/scripts/lib/cost-model.mjs +95 -0
- package/scripts/lib/dispatch-resolver.mjs +224 -0
- package/scripts/lib/mcp-project-root.mjs +183 -0
- package/scripts/size-budget-overrides.json +74 -0
- package/scripts/somi-check.mjs +6 -13
- package/scripts/somi-dispatch.mjs +141 -0
- package/scripts/somi-findings.mjs +4 -5
- package/scripts/somi-loop.mjs +181 -13
- package/scripts/somi-mcp.mjs +357 -0
- package/scripts/validate.sh +690 -29
- package/skills/clean-code/SKILL.md +1 -1
- package/skills/plan-review/SKILL.md +79 -0
- package/skills/rules/SKILL.md +47 -0
- package/skills/sdlc-process/SKILL.md +74 -0
- package/skills/somi-dispatch/SKILL.md +220 -0
- package/skills/somi-routing/SKILL.md +31 -6
- package/skills/{test-strategy → testing-playbook}/SKILL.md +1 -1
- package/templates/ATLAS.md.tmpl +4 -4
- package/templates/BRIEF.md.tmpl +23 -22
- package/templates/DECISIONS.md.tmpl +1 -0
- package/templates/DESIGN.md.tmpl +5 -5
- package/templates/DIARY.md.tmpl +10 -5
- package/templates/DOD.md.tmpl +24 -0
- package/templates/PROGRESS.md.tmpl +3 -16
- package/templates/SOMI-README.md.tmpl +3 -3
- package/tests/hooks/cases/inject-workflow-context.json +120 -7
- package/tests/hooks/run.sh +20 -11
- package/tests/scripts/check-links.sh +105 -0
- package/tests/scripts/check-session-artefacts.sh +60 -0
- package/tests/scripts/check-size-budget.sh +71 -0
- package/tests/scripts/checkpoint-gate.sh +90 -0
- package/tests/scripts/convergence-runner.sh +1510 -0
- package/tests/scripts/cost-ceiling.sh +343 -0
- package/tests/scripts/cost-model.sh +192 -0
- package/tests/scripts/dispatch-guard.sh +108 -0
- package/tests/scripts/dispatch-reference-gate.sh +173 -0
- package/tests/scripts/eval-runner.sh +3691 -0
- package/tests/scripts/evals-packaging.sh +117 -0
- package/tests/scripts/fixtures/legacy-shard-no-schema.json +50 -0
- package/tests/scripts/generate-digest.sh +180 -0
- package/tests/scripts/goldens/loop-state-breach.json +1 -0
- package/tests/scripts/goldens/loop-state-done.json +1 -0
- package/tests/scripts/goldens/somi-loop.json +1 -1
- package/tests/scripts/lib/adr-shape.mjs +41 -0
- package/tests/scripts/lib/checkpoint-gate.mjs +54 -0
- package/tests/scripts/lib/digest-marker-coupling.mjs +52 -0
- package/tests/scripts/lib/dispatch-reference-gate.mjs +112 -0
- package/tests/scripts/lib/multipass-dependence.mjs +192 -0
- package/tests/scripts/lib/multipass-source-identity.mjs +243 -0
- package/tests/scripts/lib/reference-pair.mjs +61 -0
- package/tests/scripts/lib/retirement-gate.mjs +167 -0
- package/tests/scripts/lib/shell-embedded-js.mjs +47 -0
- package/tests/scripts/lib/somi-mcp-client.mjs +102 -0
- package/tests/scripts/lib/somi-mcp-tests.mjs +401 -0
- package/tests/scripts/lib/somi-mcp-tool-schema.mjs +38 -0
- package/tests/scripts/retirement-gate.sh +287 -0
- package/tests/scripts/review-panel-seats.sh +32 -0
- package/tests/scripts/run.sh +184 -2
- package/tests/scripts/somi-agent-dispatch-contract.sh +195 -0
- package/tests/scripts/somi-dispatch.sh +354 -0
- package/tests/scripts/somi-mcp.sh +12 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// dispatch-resolver.mjs — the resolve logic shared by scripts/somi-dispatch.mjs (the CLI) and
|
|
2
|
+
// scripts/somi-mcp.mjs (the MCP server): for a given agent, right now, which tier and model
|
|
3
|
+
// should run it? Factored out so the two surfaces share one validation order and one set of
|
|
4
|
+
// error codes/messages rather than each re-deriving the composition and risking drift.
|
|
5
|
+
//
|
|
6
|
+
// Pure function over explicit inputs: no argv parsing, no process.exit, no stdout/stderr writes.
|
|
7
|
+
// Each caller turns the returned {ok, ...} shape into its own surface — the CLI: exit code +
|
|
8
|
+
// stdout JSON, or a stderr message; the MCP server: a tool result. Composition (never inline the
|
|
9
|
+
// pieces yourself; this is the one sanctioned call site for it):
|
|
10
|
+
// resolveCeiling(projectRoot, ceiling) -> decideDispatch(declaredCost, ceiling)
|
|
11
|
+
// -> modelForDispatch(decision, host, mergeHostMapping(HOST_MODELS, cost.mapping))
|
|
12
|
+
//
|
|
13
|
+
// `cost.mapping` is read from the project's .somi/config.json on EVERY call, unlike the ceiling
|
|
14
|
+
// (re-read only at bootstrap — see cost-ceiling.mjs's header): the mapping is a lookup table, not
|
|
15
|
+
// a gate, so a mid-session config fix taking effect on the very next dispatch is correct.
|
|
16
|
+
//
|
|
17
|
+
// `agent` is validated against a strict allowlist before any path is built — it may come from a
|
|
18
|
+
// prompt carrying untrusted text (and, via the MCP server, from a model-supplied tool argument).
|
|
19
|
+
//
|
|
20
|
+
// Codes both surfaces reuse verbatim (see each caller for how it turns these into its own shape):
|
|
21
|
+
// 64 usage error (bad/missing arguments, including an unrecognized ceiling/env-ceiling value) —
|
|
22
|
+
// `validateDispatchArgs` below is the ONE place both surfaces run this family of checks, so
|
|
23
|
+
// the same bad input produces the same code through either surface, checked before either
|
|
24
|
+
// surface's own project-root lookup (the MCP server's own can be a 2s `roots/list` round trip)
|
|
25
|
+
// 65 unknown agent (no agents/<name>.md at the install root)
|
|
26
|
+
// 66 malformed declaration (no cost:, or an invalid/unordered/duplicated cost set) — OR the
|
|
27
|
+
// merged mapping's own resolution for the selected tier failing WHEN the project's own
|
|
28
|
+
// cost.mapping never named that host at all (a bug in the shipped HOST_MODELS table, not a
|
|
29
|
+
// broken project file)
|
|
30
|
+
// 67 project environment/config failure (corrupt .somi/somi-state/ceiling.json; an
|
|
31
|
+
// unparsable/malformed .somi/config.json; a structurally invalid cost.mapping) — distinct
|
|
32
|
+
// from 64 because the problem is the project's own persisted files, not this call's
|
|
33
|
+
// arguments — ALSO when the mapping fails for a host the project's mapping DID name (a
|
|
34
|
+
// partial override of a shipped host replaces its whole tier map, not per-tier) — ALSO the
|
|
35
|
+
// code scripts/lib/mcp-project-root.mjs returns for "no project root could be established":
|
|
36
|
+
// the MCP server's own environment failing to add up to a usable answer, same family.
|
|
37
|
+
// A guessed model is never returned on any error path — only the ok:true result carries one.
|
|
38
|
+
|
|
39
|
+
import fs from 'node:fs';
|
|
40
|
+
import path from 'node:path';
|
|
41
|
+
import { resolveCeiling, decideDispatch, modelForDispatch, readCostConfig, CEILING_ENV } from './cost-ceiling.mjs';
|
|
42
|
+
import { HOST_MODELS, mergeHostMapping, VALID_COSTS } from './cost-model.mjs';
|
|
43
|
+
|
|
44
|
+
export const AGENT_NAME_RE = /^[a-z][a-z0-9-]*$/;
|
|
45
|
+
|
|
46
|
+
export const EXIT_USAGE = 64;
|
|
47
|
+
export const EXIT_UNKNOWN_AGENT = 65;
|
|
48
|
+
export const EXIT_MALFORMED = 66;
|
|
49
|
+
export const EXIT_PROJECT_ENV = 67;
|
|
50
|
+
|
|
51
|
+
function err(code, message) {
|
|
52
|
+
return { ok: false, code, message };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Extracts one field's raw value from the frontmatter block (the lines between the first two
|
|
56
|
+
// literal `---` delimiters) — same block scripts/validate.sh's cost_of()/model_of() awk parses.
|
|
57
|
+
function frontmatterField(content, field) {
|
|
58
|
+
const re = new RegExp(`^${field}:\\s*(.*)$`);
|
|
59
|
+
let dashes = 0;
|
|
60
|
+
for (const line of content.split('\n')) {
|
|
61
|
+
if (line === '---') {
|
|
62
|
+
dashes++;
|
|
63
|
+
if (dashes >= 2) break;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (dashes === 1) {
|
|
67
|
+
const m = re.exec(line);
|
|
68
|
+
if (m) return m[1].trim();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// The ONE validation order both surfaces run, over the SAME three fields, producing the SAME code
|
|
75
|
+
// for the SAME bad input. scripts/somi-dispatch.mjs and scripts/somi-mcp.mjs each call this
|
|
76
|
+
// directly before either does its own project-root lookup (the MCP server's can be a 2s
|
|
77
|
+
// `roots/list` round trip); `resolveDispatch` below also calls it first thing, so a direct caller
|
|
78
|
+
// of that alone still gets the same checks before any disk I/O.
|
|
79
|
+
//
|
|
80
|
+
// `undefined` means "not given" for `host`/`ceiling` (the CLI's own sentinel — requireValue()
|
|
81
|
+
// already dies on an explicitly empty flag before assignment, so it never passes `''` here). A
|
|
82
|
+
// DEFINED-but-wrong-shaped value (an empty string on purpose, an array, an object, a number) is a
|
|
83
|
+
// caller mistake, not an omission, and must fail the same way — silently coercing it to "not
|
|
84
|
+
// given", as an earlier shape of the MCP surface did, is the bug this function closes.
|
|
85
|
+
//
|
|
86
|
+
// `agent` has no "not given" reading — it is REQUIRED, so `undefined` fails like any non-string.
|
|
87
|
+
export function validateDispatchArgs({ agent, host, ceiling } = {}) {
|
|
88
|
+
// `JSON.stringify`, not a bare template-literal interpolation: an object-shaped `agent` (e.g.
|
|
89
|
+
// `{toString: null}`, reachable as an MCP tool argument) throws INSIDE `${agent}` instead of
|
|
90
|
+
// producing a string, surfacing as an unrelated internal error rather than this 64.
|
|
91
|
+
if (typeof agent !== 'string' || !AGENT_NAME_RE.test(agent)) {
|
|
92
|
+
return err(EXIT_USAGE, `invalid --agent ${JSON.stringify(agent)} (expected ${AGENT_NAME_RE})`);
|
|
93
|
+
}
|
|
94
|
+
// `host` has a real default ('claude-code', applied once validation passes) — only a
|
|
95
|
+
// DEFINED-but-wrong-shaped value is rejected here.
|
|
96
|
+
if (host !== undefined && (typeof host !== 'string' || host === '')) {
|
|
97
|
+
return err(EXIT_USAGE, `--host requires a non-empty string, got ${JSON.stringify(host)}`);
|
|
98
|
+
}
|
|
99
|
+
// Validated here, before resolveCeiling() ever touches disk: a bad ceiling used to reach
|
|
100
|
+
// loadOrInitState() first, which bootstraps (and WRITES) the state file before the value is
|
|
101
|
+
// checked, so a rejected argument still left a state file behind.
|
|
102
|
+
if (ceiling !== undefined) {
|
|
103
|
+
if (typeof ceiling !== 'string' || ceiling === '') {
|
|
104
|
+
return err(EXIT_USAGE, `--ceiling requires a non-empty string, got ${JSON.stringify(ceiling)}`);
|
|
105
|
+
}
|
|
106
|
+
if (!VALID_COSTS.includes(ceiling)) {
|
|
107
|
+
return err(EXIT_USAGE, `--ceiling expected one of ${VALID_COSTS.join('|')}, got "${ceiling}"`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// Same reasoning for the env-var channel: SOMI_COST_CEILING is caller input like --ceiling, so a
|
|
111
|
+
// bad value is 64 here, never routed into resolveCeiling() to surface as a project-env failure
|
|
112
|
+
// (67) alongside a genuinely corrupt config/state file.
|
|
113
|
+
const envCeiling = process.env[CEILING_ENV];
|
|
114
|
+
if (envCeiling && !VALID_COSTS.includes(envCeiling)) {
|
|
115
|
+
return err(EXIT_USAGE, `${CEILING_ENV} expected one of ${VALID_COSTS.join('|')}, got "${envCeiling}"`);
|
|
116
|
+
}
|
|
117
|
+
return { ok: true };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Resolves one agent's dispatch tier + model. `host` defaults to `'claude-code'` when not given;
|
|
122
|
+
* `ceiling`, when given, is this call's explicit override. Both, and `agent`, are validated by
|
|
123
|
+
* `validateDispatchArgs` above. `projectRoot`/`installRoot` are plain strings the caller resolves
|
|
124
|
+
* itself (the CLI's own convention differs from the MCP server's — see mcp-project-root.mjs).
|
|
125
|
+
*
|
|
126
|
+
* @returns {{ok: true, result: object} | {ok: false, code: number, message: string}}
|
|
127
|
+
*/
|
|
128
|
+
export function resolveDispatch({ agent, host, ceiling, projectRoot, installRoot }) {
|
|
129
|
+
const validated = validateDispatchArgs({ agent, host, ceiling });
|
|
130
|
+
if (!validated.ok) return validated;
|
|
131
|
+
const HOST = host || 'claude-code';
|
|
132
|
+
|
|
133
|
+
const agentPath = path.join(installRoot, 'agents', `${agent}.md`);
|
|
134
|
+
let content;
|
|
135
|
+
try {
|
|
136
|
+
content = fs.readFileSync(agentPath, 'utf8');
|
|
137
|
+
} catch (e) {
|
|
138
|
+
if (e.code === 'ENOENT') return err(EXIT_UNKNOWN_AGENT, `unknown agent "${agent}" (no ${agentPath})`);
|
|
139
|
+
throw e;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const rawCost = frontmatterField(content, 'cost');
|
|
143
|
+
if (!rawCost) {
|
|
144
|
+
if (agent === 'somi') {
|
|
145
|
+
return err(
|
|
146
|
+
EXIT_MALFORMED,
|
|
147
|
+
`agent "somi" is exempt from cost: — the host binds its model when the user selects it ` +
|
|
148
|
+
`in the host's own UI, so there is nothing for this resolver to compute`,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
return err(EXIT_MALFORMED, `agent "${agent}" declares no cost: in its frontmatter`);
|
|
152
|
+
}
|
|
153
|
+
const declaredCost = rawCost.split(',').map((s) => s.trim());
|
|
154
|
+
|
|
155
|
+
// Both --ceiling and SOMI_COST_CEILING were already syntax-checked above, before any disk write —
|
|
156
|
+
// so anything resolveCeiling() throws now is the PROJECT's own persisted state, not this call's
|
|
157
|
+
// arguments: an unparsable .somi/config.json, or a corrupt .somi/somi-state/ceiling.json.
|
|
158
|
+
let ceilingResult;
|
|
159
|
+
try {
|
|
160
|
+
ceilingResult = resolveCeiling(projectRoot, ceiling || undefined);
|
|
161
|
+
} catch (e) {
|
|
162
|
+
return err(EXIT_PROJECT_ENV, e.message);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Read fresh per call, not per-bootstrap (see this file's header): this is how a project on a
|
|
166
|
+
// host SoMi doesn't ship a mapping for (e.g. Copilot) tells SoMi which of ITS models counts as
|
|
167
|
+
// low/medium/high. Captured once, before the merge, and kept around past it: modelForDispatch's
|
|
168
|
+
// own catch below needs to know whether the PROJECT's mapping named this specific host at all,
|
|
169
|
+
// to attribute a later resolution failure correctly.
|
|
170
|
+
let projectMapping;
|
|
171
|
+
try {
|
|
172
|
+
projectMapping = readCostConfig(projectRoot).mapping;
|
|
173
|
+
} catch (e) {
|
|
174
|
+
return err(EXIT_PROJECT_ENV, e.message);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// mergeHostMapping rejects a __proto__/constructor/prototype key, a non-object per-host value,
|
|
178
|
+
// and a tier key outside low|medium|high outright — kept in its own try/catch, distinct from
|
|
179
|
+
// decideDispatch/modelForDispatch below: this fails only on the PROJECT's own persisted
|
|
180
|
+
// .somi/config.json being structurally broken, never on this agent's own `cost:` frontmatter.
|
|
181
|
+
let mapping;
|
|
182
|
+
try {
|
|
183
|
+
mapping = mergeHostMapping(HOST_MODELS, projectMapping);
|
|
184
|
+
} catch (e) {
|
|
185
|
+
return err(EXIT_PROJECT_ENV, e.message);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// decideDispatch failing is always this AGENT's own cost: declaration (66) — unrelated to the
|
|
189
|
+
// mapping, so it stays in its own try/catch rather than sharing modelForDispatch's below.
|
|
190
|
+
let decision;
|
|
191
|
+
try {
|
|
192
|
+
decision = decideDispatch(declaredCost, ceilingResult.ceiling);
|
|
193
|
+
} catch (e) {
|
|
194
|
+
return err(EXIT_MALFORMED, e.message);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// modelForDispatch failing here means the MERGED mapping is shape-valid but incomplete or wrong
|
|
198
|
+
// for the tier this call actually selected. Whose fault that is turns on whether the project's
|
|
199
|
+
// OWN cost.mapping named this host at all: if it did, the gap is the project's own file (67),
|
|
200
|
+
// and the message says so explicitly. Only a host the project's mapping never mentioned failing
|
|
201
|
+
// this way would point at a bug in the shipped HOST_MODELS table itself, which stays 66.
|
|
202
|
+
let model;
|
|
203
|
+
try {
|
|
204
|
+
model = modelForDispatch(decision, HOST, mapping);
|
|
205
|
+
} catch (e) {
|
|
206
|
+
if (projectMapping && Object.hasOwn(projectMapping, HOST)) {
|
|
207
|
+
return err(EXIT_PROJECT_ENV, `${e.message} (in .somi/config.json cost.mapping)`);
|
|
208
|
+
}
|
|
209
|
+
return err(EXIT_MALFORMED, e.message);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
ok: true,
|
|
214
|
+
result: {
|
|
215
|
+
agent,
|
|
216
|
+
supported: decision.supported,
|
|
217
|
+
tier: decision.selected,
|
|
218
|
+
model,
|
|
219
|
+
ceiling: ceilingResult.ceiling,
|
|
220
|
+
ceiling_source: ceilingResult.source,
|
|
221
|
+
ceiling_origin: ceilingResult.ceiling_origin,
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
// mcp-project-root.mjs — resolves the CONSUMING project's root for scripts/somi-mcp.mjs's
|
|
2
|
+
// `somi_resolve` tool. An MCP server is launched once, at session start, from the plugin's own
|
|
3
|
+
// location, so scripts/somi-dispatch.mjs's own projectRoot() (CLAUDE_PROJECT_DIR, else this
|
|
4
|
+
// PROCESS's cwd) is unsafe to reuse: neither host's docs confirm the server's launch-time cwd is
|
|
5
|
+
// the user's project rather than the plugin's own install dir. No cwd fallback here — that would
|
|
6
|
+
// risk silently resolving to SoMi's own repo, the exact failure this module exists to rule out.
|
|
7
|
+
//
|
|
8
|
+
// Two kinds of root:
|
|
9
|
+
// - a HOST root: `CLAUDE_PROJECT_DIR` (this process's own env — tried first, no round trip) or
|
|
10
|
+
// the client's MCP `roots/list` answer. Neither is text a model wrote.
|
|
11
|
+
// - `project_dir`: a tool argument — text a MODEL supplied, validated (`validateDir()` below)
|
|
12
|
+
// either way, but validity alone is no longer sufficient once a host root exists.
|
|
13
|
+
//
|
|
14
|
+
// Precedence: a HOST root, once established, is the one thing `project_dir` cannot override — it
|
|
15
|
+
// can only narrow it (select a subdirectory, e.g. a nested project in a workspace).
|
|
16
|
+
// - host root + project_dir given -> project_dir must realpath to that root or inside it, or
|
|
17
|
+
// refused (64) before anything is read or written; passing -> project_dir itself is used.
|
|
18
|
+
// - host root, no project_dir -> the host root is used directly.
|
|
19
|
+
// - no host root, project_dir given -> project_dir alone decides (the only case a model's text
|
|
20
|
+
// still governs outright, since nothing more authoritative was offered).
|
|
21
|
+
// - no host root, no project_dir -> refuse (67 — reuses dispatch-resolver.mjs's own family;
|
|
22
|
+
// see its header). Never a cwd fallback.
|
|
23
|
+
//
|
|
24
|
+
// `roots/list` can answer with more than one root. A single one behaves like any host root above;
|
|
25
|
+
// more than one is ambiguous by itself, resolved only if project_dir selects exactly one of them.
|
|
26
|
+
//
|
|
27
|
+
// Every `roots/list` URI must be a `file:` URI — anything else is dropped, never treated as a path
|
|
28
|
+
// (an earlier shape of this file hand-stripped a literal `file://` prefix and left anything else
|
|
29
|
+
// untouched, so a non-file URI resolved relative to the server's own cwd). Decoded with
|
|
30
|
+
// `url.fileURLToPath` (handles percent-encoding and a `file://host/` authority correctly, unlike
|
|
31
|
+
// hand-slicing), then validated like `project_dir`: a root that doesn't exist is rejected, never
|
|
32
|
+
// silently created by whatever writes to it downstream (cost-ceiling.mjs's saveState mkdir -p's).
|
|
33
|
+
|
|
34
|
+
import fs from 'node:fs';
|
|
35
|
+
import path from 'node:path';
|
|
36
|
+
import { fileURLToPath } from 'node:url';
|
|
37
|
+
import { EXIT_USAGE, EXIT_PROJECT_ENV } from './dispatch-resolver.mjs';
|
|
38
|
+
|
|
39
|
+
// Structural check only (absolute, exists, a directory) — shared by `project_dir` and every
|
|
40
|
+
// `roots/list` candidate so the two can't drift on what counts as usable. Returns a reason, not a
|
|
41
|
+
// ready-made error: the two call sites attribute a bad value to a different code (project_dir is
|
|
42
|
+
// caller/model text -> 64; a bad roots/list entry is the HOST's own environment -> 67).
|
|
43
|
+
function validateDir(dir) {
|
|
44
|
+
if (typeof dir !== 'string' || dir === '') return { ok: false, reason: 'empty' };
|
|
45
|
+
if (!path.isAbsolute(dir)) return { ok: false, reason: 'not-absolute' };
|
|
46
|
+
let st;
|
|
47
|
+
try {
|
|
48
|
+
st = fs.statSync(dir);
|
|
49
|
+
} catch {
|
|
50
|
+
return { ok: false, reason: 'missing' };
|
|
51
|
+
}
|
|
52
|
+
if (!st.isDirectory()) return { ok: false, reason: 'not-a-directory' };
|
|
53
|
+
return { ok: true, dir };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function realpathOrNull(dir) {
|
|
57
|
+
try {
|
|
58
|
+
return fs.realpathSync(dir);
|
|
59
|
+
} catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Resolves the host's own root, if it supplies one. Returns undefined when the host supplies
|
|
65
|
+
// nothing usable, else `{ dirs, source }` — `dirs` is always an array (length 1 for env / a single
|
|
66
|
+
// root) so the caller has one shape to bound project_dir against either way.
|
|
67
|
+
async function hostRoot({ listRoots }) {
|
|
68
|
+
const envRoot = process.env.CLAUDE_PROJECT_DIR;
|
|
69
|
+
// Mirrors somi-dispatch.mjs's own projectRoot(): an unexpanded `${...}` placeholder is not real.
|
|
70
|
+
if (envRoot && !envRoot.includes('${')) {
|
|
71
|
+
return { dirs: [envRoot], source: 'env' };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!listRoots) return undefined;
|
|
75
|
+
let rawRoots;
|
|
76
|
+
try {
|
|
77
|
+
rawRoots = await listRoots();
|
|
78
|
+
} catch {
|
|
79
|
+
rawRoots = undefined;
|
|
80
|
+
}
|
|
81
|
+
if (!Array.isArray(rawRoots)) return undefined;
|
|
82
|
+
|
|
83
|
+
const dirs = [];
|
|
84
|
+
for (const uri of rawRoots) {
|
|
85
|
+
if (typeof uri !== 'string' || !uri.startsWith('file:')) continue; // non-file: dropped
|
|
86
|
+
let p;
|
|
87
|
+
try {
|
|
88
|
+
p = fileURLToPath(uri); // decodes percent-encoding; handles a file://host/ authority
|
|
89
|
+
} catch {
|
|
90
|
+
continue; // malformed file: URI -- dropped, not guessed at
|
|
91
|
+
}
|
|
92
|
+
dirs.push(p);
|
|
93
|
+
}
|
|
94
|
+
return dirs.length > 0 ? { dirs, source: 'roots' } : undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @param {object} opts
|
|
99
|
+
* @param {string} [opts.projectDirArg] - the tool call's own `project_dir` argument, if given.
|
|
100
|
+
* @param {() => Promise<string[]|undefined>} [opts.listRoots] - calls the client's `roots/list`
|
|
101
|
+
* and returns the raw URI strings, or `undefined` when the client never declared `roots`.
|
|
102
|
+
* @returns {Promise<{ok: true, root: string, source: string} | {ok: false, code: number, message: string}>}
|
|
103
|
+
*/
|
|
104
|
+
export async function resolveProjectRoot({ projectDirArg, listRoots }) {
|
|
105
|
+
// Validated FIRST regardless of whether a host root exists -- a malformed value is 64 either
|
|
106
|
+
// way, and the bounding check below needs a real, fs-verified directory to compare against.
|
|
107
|
+
let projectDir; // undefined (not given), or the validated absolute path
|
|
108
|
+
if (projectDirArg !== undefined) {
|
|
109
|
+
const v = validateDir(projectDirArg);
|
|
110
|
+
if (!v.ok) {
|
|
111
|
+
const reason = {
|
|
112
|
+
empty: `project_dir requires a non-empty string, got ${JSON.stringify(projectDirArg)}`,
|
|
113
|
+
'not-absolute': `project_dir must be an absolute path, got ${JSON.stringify(projectDirArg)}`,
|
|
114
|
+
missing: `project_dir ${JSON.stringify(projectDirArg)} does not exist`,
|
|
115
|
+
'not-a-directory': `project_dir ${JSON.stringify(projectDirArg)} is not a directory`,
|
|
116
|
+
}[v.reason];
|
|
117
|
+
return { ok: false, code: EXIT_USAGE, message: reason };
|
|
118
|
+
}
|
|
119
|
+
projectDir = v.dir;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const host = await hostRoot({ listRoots });
|
|
123
|
+
|
|
124
|
+
if (!host) {
|
|
125
|
+
// Host supplies nothing: project_dir alone decides.
|
|
126
|
+
if (projectDir) return { ok: true, root: projectDir, source: 'project_dir' };
|
|
127
|
+
return {
|
|
128
|
+
ok: false,
|
|
129
|
+
code: EXIT_PROJECT_ENV,
|
|
130
|
+
message:
|
|
131
|
+
'no project root could be established: CLAUDE_PROJECT_DIR is unset, the client offered no ' +
|
|
132
|
+
'MCP roots (or none were returned), and no project_dir argument was given -- pass ' +
|
|
133
|
+
'project_dir explicitly; this never falls back to the SoMi install directory',
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Each host-offered candidate must itself exist as a directory -- an entry that fails is
|
|
138
|
+
// dropped, not treated as its own hard error (a multi-folder client can legitimately offer one
|
|
139
|
+
// root this process can't see). CLAUDE_PROJECT_DIR's single candidate failing is exactly "a
|
|
140
|
+
// root that doesn't exist", refused below like any other.
|
|
141
|
+
const validCandidates = host.dirs
|
|
142
|
+
.map((d) => validateDir(d))
|
|
143
|
+
.filter((v) => v.ok)
|
|
144
|
+
.map((v) => v.dir);
|
|
145
|
+
|
|
146
|
+
if (validCandidates.length === 0) {
|
|
147
|
+
return {
|
|
148
|
+
ok: false,
|
|
149
|
+
code: EXIT_PROJECT_ENV,
|
|
150
|
+
message: `the host-supplied root (${host.source}) does not resolve to an existing directory -- refusing rather than creating one`,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (!projectDir) {
|
|
155
|
+
if (validCandidates.length > 1) {
|
|
156
|
+
return {
|
|
157
|
+
ok: false,
|
|
158
|
+
code: EXIT_PROJECT_ENV,
|
|
159
|
+
message: `ambiguous: the client offered ${validCandidates.length} MCP roots and no project_dir was given to select one`,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
return { ok: true, root: validCandidates[0], source: host.source };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// A host root exists AND project_dir is given: it must realpath to one of the host's own
|
|
166
|
+
// candidates, or inside one -- a model argument narrows the host's root, never overrides it.
|
|
167
|
+
const projectDirReal = realpathOrNull(projectDir);
|
|
168
|
+
const matches = projectDirReal !== null && validCandidates.some((c) => {
|
|
169
|
+
const candidateReal = realpathOrNull(c);
|
|
170
|
+
if (candidateReal === null) return false;
|
|
171
|
+
return projectDirReal === candidateReal || projectDirReal.startsWith(candidateReal + path.sep);
|
|
172
|
+
});
|
|
173
|
+
if (!matches) {
|
|
174
|
+
return {
|
|
175
|
+
ok: false,
|
|
176
|
+
code: EXIT_USAGE,
|
|
177
|
+
message:
|
|
178
|
+
`project_dir "${projectDir}" is outside the host-supplied root -- refusing (project_dir ` +
|
|
179
|
+
`may only select a directory inside the host's own root, never override it)`,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
return { ok: true, root: projectDir, source: 'project_dir' };
|
|
183
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"file": "agents/planner.md",
|
|
4
|
+
"budget": 300,
|
|
5
|
+
"current_size": 402,
|
|
6
|
+
"source": "ecbad71",
|
|
7
|
+
"at": "2026-10-01",
|
|
8
|
+
"reason": "Prompt file over the context-cost budget; split or trim to drive to zero."
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"file": "agents/discovery-analyst.md",
|
|
12
|
+
"budget": 300,
|
|
13
|
+
"current_size": 331,
|
|
14
|
+
"source": "e1701df",
|
|
15
|
+
"at": "2026-10-01",
|
|
16
|
+
"reason": "Prompt file over the context-cost budget; split or trim to drive to zero."
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"file": "hooks/user-prompt-submit/inject-workflow-context.mjs",
|
|
20
|
+
"budget": 500,
|
|
21
|
+
"current_size": 580,
|
|
22
|
+
"source": "df8cf8b",
|
|
23
|
+
"at": "2026-10-01",
|
|
24
|
+
"reason": "Over budget at the time the gate was introduced; split to drive the ledger to zero."
|
|
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
|
+
{
|
|
35
|
+
"file": "tests/evals/convergence.mjs",
|
|
36
|
+
"budget": 500,
|
|
37
|
+
"current_size": 1218,
|
|
38
|
+
"source": "e3243d6",
|
|
39
|
+
"at": "2026-10-01",
|
|
40
|
+
"reason": "Over budget at the time the gate was introduced; split to drive the ledger to zero."
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"file": "tests/evals/run.mjs",
|
|
44
|
+
"budget": 500,
|
|
45
|
+
"current_size": 1433,
|
|
46
|
+
"source": "e3243d6",
|
|
47
|
+
"at": "2026-10-01",
|
|
48
|
+
"reason": "Over budget at the time the gate was introduced; split to drive the ledger to zero."
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"file": "tests/scripts/eval-runner.sh",
|
|
52
|
+
"budget": 800,
|
|
53
|
+
"current_size": 3692,
|
|
54
|
+
"source": "6d9dec9",
|
|
55
|
+
"at": "2026-10-01",
|
|
56
|
+
"reason": "Over budget at the time the gate was introduced; split to drive the ledger to zero."
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"file": "tests/scripts/convergence-runner.sh",
|
|
60
|
+
"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."
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"file": "tests/scripts/evals-fixtures.sh",
|
|
68
|
+
"budget": 800,
|
|
69
|
+
"current_size": 808,
|
|
70
|
+
"source": "6d9dec9",
|
|
71
|
+
"at": "2026-10-01",
|
|
72
|
+
"reason": "Over budget at the time the gate was introduced; split to drive the ledger to zero."
|
|
73
|
+
}
|
|
74
|
+
]
|
package/scripts/somi-check.mjs
CHANGED
|
@@ -1,22 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// somi-check.mjs — host-agnostic working-tree guard (the portable enforcement layer).
|
|
3
3
|
//
|
|
4
|
-
// Node port of scripts/somi-check.sh (node-runtime-port, phase 1, iteration 1.3).
|
|
5
4
|
// Zero-dependency: stdlib only (node:fs, node:path, node:child_process). No jq, no bash.
|
|
6
5
|
// Behavior-preserving — exit codes are frozen and reproduced exactly; see the bash
|
|
7
6
|
// original's header comment for the full rationale.
|
|
8
7
|
//
|
|
9
|
-
// SHEBANG NOTE
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
// ("somi-check": "scripts/somi-check.sh", repointed to this file in Phase 3 iteration
|
|
14
|
-
// 3.2) is a *separate* invocation path via npm's own bin-symlinking, which on POSIX
|
|
8
|
+
// SHEBANG NOTE: the other scripts (somi-loop.mjs, somi-findings.mjs) are invoked as
|
|
9
|
+
// `node <file>` and carry no shebang, so nothing relies on a shebang or exec bit for the
|
|
10
|
+
// hook/manifest invocation path. This file keeps one because package.json's `bin` field
|
|
11
|
+
// ("somi-check") is a *separate* invocation path via npm's own bin-symlinking, which on POSIX
|
|
15
12
|
// needs the linked file to be directly executable — that relies on a shebang. That's
|
|
16
13
|
// npm's own contract, distinct from how hooks.json/somi-loop/somi-findings are invoked.
|
|
17
|
-
// See phases/01-state-scripts-port.md's Iteration 1.3 implementation note. (The
|
|
18
|
-
// executable bit itself is left to Phase 3 iteration 3.2, which owns the bin repoint
|
|
19
|
-
// and the live-wiring smoke test — this iteration only adds the file content needed.)
|
|
20
14
|
//
|
|
21
15
|
// Checks (each maps to a hook-layer guarantee):
|
|
22
16
|
// 1. Staged secret-bearing files (block-secret-writes' basename patterns)
|
|
@@ -40,8 +34,7 @@ const PROG = 'somi-check';
|
|
|
40
34
|
|
|
41
35
|
// Thrown to unwind to the top-level handler, which sets process.exitCode and lets Node
|
|
42
36
|
// exit naturally — avoids process.exit()'s risk of truncating buffered stdout/stderr
|
|
43
|
-
// writes on a pipe. Same idiom as somi-loop.mjs / somi-findings.mjs
|
|
44
|
-
// reviewer-blessed).
|
|
37
|
+
// writes on a pipe. Same idiom as somi-loop.mjs / somi-findings.mjs.
|
|
45
38
|
class ExitSignal extends Error {
|
|
46
39
|
constructor(code) {
|
|
47
40
|
super(`exit ${code}`);
|
|
@@ -72,7 +65,7 @@ function projectRoot() {
|
|
|
72
65
|
// Bash matches these as POSIX ERE via `[[ "$base" =~ $p ]]` — every pattern here uses
|
|
73
66
|
// only `^`/`$`/`.`/`*`/`?` and escaped literal dots, which are byte-identical in ERE
|
|
74
67
|
// and JS regex syntax (no `[[:class:]]` bracket expressions, no ERE-vs-PCRE anchor
|
|
75
|
-
// divergence to worry about). CAVEAT, do not inherit blindly in
|
|
68
|
+
// divergence to worry about). CAVEAT, do not inherit blindly in block-dangerous-bash.mjs: JS `.` refuses to
|
|
76
69
|
// match \r/U+2028/U+2029 where POSIX `.` matches every byte except \n — harmless HERE
|
|
77
70
|
// only because git's core.quotePath C-escapes non-ASCII filenames before either
|
|
78
71
|
// implementation sees them; on raw CONTENT (see the TODO-marker check below, and all of
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// somi-dispatch.mjs — CLI over scripts/lib/dispatch-resolver.mjs's resolveDispatch(): for a given
|
|
3
|
+
// agent, right now, what tier and model should run it?
|
|
4
|
+
//
|
|
5
|
+
// Zero-dependency: stdlib only. This is the mechanism a prompt (agents/somi.md) shells out to
|
|
6
|
+
// instead of judging cost tiers itself — the front door cannot invoke resolveModel/decideDispatch
|
|
7
|
+
// directly (a prompt has no way to call a function), so the arithmetic has to live in a shipped
|
|
8
|
+
// command it can run, mirroring how scripts/somi-loop.mjs is the state engine /code-loop shells
|
|
9
|
+
// out to rather than reimplementing inline.
|
|
10
|
+
//
|
|
11
|
+
// scripts/somi-mcp.mjs is the other consumer of the identical resolveDispatch() — a
|
|
12
|
+
// zero-dependency stdio MCP server, for hosts that launch a bundled MCP server instead of a
|
|
13
|
+
// shell. Neither surface re-derives the composition or the validation order; both call
|
|
14
|
+
// resolveDispatch() and turn its {ok, ...} result into their own shape (this file: exit code +
|
|
15
|
+
// stdout JSON / stderr message; the MCP server: a tool result). See
|
|
16
|
+
// scripts/lib/dispatch-resolver.mjs for the composition itself and the full error-code table.
|
|
17
|
+
//
|
|
18
|
+
// Agent files are resolved against THIS SCRIPT's own directory's parent (installRoot below), not
|
|
19
|
+
// the caller's project — a consuming project may have no agents/ directory of its own at all.
|
|
20
|
+
// `--agent` comes from a prompt, which may carry untrusted text, so the name is validated against
|
|
21
|
+
// a strict allowlist pattern before it ever touches a path (inside resolveDispatch()).
|
|
22
|
+
//
|
|
23
|
+
// Exit codes (callers branch on these — do not repurpose; scripts/lib/dispatch-resolver.mjs
|
|
24
|
+
// documents in full what produces each one, since the MCP server maps onto the identical four):
|
|
25
|
+
// 0 ok
|
|
26
|
+
// 64 usage error
|
|
27
|
+
// 65 unknown agent
|
|
28
|
+
// 66 malformed declaration
|
|
29
|
+
// 67 project environment/config failure
|
|
30
|
+
// A guessed model is never printed on any error path — only the success path emits JSON.
|
|
31
|
+
|
|
32
|
+
import path from 'node:path';
|
|
33
|
+
import { fileURLToPath } from 'node:url';
|
|
34
|
+
import { resolveDispatch } from './lib/dispatch-resolver.mjs';
|
|
35
|
+
|
|
36
|
+
const PROG = 'somi-dispatch';
|
|
37
|
+
const USAGE = `usage: ${PROG}.mjs resolve --agent <name> [--host <host>] [--ceiling <tier>]`;
|
|
38
|
+
|
|
39
|
+
// EXIT_* are declared here, literally, even though only EXIT_USAGE is READ elsewhere in this
|
|
40
|
+
// file: scripts/lib/dispatch-resolver.mjs owns the canonical values and returns the numeric code
|
|
41
|
+
// directly in its {ok:false, code, message} result, rather than this file re-deriving it from
|
|
42
|
+
// these names. tests/scripts/somi-dispatch.sh and tests/scripts/somi-agent-dispatch-contract.sh
|
|
43
|
+
// both grep this exact `^const EXIT_[A-Z_]+ = [0-9]+;` shape from THIS file to prove the four
|
|
44
|
+
// codes stay pairwise distinct and match what agents/somi.md documents — removing the unused
|
|
45
|
+
// three would break that contract test, so they stay, matched value-for-value against
|
|
46
|
+
// scripts/lib/dispatch-resolver.mjs's own declarations.
|
|
47
|
+
const EXIT_USAGE = 64;
|
|
48
|
+
const EXIT_UNKNOWN_AGENT = 65;
|
|
49
|
+
const EXIT_MALFORMED = 66;
|
|
50
|
+
const EXIT_PROJECT_ENV = 67;
|
|
51
|
+
|
|
52
|
+
class ExitSignal extends Error {
|
|
53
|
+
constructor(code) {
|
|
54
|
+
super(`exit ${code}`);
|
|
55
|
+
this.code = code;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function fail(code, msg) {
|
|
60
|
+
process.stderr.write(`${PROG}: ${msg}\n`);
|
|
61
|
+
throw new ExitSignal(code);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function die(msg) {
|
|
65
|
+
fail(EXIT_USAGE, `${msg}\n${USAGE}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// A missing (`--host` last) or explicitly empty (`--host ""`) value must fail here, at parse
|
|
69
|
+
// time -- not fall through to a `value || 'default'` downstream, which can't tell "not given"
|
|
70
|
+
// (correctly defaulted) from "given with nothing after it" (a caller error masquerading as one).
|
|
71
|
+
function requireValue(flag, value) {
|
|
72
|
+
if (value === undefined || value === '') die(`${flag} requires a non-empty value`);
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Matches scripts/somi-loop.mjs's projectRoot(): the caller's project, where .somi/config.json
|
|
77
|
+
// and .somi/somi-state/ceiling.json live. Deliberately NOT where agent files are read from —
|
|
78
|
+
// see installRoot() below.
|
|
79
|
+
function projectRoot() {
|
|
80
|
+
let b = process.env.CLAUDE_PROJECT_DIR || process.cwd();
|
|
81
|
+
if (b.includes('${')) b = process.cwd();
|
|
82
|
+
return b;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// SoMi's own install root: this file's directory's parent. A consuming project may have no
|
|
86
|
+
// agents/ of its own, so agent frontmatter is always read from where SoMi itself is installed,
|
|
87
|
+
// never from projectRoot().
|
|
88
|
+
function installRoot() {
|
|
89
|
+
return path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function main() {
|
|
93
|
+
const argv = process.argv.slice(2);
|
|
94
|
+
const CMD = argv[0] || '';
|
|
95
|
+
let rest = argv.slice(1);
|
|
96
|
+
|
|
97
|
+
// `undefined`, not `''`, is "not given" -- the same sentinel
|
|
98
|
+
// scripts/lib/dispatch-resolver.mjs's validateDispatchArgs() expects from every caller
|
|
99
|
+
// (scripts/somi-mcp.mjs's tool arguments use it natively; an omitted JSON field is `undefined`
|
|
100
|
+
// after parsing). `--host`/`--ceiling` can never reach `main()`'s end as `''`: requireValue()
|
|
101
|
+
// dies on an explicitly empty flag value before assignment, so only "never passed" survives to
|
|
102
|
+
// here, always as `undefined`.
|
|
103
|
+
let AGENT;
|
|
104
|
+
let HOST;
|
|
105
|
+
let CEILING;
|
|
106
|
+
|
|
107
|
+
while (rest.length > 0) {
|
|
108
|
+
const a = rest[0];
|
|
109
|
+
switch (a) {
|
|
110
|
+
case '--agent': AGENT = requireValue(a, rest[1]); rest = rest.slice(2); break;
|
|
111
|
+
case '--host': HOST = requireValue(a, rest[1]); rest = rest.slice(2); break;
|
|
112
|
+
case '--ceiling': CEILING = requireValue(a, rest[1]); rest = rest.slice(2); break;
|
|
113
|
+
default: die(`unknown argument: ${a}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (CMD !== 'resolve') die(CMD ? `unknown subcommand: ${CMD}` : 'a subcommand is required');
|
|
118
|
+
if (!AGENT) die('--agent is required');
|
|
119
|
+
|
|
120
|
+
const outcome = resolveDispatch({
|
|
121
|
+
agent: AGENT,
|
|
122
|
+
host: HOST,
|
|
123
|
+
ceiling: CEILING,
|
|
124
|
+
projectRoot: projectRoot(),
|
|
125
|
+
installRoot: installRoot(),
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
if (!outcome.ok) fail(outcome.code, outcome.message);
|
|
129
|
+
|
|
130
|
+
console.log(JSON.stringify(outcome.result));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
try {
|
|
134
|
+
main();
|
|
135
|
+
} catch (e) {
|
|
136
|
+
if (e instanceof ExitSignal) {
|
|
137
|
+
process.exitCode = e.code;
|
|
138
|
+
} else {
|
|
139
|
+
throw e;
|
|
140
|
+
}
|
|
141
|
+
}
|