Something went wrong. Try again.
atproto Thingiverse but good
Something went wrong. Try again.
4.2 kB · 140 lines
Shell
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141#!/usr/bin/env bashset -euo pipefail
# Conservative workflow context hook for Polytoken.# Handles session_start, pre_model_turn, and post_compaction.# It injects compact framing only; it does not enforce gates yet.
EVENT="${POLYTOKEN_HOOK_EVENT:-}"PROJECT_DIR="${POLYTOKEN_PROJECT_DIR:-${POLYTOKEN_PROJECT_PATH:-$(pwd)}}"SESSION_ID="${POLYTOKEN_SESSION_ID:-}"
# Hook input is not needed for these events. Consume it so the handler remains# compatible with Polytoken's stdin contract without persisting session data.cat >/dev/null || true
session_api_metadata() { if [ -z "$SESSION_ID" ]; then return 1 fi
local data_home session_dir startup port credential_file token data_home="${XDG_DATA_HOME:-${HOME:-}/.local/share}" if [ -z "$data_home" ]; then return 1 fi
session_dir="$data_home/polytoken/sessions/$SESSION_ID" startup="$session_dir/startup.json" if [ ! -f "$startup" ]; then return 1 fi
port="$(jq -r '.port // empty' "$startup" 2>/dev/null)" credential_file="$(jq -r '.credential_file_path // empty' "$startup" 2>/dev/null)" if ! [[ "$port" =~ ^[0-9]+$ ]] \ || [ "$port" -lt 1 ] \ || [ "$port" -gt 65535 ] \ || [ -z "$credential_file" ] \ || [ ! -f "$credential_file" ]; then return 1 fi
token="$(jq -r '.token // empty' "$credential_file" 2>/dev/null)" if [ -z "$token" ] || printf '%s' "$token" | LC_ALL=C grep -q '[[:cntrl:]]'; then return 1 fi
printf '%s\t%s\n' "$port" "$token"}
curl_config_escape() { local value="$1" value="${value//\\/\\\\}" value="${value//\"/\\\"}" printf '%s' "$value"}
state_summary() { local port="$1" token="$2" authorization authorization="$(curl_config_escape "Authorization: Bearer ${token}")"
printf 'header = "%s"\n' "$authorization" \ | curl -fsS --max-time 1 --config - \ "http://127.0.0.1:${port}/state" 2>/dev/null \ | jq -r ' "- Active facet: \(.active_facet // "unknown")\n" + "- Active model: \(.active_model // "unknown")\n" + "- Project cwd: \(.project_cwd // "unknown")\n" + "- Todos: \((.todos // []) | length)\n" + "- Flags: \((.flags // []) | length)" ' 2>/dev/null || true}
project_vars_summary() { local vars="$PROJECT_DIR/.polytoken/project_vars.yaml" if [ ! -f "$vars" ]; then printf '%s\n' "- Project vars: none found at .polytoken/project_vars.yaml" return 0 fi
printf '%s\n' "- Project vars: .polytoken/project_vars.yaml"}
active_gates_summary() { local gates_dir="$PROJECT_DIR/agent_docs/.workflow/gates" local alt_gates_dir="$PROJECT_DIR/docs/polytoken-workflows/.workflow/gates" local found=0
for dir in "$gates_dir" "$alt_gates_dir"; do if [ -d "$dir" ]; then local count count=$(find "$dir" -maxdepth 1 -type f -name '*.json' 2>/dev/null | wc -l | tr -d ' ') if [ "${count:-0}" != "0" ]; then printf '%s\n' "- Active workflow gate files: $count in ${dir#$PROJECT_DIR/}" found=1 fi fi done
if [ "$found" = 0 ]; then printf '%s\n' "- Active workflow gates: none detected" fi}
build_context() { local metadata="" port="" token="" state="" metadata="$(session_api_metadata || true)" if [ -n "$metadata" ]; then IFS=$'\t' read -r port token <<<"$metadata" state="$(state_summary "$port" "$token")" fi
cat <<EOF<workflow-context>Polytoken workflow framing:$(project_vars_summary)${state:-"- Daemon state: unavailable from hook"}$(active_gates_summary)
Use project-specific design workflow instructions from project_vars when present. Distinguish human source of truth from agent working truth. In design/plan work, keep durable documents focused on current state and link to history rather than carrying stale content inline.</workflow-context>EOF}
context="$(build_context)"
case "$EVENT" in session_start) jq -n --arg ctx "$context" '{outcome:"allow", additional_context:$ctx}' ;; pre_model_turn) jq -n --arg ctx "$context" '{outcome:"proceed", additional_context:$ctx}' ;; post_compaction) jq -n --arg ctx "$context" '{outcome:"allow", append_to_output:$ctx}' ;; *) jq -n '{outcome:"allow"}' ;;esac