From 85135779c6c63b84e8576446b396ef0045bf15d4 Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Sun, 22 Feb 2026 17:42:03 -0600 Subject: [PATCH] fix: add mission detection to session hooks Patches session-start.sh to detect active orchestrator missions and print a banner with phase, milestone, progress, and scratchpad. Patches session-end.sh to record session results in mission.json. These were in the mosaic-bootstrap template but not yet applied to this project's existing hooks. Co-Authored-By: Claude Opus 4.6 --- scripts/agent/session-end.sh | 27 ++++++++++++++ scripts/agent/session-start.sh | 64 ++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/scripts/agent/session-end.sh b/scripts/agent/session-end.sh index 3b3bbc2..e0f85cb 100755 --- a/scripts/agent/session-end.sh +++ b/scripts/agent/session-end.sh @@ -8,6 +8,33 @@ source "$SCRIPT_DIR/common.sh" ensure_repo_root load_repo_hooks +# ─── Mission session cleanup (ORCHESTRATOR-PROTOCOL) ──────────────────────── +ORCH_DIR=".mosaic/orchestrator" +MISSION_JSON="$ORCH_DIR/mission.json" +SESSION_LOCK="$ORCH_DIR/session.lock" +COORD_LIB="$HOME/.config/mosaic/tools/orchestrator/_lib.sh" + +if [[ -f "$SESSION_LOCK" ]] && [[ -f "$COORD_LIB" ]] && command -v jq &>/dev/null; then + # shellcheck source=/dev/null + source "$COORD_LIB" + + sess_id="$(jq -r '.session_id // ""' "$SESSION_LOCK")" + if [[ -n "$sess_id" && -f "$MISSION_JSON" ]]; then + updated="$(jq \ + --arg sid "$sess_id" \ + --arg ts "$(iso_now)" \ + --arg reason "completed" \ + '(.sessions[] | select(.session_id == $sid)) |= . + { + ended_at: $ts, + ended_reason: $reason + }' "$MISSION_JSON")" + echo "$updated" > "$MISSION_JSON.tmp" && mv "$MISSION_JSON.tmp" "$MISSION_JSON" + echo "[agent-framework] Session $sess_id recorded in mission state" + fi + + session_lock_clear "." +fi + if declare -F mosaic_hook_session_end >/dev/null 2>&1; then run_step "Run repo end hook" mosaic_hook_session_end else diff --git a/scripts/agent/session-start.sh b/scripts/agent/session-start.sh index 4e2f77d..754145e 100755 --- a/scripts/agent/session-start.sh +++ b/scripts/agent/session-start.sh @@ -43,6 +43,70 @@ if git rev-parse --is-inside-work-tree >/dev/null 2>&1 && has_remote; then fi fi +# ─── Mission state detection (ORCHESTRATOR-PROTOCOL) ──────────────────────── +ORCH_DIR=".mosaic/orchestrator" +MISSION_JSON="$ORCH_DIR/mission.json" +COORD_LIB="$HOME/.config/mosaic/tools/orchestrator/_lib.sh" + +if [[ -f "$MISSION_JSON" ]] && command -v jq &>/dev/null; then + mission_status="$(jq -r '.status // "inactive"' "$MISSION_JSON")" + + if [[ "$mission_status" == "active" || "$mission_status" == "paused" ]]; then + mission_name="$(jq -r '.name // "unnamed"' "$MISSION_JSON")" + echo "" + echo "=========================================" + echo "ACTIVE MISSION DETECTED" + echo "=========================================" + echo " Mission: $mission_name" + + manifest="docs/MISSION-MANIFEST.md" + if [[ -f "$manifest" ]]; then + phase="$(grep -m1 '^\*\*Phase:\*\*' "$manifest" 2>/dev/null | sed 's/.*\*\*Phase:\*\* //' || true)" + milestone="$(grep -m1 '^\*\*Current Milestone:\*\*' "$manifest" 2>/dev/null | sed 's/.*\*\*Current Milestone:\*\* //' || true)" + progress="$(grep -m1 '^\*\*Progress:\*\*' "$manifest" 2>/dev/null | sed 's/.*\*\*Progress:\*\* //' || true)" + [[ -n "$phase" ]] && echo " Phase: $phase" + [[ -n "$milestone" ]] && echo " Milestone: $milestone" + [[ -n "$progress" ]] && echo " Progress: $progress" + fi + + if [[ -f "docs/TASKS.md" ]]; then + total="$(grep -c '^|' "docs/TASKS.md" 2>/dev/null || true)" + total="${total:-0}" + done_count="$(grep -ci '| done \|| completed ' "docs/TASKS.md" 2>/dev/null || true)" + done_count="${done_count:-0}" + approx_total=$(( total > 2 ? total - 2 : 0 )) + echo " Tasks: ~${done_count} done of ~${approx_total} total" + fi + + if [[ -d "docs/scratchpads" ]]; then + latest_sp="$(ls -t docs/scratchpads/*.md 2>/dev/null | head -1 || true)" + [[ -n "$latest_sp" ]] && echo " Scratchpad: $latest_sp" + fi + + echo "" + echo " Resume: Read manifest + scratchpad before taking action." + echo " Protocol: ~/.config/mosaic/guides/ORCHESTRATOR-PROTOCOL.md" + echo "=========================================" + echo "" + + if [[ -f "$COORD_LIB" ]]; then + # shellcheck source=/dev/null + source "$COORD_LIB" + sess_id="$(next_session_id ".")" + runtime="${MOSAIC_RUNTIME:-unknown}" + session_lock_write "." "$sess_id" "$runtime" "$$" + + updated="$(jq \ + --arg sid "$sess_id" \ + --arg rt "$runtime" \ + --arg ts "$(iso_now)" \ + '.sessions += [{"session_id":$sid,"runtime":$rt,"started_at":$ts,"ended_at":"","ended_reason":"","milestone_at_end":"","tasks_completed":[],"last_task_id":""}]' \ + "$MISSION_JSON")" + echo "$updated" > "$MISSION_JSON.tmp" && mv "$MISSION_JSON.tmp" "$MISSION_JSON" + fi + fi +fi + if declare -F mosaic_hook_session_start >/dev/null 2>&1; then run_step "Run repo start hook" mosaic_hook_session_start else