diff --git a/src/content/posts/kiln-automation-flow-en.md b/src/content/posts/kiln-automation-flow-en.md new file mode 100644 index 0000000..8ac47cd --- /dev/null +++ b/src/content/posts/kiln-automation-flow-en.md @@ -0,0 +1,173 @@ +--- +title: "How Kiln turns an issue into a release" +description: "Reading the source of Kiln (kilnflow.dev): how an issue labelled kiln:ready goes through AI development, an independent review by a second agent, Kiln's own merge queue and auto-tagging, and is published by the server with no human step." +growth: seedling +planted: 2026-11-23T01:41:38+08:00 +lastTended: 2026-11-23T01:41:38+08:00 +draft: false +lang: "en" +tags: + - "ai-agents" + - "workflow" + - "code-review" + - "self-hosting" + - "open-source" +--- + + + +
中文版:Kiln 怎么把一个 Issue 自动变成一次发布 · Companion post: How kiln-orca runs Kiln's work through agents in Orca
+Kiln is a self-hosted CI. One small server keeps all state in Postgres; build machines and AI machines connect out to it to pick up work. With the optional AI nodes attached, a GitHub issue labelled kiln:ready is designed and coded by an AI, reviewed by a different agent, then merged, tagged and released, with no human clicking anything in between.
This post follows that path through the code. I read commit e3bff51, source only: nothing was built or run. File paths are relative to internal/server/ unless stated otherwise.
In one sentence: GitHub only sends events and receives results; deciding, scheduling, merging, tagging and publishing all happen in Kiln's server, chained together by one habit: every state change commits in the same transaction as the message it causes.
Inside the server, CI and AI are separate domains with different tables, schedulers and wire protocols (runner.v1 and airunner.v2). They meet only in the assembly file server.go, through narrow interfaces, or through events in a shared outbox table. internal/archtest fails the tests if one domain imports another or queries its tables, so the boundary is checked by a machine, not just described in a doc.
Kiln builds itself with Kiln. On 22 November 2026, the day after it was open-sourced, three i18n issues labelled kiln:ready went through the whole chain. Times are UTC, from commit and tag metadata; review outcomes are from the PR pages.
| Time | What happened | Who |
|---|---|---|
| 09:44 | Pushed to kiln/dev-6 for issue #2; PR #9 opened | Claude Code writes; server opens the PR |
| after 09:44 | 19 checks run. AI review fails: one medium finding, a test that could not catch the bug it claimed to cover | Codex, as a PR review |
| 10:07 | Repair round 1: test rewritten, disposition comment posted | Claude Code |
| before 10:19 | Second review passes; kiln:merge added | Codex; server |
| 10:19 | Squash-merged as 11deef3; tag v1.0.3 created on it | kiln-runner[bot] |
| 10:20 | Release v1.0.3 published (12 assets); issue #2 closed | server |
PR #10 also took one repair round (merged 11:41, v1.0.4); PR #11 passed review first time (merged 12:32, v1.0.5). From first commit to public release: 36, 45 and 13 minutes.
+ +GitHub posts the event to /webhook. The handler checks the HMAC-SHA256 signature with a constant-time comparison, stores the event in events keyed by delivery id, and returns 202. Nothing else happens in the request.
A background loop then processes stored events one by one. Installations from accounts outside KILN_ALLOWED_ACCOUNTS are rejected and uninstalled; pull requests from forks are always dropped, with no switch to allow them; an event already superseded by a newer one for the same ref is skipped.
ingest/webhook.go:35 · ingest/event.go:159 · ingest/push.go:308
The planner reads .kiln/workflows/*.yml at the commit being built. For a PR that is the head commit, not a merge with the target branch. The syntax follows GitHub Actions minus uses:; expressions can only reference values known at planning time, so matrices, if: and needs are fully expanded here.
The run, its jobs, concurrency-group membership and the first Check Run records are written in one transaction. A broken workflow still produces a run, marked failed immediately, so the commit shows a red check carrying the error.
+ci/planner/planner.go:234,342 · internal/workflow/
When a job's dependencies finish, the engine hashes everything that can affect it into an action key: the digest of its input files, env, steps, image digest, runtime digests, upstream output digests. Secrets count by name only.
+If action_cache has the key, the job succeeds without touching a runner; if an identical job is already running, this one follows its result. That is why Kiln's own workflow keeps the version-stamped build in a separate job: after merge, the tests keep the same key and are not rerun.
ci/engine/engine.go:205,372
Runners hold a long-poll Acquire open. The scheduler locks the runner's row, takes up to 300 candidate jobs with FOR UPDATE SKIP LOCKED in priority-then-age order, and filters by platform, pool, executor, declared CPU and memory (against that machine's free capacity) and per-repo concurrency limits.
The chosen job becomes running, its epoch increments, an attempt row is inserted and a lease is set, in that same transaction. Every later write from the runner must carry the matching epoch, so a runner that lost its lease can never overwrite a newer attempt.
ci/scheduler/reserve.go:211,730 · ci/runnerapi/scheduler.go:140
The runner checks out only the files the job's inputs match, with no .git; installs Go, Node and pnpm verified against digests in .kiln/runtimes.lock; pulls upstream outputs from the content-addressed store; and runs steps on the host or in a container. Container jobs default to --network none.
Source is fetched with a token the server issues for that one fetch, scoped to that one repository, and revokes afterwards. Secrets arrive over RPC, live only in step environment variables, and are masked in logs in raw, base64 and URL-encoded form.
+Heartbeats every few seconds renew the lease and carry cancellation back. Outputs go to presigned upload URLs, and Complete ends the attempt and writes the cache entry.
internal/runner/execute.go:61 · internal/runner/source.go:178 · ci/runnerapi/runnerapi.go:392
Each state change writes an outbox row in the same transaction. The row holds only an id, so several changes collapse into one pending delivery. A worker turns rows into Check Runs, looking up by external id before creating, with exponential backoff and a dead-letter state after 20 tries.
+When the last job of a PR's run finishes, the engine emits pr.verified. The merge queue and AI development both wait on that one event.
outbox/worker.go:86 · ci/cicheck/checkrun.go:40 · server.go:390
Kiln uses neither GitHub's merge queue nor branch protection. A PR enters the queue when someone adds kiln:merge, or automatically once every entry under required has passed, if .kiln/merge.yml on the default branch sets auto: true. That config is read only from the default branch, so a PR cannot lower its own bar.
Each repository has one executor, holding a Postgres advisory lock. Every pass re-reads the PR from GitHub and decides from scratch: wait, update the branch, merge, or remove it with a reason. The merge request carries the head SHA it verified, and GitHub refuses if the head has moved.
+mergequeue/process.go:46,894 · mergequeue/decide.go:208 · mergequeue/autoenqueue.go:85
The merge, the queue-entry update and a tag record commit together. The tag worker reads existing tags, picks the next version (patch by default; release:minor, release:major and release:skip labels change that), creates the ref, then calls the CI planner directly to plan a push run for the tag.
A job with a release: block has no steps and never reaches a runner. The server stages every target first (npm integrity check, image layers pushed by digest, a draft GitHub Release), passes a last cancel point, then commits in the order npm, image tags, HTTP uploads, GitHub Release. On a rerun, identical content already on a target counts as success.
autotag/autotag.go:315,684,932 · ci/publish/job.go:52 · ci/publish/targets.go:44
An AI node is a machine with the Orca desktop app and the kiln-orca connector. Its role, review or development, is fixed by its enrolment token. The connector takes a task, starts a coding agent in a fresh Orca worktree, and hands the result back. The agent's only way to talk to Kiln is the kiln-orca task command; the companion post covers that side.
Opening or pushing to a PR plans a review request alongside the CI runs. The review brief, .kiln/agent/review.md, is read from the PR's base, so a PR cannot rewrite its own review standard. The agent must return a strict JSON report; only "no issues" passes. The result is posted as a PR review and as the ai-review check.
The server records which agents worked on each development item and refuses to give the review to any of them: checked once when planning and again when a node tries to take the task. Kiln's own config has Claude Code developing and Codex reviewing.
+The review brief requires mutation checks: break the changed logic in a scratch copy and confirm a test fails. Both failed reviews in the table above were of that kind.
+ai/review/plan.go:135,568 · ai/review/tasktype.go:104,178 · ai/review/publish.go:124
kiln:ready are mirrored into dev_pool from Issues webhooks.kiln/attempt-<id> branch. If it judges one PR is not enough, it first delivers a Markdown-only design PR; the server starts implementation automatically once that merges.kiln/dev-<item> with a short-lived token of its own, and opens the PR itself. Agent-written titles and bodies are defused so they cannot mention people or close unrelated issues..kiln/, .github/, AGENTS.md, CLAUDE.md, .claude/ or .codex/, exceed 300 files or 100 commits, or declare a change to one of the project's 23 confirmed decisions get kiln:hold and wait for a person.max_fix_rounds (10 in Kiln's own config).ai/aidev/pool.go:49 · ai/aidev/claim.go:84 · ai/aidev/executor.go:224,627 · ai/aidev/fix.go:162 · ai/aidev/state.go:14
| Rule | What it prevents | Where |
|---|---|---|
| A state change and its outbound message commit together | A green job with no Check Run; a merge with no tag record | outbox/, shared by all domains |
| Every job and task write carries the epoch | A runner that lost its lease overwriting a newer attempt | ci/engine/engine.go:569, ai/lease.go:21 |
| One executor per object, deciding from fresh state | Two processes merging or tagging the same thing; acting on stale reads | mergequeue/process.go:43, autotag/autotag.go:315 |
| Writes without an idempotency key record intent first and are never blindly retried | Duplicate comments, reviews or issue closures after a lost response | mergequeue/closeissues.go:180, ai/review/publish.go:124 |
A fifth, narrower rule: no build or AI machine stores a git credential. Each clone, fetch or push asks the server for a token scoped to one repository, and the server revokes it when the operation or the attempt ends (gitcred/).
| File or label | Controls | Read from |
|---|---|---|
.kiln/workflows/*.yml | Jobs, matrices, containers, services, release targets | The commit being built |
.kiln/runtimes.lock | Go, Node, pnpm, Bun versions and per-platform digests | The commit being built |
.kiln/merge.yml | Merge method, required checks, auto-enqueue, tag rule, allowed target branches | Default branch only |
.kiln/agent/config.yml | Who reviews, who develops, platforms, timeouts, repair rounds | Review: PR head; development: default branch |
review.md / development.md | The briefs given to the two agents | PR base / default branch |
kiln:ready / kiln:skip | Put an issue into / keep it out of the AI task pool | Issue |
kiln:merge / kiln:hold | Enter the merge queue / block auto-enqueue | PR |
release:minor / major / skip | Version bump for the tag created after merge | PR, frozen at merge |
Secrets, variables and environments are stored in Kiln, encrypted with the server's master key, not in GitHub settings. Logs, artifacts and caches are kept for 12 hours; only results are kept long-term.
+ +I read six subsystems in depth and found no high-severity defects; what is worth fixing clusters around the things the design assumes are friendly. For example: when a missed webhook is recovered by reconciliation, the push loses its "was this a bot" flag, so a third-party bot's tag can still publish; the write token a development node can get covers the whole repository; and kiln:ready approves an issue number, not its text, so an edited issue body is what the agent builds. Each of these only matters once the trust boundary widens: a third-party bot with push access, a prompt-injected agent, a malicious dependency.
The bigger risks are not in the code: there is a single human maintainer, the design docs live in a private Outline, and CI logs are not public.
+English version: How Kiln turns an issue into a release · 姊妹篇:kiln-orca 怎么让 Orca 里的 Agent 干 Kiln 的活
+Kiln 是一套自托管的 CI:一台小 Server 把全部状态放在 Postgres 里,构建机和 AI 执行机都从自己那边连过来领活。接上可选的 AI 节点之后,一个打了 kiln:ready 标签的 GitHub Issue 会被 AI 设计、写代码、由另一个 Agent 审计,然后合并、打 tag、发布,中间不需要人点任何按钮。
这篇顺着这条路读它的代码。我读的是 commit e3bff51,只读源码,没有构建,也没有运行。下文的文件路径如无说明,都相对于 internal/server/。
一句话:GitHub 只管发事件和收结果;判断、调度、合并、打 tag、发布全在 Kiln 的 Server 里,靠「状态变更和外发消息同一个事务提交」把这些步骤串成一条不会丢步的链。
Server 里 CI 和 AI 是两个分开的领域:表、调度器、通信协议(runner.v1 和 airunner.v2)都不同。它们只在组装文件 server.go 里通过窄接口接线,或者经由共用的 outbox 表传事件。internal/archtest 会在一个领域直接 import 另一个领域、或查询别人的表时让测试失败,这条边界是机器检查的,不只是写在文档里。
Kiln 用 Kiln 构建自己。2026 年 11 月 22 日,也就是开源后的第二天,三个打了 kiln:ready 的 i18n Issue 走完了整条链。下面的时间是 UTC,取自 commit 和 tag 的元数据;审计结果来自 PR 页面。
| 时间 | 发生了什么 | 谁做的 |
|---|---|---|
| 09:44 | 为 Issue #2 推到 kiln/dev-6,开出 PR #9 | Claude Code 写代码;Server 开 PR |
| 09:44 之后 | 19 个检查运行。AI 审计不通过:1 个中等问题,一个测试测不出它声称覆盖的错误 | Codex,发为 PR review |
| 10:07 | 第 1 轮返修:重写测试,贴出处置说明 | Claude Code |
| 10:19 之前 | 第二次审计通过;加上 kiln:merge | Codex;Server |
| 10:19 | squash 合并为 11deef3,在它上面打 v1.0.3 | kiln-runner[bot] |
| 10:20 | 发布 Release v1.0.3(12 个 asset),关闭 Issue #2 | Server |
PR #10 也是一轮返修(11:41 合并,v1.0.4);PR #11 一次通过审计(12:32 合并,v1.0.5)。从第一个 commit 到公开发布,三个分别用了 36、45 和 13 分钟。
+ +GitHub 把事件 POST 到 /webhook。处理函数用常数时间比较校验 HMAC-SHA256 签名,按 delivery id 去重存进 events 表,然后返回 202,请求里不做别的事。
一个后台循环再逐条处理存下的事件:不在 KILN_ALLOWED_ACCOUNTS 里的账号的安装会被拒绝并卸载;来自 fork 的 PR 一律丢弃,没有开关;同一个 ref 已经有更新事件的旧事件直接跳过。
ingest/webhook.go:35 · ingest/event.go:159 · ingest/push.go:308
Planner 按要构建的那个 commit 读 .kiln/workflows/*.yml。PR 构建的是 head commit,不是和目标分支的合并提交。语法沿用 GitHub Actions,但不支持 uses:;表达式只能引用规划时就知道的值,所以矩阵、if:、needs 在这一步全部展开。
run、job、并发组和第一批 Check Run 记录在同一个事务里写入。workflow 写错了也会建出 run,只是直接标为失败,commit 上会出现一个带错误信息的红色检查。
+ci/planner/planner.go:234,342 · internal/workflow/
一个 job 的依赖全部完成时,engine 把会影响它的一切算成 action key:inputs 文件的摘要、env、steps、镜像 digest、runtime digest、上游输出的摘要。secret 只以名字计入。
+action_cache 里有这个 key,job 就直接成功,不碰 Runner;有一个 key 相同、正在执行的 job,这个 job 就跟着它的结果走。所以 Kiln 自己的 workflow 把带版本号的构建单独放成一个 job:合并之后测试的 key 不变,不用重跑。
ci/engine/engine.go:205,372
Runner 保持一个长轮询的 Acquire。调度器锁住这台 Runner 的行,用 FOR UPDATE SKIP LOCKED 按优先级和先后取最多 300 个候选 job,再按平台、pool、执行器、声明的 CPU 和内存(对照这台机器的剩余容量)以及仓库的并发上限筛选。
选中的 job 在同一个事务里变成 running、epoch 加一、插入 attempt、设好租约。Runner 之后的每次写入都要带上匹配的 epoch,所以一台丢了租约的 Runner 不可能覆盖新一次 attempt 的结果。
ci/scheduler/reserve.go:211,730 · ci/runnerapi/scheduler.go:140
Runner 只检出 job 的 inputs 匹配到的文件,没有 .git;按 .kiln/runtimes.lock 的摘要安装并校验 Go、Node、pnpm;从内容寻址存储取上游 job 的输出;在主机上或容器里运行 steps。容器 job 默认 --network none。
拉源码用的 token 是 Server 为这一次 fetch 单独签发的、只覆盖这一个仓库,fetch 完就吊销。secret 经 RPC 下发,只放在 step 的环境变量里,日志中的原文、base64 和 URL 编码形式都会被遮掉。
+每隔几秒的心跳续租约,也把取消请求带回来。输出上传到预签名的地址,最后 Complete 结束 attempt 并写缓存。
internal/runner/execute.go:61 · internal/runner/source.go:178 · ci/runnerapi/runnerapi.go:392
每次状态变更都在同一个事务里写一条 outbox 记录。记录里只有一个 id,所以连续几次变更会合成一次待投递。worker 把它变成 Check Run:先按 external id 查找再创建,指数退避,失败 20 次进死信。
+一个 PR 的 run 最后一个 job 结束时,engine 发出 pr.verified。合并队列和 AI 开发都在等这一个事件。
outbox/worker.go:86 · ci/cicheck/checkrun.go:40 · server.go:390
Kiln 不用 GitHub 的 merge queue,也不需要分支保护。PR 有人加上 kiln:merge 就入队;默认分支上的 .kiln/merge.yml 写了 auto: true 时,required 里的每一项都通过后自动入队。这个配置只从默认分支读,PR 改不了自己的合并门槛。
每个仓库只有一个执行者,持有 Postgres 的 advisory lock。每一轮都重新从 GitHub 读 PR,从头判断:等、更新分支、合并,或者带着原因移出队列。合并请求带上它验证过的 head SHA,head 变了 GitHub 会拒绝。
+mergequeue/process.go:46,894 · mergequeue/decide.go:208 · mergequeue/autoenqueue.go:85
合并、队列条目的更新和一条 tag 记录在同一个事务里提交。tag worker 读出已有的 tag,算出下一个版本(默认升补丁号;release:minor、release:major、release:skip 标签改变规则),创建 ref,然后直接调用 CI 的 planner 为这个 tag 规划一次 push run。
写了 release: 的 job 没有 steps,不会到 Runner 上。Server 先把所有目标暂存好(npm 核对 integrity、镜像按 digest 推层、GitHub Release 建成草稿),过了最后一个可取消点后,按 npm、镜像 tag、HTTP 上传、GitHub Release 的顺序正式发布。重跑时,目标上已有相同内容就算成功。
autotag/autotag.go:315,684,932 · ci/publish/job.go:52 · ci/publish/targets.go:44
AI 执行节点是一台装了 Orca 桌面版和连接器 kiln-orca 的机器,角色(审计或开发)在注册时由令牌决定。连接器领一个任务,在新的 Orca 工作区里启动编程 Agent,再把结果交回。Agent 和 Kiln 打交道只有一个入口:kiln-orca task 命令。
PR 打开或推送时,和 CI run 一起规划一个审计请求。审计说明 .kiln/agent/review.md 从 PR 的 base 读,PR 改不了自己的审计标准。Agent 必须交回格式严格的 JSON 报告,只有「没有问题」才算通过;结果发为 PR review 和 ai-review 检查。
Server 记下每个开发工作项用过哪些 Agent,审计时拒绝派给其中任何一个:规划时检查一次,节点来领任务时再检查一次。Kiln 自己的配置是 Claude Code 开发、Codex 审计。
+审计说明要求做变异验证:在临时副本里把改动的关键逻辑改坏,确认测试会失败。上面表格里两次审计不通过,都是这一类问题。
+ai/review/plan.go:135,568 · ai/review/tasktype.go:104,178 · ai/review/publish.go:124
kiln:ready 的 Issue 经 Issues webhook 同步进 dev_pool。kiln/attempt-<id> 分支。判断一个 PR 做不完时,先交付只改 Markdown 的设计 PR,合并后 Server 自动开始实现。kiln/dev-<工作项> 快进过去,由自己开 PR。Agent 写的标题和正文会被处理,不能 @ 人,也不能顺手关掉别的 Issue。.kiln/、.github/、AGENTS.md、CLAUDE.md、.claude/、.codex/,超过 300 个文件或 100 个 commit,或者声明要改项目 23 条已确认决定之一的交付,加 kiln:hold 等人确认。max_fix_rounds 轮(Kiln 自己配的是 10)。ai/aidev/pool.go:49 · ai/aidev/claim.go:84 · ai/aidev/executor.go:224,627 · ai/aidev/fix.go:162 · ai/aidev/state.go:14
| 规则 | 防的是什么 | 在哪 |
|---|---|---|
| 状态变更和外发消息同一个事务提交 | job 绿了却没有 Check Run;合并了却没记下 tag | outbox/,各领域共用 |
| job 和任务的每次写入都带 epoch | 丢了租约的 Runner 覆盖新一次 attempt 的结果 | ci/engine/engine.go:569、ai/lease.go:21 |
| 每个对象一个执行者,每次按最新状态判断 | 两个进程合并、打 tag 同一个东西;按过期的读结果行事 | mergequeue/process.go:43、autotag/autotag.go:315 |
| 没有幂等键的写操作先记意图,绝不盲目重试 | 响应丢失后重复评论、重复 review、重复关 Issue | mergequeue/closeissues.go:180、ai/review/publish.go:124 |
还有一条范围更窄的:构建机和 AI 机器上都不存 git 凭据。每次 clone、fetch、push 都向 Server 要一枚只覆盖一个仓库的 token,操作结束或 attempt 结束时由 Server 吊销(gitcred/)。
| 文件或标签 | 管什么 | 从哪读 |
|---|---|---|
.kiln/workflows/*.yml | job、矩阵、容器、services、发布目标 | 正在构建的 commit |
.kiln/runtimes.lock | Go、Node、pnpm、Bun 的版本和各平台摘要 | 正在构建的 commit |
.kiln/merge.yml | 合并方式、必需检查、自动入队、打 tag 规则、允许的目标分支 | 只从默认分支 |
.kiln/agent/config.yml | 谁审计、谁开发、平台、超时、返修轮数 | 审计:PR head;开发:默认分支 |
review.md / development.md | 给两个 Agent 的说明 | PR base / 默认分支 |
kiln:ready / kiln:skip | 把 Issue 放进 / 排除出 AI 任务池 | Issue |
kiln:merge / kiln:hold | 进入合并队列 / 不自动入队 | PR |
release:minor / major / skip | 合并后打 tag 时怎么升版本 | PR,合并时冻结 |
secret、变量和 environment 都存在 Kiln 里,用 Server 的主密钥加密,不放在 GitHub Settings。日志、产物和缓存只保留 12 小时,长期只留结果。
+ +我把六个子系统各读了一遍,没有找到高危问题;值得改的地方集中在设计上默认可信的那些对象。例如:漏掉 webhook 后补对账时,push 不再带「是不是 Bot」的标记,第三方 Bot 推的 tag 可能照常发布;开发节点拿到的写权限 token 覆盖整个仓库;kiln:ready 批准的是 Issue 编号而不是正文,之后改了正文,Agent 照新内容做。这些都只在信任边界被拉宽时才要紧(第三方 Bot 有推送权限、Agent 被提示注入、依赖里混进恶意代码)。
更大的风险不在代码里:只有一位人类维护者,设计文档在私有的 Outline 里,CI 日志不对外公开。
+中文版:kiln-orca 怎么让 Orca 里的 Agent 干 Kiln 的活 · Previous post: How Kiln turns an issue into a release
+The previous post covered Kiln's whole automation chain: an issue is developed by an AI, reviewed by another agent, then merged, tagged and released. On the AI side it stopped at "the server hands the task to an AI execution node". This post goes one level down: what happens on that node, how Kiln gets a coding agent inside Orca to do its work, and how it keeps the agent from doing anything else.
+I read the kiln-orca/ directory of kilnflow.dev (TypeScript, about 11.5k lines of source) at commit e3bff51. Source and tests only: nothing was installed or run. Orca itself is not open source; I only looked at how the connector calls its command line and at Orca output recorded in the tests. Paths are relative to kiln-orca/src/.
In one sentence: kiln-orca is a resident user service between the Kiln server and Orca. It takes tasks from the server, starts an agent in a one-shot Orca worktree, and writes each step to a local journal so it can carry on after a crash; the agent can only act by asking it through the kiln-orca task command, and git credentials are fetched from the server per transfer and handed back when done.
Orca is a desktop app that manages agents' worktrees, terminals and orchestration (runs, dispatches, workers). How Orca divides work inside, and whether it spawns sub-agents, is its own business; Kiln does not care.
+kiln-orca runs as a macOS LaunchAgent or a Linux systemd --user service, and one data directory is one node. The token a node enrols with decides whether it is a review node or a development node; a machine with both Codex and Claude Code installed can enrol one of each, each with its own data directory.
On start, the connector reads its enrolment and private key, takes a single-instance lock, opens a socket at run/kiln-orca.sock in the data directory, then checks each dependency: Orca is open and at least version 1.4.258; Docker and Compose work; the bundled kiln-runner can prepare toolchains; the journal is writable; each agent answers --version; and the kiln-task skill is synced into each agent's skills directory.
This status, plus the protocol version (currently 10), goes to the server with every Acquire. When capacity is full (default max_tasks: 2), local drain is on, Orca is not ready, or the journal or skill has a problem, the node stays connected but declares it takes no new work. An unreadable journal record counts as one slot of capacity, because it may describe an agent still running in Orca that the connector cannot identify.
host/run.ts:132 · core/node.ts:176,473,728 · core/orca.ts:306
After taking a task, the connector prepares the repository's clone under repos/<repo id>/<name>. It asks the server for a read token (IssueCredential with an op_id), passes it to git as http.<url>.extraHeader through environment variables so it never appears in argv, and calls RevokeCredential as soon as the clone is done. The connector's own git ignores the global and system git config.
It then registers the repository with Orca, writes task files under tasks/<attempt>/ (work.json, candidates.json for development selection, and so on), runs kiln-runner toolchain ensure to prepare Go, Node and pnpm from the repository's .kiln/runtimes.lock, and, if the repository declares test services, brings up Postgres or Redis with docker compose up -d --wait.
core/attempt.ts:459,746 · core/git.ts:171,410 · core/credentials.ts:77 · core/toolchain.ts:167 · core/services.ts:305
Before starting, the connector writes this start's request id to the journal, and only then calls:
+orca orchestration worker-start --worktree new-top-level --name kiln-a<attempt>-e<epoch> --base-branch <commit> --comment kiln:attempt=…;epoch=…;task=…;kind=… --agent <agent> --spec <prompt>
Orca creates a new top-level worktree and starts the agent in it. If the call's outcome is unclear (timeout, dropped connection), the connector does not retry; it asks request-show with the same request id whether Orca actually started it. If that cannot be settled, the task ends as infra.lost: failing is better than one task running two agents. The worktree's name and comment carry the attempt and epoch, which is how cleanup later proves "I created this".
core/attempt.ts:864,958 · core/orca.ts:611
When the agent finishes it sends a worker_done message. The connector keeps a "mailbox" terminal of its own in Orca running orchestration check --types worker_done,escalation,question and writes each message to a file; if the agent asks a question mid-task, it gets a fixed reply telling it to decide according to its brief.
While a task runs, the connector checks the mailbox every 2 seconds, falls back to worker-show every 30 seconds, and sends the server a heartbeat with the current stage every 5 seconds. The heartbeat reply can carry a cancel request; exceeding the task's time limit, losing the server for 10 minutes, a stale epoch, or the node being revoked all make the connector stop the agent. When the connector itself restarts it simply stops tracking; the agent keeps running in Orca and is picked up again from the journal.
core/mailbox.ts:585 · core/attempt.ts:118-123,1000,1358
kiln-orca taskThe agent's brief contains a command of the form node cli.js task --home <data dir> --attempt <id>; its usage is documented in the kiln-task skill, which ships with the package and is synced into the agent's directory by the connector. The command hands each request over the unix socket to the running connector, which calls the server on its behalf.
| Subcommand | What it does | Who validates |
|---|---|---|
info | Basic task information | Local |
check-report | Review: validate report.json before finishing | Server ValidateReport; locally only file type and a 1 MiB cap |
claim | Development: claim the selected issues | Server ValidateReport + ClaimDevIssues |
deliver | Development: submit the delivery report | Server validates the report; locally: clean tree, HEAD contains the delivered commit, design phase Markdown-only |
ci | Latest CI result for a PR or commit | Server GetVerification |
doc | Read the frozen external design docs | Server, authorised from task relations |
exec | Run a command with the managed toolchain; git gets credentials via the connector | See the next section |
done | End the round: validate once more, then send worker_done to Orca from the agent's own process | Connector |
One detail in that last row: worker_done is sent by the agent's process, using the identity Orca set in that session's environment; the connector never impersonates the agent to report completion.
host/socket.ts:295 · core/attempt.ts:1412-1649 · core/done.ts:138
task exec: one token per git transfertask exec -- <command> puts the toolchain first on PATH, points GIT_EXEC_PATH at a mirror directory where git-remote-http(s) is replaced by the connector's wrapper, and configures the connector's credential helper for the task repository's host.
Each time git goes over HTTP(S), the wrapper first checks whether the transfer would carry some other credential: a user or password in the URL, an http.extraHeader effective for that URL, netrc mentioning the host or default, or any of these being unreadable. Any one of them refuses the transfer. Otherwise it opens a read operation with the connector over the socket, and the connector asks the server for a token only if the URL is exactly the task repository or a declared dependency; when the connection closes, the token is revoked.
Write access is used only for delivery: the connector requests a separate write token for its own push of the attempt branch and returns it right after. The agent never gets a write credential.
+host/cli.ts:306,365 · core/gitexec.ts:45,109 · core/credentials.ts:246,362 · core/attempt.ts:1468,1490
When the agent finishes, the connector first records the verdict in the journal (stage finishing). For a development task that delivered, it pushes the delivered commit to kiln/attempt-<id> with a write token and confirms with ls-remote; if nothing was delivered, it pushes the current changes to the same branch as a checkpoint, so the next round can continue on a different machine. Then it sends Complete, retrying for up to 30 minutes.
Only after the result is confirmed does cleanup start: stop test services, confirm the agent has stopped and its terminal is released, delete the worktree, checkout and local branch through Orca, verify that Orca, git and the disk no longer have them, and finally delete the journal record. Before deleting, it checks name, comment, path and git registration; anything the connector did not create is left alone. Unfinished reclaim is retried every minute and abandoned after 12 hours with a log entry explaining the manual fix.
+core/attempt.ts:1159,1223,1256,1297 · core/git.ts:558 · core/reclaim.ts:185-411
This is the core of the design. Before every action that changes something outside the process, the connector records what it is about to do in journal/<attempt>.json: the start request id, the verdict, whether to push, the content of Complete. On restart it reads each record:
Complete is sent again;stopping, releasing or reclaiming are retried every minute;doctor and the dashboard until a person deals with it.The tests follow the same idea: about 580 cases using real git, a local git HTTP server, a real unix socket and multi-process lock races, plus many "epoch goes stale / process crashes in the middle of step X" cases.
+ +No high-severity defects. Most of what is worth fixing only matters if "the agent and the connector run as the same user" is treated as a security boundary; Kiln's own docs call these guard rails, not a boundary.
+| Problem | Effect | Where |
|---|---|---|
| The socket identifies callers by attempt id alone | With two tasks running, one agent can act as the other: get its credentials, claim issues, read docs. A random per-task secret in the task command would fix it | host/run.ts:202 |
| Fixed 10-minute git timeout on a full clone | On a large repo or slow link the clone times out, the next task starts from zero, and it can fail with infra.fetch indefinitely | core/git.ts:50,412 |
The shared clone's own .git/config and hooks still apply | A proxy or hook left by an earlier development agent affects later tasks' token-carrying git | core/git.ts:183 |
| Journal and state files are not fsynced | After power loss a record can be empty; an empty journal stops the node taking work until someone deletes it | core/journal.ts:215 |
| The clone path is built from the server-supplied repo name with no containment check before deletion | Only reachable if the server is buggy or compromised; the reclaim code already has this check, this path does not | core/attempt.ts:428 |
| A report file over 1 MiB is reported to the agent as "retry later" | The agent may keep retrying until the task times out | core/attempt.ts:1534 |
What is done well is just as clear: every child process is started with an argv array, never shell: true; tokens never appear in argv, the journal or logs, which record only fingerprints; external input is parsed as unknown and then validated, with not one as any in the source; and every external command has a timeout.
English version: How kiln-orca runs Kiln's work through agents in Orca · 前一篇:Kiln 怎么把一个 Issue 自动变成一次发布
+上一篇讲了 Kiln 的整条自动化链:Issue 被 AI 开发、被另一个 Agent 审计,然后合并、打 tag、发布。那篇里 AI 的部分只说到「Server 把任务派给 AI 执行节点」。这篇往下看一层:执行节点上发生了什么,Kiln 怎么让 Orca 里的编程 Agent 替它干活,又怎么防它乱来。
+读的是 kilnflow.dev 的 kiln-orca/ 目录(TypeScript,约 1.15 万行源码),commit e3bff51。只读源码和测试,没有安装,也没有运行;Orca 本身不开源,我只看了连接器怎么调用它的命令行,以及测试里录下的 Orca 输出。下文路径都相对于 kiln-orca/src/。
一句话:kiln-orca 是一个常驻的用户服务,夹在 Kiln Server 和 Orca 之间。它从 Server 领任务、在 Orca 里开一个一次性的工作区启动 Agent、把每一步写进本地 journal 以便崩溃后接着做;Agent 能做的事只有通过 kiln-orca task 命令问它,git 凭据也是它按每一次传输向 Server 要来、用完就还。
Orca 是一个桌面应用,管 Agent 的工作区(git worktree)、终端和编排(run、dispatch、worker)。Orca 内部怎么分工、要不要拆子 Agent,是它自己的事,Kiln 不管。
+kiln-orca 以 macOS LaunchAgent 或 Linux systemd --user 服务运行,一个数据目录就是一个节点。节点注册时用的令牌决定它是审计节点还是开发节点;一台装了 Codex 和 Claude Code 的机器可以同时注册两个节点,各用一个数据目录。
服务启动时,连接器读注册信息和私钥,拿单实例锁,在数据目录的 run/kiln-orca.sock 上开 socket,然后逐项检查:Orca 是否打开、版本是否不低于 1.4.258,Docker 和 Compose 能不能用,随包的 kiln-runner 能不能准备工具链,journal 能不能写,每个 Agent 的 --version,以及 kiln-task skill 是否已同步进 Agent 的 skill 目录。
这些状态连同协议版本(现在是 10)每次都随 Acquire 报给 Server。容量满了(默认 max_tasks: 2)、本机设了 drain、Orca 没就绪、journal 或 skill 有问题时,节点照样连着,但声明自己不接新任务。读不了的 journal 记录也按占一份容量算,因为里面可能有一个还在 Orca 中运行、连接器却不认识的 Agent。
host/run.ts:132 · core/node.ts:176,473,728 · core/orca.ts:306
领到任务后,连接器在 repos/<repo id>/<仓库名> 下准备这个仓库的 clone。需要的只读 token 向 Server 要(IssueCredential,带一个 op_id),经环境变量里的 http.<地址>.extraHeader 交给 git,不出现在命令行参数里;clone 完立刻 RevokeCredential。连接器自己的 git 不读全局和系统的 git 配置。
然后把仓库登记到 Orca,在 tasks/<attempt>/ 下写好任务文件(work.json、开发选题时的 candidates.json 等),按仓库的 .kiln/runtimes.lock 调用 kiln-runner toolchain ensure 准备 Go、Node、pnpm,仓库声明了测试服务时用 docker compose up -d --wait 拉起 Postgres 或 Redis。
core/attempt.ts:459,746 · core/git.ts:171,410 · core/credentials.ts:77 · core/toolchain.ts:167 · core/services.ts:305
启动前,连接器先把这次启动的请求 id 写进 journal,然后才调用:
+orca orchestration worker-start --worktree new-top-level --name kiln-a<attempt>-e<epoch> --base-branch <commit> --comment kiln:attempt=…;epoch=…;task=…;kind=… --agent <agent> --spec <prompt>
Orca 新建一个顶层工作区、在里面启动 Agent。如果这次调用的结果不明(超时、连接断开),连接器不重试,而是用同一个请求 id 去 request-show 查 Orca 到底启动了没有。查不出来就以 infra.lost 结束,宁可失败也不让同一个任务跑出两个 Agent。工作区的名字和备注带着 attempt 和 epoch,后面回收时靠它们确认「这是我建的」。
core/attempt.ts:864,958 · core/orca.ts:611
Agent 结束时会发一条 worker_done。连接器在 Orca 里有一个自己的「邮箱」终端,持续执行 orchestration check --types worker_done,escalation,question,把收到的消息落成文件;Agent 中途提问时给它一个固定的回复,让它按任务说明自己判断。
任务进行中,连接器每 2 秒看一次邮箱,每 30 秒用 worker-show 兜底查一次 Agent 的状态,每 5 秒给 Server 发一次心跳(带上当前阶段)。心跳的回复里可能有取消请求;超过任务时限、和 Server 失联 10 分钟、epoch 已经过期、节点被吊销,都会让连接器停下这个 Agent。连接器自己重启时只是不再跟踪,Agent 在 Orca 里继续跑,重启后从 journal 接上。
core/mailbox.ts:585 · core/attempt.ts:118-123,1000,1358
kiln-orca taskAgent 拿到的任务说明里有一条形如 node cli.js task --home <数据目录> --attempt <id> 的命令,用法写在随包分发、由连接器同步进 Agent 目录的 kiln-task skill 里。这个命令把请求经 unix socket 交给运行中的连接器,连接器再替它调用 Server。
| 子命令 | 做什么 | 谁来校验 |
|---|---|---|
info | 任务的基本信息 | 本地 |
check-report | 审计:提交前先校验 report.json | Server 的 ValidateReport;本地只查文件类型和 1 MiB 上限 |
claim | 开发:领取选中的 Issue | Server 的 ValidateReport + ClaimDevIssues |
deliver | 开发:交付报告 | Server 校验报告;本地查工作区干净、HEAD 包含交付的 commit、设计阶段只改 Markdown |
ci | 查 PR 或 commit 最新的 CI 结果 | Server 的 GetVerification |
doc | 读冻结的外部设计文档 | Server 按任务关系授权 |
exec | 用受管理的工具链跑命令,git 经连接器拿凭据 | 见下一节 |
done | 结束这一轮:再校验一次,然后在 Agent 自己的进程里向 Orca 发 worker_done | 连接器 |
最后一条有个细节:worker_done 由 Agent 的进程发出,身份来自 Orca 给这个会话设置的环境,连接器不冒充 Agent 报结束。
host/socket.ts:295 · core/attempt.ts:1412-1649 · core/done.ts:138
task exec:每一次 git 传输一枚 tokentask exec -- <命令> 把工具链放到 PATH 最前面,再用 GIT_EXEC_PATH 指向一个镜像目录,其中 git-remote-http(s) 换成了连接器的包装程序,并给任务仓库的主机配上连接器的 credential helper。
每次 git 走 HTTP(S) 时,包装程序先检查这次传输会不会带上别的凭据:地址里有用户名或密码、对这个地址生效的 http.extraHeader、netrc 里出现这个主机或 default,或者这些查不了。有任何一项就拒绝这次传输。都没有时,它经 socket 向连接器开一个只读操作,连接器只在地址正好是任务仓库或声明过的依赖仓库时才向 Server 要 token;连接一断,token 立即吊销。
写权限只在交付时用:连接器自己推 attempt 分支时单独申请一枚写 token,推完就还。Agent 拿不到写凭据。
+host/cli.ts:306,365 · core/gitexec.ts:45,109 · core/credentials.ts:246,362 · core/attempt.ts:1468,1490
Agent 结束后,连接器先把结论写进 journal(阶段 finishing)。开发任务交付了的,用写 token 把交付的 commit 推到 kiln/attempt-<id>,再用 ls-remote 确认;没交付的,把现有改动作为 checkpoint 推到同一个分支,下一轮可以换一台机器接着做。然后 Complete,失败就重试,最长 30 分钟。
结果确认之后才清理:停掉测试服务,确认 Agent 已停、终端已释放,经 Orca 删除工作区、检出和本地分支,再核对 Orca、git 和磁盘上都没有了,最后删掉 journal 记录。删除前会核对名字、备注、路径和 git 登记,不是连接器建的一律不动。回收没完成就每分钟重试,12 小时后放弃并在日志里写明怎么手工处理。
+core/attempt.ts:1159,1223,1256,1297 · core/git.ts:558 · core/reclaim.ts:185-411
整套设计的重点在这里。每一个会改变外部状态的动作之前,连接器都先在 journal/<attempt>.json 里记下要做什么:启动请求 id、结论、要不要推送、Complete 的内容。重启后逐条读 journal:
Complete 的,重发;stopping、releasing、reclaiming 的,每分钟再试一次;doctor 和 Dashboard 上写明哪个文件、什么原因,等人处理。测试也是按这个思路写的:约 580 个用例,用真实的 git、本地的 git HTTP 服务、真实的 unix socket 和多进程抢锁,还有大量「在某一步中途 epoch 过期 / 进程崩溃」的用例。
+ +没有找到高危问题。值得改的几处,大多只在「Agent 和连接器是同一个用户」这个前提被当成安全边界时才要紧;Kiln 的文档自己也说这些是护栏,不是边界。
+| 问题 | 会怎样 | 在哪 |
|---|---|---|
| socket 只按 attempt id 区分调用方 | 同时跑两个任务时,一个 Agent 可以冒充另一个任务领凭据、领 Issue、读文档。给任务命令加一个随机的每任务密钥就能补上 | host/run.ts:202 |
| git 固定 10 分钟超时,clone 是完整 clone | 大仓库或慢网络下 clone 超时,下一次又从零开始,可能一直 infra.fetch | core/git.ts:50,412 |
共享 clone 自己的 .git/config 和 hooks 仍然生效 | 上一轮的开发 Agent 留下的代理或 hook,会作用到后面任务里带 token 的 git | core/git.ts:183 |
| journal、状态文件写入后不 fsync | 断电后记录可能是空的;空的 journal 会让节点停止接活,直到有人删掉它 | core/journal.ts:215 |
| clone 路径由 Server 给的仓库名拼出,删除前不检查是否在根目录内 | 只有 Server 出错或被攻破时才会触发;回收代码里已经有这类检查,这里没有 | core/attempt.ts:428 |
| 报告文件超过 1 MiB 时,告诉 Agent 的是「可以稍后重试」 | Agent 可能反复重试直到超时 | core/attempt.ts:1534 |
做得好的地方也很明显:所有子进程都用参数数组启动,没有 shell: true;token 不出现在命令行参数、journal 和日志里,日志只记指纹;外部输入都按 unknown 解析再校验,源码里没有一处 as any;每个外部命令都有超时。