|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Example: Parallel agents — 3 agents working on the same file simultaneously |
| 3 | +# |
| 4 | +# Demonstrates: multiple claims on different functions in the SAME file, |
| 5 | +# parallel worktrees, sequential done (merge serialization) |
| 6 | + |
| 7 | +set -euo pipefail |
| 8 | + |
| 9 | +PROJECT_DIR=$(mktemp -d) |
| 10 | +echo "=== Creating sample project in $PROJECT_DIR ===" |
| 11 | + |
| 12 | +cd "$PROJECT_DIR" |
| 13 | +git init -q |
| 14 | +mkdir -p src |
| 15 | + |
| 16 | +cat > src/auth.ts << 'EOF' |
| 17 | +export function validateToken(token: string): boolean { |
| 18 | + return token.length > 0; |
| 19 | +} |
| 20 | +
|
| 21 | +export function refreshToken(token: string): string { |
| 22 | + return token + "-refreshed"; |
| 23 | +} |
| 24 | +
|
| 25 | +export function revokeToken(token: string): void { |
| 26 | + console.log("revoked:", token); |
| 27 | +} |
| 28 | +
|
| 29 | +export function generateToken(user: string): string { |
| 30 | + return btoa(user + ":" + Date.now()); |
| 31 | +} |
| 32 | +
|
| 33 | +export function parseToken(token: string): { user: string; exp: number } { |
| 34 | + const decoded = atob(token); |
| 35 | + const [user, exp] = decoded.split(":"); |
| 36 | + return { user, exp: parseInt(exp) }; |
| 37 | +} |
| 38 | +EOF |
| 39 | + |
| 40 | +git add -A && git commit -q -m "initial commit" |
| 41 | + |
| 42 | +# Initialize |
| 43 | +grit init |
| 44 | +echo "" |
| 45 | +echo "=== Symbols indexed ===" |
| 46 | +grit symbols |
| 47 | + |
| 48 | +# 3 agents claim different functions in the SAME file |
| 49 | +echo "" |
| 50 | +echo "=== Agent 1: claims validateToken + parseToken ===" |
| 51 | +grit claim -a agent-1 -i "add JWT validation" \ |
| 52 | + "src/auth.ts::validateToken" \ |
| 53 | + "src/auth.ts::parseToken" |
| 54 | + |
| 55 | +echo "" |
| 56 | +echo "=== Agent 2: claims refreshToken ===" |
| 57 | +grit claim -a agent-2 -i "add expiry check" \ |
| 58 | + "src/auth.ts::refreshToken" |
| 59 | + |
| 60 | +echo "" |
| 61 | +echo "=== Agent 3: claims generateToken ===" |
| 62 | +grit claim -a agent-3 -i "use crypto.randomUUID" \ |
| 63 | + "src/auth.ts::generateToken" |
| 64 | + |
| 65 | +echo "" |
| 66 | +echo "=== Status: 3 agents, same file, no conflicts ===" |
| 67 | +grit status |
| 68 | + |
| 69 | +# Agent 2 tries to claim a symbol already held by Agent 1 |
| 70 | +echo "" |
| 71 | +echo "=== Agent 2 tries to claim parseToken (held by Agent 1) ===" |
| 72 | +grit claim -a agent-2 -i "also want parseToken" \ |
| 73 | + "src/auth.ts::parseToken" 2>&1 || echo " → Blocked as expected!" |
| 74 | + |
| 75 | +# Each agent edits in their own worktree |
| 76 | +echo "" |
| 77 | +echo "=== Agents work in parallel worktrees ===" |
| 78 | +ls -la .grit/worktrees/ |
| 79 | + |
| 80 | +# Agent 1 finishes |
| 81 | +echo "" |
| 82 | +echo "=== Agent 1: done ===" |
| 83 | +grit done -a agent-1 |
| 84 | + |
| 85 | +# Agent 2 finishes |
| 86 | +echo "" |
| 87 | +echo "=== Agent 2: done ===" |
| 88 | +grit done -a agent-2 |
| 89 | + |
| 90 | +# Agent 3 finishes |
| 91 | +echo "" |
| 92 | +echo "=== Agent 3: done ===" |
| 93 | +grit done -a agent-3 |
| 94 | + |
| 95 | +echo "" |
| 96 | +echo "=== Final status: all locks released ===" |
| 97 | +grit status |
| 98 | + |
| 99 | +echo "" |
| 100 | +echo "=== Git log: each agent's merge is a separate commit ===" |
| 101 | +git log --oneline -10 |
| 102 | + |
| 103 | +rm -rf "$PROJECT_DIR" |
0 commit comments