@skathio/somi 4.1.1 → 5.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.
Files changed (372) hide show
  1. package/.claude-plugin/icon.png +0 -0
  2. package/.claude-plugin/marketplace.json +8 -3
  3. package/.claude-plugin/plugin.json +9 -10
  4. package/.copilot-extension/extension.json +32 -40
  5. package/.copilot-extension/marketplace.json +8 -3
  6. package/README.md +88 -227
  7. package/VERSION +1 -1
  8. package/agents/somi-reviewer.md +21 -40
  9. package/agents/somi-security-reviewer.md +16 -90
  10. package/agents/somi-worker-high.md +11 -0
  11. package/agents/somi-worker-low.md +11 -0
  12. package/agents/somi-worker-medium.md +11 -0
  13. package/agents/somi.md +8 -75
  14. package/commands/somi-config.md +5 -14
  15. package/commands/somi-deliver.md +11 -0
  16. package/commands/somi-quality.md +10 -0
  17. package/commands/somi-review.md +10 -0
  18. package/commands/somi-status.md +11 -0
  19. package/commands/somi.md +10 -0
  20. package/hooks/hooks.json +29 -23
  21. package/hooks/lib/common.mjs +45 -34
  22. package/hooks/pre-tool/apply-tier.mjs +31 -0
  23. package/hooks/pre-tool/block-dangerous-bash.mjs +15 -0
  24. package/hooks/pre-tool/gate-dep-install.mjs +2 -2
  25. package/hooks/pre-tool/guard-protected-paths.mjs +9 -16
  26. package/hooks/pre-tool/guard-tier-config.mjs +17 -0
  27. package/hooks/pre-tool/reviewers-read-only.mjs +11 -0
  28. package/hooks/session-start/inject-digest.mjs +51 -0
  29. package/hooks/stop/review-due.mjs +44 -0
  30. package/hooks/user-prompt/capture-prompt.mjs +17 -0
  31. package/library/lenses/architecture.md +1 -0
  32. package/library/lenses/test-strategy.md +1 -0
  33. package/library/roles/accessibility.md +1 -0
  34. package/library/roles/answer.md +1 -0
  35. package/library/roles/atlas.md +1 -0
  36. package/library/roles/build.md +1 -0
  37. package/library/roles/debug.md +6 -0
  38. package/library/roles/design.md +1 -0
  39. package/library/roles/discover.md +1 -0
  40. package/library/roles/incident.md +1 -0
  41. package/library/roles/migrate.md +1 -0
  42. package/library/roles/perf.md +1 -0
  43. package/library/roles/plan.md +1 -0
  44. package/library/roles/refactor.md +1 -0
  45. package/library/roles/review.md +1 -0
  46. package/library/roles/security-review.md +1 -0
  47. package/library/roles/upgrade.md +1 -0
  48. package/library/skills/accessibility.md +16 -0
  49. package/library/skills/ci-infra-gotchas.md +12 -0
  50. package/library/skills/perf-method.md +10 -0
  51. package/library/skills/secure-coding.md +18 -0
  52. package/library/skills/testing-checklist.md +16 -0
  53. package/library/skills/threat-modeling.md +10 -0
  54. package/package.json +12 -17
  55. package/profiles/claude.json +21 -0
  56. package/profiles/copilot.json +5 -0
  57. package/rules/digest.md +40 -0
  58. package/scripts/check-links.mjs +65 -76
  59. package/scripts/lib/advisor.mjs +207 -0
  60. package/scripts/lib/check.mjs +167 -0
  61. package/scripts/lib/copilot-workflow.mjs +41 -0
  62. package/scripts/lib/doctor.mjs +91 -0
  63. package/scripts/lib/gates.mjs +69 -0
  64. package/scripts/lib/git-tree.mjs +201 -0
  65. package/scripts/lib/init.mjs +23 -0
  66. package/scripts/lib/ledger.mjs +38 -0
  67. package/scripts/lib/mcp-project-root.mjs +63 -67
  68. package/scripts/lib/migrate.mjs +73 -0
  69. package/scripts/lib/path-facts.mjs +2 -2
  70. package/scripts/lib/path-safety.mjs +6 -134
  71. package/scripts/lib/plan.mjs +69 -0
  72. package/scripts/lib/policy.mjs +103 -0
  73. package/scripts/lib/pr.mjs +28 -0
  74. package/scripts/lib/prompt.mjs +72 -0
  75. package/scripts/lib/quality/clones.mjs +76 -0
  76. package/scripts/lib/quality/css.mjs +43 -0
  77. package/scripts/lib/quality/gate.mjs +230 -0
  78. package/scripts/lib/quality/limits.mjs +97 -0
  79. package/scripts/lib/quality/metrics.mjs +159 -0
  80. package/scripts/lib/quality/parse.mjs +73 -0
  81. package/scripts/lib/quality/repo.mjs +69 -0
  82. package/scripts/lib/quality/run.mjs +58 -0
  83. package/scripts/lib/reviews.mjs +132 -0
  84. package/scripts/lib/routes.mjs +108 -0
  85. package/scripts/lib/setup.mjs +34 -0
  86. package/scripts/lib/shards.mjs +166 -0
  87. package/scripts/lib/signals.mjs +110 -0
  88. package/scripts/lib/state.mjs +42 -0
  89. package/scripts/lib/tiers.mjs +42 -0
  90. package/scripts/lib/update-notice.mjs +48 -0
  91. package/scripts/lib/views.mjs +62 -0
  92. package/scripts/lib/workflows.mjs +56 -0
  93. package/scripts/somi-mcp.mjs +182 -323
  94. package/scripts/somi.mjs +208 -0
  95. package/scripts/vendor/tree-sitter/LICENSE.vscode-tree-sitter-wasm +21 -0
  96. package/scripts/vendor/tree-sitter/LICENSE.web-tree-sitter +21 -0
  97. package/scripts/vendor/tree-sitter/cgmanifest.vscode-tree-sitter-wasm.json +16 -0
  98. package/scripts/vendor/tree-sitter/manifest.json +59 -0
  99. package/scripts/vendor/tree-sitter/package.json +3 -0
  100. package/scripts/vendor/tree-sitter/tree-sitter-css.wasm +0 -0
  101. package/scripts/vendor/tree-sitter/tree-sitter-javascript.wasm +0 -0
  102. package/scripts/vendor/tree-sitter/tree-sitter-tsx.wasm +0 -0
  103. package/scripts/vendor/tree-sitter/tree-sitter-typescript.wasm +0 -0
  104. package/scripts/vendor/tree-sitter/web-tree-sitter.js +4081 -0
  105. package/scripts/vendor/tree-sitter/web-tree-sitter.wasm +0 -0
  106. package/skills/somi-expertise/SKILL.md +6 -0
  107. package/workflows/deliver-stages.js +81 -0
  108. package/.claude/settings.json +0 -131
  109. package/agents/somi-architecture-reviewer.md +0 -101
  110. package/agents/somi-atlas.md +0 -100
  111. package/agents/somi-coder.md +0 -60
  112. package/agents/somi-designer.md +0 -36
  113. package/agents/somi-discovery-analyst.md +0 -57
  114. package/agents/somi-flow.md +0 -47
  115. package/agents/somi-general.md +0 -36
  116. package/agents/somi-impact.md +0 -95
  117. package/agents/somi-incident.md +0 -94
  118. package/agents/somi-plan-reviewer.md +0 -68
  119. package/agents/somi-planner.md +0 -57
  120. package/agents/somi-pr.md +0 -99
  121. package/agents/somi-refactor-designer.md +0 -81
  122. package/agents/somi-refactorer.md +0 -99
  123. package/agents/somi-sdlc-reviewer.md +0 -67
  124. package/agents/somi-test-strategist.md +0 -87
  125. package/commands/adopt.md +0 -14
  126. package/commands/architecture-review.md +0 -19
  127. package/commands/atlas.md +0 -19
  128. package/commands/code-loop.md +0 -19
  129. package/commands/code-parallel.md +0 -19
  130. package/commands/code.md +0 -19
  131. package/commands/debug.md +0 -19
  132. package/commands/design.md +0 -19
  133. package/commands/discover.md +0 -19
  134. package/commands/impact.md +0 -19
  135. package/commands/incident.md +0 -19
  136. package/commands/plan-loop.md +0 -19
  137. package/commands/plan.md +0 -19
  138. package/commands/pr.md +0 -19
  139. package/commands/refactor-design.md +0 -19
  140. package/commands/refactor.md +0 -19
  141. package/commands/release-readiness.md +0 -19
  142. package/commands/review-panel.md +0 -19
  143. package/commands/review.md +0 -19
  144. package/commands/security-review.md +0 -19
  145. package/commands/ship-loop.md +0 -19
  146. package/commands/ship.md +0 -19
  147. package/commands/test-strategy.md +0 -19
  148. package/commands/upgrade.md +0 -19
  149. package/examples/README.md +0 -19
  150. package/examples/code-review-example.md +0 -88
  151. package/examples/discovery-example.md +0 -200
  152. package/examples/feature-plan-example.md +0 -377
  153. package/examples/full-pipeline-example.md +0 -188
  154. package/examples/sample-consumer/.claude/settings.json +0 -29
  155. package/examples/sample-consumer/CLAUDE.md +0 -78
  156. package/examples/sample-consumer/README.md +0 -102
  157. package/hooks/post-tool/lint-changed-files.mjs +0 -209
  158. package/hooks/pre-tool/guard-cost-lowering.mjs +0 -192
  159. package/hooks/session-start/detect-repo-instructions.mjs +0 -287
  160. package/hooks/session-start/ensure-default-config.mjs +0 -113
  161. package/hooks/user-prompt-submit/inject-workflow-context.mjs +0 -580
  162. package/rules/00-priorities.md +0 -48
  163. package/rules/10-solid.md +0 -87
  164. package/rules/20-clean-code.md +0 -62
  165. package/rules/30-security-owasp.md +0 -83
  166. package/rules/40-engineering-practices.md +0 -65
  167. package/rules/50-collaboration.md +0 -114
  168. package/rules/99-overrides.md +0 -52
  169. package/rules/CLAUDE.md +0 -200
  170. package/scripts/check-session-artefacts.mjs +0 -149
  171. package/scripts/check-size-budget.mjs +0 -137
  172. package/scripts/generate-digest.mjs +0 -176
  173. package/scripts/lib/agent-info.mjs +0 -85
  174. package/scripts/lib/agent-registry.mjs +0 -92
  175. package/scripts/lib/cost-ceiling.mjs +0 -328
  176. package/scripts/lib/cost-config-edit.mjs +0 -102
  177. package/scripts/lib/cost-config.mjs +0 -64
  178. package/scripts/lib/cost-defaults.mjs +0 -53
  179. package/scripts/lib/cost-lowering.mjs +0 -89
  180. package/scripts/lib/cost-model.mjs +0 -110
  181. package/scripts/lib/dispatch-resolver.mjs +0 -243
  182. package/scripts/lib/escalation-facts.mjs +0 -146
  183. package/scripts/lib/escalation.mjs +0 -129
  184. package/scripts/lib/findings-ledger.mjs +0 -241
  185. package/scripts/lib/install-text.mjs +0 -97
  186. package/scripts/lib/loop-caps.mjs +0 -140
  187. package/scripts/lib/loop-diff.mjs +0 -38
  188. package/scripts/lib/loop-dispatch.mjs +0 -19
  189. package/scripts/lib/loop-engine.mjs +0 -227
  190. package/scripts/lib/loop-escalate.mjs +0 -49
  191. package/scripts/lib/loop-exit.mjs +0 -47
  192. package/scripts/lib/loop-notes-compact.mjs +0 -72
  193. package/scripts/lib/loop-notes.mjs +0 -282
  194. package/scripts/lib/loop-review.mjs +0 -395
  195. package/scripts/lib/loop-state.mjs +0 -44
  196. package/scripts/lib/loop-step-close.mjs +0 -75
  197. package/scripts/lib/loop-step-kit.mjs +0 -179
  198. package/scripts/lib/loop-step-review.mjs +0 -140
  199. package/scripts/lib/loop-step-tool.mjs +0 -89
  200. package/scripts/lib/loop-step.mjs +0 -303
  201. package/scripts/lib/run-budget.mjs +0 -28
  202. package/scripts/lib/run-card.mjs +0 -73
  203. package/scripts/lib/size-tier.mjs +0 -184
  204. package/scripts/lib/tier-record.mjs +0 -82
  205. package/scripts/size-budget-overrides.json +0 -58
  206. package/scripts/somi-card.mjs +0 -39
  207. package/scripts/somi-check.mjs +0 -237
  208. package/scripts/somi-config.mjs +0 -421
  209. package/scripts/somi-dispatch.mjs +0 -141
  210. package/scripts/somi-findings.mjs +0 -100
  211. package/scripts/somi-loop.mjs +0 -139
  212. package/scripts/somi-tier.mjs +0 -393
  213. package/scripts/validate.sh +0 -653
  214. package/skills/api-design/SKILL.md +0 -147
  215. package/skills/clean-code/SKILL.md +0 -142
  216. package/skills/flow-adopt/SKILL.md +0 -92
  217. package/skills/flow-architecture-review/SKILL.md +0 -94
  218. package/skills/flow-atlas/SKILL.md +0 -34
  219. package/skills/flow-code/SKILL.md +0 -167
  220. package/skills/flow-code-loop/SKILL.md +0 -263
  221. package/skills/flow-code-parallel/SKILL.md +0 -160
  222. package/skills/flow-debug/SKILL.md +0 -141
  223. package/skills/flow-design/SKILL.md +0 -170
  224. package/skills/flow-discover/SKILL.md +0 -204
  225. package/skills/flow-impact/SKILL.md +0 -35
  226. package/skills/flow-incident/SKILL.md +0 -49
  227. package/skills/flow-plan/SKILL.md +0 -254
  228. package/skills/flow-plan-loop/SKILL.md +0 -254
  229. package/skills/flow-pr/SKILL.md +0 -34
  230. package/skills/flow-refactor/SKILL.md +0 -58
  231. package/skills/flow-refactor-design/SKILL.md +0 -97
  232. package/skills/flow-release-readiness/SKILL.md +0 -82
  233. package/skills/flow-review/SKILL.md +0 -193
  234. package/skills/flow-review-panel/SKILL.md +0 -154
  235. package/skills/flow-security-review/SKILL.md +0 -83
  236. package/skills/flow-ship/SKILL.md +0 -125
  237. package/skills/flow-ship-loop/SKILL.md +0 -274
  238. package/skills/flow-somi-config/SKILL.md +0 -142
  239. package/skills/flow-status/SKILL.md +0 -43
  240. package/skills/flow-test-strategy/SKILL.md +0 -83
  241. package/skills/flow-upgrade/SKILL.md +0 -82
  242. package/skills/market-research/SKILL.md +0 -125
  243. package/skills/observability/SKILL.md +0 -133
  244. package/skills/owasp-defense/SKILL.md +0 -141
  245. package/skills/plan-review/SKILL.md +0 -83
  246. package/skills/reasoning-craft/SKILL.md +0 -213
  247. package/skills/requirements-engineering/SKILL.md +0 -137
  248. package/skills/role-coder/SKILL.md +0 -247
  249. package/skills/role-designer/SKILL.md +0 -172
  250. package/skills/role-discovery-analyst/SKILL.md +0 -288
  251. package/skills/role-planner/SKILL.md +0 -353
  252. package/skills/role-reviewer/SKILL.md +0 -218
  253. package/skills/rules/SKILL.md +0 -47
  254. package/skills/sdlc-process/SKILL.md +0 -74
  255. package/skills/solid-principles/SKILL.md +0 -143
  256. package/skills/somi-checkpoint/SKILL.md +0 -95
  257. package/skills/somi-dispatch/SKILL.md +0 -259
  258. package/skills/somi-routing/SKILL.md +0 -158
  259. package/skills/testing-playbook/SKILL.md +0 -133
  260. package/skills/threat-modeling/SKILL.md +0 -117
  261. package/templates/ADR.md.tmpl +0 -73
  262. package/templates/ATLAS.md.tmpl +0 -75
  263. package/templates/BRD.md.tmpl +0 -68
  264. package/templates/BRIEF.md.tmpl +0 -119
  265. package/templates/CONTEXT.md.tmpl +0 -74
  266. package/templates/DECISIONS.md.tmpl +0 -105
  267. package/templates/DESIGN.md.tmpl +0 -83
  268. package/templates/DIARY.md.tmpl +0 -70
  269. package/templates/DOD.md.tmpl +0 -90
  270. package/templates/FRD.md.tmpl +0 -64
  271. package/templates/PHASE.md.tmpl +0 -80
  272. package/templates/PROGRESS.md.tmpl +0 -63
  273. package/templates/RCA.md.tmpl +0 -53
  274. package/templates/RD-README.md.tmpl +0 -70
  275. package/templates/RESEARCH.md.tmpl +0 -96
  276. package/templates/REVIEW.md.tmpl +0 -82
  277. package/templates/SDD.md.tmpl +0 -82
  278. package/templates/SOMI-README.md.tmpl +0 -83
  279. package/templates/SPEC.md.tmpl +0 -123
  280. package/templates/SRS.md.tmpl +0 -85
  281. package/templates/TDD.md.tmpl +0 -77
  282. package/tests/hooks/cases/audit-log.json +0 -21
  283. package/tests/hooks/cases/block-dangerous-bash.json +0 -166
  284. package/tests/hooks/cases/block-secret-writes.json +0 -140
  285. package/tests/hooks/cases/detect-repo-instructions.json +0 -105
  286. package/tests/hooks/cases/gate-dep-install.json +0 -42
  287. package/tests/hooks/cases/guard-protected-paths.json +0 -222
  288. package/tests/hooks/cases/inject-workflow-context.json +0 -239
  289. package/tests/hooks/cases/lint-changed-files.json +0 -120
  290. package/tests/hooks/run.sh +0 -392
  291. package/tests/scripts/agent-model-frontmatter.sh +0 -38
  292. package/tests/scripts/agent-names.sh +0 -77
  293. package/tests/scripts/check-frontmatter.sh +0 -37
  294. package/tests/scripts/check-links.sh +0 -105
  295. package/tests/scripts/check-session-artefacts.sh +0 -60
  296. package/tests/scripts/check-size-budget.sh +0 -71
  297. package/tests/scripts/checkpoint-gate.sh +0 -131
  298. package/tests/scripts/convergence-runner.sh +0 -1514
  299. package/tests/scripts/cost-ceiling.sh +0 -255
  300. package/tests/scripts/cost-defaults.sh +0 -130
  301. package/tests/scripts/cost-hardcap.sh +0 -338
  302. package/tests/scripts/cost-lowering.sh +0 -261
  303. package/tests/scripts/cost-model.sh +0 -196
  304. package/tests/scripts/dispatch-guard.sh +0 -108
  305. package/tests/scripts/dispatch-line-gate.sh +0 -61
  306. package/tests/scripts/dispatch-reference-gate.sh +0 -173
  307. package/tests/scripts/ensure-default-config.sh +0 -162
  308. package/tests/scripts/escalation.sh +0 -409
  309. package/tests/scripts/eval-runner.sh +0 -3692
  310. package/tests/scripts/evals-packaging.sh +0 -224
  311. package/tests/scripts/fact-sheet.sh +0 -296
  312. package/tests/scripts/fixtures/legacy-shard-no-schema.json +0 -50
  313. package/tests/scripts/fixtures/mock-stdin-read-throw.cjs +0 -18
  314. package/tests/scripts/fixtures/size-tier-cases.json +0 -87
  315. package/tests/scripts/generate-digest.sh +0 -180
  316. package/tests/scripts/global-config.sh +0 -167
  317. package/tests/scripts/goldens/loop-state-breach.json +0 -1
  318. package/tests/scripts/goldens/loop-state-done.json +0 -1
  319. package/tests/scripts/goldens/somi-findings.json +0 -336
  320. package/tests/scripts/goldens/somi-loop.json +0 -168
  321. package/tests/scripts/lib/adr-shape.mjs +0 -41
  322. package/tests/scripts/lib/agent-registry-tests.mjs +0 -95
  323. package/tests/scripts/lib/brief-only-guard.mjs +0 -27
  324. package/tests/scripts/lib/checkpoint-gate.mjs +0 -91
  325. package/tests/scripts/lib/digest-marker-coupling.mjs +0 -52
  326. package/tests/scripts/lib/dispatch-reference-gate.mjs +0 -121
  327. package/tests/scripts/lib/escalation-facts-tests.mjs +0 -140
  328. package/tests/scripts/lib/escalation-tests.mjs +0 -154
  329. package/tests/scripts/lib/findings-ledger-tests.mjs +0 -118
  330. package/tests/scripts/lib/loop-engine-tests.mjs +0 -287
  331. package/tests/scripts/lib/loop-escalate-tests.mjs +0 -356
  332. package/tests/scripts/lib/loop-flow-gate.mjs +0 -173
  333. package/tests/scripts/lib/loop-notes-tests.mjs +0 -384
  334. package/tests/scripts/lib/loop-step-cli-tests.mjs +0 -156
  335. package/tests/scripts/lib/loop-step-close-tests.mjs +0 -351
  336. package/tests/scripts/lib/loop-step-lenient-tests.mjs +0 -114
  337. package/tests/scripts/lib/loop-step-state-tests.mjs +0 -127
  338. package/tests/scripts/lib/loop-step-tests.mjs +0 -499
  339. package/tests/scripts/lib/loop-step-unwritten-tests.mjs +0 -55
  340. package/tests/scripts/lib/multipass-dependence.mjs +0 -192
  341. package/tests/scripts/lib/multipass-source-identity.mjs +0 -243
  342. package/tests/scripts/lib/path-facts-tests.mjs +0 -198
  343. package/tests/scripts/lib/path-safety-tests.mjs +0 -201
  344. package/tests/scripts/lib/reference-pair.mjs +0 -61
  345. package/tests/scripts/lib/retirement-gate.mjs +0 -172
  346. package/tests/scripts/lib/run-suites.mjs +0 -139
  347. package/tests/scripts/lib/shell-embedded-js.mjs +0 -47
  348. package/tests/scripts/lib/ship-loop-sizing-guard.mjs +0 -52
  349. package/tests/scripts/lib/site-roadmap-gate.mjs +0 -64
  350. package/tests/scripts/lib/size-tier-drift.mjs +0 -74
  351. package/tests/scripts/lib/sizing-score-tests.mjs +0 -210
  352. package/tests/scripts/lib/sizing-set-tests.mjs +0 -131
  353. package/tests/scripts/lib/somi-mcp-client.mjs +0 -102
  354. package/tests/scripts/lib/somi-mcp-config-tests.mjs +0 -92
  355. package/tests/scripts/lib/somi-mcp-loop-tests.mjs +0 -226
  356. package/tests/scripts/lib/somi-mcp-tests.mjs +0 -491
  357. package/tests/scripts/lib/somi-mcp-tier-tests.mjs +0 -64
  358. package/tests/scripts/lib/somi-mcp-tool-schema.mjs +0 -38
  359. package/tests/scripts/loop-engine.sh +0 -22
  360. package/tests/scripts/loop-flow-gate.sh +0 -11
  361. package/tests/scripts/retirement-gate.sh +0 -306
  362. package/tests/scripts/review-panel-seats.sh +0 -32
  363. package/tests/scripts/run-card.sh +0 -57
  364. package/tests/scripts/run-suites.sh +0 -74
  365. package/tests/scripts/run.sh +0 -689
  366. package/tests/scripts/site-roadmap-gate.sh +0 -76
  367. package/tests/scripts/size-tier.sh +0 -562
  368. package/tests/scripts/somi-agent-dispatch-contract.sh +0 -224
  369. package/tests/scripts/somi-config.sh +0 -286
  370. package/tests/scripts/somi-dispatch.sh +0 -465
  371. package/tests/scripts/somi-mcp.sh +0 -15
  372. package/tests/scripts/suite-plan.json +0 -20
@@ -1,338 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Guard for the shared `cost.*` config validator (scripts/lib/cost-config.mjs), read through
3
- # readCostConfig. Each rejection case is also proven red: the check is deleted from a COPY of the
4
- # lib and the same case must then be accepted, so a passing run means the check is what rejects it.
5
- set -uo pipefail
6
-
7
- ROOT_REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
8
- LIB_SRC="$ROOT_REPO/scripts/lib"
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 "== cost config validator =="
15
-
16
- TMP="$(mktemp -d)"
17
- trap 'rm -rf "$TMP"' EXIT
18
-
19
- # $1 = lib dir, $2 = config.json body. Prints "ok" or "reject: <message>".
20
- verdict() {
21
- local root; root="$(mktemp -d -p "$TMP")"; mkdir -p "$root/.somi"
22
- printf '%s' "$2" > "$root/.somi/config.json"
23
- node --input-type=module -e "
24
- const { readCostConfig } = await import('$1/cost-ceiling.mjs');
25
- try { readCostConfig('$root'); console.log('ok'); } catch (e) { console.log('reject: ' + e.message); }"
26
- }
27
-
28
- mutant() { # $1 = name, $2 = sed expression deleting one check; builds $TMP/$1 from the real lib
29
- mkdir -p "$TMP/$1"
30
- cp "$LIB_SRC/cost-ceiling.mjs" "$LIB_SRC/cost-model.mjs" "$LIB_SRC/cost-config.mjs" "$LIB_SRC/config-files.mjs" "$TMP/$1/"
31
- sed -i "$2" "$TMP/$1/cost-config.mjs"
32
- cmp -s "$LIB_SRC/cost-config.mjs" "$TMP/$1/cost-config.mjs" && { bad "mutant $1 did not change the file"; return 1; }
33
- return 0
34
- }
35
-
36
- # name | config body | mutant that must make it pass | substring of the real error
37
- reject_case() {
38
- local name="$1" body="$2" mut="$3" want="$4" got red
39
- got="$(verdict "$LIB_SRC" "$body")"
40
- case "$got" in "reject: "*"$want"*) ok "rejects $name" ;; *) bad "rejects $name (got: $got)" ;; esac
41
- red="$(verdict "$TMP/$mut" "$body")"
42
- if [ "$red" = "ok" ]; then ok "red proof: $name is accepted once its check is removed"; else bad "red proof: $name still rejected without its check ($red)"; fi
43
- }
44
-
45
- mutant unknown-key 's/!COST_CONFIG_KEYS.includes(key)/false/' &&
46
- mutant forbidden 's/FORBIDDEN_KEYS.includes(name)/false/' &&
47
- mutant name 's/!AGENT_NAME_RE.test(name)/false/' &&
48
- mutant tier 's/!VALID_COSTS.includes(tier)/false/' &&
49
- mutant agents-type 's/agents === null || typeof agents !== .object. || Array.isArray(agents)/false/' || exit 1
50
-
51
- reject_case "an unknown cost key" '{"cost":{"cieling":"low"}}' unknown-key 'unrecognized key "cieling"'
52
- reject_case "a constructor agent name" '{"cost":{"agents":{"constructor":"low"}}}' forbidden 'forbidden agent name "constructor"'
53
- reject_case "a prototype agent name" '{"cost":{"agents":{"prototype":"low"}}}' forbidden 'forbidden agent name "prototype"'
54
- reject_case "a malformed agent name" '{"cost":{"agents":{"Bad_Name":"low"}}}' name 'invalid agent name'
55
- reject_case "a bad tier" '{"cost":{"agents":{"somi-coder":"turbo"}}}' tier 'cost.agents.somi-coder expected one of'
56
- reject_case "a non-object agents map" '{"cost":{"agents":5}}' agents-type 'cost.agents'
57
-
58
- # __proto__ is an own key after JSON.parse. Both the allowlist and the name regex already reject it,
59
- # so no single-check mutant can turn it green; assert the real behaviour only.
60
- for body in '{"cost":{"__proto__":{"ceiling":"low"}}}' '{"cost":{"agents":{"__proto__":"low"}}}'; do
61
- case "$(verdict "$LIB_SRC" "$body")" in reject:*) ok "rejects __proto__ ($body)" ;; *) bad "rejects __proto__ ($body)" ;; esac
62
- done
63
-
64
- reject_msg() { # $1 = label, $2 = body, $3 = message fragment
65
- case "$(verdict "$LIB_SRC" "$2")" in *"$3"*) ok "$1" ;; *) bad "$1" ;; esac
66
- }
67
- reject_msg "__proto__ at the top level names the key" '{"cost":{"__proto__":{"ceiling":"low"}}}' 'unrecognized key "__proto__"'
68
- reject_msg "__proto__ as an agent name is forbidden" '{"cost":{"agents":{"__proto__":"low"}}}' 'forbidden agent name "__proto__"'
69
- reject_msg "agents: null is rejected" '{"cost":{"agents":null}}' 'cost.agents'
70
- reject_msg "agents: [] is rejected" '{"cost":{"agents":[]}}' 'cost.agents'
71
- case "$(verdict "$LIB_SRC" "{\"cost\":{\"$(printf 'x%.0s' $(seq 1 300))\":1}}")" in
72
- *"..."*) ok "a long unknown key is truncated in the message" ;; *) bad "a long unknown key is truncated in the message" ;; esac
73
-
74
- case "$(verdict "$LIB_SRC" '{"cost":{"ceiling":"medium","mapping":{},"agents":{"somi-coder":"medium","somi-security-reviewer":"high"}}}')" in
75
- ok) ok "accepts ceiling, mapping and an agents map together" ;; *) bad "accepts ceiling, mapping and an agents map together" ;; esac
76
- case "$(verdict "$LIB_SRC" '{"cost":{"agents":{}}}')" in ok) ok "accepts an empty agents map" ;; *) bad "accepts an empty agents map" ;; esac
77
-
78
- # ---- clamp, override-wins, tier_source/degraded/declared (decideDispatch + resolver + CLI) ----------
79
- echo "== hard cap / override =="
80
- CLI="$ROOT_REPO/scripts/somi-dispatch.mjs"
81
-
82
- dd() { # $1 = lib dir, $2 = decideDispatch args -> selected,degraded,tier_source
83
- node --input-type=module -e "
84
- const C = await import('$1/cost-ceiling.mjs');
85
- const d = C.decideDispatch($2); process.stdout.write([d.selected, d.degraded, d.tier_source].join(','))"
86
- }
87
- eq() { if [ "$2" = "$3" ]; then ok "$1"; else bad "$1 (want '$3', got '$2')"; fi; }
88
-
89
- # Red proofs: each mutant breaks exactly one rule in a COPY of the lib; the real case must then fail.
90
- mutant_sel() { # $1 = name, $2 = replacement for the selection expression
91
- mkdir -p "$TMP/$1"; cp "$LIB_SRC/cost-ceiling.mjs" "$LIB_SRC/cost-model.mjs" "$LIB_SRC/cost-config.mjs" "$LIB_SRC/config-files.mjs" "$TMP/$1/"
92
- sed -i "s/const selected = override === undefined ? ceiling : override;/const selected = $2;/" "$TMP/$1/cost-ceiling.mjs"
93
- cmp -s "$LIB_SRC/cost-ceiling.mjs" "$TMP/$1/cost-ceiling.mjs" && bad "mutant $1 did not change the file"
94
- }
95
- mutant_sel no-clamp 'override === undefined ? supported[0] : override' # old never-degrade: ignore the ceiling
96
- mutant_sel no-override 'ceiling' # override ignored
97
-
98
- eq "clamp: high-only agent under ceiling low -> low, degraded, from the ceiling" \
99
- "$(dd "$LIB_SRC" '["high"], "low"')" "low,true,ceiling"
100
- eq "red proof (clamp): without the clamp the same case runs at high" \
101
- "$(dd "$TMP/no-clamp" '["high"], "low"')" "high,false,ceiling"
102
- eq "override wins: designer-style high override under ceiling low -> high, source override" \
103
- "$(dd "$LIB_SRC" '["high"], "low", "high"')" "high,false,override"
104
- eq "red proof (override-wins): with overrides ignored the same case stays at low" \
105
- "$(dd "$TMP/no-override" '["high"], "low", "high"')" "low,true,override"
106
- eq "an override below the ceiling also wins" \
107
- "$(dd "$LIB_SRC" '["low","medium","high"], "high", "low"')" "low,true,override"
108
- eq "an override at the declared top is not degraded" \
109
- "$(dd "$LIB_SRC" '["medium","high"], "low", "high"')" "high,false,override"
110
- case "$(node --input-type=module -e "
111
- const C = await import('$LIB_SRC/cost-ceiling.mjs');
112
- try { C.decideDispatch(['high'], 'low', 'turbo'); console.log('ok'); } catch (e) { console.log('reject'); }")" in
113
- reject) ok "decideDispatch rejects an override that is not a tier" ;; *) bad "decideDispatch rejects an override that is not a tier" ;; esac
114
-
115
- # Resolver + CLI against a throwaway project; CLI agents come from the real agents/ directory.
116
- proj() { local r; r="$(mktemp -d -p "$TMP")"; mkdir -p "$r/.somi"; [ -n "${1:-}" ] && printf '%s' "$1" > "$r/.somi/config.json"; echo "$r"; }
117
- cli() { local r="$1"; shift; CLAUDE_PROJECT_DIR="$r" env -u SOMI_COST_CEILING node "$CLI" resolve "$@"; }
118
- field() { node -e "const r=JSON.parse(process.argv[1]); const v=r[process.argv[2]]; process.stdout.write(Array.isArray(v)?v.join('|'):String(v))" "$1" "$2"; }
119
-
120
- P="$(proj '{"cost":{"ceiling":"low"}}')"
121
- out="$(cli "$P" --agent somi-designer)"
122
- eq "CLI: high-only designer, ceiling low, no override -> tier low" "$(field "$out" tier)" "low"
123
- eq "CLI: ... degraded true" "$(field "$out" degraded)" "true"
124
- eq "CLI: ... declared [high]" "$(field "$out" declared)" "high"
125
- eq "CLI: ... tier_source ceiling" "$(field "$out" tier_source)" "ceiling"
126
- eq "CLI: ... supported still reported" "$(field "$out" supported)" "high"
127
-
128
- P="$(proj '{"cost":{"ceiling":"low","agents":{"somi-designer":"high"}}}')"
129
- out="$(cli "$P" --agent somi-designer)"
130
- eq "CLI: cost.agents.somi-designer=high beats ceiling low -> tier high" "$(field "$out" tier)" "high"
131
- eq "CLI: ... tier_source override" "$(field "$out" tier_source)" "override"
132
- eq "CLI: ... not degraded" "$(field "$out" degraded)" "false"
133
- out="$(cli "$P" --agent somi-coder)"
134
- eq "CLI: an agent with no override (coder, declared low|medium) resolves to the ceiling" "$(field "$out" tier)" "low"
135
- eq "CLI: ... tier_source ceiling" "$(field "$out" tier_source)" "ceiling"
136
-
137
- P="$(proj '{"cost":{"ceiling":"high"}}')"
138
- out="$(cli "$P" --agent somi-coder)"
139
- eq "CLI: ceiling above the declared set -> the ceiling (high), not degraded" "$(field "$out" tier)/$(field "$out" degraded)" "high/false"
140
-
141
- P="$(proj '{"cost":{"agents":{"somi-designer":"turbo"}}}')"; rc=0
142
- cli "$P" --agent somi-designer >/dev/null 2>&1 || rc=$?
143
- eq "CLI: a bad override value exits 67" "$rc" "67"
144
- P="$(proj '{"cost":{"agents":{"nonexistent-agent":"high"}}}')"; rc=0
145
- cli "$P" --agent somi-coder >/dev/null 2>&1 || rc=$?
146
- eq "CLI: an unknown agent name in cost.agents is not an error" "$rc" "0"
147
-
148
- # An agent file literally named "constructor" has no default-tier table entry, so it is 66: the null-prototype
149
- # table must not hand back an inherited value. (The cost.agents hasOwn guard is now unreachable from
150
- # resolveDispatch by construction: the table lookup fails first.)
151
- INST="$TMP/inst"; mkdir -p "$INST/agents"
152
- printf -- '---\nname: constructor\n---\n' > "$INST/agents/constructor.md"
153
- P="$(proj '{"cost":{"ceiling":"low","agents":{"somi-coder":"high"}}}')"
154
- eq "resolver: --agent constructor (no table entry) is 66 and names the default tier table, not an inherited value" \
155
- "$(node --input-type=module -e "
156
- const R = await import('$LIB_SRC/dispatch-resolver.mjs');
157
- const o = R.resolveDispatch({ agent: 'constructor', projectRoot: '$P', installRoot: '$INST' });
158
- process.stdout.write(o.ok ? o.result.tier + ',' + o.result.tier_source : 'code ' + o.code + ' ' + o.message)")" "code 66 agent \"constructor\" has no entry in the default tier table (scripts/lib/cost-defaults.mjs)"
159
-
160
- eq "resolver re-exports the one AGENT_NAME_RE from cost-config" \
161
- "$(node --input-type=module -e "
162
- const R = await import('$LIB_SRC/dispatch-resolver.mjs'); const K = await import('$LIB_SRC/cost-config.mjs');
163
- process.stdout.write(String(R.AGENT_NAME_RE === K.AGENT_NAME_RE))")" "true"
164
- ! grep -q 'AGENT_NAME_RE = /' "$LIB_SRC/dispatch-resolver.mjs" && ok "dispatch-resolver.mjs holds no AGENT_NAME_RE literal" || bad "dispatch-resolver.mjs holds no AGENT_NAME_RE literal"
165
-
166
- # ---- size cap on .somi/config.json (spec section 8) --------------------------------------------
167
- big() { printf '{"cost":{"ceiling":"low"},"pad":"'; head -c "$1" /dev/zero | tr '\0' 'x'; printf '"}'; }
168
- P="$(proj)"; big 70000 > "$P/.somi/config.json"; rc=0
169
- cli "$P" --agent somi-coder >/dev/null 2>&1 || rc=$?
170
- eq "an oversize config.json (70000 bytes) exits 67 through the CLI" "$rc" "67"
171
- eq "... and the message names the byte limit" \
172
- "$(cli "$P" --agent somi-coder 2>&1 | grep -c 'byte limit')" "1"
173
- P="$(proj)"; big 60000 > "$P/.somi/config.json"; rc=0
174
- cli "$P" --agent somi-coder >/dev/null 2>&1 || rc=$?
175
- eq "a config.json under the cap (60000 bytes) is still read" "$rc" "0"
176
- # Red proof: raise the cap in a copy and the oversize file is accepted.
177
- mkdir -p "$TMP/nocap"; cp "$LIB_SRC"/cost-*.mjs "$LIB_SRC/config-files.mjs" "$TMP/nocap/"
178
- sed -i 's/64 \* 1024/64 * 1024 * 1024/' "$TMP/nocap/config-files.mjs"
179
- P="$(proj)"; big 70000 > "$P/.somi/config.json"
180
- eq "red proof (size cap): without the cap the 70000-byte file is read" \
181
- "$(node --input-type=module -e "
182
- const C = await import('$TMP/nocap/cost-ceiling.mjs');
183
- try { C.readCostConfig('$P'); console.log('ok'); } catch (e) { console.log('reject'); }")" "ok"
184
-
185
- # ---- the read is fd-based: boundary sizes, links followed, non-regular files refused ------------
186
- exact() { local pre='{"cost":{"ceiling":"low"},"pad":"' suf='"}'; printf '%s' "$pre"; head -c "$(( $1 - ${#pre} - ${#suf} ))" /dev/zero | tr '\0' 'x'; printf '%s' "$suf"; }
187
- P="$(proj)"; exact 65536 > "$P/.somi/config.json"; rc=0
188
- cli "$P" --agent somi-coder >/dev/null 2>&1 || rc=$?
189
- eq "a config.json of exactly 65536 bytes is accepted" "$rc" "0"
190
- P="$(proj)"; exact 65537 > "$P/.somi/config.json"; rc=0
191
- cli "$P" --agent somi-coder >/dev/null 2>&1 || rc=$?
192
- eq "a config.json of 65537 bytes is rejected with 67" "$rc" "67"
193
-
194
- # Config reads follow links (a junction or symlinked .somi, a linked config file); what a link could
195
- # abuse -- a device or FIFO that never ends, an endless read -- is refused on the open descriptor.
196
- P="$(proj)"; ln -s /dev/zero "$P/.somi/config.json"; rc=0
197
- timeout 10 env CLAUDE_PROJECT_DIR="$P" env -u SOMI_COST_CEILING node "$CLI" resolve --agent somi-coder >"$TMP/sym.out" 2>"$TMP/sym.err" || rc=$?
198
- eq "a config.json symlink to /dev/zero is rejected quickly with 67 (no timeout)" "$rc" "67"
199
- eq "... and the message says it is not a regular file" "$(grep -c 'not a regular file' "$TMP/sym.err")" "1"
200
- P="$(proj)"; rm -f "$P/.somi/config.json"; mkfifo "$P/.somi/config.json"; rc=0
201
- timeout 10 env CLAUDE_PROJECT_DIR="$P" env -u SOMI_COST_CEILING node "$CLI" resolve --agent somi-coder >/dev/null 2>"$TMP/fifo.err" || rc=$?
202
- eq "a config.json that is a FIFO is rejected quickly with 67 (the open does not block)" "$rc" "67"
203
- P="$(proj '{"cost":{"ceiling":"low"}}')"; mv "$P/.somi/config.json" "$P/real.json"; ln -s ../real.json "$P/.somi/config.json"; rc=0
204
- out="$(cli "$P" --agent somi-coder 2>/dev/null)" || rc=$?
205
- eq "a symlink to an ordinary valid config is read" "$rc|$(printf '%s' "$out" | grep -o '"tier":"[a-z]*"')" '0|"tier":"low"'
206
- P="$(mktemp -d -p "$TMP")"; SHARED="$(mktemp -d -p "$TMP")"; printf '{"cost":{"ceiling":"low"}}' > "$SHARED/config.json"; ln -s "$SHARED" "$P/.somi"; rc=0
207
- out="$(cli "$P" --agent somi-coder 2>/dev/null)" || rc=$?
208
- eq "a .somi that is a link to another directory (a junction on Windows) is read through" "$rc|$(printf '%s' "$out" | grep -o '"tier":"[a-z]*"')" '0|"tier":"low"'
209
- P="$(proj)"; mkdir "$P/.somi/config.json"; rc=0
210
- cli "$P" --agent somi-coder >"$TMP/dir.out" 2>"$TMP/dir.err" || rc=$?
211
- eq "a config.json that is a directory is rejected with 67" "$rc" "67"
212
-
213
- verdict_file() { # $1 = lib dir, $2 = project root -> ok | reject: msg
214
- node --input-type=module -e "
215
- const C = await import('$1/cost-ceiling.mjs');
216
- try { C.readCostConfig('$2'); console.log('ok'); } catch (e) { console.log('reject: ' + e.message); }"
217
- }
218
- mkdir -p "$TMP/nofile" "$TMP/blocking"
219
- cp "$LIB_SRC"/cost-*.mjs "$LIB_SRC/config-files.mjs" "$TMP/nofile/"; cp "$LIB_SRC"/cost-*.mjs "$LIB_SRC/config-files.mjs" "$TMP/blocking/"
220
- sed -i 's/!fs.fstatSync(fd).isFile()/false/' "$TMP/nofile/config-files.mjs"
221
- sed -i 's/fs.constants.O_RDONLY | (fs.constants.O_NONBLOCK ?? 0)/fs.constants.O_RDONLY/' "$TMP/blocking/config-files.mjs"
222
- cmp -s "$LIB_SRC/config-files.mjs" "$TMP/nofile/config-files.mjs" && bad "mutant nofile did not change the file"
223
- cmp -s "$LIB_SRC/config-files.mjs" "$TMP/blocking/config-files.mjs" && bad "mutant blocking did not change the file"
224
- P="$(proj)"; rm -f "$P/.somi/config.json"; mkfifo "$P/.somi/config.json"; rc=0
225
- timeout 5 node --input-type=module -e "const C = await import('$TMP/blocking/cost-ceiling.mjs'); try { C.readCostConfig('$P'); } catch {}" >/dev/null 2>&1 || rc=$?
226
- eq "red proof (O_NONBLOCK): without it a FIFO config hangs the read (timeout)" "$rc" "124"
227
- P="$(proj)"; mkdir "$P/.somi/config.json"
228
- case "$(verdict_file "$LIB_SRC" "$P")" in *"not a regular file"*) ok "a directory config is rejected by the regular-file check" ;; *) bad "a directory config is rejected by the regular-file check" ;; esac
229
- case "$(verdict_file "$TMP/nofile" "$P")" in *"not a regular file"*) bad "red proof (isFile): the regular-file message persists without the check" ;; *) ok "red proof (isFile): without the check the regular-file rejection is gone" ;; esac
230
-
231
- # The JSON-parse error never echoes file text: only the error name and a position.
232
- P="$(proj)"; printf '\n\x01\x1b[31mSECRET-TOKEN {not json' > "$P/.somi/config.json"
233
- msg="$(verdict_file "$LIB_SRC" "$P")"
234
- case "$msg" in *SECRET-TOKEN*|*$'\x1b'*|*$'\x01'*) bad "parse error leaked file text: $msg" ;; reject:*"not valid JSON"*) ok "a malformed config's parse error carries no file text or control characters" ;; *) bad "unexpected parse verdict: $msg" ;; esac
235
- eq "... and the message is a single line" "$(printf '%s' "$msg" | wc -l)" "0"
236
-
237
- # ---- ceiling precedence: explicit > live config > persisted CLI/env state > default (medium) -----
238
- echo "== ceiling precedence =="
239
- rc_() { # $1 = lib dir, $2 = project root -> ceiling,source,origin for a bare resolve
240
- node --input-type=module -e "
241
- const C = await import('$1/cost-ceiling.mjs');
242
- const r = C.resolveCeiling('$2', undefined); process.stdout.write([r.ceiling, r.source, r.ceiling_origin].join(','))"
243
- }
244
- pj() { # $1 = config body or '', $2 = state body or ''
245
- local r; r="$(mktemp -d -p "$TMP")"; mkdir -p "$r/.somi/somi-state"
246
- [ -n "$1" ] && printf '%s' "$1" > "$r/.somi/config.json"
247
- [ -n "$2" ] && printf '%s' "$2" > "$r/.somi/somi-state/ceiling.json"
248
- echo "$r"
249
- }
250
- mut_prec() { # $1 = name, $2 = sed expression
251
- mkdir -p "$TMP/$1"; cp "$LIB_SRC"/cost-*.mjs "$LIB_SRC/config-files.mjs" "$TMP/$1/"; sed -i "$2" "$TMP/$1/cost-ceiling.mjs"
252
- cmp -s "$LIB_SRC/cost-ceiling.mjs" "$TMP/$1/cost-ceiling.mjs" && bad "mutant $1 did not change the file"
253
- }
254
- mut_prec config-ignored 's/^ if (isConfigured) {$/ if (false) {/'
255
- mut_prec state-outranks 's/if (state \&\& EXPLICIT_ORIGINS.includes(state.ceiling_origin)) {/if (state) {/'
256
- mut_prec default-high "s/export const DEFAULT_CEILING = 'medium';/export const DEFAULT_CEILING = 'high';/"
257
-
258
- P="$(pj '{"cost":{"ceiling":"low"}}' '')"
259
- eq "config alone sets the ceiling, source config" "$(rc_ "$LIB_SRC" "$P")" "low,config,config"
260
- printf '{"cost":{"ceiling":"high"}}' > "$P/.somi/config.json"
261
- eq "an edit between two resolves moves the ceiling (config is read live)" "$(rc_ "$LIB_SRC" "$P")" "high,config,config"
262
- eq "red proof (config-live): with the config read removed the edit has no effect" "$(rc_ "$TMP/config-ignored" "$P")" "medium,default,default"
263
-
264
- P="$(pj '{"cost":{"ceiling":"low"}}' '{"ceiling":"high","ceiling_origin":"cli"}')"
265
- eq "config outranks a persisted cli ceiling (config > persisted state)" "$(rc_ "$LIB_SRC" "$P")" "low,config,config"
266
- P="$(pj '' '{"ceiling":"low","ceiling_origin":"env"}')"
267
- eq "a persisted env ceiling applies when config is silent" "$(rc_ "$LIB_SRC" "$P")" "low,state,env"
268
-
269
- LEGACY='{"ceiling":"high"}' # a state file written before origins were recorded
270
- P="$(pj '' "$LEGACY")"
271
- [ "$(cat "$P/.somi/somi-state/ceiling.json")" = "$LEGACY" ] || bad "legacy fixture was not written"
272
- eq "a legacy state file (no origin) yields to the medium default" "$(rc_ "$LIB_SRC" "$P")" "medium,default,default"
273
- eq "red proof (state-yields): if any state outranked the default, the legacy file would win" "$(rc_ "$TMP/state-outranks" "$P")" "high,state,"
274
- P="$(pj '' '{"ceiling":"high","ceiling_origin":"default"}')"
275
- eq "a default-origin state yields to the default" "$(rc_ "$LIB_SRC" "$P")" "medium,default,default"
276
- P="$(pj '' '')"
277
- eq "no config and no state: medium" "$(rc_ "$LIB_SRC" "$P")" "medium,default,default"
278
- eq "red proof (default medium): the mutant default is high" "$(rc_ "$TMP/default-high" "$P")" "high,default,default"
279
-
280
- # ---- untrusted text never echoed; state writes are atomic and symlink-safe; raises recorded ------
281
- echo "== ceiling hardening =="
282
- nasty() { printf 'bad\n\033[31m'; head -c 100 /dev/zero | tr '\0' 'y'; printf 'SECRET-TOKEN'; head -c 3000 /dev/zero | tr '\0' 'y'; }
283
- clean_msg() { # $1 = label, $2 = message
284
- case "$2" in
285
- reject:*SECRET-TOKEN*|*$'\033'*|*$'\n'*SECRET*) bad "$1 leaked untrusted text" ;;
286
- reject:*) if [ "${#2}" -lt 600 ]; then ok "$1 carries no file text, control characters or 3000-char echo"; else bad "$1 message is unbounded (${#2})"; fi ;;
287
- *) bad "$1 was not rejected ($2)" ;;
288
- esac
289
- }
290
- resolve_msg() { # $1 = project root -> reject: <message> | ok
291
- node --input-type=module -e "
292
- const C = await import('$LIB_SRC/cost-ceiling.mjs');
293
- try { C.resolveCeiling('$1', undefined); console.log('ok'); } catch (e) { console.log('reject: ' + e.message); }"
294
- }
295
- P="$(proj)"; node -e "require('fs').writeFileSync(process.argv[1], JSON.stringify({cost:{ceiling:process.argv[2]}}))" "$P/.somi/config.json" "$(nasty)"
296
- clean_msg "a bad cost.ceiling value" "$(resolve_msg "$P")"
297
- P="$(pj '' '')"; node -e "require('fs').writeFileSync(process.argv[1], JSON.stringify({ceiling:process.argv[2]}))" "$P/.somi/somi-state/ceiling.json" "$(nasty)"
298
- clean_msg "a bad state ceiling" "$(resolve_msg "$P")"
299
- P="$(pj '' '')"; node -e "require('fs').writeFileSync(process.argv[1], JSON.stringify({ceiling:'low',ceiling_origin:process.argv[2]}))" "$P/.somi/somi-state/ceiling.json" "$(nasty)"
300
- clean_msg "a bad state ceiling_origin" "$(resolve_msg "$P")"
301
- P="$(pj '' '')"; printf '\n\033[31mSECRET-TOKEN {not json' > "$P/.somi/somi-state/ceiling.json"
302
- clean_msg "an unparsable state file" "$(resolve_msg "$P")"
303
-
304
- # A symlink at ceiling.json is refused on read (67)...
305
- P="$(pj '' '')"; echo KEEP > "$TMP/victim.txt"; ln -s "$TMP/victim.txt" "$P/.somi/somi-state/ceiling.json"
306
- rc=0; CLAUDE_PROJECT_DIR="$P" env -u SOMI_COST_CEILING node "$CLI" resolve --agent somi-coder --ceiling low >/dev/null 2>&1 || rc=$?
307
- eq "resolve --ceiling over a symlinked state path exits 67" "$rc" "67"
308
- eq "... and the symlink target was never modified" "$(cat "$TMP/victim.txt")" "KEEP"
309
- # ...and one planted between the read and the write is replaced by the atomic rename, not followed.
310
- P="$(pj '' '')"
311
- node --input-type=module -e "
312
- import fs from 'node:fs';
313
- const C = await import('$LIB_SRC/cost-ceiling.mjs');
314
- const f = C.ceilingStatePath('$P');
315
- const mk = fs.mkdirSync;
316
- fs.mkdirSync = (...a) => { const r = mk(...a); fs.symlinkSync('$TMP/victim.txt', f); return r; };
317
- C.resolveCeiling('$P', 'low');"
318
- eq "a symlink planted before the write is replaced, never written through" "$(cat "$TMP/victim.txt")" "KEEP"
319
- eq "... ceiling.json is now a regular file holding the new state, with no temp file left" \
320
- "$([ -f "$P/.somi/somi-state/ceiling.json" ] && [ ! -L "$P/.somi/somi-state/ceiling.json" ] && tr -d '\n' < "$P/.somi/somi-state/ceiling.json" | grep -c '"ceiling":"low"'; ls "$P/.somi/somi-state" | tr '\n' ' ')" "1
321
- ceiling.json "
322
-
323
- # A raise over a differing config is recorded even when persisted state already holds the value.
324
- P="$(pj '{"cost":{"ceiling":"low"}}' '{"ceiling":"high","ceiling_origin":"cli"}')"
325
- out="$(node --input-type=module -e "
326
- const C = await import('$LIB_SRC/cost-ceiling.mjs');
327
- const r = C.resolveCeiling('$P', 'high');
328
- const r2 = C.resolveCeiling('$P', 'high');
329
- const fs = await import('node:fs');
330
- const s = JSON.parse(fs.readFileSync(C.ceilingStatePath('$P'), 'utf8'));
331
- process.stdout.write([r.overridden, r.override?.from, r.override?.to, r2.overridden, s.ceiling_overrides.length].join(','))")"
332
- eq "persisted high over config low: the raise is recorded (from the config value), a repeat is reported but not duplicated" "$out" "true,low,high,true,1"
333
- P="$(pj '{"cost":{"ceiling":"high"}}' '{"ceiling":"high","ceiling_origin":"cli"}')"
334
- eq "persisted high, config high, --ceiling high: nothing changes, nothing recorded" \
335
- "$(node --input-type=module -e "const C = await import('$LIB_SRC/cost-ceiling.mjs'); process.stdout.write(String(C.resolveCeiling('$P', 'high').overridden))")" "false"
336
-
337
- echo "cost hardcap tests: $pass ok, $fail failed"
338
- [ "$fail" -eq 0 ] || exit 1