@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.
- package/.claude-plugin/icon.png +0 -0
- package/.claude-plugin/marketplace.json +8 -3
- package/.claude-plugin/plugin.json +9 -10
- package/.copilot-extension/extension.json +32 -40
- package/.copilot-extension/marketplace.json +8 -3
- package/README.md +88 -227
- package/VERSION +1 -1
- package/agents/somi-reviewer.md +21 -40
- package/agents/somi-security-reviewer.md +16 -90
- package/agents/somi-worker-high.md +11 -0
- package/agents/somi-worker-low.md +11 -0
- package/agents/somi-worker-medium.md +11 -0
- package/agents/somi.md +8 -75
- package/commands/somi-config.md +5 -14
- package/commands/somi-deliver.md +11 -0
- package/commands/somi-quality.md +10 -0
- package/commands/somi-review.md +10 -0
- package/commands/somi-status.md +11 -0
- package/commands/somi.md +10 -0
- package/hooks/hooks.json +29 -23
- package/hooks/lib/common.mjs +45 -34
- package/hooks/pre-tool/apply-tier.mjs +31 -0
- package/hooks/pre-tool/block-dangerous-bash.mjs +15 -0
- package/hooks/pre-tool/gate-dep-install.mjs +2 -2
- package/hooks/pre-tool/guard-protected-paths.mjs +9 -16
- package/hooks/pre-tool/guard-tier-config.mjs +17 -0
- package/hooks/pre-tool/reviewers-read-only.mjs +11 -0
- package/hooks/session-start/inject-digest.mjs +51 -0
- package/hooks/stop/review-due.mjs +44 -0
- package/hooks/user-prompt/capture-prompt.mjs +17 -0
- package/library/lenses/architecture.md +1 -0
- package/library/lenses/test-strategy.md +1 -0
- package/library/roles/accessibility.md +1 -0
- package/library/roles/answer.md +1 -0
- package/library/roles/atlas.md +1 -0
- package/library/roles/build.md +1 -0
- package/library/roles/debug.md +6 -0
- package/library/roles/design.md +1 -0
- package/library/roles/discover.md +1 -0
- package/library/roles/incident.md +1 -0
- package/library/roles/migrate.md +1 -0
- package/library/roles/perf.md +1 -0
- package/library/roles/plan.md +1 -0
- package/library/roles/refactor.md +1 -0
- package/library/roles/review.md +1 -0
- package/library/roles/security-review.md +1 -0
- package/library/roles/upgrade.md +1 -0
- package/library/skills/accessibility.md +16 -0
- package/library/skills/ci-infra-gotchas.md +12 -0
- package/library/skills/perf-method.md +10 -0
- package/library/skills/secure-coding.md +18 -0
- package/library/skills/testing-checklist.md +16 -0
- package/library/skills/threat-modeling.md +10 -0
- package/package.json +12 -17
- package/profiles/claude.json +21 -0
- package/profiles/copilot.json +5 -0
- package/rules/digest.md +40 -0
- package/scripts/check-links.mjs +65 -76
- package/scripts/lib/advisor.mjs +207 -0
- package/scripts/lib/check.mjs +167 -0
- package/scripts/lib/copilot-workflow.mjs +41 -0
- package/scripts/lib/doctor.mjs +91 -0
- package/scripts/lib/gates.mjs +69 -0
- package/scripts/lib/git-tree.mjs +201 -0
- package/scripts/lib/init.mjs +23 -0
- package/scripts/lib/ledger.mjs +38 -0
- package/scripts/lib/mcp-project-root.mjs +63 -67
- package/scripts/lib/migrate.mjs +73 -0
- package/scripts/lib/path-facts.mjs +2 -2
- package/scripts/lib/path-safety.mjs +6 -134
- package/scripts/lib/plan.mjs +69 -0
- package/scripts/lib/policy.mjs +103 -0
- package/scripts/lib/pr.mjs +28 -0
- package/scripts/lib/prompt.mjs +72 -0
- package/scripts/lib/quality/clones.mjs +76 -0
- package/scripts/lib/quality/css.mjs +43 -0
- package/scripts/lib/quality/gate.mjs +230 -0
- package/scripts/lib/quality/limits.mjs +97 -0
- package/scripts/lib/quality/metrics.mjs +159 -0
- package/scripts/lib/quality/parse.mjs +73 -0
- package/scripts/lib/quality/repo.mjs +69 -0
- package/scripts/lib/quality/run.mjs +58 -0
- package/scripts/lib/reviews.mjs +132 -0
- package/scripts/lib/routes.mjs +108 -0
- package/scripts/lib/setup.mjs +34 -0
- package/scripts/lib/shards.mjs +166 -0
- package/scripts/lib/signals.mjs +110 -0
- package/scripts/lib/state.mjs +42 -0
- package/scripts/lib/tiers.mjs +42 -0
- package/scripts/lib/update-notice.mjs +48 -0
- package/scripts/lib/views.mjs +62 -0
- package/scripts/lib/workflows.mjs +56 -0
- package/scripts/somi-mcp.mjs +182 -323
- package/scripts/somi.mjs +208 -0
- package/scripts/vendor/tree-sitter/LICENSE.vscode-tree-sitter-wasm +21 -0
- package/scripts/vendor/tree-sitter/LICENSE.web-tree-sitter +21 -0
- package/scripts/vendor/tree-sitter/cgmanifest.vscode-tree-sitter-wasm.json +16 -0
- package/scripts/vendor/tree-sitter/manifest.json +59 -0
- package/scripts/vendor/tree-sitter/package.json +3 -0
- package/scripts/vendor/tree-sitter/tree-sitter-css.wasm +0 -0
- package/scripts/vendor/tree-sitter/tree-sitter-javascript.wasm +0 -0
- package/scripts/vendor/tree-sitter/tree-sitter-tsx.wasm +0 -0
- package/scripts/vendor/tree-sitter/tree-sitter-typescript.wasm +0 -0
- package/scripts/vendor/tree-sitter/web-tree-sitter.js +4081 -0
- package/scripts/vendor/tree-sitter/web-tree-sitter.wasm +0 -0
- package/skills/somi-expertise/SKILL.md +6 -0
- package/workflows/deliver-stages.js +81 -0
- package/.claude/settings.json +0 -131
- package/agents/somi-architecture-reviewer.md +0 -101
- package/agents/somi-atlas.md +0 -100
- package/agents/somi-coder.md +0 -60
- package/agents/somi-designer.md +0 -36
- package/agents/somi-discovery-analyst.md +0 -57
- package/agents/somi-flow.md +0 -47
- package/agents/somi-general.md +0 -36
- package/agents/somi-impact.md +0 -95
- package/agents/somi-incident.md +0 -94
- package/agents/somi-plan-reviewer.md +0 -68
- package/agents/somi-planner.md +0 -57
- package/agents/somi-pr.md +0 -99
- package/agents/somi-refactor-designer.md +0 -81
- package/agents/somi-refactorer.md +0 -99
- package/agents/somi-sdlc-reviewer.md +0 -67
- package/agents/somi-test-strategist.md +0 -87
- package/commands/adopt.md +0 -14
- package/commands/architecture-review.md +0 -19
- package/commands/atlas.md +0 -19
- package/commands/code-loop.md +0 -19
- package/commands/code-parallel.md +0 -19
- package/commands/code.md +0 -19
- package/commands/debug.md +0 -19
- package/commands/design.md +0 -19
- package/commands/discover.md +0 -19
- package/commands/impact.md +0 -19
- package/commands/incident.md +0 -19
- package/commands/plan-loop.md +0 -19
- package/commands/plan.md +0 -19
- package/commands/pr.md +0 -19
- package/commands/refactor-design.md +0 -19
- package/commands/refactor.md +0 -19
- package/commands/release-readiness.md +0 -19
- package/commands/review-panel.md +0 -19
- package/commands/review.md +0 -19
- package/commands/security-review.md +0 -19
- package/commands/ship-loop.md +0 -19
- package/commands/ship.md +0 -19
- package/commands/test-strategy.md +0 -19
- package/commands/upgrade.md +0 -19
- package/examples/README.md +0 -19
- package/examples/code-review-example.md +0 -88
- package/examples/discovery-example.md +0 -200
- package/examples/feature-plan-example.md +0 -377
- package/examples/full-pipeline-example.md +0 -188
- package/examples/sample-consumer/.claude/settings.json +0 -29
- package/examples/sample-consumer/CLAUDE.md +0 -78
- package/examples/sample-consumer/README.md +0 -102
- package/hooks/post-tool/lint-changed-files.mjs +0 -209
- package/hooks/pre-tool/guard-cost-lowering.mjs +0 -192
- package/hooks/session-start/detect-repo-instructions.mjs +0 -287
- package/hooks/session-start/ensure-default-config.mjs +0 -113
- package/hooks/user-prompt-submit/inject-workflow-context.mjs +0 -580
- package/rules/00-priorities.md +0 -48
- package/rules/10-solid.md +0 -87
- package/rules/20-clean-code.md +0 -62
- package/rules/30-security-owasp.md +0 -83
- package/rules/40-engineering-practices.md +0 -65
- package/rules/50-collaboration.md +0 -114
- package/rules/99-overrides.md +0 -52
- package/rules/CLAUDE.md +0 -200
- package/scripts/check-session-artefacts.mjs +0 -149
- package/scripts/check-size-budget.mjs +0 -137
- package/scripts/generate-digest.mjs +0 -176
- package/scripts/lib/agent-info.mjs +0 -85
- package/scripts/lib/agent-registry.mjs +0 -92
- package/scripts/lib/cost-ceiling.mjs +0 -328
- package/scripts/lib/cost-config-edit.mjs +0 -102
- package/scripts/lib/cost-config.mjs +0 -64
- package/scripts/lib/cost-defaults.mjs +0 -53
- package/scripts/lib/cost-lowering.mjs +0 -89
- package/scripts/lib/cost-model.mjs +0 -110
- package/scripts/lib/dispatch-resolver.mjs +0 -243
- package/scripts/lib/escalation-facts.mjs +0 -146
- package/scripts/lib/escalation.mjs +0 -129
- package/scripts/lib/findings-ledger.mjs +0 -241
- package/scripts/lib/install-text.mjs +0 -97
- package/scripts/lib/loop-caps.mjs +0 -140
- package/scripts/lib/loop-diff.mjs +0 -38
- package/scripts/lib/loop-dispatch.mjs +0 -19
- package/scripts/lib/loop-engine.mjs +0 -227
- package/scripts/lib/loop-escalate.mjs +0 -49
- package/scripts/lib/loop-exit.mjs +0 -47
- package/scripts/lib/loop-notes-compact.mjs +0 -72
- package/scripts/lib/loop-notes.mjs +0 -282
- package/scripts/lib/loop-review.mjs +0 -395
- package/scripts/lib/loop-state.mjs +0 -44
- package/scripts/lib/loop-step-close.mjs +0 -75
- package/scripts/lib/loop-step-kit.mjs +0 -179
- package/scripts/lib/loop-step-review.mjs +0 -140
- package/scripts/lib/loop-step-tool.mjs +0 -89
- package/scripts/lib/loop-step.mjs +0 -303
- package/scripts/lib/run-budget.mjs +0 -28
- package/scripts/lib/run-card.mjs +0 -73
- package/scripts/lib/size-tier.mjs +0 -184
- package/scripts/lib/tier-record.mjs +0 -82
- package/scripts/size-budget-overrides.json +0 -58
- package/scripts/somi-card.mjs +0 -39
- package/scripts/somi-check.mjs +0 -237
- package/scripts/somi-config.mjs +0 -421
- package/scripts/somi-dispatch.mjs +0 -141
- package/scripts/somi-findings.mjs +0 -100
- package/scripts/somi-loop.mjs +0 -139
- package/scripts/somi-tier.mjs +0 -393
- package/scripts/validate.sh +0 -653
- package/skills/api-design/SKILL.md +0 -147
- package/skills/clean-code/SKILL.md +0 -142
- package/skills/flow-adopt/SKILL.md +0 -92
- package/skills/flow-architecture-review/SKILL.md +0 -94
- package/skills/flow-atlas/SKILL.md +0 -34
- package/skills/flow-code/SKILL.md +0 -167
- package/skills/flow-code-loop/SKILL.md +0 -263
- package/skills/flow-code-parallel/SKILL.md +0 -160
- package/skills/flow-debug/SKILL.md +0 -141
- package/skills/flow-design/SKILL.md +0 -170
- package/skills/flow-discover/SKILL.md +0 -204
- package/skills/flow-impact/SKILL.md +0 -35
- package/skills/flow-incident/SKILL.md +0 -49
- package/skills/flow-plan/SKILL.md +0 -254
- package/skills/flow-plan-loop/SKILL.md +0 -254
- package/skills/flow-pr/SKILL.md +0 -34
- package/skills/flow-refactor/SKILL.md +0 -58
- package/skills/flow-refactor-design/SKILL.md +0 -97
- package/skills/flow-release-readiness/SKILL.md +0 -82
- package/skills/flow-review/SKILL.md +0 -193
- package/skills/flow-review-panel/SKILL.md +0 -154
- package/skills/flow-security-review/SKILL.md +0 -83
- package/skills/flow-ship/SKILL.md +0 -125
- package/skills/flow-ship-loop/SKILL.md +0 -274
- package/skills/flow-somi-config/SKILL.md +0 -142
- package/skills/flow-status/SKILL.md +0 -43
- package/skills/flow-test-strategy/SKILL.md +0 -83
- package/skills/flow-upgrade/SKILL.md +0 -82
- package/skills/market-research/SKILL.md +0 -125
- package/skills/observability/SKILL.md +0 -133
- package/skills/owasp-defense/SKILL.md +0 -141
- package/skills/plan-review/SKILL.md +0 -83
- package/skills/reasoning-craft/SKILL.md +0 -213
- package/skills/requirements-engineering/SKILL.md +0 -137
- package/skills/role-coder/SKILL.md +0 -247
- package/skills/role-designer/SKILL.md +0 -172
- package/skills/role-discovery-analyst/SKILL.md +0 -288
- package/skills/role-planner/SKILL.md +0 -353
- package/skills/role-reviewer/SKILL.md +0 -218
- package/skills/rules/SKILL.md +0 -47
- package/skills/sdlc-process/SKILL.md +0 -74
- package/skills/solid-principles/SKILL.md +0 -143
- package/skills/somi-checkpoint/SKILL.md +0 -95
- package/skills/somi-dispatch/SKILL.md +0 -259
- package/skills/somi-routing/SKILL.md +0 -158
- package/skills/testing-playbook/SKILL.md +0 -133
- package/skills/threat-modeling/SKILL.md +0 -117
- package/templates/ADR.md.tmpl +0 -73
- package/templates/ATLAS.md.tmpl +0 -75
- package/templates/BRD.md.tmpl +0 -68
- package/templates/BRIEF.md.tmpl +0 -119
- package/templates/CONTEXT.md.tmpl +0 -74
- package/templates/DECISIONS.md.tmpl +0 -105
- package/templates/DESIGN.md.tmpl +0 -83
- package/templates/DIARY.md.tmpl +0 -70
- package/templates/DOD.md.tmpl +0 -90
- package/templates/FRD.md.tmpl +0 -64
- package/templates/PHASE.md.tmpl +0 -80
- package/templates/PROGRESS.md.tmpl +0 -63
- package/templates/RCA.md.tmpl +0 -53
- package/templates/RD-README.md.tmpl +0 -70
- package/templates/RESEARCH.md.tmpl +0 -96
- package/templates/REVIEW.md.tmpl +0 -82
- package/templates/SDD.md.tmpl +0 -82
- package/templates/SOMI-README.md.tmpl +0 -83
- package/templates/SPEC.md.tmpl +0 -123
- package/templates/SRS.md.tmpl +0 -85
- package/templates/TDD.md.tmpl +0 -77
- package/tests/hooks/cases/audit-log.json +0 -21
- package/tests/hooks/cases/block-dangerous-bash.json +0 -166
- package/tests/hooks/cases/block-secret-writes.json +0 -140
- package/tests/hooks/cases/detect-repo-instructions.json +0 -105
- package/tests/hooks/cases/gate-dep-install.json +0 -42
- package/tests/hooks/cases/guard-protected-paths.json +0 -222
- package/tests/hooks/cases/inject-workflow-context.json +0 -239
- package/tests/hooks/cases/lint-changed-files.json +0 -120
- package/tests/hooks/run.sh +0 -392
- package/tests/scripts/agent-model-frontmatter.sh +0 -38
- package/tests/scripts/agent-names.sh +0 -77
- package/tests/scripts/check-frontmatter.sh +0 -37
- package/tests/scripts/check-links.sh +0 -105
- package/tests/scripts/check-session-artefacts.sh +0 -60
- package/tests/scripts/check-size-budget.sh +0 -71
- package/tests/scripts/checkpoint-gate.sh +0 -131
- package/tests/scripts/convergence-runner.sh +0 -1514
- package/tests/scripts/cost-ceiling.sh +0 -255
- package/tests/scripts/cost-defaults.sh +0 -130
- package/tests/scripts/cost-hardcap.sh +0 -338
- package/tests/scripts/cost-lowering.sh +0 -261
- package/tests/scripts/cost-model.sh +0 -196
- package/tests/scripts/dispatch-guard.sh +0 -108
- package/tests/scripts/dispatch-line-gate.sh +0 -61
- package/tests/scripts/dispatch-reference-gate.sh +0 -173
- package/tests/scripts/ensure-default-config.sh +0 -162
- package/tests/scripts/escalation.sh +0 -409
- package/tests/scripts/eval-runner.sh +0 -3692
- package/tests/scripts/evals-packaging.sh +0 -224
- package/tests/scripts/fact-sheet.sh +0 -296
- package/tests/scripts/fixtures/legacy-shard-no-schema.json +0 -50
- package/tests/scripts/fixtures/mock-stdin-read-throw.cjs +0 -18
- package/tests/scripts/fixtures/size-tier-cases.json +0 -87
- package/tests/scripts/generate-digest.sh +0 -180
- package/tests/scripts/global-config.sh +0 -167
- package/tests/scripts/goldens/loop-state-breach.json +0 -1
- package/tests/scripts/goldens/loop-state-done.json +0 -1
- package/tests/scripts/goldens/somi-findings.json +0 -336
- package/tests/scripts/goldens/somi-loop.json +0 -168
- package/tests/scripts/lib/adr-shape.mjs +0 -41
- package/tests/scripts/lib/agent-registry-tests.mjs +0 -95
- package/tests/scripts/lib/brief-only-guard.mjs +0 -27
- package/tests/scripts/lib/checkpoint-gate.mjs +0 -91
- package/tests/scripts/lib/digest-marker-coupling.mjs +0 -52
- package/tests/scripts/lib/dispatch-reference-gate.mjs +0 -121
- package/tests/scripts/lib/escalation-facts-tests.mjs +0 -140
- package/tests/scripts/lib/escalation-tests.mjs +0 -154
- package/tests/scripts/lib/findings-ledger-tests.mjs +0 -118
- package/tests/scripts/lib/loop-engine-tests.mjs +0 -287
- package/tests/scripts/lib/loop-escalate-tests.mjs +0 -356
- package/tests/scripts/lib/loop-flow-gate.mjs +0 -173
- package/tests/scripts/lib/loop-notes-tests.mjs +0 -384
- package/tests/scripts/lib/loop-step-cli-tests.mjs +0 -156
- package/tests/scripts/lib/loop-step-close-tests.mjs +0 -351
- package/tests/scripts/lib/loop-step-lenient-tests.mjs +0 -114
- package/tests/scripts/lib/loop-step-state-tests.mjs +0 -127
- package/tests/scripts/lib/loop-step-tests.mjs +0 -499
- package/tests/scripts/lib/loop-step-unwritten-tests.mjs +0 -55
- package/tests/scripts/lib/multipass-dependence.mjs +0 -192
- package/tests/scripts/lib/multipass-source-identity.mjs +0 -243
- package/tests/scripts/lib/path-facts-tests.mjs +0 -198
- package/tests/scripts/lib/path-safety-tests.mjs +0 -201
- package/tests/scripts/lib/reference-pair.mjs +0 -61
- package/tests/scripts/lib/retirement-gate.mjs +0 -172
- package/tests/scripts/lib/run-suites.mjs +0 -139
- package/tests/scripts/lib/shell-embedded-js.mjs +0 -47
- package/tests/scripts/lib/ship-loop-sizing-guard.mjs +0 -52
- package/tests/scripts/lib/site-roadmap-gate.mjs +0 -64
- package/tests/scripts/lib/size-tier-drift.mjs +0 -74
- package/tests/scripts/lib/sizing-score-tests.mjs +0 -210
- package/tests/scripts/lib/sizing-set-tests.mjs +0 -131
- package/tests/scripts/lib/somi-mcp-client.mjs +0 -102
- package/tests/scripts/lib/somi-mcp-config-tests.mjs +0 -92
- package/tests/scripts/lib/somi-mcp-loop-tests.mjs +0 -226
- package/tests/scripts/lib/somi-mcp-tests.mjs +0 -491
- package/tests/scripts/lib/somi-mcp-tier-tests.mjs +0 -64
- package/tests/scripts/lib/somi-mcp-tool-schema.mjs +0 -38
- package/tests/scripts/loop-engine.sh +0 -22
- package/tests/scripts/loop-flow-gate.sh +0 -11
- package/tests/scripts/retirement-gate.sh +0 -306
- package/tests/scripts/review-panel-seats.sh +0 -32
- package/tests/scripts/run-card.sh +0 -57
- package/tests/scripts/run-suites.sh +0 -74
- package/tests/scripts/run.sh +0 -689
- package/tests/scripts/site-roadmap-gate.sh +0 -76
- package/tests/scripts/size-tier.sh +0 -562
- package/tests/scripts/somi-agent-dispatch-contract.sh +0 -224
- package/tests/scripts/somi-config.sh +0 -286
- package/tests/scripts/somi-dispatch.sh +0 -465
- package/tests/scripts/somi-mcp.sh +0 -15
- package/tests/scripts/suite-plan.json +0 -20
package/scripts/somi-config.mjs
DELETED
|
@@ -1,421 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// somi-config.mjs — show and change the cost settings in SoMi's config: the global file every project
|
|
3
|
-
// and session reads, and a project's .somi/config.json merged over it (scripts/lib/config-files.mjs).
|
|
4
|
-
//
|
|
5
|
-
// show one JSON line: both files, the ceiling, and every agent's effective
|
|
6
|
-
// tier (the resolver's own rule: the agent's cost.agents entry, else the
|
|
7
|
-
// ceiling) in the merged config, each with the file it came from, plus
|
|
8
|
-
// fixed warnings
|
|
9
|
-
// get cost.ceiling | cost.agents.<name>
|
|
10
|
-
// set cost.ceiling <low|medium|high> | set cost.agents.<name> <low|medium|high>
|
|
11
|
-
// set cost.mapping.<host>.<tier> <model-id[,model-id...]> (a preference list, first offered wins)
|
|
12
|
-
// unset cost.agents.<name> | unset cost.mapping.<host>
|
|
13
|
-
// init create the default file when absent (the exact text the SessionStart
|
|
14
|
-
// hook writes); a no-op if anything exists at the path, and for the
|
|
15
|
-
// project file also when a global config exists (it holds the defaults)
|
|
16
|
-
// [--global] (set, unset, init) change the global file instead of the project's.
|
|
17
|
-
// For the user to run: the somi_config MCP tool never passes it
|
|
18
|
-
// [--project-dir DIR] default: CLAUDE_PROJECT_DIR, else the current directory
|
|
19
|
-
// [--host NAME] (show) the host this session runs on; adds a warning when that host has
|
|
20
|
-
// no model mapping while any agent resolves to high (#36)
|
|
21
|
-
// [--refuse-lowering] (set, unset) exit 64 and write nothing if the change would lower any
|
|
22
|
-
// agent's effective tier (see scripts/lib/cost-lowering.mjs)
|
|
23
|
-
//
|
|
24
|
-
// Only those keys can be changed. Any cost.mapping change counts as needing the user (cost-lowering.mjs),
|
|
25
|
-
// so --refuse-lowering refuses it: which model a tier means cannot be ranked here. The config is read on every dispatch, so a set or unset takes effect
|
|
26
|
-
// on the very next resolve. Everything else in the file is preserved as parsed.
|
|
27
|
-
//
|
|
28
|
-
// A set or unset rewrites the whole file, so it refuses (67, nothing written) any file whose content
|
|
29
|
-
// would not survive a round trip through JSON.parse/JSON.stringify: a duplicate key, an integer beyond
|
|
30
|
-
// the safe range, or a number outside the double range, anywhere in the file; text whose rewrite would
|
|
31
|
-
// exceed the 64 KiB limit the reader enforces; or nesting too deep to serialise. Other formatting
|
|
32
|
-
// (indentation, key spacing, float spelling such as 1.0) is normalised to 2-space JSON. The temp file is
|
|
33
|
-
// created in .somi and fsynced; .somi's identity is re-checked just before the rename. A same-user
|
|
34
|
-
// process that swaps .somi in that last instant, or edits config.json between our read and rename, is
|
|
35
|
-
// not defended against (the edit would be lost). A replaced file keeps the original's permission bits.
|
|
36
|
-
//
|
|
37
|
-
// With the ceiling, `show` and the output of `set` report what the resolver would use: the
|
|
38
|
-
// SOMI_COST_CEILING variable, else cost.ceiling, else a ceiling persisted by an earlier --ceiling or
|
|
39
|
-
// env call, else the built-in default (`effective_ceiling`), and warn when the variable or a
|
|
40
|
-
// persisted value is why it differs from the config.
|
|
41
|
-
//
|
|
42
|
-
// Safety: the committed config is untrusted. show and get read it as the resolver does (bounded to
|
|
43
|
-
// 64 KiB, regular files only, links followed so a junction or symlinked .somi works), and the whole
|
|
44
|
-
// `cost` block is validated by the shared validator before and after an edit. A project write goes to
|
|
45
|
-
// a fresh temp file in the same .somi directory (O_EXCL, no-follow) and is renamed over config.json, so
|
|
46
|
-
// a reader never sees half a file and nothing is written through a link: a symlinked .somi or
|
|
47
|
-
// config.json is refused for set, unset and init. The global file is the user's own: a write to it
|
|
48
|
-
// follows a link to the real file (a dotfiles setup keeps its link) and creates missing parent
|
|
49
|
-
// directories. A lowering is judged on the merged config, before and after. Output is pure ASCII and
|
|
50
|
-
// no message echoes text from a file (values are quoted and bounded).
|
|
51
|
-
//
|
|
52
|
-
// Exit codes (callers branch on these — do not repurpose):
|
|
53
|
-
// 0 ok
|
|
54
|
-
// 64 usage: bad arguments, a key that may not be set, a bad tier or agent name
|
|
55
|
-
// 67 the project's files: .somi or config.json unreadable, oversize, a link, not valid JSON, or an
|
|
56
|
-
// invalid cost block (nothing is written); also a failed write
|
|
57
|
-
// Nothing is printed to stdout on an error path. Zero-dependency: stdlib only.
|
|
58
|
-
|
|
59
|
-
import fs from 'node:fs';
|
|
60
|
-
import path from 'node:path';
|
|
61
|
-
import { readTextFile, loadState, decideDispatch, DEFAULT_CEILING, MAX_CONFIG_BYTES } from './lib/cost-ceiling.mjs';
|
|
62
|
-
import { ConfigFileError, globalConfigPath, loadConfig, mergeConfig, readConfigText } from './lib/config-files.mjs';
|
|
63
|
-
import { quote } from './lib/cost-config.mjs';
|
|
64
|
-
import { VALID_COSTS, HOST_MODELS } from './lib/cost-model.mjs';
|
|
65
|
-
import { AGENT_DEFAULT_TIERS, serializeDefaultConfig } from './lib/cost-defaults.mjs';
|
|
66
|
-
import { EditError, applyEdit, parseKey, validateCostBlock, missingMappingTiers } from './lib/cost-config-edit.mjs';
|
|
67
|
-
import { lowers, REASONS } from './lib/cost-lowering.mjs';
|
|
68
|
-
|
|
69
|
-
const PROG = 'somi-config';
|
|
70
|
-
const USAGE = `usage: ${PROG}.mjs show [--host NAME] | get <key> | set <key> <value> | unset <key> | init [--project-dir DIR] [--global] [--refuse-lowering] (keys: cost.ceiling, cost.agents.<name>, cost.mapping.<host>.<tier> = model-id[,model-id...]; unset cost.agents.<name> or cost.mapping.<host>)`;
|
|
71
|
-
const HOST_RE = /^[a-z][a-z0-9-]{0,31}$/;
|
|
72
|
-
const EXIT_USAGE = 64;
|
|
73
|
-
const EXIT_PROJECT = 67;
|
|
74
|
-
|
|
75
|
-
const SEC_WARNING = 'somi-security-reviewer runs below high; pin it with: set cost.agents.somi-security-reviewer high';
|
|
76
|
-
const ENV_WARNING = 'SOMI_COST_CEILING is set in this environment and outranks cost.ceiling in the config';
|
|
77
|
-
const STATE_WARNING = 'a ceiling set earlier by a flag or SOMI_COST_CEILING is in force because cost.ceiling is not set in the config';
|
|
78
|
-
const GLOBAL_ONLY_WARNING = 'that value is set in your global config, not this project, so nothing changed here; change it with --global (run the CLI yourself)';
|
|
79
|
-
const UNKNOWN_WARNING = 'that agent name is not a shipped agent; the entry is accepted but may never be used';
|
|
80
|
-
const unmappedWarning = (host, n) => `${host} has no model mapping, so ${n} high-tier agent(s) run on whatever model the host picks; set cost.mapping.${host} with low, medium and high (docs/USAGE.md, "Pinning models per tier on Copilot")`;
|
|
81
|
-
const MAPPING_MESSAGE = 'this change remaps a tier to a different model and needs the user: ask them to run node scripts/somi-config.mjs themselves or edit .somi/config.json';
|
|
82
|
-
const LOWERING_MESSAGE = 'this change lowers a tier and needs the user: ask them to run node scripts/somi-config.mjs themselves or edit .somi/config.json';
|
|
83
|
-
const WRITE_FLAGS = fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | (fs.constants.O_NOFOLLOW ?? 0);
|
|
84
|
-
|
|
85
|
-
class ExitSignal extends Error {
|
|
86
|
-
constructor(code) { super(`exit ${code}`); this.code = code; }
|
|
87
|
-
}
|
|
88
|
-
const asciiEscape = (s) => s.replace(/[\u0000-\u001f\u007f-]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, '0')}`);
|
|
89
|
-
const ascii = (x) => asciiEscape(JSON.stringify(x));
|
|
90
|
-
function fail(code, msg, tail = '') {
|
|
91
|
-
process.stderr.write(`${PROG}: ${asciiEscape(msg)}${tail}\n`);
|
|
92
|
-
throw new ExitSignal(code);
|
|
93
|
-
}
|
|
94
|
-
const usage = (msg) => fail(EXIT_USAGE, msg, `\n${USAGE}`);
|
|
95
|
-
const project = (msg) => fail(EXIT_PROJECT, msg);
|
|
96
|
-
|
|
97
|
-
function parseArgs(argv) {
|
|
98
|
-
const [cmd, ...rest] = argv;
|
|
99
|
-
const pos = [];
|
|
100
|
-
let dir;
|
|
101
|
-
let refuse = false;
|
|
102
|
-
let global = false;
|
|
103
|
-
let host;
|
|
104
|
-
for (let i = 0; i < rest.length; i++) {
|
|
105
|
-
if (rest[i] === '--refuse-lowering') refuse = true;
|
|
106
|
-
else if (rest[i] === '--global') global = true;
|
|
107
|
-
else if (rest[i] === '--host') {
|
|
108
|
-
if (host !== undefined) usage('--host given twice');
|
|
109
|
-
host = rest[++i];
|
|
110
|
-
if (host === undefined || !HOST_RE.test(host)) usage(`--host must match ${HOST_RE}`);
|
|
111
|
-
}
|
|
112
|
-
else if (rest[i] === '--project-dir') {
|
|
113
|
-
if (dir !== undefined) usage('--project-dir given twice');
|
|
114
|
-
dir = rest[++i];
|
|
115
|
-
if (dir === undefined || dir === '') usage('--project-dir requires a non-empty value');
|
|
116
|
-
} else if (rest[i].startsWith('--')) usage(`unknown argument: ${quote(rest[i])}`);
|
|
117
|
-
else pos.push(rest[i]);
|
|
118
|
-
}
|
|
119
|
-
const want = { show: 0, get: 1, set: 2, unset: 1, init: 0 };
|
|
120
|
-
if (!Object.hasOwn(want, cmd ?? '')) usage(cmd ? `unknown command: ${quote(cmd)}` : 'a command is required');
|
|
121
|
-
if (pos.length !== want[cmd]) usage(`${cmd} takes ${want[cmd]} argument(s)`);
|
|
122
|
-
if (refuse && cmd !== 'set' && cmd !== 'unset') usage('--refuse-lowering applies to set and unset only');
|
|
123
|
-
if (host !== undefined && cmd !== 'show') usage('--host applies to show only');
|
|
124
|
-
if (global && !['set', 'unset', 'init'].includes(cmd)) usage('--global applies to set, unset and init');
|
|
125
|
-
return { cmd, pos, dir, refuse, host, global };
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function projectRoot(dir) {
|
|
129
|
-
let base = dir ?? process.env.CLAUDE_PROJECT_DIR ?? '';
|
|
130
|
-
if (base === '' || base.includes('${')) base = dir ?? process.cwd();
|
|
131
|
-
try {
|
|
132
|
-
if (!fs.statSync(base).isDirectory()) usage('the project directory is not a directory');
|
|
133
|
-
} catch (e) {
|
|
134
|
-
if (e instanceof ExitSignal) throw e;
|
|
135
|
-
usage('the project directory cannot be read');
|
|
136
|
-
}
|
|
137
|
-
return path.resolve(base);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const lstatOrNull = (p) => { try { return fs.lstatSync(p); } catch (e) { if (e.code === 'ENOENT') return null; throw e; } };
|
|
141
|
-
|
|
142
|
-
// -> {somi, file, dirExists, fileExists}; refuses a .somi that is not a real directory. Writes only:
|
|
143
|
-
// show and get read through links.
|
|
144
|
-
function locate(root) {
|
|
145
|
-
const somi = path.join(root, '.somi');
|
|
146
|
-
const file = path.join(somi, 'config.json');
|
|
147
|
-
let d; let f = null;
|
|
148
|
-
try { d = lstatOrNull(somi); if (d && !d.isDirectory()) project('.somi is a link or not a directory; refusing'); if (d) f = lstatOrNull(file); } catch (e) {
|
|
149
|
-
if (e instanceof ExitSignal) throw e;
|
|
150
|
-
project('.somi cannot be inspected');
|
|
151
|
-
}
|
|
152
|
-
return { somi, file, dirExists: d !== null, fileExists: f !== null, dirStat: d };
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Why a rewrite of `text` (already parsed OK) would change its meaning, or null: a duplicate key or a
|
|
156
|
-
// number JSON.stringify cannot reproduce. A small scanner; structure is already known to be valid.
|
|
157
|
-
function roundTripHazard(text) {
|
|
158
|
-
const stack = [];
|
|
159
|
-
const n = text.length;
|
|
160
|
-
for (let i = 0; i < n;) {
|
|
161
|
-
const c = text[i];
|
|
162
|
-
const top = stack[stack.length - 1];
|
|
163
|
-
if (c === '"') {
|
|
164
|
-
let j = i + 1;
|
|
165
|
-
while (j < n && text[j] !== '"') j += text[j] === '\\' ? 2 : 1;
|
|
166
|
-
if (top?.obj && top.expectKey) {
|
|
167
|
-
const key = JSON.parse(text.slice(i, j + 1));
|
|
168
|
-
if (top.keys.has(key)) return 'a duplicate key';
|
|
169
|
-
top.keys.add(key);
|
|
170
|
-
top.expectKey = false;
|
|
171
|
-
}
|
|
172
|
-
i = j + 1;
|
|
173
|
-
} else if (c === '{') { stack.push({ obj: true, keys: new Set(), expectKey: true }); i++; }
|
|
174
|
-
else if (c === '[') { stack.push({ obj: false }); i++; }
|
|
175
|
-
else if (c === '}' || c === ']') { stack.pop(); i++; }
|
|
176
|
-
else if (c === ',') { if (top?.obj) top.expectKey = true; i++; }
|
|
177
|
-
else if (c === '-' || (c >= '0' && c <= '9')) {
|
|
178
|
-
const tok = /^-?\d+(\.\d+)?([eE][+-]?\d+)?/.exec(text.slice(i, i + 400))[0];
|
|
179
|
-
const v = Number(tok);
|
|
180
|
-
if (!Number.isFinite(v)) return 'a number outside the double range';
|
|
181
|
-
if (/^-?\d+$/.test(tok) && !Number.isSafeInteger(v)) return 'an integer beyond the safe range';
|
|
182
|
-
i += tok.length;
|
|
183
|
-
} else i++;
|
|
184
|
-
}
|
|
185
|
-
return null;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// The global file to write: its real path when it exists (a link is followed to it), and the stat of
|
|
189
|
-
// the directory it sits in, which writeAtomic re-checks. Parent directories are created when missing.
|
|
190
|
-
function locateGlobal() {
|
|
191
|
-
const g = globalConfigPath();
|
|
192
|
-
if (g === null) usage('there is no global config file: SOMI_GLOBAL_CONFIG is set to an empty value');
|
|
193
|
-
let file = g;
|
|
194
|
-
try { file = fs.realpathSync(g); } catch (e) { if (e.code !== 'ENOENT') project('the global config cannot be resolved'); }
|
|
195
|
-
const somi = path.dirname(file);
|
|
196
|
-
const fileExists = fs.existsSync(file);
|
|
197
|
-
return { somi, file, dirExists: fs.existsSync(somi), fileExists, global: true, shown: g };
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// The merged config and both files, for reading; any problem with either file is 67.
|
|
201
|
-
function loadAll(root) {
|
|
202
|
-
try { return loadConfig(root); } catch (e) {
|
|
203
|
-
if (e instanceof ConfigFileError) return project(e.message);
|
|
204
|
-
throw e;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// -> the parsed config object ({} when absent), with its whole cost block validated. With
|
|
209
|
-
// `forWrite`, also refuses a file a rewrite could not reproduce faithfully. A project file is read
|
|
210
|
-
// without following a link (it is about to be replaced); the global file through one.
|
|
211
|
-
function load(root, loc, forWrite = false) {
|
|
212
|
-
if (!loc.fileExists) return {};
|
|
213
|
-
let text; let cfg;
|
|
214
|
-
try { text = loc.global ? readConfigText(loc.file) : readTextFile(loc.file); } catch (e) { project(String(e.message).replace(/^cost-ceiling: /, '')); }
|
|
215
|
-
try { cfg = JSON.parse(text); } catch { project('config.json is not valid JSON'); }
|
|
216
|
-
if (cfg === null || typeof cfg !== 'object' || Array.isArray(cfg)) project('config.json is not a JSON object');
|
|
217
|
-
if (cfg.ceiling !== undefined) project('"ceiling" at the top level of config.json belongs under "cost.ceiling"');
|
|
218
|
-
checkCost(cfg.cost, project);
|
|
219
|
-
if (forWrite) {
|
|
220
|
-
const why = roundTripHazard(text);
|
|
221
|
-
if (why) project(`config.json holds ${why}, which a rewrite would change; edit it by hand`);
|
|
222
|
-
}
|
|
223
|
-
return cfg;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function checkCost(cost, onFail) {
|
|
227
|
-
try { validateCostBlock(cost); } catch (e) { onFail(String(e.message).replace(/^cost-ceiling: /, '')); }
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// What the resolver would use for the ceiling, without writing state: SOMI_COST_CEILING, else the
|
|
231
|
-
// config, else a ceiling an earlier flag/env call persisted, else the default.
|
|
232
|
-
function ceilingInForce(root, cost) {
|
|
233
|
-
const env = process.env.SOMI_COST_CEILING;
|
|
234
|
-
if (env !== undefined && env !== '') {
|
|
235
|
-
if (!VALID_COSTS.includes(env)) usage(`SOMI_COST_CEILING must be one of ${VALID_COSTS.join('|')}, got ${quote(env)}`);
|
|
236
|
-
return { value: env, source: 'env' };
|
|
237
|
-
}
|
|
238
|
-
if (cost?.ceiling !== undefined) return { value: cost.ceiling, source: 'config' };
|
|
239
|
-
let state;
|
|
240
|
-
try { state = loadState(root); } catch (e) { project(String(e.message).replace(/^cost-ceiling: /, '')); }
|
|
241
|
-
if (state && ['cli', 'env'].includes(state.ceiling_origin)) return { value: state.ceiling, source: 'state' };
|
|
242
|
-
return { value: DEFAULT_CEILING, source: 'default' };
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// The resolver's rule, via the library: the agent's override if present, else the ceiling.
|
|
246
|
-
function effective(name, cost, ceiling) {
|
|
247
|
-
const override = cost?.agents && Object.hasOwn(cost.agents, name) ? cost.agents[name] : undefined;
|
|
248
|
-
const declared = AGENT_DEFAULT_TIERS[name] ?? override ?? ceiling;
|
|
249
|
-
const d = decideDispatch(declared, ceiling, override);
|
|
250
|
-
return { tier: d.selected, source: d.tier_source };
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function warningsFor(cost, force, extra = []) {
|
|
254
|
-
const w = [...extra];
|
|
255
|
-
for (const host of Object.keys(cost?.mapping ?? {}).sort()) {
|
|
256
|
-
const missing = missingMappingTiers(cost, host);
|
|
257
|
-
if (missing.length) w.push(`cost.mapping.${host} has no ${missing.join(', ')} entry yet, so a dispatch at that tier on ${host} fails until it is set`);
|
|
258
|
-
}
|
|
259
|
-
if (force.source === 'env' && cost?.ceiling !== undefined && cost.ceiling !== force.value) w.push(ENV_WARNING);
|
|
260
|
-
if (force.source === 'state') w.push(STATE_WARNING);
|
|
261
|
-
if (effective('somi-security-reviewer', cost, force.value).tier !== 'high') w.push(SEC_WARNING);
|
|
262
|
-
return w;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// Which file a merged cost value came from: the project file when it sets it, else the global one.
|
|
266
|
-
const fromFile = (files, get) => (get(files.project.config) !== undefined ? 'project' : 'global');
|
|
267
|
-
|
|
268
|
-
function show(root, files, host) {
|
|
269
|
-
const cfg = files.config;
|
|
270
|
-
const cost = cfg.cost ?? {};
|
|
271
|
-
const force = ceilingInForce(root, cost);
|
|
272
|
-
const names = Object.keys(AGENT_DEFAULT_TIERS).sort();
|
|
273
|
-
const extras = Object.keys(cost.agents ?? {}).filter((n) => !(n in AGENT_DEFAULT_TIERS)).sort();
|
|
274
|
-
const agent = (name, unknown) => {
|
|
275
|
-
const e = effective(name, cost, force.value);
|
|
276
|
-
const from = e.source === 'override' ? { from: fromFile(files, (c) => c.cost?.agents?.[name]) } : {};
|
|
277
|
-
return { name, tier: e.tier, source: e.source, ...from, default: unknown ? null : AGENT_DEFAULT_TIERS[name], ...(unknown ? { unknown: true } : {}) };
|
|
278
|
-
};
|
|
279
|
-
const agents = [...names.map((n) => agent(n, false)), ...extras.map((n) => agent(n, true))];
|
|
280
|
-
const extra = [];
|
|
281
|
-
const mapped = host !== undefined && (Object.hasOwn(HOST_MODELS, host) || (cost.mapping && Object.hasOwn(cost.mapping, host)));
|
|
282
|
-
const highs = agents.filter((a) => a.tier === 'high').length;
|
|
283
|
-
if (host !== undefined && !mapped && highs > 0) extra.push(unmappedWarning(host, highs));
|
|
284
|
-
return {
|
|
285
|
-
path: path.join('.somi', 'config.json'),
|
|
286
|
-
exists: files.project.present,
|
|
287
|
-
global: { path: files.global.path, exists: files.global.present },
|
|
288
|
-
ceiling: cost.ceiling === undefined ? { value: DEFAULT_CEILING, source: 'default' } : { value: cost.ceiling, source: 'config', from: fromFile(files, (c) => c.cost?.ceiling) },
|
|
289
|
-
effective_ceiling: force,
|
|
290
|
-
agents,
|
|
291
|
-
warnings: warningsFor(cost, force, extra),
|
|
292
|
-
};
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// Key and tier errors are the caller's (64).
|
|
296
|
-
const edit = (fn) => { try { return fn(); } catch (e) { if (e instanceof EditError) return usage(e.message); throw e; } };
|
|
297
|
-
|
|
298
|
-
function ensureSomi(loc) {
|
|
299
|
-
if (loc.global) {
|
|
300
|
-
try { fs.mkdirSync(loc.somi, { recursive: true, mode: 0o700 }); loc.dirStat = fs.statSync(loc.somi); } catch { project('the global config directory could not be created'); }
|
|
301
|
-
if (!loc.dirStat.isDirectory()) project('the global config directory is not a directory');
|
|
302
|
-
return;
|
|
303
|
-
}
|
|
304
|
-
if (!loc.dirExists) {
|
|
305
|
-
try { fs.mkdirSync(loc.somi, { mode: 0o700 }); } catch (e) {
|
|
306
|
-
if (e.code !== 'EEXIST' || !lstatOrNull(loc.somi)?.isDirectory()) project('.somi could not be created');
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
loc.dirStat = lstatOrNull(loc.somi);
|
|
310
|
-
if (!loc.dirStat?.isDirectory()) project('.somi is a link or not a directory; refusing');
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
// Writes `text` to a fresh temp file beside config.json (exclusive, no-follow), fsyncs it, re-checks that
|
|
314
|
-
// .somi is still the directory we inspected, and renames it over; with `noClobber` it hard-links instead,
|
|
315
|
-
// which fails rather than replace a file that appeared meanwhile. `mode`, when given (replacing an
|
|
316
|
-
// existing file), is that file's permission bits; a new file gets 0644 minus the umask.
|
|
317
|
-
function writeAtomic(loc, text, { noClobber = false, mode } = {}) {
|
|
318
|
-
const tmp = path.join(loc.somi, `config.json.${process.pid}.${Math.random().toString(36).slice(2)}.tmp`);
|
|
319
|
-
let fd;
|
|
320
|
-
try {
|
|
321
|
-
fd = fs.openSync(tmp, WRITE_FLAGS, 0o644);
|
|
322
|
-
const buf = Buffer.from(text);
|
|
323
|
-
for (let off = 0; off < buf.length;) off += fs.writeSync(fd, buf, off, buf.length - off);
|
|
324
|
-
if (mode !== undefined) fs.fchmodSync(fd, mode);
|
|
325
|
-
fs.fsyncSync(fd);
|
|
326
|
-
fs.closeSync(fd); fd = undefined;
|
|
327
|
-
const now = loc.global ? fs.statSync(loc.somi, { throwIfNoEntry: false }) : lstatOrNull(loc.somi);
|
|
328
|
-
if (!now || !now.isDirectory() || now.dev !== loc.dirStat.dev || now.ino !== loc.dirStat.ino) project(`${loc.global ? 'the global config directory' : '.somi'} changed while writing; nothing was replaced`);
|
|
329
|
-
if (noClobber) fs.linkSync(tmp, loc.file); else fs.renameSync(tmp, loc.file);
|
|
330
|
-
return true;
|
|
331
|
-
} catch (e) {
|
|
332
|
-
if (e instanceof ExitSignal) throw e;
|
|
333
|
-
if (noClobber && e.code === 'EEXIST') return false;
|
|
334
|
-
return project('config.json could not be written');
|
|
335
|
-
} finally {
|
|
336
|
-
if (fd !== undefined) try { fs.closeSync(fd); } catch { /* closed */ }
|
|
337
|
-
try { fs.rmSync(tmp, { force: true }); } catch { /* best effort */ }
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
function main() {
|
|
342
|
-
const { cmd, pos, dir, refuse, host, global } = parseArgs(process.argv.slice(2));
|
|
343
|
-
const root = projectRoot(dir);
|
|
344
|
-
const writes = cmd === 'set' || cmd === 'unset' || cmd === 'init';
|
|
345
|
-
// Only a write inspects the target's links; show and get read through them.
|
|
346
|
-
const loc = !writes ? null : (global ? locateGlobal() : locate(root));
|
|
347
|
-
const rel = global ? loc.shown : path.join('.somi', 'config.json');
|
|
348
|
-
|
|
349
|
-
if (cmd === 'init') {
|
|
350
|
-
if (loc.fileExists) return { path: rel, created: false };
|
|
351
|
-
const g = global ? null : globalConfigPath();
|
|
352
|
-
if (g !== null && fs.lstatSync(g, { throwIfNoEntry: false })) return { path: rel, created: false, reason: 'a global config exists and holds the defaults' };
|
|
353
|
-
ensureSomi(loc);
|
|
354
|
-
return { path: rel, created: writeAtomic(loc, serializeDefaultConfig(), { noClobber: true }) };
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const files = loadAll(root);
|
|
358
|
-
const k = cmd === 'show' ? null : edit(() => parseKey(pos[0]));
|
|
359
|
-
if (cmd === 'unset' && !(k.kind === 'agent' || (k.kind === 'mapping' && k.tier === undefined))) usage('only cost.agents.<name> and cost.mapping.<host> can be unset');
|
|
360
|
-
if (files.config.ceiling !== undefined) project('"ceiling" at the top level of config.json belongs under "cost.ceiling"');
|
|
361
|
-
checkCost(files.config.cost, project);
|
|
362
|
-
if (cmd === 'show') return show(root, files, host);
|
|
363
|
-
|
|
364
|
-
if (cmd === 'get') {
|
|
365
|
-
const merged = files.config.cost;
|
|
366
|
-
const force = ceilingInForce(root, merged);
|
|
367
|
-
if (k.kind === 'ceiling') return { key: pos[0], value: merged?.ceiling ?? DEFAULT_CEILING, source: merged?.ceiling === undefined ? 'default' : 'config', effective_ceiling: force };
|
|
368
|
-
if (k.kind === 'mapping') {
|
|
369
|
-
const hostMap = merged?.mapping?.[k.host];
|
|
370
|
-
const value = hostMap === undefined ? null : (k.tier === undefined ? hostMap : (hostMap[k.tier] ?? null));
|
|
371
|
-
return { key: pos[0], value, source: value === null ? 'none' : 'config' };
|
|
372
|
-
}
|
|
373
|
-
const has = merged?.agents && Object.hasOwn(merged.agents, k.name);
|
|
374
|
-
return { key: pos[0], value: has ? merged.agents[k.name] : null, source: has ? 'override' : 'none' };
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
// set/unset edit one file. A set on an absent file starts from the default file, so the default
|
|
378
|
-
// pins exist -- except a project file over a global config, which starts empty: the global file
|
|
379
|
-
// holds the defaults, and copies of them in the project would mask later global changes.
|
|
380
|
-
const cfg = load(root, loc, true);
|
|
381
|
-
const absent = !loc.fileExists;
|
|
382
|
-
const start = absent && !global && files.global.present ? {} : (absent ? null : cfg);
|
|
383
|
-
// What the resolver sees, before and after: a project edit is judged merged over the global file;
|
|
384
|
-
// a global edit on its own, since it applies to projects whose files differ.
|
|
385
|
-
const view = (c) => (global ? c : mergeConfig(files.global.config, c ?? {}));
|
|
386
|
-
const beforeCfg = view(absent ? {} : cfg);
|
|
387
|
-
const { next, changed } = edit(() => applyEdit(start, { action: cmd, key: pos[0], value: pos[1] }));
|
|
388
|
-
if (!changed) {
|
|
389
|
-
// Nothing to unset in the project, but the same unset would change the global file: say where it lives.
|
|
390
|
-
const inGlobalOnly = !global && cmd === 'unset' && edit(() => applyEdit(files.global.config, { action: cmd, key: pos[0] })).changed;
|
|
391
|
-
return { path: rel, key: pos[0], changed: false, warnings: warningsFor(beforeCfg.cost, ceilingInForce(root, beforeCfg.cost), inGlobalOnly ? [GLOBAL_ONLY_WARNING] : []) };
|
|
392
|
-
}
|
|
393
|
-
checkCost(next.cost, (m) => usage(m)); // the whole resulting block; a failure here is the caller's input
|
|
394
|
-
const after = view(next);
|
|
395
|
-
if (refuse) {
|
|
396
|
-
const v = lowers(beforeCfg, after, root);
|
|
397
|
-
if (v.lowers) usage(v.reasons.every((r) => r === REASONS.mapping) ? MAPPING_MESSAGE : LOWERING_MESSAGE);
|
|
398
|
-
}
|
|
399
|
-
const value = cmd !== 'set' ? undefined : (k.kind === 'mapping' ? next.cost.mapping[k.host][k.tier] : pos[1]);
|
|
400
|
-
const extra = k.kind === 'agent' && cmd === 'set' && !(k.name in AGENT_DEFAULT_TIERS) && k.name !== 'somi' ? [UNKNOWN_WARNING] : [];
|
|
401
|
-
let text;
|
|
402
|
-
try {
|
|
403
|
-
text = `${JSON.stringify(next, null, 2)}\n`;
|
|
404
|
-
if (JSON.stringify(JSON.parse(text)) !== JSON.stringify(next)) project('config.json could not be rewritten faithfully; nothing was written');
|
|
405
|
-
} catch (e) {
|
|
406
|
-
if (e instanceof ExitSignal) throw e;
|
|
407
|
-
project('config.json is too deeply nested to rewrite');
|
|
408
|
-
}
|
|
409
|
-
if (Buffer.byteLength(text) > MAX_CONFIG_BYTES) project('the edited config.json would exceed the size limit; nothing was written');
|
|
410
|
-
ensureSomi(loc);
|
|
411
|
-
writeAtomic(loc, text, { mode: loc.fileExists ? (fs.statSync(loc.file).mode & 0o777) : undefined });
|
|
412
|
-
return { path: rel, key: pos[0], ...(cmd === 'set' ? { value } : {}), changed, effective_ceiling: ceilingInForce(root, after.cost), warnings: warningsFor(after.cost, ceilingInForce(root, after.cost), extra) };
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
try {
|
|
416
|
-
const out = main();
|
|
417
|
-
process.stdout.write(`${ascii(out)}\n`);
|
|
418
|
-
} catch (e) {
|
|
419
|
-
if (e instanceof ExitSignal) process.exitCode = e.code;
|
|
420
|
-
else throw e;
|
|
421
|
-
}
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// somi-dispatch.mjs — CLI over scripts/lib/dispatch-resolver.mjs's resolveDispatch(): for a given
|
|
3
|
-
// agent, right now, what tier and model should run it?
|
|
4
|
-
//
|
|
5
|
-
// Zero-dependency: stdlib only. This is the mechanism a prompt (agents/somi.md) shells out to
|
|
6
|
-
// instead of judging cost tiers itself — the front door cannot invoke resolveModel/decideDispatch
|
|
7
|
-
// directly (a prompt has no way to call a function), so the arithmetic has to live in a shipped
|
|
8
|
-
// command it can run, mirroring how scripts/somi-loop.mjs is the state engine /code-loop shells
|
|
9
|
-
// out to rather than reimplementing inline.
|
|
10
|
-
//
|
|
11
|
-
// scripts/somi-mcp.mjs is the other consumer of the identical resolveDispatch() — a
|
|
12
|
-
// zero-dependency stdio MCP server, for hosts that launch a bundled MCP server instead of a
|
|
13
|
-
// shell. Neither surface re-derives the composition or the validation order; both call
|
|
14
|
-
// resolveDispatch() and turn its {ok, ...} result into their own shape (this file: exit code +
|
|
15
|
-
// stdout JSON / stderr message; the MCP server: a tool result). See
|
|
16
|
-
// scripts/lib/dispatch-resolver.mjs for the composition itself and the full error-code table.
|
|
17
|
-
//
|
|
18
|
-
// Agent files are resolved against THIS SCRIPT's own directory's parent (installRoot below), not
|
|
19
|
-
// the caller's project — a consuming project may have no agents/ directory of its own at all.
|
|
20
|
-
// `--agent` comes from a prompt, which may carry untrusted text, so the name is validated against
|
|
21
|
-
// a strict allowlist pattern before it ever touches a path (inside resolveDispatch()).
|
|
22
|
-
//
|
|
23
|
-
// Exit codes (callers branch on these — do not repurpose; scripts/lib/dispatch-resolver.mjs
|
|
24
|
-
// documents in full what produces each one, since the MCP server maps onto the identical four):
|
|
25
|
-
// 0 ok
|
|
26
|
-
// 64 usage error
|
|
27
|
-
// 65 unknown agent
|
|
28
|
-
// 66 no tier source (the exempt `somi` agent, or an agent file with no default-tier-table entry)
|
|
29
|
-
// 67 project environment/config failure
|
|
30
|
-
// A guessed model is never printed on any error path — only the success path emits JSON.
|
|
31
|
-
|
|
32
|
-
import path from 'node:path';
|
|
33
|
-
import { fileURLToPath } from 'node:url';
|
|
34
|
-
import { resolveDispatch } from './lib/dispatch-resolver.mjs';
|
|
35
|
-
|
|
36
|
-
const PROG = 'somi-dispatch';
|
|
37
|
-
const USAGE = `usage: ${PROG}.mjs resolve --agent <name> [--host <host>] [--ceiling <tier>]`;
|
|
38
|
-
|
|
39
|
-
// EXIT_* are declared here, literally, even though only EXIT_USAGE is READ elsewhere in this
|
|
40
|
-
// file: scripts/lib/dispatch-resolver.mjs owns the canonical values and returns the numeric code
|
|
41
|
-
// directly in its {ok:false, code, message} result, rather than this file re-deriving it from
|
|
42
|
-
// these names. tests/scripts/somi-dispatch.sh and tests/scripts/somi-agent-dispatch-contract.sh
|
|
43
|
-
// both grep this exact `^const EXIT_[A-Z_]+ = [0-9]+;` shape from THIS file to prove the four
|
|
44
|
-
// codes stay pairwise distinct and match what agents/somi.md documents — removing the unused
|
|
45
|
-
// three would break that contract test, so they stay, matched value-for-value against
|
|
46
|
-
// scripts/lib/dispatch-resolver.mjs's own declarations.
|
|
47
|
-
const EXIT_USAGE = 64;
|
|
48
|
-
const EXIT_UNKNOWN_AGENT = 65;
|
|
49
|
-
const EXIT_MALFORMED = 66;
|
|
50
|
-
const EXIT_PROJECT_ENV = 67;
|
|
51
|
-
|
|
52
|
-
class ExitSignal extends Error {
|
|
53
|
-
constructor(code) {
|
|
54
|
-
super(`exit ${code}`);
|
|
55
|
-
this.code = code;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function fail(code, msg) {
|
|
60
|
-
process.stderr.write(`${PROG}: ${msg}\n`);
|
|
61
|
-
throw new ExitSignal(code);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function die(msg) {
|
|
65
|
-
fail(EXIT_USAGE, `${msg}\n${USAGE}`);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// A missing (`--host` last) or explicitly empty (`--host ""`) value must fail here, at parse
|
|
69
|
-
// time -- not fall through to a `value || 'default'` downstream, which can't tell "not given"
|
|
70
|
-
// (correctly defaulted) from "given with nothing after it" (a caller error masquerading as one).
|
|
71
|
-
function requireValue(flag, value) {
|
|
72
|
-
if (value === undefined || value === '') die(`${flag} requires a non-empty value`);
|
|
73
|
-
return value;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// Matches scripts/somi-loop.mjs's projectRoot(): the caller's project, where .somi/config.json
|
|
77
|
-
// and .somi/somi-state/ceiling.json live. Deliberately NOT where agent files are read from —
|
|
78
|
-
// see installRoot() below.
|
|
79
|
-
function projectRoot() {
|
|
80
|
-
let b = process.env.CLAUDE_PROJECT_DIR || process.cwd();
|
|
81
|
-
if (b.includes('${')) b = process.cwd();
|
|
82
|
-
return b;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// SoMi's own install root: this file's directory's parent. A consuming project may have no
|
|
86
|
-
// agents/ of its own, so agent frontmatter is always read from where SoMi itself is installed,
|
|
87
|
-
// never from projectRoot().
|
|
88
|
-
function installRoot() {
|
|
89
|
-
return path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function main() {
|
|
93
|
-
const argv = process.argv.slice(2);
|
|
94
|
-
const CMD = argv[0] || '';
|
|
95
|
-
let rest = argv.slice(1);
|
|
96
|
-
|
|
97
|
-
// `undefined`, not `''`, is "not given" -- the same sentinel
|
|
98
|
-
// scripts/lib/dispatch-resolver.mjs's validateDispatchArgs() expects from every caller
|
|
99
|
-
// (scripts/somi-mcp.mjs's tool arguments use it natively; an omitted JSON field is `undefined`
|
|
100
|
-
// after parsing). `--host`/`--ceiling` can never reach `main()`'s end as `''`: requireValue()
|
|
101
|
-
// dies on an explicitly empty flag value before assignment, so only "never passed" survives to
|
|
102
|
-
// here, always as `undefined`.
|
|
103
|
-
let AGENT;
|
|
104
|
-
let HOST;
|
|
105
|
-
let CEILING;
|
|
106
|
-
|
|
107
|
-
while (rest.length > 0) {
|
|
108
|
-
const a = rest[0];
|
|
109
|
-
switch (a) {
|
|
110
|
-
case '--agent': AGENT = requireValue(a, rest[1]); rest = rest.slice(2); break;
|
|
111
|
-
case '--host': HOST = requireValue(a, rest[1]); rest = rest.slice(2); break;
|
|
112
|
-
case '--ceiling': CEILING = requireValue(a, rest[1]); rest = rest.slice(2); break;
|
|
113
|
-
default: die(`unknown argument: ${a}`);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (CMD !== 'resolve') die(CMD ? `unknown subcommand: ${CMD}` : 'a subcommand is required');
|
|
118
|
-
if (!AGENT) die('--agent is required');
|
|
119
|
-
|
|
120
|
-
const outcome = resolveDispatch({
|
|
121
|
-
agent: AGENT,
|
|
122
|
-
host: HOST,
|
|
123
|
-
ceiling: CEILING,
|
|
124
|
-
projectRoot: projectRoot(),
|
|
125
|
-
installRoot: installRoot(),
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
if (!outcome.ok) fail(outcome.code, outcome.message);
|
|
129
|
-
|
|
130
|
-
console.log(JSON.stringify(outcome.result));
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
try {
|
|
134
|
-
main();
|
|
135
|
-
} catch (e) {
|
|
136
|
-
if (e instanceof ExitSignal) {
|
|
137
|
-
process.exitCode = e.code;
|
|
138
|
-
} else {
|
|
139
|
-
throw e;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
// somi-findings.mjs — the findings ledger: identity and lifecycle for review findings.
|
|
2
|
-
//
|
|
3
|
-
// Zero-dependency: stdlib only. A thin CLI: it parses arguments, reads the project root, and reads and parses
|
|
4
|
-
// stdin (the trust boundary of the shell path); every handler lives in lib/findings-ledger.mjs and returns
|
|
5
|
-
// {lines, code, message}, which is printed here. Exit codes and stdout JSON shapes are frozen; see the bash
|
|
6
|
-
// original's header comment for the rationale (finding identity/lifecycle, consecutive-vs-cross-run
|
|
7
|
-
// recurrence, why line numbers are excluded from the locus).
|
|
8
|
-
//
|
|
9
|
-
// Ledger: .somi/reviews/<slug>/findings.json under the project root, committed with the other review artifacts.
|
|
10
|
-
// Subcommands (all take --slug S):
|
|
11
|
-
// record [--review FILE] [--run ID] [--pass N] stdin: JSON array [{file, symbol, title, severity, confidence}, …]
|
|
12
|
-
// Upserts each finding (new locus → new F-<n>; known OPEN locus → appends a sighting); one JSON line per
|
|
13
|
-
// finding. Exit 5 if ANY finding recurred in consecutive passes, judged after all are processed.
|
|
14
|
-
// resolve --id F-3 --status fixed|accepted|wontfix [--by REVIEW]
|
|
15
|
-
// reopen --id F-3 [--by REVIEW]
|
|
16
|
-
// open → JSON array of open findings get --id F-3 → one finding
|
|
17
|
-
// Exit codes: 0 ok · 5 consecutive recurrence detected (record only) · 64 error.
|
|
18
|
-
// Tested by tests/scripts/run.sh (wired into scripts/validate.sh / CI).
|
|
19
|
-
|
|
20
|
-
import fs from 'node:fs';
|
|
21
|
-
import { PROG, getFinding, openFindings, recordFindings, reopenFinding, resolveFinding } from './lib/findings-ledger.mjs';
|
|
22
|
-
|
|
23
|
-
const FLAGS = { '--slug': 'slug', '--review': 'review', '--run': 'run', '--pass': 'pass', '--id': 'id', '--status': 'status', '--by': 'by' };
|
|
24
|
-
|
|
25
|
-
// Thrown to unwind to the top-level handler, which sets process.exitCode and lets Node exit naturally —
|
|
26
|
-
// avoids process.exit()'s risk of truncating buffered stdout/stderr writes on a pipe.
|
|
27
|
-
class ExitSignal extends Error {
|
|
28
|
-
constructor(code) {
|
|
29
|
-
super(`exit ${code}`);
|
|
30
|
-
this.code = code;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function die(msg) {
|
|
35
|
-
process.stderr.write(`${PROG}: ${msg}\n`);
|
|
36
|
-
throw new ExitSignal(64);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function projectRoot() {
|
|
40
|
-
let b = process.env.CLAUDE_PROJECT_DIR || process.cwd();
|
|
41
|
-
if (b.includes('${')) b = process.cwd();
|
|
42
|
-
return b;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function readFindingsFromStdin() {
|
|
46
|
-
let text;
|
|
47
|
-
try {
|
|
48
|
-
text = fs.readFileSync(0, 'utf8');
|
|
49
|
-
} catch {
|
|
50
|
-
// Any OS-level read failure — e.g. Windows raises EAGAIN for a synchronous read of a TTY console handle
|
|
51
|
-
// (no piped input attached) rather than blocking — degrades to the same clean die() as every bad input.
|
|
52
|
-
die('failed to read stdin — findings must be piped in as a JSON array');
|
|
53
|
-
}
|
|
54
|
-
let input;
|
|
55
|
-
try {
|
|
56
|
-
input = JSON.parse(text);
|
|
57
|
-
} catch {
|
|
58
|
-
die('stdin must be a JSON array of findings');
|
|
59
|
-
}
|
|
60
|
-
if (!Array.isArray(input)) die('stdin must be a JSON array of findings');
|
|
61
|
-
return input;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function main() {
|
|
65
|
-
const [cmd = '', ...argv] = process.argv.slice(2);
|
|
66
|
-
const a = { slug: '', review: '', run: '', pass: 0, id: '', status: '', by: '' };
|
|
67
|
-
for (let rest = argv; rest.length > 0; rest = rest.slice(2)) {
|
|
68
|
-
if (!Object.hasOwn(FLAGS, rest[0])) die(`unknown argument: ${rest[0]}`);
|
|
69
|
-
const key = FLAGS[rest[0]];
|
|
70
|
-
a[key] = key === 'pass' ? Number(rest[1]) : rest[1];
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (!cmd) die('usage: somi-findings.mjs <record|resolve|reopen|open|get> --slug <slug> …');
|
|
74
|
-
if (!a.slug) die('--slug is required');
|
|
75
|
-
|
|
76
|
-
const call = { root: projectRoot(), ...a };
|
|
77
|
-
const handlers = {
|
|
78
|
-
record: () => recordFindings({ ...call, input: readFindingsFromStdin() }),
|
|
79
|
-
resolve: () => resolveFinding(call),
|
|
80
|
-
reopen: () => reopenFinding(call),
|
|
81
|
-
open: () => openFindings(call),
|
|
82
|
-
get: () => getFinding(call),
|
|
83
|
-
};
|
|
84
|
-
if (!Object.hasOwn(handlers, cmd)) die(`unknown subcommand: ${cmd}`);
|
|
85
|
-
|
|
86
|
-
const result = handlers[cmd]();
|
|
87
|
-
for (const line of result.lines) console.log(line);
|
|
88
|
-
if (result.message) process.stderr.write(`${result.message}\n`);
|
|
89
|
-
process.exitCode = result.code;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
try {
|
|
93
|
-
main();
|
|
94
|
-
} catch (e) {
|
|
95
|
-
if (e instanceof ExitSignal) {
|
|
96
|
-
process.exitCode = e.code;
|
|
97
|
-
} else {
|
|
98
|
-
throw e;
|
|
99
|
-
}
|
|
100
|
-
}
|