#!/usr/bin/env bash # # Claude Code SessionEnd hook: give back the node this session's worktree holds. # A lease is a backstop for a session that died, not the normal # way a node comes back. Whatever the session left running there is cleared by # the node when the next holder's first session starts. A node running a # hosted bot (`midas bot start --agent`) is kept: the bot outlives the session # that started it, and its host gives the node back. FAILS OPEN. set -uo pipefail payload=$(cat 2>/dev/null || true) cwd=$(printf '%s' "$payload" | jq -r '.cwd // empty' 2>/dev/null) [ -n "$cwd" ] && [ -d "$cwd" ] && cd "$cwd" || exit 0 reason=$(printf '%s' "$payload" | jq -r '.reason // empty' 2>/dev/null) # What it learnt, to the hub before it goes (memory.rs). timeout 10 "$HOME/.local/bin/midas" memory sync --quiet >/dev/null 2>&1 timeout 5 "$HOME/.local/bin/midas" session end ${reason:+--reason "$reason"} >/dev/null 2>&1 "$HOME/.local/bin/midas" mine >/dev/null 2>&1 && "$HOME/.local/bin/midas" release --keep-hosted >/dev/null 2>&1 exit 0