@skathio/somi 4.0.0 → 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.
Files changed (163) hide show
  1. package/.claude/settings.json +1 -1
  2. package/.claude-plugin/marketplace.json +1 -1
  3. package/.claude-plugin/plugin.json +1 -1
  4. package/.copilot-extension/extension.json +1 -1
  5. package/.copilot-extension/marketplace.json +1 -1
  6. package/README.md +11 -5
  7. package/VERSION +1 -1
  8. package/agents/somi-coder.md +4 -228
  9. package/agents/somi-designer.md +4 -160
  10. package/agents/somi-discovery-analyst.md +4 -276
  11. package/agents/somi-flow.md +47 -0
  12. package/agents/somi-general.md +36 -0
  13. package/agents/somi-planner.md +4 -341
  14. package/agents/somi-reviewer.md +4 -199
  15. package/agents/somi.md +81 -253
  16. package/commands/adopt.md +6 -84
  17. package/commands/architecture-review.md +11 -86
  18. package/commands/atlas.md +10 -25
  19. package/commands/code-loop.md +10 -221
  20. package/commands/code-parallel.md +10 -151
  21. package/commands/code.md +9 -157
  22. package/commands/debug.md +9 -131
  23. package/commands/design.md +9 -160
  24. package/commands/discover.md +9 -194
  25. package/commands/impact.md +9 -25
  26. package/commands/incident.md +9 -39
  27. package/commands/plan-loop.md +9 -181
  28. package/commands/plan.md +9 -244
  29. package/commands/pr.md +10 -25
  30. package/commands/refactor-design.md +11 -89
  31. package/commands/refactor.md +11 -50
  32. package/commands/release-readiness.md +11 -70
  33. package/commands/review-panel.md +10 -141
  34. package/commands/review.md +11 -181
  35. package/commands/security-review.md +11 -75
  36. package/commands/ship-loop.md +9 -260
  37. package/commands/ship.md +9 -115
  38. package/commands/somi-config.md +12 -124
  39. package/commands/test-strategy.md +11 -75
  40. package/commands/upgrade.md +9 -72
  41. package/hooks/hooks.json +1 -1
  42. package/hooks/lib/common.mjs +15 -13
  43. package/hooks/pre-tool/block-dangerous-bash.mjs +1 -1
  44. package/hooks/pre-tool/block-secret-writes.mjs +5 -7
  45. package/hooks/pre-tool/guard-cost-lowering.mjs +26 -11
  46. package/hooks/pre-tool/guard-protected-paths.mjs +54 -34
  47. package/hooks/session-start/ensure-default-config.mjs +34 -42
  48. package/package.json +1 -1
  49. package/scripts/lib/config-files.mjs +145 -0
  50. package/scripts/lib/cost-ceiling.mjs +29 -22
  51. package/scripts/lib/cost-defaults.mjs +2 -0
  52. package/scripts/lib/escalation-facts.mjs +146 -0
  53. package/scripts/lib/escalation.mjs +129 -0
  54. package/scripts/lib/findings-ledger.mjs +241 -0
  55. package/scripts/lib/install-text.mjs +97 -0
  56. package/scripts/lib/loop-caps.mjs +140 -0
  57. package/scripts/lib/loop-diff.mjs +12 -70
  58. package/scripts/lib/loop-engine.mjs +226 -0
  59. package/scripts/lib/loop-escalate.mjs +49 -0
  60. package/scripts/lib/loop-exit.mjs +47 -0
  61. package/scripts/lib/loop-notes-compact.mjs +72 -0
  62. package/scripts/lib/loop-notes.mjs +278 -0
  63. package/scripts/lib/loop-review.mjs +355 -12
  64. package/scripts/lib/loop-state.mjs +44 -0
  65. package/scripts/lib/loop-step-close.mjs +75 -0
  66. package/scripts/lib/loop-step-kit.mjs +179 -0
  67. package/scripts/lib/loop-step-review.mjs +140 -0
  68. package/scripts/lib/loop-step-tool.mjs +89 -0
  69. package/scripts/lib/loop-step.mjs +301 -0
  70. package/scripts/lib/mcp-project-root.mjs +18 -7
  71. package/scripts/lib/path-facts.mjs +187 -0
  72. package/scripts/lib/path-safety.mjs +171 -0
  73. package/scripts/lib/run-card.mjs +73 -0
  74. package/scripts/size-budget-overrides.json +5 -13
  75. package/scripts/somi-card.mjs +39 -0
  76. package/scripts/somi-check.mjs +4 -3
  77. package/scripts/somi-config.mjs +113 -50
  78. package/scripts/somi-findings.mjs +56 -271
  79. package/scripts/somi-loop.mjs +77 -446
  80. package/scripts/somi-mcp.mjs +27 -96
  81. package/scripts/validate.sh +92 -54
  82. package/skills/flow-adopt/SKILL.md +92 -0
  83. package/skills/flow-architecture-review/SKILL.md +94 -0
  84. package/skills/flow-atlas/SKILL.md +34 -0
  85. package/skills/flow-code/SKILL.md +167 -0
  86. package/skills/flow-code-loop/SKILL.md +263 -0
  87. package/skills/flow-code-parallel/SKILL.md +160 -0
  88. package/skills/flow-debug/SKILL.md +141 -0
  89. package/skills/flow-design/SKILL.md +170 -0
  90. package/skills/flow-discover/SKILL.md +204 -0
  91. package/skills/flow-impact/SKILL.md +35 -0
  92. package/skills/flow-incident/SKILL.md +49 -0
  93. package/skills/flow-plan/SKILL.md +254 -0
  94. package/skills/flow-plan-loop/SKILL.md +254 -0
  95. package/skills/flow-pr/SKILL.md +34 -0
  96. package/skills/flow-refactor/SKILL.md +58 -0
  97. package/skills/flow-refactor-design/SKILL.md +97 -0
  98. package/skills/flow-release-readiness/SKILL.md +82 -0
  99. package/skills/flow-review/SKILL.md +193 -0
  100. package/skills/flow-review-panel/SKILL.md +154 -0
  101. package/skills/flow-security-review/SKILL.md +83 -0
  102. package/skills/flow-ship/SKILL.md +125 -0
  103. package/skills/flow-ship-loop/SKILL.md +274 -0
  104. package/skills/flow-somi-config/SKILL.md +142 -0
  105. package/skills/flow-status/SKILL.md +43 -0
  106. package/skills/flow-test-strategy/SKILL.md +83 -0
  107. package/skills/flow-upgrade/SKILL.md +82 -0
  108. package/skills/role-coder/SKILL.md +247 -0
  109. package/skills/role-designer/SKILL.md +172 -0
  110. package/skills/role-discovery-analyst/SKILL.md +288 -0
  111. package/skills/role-planner/SKILL.md +353 -0
  112. package/skills/role-reviewer/SKILL.md +218 -0
  113. package/skills/somi-checkpoint/SKILL.md +95 -0
  114. package/skills/somi-dispatch/SKILL.md +14 -14
  115. package/skills/somi-routing/SKILL.md +6 -2
  116. package/tests/hooks/cases/block-secret-writes.json +6 -1
  117. package/tests/hooks/cases/guard-protected-paths.json +44 -2
  118. package/tests/hooks/run.sh +21 -0
  119. package/tests/scripts/checkpoint-gate.sh +2 -2
  120. package/tests/scripts/cost-defaults.sh +2 -0
  121. package/tests/scripts/cost-hardcap.sh +26 -17
  122. package/tests/scripts/dispatch-line-gate.sh +4 -4
  123. package/tests/scripts/dispatch-reference-gate.sh +14 -14
  124. package/tests/scripts/ensure-default-config.sh +4 -4
  125. package/tests/scripts/escalation.sh +409 -0
  126. package/tests/scripts/eval-runner.sh +8 -7
  127. package/tests/scripts/fact-sheet.sh +296 -0
  128. package/tests/scripts/global-config.sh +167 -0
  129. package/tests/scripts/goldens/somi-loop.json +2 -2
  130. package/tests/scripts/lib/dispatch-reference-gate.mjs +14 -5
  131. package/tests/scripts/lib/escalation-facts-tests.mjs +140 -0
  132. package/tests/scripts/lib/escalation-tests.mjs +154 -0
  133. package/tests/scripts/lib/findings-ledger-tests.mjs +118 -0
  134. package/tests/scripts/lib/loop-engine-tests.mjs +287 -0
  135. package/tests/scripts/lib/loop-escalate-tests.mjs +356 -0
  136. package/tests/scripts/lib/loop-flow-gate.mjs +173 -0
  137. package/tests/scripts/lib/loop-notes-tests.mjs +371 -0
  138. package/tests/scripts/lib/loop-step-cli-tests.mjs +141 -0
  139. package/tests/scripts/lib/loop-step-close-tests.mjs +351 -0
  140. package/tests/scripts/lib/loop-step-lenient-tests.mjs +114 -0
  141. package/tests/scripts/lib/loop-step-state-tests.mjs +127 -0
  142. package/tests/scripts/lib/loop-step-tests.mjs +499 -0
  143. package/tests/scripts/lib/loop-step-unwritten-tests.mjs +55 -0
  144. package/tests/scripts/lib/path-facts-tests.mjs +198 -0
  145. package/tests/scripts/lib/path-safety-tests.mjs +201 -0
  146. package/tests/scripts/lib/run-suites.mjs +139 -0
  147. package/tests/scripts/lib/site-roadmap-gate.mjs +64 -0
  148. package/tests/scripts/lib/sizing-score-tests.mjs +210 -0
  149. package/tests/scripts/lib/sizing-set-tests.mjs +131 -0
  150. package/tests/scripts/lib/somi-mcp-loop-tests.mjs +226 -0
  151. package/tests/scripts/lib/somi-mcp-tests.mjs +21 -1
  152. package/tests/scripts/loop-engine.sh +22 -0
  153. package/tests/scripts/loop-flow-gate.sh +11 -0
  154. package/tests/scripts/review-panel-seats.sh +4 -4
  155. package/tests/scripts/run-card.sh +57 -0
  156. package/tests/scripts/run-suites.sh +74 -0
  157. package/tests/scripts/run.sh +11 -0
  158. package/tests/scripts/site-roadmap-gate.sh +76 -0
  159. package/tests/scripts/size-tier.sh +3 -3
  160. package/tests/scripts/somi-agent-dispatch-contract.sh +23 -3
  161. package/tests/scripts/somi-config.sh +9 -4
  162. package/tests/scripts/somi-mcp.sh +1 -0
  163. package/tests/scripts/suite-plan.json +20 -0
@@ -1,15 +1,20 @@
1
1
  #!/usr/bin/env node
2
- // somi-config.mjs — show and change the cost settings in a project's .somi/config.json.
2
+ // somi-config.mjs — show and change the cost settings in SoMi's config: the global file every project
3
+ // and session reads, and a project's .somi/config.json merged over it (scripts/lib/config-files.mjs).
3
4
  //
4
- // show one JSON line: where the file is, the ceiling, and every agent's
5
- // effective tier (the resolver's own rule: the agent's cost.agents entry,
6
- // else the ceiling), plus fixed warnings
5
+ // show one JSON line: both files, the ceiling, and every agent's effective
6
+ // tier (the resolver's own rule: the agent's cost.agents entry, else the
7
+ // ceiling) in the merged config, each with the file it came from, plus
8
+ // fixed warnings
7
9
  // get cost.ceiling | cost.agents.<name>
8
10
  // set cost.ceiling <low|medium|high> | set cost.agents.<name> <low|medium|high>
9
11
  // set cost.mapping.<host>.<tier> <model-id[,model-id...]> (a preference list, first offered wins)
10
12
  // unset cost.agents.<name> | unset cost.mapping.<host>
11
13
  // init create the default file when absent (the exact text the SessionStart
12
- // hook writes); a no-op if anything exists at the path
14
+ // hook writes); a no-op if anything exists at the path, and for the
15
+ // project file also when a global config exists (it holds the defaults)
16
+ // [--global] (set, unset, init) change the global file instead of the project's.
17
+ // For the user to run: the somi_config MCP tool never passes it
13
18
  // [--project-dir DIR] default: CLAUDE_PROJECT_DIR, else the current directory
14
19
  // [--host NAME] (show) the host this session runs on; adds a warning when that host has
15
20
  // no model mapping while any agent resolves to high (#36)
@@ -34,12 +39,15 @@
34
39
  // env call, else the built-in default (`effective_ceiling`), and warn when the variable or a
35
40
  // persisted value is why it differs from the config.
36
41
  //
37
- // Safety: the committed config is untrusted. It is read with the same bounded (64 KiB), no-follow,
38
- // regular-file reader the resolver uses, and the whole `cost` block is validated by the shared
39
- // validator before and after an edit. A write goes to a fresh temp file in the same .somi directory
40
- // (O_EXCL, no-follow) and is renamed over config.json, so a reader never sees half a file and nothing
41
- // is written through a link. A symlinked .somi or config.json is refused. Output is pure ASCII and no
42
- // message echoes text from the file (values are quoted and bounded).
42
+ // Safety: the committed config is untrusted. show and get read it as the resolver does (bounded to
43
+ // 64 KiB, regular files only, links followed so a junction or symlinked .somi works), and the whole
44
+ // `cost` block is validated by the shared validator before and after an edit. A project write goes to
45
+ // a fresh temp file in the same .somi directory (O_EXCL, no-follow) and is renamed over config.json, so
46
+ // a reader never sees half a file and nothing is written through a link: a symlinked .somi or
47
+ // config.json is refused for set, unset and init. The global file is the user's own: a write to it
48
+ // follows a link to the real file (a dotfiles setup keeps its link) and creates missing parent
49
+ // directories. A lowering is judged on the merged config, before and after. Output is pure ASCII and
50
+ // no message echoes text from a file (values are quoted and bounded).
43
51
  //
44
52
  // Exit codes (callers branch on these — do not repurpose):
45
53
  // 0 ok
@@ -51,6 +59,7 @@
51
59
  import fs from 'node:fs';
52
60
  import path from 'node:path';
53
61
  import { readTextFile, loadState, decideDispatch, DEFAULT_CEILING, MAX_CONFIG_BYTES } from './lib/cost-ceiling.mjs';
62
+ import { ConfigFileError, globalConfigPath, loadConfig, mergeConfig, readConfigText } from './lib/config-files.mjs';
54
63
  import { quote } from './lib/cost-config.mjs';
55
64
  import { VALID_COSTS, HOST_MODELS } from './lib/cost-model.mjs';
56
65
  import { AGENT_DEFAULT_TIERS, serializeDefaultConfig } from './lib/cost-defaults.mjs';
@@ -58,7 +67,7 @@ import { EditError, applyEdit, parseKey, validateCostBlock, missingMappingTiers
58
67
  import { lowers, REASONS } from './lib/cost-lowering.mjs';
59
68
 
60
69
  const PROG = 'somi-config';
61
- const USAGE = `usage: ${PROG}.mjs show [--host NAME] | get <key> | set <key> <value> | unset <key> | init [--project-dir DIR] [--refuse-lowering] (keys: cost.ceiling, cost.agents.<name>, cost.mapping.<host>.<tier> = model-id[,model-id...]; unset cost.agents.<name> or cost.mapping.<host>)`;
70
+ const USAGE = `usage: ${PROG}.mjs show [--host NAME] | get <key> | set <key> <value> | unset <key> | init [--project-dir DIR] [--global] [--refuse-lowering] (keys: cost.ceiling, cost.agents.<name>, cost.mapping.<host>.<tier> = model-id[,model-id...]; unset cost.agents.<name> or cost.mapping.<host>)`;
62
71
  const HOST_RE = /^[a-z][a-z0-9-]{0,31}$/;
63
72
  const EXIT_USAGE = 64;
64
73
  const EXIT_PROJECT = 67;
@@ -66,6 +75,7 @@ const EXIT_PROJECT = 67;
66
75
  const SEC_WARNING = 'somi-security-reviewer runs below high; pin it with: set cost.agents.somi-security-reviewer high';
67
76
  const ENV_WARNING = 'SOMI_COST_CEILING is set in this environment and outranks cost.ceiling in the config';
68
77
  const STATE_WARNING = 'a ceiling set earlier by a flag or SOMI_COST_CEILING is in force because cost.ceiling is not set in the config';
78
+ const GLOBAL_ONLY_WARNING = 'that value is set in your global config, not this project, so nothing changed here; change it with --global (run the CLI yourself)';
69
79
  const UNKNOWN_WARNING = 'that agent name is not a shipped agent; the entry is accepted but may never be used';
70
80
  const unmappedWarning = (host, n) => `${host} has no model mapping, so ${n} high-tier agent(s) run on whatever model the host picks; set cost.mapping.${host} with low, medium and high (docs/USAGE.md, "Pinning models per tier on Copilot")`;
71
81
  const MAPPING_MESSAGE = 'this change remaps a tier to a different model and needs the user: ask them to run node scripts/somi-config.mjs themselves or edit .somi/config.json';
@@ -89,9 +99,11 @@ function parseArgs(argv) {
89
99
  const pos = [];
90
100
  let dir;
91
101
  let refuse = false;
102
+ let global = false;
92
103
  let host;
93
104
  for (let i = 0; i < rest.length; i++) {
94
105
  if (rest[i] === '--refuse-lowering') refuse = true;
106
+ else if (rest[i] === '--global') global = true;
95
107
  else if (rest[i] === '--host') {
96
108
  if (host !== undefined) usage('--host given twice');
97
109
  host = rest[++i];
@@ -109,7 +121,8 @@ function parseArgs(argv) {
109
121
  if (pos.length !== want[cmd]) usage(`${cmd} takes ${want[cmd]} argument(s)`);
110
122
  if (refuse && cmd !== 'set' && cmd !== 'unset') usage('--refuse-lowering applies to set and unset only');
111
123
  if (host !== undefined && cmd !== 'show') usage('--host applies to show only');
112
- return { cmd, pos, dir, refuse, host };
124
+ if (global && !['set', 'unset', 'init'].includes(cmd)) usage('--global applies to set, unset and init');
125
+ return { cmd, pos, dir, refuse, host, global };
113
126
  }
114
127
 
115
128
  function projectRoot(dir) {
@@ -126,7 +139,8 @@ function projectRoot(dir) {
126
139
 
127
140
  const lstatOrNull = (p) => { try { return fs.lstatSync(p); } catch (e) { if (e.code === 'ENOENT') return null; throw e; } };
128
141
 
129
- // -> {somi, file, dirExists, fileExists}; refuses a .somi that is not a real directory.
142
+ // -> {somi, file, dirExists, fileExists}; refuses a .somi that is not a real directory. Writes only:
143
+ // show and get read through links.
130
144
  function locate(root) {
131
145
  const somi = path.join(root, '.somi');
132
146
  const file = path.join(somi, 'config.json');
@@ -171,12 +185,33 @@ function roundTripHazard(text) {
171
185
  return null;
172
186
  }
173
187
 
188
+ // The global file to write: its real path when it exists (a link is followed to it), and the stat of
189
+ // the directory it sits in, which writeAtomic re-checks. Parent directories are created when missing.
190
+ function locateGlobal() {
191
+ const g = globalConfigPath();
192
+ if (g === null) usage('there is no global config file: SOMI_GLOBAL_CONFIG is set to an empty value');
193
+ let file = g;
194
+ try { file = fs.realpathSync(g); } catch (e) { if (e.code !== 'ENOENT') project('the global config cannot be resolved'); }
195
+ const somi = path.dirname(file);
196
+ const fileExists = fs.existsSync(file);
197
+ return { somi, file, dirExists: fs.existsSync(somi), fileExists, global: true, shown: g };
198
+ }
199
+
200
+ // The merged config and both files, for reading; any problem with either file is 67.
201
+ function loadAll(root) {
202
+ try { return loadConfig(root); } catch (e) {
203
+ if (e instanceof ConfigFileError) return project(e.message);
204
+ throw e;
205
+ }
206
+ }
207
+
174
208
  // -> the parsed config object ({} when absent), with its whole cost block validated. With
175
- // `forWrite`, also refuses a file a rewrite could not reproduce faithfully.
209
+ // `forWrite`, also refuses a file a rewrite could not reproduce faithfully. A project file is read
210
+ // without following a link (it is about to be replaced); the global file through one.
176
211
  function load(root, loc, forWrite = false) {
177
212
  if (!loc.fileExists) return {};
178
213
  let text; let cfg;
179
- try { text = readTextFile(loc.file); } catch (e) { project(String(e.message).replace(/^cost-ceiling: /, '')); }
214
+ try { text = loc.global ? readConfigText(loc.file) : readTextFile(loc.file); } catch (e) { project(String(e.message).replace(/^cost-ceiling: /, '')); }
180
215
  try { cfg = JSON.parse(text); } catch { project('config.json is not valid JSON'); }
181
216
  if (cfg === null || typeof cfg !== 'object' || Array.isArray(cfg)) project('config.json is not a JSON object');
182
217
  if (cfg.ceiling !== undefined) project('"ceiling" at the top level of config.json belongs under "cost.ceiling"');
@@ -227,14 +262,19 @@ function warningsFor(cost, force, extra = []) {
227
262
  return w;
228
263
  }
229
264
 
230
- function show(root, loc, cfg, host) {
265
+ // Which file a merged cost value came from: the project file when it sets it, else the global one.
266
+ const fromFile = (files, get) => (get(files.project.config) !== undefined ? 'project' : 'global');
267
+
268
+ function show(root, files, host) {
269
+ const cfg = files.config;
231
270
  const cost = cfg.cost ?? {};
232
271
  const force = ceilingInForce(root, cost);
233
272
  const names = Object.keys(AGENT_DEFAULT_TIERS).sort();
234
273
  const extras = Object.keys(cost.agents ?? {}).filter((n) => !(n in AGENT_DEFAULT_TIERS)).sort();
235
274
  const agent = (name, unknown) => {
236
275
  const e = effective(name, cost, force.value);
237
- return { name, tier: e.tier, source: e.source, default: unknown ? null : AGENT_DEFAULT_TIERS[name], ...(unknown ? { unknown: true } : {}) };
276
+ const from = e.source === 'override' ? { from: fromFile(files, (c) => c.cost?.agents?.[name]) } : {};
277
+ return { name, tier: e.tier, source: e.source, ...from, default: unknown ? null : AGENT_DEFAULT_TIERS[name], ...(unknown ? { unknown: true } : {}) };
238
278
  };
239
279
  const agents = [...names.map((n) => agent(n, false)), ...extras.map((n) => agent(n, true))];
240
280
  const extra = [];
@@ -243,8 +283,9 @@ function show(root, loc, cfg, host) {
243
283
  if (host !== undefined && !mapped && highs > 0) extra.push(unmappedWarning(host, highs));
244
284
  return {
245
285
  path: path.join('.somi', 'config.json'),
246
- exists: loc.fileExists,
247
- ceiling: cost.ceiling === undefined ? { value: DEFAULT_CEILING, source: 'default' } : { value: cost.ceiling, source: 'config' },
286
+ exists: files.project.present,
287
+ global: { path: files.global.path, exists: files.global.present },
288
+ ceiling: cost.ceiling === undefined ? { value: DEFAULT_CEILING, source: 'default' } : { value: cost.ceiling, source: 'config', from: fromFile(files, (c) => c.cost?.ceiling) },
248
289
  effective_ceiling: force,
249
290
  agents,
250
291
  warnings: warningsFor(cost, force, extra),
@@ -255,6 +296,11 @@ function show(root, loc, cfg, host) {
255
296
  const edit = (fn) => { try { return fn(); } catch (e) { if (e instanceof EditError) return usage(e.message); throw e; } };
256
297
 
257
298
  function ensureSomi(loc) {
299
+ if (loc.global) {
300
+ try { fs.mkdirSync(loc.somi, { recursive: true, mode: 0o700 }); loc.dirStat = fs.statSync(loc.somi); } catch { project('the global config directory could not be created'); }
301
+ if (!loc.dirStat.isDirectory()) project('the global config directory is not a directory');
302
+ return;
303
+ }
258
304
  if (!loc.dirExists) {
259
305
  try { fs.mkdirSync(loc.somi, { mode: 0o700 }); } catch (e) {
260
306
  if (e.code !== 'EEXIST' || !lstatOrNull(loc.somi)?.isDirectory()) project('.somi could not be created');
@@ -278,8 +324,8 @@ function writeAtomic(loc, text, { noClobber = false, mode } = {}) {
278
324
  if (mode !== undefined) fs.fchmodSync(fd, mode);
279
325
  fs.fsyncSync(fd);
280
326
  fs.closeSync(fd); fd = undefined;
281
- const now = lstatOrNull(loc.somi);
282
- if (!now || !now.isDirectory() || now.dev !== loc.dirStat.dev || now.ino !== loc.dirStat.ino) project('.somi changed while writing; nothing was replaced');
327
+ const now = loc.global ? fs.statSync(loc.somi, { throwIfNoEntry: false }) : lstatOrNull(loc.somi);
328
+ if (!now || !now.isDirectory() || now.dev !== loc.dirStat.dev || now.ino !== loc.dirStat.ino) project(`${loc.global ? 'the global config directory' : '.somi'} changed while writing; nothing was replaced`);
283
329
  if (noClobber) fs.linkSync(tmp, loc.file); else fs.renameSync(tmp, loc.file);
284
330
  return true;
285
331
  } catch (e) {
@@ -293,60 +339,77 @@ function writeAtomic(loc, text, { noClobber = false, mode } = {}) {
293
339
  }
294
340
 
295
341
  function main() {
296
- const { cmd, pos, dir, refuse, host } = parseArgs(process.argv.slice(2));
342
+ const { cmd, pos, dir, refuse, host, global } = parseArgs(process.argv.slice(2));
297
343
  const root = projectRoot(dir);
298
- const loc = locate(root);
299
- const rel = path.join('.somi', 'config.json');
344
+ const writes = cmd === 'set' || cmd === 'unset' || cmd === 'init';
345
+ // Only a write inspects the target's links; show and get read through them.
346
+ const loc = !writes ? null : (global ? locateGlobal() : locate(root));
347
+ const rel = global ? loc.shown : path.join('.somi', 'config.json');
300
348
 
301
349
  if (cmd === 'init') {
302
350
  if (loc.fileExists) return { path: rel, created: false };
351
+ const g = global ? null : globalConfigPath();
352
+ if (g !== null && fs.lstatSync(g, { throwIfNoEntry: false })) return { path: rel, created: false, reason: 'a global config exists and holds the defaults' };
303
353
  ensureSomi(loc);
304
354
  return { path: rel, created: writeAtomic(loc, serializeDefaultConfig(), { noClobber: true }) };
305
355
  }
306
356
 
357
+ const files = loadAll(root);
307
358
  const k = cmd === 'show' ? null : edit(() => parseKey(pos[0]));
308
359
  if (cmd === 'unset' && !(k.kind === 'agent' || (k.kind === 'mapping' && k.tier === undefined))) usage('only cost.agents.<name> and cost.mapping.<host> can be unset');
309
- const cfg = load(root, loc, cmd === 'set' || cmd === 'unset');
310
- if (cmd === 'show') return show(root, loc, cfg, host);
360
+ if (files.config.ceiling !== undefined) project('"ceiling" at the top level of config.json belongs under "cost.ceiling"');
361
+ checkCost(files.config.cost, project);
362
+ if (cmd === 'show') return show(root, files, host);
311
363
 
312
364
  if (cmd === 'get') {
313
- const force = ceilingInForce(root, cfg.cost);
314
- if (k.kind === 'ceiling') return { key: pos[0], value: cfg.cost?.ceiling ?? DEFAULT_CEILING, source: cfg.cost?.ceiling === undefined ? 'default' : 'config', effective_ceiling: force };
365
+ const merged = files.config.cost;
366
+ const force = ceilingInForce(root, merged);
367
+ if (k.kind === 'ceiling') return { key: pos[0], value: merged?.ceiling ?? DEFAULT_CEILING, source: merged?.ceiling === undefined ? 'default' : 'config', effective_ceiling: force };
315
368
  if (k.kind === 'mapping') {
316
- const hostMap = cfg.cost?.mapping?.[k.host];
369
+ const hostMap = merged?.mapping?.[k.host];
317
370
  const value = hostMap === undefined ? null : (k.tier === undefined ? hostMap : (hostMap[k.tier] ?? null));
318
371
  return { key: pos[0], value, source: value === null ? 'none' : 'config' };
319
372
  }
320
- const has = cfg.cost?.agents && Object.hasOwn(cfg.cost.agents, k.name);
321
- return { key: pos[0], value: has ? cfg.cost.agents[k.name] : null, source: has ? 'override' : 'none' };
373
+ const has = merged?.agents && Object.hasOwn(merged.agents, k.name);
374
+ return { key: pos[0], value: has ? merged.agents[k.name] : null, source: has ? 'override' : 'none' };
322
375
  }
323
376
 
324
- // set/unset. A set on an absent file starts from the default file, so the default pins exist.
377
+ // set/unset edit one file. A set on an absent file starts from the default file, so the default
378
+ // pins exist -- except a project file over a global config, which starts empty: the global file
379
+ // holds the defaults, and copies of them in the project would mask later global changes.
380
+ const cfg = load(root, loc, true);
325
381
  const absent = !loc.fileExists;
326
- const beforeCfg = { cost: JSON.parse(JSON.stringify(cfg.cost ?? null)) };
327
- const { next, changed } = edit(() => applyEdit(absent ? null : cfg, { action: cmd, key: pos[0], value: pos[1] }));
328
- if (!changed) return { path: rel, key: pos[0], changed: false, warnings: warningsFor(cfg.cost, ceilingInForce(root, cfg.cost)) };
382
+ const start = absent && !global && files.global.present ? {} : (absent ? null : cfg);
383
+ // What the resolver sees, before and after: a project edit is judged merged over the global file;
384
+ // a global edit on its own, since it applies to projects whose files differ.
385
+ const view = (c) => (global ? c : mergeConfig(files.global.config, c ?? {}));
386
+ const beforeCfg = view(absent ? {} : cfg);
387
+ const { next, changed } = edit(() => applyEdit(start, { action: cmd, key: pos[0], value: pos[1] }));
388
+ if (!changed) {
389
+ // Nothing to unset in the project, but the same unset would change the global file: say where it lives.
390
+ const inGlobalOnly = !global && cmd === 'unset' && edit(() => applyEdit(files.global.config, { action: cmd, key: pos[0] })).changed;
391
+ return { path: rel, key: pos[0], changed: false, warnings: warningsFor(beforeCfg.cost, ceilingInForce(root, beforeCfg.cost), inGlobalOnly ? [GLOBAL_ONLY_WARNING] : []) };
392
+ }
329
393
  checkCost(next.cost, (m) => usage(m)); // the whole resulting block; a failure here is the caller's input
394
+ const after = view(next);
330
395
  if (refuse) {
331
- const v = lowers(beforeCfg, next, root);
396
+ const v = lowers(beforeCfg, after, root);
332
397
  if (v.lowers) usage(v.reasons.every((r) => r === REASONS.mapping) ? MAPPING_MESSAGE : LOWERING_MESSAGE);
333
398
  }
334
399
  const value = cmd !== 'set' ? undefined : (k.kind === 'mapping' ? next.cost.mapping[k.host][k.tier] : pos[1]);
335
400
  const extra = k.kind === 'agent' && cmd === 'set' && !(k.name in AGENT_DEFAULT_TIERS) && k.name !== 'somi' ? [UNKNOWN_WARNING] : [];
336
- if (changed) {
337
- let text;
338
- try {
339
- text = `${JSON.stringify(next, null, 2)}\n`;
340
- if (JSON.stringify(JSON.parse(text)) !== JSON.stringify(next)) project('config.json could not be rewritten faithfully; nothing was written');
341
- } catch (e) {
342
- if (e instanceof ExitSignal) throw e;
343
- project('config.json is too deeply nested to rewrite');
344
- }
345
- if (Buffer.byteLength(text) > MAX_CONFIG_BYTES) project('the edited config.json would exceed the size limit; nothing was written');
346
- ensureSomi(loc);
347
- writeAtomic(loc, text, { mode: loc.fileExists ? (fs.lstatSync(loc.file).mode & 0o777) : undefined });
401
+ let text;
402
+ try {
403
+ text = `${JSON.stringify(next, null, 2)}\n`;
404
+ if (JSON.stringify(JSON.parse(text)) !== JSON.stringify(next)) project('config.json could not be rewritten faithfully; nothing was written');
405
+ } catch (e) {
406
+ if (e instanceof ExitSignal) throw e;
407
+ project('config.json is too deeply nested to rewrite');
348
408
  }
349
- return { path: rel, key: pos[0], ...(cmd === 'set' ? { value } : {}), changed, effective_ceiling: ceilingInForce(root, next.cost), warnings: warningsFor(next.cost, ceilingInForce(root, next.cost), extra) };
409
+ if (Buffer.byteLength(text) > MAX_CONFIG_BYTES) project('the edited config.json would exceed the size limit; nothing was written');
410
+ ensureSomi(loc);
411
+ writeAtomic(loc, text, { mode: loc.fileExists ? (fs.statSync(loc.file).mode & 0o777) : undefined });
412
+ return { path: rel, key: pos[0], ...(cmd === 'set' ? { value } : {}), changed, effective_ceiling: ceilingInForce(root, after.cost), warnings: warningsFor(after.cost, ceilingInForce(root, after.cost), extra) };
350
413
  }
351
414
 
352
415
  try {