Skip to content

Commit 65d8ab0

Browse files
cahaselerclaude
andauthored
feat: implement autoflow mode hooks for autonomous operation (#179)
* feat: add autoflow mode specification - Create comprehensive spec for autoflow mode feature - Define user stories and acceptance criteria - Document safety rules and constraints - Specify infinite loop prevention mechanisms - Add work completion detection requirements * feat: add technical plan for autoflow mode - Define three-hook architecture (UserPromptSubmit, PermissionRequest, Stop) - Design state management with .autoflow-state.json - Create safety rules system for auto-approval - Implement infinite loop detection algorithm - Plan statusline integration and testing strategy - Estimate 14-20 hours implementation effort * wip: autoflow state management (incorrect approach, will revise) * feat: implement autoflow mode hooks Implements simplified autoflow mode with three hooks: - UserPromptSubmit: Detect 'autoflow' to activate/deactivate - PermissionRequest: Deny permissions with 'user unavailable' message - Stop: Use Claude SDK to evaluate if Claude is done or asking to continue State tracked in simple JSON file (.cc-track/.autoflow-state.json): - active: boolean - stopCount: number for loop detection - lastStop: timestamp for 30-second window Loop detection: >2 stops in 30 seconds auto-deactivates autoflow Key files: - hooks/user-message.ts - Activation control - hooks/permission-request.ts - Permission denial - hooks/stop.ts - Stop evaluation with Claude SDK - hooks/hooks.json - Hook registration - .cc-track/specs/113-autoflow-mode-hooks/README.md - User guide * docs: rewrite plan.md to match actual implementation and remove unused code - Rewrote plan.md to describe the simple implementation (not complex architecture) - Removed unused autoflow-state.ts file (344 lines not needed) - Plan now accurately reflects what was built: simple hooks with JSON state * feat: implement autoflow mode hooks for autonomous operation Add three hooks that enable Claude Code to work autonomously with minimal user intervention: - UserPromptSubmit hook: Per-message activation via "autoflow" keyword with negation handling (don't/no/without autoflow won't activate) - PermissionRequest hook: Denies permissions with "find safe alternative" guidance when autoflow active - Stop hook: Uses Claude SDK (haiku) to evaluate if Claude should continue or stop, with throttle detection (3 continues per 5-minute window) Key features: - Per-message opt-in (not persistent mode) for natural control - Battle-tested evaluation prompt from double-shot-latte project - Statusline indicator showing autoflow state (🤖 Autoflow N/3) - Comprehensive unit tests (53 tests across 3 test files) - Fixed Claude SDK stale fnm path issue on Windows - Fixed transcript parsing for SDK message format 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * docs: update spec to match implemented permission denial approach - FR2: Changed from auto-approval to permission denial approach - FR4: Updated to throttle detection (3/5min) from loop detection (2/30s) - Updated success criteria to reflect implemented features - Updated risks table for denial-based approach - Added design rationale for simplified permission handling The implementation denies all permission requests when autoflow is active rather than auto-approving safe operations. This is simpler and respects user's pre-configured permission boundaries in settings.json. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Craig Haseler <cahaseler@users.noreply.github.com>
1 parent 3c12dcc commit 65d8ab0

File tree

22 files changed

+2538
-9
lines changed

22 files changed

+2538
-9
lines changed

.cc-track/code_index.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
| hooks/edit-validation.ts | Real-time TypeScript and Biome validation on edits |
4040
| hooks/pre-tool-validation.ts | Branch protection and task file validation |
4141
| hooks/session-start.ts | Session initialization |
42+
| hooks/user-message.ts | Autoflow activation/deactivation via keyword detection |
43+
| hooks/permission-request.ts | Autoflow permission denial with safe alternative guidance |
44+
| hooks/stop.ts | Autoflow stop evaluation using Claude SDK |
4245

4346
### Scripts (scripts/)
4447
| File | Purpose |
@@ -112,4 +115,5 @@ All shared code lives in the skill's lib directory. Hooks and scripts import fro
112115
[2025-09-11 19:30] - Migrated all code to src/ directory, removed legacy .claude script locations
113116
[2025-09-12 21:30] - Added log parser library and parse-logs command
114117
[2025-12-06] - Added skills/ directory for cc-track-tools skill with utility scripts
115-
[2025-12-06] - Consolidated lib/ into skills/cc-track-tools/lib/ as single source of truth
118+
[2025-12-06] - Consolidated lib/ into skills/cc-track-tools/lib/ as single source of truth
119+
[2025-12-19] - Added autoflow hooks (user-message.ts, permission-request.ts, stop.ts)

.cc-track/product_context.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ Solve the fundamental problem of context loss in Claude Code sessions through in
6565
- One-command initialization (`/init-track`)
6666
- Safe file handling with backups and validation
6767

68+
### Autoflow Mode
69+
- Per-message opt-in autonomous operation ("autoflow" keyword)
70+
- Permission denial with "find safe workaround" guidance
71+
- Intelligent stop evaluation using Claude SDK
72+
- Throttle detection (3 continuations per 5-minute window)
73+
- Automatic deactivation on task completion or throttle limit
74+
6875
## Non-Goals / Out of Scope
6976

7077
- Strict TDD enforcement (offers guidance, not enforcement)
@@ -88,4 +95,5 @@ Solve the fundamental problem of context loss in Claude Code sessions through in
8895

8996
[2025-01-09 16:30] - Initial product context documented based on project analysis
9097
[2025-01-10] - Renamed project from cc-pars to cc-track with new branding
91-
[2025-09-10] - Updated features to reflect actual implementation: real-time validation, two-line statusline, train theming, configurable API timer
98+
[2025-09-10] - Updated features to reflect actual implementation: real-time validation, two-line statusline, train theming, configurable API timer
99+
[2025-12-19] - Added Autoflow Mode feature: per-message autonomous operation with intelligent stop evaluation
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"task_id": "113",
3+
"feature_name": "autoflow-mode-hooks",
4+
"branch": "113-autoflow-mode-hooks",
5+
"status": "in_progress",
6+
"started": "2025-12-19T11:30:00.000Z"
7+
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
# Autoflow Mode - Quick Start Guide
2+
3+
## What is Autoflow Mode?
4+
5+
Autoflow mode enables Claude Code to work autonomously with minimal user interaction. When activated:
6+
7+
- 🤖 Claude works through tasks without asking "should I continue?"
8+
- 🚫 Permission requests are denied with "user not available, find a safe workaround"
9+
- 🔁 Claude automatically continues when stopping to ask for confirmation
10+
- 🛡️ Throttle detection prevents runaway sessions (3 continues per 5 minutes)
11+
12+
**Key design:** Autoflow is **per-message opt-in**, not a persistent mode. Each user message must include "autoflow" to keep the mode active. This gives you natural control - just send a message without "autoflow" to regain manual control.
13+
14+
## How to Use
15+
16+
### Activate Autoflow Mode
17+
18+
Include `autoflow` anywhere in your message to Claude:
19+
20+
```
21+
Please implement the user authentication feature. autoflow
22+
```
23+
24+
Claude will respond with:
25+
```
26+
🤖 Autoflow mode active for this task.
27+
```
28+
29+
### Keeping Autoflow Active
30+
31+
If Claude completes the task and you want to continue autonomously:
32+
```
33+
Great, now add the tests. autoflow
34+
```
35+
36+
### Regaining Manual Control
37+
38+
Simply send any message without "autoflow":
39+
```
40+
Wait, let me review what you did first.
41+
```
42+
43+
Autoflow silently deactivates and Claude waits for your input normally.
44+
45+
### What Happens in Autoflow Mode
46+
47+
1. **Permission Requests**: If Claude tries to do something requiring permission, it will be denied with a message:
48+
> "User is not available. Please find a safe alternative approach to accomplish this goal."
49+
50+
2. **Stop Events**: When Claude stops to ask "Should I continue?", the hook:
51+
- Reads recent conversation messages
52+
- Uses Claude SDK (haiku) to evaluate if really done or just asking
53+
- If asking to continue: Blocks stop with "Continue working according to the plan"
54+
- If genuinely done: Allows stop
55+
56+
3. **Throttle Detection**: If Claude auto-continues 3 times within 5 minutes:
57+
- Autoflow mode automatically deactivates
58+
- Claude is shown: "⚠️ Autoflow throttle limit reached"
59+
60+
### Deactivating Autoflow Mode
61+
62+
Autoflow deactivates automatically when:
63+
64+
1. **You send a message without "autoflow"** - Most natural way
65+
2. **Throttle limit reached** - 3 auto-continues in 5 minutes triggers safety stop
66+
3. **Work is genuinely complete** - Claude finishes and the Stop hook allows it
67+
68+
## Prerequisites
69+
70+
Before using autoflow mode, you should:
71+
72+
1. **Configure Claude Code permissions** sensibly in `.claude/settings.json`:
73+
```json
74+
{
75+
"allow": [
76+
"Read",
77+
"Grep",
78+
"Glob",
79+
"Edit",
80+
"Write",
81+
"Bash(git:*)",
82+
"Bash(npm:*)",
83+
"Bash(bun:*)"
84+
],
85+
"ask": [
86+
"Bash(rm:*)",
87+
"Bash(curl:*)"
88+
]
89+
}
90+
```
91+
92+
2. **Have a clear plan** - Autoflow works best when there's a defined task list (e.g., `tasks.md`)
93+
94+
## How It Works (Technical)
95+
96+
### Hooks
97+
98+
Three hooks power autoflow mode:
99+
100+
1. **UserPromptSubmit** (`hooks/user-message.ts`):
101+
- Detects "autoflow" → writes `{active: true}` to `.cc-track/.autoflow-state.json`
102+
- Detects "stop autoflow" → writes `{active: false}`
103+
104+
2. **PermissionRequest** (`hooks/permission-request.ts`):
105+
- If autoflow active → DENY with helpful message
106+
- If not active → normal permission flow
107+
108+
3. **Stop** (`hooks/stop.ts`):
109+
- If autoflow active:
110+
- Read transcript to get recent messages
111+
- Call Claude SDK: "Is this agent done or asking to continue?"
112+
- If asking to continue → BLOCK (exit code 2) with continuation message
113+
- If really done → allow stop
114+
- Track stop count for loop detection
115+
116+
### State File
117+
118+
`.cc-track/.autoflow-state.json`:
119+
```json
120+
{
121+
"active": true,
122+
"sessionId": "abc123",
123+
"activatedAt": "2025-12-19T12:00:00.000Z",
124+
"continueCount": 1,
125+
"windowStart": "2025-12-19T12:00:00.000Z"
126+
}
127+
```
128+
129+
## Troubleshooting
130+
131+
### Autoflow keeps stopping
132+
133+
If Claude stops repeatedly:
134+
- Throttle detection will auto-deactivate after 3 auto-continues in 5 minutes
135+
- Check if there's a blocker preventing progress (missing file, unclear requirement)
136+
- Review the task list - Claude might genuinely be done
137+
138+
### Permissions blocking progress
139+
140+
If a permission denial is blocking important work:
141+
1. Type `stop autoflow` to deactivate
142+
2. Manually approve the operation
143+
3. Optionally add to your `.claude/settings.json` allow list
144+
4. Type `autoflow` to re-activate
145+
146+
### Claude isn't continuing
147+
148+
The Stop hook uses Claude SDK to evaluate stops. If it's not working:
149+
- Check logs in `~/.local/share/cc-track/logs/` (Linux) or platform equivalent
150+
- Verify Claude SDK is installed: `bun pm ls @anthropic-ai/claude-agent-sdk`
151+
- Check transcript is accessible at the path provided in hook input
152+
153+
## Examples
154+
155+
### Example 1: Feature Implementation
156+
157+
```
158+
User: Please implement the shopping cart feature according to spec.md. autoflow
159+
160+
[Claude activates autoflow, reads spec, starts implementing]
161+
[Claude encounters permission request for editing package.json]
162+
[Permission denied with "find safe workaround" message]
163+
[Claude continues using existing dependencies]
164+
[Claude stops after completing all tasks]
165+
```
166+
167+
### Example 2: Bug Fix
168+
169+
```
170+
User: Fix the authentication bug described in issue #123. autoflow
171+
172+
[Claude activates autoflow, reads issue, investigates]
173+
[Claude makes fixes, writes tests]
174+
[Claude stops to ask "Should I run the test suite?"]
175+
[Stop hook evaluates: Claude is asking to continue]
176+
[Stop blocked with "Yes, continue working"]
177+
[Claude runs tests, sees pass, genuinely done]
178+
[Stop hook evaluates: Claude is done]
179+
[Stop allowed]
180+
```
181+
182+
## Limitations
183+
184+
- **Cannot override pre-configured permissions**: Only denies additional permission requests
185+
- **Relies on Claude SDK**: Requires working Claude SDK installation for stop evaluation
186+
- **Best with clear tasks**: Works best when there's a defined plan or task list
187+
- **Throttle-based safety**: 3 continuations per 5-minute window before auto-deactivation
188+
189+
## Configuration
190+
191+
Currently, autoflow mode uses hardcoded defaults:
192+
- Throttle limit: 3 continuations per 5-minute window (from double-shot-latte testing)
193+
- Claude SDK model: haiku (fast, cheap)
194+
- Evaluation timeout: 15 seconds
195+
196+
Future versions may expose these in `track.config.json`.
197+
198+
## Activation Patterns
199+
200+
The keyword detection is smart about negations:
201+
202+
**Will activate:**
203+
- "implement the feature autoflow"
204+
- "autoflow please"
205+
- "let's go autoflow"
206+
207+
**Will NOT activate (negations):**
208+
- "don't use autoflow"
209+
- "no autoflow"
210+
- "without autoflow"
211+
- "not autoflow"
212+
213+
**Will deactivate (any message without "autoflow"):**
214+
- "looks good, what's next?"
215+
- "wait, let me check something"
216+
- Any follow-up question or comment

0 commit comments

Comments
 (0)