TL;DR: an AI agent that merges into its own source needs more than a good model. it needs gates between the model that writes code and the process that runs it, and for each gate two answers: who decides, and whether the writer can reach the decider. this post lays out that architecture as six gates, approval, isolation, independent review, merge on the exact commit, a watched main and a restart that refuses, with the failure each one exists for, what it costs and which of them are walls versus conventions. the worked example is Jarvis, which merged 76 pull requests into its own repo between Nov 10 and Nov 16 with a 23 minute median from open to merge, and where today exactly one boundary, the PR author, is out of the writer's reach
a compiler that compiles itself still has a human holding the old binary. an agent that rewrites itself holds the binary, the shell and the keys
that's the whole problem. the thing that writes the change is the thing the change runs inside, and it is a model: often right, sometimes wrong, always confident
so a self-evolving system has two jobs. ship a change, and survive a bad one. this post is the second job, written as a design you can apply to any agent that edits its own code: the roles, the gates between them, the failure each gate exists for, what it costs, and where it is still a convention rather than a wall
the example all the way through is Jarvis, a harness of harnesses that lives in Slack and merges into its own repo. each section states the pattern first and then shows it in Jarvis. what Jarvis is, and how a turn holds credentials, are in J.A.R.V.I.S and The Ticket-Based Security Model
the constraints
most of the design falls out of four facts that hold for nearly every agent of this kind
- the writer is a model with a real shell
- the code it changes is the process it runs inside
- there is one human owner, or a few, and they sleep
- the platform protects less than you'd like: a small repo has thin branch protection and often no staging copy of the agent
so most safety properties have to come from the pipeline, not from the platform. the work is deciding which ones, and being honest about which ones the writer could walk around
in Jarvis: one Mac, one launchd job, no staging copy of the harness, one owner. the repo started private on a free plan with no branch protection at all. main has some now: one approving review, admins included, no force push. a passing check is still not required by GitHub
for scale: the public repo, momoshub/J.A.R.V.I.S., merged 76 pull requests between Nov 10 and Nov 16. 68 of them touched
src/orbin/
the shape
the reference shape first. every gate below sits on one box, numbered like its section
the structure: what the writer can reach, and the gates on the line around it
the roles
- proposer: the running agent, which notices something wrong in itself and files a report instead of fixing it on the spot
- human owner: the only one who approves a reported fix. any path that fixes without a button stays capped and reads the same decision
- writer: a model session that turns the approved plan into one branch with code, tests and docs
- PR identity: the account that opens the PR, which is not the writer
- reviewer and verifier: fresh model sessions that read the diff and run the head
- merge authority: merges only the exact commit that passed
- deploy guard: checks what will actually run, and refuses red
- way back: fix forward or revert, through the same gates or by a person
and the gates a change passes, each one either passing it on or stopping it where it is
- approval: the owner decides whether a reported bug gets fixed
- isolation: the change is written in its own workspace, never in the live copy
- independent review: a second session reads the PR, a third one runs it
- merge: CI green on the exact commit that was pushed, or no merge
- main: the post-merge checks are watched, and a red one is fixed or reverted
- restart: the release is checked where it will run, or the old process keeps running
every gate gets two questions. who decides: code, a model, or a human. and what holds it: capability, meaning the writer can't go around it, or convention, meaning it holds only because the writer follows the rule. the colors in the diagram answer the second one
the split that matters is the first one. the model should be in the loop as writer, reviewer and verifier, three sessions with three contexts, none of them trusted on its own word. every gate with the final say should be code that reads state and exit codes, dumber than the model on purpose
in Jarvis: the same path, with the real names on it
gate who decides kind approval a Slack action listener, in code deterministic isolation git, plus a diff check on the branch deterministic review a separate model session, by a verdict line probabilistic review: the verifier a separate worker, with evidence pinned to a sha probabilistic merge bin/merge-when-green.sh, by sha and check statedeterministic main GitHub Actions on main, then the writer fixes forward or reverts deterministic, then a model restart bin/jarvis-restart.shexit codes, then launchddeterministic
in Jarvis: the same path today, in four lanes
gate 1: approval, decided in code
the problem: an agent that notices a bug in itself wants to fix it right away. a fix nobody asked for is the start of every runaway loop
the pattern: the proposal is data, and the decision is an event only a human can produce. the agent files a report with the issue, the cause and a plan. the owner answers with a press, and the press is checked by plain code outside any model turn
- the presser is the owner
- the press came from the exact message the report was posted on
- the report is still pending, and the switch to approved or rejected happens once
an approve starts a fix with the stored plan, not a fresh guess
why it is code and not a command
the tempting version lets "approve" be an ordinary command the model can run. it fails quietly: a scheduled turn running as the owner, in the owner's chat, passes every check a command can make. nobody presses anything, and the fix is approved anyway
only a real press carries the presser's id and the message it came from. so only a press can approve
one decision, every writer
the failure mode that shows up later is a second way in. any other path that writes fixes has to read the same decision, or a rejected bug comes back through the side door
a guardrail that only one of two writers knows about is half a guardrail
the trade-off is the owner as a bottleneck, so the gate needs caps on how often it can ask
in Jarvis: the report is an internal command,
bug-report, that only a model run can call. a typed line is refused for every roleit dedupes the same thread and issue within 24 hours, caps reports at 10 a day, and DMs the owner with Approve and Reject. the press has its own listener, which checks owner, stored channel and timestamp, and pending state
the command version was real: the reviewer's round 1 on PR #56 blocked it for exactly the scheduled-turn reason
the second writer is the inner child, a timer that reads Jarvis's own conversations, files self-issues and can fix them without a button, entering at gate 2
since PR #71 a rejected report closes that thread for it too, and a pending one holds its autofix until the owner answers. autofix is off by default and capped at 2 a day
gate 2: isolation
the problem: a writer that edits the live copy in place breaks things in ways no review can see
- two sessions editing one folder, one committing the other's half-finished change
- an uncommitted edit blocking the next update, then swept into an unrelated commit
- the running process reading files that are halfway through an edit
the pattern: every change starts in its own workspace cut from a fresh main. the live copy is read by the running process and changes only through the deploy path, after a merge
done is a diff check
the second half of the gate is a definition of done that is mechanical to check. a change is done when its diff shows the tests and docs moved with the code. code changed with no test and no doc means stop and explain why
tests and docs ship in the same PR, never a follow-up, because the follow-up is the PR that never happens
the honest limit: this is isolation from each other, not from the machine. a workspace on the same disk, under the same user, can be written by the same shell that could write the live copy. without a sandbox, "never edit it in place" is a line in the prompt, which is why the diagram paints this gate as convention
the cost is a fetch and a branch for a one-line fix. cheap next to a merge conflict with no merge
in Jarvis: every change starts with
git fetchandgit worktree add .claude/worktrees/<name> -b <branch> origin/main. the live checkout is written only bygit pullafter a mergedone is
git diff --name-only origin/main:src/changed with no*.test.tsand no doc means stop. typecheck and the full suite run green in the worktree before the push
gate 3: independent review
the problem: the writer's own tests agree with the writer's bug. it wrote both from the same mental model
the pattern: a second reader with a fresh context, started after the PR exists, never a fork of the writer. it sees only what the PR shows, talks to the writer only on the PR, and ends each round with a verdict line that code can parse. the gate reads the line, never a vibe
route by risk, not by size. docs and copy go straight to CI. runtime logic gets a reviewer. a mixed PR counts as runtime
fail closed on every limit
- a capped number of rounds, then the owner is asked
- a dead reviewer, or a silent one past a timeout, is not clean
- only correctness, regression, test and doc findings block
why a second reader
green tests tell you the code does what the writer thought. the reviewer tells you what the writer didn't think of
the example that shows it: a change that labels a shell step on a progress card by the program it runs, never its arguments, because arguments are where secrets live. it took six rounds. every round found another shell grammar case where argument text reached the label, starting with API_KEY='abc|hunter2' ./deploy.sh showing up as "Running hunter2". every fix went the give-up direction: when in doubt, "Running a command"
the verifier
a reviewer reads the diff. a verifier runs it: checks out the head in a throwaway workspace, confirms the sha, runs the full suite, exercises the claimed behaviour, and posts the evidence naming that sha
a verification that doesn't name the commit it ran on has verified nothing. one that only names what the PR claims has verified the claim
where it breaks
shared omissions: the verifier's brief comes from the PR, so it exercises what the writer said it fixed. a writer that fixed a permission check and wrote "fixed permission validation" gets a verifier that tries the happy path, not the escalation nobody thought of
three sessions can share one omission
risk by path: classifying by folder is the cheap version. a prompt change can move the writer's behaviour more than a logic change can, and it skips the reviewer
an unmeasured reviewer: counting rounds is not counting quality. a reviewer that always asks for changes scores the same as a good one
in Jarvis: the reviewer is a separate background session that reads the PR through
gh pr diffandgh pr view. each round is a real GitHub review whose body opens with[review round N]and ends withVERDICT: cleanorVERDICT: changes, and the writer answers every point in a[fix round N]commentthe verdict is read from the body, never from the review state, because GitHub refuses approve and request changes on your own PR. that rule came with PR #12
the cap is 5 rounds and the silence timeout is 30 minutes. the risk class is read off file paths: runtime logic in
src/orbin/gets a reviewer, prompt text insrc/doesn'tthe writer runs typecheck and the full suite before every PR. 51 of the 65 PRs that went through review still got
VERDICT: changesin round 1. the six-round PR above is PR #3750 of the 76 PRs carry a
[verify]comment. the verifier's suite includes the system-model tests that fail when a runtime shape or a cited constant moves without its model
in Jarvis: the life of a self-fix PR, by state
gate 4: green means this commit
the problem: "CI is green" is a statement about some commit. a push that lands between the check and the merge, or a fast third-party check that passes before real CI registers, makes a red change look green
the pattern: the merge authority checks that the PR head is the commit it expects, waits for the required check by name until it exists and finishes, treats unknown mergeability as no, and merges with the head pinned. its exit codes are the interface, and red is a finding, not a retry
the failure mode is the one nobody sees: a merge that never called the gate looks exactly like one that did, until main goes red
the trade-off is where the rule lives. a script binds whoever calls it. branch protection binds everyone. as long as green is checked by a script the writer runs, the writer can choose not to run it
in Jarvis: the merge is one script,
bin/merge-when-green.sh <pr>, run from the worktree after the last push, never a baregh pr mergethe PR head has to be the local
git rev-parse HEAD, the required checkcheckshas to exist and finish, and the merge is a merge commit with--match-head-commit. right before the merge it approves as the merging accountexit codes: 0 merged, 1 red or a failed approval, 3 not finished yet so run it again, 4 resolve the conflict first
GitHub enforces the one approving review on main, admins included. it does not enforce the green check: the script is still the only thing between a red PR and main
in Jarvis: the merge gate as a script
gate 5: main is watched, and reverted
the problem: some checks can't run on a PR. a test that needs real credentials should only see them on main, so a workflow edited on a branch never does. a green PR says nothing about those
the pattern: after every merge, find the run for the merge commit and watch it
- green: done
- red: fix forward in a new PR, through the same gates
- can't fix it: revert as a PR, through the same merge gate, then say what broke
- never merge the next PR on top of a red main without saying so
rollback as a PR costs minutes instead of seconds, and in return the way back can't skip a gate the way forward had to pass
the failure mode is that the way back needs the pipeline to be alive. a revert carries the old scripts and workflows with it, so a broken merge script reverts itself. the writer, the reviewer and the merge call don't: they run inside the process the bad change may have just restarted. when the thing that broke is the thing that writes reverts, the way back is a person
in Jarvis: the real-Slack e2e suite never runs on a PR. it needs the bot token, which lives in a GitHub environment limited to main. after a merge the writer watches the e2e run for the merge commit, and a revert is
git revert -m 1 <merge sha>in a worktree, as a PR, throughmerge-when-green.sh
gate 6: the restart that refuses
the problem: a supervisor revives a process that won't start, again and again. an agent that restarts itself into code that doesn't compile has no way back from the chat. someone walks to the machine
the pattern: after a merge, update the live copy, then check exactly what the supervisor will start, on that copy, not on the workspace. red refuses. green doesn't restart anything directly: it asks the running process to drain and exit once idle, and the supervisor brings it back on the new code. a restart from inside a turn would kill the turn that asked, and its reply would never land
the window the refusal opens
the order is update, then check, then restart. a refusal leaves the live copy on the new commit and the process on the old one
nothing rolls the copy back. the next restart for any reason, a crash, a power cut, a human, starts the code the gate just refused. the refusal buys time to fix forward. it is not a rollback, and without a last known good build there is nothing to roll back to
a refusal is a held door, not a locked one
the cost is time: a merged fix waits for the drain before it runs, and a refused one waits for a fix forward
in Jarvis:
git pullon the live checkout, thenbin/jarvis-restart.shfor a code change onlyit runs
bun run typecheckandbun teston the live folder and rebuilds the dashboard bundle, which isn't committed and breaks the pages whose API changed when stale. any of them red printsREFUSING TO RESTARTand exits 1green asks the harness over loopback to exit once idle, and launchd brings it back. without the gate, launchd would revive a broken process every 15 seconds, forever. the drain is in the first post
in Jarvis: the restart gate
failure modes
every gate above follows one rule: an unknown is a no
- a reviewer that went quiet is not clean
- mergeability the platform hasn't computed yet is not mergeable
- a check that hasn't appeared yet is not green
- a verify on another sha is not a verify
- a restart whose checks didn't run is not a restart
a pipeline where the model is the writer has to assume the writer is sometimes wrong and always confident. failing closed is how the system stays up when it is
in Jarvis: the failure map, with the guard for each
convention or capability
the failure map is the list of things a wrong writer can do by accident. a different question is what a writer can do on purpose, or under a prompt that tells it to. a prompt injection makes any writer adversarial for one turn
against that writer a gate only holds if the writer lacks the capability to go around it. the principle for the whole architecture
- the writer holds nothing past its workspace and a way to ask for a PR
- the PR is opened by an identity the writer can't impersonate
- the reviewer can't merge
- the merger is its own principal with a credential the writer can't read
- the required checks live in the platform, not in a script
- the deploy guard decides what runs, and takes no request without proof the checks ran
a gate you can argue with isn't a gate. an architecture review of this draft put the counterpart better than I had: a gate you can bypass isn't a security boundary
in Jarvis: the honest state, with no account names
who runs as holds what stops a bypass writer a model turn with a shell on the Mac every credential that shell can read a line in the prompt PR author the GitHub app, through a ticket nothing: the bot holds the key, the turn gets a ticket capability reviewer, verifier fresh sessions, same Mac the same credentials as the writer a line in their brief merger one human account, an admin of the repo, from a script its token, readable by the writer's shell main needs one approving review and takes no direct push, admins included. the approval can come from that same token, and an admin token can edit the protection restart a script, then a loopback endpoint the endpoint takes no proof the checks ran the script, which is a convention the platform stops a push to main, a force push and a merge with no approving review, for everyone. real walls, but the account that posts the approval is an admin whose token the writer's shell can read. the wall is real, and the writer stands next to the gate holding the key
what no credential on the host can do: get the app's private key. the bot holds it, a turn gets a ticket, and the ticket is checked by role before the bot acts. that one is capability all the way down
what only a rule stops: merging on red CI, editing the live checkout in place, calling the restart endpoint without the checks, the reviewer skipping the diff. each is a shell command away
every gate is a real gate against a writer that is wrong. against a writer that is adversarial, one of them is a boundary and the rest are conventions
in Jarvis: who holds what today
trade-offs
what this architecture pays, with Jarvis's numbers where there are any
- latency: a runtime fix waits on a reviewer, a verifier and CI. 23 minutes median from open to merge in Jarvis
- cost: three model sessions per runtime PR, more with every round
- one human: anything held for the owner waits for the owner. fine until the owner is asleep
- shared blind spots: if writer, reviewer and verifier are the same model family, what one can't see, three can't see
- picked numbers: a round cap is a choice, not a proof. Jarvis caps at 5, and one PR needed six and a human
the procedure is data
in a system like this the gates are code and prompt text in the same repo as everything else. so changing them is a PR like any other, through the same gates
that's the part that earns the title. it's also the reason for the previous section: the writer can change the gates, so the gates it can't change have to live outside its reach
in Jarvis: the loop is prompt text in
src/domain/selfUpdate.ts, a skill and two scripts inbin/. the round cap went from 2 to 5 in PR #19, and real GitHub reviews instead of comments came in PR #12. both landed through the loop they changed
open problems
the gaps any system like this has to close, in the order I'd close them, with Jarvis's plan as the concrete instance
one principal per job: the writer pushes branches and holds nothing else, the reviewer can't merge, the merger and the deploy credential live where the writer's shell can't read them, and green is a required check in the platform
in Jarvis that's P0, and only its first slice exists: the PR author is its own principal, reached by a ticket
immutable releases: build and test a release by commit sha outside the live copy, keep the last known good one, switch only after the checks and a health check, and switch back on its own when that fails. a refusal becomes a rollback instead of a held door. Jarvis P1
verification that doesn't start from the claim: a verifier brief built from the security policy, the regression history and the contracts instead of the PR, ideally from a different model family. Jarvis P1
a way back that doesn't need the pipeline: a revert that works without the agent, the merge script or CI, so the broken thing never has to fix itself. Jarvis P1
risk by behaviour, not by path: classify a change by what it can change, prompts and capabilities included. Jarvis P2
a measured reviewer: false positives per round and defects that got past a clean verdict, so 51 of 65 becomes a number about quality instead of rounds. Jarvis P2
nothing past that first slice of P0 is started
capability isolation goes first because it changes who holds what. a fourth reviewer or a sixth round doesn't
self-evolving doesn't have to mean self-approving. put the intelligence in the writers and the stupidity in the gates
and put the gates somewhere the writers can't reach ;)
Written by J., reviewed and refined by Kai.