Concepts
This page describes the model of kōjō as it is built: how a request becomes a session, what runs inside a session, and what limits apply. Read it before you write skills or change policies.
The pipeline
Every piece of work follows the same path:
- A request arrives on a surface: a forge webhook, a Slack message, the CLI, the dashboard, an MCP client or a schedule.
- Intake decides whether kōjō acts on it and whether it may change code.
- The request is stored and a session is queued in Postgres.
- A worker claims the session and runs the orchestrator, or one skill directly when the request names it.
- The orchestrator delegates to subagents. Each subagent is one skill, with that skill’s tools, running against a sandbox.
- The goal critic checks each subagent’s result against the skill’s acceptance criteria and sends it back with feedback when it falls short.
- If the workspace changed, the merge policy decides how the change is published. The default is a draft pull request.
- The result is delivered where the request came from: a comment, a thread reply, or the session record that the CLI, the dashboard and MCP read.
The parts that take part, and what each one talks to, for a session the orchestrator runs:
flowchart TD
S["Surfaces: forge webhook, Slack, CLI, dashboard, MCP, schedule"]
subgraph core["kōjō core"]
I["Intake"]
O["Orchestrator"] -- delegate --> A["Subagent: one skill"]
A -- "final report" --> C["Goal critic"]
C -- "rejected: feedback" --> A
C -- verdict --> O
O -- "work done" --> M["Merge policy"]
G["Model gateway"]
end
S --> I
I -- "request stored, session queued" --> Q[("Postgres")]
Q -- "a worker claims the session" --> O
O -. "model calls" .-> G
A -.-> G
C -.-> G
A -- "tools: exec, copy" --> X["Sandbox, through the driver"]
C -- checks --> X
X -- egress --> P["Proxy: allowlist, credentials"]
M -- "branch, commit, pull request" --> F["Forge API"]
Requests and intake
Every surface normalizes into one request: who asked (the actor), on which surface, about which repository and issue or pull request, and the text. A request also carries a trusted flag for its actor. Intake sets it; nothing later can raise it.
| Source | Becomes work when | Trusted (may change code) when |
|---|---|---|
| Forge mention of the bot account | The author has the repository’s trigger_permission (default write). Otherwise the request is rejected and audited. |
Always, once accepted |
| Forge event: issue opened, pull request opened or updated, check failed | An operator configured a skill for that event in the repository’s auto map. Otherwise it is ignored. |
The event’s author has trigger_permission |
| Forge push to the default branch | Never a session: it re-reads the repository’s .factory/ files |
Not applicable |
| Slack direct message or mention | Always | The sender’s email has the operator or admin role in kōjō |
kojo run, the dashboard, MCP |
The caller has the operator role (the API requires it) |
Always |
| Schedule | The schedule is enabled and due | Always |
A mention or a chat message that would become work does not start a second session when a session is already at work on the same issue, pull request or chat thread: it goes to that session as a message its next model step reads. See Talking to a session at work.
Further rules that intake applies:
- A request for a repository that is not enabled is rejected. That includes dry runs: a skill test cannot read a repository kōjō was not enabled on.
- A forge delivery is enqueued once. The delivery ID is the deduplication key.
- A skill draft can only run in a dry-run request.
- A chat message names its repository as
forge/owner/name. Without one, the request uses the only enabled repository, if there is exactly one.
A request from an untrusted actor is handled read-only: it only reaches skills that cannot change the workspace, it never publishes a change, and it cannot save team memory. See Security for the full list, and Surfaces for how each surface is connected.
Sessions
A session is one request being handled. It is also the queue entry: workers claim sessions from Postgres with FOR UPDATE SKIP LOCKED, so there is no separate queue.
States
| Status | Meaning |
|---|---|
queued |
Waiting for a worker, or waiting for its next look at something (a retry, the merge wait, a spent daily budget) |
running |
Claimed by a worker, which holds a lease on it |
waiting |
Parked until a human decides an approval, or until the approval’s deadline passes |
succeeded |
Finished, and the goal critic accepted every delegated skill |
failed |
Finished with an error, or delivered with a report that the work fell short |
cancelled |
Stopped on request |
A session moves between them like this:
stateDiagram-v2
[*] --> queued: request accepted
queued --> running: a worker claims it
running --> queued: retry, merge wait, spent daily budget, shutdown
running --> waiting: approval asked
waiting --> queued: approval decided
waiting --> running: approval deadline passed
running --> succeeded
running --> failed
running --> cancelled: cancel requested
queued --> cancelled: cancel requested
waiting --> cancelled: cancel requested
succeeded --> [*]
failed --> [*]
cancelled --> [*]
A session also has a phase that is stored in its checkpoint:
| Phase | What happens in it |
|---|---|
work |
The orchestrator or the named skill runs, then the result is finalized |
plan |
Before a skill that changes code, the session posted a plan and waits for the go-ahead, writes a revised plan, or ends without changing anything when the plan is declined or nobody answers by the deadline |
approval |
The session waits for an approval decision, then acts on it. Without a decision by the deadline it opens a draft pull request, leaves its pull request unmerged, or, in a follow-up, pushes only to a draft: see No answer. |
merge_wait |
The session looks at the pull request’s checks once a minute and merges when they are green |
Outcomes
The session result records one outcome:
| Outcome | Meaning |
|---|---|
draft_pr_opened |
A draft pull request was opened |
pr_opened |
A pull request ready for review was opened |
merged |
The pull request was merged after its checks passed |
answered |
Nothing to publish; the answer was posted where the request came from |
no_change |
No skill ran and there was nothing to say |
dry_run |
A dry run: the diff is kept in the result, nothing was pushed or posted |
declined |
A human declined to open the pull request, or, in a follow-up, to push to it; or the policy asked for a person and nobody could be asked about a pull request ready for review |
pushed |
A follow-up added a commit to the pull request kōjō opened |
branch_moved |
A follow-up pushed nothing: someone pushed to the pull request’s branch while it worked |
round_limit |
A follow-up did nothing: the pull request had used all its rounds |
plan_declined |
A person declined the plan, or asked for more revisions than allowed: nothing was changed |
plan_expired |
Nobody answered the plan before its deadline: nothing was changed |
failed_with_report |
The work did not meet its acceptance criteria and was delivered with the report. In a follow-up on a pull request ready for review, nothing was pushed and the report was posted. |
failed |
The session ended with an error before it produced a result |
Workers, leases and resume
kojo serve runs server.workers workers per replica (default 4). A worker polls the queue every 2 seconds and is woken at once when a request is enqueued.
- A claim holds a lease of 2 minutes. The worker renews it every 30 seconds.
- If a worker dies, its lease expires and another worker claims the session.
- If a worker loses its lease to another worker, it stops and touches nothing.
- On shutdown the session is checkpointed and put back in the queue. Another replica, or the same one after the restart, resumes it.
Checkpoints
The checkpoint is a JSON document on the session row. It holds the phase, the orchestrator’s conversation, the results of the delegated skills, the values they recorded, the sandboxes the session owns, the base commit, the pull request it opened and the approval it waits on.
The checkpoint is saved after every orchestrator turn and after every delegated skill. A resumed session continues from the last complete orchestrator turn. A subagent’s own conversation is not checkpointed: a skill run that was interrupted starts again from its first turn, in the same sandbox, so it sees the workspace as the interrupted run left it.
A session pins its configuration. It records the config revision IDs and the skill versions it started with, and a resumed session uses those revisions even if the dashboard changed them meanwhile. See Managing kōjō.
Retries
There are three kinds, at three levels:
| Level | What is retried | Limit |
|---|---|---|
| Goal critic | A rejected result goes back to the same subagent with the critic’s feedback | The skill’s max_attempts (default 3) |
| Rubric judge | A judge answer that carries no verdict is asked for again | One retry; then the skill run ends with an error |
| Session | A transient failure puts the session back in the queue, 30 seconds later the first time and 60 seconds later the second | At most 2 retries per session, counted in its checkpoint. Dry runs are never retried. |
Failures that are not retried: any failure of a dry run (a skill test or an eval), a budget that is reached, a sandbox driver that is unavailable, a missing recording of the replay model provider, and errors kōjō marks as permanent (an unknown skill, a forge that is not configured, a change too large to publish).
A session that fails tells whoever asked, with the error and a link to the session.
A session gets its terminal status last. Its sandboxes are destroyed and session.end is written to the audit log before the status changes, so a client that waits for a terminal status finds nothing still in flight.
Budgets and limits
kōjō enforces spend in USD. It does not enforce a token cap; tokens are counted and shown per session and per step.
| Limit | Default | Where it is set |
|---|---|---|
| Spend per session | From kojo.yaml at first start |
budgets.per_session_usd in the models settings |
| Spend per day (rolling 24 hours, every session of the install, evals included) | From kojo.yaml at first start |
budgets.per_day_usd in the models settings |
| Orchestrator turns per session | 30 | Fixed |
| Model turns per attempt of a skill run | 40 | max_steps in SKILL.md |
| Critic attempts per skill run | 3 | max_attempts in goals.yaml |
One exec tool call |
600 s, at most 3600 s | timeout_seconds argument |
| One goal check | 10 min | timeout on the check |
| Sandbox lifetime | 4 h | sandbox.ttl in kojo.yaml |
| Wait for green checks before a merge | 120 looks, one a minute | Fixed |
| Interim replies per session | 5 | Fixed |
| Comments per skill run | 3 | Fixed |
| Files published by one session | 500 files, 1 MiB each, 16 MiB together | Fixed |
Both budgets are checked before every model call. A running session that reaches a budget or a turn limit stops working, and what exists is still delivered with a report. A session that has not started yet waits while the daily budget is spent: it is put back in the queue for 10 minutes at a time, instead of starting and failing at its first model call. A value of 0 disables that budget.
kōjō ships no model price table. A tier without a configured price costs 0, so the USD budgets do not bound it. See Models.
Cancellation
Cancel a session with kojo sessions cancel, from the session page of the dashboard, or with POST /api/v1/sessions/{id}/cancel.
- A
queuedorwaitingsession is cancelled at once. - A
runningsession is flagged. Its worker sees the flag at the next heartbeat (within 30 seconds), stops, destroys the session’s sandboxes and recordssession.cancelin the audit log.
Cancelling is always this explicit action. A message to a session at work never cancels it, whatever its words.
The orchestrator
Each session without a named skill runs one orchestrating agent on the strong model tier. There is no workflow graph: the orchestrator reads the request and decides which skills to run and in which order.
The orchestrator has no shell and no file access. Its tools are:
| Tool | What it does | Available when |
|---|---|---|
delegate |
Runs one skill as a subagent and returns the critic’s verdict, the report, the recorded values and the changed files | Always |
memory_search |
Searches memory: the team’s for the repository, and the personal memory of whoever asked | Always |
memory_save |
Saves a preference to the personal memory of whoever asked | The request was made by a person (a mention, a chat message, a manual run, a campaign) and is not a dry run |
reply |
Posts a short interim message to whoever asked | The request has a thread, issue or pull request to answer in, and is not a dry run |
propose_schedule |
Proposes a recurring job, shown in chat for confirmation | The request came from chat and its actor is trusted |
The orchestrator’s last answer without a tool call is the session’s summary.
When the request names a skill (a schedule, an automatic forge event, kojo run --skill), the orchestrator is skipped and that skill runs once.
Subagents and tools
A subagent is one run of one skill. It starts with a fresh context: the skill’s instructions, a description of its environment, the repository’s AGENTS.md and .factory/memory knowledge, what kōjō remembers (the asker’s own preferences, the lessons maintainers taught the skill in reviews, matching team memory), and the task the orchestrator wrote. It does not see the orchestrator’s conversation.
A subagent has exactly the tools its skill lists, and in a follow-up on one of kōjō’s pull requests the two that read that pull request’s review and checks. The limit is enforced when a tool call is dispatched, not only in what the model is shown.
| Tool | What it does | Runs in the sandbox | Changes the workspace |
|---|---|---|---|
read_file |
Reads a file, optionally a line range | Yes | No |
list_files |
Lists files under a directory, optionally by glob | Yes | No |
search |
Searches the workspace for a regular expression | Yes | No |
write_file |
Creates or overwrites a file | Yes | Yes |
edit_file |
Replaces one exact occurrence of a string | Yes | Yes |
exec |
Runs a shell command | Yes | Yes |
git_diff |
Shows the changes made so far | Yes | No |
set_output |
Records a named value for later skills and for the goal checks | No | No |
memory_search |
Searches team memory and the asker’s personal memory | No | No |
memory_save |
Saves a fact to team memory, or a preference to the asker’s personal memory | No | No |
get_issue |
Reads an issue and its comments | No | No |
list_issues |
Lists the repository’s issues | No | No |
list_pull_requests |
Lists the repository’s pull requests: open, closed, merged or all | No | No |
get_pr |
Reads a pull request | No | No |
get_pr_diff |
Reads the diff of a pull request | No | No |
comment |
Posts a comment on the issue or pull request the session is about | No | No |
get_review_comments |
Reads the review of a pull request: comments on lines and the text of each review | No | No |
get_check_runs |
Reads the checks of a commit, with their output where the forge keeps it | No | No |
The tools that read the forge, and comment, go through the core’s own forge client. The sandbox is not involved.
A skill “writes” when it lists a tool that changes the workspace and does not set constraints.no_changes. Untrusted requests cannot reach such skills.
Skills, their files and their layering are described in Skills.
Sandboxes and the workspace
A skill that uses a sandbox tool, or that has goal checks, gets a sandbox. A session has one sandbox per image. The first one clones the repository at the default branch, or at the pull request’s head when the session is about a pull request. When a later skill needs another image, the workspace is copied into the new sandbox.
Before every skill run the sandbox’s policy is applied again: the egress allowlist is narrowed to that skill’s kit and short-lived credentials are refreshed. A kit’s setup commands run once per skill and session, each recorded as a step. When the session ends, its sandboxes are destroyed. See Sandboxes.
The goal critic
The critic judges what one skill run changed, measured from the workspace as it was when that run started. It applies the skill’s acceptance criteria in this order:
- Every value in the skill’s
outputswas recorded withset_output, and there is a final report. - The constraints on the diff:
no_changes,max_changed_lines,forbidden_paths,required_paths. - The checks: commands that run in the sandbox and must exit 0. A check with
expect_failuremust exit non-zero. A check withskip_if_unsetis skipped when a value it names was not recorded. A check that times out never passes. - The rubric judge, when the skill has one. It runs only if everything above passed, so no model call is spent on work that already failed. It grades the task, the report and the diff, and answers with a pass or the unmet rubric items.
A rejected result returns to the same subagent with the unmet criteria and the output of the failed checks. After the last attempt the result is kept either way: failed work is reported, never dropped.
The schema of goals.yaml is on the goals.yaml page.
Merge policy and publishing
When the session’s work is done, kōjō diffs the workspace against the base commit.
- No change, a request without a repository, or an untrusted actor: the summary is posted as an answer.
- A dry run: the diff is stored in the session result and nothing is pushed or posted.
- Otherwise the merge policy scores the change and chooses an action. Under the default mode,
draft_only, that is always a draft pull request.
The core publishes. It reads the changed files out of the sandbox, creates a kojo/<slug>-<id> branch through the forge API, commits the files and opens the pull request. The sandbox never pushes. The modes, the risk score and auto-merge are described in Merge policy.
The trust boundary
kōjō has two sides, and the line between them is the main security property.
flowchart TD
subgraph trusted["kōjō core: holds every credential"]
C["Decides what runs, never runs repository code"]
end
subgraph untrusted["Sandbox: sees placeholders only"]
W["Shell, git, toolchains: runs repository and model-written code"]
end
C -- "exec and copy, through the driver" --> W
W -- "output, files, diffs" --> C
C -- "allowlist and credentials" --> P["Proxy, outside the sandbox"]
W -- "all egress" --> P
P -- "credential set on the way out" --> H["Hosts on the skill's allowlist"]
C -- "push, comment, merge" --> F["Forge API"]
- The core holds every credential. It never runs a repository command and never shells out to git.
- The sandbox runs everything that is untrusted. It holds placeholders; a proxy outside the sandbox sets the real credential on requests to allowlisted hosts. kojo-proxy sets a forge’s clone credential only on git’s fetch.
- What the core reads back from a sandbox is bounded in size and count.
Security covers the boundary in detail, with its known limits.
Lifecycle of a bug report
A maintainer comments on issue 123 and mentions the bot. The repository uses the default merge policy.
sequenceDiagram
participant F as GitHub
participant C as Core
participant O as Orchestrator
participant S as Subagent
participant G as Goal critic
participant X as Sandbox
F->>C: webhook, a mention on issue 123
Note over C: Signature verified. Intake checks the repository, the author's permission and the delivery ID
Note over C: Session queued, then claimed by a worker. Config revisions pinned, skills resolved
C->>O: run on the strong tier
O->>S: delegate reproduce-bug
C->>X: create, fetch the repository
S->>X: write a failing test
S->>G: final report
G->>X: run the checks
G-->>O: accepted, checks and rubric passed
O->>S: delegate fix-bug
S->>X: edit, run tests
S->>G: final report
G->>X: run the checks
G-->>S: rejected, with feedback
S->>X: edit again
S->>G: final report
G->>X: run the checks
G-->>O: accepted on attempt 2
O-->>C: final answer, the summary
Note over C: Diff since the base commit, risk score, merge policy draft_only
C->>X: read the changed files
C->>F: branch kojo/slug-id, commit, draft pull request
C->>F: comment on issue 123 with the summary and the link
C->>X: destroy
Note over C: Status succeeded, outcome draft_pr_opened
If the critic had rejected the fix on its last attempt, the change would still be published as a draft, with the critic’s report in the pull request body, and the session would end as failed with the outcome failed_with_report.
Glossary
| Term | Meaning |
|---|---|
| Request | The normalized form of something asked of kōjō: actor, surface, repository, issue or pull request, text |
| Actor | Who asked. trusted when they may trigger write actions |
| Surface | Where requests come from and answers go: github, gitlab, forgejo, bitbucket, slack, cli, mcp, dashboard, schedule |
| Intake | The code that accepts or rejects a request and sets its trust |
| Session | One request being handled; also the queue entry a worker claims |
| Phase | Where a session is in its life: work, approval or merge_wait |
| Checkpoint | The session’s saved state, from which another worker can resume it |
| Lease | A worker’s time-limited claim on a session (2 minutes, renewed by heartbeat) |
| Orchestrator | The agent that decides which skills to run. It has no shell and no file access |
| Skill | A role defined as files: instructions, tools, model tier, acceptance criteria and a sandbox kit |
| Subagent | One run of one skill, with a fresh context |
| Tool | An action a subagent may take. A skill lists the tools it has |
| Output | A named value a subagent records with set_output; later skills and goal checks read it |
| Kit | The sandbox part of a skill: image, resources, egress allowlist, credential names |
| Sandbox | The isolated environment that holds the workspace and runs untrusted code |
| Workspace | The repository checkout inside the sandbox, at /workspace |
| Goal critic | The check of a subagent’s result against its skill’s acceptance criteria |
| Check | A command that must exit 0 (or non-zero with expect_failure) for the critic to accept |
| Judge | The model that grades a result against the skill’s rubric, after the deterministic criteria passed |
| Merge policy | The rules that decide how a change is published: draft, ask, or merge |
| Risk score | A number from 0 to 1 that the merge policy computes for a change; it maps to low, medium or high |
| Approval | A question put to a human in chat with Approve and Deny buttons |
| Step | One recorded action of a session: a model turn, a tool call, a sandbox command, a critic verdict. Steps are what session replay shows |
| Team memory | Facts about a repository or the org, stored in Postgres and searched by subagents. See Memory |
| Personal memory | What one person asked kōjō to remember; read only in sessions that person starts |
| Lesson | A team memory learned from a maintainer’s review of a pull request kōjō opened |
| Config revision | One version of one runtime config object. Sessions pin the revisions they start with |
| Tier | A named model slot (fast, strong) that the models settings map to a concrete model |
| Dry run | A session that pushes and posts nothing: skill tests and evals |