@skathio/somi 1.1.1 → 2.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/settings.json +8 -8
- package/.claude-plugin/marketplace.json +1 -1
- package/.claude-plugin/plugin.json +1 -1
- package/.copilot-extension/extension.json +11 -2
- package/.copilot-extension/marketplace.json +1 -1
- package/README.md +21 -4
- package/VERSION +1 -1
- package/agents/coder.md +8 -3
- package/agents/designer.md +22 -7
- package/agents/discovery-analyst.md +11 -0
- package/agents/planner.md +69 -9
- package/agents/refactorer.md +3 -1
- package/agents/reviewer.md +4 -0
- package/commands/adopt.md +77 -0
- package/commands/atlas.md +83 -0
- package/commands/code-loop.md +70 -44
- package/commands/code-parallel.md +3 -1
- package/commands/code.md +8 -2
- package/commands/debug.md +136 -0
- package/commands/design.md +17 -7
- package/commands/discover.md +23 -18
- package/commands/impact.md +74 -0
- package/commands/incident.md +83 -0
- package/commands/plan-loop.md +44 -19
- package/commands/plan.md +38 -26
- package/commands/pr.md +82 -0
- package/commands/release-readiness.md +70 -0
- package/commands/review-panel.md +5 -0
- package/commands/review.md +19 -0
- package/commands/ship-loop.md +18 -10
- package/commands/ship.md +4 -2
- package/commands/somi.md +85 -0
- package/commands/upgrade.md +81 -0
- package/hooks/hooks.json +8 -8
- package/hooks/lib/common.mjs +328 -0
- package/hooks/post-tool/audit-log.mjs +93 -0
- package/hooks/post-tool/lint-changed-files.mjs +212 -0
- package/hooks/pre-tool/block-dangerous-bash.mjs +382 -0
- package/hooks/pre-tool/block-secret-writes.mjs +96 -0
- package/hooks/pre-tool/gate-dep-install.mjs +229 -0
- package/hooks/pre-tool/guard-protected-paths.mjs +157 -0
- package/hooks/session-start/detect-repo-instructions.mjs +264 -0
- package/hooks/user-prompt-submit/inject-workflow-context.mjs +432 -0
- package/package.json +6 -1
- package/rules/50-collaboration.md +4 -1
- package/scripts/somi-check.mjs +243 -0
- package/scripts/somi-findings.mjs +307 -0
- package/scripts/somi-loop.mjs +354 -0
- package/scripts/validate.sh +120 -0
- package/skills/clean-code/SKILL.md +1 -1
- package/templates/ATLAS.md.tmpl +75 -0
- package/templates/BRIEF.md.tmpl +20 -4
- package/templates/DOD.md.tmpl +2 -2
- package/templates/PHASE.md.tmpl +1 -1
- package/templates/RCA.md.tmpl +53 -0
- package/templates/RD-README.md.tmpl +2 -2
- package/templates/SDD.md.tmpl +4 -4
- package/templates/SOMI-README.md.tmpl +20 -3
- package/templates/SRS.md.tmpl +1 -1
- package/templates/TDD.md.tmpl +5 -5
- package/tests/hooks/cases/audit-log.json +21 -0
- package/tests/hooks/cases/block-dangerous-bash.json +166 -0
- package/tests/hooks/cases/block-secret-writes.json +22 -0
- package/tests/hooks/cases/detect-repo-instructions.json +84 -0
- package/tests/hooks/cases/gate-dep-install.json +42 -0
- package/tests/hooks/cases/guard-protected-paths.json +30 -0
- package/tests/hooks/cases/inject-workflow-context.json +126 -0
- package/tests/hooks/cases/lint-changed-files.json +120 -0
- package/tests/hooks/run.sh +362 -0
- package/tests/scripts/goldens/somi-findings.json +336 -0
- package/tests/scripts/goldens/somi-loop.json +168 -0
- package/tests/scripts/run.sh +389 -0
- package/hooks/lib/common.sh +0 -120
- package/hooks/post-tool/audit-log.sh +0 -40
- package/hooks/post-tool/lint-changed-files.sh +0 -80
- package/hooks/pre-tool/block-dangerous-bash.sh +0 -83
- package/hooks/pre-tool/block-secret-writes.sh +0 -83
- package/hooks/pre-tool/gate-dep-install.sh +0 -72
- package/hooks/pre-tool/guard-protected-paths.sh +0 -74
- package/hooks/session-start/detect-repo-instructions.sh +0 -73
- package/hooks/user-prompt-submit/inject-workflow-context.sh +0 -143
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
{
|
|
2
|
+
"_note": "Phase 0 iteration 0.5 — the brief's named heaviest procedural-bash hook, zero fixtures before this iteration. Every case's expected output was captured by running the unmodified bash hook directly against a staged throwaway CLAUDE_PROJECT_DIR before being written here, never assumed (see the runner mechanisms added alongside this file: \"setup_git\"/\"dirty_files\" for git-backed nudge cases, \"prime\"/\"files_after_prime\" for the signature-gate cases, and \"expect_context_excludes\" — a new, symmetric-to-\"expect_context\" assertion — for the one discovery below that a positive substring match cannot rule out). SURPRISING DISCOVERY (parallel to 0.4's non-functional prune list; pinned as current behavior per D2, not corrected here): the hook's own in-progress detection, hooks/user-prompt-submit/inject-workflow-context.sh:59-60, only recognizes a bare status line matching `^[[:space:]]*<?in-progress>?[[:space:]]*$` (optional angle brackets, NOT backticks) or a line matching `status:[[:space:]]*`?in-progress`?` (requires a literal \"status:\" prefix). This project's OWN progress.md convention — a bare backtick-wrapped line, e.g. this very work item's own .somi/plans/node-runtime-port/progress.md line 10, `` `in-progress` `` with no \"Status:\" prefix — matches NEITHER pattern, confirmed directly with grep -qiE against both regexes. So this hook, as currently written, would silently fail to surface node-runtime-port itself (or any other work item that follows this repo's own progress.md style) as an active work item on a fresh Claude Code turn. Meanwhile the R&D README.md convention (`> Status: `ready-for-planning`` — see examples/discovery-example.md:22) DOES match its sibling regex, because it already carries the \"Status:\" prefix the plans-detection regex additionally requires. This asymmetry is real, reproducible, and worth a naming call, not a silent default — filed below and in progress.md's Follow-ups as a decision for Phase 2 iteration 2.8 (the Node port of this hook) to make explicitly: normalize this repo's own progress.md convention to match the regex, or normalize the port's detection to match the convention actually in use. Note on the \"files_after_prime\" perturbation used in the signature-gate cases: it stages a brand-new .somi/reviews/*.md path (or a new file entirely) rather than touching an existing file's mtime, so the signature change is exact-listing-driven, not timing/mtime-resolution-dependent — deliberately avoids a flaky sleep-then-touch pattern. Note on ordering: the multi-in-progress case does not assert the specific slug list text, only the static surrounding phrase — verified directly that the slug order in that message follows `find`'s raw (unsorted) directory-traversal order, which is not stable across differently-created directories, so asserting an exact order would be a false, environment-fragile precision this fixture avoids. RESOLVED at Phase 2 iteration 2.8 (the Node port): chose to WIDEN the bare-line regex to accept backtick wrapping (full-line anchor preserved; empirically verified zero false positives against this repo's own progress.md narrative text) rather than reproduce the narrow bash detection for parity — see inject-workflow-context.mjs's header for the full reasoning and decisions.md's new D-entry. The `plan-hint-bare-backtick-status-not-detected` case below is renamed to `plan-hint-bare-backtick-status-now-detected` and its expectation flipped accordingly; every other case is unaffected (the R&D status regexes and the \"status:\"-prefixed in-progress regex had no discovered gap and are reproduced unchanged).",
|
|
3
|
+
"script": "user-prompt-submit/inject-workflow-context.mjs",
|
|
4
|
+
"cases": [
|
|
5
|
+
{
|
|
6
|
+
"name": "emits-reminder-on-first-turn-absent-signature",
|
|
7
|
+
"_note": "No .somi/plans, .somi/reviews, .somi/rd, or git repo staged at all — a genuinely fresh project. No prior state file exists in this case's throwaway CLAUDE_PROJECT_DIR/.claude/somi-state/, so LAST_SIG is empty and EMIT_REMINDER is forced to \"yes\" regardless of content. This is the baseline 'signature absent -> emission happens' proof the phase spec asks for, and the pairing partner for the two signature-gate cases below (same empty staging, but those add \"prime\").",
|
|
8
|
+
"expect_context": "somi is active. Reminders:\n- Follow rules/CLAUDE.md priorities: security > correctness > maintainability > performance > convenience.",
|
|
9
|
+
"expect_context_excludes": "loose-end check",
|
|
10
|
+
"payload": {}
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"name": "signature-gate-suppresses-unchanged-repeat",
|
|
14
|
+
"_note": "Same (empty) staging as the case above, plus \"prime\": true — the runner invokes the hook once first (writing .claude/somi-state/last-context-signature naturally, via the hook's own code, not a hand-computed hash), discards that output, then runs again for the real assertion. Nothing changes between the two invocations, so the second call's computed signature equals what the first call just persisted -> EMIT_REMINDER=\"no\" -> the PARTS array stays empty (no nudges either, no git repo staged) -> no hookSpecificOutput at all. This is the core 'no re-emission when mtimes are unchanged since the last emission' proof the phase spec requires.",
|
|
15
|
+
"expect_no_context": true,
|
|
16
|
+
"prime": true,
|
|
17
|
+
"payload": {}
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"name": "signature-gate-reemits-when-state-changes",
|
|
21
|
+
"_note": "The required companion to the suppression case above, proving it is not vacuously green: identical empty baseline and identical \"prime\": true, but \"files_after_prime\" stages a brand-new .somi/reviews/probe/note.md between the priming call and the real one. The reviews_state segment of the hook's signature (find .somi/reviews -maxdepth 3 -name '*.md' | sort | sha256sum) now differs from what priming persisted, so the real call's LAST_SIG != CURRENT_SIG and the reminder is emitted again — proving the suppression case above is actually gating on the signature, not on some unrelated always-suppress condition.",
|
|
22
|
+
"expect_context": "somi is active. Reminders:",
|
|
23
|
+
"prime": true,
|
|
24
|
+
"files_after_prime": {
|
|
25
|
+
".somi/reviews/probe/note.md": "throwaway review note used only to change the reviews_state hash\n"
|
|
26
|
+
},
|
|
27
|
+
"payload": {}
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"name": "plan-hint-single-in-progress-surfaced",
|
|
31
|
+
"_note": "PLAN_HINT's singular branch: exactly one .somi/plans/*/progress.md matching the \"status:\" + in-progress regex. First turn (absent signature) so the reminder block (and therefore PLAN_HINT, embedded inside it) is guaranteed to render.",
|
|
32
|
+
"expect_context": "Active work item: `.somi/plans/demo-active/`. Follow its `spec.md` and active iteration in `phases/`; update `progress.md` / `diary.md` as work proceeds.",
|
|
33
|
+
"expect_context_excludes": "Multiple in-progress work items",
|
|
34
|
+
"files": {
|
|
35
|
+
".somi/plans/demo-active/progress.md": "## Status\n\nStatus: `in-progress`\n"
|
|
36
|
+
},
|
|
37
|
+
"payload": {}
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"name": "plan-hint-bare-backtick-status-now-detected",
|
|
41
|
+
"_note": "RENAMED + FLIPPED at Phase 2 iteration 2.8 (the named, reviewed edit per THE STATUS-DETECTION DECISION in inject-workflow-context.mjs's header). Previously named plan-hint-bare-backtick-status-not-detected, pinning the bash gap as current behavior: this project's OWN progress.md convention (a bare backtick-wrapped status line, no \"status:\" prefix — the exact shape templates/PROGRESS.md.tmpl and this very work item's own progress.md line 10 both use) matched neither of bash's two in-progress regexes. 2.8 chose to WIDEN the Node port's bare-line regex to accept backtick wrapping alongside the original's angle-bracket wrapping (full-line anchor preserved exactly — verified this doesn't false-positive against this repo's own ~1100-line progress.md, which repeats the substring \"in-progress\" inside narrative prose dozens of times but has exactly one line matching the full-line-anchored pattern: the real Status field). So the .mjs port now DOES detect this case's staged content as in-progress, a deliberate, consumer-observable divergence from bash (bash still stays silent on this same input — expected and intentional, not a regression). Sanity-checked the flip is non-vacuous: reverted the regex to the narrow bash-parity form locally, confirmed this case fails loudly (expect_context substring missing), restored — see diary.",
|
|
42
|
+
"expect_context": "Active work item: `.somi/plans/demo-slug/`. Follow its `spec.md` and active iteration in `phases/`; update `progress.md` / `diary.md` as work proceeds.",
|
|
43
|
+
"files": {
|
|
44
|
+
".somi/plans/demo-slug/progress.md": "## Status\n\n`in-progress`\n\nSome other text.\n"
|
|
45
|
+
},
|
|
46
|
+
"payload": {}
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"name": "plan-hint-multiple-in-progress",
|
|
50
|
+
"_note": "PLAN_HINT's plural branch: two .somi/plans/*/progress.md files both matching the in-progress regex. Asserts only the static surrounding phrase, not the specific slug list or its order — verified directly that the comma-joined slug order follows find's raw (unsorted) traversal order, which differs run to run across differently-created directories, so pinning an exact order here would be an environment-fragile false precision.",
|
|
51
|
+
"expect_context": "Multiple in-progress work items in `.somi/plans/`:",
|
|
52
|
+
"expect_context_excludes": "Active work item:",
|
|
53
|
+
"files": {
|
|
54
|
+
".somi/plans/alpha-work/progress.md": "Status: `in-progress`\n",
|
|
55
|
+
".somi/plans/beta-work/progress.md": "Status: `in-progress`\n"
|
|
56
|
+
},
|
|
57
|
+
"payload": {}
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"name": "rd-hint-active-discovery-surfaced",
|
|
61
|
+
"_note": "RD_HINT's active branch (status researching/drafting/awaiting-verification). Unlike the plans convention above, the real R&D README.md convention (`> Status: `researching``, matching examples/discovery-example.md's own style) already carries the \"status:\" prefix the regex requires, so this branch fires exactly as documented — no discovered gap here.",
|
|
62
|
+
"expect_context": "Active discovery in `.somi/rd/`: widget-research. This is requirements-engineering work (`/discover`): research the competition, author the R&D doc set, verify crossroads, before handing to `/plan`.",
|
|
63
|
+
"expect_context_excludes": "R&D foundation ready in",
|
|
64
|
+
"files": {
|
|
65
|
+
".somi/rd/widget-research/README.md": "> Status: `researching`\n"
|
|
66
|
+
},
|
|
67
|
+
"payload": {}
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"name": "rd-hint-ready-for-planning-surfaced",
|
|
71
|
+
"_note": "RD_HINT's ready-for-planning branch — only reachable when zero active-status R&D directories exist (see the precedence case below).",
|
|
72
|
+
"expect_context": "R&D foundation ready in `.somi/rd/widget-ready/`. `/plan widget-ready` will consume its SRS/FRD as the requirements source and SDD/TDD as architectural direction.",
|
|
73
|
+
"expect_context_excludes": "Active discovery in",
|
|
74
|
+
"files": {
|
|
75
|
+
".somi/rd/widget-ready/README.md": "> Status: `ready-for-planning`\n"
|
|
76
|
+
},
|
|
77
|
+
"payload": {}
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
"name": "rd-hint-active-overrides-ready-when-both-present",
|
|
81
|
+
"_note": "Pins the precedence branch directly: the hook's RD_HINT logic is `if active>=1 ... elif ready==1`, so a ready-for-planning directory is silently ignored whenever any active-status directory exists, even though both were scanned. Distinguished from rd-hint-active-discovery-surfaced (which has no ready dir at all) by staging both simultaneously and asserting the ready branch's distinct text is absent.",
|
|
82
|
+
"expect_context": "Active discovery in `.somi/rd/`: widget-active.",
|
|
83
|
+
"expect_context_excludes": "R&D foundation ready in",
|
|
84
|
+
"files": {
|
|
85
|
+
".somi/rd/widget-active/README.md": "> Status: `drafting`\n",
|
|
86
|
+
".somi/rd/widget-ready2/README.md": "> Status: `ready-for-planning`\n"
|
|
87
|
+
},
|
|
88
|
+
"payload": {}
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
"name": "todo-claude-marker-in-diff-nudge",
|
|
92
|
+
"_note": "The hook checks `git -C PROJECT_ROOT diff --no-color --unified=0 HEAD | grep '^\\+.*(TODO(claude)|TODO(agent)|FIXME(claude))'` — an added line containing the marker, relative to HEAD, regardless of whether it's staged in the index. \"setup_git\" commits the baseline src/app.js untouched; \"dirty_files\" then overwrites it post-commit (left unstaged on purpose, proving the check is not staged-only) with a TODO(claude) line added.",
|
|
93
|
+
"expect_context": "Detected new TODO(claude)/FIXME(claude) markers in the diff against HEAD. List them explicitly as 'not done' in your next summary.",
|
|
94
|
+
"setup_git": true,
|
|
95
|
+
"files": {
|
|
96
|
+
"src/app.js": "function foo() {}\n"
|
|
97
|
+
},
|
|
98
|
+
"dirty_files": {
|
|
99
|
+
"src/app.js": "function foo() {}\n// TODO(claude): fix this\n"
|
|
100
|
+
},
|
|
101
|
+
"payload": {}
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
"name": "scratch-file-in-status-nudge",
|
|
105
|
+
"_note": "The hook checks `git status --porcelain | grep '^\\?\\? .*(\\.bak|\\.orig|scratch_)'`. \"dirty_files\" here lands a brand-new, never-added path after the baseline commit, so it's untracked (`?? scratch_debug.txt` in porcelain output) — exactly the shape the regex is looking for.",
|
|
106
|
+
"expect_context": "Detected scratch / .bak files in working tree. Clean them up before declaring done.",
|
|
107
|
+
"setup_git": true,
|
|
108
|
+
"dirty_files": {
|
|
109
|
+
"scratch_debug.txt": "leftover debug output\n"
|
|
110
|
+
},
|
|
111
|
+
"payload": {}
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
"name": "nudge-fires-independent-of-suppressed-reminder",
|
|
115
|
+
"_note": "Combines the signature-gate mechanism with a nudge to pin a subtle, easy-to-regress interaction: NUDGES are computed unconditionally every call, while the reminder block (with PLAN_HINT/RD_HINT inside it) is gated on EMIT_REMINDER alone — the two are independent branches in the same PARTS array, not one gated by the other. \"prime\" establishes a git repo with no loose ends yet (first call: absent signature, emits the plain reminder, discarded); \"files_after_prime\" then lands an untracked scratch file with the signature-relevant state (.somi/plans|reviews|rd) completely unchanged, so the real call's reminder is suppressed exactly as in signature-gate-suppresses-unchanged-repeat, yet the loose-end nudge still fires and is the ONLY thing in the output — a naive port that nested the nudge check inside the same `if EMIT_REMINDER` branch as the reminder would silently drop end-of-turn nudges on every suppressed turn, a real regression this case is designed to catch.",
|
|
116
|
+
"expect_context": "Detected scratch / .bak files in working tree.",
|
|
117
|
+
"expect_context_excludes": "somi is active. Reminders:",
|
|
118
|
+
"setup_git": true,
|
|
119
|
+
"prime": true,
|
|
120
|
+
"files_after_prime": {
|
|
121
|
+
"scratch_debug.txt": "leftover debug output\n"
|
|
122
|
+
},
|
|
123
|
+
"payload": {}
|
|
124
|
+
}
|
|
125
|
+
]
|
|
126
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
{
|
|
2
|
+
"_note": "lint-changed-files hook is PostToolUse (matcher: Write|Edit), best-effort/informational \u2014 never blocks. Retargeted to the .mjs port in Phase 2 iteration 2.5. Each case's throwaway case_dir (used as CLAUDE_PROJECT_DIR) is guaranteed fresh, so it never contains pyproject.toml/.ruff.toml/an eslintrc/go.mod/Cargo.toml regardless of host tooling \u2014 the .py/.ts(no-config)/.go/.rs no-op cases below are therefore deterministic in any environment, not just this one. LINTER-AVAILABILITY FINDING (2.5, re-checked, not inherited from 0.3): ruff/eslint/go/cargo/shellcheck/pyright are all still confirmed absent via `command -v` in this authoring sandbox, AND absent in CI (.github/workflows/ci.yml's reusable job only installs node; only publish.yml's release-dispatch job apt-get-installs shellcheck \u2014 ci.yml's actual PR/push gate, the one this fixture corpus really needs to hold under, does NOT, though GitHub's ubuntu-latest hosted runner image typically ships shellcheck preinstalled regardless). npx (bundled with npm, which ships with the official Node distributions, actions/setup-node, and nvm \u2014 NOT guaranteed on split-packaged hosts like Debian's nodejs-without-npm apt package, where this one case would fail) is present everywhere this repo's CI and dev environments actually run Node \u2014 used below for a genuine, non-fabricated positive emit-path case (F-13, from 0.3's review): the eslint branch's `command -v` gate checks 'npx', not 'eslint' (bash: `run_if_present npx --no-install eslint --no-color \"$PATH_INPUT\"`), and no eslint package is installed anywhere in this repo's tree (checked: no root .eslintrc*/eslint.config.*, no eslint in package.json) or on any CI runner image by default \u2014 so `npx --no-install eslint ...` deterministically fails to resolve eslint and surfaces an `npm error npx canceled ...` diagnostic on stderr, EVERY time, in every environment this repo's CI or dev sandboxes actually run in. This is the real bash-original code path being exercised for real, not a stand-in. FAKE-LINTER-VIA-PATH MECHANISM CONSIDERED AND DECLINED for the .sh/shellcheck case below: tests/hooks/run.sh's per-case \"env\" field (the only case-level PATH-influencing mechanism the runner exposes) passes each `key=value` straight through to the `env` command via a `jq -r` read with NO shell variable expansion \u2014 a value like \"$case_dir/bin:$PATH\" is passed as that literal 20-character string, not interpolated, and case_dir is a `mktemp -d` path unknown until runtime, so it can't be baked into the static JSON either. There is no way, with the runner's CURRENT field semantics, to prepend a fake-binary directory onto PATH while still preserving whatever PATH entry the host actually needs to resolve `node` itself (verified this would break: this authoring sandbox's `node` lives under an nvm path, not any hardcodable /usr/bin-style default) \u2014 so a case authored to rely on that mechanism would not actually run, on this host or any other. Not fabricated; instead the .sh gap (previously zero coverage of any kind, per 0.3's deferral note) is filled with 0.3's other explicitly pre-approved option: a shellcheck-CLEAN sample script, staged as a deterministic no-op that holds identically whether shellcheck is real (clean input -> empty stdout, exit 0) or absent (`command -v` gate no-ops) \u2014 verified against the actual hook, both branches converge on `expect_no_context`. SHELLCHECK-CLEAN CLAIM SCOPE (2.5 review): neither the authoring sandbox nor the review environment had a shellcheck binary, so sample.sh's cleanliness is verified by inspection only (trivially clean POSIX: quoted echo, valid shebang, no expansions) \u2014 on CI hosts that ship shellcheck (GitHub ubuntu-latest does) the case runs it for real and the no-op determinism rests on that inspection; if shellcheck ever flags it, fix the SAMPLE, not the assertion.",
|
|
3
|
+
"script": "post-tool/lint-changed-files.mjs",
|
|
4
|
+
"cases": [
|
|
5
|
+
{
|
|
6
|
+
"name": "no-file-path-noop",
|
|
7
|
+
"expect_no_context": true,
|
|
8
|
+
"payload": {
|
|
9
|
+
"tool_name": "Write",
|
|
10
|
+
"tool_input": {}
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"name": "file-not-found-noop",
|
|
15
|
+
"expect_no_context": true,
|
|
16
|
+
"payload": {
|
|
17
|
+
"tool_name": "Write",
|
|
18
|
+
"tool_input": {
|
|
19
|
+
"file_path": "{{CASE_DIR}}/does-not-exist.py"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"name": "python-no-linter-config-noop",
|
|
25
|
+
"expect_no_context": true,
|
|
26
|
+
"files": {
|
|
27
|
+
"sample.py": "x = 1\n"
|
|
28
|
+
},
|
|
29
|
+
"payload": {
|
|
30
|
+
"tool_name": "Write",
|
|
31
|
+
"tool_input": {
|
|
32
|
+
"file_path": "{{CASE_DIR}}/sample.py"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
"name": "typescript-no-eslint-config-noop",
|
|
38
|
+
"expect_no_context": true,
|
|
39
|
+
"files": {
|
|
40
|
+
"sample.ts": "const x = 1;\n"
|
|
41
|
+
},
|
|
42
|
+
"payload": {
|
|
43
|
+
"tool_name": "Write",
|
|
44
|
+
"tool_input": {
|
|
45
|
+
"file_path": "{{CASE_DIR}}/sample.ts"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"name": "go-no-gomod-noop",
|
|
51
|
+
"expect_no_context": true,
|
|
52
|
+
"files": {
|
|
53
|
+
"sample.go": "package main\n"
|
|
54
|
+
},
|
|
55
|
+
"payload": {
|
|
56
|
+
"tool_name": "Write",
|
|
57
|
+
"tool_input": {
|
|
58
|
+
"file_path": "{{CASE_DIR}}/sample.go"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"name": "rust-no-cargotoml-noop",
|
|
64
|
+
"expect_no_context": true,
|
|
65
|
+
"files": {
|
|
66
|
+
"sample.rs": "fn main() {}\n"
|
|
67
|
+
},
|
|
68
|
+
"payload": {
|
|
69
|
+
"tool_name": "Write",
|
|
70
|
+
"tool_input": {
|
|
71
|
+
"file_path": "{{CASE_DIR}}/sample.rs"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"name": "unhandled-extension-noop",
|
|
77
|
+
"expect_no_context": true,
|
|
78
|
+
"files": {
|
|
79
|
+
"sample.md": "# hi\n"
|
|
80
|
+
},
|
|
81
|
+
"payload": {
|
|
82
|
+
"tool_name": "Write",
|
|
83
|
+
"tool_input": {
|
|
84
|
+
"file_path": "{{CASE_DIR}}/sample.md"
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"name": "typescript-eslint-config-present-delegate-emits-context",
|
|
90
|
+
"_note": "Positive emit-path case (F-13, mandatory per this iteration): a real .eslintrc.json config gates the eslint branch open; the branch's command-presence gate checks 'npx' (bundled with Node, always present here), not 'eslint' (never installed in this repo). `npx --no-install eslint ...` therefore always fails to resolve eslint and surfaces a real npm/npx diagnostic on stderr \u2014 a genuine subprocess run through the real capture/wrap/emit path, not a stand-in. sample.ts is also deliberately invalid syntax so that IF a real eslint were ever reachable in some other environment, a parse error would still guarantee non-empty lint output (deterministic either way, not just deterministic in THIS sandbox's specific absence of eslint).",
|
|
91
|
+
"expect": "allow",
|
|
92
|
+
"expect_context": "Lint output for the file just changed:",
|
|
93
|
+
"files": {
|
|
94
|
+
".eslintrc.json": "{}\n",
|
|
95
|
+
"sample.ts": "const x = ;\n"
|
|
96
|
+
},
|
|
97
|
+
"payload": {
|
|
98
|
+
"tool_name": "Write",
|
|
99
|
+
"tool_input": {
|
|
100
|
+
"file_path": "{{CASE_DIR}}/sample.ts"
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
"name": "shell-shellcheck-clean-noop",
|
|
106
|
+
"_note": "Deferred .sh/shellcheck case (from 0.3's review follow-up). The shellcheck branch has no config-file gate (command-presence only), so unlike the other extensions a 'clean case_dir' alone doesn't guarantee determinism \u2014 it depends on whether shellcheck itself is reachable, which differs between this sandbox (absent) and some CI hosts (may be preinstalled). Staged sample.sh is deliberately shellcheck-clean (correct shebang, no unquoted expansions, no deprecated constructs) so this case holds identically under EITHER a real, clean-passing shellcheck (exit 0, empty stdout -> no context) OR no shellcheck at all (command-presence gate no-ops -> no context) \u2014 see the file-level _note for why a fake-binary-via-PATH mechanism was considered and declined instead of this approach.",
|
|
107
|
+
"expect": "allow",
|
|
108
|
+
"expect_no_context": true,
|
|
109
|
+
"files": {
|
|
110
|
+
"sample.sh": "#!/bin/sh\necho \"example\"\n"
|
|
111
|
+
},
|
|
112
|
+
"payload": {
|
|
113
|
+
"tool_name": "Write",
|
|
114
|
+
"tool_input": {
|
|
115
|
+
"file_path": "{{CASE_DIR}}/sample.sh"
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
]
|
|
120
|
+
}
|
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Behavioral fixture runner for the deterministic guardrail hooks.
|
|
3
|
+
#
|
|
4
|
+
# Each file in tests/hooks/cases/<name>.json declares the hook script it
|
|
5
|
+
# exercises plus a list of cases:
|
|
6
|
+
#
|
|
7
|
+
# {
|
|
8
|
+
# "script": "pre-tool/block-dangerous-bash.sh",
|
|
9
|
+
# "cases": [
|
|
10
|
+
# { "name": "rm-rf-root", "expect": "deny",
|
|
11
|
+
# "expect_reason": "rm -rf /",
|
|
12
|
+
# "payload": { "tool_name": "Bash", "tool_input": { "command": "rm -rf /" } } },
|
|
13
|
+
# { "name": "opted-in", "expect": "allow",
|
|
14
|
+
# "env": { "SOMI_ALLOW_DEP_INSTALL": "1" },
|
|
15
|
+
# "payload": { ... } },
|
|
16
|
+
# { "name": "config-allowlisted", "expect": "allow",
|
|
17
|
+
# "config": { "dep_install": { "allow": ["@types/"] } },
|
|
18
|
+
# "payload": { ... } },
|
|
19
|
+
# { "name": "surfaces-context", "expect_context": "somi repo-awareness",
|
|
20
|
+
# "files": { "AGENTS.md": "# repo instructions\n" },
|
|
21
|
+
# "payload": { ... } }
|
|
22
|
+
# ]
|
|
23
|
+
# }
|
|
24
|
+
#
|
|
25
|
+
# Case fields beyond "expect" (PreToolUse allow/deny):
|
|
26
|
+
# - "files": an object mapping a path (relative to the case's throwaway
|
|
27
|
+
# CLAUDE_PROJECT_DIR) to file content, written before the hook runs — for
|
|
28
|
+
# hooks that read repo-local files (e.g. detect-repo-instructions.sh's
|
|
29
|
+
# AGENTS.md/CLAUDE.md scan). Mirrors the "config" → .somi/config.json
|
|
30
|
+
# staging below, generalized to arbitrary relative paths. Keys are
|
|
31
|
+
# rejected (case FAILS loudly) if they contain a ".." path segment or a
|
|
32
|
+
# leading "/" — such a key would otherwise escape the case dir.
|
|
33
|
+
# - "setup_git": true — initializes a real git repo in the case dir (`git
|
|
34
|
+
# init -b main`, a throwaway commit identity, `git add -A` + an
|
|
35
|
+
# `--allow-empty` commit of whatever "files" staged) before the hook
|
|
36
|
+
# runs — for hooks that shell out to `git diff`/`git status` (e.g.
|
|
37
|
+
# inject-workflow-context.sh's loose-end nudges). The commit gives those
|
|
38
|
+
# hooks a real HEAD to diff against even when "files" staged nothing.
|
|
39
|
+
# - "dirty_files": like "files", but written *after* the "setup_git"
|
|
40
|
+
# baseline commit — so a new path lands untracked (visible in `git status
|
|
41
|
+
# --porcelain` as `??`) and an overwritten already-committed path lands
|
|
42
|
+
# modified-unstaged (visible in `git diff HEAD`). Same unsafe-key guard as
|
|
43
|
+
# "files". Meaningless without "setup_git" (no git repo to be dirty in).
|
|
44
|
+
# - "prime": true — runs the hook once beforehand (same sanitized env and
|
|
45
|
+
# payload as the real run below), discarding its output but still failing
|
|
46
|
+
# the case loudly if that priming invocation errors, so a hook whose
|
|
47
|
+
# behavior depends on its own prior invocation (e.g.
|
|
48
|
+
# inject-workflow-context.sh's signature-gated re-emission, which reads a
|
|
49
|
+
# state file the hook itself wrote last time) can be tested against its
|
|
50
|
+
# *second* call rather than its first. Note: this reuses the case's audit
|
|
51
|
+
# log path, so pairing "prime" with an audit-log assertion on a hook that
|
|
52
|
+
# writes audit lines would double-count; harmless for hooks (like
|
|
53
|
+
# inject-workflow-context.sh) that never call somi::audit.
|
|
54
|
+
# - "files_after_prime": like "files", but written after the priming
|
|
55
|
+
# invocation and before the real one — for perturbing whatever state the
|
|
56
|
+
# prior invocation just persisted (e.g. adding a new
|
|
57
|
+
# .somi/reviews/*/*.md so a signature computed from directory-listing
|
|
58
|
+
# mtimes changes deterministically, with no sleep/timing dependency,
|
|
59
|
+
# rather than relying on touching an existing file's mtime). Only
|
|
60
|
+
# meaningful alongside "prime".
|
|
61
|
+
# - A payload may reference the literal token "{{CASE_DIR}}", substituted
|
|
62
|
+
# with the case's absolute throwaway-dir path before the hook runs — for
|
|
63
|
+
# hooks that check real absolute paths (e.g. lint-changed-files.sh's
|
|
64
|
+
# `[[ -f "$PATH_INPUT" ]]`), so a case can point at a file staged via
|
|
65
|
+
# "files" without hardcoding a path the hook's own CWD wouldn't resolve.
|
|
66
|
+
# - "expect_context": asserted as a substring of
|
|
67
|
+
# `.hookSpecificOutput.additionalContext` — for PostToolUse/UserPromptSubmit/
|
|
68
|
+
# SessionStart hooks that surface context instead of a permission decision.
|
|
69
|
+
# Independent of "expect"; a case may declare either, both, or (paired with
|
|
70
|
+
# "expect": "deny") "expect_reason" as well. There is deliberately no
|
|
71
|
+
# Stop-event (`decision`-shape) assertion path — no Stop hooks are wired in
|
|
72
|
+
# hooks.json today, so that shape would be untested speculative scope.
|
|
73
|
+
# - "expect_no_context": true — asserts `.hookSpecificOutput.additionalContext`
|
|
74
|
+
# is absent/empty — for PostToolUse hooks whose current behavior is a
|
|
75
|
+
# documented no-op (e.g. lint-changed-files.sh with no configured linter).
|
|
76
|
+
# - "expect_context_excludes": asserted as a substring that must NOT appear
|
|
77
|
+
# in `.hookSpecificOutput.additionalContext` — the negative complement to
|
|
78
|
+
# "expect_context", for a hook that emits *some* context (so
|
|
79
|
+
# "expect_no_context" doesn't apply — a plain substring match on the part
|
|
80
|
+
# that IS present can't rule out something else silently being appended
|
|
81
|
+
# alongside it) but where one specific piece of content must be absent
|
|
82
|
+
# (e.g. inject-workflow-context.sh still prints its standing reminder
|
|
83
|
+
# block on an unrelated branch, but a particular work-item hint inside
|
|
84
|
+
# that block must not appear for this case's staged input).
|
|
85
|
+
# - "expect_reason": asserted as a substring of
|
|
86
|
+
# `.hookSpecificOutput.permissionDecisionReason` on deny cases — catches a
|
|
87
|
+
# hook that still denies (so `expect: deny` alone would pass) but whose
|
|
88
|
+
# matched/interpolated text drifted, e.g. an ERE→RegExp port that shifts
|
|
89
|
+
# *which* substring a pattern captures.
|
|
90
|
+
# - "expect_audit_log": asserted as a substring of the case's audit-log file
|
|
91
|
+
# (see below) — for hooks whose only observable effect is the audit-log
|
|
92
|
+
# side-effect (audit-log.sh emits no stdout decision at all).
|
|
93
|
+
# - "expect_no_audit_log": true — asserts the case's audit-log file was never
|
|
94
|
+
# created (e.g. audit-log.sh's no-tool_name early exit).
|
|
95
|
+
#
|
|
96
|
+
# A case that declares none of the seven recognized assertion fields above
|
|
97
|
+
# runs the hook and passes vacuously (a typo'd field name would silently
|
|
98
|
+
# produce a meaningless green case) — the runner fails such a case loudly
|
|
99
|
+
# instead.
|
|
100
|
+
#
|
|
101
|
+
# The runner pipes each payload into the script under a sanitized environment
|
|
102
|
+
# (session opt-ins unset unless the case sets them; SOMI_AUDIT_LOG defaults to
|
|
103
|
+
# a per-case throwaway file, "$case_dir/audit.log", so audit writes never leak
|
|
104
|
+
# across cases; CLAUDE_PROJECT_DIR points at the same throwaway per-case dir,
|
|
105
|
+
# into which the optional `config` object is written as .somi/config.json)
|
|
106
|
+
# and asserts every expectation the case declares.
|
|
107
|
+
#
|
|
108
|
+
# Wired into scripts/validate.sh (npm test) — CI fails when a pattern change
|
|
109
|
+
# weakens a guarantee. If you change a hook pattern, add or update a fixture.
|
|
110
|
+
|
|
111
|
+
set -euo pipefail
|
|
112
|
+
|
|
113
|
+
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
|
|
114
|
+
CASES_DIR="$ROOT/tests/hooks/cases"
|
|
115
|
+
TMP="$(mktemp -d)"
|
|
116
|
+
trap 'rm -rf "$TMP"' EXIT
|
|
117
|
+
|
|
118
|
+
total=0
|
|
119
|
+
failures=0
|
|
120
|
+
|
|
121
|
+
# Reject "files" staging keys that could escape the per-case throwaway dir: a
|
|
122
|
+
# ".." path segment (anywhere in the path, not just a bare prefix) or a
|
|
123
|
+
# leading "/". Fixture keys are maintainer-committed, not attacker-controlled,
|
|
124
|
+
# but a typo should fail the case loudly rather than silently write outside
|
|
125
|
+
# case_dir.
|
|
126
|
+
is_unsafe_staging_key() {
|
|
127
|
+
local key="$1"
|
|
128
|
+
[[ "$key" == /* ]] && return 0
|
|
129
|
+
local part
|
|
130
|
+
local IFS='/'
|
|
131
|
+
for part in $key; do
|
|
132
|
+
[[ "$part" == ".." ]] && return 0
|
|
133
|
+
done
|
|
134
|
+
return 1
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
# Writes cases[$idx].$field (an object mapping relative path -> content) into
|
|
138
|
+
# $dir, one file per key, validating every key via is_unsafe_staging_key first
|
|
139
|
+
# (shared by "files", "dirty_files", and "files_after_prime" — same shape,
|
|
140
|
+
# different write timing). Prints a FAIL line and returns 1 on an unsafe key
|
|
141
|
+
# (caller must count a failure and skip the case); returns 0 otherwise,
|
|
142
|
+
# including when the field is absent/empty.
|
|
143
|
+
stage_case_files() {
|
|
144
|
+
local case_file="$1" idx="$2" dir="$3" field="$4" script_rel="$5" name="$6"
|
|
145
|
+
local rel_path
|
|
146
|
+
while IFS= read -r rel_path; do
|
|
147
|
+
[[ -z "$rel_path" ]] && continue
|
|
148
|
+
if is_unsafe_staging_key "$rel_path"; then
|
|
149
|
+
echo "FAIL: [$script_rel] $name — unsafe \"$field\" key escapes the case dir: $rel_path" >&2
|
|
150
|
+
return 1
|
|
151
|
+
fi
|
|
152
|
+
mkdir -p "$dir/$(dirname "$rel_path")"
|
|
153
|
+
jq -r --argjson idx "$idx" --arg key "$rel_path" ".cases[\$idx].$field[\$key]" "$case_file" \
|
|
154
|
+
> "$dir/$rel_path"
|
|
155
|
+
done < <(jq -r ".cases[$idx].$field // {} | keys[]" "$case_file")
|
|
156
|
+
return 0
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
for case_file in "$CASES_DIR"/*.json; do
|
|
160
|
+
script_rel="$(jq -r '.script' "$case_file")"
|
|
161
|
+
script="$ROOT/hooks/$script_rel"
|
|
162
|
+
# Dispatch by extension (node-runtime-port phase 2, iteration 2.1): a
|
|
163
|
+
# ".mjs" hook is invoked via `node "$script"`; a ".sh" hook stays a direct
|
|
164
|
+
# exec, as before. This lets a single hook flip to its .mjs port (by
|
|
165
|
+
# changing the fixture's "script" field) without disturbing any other
|
|
166
|
+
# fixture still pointed at ".sh" — no hook is deleted until Phase 3.
|
|
167
|
+
#
|
|
168
|
+
# The executable-bit precondition is relaxed to the ".sh" branch only: a
|
|
169
|
+
# ".mjs" file invoked via `node "$script"` doesn't need an exec bit, and
|
|
170
|
+
# the exec bit doesn't exist on Windows — the exact host this work item
|
|
171
|
+
# targets. The ".mjs" branch checks readability instead.
|
|
172
|
+
case "$script_rel" in
|
|
173
|
+
*.mjs)
|
|
174
|
+
if [[ ! -r "$script" ]]; then
|
|
175
|
+
echo "FAIL: $(basename "$case_file") — hook script missing or not readable: hooks/$script_rel" >&2
|
|
176
|
+
failures=$((failures + 1))
|
|
177
|
+
continue
|
|
178
|
+
fi
|
|
179
|
+
cmd=(node "$script")
|
|
180
|
+
;;
|
|
181
|
+
*)
|
|
182
|
+
if [[ ! -x "$script" ]]; then
|
|
183
|
+
echo "FAIL: $(basename "$case_file") — hook script missing or not executable: hooks/$script_rel" >&2
|
|
184
|
+
failures=$((failures + 1))
|
|
185
|
+
continue
|
|
186
|
+
fi
|
|
187
|
+
cmd=("$script")
|
|
188
|
+
;;
|
|
189
|
+
esac
|
|
190
|
+
|
|
191
|
+
count="$(jq '.cases | length' "$case_file")"
|
|
192
|
+
for ((i = 0; i < count; i++)); do
|
|
193
|
+
total=$((total + 1))
|
|
194
|
+
name="$(jq -r ".cases[$i].name" "$case_file")"
|
|
195
|
+
expect="$(jq -r ".cases[$i].expect // empty" "$case_file")"
|
|
196
|
+
expect_context="$(jq -r ".cases[$i].expect_context // empty" "$case_file")"
|
|
197
|
+
expect_reason="$(jq -r ".cases[$i].expect_reason // empty" "$case_file")"
|
|
198
|
+
expect_no_context="$(jq -r ".cases[$i].expect_no_context // false" "$case_file")"
|
|
199
|
+
expect_context_excludes="$(jq -r ".cases[$i].expect_context_excludes // empty" "$case_file")"
|
|
200
|
+
expect_audit_log="$(jq -r ".cases[$i].expect_audit_log // empty" "$case_file")"
|
|
201
|
+
expect_no_audit_log="$(jq -r ".cases[$i].expect_no_audit_log // false" "$case_file")"
|
|
202
|
+
has_assertion="$(jq -r ".cases[$i] | (has(\"expect\") or has(\"expect_context\") or has(\"expect_reason\") or has(\"expect_no_context\") or has(\"expect_context_excludes\") or has(\"expect_audit_log\") or has(\"expect_no_audit_log\"))" "$case_file")"
|
|
203
|
+
payload="$(jq -c ".cases[$i].payload" "$case_file")"
|
|
204
|
+
|
|
205
|
+
# Sanitized environment: session opt-ins never leak in from the caller;
|
|
206
|
+
# audit-log writes land in a per-case throwaway file, not the repo's
|
|
207
|
+
# .claude/; the project root is a per-case temp dir so .somi/config.json
|
|
208
|
+
# is case-controlled.
|
|
209
|
+
case_dir="$TMP/case-$total"
|
|
210
|
+
mkdir -p "$case_dir"
|
|
211
|
+
# {{CASE_DIR}} in the payload resolves to this case's absolute throwaway
|
|
212
|
+
# dir, for hooks that check real absolute paths (see header comment).
|
|
213
|
+
payload="${payload//\{\{CASE_DIR\}\}/$case_dir}"
|
|
214
|
+
config="$(jq -c ".cases[$i].config // empty" "$case_file")"
|
|
215
|
+
if [[ -n "$config" ]]; then
|
|
216
|
+
mkdir -p "$case_dir/.somi"
|
|
217
|
+
printf '%s' "$config" > "$case_dir/.somi/config.json"
|
|
218
|
+
fi
|
|
219
|
+
# Arbitrary file staging: a case may declare "files": { "rel/path": "content" }
|
|
220
|
+
# to be written into the case dir before the hook runs (e.g. AGENTS.md for
|
|
221
|
+
# detect-repo-instructions.sh). Generalizes the config staging above.
|
|
222
|
+
# Keys are validated (no ".." segment, no leading "/") before any write.
|
|
223
|
+
if ! stage_case_files "$case_file" "$i" "$case_dir" "files" "$script_rel" "$name"; then
|
|
224
|
+
failures=$((failures + 1))
|
|
225
|
+
continue
|
|
226
|
+
fi
|
|
227
|
+
|
|
228
|
+
# "setup_git": true — real git repo in the case dir, with whatever
|
|
229
|
+
# "files" staged committed as its baseline, so hooks that shell out to
|
|
230
|
+
# `git diff`/`git status` (e.g. inject-workflow-context.sh) have a real
|
|
231
|
+
# HEAD to compare against.
|
|
232
|
+
setup_git="$(jq -r ".cases[$i].setup_git // false" "$case_file")"
|
|
233
|
+
if [[ "$setup_git" == "true" ]]; then
|
|
234
|
+
git -C "$case_dir" init -q -b main
|
|
235
|
+
git -C "$case_dir" config user.email "somi-fixture@test.local"
|
|
236
|
+
git -C "$case_dir" config user.name "somi-fixture"
|
|
237
|
+
git -C "$case_dir" add -A
|
|
238
|
+
git -C "$case_dir" commit -q -m "fixture baseline" --allow-empty
|
|
239
|
+
fi
|
|
240
|
+
|
|
241
|
+
# "dirty_files": written after the baseline commit above, so a new path
|
|
242
|
+
# is untracked and an overwritten committed path is modified-unstaged.
|
|
243
|
+
if ! stage_case_files "$case_file" "$i" "$case_dir" "dirty_files" "$script_rel" "$name"; then
|
|
244
|
+
failures=$((failures + 1))
|
|
245
|
+
continue
|
|
246
|
+
fi
|
|
247
|
+
|
|
248
|
+
env_args=("SOMI_AUDIT_LOG=$case_dir/audit.log" "CLAUDE_PROJECT_DIR=$case_dir")
|
|
249
|
+
while IFS= read -r kv; do
|
|
250
|
+
[[ -z "$kv" ]] && continue
|
|
251
|
+
env_args+=("$kv")
|
|
252
|
+
done < <(jq -r ".cases[$i].env // {} | to_entries[] | \"\(.key)=\(.value)\"" "$case_file")
|
|
253
|
+
|
|
254
|
+
# "prime": true — run the hook once first, discarding output, so a
|
|
255
|
+
# signature/state file the hook itself writes is already in place before
|
|
256
|
+
# the real (assertable) invocation below. "files_after_prime" perturbs
|
|
257
|
+
# that state between the two invocations (see header comment).
|
|
258
|
+
prime="$(jq -r ".cases[$i].prime // false" "$case_file")"
|
|
259
|
+
if [[ "$prime" == "true" ]]; then
|
|
260
|
+
if ! printf '%s' "$payload" \
|
|
261
|
+
| env -u SOMI_ALLOW_DEP_INSTALL -u SOMI_ALLOW_LOCKFILES "${env_args[@]}" "${cmd[@]}" >/dev/null; then
|
|
262
|
+
echo "FAIL: [$script_rel] $name — priming invocation exited non-zero" >&2
|
|
263
|
+
failures=$((failures + 1))
|
|
264
|
+
continue
|
|
265
|
+
fi
|
|
266
|
+
if ! stage_case_files "$case_file" "$i" "$case_dir" "files_after_prime" "$script_rel" "$name"; then
|
|
267
|
+
failures=$((failures + 1))
|
|
268
|
+
continue
|
|
269
|
+
fi
|
|
270
|
+
fi
|
|
271
|
+
|
|
272
|
+
if ! out="$(printf '%s' "$payload" \
|
|
273
|
+
| env -u SOMI_ALLOW_DEP_INSTALL -u SOMI_ALLOW_LOCKFILES "${env_args[@]}" "${cmd[@]}")"; then
|
|
274
|
+
echo "FAIL: [$script_rel] $name — hook exited non-zero" >&2
|
|
275
|
+
failures=$((failures + 1))
|
|
276
|
+
continue
|
|
277
|
+
fi
|
|
278
|
+
|
|
279
|
+
decision="allow"
|
|
280
|
+
if [[ -n "$out" ]] \
|
|
281
|
+
&& jq -e '.hookSpecificOutput.permissionDecision == "deny"' <<<"$out" >/dev/null 2>&1; then
|
|
282
|
+
decision="deny"
|
|
283
|
+
fi
|
|
284
|
+
|
|
285
|
+
if [[ -n "$expect" ]] && [[ "$decision" != "$expect" ]]; then
|
|
286
|
+
echo "FAIL: [$script_rel] $name — expected $expect, got $decision" >&2
|
|
287
|
+
[[ -n "$out" ]] && echo " output: $out" >&2
|
|
288
|
+
failures=$((failures + 1))
|
|
289
|
+
fi
|
|
290
|
+
|
|
291
|
+
if [[ -n "$expect_reason" ]]; then
|
|
292
|
+
reason=""
|
|
293
|
+
[[ -n "$out" ]] && reason="$(jq -r '.hookSpecificOutput.permissionDecisionReason // empty' <<<"$out")"
|
|
294
|
+
if [[ "$reason" != *"$expect_reason"* ]]; then
|
|
295
|
+
echo "FAIL: [$script_rel] $name — permissionDecisionReason did not contain expected substring" >&2
|
|
296
|
+
echo " expected substring: $expect_reason" >&2
|
|
297
|
+
echo " actual reason: $reason" >&2
|
|
298
|
+
failures=$((failures + 1))
|
|
299
|
+
fi
|
|
300
|
+
fi
|
|
301
|
+
|
|
302
|
+
if [[ -n "$expect_context" ]]; then
|
|
303
|
+
context=""
|
|
304
|
+
[[ -n "$out" ]] && context="$(jq -r '.hookSpecificOutput.additionalContext // empty' <<<"$out")"
|
|
305
|
+
if [[ "$context" != *"$expect_context"* ]]; then
|
|
306
|
+
echo "FAIL: [$script_rel] $name — additionalContext did not contain expected substring" >&2
|
|
307
|
+
echo " expected substring: $expect_context" >&2
|
|
308
|
+
echo " actual context: $context" >&2
|
|
309
|
+
failures=$((failures + 1))
|
|
310
|
+
fi
|
|
311
|
+
fi
|
|
312
|
+
|
|
313
|
+
if [[ "$expect_no_context" == "true" ]]; then
|
|
314
|
+
context=""
|
|
315
|
+
[[ -n "$out" ]] && context="$(jq -r '.hookSpecificOutput.additionalContext // empty' <<<"$out")"
|
|
316
|
+
if [[ -n "$context" ]]; then
|
|
317
|
+
echo "FAIL: [$script_rel] $name — expected no additionalContext, got one" >&2
|
|
318
|
+
echo " actual context: $context" >&2
|
|
319
|
+
failures=$((failures + 1))
|
|
320
|
+
fi
|
|
321
|
+
fi
|
|
322
|
+
|
|
323
|
+
if [[ -n "$expect_context_excludes" ]]; then
|
|
324
|
+
context=""
|
|
325
|
+
[[ -n "$out" ]] && context="$(jq -r '.hookSpecificOutput.additionalContext // empty' <<<"$out")"
|
|
326
|
+
if [[ "$context" == *"$expect_context_excludes"* ]]; then
|
|
327
|
+
echo "FAIL: [$script_rel] $name — additionalContext contained a substring it must not" >&2
|
|
328
|
+
echo " excluded substring: $expect_context_excludes" >&2
|
|
329
|
+
echo " actual context: $context" >&2
|
|
330
|
+
failures=$((failures + 1))
|
|
331
|
+
fi
|
|
332
|
+
fi
|
|
333
|
+
|
|
334
|
+
if [[ -n "$expect_audit_log" ]]; then
|
|
335
|
+
audit_log_content=""
|
|
336
|
+
[[ -f "$case_dir/audit.log" ]] && audit_log_content="$(cat "$case_dir/audit.log")"
|
|
337
|
+
if [[ "$audit_log_content" != *"$expect_audit_log"* ]]; then
|
|
338
|
+
echo "FAIL: [$script_rel] $name — audit log did not contain expected substring" >&2
|
|
339
|
+
echo " expected substring: $expect_audit_log" >&2
|
|
340
|
+
echo " actual audit log: $audit_log_content" >&2
|
|
341
|
+
failures=$((failures + 1))
|
|
342
|
+
fi
|
|
343
|
+
fi
|
|
344
|
+
|
|
345
|
+
if [[ "$expect_no_audit_log" == "true" ]] && [[ -f "$case_dir/audit.log" ]]; then
|
|
346
|
+
echo "FAIL: [$script_rel] $name — expected no audit-log write, but one occurred" >&2
|
|
347
|
+
echo " actual audit log: $(cat "$case_dir/audit.log")" >&2
|
|
348
|
+
failures=$((failures + 1))
|
|
349
|
+
fi
|
|
350
|
+
|
|
351
|
+
if [[ "$has_assertion" != "true" ]]; then
|
|
352
|
+
echo "FAIL: [$script_rel] $name — case declares no recognized assertion field (expect/expect_context/expect_reason/expect_no_context/expect_audit_log/expect_no_audit_log); it would pass vacuously" >&2
|
|
353
|
+
failures=$((failures + 1))
|
|
354
|
+
fi
|
|
355
|
+
done
|
|
356
|
+
done
|
|
357
|
+
|
|
358
|
+
if (( failures > 0 )); then
|
|
359
|
+
echo "hook fixtures: $failures of $total cases FAILED" >&2
|
|
360
|
+
exit 1
|
|
361
|
+
fi
|
|
362
|
+
echo "hook fixtures: all $total cases passed."
|