@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.
Files changed (81) hide show
  1. package/.claude/settings.json +8 -8
  2. package/.claude-plugin/marketplace.json +1 -1
  3. package/.claude-plugin/plugin.json +1 -1
  4. package/.copilot-extension/extension.json +11 -2
  5. package/.copilot-extension/marketplace.json +1 -1
  6. package/README.md +21 -4
  7. package/VERSION +1 -1
  8. package/agents/coder.md +8 -3
  9. package/agents/designer.md +22 -7
  10. package/agents/discovery-analyst.md +11 -0
  11. package/agents/planner.md +69 -9
  12. package/agents/refactorer.md +3 -1
  13. package/agents/reviewer.md +4 -0
  14. package/commands/adopt.md +77 -0
  15. package/commands/atlas.md +83 -0
  16. package/commands/code-loop.md +70 -44
  17. package/commands/code-parallel.md +3 -1
  18. package/commands/code.md +8 -2
  19. package/commands/debug.md +136 -0
  20. package/commands/design.md +17 -7
  21. package/commands/discover.md +23 -18
  22. package/commands/impact.md +74 -0
  23. package/commands/incident.md +83 -0
  24. package/commands/plan-loop.md +44 -19
  25. package/commands/plan.md +38 -26
  26. package/commands/pr.md +82 -0
  27. package/commands/release-readiness.md +70 -0
  28. package/commands/review-panel.md +5 -0
  29. package/commands/review.md +19 -0
  30. package/commands/ship-loop.md +18 -10
  31. package/commands/ship.md +4 -2
  32. package/commands/somi.md +85 -0
  33. package/commands/upgrade.md +81 -0
  34. package/hooks/hooks.json +8 -8
  35. package/hooks/lib/common.mjs +328 -0
  36. package/hooks/post-tool/audit-log.mjs +93 -0
  37. package/hooks/post-tool/lint-changed-files.mjs +212 -0
  38. package/hooks/pre-tool/block-dangerous-bash.mjs +382 -0
  39. package/hooks/pre-tool/block-secret-writes.mjs +96 -0
  40. package/hooks/pre-tool/gate-dep-install.mjs +229 -0
  41. package/hooks/pre-tool/guard-protected-paths.mjs +157 -0
  42. package/hooks/session-start/detect-repo-instructions.mjs +264 -0
  43. package/hooks/user-prompt-submit/inject-workflow-context.mjs +432 -0
  44. package/package.json +6 -1
  45. package/rules/50-collaboration.md +4 -1
  46. package/scripts/somi-check.mjs +243 -0
  47. package/scripts/somi-findings.mjs +307 -0
  48. package/scripts/somi-loop.mjs +354 -0
  49. package/scripts/validate.sh +120 -0
  50. package/skills/clean-code/SKILL.md +1 -1
  51. package/templates/ATLAS.md.tmpl +75 -0
  52. package/templates/BRIEF.md.tmpl +20 -4
  53. package/templates/DOD.md.tmpl +2 -2
  54. package/templates/PHASE.md.tmpl +1 -1
  55. package/templates/RCA.md.tmpl +53 -0
  56. package/templates/RD-README.md.tmpl +2 -2
  57. package/templates/SDD.md.tmpl +4 -4
  58. package/templates/SOMI-README.md.tmpl +20 -3
  59. package/templates/SRS.md.tmpl +1 -1
  60. package/templates/TDD.md.tmpl +5 -5
  61. package/tests/hooks/cases/audit-log.json +21 -0
  62. package/tests/hooks/cases/block-dangerous-bash.json +166 -0
  63. package/tests/hooks/cases/block-secret-writes.json +22 -0
  64. package/tests/hooks/cases/detect-repo-instructions.json +84 -0
  65. package/tests/hooks/cases/gate-dep-install.json +42 -0
  66. package/tests/hooks/cases/guard-protected-paths.json +30 -0
  67. package/tests/hooks/cases/inject-workflow-context.json +126 -0
  68. package/tests/hooks/cases/lint-changed-files.json +120 -0
  69. package/tests/hooks/run.sh +362 -0
  70. package/tests/scripts/goldens/somi-findings.json +336 -0
  71. package/tests/scripts/goldens/somi-loop.json +168 -0
  72. package/tests/scripts/run.sh +389 -0
  73. package/hooks/lib/common.sh +0 -120
  74. package/hooks/post-tool/audit-log.sh +0 -40
  75. package/hooks/post-tool/lint-changed-files.sh +0 -80
  76. package/hooks/pre-tool/block-dangerous-bash.sh +0 -83
  77. package/hooks/pre-tool/block-secret-writes.sh +0 -83
  78. package/hooks/pre-tool/gate-dep-install.sh +0 -72
  79. package/hooks/pre-tool/guard-protected-paths.sh +0 -74
  80. package/hooks/session-start/detect-repo-instructions.sh +0 -73
  81. package/hooks/user-prompt-submit/inject-workflow-context.sh +0 -143
@@ -0,0 +1,264 @@
1
+ #!/usr/bin/env node
2
+ // hooks/session-start/detect-repo-instructions.mjs — SessionStart hook — detect repo-local
3
+ // agent/instruction files and surface them.
4
+ //
5
+ // Node port of hooks/session-start/detect-repo-instructions.sh (node-runtime-port, phase 2,
6
+ // iteration 2.7). Imports the shared read/context/projectRoot helpers from ../lib/common.mjs
7
+ // (2.1, reviewer-blessed) rather than reimplementing them.
8
+ //
9
+ // SoMi's MAX→ECO economy is "respect repo conventions as context": when a project ships its
10
+ // own instructions (CLAUDE.md / AGENTS.md / copilot-instructions / .cursorrules) or its own
11
+ // subagents (.claude/agents/), SoMi's MAX actions should read them once and fold the relevant
12
+ // conventions into the work-item brief.md, so the ECO tier inherits them without re-reading.
13
+ // Repo-local instructions WIN over SoMi defaults where they conflict; SoMi does NOT
14
+ // auto-invoke foreign agents. This hook only *surfaces* what exists (paths + a one-line
15
+ // directive) — it does not read or ingest file contents. It fires once per session and stays
16
+ // silent when nothing repo-local is present.
17
+ //
18
+ // === THE PRUNE-LIST DECISION (explicit, named, per the phase file's requirement) =============
19
+ //
20
+ // bash's nested-file find (`detect-repo-instructions.sh:41-46`) declares a prune list
21
+ // (.git/node_modules/.somi/vendor) but it is CONFIRMED NON-FUNCTIONAL on GNU find (0.4's
22
+ // finding, independently reproduced by 0.4 pass 2's review with a control experiment): GNU
23
+ // find's `-mindepth 2` suppresses evaluation of EVERY predicate — including `-prune` — for
24
+ // entries at depth < 2, and the pruned directories themselves (.git, node_modules, .somi,
25
+ // vendor) are always depth-1 children of PROJECT_ROOT. The `-prune` clause therefore never
26
+ // fires; find still descends into all four directories, bounded only by `-maxdepth 3`.
27
+ //
28
+ // This port chooses **(b): fix forward.** A directory whose basename is one of the four
29
+ // pruned names, when it appears as a direct (depth-1) child of PROJECT_ROOT — exactly the
30
+ // entries bash's `-path "$PROJECT_ROOT/.git"`-style literal-path tests target, no broader — is
31
+ // never descended into. This is the narrowest possible fix: it reproduces exactly what the
32
+ // bash `-prune` clause is WRITTEN to do (literal full-path equality against the four
33
+ // top-level names, not a basename match at arbitrary depth), just makes it actually fire. It
34
+ // is not a redesign of the prune policy, only a bug fix confined to this one file.
35
+ //
36
+ // Reasoning, considered against reproducing the bug for strict parity instead:
37
+ //
38
+ // 1. Self-documented intent, violated by an accident, not a choice. This file's own header
39
+ // comment (both bash's and this one's) states the design goal in plain language: "cap
40
+ // depth and count so this stays cheap and never wanders into .git / node_modules /
41
+ // .somi." The current bash behavior directly contradicts its own stated purpose because
42
+ // of a mechanical find/mindepth interaction bug, not because anyone decided vendored
43
+ // dependencies should be scanned. Reproducing a bug that contradicts the file's own
44
+ // documented intent is a weaker form of "parity" than fixing it to match that intent.
45
+ //
46
+ // 2. Correctness, not just performance: what gets SURFACED. A CLAUDE.md/AGENTS.md that
47
+ // happens to live inside a vendored dependency (node_modules), a submodule under
48
+ // vendor/, or somi's own internal state directory is not "this repository's own
49
+ // instructions" in the sense this hook's entire purpose describes to the MAX actions
50
+ // that consume its signal. Surfacing a third-party package's own CLAUDE.md as if it were
51
+ // this repo's convention is actively misleading downstream context, not neutral noise.
52
+ //
53
+ // 3. Performance, empirically measured, not assumed (this is the argument the phase file
54
+ // flagged as possibly strongest, and it held up under measurement). Built a synthetic
55
+ // 800-package node_modules (4802 fs entries at depth <= 3, no matches, so `head -n 10`
56
+ // cannot short-circuit either implementation — full traversal is forced both ways) and
57
+ // timed three walks against it: bash's find (buggy, still descends node_modules) ~14ms;
58
+ // a naive Node readdirSync walk reproducing the SAME non-pruning bug ~31-62ms (Node's
59
+ // per-entry overhead — Dirent construction, JS call/recursion cost — is measurably higher
60
+ // than find's C-native traversal, so "just port the bug" is not perf-neutral, it is
61
+ // WORSE); the fix-forward pruned walk below ~0.17-0.24ms (2 entries visited: node_modules
62
+ // itself, pruned before descending, plus the one root-level file). That is a ~150-350x
63
+ // difference in this synthetic case, and it scales with real node_modules trees, which
64
+ // commonly have thousands of packages — non-negligible for a hook whose whole premise is
65
+ // being cheap enough to run unconditionally on every SessionStart.
66
+ //
67
+ // 4. Fail-quiet in the safe direction, matching this work item's existing precedent for
68
+ // accepted divergences (2.4's BASH_REMATCH-clobber fix, 2.6's UTF-8 truncation
69
+ // divergence): this change can only ever make a previously-surfaced file go silent
70
+ // (prune now excludes something it used to include); in every realistic case it never
71
+ // surfaces something new. (Pedantic corner from the 2.7 review: with >10 nested matches
72
+ // where pruned-dir junk sorts ahead of real files, bash's head -n 10 could truncate a
73
+ // REAL file the pruned walk now surfaces — arguably more correct, but not absolutely
74
+ // "never new".) A regression from this fix is "the hook says nothing when it used to
75
+ // say something," never a false positive.
76
+ //
77
+ // 5. Confined blast radius: unlike the audit()-embedded-delimiter issue (filed work-item-
78
+ // wide, deliberately NOT fixed in a single hook's port), this fix touches only this file
79
+ // — no shared common.mjs contract, no other hook's behavior, no cross-file coupling.
80
+ //
81
+ // What this means for the fixture (tests/hooks/cases/detect-repo-instructions.json):
82
+ // `surfaces-nested-claude-md-under-pruned-dir` (which pinned the CURRENT bash bug — a
83
+ // node_modules/CLAUDE.md being surfaced) is renamed and re-pointed to assert the FIXED
84
+ // behavior (silent exit — node_modules is now actually pruned) as a named, reviewed edit, per
85
+ // the same convention 2.8's status-detection decision is instructed to follow. The stated
86
+ // fixture-implications of the OTHER option (had (a) been chosen instead): every other case in
87
+ // the corpus would be untouched, this one case would pass completely as-is with zero edits,
88
+ // and the.mjs would need to intentionally NOT prune node_modules/.git/.somi/vendor at
89
+ // depth 1 — i.e. deliberately reproduce the bug, at the ~150-350x-in-synthetic-testing
90
+ // performance cost measured above, and at the cost of surfacing third-party CLAUDE.md files
91
+ // as if they were this repository's own conventions.
92
+ //
93
+ // === Depth-semantics translation (find -maxdepth 3 -mindepth 2) ==============================
94
+ // PROJECT_ROOT itself is depth 0; its direct children are depth 1; two directories down is
95
+ // depth 3 — the deepest this scan reaches (maxdepth 3). mindepth 2 excludes depth-0 (root
96
+ // itself, never a candidate anyway) and depth-1 files (root-level files are handled by the
97
+ // separate ROOT_FILES loop below, matching bash's separate `-f` loop + find). Verified against
98
+ // the fixture's depth-boundary pair: a/b/CLAUDE.md (file depth 3) IS surfaced
99
+ // (surfaces-nested-claude-md-at-max-scan-depth); a/b/c/CLAUDE.md (file depth 4) is NOT
100
+ // (omits-claude-md-beyond-scan-depth).
101
+ //
102
+ // === Symlink semantics (verified by direct probe against the unmodified bash hook, not
103
+ // assumed) ======================================================================================
104
+ // bash's ROOT-file loop uses `[[ -f "$PROJECT_ROOT/$f" ]]`, which FOLLOWS symlinks — a
105
+ // symlinked root CLAUDE.md IS surfaced. bash's nested find and the .claude/agents/ count both
106
+ // use `-type f` WITHOUT `-L`/`-follow` — this does NOT follow symlinks (a symlink has type
107
+ // 'l', not 'f'), confirmed with a live probe: a symlinked nested CLAUDE.md is silently
108
+ // excluded from the find output. Node's `fs.readdirSync(dir, { withFileTypes: true })` Dirent
109
+ // entries mirror this exactly, by construction, with no special-casing needed: `isFile()`/
110
+ // `isDirectory()` are both `false` for a symlink entry (confirmed with a live probe on the
111
+ // same fixture directories) — using `fs.statSync` (follows symlinks, like bash's `-f`) for the
112
+ // root-file loop and Dirent's `isFile()`/`isDirectory()` (does not follow, like bash's
113
+ // `-type f`/`-type d`) for the nested walk and the agents count reproduces both behaviors for
114
+ // free. Same reasoning covers a CLAUDE.md that is a DIRECTORY, not a file (verified live): the
115
+ // root loop's `-f` test is false for a directory, and the nested find's `-type f` test is
116
+ // false too (but the directory is still descended into like any other, matching bash) —
117
+ // `fs.statSync(...).isFile()` and Dirent's `isFile()` both return `false` for a directory
118
+ // identically, no extra branch required.
119
+ //
120
+ // === Windows caveat (same class already flagged in 2.2/2.3/2.5) ==============================
121
+ // `path.relative()` (platform-default separator: posix on Linux/macOS, win32 on Windows) is
122
+ // used to build the relative paths this hook emits in its message text. On Windows this would
123
+ // emit backslash-separated relative paths where bash's `${nested#"$PROJECT_ROOT"/}` prefix
124
+ // strip always produced forward slashes — cosmetic only (the message is free text shown to the
125
+ // model, not machine-parsed), not fixed here, consistent with prior ports' documented-not-
126
+ // fixed Windows path-separator gaps.
127
+
128
+ import fs from 'node:fs';
129
+ import path from 'node:path';
130
+ import { readPayload, projectRoot, contextOutput, runHook } from '../lib/common.mjs';
131
+
132
+ // bash: `for f in "CLAUDE.md" "AGENTS.md" ".cursorrules" ".github/copilot-instructions.md"`.
133
+ const ROOT_FILES = ['CLAUDE.md', 'AGENTS.md', '.cursorrules', '.github/copilot-instructions.md'];
134
+
135
+ // bash: `-name 'CLAUDE.md' -o -name 'AGENTS.md'` in the nested find.
136
+ const NESTED_NAMES = new Set(['CLAUDE.md', 'AGENTS.md']);
137
+
138
+ // bash's documented prune list (`detect-repo-instructions.sh:42-43`), now actually enforced —
139
+ // see THE PRUNE-LIST DECISION above. Only matched at depth 1 (PROJECT_ROOT's direct children),
140
+ // mirroring bash's literal `-path "$PROJECT_ROOT/<name>"` full-path equality tests exactly —
141
+ // this is not a basename match at arbitrary depth.
142
+ const PRUNED_TOP_LEVEL_DIRS = new Set(['.git', 'node_modules', '.somi', 'vendor']);
143
+
144
+ const MAX_DEPTH = 3; // bash: find -maxdepth 3
145
+ const MIN_DEPTH = 2; // bash: -mindepth 2
146
+ const MAX_NESTED_RESULTS = 10; // bash: | head -n 10
147
+
148
+ // bash's closing directive line (verbatim, including the embedded single quotes bash's
149
+ // `$'...'\''...'\''...'` escaping decodes to). Byte-exact against the fixture's
150
+ // expect_context substrings.
151
+ const DIRECTIVE =
152
+ "\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.";
153
+
154
+ function isFile(p) {
155
+ try {
156
+ return fs.statSync(p).isFile();
157
+ } catch {
158
+ return false;
159
+ }
160
+ }
161
+
162
+ function isDirectory(p) {
163
+ try {
164
+ return fs.statSync(p).isDirectory();
165
+ } catch {
166
+ return false;
167
+ }
168
+ }
169
+
170
+ // Nested CLAUDE.md/AGENTS.md scan, bounded to depth 3, actually pruning the four top-level
171
+ // directories (see THE PRUNE-LIST DECISION above). `depth` is the depth of the entries being
172
+ // iterated inside `dir` (root's direct children are depth 1). Capped at MAX_NESTED_RESULTS,
173
+ // matching bash's `| head -n 10`. Directory-listing order is filesystem-dependent in both
174
+ // implementations (find and readdirSync both return raw, unsorted directory order) — not a
175
+ // port-introduced nondeterminism, and no fixture case stages more than 2 nested matches, so
176
+ // this never matters for what's actually asserted.
177
+ function scanNested(root) {
178
+ const results = [];
179
+
180
+ function walk(dir, depth) {
181
+ if (results.length >= MAX_NESTED_RESULTS) return;
182
+ let entries;
183
+ try {
184
+ entries = fs.readdirSync(dir, { withFileTypes: true });
185
+ } catch {
186
+ return; // matches find's `2>/dev/null` — an unreadable subtree is silently skipped.
187
+ }
188
+ for (const entry of entries) {
189
+ if (results.length >= MAX_NESTED_RESULTS) return;
190
+ const full = path.join(dir, entry.name);
191
+ if (entry.isDirectory()) {
192
+ if (depth === 1 && PRUNED_TOP_LEVEL_DIRS.has(entry.name)) continue;
193
+ if (depth + 1 <= MAX_DEPTH) walk(full, depth + 1);
194
+ continue;
195
+ }
196
+ if (!entry.isFile()) continue; // false for symlinks and dir-named entries — see header.
197
+ if (depth < MIN_DEPTH) continue;
198
+ if (!NESTED_NAMES.has(entry.name)) continue;
199
+ results.push(path.relative(root, full));
200
+ }
201
+ }
202
+
203
+ walk(root, 1);
204
+ return results;
205
+ }
206
+
207
+ // bash: `find "$PROJECT_ROOT/.claude/agents" -maxdepth 1 -name '*.md' -type f | wc -l`.
208
+ function countAgentDefs(agentsDir) {
209
+ let entries;
210
+ try {
211
+ entries = fs.readdirSync(agentsDir, { withFileTypes: true });
212
+ } catch {
213
+ return 0;
214
+ }
215
+ return entries.filter((e) => e.isFile() && e.name.endsWith('.md')).length;
216
+ }
217
+
218
+ function main() {
219
+ readPayload(); // Drains stdin, matching bash's `somi::read_payload` call. No payload field is
220
+ // read anywhere in this hook (grepped: bash never calls `somi::field` here either) — this is
221
+ // pure discovery over the filesystem, not the tool-call JSON.
222
+
223
+ const root = projectRoot();
224
+
225
+ const found = [];
226
+ for (const f of ROOT_FILES) {
227
+ if (isFile(path.join(root, f))) found.push(f);
228
+ }
229
+ for (const rel of scanNested(root)) {
230
+ found.push(rel);
231
+ }
232
+
233
+ let repoAgents = '';
234
+ const agentsDir = path.join(root, '.claude', 'agents');
235
+ if (isDirectory(agentsDir)) {
236
+ const count = countAgentDefs(agentsDir);
237
+ if (count > 0) {
238
+ repoAgents = `.claude/agents/ (${count} repo-local subagent definition(s))`;
239
+ }
240
+ }
241
+
242
+ if (found.length === 0 && !repoAgents) return;
243
+
244
+ let msg =
245
+ 'somi repo-awareness — this repository ships its own instructions/agents. Respect them as context:';
246
+
247
+ // De-duplicate while preserving order (mirrors bash's `seen` accumulator). Structurally
248
+ // unreachable today — ROOT_FILES entries and scanNested() results can never collide, since
249
+ // the former are always single-segment (or the fixed two-segment copilot path) and the
250
+ // latter are always depth >= 2 relative paths pointing at a different filename set — but
251
+ // reproduced anyway for defensive fidelity with the bash original.
252
+ const seen = new Set();
253
+ for (const f of found) {
254
+ if (seen.has(f)) continue;
255
+ seen.add(f);
256
+ msg += `\n - ${f}`;
257
+ }
258
+ if (repoAgents) msg += `\n - ${repoAgents}`;
259
+ msg += DIRECTIVE;
260
+
261
+ contextOutput('SessionStart', msg);
262
+ }
263
+
264
+ runHook(main);