Architecture
Kraft is one Python process on your machine, and a second one beside it. The first serves a React SPA and an HTTP API and walks each work item's chain; the second, the local worker, starts the coding agent or the command for each local task.
This page is for contributors and for anyone who wants to know what runs
where. It shows how the parts fit and where each one is enforced in the code.
Everything Kraft knows is in one SQLite database, run/orchestrator.db, beside
a search index it can rebuild.
For what a work item does, step by step and status by status, read
How a work item runs first. For the words,
see Vocabulary. For which module under src/kraft/
holds what, see the Source map.
The parts
- BrowserReact SPA
- HTTP API and WebSocket
- kraft CLI
- HTTP API and WebSocket
- Coding agentkraft admin mcp
- HTTP API and WebSocket
- HTTP API and WebSocket
- orchestrator.db
- index.db
- Executor
- Executor
- orchestrator.db
- asks to start: Local worker
- forge tasks: gh or glab
- bd, optional
- Pollerswaits, rate limits, time caps, schedules, auto-intake, delayed escalations, closed MRs, archive
- Executor
- orchestrator.dbSQLite, one writer
- index.dbsearch
- Egress proxy
- Local workerunix socket in run/local-worker
- starts and reaps: Worker session
- Worker sessionan agent CLI or a subprocess
- edits and commits: git worktree
- permission hook, progress, replies: HTTP API and WebSocket
- network, when sandboxed: sometimes Egress proxy
- git worktreeone per work item
- gh or glab
- bd, optional
- The server.
kraft(orkraft admin start) runs uvicorn with the FastAPI app fromsrc/kraft/api/. The same process serves the built SPA, answers the API under/api, pushes events over a WebSocket, and runs every work item's walk as an asyncio task. There is no queue or broker. - The local worker. The server also starts
kraft admin worker local, a separate process that listens on a unix socket underrun/local-worker/(src/kraft/remote/local_serve.py). The server builds each local session's command and environment and asks the worker to start it. The worker starts it, reaps it and records it on disk; the server keeps watching it by pid and by the files it writes. - Surviving the worker. A session is the worker's child in a process group of its own, so it outlives a crash of the worker. The next worker takes it over by its recorded pid and start time.
- Surviving the server. The worker is a process of its own, outside the server's process group and, under a systemd service, outside its cgroup. A stop or restart of the server leaves it, and the sessions, running.
- Clients. The SPA, the
kraftCLI and the MCP server all call the same HTTP API. The MCP server iskraft admin mcp: a separate stdio process that your coding agent starts, with a tool for mostitemandviewverbs and forrepo connect(every tool butpermission_requestis also a CLI subcommand). Both go throughsrc/kraft/client/, so a verb and its tool can't drift apart. - The executor.
src/kraft/executor/walks a work item's frozen chain node by node. It runs builtin and forge tasks inside the process and launches agent and subprocess tasks through the local worker. It is started by an API request (resume, retry, approve, reject, skip) or by a poller. - Worker sessions. Each agent task is a headless agent CLI (Claude Code, Codex, Gemini, Antigravity, Cursor, OpenCode or Amp) started in the item's git worktree, with an environment built from an allowlist. With a sandbox configured, it runs in a container (Docker or Podman) instead, and reaches the network only through Kraft's egress proxy. A worker talks back to Kraft for permission decisions, plan progress and review replies.
- Machines. A step can also run on another machine; see
Where a step runs.
src/kraft/remote/handles these remote workers (src/kraft/worker/is the sandbox backends). The server keeps one connection per machine inremote/hub.py, over a dial-in WebSocket or ansshit runs itself, and speaks the versioned protocol inremote/protocol.py.remote/registry.pyreads workers.yaml. - Add-ons.
src/kraft/addons/installs the chains and components an instance takes from a collection. It fetches a collection into a bare mirror, extracts each add-on into a content-addressed store underrun/addons/, and gives the template library a read-only layer per add-on. See Add-ons and collections. - Pollers. Background tasks in the same process, each on a fixed interval.
Together they:
- re-enter items that are
waitingon an external wait that is now due, orrate_limitedand past their limit; - start
queueditems as slots free, and releaseblockeditems whose dependencies completed; - stop parked items whose time cap ran out, and gates whose own timeout ran out;
- fire schedules, and pick up ready issues from beads when auto-intake is on;
- fire delayed auto-escalations;
- archive old ended items, and check worktree disk use against its limit;
- notice a merge request closed on the forge without merging, and check that each forge token still works;
- flag saved settings that need a reload or restart, and update add-ons and Kraft itself when allowed.
- re-enter items that are
- Stores.
run/orchestrator.dbholds work items, sessions, gates, review threads and an append-onlyeventstable. All writes go through one writer queue (src/kraft/db.py). The WebSocket, notifications and the search index each follow theeventstail.run/index.dbis the full-text and vector search index over your repos' documents. - Outside tools. Kraft calls
gitfor worktrees and rebases,ghorglabfor merge requests and CI, andbdfor beads, each as a subprocess with your own login.
From filing to merge
- 1You to HTTP APIkraft item create
- 2HTTP API to orchestrator.dbintake, status paused
- 3You to HTTP APIkraft item resume
- 4HTTP API to orchestrator.dbclaim, paused to active
- 5HTTP API to Executorspawn the walk
- 6Executor to Executorcut the worktree
- 7Executor to Worker sessionlaunch the spec author
- 8Worker session replies to Executorexit code and result file
- 9Executor to orchestrator.dbgate_requested, status needs_human
- 10orchestrator.db replies to Youevent over the WebSocket
- 11You to HTTP APIkraft item approve
- 12HTTP API to orchestrator.dbgate_approved, status active
- 13HTTP API to Executorspawn the walk at the next node
- 14Executor to Worker sessionplan, implementation, their gates
- 15Executor to gh or glabopen a draft merge request
- 16Executor to orchestrator.dbstatus waiting on CIExecutor, orchestrator.db: re-entered when CI settles
- 17You to HTTP APIapprove final_review
- 18Executor to gh or glabmark ready, merge
- 19Executor to orchestrator.dbstatus completed, close beads
The table follows one work item. Its second column gives the numbered steps of the diagram above.
| Step | What happens | Diagram | Where in src/kraft/ |
|---|---|---|---|
| 1. File | kraft item create posts the item. Intake resolves its chain, freezes the chain and the effective policy onto a new row, and copies attachments. The item lands paused. | 1 to 2 | api/routes/work_items.py, executor/entry.py, store/work_items.py; chains in config/chains/; copies in run/items/<id>/filed/ |
| 2. Resume | kraft item resume claims the item with a conditional update from paused to active, checks the slot limit in the same statement, and starts its walk as a task. | 3 to 5 | api/routes/lifecycle.py, executor.run |
| 3. Walk | The walk loads the chain, cuts the worktree, and runs each node's steps. An agent task builds its command line from the harness profile, then launches it, watches its log, and reads its result file and usage. | 6 to 8 | executor/walk.py, builtins.py, executor/dispatch.py, adapters/agent.py, harness.py, harnesses/*.yaml, adapters/subprocess.py |
| 4. Gate | At a gate node the item becomes needs_human and Kraft writes gate_requested. The walk task ends. The event reaches the board over the WebSocket and, if you configured one, a notification. | 9 to 10 | executor/gates.py, notify.py |
| 5. Approve | kraft item approve checks for open must-fix threads, sets the item active again, and starts a new walk at the node after the gate. | 11 to 14 | api/routes/gates.py |
| 6. Merge request | Forge nodes open the draft merge request and read CI. A pending pipeline is an external wait: the item goes waiting, the walk task ends, and the wait scheduler re-enters the walk when the next check is due. | 15 to 16 | adapters/forge/, waits.py |
| 7. Finish | After final_review, forge nodes mark the merge request ready, wait for any required approval, merge and watch the post-merge pipeline. The walk marks the item completed and closes its issues in beads. | 17 to 19 | adapters/forge/, executor/entry.py |
What a restart does to running items
A clean stop (kraft admin stop, restart, Ctrl-C, a service stop) leaves the
local steps of worktree items running in the local worker, as a crash does. The
server's tasks are cancelled; a step that is still running, not capped and not
paused is left alone, its sandbox and egress route included.
A step of an item that runs in place is ended, since it runs in your own
checkout, and so is any step by kraft admin stop --now (it writes
run/stop-now for the server to read). See
A restart leaves local steps running
for the commands.
A restart doesn't lose finished work. On startup, worker/reattach.py finds
the sessions that were running, asks the local worker which it holds, adopts
those and resumes each item's walk from the node and step it had reached. A
session outlives a crash or a kill -9 of the server, because each agent runs
in its own process group, a child of the worker.
An update within a major version starts a worker of its own: each installed
Kraft has its worker, its socket and its lock (local-worker/worker-<version>-<hash>.sock).
The new worker adopts the old one's sessions by pid identity; the old one gets
no new sessions, reaps its own children and exits.
The handshake with a worker or machine carries a caps list, and frame readers
ignore fields they don't know, so a machine on an older release of the same
major keeps getting steps.
Trust boundaries
Four lines matter. The Security page states what each one does and doesn't stop by default; this is only where each one lives in the code.
| Boundary | Enforced by |
|---|---|
| Network clients to the API | api/perimeter.py (the local-client check and auth middleware), auth.py (password login and sessions), the bearer token in run/ |
| A worker to Kraft | The permission gate (api/routes/sessions.py, permission_hooks.py, permission_rules.py, grants.py), and the self-action guard: client/context.py in the CLI and MCP server, and forbid_self_action in api/deps.py for a worker that identifies itself |
| A worker to your machine | Nothing, unless the repo sets sandbox:. Then worker/backends/docker.py, worker/sandbox.py, worker/refstore.py, assert_on_branch in adapters/forge/git.py before every host-side commit, rebase or push from its worktree, and for network: the egress proxy in worker/egress.py, worker/channel.py and worker/inject.py |
| Kraft to your forge | Your own gh or glab login, used as is by adapters/forge/ |
A sandbox covers a workspace's members as it covers the root. Where each hold lives in the code is under Workspace members on the Source map.
State on disk
State lives in $KRAFT_HOME (default ~/.kraft), in two folders:
run/holds the databases, the worktrees,items/<id>/, the process ID file and the sandbox's sockets, CA and ref stores.config/holds the YAML the Templates and Settings screens edit. Kraft seeds it from the shipped defaults on first run and never overwrites it, so an upgrade can't clobber an edited policy.
items/<id>/ is one folder per work item. It holds the item's documents and
their earlier versions, its attachment copies, its session logs and result
files, and its sandbox home.
kraft.item_paths is the one place that says where an item's file is. Nothing
is moved at an upgrade: an older item's files stay in logs/, results/,
attachments/, sandbox-home/ and its worktree's .engineering/, and are
read there.
Data and privacy lists every
file in run/ with its mode. Configuration lists
what is in config/.
Related
- Source map: which module under
src/kraft/holds what, and where to change a given thing. - Security: what each trust boundary does and doesn't stop by default.
- How a work item runs: the flow above in terms of statuses and gates.