@skathio/somi 4.0.0 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -129,14 +129,14 @@ check "an unexpanded \${...} root falls back to the working directory (no litera
129
129
  # --- red proofs: each safety check removed from a copy must be caught -----------------------------
130
130
  mutant() { # $1 = name, $2 = sed expression -> $TMP/$1/hooks/session-start/ensure-default-config.mjs (a copy inside a plugin-shaped tree)
131
131
  local d="$TMP/$1"; mkdir -p "$d/hooks/session-start" "$d/hooks/lib" "$d/scripts/lib"
132
- cp "$ROOT_REPO/hooks/lib/common.mjs" "$d/hooks/lib/"; cp "$ROOT_REPO"/scripts/lib/cost-*.mjs "$d/scripts/lib/"
132
+ cp "$ROOT_REPO/hooks/lib/common.mjs" "$d/hooks/lib/"; cp "$ROOT_REPO"/scripts/lib/cost-*.mjs "$ROOT_REPO/scripts/lib/config-files.mjs" "$d/scripts/lib/"
133
133
  sed "$2" "$HOOK_SRC" > "$d/hooks/session-start/ensure-default-config.mjs"
134
134
  cmp -s "$HOOK_SRC" "$d/hooks/session-start/ensure-default-config.mjs" && bad "mutant $1 did not change the hook"
135
135
  }
136
136
  mutant no-excl "s/ | fs.constants.O_EXCL//"
137
137
  mutant no-nofollow "s/ | (fs.constants.O_NOFOLLOW ?? 0)//"
138
138
  mutant no-somi-check "s/if (dir \&\& !dir.isDirectory()) return;//"
139
- mutant overwrite "s/if (existing) {/if (false) {/; s/ | fs.constants.O_EXCL/ | fs.constants.O_TRUNC/"
139
+ mutant overwrite "s/if (existing || hasGlobal) {/if (false) {/; s/ | fs.constants.O_EXCL/ | fs.constants.O_TRUNC/"
140
140
  M="$TMP/no-somi-check/hooks/session-start/ensure-default-config.mjs"
141
141
  P="$(proj)"; OUTSIDE="$(mktemp -d -p "$TMP")"; ln -s "$OUTSIDE" "$P/.somi"; runhook "$M" "$P" > /dev/null
142
142
  check "red proof: without the .somi check a symlinked .somi is written through" "$([ -e "$OUTSIDE/config.json" ] && echo written || echo clean)" "written"
@@ -146,10 +146,10 @@ check "red proof: without the exists-check and O_EXCL an existing file is overwr
146
146
  M="$TMP/no-excl/hooks/session-start/ensure-default-config.mjs"
147
147
  P="$(proj)"; mkdir "$P/.somi"; printf 'OLD-CONTENT-LONGER-THAN-NOTHING' > "$P/.somi/config.json"
148
148
  # the exists-check still stops it, so remove that too: only O_EXCL is the last line of defence against a race
149
- sed -i 's/if (existing) {/if (false) {/' "$M"; runhook "$M" "$P" > /dev/null
149
+ sed -i 's/if (existing || hasGlobal) {/if (false) {/' "$M"; runhook "$M" "$P" > /dev/null
150
150
  check "red proof: without O_EXCL (and the early check) an existing file is rewritten" "$(cmp -s "$P/.somi/config.json" "$EXPECT" && echo overwritten || echo kept)" "overwritten"
151
151
  M="$TMP/no-nofollow/hooks/session-start/ensure-default-config.mjs"
152
- sed -i 's/if (existing) {/if (false) {/; s/ | fs.constants.O_EXCL//' "$M"
152
+ sed -i 's/if (existing || hasGlobal) {/if (false) {/; s/ | fs.constants.O_EXCL//' "$M"
153
153
  P="$(proj)"; mkdir "$P/.somi"; echo KEEP2 > "$TMP/victim2.txt"; ln -s "$TMP/victim2.txt" "$P/.somi/config.json"; runhook "$M" "$P" > /dev/null
154
154
  check "red proof: without O_NOFOLLOW (and O_EXCL, early check) a symlink at config.json is written through" "$([ "$(cat "$TMP/victim2.txt")" = KEEP2 ] && echo kept || echo written)" "written"
155
155
 
@@ -0,0 +1,167 @@
1
+ #!/usr/bin/env bash
2
+ # Guard for the global config and the merge (scripts/lib/config-files.mjs) and for reads through links:
3
+ # the loader on its own, then every consumer that must see the merged config -- the resolver, the
4
+ # somi-config CLI, the SessionStart default-config hook, the cost-lowering hook and the hooks' config().
5
+ # Every case sets SOMI_GLOBAL_CONFIG itself, so a real global config on the machine never leaks in.
6
+ set -uo pipefail
7
+
8
+ ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
9
+ LIB="$ROOT/scripts/lib/config-files.mjs"
10
+ CLI="$ROOT/scripts/somi-config.mjs"
11
+ DISPATCH="$ROOT/scripts/somi-dispatch.mjs"
12
+ ENSURE="$ROOT/hooks/session-start/ensure-default-config.mjs"
13
+ GUARD="$ROOT/hooks/pre-tool/guard-cost-lowering.mjs"
14
+
15
+ pass=0; fail=0
16
+ ok() { pass=$((pass+1)); printf ' ok %s\n' "$1"; }
17
+ bad() { fail=$((fail+1)); printf ' FAIL %s\n' "$1"; }
18
+ check() { if [ "$2" = "$3" ]; then ok "$1"; else bad "$1 (want '$3', got '$2')"; fi; }
19
+
20
+ TMP="$(mktemp -d)"
21
+ trap 'rm -rf "$TMP"' EXIT
22
+ proj() { mktemp -d -p "$TMP"; }
23
+ node_lib() { SOMI_GLOBAL_CONFIG="${G:-}" node --input-type=module -e "import * as L from '$LIB'; $1"; }
24
+
25
+ echo "== global config: the loader =="
26
+
27
+ check "SOMI_GLOBAL_CONFIG names the file" "$(SOMI_GLOBAL_CONFIG=/x/y.json node --input-type=module -e "import {globalConfigPath} from '$LIB'; process.stdout.write(String(globalConfigPath()))")" "/x/y.json"
28
+ check "an empty SOMI_GLOBAL_CONFIG means no global config" "$(SOMI_GLOBAL_CONFIG= node --input-type=module -e "import {globalConfigPath} from '$LIB'; process.stdout.write(String(globalConfigPath()))")" "null"
29
+ check "platform defaults: XDG, ~/.config, %APPDATA%" "$(node --input-type=module -e "
30
+ import {globalConfigPath as g} from '$LIB';
31
+ process.stdout.write([g({XDG_CONFIG_HOME:'/xdg'},'linux','/h'), g({XDG_CONFIG_HOME:'rel'},'linux','/h'), g({},'darwin','/h'), g({APPDATA:'C:/A'},'win32','C:/h')].join('|'))")" \
32
+ "/xdg/somi/config.json|/h/.config/somi/config.json|/h/.config/somi/config.json|C:/A/somi/config.json"
33
+
34
+ check "merge: the user's example (project changes only somi-coder and the medium mapping)" "$(node_lib "
35
+ const g = {cost:{ceiling:'medium',agents:{'somi-coder':'medium','somi-reviewer':'high'},mapping:{'copilot-cli':{low:['a'],medium:['b','c'],high:['d']}}},dep_install:{allow:['x']}};
36
+ const p = {cost:{agents:{'somi-coder':'high'},mapping:{'copilot-cli':{medium:['z']}}}};
37
+ const m = L.mergeConfig(g, p);
38
+ process.stdout.write(JSON.stringify(m) + '|' + g.cost.agents['somi-coder'] + '|' + JSON.stringify(p))")" \
39
+ '{"cost":{"ceiling":"medium","agents":{"somi-coder":"high","somi-reviewer":"high"},"mapping":{"copilot-cli":{"low":["a"],"medium":["z"],"high":["d"]}}},"dep_install":{"allow":["x"]}}|medium|{"cost":{"agents":{"somi-coder":"high"},"mapping":{"copilot-cli":{"medium":["z"]}}}}'
40
+ check "merge: arrays, scalars and null replace; objects merge" "$(node_lib "
41
+ process.stdout.write(JSON.stringify(L.mergeConfig({a:[1,2],b:{c:1,d:2},e:'x',f:{g:1}}, {a:[3],b:{d:3},e:null,f:5})))")" \
42
+ '{"a":[3],"b":{"c":1,"d":3},"e":null,"f":5}'
43
+ check "merge: a __proto__ key stays data and pollutes nothing" "$(node_lib "
44
+ const m = L.mergeConfig({}, JSON.parse('{\"__proto__\":{\"polluted\":1},\"cost\":{\"__proto__\":{\"x\":1}}}'));
45
+ process.stdout.write([String(({}).polluted), Object.getPrototypeOf(m) === Object.prototype, Object.hasOwn(m, '__proto__'), Object.getPrototypeOf(m.cost) === Object.prototype].join(','))")" \
46
+ "undefined,true,true,true"
47
+
48
+ P="$(proj)"; mkdir "$P/.somi"
49
+ check "absent files read as unconfigured" "$(G="$TMP/none.json" node_lib "const r = L.loadConfig('$P'); process.stdout.write(JSON.stringify([r.config, r.global.present, r.project.present]))")" '[{},false,false]'
50
+ printf '{"cost":{"ceiling":"high"}}' > "$TMP/g1.json"; printf '{"cost":{"agents":{"somi-coder":"low"}}}' > "$P/.somi/config.json"
51
+ check "global and project merge, each file reported" "$(G="$TMP/g1.json" node_lib "const r = L.loadConfig('$P'); process.stdout.write(JSON.stringify([r.config, r.global.present, r.project.present]))")" \
52
+ '[{"cost":{"ceiling":"high","agents":{"somi-coder":"low"}}},true,true]'
53
+ P="$(proj)"; SHARED="$(mktemp -d -p "$TMP")"; printf '{"cost":{"ceiling":"low"}}' > "$SHARED/config.json"; ln -s "$SHARED" "$P/.somi"
54
+ check "a symlinked .somi (a junction on Windows) is read through" "$(G= node_lib "process.stdout.write(L.loadConfig('$P').config.cost.ceiling)")" "low"
55
+ ln -s "$SHARED/config.json" "$TMP/linked.json"
56
+ check "a symlinked global config is read through" "$(G="$TMP/linked.json" node_lib "process.stdout.write(L.loadConfig('$(proj)').config.cost.ceiling)")" "low"
57
+ ln -s "$TMP/gone.json" "$TMP/dangling.json"
58
+ check "a dangling link is an error, not 'no config'" "$(G="$TMP/dangling.json" node_lib "try { L.loadConfig('$(proj)'); console.log('read'); } catch (e) { console.log(e instanceof L.ConfigFileError && /link to a missing file/.test(e.message) ? 'refused' : e.message); }")" "refused"
59
+ mkdir "$TMP/dir.json"
60
+ check "a directory is not a config" "$(G="$TMP/dir.json" node_lib "try { L.loadConfig('$(proj)'); console.log('read'); } catch (e) { console.log(/not a regular file/.test(e.message) ? 'refused' : e.message); }")" "refused"
61
+ mkfifo "$TMP/fifo.json"; rc=0
62
+ out="$(G="$TMP/fifo.json" timeout 5 node --input-type=module -e "import * as L from '$LIB'; process.env.SOMI_GLOBAL_CONFIG='$TMP/fifo.json'; try { L.loadConfig('$(proj)'); console.log('read'); } catch (e) { console.log(/not a regular file/.test(e.message) ? 'refused' : e.message); }")" || rc=$?
63
+ check "a FIFO is refused without blocking" "$rc|$out" "0|refused"
64
+ { printf '{"pad":"'; head -c 70000 /dev/zero | tr '\0' x; printf '"}'; } > "$TMP/big.json"
65
+ check "an oversize global config is refused" "$(G="$TMP/big.json" node_lib "try { L.loadConfig('$(proj)'); console.log('read'); } catch (e) { console.log(/byte limit/.test(e.message) ? 'refused' : e.message); }")" "refused"
66
+ printf '\x1b[31mSECRET {nope' > "$TMP/bad.json"
67
+ msg="$(G="$TMP/bad.json" node_lib "try { L.loadConfig('$(proj)'); } catch (e) { console.log(e.message); }")"
68
+ case "$msg" in *SECRET*|*$'\x1b'*) bad "a parse error leaks file text: $msg" ;; *"not valid JSON"*) ok "a parse error names the file and leaks no text" ;; *) bad "unexpected parse error: $msg" ;; esac
69
+
70
+ node -e "const n=20000; require('fs').writeFileSync('$TMP/deep.json', '{\"deep\":'+'['.repeat(n)+']'.repeat(n)+'}')"
71
+ out="$(SOMI_GLOBAL_CONFIG="$TMP/deep.json" node --stack-size=200 --input-type=module -e "import * as L from '$LIB'; try { L.loadConfig('$(proj)'); console.log('read'); } catch (e) { console.log(e instanceof L.ConfigFileError && /nested deeper/.test(e.message) ? 'refused' : 'crash'); }" 2>&1)"
72
+ check "a config nested 20000 deep is refused cleanly, even on a small stack (CI's Node)" "$out" "refused"
73
+
74
+ echo "== global config: the resolver =="
75
+ resolve() { # $1 = global file, $2 = project, $3.. = resolve args -> stdout JSON
76
+ local g="$1" p="$2"; shift 2
77
+ (cd "$p" && SOMI_GLOBAL_CONFIG="$g" CLAUDE_PROJECT_DIR="$p" env -u SOMI_COST_CEILING node "$DISPATCH" resolve "$@" 2>&1)
78
+ }
79
+ jget() { node -e "const o = JSON.parse(require('fs').readFileSync(0,'utf8')); process.stdout.write(String($1))"; }
80
+ GF="$TMP/global.json"
81
+ printf '%s' '{"cost":{"ceiling":"medium","agents":{"somi-coder":"medium","somi-planner":"high"},"mapping":{"copilot-cli":{"low":["g-low"],"medium":["g-med"],"high":["g-high"]}}}}' > "$GF"
82
+ P="$(proj)"; mkdir "$P/.somi"; printf '%s' '{"cost":{"agents":{"somi-coder":"high"},"mapping":{"copilot-cli":{"medium":["p-med"]}}}}' > "$P/.somi/config.json"
83
+ check "the project overrides somi-coder" "$(resolve "$GF" "$P" --agent somi-coder --host copilot-cli | jget 'o.tier + "," + o.tier_source + "," + o.model')" "high,override,g-high"
84
+ check "an agent the project does not name keeps the global pin" "$(resolve "$GF" "$P" --agent somi-planner --host copilot-cli | jget 'o.tier + "," + o.model')" "high,g-high"
85
+ check "the project's medium mapping replaces the global one; other tiers stay" "$(resolve "$GF" "$P" --agent somi-impact --host copilot-cli | jget 'o.tier + "," + o.model')" "medium,p-med"
86
+ check "the ceiling comes from the global file" "$(resolve "$GF" "$P" --agent somi-impact --host copilot-cli | jget 'o.ceiling + "," + o.ceiling_source')" "medium,config"
87
+ printf '%s' '{"cost":{"celing":"low"}}' > "$TMP/typo.json"; rc=0
88
+ resolve "$TMP/typo.json" "$(proj)" --agent somi-coder > "$TMP/typo.out" || rc=$?
89
+ check "a typo in the global file exits 67 and names that file" "$rc|$(grep -c 'typo.json' "$TMP/typo.out")" "67|1"
90
+
91
+ echo "== global config: somi-config =="
92
+ cfg() { # $1 = global file, $2 = project, $3.. = args -> exit code; stdout in $TMP/out
93
+ local g="$1" p="$2"; shift 2
94
+ SOMI_GLOBAL_CONFIG="$g" env -u SOMI_COST_CEILING node "$CLI" "$@" --project-dir "$p" > "$TMP/out" 2> "$TMP/err"; echo $?
95
+ }
96
+ jf() { node -e "const o = JSON.parse(require('fs').readFileSync('$TMP/out','utf8')); process.stdout.write(typeof ($1) === 'string' ? ($1) : JSON.stringify($1))"; }
97
+ check "show merges both files and says where each value came from" \
98
+ "$(cfg "$GF" "$P" show)|$(jf 'o.global.exists')|$(jf 'o.ceiling.from')|$(jf 'o.agents.find((a) => a.name === "somi-coder").from')|$(jf 'o.agents.find((a) => a.name === "somi-planner").from')" \
99
+ "0|true|global|project|global"
100
+ check "get reads the merged value" "$(cfg "$GF" "$P" get cost.mapping.copilot-cli.medium)|$(jf 'o.value')" '0|["p-med"]'
101
+
102
+ H="$TMP/home"; NG="$H/.config/somi/config.json"
103
+ check "init --global creates the default global file, parents included" "$(cfg "$NG" "$(proj)" init --global)|$(jf 'o.created')|$([ -f "$NG" ] && echo file)" "0|true|file"
104
+ P="$(proj)"
105
+ check "init in a project is a no-op while a global config exists" "$(cfg "$NG" "$P" init)|$(jf 'o.created')|$([ -e "$P/.somi/config.json" ] && echo made || echo none)" "0|false|none"
106
+ check "set --global changes the global file" "$(cfg "$NG" "$P" set cost.agents.somi-coder high --global)|$(node -e "process.stdout.write(JSON.parse(require('fs').readFileSync('$NG','utf8')).cost.agents['somi-coder'])")" "0|high"
107
+ check "a first project set over a global config writes only that key" "$(cfg "$NG" "$P" set cost.agents.somi-pr high)|$(cat "$P/.somi/config.json" | tr -d ' \n')" '0|{"cost":{"agents":{"somi-pr":"high"}}}'
108
+ check "unset of a value only the global file sets changes nothing and says where it lives" "$(cfg "$NG" "$P" unset cost.agents.somi-coder)|$(jf 'o.changed')|$(jf 'String(o.warnings[0].startsWith("that value is set in your global config"))')" "0|false|true"
109
+ check "--global is refused on show" "$(cfg "$NG" "$P" show --global)" "64"
110
+ check "--refuse-lowering judges a project change merged over the global file" "$(cfg "$NG" "$P" set cost.agents.somi-coder low --refuse-lowering)|$(cat "$P/.somi/config.json" | tr -d ' \n')" '64|{"cost":{"agents":{"somi-pr":"high"}}}'
111
+ DOT="$TMP/dotfiles"; mkdir -p "$DOT" "$H/.config/linked"; printf '{"cost":{"ceiling":"medium"}}' > "$DOT/somi.json"; ln -s "$DOT/somi.json" "$H/.config/linked/config.json"
112
+ check "set --global through a dotfiles link writes the real file and keeps the link" \
113
+ "$(cfg "$H/.config/linked/config.json" "$P" set cost.ceiling high --global)|$([ -L "$H/.config/linked/config.json" ] && echo link)|$(node -e "process.stdout.write(JSON.parse(require('fs').readFileSync('$DOT/somi.json','utf8')).cost.ceiling)")" \
114
+ "0|link|high"
115
+ check "with SOMI_GLOBAL_CONFIG empty, --global is a usage error" "$(cfg "" "$P" set cost.ceiling high --global)" "64"
116
+
117
+ echo "== global config: hooks =="
118
+ runhook() { # $1 = hook, $2 = global file, $3 = project; payload on stdin -> stdout
119
+ (cd "$3" && SOMI_GLOBAL_CONFIG="$2" CLAUDE_PROJECT_DIR="$3" node "$1")
120
+ }
121
+ P="$(proj)"
122
+ out="$(printf '{}' | runhook "$ENSURE" "$NG" "$P")"
123
+ check "SessionStart writes no project default while a global config exists" "$([ -e "$P/.somi/config.json" ] && echo made || echo none)" "none"
124
+ printf '{"cost":{"ceiling":"medium","agents":{}}}' > "$TMP/nopin.json"
125
+ out="$(printf '{}' | runhook "$ENSURE" "$TMP/nopin.json" "$(proj)")"
126
+ case "$out" in *"your global config"*"no cost.agents pin for somi-security-reviewer"*) ok "the pin notice reads the merged config and names the global file" ;; *) bad "the pin notice reads the merged config and names the global file ($out)" ;; esac
127
+ P="$(proj)"; mkdir "$P/.somi"; printf '{"cost":{"agents":{"somi-security-reviewer":"high"}}}' > "$P/.somi/config.json"
128
+ check "... and a project pin satisfies it" "$(printf '{}' | runhook "$ENSURE" "$TMP/nopin.json" "$P")" ""
129
+
130
+ verdict() { # $1 = global file, $2 = project, $3 = payload -> ask | allow
131
+ local out; out="$(printf '%s' "$3" | runhook "$GUARD" "$1" "$2")"
132
+ case "$out" in *'"permissionDecision":"ask"'*) echo ask ;; *) echo allow ;; esac
133
+ }
134
+ wr() { node -e "process.stdout.write(JSON.stringify({tool_name:'Write',tool_input:{file_path:process.argv[1],content:process.argv[2]}}))" "$1" "$2"; }
135
+ GP="$TMP/gp.json"; printf '{"cost":{"ceiling":"medium","agents":{"somi-security-reviewer":"high","somi-coder":"high"}}}' > "$GP"
136
+ P="$(proj)"; mkdir "$P/.somi"; printf '{}' > "$P/.somi/config.json"
137
+ check "a tool write to the global config always asks" "$(verdict "$GP" "$P" "$(wr "$GP" '{"cost":{"ceiling":"high"}}')")" "ask"
138
+ check "... also when the global file does not exist yet" "$(verdict "$TMP/new/somi/config.json" "$P" "$(wr "$TMP/new/somi/config.json" '{}')")" "ask"
139
+ check "a project write that drops below a global pin asks" "$(verdict "$GP" "$P" "$(wr "$P/.somi/config.json" '{"cost":{"agents":{"somi-coder":"low"}}}')")" "ask"
140
+ check "a project write that raises over the global file passes" "$(verdict "$GP" "$P" "$(wr "$P/.somi/config.json" '{"cost":{"ceiling":"high"}}')")" "allow"
141
+ check "an unrelated write passes" "$(verdict "$GP" "$P" "$(wr "$P/notes.txt" 'hi')")" "allow"
142
+
143
+ printf '{"dep_install":{"allow":["left-pad"]},"lockfiles":{"allow_edit":true}}' > "$TMP/policy.json"
144
+ P="$(proj)"; mkdir "$P/.somi"; printf '{"dep_install":{"allow":["is-odd"]}}' > "$P/.somi/config.json"
145
+ check "the hooks' config() sees the merge (project array replaces, global key stays)" "$(cd "$P" && SOMI_GLOBAL_CONFIG="$TMP/policy.json" CLAUDE_PROJECT_DIR="$P" node --input-type=module -e "
146
+ import { config } from '$ROOT/hooks/lib/common.mjs';
147
+ process.stdout.write(JSON.stringify([config('.dep_install.allow[]?'), config('.lockfiles.allow_edit')]))")" '[["is-odd"],"true"]'
148
+
149
+ echo "== global config: red proofs =="
150
+ # Each mutant removes one rule from a copy of the plugin's scripts/ and hooks/; the case it guards must flip.
151
+ mutant() { # $1 = name, $2 = file below the copy, $3 = sed expression
152
+ local d="$TMP/mut-$1"; mkdir -p "$d"; cp -r "$ROOT/scripts" "$ROOT/hooks" "$d/"
153
+ sed -i "$3" "$d/$2"
154
+ cmp -s "$ROOT/$2" "$d/$2" && bad "mutant $1 did not change $2"
155
+ }
156
+ mutant shallow scripts/lib/config-files.mjs 's/isObj(v) \&\& isObj(prev) ? mergeConfig(prev, v) : copy(v)/copy(v)/'
157
+ check "red proof: a shallow merge loses the global pins the project does not name" "$(node --input-type=module -e "
158
+ import * as L from '$TMP/mut-shallow/scripts/lib/config-files.mjs';
159
+ process.stdout.write(String(L.mergeConfig({cost:{agents:{a:'high',b:'high'}}}, {cost:{agents:{a:'low'}}}).cost.agents.b))")" "undefined"
160
+ mutant no-global-ask hooks/pre-tool/guard-cost-lowering.mjs 's/askPretool(payload, GLOBAL_WRITE)/void 0/'
161
+ check "red proof: without its rule a global write is not asked about" "$(out="$(wr "$GP" '{"cost":{"ceiling":"high"}}' | (cd "$P" && SOMI_GLOBAL_CONFIG="$GP" CLAUDE_PROJECT_DIR="$P" node "$TMP/mut-no-global-ask/hooks/pre-tool/guard-cost-lowering.mjs"))"; case "$out" in *ask*) echo ask ;; *) echo allow ;; esac)" "allow"
162
+ mutant no-global-skip hooks/session-start/ensure-default-config.mjs 's/if (existing || hasGlobal) {/if (existing) {/'
163
+ P="$(proj)"; printf '{}' | runhook "$TMP/mut-no-global-skip/hooks/session-start/ensure-default-config.mjs" "$NG" "$P" > /dev/null
164
+ check "red proof: without the global check SessionStart writes a project default that masks it" "$([ -e "$P/.somi/config.json" ] && echo made || echo none)" "made"
165
+
166
+ echo " $pass passed, $fail failed"
167
+ [ "$fail" -eq 0 ]
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env node
2
+ // The overview page's roadmap (site/src/content/roadmap.json) must account for every release in
3
+ // CHANGELOG.md, so a release cannot be cut with the published page still describing the one before.
4
+ // scripts/validate.sh runs this, and publish.yml's `version` job runs validate.sh against the
5
+ // dispatched ref, so the check holds at release time, not only on PRs.
6
+ //
7
+ // Each roadmap entry lists the releases it `covers`. Together the entries must cover every
8
+ // `## [X.Y.Z] — YYYY-MM-DD — …` heading exactly once, name no release the CHANGELOG lacks, include
9
+ // their own `version` in `covers`, and end (last era, last entry) with the latest release.
10
+ //
11
+ // Usage: node tests/scripts/lib/site-roadmap-gate.mjs [repoRoot]
12
+ // Exit: 0 consistent; 1 a mismatch (each one listed); 2 a file missing or unreadable.
13
+
14
+ import fs from 'node:fs';
15
+ import path from 'node:path';
16
+
17
+ const root = path.resolve(process.argv[2] ?? '.');
18
+ const CHANGELOG = 'CHANGELOG.md';
19
+ const ROADMAP = 'site/src/content/roadmap.json';
20
+
21
+ function read(rel, parse) {
22
+ try {
23
+ return parse(fs.readFileSync(path.join(root, rel), 'utf8'));
24
+ } catch (err) {
25
+ console.error(`site-roadmap-gate: cannot read ${rel}: ${err.message}`);
26
+ process.exit(2);
27
+ }
28
+ }
29
+
30
+ const releases = read(CHANGELOG, (t) => [...t.matchAll(/^## \[(\d+\.\d+\.\d+)\] — \d{4}-\d{2}-\d{2} — /gm)].map((m) => m[1]));
31
+ const roadmap = read(ROADMAP, JSON.parse);
32
+ if (!releases.length) {
33
+ console.error(`site-roadmap-gate: no release headings in ${CHANGELOG}`);
34
+ process.exit(2);
35
+ }
36
+ if (!Array.isArray(roadmap.eras)) {
37
+ console.error(`site-roadmap-gate: ${ROADMAP} has no "eras" list`);
38
+ process.exit(2);
39
+ }
40
+
41
+ const entries = roadmap.eras.flatMap((era) => (Array.isArray(era.entries) ? era.entries : []));
42
+ const problems = [];
43
+ const seen = new Map();
44
+ for (const e of entries) {
45
+ const covers = Array.isArray(e.covers) ? e.covers : [];
46
+ if (!covers.includes(e.version)) problems.push(`entry ${e.version} does not list its own version in "covers"`);
47
+ for (const v of covers) {
48
+ if (seen.has(v)) problems.push(`${v} is covered twice (entries ${seen.get(v)} and ${e.version})`);
49
+ seen.set(v, e.version);
50
+ if (!releases.includes(v)) problems.push(`${v} is on the roadmap but not a release in ${CHANGELOG}`);
51
+ }
52
+ }
53
+ for (const v of releases) {
54
+ if (!seen.has(v)) problems.push(`release ${v} is missing from the roadmap: add it to an entry's "covers" in ${ROADMAP}`);
55
+ }
56
+ const last = entries.at(-1)?.version;
57
+ if (last !== releases[0]) problems.push(`the roadmap ends at ${last}, not the latest release ${releases[0]}`);
58
+
59
+ if (problems.length) {
60
+ console.error(`SITE ROADMAP OUT OF DATE (${ROADMAP} against ${CHANGELOG}):`);
61
+ for (const p of problems) console.error(` ${p}`);
62
+ process.exit(1);
63
+ }
64
+ console.log(` site roadmap covers all ${releases.length} releases, latest ${releases[0]}`);
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env bash
2
+ # Unit guard for tests/scripts/lib/site-roadmap-gate.mjs (the overview page's roadmap must cover every
3
+ # CHANGELOG release). Proved both ways against synthetic repos -- green on a consistent pair, red on
4
+ # each mismatch a release can produce -- rather than trusted because the real tree passes today.
5
+ set -uo pipefail
6
+
7
+ ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
8
+ GATE="$ROOT/tests/scripts/lib/site-roadmap-gate.mjs"
9
+
10
+ pass=0; fail=0
11
+ ok() { pass=$((pass+1)); printf ' ok %s\n' "$1"; }
12
+ bad() { fail=$((fail+1)); printf ' FAIL %s\n' "$1"; }
13
+
14
+ echo "== site roadmap gate =="
15
+
16
+ TMP="$(mktemp -d)"
17
+ trap 'rm -rf "$TMP"' EXIT
18
+
19
+ # mkrepo <dir> <changelog versions...> -- writes a CHANGELOG with one dated heading per version,
20
+ # newest first, plus an [Unreleased] heading the gate must skip.
21
+ mkrepo() {
22
+ local dir="$1"; shift
23
+ mkdir -p "$dir/site/src/content"
24
+ { echo '# Changelog'; echo; echo '## [Unreleased]'; echo
25
+ for v in "$@"; do echo "## [$v] — 2026-01-01 — release $v"; echo; done; } > "$dir/CHANGELOG.md"
26
+ }
27
+ roadmap() { printf '%s\n' "$2" > "$1/site/src/content/roadmap.json"; }
28
+
29
+ # expect <name> <want-exit> <dir> [stderr-substring]
30
+ expect() {
31
+ local name="$1" want="$2" dir="$3" needle="${4:-}" out rc
32
+ out="$(node "$GATE" "$dir" 2>&1)"; rc=$?
33
+ if [ "$rc" -ne "$want" ]; then bad "$name -- expected exit $want, got $rc ($out)"; return; fi
34
+ if [ -n "$needle" ] && [[ "$out" != *"$needle"* ]]; then bad "$name -- output lacks '$needle' ($out)"; return; fi
35
+ ok "$name"
36
+ }
37
+
38
+ GOOD='{"eras":[{"label":"a","entries":[{"version":"1.0.0","covers":["1.0.0","1.0.1"]},{"version":"1.1.0","covers":["1.1.0"]}]}]}'
39
+
40
+ R="$TMP/green"; mkrepo "$R" 1.1.0 1.0.1 1.0.0; roadmap "$R" "$GOOD"
41
+ expect "a roadmap covering every release passes" 0 "$R" "covers all 3 releases"
42
+
43
+ R="$TMP/new-release"; mkrepo "$R" 1.2.0 1.1.0 1.0.1 1.0.0; roadmap "$R" "$GOOD"
44
+ expect "a new CHANGELOG release missing from the roadmap fails" 1 "$R" "release 1.2.0 is missing"
45
+ expect " ...and the roadmap no longer ends at the latest release" 1 "$R" "not the latest release 1.2.0"
46
+
47
+ R="$TMP/twice"; mkrepo "$R" 1.1.0 1.0.1 1.0.0
48
+ roadmap "$R" '{"eras":[{"entries":[{"version":"1.0.0","covers":["1.0.0","1.0.1"]},{"version":"1.1.0","covers":["1.1.0","1.0.1"]}]}]}'
49
+ expect "a release covered by two entries fails" 1 "$R" "1.0.1 is covered twice"
50
+
51
+ R="$TMP/unknown"; mkrepo "$R" 1.1.0 1.0.1 1.0.0
52
+ roadmap "$R" '{"eras":[{"entries":[{"version":"1.0.0","covers":["1.0.0","1.0.1","0.9.0"]},{"version":"1.1.0","covers":["1.1.0"]}]}]}'
53
+ expect "a roadmap version the CHANGELOG lacks fails" 1 "$R" "0.9.0 is on the roadmap but not a release"
54
+
55
+ R="$TMP/own"; mkrepo "$R" 1.1.0 1.0.1 1.0.0
56
+ roadmap "$R" '{"eras":[{"entries":[{"version":"1.0.0","covers":["1.0.1"]},{"version":"1.1.0","covers":["1.1.0"]}]}]}'
57
+ expect "an entry whose covers omits its own version fails" 1 "$R" "does not list its own version"
58
+
59
+ R="$TMP/order"; mkrepo "$R" 1.1.0 1.0.1 1.0.0
60
+ roadmap "$R" '{"eras":[{"entries":[{"version":"1.1.0","covers":["1.1.0"]},{"version":"1.0.0","covers":["1.0.0","1.0.1"]}]}]}'
61
+ expect "a roadmap that does not end at the latest release fails" 1 "$R" "ends at 1.0.0"
62
+
63
+ R="$TMP/malformed"; mkrepo "$R" 1.0.0; roadmap "$R" '{"eras": [ broken'
64
+ expect "an unreadable roadmap exits 2" 2 "$R" "cannot read site/src/content/roadmap.json"
65
+
66
+ R="$TMP/no-eras"; mkrepo "$R" 1.0.0; roadmap "$R" '{"intro":"x"}'
67
+ expect "a roadmap without eras exits 2" 2 "$R" 'no "eras" list'
68
+
69
+ R="$TMP/no-releases"; mkdir -p "$R/site/src/content"; printf '# Changelog\n\n## [Unreleased]\n' > "$R/CHANGELOG.md"; roadmap "$R" "$GOOD"
70
+ expect "a CHANGELOG without dated releases exits 2" 2 "$R" "no release headings"
71
+
72
+ # site/ is not in the npm package (package.json `files`), so the real-tree case runs only in a checkout.
73
+ if [ -d "$ROOT/site" ]; then expect "the real tree passes" 0 "$ROOT"; else echo " (skipped: site/ not packaged)"; fi
74
+
75
+ echo " $pass passed, $fail failed"
76
+ [ "$fail" -eq 0 ]
@@ -41,7 +41,7 @@ check "... a no-op, file untouched, reported created:false" "$(cat "$P/.somi/con
41
41
  # --- show -----------------------------------------------------------------------------------------
42
42
  P="$(proj)"; cfg "$CLI" "$P" init > /dev/null
43
43
  check "show: exit 0" "$(cfg "$CLI" "$P" show)" "0"
44
- check "show on a fresh default config: ceiling medium from config" "$(jf 'o.ceiling')" '{"value":"medium","source":"config"}'
44
+ check "show on a fresh default config: ceiling medium from config" "$(jf 'o.ceiling')" '{"value":"medium","source":"config","from":"project"}'
45
45
  check "show: every shipped agent is listed with default, tier, source" "$(jf 'o.agents.length === Object.keys(JSON.parse(require("fs").readFileSync("'"$EXPECT"'","utf8")).cost.agents).length && o.agents.every((a) => a.tier === a.default && a.source === "override")')" "true"
46
46
  check "show: no warnings for a fresh default config" "$(jf 'o.warnings')" "[]"
47
47
  check "show output is one ASCII line" "$(wc -l < "$TMP/out" | tr -d ' ')|$(ascii_only "$TMP/out")" "1|yes"
@@ -126,7 +126,7 @@ check "show carries the same warning" "$(cfg "$CLI" "$P" show)|$(jf 'o.warnings.
126
126
  check "set security-reviewer back to high clears it" "$(cfg "$CLI" "$P" set cost.agents.somi-security-reviewer high)|$(jf 'o.warnings')" "0|[]"
127
127
  check "set an agent not in the default table: accepted, unknown warning" "$(cfg "$CLI" "$P" set cost.agents.my-agent high)|$(jf 'o.warnings.length')" "0|1"
128
128
  cfg "$CLI" "$P" show > /dev/null
129
- check "show flags the extra name unknown:true with no default" "$(jf 'JSON.stringify(o.agents.find((a) => a.name === "my-agent"))')" '{"name":"my-agent","tier":"high","source":"override","default":null,"unknown":true}'
129
+ check "show flags the extra name unknown:true with no default" "$(jf 'JSON.stringify(o.agents.find((a) => a.name === "my-agent"))')" '{"name":"my-agent","tier":"high","source":"override","from":"project","default":null,"unknown":true}'
130
130
 
131
131
  # --- 64: arguments ---------------------------------------------------------------------------------
132
132
  P="$(proj)"; cfg "$CLI" "$P" init > /dev/null; cp "$P/.somi/config.json" "$TMP/before"
@@ -156,15 +156,20 @@ P="$(proj)"; mkdir "$P/.somi"; head -c 70000 /dev/zero | tr '\0' ' ' > "$P/.somi
156
156
  check "an oversize config exits 67, untouched" "$(cfg "$CLI" "$P" set cost.ceiling low)|$(cmp -s "$P/.somi/config.json" "$TMP/big.before" && echo same)" "67|same"
157
157
  check "error text does not echo file text" "$(P="$(proj)"; mkdir "$P/.somi"; printf '{"cost":{"cieling":"SECRET-VALUE"}}' > "$P/.somi/config.json"; cfg "$CLI" "$P" show > /dev/null; grep -c SECRET-VALUE "$TMP/err" | tr -d ' ')" "0"
158
158
 
159
- # --- no-follow ---------------------------------------------------------------------------------------
159
+ # --- links: writes never follow one, reads (show, get) do -----------------------------------------
160
160
  echo KEEP > "$TMP/victim.txt"; OUTSIDE="$(mktemp -d -p "$TMP")"
161
161
  P="$(proj)"; ln -s "$OUTSIDE" "$P/.somi"
162
- for op in "set cost.ceiling low" init show; do
162
+ for op in "set cost.ceiling low" "unset cost.agents.somi-coder" init; do
163
163
  # shellcheck disable=SC2086
164
164
  check "a symlinked .somi: $op exits 67" "$(cfg "$CLI" "$P" $op)" "67"
165
165
  done
166
166
  check "... nothing was written outside" "$(ls -A "$OUTSIDE" | wc -l | tr -d ' ')" "0"
167
+ check "a symlinked .somi: show reads through it (a junction on Windows)" "$(cfg "$CLI" "$P" show)|$(jf 'o.exists')" "0|false"
168
+ SHARED="$(mktemp -d -p "$TMP")"; printf '{"cost":{"ceiling":"low"}}' > "$SHARED/config.json"; P="$(proj)"; ln -s "$SHARED" "$P/.somi"
169
+ check "... and reads the config behind it" "$(cfg "$CLI" "$P" show)|$(jf 'o.ceiling.value')" "0|low"
170
+ check "... get too" "$(cfg "$CLI" "$P" get cost.ceiling)|$(jf 'o.value')" "0|low"
167
171
  P="$(proj)"; mkdir "$P/.somi"; printf '{"cost":{"ceiling":"high"}}' > "$TMP/real.json"; ln -s "$TMP/real.json" "$P/.somi/config.json"
172
+ check "a symlinked config.json: show reads through it" "$(cfg "$CLI" "$P" show)|$(jf 'o.ceiling.value')" "0|high"
168
173
  check "a symlinked config.json: set exits 67" "$(cfg "$CLI" "$P" set cost.ceiling low)" "67"
169
174
  check "... the link target is untouched" "$(cat "$TMP/real.json")" '{"cost":{"ceiling":"high"}}'
170
175
  check "init over a symlinked config.json is a no-op (it exists)" "$(cfg "$CLI" "$P" init)|$(jf 'o.created')" "0|false"