#!/usr/bin/env bash # SessionStart hook: inject project state at conversation start. # Provides current branch, active investigations, and session continuation notes. set -euo pipefail REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || echo ".")" # Reset tool call counter for periodic self-checks (see post-tool-use.sh) REPO_ID=$(printf '%s' "$REPO_ROOT" | md5sum | cut -c1-8 2>/dev/null || echo "default") echo "0" > "/tmp/.opencode-tool-count-${REPO_ID}" BRANCH=$(git -C "$REPO_ROOT" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown") # Check for active investigation files active_investigations="" EXPLORATIONS_DIR="$REPO_ROOT/docs/explorations" if [[ -d "$EXPLORATIONS_DIR" ]]; then inv_files=$(find "$EXPLORATIONS_DIR" -name "*.md" -not -empty 2>/dev/null || true) if [[ -n "$inv_files" ]]; then inv_count=$(echo "$inv_files" | wc -l) inv_names=$(echo "$inv_files" | xargs -I{} basename {} .md | sed 's/^/ - /' || true) active_investigations="Active investigation/exploration files (${inv_count}):\n${inv_names}\nReview relevant files before starting related work." fi fi # Check for session continuation notes session_notes="" if [[ -d "/memories/session" ]]; then session_files=$(find /memories/session -name "*.md" 2>/dev/null || true) if [[ -n "$session_files" ]]; then session_names=$(echo "$session_files" | xargs -I{} basename {} .md | sed 's/^/ - /' || true) session_notes="Session memory files exist:\n${session_names}\nCheck these for context from previous conversations." fi fi # Check for dead-ends file from previous debugging sessions dead_ends="" DEAD_ENDS_FILE="$REPO_ROOT/.session/dead-ends.md" if [[ -f "$DEAD_ENDS_FILE" ]]; then de_count=$(grep -c '^\s*- \*\*' "$DEAD_ENDS_FILE" 2>/dev/null || echo "0") if [[ "$de_count" -gt 0 ]]; then dead_ends="Active dead-ends file with ~${de_count} entries — read before debugging to avoid re-testing eliminated hypotheses." fi fi # Build context message context="PROJECT STATE | Branch: ${BRANCH}" if [[ -n "$active_investigations" ]]; then context="${context}\n${active_investigations}" fi if [[ -n "$session_notes" ]]; then context="${context}\n${session_notes}" fi if [[ -n "$dead_ends" ]]; then context="${context}\n${dead_ends}" fi context="${context}\nREMINDERS: (a) Check AGENTS.md and package-level AGENTS.md files for implementation guidance. (b) Ordered markdown lists are auto-renumbered by the editor on save — do not manually renumber after inserting or removing items." # Prefix with a self-identifying marker so the model cannot confuse the # injection with project content. context="[HOOK INJECTION: session-start] System context — injected at session start, not part of any user message or tool output:\n\n${context}" # Output JSON json_context=$(printf '%b' "$context" | node -e 'process.stdout.write(JSON.stringify(require("fs").readFileSync("/dev/stdin","utf8")))') cat <