#!/bin/bash # Curation System: capture-turn (Stop Hook) # Dual-append: writes the assistant turn to BOTH the memory queue and the # curation queue, each independently gated by its own feature flag (AC-F4). The # 120s-throttle/nohup-spawn block lives in memory-worker, a separate Stop # hook that Claude Code runs in parallel with this one; the worker tolerates a # queue this hook has not appended to yet (D-QUEUE-NO-ORPHAN-DELETE). # This hook does not start the memory worker (AC-F5). # Safe no-op fallback: must exist before set -e and before hook-bootstrap is sourced. dbg() { :; } set -e # Feedback-loop guard -- before hook-bootstrap to minimize background session overhead. if [ "${DEVFLOW_BG_UPDATER:-}" = "1" ]; then dbg "EXIT: bg_updater"; exit 0; fi SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" source "$SCRIPT_DIR/hook-bootstrap" "capture-turn" source "$SCRIPT_DIR/json-parse" || { echo "capture-turn: failed to source json-parse" >&2; exit 1; } if [ "$_JSON_AVAILABLE" = "false" ]; then dbg "EXIT: no json"; exit 0; fi INPUT=$(cat) dbg "INPUT length=${#INPUT}" # Read the directory, session ID and response in one parser call. _FIELDS=$(printf '%s' "$INPUT" | json_extract_cwd_sid_field "last_assistant_message") || { dbg "EXIT: extraction failed"; exit 0; } CWD="${_FIELDS%%$'\001'*}" _FIELDS="${_FIELDS#*$'\001'}" SESSION_ID="${_FIELDS%%$'\001'*}" ASSISTANT_MSG="${_FIELDS#*$'\001'}" if [ -z "$CWD" ] || [ ! -d "$CWD" ]; then dbg "EXIT: bad CWD"; exit 0; fi devflow_debug_set_cwd "$CWD" dbg "CWD=$CWD" # SECURITY: Never log ASSISTANT_MSG or INPUT content -- may contain secrets. dbg "ASSISTANT_MSG length=${#ASSISTANT_MSG}" # Anchor .devflow/ to the project root (prevents a stray nested .devflow/ when this # hook runs with a CWD inside .devflow/...). Empty → fall back to CWD. source "$SCRIPT_DIR/resolve-project-root" 2>/dev/null || true df_resolve_roots "$CWD" 2>/dev/null || true PROJECT_ROOT="${DF_ROOT:-$CWD}" # The curation queue is the repository's, not the checkout's: in a linked worktree # it is the main worktree's queue (D-LEDGER-MAIN-WORKTREE, resolve-project-root), # the one the Curate agent drains into the shared ledger. Memory stays per checkout. LEDGER_ROOT="${DF_LEDGER_ROOT:-$PROJECT_ROOT}" # The machine-wide manifest (the memory/curation switches, D-FEATURES-NARROW-ONLY # in queue-append) lives at the machine root, # $HOME/.devflow (D-ONE-HOME). DEVFLOW_MANIFEST="$HOME/.devflow/manifest.json" PROJECT_DEVFLOW_DIR="$PROJECT_ROOT/.devflow" MEMORY_DIR="$PROJECT_DEVFLOW_DIR/memory" CURATION_DIR="$LEDGER_ROOT/.devflow/curation" # Skip if empty response if [ -z "$ASSISTANT_MSG" ]; then dbg "EXIT: empty ASSISTANT_MSG" exit 0 fi source "$SCRIPT_DIR/queue-append" || { echo "capture-turn: failed to source queue-append" >&2; exit 1; } # --- AC-P1: at most ONE gate-read fork for memory + curation --- # The machine switch, narrowed by this checkout's project.json/config.json # `features` (D-FEATURES-NARROW-ONLY, see queue-append). queue_read_gates "$DEVFLOW_MANIFEST" "$PROJECT_ROOT" MEMORY_ENABLED="$_QG_MEMORY" CURATION_ENABLED="$_QG_CURATION" dbg "MEMORY_ENABLED=$MEMORY_ENABLED CURATION_ENABLED=$CURATION_ENABLED" if [ "$MEMORY_ENABLED" != "true" ] && [ "$CURATION_ENABLED" != "true" ]; then dbg "EXIT: both features disabled" exit 0 fi # Logging (needs CWD and at-least-one-feature-enabled path — source after the # gate check so the log-paths subprocess overhead is only paid when needed) source "$SCRIPT_DIR/hook-log-init" "capture-turn" # Auto-create .devflow/ and ensure .gitignore entries source "$SCRIPT_DIR/ensure-devflow-init" "$CWD" || exit 0 source "$SCRIPT_DIR/get-mtime" || { echo "capture-turn: failed to source get-mtime" >&2; exit 1; } source "$SCRIPT_DIR/curation-lock" || { echo "capture-turn: failed to source curation-lock" >&2; exit 1; } # Each queue is checked from the root it lies below, and its folder created there, # by queue_append_both (D-HOOKS-NO-SYMLINK, git-marker). TS=$(date +%s) queue_append_both \ "$PROJECT_ROOT" "$MEMORY_DIR/.pending-turns.jsonl" \ "$LEDGER_ROOT" "$CURATION_DIR/.pending-turns.jsonl" \ "$MEMORY_ENABLED" "$CURATION_ENABLED" \ "assistant" "$ASSISTANT_MSG" "$TS" "$SESSION_ID" log "Captured assistant turn (${#ASSISTANT_MSG} chars)" if [ -z "$SESSION_ID" ]; then log "Capture: no valid session_id" fi dbg "=== HOOK COMPLETE ===" exit 0