#!/usr/bin/env bash
#
# okstra-team-reconcile.sh — flip dead-pane stale-active team members to
# inactive so run-end teammate dismissal leaves a clean FleetView roster.
#
# A Claude Code team member clears its own `isActive` flag in
# `~/.claude/teams/<team>/config.json` when its `Agent()` dispatch returns. A
# member whose tmux pane died WITHOUT that flip stays `isActive: true` and keeps
# showing as a live member that no re-sent `shutdown_request` can clear, since
# the addressee is already gone. This reconciles exactly that case; it never
# touches a live-pane member, the lead, or a member with no recorded pane (those
# are left for graceful shutdown). It no-ops when tmux is unavailable or nothing
# is stale.
#
# Usage:
#   okstra-team-reconcile.sh [--list] <team-name|team-dir|config.json>
#   okstra-team-reconcile.sh [--list] --project-root <dir> [--fallback-team <label>]
#   okstra-team-reconcile.sh [--list] --session-end   # reads SessionEnd JSON on stdin
#
#   --project-root  resolve the CURRENT live session's roster (robust to the
#                   phase-3 lead.sessionId snapshot drifting across resume), then
#                   reconcile and print `dismissible-member: <name>` lines.
#   --session-end   SessionEnd-hook net: reconcile the ending session's roster.
#   --list          report what WOULD be deactivated; do not write (alias --dry-run).
#
# Failures are non-fatal to the run — teardown must never block on this.
set -u

_dir="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Logic lives in okstra_ctl.team_reconcile. In the repo layout the package is a
# bin-sibling (scripts/okstra_ctl); in the installed layout it is under
# $OKSTRA_HOME/lib/python. Put both on PYTHONPATH so either resolves.
home="${OKSTRA_HOME:-$HOME/.okstra}"
export PYTHONPATH="${_dir}:${home}/lib/python${PYTHONPATH:+:$PYTHONPATH}"

exec python3 -c 'import sys; from okstra_ctl.team_reconcile import main; sys.exit(main(sys.argv[1:]))' "$@"
