The workspace probe's helper: a second plugin that starts the agent in its slot, guards its writes and gates its finish

A software factory that runs inside a single Claude Code session.
How it works · Architecture · Docs · Releases
<img alt="The factory as a session in the Claude app: the loop asks the model whether to cut a release, and the session title shows the issues queued, in flight and merged" src="docs/images/hero-light.svg" width="744"> <sub>The session title is a live status board: Q queued · D in flight · M merged</sub>
Label a GitHub issue ready. The factory designs it, builds it, reviews it and merges it. When it needs a decision, it asks you - on the issue, or in the Claude app on your phone.
Most agent factories put a program in charge and call Claude for each task. Others hand one model the whole job and let it improvise. This one puts the program inside the Claude session.
/factory pause, priority or drop.flowchart LR
you(["You"])
gh["GitHub<br/>issues, pull requests, CI"]
app["Claude app<br/>phone, desktop, terminal"]
you ==>|"create issues, label them ready"| gh
you -.->|"optional: ask, steer, answer"| app
subgraph factory["The factory: one Claude Code session"]
loop["Loop (code)<br/>plans and acts every minute"]
model["Model<br/>judgment and owner chat"]
agents["Agents<br/>design, build, review, fix"]
loop -->|wakes| model
loop --> agents
end
gh <-->|"claims issues, merges PRs"| loop
app <-.-> model
Writing issues is enough: the factory picks up anything labelled ready and comes back with a merged pull request. The Claude app is there for when you want to talk to it.
<img alt="The line: an issue moves from queued through design, build, review and the merge queue to merged; review sends changes to fix and back, and a stuck issue is parked with a question for you, then requeued when you answer" src="docs/images/pipeline-light.svg" width="744">
| Command | What it does | ||
|---|---|---|---|
status | The last pass and the directives in force | ||
pause <N> / unpause <N> | No action at all on an issue, though its claim still renews | ||
skip <N> / noskip <N> | Never claim an issue | ||
priority <N...> | Claim these first, in order | ||
only <N...> | Claim only these, in order | ||
| `serial on\ | off` | One issue in flight at a time | |
| `new on\ | off` | Off starts no new claims, designs or implementations | |
resume <N> | Release a hold on an issue or a run | ||
retry <N> | Let an issue's exhausted actions run again | ||
drop <N> | Abandon an issue and stop its agents | ||
| `note [<N>] <text>\ | off` | An instruction the next agent prompts carry; a newer note replaces the older one, off clears it | |
| `mode off\ | observe\ | act` | Switch the loop off, to watching only, or to acting |
| `trace on\ | off` | Detailed per-pass timing records |
The full list, with every option, is in the factory loop README.
plan(state, config) does no I/O and reads no clock, so recorded days of events can be replayed through it.[!NOTE] A factory runs in a Coder workspace built from this repo's
orchestrator-looptemplate: the base image, a factory database and the factory-loop plugins.Where we're heading is simpler: install a plugin, type one command, and the session you're in becomes the factory. Set up a factory.
This evolved from my own setup, which relies heavily on Coder, a Docker swarm to run the workloads, and a few other things. I hope to soon have the capacity to make it more general, and I'm open to learning how someone else would want to run it.
| Page | Read it for |
|---|---|
| Factory loop | Running and steering the loop, /factory commands |
| Factory loop v2 spec | States, steps, failure handling, the merge queue |
| Onboarding | Making a repo a dev-system repo |
| Architecture | Layers, versioning, the runtime map |
| Base image | The environment every workspace runs |
| Factory database and metrics | Standing up the database; measuring the factory |
| Releasing | Cutting a release |
hooks/register.ts 102 lines1// The workspace probe's helper (docs/factory-loop/v2/SPEC.md 10.9): a plugin other than the
2// spawner, in the companion plugin's role. Its agent.spawn hook starts an agent whose prompt opens
3// with `factory-slot: <path>` in that folder and maps the agent's id to it; its tool.call hook logs
4// the agent's calls and refuses a Write outside the slot; its classic.SubagentStop hook runs a gate
5// of about 75 s in the slot with $.process.run and blocks the finish while it fails. In the
6// gate-queue-bare case it waits 540 s in 5 s host-call slices and then runs a 300 s command, the shape of
7// a queued gate in the companion (hooks/register.ts runStopGate), and logs when each ends or fails.
8// In the timeout cases it only logs, so the Bash call's own `timeout` shows the agent passed none.
9import type { EngineInterface, Register } from 'claude-code'
10
11// fails until done.txt holds the line `fixed`; about 75 s either way
12const GATE = 'sleep 75; if grep -qx fixed done.txt; then echo gate passed; else echo "done.txt must hold exactly the line: fixed. Write it with the Write tool." >&2; exit 2; fi'
13
14// the gate-queue-bare case: PROBE_WAIT_S (540) of waiting in SLICE_S host-call slices (the companion's IDLE_S),
15// then a PROBE_RUN_S (300) command
16const SLICE_S = 5
17
18const slotOf = new Map<string, string>()
19
20async function cfg($: EngineInterface) {
21 return { run: (await $.env.get('PROBE_RUN')) || '/tmp/workspace-probe' }
22}
23
24async function log($: EngineInterface, check: string, fields: Record<string, unknown> = {}) {
25 const { run } = await cfg($)
26 const line = JSON.stringify({ at: new Date(await $.clock.now()).toISOString(), plugin: 'helper', check, ...fields }) + '\n'
27 await $.process.run(['sh', '-c', 'mkdir -p "$(dirname "$1")" && cat >> "$1"', 'sh', `${run}/log.jsonl`], { stdin: line, timeoutMs: 10_000 }).catch(() => undefined)
28}
29
30function norm(p: string): string {
31 const out: string[] = []
32 for (const part of p.split('/')) {
33 if (part === '' || part === '.') continue
34 if (part === '..') out.pop()
35 else out.push(part)
36 }
37 return '/' + out.join('/')
38}
39
40// queued: wait, then run, as the companion's runStopGate does for a gate behind another. The run's
41// timeoutMs is its own 600 s, so only the engine can cut it.
42async function queued($: EngineInterface, e: { agent_id: string }, next: (e: never) => Promise<unknown>, slot: string, t0: number) {
43 const waitS = Number((await $.env.get('PROBE_WAIT_S')) ?? 540)
44 const runS = Number((await $.env.get('PROBE_RUN_S')) ?? 300)
45 await log($, 'queue-start', { agentId: e.agent_id, waitS, runS })
46 const secs = async () => Math.round(((await $.clock.now()) - t0) / 1000)
47 try {
48 while ((await $.clock.now()) - t0 < waitS * 1000) await $.process.run(['sleep', String(SLICE_S)], { timeoutMs: SLICE_S * 1000 + 15_000 })
49 await log($, 'queue-waited', { agentId: e.agent_id, secs: await secs() })
50 const ran = await $.process.run(['sh', '-c', `sleep ${runS}; echo ran`], { cwd: slot, timeoutMs: 600_000 })
51 await log($, 'queue-end', { agentId: e.agent_id, exitCode: ran.exitCode, secs: await secs() })
52 } catch (err) {
53 await log($, 'queue-error', { agentId: e.agent_id, error: String(err), secs: await secs() })
54 }
55 return (next as (e: unknown) => Promise<unknown>)(e)
56}
57
58export const register: Register = on => {
59 on('agent.spawn', async ($, e, next) => {
60 const m = /^factory-slot: (\/\S+)$/m.exec(e.prompt)
61 const r = await next(m === null ? e : { ...e, cwd: m[1] })
62 if (m !== null && r.agentId !== undefined) slotOf.set(r.agentId, m[1])
63 await log($, 'agent.spawn', { origin: (next as unknown as { origin?: unknown }).origin ?? null, resume: e.resume?.agentId ?? null, cwdIn: e.cwd ?? null, slot: m?.[1] ?? null, agentId: r.agentId ?? null, deny: r.deny ?? null })
64 return r
65 }).catch(($, e, next) => next(e))
66
67 on('tool.call', async ($, e, next) => {
68 if (e.agentId === undefined) return next(e)
69 const input = e as unknown as Record<string, unknown>
70 const slot = slotOf.get(e.agentId) ?? null
71 await log($, 'tool.call', { agentId: e.agentId, slot, tool: e.tool, command: input.command ?? null, timeout: input.timeout ?? null, file_path: input.file_path ?? null })
72 if (slot !== null && e.tool === 'Write' && typeof input.file_path === 'string') {
73 const p = norm(input.file_path.startsWith('/') ? input.file_path : `${slot}/${input.file_path}`)
74 if (p !== slot && !p.startsWith(`${slot}/`)) {
75 await log($, 'guard-deny', { agentId: e.agentId, file_path: input.file_path })
76 return { deny: `workspace-probe: ${input.file_path} is outside your slot ${slot}` }
77 }
78 }
79 const t0 = await $.clock.now()
80 const r = await next(e)
81 await log($, 'tool.result', { agentId: e.agentId, tool: e.tool, ms: (await $.clock.now()) - t0, isError: r.isError ?? false, text: String(r.text ?? r.deny ?? '').slice(0, 600) })
82 return r
83 }).catch(($, e, next) => (next.called ? next(e) : { deny: 'workspace-probe: its guard failed' }))
84
85 on('classic.SubagentStop', async ($, e, next) => {
86 const slot = slotOf.get(e.agent_id) ?? null
87 if (slot === null) return next(e)
88 const t0 = await $.clock.now()
89 if ((await $.env.get('PROBE_CASE')) === 'gate-queue-bare') return queued($, e, next, slot, t0)
90 await log($, 'gate-start', { agentId: e.agent_id, stop_hook_active: e.stop_hook_active, cwd: e.cwd })
91 try {
92 const ran = await $.process.run(['sh', '-c', GATE], { cwd: slot, timeoutMs: 600_000 })
93 const secs = Math.round(((await $.clock.now()) - t0) / 1000)
94 await log($, 'gate-end', { agentId: e.agent_id, exitCode: ran.exitCode, secs, stderr: ran.stderr.trim() })
95 if (ran.exitCode !== 0) return { block: `GATE failed (exit ${ran.exitCode}) in ${slot}: ${ran.stderr.trim()}` }
96 } catch (err) {
97 await log($, 'gate-error', { agentId: e.agent_id, error: String(err), secs: Math.round(((await $.clock.now()) - t0) / 1000) })
98 }
99 return next(e)
100 }).catch(($, e, next) => next(e))
101}
102