@skathio/somi 1.1.1 → 1.2.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.
Files changed (57) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/.copilot-extension/extension.json +11 -2
  4. package/.copilot-extension/marketplace.json +1 -1
  5. package/README.md +16 -4
  6. package/agents/coder.md +8 -3
  7. package/agents/designer.md +22 -7
  8. package/agents/discovery-analyst.md +11 -0
  9. package/agents/planner.md +69 -9
  10. package/agents/refactorer.md +3 -1
  11. package/agents/reviewer.md +4 -0
  12. package/commands/adopt.md +77 -0
  13. package/commands/atlas.md +83 -0
  14. package/commands/code-loop.md +70 -44
  15. package/commands/code-parallel.md +3 -1
  16. package/commands/code.md +8 -2
  17. package/commands/debug.md +136 -0
  18. package/commands/design.md +17 -7
  19. package/commands/discover.md +23 -18
  20. package/commands/impact.md +74 -0
  21. package/commands/incident.md +83 -0
  22. package/commands/plan-loop.md +44 -19
  23. package/commands/plan.md +38 -26
  24. package/commands/pr.md +82 -0
  25. package/commands/release-readiness.md +70 -0
  26. package/commands/review-panel.md +5 -0
  27. package/commands/review.md +19 -0
  28. package/commands/ship-loop.md +18 -10
  29. package/commands/ship.md +4 -2
  30. package/commands/somi.md +85 -0
  31. package/commands/upgrade.md +81 -0
  32. package/hooks/lib/common.sh +13 -0
  33. package/hooks/pre-tool/block-dangerous-bash.sh +100 -12
  34. package/hooks/pre-tool/gate-dep-install.sh +42 -1
  35. package/hooks/pre-tool/guard-protected-paths.sh +18 -3
  36. package/package.json +6 -1
  37. package/rules/50-collaboration.md +4 -1
  38. package/scripts/somi-check.sh +135 -0
  39. package/scripts/somi-findings.sh +173 -0
  40. package/scripts/somi-loop.sh +225 -0
  41. package/scripts/validate.sh +115 -0
  42. package/templates/ATLAS.md.tmpl +75 -0
  43. package/templates/BRIEF.md.tmpl +20 -4
  44. package/templates/DOD.md.tmpl +2 -2
  45. package/templates/PHASE.md.tmpl +1 -1
  46. package/templates/RCA.md.tmpl +53 -0
  47. package/templates/RD-README.md.tmpl +2 -2
  48. package/templates/SDD.md.tmpl +4 -4
  49. package/templates/SOMI-README.md.tmpl +20 -3
  50. package/templates/SRS.md.tmpl +1 -1
  51. package/templates/TDD.md.tmpl +5 -5
  52. package/tests/hooks/cases/block-dangerous-bash.json +78 -0
  53. package/tests/hooks/cases/block-secret-writes.json +22 -0
  54. package/tests/hooks/cases/gate-dep-install.json +37 -0
  55. package/tests/hooks/cases/guard-protected-paths.json +30 -0
  56. package/tests/hooks/run.sh +97 -0
  57. package/tests/scripts/run.sh +168 -0
@@ -44,6 +44,19 @@ somi::audit_log_path() {
44
44
  printf '%s' "$log"
45
45
  }
46
46
 
47
+ # Read a value from the project's committed config at .somi/config.json.
48
+ # Usage: somi::config '.dep_install.allow[]?' → prints the jq result (or nothing).
49
+ # Precedence contract (callers enforce it): env var > .somi/config.json > default.
50
+ # Same unexpanded-variable guard as somi::audit_log_path.
51
+ somi::config() {
52
+ local path="$1"
53
+ local base="${CLAUDE_PROJECT_DIR:-$PWD}"
54
+ [[ "$base" == *'${'* ]] && base="$PWD"
55
+ local cfg="$base/.somi/config.json"
56
+ [[ -f "$cfg" ]] || return 0
57
+ jq -r "$path // empty" "$cfg" 2>/dev/null || true
58
+ }
59
+
47
60
  # Append a structured line to the audit log.
48
61
  somi::audit() {
49
62
  local kind="$1"
@@ -4,8 +4,14 @@
4
4
  # This is a deterministic guardrail, not a policy debate. It catches the
5
5
  # common-and-catastrophic class of mistakes; nuanced cases are the human's call.
6
6
  #
7
- # Block list focuses on irreversible / system-destructive / supply-chain shapes.
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.
8
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.
9
15
 
10
16
  set -euo pipefail
11
17
 
@@ -18,6 +24,14 @@ somi::read_payload
18
24
  CMD="$(somi::field '.tool_input.command')"
19
25
  [[ -z "$CMD" ]] && exit 0
20
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
+
21
35
  # Case-sensitive patterns (filesystem paths, exact tools, exact flags).
22
36
  DANGEROUS_PATTERNS=(
23
37
  # filesystem nukes
@@ -42,6 +56,9 @@ DANGEROUS_PATTERNS=(
42
56
  # Covers --force, -f, --force-with-lease (with or without =value), and refspec form (origin HEAD:main).
43
57
  'git[[:space:]]+push[[:space:]]+(-{1,2}force|-f)([[:space:]=]|$).*[[:space:]:](main|master|trunk|release)([[:space:]]|$)'
44
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:]]|$)'
45
62
  'git[[:space:]]+branch[[:space:]]+-D[[:space:]]+(main|master|trunk)'
46
63
  'git[[:space:]]+reset[[:space:]]+--hard[[:space:]]+(origin/)?(main|master|trunk)'
47
64
  'git[[:space:]]+clean[[:space:]]+-[fdx]+[[:space:]]'
@@ -64,20 +81,91 @@ DANGEROUS_PATTERNS_NOCASE=(
64
81
  'DELETE[[:space:]]+FROM[[:space:]]+[a-zA-Z_]+[[:space:]]*;'
65
82
  )
66
83
 
67
- for pattern in "${DANGEROUS_PATTERNS[@]}"; do
68
- if [[ "$CMD" =~ $pattern ]]; then
69
- somi::deny_pretool "somi refused this command: it matches a dangerous-shell pattern (\`${BASH_REMATCH[0]}\`).
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]}\`).
70
102
  If this is genuinely intended, stop and ask the human to run it themselves — never work around this hook silently."
71
- fi
72
- done
103
+ fi
104
+ done
73
105
 
74
- shopt -s nocasematch
75
- for pattern in "${DANGEROUS_PATTERNS_NOCASE[@]}"; do
76
- if [[ "$CMD" =~ $pattern ]]; then
77
- somi::deny_pretool "somi refused this command: it matches a destructive-SQL pattern (\`${BASH_REMATCH[0]}\`).
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]}\`).
78
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."
79
163
  fi
80
- done
81
- shopt -u nocasematch
164
+ }
165
+
166
+ check_dangerous "$CMD"
167
+ check_dangerous "$CMD_STRIPPED"
168
+ check_secret_writes "$CMD_STRIPPED"
169
+ check_bare_force_push "$CMD_STRIPPED"
82
170
 
83
171
  exit 0
@@ -27,6 +27,42 @@ if [[ "${SOMI_ALLOW_DEP_INSTALL:-0}" == "1" ]]; then
27
27
  exit 0
28
28
  fi
29
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
+
30
66
  # Patterns for "add a new dependency". A trailing package argument is required;
31
67
  # bare `<pm> install` (no package) is the lockfile-respecting form and is fine.
32
68
  DEP_ADD_PATTERNS=(
@@ -60,10 +96,15 @@ DEP_ADD_PATTERNS=(
60
96
 
61
97
  for pattern in "${DEP_ADD_PATTERNS[@]}"; do
62
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
63
103
  somi::deny_pretool "somi refused this command: it would add a new dependency (\`${BASH_REMATCH[0]}\`).
64
104
  Adding a dep is a decision — record it in \`.somi/plans/<slug>/decisions.md\` (or surface it in
65
105
  the iteration summary for the human) and re-run with \`SOMI_ALLOW_DEP_INSTALL=1\` in the
66
- environment for this session, or have the human run the install themselves.
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\`.
67
108
  Lockfile-respecting reinstalls (bare \`npm install\` / \`pip install -r requirements.txt\`-less,
68
109
  \`bundle install\`, etc.) are allowed without acknowledgement."
69
110
  fi
@@ -20,6 +20,15 @@ TOOL="$(somi::field '.tool_name')"
20
20
  PATH_INPUT="$(somi::field '.tool_input.file_path')"
21
21
  [[ -z "$PATH_INPUT" ]] && exit 0
22
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
+
23
32
  PROTECTED_GLOBS=(
24
33
  # Source-control internals.
25
34
  '*/.git/*'
@@ -60,13 +69,19 @@ These paths are owned by tooling (git / package managers / SOMI itself); changes
60
69
  fi
61
70
  done
62
71
 
63
- # Lockfile gate.
64
- if [[ "${SOMI_ALLOW_LOCKFILES:-0}" != "1" ]]; then
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
65
79
  BASENAME="$(basename "$PATH_INPUT")"
66
80
  for lock in "${LOCKFILES[@]}"; do
67
81
  if [[ "$BASENAME" == "$lock" ]]; then
68
82
  somi::deny_pretool "somi refused to ${TOOL} \`${PATH_INPUT}\`: lockfiles should be regenerated by the package manager, not hand-edited.
69
- If you genuinely need to hand-edit this lockfile, set SOMI_ALLOW_LOCKFILES=1 in your environment for this session."
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)."
70
85
  fi
71
86
  done
72
87
  fi
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@skathio/somi",
3
- "version": "1.1.1",
3
+ "version": "1.2.0",
4
4
  "scripts": {
5
5
  "test": "bash scripts/validate.sh"
6
6
  },
7
+ "bin": {
8
+ "somi-check": "scripts/somi-check.sh"
9
+ },
7
10
  "description": "SoMi — multi-agent engineering workflow system: plan, code, review — with SOLID/OWASP guardrails, deterministic hooks, and a global ruleset.",
8
11
  "keywords": [
9
12
  "workflow",
@@ -42,6 +45,8 @@
42
45
  "hooks/",
43
46
  "templates/",
44
47
  "examples/",
48
+ "scripts/",
49
+ "tests/",
45
50
  ".claude-plugin/",
46
51
  ".copilot-extension/",
47
52
  ".claude/",
@@ -18,7 +18,10 @@ How to work with humans, and how agents hand off to each other inside SOMI.
18
18
  architectural choice shapes the work, present 2–4 concrete options with **specific** pros and
19
19
  cons (no vague phrasings), recommend one, and always offer two escape hatches: **Other** (the
20
20
  user proposes a custom option) and **Discover** (you ask narrowing questions to guide the
21
- choice). Record verified decisions in `decisions.md`.
21
+ choice). Record verified decisions in `decisions.md`. When you are running as a Tasked
22
+ subagent that cannot reach the user, **return the decisions for the orchestrator to present**
23
+ (the batch round-trip in `agents/planner.md`) — never answer them yourself and mark them
24
+ verified.
22
25
  - **Stop asking, start showing** when ambiguity is small. Don't ping the human for every
23
26
  micro-decision; pick the most reasonable default for non-architectural calls and call it out.
24
27
 
@@ -0,0 +1,135 @@
1
+ #!/usr/bin/env bash
2
+ # somi-check.sh — host-agnostic working-tree guard (the portable enforcement layer).
3
+ #
4
+ # SoMi's deterministic hooks only run inside Claude Code. This script carries the
5
+ # working-tree subset of those guarantees to every other host: run it as a git
6
+ # pre-commit hook, a CI step, or via `npx` on GitHub Copilot setups — the
7
+ # environments where the tool-call-time hooks don't exist.
8
+ #
9
+ # Checks (each maps to a hook-layer guarantee):
10
+ # 1. Staged secret-bearing files (block-secret-writes' basename patterns)
11
+ # 2. Staged lockfile hand-edits (guard-protected-paths' lockfile gate;
12
+ # honors .somi/config.json lockfiles.allow_edit and SOMI_ALLOW_LOCKFILES)
13
+ # 3. TODO(claude)/FIXME(claude) markers staged for commit (the loose-end nudge)
14
+ # 4. Scratch/backup files staged (.bak/.orig/scratch_ — the same nudge)
15
+ #
16
+ # Usage:
17
+ # scripts/somi-check.sh [--staged|--all] (default: --staged; --all scans the
18
+ # full working tree vs HEAD)
19
+ # Exit codes: 0 clean · 1 findings (fail the commit / CI step) · 64 error.
20
+ #
21
+ # Install as a pre-commit hook:
22
+ # ln -s ../../<path-to-somi>/scripts/somi-check.sh .git/hooks/pre-commit
23
+ # Or as a CI step:
24
+ # - run: bash <path-to-somi>/scripts/somi-check.sh --all
25
+ #
26
+ # Tested by tests/scripts/run.sh (wired into scripts/validate.sh / CI).
27
+
28
+ set -euo pipefail
29
+
30
+ die() { echo "somi-check: $*" >&2; exit 64; }
31
+ command -v git >/dev/null 2>&1 || die "requires git"
32
+
33
+ MODE="staged"
34
+ case "${1:-}" in
35
+ --all) MODE="all" ;;
36
+ --staged|"") ;;
37
+ *) die "unknown argument: $1" ;;
38
+ esac
39
+
40
+ ROOT="${CLAUDE_PROJECT_DIR:-$PWD}"
41
+ [[ "$ROOT" == *'${'* ]] && ROOT="$PWD"
42
+ git -C "$ROOT" rev-parse --git-dir >/dev/null 2>&1 || die "not a git repository: $ROOT"
43
+
44
+ changed_files() {
45
+ if [[ "$MODE" == "staged" ]]; then
46
+ git -C "$ROOT" diff --cached --name-only --diff-filter=ACM
47
+ else
48
+ git -C "$ROOT" diff HEAD --name-only --diff-filter=ACM
49
+ git -C "$ROOT" ls-files --others --exclude-standard
50
+ fi
51
+ }
52
+
53
+ changed_content() { # additions only
54
+ if [[ "$MODE" == "staged" ]]; then
55
+ git -C "$ROOT" diff --cached --no-color --unified=0
56
+ else
57
+ git -C "$ROOT" diff HEAD --no-color --unified=0
58
+ fi
59
+ }
60
+
61
+ # Mirrors block-secret-writes.sh's basename patterns (keep in sync when extending).
62
+ SECRET_PATTERNS=(
63
+ '^\.env$' '^\.env\.local$' '^\.env\.production$' '^\.env\.prod$' '^\.env\.staging$' '^\.env\.secret$'
64
+ '\.pem$' '\.key$' '\.p12$' '\.pfx$' '\.jks$'
65
+ '^id_rsa$' '^id_ed25519$' '^id_ecdsa$' '^id_dsa$'
66
+ '-key\.json$' '-credentials\.json$' 'service-account.*\.json$'
67
+ '\.netrc$' '\.pgpass$' '\.kdbx$' 'secrets?\.ya?ml$' 'secrets?\.json$'
68
+ )
69
+ EXAMPLE_BASENAMES=('.env.example' '.env.sample' '.env.template' '.env.dist')
70
+
71
+ LOCKFILES=(package-lock.json yarn.lock pnpm-lock.yaml Cargo.lock Gemfile.lock poetry.lock uv.lock composer.lock go.sum)
72
+
73
+ lockfiles_allowed() {
74
+ # env wins (including =0); then committed config; default deny.
75
+ if [[ -n "${SOMI_ALLOW_LOCKFILES:-}" ]]; then
76
+ [[ "$SOMI_ALLOW_LOCKFILES" == "1" ]]
77
+ return
78
+ fi
79
+ local cfg="$ROOT/.somi/config.json"
80
+ [[ -f "$cfg" ]] && [[ "$(jq -r '.lockfiles.allow_edit // empty' "$cfg" 2>/dev/null)" == "true" ]]
81
+ }
82
+
83
+ findings=0
84
+ report() { echo "somi-check: $1" >&2; findings=$((findings + 1)); }
85
+
86
+ # 1 + 2 + 4 — file-name checks.
87
+ while IFS= read -r f; do
88
+ [[ -z "$f" ]] && continue
89
+ base="$(basename "$f")"
90
+
91
+ example=0
92
+ for ex in "${EXAMPLE_BASENAMES[@]}"; do
93
+ [[ "$base" == "$ex" ]] && { example=1; break; }
94
+ done
95
+ if (( ! example )); then
96
+ for p in "${SECRET_PATTERNS[@]}"; do
97
+ if [[ "$base" =~ $p ]]; then
98
+ report "secret-bearing file in the change set: $f (commit only .env.example-style templates)"
99
+ break
100
+ fi
101
+ done
102
+ fi
103
+
104
+ if ! lockfiles_allowed; then
105
+ for l in "${LOCKFILES[@]}"; do
106
+ if [[ "$base" == "$l" ]]; then
107
+ # Hand-edit heuristic: a lockfile changing without its manifest alongside.
108
+ dir="$(dirname "$f")"
109
+ prefix=""
110
+ [[ "$dir" != "." ]] && prefix="${dir}/"
111
+ if ! changed_files | grep -qE "^${prefix}(package\.json|Cargo\.toml|Gemfile|pyproject\.toml|composer\.json|go\.mod)$"; then
112
+ report "lockfile changed without its manifest: $f (regenerate via the package manager, or set lockfiles.allow_edit in .somi/config.json)"
113
+ fi
114
+ break
115
+ fi
116
+ done
117
+ fi
118
+
119
+ case "$base" in
120
+ *.bak|*.orig|scratch_*)
121
+ report "scratch/backup file in the change set: $f"
122
+ ;;
123
+ esac
124
+ done < <(changed_files)
125
+
126
+ # 3 — added TODO(claude)/FIXME(claude) markers.
127
+ if changed_content | grep -E '^\+.*(TODO\(claude\)|TODO\(agent\)|FIXME\(claude\))' -q; then
128
+ report "TODO(claude)/FIXME(claude) markers added — resolve them or convert to owned follow-ups before committing"
129
+ fi
130
+
131
+ if (( findings > 0 )); then
132
+ echo "somi-check: $findings finding(s) — see above." >&2
133
+ exit 1
134
+ fi
135
+ echo "somi-check: clean."
@@ -0,0 +1,173 @@
1
+ #!/usr/bin/env bash
2
+ # somi-findings.sh — the findings ledger: identity and lifecycle for review findings.
3
+ #
4
+ # Review findings used to live only as prose in per-review markdown files. The
5
+ # loop circuit breakers matched recurrences from conversation memory (lost on
6
+ # session death), and a re-review had no way to assert "is F-3 fixed?" — it
7
+ # rediscovered everything. This ledger gives each finding a stable ID and a
8
+ # status, computed mechanically on a stable locus (file + symbol + normalized
9
+ # title), so:
10
+ # - /code-loop's circuit breaker fires on a CONSECUTIVE-pass recurrence,
11
+ # - /ship-loop's cross-layer breaker fires on a CROSS-RUN recurrence,
12
+ # - /review starts by re-checking open findings instead of starting blind,
13
+ # - progress.md follow-ups can reference F-<n> instead of prose.
14
+ #
15
+ # Ledger: .somi/reviews/<slug>/findings.json — committed with the other review
16
+ # artifacts (it is the machine view; the markdown review file stays the human
17
+ # view; the same command writes both in the same step).
18
+ #
19
+ # Subcommands:
20
+ # record --slug S [--review FILE] [--run ID] [--pass N]
21
+ # stdin: JSON array [{file, symbol, title, severity, confidence}, …]
22
+ # Upserts each finding: new locus → new F-<n>; known OPEN locus →
23
+ # appends a sighting. Prints one JSON line per finding with
24
+ # {id, state: "new"|"known", recurring_consecutive, recurring_cross_run}.
25
+ # Exit 5 if ANY finding is recurring_consecutive (the in-loop circuit
26
+ # breaker signal) — the caller decides what to do with it.
27
+ # resolve --slug S --id F-3 --status fixed|accepted|wontfix [--by REVIEW]
28
+ # reopen --slug S --id F-3 [--by REVIEW]
29
+ # open --slug S → JSON array of open findings
30
+ # get --slug S --id F-3 → one finding
31
+ #
32
+ # Locus matching: same file + same symbol (case-insensitive) + same normalized
33
+ # title (lowercased, non-alphanumerics collapsed, first 8 words). Line numbers
34
+ # are deliberately NOT part of the locus — lines drift between passes.
35
+ #
36
+ # Exit codes: 0 ok · 5 consecutive recurrence detected (record only) · 64 error.
37
+ # Tested by tests/scripts/run.sh (wired into scripts/validate.sh / CI).
38
+
39
+ set -euo pipefail
40
+
41
+ die() { echo "somi-findings: $*" >&2; exit 64; }
42
+ command -v jq >/dev/null 2>&1 || die "requires jq"
43
+
44
+ project_root() {
45
+ local b="${CLAUDE_PROJECT_DIR:-$PWD}"
46
+ [[ "$b" == *'${'* ]] && b="$PWD"
47
+ printf '%s' "$b"
48
+ }
49
+
50
+ CMD="${1:-}"; shift || true
51
+ SLUG="" REVIEW="" RUN="" PASS=0 ID="" STATUS="" BY=""
52
+ while (( $# > 0 )); do
53
+ case "$1" in
54
+ --slug) SLUG="$2"; shift 2 ;;
55
+ --review) REVIEW="$2"; shift 2 ;;
56
+ --run) RUN="$2"; shift 2 ;;
57
+ --pass) PASS="$2"; shift 2 ;;
58
+ --id) ID="$2"; shift 2 ;;
59
+ --status) STATUS="$2"; shift 2 ;;
60
+ --by) BY="$2"; shift 2 ;;
61
+ *) die "unknown argument: $1" ;;
62
+ esac
63
+ done
64
+ [[ -n "$CMD" ]] || die "usage: somi-findings.sh <record|resolve|reopen|open|get> --slug <slug> …"
65
+ [[ -n "$SLUG" ]] || die "--slug is required"
66
+
67
+ LEDGER_DIR="$(project_root)/.somi/reviews/$SLUG"
68
+ LEDGER="$LEDGER_DIR/findings.json"
69
+
70
+ ensure_ledger() {
71
+ mkdir -p "$LEDGER_DIR"
72
+ [[ -f "$LEDGER" ]] || printf '{"next_id": 1, "findings": []}\n' > "$LEDGER"
73
+ }
74
+
75
+ normalize_title() {
76
+ printf '%s' "$1" \
77
+ | tr '[:upper:]' '[:lower:]' \
78
+ | tr -cs 'a-z0-9' ' ' \
79
+ | awk '{ for (i = 1; i <= NF && i <= 8; i++) printf "%s%s", (i > 1 ? " " : ""), $i }'
80
+ }
81
+
82
+ case "$CMD" in
83
+ record)
84
+ ensure_ledger
85
+ input="$(cat)"
86
+ count="$(jq 'length' <<<"$input")" || die "stdin must be a JSON array of findings"
87
+ now="$(date -u +%Y-%m-%d)"
88
+ breaker=0
89
+ for ((i = 0; i < count; i++)); do
90
+ f="$(jq -c ".[$i]" <<<"$input")"
91
+ file="$(jq -r '.file // empty' <<<"$f")"
92
+ symbol="$(jq -r '.symbol // ""' <<<"$f" | tr '[:upper:]' '[:lower:]')"
93
+ title="$(jq -r '.title // empty' <<<"$f")"
94
+ [[ -n "$file" && -n "$title" ]] || die "finding $i needs at least {file, title}"
95
+ key="${file}|${symbol}|$(normalize_title "$title")"
96
+
97
+ existing="$(jq -r --arg k "$key" \
98
+ '[.findings[] | select(.key == $k and .status == "open")][0].id // empty' "$LEDGER")"
99
+
100
+ if [[ -n "$existing" ]]; then
101
+ # Known open finding: recurrence classification BEFORE appending the sighting.
102
+ recurring_consecutive="$(jq -r --arg id "$existing" --arg run "$RUN" --argjson p "$PASS" \
103
+ '[.findings[] | select(.id == $id) | .seen[]
104
+ | select(.run == $run and .pass == ($p - 1))] | length > 0' "$LEDGER")"
105
+ recurring_cross="$(jq -r --arg id "$existing" --arg run "$RUN" \
106
+ '[.findings[] | select(.id == $id) | .seen[] | select(.run != $run)] | length > 0' "$LEDGER")"
107
+ tmp="$(mktemp)"
108
+ jq --arg id "$existing" --arg rev "$REVIEW" --arg run "$RUN" \
109
+ --argjson p "$PASS" --arg d "$now" \
110
+ '(.findings[] | select(.id == $id) | .seen) += [{review: $rev, run: $run, pass: $p, date: $d}]' \
111
+ "$LEDGER" > "$tmp" && mv "$tmp" "$LEDGER"
112
+ [[ "$recurring_consecutive" == "true" ]] && breaker=1
113
+ jq -nc --arg id "$existing" --argjson rc "$recurring_consecutive" --argjson rx "$recurring_cross" \
114
+ '{id: $id, state: "known", recurring_consecutive: $rc, recurring_cross_run: $rx}'
115
+ else
116
+ new_id="F-$(jq -r '.next_id' "$LEDGER")"
117
+ tmp="$(mktemp)"
118
+ jq --arg id "$new_id" --arg key "$key" --argjson f "$f" \
119
+ --arg rev "$REVIEW" --arg run "$RUN" --argjson p "$PASS" --arg d "$now" \
120
+ '.next_id += 1
121
+ | .findings += [{
122
+ id: $id, key: $key,
123
+ locus: {file: $f.file, symbol: ($f.symbol // "")},
124
+ title: $f.title,
125
+ severity: ($f.severity // "Minor"),
126
+ confidence: ($f.confidence // "Medium"),
127
+ status: "open",
128
+ introduced_by: $rev, resolved_by: null,
129
+ seen: [{review: $rev, run: $run, pass: $p, date: $d}]
130
+ }]' "$LEDGER" > "$tmp" && mv "$tmp" "$LEDGER"
131
+ jq -nc --arg id "$new_id" \
132
+ '{id: $id, state: "new", recurring_consecutive: false, recurring_cross_run: false}'
133
+ fi
134
+ done
135
+ (( breaker )) && exit 5
136
+ exit 0
137
+ ;;
138
+
139
+ resolve)
140
+ ensure_ledger
141
+ [[ -n "$ID" && -n "$STATUS" ]] || die "resolve requires --id and --status"
142
+ case "$STATUS" in fixed|accepted|wontfix) ;; *) die "--status must be fixed|accepted|wontfix" ;; esac
143
+ tmp="$(mktemp)"
144
+ jq --arg id "$ID" --arg s "$STATUS" --arg by "$BY" \
145
+ '(.findings[] | select(.id == $id)) |= (.status = $s | .resolved_by = $by)' \
146
+ "$LEDGER" > "$tmp" && mv "$tmp" "$LEDGER"
147
+ jq -c --arg id "$ID" '.findings[] | select(.id == $id) | {id, status, resolved_by}' "$LEDGER"
148
+ ;;
149
+
150
+ reopen)
151
+ ensure_ledger
152
+ [[ -n "$ID" ]] || die "reopen requires --id"
153
+ tmp="$(mktemp)"
154
+ jq --arg id "$ID" --arg by "$BY" \
155
+ '(.findings[] | select(.id == $id)) |= (.status = "open" | .resolved_by = null | .reopened_by = $by)' \
156
+ "$LEDGER" > "$tmp" && mv "$tmp" "$LEDGER"
157
+ jq -c --arg id "$ID" '.findings[] | select(.id == $id) | {id, status}' "$LEDGER"
158
+ ;;
159
+
160
+ open)
161
+ ensure_ledger
162
+ jq '[.findings[] | select(.status == "open")
163
+ | {id, locus, title, severity, confidence, introduced_by}]' "$LEDGER"
164
+ ;;
165
+
166
+ get)
167
+ ensure_ledger
168
+ [[ -n "$ID" ]] || die "get requires --id"
169
+ jq --arg id "$ID" '.findings[] | select(.id == $id)' "$LEDGER"
170
+ ;;
171
+
172
+ *) die "unknown subcommand: $CMD" ;;
173
+ esac