@skathio/somi 1.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.
Files changed (84) hide show
  1. package/.claude/settings.json +108 -0
  2. package/.claude/settings.local.json +9 -0
  3. package/.claude/somi-state/last-context-signature +1 -0
  4. package/.claude-plugin/marketplace.json +21 -0
  5. package/.claude-plugin/plugin.json +24 -0
  6. package/.copilot-extension/extension.json +51 -0
  7. package/.copilot-extension/marketplace.json +19 -0
  8. package/LICENSE +21 -0
  9. package/README.md +209 -0
  10. package/VERSION +1 -0
  11. package/agents/architecture-reviewer.md +86 -0
  12. package/agents/coder.md +222 -0
  13. package/agents/discovery-analyst.md +305 -0
  14. package/agents/planner.md +256 -0
  15. package/agents/refactorer.md +72 -0
  16. package/agents/reviewer.md +233 -0
  17. package/agents/security-reviewer.md +85 -0
  18. package/agents/test-strategist.md +74 -0
  19. package/commands/architecture-review.md +92 -0
  20. package/commands/code-loop.md +142 -0
  21. package/commands/code-parallel.md +155 -0
  22. package/commands/code.md +138 -0
  23. package/commands/discover.md +175 -0
  24. package/commands/plan-loop.md +124 -0
  25. package/commands/plan.md +174 -0
  26. package/commands/refactor.md +44 -0
  27. package/commands/review-panel.md +123 -0
  28. package/commands/review.md +149 -0
  29. package/commands/security-review.md +73 -0
  30. package/commands/ship-loop.md +135 -0
  31. package/commands/ship.md +108 -0
  32. package/commands/test-strategy.md +82 -0
  33. package/examples/README.md +19 -0
  34. package/examples/code-review-example.md +88 -0
  35. package/examples/discovery-example.md +200 -0
  36. package/examples/feature-plan-example.md +377 -0
  37. package/examples/full-pipeline-example.md +188 -0
  38. package/examples/sample-consumer/.claude/settings.json +29 -0
  39. package/examples/sample-consumer/CLAUDE.md +41 -0
  40. package/examples/sample-consumer/README.md +77 -0
  41. package/hooks/hooks.json +64 -0
  42. package/hooks/lib/common.sh +120 -0
  43. package/hooks/post-tool/audit-log.sh +40 -0
  44. package/hooks/post-tool/lint-changed-files.sh +80 -0
  45. package/hooks/pre-tool/block-dangerous-bash.sh +83 -0
  46. package/hooks/pre-tool/block-secret-writes.sh +83 -0
  47. package/hooks/pre-tool/gate-dep-install.sh +72 -0
  48. package/hooks/pre-tool/guard-protected-paths.sh +74 -0
  49. package/hooks/user-prompt-submit/inject-workflow-context.sh +143 -0
  50. package/package.json +51 -0
  51. package/rules/00-priorities.md +48 -0
  52. package/rules/10-solid.md +87 -0
  53. package/rules/20-clean-code.md +62 -0
  54. package/rules/30-security-owasp.md +83 -0
  55. package/rules/40-engineering-practices.md +65 -0
  56. package/rules/50-collaboration.md +108 -0
  57. package/rules/99-overrides.md +52 -0
  58. package/rules/CLAUDE.md +164 -0
  59. package/skills/api-design/SKILL.md +147 -0
  60. package/skills/clean-code/SKILL.md +142 -0
  61. package/skills/market-research/SKILL.md +125 -0
  62. package/skills/observability/SKILL.md +133 -0
  63. package/skills/owasp-defense/SKILL.md +141 -0
  64. package/skills/requirements-engineering/SKILL.md +137 -0
  65. package/skills/solid-principles/SKILL.md +143 -0
  66. package/skills/test-strategy/SKILL.md +133 -0
  67. package/skills/threat-modeling/SKILL.md +117 -0
  68. package/templates/ADR.md.tmpl +73 -0
  69. package/templates/BRD.md.tmpl +68 -0
  70. package/templates/CONTEXT.md.tmpl +74 -0
  71. package/templates/DECISIONS.md.tmpl +104 -0
  72. package/templates/DIARY.md.tmpl +65 -0
  73. package/templates/DOD.md.tmpl +66 -0
  74. package/templates/FRD.md.tmpl +64 -0
  75. package/templates/PHASE.md.tmpl +80 -0
  76. package/templates/PROGRESS.md.tmpl +76 -0
  77. package/templates/RD-README.md.tmpl +70 -0
  78. package/templates/RESEARCH.md.tmpl +96 -0
  79. package/templates/REVIEW.md.tmpl +81 -0
  80. package/templates/SDD.md.tmpl +82 -0
  81. package/templates/SOMI-README.md.tmpl +66 -0
  82. package/templates/SPEC.md.tmpl +123 -0
  83. package/templates/SRS.md.tmpl +85 -0
  84. package/templates/TDD.md.tmpl +77 -0
@@ -0,0 +1,188 @@
1
+ # Example — `/ship` end-to-end pipeline
2
+
3
+ A condensed transcript of using `/ship` for a tiny feature: **add a `--dry-run` flag to the
4
+ `migrate` CLI**. Shows the human-in-the-loop gates between stages, the inline verification of
5
+ architectural decisions during planning, and how the `.somi/plans/<slug>/` artifact set gets populated.
6
+
7
+ ---
8
+
9
+ ## Turn 1 — User invokes `/ship`
10
+
11
+ ```
12
+ > /ship Add a --dry-run flag to the migrate CLI that prints the SQL it would execute without
13
+ applying any changes. Should work for both up and down migrations.
14
+ ```
15
+
16
+ ## Turn 2 — Claude (Stage 1: Plan — slug and scaffold)
17
+
18
+ > Proposed slug: `migrate-dry-run`. Creating `.somi/plans/migrate-dry-run/` …
19
+ >
20
+ > [scaffolds `context.md`, `spec.md`, `decisions.md`, `progress.md`, `diary.md`, `phases/`,
21
+ > `reviews/`]
22
+ >
23
+ > Reading `cmd/migrate/main.go`, `internal/migrate/runner.go`, existing tests. Drafting
24
+ > `context.md` and the spec skeleton.
25
+
26
+ ## Turn 3 — Claude (Stage 1: Plan — first verification)
27
+
28
+ > One architectural decision to verify before I write the phases:
29
+ >
30
+ > **Decision: shape of the dry-run option on `Runner`.**
31
+ >
32
+ > **Option A — `Options.DryRun bool`** — **Recommended**
33
+ > - Pros: minimal API surface; matches existing `Options.{Verbose, RetryCount}` style; one place
34
+ > to read at the gate.
35
+ > - Cons: closes the door on future modes (`explain`, `verify`) without an additive change later.
36
+ >
37
+ > **Option B — `Options.Mode runner.Mode`** (typed enum: `apply` / `dryRun`)
38
+ > - Pros: leaves room for `explain` / `verify` modes by adding enum variants instead of new bools;
39
+ > makes the gate exhaustive at compile time.
40
+ > - Cons: more code today for a one-mode addition; requires a default value when callers don't
41
+ > supply one.
42
+ >
43
+ > **Other** — describe a different option.
44
+ > **Discover** — I'll ask narrowing questions.
45
+ >
46
+ > My recommendation: Option A. There's no concrete pull for `explain` / `verify` yet; we can
47
+ > migrate to an enum if and when we add a second alternative mode.
48
+
49
+ ## Turn 4 — User
50
+
51
+ ```
52
+ > A
53
+ ```
54
+
55
+ ## Turn 5 — Claude (Stage 1: Plan — finishing)
56
+
57
+ > Recorded as D1 in `decisions.md` (Verified with user: yes).
58
+ >
59
+ > Writing `phases/01-thread-flag.md`, `phases/02-tests-docs.md`. Updating `spec.md` §5 with the
60
+ > one-liner for D1. Initial `progress.md` written (status `awaiting-approval`). First diary entry
61
+ > appended.
62
+ >
63
+ > **Summary**: Two iterations under one phase. (1) Thread `--dry-run` through CLI and
64
+ > `Runner.Apply` using `Options.DryRun bool`. (2) Add tests + docs.
65
+ >
66
+ > **Risks**: (a) printing SQL with secrets — none of our migrations embed secrets, but worth a
67
+ > one-line redaction pass; (b) ordering — dry-run must still resolve dependencies in the same
68
+ > order as the real apply.
69
+ >
70
+ > **Open questions**: none.
71
+ >
72
+ > Plan ready under `.somi/plans/migrate-dry-run/`. Reply `approve` to proceed to Stage 2, `revise
73
+ > <notes>` to iterate on the plan, or `abort` to stop.
74
+
75
+ ## Turn 6 — User
76
+
77
+ ```
78
+ > approve
79
+ ```
80
+
81
+ ## Turn 7 — Claude (Stage 2: Code, Iteration 1.1)
82
+
83
+ > Marking phase 1, iteration 1 `in-progress` in `progress.md`. Reading the relevant files.
84
+ >
85
+ > [edits `cmd/migrate/main.go` to register the flag and pass it]
86
+ > [edits `internal/migrate/runner.go` to add `DryRun bool` to options and short-circuit `Exec` to
87
+ > `fmt.Println`]
88
+ >
89
+ > Tests green. Marking iteration 1.1 `done`. Updated `progress.md`. Diary entry added.
90
+ >
91
+ > **Work item**: `migrate-dry-run` • iteration 1.1.
92
+ > **What changed**:
93
+ > - `cmd/migrate/main.go` — registered `--dry-run` flag; passed through to `Runner.Apply`.
94
+ > - `internal/migrate/runner.go` — added `Options.DryRun`; in dry-run mode, prints `BEGIN; <SQL>;
95
+ > COMMIT;` for each migration in dependency order without opening a transaction.
96
+ > **Plan changes**: none.
97
+ > **Why**: matches phase 1 iteration 1 acceptance.
98
+ > **Not done**: tests + docs — planned for iteration 1.2.
99
+ > **What to look at**: the dependency-resolution path in `runner.go:142-158` — same code path as
100
+ > real apply (which is the whole point); if you re-order migrations, dry-run output reorders too.
101
+ > **Tradeoffs taken**: none material.
102
+ >
103
+ > Iteration 1.1 implemented. Reply `review` to invoke the reviewer on this iteration, `next` to
104
+ > proceed to the next iteration without a review, or `stop`.
105
+
106
+ ## Turn 8 — User
107
+
108
+ ```
109
+ > review
110
+ ```
111
+
112
+ ## Turn 9 — Claude (Stage 3: Review)
113
+
114
+ > [reads the diff, `spec.md`, `phases/01-…`, recent diary entries]
115
+ >
116
+ > **Verdict**: `approve-with-comments`.
117
+ > **Counts**: 0 Blockers, 0 Majors, 1 Minor, 1 Nit.
118
+ >
119
+ > **Top findings**:
120
+ > - **[Minor / Medium]** Dry-run prints `BEGIN/COMMIT` per migration, but real apply uses a single
121
+ > transaction per `up` batch. Output suggests semantics that don't quite match real apply.
122
+ > Suggest matching the real shape or noting the divergence in the printed header.
123
+ > - **[Nit / Low]** None material on naming or structure.
124
+ >
125
+ > Plan-vs-code: no drift; iteration scope matched `phases/01-thread-flag.md` exactly.
126
+ >
127
+ > Review file at `.somi/reviews/migrate-dry-run/2026-05-21-iteration-1-1-approve-with-comments.md`.
128
+ > Recent activity logged in `progress.md`. No diary entry needed — finding is a code concern, not
129
+ > a plan concern.
130
+ >
131
+ > Reply `next` to proceed to iteration 1.2 (tests + docs), `revise` to address the Minor first,
132
+ > or `stop`.
133
+
134
+ ## Turn 10 — User
135
+
136
+ ```
137
+ > revise — address the Minor by matching the real transaction shape in dry-run output.
138
+ ```
139
+
140
+ ## Turn 11 — Claude (Stage 2 again, addressing review feedback)
141
+
142
+ > Adjusting iteration 1.1 per review feedback.
143
+ >
144
+ > [edits `internal/migrate/runner.go` so dry-run emits one `BEGIN` at the start of a batch and one
145
+ > `COMMIT` at the end, matching real apply semantics]
146
+ >
147
+ > Done. Re-running review …
148
+ >
149
+ > **Verdict**: `approve` (clean). Review file updated.
150
+ >
151
+ > Reply `next` to proceed to iteration 1.2.
152
+
153
+ ## Turn 12 — User
154
+
155
+ ```
156
+ > next
157
+ ```
158
+
159
+ ## Turn 13 — Claude (Stage 2, Iteration 1.2)
160
+
161
+ > [writes table tests for dry-run output; updates `docs/migrate.md`]
162
+ >
163
+ > ... [final review on iteration 1.2 — clean approve] ...
164
+ >
165
+ > All iterations complete. Setting `progress.md` status to `done`. Final diary entry appended.
166
+ > Work item: `migrate-dry-run`.
167
+ >
168
+ > Artifacts at `.somi/plans/migrate-dry-run/` — full audit trail of the work. Ready for PR.
169
+
170
+ ---
171
+
172
+ ## What the example demonstrates
173
+
174
+ - **Slug + scaffold up front** — every work item gets its own `.somi/plans/<slug>/` directory; the
175
+ six-file artifact set is created from templates before drafting starts.
176
+ - **Inline verification on architectural decisions** — the agent surfaces options with concrete
177
+ pros and cons and offers `Other` / `Discover` escape hatches. The user decides; the choice is
178
+ recorded in `decisions.md`.
179
+ - **Hard gates** between stages — Claude never silently progresses.
180
+ - **One iteration per coding cycle** — even though the plan had two iterations, each one got its
181
+ own code → review loop.
182
+ - **Progress + diary updates** — `progress.md` reflects status; `diary.md` captures notable
183
+ events. If the coder had to change the plan mid-implementation, a diary entry with category
184
+ `plan-change` would have recorded what changed and why.
185
+ - **Reviews are scoped to the work item** — they land under `reviews/` and update `progress.md`.
186
+ A plan-affecting finding would have appended a `review-feedback` diary entry.
187
+ - **Artifacts persist** — `.somi/plans/migrate-dry-run/` is the durable record of the work, auditable
188
+ after the session ends.
@@ -0,0 +1,29 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/claude-code-settings.json",
3
+ "_comment": "Sample post-install settings.json for a project that installed somi as a plugin. Hooks are NOT declared here — Claude Code auto-merges them from the plugin's hooks/hooks.json (paths resolve via ${CLAUDE_PLUGIN_ROOT}). This file holds project-specific env, permissions, and any project-only hooks. If you vendor somi instead of using the plugin marketplace, copy the hooks block from somi's own .claude/settings.json (which uses ${SOMI_VENDOR_ROOT}) and merge it here.",
4
+ "env": {
5
+ "NODE_ENV": "development"
6
+ },
7
+ "permissions": {
8
+ "allow": [
9
+ "Read(**)",
10
+ "Grep(**)",
11
+ "Glob(**)",
12
+ "Bash(npm test:*)",
13
+ "Bash(npm run lint:*)",
14
+ "Bash(npm run typecheck:*)",
15
+ "Bash(git status:*)",
16
+ "Bash(git diff:*)",
17
+ "Bash(git log:*)"
18
+ ],
19
+ "deny": [
20
+ "Read(./.env)",
21
+ "Read(./.env.local)",
22
+ "Read(./.env.production)",
23
+ "Bash(curl * | sh)",
24
+ "Bash(rm -rf /:*)",
25
+ "Bash(git push --force:*)",
26
+ "Bash(git push --force-with-lease:*)"
27
+ ]
28
+ }
29
+ }
@@ -0,0 +1,41 @@
1
+ # sample-consumer — Project instructions
2
+
3
+ This file is the **project's** `CLAUDE.md`, generated by SOMI at install time. It composes the SOMI
4
+ ruleset with project-specific instructions that you add below.
5
+
6
+ ---
7
+
8
+ ## SOMI global ruleset (do not edit)
9
+
10
+ The composed SOMI ruleset is included from [`.claude/rules/CLAUDE.md`](.claude/rules/CLAUDE.md). When
11
+ this `CLAUDE.md` is loaded, Claude Code follows both the SOMI rules and any project rules below.
12
+
13
+ > If you want to override a SOMI default, edit
14
+ > [`.claude/rules/99-overrides.md`](.claude/rules/99-overrides.md) — SOMI will never touch that file.
15
+
16
+ ---
17
+
18
+ ## Project-specific instructions
19
+
20
+ <!-- Add your project's conventions, idioms, and gotchas below. SOMI rules already cover SOLID,
21
+ clean code, OWASP, and engineering practices — focus on what's unique about THIS project. -->
22
+
23
+ ### Stack
24
+
25
+ - Language: TypeScript (Node 20).
26
+ - Framework: Fastify.
27
+ - DB: Postgres via Prisma.
28
+ - Tests: Vitest + Testcontainers for DB integration.
29
+
30
+ ### Project conventions
31
+
32
+ - HTTP error envelope is defined in `src/lib/httperr.ts` — use it everywhere.
33
+ - Domain code lives in `src/domain/`; it must not import from `src/infra/`.
34
+ - All migrations are reversible; the down migration is part of the same PR as the up.
35
+ - Logs are JSON via `pino`; never `console.log` in committed code.
36
+
37
+ ### What goes in PRs
38
+
39
+ - Tests (no exceptions for non-trivial logic).
40
+ - Updated `docs/` if behavior or interfaces changed.
41
+ - A short summary referencing the plan iteration if there was one.
@@ -0,0 +1,77 @@
1
+ # sample-consumer — minimal project consuming somi
2
+
3
+ This directory shows what a project looks like **after** installing SoMi via the Claude Code
4
+ plugin marketplace:
5
+
6
+ ```text
7
+ /plugin marketplace add https://github.com/skathio/somi
8
+ /plugin install somi@somi
9
+ ```
10
+
11
+ It's a layout reference, not a runnable project — there's no application code here, just the
12
+ files SoMi's plugin runtime places.
13
+
14
+ ## What you should see in your own project after install
15
+
16
+ ```
17
+ <your project>/
18
+ ├── CLAUDE.md # composed from rules/CLAUDE.md
19
+ ├── .somi/ # workflow artifacts (created when /discover or /plan runs)
20
+ │ ├── README.md
21
+ │ ├── rd/ # discovery initiatives (created when /discover runs)
22
+ │ │ └── <slug>/ # research-report, brd, srs, frd, sdd, tdd, decisions, diary
23
+ │ ├── plans/
24
+ │ │ └── <slug>/ # one per /plan invocation
25
+ │ │ ├── context.md
26
+ │ │ ├── spec.md
27
+ │ │ ├── decisions.md
28
+ │ │ ├── progress.md
29
+ │ │ ├── diary.md
30
+ │ │ └── phases/
31
+ │ └── reviews/
32
+ │ └── <slug>/ # reviews keyed by work-item slug
33
+ └── .claude/
34
+ ├── settings.json # SoMi hooks wired up (merged with yours if it existed)
35
+ └── plugins/
36
+ └── somi/
37
+ ├── .claude-plugin/plugin.json
38
+ ├── agents/ # discovery-analyst, planner, coder, reviewer + support
39
+ ├── commands/ # /discover, /plan, /code, /review, /ship + support
40
+ ├── skills/ # market-research, requirements-engineering, OWASP, SOLID, ...
41
+ ├── rules/ # global ruleset
42
+ ├── templates/ # context, spec, decisions, phase, progress, diary, review, ADR, DoD; R&D: RD-README, RESEARCH, BRD, SRS, FRD, SDD, TDD
43
+ └── hooks/ # guardrail scripts settings.json points at
44
+ ```
45
+
46
+ ## Things to notice
47
+
48
+ - **`CLAUDE.md` is at the project root**, not under `.claude/`. Claude Code automatically loads it
49
+ as project-level instructions.
50
+ - **Hooks live under `.claude/plugins/somi/hooks/`** and are referenced via `${SOMI_ROOT}` in
51
+ `settings.json` so they work regardless of where the plugin root resolves.
52
+ - **`settings.json` is the merge of your existing settings + SoMi hooks/permissions**. Your
53
+ existing `permissions.allow` is preserved; SoMi hook entries are appended; SoMi deny rules are
54
+ added (union-merge).
55
+
56
+ ## What stays yours after install
57
+
58
+ - `CLAUDE.md` — the plugin runtime does not overwrite a hand-edited `CLAUDE.md`. Add
59
+ project-specific instructions in [`rules/99-overrides.md`](../../rules/99-overrides.md)
60
+ (which SoMi never touches) or directly in your `CLAUDE.md`.
61
+ - All your existing `settings.json` keys outside of `hooks`, `permissions`, and `env`.
62
+ - Everything under `.somi/` — workflow artifacts, not SoMi internals. Work items persist
63
+ indefinitely; only you delete from there.
64
+
65
+ ## Updating
66
+
67
+ ```text
68
+ /plugin update somi
69
+ ```
70
+
71
+ ## Uninstalling
72
+
73
+ ```text
74
+ /plugin uninstall somi
75
+ ```
76
+
77
+ Removes the plugin. Leaves your `CLAUDE.md`, `.somi/` artifacts, and `audit.log` alone.
@@ -0,0 +1,64 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/claude-code-hooks.json",
3
+ "description": "somi deterministic guardrails: dangerous-bash / secret-writes / protected-paths / dep-install gates, lint surfacing, audit logging, workflow context injection.",
4
+ "hooks": {
5
+ "PreToolUse": [
6
+ {
7
+ "matcher": "Bash",
8
+ "hooks": [
9
+ {
10
+ "type": "command",
11
+ "command": "${CLAUDE_PLUGIN_ROOT}/hooks/pre-tool/block-dangerous-bash.sh"
12
+ },
13
+ {
14
+ "type": "command",
15
+ "command": "${CLAUDE_PLUGIN_ROOT}/hooks/pre-tool/gate-dep-install.sh"
16
+ }
17
+ ]
18
+ },
19
+ {
20
+ "matcher": "Write|Edit",
21
+ "hooks": [
22
+ {
23
+ "type": "command",
24
+ "command": "${CLAUDE_PLUGIN_ROOT}/hooks/pre-tool/block-secret-writes.sh"
25
+ },
26
+ {
27
+ "type": "command",
28
+ "command": "${CLAUDE_PLUGIN_ROOT}/hooks/pre-tool/guard-protected-paths.sh"
29
+ }
30
+ ]
31
+ }
32
+ ],
33
+ "PostToolUse": [
34
+ {
35
+ "matcher": "Write|Edit",
36
+ "hooks": [
37
+ {
38
+ "type": "command",
39
+ "command": "${CLAUDE_PLUGIN_ROOT}/hooks/post-tool/lint-changed-files.sh"
40
+ }
41
+ ]
42
+ },
43
+ {
44
+ "matcher": "*",
45
+ "hooks": [
46
+ {
47
+ "type": "command",
48
+ "command": "${CLAUDE_PLUGIN_ROOT}/hooks/post-tool/audit-log.sh"
49
+ }
50
+ ]
51
+ }
52
+ ],
53
+ "UserPromptSubmit": [
54
+ {
55
+ "hooks": [
56
+ {
57
+ "type": "command",
58
+ "command": "${CLAUDE_PLUGIN_ROOT}/hooks/user-prompt-submit/inject-workflow-context.sh"
59
+ }
60
+ ]
61
+ }
62
+ ]
63
+ }
64
+ }
@@ -0,0 +1,120 @@
1
+ #!/usr/bin/env bash
2
+ # Shared helpers for somi hooks.
3
+ #
4
+ # Hooks receive a JSON payload on stdin describing the tool invocation, and may
5
+ # emit a JSON response on stdout to control the harness. Output schema is
6
+ # event-specific (see https://code.claude.com/docs/en/hooks):
7
+ #
8
+ # PreToolUse — { hookSpecificOutput: { hookEventName, permissionDecision, permissionDecisionReason } }
9
+ # PostToolUse — { hookSpecificOutput: { hookEventName, additionalContext } } or { decision, reason }
10
+ # UserPromptSubmit — { hookSpecificOutput: { hookEventName, additionalContext } } or { decision, reason }
11
+ # Stop — { decision: "block", reason } (no additionalContext channel)
12
+ #
13
+ # Helpers below emit the right shape per event. Use them — do not hand-emit
14
+ # legacy `{decision:"block"}` or bare `{additionalContext}` shapes; the harness
15
+ # silently drops the wrong shape for the wrong event.
16
+
17
+ set -euo pipefail
18
+
19
+ # Read the full JSON payload from stdin once. Subsequent jq calls use this.
20
+ somi::read_payload() {
21
+ SOMI_PAYLOAD="$(cat)"
22
+ export SOMI_PAYLOAD
23
+ }
24
+
25
+ # Extract a field from the payload using jq. Returns empty string if missing.
26
+ somi::field() {
27
+ local path="$1"
28
+ printf '%s' "${SOMI_PAYLOAD:-}" | jq -r "${path} // empty" 2>/dev/null || true
29
+ }
30
+
31
+ # Path to the audit log. Project-local by default. Configurable via SOMI_AUDIT_LOG.
32
+ #
33
+ # Guards against UNEXPANDED variables. Some hosts don't expand ${CLAUDE_PROJECT_DIR}
34
+ # inside settings.json's `env` block, so SOMI_AUDIT_LOG (or CLAUDE_PROJECT_DIR) can
35
+ # arrive as the literal string "${CLAUDE_PROJECT_DIR}/.claude/audit.log". Without this
36
+ # guard, `mkdir -p "$(dirname "$log")"` then creates a literal '${CLAUDE_PROJECT_DIR}'
37
+ # directory in the repo root. If a candidate still contains a "${", we discard it and
38
+ # fall back to a path the shell can resolve here.
39
+ somi::audit_log_path() {
40
+ local base="${CLAUDE_PROJECT_DIR:-$PWD}"
41
+ [[ "$base" == *'${'* ]] && base="$PWD"
42
+ local log="${SOMI_AUDIT_LOG:-${base}/.claude/audit.log}"
43
+ [[ "$log" == *'${'* ]] && log="${base}/.claude/audit.log"
44
+ printf '%s' "$log"
45
+ }
46
+
47
+ # Append a structured line to the audit log.
48
+ somi::audit() {
49
+ local kind="$1"
50
+ local detail="$2"
51
+ local tool
52
+ local log
53
+ tool="$(somi::field '.tool_name')"
54
+ log="$(somi::audit_log_path)"
55
+ mkdir -p "$(dirname "$log")"
56
+ printf '%s\t%s\t%s\t%s\n' \
57
+ "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
58
+ "$kind" \
59
+ "${tool:-unknown}" \
60
+ "$detail" \
61
+ >> "$log"
62
+ }
63
+
64
+ # Deny a PreToolUse tool call. Use this for block-* hooks.
65
+ # Emits the modern hookSpecificOutput.permissionDecision schema.
66
+ somi::deny_pretool() {
67
+ local reason="$1"
68
+ jq -nc --arg r "$reason" '{
69
+ hookSpecificOutput: {
70
+ hookEventName: "PreToolUse",
71
+ permissionDecision: "deny",
72
+ permissionDecisionReason: $r
73
+ }
74
+ }'
75
+ somi::audit "DENY" "$reason"
76
+ exit 0
77
+ }
78
+
79
+ # Emit additionalContext for events that support it (PreToolUse, PostToolUse,
80
+ # UserPromptSubmit). The harness shows this to the model on its next turn.
81
+ # First arg is the event name; second is the context string.
82
+ # Stop does NOT support additionalContext — do not call this from a Stop hook.
83
+ somi::context() {
84
+ local event="$1"
85
+ local context="$2"
86
+ jq -nc --arg e "$event" --arg c "$context" '{
87
+ hookSpecificOutput: {
88
+ hookEventName: $e,
89
+ additionalContext: $c
90
+ }
91
+ }'
92
+ }
93
+
94
+ # Test whether a command string matches any extended-regex pattern.
95
+ # Usage: somi::matches_any "$cmd" "pattern1" "pattern2" ...
96
+ somi::matches_any() {
97
+ local cmd="$1"; shift
98
+ local pattern
99
+ for pattern in "$@"; do
100
+ if [[ "$cmd" =~ $pattern ]]; then
101
+ return 0
102
+ fi
103
+ done
104
+ return 1
105
+ }
106
+
107
+ # Case-insensitive variant. Uses nocasematch in a subshell to avoid leaking shell state.
108
+ somi::matches_any_nocase() {
109
+ local cmd="$1"; shift
110
+ local pattern
111
+ (
112
+ shopt -s nocasematch
113
+ for pattern in "$@"; do
114
+ if [[ "$cmd" =~ $pattern ]]; then
115
+ exit 0
116
+ fi
117
+ done
118
+ exit 1
119
+ )
120
+ }
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env bash
2
+ # PostToolUse hook (matcher: *) — record every tool call to the SOMI audit log.
3
+ #
4
+ # Pairs with the BLOCK entries written from pre-tool hooks: gives you a single
5
+ # log to grep for "what did the agent actually do during this session?"
6
+ #
7
+ # Sensitive arguments are trimmed; we record tool name, status, and a short summary.
8
+
9
+ set -euo pipefail
10
+
11
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
12
+ # shellcheck source=../lib/common.sh
13
+ source "$SCRIPT_DIR/../lib/common.sh"
14
+
15
+ somi::read_payload
16
+
17
+ TOOL="$(somi::field '.tool_name')"
18
+ [[ -z "$TOOL" ]] && exit 0
19
+
20
+ SUMMARY=""
21
+ case "$TOOL" in
22
+ Bash)
23
+ CMD="$(somi::field '.tool_input.command' | head -c 240)"
24
+ SUMMARY="cmd=\"${CMD}\""
25
+ ;;
26
+ Write|Edit)
27
+ P="$(somi::field '.tool_input.file_path')"
28
+ SUMMARY="path=\"${P}\""
29
+ ;;
30
+ Read)
31
+ P="$(somi::field '.tool_input.file_path')"
32
+ SUMMARY="path=\"${P}\""
33
+ ;;
34
+ *)
35
+ SUMMARY="$(printf '%s' "$SOMI_PAYLOAD" | jq -c '.tool_input // {}' 2>/dev/null | head -c 240 || true)"
36
+ ;;
37
+ esac
38
+
39
+ somi::audit "CALL" "$SUMMARY"
40
+ exit 0
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/env bash
2
+ # PostToolUse hook (matcher: Write|Edit) — best-effort lint of just-changed files.
3
+ #
4
+ # Runs the project's configured linter on the touched file, if one is available.
5
+ # Result is informational — we never block from a post-tool hook (the file is
6
+ # already written). We surface lint failures back to the model so it can self-correct.
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
+ PATH_INPUT="$(somi::field '.tool_input.file_path')"
17
+ [[ -z "$PATH_INPUT" ]] && exit 0
18
+ [[ ! -f "$PATH_INPUT" ]] && exit 0
19
+
20
+ # Skip oversized files (likely generated).
21
+ SIZE_BYTES=$(wc -c < "$PATH_INPUT" 2>/dev/null || echo 0)
22
+ if (( SIZE_BYTES > 500000 )); then
23
+ exit 0
24
+ fi
25
+
26
+ PROJECT_ROOT="${CLAUDE_PROJECT_DIR:-$PWD}"
27
+
28
+ run_if_present() {
29
+ local cmd="$1"; shift
30
+ if command -v "$cmd" >/dev/null 2>&1; then
31
+ "$cmd" "$@" 2>&1 || true
32
+ fi
33
+ }
34
+
35
+ LINT_OUTPUT=""
36
+
37
+ case "$PATH_INPUT" in
38
+ *.py)
39
+ if [[ -f "$PROJECT_ROOT/pyproject.toml" ]] || [[ -f "$PROJECT_ROOT/.ruff.toml" ]]; then
40
+ LINT_OUTPUT="$(run_if_present ruff check --quiet "$PATH_INPUT")"
41
+ fi
42
+ ;;
43
+ *.ts|*.tsx|*.js|*.jsx|*.mjs|*.cjs)
44
+ has_eslint_config=false
45
+ shopt -s nullglob
46
+ for file in "$PROJECT_ROOT"/.eslintrc*; do
47
+ if [[ -f "$file" ]]; then
48
+ has_eslint_config=true
49
+ break
50
+ fi
51
+ done
52
+ shopt -u nullglob
53
+
54
+ if [[ "$has_eslint_config" = true ]] || [[ -f "$PROJECT_ROOT/eslint.config.js" ]] || [[ -f "$PROJECT_ROOT/eslint.config.mjs" ]]; then
55
+ LINT_OUTPUT="$(cd "$PROJECT_ROOT" && run_if_present npx --no-install eslint --no-color "$PATH_INPUT")"
56
+ fi
57
+ ;;
58
+ *.go)
59
+ if [[ -f "$PROJECT_ROOT/go.mod" ]]; then
60
+ LINT_OUTPUT="$(cd "$PROJECT_ROOT" && run_if_present go vet "./$(dirname "${PATH_INPUT#$PROJECT_ROOT/}")/...")"
61
+ fi
62
+ ;;
63
+ *.rs)
64
+ if [[ -f "$PROJECT_ROOT/Cargo.toml" ]]; then
65
+ LINT_OUTPUT="$(cd "$PROJECT_ROOT" && run_if_present cargo clippy --quiet --message-format=short 2>&1 | head -50)"
66
+ fi
67
+ ;;
68
+ *.sh)
69
+ LINT_OUTPUT="$(run_if_present shellcheck "$PATH_INPUT")"
70
+ ;;
71
+ esac
72
+
73
+ if [[ -n "$LINT_OUTPUT" ]]; then
74
+ # PostToolUse additionalContext must live under hookSpecificOutput per the
75
+ # Claude Code hook schema. The bare {additionalContext} form is silently dropped.
76
+ somi::context "PostToolUse" "Lint output for the file just changed:
77
+ $LINT_OUTPUT"
78
+ fi
79
+
80
+ exit 0