@skathio/somi 3.1.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (148) hide show
  1. package/.claude/settings.json +13 -0
  2. package/.claude-plugin/marketplace.json +2 -2
  3. package/.claude-plugin/plugin.json +1 -1
  4. package/.copilot-extension/extension.json +1 -1
  5. package/.copilot-extension/marketplace.json +2 -2
  6. package/README.md +28 -23
  7. package/VERSION +1 -1
  8. package/agents/{architecture-reviewer.md → somi-architecture-reviewer.md} +10 -11
  9. package/agents/{atlas.md → somi-atlas.md} +3 -5
  10. package/agents/{coder.md → somi-coder.md} +29 -26
  11. package/agents/{designer.md → somi-designer.md} +9 -11
  12. package/agents/{discovery-analyst.md → somi-discovery-analyst.md} +7 -9
  13. package/agents/{impact.md → somi-impact.md} +6 -8
  14. package/agents/{incident.md → somi-incident.md} +2 -4
  15. package/agents/{plan-reviewer.md → somi-plan-reviewer.md} +9 -11
  16. package/agents/{planner.md → somi-planner.md} +19 -27
  17. package/agents/{pr.md → somi-pr.md} +2 -4
  18. package/agents/{refactor-designer.md → somi-refactor-designer.md} +14 -15
  19. package/agents/{refactorer.md → somi-refactorer.md} +14 -15
  20. package/agents/{reviewer.md → somi-reviewer.md} +8 -7
  21. package/agents/{sdlc-reviewer.md → somi-sdlc-reviewer.md} +8 -10
  22. package/agents/{security-reviewer.md → somi-security-reviewer.md} +8 -8
  23. package/agents/{test-strategist.md → somi-test-strategist.md} +4 -6
  24. package/agents/somi.md +27 -14
  25. package/commands/adopt.md +9 -10
  26. package/commands/architecture-review.md +4 -5
  27. package/commands/atlas.md +3 -4
  28. package/commands/code-loop.md +38 -14
  29. package/commands/code-parallel.md +4 -4
  30. package/commands/code.md +24 -15
  31. package/commands/debug.md +8 -9
  32. package/commands/design.md +14 -15
  33. package/commands/discover.md +11 -12
  34. package/commands/impact.md +2 -3
  35. package/commands/incident.md +3 -4
  36. package/commands/plan-loop.md +16 -12
  37. package/commands/plan.md +15 -16
  38. package/commands/pr.md +2 -3
  39. package/commands/refactor-design.md +10 -11
  40. package/commands/refactor.md +7 -8
  41. package/commands/release-readiness.md +6 -7
  42. package/commands/review-panel.md +24 -23
  43. package/commands/review.md +14 -14
  44. package/commands/security-review.md +8 -7
  45. package/commands/ship-loop.md +107 -28
  46. package/commands/ship.md +6 -6
  47. package/commands/somi-config.md +131 -0
  48. package/commands/test-strategy.md +3 -4
  49. package/commands/upgrade.md +4 -5
  50. package/examples/code-review-example.md +1 -1
  51. package/examples/discovery-example.md +1 -1
  52. package/examples/feature-plan-example.md +2 -2
  53. package/hooks/hooks.json +14 -1
  54. package/hooks/lib/common.mjs +64 -1
  55. package/hooks/post-tool/audit-log.mjs +3 -2
  56. package/hooks/pre-tool/block-secret-writes.mjs +12 -11
  57. package/hooks/pre-tool/guard-cost-lowering.mjs +177 -0
  58. package/hooks/pre-tool/guard-protected-paths.mjs +13 -3
  59. package/hooks/session-start/detect-repo-instructions.mjs +27 -2
  60. package/hooks/session-start/ensure-default-config.mjs +121 -0
  61. package/package.json +1 -1
  62. package/rules/00-priorities.md +1 -1
  63. package/rules/30-security-owasp.md +1 -1
  64. package/rules/50-collaboration.md +9 -6
  65. package/rules/CLAUDE.md +11 -10
  66. package/scripts/lib/agent-info.mjs +85 -0
  67. package/scripts/lib/agent-registry.mjs +92 -0
  68. package/scripts/lib/cost-ceiling.mjs +175 -171
  69. package/scripts/lib/cost-config-edit.mjs +102 -0
  70. package/scripts/lib/cost-config.mjs +64 -0
  71. package/scripts/lib/cost-defaults.mjs +51 -0
  72. package/scripts/lib/cost-lowering.mjs +89 -0
  73. package/scripts/lib/cost-model.mjs +37 -22
  74. package/scripts/lib/dispatch-resolver.mjs +68 -50
  75. package/scripts/lib/loop-diff.mjs +96 -0
  76. package/scripts/lib/loop-dispatch.mjs +19 -0
  77. package/scripts/lib/loop-review.mjs +52 -0
  78. package/scripts/lib/run-budget.mjs +28 -0
  79. package/scripts/lib/size-tier.mjs +184 -0
  80. package/scripts/lib/tier-record.mjs +82 -0
  81. package/scripts/size-budget-overrides.json +7 -15
  82. package/scripts/somi-config.mjs +358 -0
  83. package/scripts/somi-dispatch.mjs +1 -1
  84. package/scripts/somi-loop.mjs +71 -96
  85. package/scripts/somi-mcp.mjs +135 -14
  86. package/scripts/somi-tier.mjs +393 -0
  87. package/scripts/validate.sh +37 -215
  88. package/skills/api-design/SKILL.md +1 -1
  89. package/skills/clean-code/SKILL.md +1 -1
  90. package/skills/market-research/SKILL.md +2 -2
  91. package/skills/owasp-defense/SKILL.md +2 -2
  92. package/skills/plan-review/SKILL.md +5 -1
  93. package/skills/reasoning-craft/SKILL.md +1 -1
  94. package/skills/requirements-engineering/SKILL.md +3 -3
  95. package/skills/solid-principles/SKILL.md +1 -1
  96. package/skills/somi-dispatch/SKILL.md +116 -87
  97. package/skills/somi-routing/SKILL.md +88 -1
  98. package/skills/testing-playbook/SKILL.md +2 -2
  99. package/skills/threat-modeling/SKILL.md +3 -3
  100. package/templates/BRIEF.md.tmpl +2 -2
  101. package/templates/DESIGN.md.tmpl +1 -1
  102. package/templates/DOD.md.tmpl +2 -2
  103. package/templates/PHASE.md.tmpl +2 -2
  104. package/templates/RCA.md.tmpl +1 -1
  105. package/templates/REVIEW.md.tmpl +3 -2
  106. package/templates/SDD.md.tmpl +1 -1
  107. package/templates/SPEC.md.tmpl +1 -1
  108. package/templates/TDD.md.tmpl +1 -1
  109. package/tests/hooks/cases/block-secret-writes.json +130 -17
  110. package/tests/hooks/cases/detect-repo-instructions.json +22 -1
  111. package/tests/hooks/cases/guard-protected-paths.json +175 -25
  112. package/tests/scripts/agent-model-frontmatter.sh +38 -0
  113. package/tests/scripts/agent-names.sh +77 -0
  114. package/tests/scripts/check-frontmatter.sh +1 -1
  115. package/tests/scripts/checkpoint-gate.sh +44 -3
  116. package/tests/scripts/convergence-runner.sh +10 -6
  117. package/tests/scripts/cost-ceiling.sh +127 -215
  118. package/tests/scripts/cost-defaults.sh +128 -0
  119. package/tests/scripts/cost-hardcap.sh +329 -0
  120. package/tests/scripts/cost-lowering.sh +261 -0
  121. package/tests/scripts/cost-model.sh +18 -14
  122. package/tests/scripts/dispatch-guard.sh +7 -7
  123. package/tests/scripts/dispatch-line-gate.sh +61 -0
  124. package/tests/scripts/dispatch-reference-gate.sh +13 -13
  125. package/tests/scripts/ensure-default-config.sh +162 -0
  126. package/tests/scripts/eval-runner.sh +9 -9
  127. package/tests/scripts/evals-packaging.sh +127 -20
  128. package/tests/scripts/fixtures/size-tier-cases.json +87 -0
  129. package/tests/scripts/goldens/loop-state-done.json +1 -1
  130. package/tests/scripts/goldens/somi-loop.json +3 -3
  131. package/tests/scripts/lib/agent-registry-tests.mjs +95 -0
  132. package/tests/scripts/lib/brief-only-guard.mjs +27 -0
  133. package/tests/scripts/lib/checkpoint-gate.mjs +40 -3
  134. package/tests/scripts/lib/dispatch-reference-gate.mjs +5 -5
  135. package/tests/scripts/lib/retirement-gate.mjs +7 -2
  136. package/tests/scripts/lib/ship-loop-sizing-guard.mjs +52 -0
  137. package/tests/scripts/lib/size-tier-drift.mjs +74 -0
  138. package/tests/scripts/lib/somi-mcp-config-tests.mjs +92 -0
  139. package/tests/scripts/lib/somi-mcp-tests.mjs +80 -25
  140. package/tests/scripts/lib/somi-mcp-tier-tests.mjs +64 -0
  141. package/tests/scripts/retirement-gate.sh +19 -0
  142. package/tests/scripts/review-panel-seats.sh +2 -2
  143. package/tests/scripts/run.sh +95 -0
  144. package/tests/scripts/size-tier.sh +562 -0
  145. package/tests/scripts/somi-agent-dispatch-contract.sh +20 -11
  146. package/tests/scripts/somi-config.sh +281 -0
  147. package/tests/scripts/somi-dispatch.sh +194 -95
  148. package/tests/scripts/somi-mcp.sh +2 -0
@@ -3,8 +3,8 @@
3
3
  #
4
4
  # Exercises: re-resolution per dispatch across separate process invocations (state lives on disk,
5
5
  # not in memory), a bare .somi/config.json edit cannot move a ceiling already recorded,
6
- # capability-set selection (allow picks the highest permitted declared member; when none fits, allow
7
- # at the cheapest declared member instead of blocking, never a tier outside the declared set), and a
6
+ # hard-cap selection (no override: the ceiling tier itself, whatever was declared; the declared set
7
+ # only feeds `degraded`; the old never-degrade rule is reversed), and a
8
8
  # malformed ceiling or declared set — from config, an env var, or an explicit argument — dying loudly
9
9
  # instead of silently disabling the ceiling.
10
10
  set -uo pipefail
@@ -30,172 +30,87 @@ echo "== cost ceiling =="
30
30
  TMP="$(mktemp -d)"
31
31
  trap 'rm -rf "$TMP"' EXIT
32
32
 
33
- # --- bootstrap: no config, no env, no arg -> DEFAULT_CEILING, no overrides array yet -------------
33
+ # State file helper: prints the persisted state as compact JSON, or "none".
34
+ state() { run "$1" 'const fs = await import("node:fs"); const f = C.ceilingStatePath(ROOT); process.stdout.write(fs.existsSync(f) ? fs.readFileSync(f, "utf8") : "none")'; }
35
+ res() { run "$1" 'const r = C.resolveCeiling(ROOT, undefined); process.stdout.write([r.ceiling, r.overridden, r.source, r.ceiling_origin].join(","))'; }
36
+
37
+ # --- nothing set: the built-in default is medium, and nothing is written ------------------------
34
38
  A="$TMP/a"; mkdir -p "$A/.somi"
35
- check "first resolve with nothing set bootstraps to the default" \
36
- "$(run "$A" 'const r = C.resolveCeiling(ROOT, undefined); process.stdout.write(r.ceiling + "," + r.overridden)')" \
37
- "high,false"
38
- check "bootstrap writes state with no ceiling_overrides key (lazy creation)" \
39
- "$(run "$A" 'const fs = await import("node:fs"); const s = JSON.parse(fs.readFileSync(C.ceilingStatePath(ROOT), "utf8")); process.stdout.write(String("ceiling_overrides" in s))')" \
40
- "false"
39
+ check "no config, no state, no env, no arg: medium, from the default" "$(res "$A")" "medium,false,default,default"
40
+ check "... and a resolve with nothing explicit writes no state file" "$(state "$A")" "none"
41
+ check "DEFAULT_CEILING is medium" "$(run "$A" 'process.stdout.write(C.DEFAULT_CEILING)')" "medium"
41
42
 
42
- # --- bootstrap reads .somi/config.json's cost.ceiling exactly once ------------------------------
43
+ # --- config is read LIVE: an edit between two resolves moves the ceiling -------------------------
43
44
  B="$TMP/b"; mkdir -p "$B/.somi"
44
45
  printf '{"cost": {"ceiling": "low"}}\n' > "$B/.somi/config.json"
45
- check "bootstrap honours config's cost.ceiling" \
46
- "$(run "$B" 'process.stdout.write(C.resolveCeiling(ROOT, undefined).ceiling)')" \
47
- "low"
48
-
49
- # The loop-cap precedent's central guard, mirrored: a bare config edit AFTER state exists must
50
- # not move a ceiling already in force.
46
+ check "config's cost.ceiling is honoured" "$(res "$B")" "low,false,config,config"
51
47
  printf '{"cost": {"ceiling": "high"}}\n' > "$B/.somi/config.json"
52
- check "a bare config edit after bootstrap does not move the ceiling already in force" \
53
- "$(run "$B" 'const r = C.resolveCeiling(ROOT, undefined); process.stdout.write(r.ceiling + "," + r.overridden)')" \
54
- "low,false"
48
+ check "a bare config edit moves the ceiling on the very next resolve (config is live)" "$(res "$B")" "high,false,config,config"
49
+ printf '{}\n' > "$B/.somi/config.json"
50
+ check "removing cost.ceiling from the config falls back to the default" "$(res "$B")" "medium,false,default,default"
51
+ check "config-only resolves never write state" "$(state "$B")" "none"
55
52
 
56
- # --- an explicit env var THIS call raises the ceiling and is recorded ---------------------------
57
- out="$(SOMI_COST_CEILING=high node --input-type=module -e "const C = await import('$LIB'); const r = C.resolveCeiling('$B', undefined); process.stdout.write(r.ceiling+','+r.overridden+','+r.override.field+','+r.override.from+','+r.override.to+','+r.override.source)")"
58
- check "env override moves the ceiling and reports it" "$out" "high,true,ceiling,low,high,env"
59
- check "the override was persisted to disk, in a ceiling_overrides array" \
60
- "$(run "$B" 'const fs = await import("node:fs"); const s = JSON.parse(fs.readFileSync(C.ceilingStatePath(ROOT), "utf8")); process.stdout.write(s.ceiling_overrides.length + "," + s.ceiling_overrides[0].source)')" \
61
- "1,env"
62
-
63
- # --- re-resolution: a later bare call keeps the raised ceiling, records no new entry -------------
64
- check "a later bare call keeps the raised ceiling in force" \
65
- "$(run "$B" 'const r = C.resolveCeiling(ROOT, undefined); process.stdout.write(r.ceiling + "," + r.overridden)')" \
66
- "high,false"
67
- check "no duplicate override entry was recorded for the bare call" \
53
+ # --- CLI/env beat config, and are persisted with their origin ------------------------------------
54
+ printf '{"cost": {"ceiling": "low"}}\n' > "$B/.somi/config.json"
55
+ out="$(SOMI_COST_CEILING=high node --input-type=module -e "const C = await import('$LIB'); const r = C.resolveCeiling('$B', undefined); process.stdout.write(r.ceiling+','+r.overridden+','+r.source+','+r.override.field+','+r.override.from+','+r.override.to+','+r.override.source)")"
56
+ check "env value beats config, and the override record names what was in force" "$out" "high,true,env,ceiling,low,high,env"
57
+ check "the override was persisted with origin env and a one-entry ceiling_overrides" \
58
+ "$(run "$B" 'const fs = await import("node:fs"); const s = JSON.parse(fs.readFileSync(C.ceilingStatePath(ROOT), "utf8")); process.stdout.write([s.ceiling, s.ceiling_origin, s.ceiling_overrides.length, s.ceiling_overrides[0].source].join(","))')" \
59
+ "high,env,1,env"
60
+
61
+ # Config OUTRANKS a persisted cli/env ceiling (it is live; state only fills in when config is silent).
62
+ check "persisted env ceiling is outranked by cost.ceiling in config (config > persisted state)" \
63
+ "$(res "$B")" "low,false,config,config"
64
+ printf '{}\n' > "$B/.somi/config.json"
65
+ check "with config silent, the persisted env ceiling stands: source state, origin env" \
66
+ "$(res "$B")" "high,false,state,env"
67
+ printf '{"cost": {"ceiling": "low"}}\n' > "$B/.somi/config.json"
68
+ check "no duplicate override entry was recorded by the bare calls" \
68
69
  "$(run "$B" 'const fs = await import("node:fs"); const s = JSON.parse(fs.readFileSync(C.ceilingStatePath(ROOT), "utf8")); process.stdout.write(String(s.ceiling_overrides.length))')" \
69
70
  "1"
70
71
 
71
72
  # --- an explicit argument beats an env var set in the SAME call ---------------------------------
72
- out="$(SOMI_COST_CEILING=low node --input-type=module -e "const C = await import('$LIB'); const r = C.resolveCeiling('$B', 'medium'); process.stdout.write(r.ceiling+','+r.override.source+','+r.override.from)")"
73
- check "explicit argument wins over env var, and is recorded with source cli" "$out" "medium,cli,high"
74
-
75
- # --- resolveCeiling reports WHERE this call's ceiling came from -- this is what lets a dispatcher
76
- # announce a `low` ceiling honestly, by its real source, not just its bare value. Additive field,
77
- # checked independently of `ceiling`/`overridden` above so a caller that never reads it stays
78
- # unaffected. ---------------------------------------------------------------------------------
79
- DEF="$TMP/source-default"; mkdir -p "$DEF/.somi"
80
- check "bootstrap with no config, no env, no arg: source is 'default'" \
81
- "$(run "$DEF" 'process.stdout.write(C.resolveCeiling(ROOT, undefined).source)')" \
82
- "default"
83
-
84
- CFGSRC="$TMP/source-config"; mkdir -p "$CFGSRC/.somi"
85
- printf '{"cost": {"ceiling": "low"}}\n' > "$CFGSRC/.somi/config.json"
86
- check "bootstrap from .somi/config.json's cost.ceiling: source is 'config'" \
87
- "$(run "$CFGSRC" 'process.stdout.write(C.resolveCeiling(ROOT, undefined).source)')" \
88
- "config"
89
- check "a later bare call against the same state: source is 'state', not 'config' again" \
90
- "$(run "$CFGSRC" 'process.stdout.write(C.resolveCeiling(ROOT, undefined).source)')" \
91
- "state"
92
- check "an explicit CLI argument: source is 'cli' even when it matches the value already on disk" \
93
- "$(run "$CFGSRC" 'process.stdout.write(C.resolveCeiling(ROOT, "low").source)')" \
94
- "cli"
95
- out="$(SOMI_COST_CEILING=low node --input-type=module -e "const C = await import('$LIB'); process.stdout.write(C.resolveCeiling('$CFGSRC', undefined).source)")"
73
+ out="$(SOMI_COST_CEILING=low node --input-type=module -e "const C = await import('$LIB'); const r = C.resolveCeiling('$B', 'medium'); process.stdout.write(r.ceiling+','+r.override.source+','+r.override.from+','+r.ceiling_origin)")"
74
+ check "explicit argument wins over env, is recorded with source cli; from is the ceiling in force" "$out" "medium,cli,low,cli"
75
+ check "an explicit CLI value matching what config gives still reports source cli and persists origin cli" \
76
+ "$(run "$B" 'const r = C.resolveCeiling(ROOT, "low"); process.stdout.write([r.overridden, r.source, r.ceiling_origin].join(","))')" \
77
+ "false,cli,cli"
78
+ check "... the persisted origin is now cli" \
79
+ "$(run "$B" 'const fs = await import("node:fs"); process.stdout.write(JSON.parse(fs.readFileSync(C.ceilingStatePath(ROOT), "utf8")).ceiling_origin)')" "cli"
80
+ out="$(SOMI_COST_CEILING=low node --input-type=module -e "const C = await import('$LIB'); process.stdout.write(C.resolveCeiling('$B', undefined).source)")"
96
81
  check "an explicit env var: source is 'env'" "$out" "env"
97
82
 
98
- # --- ceiling_origin: what actually produced the value now in force, distinct from `source` (what
99
- # THIS call did). This is the field that makes "source: state" actionable -- see cost-ceiling.mjs's
100
- # own doc comment. --------------------------------------------------------------------------------
101
- ORIG_DEF="$TMP/origin-default"; mkdir -p "$ORIG_DEF/.somi"
102
- check "bootstrap with nothing set: ceiling_origin is 'default', matching source on this first call" \
103
- "$(run "$ORIG_DEF" 'const r = C.resolveCeiling(ROOT, undefined); process.stdout.write(r.source + "," + r.ceiling_origin)')" \
104
- "default,default"
105
-
106
- ORIG_CFG="$TMP/origin-config"; mkdir -p "$ORIG_CFG/.somi"
107
- printf '{"cost": {"ceiling": "low"}}\n' > "$ORIG_CFG/.somi/config.json"
108
- check "bootstrap from config: ceiling_origin is 'config', matching source on this first call" \
109
- "$(run "$ORIG_CFG" 'const r = C.resolveCeiling(ROOT, undefined); process.stdout.write(r.source + "," + r.ceiling_origin)')" \
110
- "config,config"
111
- # The crux of the feature: a LATER bare call reports source "state" (nothing moved this call), but
112
- # ceiling_origin still says "config" -- "saved state" alone would not explain why the user is at
113
- # `low`; the origin does.
114
- check "a later bare call: source is 'state', but ceiling_origin still says 'config' -- actionable" \
115
- "$(run "$ORIG_CFG" 'const r = C.resolveCeiling(ROOT, undefined); process.stdout.write(r.source + "," + r.ceiling_origin)')" \
116
- "state,config"
117
-
118
- # An override updates ceiling_origin to track the value NOW in force, and a later bare call carries
119
- # that origin forward too -- proving the disclosure survives across process invocations, not just
120
- # within the one call that made the override.
121
- ORIG_OVR="$TMP/origin-override"; mkdir -p "$ORIG_OVR/.somi"
122
- out="$(SOMI_COST_CEILING=low node --input-type=module -e "const C = await import('$LIB'); const r = C.resolveCeiling('$ORIG_OVR', undefined); process.stdout.write(r.source + ',' + r.ceiling_origin)")"
123
- check "an env override this call: source and ceiling_origin both report 'env'" "$out" "env,env"
124
- check "a bare call after that override: source is 'state', ceiling_origin still says 'env'" \
125
- "$(run "$ORIG_OVR" 'const r = C.resolveCeiling(ROOT, undefined); process.stdout.write(r.source + "," + r.ceiling_origin)')" \
126
- "state,env"
127
-
128
- # A CLI argument that matches the value already on disk records no override (per the existing
129
- # no-duplicate-entry behavior above) -- ceiling_origin must therefore be left exactly as it was,
130
- # not overwritten to "cli" for a value that didn't actually move.
131
- ORIG_NOOP="$TMP/origin-noop"; mkdir -p "$ORIG_NOOP/.somi"
132
- printf '{"cost": {"ceiling": "medium"}}\n' > "$ORIG_NOOP/.somi/config.json"
133
- run "$ORIG_NOOP" 'C.resolveCeiling(ROOT, undefined)' >/dev/null # bootstrap from config first
134
- check "a --ceiling matching the value already on disk: ceiling_origin is untouched (still 'config')" \
135
- "$(run "$ORIG_NOOP" 'const r = C.resolveCeiling(ROOT, "medium"); process.stdout.write(r.overridden + "," + r.source + "," + r.ceiling_origin)')" \
136
- "false,cli,config"
137
-
138
- # M3: a state file written BEFORE ceiling_origin existed (this repo's own committed
139
- # .somi/somi-state/ceiling.json is exactly this shape: {"ceiling":"high"}, no origin key at all) has
140
- # no way to have set it -- absence is legacy, not corruption, and must not hard-fail. Reported as the
141
- # distinct value 'unknown' rather than a guess. Contrast with ORIG_BADVAL below: PRESENT but wrong
142
- # is still rejected.
143
- ORIG_LEGACY="$TMP/origin-legacy"; mkdir -p "$ORIG_LEGACY/.somi/somi-state"
144
- printf '{"ceiling": "high"}' > "$ORIG_LEGACY/.somi/somi-state/ceiling.json"
145
- check "a state file predating ceiling_origin does not hard-fail; reports 'unknown'" \
146
- "$(run "$ORIG_LEGACY" 'const r = C.resolveCeiling(ROOT, undefined); process.stdout.write(r.ceiling + "," + r.ceiling_origin)')" \
147
- "high,unknown"
148
-
149
- # M2: `ceiling_origin: "config"` goes stale when .somi/config.json's cost.ceiling changes to a
150
- # DIFFERENT value after state already exists -- a bare edit still cannot MOVE the ceiling already in
151
- # force (proven above), but continuing to report "config" would keep claiming a committed policy
152
- # that no longer matches what's committed. Reported as "config-stale", never persisted.
153
- ORIG_STALE="$TMP/origin-stale"; mkdir -p "$ORIG_STALE/.somi"
154
- printf '{"cost": {"ceiling": "low"}}\n' > "$ORIG_STALE/.somi/config.json"
155
- run "$ORIG_STALE" 'C.resolveCeiling(ROOT, undefined)' >/dev/null # bootstrap from config: low, origin config
156
- printf '{"cost": {"ceiling": "high"}}\n' > "$ORIG_STALE/.somi/config.json" # config now says something else
157
- check "config changed after bootstrap: ceiling stays put, ceiling_origin reports 'config-stale'" \
158
- "$(run "$ORIG_STALE" 'const r = C.resolveCeiling(ROOT, undefined); process.stdout.write(r.ceiling + "," + r.overridden + "," + r.ceiling_origin)')" \
159
- "low,false,config-stale"
160
- check "config-stale is never written back to the state file itself" \
161
- "$(run "$ORIG_STALE" 'const fs = await import("node:fs"); const s = JSON.parse(fs.readFileSync(C.ceilingStatePath(ROOT), "utf8")); process.stdout.write(s.ceiling_origin)')" \
162
- "config"
163
-
164
- ORIG_REMOVED="$TMP/origin-removed"; mkdir -p "$ORIG_REMOVED/.somi"
165
- printf '{"cost": {"ceiling": "medium"}}\n' > "$ORIG_REMOVED/.somi/config.json"
166
- run "$ORIG_REMOVED" 'C.resolveCeiling(ROOT, undefined)' >/dev/null # bootstrap from config
167
- printf '{}\n' > "$ORIG_REMOVED/.somi/config.json" # the policy is removed entirely, not just changed
168
- check "config.json's cost.ceiling removed entirely after bootstrap: still reported 'config-stale'" \
169
- "$(run "$ORIG_REMOVED" 'process.stdout.write(C.resolveCeiling(ROOT, undefined).ceiling_origin)')" \
170
- "config-stale"
171
-
172
- ORIG_UNCHANGED="$TMP/origin-unchanged"; mkdir -p "$ORIG_UNCHANGED/.somi"
173
- printf '{"cost": {"ceiling": "low"}}\n' > "$ORIG_UNCHANGED/.somi/config.json"
174
- run "$ORIG_UNCHANGED" 'C.resolveCeiling(ROOT, undefined)' >/dev/null # bootstrap
175
- check "config.json unchanged after bootstrap: ceiling_origin stays plain 'config', not stale" \
176
- "$(run "$ORIG_UNCHANGED" 'process.stdout.write(C.resolveCeiling(ROOT, undefined).ceiling_origin)')" \
177
- "config"
178
-
179
- # A ceiling_origin of 'cli'/'env'/'default' never gets the staleness check applied, even against a
180
- # config.json that would otherwise look stale by value -- staleness is only ever about a 'config'
181
- # claim specifically.
182
- ORIG_NOTCONFIG="$TMP/origin-not-config"; mkdir -p "$ORIG_NOTCONFIG/.somi"
183
- out="$(SOMI_COST_CEILING=low node --input-type=module -e "const C = await import('$LIB'); const r = C.resolveCeiling('$ORIG_NOTCONFIG', undefined); process.stdout.write(r.ceiling_origin)")"
184
- check "bootstrap via env override: ceiling_origin is 'env', unaffected by the config-staleness check" "$out" "env"
185
-
186
- # M3 + M2 do not interact: a legacy (no ceiling_origin) state file is never treated as possibly stale.
187
- ORIG_LEGACY_CFG="$TMP/origin-legacy-cfg"; mkdir -p "$ORIG_LEGACY_CFG/.somi/somi-state"
188
- mkdir -p "$ORIG_LEGACY_CFG/.somi"
189
- printf '{"ceiling": "high"}' > "$ORIG_LEGACY_CFG/.somi/somi-state/ceiling.json"
190
- printf '{"cost": {"ceiling": "low"}}\n' > "$ORIG_LEGACY_CFG/.somi/config.json"
191
- check "a legacy state file with a differing config.json still reports 'unknown', not 'config-stale'" \
192
- "$(run "$ORIG_LEGACY_CFG" 'process.stdout.write(C.resolveCeiling(ROOT, undefined).ceiling_origin)')" \
193
- "unknown"
194
-
195
- ORIG_BADVAL="$TMP/origin-badval"; mkdir -p "$ORIG_BADVAL/.somi/somi-state"
196
- printf '{"ceiling": "low", "ceiling_origin": "bogus"}' > "$ORIG_BADVAL/.somi/somi-state/ceiling.json"
197
- expect_exit "a present state file with an invalid (not merely absent) ceiling_origin value dies loudly" 1 \
198
- "$ORIG_BADVAL" 'C.resolveCeiling(ROOT, undefined)'
83
+ # --- a persisted ceiling that was NOT set by cli/env yields to config and to the default ---------
84
+ STATES="$TMP/states"
85
+ for spec in 'legacy|{"ceiling":"high"}' 'default-origin|{"ceiling":"high","ceiling_origin":"default"}' 'config-origin|{"ceiling":"high","ceiling_origin":"config"}'; do
86
+ name="${spec%%|*}"; body="${spec#*|}"
87
+ d="$STATES-$name"; mkdir -p "$d/.somi/somi-state"; printf '%s' "$body" > "$d/.somi/somi-state/ceiling.json"
88
+ check "$name fixture was written" "$(state "$d")" "$body"
89
+ check "$name state (high) yields to the medium default" "$(res "$d")" "medium,false,default,default"
90
+ printf '{"cost": {"ceiling": "low"}}\n' > "$d/.somi/config.json"
91
+ check "$name state (high) yields to config low" "$(res "$d")" "low,false,config,config"
92
+ rm "$d/.somi/config.json"
93
+ done
94
+ # A state file written before origins were recorded: just {"ceiling":"high"}.
95
+ LEG="$TMP/legacy-literal"; mkdir -p "$LEG/.somi/somi-state"; printf '{"ceiling":"high"}' > "$LEG/.somi/somi-state/ceiling.json"
96
+ check "the legacy fixture really holds high" "$(state "$LEG")" '{"ceiling":"high"}'
97
+ check "a legacy state file does not outrank the default" "$(res "$LEG")" "medium,false,default,default"
98
+ d="$STATES-cli"; mkdir -p "$d/.somi/somi-state"; printf '{"ceiling":"low","ceiling_origin":"cli"}' > "$d/.somi/somi-state/ceiling.json"
99
+ check "a cli-origin state (low) does apply when config is silent" "$(res "$d")" "low,false,state,cli"
100
+ printf '{"cost": {"ceiling": "high"}}\n' > "$d/.somi/config.json"
101
+ check "... and config high outranks it" "$(res "$d")" "high,false,config,config"
102
+ d="$STATES-yield-write"; mkdir -p "$d/.somi/somi-state"; printf '{"ceiling":"high"}' > "$d/.somi/somi-state/ceiling.json"
103
+ run "$d" 'C.resolveCeiling(ROOT, undefined)' >/dev/null
104
+ check "a yielding legacy state file is left untouched by a bare resolve" "$(state "$d")" '{"ceiling":"high"}'
105
+ check "an explicit value persists over a legacy state file, with origin" \
106
+ "$(run "$d" 'C.resolveCeiling(ROOT, "low"); const fs = await import("node:fs"); const s = JSON.parse(fs.readFileSync(C.ceilingStatePath(ROOT), "utf8")); process.stdout.write(s.ceiling + "," + s.ceiling_origin + "," + s.ceiling_overrides[0].from)')" \
107
+ "low,cli,medium"
108
+
109
+ check "a present state file with an invalid (not merely absent) ceiling_origin value dies loudly" \
110
+ "$(run "$STATES-yield-write" 'const fs = await import("node:fs"); fs.writeFileSync(C.ceilingStatePath(ROOT), JSON.stringify({ceiling:"low",ceiling_origin:"bogus"})); try { C.resolveCeiling(ROOT, undefined); process.stdout.write("no"); } catch { process.stdout.write("threw"); }')" \
111
+ "threw"
112
+ check "config-stale is gone: no resolve ever reports it" \
113
+ "$(run "$B" 'process.stdout.write(String(JSON.stringify(C.resolveCeiling(ROOT, undefined)).includes("stale")))')" "false"
199
114
 
200
115
  # --- an UNPARSABLE state file (bad JSON, not just a wrong shape) dies loudly and names the file --
201
116
  STATE_BROKEN="$TMP/state-broken"; mkdir -p "$STATE_BROKEN/.somi/somi-state"
@@ -205,21 +120,26 @@ expect_exit "an unparsable state file dies loudly rather than a bare uncaught Sy
205
120
  check "the unparsable-state error names the ceiling.json file path, not a bare JSON position" \
206
121
  "$(run "$STATE_BROKEN" 'try { C.resolveCeiling(ROOT, undefined); } catch (e) { process.stdout.write(String(e.message.includes("ceiling.json"))); }')" \
207
122
  "true"
123
+ SYMS="$TMP/state-symlink"; mkdir -p "$SYMS/.somi/somi-state"; printf '{"ceiling":"low","ceiling_origin":"cli"}' > "$SYMS/real.json"
124
+ ln -s ../../real.json "$SYMS/.somi/somi-state/ceiling.json"
125
+ expect_exit "a symlinked state file is refused rather than followed" 1 "$SYMS" 'C.resolveCeiling(ROOT, undefined)'
208
126
 
209
127
  # --- malformed values die loudly, never silently defang the ceiling -----------------------------
128
+ M="$TMP/m"; mkdir -p "$M/.somi"
129
+ run "$M" 'C.resolveCeiling(ROOT, "low")' >/dev/null; run "$M" 'C.resolveCeiling(ROOT, "high")' >/dev/null
210
130
  expect_exit "a malformed explicit argument is rejected (uncaught, not swallowed)" 1 \
211
- "$B" 'C.resolveCeiling(ROOT, "unlimited")'
131
+ "$M" 'C.resolveCeiling(ROOT, "unlimited")'
212
132
  expect_exit "a malformed env var is rejected" 1 \
213
- "$B" 'process.env.SOMI_COST_CEILING = "unlimited"; C.resolveCeiling(ROOT, undefined)'
133
+ "$M" 'process.env.SOMI_COST_CEILING = "unlimited"; C.resolveCeiling(ROOT, undefined)'
214
134
  check "a rejected malformed value leaves the on-disk ceiling and override count untouched" \
215
- "$(run "$B" 'const fs = await import("node:fs"); const s = JSON.parse(fs.readFileSync(C.ceilingStatePath(ROOT), "utf8")); process.stdout.write(s.ceiling + "," + s.ceiling_overrides.length)')" \
216
- "medium,2"
135
+ "$(run "$M" 'const fs = await import("node:fs"); const s = JSON.parse(fs.readFileSync(C.ceilingStatePath(ROOT), "utf8")); process.stdout.write(s.ceiling + "," + s.ceiling_overrides.length)')" \
136
+ "high,2"
217
137
 
218
138
  CFG="$TMP/c"; mkdir -p "$CFG/.somi"
219
139
  printf '{"cost": {"ceiling": "unlimited"}}\n' > "$CFG/.somi/config.json"
220
- expect_exit "a malformed config-sourced ceiling dies loudly at bootstrap" 1 \
140
+ expect_exit "a malformed config-sourced ceiling dies loudly" 1 \
221
141
  "$CFG" 'C.resolveCeiling(ROOT, undefined)'
222
- check "a failed bootstrap leaves no state file behind" \
142
+ check "a failed config read leaves no state file behind" \
223
143
  "$(run "$CFG" 'const fs = await import("node:fs"); process.stdout.write(String(fs.existsSync(C.ceilingStatePath(ROOT))))')" \
224
144
  "false"
225
145
 
@@ -257,51 +177,36 @@ check "the thrown message names the state file, not a flag or env var" \
257
177
  "$(run "$CORRUPT" 'try { C.resolveCeiling(ROOT, undefined); } catch (e) { process.stdout.write(String(e.message.includes("ceiling.json"))); }')" \
258
178
  "true"
259
179
 
260
- # --- decideDispatch: a declared cost is a CAPABILITY SET; the ceiling selects the highest member
261
- # it permits, and always allows -- when no member fits, it selects the cheapest declared member
262
- # instead of blocking. A bare string is a one-member set (back-compat, not a special case). ------
263
-
264
- # One-member set (bare string) -- behaves exactly like a single declared tier.
265
- check "one-member set within ceiling: allow, selected echoes the sole member" \
266
- "$(run "$B" 'const d = C.decideDispatch("low", "high"); process.stdout.write(d.action + "," + d.selected + "," + d.supported.join("|"))')" \
267
- "allow,low,low"
268
-
269
- # The canonical case: a unit declaring only `high` has no cheaper mode. Under a ceiling that
270
- # permits none of its members, it still runs, at that one declared tier -- blocking it would not be
271
- # a saving, only a stoppage.
272
- check "single-high declaration under a medium ceiling: still allow, at high (its only declared tier)" \
273
- "$(run "$B" 'const d = C.decideDispatch("high", "medium"); process.stdout.write(d.action + "," + d.selected + "," + d.supported.join("|"))')" \
274
- "allow,high,high"
275
-
276
- # Two-member set -- selection across three ceilings: below both, between them, above both.
277
- # The "no member fits" case is the general form the single-high case generalizes: `selected` falls
278
- # to the cheapest declared member (supported[0]), not the unit's only member.
279
- check "two-member set [medium,high], ceiling low: no member fits -> allow at cheapest (medium)" \
280
- "$(run "$B" 'const d = C.decideDispatch(["medium","high"], "low"); process.stdout.write(d.action + "," + d.selected + "," + d.supported.join("|"))')" \
281
- "allow,medium,medium|high"
282
- check "two-member set [medium,high], ceiling medium: selects medium (highest permitted), silently" \
283
- "$(run "$B" 'const d = C.decideDispatch(["medium","high"], "medium"); process.stdout.write(d.action + "," + d.selected)')" \
284
- "allow,medium"
285
- check "two-member set [medium,high], ceiling high: selects high (highest permitted)" \
286
- "$(run "$B" 'const d = C.decideDispatch(["medium","high"], "high"); process.stdout.write(d.action + "," + d.selected)')" \
287
- "allow,high"
288
-
289
- # Three-member set -- selection at each of the three ceilings, proving the richest permitted
290
- # member is always the one chosen, never a lower one left on the table.
291
- check "three-member set [low,medium,high], ceiling low: selects low" \
292
- "$(run "$B" 'const d = C.decideDispatch(["low","medium","high"], "low"); process.stdout.write(d.action + "," + d.selected)')" \
293
- "allow,low"
294
- check "three-member set [low,medium,high], ceiling medium: selects medium, not low" \
295
- "$(run "$B" 'const d = C.decideDispatch(["low","medium","high"], "medium"); process.stdout.write(d.action + "," + d.selected)')" \
296
- "allow,medium"
297
- check "three-member set [low,medium,high], ceiling high: selects high, not a lower member" \
298
- "$(run "$B" 'const d = C.decideDispatch(["low","medium","high"], "high"); process.stdout.write(d.action + "," + d.selected)')" \
299
- "allow,high"
300
-
301
- # No code path ever yields a tier absent from the declared set, and every outcome is "allow": sweep
302
- # every set against every ceiling.
303
- check "action is always allow, and selected is always a member of the declared set, across every set x ceiling" \
304
- "$(run "$B" 'const sets=[["low"],["medium"],["high"],["low","medium"],["medium","high"],["low","high"],["low","medium","high"]]; const ceilings=["low","medium","high"]; let bad=0; for (const s of sets) for (const c of ceilings) { const d=C.decideDispatch(s,c); if (d.action!=="allow") bad++; if (!s.includes(d.selected)) bad++; } process.stdout.write(String(bad))')" \
180
+ # --- decideDispatch: the ceiling is a HARD cap. With no override the selected tier IS the ceiling,
181
+ # whatever the unit declared; the declared set (a capability set) is echoed in `supported` and used
182
+ # only to compute `degraded`. A bare string is a one-member set. The old never-degrade rule (run at
183
+ # the cheapest declared member when none fits) is reversed. Override / source cases: cost-hardcap.sh.
184
+ # Each case prints action,selected,supported,degraded,tier_source.
185
+ dd() { run "$B" "const d = C.decideDispatch($1); process.stdout.write([d.action, d.selected, d.supported.join('|'), d.degraded, d.tier_source].join(','))"; }
186
+
187
+ check "one-member set under a higher ceiling: runs at the ceiling, not degraded" \
188
+ "$(dd '"low", "high"')" "allow,high,low,false,ceiling"
189
+ check "single-high declaration under a medium ceiling: clamped to medium, degraded" \
190
+ "$(dd '"high", "medium"')" "allow,medium,high,true,ceiling"
191
+ check "single-high declaration under a low ceiling: clamped to low, degraded" \
192
+ "$(dd '["high"], "low"')" "allow,low,high,true,ceiling"
193
+ check "two-member set [medium,high], ceiling low: clamped to low, below every declared member" \
194
+ "$(dd '["medium","high"], "low"')" "allow,low,medium|high,true,ceiling"
195
+ check "two-member set [medium,high], ceiling medium: medium, degraded (below its highest member)" \
196
+ "$(dd '["medium","high"], "medium"')" "allow,medium,medium|high,true,ceiling"
197
+ check "two-member set [medium,high], ceiling high: high, not degraded" \
198
+ "$(dd '["medium","high"], "high"')" "allow,high,medium|high,false,ceiling"
199
+ check "three-member set, ceiling low: low, degraded" \
200
+ "$(dd '["low","medium","high"], "low"')" "allow,low,low|medium|high,true,ceiling"
201
+ check "three-member set, ceiling medium: medium, degraded" \
202
+ "$(dd '["low","medium","high"], "medium"')" "allow,medium,low|medium|high,true,ceiling"
203
+ check "three-member set, ceiling high: high, not degraded" \
204
+ "$(dd '["low","medium","high"], "high"')" "allow,high,low|medium|high,false,ceiling"
205
+
206
+ # Sweep: without an override, selected is exactly the ceiling for every set x ceiling; the selected
207
+ # tier may be outside the declared set, and degraded is exactly "selected below the top member".
208
+ check "no override: selected === ceiling and degraded matches rank, across every set x ceiling" \
209
+ "$(run "$B" 'const sets=[["low"],["medium"],["high"],["low","medium"],["medium","high"],["low","high"],["low","medium","high"]]; const R={low:0,medium:1,high:2}; let bad=0; for (const s of sets) for (const c of ["low","medium","high"]) { const d=C.decideDispatch(s,c); if (d.action!=="allow"||d.selected!==c||d.tier_source!=="ceiling") bad++; if (d.degraded !== (R[c] < R[s[s.length-1]])) bad++; } process.stdout.write(String(bad))')" \
305
210
  "0"
306
211
 
307
212
  check "the decision object is frozen -- a caller cannot rewrite the selection after the fact" \
@@ -310,9 +215,9 @@ check "the decision object is frozen -- a caller cannot rewrite the selection af
310
215
  check "the supported array itself is frozen too, not just the decision object wrapping it" \
311
216
  "$(run "$B" 'const d = C.decideDispatch(["low","high"], "high"); const before = d.supported.length; try { d.supported.push("medium"); } catch {} process.stdout.write(String(d.supported.length === before))')" \
312
217
  "true"
313
- check "the allow shape carries exactly action, selected, supported, ceiling" \
218
+ check "the allow shape carries exactly action, ceiling, degraded, selected, supported, tier_source" \
314
219
  "$(run "$B" 'process.stdout.write(Object.keys(C.decideDispatch(["low","high"], "high")).sort().join("|"))')" \
315
- "action|ceiling|selected|supported"
220
+ "action|ceiling|degraded|selected|supported|tier_source"
316
221
  expect_exit "an unrecognized cost value throws rather than silently allowing" 1 \
317
222
  "$B" 'C.decideDispatch("critical", "high")'
318
223
  expect_exit "an unrecognized ceiling value throws rather than silently allowing" 1 \
@@ -324,16 +229,18 @@ expect_exit "a duplicate in the declared set throws" 1 \
324
229
  expect_exit "an empty declared set throws rather than resolving to no-capability-silently" 1 \
325
230
  "$B" 'C.decideDispatch([], "high")'
326
231
 
327
- # --- modelForDispatch: the sanctioned composition, structurally unable to degrade ---------------
232
+ # --- modelForDispatch: the sanctioned composition from a decideDispatch decision ----------------
328
233
  check "modelForDispatch resolves the model for the SELECTED tier, not just any declared member" \
329
234
  "$(run "$B" 'process.stdout.write(C.modelForDispatch(C.decideDispatch(["low","medium"],"medium"), "claude-code"))')" \
330
235
  "sonnet"
331
- check "modelForDispatch on a one-member set resolves that member's model" \
236
+ check "modelForDispatch on a one-member set resolves the CEILING tier's model (hard cap), not the member's" \
332
237
  "$(run "$B" 'process.stdout.write(C.modelForDispatch(C.decideDispatch("low","high"), "claude-code"))')" \
333
- "haiku"
334
- # decideDispatch itself can no longer produce anything but "allow" -- the guard is unreachable by
335
- # construction now, but it is still exercised directly against a hand-built non-allow shape, so the
336
- # never-degrade backstop stays proven rather than merely unreachable-and-untested.
238
+ "opus"
239
+ check "modelForDispatch follows an override above the ceiling" \
240
+ "$(run "$B" 'process.stdout.write(C.modelForDispatch(C.decideDispatch("low","low","high"), "claude-code"))')" \
241
+ "opus"
242
+ # decideDispatch itself can only produce "allow" -- the guard is unreachable by construction, but it
243
+ # is still exercised directly against a hand-built non-allow shape, so the backstop stays proven.
337
244
  expect_exit "modelForDispatch refuses a decision shaped as anything but allow" 1 \
338
245
  "$B" 'C.modelForDispatch({ action: "refuse", supported: ["high"], ceiling: "low" }, "claude-code")'
339
246
  expect_exit "modelForDispatch refuses a missing decision" 1 \
@@ -341,3 +248,8 @@ expect_exit "modelForDispatch refuses a missing decision" 1 \
341
248
 
342
249
  echo "cost ceiling tests: $pass ok, $fail failed"
343
250
  [ "$fail" -eq 0 ] || exit 1
251
+ bash "$(dirname "${BASH_SOURCE[0]}")/cost-hardcap.sh" || exit 1
252
+ bash "$(dirname "${BASH_SOURCE[0]}")/ensure-default-config.sh" || exit 1
253
+ bash "$(dirname "${BASH_SOURCE[0]}")/somi-config.sh" || exit 1
254
+ bash "$(dirname "${BASH_SOURCE[0]}")/cost-lowering.sh" || exit 1
255
+ bash "$(dirname "${BASH_SOURCE[0]}")/agent-model-frontmatter.sh" || exit 1
@@ -0,0 +1,128 @@
1
+ #!/usr/bin/env bash
2
+ # Guard for the default tier table (scripts/lib/cost-defaults.mjs), the one source of each agent's tier:
3
+ # - the table lists exactly the agents/*.md files, minus the exempt front-door `somi` (existence, both
4
+ # directions);
5
+ # - no agents/*.md or commands/*.md declares `cost:` (the real check from scripts/validate.sh, extracted
6
+ # from a copy of it and run over fixtures, so this proves that block's own logic).
7
+ # Each check is also proven red: a copy of the table with one row removed or added, and fixtures that
8
+ # declare `cost:`, must fail the same comparison.
9
+ set -uo pipefail
10
+
11
+ ROOT_REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
12
+ LIB_SRC="$ROOT_REPO/scripts/lib"
13
+
14
+ pass=0; fail=0
15
+ ok() { pass=$((pass+1)); printf ' ok %s\n' "$1"; }
16
+ bad() { fail=$((fail+1)); printf ' FAIL %s\n' "$1"; }
17
+ check() { if [ "$2" = "$3" ]; then ok "$1"; else bad "$1 (want '$3', got '$2')"; fi; }
18
+
19
+ echo "== cost defaults =="
20
+
21
+ TMP="$(mktemp -d)"
22
+ trap 'rm -rf "$TMP"' EXIT
23
+
24
+ # Prints the agent names (agents/*.md minus the exempt `somi`), sorted.
25
+ agent_files() { for f in "$1"/*.md; do n="$(basename "$f" .md)"; [ "$n" = somi ] || printf '%s\n' "$n"; done | sort; }
26
+ # Prints the table's agent names for the lib dir given, sorted.
27
+ table_names() {
28
+ node --input-type=module -e "
29
+ const D = await import('$1/cost-defaults.mjs');
30
+ for (const k of Object.keys(D.AGENT_DEFAULT_TIERS)) console.log(k);" | sort
31
+ }
32
+
33
+ EXPECT="$(agent_files "$ROOT_REPO/agents")"
34
+ [ -n "$EXPECT" ] || bad "no agent file was found"
35
+ check "the table lists exactly the agent files (minus the exempt somi)" "$(table_names "$LIB_SRC")" "$EXPECT"
36
+
37
+ # The tier values themselves: a literal expected list (changing a tier is a deliberate edit here).
38
+ EXPECT_TIERS="somi-architecture-reviewer:high
39
+ somi-atlas:high
40
+ somi-coder:medium
41
+ somi-designer:high
42
+ somi-discovery-analyst:high
43
+ somi-impact:medium
44
+ somi-incident:medium
45
+ somi-plan-reviewer:high
46
+ somi-planner:medium
47
+ somi-pr:medium
48
+ somi-refactor-designer:high
49
+ somi-refactorer:medium
50
+ somi-reviewer:high
51
+ somi-sdlc-reviewer:high
52
+ somi-security-reviewer:high
53
+ somi-test-strategist:high"
54
+ table_tiers() {
55
+ node --input-type=module -e "
56
+ const D = await import('$1/cost-defaults.mjs');
57
+ for (const [k, v] of Object.entries(D.AGENT_DEFAULT_TIERS)) console.log(k + ':' + v);" | sort
58
+ }
59
+ EXPECT_TIERS="$(printf '%s\n' "$EXPECT_TIERS" | sort)"
60
+ check "the table's name:tier list equals the literal expected list" "$(table_tiers "$LIB_SRC")" "$EXPECT_TIERS"
61
+
62
+ # Red proofs on copies of the lib.
63
+ mutant() { # $1 = name, $2 = sed expression
64
+ mkdir -p "$TMP/$1"; cp "$LIB_SRC"/cost-*.mjs "$TMP/$1/"; sed -i "$2" "$TMP/$1/cost-defaults.mjs"
65
+ cmp -s "$LIB_SRC/cost-defaults.mjs" "$TMP/$1/cost-defaults.mjs" && bad "mutant $1 did not change the file"
66
+ }
67
+ mutant tier-changed "s/^ 'somi-coder': 'medium',/ 'somi-coder': 'high',/"
68
+ mutant row-removed "/^ 'somi-atlas': 'high',/d"
69
+ mutant row-added "s/^ 'somi-atlas': 'high',/ 'somi-atlas': 'high',\n 'somi-ghost': 'low',/"
70
+ if [ "$(table_tiers "$TMP/tier-changed")" != "$EXPECT_TIERS" ]; then ok "red proof: the tier list check fails for tier-changed"; else bad "red proof: a changed tier went unnoticed"; fi
71
+ for m in row-removed row-added; do
72
+ if [ "$(table_names "$TMP/$m")" != "$EXPECT" ]; then ok "red proof: existence check fails for $m"; else bad "red proof: existence check still holds for $m"; fi
73
+ done
74
+ mkdir -p "$TMP/agents-extra"; cp "$ROOT_REPO"/agents/*.md "$TMP/agents-extra/"; printf -- '---\nname: extra\n---\n' > "$TMP/agents-extra/extra.md"
75
+ if [ "$(agent_files "$TMP/agents-extra")" != "$(table_names "$LIB_SRC")" ]; then ok "red proof: an agent file with no table row fails the check"; else bad "red proof: an unlisted agent file went unnoticed"; fi
76
+
77
+ # No agent or command declares `cost:` -- the real block from scripts/validate.sh, over fixtures.
78
+ HELPER="$(awk '/^fm_has\(\) \{/{c=1} c{print} c&&/^\}$/{exit}' "$ROOT_REPO/scripts/validate.sh")"
79
+ BLOCK="$(awk '/^cost_failed=0$/{c=1} c{print} c&&/^done$/{exit}' "$ROOT_REPO/scripts/validate.sh")"
80
+ if [ -z "$HELPER" ] || [ -z "$BLOCK" ]; then bad "could not extract the cost check from scripts/validate.sh -- its markers may have moved"; else
81
+ SNIPPET="$HELPER"$'\n'"$BLOCK"$'\nexit $cost_failed'
82
+ fx() { (cd "$1" && bash -c "$SNIPPET" 2>"$1/err"); echo $?; }
83
+ mkdir -p "$TMP/fx-ok/agents" "$TMP/fx-ok/commands" "$TMP/fx-agent/agents" "$TMP/fx-agent/commands" "$TMP/fx-cmd/agents" "$TMP/fx-cmd/commands" "$TMP/fx-crlf/agents" "$TMP/fx-crlf/commands"
84
+ printf -- '---\nname: x\ndescription: d\n---\nbody line: cost: high\n' > "$TMP/fx-ok/agents/x.md"
85
+ printf -- '---\ndescription: d\n---\nbody\n' > "$TMP/fx-ok/commands/c.md"
86
+ for d in fx-agent fx-cmd fx-crlf; do cp "$TMP/fx-ok/agents/x.md" "$TMP/$d/agents/x.md"; cp "$TMP/fx-ok/commands/c.md" "$TMP/$d/commands/c.md"; done
87
+ printf -- '---\nname: x\ncost: medium, high\n---\n' > "$TMP/fx-agent/agents/x.md"
88
+ printf -- '---\ndescription: d\ncost: high\n---\n' > "$TMP/fx-cmd/commands/c.md"
89
+ printf -- '---\r\nname: x\r\ncost: high\r\n---\r\n' > "$TMP/fx-crlf/agents/x.md"
90
+ check "an agent and a command without a frontmatter cost: pass (a body line is not frontmatter)" "$(fx "$TMP/fx-ok")" "0"
91
+ check "red proof: an agent declaring cost: fails" "$(fx "$TMP/fx-agent")" "1"
92
+ check "red proof: a command declaring cost: fails" "$(fx "$TMP/fx-cmd")" "1"
93
+ check "red proof: a CRLF agent declaring cost: fails" "$(fx "$TMP/fx-crlf")" "1"
94
+ grep -q "agents/x.md declares cost:" "$TMP/fx-agent/err" && ok "the message names the offending file" || bad "message wrong: $(cat "$TMP/fx-agent/err")"
95
+ n="$(awk 'FNR==1{c=0} /^---$/{c++} c==1 && /^cost:/{print FILENAME; nextfile}' "$ROOT_REPO"/agents/*.md "$ROOT_REPO"/commands/*.md | wc -l)"
96
+ check "no shipped agent or command declares cost:" "$n" "0"
97
+ fi
98
+
99
+ # The shape of the default config.
100
+ node --input-type=module -e "
101
+ const D = await import('$LIB_SRC/cost-defaults.mjs');
102
+ const C = await import('$LIB_SRC/cost-ceiling.mjs');
103
+ const V = await import('$LIB_SRC/cost-config.mjs');
104
+ const cfg = D.buildDefaultConfig();
105
+ const out = [];
106
+ out.push(['ceiling is the built-in default', cfg.cost.ceiling === C.DEFAULT_CEILING && D.DEFAULT_CEILING_TIER === C.DEFAULT_CEILING]);
107
+ out.push(['validates through the shared validator', (() => { try { V.validateCostConfig(cfg.cost); return true; } catch { return false; } })()]);
108
+ out.push(['agents are sorted by name', Object.keys(cfg.cost.agents).join() === Object.keys(cfg.cost.agents).sort().join()]);
109
+ out.push(['the table is frozen and null-prototype', Object.isFrozen(D.AGENT_DEFAULT_TIERS) && Object.getPrototypeOf(D.AGENT_DEFAULT_TIERS) === null]);
110
+ out.push(['serialization is stable and is JSON, 2-space indent, trailing newline', D.serializeDefaultConfig() === D.serializeDefaultConfig() && D.serializeDefaultConfig() === JSON.stringify(cfg, null, 2) + '\n' && D.serializeDefaultConfig().endsWith('}\n')]);
111
+ out.push(['serialization round-trips to the same object', JSON.stringify(JSON.parse(D.serializeDefaultConfig())) === JSON.stringify(cfg)]);
112
+ out.push(['the somi front-door agent is not listed', !('somi' in D.AGENT_DEFAULT_TIERS)]);
113
+ out.push(['a bad table row is rejected', (() => { try { D.buildDefaultConfig({ 'somi-coder': 'turbo' }); return false; } catch { return true; } })()]);
114
+ out.push(['a bad agent name is rejected', (() => { try { D.buildDefaultConfig({ 'Bad_Name': 'low' }); return false; } catch { return true; } })()]);
115
+ for (const [n, v] of out) console.log((v ? 'ok' : 'FAIL') + '\t' + n);" > "$TMP/shape.out" 2>&1
116
+ while IFS=$'\t' read -r s n; do case "$s" in ok) ok "$n" ;; FAIL) bad "$n" ;; *) bad "unexpected output: $s $n" ;; esac; done < "$TMP/shape.out"
117
+
118
+ # No model names anywhere in the file or its output: the pattern comes from the shipped mapping.
119
+ PATTERN="$(node --input-type=module -e "
120
+ const M = await import('$LIB_SRC/cost-model.mjs');
121
+ console.log(Object.values(M.HOST_MODELS).flatMap((h) => Object.values(h)).join('|'))")"
122
+ if [ -n "$PATTERN" ] && ! grep -qiE "$PATTERN" "$LIB_SRC/cost-defaults.mjs" \
123
+ && ! node --input-type=module -e "const D = await import('$LIB_SRC/cost-defaults.mjs'); process.stdout.write(D.serializeDefaultConfig())" | grep -qiE "$PATTERN"; then
124
+ ok "no model name in the table file or in the serialized config"
125
+ else bad "a model name appears in the table file or the serialized config"; fi
126
+
127
+ echo "cost defaults tests: $pass ok, $fail failed"
128
+ [ "$fail" -eq 0 ] || exit 1