#!/usr/bin/env bash # register-guards.sh — Detect available harnesses and install the appropriate # automaton pre-edit guards. # # Called by install.sh and update.sh during framework setup. # # Detection: # - OpenCode: checks for ~/.config/opencode/opencode.json or .jsonc # - Pi Dev: checks for `pi` in PATH # # Usage: bash ~/.automaton/scripts/register-guards.sh set -euo pipefail FRAMEWORK_DIR="$HOME/.automaton" INSTALLED_OPENCODE=false INSTALLED_PI=false echo "" echo "=== Pre-Edit Guard Registration ===" # OpenCode guard # Check for both opencode.json (default) and opencode.jsonc OPENCODE_CONFIG="" for candidate in "$HOME/.config/opencode/opencode.json" "$HOME/.config/opencode/opencode.jsonc"; do if [ -f "$candidate" ]; then OPENCODE_CONFIG="$candidate" break fi done OPENCODE_SOURCE="$FRAMEWORK_DIR/plugins/automaton-guard" if [ -n "$OPENCODE_CONFIG" ]; then if grep -q "automaton-guard" "$OPENCODE_CONFIG" 2>/dev/null; then echo "OpenCode: already registered" else echo "OpenCode: registering guard plugin..." # Use 'plugin' key (singular) per opencode config schema. # Strip // comments (JSONC) before parsing for robustness. python3 -c " import json, re with open('$OPENCODE_CONFIG') as f: text = f.read() # Strip single-line // comments (JSONC) outside of strings text = re.sub(r'//.*?\$', '', text) cfg = json.loads(text) cfg.setdefault('plugin', []).append('$OPENCODE_SOURCE') with open('$OPENCODE_CONFIG', 'w') as f: json.dump(cfg, f, indent=2) " INSTALLED_OPENCODE=true echo "OpenCode: registered (restart opencode to activate)" fi else echo "OpenCode: not detected (no ~/.config/opencode/opencode.json or .jsonc)" fi # Pi Dev guard PI_SOURCE="$FRAMEWORK_DIR/plugins/automaton-guard-pi" if command -v pi &>/dev/null; then INSTALLED=$(pi list 2>/dev/null | grep -c "automaton-guard-pi" || true) if [ "$INSTALLED" -gt 0 ]; then echo "Pi Dev: already installed" else echo "Pi Dev: installing guard extension..." pi install "$PI_SOURCE" 2>&1 | sed 's/^/ /' INSTALLED_PI=true echo "Pi Dev: installed" fi # Offer pi-automaton startup wrapper PI_WRAPPER="$FRAMEWORK_DIR/scripts/pi-automaton.sh" if [ -f "$PI_WRAPPER" ]; then echo "" echo "Pi Dev context wrapper available at:" echo " $PI_WRAPPER" echo "" echo "Before starting a Pi Dev session, run this script to print" echo "automaton project context that you can paste as your first message." echo "" echo " bash ~/.automaton/scripts/pi-automaton.sh" echo "" # Only prompt interactively if stdin is a terminal BIN_DIR="$HOME/bin" if [ -t 0 ] && [ ! -f "$BIN_DIR/pi-automaton" ]; then echo -n "Symlink to ~/bin/pi-automaton for easier access? [Y/n] " read -r REPLY if [ -z "$REPLY" ] || [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ]; then mkdir -p "$BIN_DIR" ln -sf "$PI_WRAPPER" "$BIN_DIR/pi-automaton" echo " Created $BIN_DIR/pi-automaton → $PI_WRAPPER" echo " (ensure ~/bin is in your PATH)" fi fi fi else echo "Pi Dev: not detected (pi not in PATH)" fi echo "" if $INSTALLED_OPENCODE || $INSTALLED_PI; then echo "Restart your harness for the guard to take effect." fi if ! $INSTALLED_OPENCODE && ! $INSTALLED_PI; then echo "No harness detected. To install a guard manually:" echo " OpenCode: add '\"plugin\": [\"$OPENCODE_SOURCE\"]' to ~/.config/opencode/opencode.json" echo " Pi Dev: pi install $PI_SOURCE" echo "" echo "Without a pre-edit guard, git hooks (pre-commit + pre-push)" echo "provide enforcement at commit/push time instead." fi