sequenceDiagram
    autonumber
    title Auto Mode Flow (Stop Hook Feedback Loop)

    actor Developer
    participant CLI as specweave CLI
    participant State as auto-mode.json
    participant Claude as Claude Code
    participant Reflect as stop-reflect.sh
    participant AutoHook as stop-auto.sh
    participant Sync as stop-sync.sh
    participant Tasks as tasks.md
    participant Meta as MetadataManager

    Developer->>CLI: /sw:auto

    CLI->>State: Create auto-mode.json
    Note over State: active: true<br/>maxTurns: 20

    CLI->>Claude: Start session with auto prompt

    Note over Claude: Stop Hook Feedback Loop

    loop While tasks remain and turns < max
        Claude->>Tasks: Read tasks.md
        Tasks-->>Claude: Find next [ ] pending task

        alt Task found
            Claude->>Claude: Execute task implementation
            Claude->>Tasks: Mark task [x] complete

            Note over Claude: Session naturally ends

            Claude->>Reflect: Stop hook 1: reflect
            activate Reflect
            Reflect->>Reflect: Extract learnings from session
            Reflect->>Reflect: Queue for async processing
            Reflect-->>Claude: decision: approve
            deactivate Reflect

            Claude->>AutoHook: Stop hook 2: auto
            activate AutoHook

            AutoHook->>Tasks: Check remaining tasks
            Tasks-->>AutoHook: 3 tasks remaining

            AutoHook->>State: Update turn count
            State-->>AutoHook: turn: 5

            AutoHook->>AutoHook: Build continuation prompt

            AutoHook-->>Claude: decision: block<br/>systemMessage: Continue with next task
            deactivate AutoHook

            Note over Claude: Session continues (not terminated)

        else All tasks complete
            Claude->>Meta: Transition to READY_FOR_REVIEW
            Meta-->>Claude: Status updated

            Note over Claude: Session ends

            Claude->>Reflect: Stop hook 1: reflect
            Reflect-->>Claude: approve (learnings queued)

            Claude->>AutoHook: Stop hook 2: auto
            activate AutoHook
            AutoHook->>Tasks: Check remaining tasks
            Tasks-->>AutoHook: 0 tasks remaining

            AutoHook->>State: Mark session inactive
            AutoHook-->>Claude: decision: approve<br/>reason: All tasks complete
            deactivate AutoHook

            Claude->>Sync: Stop hook 3: sync
            Sync-->>Claude: approve (changes synced)

            Claude-->>Developer: Auto session complete
        end
    end

    Note over Developer: Increment in READY_FOR_REVIEW<br/>Run /sw:done to complete
