|
| 1 | +#!/bin/sh |
| 2 | +# ccstatusline IPC client (protocol v1, issue #16). |
| 3 | +# |
| 4 | +# Reads the Claude Code status JSON from stdin, forwards it to the local |
| 5 | +# ccstatusline daemon over its private Unix socket, and prints the rendered |
| 6 | +# status line on stdout. All diagnostics go to stderr; on any failure stdout |
| 7 | +# stays empty, so a partial or error response can never become a status line. |
| 8 | +# |
| 9 | +# Shell + curl only: no Node, jq, or Python on the repaint path. The auth |
| 10 | +# token reaches curl through its config read from stdin — never argv, a URL, |
| 11 | +# or a temp credential file. User curl config, proxy routing, and redirects |
| 12 | +# are disabled; connection and total deadlines are explicit. |
| 13 | +# |
| 14 | +# Environment: |
| 15 | +# CCSTATUSLINE_RUNTIME_DIR override the daemon runtime directory |
| 16 | +# CCSTATUSLINE_IPC_CONNECT_TIMEOUT seconds to connect (default 2) |
| 17 | +# CCSTATUSLINE_IPC_TIMEOUT total seconds (default 10) |
| 18 | +# CCSTATUSLINE_DAEMON_START override the lazy-start command (see |
| 19 | +# start_daemon below) |
| 20 | +# CCSTATUSLINE_NO_AUTOSTART when set, never start a daemon lazily |
| 21 | +# |
| 22 | +# On-demand lifecycle (#53): this wrapper runs only in installed shared mode |
| 23 | +# (`daemon install`), so its lazy start is opt-in by construction — one-shot |
| 24 | +# users never execute this file. On a missing daemon it starts one (`daemon |
| 25 | +# start`, serialized by the #47 cold-start lock) and retries into it. |
| 26 | +# |
| 27 | +# Exit status: 0 on a rendered status line, 1 on any failure (stdout empty). |
| 28 | + |
| 29 | +set -u |
| 30 | + |
| 31 | +connect_timeout=${CCSTATUSLINE_IPC_CONNECT_TIMEOUT:-2} |
| 32 | +total_timeout=${CCSTATUSLINE_IPC_TIMEOUT:-10} |
| 33 | +max_context_bytes=16384 |
| 34 | + |
| 35 | +die() { |
| 36 | + printf 'ccstatusline-ipc: %s\n' "$1" >&2 |
| 37 | + exit 1 |
| 38 | +} |
| 39 | + |
| 40 | +if [ -n "${CCSTATUSLINE_RUNTIME_DIR:-}" ]; then |
| 41 | + runtime_dir=$CCSTATUSLINE_RUNTIME_DIR |
| 42 | +elif [ -n "${XDG_RUNTIME_DIR:-}" ]; then |
| 43 | + runtime_dir=$XDG_RUNTIME_DIR/ccstatusline |
| 44 | +else |
| 45 | + runtime_dir=${TMPDIR:-/tmp}/ccstatusline-$(id -u) |
| 46 | +fi |
| 47 | +discovery=$runtime_dir/daemon.env |
| 48 | + |
| 49 | +# Endpoint discovery: flat KEY=VALUE lines written by the daemon. Prefix |
| 50 | +# matching keeps values (socket paths may contain '=') intact. |
| 51 | +read_discovery() { |
| 52 | + protocol='' |
| 53 | + socket='' |
| 54 | + token='' |
| 55 | + [ -r "$discovery" ] || return 1 |
| 56 | + while IFS= read -r line || [ -n "$line" ]; do |
| 57 | + case $line in |
| 58 | + '#'*) continue ;; |
| 59 | + protocol=*) protocol=${line#protocol=} ;; |
| 60 | + socket=*) socket=${line#socket=} ;; |
| 61 | + token=*) token=${line#token=} ;; |
| 62 | + esac |
| 63 | + done < "$discovery" |
| 64 | + return 0 |
| 65 | +} |
| 66 | + |
| 67 | +# Lazy start (#53): run `ccstatusline daemon start` — the #47 cold-start lock |
| 68 | +# serializes concurrent starters onto one server, so racing clients each run |
| 69 | +# this and converge on the same daemon. Tried at most once per invocation, |
| 70 | +# bounded by the starter's own startup timeout. Opt out with |
| 71 | +# CCSTATUSLINE_NO_AUTOSTART; override the command with |
| 72 | +# CCSTATUSLINE_DAEMON_START (a full command line, split on whitespace). |
| 73 | +autostart_attempted=0 |
| 74 | +start_daemon() { |
| 75 | + [ -z "${CCSTATUSLINE_NO_AUTOSTART:-}" ] || return 1 |
| 76 | + [ "$autostart_attempted" -eq 0 ] || return 1 |
| 77 | + autostart_attempted=1 |
| 78 | + if [ -n "${CCSTATUSLINE_DAEMON_START:-}" ]; then |
| 79 | + $CCSTATUSLINE_DAEMON_START >/dev/null 2>&1 |
| 80 | + return $? |
| 81 | + fi |
| 82 | + # Same install root as this wrapper: the npm package ships |
| 83 | + # client/ccstatusline-ipc next to dist/ccstatusline.js (node), a dev |
| 84 | + # checkout next to src/ccstatusline.ts (bun). |
| 85 | + install_root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) || return 1 |
| 86 | + if [ -f "$install_root/dist/ccstatusline.js" ]; then |
| 87 | + node "$install_root/dist/ccstatusline.js" daemon start >/dev/null 2>&1 |
| 88 | + elif [ -f "$install_root/src/ccstatusline.ts" ]; then |
| 89 | + bun "$install_root/src/ccstatusline.ts" daemon start >/dev/null 2>&1 |
| 90 | + else |
| 91 | + return 1 |
| 92 | + fi |
| 93 | +} |
| 94 | + |
| 95 | +if ! read_discovery; then |
| 96 | + # No live daemon: start one on demand and re-read what it published. |
| 97 | + start_daemon || die "no readable discovery file at $discovery; is the daemon running?" |
| 98 | + read_discovery || die "lazy daemon start left no discovery file at $discovery" |
| 99 | +fi |
| 100 | + |
| 101 | +[ "$protocol" = "1" ] || die "unsupported daemon protocol '${protocol:-none}'; want 1" |
| 102 | +[ -n "$socket" ] || die "discovery file has no socket path" |
| 103 | +[ -n "$token" ] || die "discovery file has no auth token" |
| 104 | + |
| 105 | +# Invocation context: base64 over NUL-delimited KEY=VALUE entries. Mirrors |
| 106 | +# ENV_ALLOWLIST in src/daemon/protocol.ts — keep the two lists in sync. Only |
| 107 | +# set variables are encoded; the server clears its own copy of any allowlisted |
| 108 | +# name the client omits, which preserves absent-vs-empty exactly. The NUL |
| 109 | +# bytes live only inside the pipeline; base64 output is shell-safe. |
| 110 | +context=$( |
| 111 | + { |
| 112 | + [ -n "${CLAUDE_CONFIG_DIR+x}" ] && printf 'CLAUDE_CONFIG_DIR=%s\0' "$CLAUDE_CONFIG_DIR" |
| 113 | + [ -n "${CLAUDE_SECURESTORAGE_CONFIG_DIR+x}" ] && printf 'CLAUDE_SECURESTORAGE_CONFIG_DIR=%s\0' "$CLAUDE_SECURESTORAGE_CONFIG_DIR" |
| 114 | + [ -n "${CCSTATUSLINE_CONTEXT_SIZE_FALLBACK+x}" ] && printf 'CCSTATUSLINE_CONTEXT_SIZE_FALLBACK=%s\0' "$CCSTATUSLINE_CONTEXT_SIZE_FALLBACK" |
| 115 | + [ -n "${CCSTATUSLINE_WIDTH+x}" ] && printf 'CCSTATUSLINE_WIDTH=%s\0' "$CCSTATUSLINE_WIDTH" |
| 116 | + [ -n "${COLUMNS+x}" ] && printf 'COLUMNS=%s\0' "$COLUMNS" |
| 117 | + [ -n "${HTTPS_PROXY+x}" ] && printf 'HTTPS_PROXY=%s\0' "$HTTPS_PROXY" |
| 118 | + [ -n "${NO_PROXY+x}" ] && printf 'NO_PROXY=%s\0' "$NO_PROXY" |
| 119 | + printf 'cwd=%s\0' "$(pwd -P)" |
| 120 | + } | base64 | tr -d '\n' |
| 121 | +) |
| 122 | + |
| 123 | +[ "${#context}" -le "$max_context_bytes" ] \ |
| 124 | + || die "invocation context too large (${#context} > $max_context_bytes bytes)" |
| 125 | + |
| 126 | +# Buffer the piped status JSON once: retry attempts below must re-send the |
| 127 | +# same payload, and stdin cannot be rewound. Both temp files hold no secrets |
| 128 | +# (payload + rendered line are what the caller piped/asked for); the auth |
| 129 | +# token still travels only through the curl config on stdin. |
| 130 | +payload_tmp=$(mktemp "${TMPDIR:-/tmp}/ccstatusline-ipc-payload.XXXXXX") || die "cannot buffer stdin" |
| 131 | +body_tmp=$(mktemp "${TMPDIR:-/tmp}/ccstatusline-ipc-body.XXXXXX") || { rm -f "$payload_tmp"; die "cannot create temp file"; } |
| 132 | +cleanup() { rm -f "$payload_tmp" "$body_tmp"; } |
| 133 | +trap cleanup EXIT |
| 134 | +# Signal exits must not resume the retry loop (a handler without exit would |
| 135 | +# return into the backoff sleep); the EXIT trap still removes the temp files. |
| 136 | +trap 'exit 129' HUP |
| 137 | +trap 'exit 130' INT |
| 138 | +trap 'exit 143' TERM |
| 139 | +cat > "$payload_tmp" |
| 140 | + |
| 141 | +attempt=0 |
| 142 | +while :; do |
| 143 | + code=$(curl -q -sS --fail \ |
| 144 | + --unix-socket "$socket" \ |
| 145 | + --config - \ |
| 146 | + --data-binary @"$payload_tmp" \ |
| 147 | + -o "$body_tmp" \ |
| 148 | + -w '%{http_code}' \ |
| 149 | + -H 'Content-Type: application/json' \ |
| 150 | + --noproxy '*' \ |
| 151 | + --connect-timeout "$connect_timeout" \ |
| 152 | + --max-time "$total_timeout" \ |
| 153 | + 'http://localhost/v1/render' \ |
| 154 | + <<CONFIG |
| 155 | +header = "Authorization: Bearer $token" |
| 156 | +header = "X-CCStatusLine-Context: $context" |
| 157 | +CONFIG |
| 158 | + ) |
| 159 | + curl_rc=$? |
| 160 | + [ "$curl_rc" -eq 0 ] && break |
| 161 | + # Connect failure (curl exit 7): the daemon died after discovery was |
| 162 | + # read. Transparently lazy-start a fresh one and retry into it — the |
| 163 | + # autostart guard inside start_daemon bounds this to one restart. |
| 164 | + if [ "$curl_rc" -eq 7 ]; then |
| 165 | + if start_daemon && read_discovery && [ "$protocol" = "1" ]; then |
| 166 | + continue |
| 167 | + fi |
| 168 | + fi |
| 169 | + attempt=$((attempt + 1)) |
| 170 | + # 503 busy: the render queue is momentarily full (a burst of repaints |
| 171 | + # across sessions). Back off on a growing ladder — 8s total budget — so |
| 172 | + # even a cold burst of dozens of large renders waits out the queue, the |
| 173 | + # same thing the next Claude Code repaint tick would do. Any other |
| 174 | + # failure (dead daemon, bad request, render error) fails fast so stdout |
| 175 | + # stays empty. |
| 176 | + if [ "$curl_rc" -eq 22 ] && [ "$code" = "503" ] && [ "$attempt" -lt 14 ]; then |
| 177 | + case $attempt in |
| 178 | + 1 | 2) sleep 0.05 ;; |
| 179 | + 3 | 4) sleep 0.1 ;; |
| 180 | + 5 | 6) sleep 0.25 ;; |
| 181 | + 7 | 8) sleep 0.5 ;; |
| 182 | + *) sleep 1 ;; |
| 183 | + esac |
| 184 | + continue |
| 185 | + fi |
| 186 | + die "daemon request failed (curl exit $curl_rc)" |
| 187 | +done |
| 188 | + |
| 189 | +# An empty 200 body is a successful "nothing to render" answer — it matches |
| 190 | +# piped mode, which prints nothing. Report success with no output rather than |
| 191 | +# falling off the end with the failed [ -n test as the exit status. |
| 192 | +body=$(cat "$body_tmp") |
| 193 | +if [ -n "$body" ]; then |
| 194 | + printf '%s\n' "$body" |
| 195 | +fi |
| 196 | +exit 0 |
0 commit comments