|
| 1 | +#!/bin/bash |
| 2 | +# Claude SessionStart hook that prompts for semantic-anchor onboarding |
| 3 | +# when no managed anchor block is present yet. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +command -v python3 >/dev/null 2>&1 || { echo "Error: python3 is required but not found" >&2; exit 1; } |
| 8 | + |
| 9 | +PROJECT_DIR="${CLAUDE_PROJECT_DIR:-$PWD}" |
| 10 | +CODEX_HOME_DIR="${CODEX_HOME:-$HOME/.codex}" |
| 11 | +STATE_DIR="$HOME/.claude/semantic-anchor-onboarding" |
| 12 | +STATE_FILE="$STATE_DIR/state.json" |
| 13 | +MARKER_START="<!-- semantic-anchors:start -->" |
| 14 | +MARKER_END="<!-- semantic-anchors:end -->" |
| 15 | +COOLDOWN_HOURS=24 |
| 16 | + |
| 17 | +has_marker() { |
| 18 | + local file |
| 19 | + for file in "$@"; do |
| 20 | + if [ -f "$file" ] && |
| 21 | + grep -qF "$MARKER_START" "$file" && |
| 22 | + grep -qF "$MARKER_END" "$file"; then |
| 23 | + return 0 |
| 24 | + fi |
| 25 | + done |
| 26 | + return 1 |
| 27 | +} |
| 28 | + |
| 29 | +if has_marker \ |
| 30 | + "$PROJECT_DIR/AGENTS.md" \ |
| 31 | + "$PROJECT_DIR/CLAUDE.md" \ |
| 32 | + "$PROJECT_DIR/GEMINI.md" \ |
| 33 | + "$PROJECT_DIR/.github/copilot-instructions.md" \ |
| 34 | + "$PROJECT_DIR/.claude/AGENTS.md" \ |
| 35 | + "$HOME/.claude/CLAUDE.md" \ |
| 36 | + "$CODEX_HOME_DIR/AGENTS.md" \ |
| 37 | + "$HOME/.gemini/GEMINI.md"; then |
| 38 | + exit 0 |
| 39 | +fi |
| 40 | + |
| 41 | +mkdir -p "$STATE_DIR" 2>/dev/null || true |
| 42 | + |
| 43 | +python3 - "$STATE_FILE" "$PROJECT_DIR" "$COOLDOWN_HOURS" <<'PY' |
| 44 | +import json |
| 45 | +import os |
| 46 | +import sys |
| 47 | +import tempfile |
| 48 | +from datetime import datetime, timedelta, timezone |
| 49 | +
|
| 50 | +state_path, project_dir, cooldown_hours = sys.argv[1], sys.argv[2], int(sys.argv[3]) |
| 51 | +now = datetime.now(timezone.utc) |
| 52 | +
|
| 53 | +try: |
| 54 | + with open(state_path, "r", encoding="utf-8") as handle: |
| 55 | + state = json.load(handle) |
| 56 | +except (FileNotFoundError, json.JSONDecodeError, OSError): |
| 57 | + state = {} |
| 58 | +
|
| 59 | +projects = state.setdefault("projects", {}) |
| 60 | +entry = projects.setdefault(project_dir, {}) |
| 61 | +last_prompt_raw = entry.get("last_prompt") |
| 62 | +
|
| 63 | +if last_prompt_raw: |
| 64 | + try: |
| 65 | + last_prompt = datetime.fromisoformat(last_prompt_raw.replace("Z", "+00:00")) |
| 66 | + except (ValueError, TypeError): |
| 67 | + last_prompt = None |
| 68 | + if last_prompt and now - last_prompt < timedelta(hours=cooldown_hours): |
| 69 | + raise SystemExit(0) |
| 70 | +
|
| 71 | +entry["last_prompt"] = now.isoformat().replace("+00:00", "Z") |
| 72 | +
|
| 73 | +tmp_path = None |
| 74 | +try: |
| 75 | + state_dir = os.path.dirname(state_path) |
| 76 | + fd, tmp_path = tempfile.mkstemp(dir=state_dir, suffix=".tmp") |
| 77 | + with os.fdopen(fd, "w", encoding="utf-8") as handle: |
| 78 | + json.dump(state, handle, indent=2) |
| 79 | + handle.write("\n") |
| 80 | + os.replace(tmp_path, state_path) |
| 81 | +except OSError: |
| 82 | + # Best-effort: if write fails, skip silently rather than breaking the hook |
| 83 | + if tmp_path and os.path.exists(tmp_path): |
| 84 | + os.unlink(tmp_path) |
| 85 | +
|
| 86 | +message = ( |
| 87 | + "Semantic anchors are not configured for this workspace. " |
| 88 | + "On your next response, ask one short onboarding question: " |
| 89 | + "whether the user wants to onboard semantic anchors now for this project " |
| 90 | + "or for their home directory. If they agree and the semantic-anchor-onboarding " |
| 91 | + "skill is available, use it; otherwise guide them to the installation instructions " |
| 92 | + "in the repository README." |
| 93 | +) |
| 94 | +
|
| 95 | +payload = { |
| 96 | + "hookSpecificOutput": { |
| 97 | + "hookEventName": "SessionStart", |
| 98 | + "additionalContext": message, |
| 99 | + } |
| 100 | +} |
| 101 | +print(json.dumps(payload)) |
| 102 | +PY |
0 commit comments