@warnyin/sdlc 0.1.2 → 0.2.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 (83) hide show
  1. package/CHANGELOG.md +66 -19
  2. package/LICENSE +21 -21
  3. package/README.md +83 -74
  4. package/bin/cli.mjs +103 -30
  5. package/bin/detect.mjs +51 -0
  6. package/bin/multiselect.mjs +147 -0
  7. package/bin/ui.mjs +78 -0
  8. package/lib/caps.mjs +45 -45
  9. package/lib/config.mjs +41 -41
  10. package/lib/delta.mjs +160 -160
  11. package/lib/frontmatter.mjs +59 -59
  12. package/lib/glob.mjs +29 -29
  13. package/lib/manifest.mjs +99 -99
  14. package/lib/observe.mjs +174 -174
  15. package/lib/settings-merge.mjs +63 -63
  16. package/lib/usage.mjs +46 -46
  17. package/lib/validate.mjs +186 -186
  18. package/package.json +42 -42
  19. package/payload/adapters/agents-md.md +8 -8
  20. package/payload/adapters/claude/agents/sdlc-architect.md +12 -12
  21. package/payload/adapters/claude/agents/sdlc-builder.md +14 -14
  22. package/payload/adapters/claude/agents/sdlc-contractor.md +13 -13
  23. package/payload/adapters/claude/agents/sdlc-evaluator.md +13 -13
  24. package/payload/adapters/claude/agents/sdlc-learner.md +16 -16
  25. package/payload/adapters/claude/agents/sdlc-ops.md +11 -11
  26. package/payload/adapters/claude/agents/sdlc-quality.md +13 -13
  27. package/payload/adapters/claude/agents/sdlc-security.md +12 -12
  28. package/payload/adapters/claude/commands/sdlc/auto.md +5 -5
  29. package/payload/adapters/claude/commands/sdlc/build.md +5 -5
  30. package/payload/adapters/claude/commands/sdlc/contract.md +5 -5
  31. package/payload/adapters/claude/commands/sdlc/converge.md +5 -5
  32. package/payload/adapters/claude/commands/sdlc/design.md +5 -5
  33. package/payload/adapters/claude/commands/sdlc/init.md +4 -4
  34. package/payload/adapters/claude/commands/sdlc/new.md +5 -5
  35. package/payload/adapters/claude/commands/sdlc/next.md +4 -4
  36. package/payload/adapters/claude/commands/sdlc/observe.md +4 -4
  37. package/payload/adapters/claude/commands/sdlc/review.md +5 -5
  38. package/payload/adapters/claude/commands/sdlc/ship.md +5 -5
  39. package/payload/adapters/claude/commands/sdlc/steer.md +4 -4
  40. package/payload/adapters/claude/commands/sdlc/verify.md +5 -5
  41. package/payload/adapters/claude/skills/contract-writing/SKILL.md +26 -26
  42. package/payload/adapters/claude/skills/delta-spec-format/SKILL.md +33 -33
  43. package/payload/adapters/claude/skills/sdlc-conventions/SKILL.md +26 -26
  44. package/payload/adapters/cline.md +8 -8
  45. package/payload/adapters/copilot.md +8 -8
  46. package/payload/adapters/cursor.mdc +7 -7
  47. package/payload/adapters/gemini.md +8 -8
  48. package/payload/adapters/windsurf.md +4 -4
  49. package/payload/hooks/_shared.mjs +154 -154
  50. package/payload/hooks/guard-writes.mjs +83 -83
  51. package/payload/hooks/inject-context.mjs +55 -55
  52. package/payload/hooks/journal.mjs +58 -58
  53. package/payload/hooks/session-summary.mjs +50 -50
  54. package/payload/hooks/validate-artifact.mjs +80 -80
  55. package/payload/playbook/README.md +30 -30
  56. package/payload/playbook/auto.md +21 -21
  57. package/payload/playbook/build.md +23 -23
  58. package/payload/playbook/context.md +26 -26
  59. package/payload/playbook/contract.md +23 -23
  60. package/payload/playbook/converge.md +19 -19
  61. package/payload/playbook/design.md +20 -20
  62. package/payload/playbook/init.md +22 -22
  63. package/payload/playbook/new.md +22 -22
  64. package/payload/playbook/next.md +12 -12
  65. package/payload/playbook/observe.md +20 -20
  66. package/payload/playbook/principles.md +28 -28
  67. package/payload/playbook/review.md +17 -17
  68. package/payload/playbook/routing.md +19 -19
  69. package/payload/playbook/rules-card.md +16 -16
  70. package/payload/playbook/ship.md +24 -24
  71. package/payload/playbook/steer.md +21 -21
  72. package/payload/playbook/verify.md +24 -24
  73. package/payload/templates/change-deep.md +29 -29
  74. package/payload/templates/change-standard.md +28 -28
  75. package/payload/templates/change-vibe.md +19 -19
  76. package/payload/templates/config.yaml +8 -8
  77. package/payload/templates/constitution.md +14 -14
  78. package/payload/templates/contract-evals.md +9 -9
  79. package/payload/templates/contract-tests.md +9 -9
  80. package/payload/templates/harness.md +33 -33
  81. package/payload/templates/spec.md +14 -14
  82. package/payload/templates/steering.md +9 -9
  83. package/scripts/validate.mjs +47 -47
@@ -1,26 +1,26 @@
1
- ---
2
- name: sdlc-conventions
3
- description: Cheat-sheet for the sdlc/ artifact layout, line caps, statuses, gates, and machine-owned files. Load when working with any file under sdlc/.
4
- user-invocable: false
5
- ---
6
- # sdlc/ conventions
7
-
8
- Layout: `config.yaml` · `context/{constitution.md, steering/*.md}` · `harness.md`
9
- · `specs/<capability>/spec.md` · `changes/<id>/{change.md, contract/, journal.ndjson}`
10
- · `changes/archive/<date>-<id>/` · `evals/<capability>/rubric.md` · `.state/` (machine).
11
-
12
- Line caps (validator-enforced; count = non-blank, non-comment body lines):
13
- constitution 30 · steering 40 each · always-budget 60 total · harness 60 ·
14
- change vibe/standard/deep 40/100/150 · tests.md 60 · evals.md 40 · spec soft 150.
15
-
16
- Frontmatter: `id` (= folder name) · `tier: vibe|standard|deep` ·
17
- `status: new|contracted|building|verified|shipped`.
18
- Steering: `inclusion: always|paths|manual|agent` (+ `pathMatch` for paths).
19
-
20
- Hard rules (hook-enforced):
21
- - `sdlc/specs/**` + `changes/archive/**`: writable only during an open ship gate.
22
- - `constitution.md`: writable only during an open steer gate.
23
- - `journal.ndjson` + `.state/**`: machine-owned, never hand-edit.
24
-
25
- Gates: `node sdlc/.hooks/journal.mjs open-ship <id> | open-steer | close | set-active <id> | note <name> [k=v]`.
26
- Validation: `npx @warnyin/sdlc validate [id] [--strict]` — red = the gate did not pass.
1
+ ---
2
+ name: sdlc-conventions
3
+ description: Cheat-sheet for the sdlc/ artifact layout, line caps, statuses, gates, and machine-owned files. Load when working with any file under sdlc/.
4
+ user-invocable: false
5
+ ---
6
+ # sdlc/ conventions
7
+
8
+ Layout: `config.yaml` · `context/{constitution.md, steering/*.md}` · `harness.md`
9
+ · `specs/<capability>/spec.md` · `changes/<id>/{change.md, contract/, journal.ndjson}`
10
+ · `changes/archive/<date>-<id>/` · `evals/<capability>/rubric.md` · `.state/` (machine).
11
+
12
+ Line caps (validator-enforced; count = non-blank, non-comment body lines):
13
+ constitution 30 · steering 40 each · always-budget 60 total · harness 60 ·
14
+ change vibe/standard/deep 40/100/150 · tests.md 60 · evals.md 40 · spec soft 150.
15
+
16
+ Frontmatter: `id` (= folder name) · `tier: vibe|standard|deep` ·
17
+ `status: new|contracted|building|verified|shipped`.
18
+ Steering: `inclusion: always|paths|manual|agent` (+ `pathMatch` for paths).
19
+
20
+ Hard rules (hook-enforced):
21
+ - `sdlc/specs/**` + `changes/archive/**`: writable only during an open ship gate.
22
+ - `constitution.md`: writable only during an open steer gate.
23
+ - `journal.ndjson` + `.state/**`: machine-owned, never hand-edit.
24
+
25
+ Gates: `node sdlc/.hooks/journal.mjs open-ship <id> | open-steer | close | set-active <id> | note <name> [k=v]`.
26
+ Validation: `npx @warnyin/sdlc validate [id] [--strict]` — red = the gate did not pass.
@@ -1,8 +1,8 @@
1
- <!-- sdlc:start -->
2
- ## Warnyin SDLC — spec-driven AI workflow
3
-
4
- This project uses @warnyin/sdlc. Stage playbooks live in `sdlc/.playbook/`
5
- (start at `README.md`); read the playbook for the stage you are asked to run.
6
-
7
- {{RULES_CARD}}
8
- <!-- sdlc:end -->
1
+ <!-- sdlc:start -->
2
+ ## Warnyin SDLC — spec-driven AI workflow
3
+
4
+ This project uses @warnyin/sdlc. Stage playbooks live in `sdlc/.playbook/`
5
+ (start at `README.md`); read the playbook for the stage you are asked to run.
6
+
7
+ {{RULES_CARD}}
8
+ <!-- sdlc:end -->
@@ -1,8 +1,8 @@
1
- <!-- sdlc:start -->
2
- ## Warnyin SDLC — spec-driven AI workflow
3
-
4
- This project uses @warnyin/sdlc. Stage playbooks live in `sdlc/.playbook/`
5
- (start at `README.md`); read the playbook for the stage you are asked to run.
6
-
7
- {{RULES_CARD}}
8
- <!-- sdlc:end -->
1
+ <!-- sdlc:start -->
2
+ ## Warnyin SDLC — spec-driven AI workflow
3
+
4
+ This project uses @warnyin/sdlc. Stage playbooks live in `sdlc/.playbook/`
5
+ (start at `README.md`); read the playbook for the stage you are asked to run.
6
+
7
+ {{RULES_CARD}}
8
+ <!-- sdlc:end -->
@@ -1,7 +1,7 @@
1
- ---
2
- description: Warnyin SDLC workflow rules (spec-driven, contract-first)
3
- alwaysApply: true
4
- ---
5
- Stage playbooks: `sdlc/.playbook/` (start at README.md) — read the stage playbook before acting.
6
-
7
- {{RULES_CARD}}
1
+ ---
2
+ description: Warnyin SDLC workflow rules (spec-driven, contract-first)
3
+ alwaysApply: true
4
+ ---
5
+ Stage playbooks: `sdlc/.playbook/` (start at README.md) — read the stage playbook before acting.
6
+
7
+ {{RULES_CARD}}
@@ -1,8 +1,8 @@
1
- <!-- sdlc:start -->
2
- ## Warnyin SDLC — spec-driven AI workflow
3
-
4
- This project uses @warnyin/sdlc. Stage playbooks live in `sdlc/.playbook/`
5
- (start at `README.md`); read the playbook for the stage you are asked to run.
6
-
7
- {{RULES_CARD}}
8
- <!-- sdlc:end -->
1
+ <!-- sdlc:start -->
2
+ ## Warnyin SDLC — spec-driven AI workflow
3
+
4
+ This project uses @warnyin/sdlc. Stage playbooks live in `sdlc/.playbook/`
5
+ (start at `README.md`); read the playbook for the stage you are asked to run.
6
+
7
+ {{RULES_CARD}}
8
+ <!-- sdlc:end -->
@@ -1,4 +1,4 @@
1
- # Warnyin SDLC workflow rules
2
- Stage playbooks: `sdlc/.playbook/` (start at README.md) — read the stage playbook before acting.
3
-
4
- {{RULES_CARD}}
1
+ # Warnyin SDLC workflow rules
2
+ Stage playbooks: `sdlc/.playbook/` (start at README.md) — read the stage playbook before acting.
3
+
4
+ {{RULES_CARD}}
@@ -1,154 +1,154 @@
1
- // Shared plumbing for installed hooks. This file lives at
2
- // <project>/sdlc/.hooks/_shared.mjs with lib/ as a sibling directory.
3
- // Every hook must be fail-open: on any unexpected condition, exit 0 silently
4
- // so the harness is never blocked by our tooling.
5
-
6
- import fs from 'node:fs';
7
- import path from 'node:path';
8
- import process from 'node:process';
9
- import { fileURLToPath } from 'node:url';
10
-
11
- export function resolveRoots(importMetaUrl) {
12
- const hooksDir = path.dirname(fileURLToPath(importMetaUrl));
13
- const sdlcRoot = path.dirname(hooksDir);
14
- const projectRoot = path.dirname(sdlcRoot);
15
- return { hooksDir, sdlcRoot, projectRoot };
16
- }
17
-
18
- // Reads the hook payload from stdin. Must NEVER hang: when a playbook or a
19
- // user script invokes a hook utility with stdin open-but-idle (no piped JSON),
20
- // resolve null after a short grace period instead of blocking forever.
21
- export function readStdinJson({ timeoutMs = 1000 } = {}) {
22
- return new Promise((resolve) => {
23
- let data = '';
24
- let done = false;
25
- const finish = () => {
26
- if (done) return;
27
- done = true;
28
- // Release stdin so an open-idle stream cannot keep the event loop alive.
29
- process.stdin.pause();
30
- if (typeof process.stdin.unref === 'function') process.stdin.unref();
31
- try { resolve(data.trim() ? JSON.parse(data) : null); } catch { resolve(null); }
32
- };
33
- const timer = setTimeout(finish, timeoutMs);
34
- if (typeof timer.unref === 'function') timer.unref();
35
- process.stdin.on('data', (chunk) => { data += chunk; });
36
- process.stdin.on('end', finish);
37
- process.stdin.on('error', finish);
38
- });
39
- }
40
-
41
- // Resolve symlinks on the deepest EXISTING ancestor, then re-attach the tail.
42
- // Needed because import.meta.url is symlink-resolved while tool file_paths may
43
- // arrive through a symlink (/tmp → /private/tmp on macOS).
44
- export function realResolve(p) {
45
- let cur = path.resolve(p);
46
- const tail = [];
47
- while (!fs.existsSync(cur)) {
48
- const parent = path.dirname(cur);
49
- if (parent === cur) break;
50
- tail.unshift(path.basename(cur));
51
- cur = parent;
52
- }
53
- try { cur = fs.realpathSync.native(cur); } catch { /* keep as-is */ }
54
- return tail.length ? path.join(cur, ...tail) : cur;
55
- }
56
-
57
- export function toPosixRel(projectRoot, absPath) {
58
- const rel = path.relative(realResolve(projectRoot), realResolve(absPath));
59
- if (rel.startsWith('..')) return null;
60
- return rel.split(path.sep).join('/');
61
- }
62
-
63
- // Lexical (no-symlink-resolution) relative path: what the path CLAIMS to be.
64
- // Tried against both the raw and the realpathed project root so /tmp-style
65
- // root symlinks don't break matching. Guards must compare this against
66
- // toPosixRel — a divergence means a symlink sits inside the project.
67
- export function lexicalPosixRel(projectRoot, absPath) {
68
- const abs = path.resolve(absPath);
69
- const realRoot = realResolve(projectRoot);
70
- for (const base of [path.resolve(projectRoot), realRoot]) {
71
- const rel = path.relative(base, abs);
72
- if (rel && !rel.startsWith('..') && !path.isAbsolute(rel)) {
73
- return rel.split(path.sep).join('/');
74
- }
75
- }
76
- // Root-level symlinks (/tmp → /private/tmp): find the SHALLOWEST ancestor of
77
- // abs whose realpath IS the project root; the remaining tail is the lexical
78
- // claim. In-project symlinks are deliberately not resolved here.
79
- const segs = abs.split(path.sep);
80
- for (let i = 1; i < segs.length; i++) {
81
- const ancestor = segs.slice(0, i).join(path.sep) || path.sep;
82
- let real;
83
- try { real = fs.realpathSync.native(ancestor); } catch { continue; }
84
- if (real === realRoot) {
85
- const tail = segs.slice(i).join('/');
86
- return tail || null;
87
- }
88
- }
89
- return null;
90
- }
91
-
92
- // Gate state written by `journal.mjs open-<phase>` — {phase, change?, expires}.
93
- export function readPhase(sdlcRoot) {
94
- try {
95
- const raw = fs.readFileSync(path.join(sdlcRoot, '.state', 'phase.json'), 'utf8');
96
- const phase = JSON.parse(raw);
97
- if (phase.expires && Date.parse(phase.expires) < Date.now()) return null;
98
- return phase;
99
- } catch {
100
- return null;
101
- }
102
- }
103
-
104
- export function writePhase(sdlcRoot, phase, ttlMinutes = 30) {
105
- const stateDir = path.join(sdlcRoot, '.state');
106
- fs.mkdirSync(stateDir, { recursive: true });
107
- const payload = { ...phase, expires: new Date(Date.now() + ttlMinutes * 60_000).toISOString() };
108
- fs.writeFileSync(path.join(stateDir, 'phase.json'), JSON.stringify(payload));
109
- return payload;
110
- }
111
-
112
- export function clearPhase(sdlcRoot) {
113
- fs.rmSync(path.join(sdlcRoot, '.state', 'phase.json'), { force: true });
114
- }
115
-
116
- // Active change: explicit .state/active.json first, else the most recently
117
- // modified changes/*/change.md.
118
- export function activeChange(sdlcRoot) {
119
- try {
120
- const explicit = JSON.parse(fs.readFileSync(path.join(sdlcRoot, '.state', 'active.json'), 'utf8'));
121
- if (explicit?.change && fs.existsSync(path.join(sdlcRoot, 'changes', explicit.change))) {
122
- return explicit.change;
123
- }
124
- } catch { /* fall through */ }
125
- const changesDir = path.join(sdlcRoot, 'changes');
126
- if (!fs.existsSync(changesDir)) return null;
127
- let best = null;
128
- for (const d of fs.readdirSync(changesDir, { withFileTypes: true })) {
129
- if (!d.isDirectory() || d.name === 'archive') continue;
130
- const p = path.join(changesDir, d.name, 'change.md');
131
- if (!fs.existsSync(p)) continue;
132
- const mtime = fs.statSync(p).mtimeMs;
133
- if (!best || mtime > best.mtime) best = { change: d.name, mtime };
134
- }
135
- return best?.change ?? null;
136
- }
137
-
138
- // Journal: per-change ndjson when a change is active, else a global one under
139
- // .state/ so no signal is lost. Hook-written only — agents never hand-edit.
140
- export function appendJournal(sdlcRoot, change, event) {
141
- try {
142
- const line = JSON.stringify({ ts: new Date().toISOString(), ...event }) + '\n';
143
- if (change) {
144
- const dir = path.join(sdlcRoot, 'changes', change);
145
- if (fs.existsSync(dir)) {
146
- fs.appendFileSync(path.join(dir, 'journal.ndjson'), line);
147
- return;
148
- }
149
- }
150
- const stateDir = path.join(sdlcRoot, '.state');
151
- fs.mkdirSync(stateDir, { recursive: true });
152
- fs.appendFileSync(path.join(stateDir, 'journal.ndjson'), line);
153
- } catch { /* fail open */ }
154
- }
1
+ // Shared plumbing for installed hooks. This file lives at
2
+ // <project>/sdlc/.hooks/_shared.mjs with lib/ as a sibling directory.
3
+ // Every hook must be fail-open: on any unexpected condition, exit 0 silently
4
+ // so the harness is never blocked by our tooling.
5
+
6
+ import fs from 'node:fs';
7
+ import path from 'node:path';
8
+ import process from 'node:process';
9
+ import { fileURLToPath } from 'node:url';
10
+
11
+ export function resolveRoots(importMetaUrl) {
12
+ const hooksDir = path.dirname(fileURLToPath(importMetaUrl));
13
+ const sdlcRoot = path.dirname(hooksDir);
14
+ const projectRoot = path.dirname(sdlcRoot);
15
+ return { hooksDir, sdlcRoot, projectRoot };
16
+ }
17
+
18
+ // Reads the hook payload from stdin. Must NEVER hang: when a playbook or a
19
+ // user script invokes a hook utility with stdin open-but-idle (no piped JSON),
20
+ // resolve null after a short grace period instead of blocking forever.
21
+ export function readStdinJson({ timeoutMs = 1000 } = {}) {
22
+ return new Promise((resolve) => {
23
+ let data = '';
24
+ let done = false;
25
+ const finish = () => {
26
+ if (done) return;
27
+ done = true;
28
+ // Release stdin so an open-idle stream cannot keep the event loop alive.
29
+ process.stdin.pause();
30
+ if (typeof process.stdin.unref === 'function') process.stdin.unref();
31
+ try { resolve(data.trim() ? JSON.parse(data) : null); } catch { resolve(null); }
32
+ };
33
+ const timer = setTimeout(finish, timeoutMs);
34
+ if (typeof timer.unref === 'function') timer.unref();
35
+ process.stdin.on('data', (chunk) => { data += chunk; });
36
+ process.stdin.on('end', finish);
37
+ process.stdin.on('error', finish);
38
+ });
39
+ }
40
+
41
+ // Resolve symlinks on the deepest EXISTING ancestor, then re-attach the tail.
42
+ // Needed because import.meta.url is symlink-resolved while tool file_paths may
43
+ // arrive through a symlink (/tmp → /private/tmp on macOS).
44
+ export function realResolve(p) {
45
+ let cur = path.resolve(p);
46
+ const tail = [];
47
+ while (!fs.existsSync(cur)) {
48
+ const parent = path.dirname(cur);
49
+ if (parent === cur) break;
50
+ tail.unshift(path.basename(cur));
51
+ cur = parent;
52
+ }
53
+ try { cur = fs.realpathSync.native(cur); } catch { /* keep as-is */ }
54
+ return tail.length ? path.join(cur, ...tail) : cur;
55
+ }
56
+
57
+ export function toPosixRel(projectRoot, absPath) {
58
+ const rel = path.relative(realResolve(projectRoot), realResolve(absPath));
59
+ if (rel.startsWith('..')) return null;
60
+ return rel.split(path.sep).join('/');
61
+ }
62
+
63
+ // Lexical (no-symlink-resolution) relative path: what the path CLAIMS to be.
64
+ // Tried against both the raw and the realpathed project root so /tmp-style
65
+ // root symlinks don't break matching. Guards must compare this against
66
+ // toPosixRel — a divergence means a symlink sits inside the project.
67
+ export function lexicalPosixRel(projectRoot, absPath) {
68
+ const abs = path.resolve(absPath);
69
+ const realRoot = realResolve(projectRoot);
70
+ for (const base of [path.resolve(projectRoot), realRoot]) {
71
+ const rel = path.relative(base, abs);
72
+ if (rel && !rel.startsWith('..') && !path.isAbsolute(rel)) {
73
+ return rel.split(path.sep).join('/');
74
+ }
75
+ }
76
+ // Root-level symlinks (/tmp → /private/tmp): find the SHALLOWEST ancestor of
77
+ // abs whose realpath IS the project root; the remaining tail is the lexical
78
+ // claim. In-project symlinks are deliberately not resolved here.
79
+ const segs = abs.split(path.sep);
80
+ for (let i = 1; i < segs.length; i++) {
81
+ const ancestor = segs.slice(0, i).join(path.sep) || path.sep;
82
+ let real;
83
+ try { real = fs.realpathSync.native(ancestor); } catch { continue; }
84
+ if (real === realRoot) {
85
+ const tail = segs.slice(i).join('/');
86
+ return tail || null;
87
+ }
88
+ }
89
+ return null;
90
+ }
91
+
92
+ // Gate state written by `journal.mjs open-<phase>` — {phase, change?, expires}.
93
+ export function readPhase(sdlcRoot) {
94
+ try {
95
+ const raw = fs.readFileSync(path.join(sdlcRoot, '.state', 'phase.json'), 'utf8');
96
+ const phase = JSON.parse(raw);
97
+ if (phase.expires && Date.parse(phase.expires) < Date.now()) return null;
98
+ return phase;
99
+ } catch {
100
+ return null;
101
+ }
102
+ }
103
+
104
+ export function writePhase(sdlcRoot, phase, ttlMinutes = 30) {
105
+ const stateDir = path.join(sdlcRoot, '.state');
106
+ fs.mkdirSync(stateDir, { recursive: true });
107
+ const payload = { ...phase, expires: new Date(Date.now() + ttlMinutes * 60_000).toISOString() };
108
+ fs.writeFileSync(path.join(stateDir, 'phase.json'), JSON.stringify(payload));
109
+ return payload;
110
+ }
111
+
112
+ export function clearPhase(sdlcRoot) {
113
+ fs.rmSync(path.join(sdlcRoot, '.state', 'phase.json'), { force: true });
114
+ }
115
+
116
+ // Active change: explicit .state/active.json first, else the most recently
117
+ // modified changes/*/change.md.
118
+ export function activeChange(sdlcRoot) {
119
+ try {
120
+ const explicit = JSON.parse(fs.readFileSync(path.join(sdlcRoot, '.state', 'active.json'), 'utf8'));
121
+ if (explicit?.change && fs.existsSync(path.join(sdlcRoot, 'changes', explicit.change))) {
122
+ return explicit.change;
123
+ }
124
+ } catch { /* fall through */ }
125
+ const changesDir = path.join(sdlcRoot, 'changes');
126
+ if (!fs.existsSync(changesDir)) return null;
127
+ let best = null;
128
+ for (const d of fs.readdirSync(changesDir, { withFileTypes: true })) {
129
+ if (!d.isDirectory() || d.name === 'archive') continue;
130
+ const p = path.join(changesDir, d.name, 'change.md');
131
+ if (!fs.existsSync(p)) continue;
132
+ const mtime = fs.statSync(p).mtimeMs;
133
+ if (!best || mtime > best.mtime) best = { change: d.name, mtime };
134
+ }
135
+ return best?.change ?? null;
136
+ }
137
+
138
+ // Journal: per-change ndjson when a change is active, else a global one under
139
+ // .state/ so no signal is lost. Hook-written only — agents never hand-edit.
140
+ export function appendJournal(sdlcRoot, change, event) {
141
+ try {
142
+ const line = JSON.stringify({ ts: new Date().toISOString(), ...event }) + '\n';
143
+ if (change) {
144
+ const dir = path.join(sdlcRoot, 'changes', change);
145
+ if (fs.existsSync(dir)) {
146
+ fs.appendFileSync(path.join(dir, 'journal.ndjson'), line);
147
+ return;
148
+ }
149
+ }
150
+ const stateDir = path.join(sdlcRoot, '.state');
151
+ fs.mkdirSync(stateDir, { recursive: true });
152
+ fs.appendFileSync(path.join(stateDir, 'journal.ndjson'), line);
153
+ } catch { /* fail open */ }
154
+ }
@@ -1,83 +1,83 @@
1
- #!/usr/bin/env node
2
- // PreToolUse hook — the deterministic guardrail ("things the agent should
3
- // never forget but often does"). Denies direct edits to:
4
- // sdlc/specs/** outside an open ship gate
5
- // sdlc/changes/archive/** outside an open ship gate
6
- // sdlc/context/constitution.md (existing) outside an open steer gate
7
- // sdlc/.state/** and any journal.ndjson always (machine-owned)
8
- // The sanctioned paths are the CLI (`warnyin-sdlc archive`) and the gates
9
- // opened by `journal.mjs open-ship|open-steer`.
10
-
11
- import fs from 'node:fs';
12
- import process from 'node:process';
13
- import path from 'node:path';
14
- import {
15
- resolveRoots, readStdinJson, readPhase, activeChange, appendJournal, toPosixRel, lexicalPosixRel,
16
- } from './_shared.mjs';
17
-
18
- const { sdlcRoot, projectRoot } = resolveRoots(import.meta.url);
19
-
20
- function deny(reason, rel) {
21
- appendJournal(sdlcRoot, activeChange(sdlcRoot), { event: 'guard', action: 'deny', path: rel, reason });
22
- console.log(JSON.stringify({
23
- hookSpecificOutput: {
24
- hookEventName: 'PreToolUse',
25
- permissionDecision: 'deny',
26
- permissionDecisionReason: reason,
27
- },
28
- }));
29
- }
30
-
31
- // Evaluate the lock rules against ONE view of the path. Returns true when a
32
- // deny was emitted. Rules must hold for BOTH the lexical (claimed) and the
33
- // realpath-resolved view — a symlink must never weaken a lock.
34
- function guard(rel, phase) {
35
- if (rel.startsWith('sdlc/.state/') || rel.endsWith('journal.ndjson')) {
36
- deny(`"${rel}" is machine-owned (hooks/CLI write it) — never edit it by hand.`, rel);
37
- return true;
38
- }
39
- if (rel.startsWith('sdlc/specs/') || rel.startsWith('sdlc/changes/archive/')) {
40
- if (phase?.phase === 'ship') return false;
41
- deny(
42
- `"${rel}" is write-locked outside ship. Living specs change only by merging a change's Delta: `
43
- + 'run `warnyin-sdlc archive <id>` (or `node sdlc/.hooks/journal.mjs open-ship <id>` first if you must edit).',
44
- rel,
45
- );
46
- return true;
47
- }
48
- if (rel === 'sdlc/context/constitution.md' && fs.existsSync(path.join(projectRoot, rel))) {
49
- if (phase?.phase === 'steer' || phase?.phase === 'ship') return false;
50
- deny(
51
- 'The constitution is always-loaded context — edits go through /sdlc:steer '
52
- + '(`node sdlc/.hooks/journal.mjs open-steer` opens the gate).',
53
- rel,
54
- );
55
- return true;
56
- }
57
- return false;
58
- }
59
-
60
- async function main() {
61
- const input = await readStdinJson();
62
- const filePath = input?.tool_input?.file_path ?? input?.tool_input?.notebook_path;
63
- if (!filePath || !fs.existsSync(sdlcRoot)) return;
64
-
65
- const abs = path.resolve(projectRoot, filePath);
66
- const relLexical = lexicalPosixRel(projectRoot, abs);
67
- const relReal = toPosixRel(projectRoot, abs);
68
-
69
- // A path that CLAIMS to live under sdlc/ but resolves elsewhere (or out of
70
- // the project) went through a symlink — deny conservatively; a symlink must
71
- // never disable the write-lock.
72
- if (relLexical?.startsWith('sdlc/') && relReal !== relLexical) {
73
- deny(`"${relLexical}" resolves through a symlink to "${relReal ?? 'outside the project'}" — refusing to touch it.`, relLexical);
74
- return;
75
- }
76
-
77
- const phase = readPhase(sdlcRoot);
78
- for (const rel of new Set([relLexical, relReal].filter(Boolean))) {
79
- if (rel.startsWith('sdlc/') && guard(rel, phase)) return;
80
- }
81
- }
82
-
83
- main().catch(() => process.exit(0)); // fail open
1
+ #!/usr/bin/env node
2
+ // PreToolUse hook — the deterministic guardrail ("things the agent should
3
+ // never forget but often does"). Denies direct edits to:
4
+ // sdlc/specs/** outside an open ship gate
5
+ // sdlc/changes/archive/** outside an open ship gate
6
+ // sdlc/context/constitution.md (existing) outside an open steer gate
7
+ // sdlc/.state/** and any journal.ndjson always (machine-owned)
8
+ // The sanctioned paths are the CLI (`warnyin-sdlc archive`) and the gates
9
+ // opened by `journal.mjs open-ship|open-steer`.
10
+
11
+ import fs from 'node:fs';
12
+ import process from 'node:process';
13
+ import path from 'node:path';
14
+ import {
15
+ resolveRoots, readStdinJson, readPhase, activeChange, appendJournal, toPosixRel, lexicalPosixRel,
16
+ } from './_shared.mjs';
17
+
18
+ const { sdlcRoot, projectRoot } = resolveRoots(import.meta.url);
19
+
20
+ function deny(reason, rel) {
21
+ appendJournal(sdlcRoot, activeChange(sdlcRoot), { event: 'guard', action: 'deny', path: rel, reason });
22
+ console.log(JSON.stringify({
23
+ hookSpecificOutput: {
24
+ hookEventName: 'PreToolUse',
25
+ permissionDecision: 'deny',
26
+ permissionDecisionReason: reason,
27
+ },
28
+ }));
29
+ }
30
+
31
+ // Evaluate the lock rules against ONE view of the path. Returns true when a
32
+ // deny was emitted. Rules must hold for BOTH the lexical (claimed) and the
33
+ // realpath-resolved view — a symlink must never weaken a lock.
34
+ function guard(rel, phase) {
35
+ if (rel.startsWith('sdlc/.state/') || rel.endsWith('journal.ndjson')) {
36
+ deny(`"${rel}" is machine-owned (hooks/CLI write it) — never edit it by hand.`, rel);
37
+ return true;
38
+ }
39
+ if (rel.startsWith('sdlc/specs/') || rel.startsWith('sdlc/changes/archive/')) {
40
+ if (phase?.phase === 'ship') return false;
41
+ deny(
42
+ `"${rel}" is write-locked outside ship. Living specs change only by merging a change's Delta: `
43
+ + 'run `warnyin-sdlc archive <id>` (or `node sdlc/.hooks/journal.mjs open-ship <id>` first if you must edit).',
44
+ rel,
45
+ );
46
+ return true;
47
+ }
48
+ if (rel === 'sdlc/context/constitution.md' && fs.existsSync(path.join(projectRoot, rel))) {
49
+ if (phase?.phase === 'steer' || phase?.phase === 'ship') return false;
50
+ deny(
51
+ 'The constitution is always-loaded context — edits go through /sdlc:steer '
52
+ + '(`node sdlc/.hooks/journal.mjs open-steer` opens the gate).',
53
+ rel,
54
+ );
55
+ return true;
56
+ }
57
+ return false;
58
+ }
59
+
60
+ async function main() {
61
+ const input = await readStdinJson();
62
+ const filePath = input?.tool_input?.file_path ?? input?.tool_input?.notebook_path;
63
+ if (!filePath || !fs.existsSync(sdlcRoot)) return;
64
+
65
+ const abs = path.resolve(projectRoot, filePath);
66
+ const relLexical = lexicalPosixRel(projectRoot, abs);
67
+ const relReal = toPosixRel(projectRoot, abs);
68
+
69
+ // A path that CLAIMS to live under sdlc/ but resolves elsewhere (or out of
70
+ // the project) went through a symlink — deny conservatively; a symlink must
71
+ // never disable the write-lock.
72
+ if (relLexical?.startsWith('sdlc/') && relReal !== relLexical) {
73
+ deny(`"${relLexical}" resolves through a symlink to "${relReal ?? 'outside the project'}" — refusing to touch it.`, relLexical);
74
+ return;
75
+ }
76
+
77
+ const phase = readPhase(sdlcRoot);
78
+ for (const rel of new Set([relLexical, relReal].filter(Boolean))) {
79
+ if (rel.startsWith('sdlc/') && guard(rel, phase)) return;
80
+ }
81
+ }
82
+
83
+ main().catch(() => process.exit(0)); // fail open