#!/usr/bin/env bash
# peer-workspace-state-emit — substrate mirror of this peer's local
# workspace state. Closes the "stale local state" continuity gap named
# in arq://doc/operator_directive/substrate-routable-execution-territory-2026-05-18.
#
# Without this, a worker on another habitat looking at the substrate
# can see the convergence queue + envelope + approval, but cannot tell
# WHAT THIS MAC IS CURRENTLY IN THE MIDDLE OF (current branch, dirty
# files, stash count, open PR claims). With this, the substrate carries
# a periodic snapshot of every peer's workspace state, addressable as:
#
#   arq://body/peer_workspace_state/<peer-fingerprint>-current
#
# Re-emit pattern: idempotent — runs on session start, after major
# branch operations, and (optionally) on a periodic launchd/systemd
# beat. Each emission supersedes the prior one (same address).
#
# Usage:
#   scripts/peer-workspace-state-emit                # emit + print summary
#   scripts/peer-workspace-state-emit --dry-run      # print payload without emit
#
# Requirements:
#   - twin CLI on PATH (Keychain identity)
#   - git
#   - python3 (for JSON shaping)
#   - jq (optional; only for pretty-print)

set -euo pipefail

DRY_RUN="false"
while [[ $# -gt 0 ]]; do
  case "$1" in
    --dry-run) DRY_RUN="true"; shift ;;
    --help|-h) sed -n '2,28p' "$0"; exit 0 ;;
    *) echo "unknown arg: $1" >&2; exit 2 ;;
  esac
done

if ! command -v twin >/dev/null 2>&1; then
  echo "error: twin CLI not on PATH" >&2; exit 2
fi
if ! command -v git >/dev/null 2>&1; then
  echo "error: git not on PATH" >&2; exit 2
fi
if ! command -v python3 >/dev/null 2>&1; then
  echo "error: python3 not on PATH" >&2; exit 2
fi

# Resolve peer fingerprint from `twin status` (the substrate-canonical
# habitat anchor). If twin cannot report the local peer, this script
# cannot continue safely.
PEER_FP="$(twin --use-keychain status 2>/dev/null \
  | grep -E '^\s*fingerprint' | head -1 \
  | sed -E 's/.*:[[:space:]]*//' | tr -d '[:space:]' || true)"
if [[ -z "$PEER_FP" ]]; then
  echo "error: could not resolve peer fingerprint from twin status" >&2
  exit 3
fi

REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || echo unknown)"
REPO_NAME="$(basename "$REPO_ROOT" 2>/dev/null || echo unknown)"
BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo unknown)"
HEAD_SHA="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
HEAD_SUBJECT="$(git log -1 --pretty=%s 2>/dev/null || echo unknown)"
DIRTY_COUNT="$(git status --porcelain 2>/dev/null | wc -l | tr -d '[:space:]' || echo 0)"
STASH_COUNT="$(git stash list 2>/dev/null | wc -l | tr -d '[:space:]' || echo 0)"
UNTRACKED_COUNT="$(git ls-files --others --exclude-standard 2>/dev/null | wc -l | tr -d '[:space:]' || echo 0)"
AHEAD_BEHIND="$(git rev-list --left-right --count origin/main...HEAD 2>/dev/null | tr '\t' ',' || echo "0,0")"
AHEAD="${AHEAD_BEHIND##*,}"
BEHIND="${AHEAD_BEHIND%%,*}"

EMITTED_AT="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
HOSTNAME="$(hostname -s 2>/dev/null || echo unknown)"
UNAME="$(uname -srm 2>/dev/null || echo unknown)"

# Quoted heredoc (<<'PY') prevents shell expansion; values passed via
# env vars and read with os.environ.get() so a double-quote in a git
# commit subject can't break the JSON generation (Sentry HIGH on #3908).
PAYLOAD="$(
  PEER_FP="$PEER_FP" \
  HOSTNAME_SHORT="$HOSTNAME" \
  UNAME_STR="$UNAME" \
  EMITTED_AT="$EMITTED_AT" \
  REPO_NAME="$REPO_NAME" \
  REPO_ROOT="$REPO_ROOT" \
  BRANCH="$BRANCH" \
  HEAD_SHA="$HEAD_SHA" \
  HEAD_SUBJECT="$HEAD_SUBJECT" \
  DIRTY_COUNT="$DIRTY_COUNT" \
  STASH_COUNT="$STASH_COUNT" \
  UNTRACKED_COUNT="$UNTRACKED_COUNT" \
  AHEAD="$AHEAD" \
  BEHIND="$BEHIND" \
  python3 - <<'PY'
import json, os
peer_fp = os.environ.get("PEER_FP", "")
print(json.dumps({
  "schema_version": 1,
  "peer_fingerprint": peer_fp,
  "peer_address": f"arq://body/peer/{peer_fp}",
  "hostname_short": os.environ.get("HOSTNAME_SHORT", ""),
  "uname": os.environ.get("UNAME_STR", ""),
  "emitted_at": os.environ.get("EMITTED_AT", ""),
  "repo_name": os.environ.get("REPO_NAME", ""),
  "repo_root": os.environ.get("REPO_ROOT", ""),
  "branch": os.environ.get("BRANCH", ""),
  "head_sha": os.environ.get("HEAD_SHA", ""),
  "head_subject": os.environ.get("HEAD_SUBJECT", ""),
  "dirty_count": int(os.environ.get("DIRTY_COUNT", "0")),
  "stash_count": int(os.environ.get("STASH_COUNT", "0")),
  "untracked_count": int(os.environ.get("UNTRACKED_COUNT", "0")),
  "commits_ahead_of_main": int(os.environ.get("AHEAD", "0")),
  "commits_behind_main": int(os.environ.get("BEHIND", "0")),
  "continuity_purpose": (
    "Closes the stale-local-state continuity gap. Any peer reading "
    f"arq://body/peer_workspace_state/{peer_fp}-current can see what "
    "this peer has in flight from substrate alone."
  ),
}))
PY
)"

ADDRESS="arq://body/peer_workspace_state/${PEER_FP}-current"

if [[ "$DRY_RUN" == "true" ]]; then
  echo "would emit: $ADDRESS"
  echo "$PAYLOAD" | python3 -m json.tool 2>/dev/null || echo "$PAYLOAD"
  exit 0
fi

twin --use-keychain act emit body peer_workspace_state "${PEER_FP}-current" \
  --payload "$PAYLOAD" \
  --source twin-peer-workspace-state-emit \
  --sync 2>&1 | tail -5

echo ""
echo "emitted: $ADDRESS"
echo "  branch=$BRANCH"
echo "  head=${HEAD_SHA:0:10}"
echo "  dirty=$DIRTY_COUNT untracked=$UNTRACKED_COUNT stash=$STASH_COUNT"
echo "  ahead/behind=$AHEAD/$BEHIND"
