@skathio/somi 1.2.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/.claude/settings.json +9 -9
  2. package/.claude-plugin/marketplace.json +1 -1
  3. package/.claude-plugin/plugin.json +1 -1
  4. package/.copilot-extension/extension.json +1 -1
  5. package/.copilot-extension/marketplace.json +1 -1
  6. package/README.md +6 -1
  7. package/VERSION +1 -1
  8. package/commands/adopt.md +6 -4
  9. package/commands/code-loop.md +13 -13
  10. package/commands/plan-loop.md +10 -10
  11. package/commands/pr.md +1 -1
  12. package/commands/release-readiness.md +4 -4
  13. package/commands/review-panel.md +1 -1
  14. package/commands/review.md +3 -3
  15. package/commands/ship-loop.md +2 -2
  16. package/commands/somi.md +2 -2
  17. package/commands/upgrade.md +1 -1
  18. package/examples/sample-consumer/CLAUDE.md +5 -2
  19. package/examples/sample-consumer/README.md +20 -9
  20. package/hooks/hooks.json +8 -8
  21. package/hooks/lib/common.mjs +328 -0
  22. package/hooks/post-tool/audit-log.mjs +93 -0
  23. package/hooks/post-tool/lint-changed-files.mjs +212 -0
  24. package/hooks/pre-tool/block-dangerous-bash.mjs +382 -0
  25. package/hooks/pre-tool/block-secret-writes.mjs +96 -0
  26. package/hooks/pre-tool/gate-dep-install.mjs +229 -0
  27. package/hooks/pre-tool/guard-protected-paths.mjs +157 -0
  28. package/hooks/session-start/detect-repo-instructions.mjs +264 -0
  29. package/hooks/user-prompt-submit/inject-workflow-context.mjs +432 -0
  30. package/package.json +2 -2
  31. package/rules/CLAUDE.md +6 -5
  32. package/scripts/somi-check.mjs +243 -0
  33. package/scripts/somi-findings.mjs +307 -0
  34. package/scripts/somi-loop.mjs +354 -0
  35. package/scripts/validate.sh +21 -16
  36. package/skills/clean-code/SKILL.md +1 -1
  37. package/tests/hooks/cases/audit-log.json +21 -0
  38. package/tests/hooks/cases/block-dangerous-bash.json +90 -2
  39. package/tests/hooks/cases/block-secret-writes.json +1 -1
  40. package/tests/hooks/cases/detect-repo-instructions.json +84 -0
  41. package/tests/hooks/cases/gate-dep-install.json +6 -1
  42. package/tests/hooks/cases/guard-protected-paths.json +1 -1
  43. package/tests/hooks/cases/inject-workflow-context.json +126 -0
  44. package/tests/hooks/cases/lint-changed-files.json +120 -0
  45. package/tests/hooks/run.sh +280 -15
  46. package/tests/scripts/goldens/somi-findings.json +336 -0
  47. package/tests/scripts/goldens/somi-loop.json +168 -0
  48. package/tests/scripts/run.sh +251 -30
  49. package/hooks/lib/common.sh +0 -133
  50. package/hooks/post-tool/audit-log.sh +0 -40
  51. package/hooks/post-tool/lint-changed-files.sh +0 -80
  52. package/hooks/pre-tool/block-dangerous-bash.sh +0 -171
  53. package/hooks/pre-tool/block-secret-writes.sh +0 -83
  54. package/hooks/pre-tool/gate-dep-install.sh +0 -113
  55. package/hooks/pre-tool/guard-protected-paths.sh +0 -89
  56. package/hooks/session-start/detect-repo-instructions.sh +0 -73
  57. package/hooks/user-prompt-submit/inject-workflow-context.sh +0 -143
  58. package/scripts/somi-check.sh +0 -135
  59. package/scripts/somi-findings.sh +0 -173
  60. package/scripts/somi-loop.sh +0 -225
@@ -1,171 +0,0 @@
1
- #!/usr/bin/env bash
2
- # PreToolUse hook (matcher: Bash) — block clearly dangerous shell commands.
3
- #
4
- # This is a deterministic guardrail, not a policy debate. It catches the
5
- # common-and-catastrophic class of mistakes; nuanced cases are the human's call.
6
- #
7
- # Block list focuses on irreversible / system-destructive / supply-chain shapes,
8
- # plus shell-level writes to secret-bearing paths — the Bash-side complement of
9
- # block-secret-writes.sh, whose Write|Edit matcher a redirect / tee / sed -i /
10
- # cp / mv would otherwise bypass entirely.
11
- # It is intentionally conservative — false positives cost less than false negatives here.
12
- #
13
- # Behavioral fixtures for this script live in tests/hooks/cases/ and run in CI
14
- # via scripts/validate.sh. If you change a pattern, add or update a fixture.
15
-
16
- set -euo pipefail
17
-
18
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19
- # shellcheck source=../lib/common.sh
20
- source "$SCRIPT_DIR/../lib/common.sh"
21
-
22
- somi::read_payload
23
-
24
- CMD="$(somi::field '.tool_input.command')"
25
- [[ -z "$CMD" ]] && exit 0
26
-
27
- # Quote-stripped copy. `bash -c "rm -rf /"` hides the dangerous string inside
28
- # quotes where the patterns' trailing boundary classes ([[:space:]]|$) don't
29
- # match. Stripping quote characters never removes dangerous content — it only
30
- # widens matching — so every check runs against both the raw and the stripped
31
- # form. False positives are tolerated here by design.
32
- CMD_STRIPPED=${CMD//\"/}
33
- CMD_STRIPPED=${CMD_STRIPPED//\'/}
34
-
35
- # Case-sensitive patterns (filesystem paths, exact tools, exact flags).
36
- DANGEROUS_PATTERNS=(
37
- # filesystem nukes
38
- 'rm[[:space:]]+-rf?[[:space:]]+/([[:space:]]|$)'
39
- 'rm[[:space:]]+-rf?[[:space:]]+~([[:space:]]|/|$)'
40
- 'rm[[:space:]]+-rf?[[:space:]]+\*'
41
- 'rm[[:space:]]+-rf?[[:space:]]+\$HOME'
42
- ':\(\)\{[[:space:]]*:\|:&[[:space:]]*\};:' # fork bomb
43
-
44
- # device / partition writes
45
- '>[[:space:]]*/dev/(sd[a-z]|nvme|hd[a-z]|disk)'
46
- 'dd[[:space:]]+if=.*[[:space:]]+of=/dev/(sd[a-z]|nvme|hd[a-z]|disk)'
47
- 'mkfs(\.|[[:space:]])'
48
-
49
- # supply-chain / remote-exec one-liners
50
- 'curl[[:space:]]+[^|]*\|[[:space:]]*(sudo[[:space:]]+)?(ba)?sh'
51
- 'wget[[:space:]]+[^|]*\|[[:space:]]*(sudo[[:space:]]+)?(ba)?sh'
52
- 'curl[[:space:]]+[^|]*\|[[:space:]]*python[0-9]*'
53
- 'wget[[:space:]]+[^|]*\|[[:space:]]*python[0-9]*'
54
-
55
- # destructive git ops on protected branches
56
- # Covers --force, -f, --force-with-lease (with or without =value), and refspec form (origin HEAD:main).
57
- 'git[[:space:]]+push[[:space:]]+(-{1,2}force|-f)([[:space:]=]|$).*[[:space:]:](main|master|trunk|release)([[:space:]]|$)'
58
- 'git[[:space:]]+push[[:space:]]+--force-with-lease([[:space:]=][^[:space:]]*)?[[:space:]].*[[:space:]:](main|master|trunk|release)([[:space:]]|$)'
59
- # force-push via +refspec — no --force flag involved (`git push origin +main`, `+HEAD:main`)
60
- 'git[[:space:]]+push[[:space:]]+[^;&|]*[[:space:]]\+(main|master|trunk|release)([[:space:]:]|$)'
61
- 'git[[:space:]]+push[[:space:]]+[^;&|]*[[:space:]]\+[^[:space:]]*:(main|master|trunk|release)([[:space:]]|$)'
62
- 'git[[:space:]]+branch[[:space:]]+-D[[:space:]]+(main|master|trunk)'
63
- 'git[[:space:]]+reset[[:space:]]+--hard[[:space:]]+(origin/)?(main|master|trunk)'
64
- 'git[[:space:]]+clean[[:space:]]+-[fdx]+[[:space:]]'
65
-
66
- # process / permission ops
67
- 'chmod[[:space:]]+-R[[:space:]]+777[[:space:]]+/'
68
- 'chown[[:space:]]+-R[[:space:]]+.*[[:space:]]+/'
69
-
70
- # skipping safety checks (only block when used in commit/push context)
71
- 'git[[:space:]]+commit[[:space:]]+.*--no-verify'
72
- 'git[[:space:]]+push[[:space:]]+.*--no-verify'
73
- )
74
-
75
- # Case-insensitive patterns (SQL keywords arrive in lowercase from ORM logs, mixed case from REPLs).
76
- DANGEROUS_PATTERNS_NOCASE=(
77
- 'DROP[[:space:]]+DATABASE'
78
- 'DROP[[:space:]]+SCHEMA[[:space:]]+(public|prod|production)'
79
- 'DROP[[:space:]]+TABLE[[:space:]]+[a-zA-Z_]+'
80
- 'TRUNCATE[[:space:]]+(TABLE[[:space:]]+)?[a-zA-Z_]+'
81
- 'DELETE[[:space:]]+FROM[[:space:]]+[a-zA-Z_]+[[:space:]]*;'
82
- )
83
-
84
- # Secret-bearing targets for the shell-write check below. Mirrors the basename
85
- # list in block-secret-writes.sh (keep the two in sync when extending either).
86
- SECRET_BASENAME='(\.env(\.(local|production|prod|staging|secret))?|id_(rsa|ed25519|ecdsa|dsa)|[^[:space:]]*\.(pem|key|p12|pfx|jks)|[^[:space:]]*(-key|-credentials)\.json|service-account[^[:space:]]*\.json|\.netrc|\.pgpass|[^[:space:]]*secrets?\.(ya?ml|json))'
87
-
88
- # Shell-level writes to secret paths: redirection, tee, in-place sed, cp/mv onto
89
- # the target. These run against the quote-stripped command.
90
- SECRET_WRITE_PATTERNS=(
91
- "(>|>>)[[:space:]]*([^[:space:]]*/)?${SECRET_BASENAME}([[:space:]]|$)"
92
- "(^|[[:space:]|;&])tee[[:space:]]+(-[a-zA-Z]+[[:space:]]+)*([^[:space:]]*/)?${SECRET_BASENAME}([[:space:]]|$)"
93
- "(^|[[:space:]|;&])sed[[:space:]]+[^|;&]*-i[^|;&]*[[:space:]]([^[:space:]]*/)?${SECRET_BASENAME}([[:space:]]|$)"
94
- "(^|[[:space:]|;&])(cp|mv)[[:space:]]+[^|;&]*[[:space:]]([^[:space:]]*/)?${SECRET_BASENAME}([[:space:]]|$)"
95
- )
96
-
97
- check_dangerous() {
98
- local c="$1" pattern
99
- for pattern in "${DANGEROUS_PATTERNS[@]}"; do
100
- if [[ "$c" =~ $pattern ]]; then
101
- somi::deny_pretool "somi refused this command: it matches a dangerous-shell pattern (\`${BASH_REMATCH[0]}\`).
102
- If this is genuinely intended, stop and ask the human to run it themselves — never work around this hook silently."
103
- fi
104
- done
105
-
106
- shopt -s nocasematch
107
- for pattern in "${DANGEROUS_PATTERNS_NOCASE[@]}"; do
108
- if [[ "$c" =~ $pattern ]]; then
109
- somi::deny_pretool "somi refused this command: it matches a destructive-SQL pattern (\`${BASH_REMATCH[0]}\`).
110
- If this is genuinely intended, stop and ask the human to run it themselves — never work around this hook silently."
111
- fi
112
- done
113
- shopt -u nocasematch
114
- }
115
-
116
- check_secret_writes() {
117
- local c="$1" pattern matched
118
- for pattern in "${SECRET_WRITE_PATTERNS[@]}"; do
119
- if [[ "$c" =~ $pattern ]]; then
120
- matched="${BASH_REMATCH[0]}"
121
- # Explicit example/template files are fine — same exception as block-secret-writes.sh.
122
- # (Capture the match first: the exception's own =~ resets BASH_REMATCH.)
123
- if [[ "$matched" =~ \.env\.(example|sample|template|dist) ]]; then
124
- continue
125
- fi
126
- somi::deny_pretool "somi refused this command: it writes to a secret-bearing path via the shell (\`${matched}\`).
127
- Bootstrap secret files by hand, or commit only \`.env.example\`-style templates. This is the
128
- Bash-side twin of the Write/Edit secret guard — do not work around either silently."
129
- fi
130
- done
131
- }
132
-
133
- # Force-push without a verifiable target. The protected-branch patterns above
134
- # only fire when the branch is named in the command — but `git push -f`,
135
- # `git push -f origin`, and `git push -f origin HEAD` push the *current* branch,
136
- # which this hook cannot resolve (it may well be main). Deny force pushes that
137
- # don't name an explicit target branch; naming it is what makes the
138
- # protected-branch check meaningful.
139
- check_bare_force_push() {
140
- local c="$1"
141
- local re='git[[:space:]]+push([[:space:]]+[^;&|]*)?[[:space:]](-f|--force(-with-lease(=[^[:space:]]*)?)?)([[:space:]]|$)'
142
- [[ "$c" =~ $re ]] || return 0
143
-
144
- local after_push="${c#*push}"
145
- local nonflag_count=0 names_bare_head=0 tok
146
- # shellcheck disable=SC2086 # intentional word splitting of the command tail
147
- for tok in $after_push; do
148
- case "$tok" in
149
- ';'|'&&'|'||'|'|') break ;; # stop at a command boundary
150
- -*) continue ;; # flags don't name a target
151
- esac
152
- nonflag_count=$((nonflag_count + 1))
153
- if [[ $nonflag_count -eq 2 && "$tok" == "HEAD" ]]; then
154
- names_bare_head=1
155
- fi
156
- done
157
-
158
- if (( nonflag_count < 2 )) || (( names_bare_head == 1 )); then
159
- somi::deny_pretool "somi refused this command: force-push without an explicit target branch (the current branch cannot be verified and may be protected).
160
- Name the remote and branch explicitly — e.g. \`git push --force-with-lease origin feature-x\`.
161
- Force pushes naming main/master/trunk/release are always refused; if this is genuinely
162
- intended, stop and ask the human to run it themselves."
163
- fi
164
- }
165
-
166
- check_dangerous "$CMD"
167
- check_dangerous "$CMD_STRIPPED"
168
- check_secret_writes "$CMD_STRIPPED"
169
- check_bare_force_push "$CMD_STRIPPED"
170
-
171
- exit 0
@@ -1,83 +0,0 @@
1
- #!/usr/bin/env bash
2
- # PreToolUse hook (matcher: Write|Edit) — block writes to secret-bearing paths.
3
- #
4
- # We block any attempt to write/edit files that are likely to hold real secrets.
5
- # If the user genuinely needs to bootstrap a `.env`, they can do it themselves
6
- # or explicitly add an override in their settings.local.json.
7
-
8
- set -euo pipefail
9
-
10
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11
- # shellcheck source=../lib/common.sh
12
- source "$SCRIPT_DIR/../lib/common.sh"
13
-
14
- somi::read_payload
15
-
16
- TOOL="$(somi::field '.tool_name')"
17
- PATH_INPUT="$(somi::field '.tool_input.file_path')"
18
- [[ -z "$PATH_INPUT" ]] && exit 0
19
-
20
- # Normalise to basename + path for pattern matching.
21
- BASENAME="$(basename "$PATH_INPUT")"
22
-
23
- SECRET_PATTERNS=(
24
- # env files (allow .env.example, .env.sample, .env.template explicitly)
25
- '^\.env$'
26
- '^\.env\.local$'
27
- '^\.env\.production$'
28
- '^\.env\.prod$'
29
- '^\.env\.staging$'
30
- '^\.env\.secret$'
31
-
32
- # private keys and certs
33
- '\.pem$'
34
- '\.key$'
35
- '\.p12$'
36
- '\.pfx$'
37
- '\.jks$'
38
- '^id_rsa$'
39
- '^id_ed25519$'
40
- '^id_ecdsa$'
41
- '^id_dsa$'
42
-
43
- # cloud credentials
44
- '^credentials$' # ~/.aws/credentials
45
- '^config$' # ~/.aws/config (usually contains profile refs but no secrets)
46
-
47
- # service-account keys
48
- '-key\.json$'
49
- '-credentials\.json$'
50
- 'service-account.*\.json$'
51
-
52
- # shell rc files (may contain export STATEMENTS with secrets)
53
- '\.netrc$'
54
- '\.pgpass$'
55
-
56
- # vault / secret tool files
57
- '\.kdbx$'
58
- 'secrets?\.ya?ml$'
59
- 'secrets?\.json$'
60
- )
61
-
62
- # Allow explicit example/template files.
63
- EXAMPLE_BASENAMES=(
64
- '.env.example'
65
- '.env.sample'
66
- '.env.template'
67
- '.env.dist'
68
- )
69
-
70
- for ex in "${EXAMPLE_BASENAMES[@]}"; do
71
- if [[ "$BASENAME" == "$ex" ]]; then
72
- exit 0
73
- fi
74
- done
75
-
76
- for pattern in "${SECRET_PATTERNS[@]}"; do
77
- if [[ "$BASENAME" =~ $pattern ]]; then
78
- somi::deny_pretool "somi refused to ${TOOL} \`${PATH_INPUT}\`: this path is in the secret-bearing allowlist.
79
- Bootstrap secret files by hand, or commit only \`.env.example\`-style templates."
80
- fi
81
- done
82
-
83
- exit 0
@@ -1,113 +0,0 @@
1
- #!/usr/bin/env bash
2
- # PreToolUse hook (matcher: Bash) — gate dependency-adding commands.
3
- #
4
- # Adding a runtime dependency crosses a trust boundary: it imports unreviewed code,
5
- # expands attack surface, and creates a long-term maintenance obligation. The
6
- # coder should not add deps as a drive-by — they belong in `decisions.md` or at
7
- # minimum in the iteration summary so a human can sign off.
8
- #
9
- # This hook denies adding a *new* dep without an explicit acknowledgement
10
- # (SOMI_ALLOW_DEP_INSTALL=1 in the env, set by the human for the session).
11
- # Lockfile-respecting reinstalls (bare `npm install`, `yarn install`, etc.)
12
- # are allowed — those don't add deps, they materialize what's already declared.
13
-
14
- set -euo pipefail
15
-
16
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17
- # shellcheck source=../lib/common.sh
18
- source "$SCRIPT_DIR/../lib/common.sh"
19
-
20
- somi::read_payload
21
-
22
- CMD="$(somi::field '.tool_input.command')"
23
- [[ -z "$CMD" ]] && exit 0
24
-
25
- # Explicit opt-in: human acknowledged this session may add deps.
26
- if [[ "${SOMI_ALLOW_DEP_INSTALL:-0}" == "1" ]]; then
27
- exit 0
28
- fi
29
-
30
- # Per-project allowlist from .somi/config.json (`dep_install.allow`) — committed,
31
- # reviewable policy, scoped to name prefixes, unlike the all-or-nothing env opt-in.
32
- # An entry is a PREFIX of a package argument: "@types/" allows `npm install
33
- # @types/node`. Conservative by construction: compound commands (`;`, `&&`, `|`)
34
- # never qualify, and EVERY package token must match an entry.
35
- config_allows_dep() {
36
- local c="$1"
37
- local -a allow=()
38
- mapfile -t allow < <(somi::config '.dep_install.allow[]?')
39
- (( ${#allow[@]} > 0 )) || return 1
40
- [[ "$c" == *';'* || "$c" == *'&'* || "$c" == *'|'* ]] && return 1
41
-
42
- local -a toks=()
43
- local seen_verb=0 tok
44
- # shellcheck disable=SC2086 # intentional word splitting of the command
45
- for tok in $c; do
46
- if (( seen_verb )); then
47
- [[ "$tok" == -* ]] && continue
48
- toks+=("$tok")
49
- elif [[ "$tok" =~ ^(install|i|add|get|require)$ ]]; then
50
- seen_verb=1
51
- fi
52
- done
53
- (( ${#toks[@]} > 0 )) || return 1
54
-
55
- local t a ok
56
- for t in "${toks[@]}"; do
57
- ok=0
58
- for a in "${allow[@]}"; do
59
- [[ "$t" == "$a"* ]] && { ok=1; break; }
60
- done
61
- (( ok )) || return 1
62
- done
63
- return 0
64
- }
65
-
66
- # Patterns for "add a new dependency". A trailing package argument is required;
67
- # bare `<pm> install` (no package) is the lockfile-respecting form and is fine.
68
- DEP_ADD_PATTERNS=(
69
- # npm / yarn / pnpm / bun: install with at least one positional package
70
- 'npm[[:space:]]+(install|i|add)[[:space:]]+([^-][^[:space:]]*|--save[[:space:]]+[^[:space:]]+|--save-dev[[:space:]]+[^[:space:]]+)'
71
- 'yarn[[:space:]]+add[[:space:]]+[^-][^[:space:]]*'
72
- 'pnpm[[:space:]]+(add|install)[[:space:]]+[^-][^[:space:]]*'
73
- 'bun[[:space:]]+(add|install)[[:space:]]+[^-][^[:space:]]*'
74
-
75
- # python: pip / pipx / uv / poetry / conda
76
- 'pip[0-9]*[[:space:]]+install[[:space:]]+([^-][^[:space:]]*|-r[[:space:]]+[^[:space:]]+\.txt)'
77
- 'pip[0-9]*[[:space:]]+install[[:space:]]+--upgrade[[:space:]]+[^-][^[:space:]]*'
78
- 'pipx[[:space:]]+install[[:space:]]+[^-][^[:space:]]*'
79
- 'uv[[:space:]]+(add|pip[[:space:]]+install)[[:space:]]+[^-][^[:space:]]*'
80
- 'poetry[[:space:]]+add[[:space:]]+[^-][^[:space:]]*'
81
- 'conda[[:space:]]+install[[:space:]]+[^-][^[:space:]]*'
82
-
83
- # rust / go / ruby / php
84
- 'cargo[[:space:]]+(add|install)[[:space:]]+[^-][^[:space:]]*'
85
- 'go[[:space:]]+get[[:space:]]+[^-][^[:space:]]*'
86
- 'go[[:space:]]+install[[:space:]]+[^-][^[:space:]]*'
87
- 'gem[[:space:]]+install[[:space:]]+[^-][^[:space:]]*'
88
- 'bundle[[:space:]]+add[[:space:]]+[^-][^[:space:]]*'
89
- 'composer[[:space:]]+(require|install)[[:space:]]+[^-][^[:space:]]*'
90
-
91
- # generic curl-to-installer (`brew install` is borderline; install scripts often add tools).
92
- 'brew[[:space:]]+install[[:space:]]+[^-][^[:space:]]*'
93
- 'apt[[:space:]]+(install|add)[[:space:]]+[^-][^[:space:]]*'
94
- 'apt-get[[:space:]]+install[[:space:]]+[^-][^[:space:]]*'
95
- )
96
-
97
- for pattern in "${DEP_ADD_PATTERNS[@]}"; do
98
- if [[ "$CMD" =~ $pattern ]]; then
99
- if config_allows_dep "$CMD"; then
100
- somi::audit "ALLOW" "dep install permitted by .somi/config.json dep_install.allow: $CMD"
101
- exit 0
102
- fi
103
- somi::deny_pretool "somi refused this command: it would add a new dependency (\`${BASH_REMATCH[0]}\`).
104
- Adding a dep is a decision — record it in \`.somi/plans/<slug>/decisions.md\` (or surface it in
105
- the iteration summary for the human) and re-run with \`SOMI_ALLOW_DEP_INSTALL=1\` in the
106
- environment for this session, or have the human run the install themselves. Projects can also
107
- allowlist package-name prefixes in \`.somi/config.json\` under \`dep_install.allow\`.
108
- Lockfile-respecting reinstalls (bare \`npm install\` / \`pip install -r requirements.txt\`-less,
109
- \`bundle install\`, etc.) are allowed without acknowledgement."
110
- fi
111
- done
112
-
113
- exit 0
@@ -1,89 +0,0 @@
1
- #!/usr/bin/env bash
2
- # PreToolUse hook (matcher: Write|Edit) — block writes to paths the agent shouldn't be editing.
3
- #
4
- # Targets the most common "agent overstep" cases: .git internals, vendored deps,
5
- # build outputs, and the SOMI plugin install (so an agent doesn't rewrite its
6
- # own ruleset under your feet).
7
- #
8
- # Lockfile writes are blocked by default (use `npm install <pkg>` etc instead),
9
- # but a project can opt out via SOMI_ALLOW_LOCKFILES=1.
10
-
11
- set -euo pipefail
12
-
13
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
14
- # shellcheck source=../lib/common.sh
15
- source "$SCRIPT_DIR/../lib/common.sh"
16
-
17
- somi::read_payload
18
-
19
- TOOL="$(somi::field '.tool_name')"
20
- PATH_INPUT="$(somi::field '.tool_input.file_path')"
21
- [[ -z "$PATH_INPUT" ]] && exit 0
22
-
23
- # Normalise relative paths so the leading-`*/` globs below still match
24
- # (a relative `node_modules/x.js` would otherwise slip past `*/node_modules/*`).
25
- # Same unexpanded-variable guard as common.sh's audit-log path.
26
- if [[ "$PATH_INPUT" != /* ]]; then
27
- NORM_BASE="${CLAUDE_PROJECT_DIR:-$PWD}"
28
- [[ "$NORM_BASE" == *'${'* ]] && NORM_BASE="$PWD"
29
- PATH_INPUT="$NORM_BASE/$PATH_INPUT"
30
- fi
31
-
32
- PROTECTED_GLOBS=(
33
- # Source-control internals.
34
- '*/.git/*'
35
-
36
- # SOMI plugin install — never let an agent rewrite the ruleset under itself.
37
- '*/.claude/plugins/somi/*'
38
-
39
- # Vendored deps & build outputs.
40
- '*/node_modules/*'
41
- '*/vendor/*'
42
- '*/dist/*'
43
- '*/build/*'
44
- '*/target/*'
45
- '*/.next/*'
46
- '*/.nuxt/*'
47
- '*/__pycache__/*'
48
- )
49
-
50
- LOCKFILES=(
51
- 'package-lock.json'
52
- 'yarn.lock'
53
- 'pnpm-lock.yaml'
54
- 'Cargo.lock'
55
- 'Gemfile.lock'
56
- 'poetry.lock'
57
- 'uv.lock'
58
- 'composer.lock'
59
- 'go.sum'
60
- )
61
-
62
- # Protected glob match (uses bash extglob).
63
- shopt -s extglob
64
- for glob in "${PROTECTED_GLOBS[@]}"; do
65
- # shellcheck disable=SC2053
66
- if [[ "$PATH_INPUT" == $glob ]]; then
67
- somi::deny_pretool "somi refused to ${TOOL} \`${PATH_INPUT}\`: this path matches a protected glob (\`${glob}\`).
68
- These paths are owned by tooling (git / package managers / SOMI itself); changes belong in their respective workflows."
69
- fi
70
- done
71
-
72
- # Lockfile gate. Precedence: env (session override, even "0") > .somi/config.json
73
- # (`lockfiles.allow_edit`, committed project policy) > default (deny).
74
- ALLOW_LOCKFILES="${SOMI_ALLOW_LOCKFILES:-}"
75
- if [[ -z "$ALLOW_LOCKFILES" && "$(somi::config '.lockfiles.allow_edit')" == "true" ]]; then
76
- ALLOW_LOCKFILES="1"
77
- fi
78
- if [[ "$ALLOW_LOCKFILES" != "1" ]]; then
79
- BASENAME="$(basename "$PATH_INPUT")"
80
- for lock in "${LOCKFILES[@]}"; do
81
- if [[ "$BASENAME" == "$lock" ]]; then
82
- somi::deny_pretool "somi refused to ${TOOL} \`${PATH_INPUT}\`: lockfiles should be regenerated by the package manager, not hand-edited.
83
- If you genuinely need to hand-edit this lockfile, set SOMI_ALLOW_LOCKFILES=1 in your environment
84
- for this session (or set \`lockfiles.allow_edit: true\` in \`.somi/config.json\` as project policy)."
85
- fi
86
- done
87
- fi
88
-
89
- exit 0
@@ -1,73 +0,0 @@
1
- #!/usr/bin/env bash
2
- # SessionStart hook — detect repo-local agent/instruction files and surface them.
3
- #
4
- # SoMi's MAX→ECO economy is "respect repo conventions as context": when a project
5
- # ships its own instructions (CLAUDE.md / AGENTS.md / copilot-instructions /
6
- # .cursorrules) or its own subagents (.claude/agents/), SoMi's MAX actions should
7
- # read them once and fold the relevant conventions into the work-item brief.md, so
8
- # the ECO tier inherits them without re-reading. Repo-local instructions WIN over
9
- # SoMi defaults where they conflict; SoMi does NOT auto-invoke foreign agents.
10
- #
11
- # This hook only *surfaces* what exists (paths + a one-line directive) — it does not
12
- # read or ingest file contents (that's the MAX agent's job, and keeps this cheap).
13
- # It fires once per session and stays silent when nothing repo-local is present.
14
-
15
- set -euo pipefail
16
-
17
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
18
- # shellcheck source=../lib/common.sh
19
- source "$SCRIPT_DIR/../lib/common.sh"
20
-
21
- somi::read_payload
22
-
23
- PROJECT_ROOT="${CLAUDE_PROJECT_DIR:-$PWD}"
24
- # Some hosts pass an unexpanded literal; fall back to the resolvable cwd.
25
- [[ "$PROJECT_ROOT" == *'${'* ]] && PROJECT_ROOT="$PWD"
26
-
27
- FOUND=()
28
-
29
- # Root-level instruction files the consumer repo may ship.
30
- for f in "CLAUDE.md" "AGENTS.md" ".cursorrules" ".github/copilot-instructions.md"; do
31
- [[ -f "$PROJECT_ROOT/$f" ]] && FOUND+=("$f")
32
- done
33
-
34
- # Nested CLAUDE.md / AGENTS.md (cap depth and count so this stays cheap and never
35
- # wanders into .git / node_modules / .somi).
36
- while IFS= read -r nested; do
37
- [[ -z "$nested" ]] && continue
38
- rel="${nested#"$PROJECT_ROOT"/}"
39
- FOUND+=("$rel")
40
- done < <(
41
- find "$PROJECT_ROOT" -maxdepth 3 \
42
- \( -path "$PROJECT_ROOT/.git" -o -path "$PROJECT_ROOT/node_modules" \
43
- -o -path "$PROJECT_ROOT/.somi" -o -path "$PROJECT_ROOT/vendor" \) -prune -o \
44
- -mindepth 2 -type f \( -name 'CLAUDE.md' -o -name 'AGENTS.md' \) -print 2>/dev/null \
45
- | head -n 10
46
- )
47
-
48
- # Repo-local subagents (note presence only — never auto-invoke them).
49
- REPO_AGENTS=""
50
- if [[ -d "$PROJECT_ROOT/.claude/agents" ]]; then
51
- count="$(find "$PROJECT_ROOT/.claude/agents" -maxdepth 1 -name '*.md' -type f 2>/dev/null | wc -l | tr -d ' ')"
52
- [[ "${count:-0}" -gt 0 ]] && REPO_AGENTS=".claude/agents/ (${count} repo-local subagent definition(s))"
53
- fi
54
-
55
- if (( ${#FOUND[@]} == 0 )) && [[ -z "$REPO_AGENTS" ]]; then
56
- exit 0
57
- fi
58
-
59
- MSG="somi repo-awareness — this repository ships its own instructions/agents. Respect them as context:"
60
- if (( ${#FOUND[@]} > 0 )); then
61
- # De-duplicate while preserving order.
62
- seen=""
63
- for f in "${FOUND[@]}"; do
64
- case " $seen " in *" $f "*) continue;; esac
65
- seen+=" $f"
66
- MSG+=$'\n - '"$f"
67
- done
68
- fi
69
- [[ -n "$REPO_AGENTS" ]] && MSG+=$'\n - '"$REPO_AGENTS"
70
- MSG+=$'\nMAX actions (/discover, /design, /refactor analysis, and /plan on a cold start) should read these once and distil the relevant conventions into the work item'\''s brief.md / context.md so the ECO tier inherits them without re-reading. Repo-local instructions WIN over SoMi defaults where they conflict. Do NOT auto-invoke the repo'\''s own agents — surface them for the user to opt into.'
71
-
72
- somi::context "SessionStart" "$MSG"
73
- exit 0
@@ -1,143 +0,0 @@
1
- #!/usr/bin/env bash
2
- # UserPromptSubmit hook — inject SOMI context on relevant turns.
3
- #
4
- # Two responsibilities:
5
- # 1. Remind the agent of the priority stack and active work-item state.
6
- # 2. Surface end-of-turn loose ends (TODO(claude) markers, scratch files) on
7
- # the NEXT user turn — this replaces the old Stop hook, which used an
8
- # additionalContext channel that Stop events don't actually have.
9
- #
10
- # To avoid double-loading content that's already always-on, the reminder block
11
- # only fires on the first turn of a session OR when work-item state has changed
12
- # since the last turn. The loose-end nudges fire whenever there is something
13
- # to nudge about.
14
-
15
- set -euo pipefail
16
-
17
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
18
- # shellcheck source=../lib/common.sh
19
- source "$SCRIPT_DIR/../lib/common.sh"
20
-
21
- somi::read_payload
22
-
23
- PROJECT_ROOT="${CLAUDE_PROJECT_DIR:-$PWD}"
24
- STATE_DIR="${PROJECT_ROOT}/.claude/somi-state"
25
- STATE_FILE="${STATE_DIR}/last-context-signature"
26
- mkdir -p "$STATE_DIR"
27
-
28
- # Compute a signature for "current SOMI state" — first turn writes it; subsequent
29
- # turns only re-inject the reminder if the signature changed.
30
- compute_signature() {
31
- local plans_state=""
32
- if [[ -d "$PROJECT_ROOT/.somi/plans" ]]; then
33
- plans_state="$(find "$PROJECT_ROOT/.somi/plans" -maxdepth 2 -name 'progress.md' -printf '%T@ %p\n' 2>/dev/null | sort | sha256sum | cut -d' ' -f1)"
34
- fi
35
- local reviews_state=""
36
- if [[ -d "$PROJECT_ROOT/.somi/reviews" ]]; then
37
- reviews_state="$(find "$PROJECT_ROOT/.somi/reviews" -maxdepth 3 -name '*.md' -printf '%T@ %p\n' 2>/dev/null | sort | sha256sum | cut -d' ' -f1)"
38
- fi
39
- local rd_state=""
40
- if [[ -d "$PROJECT_ROOT/.somi/rd" ]]; then
41
- rd_state="$(find "$PROJECT_ROOT/.somi/rd" -maxdepth 2 -name 'README.md' -printf '%T@ %p\n' 2>/dev/null | sort | sha256sum | cut -d' ' -f1)"
42
- fi
43
- printf '%s:%s:%s' "$plans_state" "$reviews_state" "$rd_state"
44
- }
45
-
46
- CURRENT_SIG="$(compute_signature)"
47
- LAST_SIG=""
48
- if [[ -f "$STATE_FILE" ]]; then
49
- LAST_SIG="$(cat "$STATE_FILE" 2>/dev/null || true)"
50
- fi
51
- echo "$CURRENT_SIG" > "$STATE_FILE"
52
-
53
- # Active work-item state pulled from .somi/plans/<slug>/progress.md (if present).
54
- PLAN_HINT=""
55
- if [[ -d "$PROJECT_ROOT/.somi/plans" ]]; then
56
- in_progress=()
57
- while IFS= read -r progress_file; do
58
- [[ -z "$progress_file" ]] && continue
59
- if grep -qiE '^[[:space:]]*<?in-progress>?[[:space:]]*$' "$progress_file" 2>/dev/null \
60
- || grep -qiE 'status:[[:space:]]*`?in-progress`?' "$progress_file" 2>/dev/null; then
61
- slug="$(basename "$(dirname "$progress_file")")"
62
- in_progress+=("$slug")
63
- fi
64
- done < <(find "$PROJECT_ROOT/.somi/plans" -maxdepth 2 -name 'progress.md' 2>/dev/null)
65
- if (( ${#in_progress[@]} == 1 )); then
66
- PLAN_HINT=$'\n- Active work item: `.somi/plans/'"${in_progress[0]}"$'/`. Follow its `spec.md` and active iteration in `phases/`; update `progress.md` / `diary.md` as work proceeds.'
67
- elif (( ${#in_progress[@]} > 1 )); then
68
- PLAN_HINT=$'\n- Multiple in-progress work items in `.somi/plans/`: '"$(IFS=,; echo "${in_progress[*]}")"$'. Confirm with the user which one applies before coding.'
69
- fi
70
- fi
71
-
72
- # Active discovery state pulled from .somi/rd/<slug>/README.md (if present).
73
- RD_HINT=""
74
- if [[ -d "$PROJECT_ROOT/.somi/rd" ]]; then
75
- rd_active=()
76
- rd_ready=()
77
- while IFS= read -r rd_readme; do
78
- [[ -z "$rd_readme" ]] && continue
79
- slug="$(basename "$(dirname "$rd_readme")")"
80
- if grep -qiE 'status:[[:space:]]*`?(researching|drafting|awaiting-verification)`?' "$rd_readme" 2>/dev/null; then
81
- rd_active+=("$slug")
82
- elif grep -qiE 'status:[[:space:]]*`?ready-for-planning`?' "$rd_readme" 2>/dev/null; then
83
- rd_ready+=("$slug")
84
- fi
85
- done < <(find "$PROJECT_ROOT/.somi/rd" -maxdepth 2 -name 'README.md' 2>/dev/null)
86
- if (( ${#rd_active[@]} >= 1 )); then
87
- RD_HINT=$'\n- Active discovery in `.somi/rd/`: '"$(IFS=,; echo "${rd_active[*]}")"$'. This is requirements-engineering work (`/discover`): research the competition, author the R&D doc set, verify crossroads, before handing to `/plan`.'
88
- elif (( ${#rd_ready[@]} == 1 )); then
89
- RD_HINT=$'\n- R&D foundation ready in `.somi/rd/'"${rd_ready[0]}"$'/`. `/plan '"${rd_ready[0]}"$'` will consume its SRS/FRD as the requirements source and SDD/TDD as architectural direction.'
90
- fi
91
- fi
92
-
93
- # Loose-end detection (runs every turn; cheap and only fires when something to nudge).
94
- NUDGES=()
95
- if command -v git >/dev/null 2>&1 && [[ -d "$PROJECT_ROOT/.git" ]]; then
96
- # TODO(claude) / FIXME(claude) markers in working tree or staged changes.
97
- if git -C "$PROJECT_ROOT" diff --no-color --unified=0 HEAD 2>/dev/null \
98
- | grep -E '^\+.*(TODO\(claude\)|TODO\(agent\)|FIXME\(claude\))' -q; then
99
- NUDGES+=("Detected new TODO(claude)/FIXME(claude) markers in the diff against HEAD. List them explicitly as 'not done' in your next summary.")
100
- fi
101
- # Stray .bak / scratch files in working tree.
102
- if git -C "$PROJECT_ROOT" status --porcelain 2>/dev/null \
103
- | grep -E '^\?\? .*(\.bak|\.orig|scratch_)' -q; then
104
- NUDGES+=("Detected scratch / .bak files in working tree. Clean them up before declaring done.")
105
- fi
106
- fi
107
-
108
- # Decide whether to emit the reminder block.
109
- EMIT_REMINDER="no"
110
- if [[ -z "$LAST_SIG" ]] || [[ "$LAST_SIG" != "$CURRENT_SIG" ]]; then
111
- EMIT_REMINDER="yes"
112
- fi
113
-
114
- # Build the final context payload.
115
- PARTS=()
116
- if [[ "$EMIT_REMINDER" == "yes" ]]; then
117
- PARTS+=("somi is active. Reminders:
118
- - Follow rules/CLAUDE.md priorities: security > correctness > maintainability > performance > convenience.
119
- - Plan before coding non-trivial work. Code from the plan, not around it.
120
- - Surface tradeoffs and shortcuts in plain text; never silently compromise.
121
- - Hooks may deny dangerous bash, secret writes, protected paths, and unsanctioned dep installs — do not work around them.${PLAN_HINT}${RD_HINT}")
122
- fi
123
-
124
- if (( ${#NUDGES[@]} > 0 )); then
125
- NUDGE_BLOCK="somi loose-end check:"
126
- for n in "${NUDGES[@]}"; do
127
- NUDGE_BLOCK+=$'\n - '"$n"
128
- done
129
- PARTS+=("$NUDGE_BLOCK")
130
- fi
131
-
132
- if (( ${#PARTS[@]} > 0 )); then
133
- CONTEXT=""
134
- for p in "${PARTS[@]}"; do
135
- if [[ -n "$CONTEXT" ]]; then
136
- CONTEXT+=$'\n\n'
137
- fi
138
- CONTEXT+="$p"
139
- done
140
- somi::context "UserPromptSubmit" "$CONTEXT"
141
- fi
142
-
143
- exit 0