#!/usr/bin/env bash # Builds coord-episode14/ (from-import) and coord-episode14-bare/ (bare-import # contrast) — disposable repos demonstrating muse coord for Episode 14 # ("Agent Coordination"). Re-run to regenerate; never hand-edit the repo # directories directly. set -euo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO="$HERE/coord-episode14" BARE_REPO="$HERE/coord-episode14-bare" rm -rf "$REPO" "$BARE_REPO" echo "=== Part 1: shard on a realistic (from-import) codebase ===" mkdir -p "$REPO" cd "$REPO" muse init --json | python3 -m json.tool cat > app.py <<'EOF' from auth import login from billing import charge def handle_request(username, password, amount): if login(username, password): return charge(username, amount) return None EOF cat > auth.py <<'EOF' from db import query def login(username, password): row = query(f"SELECT * FROM users WHERE username='{username}'") return row is not None EOF cat > billing.py <<'EOF' from db import query def charge(username, amount): query(f"UPDATE accounts SET balance = balance - {amount} WHERE username='{username}'") return True EOF cat > db.py <<'EOF' def query(sql): return None EOF muse code add . muse commit -m "Initial: app, auth, billing, db (from-import style)" \ --agent-id claude-code --model-id claude-sonnet-5 --sign echo "--- real call graph exists (impact) ---" muse code impact "auth.py::login" --json | python3 -m json.tool echo "--- shard claims zero coupling (BUG) ---" muse coord shard --agents 2 --json | python3 -m json.tool echo echo "=== Part 2: same coupling, bare-import style — the honest contrast ===" mkdir -p "$BARE_REPO" cd "$BARE_REPO" muse init --json | python3 -m json.tool cat > app.py <<'EOF' import auth import billing def handle_request(username, password, amount): if auth.login(username, password): return billing.charge(username, amount) return None EOF cat > auth.py <<'EOF' import db def login(username, password): return db.query("SELECT 1") is not None EOF cat > billing.py <<'EOF' import db def charge(username, amount): db.query("UPDATE accounts SET balance = balance - 1") return True EOF cat > db.py <<'EOF' def query(sql): return None EOF muse code add . muse commit -m "Initial: bare-import style" \ --agent-id claude-code --model-id claude-sonnet-5 --sign echo "--- shard correctly refuses to split coupled code ---" muse coord shard --agents 2 --json | python3 -m json.tool echo echo "=== Part 3: reservations — real conflict detection ===" muse coord reserve "app.py::handle_request" --run-id agent-1 --op modify --json | python3 -m json.tool muse coord reserve "app.py::handle_request" --run-id agent-2 --op modify --json | python3 -m json.tool echo "--- forecast: predicted conflict, confidence 1.0 ---" muse coord forecast --json | python3 -m json.tool echo echo "=== Part 4: the task queue — atomic claims across two agents ===" muse coord enqueue "Refactor billing.charge to support partial refunds" --run-id orch --json | python3 -m json.tool muse coord enqueue "Add rate limiting to auth.login" --run-id orch --json | python3 -m json.tool echo "--- agent-1 claims ---" muse coord claim --run-id agent-1 --json | python3 -m json.tool echo "--- agent-2 claims (gets the OTHER task, not a collision) ---" muse coord claim --run-id agent-2 --json | python3 -m json.tool echo "--- a third agent finds the queue empty ---" muse coord claim --run-id agent-3 --json | python3 -m json.tool || true echo echo "=== Part 5: reconcile — real merge-order recommendation across branches ===" muse switch main muse checkout -b feat/agent-a muse coord reserve "app.py::handle_request" --run-id agent-1 --op modify --json > /dev/null muse switch main muse checkout -b feat/agent-b muse coord reserve "app.py::handle_request" --run-id agent-2 --op modify --json > /dev/null muse coord reconcile --json | python3 -m json.tool echo echo "Demo repos built at $REPO and $BARE_REPO"