|
| 1 | +#!/usr/bin/env bash |
| 2 | +# carranca shell-wrapper — wraps agent command execution and writes events to FIFO |
| 3 | +# |
| 4 | +# This script is the ENTRYPOINT of the agent container. It: |
| 5 | +# 1. Waits for the FIFO to be ready (created by logger) |
| 6 | +# 2. Starts a heartbeat background process (30s interval) |
| 7 | +# 3. Writes agent_start event |
| 8 | +# 4. Executes the agent command, capturing exit code |
| 9 | +# 5. Writes agent_stop event |
| 10 | +# 6. Exits immediately if the FIFO breaks (fail closed) |
| 11 | +set -uo pipefail |
| 12 | + |
| 13 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 14 | + |
| 15 | +FIFO_PATH="/fifo/events" |
| 16 | +SESSION_ID="${SESSION_ID:-unknown}" |
| 17 | +AGENT_COMMAND="${AGENT_COMMAND:-bash}" |
| 18 | + |
| 19 | +# --- Helpers --- |
| 20 | + |
| 21 | +timestamp() { |
| 22 | + date -u +%Y-%m-%dT%H:%M:%S.%3NZ |
| 23 | +} |
| 24 | + |
| 25 | +ms_now() { |
| 26 | + date +%s%3N 2>/dev/null || date +%s |
| 27 | +} |
| 28 | + |
| 29 | +fail_closed() { |
| 30 | + local message="$1" |
| 31 | + echo "[carranca] $message — exiting (fail closed)" >&2 |
| 32 | + kill 0 2>/dev/null |
| 33 | + exit 1 |
| 34 | +} |
| 35 | + |
| 36 | +fifo_is_healthy() { |
| 37 | + [ -p "$FIFO_PATH" ] && [ -w "$FIFO_PATH" ] |
| 38 | +} |
| 39 | + |
| 40 | +write_event() { |
| 41 | + if ! fifo_is_healthy; then |
| 42 | + fail_closed "FIFO is unavailable" |
| 43 | + fi |
| 44 | + |
| 45 | + printf '%s\n' "$1" > "$FIFO_PATH" 2>/dev/null |
| 46 | + local rc=$? |
| 47 | + if [ "$rc" -ne 0 ]; then |
| 48 | + fail_closed "FIFO write failed" |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +# shellcheck source=lib/json.sh |
| 53 | +source "$SCRIPT_DIR/lib/json.sh" |
| 54 | + |
| 55 | +# --- Wait for FIFO --- |
| 56 | + |
| 57 | +WAIT_LIMIT=20 |
| 58 | +WAIT_COUNT=0 |
| 59 | +while [ ! -p "$FIFO_PATH" ]; do |
| 60 | + WAIT_COUNT=$((WAIT_COUNT + 1)) |
| 61 | + if [ "$WAIT_COUNT" -ge "$WAIT_LIMIT" ]; then |
| 62 | + fail_closed "FIFO not found after ${WAIT_LIMIT}s" |
| 63 | + fi |
| 64 | + sleep 0.5 |
| 65 | +done |
| 66 | + |
| 67 | +# --- Heartbeat --- |
| 68 | + |
| 69 | +_heartbeat_loop() { |
| 70 | + while true; do |
| 71 | + sleep 30 |
| 72 | + printf '{"type":"heartbeat","source":"shell-wrapper","ts":"%s","session_id":"%s"}\n' "$(timestamp)" "$SESSION_ID" > "$FIFO_PATH" 2>/dev/null || exit 1 |
| 73 | + done |
| 74 | +} |
| 75 | + |
| 76 | +_heartbeat_loop & |
| 77 | +HEARTBEAT_PID=$! |
| 78 | + |
| 79 | +_fifo_watchdog_loop() { |
| 80 | + while true; do |
| 81 | + sleep 1 |
| 82 | + fifo_is_healthy || fail_closed "FIFO disappeared" |
| 83 | + done |
| 84 | +} |
| 85 | + |
| 86 | +_fifo_watchdog_loop & |
| 87 | +WATCHDOG_PID=$! |
| 88 | + |
| 89 | +# --- Session start event --- |
| 90 | + |
| 91 | +write_event "{\"type\":\"session_event\",\"source\":\"shell-wrapper\",\"event\":\"agent_start\",\"ts\":\"$(timestamp)\",\"session_id\":\"$SESSION_ID\"}" |
| 92 | + |
| 93 | +# --- Policy hooks setup (4.3) --- |
| 94 | + |
| 95 | +if [ "${POLICY_HOOKS:-}" = "true" ] && [ -d "/carranca-hooks" ]; then |
| 96 | + git config --global core.hooksPath /carranca-hooks 2>/dev/null || true |
| 97 | +fi |
| 98 | + |
| 99 | +# --- Execute agent command --- |
| 100 | +# We log the overall agent command as a shell_command event. |
| 101 | +# The agent may run sub-commands internally — those are captured by |
| 102 | +# inotifywait (file mutations) but not individually logged as shell_command |
| 103 | +# events in MVP (that requires execve tracing, Phase 3). |
| 104 | + |
| 105 | +START_MS="$(ms_now)" |
| 106 | +# AGENT_COMMAND is operator-authored (from .carranca.yml), not agent-controlled. |
| 107 | +# eval is required to support shell syntax (pipes, &&, env vars, subshells). |
| 108 | +# .carranca.yml is trusted operator input, hidden from the agent at runtime. |
| 109 | +eval "$AGENT_COMMAND" |
| 110 | +AGENT_EXIT=$? |
| 111 | +END_MS="$(ms_now)" |
| 112 | +DURATION=$((END_MS - START_MS)) |
| 113 | + |
| 114 | +ESCAPED_CMD="$(json_escape "$AGENT_COMMAND")" |
| 115 | +ESCAPED_CWD="$(json_escape "$(pwd)")" |
| 116 | +write_event "{\"type\":\"shell_command\",\"source\":\"shell-wrapper\",\"ts\":\"$(timestamp)\",\"session_id\":\"$SESSION_ID\",\"command\":\"$ESCAPED_CMD\",\"exit_code\":$AGENT_EXIT,\"duration_ms\":$DURATION,\"cwd\":\"$ESCAPED_CWD\"}" |
| 117 | + |
| 118 | +# --- Session stop event --- |
| 119 | + |
| 120 | +write_event "{\"type\":\"session_event\",\"source\":\"shell-wrapper\",\"event\":\"agent_stop\",\"ts\":\"$(timestamp)\",\"session_id\":\"$SESSION_ID\",\"exit_code\":$AGENT_EXIT}" |
| 121 | + |
| 122 | +# Cleanup |
| 123 | +kill $HEARTBEAT_PID 2>/dev/null || true |
| 124 | +kill $WATCHDOG_PID 2>/dev/null || true |
| 125 | +exit $AGENT_EXIT |
0 commit comments