@skathio/somi 4.1.1 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +83 -228
- 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 +28 -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 +40 -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 +165 -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 +73 -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 +38 -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 +181 -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
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
// mcp-project-root.mjs — resolves the CONSUMING project's root for scripts/somi-mcp.mjs's
|
|
2
|
-
//
|
|
3
|
-
// location, so scripts/somi-dispatch.mjs's own projectRoot() (CLAUDE_PROJECT_DIR, else this
|
|
4
|
-
// PROCESS's cwd) is unsafe to reuse: neither host's docs confirm the server's launch-time cwd is
|
|
1
|
+
// mcp-project-root.mjs — resolves the CONSUMING project's root for scripts/somi-mcp.mjs. An MCP server is launched once, at session start, from the plugin's own
|
|
2
|
+
// location, so this PROCESS's cwd is unsafe to use: neither host's docs confirm the server's launch-time cwd is
|
|
5
3
|
// the user's project rather than the plugin's own install dir. No cwd fallback here — that would
|
|
6
4
|
// risk silently resolving to SoMi's own repo, the exact failure this module exists to rule out.
|
|
7
5
|
//
|
|
@@ -34,7 +32,8 @@
|
|
|
34
32
|
import fs from 'node:fs';
|
|
35
33
|
import path from 'node:path';
|
|
36
34
|
import { fileURLToPath } from 'node:url';
|
|
37
|
-
|
|
35
|
+
const EXIT_USAGE = 64;
|
|
36
|
+
const EXIT_PROJECT_ENV = 67;
|
|
38
37
|
|
|
39
38
|
// Structural check only (absolute, exists, a directory) — shared by `project_dir` and every
|
|
40
39
|
// `roots/list` candidate so the two can't drift on what counts as usable. Returns a reason, not a
|
|
@@ -111,84 +110,81 @@ export async function resolveProjectRoot(opts) {
|
|
|
111
110
|
return { ...(await resolveWith(opts, seen)), hosted: seen.hosted };
|
|
112
111
|
}
|
|
113
112
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
projectDir = v.dir;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const host = await hostRoot({ listRoots });
|
|
133
|
-
seen.hosted = host !== undefined;
|
|
134
|
-
|
|
135
|
-
if (!host) {
|
|
136
|
-
// Host supplies nothing: project_dir alone decides.
|
|
137
|
-
if (projectDir) return { ok: true, root: projectDir, source: 'project_dir' };
|
|
138
|
-
return {
|
|
139
|
-
ok: false,
|
|
140
|
-
code: EXIT_PROJECT_ENV,
|
|
141
|
-
message:
|
|
142
|
-
'no project root could be established: CLAUDE_PROJECT_DIR is unset, the client offered no ' +
|
|
143
|
-
'MCP roots (or none were returned), and no project_dir argument was given -- pass ' +
|
|
144
|
-
'project_dir explicitly; this never falls back to the SoMi install directory',
|
|
145
|
-
};
|
|
146
|
-
}
|
|
113
|
+
// Validated first, whether or not a host root exists: a malformed value is 64 either way, and the bounding check needs a real,
|
|
114
|
+
// fs-verified directory to compare against.
|
|
115
|
+
function validatedProjectDir(projectDirArg) {
|
|
116
|
+
if (projectDirArg === undefined) return { dir: undefined };
|
|
117
|
+
const v = validateDir(projectDirArg);
|
|
118
|
+
if (v.ok) return { dir: v.dir };
|
|
119
|
+
const reason = {
|
|
120
|
+
empty: `project_dir requires a non-empty string, got ${echo(projectDirArg)}`,
|
|
121
|
+
'not-absolute': `project_dir must be an absolute path, got ${echo(projectDirArg)}`,
|
|
122
|
+
missing: `project_dir ${echo(projectDirArg)} does not exist`,
|
|
123
|
+
'not-a-directory': `project_dir ${echo(projectDirArg)} is not a directory`,
|
|
124
|
+
}[v.reason];
|
|
125
|
+
return { refusal: { ok: false, code: EXIT_USAGE, message: reason } };
|
|
126
|
+
}
|
|
147
127
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
128
|
+
// Host supplies nothing: project_dir alone decides.
|
|
129
|
+
function withoutHost(projectDir) {
|
|
130
|
+
if (projectDir) return { ok: true, root: projectDir, source: 'project_dir' };
|
|
131
|
+
return {
|
|
132
|
+
ok: false,
|
|
133
|
+
code: EXIT_PROJECT_ENV,
|
|
134
|
+
message:
|
|
135
|
+
'no project root could be established: CLAUDE_PROJECT_DIR is unset, the client offered no ' +
|
|
136
|
+
'MCP roots (or none were returned), and no project_dir argument was given -- pass ' +
|
|
137
|
+
'project_dir explicitly; this never falls back to the SoMi install directory',
|
|
138
|
+
};
|
|
139
|
+
}
|
|
156
140
|
|
|
157
|
-
|
|
141
|
+
// No project_dir: the host's one root, refused when it offers several.
|
|
142
|
+
function hostRootOnly(validCandidates, host) {
|
|
143
|
+
if (validCandidates.length > 1) {
|
|
158
144
|
return {
|
|
159
145
|
ok: false,
|
|
160
146
|
code: EXIT_PROJECT_ENV,
|
|
161
|
-
message: `the
|
|
147
|
+
message: `ambiguous: the client offered ${validCandidates.length} MCP roots and no project_dir was given to select one`,
|
|
162
148
|
};
|
|
163
149
|
}
|
|
150
|
+
return { ok: true, root: validCandidates[0], source: host.source };
|
|
151
|
+
}
|
|
164
152
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
ok: false,
|
|
169
|
-
code: EXIT_PROJECT_ENV,
|
|
170
|
-
message: `ambiguous: the client offered ${validCandidates.length} MCP roots and no project_dir was given to select one`,
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
return { ok: true, root: validCandidates[0], source: host.source };
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// A host root exists AND project_dir is given: it must realpath to one of the host's own
|
|
177
|
-
// candidates, or inside one -- a model argument narrows the host's root, never overrides it.
|
|
153
|
+
// A host root exists AND project_dir is given: it must realpath to one of the host's own candidates, or inside one -- a model
|
|
154
|
+
// argument narrows the host's root, never overrides it.
|
|
155
|
+
function boundedProjectDir(projectDir, validCandidates) {
|
|
178
156
|
const projectDirReal = realpathOrNull(projectDir);
|
|
179
157
|
const matches = projectDirReal !== null && validCandidates.some((c) => {
|
|
180
158
|
const candidateReal = realpathOrNull(c);
|
|
181
159
|
if (candidateReal === null) return false;
|
|
182
160
|
return projectDirReal === candidateReal || projectDirReal.startsWith(candidateReal + path.sep);
|
|
183
161
|
});
|
|
184
|
-
if (
|
|
162
|
+
if (matches) return { ok: true, root: projectDir, source: 'project_dir' };
|
|
163
|
+
return {
|
|
164
|
+
ok: false,
|
|
165
|
+
code: EXIT_USAGE,
|
|
166
|
+
message:
|
|
167
|
+
`project_dir ${echo(projectDir)} is outside the host-supplied root -- refusing (project_dir ` +
|
|
168
|
+
`may only select a directory inside the host's own root, never override it)`,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function resolveWith({ projectDirArg, listRoots }, seen) {
|
|
173
|
+
const { dir: projectDir, refusal } = validatedProjectDir(projectDirArg);
|
|
174
|
+
if (refusal) return refusal;
|
|
175
|
+
const host = await hostRoot({ listRoots });
|
|
176
|
+
seen.hosted = host !== undefined;
|
|
177
|
+
if (!host) return withoutHost(projectDir);
|
|
178
|
+
// Each host-offered candidate must itself exist as a directory -- an entry that fails is dropped, not treated as its own hard
|
|
179
|
+
// error (a multi-folder client can legitimately offer one root this process can't see). CLAUDE_PROJECT_DIR's single candidate
|
|
180
|
+
// failing is exactly "a root that doesn't exist", refused below like any other.
|
|
181
|
+
const validCandidates = host.dirs.map((d) => validateDir(d)).filter((v) => v.ok).map((v) => v.dir);
|
|
182
|
+
if (validCandidates.length === 0) {
|
|
185
183
|
return {
|
|
186
184
|
ok: false,
|
|
187
|
-
code:
|
|
188
|
-
message:
|
|
189
|
-
`project_dir ${echo(projectDir)} is outside the host-supplied root -- refusing (project_dir ` +
|
|
190
|
-
`may only select a directory inside the host's own root, never override it)`,
|
|
185
|
+
code: EXIT_PROJECT_ENV,
|
|
186
|
+
message: `the host-supplied root (${host.source}) does not resolve to an existing directory -- refusing rather than creating one`,
|
|
191
187
|
};
|
|
192
188
|
}
|
|
193
|
-
return
|
|
189
|
+
return projectDir ? boundedProjectDir(projectDir, validCandidates) : hostRootOnly(validCandidates, host);
|
|
194
190
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// migrate.mjs: imports what is still open from a 4.x `.somi/` tree: open findings to a record of their own, decisions to the ledger. Old files
|
|
2
|
+
// are only read, never changed, and 5.0 does not read them again afterwards. Those files may come from a cloned repository, so links,
|
|
3
|
+
// odd slugs and oversized files are skipped rather than followed.
|
|
4
|
+
|
|
5
|
+
import fs from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { addToLedger } from './ledger.mjs';
|
|
8
|
+
import { appendStateFile, statePaths } from './state.mjs';
|
|
9
|
+
import { SEVERITIES } from './policy.mjs';
|
|
10
|
+
|
|
11
|
+
const SLUG = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/;
|
|
12
|
+
const MAX_FILE_BYTES = 5 * 1024 * 1024;
|
|
13
|
+
const MAX_ITEMS = 50;
|
|
14
|
+
const MAX_TEXT = 400;
|
|
15
|
+
|
|
16
|
+
function readPlainFile(file) {
|
|
17
|
+
let st;
|
|
18
|
+
try { st = fs.lstatSync(file); } catch { return null; }
|
|
19
|
+
if (!st.isFile() || st.size > MAX_FILE_BYTES) return null;
|
|
20
|
+
return fs.readFileSync(file, 'utf8');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function listSlugs(dir) {
|
|
24
|
+
let entries;
|
|
25
|
+
try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return []; }
|
|
26
|
+
return entries.filter((e) => e.isDirectory() && SLUG.test(e.name)).map((e) => e.name);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function openFindings(root, slug) {
|
|
30
|
+
const text = readPlainFile(path.join(root, '.somi', 'reviews', slug, 'findings.json'));
|
|
31
|
+
if (text === null) return [];
|
|
32
|
+
let parsed;
|
|
33
|
+
try { parsed = JSON.parse(text); } catch { return []; }
|
|
34
|
+
const list = Array.isArray(parsed?.findings) ? parsed.findings : [];
|
|
35
|
+
return list.filter((f) => f?.status === 'open' && typeof f.title === 'string').slice(0, MAX_ITEMS).map((f) => ({
|
|
36
|
+
id: String(f.id ?? '').slice(0, 32),
|
|
37
|
+
severity: SEVERITIES.includes(f.severity) ? f.severity : 'Minor',
|
|
38
|
+
file: typeof f.locus?.file === 'string' ? f.locus.file.slice(0, 240) : slug,
|
|
39
|
+
line: null,
|
|
40
|
+
claim: f.title.slice(0, MAX_TEXT),
|
|
41
|
+
}));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function recordedDecisions(root, slug) {
|
|
45
|
+
const text = readPlainFile(path.join(root, '.somi', 'plans', slug, 'decisions.md'));
|
|
46
|
+
if (text === null) return [];
|
|
47
|
+
return [...text.matchAll(/^## (D\d+[a-z]?)\s*[—-]\s*(.+)$/gm)].slice(0, MAX_ITEMS).map((m) => `${slug}: ${m[2].slice(0, MAX_TEXT)}`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export const readMigrated = (root, env = process.env) => {
|
|
51
|
+
try { return fs.readFileSync(path.join(statePaths(root, env).dir, 'migrated.jsonl'), 'utf8').split('\n').filter(Boolean).map((l) => JSON.parse(l)); } catch (e) { if (e.code === 'ENOENT') return []; throw e; }
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export function migrate({ root, env = process.env, now }) {
|
|
55
|
+
const somi = path.join(root, '.somi');
|
|
56
|
+
try { if (fs.lstatSync(somi).isSymbolicLink()) return { imported: [], skipped: ['.somi is a link'] }; } catch { return { imported: [], skipped: [] }; }
|
|
57
|
+
const slugs = [...new Set([...listSlugs(path.join(somi, 'plans')), ...listSlugs(path.join(somi, 'reviews'))])].sort();
|
|
58
|
+
const done = new Set(readMigrated(root, env).map((m) => m.slug));
|
|
59
|
+
const imported = [];
|
|
60
|
+
const skipped = [];
|
|
61
|
+
for (const slug of slugs) {
|
|
62
|
+
if (done.has(slug)) continue;
|
|
63
|
+
const findings = openFindings(root, slug);
|
|
64
|
+
const decisions = recordedDecisions(root, slug);
|
|
65
|
+
if (findings.length + decisions.length === 0) continue;
|
|
66
|
+
// A full ledger must not lose a decision without anyone seeing, so the record keeps them all and the answer says which did not fit.
|
|
67
|
+
let noted = 0;
|
|
68
|
+
for (const text of decisions) { try { addToLedger(root, { kind: 'decision', text }, env); noted += 1; } catch { skipped.push(`${slug}: ${text}`); } }
|
|
69
|
+
appendStateFile(root, path.join(statePaths(root, env).dir, 'migrated.jsonl'), `${JSON.stringify({ at: now(), slug, findings, decisions })}\n`, env);
|
|
70
|
+
imported.push({ slug, findings: findings.length, decisions: noted });
|
|
71
|
+
}
|
|
72
|
+
return { imported, skipped };
|
|
73
|
+
}
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// A denied name (path-safety's deny list) is a hit on the built-in `deny_list` name in the sensitive and boundary
|
|
8
8
|
// lists whatever the config says, so the floor it carries survives a refusal elsewhere.
|
|
9
9
|
//
|
|
10
|
-
// Config only adds: `
|
|
10
|
+
// Config only adds: `signals.sensitive_paths` globs are appended to the built-ins and `signals.module_containers`
|
|
11
11
|
// names are unioned with BUILTIN_CONTAINERS. Neither can remove a built-in, there is no map from directories to a
|
|
12
12
|
// shared module key, and a key that fails its shape check adds nothing (the defaults still apply). That keeps every
|
|
13
13
|
// fact one-sided: no config can lower a hit list or the module count. `classifyPaths` takes the config layers
|
|
@@ -140,7 +140,7 @@ const layersOf = (l) => (field(field(l, 'global'), 'config') !== undefined && fi
|
|
|
140
140
|
function additions(layers) {
|
|
141
141
|
const out = { sensitive: [], containers: [], refused: [] };
|
|
142
142
|
for (const layer of layers) {
|
|
143
|
-
const sizing = field(layer, '
|
|
143
|
+
const sizing = field(layer, 'signals');
|
|
144
144
|
const globs = field(sizing, 'sensitive_paths') ?? [];
|
|
145
145
|
const names = field(sizing, 'module_containers') ?? [];
|
|
146
146
|
if (bounded(globs, validGlob)) out.sensitive.push(...globs.map(compile)); else out.refused.push('sensitive_paths');
|
|
@@ -1,45 +1,15 @@
|
|
|
1
|
-
// path-safety.mjs —
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
// on the string alone, no lstat, no realpath, no open, whatever the
|
|
7
|
-
// file's state, so nothing can be learned about it
|
|
8
|
-
// { kind: 'refused', refusal, denied? } unsafe or unreadable; `refusal` is one of REFUSAL_IDS (ASCII, never
|
|
9
|
-
// the input text); `denied: true` when the name is also deny-listed,
|
|
10
|
-
// so a malformed secret name (../../.env) keeps its floor
|
|
11
|
-
// { kind: 'contained', exists, rel, ... } inside the root: a plain file (exists: true, with its size, and
|
|
12
|
-
// `answer` when a probe was given) or a new path whose parent exists
|
|
13
|
-
// `rel` is the caller's own text and may be non-ASCII; the output layer escapes it. A name over MAX_PATH_CHARS is
|
|
14
|
-
// never normalised or judged: it counts as deny-listed, so it is refused `path_length` with `denied: true`.
|
|
15
|
-
// Backslashes are separators everywhere here, so `a\..\b` is a traversal on every platform.
|
|
16
|
-
//
|
|
17
|
-
// A probe is called with the open descriptor and the size, and returns true or false; this module closes the
|
|
18
|
-
// descriptor, so it never escapes. This module returns only that boolean, so the probe must be synchronous; a
|
|
19
|
-
// probe that throws becomes the `probe` refusal, never its message. The probe must not close the descriptor: a
|
|
20
|
-
// probe that does makes this module's own close fail, which is the `probe` refusal too (a close that fails with no probe is `unreadable`).
|
|
21
|
-
//
|
|
22
|
-
// Seams: `realpath` (default fs.realpathSync.native) and `constants` (default fs.constants).
|
|
23
|
-
// shortcut: containment is checked, then the file is opened by its canonical path; a directory swapped in
|
|
24
|
-
// between the walk and the open by a process already running as the user is not defended (O_NOFOLLOW covers the
|
|
25
|
-
// last component), upgrade when the script runs for a different user than the files' owner.
|
|
26
|
-
import fs from 'node:fs';
|
|
27
|
-
import path from 'node:path';
|
|
1
|
+
// path-safety.mjs — the deny list: which path names are secrets (.env*, key and certificate files, .git/ and the like),
|
|
2
|
+
// decided on the string alone, with no lstat, realpath or open, so nothing can be learned about the file. The review and
|
|
3
|
+
// security signals and path-facts read names through it, so every caller folds and splits a name the same way.
|
|
4
|
+
// A name over MAX_PATH_CHARS is never normalised or judged: it counts as deny-listed. Backslashes are separators
|
|
5
|
+
// everywhere here, so `a\..\b` splits the same way on every platform.
|
|
28
6
|
|
|
29
7
|
export const MAX_PATHS = 40;
|
|
30
8
|
export const MAX_PATH_CHARS = 240;
|
|
9
|
+
// The largest file a script reads whole: the quality gate's parser, migrations and the eval task store share it.
|
|
31
10
|
export const MAX_FILE_BYTES = 1024 * 1024;
|
|
32
11
|
|
|
33
|
-
export const REFUSAL_IDS = Object.freeze([
|
|
34
|
-
'malformed', 'path_count', 'path_length', 'control', 'absolute', 'traversal', 'symlink', 'escape',
|
|
35
|
-
'no_parent', 'not_file', 'file_size', 'platform', 'unreadable', 'probe',
|
|
36
|
-
]);
|
|
37
|
-
|
|
38
12
|
const SEPARATORS = /[\\/]+/;
|
|
39
|
-
// Cf includes ZWNJ and ZWJ: refusing them (they matter in some scripts) is deliberate, a hidden join is a spoof.
|
|
40
|
-
export const UNSAFE_CHARS = /[\p{Cc}\p{Cf}\p{Cs}\p{Zl}\p{Zp}]/u;
|
|
41
|
-
const ABSOLUTE = /^(?:[\\/]|[a-z]:)/i;
|
|
42
|
-
const PARENT_SEGMENT = /^\.{2}[. ]*$/;
|
|
43
13
|
// A bare `config` is allowed on purpose, though the secret-write hook denies it: this match is per segment, so `config`
|
|
44
14
|
// here would deny every `config/` directory. The secret-bearing configs sit under the `.git`, `.ssh`, `.aws`, `.kube`
|
|
45
15
|
// and `.docker` segments below. `credentials` also denies a `credentials/` directory, which the hook (basename only) does not.
|
|
@@ -51,9 +21,7 @@ const NAME_SUFFIXES = ['.pem', '.key', '.p12', '.pfx', '.crt', '.cer', '.der', '
|
|
|
51
21
|
// `[^]*`, not `.*`: a line terminator in the middle of a name must not stop the match (`.` does not match one).
|
|
52
22
|
const NAME_PATTERN = /service[-_]?account[^]*\.json$|client_secret[^]*\.json$|secrets?\.(?:ya?ml|json)$|^key\.json$|\.tfstate\.[^]*\.backup$/;
|
|
53
23
|
|
|
54
|
-
const segmentsOf = (raw) => raw.split(SEPARATORS).filter((s) => s !== '' && s !== '.');
|
|
55
24
|
export const trimDots = (s) => { let e = s.length; while (e > 0 && (s[e - 1] === '.' || s[e - 1] === ' ')) e--; return s.slice(0, e); };
|
|
56
|
-
const refused = (refusal, denied = false) => (denied ? { kind: 'refused', refusal, denied: true } : { kind: 'refused', refusal });
|
|
57
25
|
|
|
58
26
|
// The fold every name test shares (case, compatibility forms): callers that read names the way the deny list does import it.
|
|
59
27
|
export const foldName = (s) => s.normalize('NFKC').toUpperCase().toLowerCase();
|
|
@@ -73,99 +41,3 @@ export function denyListed(raw) {
|
|
|
73
41
|
});
|
|
74
42
|
});
|
|
75
43
|
}
|
|
76
|
-
|
|
77
|
-
// The list-level cap. A list over it is refused whole and keeps the deny-listed fact for any name in it; a list over
|
|
78
|
-
// 4 x MAX_PATHS is not scanned at all and is refused as denied outright.
|
|
79
|
-
export function refuseList(paths) {
|
|
80
|
-
if (!Array.isArray(paths)) return refused('malformed');
|
|
81
|
-
if (paths.length <= MAX_PATHS) return null;
|
|
82
|
-
if (paths.length > 4 * MAX_PATHS) return refused('path_count', true); // not scanned: a list this long is hostile
|
|
83
|
-
return refused('path_count', paths.some(denyListed));
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function malformed(raw) {
|
|
87
|
-
if (typeof raw !== 'string') return 'malformed';
|
|
88
|
-
if (raw.length > MAX_PATH_CHARS) return 'path_length';
|
|
89
|
-
if (UNSAFE_CHARS.test(raw)) return 'control';
|
|
90
|
-
if (ABSOLUTE.test(raw)) return 'absolute';
|
|
91
|
-
const segments = segmentsOf(raw);
|
|
92
|
-
if (segments.some((s) => PARENT_SEGMENT.test(s))) return 'traversal';
|
|
93
|
-
return segments.length === 0 ? 'malformed' : null;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// Walks the root downwards one component at a time; any symlink refuses. Returns the canonical path and
|
|
97
|
-
// whether it exists, or a result to hand back.
|
|
98
|
-
function locate(root, segments, realpath) {
|
|
99
|
-
const base = realpath(root);
|
|
100
|
-
let current = base;
|
|
101
|
-
for (let i = 0; i < segments.length; i++) {
|
|
102
|
-
const next = path.join(current, segments[i]);
|
|
103
|
-
let stat;
|
|
104
|
-
try {
|
|
105
|
-
stat = fs.lstatSync(next);
|
|
106
|
-
} catch (e) {
|
|
107
|
-
if (e.code !== 'ENOENT') return { result: refused('unreadable') };
|
|
108
|
-
if (i < segments.length - 1) return { result: refused('no_parent') };
|
|
109
|
-
return { base, real: path.join(realpath(current), segments[i]), exists: false };
|
|
110
|
-
}
|
|
111
|
-
if (stat.isSymbolicLink()) return { result: refused('symlink') };
|
|
112
|
-
if (i < segments.length - 1 && !stat.isDirectory()) return { result: refused('not_file') };
|
|
113
|
-
if (i === segments.length - 1 && !stat.isFile()) return { result: refused('not_file') };
|
|
114
|
-
current = next;
|
|
115
|
-
}
|
|
116
|
-
return { base, real: realpath(current), exists: true };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// Opens without following a link or blocking on a FIFO; returns the descriptor and size, or a result.
|
|
120
|
-
function openPlainFile(real, constants) {
|
|
121
|
-
let fd;
|
|
122
|
-
try {
|
|
123
|
-
fd = fs.openSync(real, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK | (constants.O_NOCTTY ?? 0));
|
|
124
|
-
} catch (e) {
|
|
125
|
-
return { result: refused(e.code === 'ELOOP' ? 'symlink' : 'unreadable') };
|
|
126
|
-
}
|
|
127
|
-
try {
|
|
128
|
-
const stat = fs.fstatSync(fd);
|
|
129
|
-
if (!stat.isFile()) throw Object.assign(new Error(), { refusal: 'not_file' });
|
|
130
|
-
if (stat.size > MAX_FILE_BYTES) throw Object.assign(new Error(), { refusal: 'file_size' });
|
|
131
|
-
return { fd, size: stat.size };
|
|
132
|
-
} catch (e) {
|
|
133
|
-
fs.closeSync(fd);
|
|
134
|
-
return { result: refused(e.refusal ?? 'unreadable') };
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function inspectPath(root, raw, probe = null, { realpath = fs.realpathSync.native, constants = fs.constants } = {}) {
|
|
139
|
-
const denied = denyListed(raw);
|
|
140
|
-
const bad = malformed(raw);
|
|
141
|
-
if (bad) return refused(bad, denied);
|
|
142
|
-
if (denied) return { kind: 'denied' };
|
|
143
|
-
if (!(constants.O_NOFOLLOW > 0 && constants.O_NONBLOCK > 0)) return refused('platform');
|
|
144
|
-
let found;
|
|
145
|
-
try {
|
|
146
|
-
found = locate(root, segmentsOf(raw), realpath);
|
|
147
|
-
} catch {
|
|
148
|
-
return refused('unreadable');
|
|
149
|
-
}
|
|
150
|
-
if (found.result) return found.result;
|
|
151
|
-
const rel = path.relative(found.base, found.real);
|
|
152
|
-
if (rel === '' || rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) return refused('escape');
|
|
153
|
-
if (denyListed(rel)) return { kind: 'denied' };
|
|
154
|
-
const contained = { kind: 'contained', exists: found.exists, rel: rel.split(path.sep).join('/') };
|
|
155
|
-
if (!found.exists) return contained;
|
|
156
|
-
const opened = openPlainFile(found.real, constants);
|
|
157
|
-
if (opened.result) return opened.result;
|
|
158
|
-
let result = contained;
|
|
159
|
-
try {
|
|
160
|
-
contained.size = opened.size;
|
|
161
|
-
if (probe) contained.answer = probe(opened.fd, opened.size) === true;
|
|
162
|
-
} catch {
|
|
163
|
-
result = refused('probe');
|
|
164
|
-
}
|
|
165
|
-
try {
|
|
166
|
-
fs.closeSync(opened.fd);
|
|
167
|
-
} catch {
|
|
168
|
-
return refused(probe ? 'probe' : 'unreadable'); // with a probe, the likely cause is the probe closing the descriptor
|
|
169
|
-
}
|
|
170
|
-
return result;
|
|
171
|
-
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// plan.mjs: large work as a plain file. The plan is a list of milestones that a person can read and that survives compaction and a new
|
|
2
|
+
// session. It does not gate anything: it says where the work has got to, and the agent and the user decide what happens next.
|
|
3
|
+
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { clearLedger } from './ledger.mjs';
|
|
7
|
+
import { StateError, ensureSomiDir, refuseLink } from './state.mjs';
|
|
8
|
+
|
|
9
|
+
const MAX_PLAN_LINES = 200;
|
|
10
|
+
const MAX_PLAN_BYTES = 16384;
|
|
11
|
+
const DONE_MARK = ' (done)';
|
|
12
|
+
const PLAN_STEP = /^\s*(?:#{1,6}\s*)?(?:\d+[.)]\s+|[-*]\s+)?(?:milestone\b[^:]*:\s*)(.+)$/i;
|
|
13
|
+
const NUMBERED = /^\s*\d+[.)]\s+(.+)$/;
|
|
14
|
+
const planFile = (root) => path.join(root, '.somi', 'plan.md');
|
|
15
|
+
|
|
16
|
+
export function parseMilestones(text) {
|
|
17
|
+
const lines = String(text).split('\n');
|
|
18
|
+
const named = lines.map((l) => PLAN_STEP.exec(l)?.[1]).filter(Boolean);
|
|
19
|
+
const list = named.length > 0 ? named : lines.map((l) => NUMBERED.exec(l)?.[1]).filter(Boolean);
|
|
20
|
+
return list.map((s) => s.trim());
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function readPlan(root) {
|
|
24
|
+
try {
|
|
25
|
+
if (refuseLink(planFile(root))) return fs.readFileSync(planFile(root), 'utf8');
|
|
26
|
+
} catch (e) { if (e.code !== 'ENOENT' && !(e instanceof StateError)) throw e; }
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function savePlan(root, text) {
|
|
31
|
+
if (text.split('\n').length > MAX_PLAN_LINES || Buffer.byteLength(text) > MAX_PLAN_BYTES) throw new StateError(`a plan is limited to ${MAX_PLAN_LINES} lines and ${MAX_PLAN_BYTES} bytes; state milestones and their proofs, not the detail`);
|
|
32
|
+
ensureSomiDir(root);
|
|
33
|
+
const { O_WRONLY, O_CREAT, O_TRUNC, O_NOFOLLOW = 0 } = fs.constants;
|
|
34
|
+
const fd = fs.openSync(planFile(root), O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0o644);
|
|
35
|
+
try { fs.writeSync(fd, text); } finally { fs.closeSync(fd); }
|
|
36
|
+
return path.relative(root, planFile(root));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const isDone = (milestone) => milestone.endsWith(DONE_MARK);
|
|
40
|
+
|
|
41
|
+
export function planStatus(root) {
|
|
42
|
+
const text = readPlan(root);
|
|
43
|
+
if (text === null) return null;
|
|
44
|
+
const milestones = parseMilestones(text);
|
|
45
|
+
const done = milestones.filter(isDone).length;
|
|
46
|
+
const next = milestones.find((m) => !isDone(m));
|
|
47
|
+
return { total: milestones.length, done, next: next ?? null };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Marks the Nth milestone by adding to the end of its line, so the plan reads the same to a person and to the parser.
|
|
51
|
+
export function markMilestoneDone(root, n) {
|
|
52
|
+
const text = readPlan(root);
|
|
53
|
+
if (text === null) throw new StateError('there is no plan to update');
|
|
54
|
+
const lines = text.split('\n');
|
|
55
|
+
const named = lines.some((l) => PLAN_STEP.test(l));
|
|
56
|
+
const at = lines.map((l, i) => ((named ? PLAN_STEP : NUMBERED).test(l) ? i : -1)).filter((i) => i >= 0);
|
|
57
|
+
const line = at[n - 1];
|
|
58
|
+
if (!Number.isInteger(n) || line === undefined) throw new StateError(`the plan has ${at.length} milestones, and there is no number ${n}`);
|
|
59
|
+
if (!lines[line].trimEnd().endsWith(DONE_MARK)) lines[line] = `${lines[line].trimEnd()}${DONE_MARK}`;
|
|
60
|
+
savePlan(root, lines.join('\n'));
|
|
61
|
+
return planStatus(root);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// The ledger belongs to the work in the plan, and left in place its decisions would read as binding later work.
|
|
65
|
+
export function closePlan(root, env = process.env) {
|
|
66
|
+
if (readPlan(root) !== null) fs.rmSync(planFile(root), { force: true });
|
|
67
|
+
clearLedger(root, env);
|
|
68
|
+
return { closed: true };
|
|
69
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// policy.mjs: the review policy as pure functions. These constants are deliberately not configuration: a committed project
|
|
2
|
+
// config is untrusted, and nothing in it may lower when a review runs or what re-loops.
|
|
3
|
+
|
|
4
|
+
import { createHash } from 'node:crypto';
|
|
5
|
+
|
|
6
|
+
export const SEVERITIES = Object.freeze(['Blocker', 'Major', 'Minor', 'Nit']);
|
|
7
|
+
export const VERDICTS = Object.freeze(['approve', 'approve-with-comments', 'request-changes']);
|
|
8
|
+
export const MAX_PASSES = 2;
|
|
9
|
+
export const MAX_REVIEW_BYTES = 2048;
|
|
10
|
+
|
|
11
|
+
export class ReviewInputError extends Error {}
|
|
12
|
+
|
|
13
|
+
const isText = (v, max) => typeof v === 'string' && v.length > 0 && v.length <= max;
|
|
14
|
+
const CONTROL = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u;
|
|
15
|
+
const safeFile = (f) => isText(f, 240) && !CONTROL.test(f) && !f.startsWith('/') && !/^[a-z]:/i.test(f) && !f.split(/[\\/]/).includes('..');
|
|
16
|
+
|
|
17
|
+
// Line numbers drift between passes, so identity is the file plus the claim's first words.
|
|
18
|
+
function deriveFingerprint(f) {
|
|
19
|
+
const claim = f.claim.toLowerCase().replace(/[^a-z0-9]+/g, ' ').trim().split(' ').slice(0, 8).join(' ');
|
|
20
|
+
return createHash('sha256').update(`${f.file}\n${claim}`).digest('hex').slice(0, 16);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// A suggestion to remove a safety net is what a confused or injected reviewer produces, so it reaches the coder flagged.
|
|
24
|
+
const REMOVES_CHECK = /\b(remove|delete|drop|disable|skip|bypass|relax|weaken|loosen|turn off)\b[^.]{0,60}\b(check|validation|validate|guard|test|assert\w*|flag|sanitiz\w*|escap\w*|auth\w*|limit|gate|hook)\b/i;
|
|
25
|
+
|
|
26
|
+
// Long text is kept to the limit. Refusing it would throw away the whole report and pay for the review again.
|
|
27
|
+
const clip = (v) => (typeof v === 'string' ? v.slice(0, 400) : v);
|
|
28
|
+
const present = (v) => v !== undefined && v !== null;
|
|
29
|
+
|
|
30
|
+
// The checks a finding must pass, in this order: what it is, how bad, where, what it claims, then its optional evidence and line.
|
|
31
|
+
function checkFinding(raw, where) {
|
|
32
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) throw new ReviewInputError(`${where} is not an object`);
|
|
33
|
+
if (!SEVERITIES.includes(raw.severity)) throw new ReviewInputError(`${where} has an unknown severity`);
|
|
34
|
+
if (!safeFile(raw.file)) throw new ReviewInputError(`${where} has an unusable file path`);
|
|
35
|
+
const claim = clip(raw.claim);
|
|
36
|
+
if (!isText(claim, 400)) throw new ReviewInputError(`${where} needs a claim`);
|
|
37
|
+
const evidence = clip(raw.evidence);
|
|
38
|
+
if (present(evidence) && !isText(evidence, 400)) throw new ReviewInputError(`${where} has unusable evidence`);
|
|
39
|
+
if (present(raw.line) && !(Number.isInteger(raw.line) && raw.line > 0)) throw new ReviewInputError(`${where} has an unusable line`);
|
|
40
|
+
return { claim, evidence };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function normalizeFinding(raw, agent, i) {
|
|
44
|
+
const { claim, evidence } = checkFinding(raw, `${agent} finding ${i + 1}`);
|
|
45
|
+
const finding = {
|
|
46
|
+
id: isText(raw.id, 32) && !CONTROL.test(raw.id) ? raw.id : `${agent}-${i + 1}`,
|
|
47
|
+
agent,
|
|
48
|
+
severity: raw.severity,
|
|
49
|
+
boundary: raw.boundary === true,
|
|
50
|
+
file: raw.file,
|
|
51
|
+
line: raw.line ?? null,
|
|
52
|
+
claim,
|
|
53
|
+
evidence: evidence ?? null,
|
|
54
|
+
};
|
|
55
|
+
finding.fingerprint = isText(raw.fingerprint, 64) && /^[\w.:-]+$/.test(raw.fingerprint) ? raw.fingerprint : deriveFingerprint(finding);
|
|
56
|
+
if (REMOVES_CHECK.test(`${claim} ${evidence ?? ''}`)) finding.needs_verification = true;
|
|
57
|
+
return finding;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// The checks one review must pass, in this order. The reviewer is counted as seen before its verdict is read, so a second report from it
|
|
61
|
+
// is refused as a repeat, not read again.
|
|
62
|
+
function checkReview(review, dispatched, seen) {
|
|
63
|
+
if (review === null || typeof review !== 'object') throw new ReviewInputError('a review is not an object');
|
|
64
|
+
if (Buffer.byteLength(JSON.stringify(review)) > MAX_REVIEW_BYTES) throw new ReviewInputError(`a review is over ${MAX_REVIEW_BYTES} bytes; return fewer, shorter findings`);
|
|
65
|
+
const { agent } = review;
|
|
66
|
+
if (!dispatched.some((d) => d.agent === agent)) throw new ReviewInputError('a review names a reviewer that was not dispatched');
|
|
67
|
+
if (seen.has(agent)) throw new ReviewInputError(`${agent} returned twice`);
|
|
68
|
+
seen.add(agent);
|
|
69
|
+
if (!VERDICTS.includes(review.verdict)) throw new ReviewInputError(`${agent} has an unknown verdict`);
|
|
70
|
+
if (!Array.isArray(review.findings)) throw new ReviewInputError(`${agent} has no findings array`);
|
|
71
|
+
return agent;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// `reviews` is what the reviewers returned: untrusted model output. Every dispatched reviewer must be present, because a
|
|
75
|
+
// missing security review must not read as a clean one.
|
|
76
|
+
export function parseReviews(reviews, dispatched) {
|
|
77
|
+
if (!Array.isArray(reviews)) throw new ReviewInputError('reviews must be an array');
|
|
78
|
+
const seen = new Set();
|
|
79
|
+
const findings = [];
|
|
80
|
+
const verdicts = [];
|
|
81
|
+
for (const review of reviews) {
|
|
82
|
+
const agent = checkReview(review, dispatched, seen);
|
|
83
|
+
verdicts.push({ agent, verdict: review.verdict, model: isText(review.model, 64) && !CONTROL.test(review.model) ? review.model : null });
|
|
84
|
+
review.findings.forEach((f, i) => findings.push(normalizeFinding(f, agent, i)));
|
|
85
|
+
}
|
|
86
|
+
const missing = dispatched.filter((d) => !seen.has(d.agent)).map((d) => d.agent);
|
|
87
|
+
if (missing.length > 0) throw new ReviewInputError(`no review from: ${missing.join(', ')}`);
|
|
88
|
+
return { findings, verdicts };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// A Major re-loops only when it is confirmed on a real boundary: the reviewer cites file, line and an exploit or failing input.
|
|
92
|
+
export const isConfirmedBoundaryMajor = (f) => f.severity === 'Major' && f.boundary && f.line !== null && f.evidence !== null;
|
|
93
|
+
export const isBlocking = (f) => f.severity === 'Blocker' || isConfirmedBoundaryMajor(f);
|
|
94
|
+
|
|
95
|
+
export function decideAfterReview({ findings, pass, extraPasses = 0, previousBlocking = [] }) {
|
|
96
|
+
const blocking = findings.filter(isBlocking);
|
|
97
|
+
const prints = [...new Set(blocking.map((f) => f.fingerprint))].sort();
|
|
98
|
+
if (blocking.length === 0) return { next: 'close', blocking: [] };
|
|
99
|
+
const recurring = prints.filter((p) => previousBlocking.includes(p));
|
|
100
|
+
if (recurring.length > 0) return { next: 'ask', blocking: prints, stop: 'recurring-finding', question: 'The same blocking finding came back after a fix. Keep fixing, or close as it is?' };
|
|
101
|
+
if (pass >= MAX_PASSES + extraPasses) return { next: 'ask', blocking: prints, stop: 'max-passes', question: `${pass} review passes ran and blocking findings remain. Allow one more pass, or close as it is?` };
|
|
102
|
+
return { next: 'fix', blocking: prints };
|
|
103
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// pr.mjs: the pull request text and the gate in front of it, the same from the CLI and from the MCP tool. Building it only reads git and
|
|
2
|
+
// SoMi's own files, so it is safe outside the sandbox.
|
|
3
|
+
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import { commitSubjects, snapshotTree } from './git-tree.mjs';
|
|
6
|
+
import { readLedger } from './ledger.mjs';
|
|
7
|
+
import { readMigrated } from './migrate.mjs';
|
|
8
|
+
import { qualityStep } from './quality/run.mjs';
|
|
9
|
+
import { prGate, readReviews } from './reviews.mjs';
|
|
10
|
+
import { statePaths } from './state.mjs';
|
|
11
|
+
import { renderPr } from './views.mjs';
|
|
12
|
+
|
|
13
|
+
export const lastCheck = (root, env) => { try { return JSON.parse(fs.readFileSync(statePaths(root, env).check, 'utf8')); } catch { return null; } };
|
|
14
|
+
export const migratedFindings = (root, env) => readMigrated(root, env).flatMap((m) => m.findings ?? []);
|
|
15
|
+
|
|
16
|
+
// {ok, text} or {ok: false, why}. Going ahead unreviewed is the user's call, and the text then says so at the top. Going ahead below the
|
|
17
|
+
// quality bar is not: the limits are the user's to set in their own config, and a pull request is held to them.
|
|
18
|
+
export function pullRequest(ctx, { base, unreviewed = false } = {}) {
|
|
19
|
+
const quality = qualityStep(ctx.root, { scope: 'pr', env: ctx.env });
|
|
20
|
+
if (!quality.ok) return { ok: false, reason: 'quality', failures: quality.failures, why: `No pull request text, because this branch does not meet the quality limits:\n${quality.failures.join('\n')}\nFix these, or set the limits you want under quality in your own config, which /somi-config explains. somi_quality has the detail.` };
|
|
21
|
+
const gate = prGate(ctx, { base });
|
|
22
|
+
if (!gate.ok && !unreviewed) {
|
|
23
|
+
const why = gate.reason === 'open-blocking' ? 'blocking findings are still open on this change' : `this change needs a review and none covers it (${gate.signals.join(', ')})`;
|
|
24
|
+
return { ok: false, reason: gate.reason, why: `No pull request text, because ${why}. Call somi_review, run the reviewer it names and record what it returns. To go ahead anyway, pass unreviewed, and the text will say so at the top.` };
|
|
25
|
+
}
|
|
26
|
+
const text = renderPr({ subjects: commitSubjects(ctx.root, gate.plan.base), gate, ledger: readLedger(ctx.root, ctx.env), reviews: readReviews(ctx.root, ctx.env), check: lastCheck(ctx.root, ctx.env), tree: snapshotTree(ctx.root), migrated: migratedFindings(ctx.root, ctx.env) });
|
|
27
|
+
return { ok: true, reviewed: gate.ok, text };
|
|
28
|
+
}
|