Skip to content

Commit 41ae7fa

Browse files
committed
chore: add the swarm scenario skill
The design calls for a swarm scenario run from T3 Code: agents in parallel workspaces landing overlapping work through a server and a CI worker, while processes are killed, then checks that chains, revisions, reruns, promotions and the single acceptance on `main` hold. `.claude/skills/swarm-scenario/SKILL.md` is that runbook, with the agent prompts and exact verification commands. `swarm` holds its helpers (per-user wrappers, the fixture and policy, the server and worker, the kill points, timings), shared with `scripted.sh`, a dry run of every step with scripted edits in place of the agents, which fails if origin regresses, such as a worker that dies with the server, or one user's rights refusing another's push.
1 parent 039b87f commit 41ae7fa

4 files changed

Lines changed: 726 additions & 0 deletions

File tree

Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
---
2+
name: swarm-scenario
3+
description: Runs Origin's swarm scenario on this machine. It starts `origin serve` and an `origin ci` worker, then has three agents, delegated through T3 Code, land overlapping changes on a feature target while processes are killed with kill -9. It then verifies the target chains, the revisions, which results reran or were promoted, and the single acceptance on `main`, and reports timings. Use when asked to run the swarm scenario, or to stress-test origin with parallel agents.
4+
---
5+
6+
# The swarm scenario
7+
8+
Three agents work in parallel workspaces of one clone and land on a feature
9+
target, `swarm`. One changes a shared function's signature, one adds a
10+
caller of the old signature, so the two conflict, and one edits only docs.
11+
Along the way a checkout, the CI worker and the server are killed with
12+
`kill -9`. Finally `swarm` lands on `main` in one acceptance, and step 9
13+
checks what should hold.
14+
15+
This is a runbook: run each block, watch the output, and note anything that
16+
differs from what's expected.
17+
18+
**Conventions.** Everything lives in `/tmp/origin-swarm`, which step 1
19+
deletes, and the server listens on `127.0.0.1:7979`. Run the blocks from
20+
the repository root, in the session's shell. Shell state doesn't carry
21+
over between Bash calls, so each block sets its variables. `$R/bin/<user>`
22+
runs the release `origin` as that user, with their own configuration and
23+
token. `swarm`, beside this file, holds the scenario's helpers: run it
24+
without arguments to list them. Start long-running processes (`swarm
25+
serve`, `swarm worker` and the `swarm kill-*` watchers) with the Bash
26+
tool's `run_in_background`.
27+
28+
**Do a dry run first.** `bash .claude/skills/swarm-scenario/scripted.sh`
29+
runs the same steps with scripted edits in place of the agents, in a
30+
temporary directory of its own, on port 7978. It takes about 15 s once
31+
built. If it fails, origin has regressed: stop and report its output.
32+
33+
## Known issues
34+
35+
None at the moment: the five the dry run found are fixed. In the report,
36+
describe anything that goes wrong in full.
37+
38+
## 1. Build
39+
40+
```sh
41+
R=/tmp/origin-swarm S=$PWD/.claude/skills/swarm-scenario/swarm
42+
rm -rf $R && mkdir -p $R/server && $S mark "1. build"
43+
cargo build --release --locked -p origin-src -p origin-ci
44+
```
45+
46+
## 2. The server
47+
48+
```sh
49+
R=/tmp/origin-swarm S=$PWD/.claude/skills/swarm-scenario/swarm
50+
$S mark "2. server"
51+
cat >$R/server/origin-server.toml <<'EOF'
52+
listen = "127.0.0.1:7979"
53+
node = "hosted"
54+
key = "node.key"
55+
56+
[repos]
57+
demo = "repos/demo"
58+
59+
[tokens]
60+
"user:lead" = "lead-secret"
61+
EOF
62+
target/release/origin repo create $R/server/repos/demo --config $R/server/origin-server.toml --admin user:lead
63+
$S user lead lead-secret
64+
```
65+
66+
Start `.claude/skills/swarm-scenario/swarm serve` in the background, and
67+
wait for `Listening on` in `/tmp/origin-swarm/serve.log`. `user:lead` acts
68+
for you: it changes the policy, creates `swarm`, and lands it on `main`.
69+
70+
## 3. The fixture and the policy
71+
72+
```sh
73+
R=/tmp/origin-swarm S=$PWD/.claude/skills/swarm-scenario/swarm URL=http://127.0.0.1:7979/demo
74+
$S mark "3. fixture and policy"
75+
$R/bin/lead clone $URL $R/lead
76+
(cd $R/lead && $S checks && $R/bin/lead describe "Add the checks" && $R/bin/lead land)
77+
$R/bin/lead clone $URL $R/policy --target policy
78+
(cd $R/policy && $S policy && $R/bin/lead describe "Require ci.test from the runners" && $R/bin/lead land)
79+
(cd $R/lead && $S fixture && $R/bin/lead describe "Add the greeter" && $R/bin/lead land)
80+
```
81+
82+
`swarm policy` makes `main` require `ci.test` from `group:runners`, which
83+
holds `user:ci`. It also makes `swarm` require both `ci.test` and
84+
`ci.docs`, so each agent's landing shows a rerun or a promotion. `swarm
85+
checks` writes `checks.toml`, with `ci.test` (`sleep 2 && python3 -m
86+
unittest discover -s src`, inputs `src`) and `ci.docs` (inputs `docs`). It
87+
lands on `main` first, before the policy requires anything: the worker
88+
reads the checks from the tip of the target landed on. `swarm fixture`
89+
then writes:
90+
91+
- `src/greet.py`, which defines `greet(name)`;
92+
- `src/app.py`, whose `welcome()` calls it;
93+
- `src/test_app.py`;
94+
- `docs/guide.md`;
95+
- 5,000 files under `assets/`, so a checkout lasts long enough to be killed.
96+
97+
Expected: the checks land as `main #1`, and the policy as `policy #1`. The
98+
greeter's landing waits for `ci.test pass from group:runners`, because no
99+
runner is up yet.
100+
101+
## 4. Tokens
102+
103+
```sh
104+
R=/tmp/origin-swarm S=$PWD/.claude/skills/swarm-scenario/swarm
105+
$S mark "4. tokens"
106+
token() { target/release/origin token issue --config $R/server/origin-server.toml --ttl 1d --as "$@"; }
107+
$S user ci "$(token user:ci --scope 'read /' --scope 'annotate ci.*')"
108+
$S user agent-a "$(token user:agent-a --scope 'read /' --scope 'write src' --scope 'land swarm')"
109+
$S user agent-b "$(token user:agent-b --scope 'read /' --scope 'write src' --scope 'land swarm')"
110+
$S user agent-c "$(token user:agent-c --scope 'read /' --scope 'write docs' --scope 'land swarm')"
111+
```
112+
113+
No agent can land on `main` or write outside its directory.
114+
115+
## 5. The worker
116+
117+
Run `.claude/skills/swarm-scenario/swarm mark "5. worker"`, then start
118+
`.claude/skills/swarm-scenario/swarm worker` in the background. Within a
119+
few seconds, `/tmp/origin-swarm/worker.log` should show `ci.test pass in
120+
2.0…s`, and `(cd /tmp/origin-swarm/lead && /tmp/origin-swarm/bin/lead land
121+
--status)` should print `No landing waits.`
122+
123+
## 6. Workspaces and agents
124+
125+
```sh
126+
R=/tmp/origin-swarm S=$PWD/.claude/skills/swarm-scenario/swarm URL=http://127.0.0.1:7979/demo
127+
$S mark "6. agents"
128+
(cd $R/lead && $R/bin/lead target new swarm --from main)
129+
$R/bin/lead clone $URL $R/clone --target swarm
130+
(cd $R/clone && $R/bin/agent-a workspace new $R/ws-a && $R/bin/agent-b workspace new $R/ws-b && $S kill-checkout c)
131+
```
132+
133+
`swarm kill-checkout c` is the first kill. It kills agent c's `workspace
134+
new` mid-checkout and shows the workspace as `(incomplete)` in `workspace
135+
list`. Then it removes the workspace and creates it again. Note how many
136+
files the checkout got through.
137+
138+
Before delegating, arm the other two kills, each in the background:
139+
140+
- `.claude/skills/swarm-scenario/swarm kill-worker-on-check` kills the
141+
worker as soon as it starts the first agent's `ci.test`, then becomes the
142+
new worker.
143+
- `.claude/skills/swarm-scenario/swarm kill-serve-after-push` kills the
144+
server once agent c says it pushed, then becomes the new server and tells
145+
c to go on.
146+
147+
Then delegate the agents. Make three `delegate_task` calls in one message,
148+
with `mode: "async"`, titled `swarm agent a`, `swarm agent b` and `swarm
149+
agent c`. Each task is the shared prompt, with the agent's letter in place
150+
of `x`, followed by that agent's task. The agents run shell commands
151+
without asking, so delegate from a full-access session. Note when they
152+
start.
153+
154+
**Shared prompt:**
155+
156+
> You are agent x in a test of Origin, a version control system. It is not
157+
> git: don't run git. Your workspace is `/tmp/origin-swarm/ws-x`. Run
158+
> origin only as `/tmp/origin-swarm/bin/agent-x`, written `origin` below,
159+
> and start every shell command with `cd /tmp/origin-swarm/ws-x &&`. There
160+
> is no commit and no staging area: commands that build on your change,
161+
> such as `describe` and `land`, record the working copy's files first.
162+
>
163+
> 1. Run `origin status`: your change is on the target `swarm`.
164+
> 2. Do your task, below. Edit only the files it names.
165+
> 3. Run `origin describe "<the description your task gives>"`.
166+
> 4. Run `origin land --onto swarm`.
167+
> 5. If the landing waits for checks, run `origin land --status` every 5
168+
> seconds until your change isn't listed, for up to 3 minutes. Then run
169+
> `origin log swarm`.
170+
> 6. If the landing conflicts, stop there. Don't resolve the conflict.
171+
> 7. The server is restarted on purpose during the test. If a command
172+
> fails with `cannot reach the server`, wait 5 seconds and retry it, up
173+
> to 12 times.
174+
>
175+
> Report every origin command you ran and its output, your change's id
176+
> from `origin status`, and how long the whole task took you.
177+
178+
**Agent a:** In `src/greet.py`, change `greet(name)` to `greet(name,
179+
punctuation)`, returning `f"Hello, {name}{punctuation}"`. Make `welcome()`
180+
in `src/app.py` pass `"!"`. Check your work with `python3 -m unittest
181+
discover -s src`. Description: "Make greet take the punctuation".
182+
183+
**Agent b:** In `src/app.py`, add `def farewell(): return
184+
greet("friends") + " Goodbye."`. It calls `greet` with the one argument
185+
it takes in your workspace. Never run `origin pull`. Check your work with
186+
`python3 -m unittest discover -s src`. Description: "Add a farewell".
187+
188+
**Agent c:** In `docs/guide.md`, add the line ``Test it with `python3 -m
189+
unittest discover -s src`.`` Description: "Document the tests". Replace
190+
step 4 with these steps:
191+
192+
1. Run `origin push`.
193+
2. Run `touch /tmp/origin-swarm/c-pushed`.
194+
3. Wait until `/tmp/origin-swarm/serve-restarted` exists. Check every 2
195+
seconds, for up to 5 minutes.
196+
4. Run `origin fetch` and `origin log swarm` every 5 seconds, for up to 3
197+
minutes, until the log shows `swarm #1`.
198+
5. Run `origin land --onto swarm`.
199+
200+
## 7. While they work
201+
202+
- The worker kill writes `killed the worker mid-check` to `worker.log`,
203+
followed by a new `Answering …` line. The check it cut short then runs
204+
again.
205+
- The server kill writes `killed the server after c's push` to
206+
`serve.log`. The worker outlives it: `worker.log` shows `Cannot reach
207+
the remote` until the server is back, and `kill -0 $(cat
208+
/tmp/origin-swarm/worker.pid)` succeeds.
209+
- To see what waits, and why, run `(cd /tmp/origin-swarm/lead &&
210+
/tmp/origin-swarm/bin/lead land --status)`.
211+
212+
Expected:
213+
214+
- Whichever of a and b lands second conflicts in `src/app.py`, with a
215+
`core.conflict`.
216+
- c's landing waits only for `ci.docs`, because its `ci.test` result is
217+
promoted from that of the first of a and b to land, on `swarm`. Results
218+
carry over within a target only, which is why c waits for `swarm #1`.
219+
220+
## 8. Resolve the conflict, then land on `main`
221+
222+
Run `.claude/skills/swarm-scenario/swarm mark "8. resolve"`. Then set `X`
223+
to the agent whose landing conflicted, `a` or `b`:
224+
225+
```sh
226+
R=/tmp/origin-swarm X=b
227+
(cd $R/ws-$X && $R/bin/agent-$X pull) # "Conflicts: src/app.py hold both versions ..."
228+
cp $R/ws-$X/src/app.py $R/app.py.$X # keep both versions, with the markers
229+
(cd $R/clone && $R/bin/lead pull) # the clone's working copy moves to swarm's tip
230+
cp $R/clone/src/app.py $R/ws-$X/src/app.py # take swarm's side
231+
```
232+
233+
`pull` rebased the change onto swarm's tip, with both versions of
234+
`src/app.py` between conflict markers and a `core.conflict` listing it, so
235+
the change can't land until `src/app.py` changes (or `resolve` accepts it
236+
as it is). Redo the agent's edit
237+
on swarm's version of `src/app.py`, so that every call passes the
238+
punctuation: for b, `farewell` calls `greet("friends", ".")`. Then test and
239+
land:
240+
241+
```sh
242+
R=/tmp/origin-swarm X=b
243+
(cd $R/ws-$X && python3 -m unittest discover -s src && $R/bin/agent-$X land --onto swarm)
244+
```
245+
246+
The landing waits for `ci.test`, which reruns because `src` changed, while
247+
`ci.docs` is promoted. Once `land --status` no longer lists the change,
248+
land `swarm` on `main`:
249+
250+
```sh
251+
R=/tmp/origin-swarm S=$PWD/.claude/skills/swarm-scenario/swarm
252+
$S mark "8b. land swarm on main"
253+
(cd $R/lead && $R/bin/lead land --from swarm --onto main)
254+
```
255+
256+
The landing is checked as the merge it would make, a revision on `main`'s
257+
tip, with `main`'s checks: results never carry over from one target to
258+
another, whose checks may differ, so it waits while the worker runs
259+
`ci.test` once more. Run
260+
`(cd $R/lead && $R/bin/lead fetch && $R/bin/lead log main)` until `main #3`
261+
shows.
262+
263+
## 9. Verify
264+
265+
The target chains are the same in a fresh clone:
266+
267+
```sh
268+
R=/tmp/origin-swarm S=$PWD/.claude/skills/swarm-scenario/swarm URL=http://127.0.0.1:7979/demo
269+
$S mark "9. verify"
270+
(cd $R/lead && $R/bin/lead fetch --target swarm && $R/bin/lead log main && $R/bin/lead log swarm)
271+
$R/bin/lead clone $URL $R/fresh --target swarm && (cd $R/fresh && $R/bin/lead fetch --target main)
272+
for t in main swarm; do
273+
diff <(cd $R/lead && $R/bin/lead log $t | grep -v -e '^@' -e '^Behind: ') <(cd $R/fresh && $R/bin/lead log $t | grep -v -e '^@' -e '^Behind: ') && echo "$t: same"
274+
done
275+
```
276+
277+
Expected:
278+
279+
- `main` has exactly four acceptances, #0 to #3, and `main #3` lists
280+
`swarm #3`: the whole swarm reached `main` in one acceptance.
281+
- `swarm` has #0 to #3, one agent's change on each of #1 to #3.
282+
- Both chains print `same`.
283+
284+
No revision is lost: the server has every revision the agent's workspace
285+
has. Each clone lists revisions in the order they arrived, and the agent's
286+
marks its working copy with `@`. Run this for each agent, with `c` set to
287+
the change id it reported:
288+
289+
```sh
290+
R=/tmp/origin-swarm x=a c=<change id>
291+
full=$(cd $R/ws-$x && $R/bin/agent-$x show $c | sed -n 's/^Change \([0-9a-f]\{32\}\)$/\1/p')
292+
(cd $R/fresh && $R/bin/lead fetch --change $full)
293+
diff <(cd $R/fresh && $R/bin/lead evolog $c | sort) <(cd $R/ws-$x && $R/bin/agent-$x evolog $c | sed 's/^@/○/' | sort) && echo "$x: no revision lost"
294+
```
295+
296+
Results reran or were promoted as they should. `statements` reads the
297+
records a clone holds, and the fresh clone fetched every change above:
298+
299+
```sh
300+
R=/tmp/origin-swarm
301+
(cd $R/fresh && $R/bin/lead statements <c's change>)
302+
(cd $R/fresh && $R/bin/lead statements <the resolved change>)
303+
grep -E 'pass in|promoted|killed|Answering' $R/worker.log
304+
```
305+
306+
- c's `ci.test` is `by node:hosted`, `promoted from statement … by
307+
user:ci`: promoted, not rerun. Its `ci.docs` is `by user:ci`.
308+
- The resolved change's `ci.test` is `by user:ci` (stale, so it reran),
309+
and its `ci.docs` is promoted.
310+
- The worker ran `ci.test` for the greeter, for the first of a and b to
311+
land, for the resolved change, and for `swarm` landing on `main`. It also
312+
ran it for the check the kill cut short, and for any revision a later one
313+
replaced. It never ran `ci.test` for c.
314+
315+
## 10. Report
316+
317+
`.claude/skills/swarm-scenario/swarm times` prints how long each step took.
318+
Report:
319+
320+
- the step timings, and how long each agent took;
321+
- each check of step 9: whether it held, with the output if not;
322+
- each kill: when it happened, and how origin recovered;
323+
- everything that went wrong, each with the command, what you expected,
324+
and what happened.
325+
326+
Then stop the server and the worker: `kill -9 $(cat /tmp/origin-swarm/serve.pid
327+
/tmp/origin-swarm/worker.pid)`.

0 commit comments

Comments
 (0)