@thecodesaiyan/claude-dev-workflow-hook 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.
- package/AGENTS.md +223 -0
- package/README.md +352 -0
- package/bin/cli.js +108 -0
- package/package.json +37 -0
- package/src/hook.js +43 -0
- package/src/installer.js +318 -0
- package/src/protocol.js +230 -0
- package/src/uninstaller.js +149 -0
- package/src/utils.js +19 -0
package/AGENTS.md
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# AGENTS.md -- Development Workflow Protocol
|
|
2
|
+
|
|
3
|
+
Follow these mandatory workflow rules for ALL implementation work.
|
|
4
|
+
Violations (skipping tests, skipping planning, ignoring TDD) are NOT acceptable.
|
|
5
|
+
|
|
6
|
+
## Compliance Reporting
|
|
7
|
+
|
|
8
|
+
Announce rule adherence as you work. Before starting any task:
|
|
9
|
+
|
|
10
|
+
1. State which rules apply (see the Quick Reference table at the bottom).
|
|
11
|
+
2. Prefix each action with `[PROTOCOL Rule N]` as you execute it.
|
|
12
|
+
3. If you skip a rule, state why (e.g. Rule 1 small-task exception).
|
|
13
|
+
|
|
14
|
+
Example output:
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
[PROTOCOL Rule 0] Planning: This task modifies 4 files -- api.ts, schema.ts, UserForm.tsx, user.test.ts.
|
|
18
|
+
[PROTOCOL Rule 1] Small task -- skipping team orchestration (2 files, no new entity).
|
|
19
|
+
[PROTOCOL Rule 2] TDD RED: Writing failing test for createUser validation...
|
|
20
|
+
[PROTOCOL Rule 3] Test loop: Running tests after schema.ts change...
|
|
21
|
+
[PROTOCOL Rule 4] Phase 1: Researching affected files and dependency graph...
|
|
22
|
+
[PROTOCOL Rule 5] UI check: Verifying responsive layout at 375px / 768px / 1024px...
|
|
23
|
+
[PROTOCOL Rule 6] Integration test: Starting container stack...
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
If you do NOT output these prefixes, you are violating this protocol.
|
|
27
|
+
|
|
28
|
+
## Rule 0 -- Plan Before Acting
|
|
29
|
+
|
|
30
|
+
Before writing ANY code:
|
|
31
|
+
|
|
32
|
+
1. Identify every file that will be created or modified.
|
|
33
|
+
2. Map dependencies between changes (e.g. backend model change breaks frontend types).
|
|
34
|
+
3. If more than 2 files are affected, create a detailed plan and get approval before proceeding.
|
|
35
|
+
4. If the plan changes mid-implementation, STOP and revise the plan.
|
|
36
|
+
|
|
37
|
+
## Rule 1 -- Small Task Exception
|
|
38
|
+
|
|
39
|
+
If a task changes 2 or fewer files AND does not add a new entity or endpoint:
|
|
40
|
+
|
|
41
|
+
- Skip team orchestration (Rule 4).
|
|
42
|
+
- Still follow Rules 0, 2, 3 (plan, TDD, test loop).
|
|
43
|
+
- Still follow Rule 5 if modifying frontend components.
|
|
44
|
+
- Still follow Rule 6 if modifying API endpoints or services.
|
|
45
|
+
- Still run the full test suite before marking done.
|
|
46
|
+
|
|
47
|
+
## Rule 2 -- TDD Enforcement
|
|
48
|
+
|
|
49
|
+
For new features and bug fixes:
|
|
50
|
+
|
|
51
|
+
1. **RED** -- Write a failing test that describes the expected behavior.
|
|
52
|
+
2. **GREEN** -- Write the minimum code to make the test pass.
|
|
53
|
+
3. **IMPROVE** -- Refactor without changing behavior; re-run tests.
|
|
54
|
+
4. Target 80%+ coverage.
|
|
55
|
+
|
|
56
|
+
## Rule 3 -- Test After Every Change
|
|
57
|
+
|
|
58
|
+
After EVERY code change (not just at the end):
|
|
59
|
+
|
|
60
|
+
1. Run the relevant test suite for the area you changed.
|
|
61
|
+
2. If ANY test fails, fix it BEFORE moving to the next change.
|
|
62
|
+
3. Never stack multiple untested changes -- each change must pass before proceeding.
|
|
63
|
+
4. After all changes, run the full test suite.
|
|
64
|
+
|
|
65
|
+
### Auto-Detect Test Runner
|
|
66
|
+
|
|
67
|
+
Use the first match found in the project root:
|
|
68
|
+
|
|
69
|
+
| Indicator | Command |
|
|
70
|
+
|-----------|---------|
|
|
71
|
+
| `package.json` with `test` script | `npm test` / `yarn test` / `pnpm test` / `bun test` (match lockfile) |
|
|
72
|
+
| `Makefile` with `test` target | `make test` |
|
|
73
|
+
| `Cargo.toml` | `cargo test` |
|
|
74
|
+
| `go.mod` | `go test ./...` |
|
|
75
|
+
| `*.sln` or `*.slnx` | `dotnet test <solution-file>` |
|
|
76
|
+
| `pyproject.toml` or `setup.py` | `pytest` or `python -m pytest` |
|
|
77
|
+
| `build.gradle` or `pom.xml` | `./gradlew test` or `mvn test` |
|
|
78
|
+
| `mix.exs` | `mix test` |
|
|
79
|
+
| `Gemfile` | `bundle exec rspec` or `bundle exec rake test` |
|
|
80
|
+
|
|
81
|
+
If multiple ecosystems exist (e.g. .NET backend + React frontend), run BOTH test suites.
|
|
82
|
+
|
|
83
|
+
## Rule 4 -- Agent Team Orchestration (Multi-File Features)
|
|
84
|
+
|
|
85
|
+
For features touching more than 2 files, use a 7-phase pipeline with maximum parallelism.
|
|
86
|
+
Do NOT do all work inline -- delegate to agent teams or parallel workers where available.
|
|
87
|
+
|
|
88
|
+
### Phase 1: Research (Read-Only) -- SEQUENTIAL
|
|
89
|
+
|
|
90
|
+
Map the affected area before implementing:
|
|
91
|
+
|
|
92
|
+
- Identify all affected files, existing patterns, test coverage, and the dependency graph.
|
|
93
|
+
- Wait for the research to complete before proceeding.
|
|
94
|
+
|
|
95
|
+
### Phase 2: Implementation -- PARALLEL WHERE POSSIBLE
|
|
96
|
+
|
|
97
|
+
Maximize parallelism by partitioning work into file-isolated subsystems:
|
|
98
|
+
|
|
99
|
+
1. Analyze Phase 1 output. Partition work so no two workers edit the same file
|
|
100
|
+
(concurrent edits to the same file cause overwrites).
|
|
101
|
+
2. Track dependencies between tasks. Example:
|
|
102
|
+
```
|
|
103
|
+
Task 1: Backend models (no deps) -- starts immediately
|
|
104
|
+
Task 2: Backend services (depends on 1)
|
|
105
|
+
Task 3: Frontend types (depends on 2 -- needs API contract)
|
|
106
|
+
Task 4: Frontend components (depends on 3)
|
|
107
|
+
Task 5: Backend tests (depends on 2)
|
|
108
|
+
Task 6: Frontend tests (depends on 4)
|
|
109
|
+
```
|
|
110
|
+
When Task 2 completes, Tasks 3 AND 5 unblock in parallel -- maximum concurrency.
|
|
111
|
+
3. Assign each subsystem to a separate worker (e.g., backend worker, frontend worker).
|
|
112
|
+
4. Workers must know their assigned files AND which files not to touch.
|
|
113
|
+
5. Follow TDD (Rule 2) and test after each file change (Rule 3).
|
|
114
|
+
|
|
115
|
+
If parallel workers are not available, implement sequentially in dependency order:
|
|
116
|
+
- Backend: models -> services -> controllers/handlers -> middleware
|
|
117
|
+
- Frontend: types -> API client -> stores/hooks -> components -> pages
|
|
118
|
+
- Fullstack: backend first, then frontend consuming the new API
|
|
119
|
+
|
|
120
|
+
### Phases 3 + 4 + 5: Review Team -- PARALLEL
|
|
121
|
+
|
|
122
|
+
After Phase 2, perform three independent review passes in parallel.
|
|
123
|
+
Reviewers should challenge each other's findings where possible.
|
|
124
|
+
|
|
125
|
+
**Phase 3 -- Fact Check:**
|
|
126
|
+
Verify all planned changes were implemented. Check API contracts -- request/response
|
|
127
|
+
types must match between backend and frontend. Check i18n completeness.
|
|
128
|
+
|
|
129
|
+
**Phase 4 -- Security and Quality Audit:**
|
|
130
|
+
Audit for hardcoded secrets, unvalidated inputs, SQL injection, XSS vectors.
|
|
131
|
+
Check for efficient queries and no N+1 patterns. Verify naming and file-structure conventions.
|
|
132
|
+
|
|
133
|
+
**Phase 5 -- Readability and Consistency:**
|
|
134
|
+
Review naming against existing patterns. Check for small functions, clear names,
|
|
135
|
+
minimal nesting. Flag redundant comments or missing comments where logic is non-obvious.
|
|
136
|
+
|
|
137
|
+
### Phase 6: Synthesis -- SEQUENTIAL
|
|
138
|
+
|
|
139
|
+
After phases 3-5 complete:
|
|
140
|
+
|
|
141
|
+
- Read all review outputs and fix any bugs, inconsistencies, or issues raised.
|
|
142
|
+
- Run the full test suite one final time.
|
|
143
|
+
|
|
144
|
+
### Phase 7: Finalize -- SEQUENTIAL
|
|
145
|
+
|
|
146
|
+
- If the project uses containers: rebuild and verify all services are healthy.
|
|
147
|
+
- Smoke-test the new feature (manual or automated).
|
|
148
|
+
- Update project docs if the project tracks build status or feature summaries.
|
|
149
|
+
|
|
150
|
+
### Phase Dependencies
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
Phase 1 (research)
|
|
154
|
+
-> Phase 2 (parallel implementation via workers OR sequential)
|
|
155
|
+
-> Phases 3 + 4 + 5 (parallel reviews)
|
|
156
|
+
-> Phase 6 (synthesis + fix + final tests)
|
|
157
|
+
-> Phase 7 (finalize)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Enforcement
|
|
161
|
+
|
|
162
|
+
- Never skip phases. If a phase finds no issues, report that it ran clean.
|
|
163
|
+
- Report each phase with `[PROTOCOL Rule 4] Phase N: <description>...`
|
|
164
|
+
|
|
165
|
+
## Rule 5 -- UI and Layout Standards
|
|
166
|
+
|
|
167
|
+
When modifying frontend components:
|
|
168
|
+
|
|
169
|
+
- **Responsive testing**: Verify at 375px (mobile), 768px (tablet), and 1024px (desktop).
|
|
170
|
+
- **Layout stability**: No content overflow, clipped dropdowns, or scroll containers trapping positioned elements.
|
|
171
|
+
- **Overflow rules**: Prefer `overflow-x-clip` over `overflow-hidden` when clipping without a scroll container. Never use `overflow-x-auto` on wrappers containing absolutely-positioned menus.
|
|
172
|
+
- **Flexbox chain**: `h-screen` (root) -> `min-h-0` (flex items) -> `overflow-y-auto` (scrollable area).
|
|
173
|
+
- **Accessibility**: Semantic HTML, keyboard navigation, visible focus indicators, sufficient color contrast.
|
|
174
|
+
- **Consistency**: Follow the project's design system, color tokens, and component patterns.
|
|
175
|
+
- **i18n**: If the project uses internationalization, all user-facing strings must go through the translation system. Update ALL locale files when adding keys.
|
|
176
|
+
|
|
177
|
+
## Rule 6 -- Integration Testing
|
|
178
|
+
|
|
179
|
+
When adding or modifying API endpoints, services, or middleware:
|
|
180
|
+
|
|
181
|
+
### Container-First Testing (if applicable)
|
|
182
|
+
|
|
183
|
+
1. Start the project's container stack BEFORE running tests.
|
|
184
|
+
2. Verify all services are healthy.
|
|
185
|
+
3. Test new endpoints against live containers first -- catches issues that mocks hide (constraint violations, nullable columns, auth middleware).
|
|
186
|
+
4. Kill stale dev processes that may occupy the same ports.
|
|
187
|
+
|
|
188
|
+
### Backend Integration Tests
|
|
189
|
+
|
|
190
|
+
1. Use the project's test factory or fixture (e.g. WebApplicationFactory, TestServer, Supertest).
|
|
191
|
+
2. Mock external dependencies: databases (in-memory/SQLite), caches (no-op), job queues (no-op), third-party APIs (mock HTTP).
|
|
192
|
+
3. When adding new DI services, add corresponding test doubles in the test factory.
|
|
193
|
+
4. When adding constructor dependencies, fix ALL test files that mock that service.
|
|
194
|
+
5. Watch for global query filters, soft-delete scopes, or tenant isolation that behaves differently in test vs production.
|
|
195
|
+
|
|
196
|
+
### Frontend Test Isolation
|
|
197
|
+
|
|
198
|
+
1. Initialize i18n in test setup so translation keys resolve to readable strings.
|
|
199
|
+
2. Mock API calls (MSW, manual mocks, test interceptors) -- never hit real endpoints from unit/integration tests.
|
|
200
|
+
3. Test paginated API consumers: verify they handle the pagination wrapper (`.items`, `.data`, `.results`), not raw arrays.
|
|
201
|
+
4. For data-fetching libraries (React Query, SWR, Apollo), wrap in the appropriate provider with a fresh client per test.
|
|
202
|
+
|
|
203
|
+
### Common Pitfalls
|
|
204
|
+
|
|
205
|
+
- In-memory database providers skip migrations, constraints, and triggers -- guard migration calls with provider checks.
|
|
206
|
+
- Background job frameworks often need no-op storage set BEFORE the app starts in test mode.
|
|
207
|
+
- WebApplicationFactory patterns may require a `public partial class Program { }` sentinel.
|
|
208
|
+
- HTTP status codes matter for client interceptors (e.g. 401 may trigger auto-logout -- do not return 401 for validation errors).
|
|
209
|
+
|
|
210
|
+
## Quick Reference
|
|
211
|
+
|
|
212
|
+
| Situation | Required Rules |
|
|
213
|
+
|-----------|---------------|
|
|
214
|
+
| New entity + API + frontend | 0, 2, 3, 4 (full pipeline), 5, 6 |
|
|
215
|
+
| Multi-file feature (>2 files) | 0, 2, 3, 4 + 5/6 if applicable |
|
|
216
|
+
| Bug fix (1-2 files) | 0, 1, 2, 3 |
|
|
217
|
+
| UI-only change (1-2 files) | 0, 1, 2, 3, 5 |
|
|
218
|
+
| UI change (>2 files) | 0, 2, 3, 4, 5 |
|
|
219
|
+
| Backend-only change (1-2 files) | 0, 1, 2, 3, 6 |
|
|
220
|
+
| Backend change (>2 files) | 0, 2, 3, 4, 6 |
|
|
221
|
+
| New API endpoint | 0, 1, 2, 3, 6 |
|
|
222
|
+
| Refactor (>2 files) | 0, 3, 4 + full test suite before AND after |
|
|
223
|
+
| Refactor (1-2 files) | 0, 1, 3 + full test suite before AND after |
|
package/README.md
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
# Claude Code Development Workflow Hook
|
|
2
|
+
|
|
3
|
+
## Purpose
|
|
4
|
+
|
|
5
|
+
A `SessionStart` hook that injects a structured development workflow protocol once per Claude Code session. It enforces planning, TDD, recursive testing, UI standards, agent team orchestration, and integration testing — automatically, project-agnostically.
|
|
6
|
+
|
|
7
|
+
> **Why SessionStart?** The protocol injects once per session (on startup, resume, clear, and compact) rather than on every prompt. This saves significant context window budget over long conversations. `SessionStart` supports `additionalContext` injection via `hookSpecificOutput`.
|
|
8
|
+
|
|
9
|
+
## Language / Stack
|
|
10
|
+
|
|
11
|
+
- **Languages:** Node.js (primary), Python 3 (alternative)
|
|
12
|
+
- **Runtime:** Claude Code CLI hook system (`SessionStart` event)
|
|
13
|
+
- **Platforms:** Windows, macOS, Linux
|
|
14
|
+
- **Dependencies:** None (Node.js built-ins only / Python stdlib only)
|
|
15
|
+
|
|
16
|
+
## AGENTS.md (Platform-Agnostic Version)
|
|
17
|
+
|
|
18
|
+
This tool also ships an `AGENTS.md` file — a portable, plain-Markdown version of the same workflow protocol that works with **any AI coding agent** (GitHub Copilot, Cursor, Codex, Google Jules, and [20+ other platforms](https://agents.md)).
|
|
19
|
+
|
|
20
|
+
### How to use it
|
|
21
|
+
|
|
22
|
+
Copy `AGENTS.md` into the root of any repository where you want agents to follow the protocol:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
cp AGENTS.md /path/to/your/project/AGENTS.md
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
That's it. No hooks, no installer, no authority binding — agents that support the [AGENTS.md standard](https://agents.md) will read it automatically.
|
|
29
|
+
|
|
30
|
+
### When to use which
|
|
31
|
+
|
|
32
|
+
| Approach | Best for | How it works |
|
|
33
|
+
|----------|----------|--------------|
|
|
34
|
+
| **Hook** (Node.js or Python) | Claude Code users | Injected once per session via `SessionStart` + CLAUDE.md authority binding |
|
|
35
|
+
| **AGENTS.md** | Any AI agent | Placed at repo root; agents read it as project context automatically |
|
|
36
|
+
|
|
37
|
+
You can use both — they contain the same rules. The hook enforces via injection; the AGENTS.md file relies on the agent reading it from the repo.
|
|
38
|
+
|
|
39
|
+
### Nested AGENTS.md
|
|
40
|
+
|
|
41
|
+
For monorepos, place an AGENTS.md in subdirectories to override or extend the root rules. The closest file to the edited code takes precedence.
|
|
42
|
+
|
|
43
|
+
## What's Inside
|
|
44
|
+
|
|
45
|
+
7 mandatory rules wrapped in `<dev-workflow-protocol>` XML tags with compliance reporting:
|
|
46
|
+
|
|
47
|
+
| Rule | Name | Purpose |
|
|
48
|
+
|------|------|---------|
|
|
49
|
+
| 0 | Plan Before Acting | Identify files, map dependencies, enter Plan Mode if >2 files |
|
|
50
|
+
| 1 | Small Task Exception | Skip team orchestration for <=2 file changes (still TDD + test) |
|
|
51
|
+
| 2 | TDD Enforcement | RED -> GREEN -> IMPROVE cycle, 80%+ coverage target |
|
|
52
|
+
| 3 | Test After Every Change | Run tests after EVERY change, never stack untested changes |
|
|
53
|
+
| 4 | Agent Team Orchestration | 7-phase pipeline with parallel implementation via Agent Teams (subagent fallback) |
|
|
54
|
+
| 5 | UI & Layout Standards | Responsive testing, overflow rules, accessibility, i18n |
|
|
55
|
+
| 6 | Integration Testing | Container-first testing, backend/frontend test isolation |
|
|
56
|
+
|
|
57
|
+
The **Compliance Reporting** section requires the model to prefix actions with `[PROTOCOL Rule N]`, making rule adherence visible.
|
|
58
|
+
|
|
59
|
+
## Prerequisites
|
|
60
|
+
|
|
61
|
+
- [Claude Code CLI](https://docs.anthropic.com/en/docs/claude-code) installed
|
|
62
|
+
- **Node.js 18+** (for npm install) OR **Python 3** (for Python install)
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Install via npm (Recommended)
|
|
67
|
+
|
|
68
|
+
Single command — no clone needed:
|
|
69
|
+
|
|
70
|
+
**Global install** (all projects):
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
npx @thecodesaiyan/claude-dev-workflow-hook --global
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Per-project install** (current project only):
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
npx @thecodesaiyan/claude-dev-workflow-hook --project
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Uninstall via npm
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
npx @thecodesaiyan/claude-dev-workflow-hook --uninstall --scope global
|
|
86
|
+
npx @thecodesaiyan/claude-dev-workflow-hook --uninstall --scope project
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Install via Python (Alternative)
|
|
92
|
+
|
|
93
|
+
If you prefer Python or don't have Node.js:
|
|
94
|
+
|
|
95
|
+
### From a local clone
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
git clone https://github.com/ntatschner/ai-utilities.git
|
|
99
|
+
cd ai-utilities/tools/claude-dev-workflow-hook
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Then run the installer:
|
|
103
|
+
|
|
104
|
+
**Global install** (all projects):
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
# macOS / Linux
|
|
108
|
+
python3 install.py --global
|
|
109
|
+
|
|
110
|
+
# Windows
|
|
111
|
+
py install.py --global
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**Per-project install** (current project only):
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
# macOS / Linux
|
|
118
|
+
python3 install.py --project
|
|
119
|
+
|
|
120
|
+
# Windows
|
|
121
|
+
py install.py --project
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
**Interactive** (prompts you):
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
# macOS / Linux
|
|
128
|
+
python3 install.py
|
|
129
|
+
|
|
130
|
+
# Windows
|
|
131
|
+
py install.py
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Remote one-liner
|
|
135
|
+
|
|
136
|
+
**macOS / Linux:**
|
|
137
|
+
```bash
|
|
138
|
+
bash <(curl -fsSL https://raw.githubusercontent.com/ntatschner/ai-utilities/main/tools/claude-dev-workflow-hook/install-remote.sh) --global
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
**Windows (PowerShell):**
|
|
142
|
+
```powershell
|
|
143
|
+
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/ntatschner/ai-utilities/main/tools/claude-dev-workflow-hook/install-remote.ps1" -OutFile "$env:TEMP\install-remote.ps1"; & "$env:TEMP\install-remote.ps1" -Scope Global
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Uninstall via Python
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
# macOS / Linux
|
|
150
|
+
python3 install.py --uninstall --scope global # or --scope project
|
|
151
|
+
|
|
152
|
+
# Windows
|
|
153
|
+
py install.py --uninstall --scope global # or --scope project
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## What the installer does
|
|
159
|
+
|
|
160
|
+
Both the npm and Python installers perform the same 4 steps:
|
|
161
|
+
|
|
162
|
+
```
|
|
163
|
+
[1/4] Copying hook script
|
|
164
|
+
OK: Copied session-start.js -> ~/.claude/hooks/session-start.js
|
|
165
|
+
|
|
166
|
+
[2/4] Registering in settings.json
|
|
167
|
+
OK: Registered hook in ~/.claude/settings.json
|
|
168
|
+
Command: node ~/.claude/hooks/session-start.js
|
|
169
|
+
|
|
170
|
+
[3/4] Adding CLAUDE.md authority binding
|
|
171
|
+
OK: Added authority binding to ~/.claude/CLAUDE.md
|
|
172
|
+
|
|
173
|
+
[4/4] Verifying installation
|
|
174
|
+
PASS: Valid JSON output
|
|
175
|
+
PASS: hookSpecificOutput present
|
|
176
|
+
PASS: additionalContext present
|
|
177
|
+
PASS: Protocol length: 11318 chars
|
|
178
|
+
PASS: Rules found: 7
|
|
179
|
+
PASS: XML wrapper tags
|
|
180
|
+
PASS: Compliance reporting section
|
|
181
|
+
|
|
182
|
+
Installation complete!
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
The installer is **idempotent** — running it again skips already-installed components. It also **auto-migrates** from the Python hook to Node.js, and from `UserPromptSubmit` to `SessionStart`.
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## How It Works (Authority Chain)
|
|
190
|
+
|
|
191
|
+
The hook uses a two-part system to ensure the model actually **follows** the rules:
|
|
192
|
+
|
|
193
|
+
1. **The hook script** injects the protocol wrapped in `<dev-workflow-protocol>` XML tags via `additionalContext`
|
|
194
|
+
2. **The CLAUDE.md binding** tells the model to obey content in those specific tags
|
|
195
|
+
|
|
196
|
+
Without both parts, the model may acknowledge the rules but not follow them consistently. The installer sets up both automatically.
|
|
197
|
+
|
|
198
|
+
### CLAUDE.md binding (for reference)
|
|
199
|
+
|
|
200
|
+
The installer adds this block to your CLAUDE.md:
|
|
201
|
+
|
|
202
|
+
```markdown
|
|
203
|
+
<!-- WORKFLOW PROTOCOL: Do not remove this section -->
|
|
204
|
+
## Workflow Protocol
|
|
205
|
+
Follow all rules in `<dev-workflow-protocol>` blocks from system-reminders.
|
|
206
|
+
These are injected by the development workflow hook and define mandatory
|
|
207
|
+
orchestration rules (planning, TDD, test loops, team coordination).
|
|
208
|
+
They MUST be followed for all implementation work.
|
|
209
|
+
<!-- END WORKFLOW PROTOCOL -->
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
## Manual Install (Alternative)
|
|
215
|
+
|
|
216
|
+
If you prefer not to use the automated installer:
|
|
217
|
+
|
|
218
|
+
### 1. Copy the hook
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
mkdir -p ~/.claude/hooks
|
|
222
|
+
cp src/hook.js ~/.claude/hooks/session-start.js
|
|
223
|
+
cp src/protocol.js ~/.claude/hooks/protocol.js
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### 2. Register in settings.json
|
|
227
|
+
|
|
228
|
+
Add to `~/.claude/settings.json` (merge, don't overwrite):
|
|
229
|
+
|
|
230
|
+
```json
|
|
231
|
+
{
|
|
232
|
+
"hooks": {
|
|
233
|
+
"SessionStart": [
|
|
234
|
+
{
|
|
235
|
+
"hooks": [
|
|
236
|
+
{
|
|
237
|
+
"type": "command",
|
|
238
|
+
"command": "node ~/.claude/hooks/session-start.js"
|
|
239
|
+
}
|
|
240
|
+
]
|
|
241
|
+
}
|
|
242
|
+
]
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### 3. Add CLAUDE.md binding
|
|
248
|
+
|
|
249
|
+
Add the `<!-- WORKFLOW PROTOCOL -->` block shown above to your project's `CLAUDE.md` or `~/.claude/CLAUDE.md`.
|
|
250
|
+
|
|
251
|
+
### 4. Verify
|
|
252
|
+
|
|
253
|
+
```bash
|
|
254
|
+
echo '{}' | node ~/.claude/hooks/session-start.js
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
## Per-Project Install
|
|
260
|
+
|
|
261
|
+
For a single project instead of global:
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
npx @thecodesaiyan/claude-dev-workflow-hook --project
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
Or manually:
|
|
268
|
+
- Place the hook in `.claude/hooks/session-start.js` (relative to project root)
|
|
269
|
+
- Copy `protocol.js` alongside it
|
|
270
|
+
- Register in `.claude/settings.json` (project-level, not `~/.claude/`)
|
|
271
|
+
- Use relative path: `node .claude/hooks/session-start.js`
|
|
272
|
+
|
|
273
|
+
Per-project hooks are committed to version control, so the whole team gets them.
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
|
|
277
|
+
## Customization
|
|
278
|
+
|
|
279
|
+
### Add project-specific test commands
|
|
280
|
+
|
|
281
|
+
Edit `src/protocol.js` (or `session-start.py`) and add to the "Auto-Detect Test Runner" section.
|
|
282
|
+
|
|
283
|
+
### Add project-specific UI rules
|
|
284
|
+
|
|
285
|
+
Append to Rule 5 in the `PROTOCOL` string.
|
|
286
|
+
|
|
287
|
+
### Add project-specific integration test patterns
|
|
288
|
+
|
|
289
|
+
Append to Rule 6 in the `PROTOCOL` string.
|
|
290
|
+
|
|
291
|
+
### Disable specific rules
|
|
292
|
+
|
|
293
|
+
Remove the rule block from the `PROTOCOL` string. Rules are independent. Update the Quick Reference table to match.
|
|
294
|
+
|
|
295
|
+
> **Tip:** For project-specific additions, create a second hook rather than modifying the generic one.
|
|
296
|
+
|
|
297
|
+
---
|
|
298
|
+
|
|
299
|
+
## Migration
|
|
300
|
+
|
|
301
|
+
### From the Python hook
|
|
302
|
+
|
|
303
|
+
The npm installer automatically migrates:
|
|
304
|
+
- Detects `session-start.py` under `SessionStart` and replaces with `session-start.js`
|
|
305
|
+
- No manual steps needed — just run `npx @thecodesaiyan/claude-dev-workflow-hook --global`
|
|
306
|
+
|
|
307
|
+
### From the UserPromptSubmit version
|
|
308
|
+
|
|
309
|
+
Both installers automatically migrate:
|
|
310
|
+
- Remove the old `UserPromptSubmit` entry
|
|
311
|
+
- Register under `SessionStart` instead
|
|
312
|
+
- The new registration injects once per session instead of on every prompt
|
|
313
|
+
|
|
314
|
+
### From the Bash version
|
|
315
|
+
|
|
316
|
+
1. Run `npx @thecodesaiyan/claude-dev-workflow-hook --global`
|
|
317
|
+
2. Remove the old bash hook entry from `settings.json`
|
|
318
|
+
3. Delete `~/.claude/hooks/session-start.sh`
|
|
319
|
+
|
|
320
|
+
---
|
|
321
|
+
|
|
322
|
+
## Troubleshooting
|
|
323
|
+
|
|
324
|
+
### "node: not found"
|
|
325
|
+
|
|
326
|
+
Install Node.js 18+ from [nodejs.org](https://nodejs.org/).
|
|
327
|
+
|
|
328
|
+
### "python3: not found" / "py: not found"
|
|
329
|
+
|
|
330
|
+
- **macOS:** `brew install python3`
|
|
331
|
+
- **Linux:** `apt install python3` or `dnf install python3`
|
|
332
|
+
- **Windows:** Install from [python.org](https://www.python.org/downloads/) (includes `py` launcher)
|
|
333
|
+
|
|
334
|
+
### Model acknowledges rules but doesn't follow them
|
|
335
|
+
|
|
336
|
+
Missing CLAUDE.md authority binding. Run the installer again or add it manually (see above).
|
|
337
|
+
|
|
338
|
+
### Model doesn't show `[PROTOCOL Rule N]` prefixes
|
|
339
|
+
|
|
340
|
+
1. Verify the hook is injecting: ask "What workflow rules are active?"
|
|
341
|
+
2. Verify the CLAUDE.md binding exists
|
|
342
|
+
3. Add `"You MUST show [PROTOCOL Rule N] prefixes for EVERY action"` to your CLAUDE.md
|
|
343
|
+
|
|
344
|
+
### Multiple hooks conflicting
|
|
345
|
+
|
|
346
|
+
`SessionStart` hooks run in array order. Each hook's `additionalContext` is concatenated. If you see duplicate rules, consolidate into a single hook.
|
|
347
|
+
|
|
348
|
+
### Hook doesn't run
|
|
349
|
+
|
|
350
|
+
1. Verify `settings.json` is valid JSON (no trailing commas)
|
|
351
|
+
2. Check the path matches where you placed the script
|
|
352
|
+
3. Restart Claude Code after modifying `settings.json`
|
package/bin/cli.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* CLI entry point for @thecodesaiyan/claude-dev-workflow-hook
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* npx @thecodesaiyan/claude-dev-workflow-hook --global
|
|
9
|
+
* npx @thecodesaiyan/claude-dev-workflow-hook --project
|
|
10
|
+
* npx @thecodesaiyan/claude-dev-workflow-hook --uninstall --scope global
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const { install, getClaudeHome, getProjectClaude } = require('../src/installer.js');
|
|
14
|
+
const { uninstall } = require('../src/uninstaller.js');
|
|
15
|
+
const { bold } = require('../src/utils.js');
|
|
16
|
+
|
|
17
|
+
function printUsage() {
|
|
18
|
+
console.log(bold('Claude Code Development Workflow Hook'));
|
|
19
|
+
console.log('');
|
|
20
|
+
console.log('Usage:');
|
|
21
|
+
console.log(' npx @thecodesaiyan/claude-dev-workflow-hook --global Install globally (~/.claude/)');
|
|
22
|
+
console.log(' npx @thecodesaiyan/claude-dev-workflow-hook --project Install in current project (.claude/)');
|
|
23
|
+
console.log(' npx @thecodesaiyan/claude-dev-workflow-hook --uninstall --scope global');
|
|
24
|
+
console.log(' npx @thecodesaiyan/claude-dev-workflow-hook --uninstall --scope project');
|
|
25
|
+
console.log(' npx @thecodesaiyan/claude-dev-workflow-hook --help Show this help');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function parseArgs(argv) {
|
|
29
|
+
const args = argv.slice(2);
|
|
30
|
+
const result = {
|
|
31
|
+
global: false,
|
|
32
|
+
project: false,
|
|
33
|
+
uninstall: false,
|
|
34
|
+
scope: null,
|
|
35
|
+
help: false,
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
for (let i = 0; i < args.length; i++) {
|
|
39
|
+
switch (args[i]) {
|
|
40
|
+
case '--global':
|
|
41
|
+
result.global = true;
|
|
42
|
+
break;
|
|
43
|
+
case '--project':
|
|
44
|
+
result.project = true;
|
|
45
|
+
break;
|
|
46
|
+
case '--uninstall':
|
|
47
|
+
result.uninstall = true;
|
|
48
|
+
break;
|
|
49
|
+
case '--scope':
|
|
50
|
+
result.scope = args[++i];
|
|
51
|
+
break;
|
|
52
|
+
case '--help':
|
|
53
|
+
case '-h':
|
|
54
|
+
result.help = true;
|
|
55
|
+
break;
|
|
56
|
+
default:
|
|
57
|
+
console.error(`Unknown argument: ${args[i]}`);
|
|
58
|
+
printUsage();
|
|
59
|
+
process.exit(1);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function main() {
|
|
67
|
+
const args = parseArgs(process.argv);
|
|
68
|
+
|
|
69
|
+
if (args.help) {
|
|
70
|
+
printUsage();
|
|
71
|
+
process.exit(0);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
console.log(bold('Claude Code Development Workflow Hook'));
|
|
75
|
+
console.log(`Node.js: ${process.version} | Platform: ${process.platform}`);
|
|
76
|
+
|
|
77
|
+
if (args.uninstall) {
|
|
78
|
+
if (!args.scope) {
|
|
79
|
+
console.error('Error: --uninstall requires --scope (global or project)');
|
|
80
|
+
printUsage();
|
|
81
|
+
process.exit(1);
|
|
82
|
+
}
|
|
83
|
+
if (!['global', 'project'].includes(args.scope)) {
|
|
84
|
+
console.error(`Error: --scope must be 'global' or 'project', got: '${args.scope}'`);
|
|
85
|
+
process.exit(1);
|
|
86
|
+
}
|
|
87
|
+
const targetDir = args.scope === 'global' ? getClaudeHome() : getProjectClaude();
|
|
88
|
+
uninstall(targetDir);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (args.global && args.project) {
|
|
93
|
+
console.error('Error: --global and --project are mutually exclusive');
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!args.global && !args.project) {
|
|
98
|
+
console.error('Error: specify --global or --project');
|
|
99
|
+
printUsage();
|
|
100
|
+
process.exit(1);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const scopeIsGlobal = args.global;
|
|
104
|
+
const targetDir = scopeIsGlobal ? getClaudeHome() : getProjectClaude();
|
|
105
|
+
install(targetDir, scopeIsGlobal);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
main();
|