How To: Run in a Loop
Task: give a failing story to a worker until the gate passes or the budget ends.
Done when: Plan.runLoop returns status: "pass".
Steps
Optional: run the root
plan.tsonce to see a single pass. The script starts the hello-world server for you:bun run prove:onceRun the loop with the built-in worker:
bun run example:hello-world:workerThis example needs no API key. It starts a local fake model server that returns a fixed fix, so you can see the loop work offline. In your own plan, pass a real model
endpointtocreateOpenCodeWorker. With noendpoint, the worker makes no change.Read the result. Each iteration reports the story, the worker summary, and the new status.
How the loop works
import { Effect } from "effect";
import { Plan, createOpenCodeWorker } from "gateproof";
await Effect.runPromise(
Plan.runLoop(plan, {
maxIterations: 3,
worker: createOpenCodeWorker({ endpoint: process.env.MODEL_ENDPOINT }),
}),
);On each iteration, Gateproof:
- Runs the stories in order. With
rerunFailedStoryPrefix: true, later iterations run only the stories up to and including the first story that failed. - Stops when the plan status is
passorskip, when there is no worker, or whenmaxIterationsis reached. With no worker andloop.stopOnFailure: true, afailalso stops it. - Sends the first failing story to the worker.
- Checks the worker's changes against the story's
scope. - Rejects a change outside scope. The loop records a scope violation, not a pass.
- Runs the plan again.
Scope
A story can limit what the worker may change:
scope: {
allowedPaths: ["examples/hello-world/"],
forbiddenPaths: ["plan.ts"],
maxChangedFiles: 2,
maxChangedLines: 50,
}With no scope, these defaults apply:
- Allowed:
src/,app/,components/,pages/,lib/ - Forbidden:
node_modules/,.git/,dist/,build/,.env,plan.ts,README.md,demo/src/routes/case-studies/
plan.ts is forbidden by default. The worker cannot weaken the proof to pass it.
Story order with dependsOn
A story can wait for earlier stories:
{ id: "user-login", title: "User can log in", dependsOn: ["user-signup"], gate }If user-signup ran and its status is anything other than pass, user-login gets fail with the summary upstream story 'user-signup' failed. Its gate does not run. Plan.define throws on cycles, forward references, unknown ids, duplicate ids, and self-references.
Worker choices
| Worker | Use |
|---|---|
createOpenCodeWorker |
Built-in path. Stable. |
createFilepathWorker |
Alpha. One bounded turn on filepath. |
| Your own function | Any (context) => Effect<WorkerResult>. |
In every case, Gateproof keeps the proof. The worker only proposes changes.