feat: auto-archive completed runs before starting new features

When /agent-loop:run detects a previous run with all stories passed (or the
feature branch deleted after merge), it archives the old artifacts and resets
.loop/ automatically — no more manual rm -rf .loop.

- Add archive_and_reset() for on-demand archiving from skills
- Add runs.log index tracking all archived runs
- Update /run and /stories skills to detect completed runs
- setup.sh archives instead of hard-failing when prd.json exists
- Bump version to 0.9.0
This commit is contained in:
2026-04-02 07:40:07 -04:00
parent ce111b4cbe
commit ad58a49182
6 changed files with 157 additions and 12 deletions

View File

@@ -1,11 +1,17 @@
#!/bin/bash
# Branch archiving — archives previous run artifacts when the branch changes.
# Preserves prd.json, progress.md, and contracts from the previous feature.
# Run archiving — preserves prd.json, progress.md, and contracts from completed runs.
#
# Design: At the end of each run, snapshot_for_archive saves current artifacts
# to .archive-staging/. On the next run, if the branch changed, check_archive
# moves the snapshot to archive/ and cleans up. This avoids archiving the
# WRONG artifacts (the new feature's) when prd.json has already been overwritten.
# Two archive triggers:
# 1. Branch change: check_archive detects a new branch and archives the staged snapshot.
# 2. Completed run: archive_and_reset is called by the /run skill when prd.json shows
# all stories passed (or the branch was deleted). This handles the common workflow
# of merging a feature branch back to main and starting a new feature.
#
# Archive layout:
# .loop/archive/
# runs.log — one-line-per-run index for quick lookup
# 2026-03-15-auth-system/ — full artifacts from that run
# prd.json, progress.md, contracts/
LAST_BRANCH_FILE="$LOOP_DIR/.last-branch"
STAGING_DIR="$LOOP_DIR/.archive-staging"
@@ -85,5 +91,91 @@ archive_run() {
rm -f "$LOOP_DIR/progress.md"
rm -rf "$LOOP_DIR/contracts"
append_runs_log "$branch_name" "$archive_dir"
log "Archived previous run to $archive_dir"
}
# Archive current run artifacts and reset for a new run.
# Called by the /run skill when a completed run is detected (all stories passed
# or the feature branch no longer exists). Unlike check_archive (which reads from
# staging), this archives the LIVE artifacts directly since we know they belong
# to the completed run.
archive_and_reset() {
local loop_dir="${1:-.loop}"
local prd="$loop_dir/prd.json"
[ -f "$prd" ] || return 0
# Read branch name from current prd.json
local branch_name=""
if command -v jq &>/dev/null; then
branch_name=$(jq -r '.branchName // empty' "$prd" 2>/dev/null)
elif command -v python3 &>/dev/null; then
branch_name=$(LOOP_PRD="$prd" python3 -c "
import json, os
print(json.load(open(os.environ['LOOP_PRD'])).get('branchName', ''), end='')
" 2>/dev/null)
fi
local feature_name
feature_name=$(echo "${branch_name:-unknown}" | sed 's|.*/||')
local archive_dir="$loop_dir/archive/$(date +%Y-%m-%d)-${feature_name}"
mkdir -p "$archive_dir"
# Archive live artifacts
[ -f "$prd" ] && cp "$prd" "$archive_dir/"
[ -f "$loop_dir/progress.md" ] && cp "$loop_dir/progress.md" "$archive_dir/"
[ -f "$loop_dir/progress-archive.md" ] && cp "$loop_dir/progress-archive.md" "$archive_dir/"
[ -d "$loop_dir/contracts" ] && cp -r "$loop_dir/contracts" "$archive_dir/"
# Verify archive has content before deleting originals
if ! find "$archive_dir" -maxdepth 1 -type f | read -r; then
echo "[archive] WARNING: Archive directory is empty — skipping reset to prevent data loss"
return 1
fi
append_runs_log "$branch_name" "$archive_dir"
# Reset run-specific files (keep config.json, init.sh, harness files)
rm -f "$loop_dir/prd.json"
rm -f "$loop_dir/progress.md"
rm -f "$loop_dir/progress-archive.md"
rm -rf "$loop_dir/contracts"
rm -rf "$loop_dir/.archive-staging"
rm -f "$loop_dir/.last-branch"
rm -f "$loop_dir/.verdict"
echo "[archive] Archived completed run to $archive_dir"
echo "[archive] .loop/ reset — ready for new stories"
}
# Append a one-line summary to the runs log.
append_runs_log() {
local branch_name="$1"
local archive_dir="$2"
local runs_log
runs_log="$(dirname "$archive_dir")/runs.log"
# Read story counts from the archived prd.json
local total=0 passed=0 blocked=0
local archived_prd="$archive_dir/prd.json"
if [ -f "$archived_prd" ]; then
if command -v jq &>/dev/null; then
total=$(jq '.userStories | length' "$archived_prd" 2>/dev/null || echo 0)
passed=$(jq '[.userStories[] | select(.passes == true)] | length' "$archived_prd" 2>/dev/null || echo 0)
blocked=$(jq '[.userStories[] | select(.blocked == true)] | length' "$archived_prd" 2>/dev/null || echo 0)
elif command -v python3 &>/dev/null; then
eval "$(LOOP_PRD="$archived_prd" python3 -c "
import json, os
d = json.load(open(os.environ['LOOP_PRD']))
s = d.get('userStories', [])
print(f'total={len(s)} passed={sum(1 for x in s if x.get(\"passes\"))} blocked={sum(1 for x in s if x.get(\"blocked\"))}')
" 2>/dev/null)" || true
fi
fi
printf '%s %-30s %s/%s passed %s blocked\n' \
"$(date +%Y-%m-%d)" "${branch_name:-unknown}" "$passed" "$total" "$blocked" \
>> "$runs_log"
}