- Ruby 95.6%
- Just 4.4%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| lib | ||
| libexec | ||
| plugin | ||
| sysext | ||
| system | ||
| test | ||
| .gitignore | ||
| .rubocop.yml | ||
| config.example.toml | ||
| Gemfile | ||
| Gemfile.lock | ||
| justfile | ||
| Rakefile | ||
| README.md | ||
claude-forge
A bot for a Forgejo forge, with two ways to ask it for something:
- Put the
agentlabel on an issue, and it tries to solve the issue and opens a pull request. - Mention it in a comment on any issue or pull request (
@Claude fix the nil case,@Claude why is this slow?), and it does what the comment asks and replies.
How it works
Two parts with very different trust, running as two users:
- The dispatcher (Ruby,
lib/) holds the forge credential. It polls for labelled issues and for mentions, checks who is asking, pushes, opens pull requests and comments. It is a system service with a user of its own that exists only while it runs. - The agent is Claude Code running headless as another user, in that user's home, in a service that is its sandbox. It has a clone of one repository. It has no forge credential and cannot push.
A job goes like this:
- The dispatcher puts a 👀 on the issue or comment, and leaves the repository in the job's work directory.
- It starts the agent, which makes its clone, works, and commits.
- The agent asks for a pull request (
submit_pr). The dispatcher fetches the commits from the clone, checks them, pushes them to the bot's fork, opens the pull request, and answers with its number. - The agent replies (
reply), and the dispatcher posts that where the work was asked for. When the agent is gone, so is the 👀.
The agent reaches the forge only through an MCP server the dispatcher serves
on loopback for that one job, limited to that one repository: read issues and
pull requests, submit_pr, reply. The dispatcher starts the agent as a
systemd unit, claude-forge-agent@JOB.service, over D-Bus; a polkit rule
allows it that and nothing else. Nothing the agent wrote is executed by the
dispatcher: fetching from the clone is the one thing it does with it. Nothing
is pushed to the upstream repository.
What the agent should do is in two deliberately short skills,
plugin/skills/solve-issue and plugin/skills/respond. Make them stricter
there if a repository deserves more care.
Where its commits go
That depends on where it was asked:
| asked on | it works on | commits become |
|---|---|---|
| one of its own pulls | that pull request's branch | more commits on that pull |
| an issue | agent/issue-N, from the default branch |
a new pull request, if it asks |
| someone else's pull | agent/pr-N, from that pull's head |
a new pull request, if it asks |
A branch that already has a pull request from an earlier job is continued, so asking again on the same issue adds to that pull request. Only new commits are accepted; rewritten history is refused.
Who can start a job
Only the users in allowed_users. The bot looks at who applied the label or
wrote the comment, not at whose repository or issue it is, so a stranger
labelling their own issue or mentioning the bot gets nothing, not even a
reply. The agent is likewise told which authors are trusted: text from anyone
is information, but only trusted users direct the work.
Only ordinary comments count as mentions, not the description of an issue or pull request, and not reviews.
While the bot works on a request, there is a 👀 reaction from it on the comment that mentioned it, or on the labelled issue. It takes that off when it is done, and its one comment comes at the end. If the agent leaves without replying, the bot says so itself.
A request is acted on once. For a label, the bot removes it when it starts if it has write access to the repository; if it doesn't, the label stays and has to be removed and applied again to ask again. A mention counts as asked until the bot has commented after it.
Requirements
Linux with systemd (257 or newer), polkit, git and Ruby with Bundler. The
claude CLI has to be installed for the agent's user. Building and installing use just, fish,
run0, systemd-repart and mksquashfs.
On the forge: an account for the bot. It needs read access to a repository to work on it (so private repositories only if it is a collaborator), and write access only for removing the label.
Setup
The bot is installed as a system extension image, merged into /usr. That
makes /usr read-only for as long as it is merged, so on a distribution that
updates packages in place, run systemd-sysext unmerge before an update and
systemd-sysext merge after.
The agent needs a user. Its unit names claude, which a drop-in for
claude-forge-agent@.service can change, along with the home's path there:
homectl create claude --storage=subvolume
A home kept by systemd-homed is closed until someone presents its password.
The dispatcher does that when it starts, if it is given the password, and
keeps the home open for as long as it runs. Use subvolume or directory
storage, not homed's default of an encrypted image: for an image, homed
allocates the whole image on the host's disk at every unlock. An ordinary
user works as well, and needs no password.
Then, from a checkout of this repository:
cp config.example.toml config.toml # and edit it
just install # builds the image, merges it, enables the service
just credential claude-forgejo # the first time: the bot's secrets, one by one
just credential claude-token
just credential claude-home
The secrets become system credentials in /etc/credstore.encrypted/:
claude-forgejo is the access token or password of the bot's forge account,
claude-token a Claude API key or a token from claude setup-token, and
claude-home the password of the agent's home. Leave the last one out for an
agent without a homed home.
The dispatcher has the network as it is. The agent is allowed loopback and the Claude API, at the addresses Anthropic publishes. What else it should reach, such as a package registry, is yours to add:
run0 systemctl edit claude-forge-agent@.service
[Service]
IPAddressAllow=192.0.2.0/24
just start
just doctor
just install again updates the bot; just start restarts it. There are also
just status, just logs and just stop. When it starts, the bot checks its
installation (the forge login, the tokens, the agent's home, Claude Code run
as the agent) and stops if something is missing; just doctor shows the log
of the last start. just home-password puts the home's password in the
clipboard, if you keep it as a credential of your own.
What is installed
sysext/repart.d/claude-forge.conf lists everything that goes into the
image:
| path | what |
|---|---|
/usr/lib/claude-forge |
the code, with its gems |
/usr/lib/systemd/system/claude-forge.service |
the dispatcher |
/usr/lib/systemd/system/claude-forge-agent@.service |
one run of the agent |
…/claude-forge-agent@.service.d/network.conf |
the Claude API's addresses |
/usr/share/polkit-1/rules.d/50-claude-forge.rules |
lets the dispatcher start agents |
Some gems are compiled, so an image goes with the Ruby of the machine that
built it. Outside the image there are only /etc/claude-forge/config.toml,
the credentials, and your drop-ins.
The services
The program does not run outside its unit: the unit is where its credentials and directories come from, and what protects it.
claude-forge.service, the dispatcher.DynamicUser=, the whole system read-only apart from its state, no home directories at all, no capabilities, no namespaces, and a system call filter. The tokens come in throughLoadCredentialEncrypted=.claude-forge-agent@JOB.service, the agent, and its sandbox; see below. What to run comes from the dispatcher as a credential, because it holds tokens.
The two are different users, so the agent cannot touch what the dispatcher has.
Logs
Both services log to the journal, each what it does itself.
The dispatcher logs what it did for a job, with structured fields, so one job or one repository can be picked out:
just logs # both, as they happen
journalctl -t claude-forge REPO=someone/widget
journalctl -t claude-forge JOB=someone-widget-12-20260101T120000.000
The agent's unit logs what the agent did, as it happens: what it says, each tool it calls and what comes back, cut to 2000 characters each, and at the end its number of turns and cost. All of that is at debug level and carries the same job fields, plus the tool's name on a tool call. What Claude Code or its tools print on stderr is logged as it is.
journalctl JOB=someone-widget-12-20260101T120000.000 # everything about one job, from both
journalctl -t claude-forge-agent TOOL=Bash # every shell command, any job
journalctl -u claude-forge-agent@someone-widget-12-20260101T120000.000
To look up what a job published:
journalctl -t claude-forge JOB=<job> -p info # its steps, each with a link: what asked for it, commits pushed, the pull request, the reply
journalctl -t claude-forge JOB=<job> -g pushing # the commits it pushed, by their first ten characters
journalctl JOB=<job> TOOL=mcp__forge__reply # the reply in full, as the agent sent it
journalctl -t claude-forge -g pushing -o verbose # every push, with the job each belongs to
A job's id starts with the repository and the issue's number. The ones that
start with check- are the check at startup, which publishes nothing. Going
the other way, from a link on the forge to its job, is journalctl URL=<link> -o verbose.
The events in full are in Claude Code's own records, in ~/.claude of the
agent's user. Why a labelled issue was passed over is logged by the
dispatcher at debug level.
The agent's home
The agent works as its user in that user's real home, and can write to it. So there is one place for tools:
- Log in as the agent's user and install what the agent should have (an opam
switch, a Rust toolchain, something in
~/.local/bin), and it is there for every job. So is anything installed system-wide. - Ask the agent to install something (
@Claude install dune), and it stays as well, provided the agent's network allowlist lets it download it.
Claude Code keeps its own state in ~/.claude there.
The repository is not in the home. Every job gets a fresh clone in its own work directory, which is where the agent starts and which is removed when the job is done. Nothing of an earlier job is in it: no build output, no leftover branches, and, the path being new each time, nothing Claude Code noted down about "this project" before.
Where the bot keeps things
In /var/lib/claude-forge, which is the dispatcher's alone:
mirrors/– a bare mirror per repository; the only place pushes come from.jobs/– per job,mirror, a link to the mirror it used, and while the job runswork/, with the repository as a bundle and the agent's clone of it.work/is the one directory the agent's unit is given, and is removed when the job is done. What happened in a job is in the journal, not here.handled.json– label events already acted on.
What the sandbox does and does not do
The agent's sandbox is its unit, system/claude-forge-agent@.service:
- It runs as the agent's user, without capabilities or a way to gain any.
- The system is read-only. Of the homes there is only its own, and of that
everything but
~/.sshand~/.gnupg, which are empty in there. - It has a
/tmpand devices of its own, and cannot reach the system bus. - It can still create namespaces and mount a fresh
/proc, so that tools with a sandbox of their own, like a build system using bubblewrap, work in it. - On the network it reaches loopback, the Claude API and what you add. Loopback includes services listening on the host.
- A job is limited in memory, tasks and time.
What it does not do:
- The home is writable, and things in it are run or trusted when someone logs in as the agent's user: shell startup files, user units. The agent can write those.
- The Claude token is in the agent's environment. An agent that is talked into misbehaving by something it reads could leak that token, or the contents of the repository it is working on, to wherever its network allowlist reaches.
- The agent sees the host's processes, and can signal those of its own user, should there be any.
It cannot reach the forge credential or other repositories.
Pull requests come from a fork so that CI treats them like any outside contribution. Review them like one.
Tests
bundle exec rake
runs whole jobs against a fake forge and a fake agent, with the tests running
the agent's command where its unit would. just test-home checks, as a
transient unit restricted like the dispatcher's, that the agent's home can be
opened through systemd-homed with its password alone.