Agent runner
Claude Code and Codex chats for a desktop app. Each transcript stays in a SQLite file on the person's computer. Farero runs its own Claude and Codex chats with these drivers. Other apps install the published build, @farero-ai/agent-runner.
From the Farero checkout, yarn pack:agent-runner writes output/farero-ai-agent-runner-<version>.tgz. A push to Farero's main branch that changes the packed files publishes the next patch release to npm. A change that breaks callers raises the minor version in this package's package.json. The package needs Node 22.13 or later, or an Electron main process with that Node.
Start the runtime
Open one runtime in the main process and close it when the app quits.
import path from 'node:path'
import { app } from 'electron'
import { AgentRuntime } from '@farero-ai/agent-runner'
const runtime = await AgentRuntime.open({
directory: path.join(app.getPath('userData'), 'agents'),
harness: { claude: { permissionMode: 'default' } }
})
app.on('will-quit', () => {
void runtime.close()
})
The runtime keeps its store in directory/agent-runner.sqlite, with file mode 0600. A chat without a cwd works in directory/workspace. The runtime sends transcripts nowhere else. It refuses a store file that another store wrote, such as an earlier app transcript database with other tables.
By default the runtime finds claude and codex on PATH and in the usual install folders: Homebrew, ~/.local/bin, nvm, Volta and Codex.app. For Claude Code it also tries the binary that ships with @anthropic-ai/claude-agent-sdk, from app.asar.unpacked in a packaged Electron app. The provider gets a minimal environment. Pass envKeys in a provider's harness settings to let a variable such as ANTHROPIC_API_KEY through.
The harness defaults ask the person before tools run: Claude Code starts in default permission mode, and Codex in the workspace-write sandbox with on-request approvals. A setting you pass replaces its default. A setting you leave out, or pass as undefined, keeps it. Claude Code's web search and web fetch run without asking, as they do in Farero. List them in disallowedTools to turn them off.
Run a chat
const chat = await runtime.start({
provider: 'claude',
title: 'Release notes',
prompt: 'Summarise the last ten commits'
})
await runtime.send(chat.id, 'Make it shorter')
await runtime.stop(chat.id)
start stores the chat with its first prompt and returns. The reply arrives as changes. send refuses while a turn runs. stop asks the provider to stop the turn. When the provider does not stop within two seconds, the runtime closes its process.
list() returns the chats, the one with the newest activity first. Each chat has a status: idle, running, waiting while the provider waits for an answer, or error. Its error holds the reason.
The runtime keeps one provider process for each open chat and resumes the chat's provider thread when it starts a new one. A process that sits idle for ten minutes closes.
Follow a chat's changes
Every write to the store takes the next value of one store-wide counter and stamps it on each chat and entry it changes. A chat's revision is the newest of those stamps.
let revision = 0
let entries = new Map<string, TranscriptEntry>()
function load(chatId: string) {
const read = runtime.read(chatId)
if (!read) return
entries = new Map(read.entries.map(entry => [entry.id, entry]))
revision = read.revision
}
runtime.subscribe(change => {
if (change.chatId !== openChatId || change.revision <= revision) return
const changed = runtime.changes(openChatId, revision)
if (!changed) return
for (const entry of changed.entries) entries.set(entry.id, entry)
for (const id of changed.removed) entries.delete(id)
revision = changed.revision
})
read(chatId) returns { chat, entries, interactions, revision } from one read transaction.
changes(chatId, after) returns the chat when it changed after after, the listed entries that changed after it in the order they changed, the ids in removed of entries that are no longer listed, and the new revision. A page that kept a revision catches up after a reload with one call.
subscribe(listener) calls the listener with { chatId, revision } after each write commits, in commit order. A page that reads during the call sees the change. A listener that throws stops neither the turn nor the other listeners.
Entries are Farero's TranscriptEntry rows, keyed by id. rowid is a stable anchor and revision counts the writes to the row. @farero-ai/ui draws them with ReadOnlyTranscript. The store applies Farero's entry rules: tool text has a budget of 1 MiB, provider bookkeeping stays out of the list, and a stream item that the provider replaced stays out of the list.
When the app opens a runtime after a crash, a chat that was running opens with status error and an interrupted message. Its open draft and tool rows close. The runtime never sends a prompt again by itself: a provider may already have taken it in. The person decides whether to send it again.
Approvals and questions
A provider that asks for an approval or a question adds it to the chat's interactions, and the chat's status becomes waiting. Show them to the person and answer with answer:
await runtime.answer(chat.id, interaction.id, { decision: 'accept' })
await runtime.answer(chat.id, question.id, { answers: { [question.questions[0].id]: ['Ship it'] } })
An approval lists its decisions, for example accept and decline. A question lists its questions, and the answer maps the id of each question to the chosen options.
To answer some requests without the person, pass decide. Return undefined to leave a request for the person. The signal aborts when the request closes another way.
const runtime = await AgentRuntime.open({
directory,
decide: async (chatId, interaction, signal) =>
interaction.kind === 'approval' && allowedTool(interaction) ? { decision: 'accept' } : undefined
})
Give the agent your tools
serveTools serves your tools as an MCP server on 127.0.0.1. Hand it to each chat through connection:
import { AgentRuntime, serveTools } from '@farero-ai/agent-runner'
const tools = await serveTools({
tools: [
{
name: 'post_message',
description: 'Post a message to a Team channel',
inputSchema: {
type: 'object',
properties: { channel_id: { type: 'string' }, body: { type: 'string' }, client_id: { type: 'string' } },
required: ['channel_id', 'body', 'client_id']
}
}
],
call: async ({ chatId, name, args, signal }) => ({
content: [{ type: 'text', text: JSON.stringify(await team.call(chatId, name, args, signal)) }]
})
})
const runtime = await AgentRuntime.open({
directory,
connection: chatId => ({
mcpServers: { spool_team: { url: tools.url, headers: tools.headers(chatId) } }
})
})
headers(chatId) gives each chat its own bearer token, so call learns which chat called and one chat cannot act as another. Close the server with tools.close() after the runtime.
A tool with side effects needs an idempotency argument, as Farero's own tools take client_id. A provider can repeat a call after a lost reply. The same id must return the first result and not do the work again.
connection can also return localMcpServers for stdio servers, instructions for the agent's system prompt and toolEnvironment for variables that every tool command sees. The runtime reads it each time a chat's provider starts.
Connection status
const status = await runtime.connectionStatus('claude')
// { state: 'ready', message: 'Claude Code is signed in.', method: 'claude.ai' }
state is ready, missing, sign-in or unavailable, and message is one sentence for the person. method says how the person signed in, so an app can require a plan sign-in instead of an API key.
A host of your own
The drivers reach the computer through a DriverHost. localHost() is the default. Pass host to AgentRuntime.open to change how a provider is found and started:
import { localHost } from '@farero-ai/agent-runner'
const runtime = await AgentRuntime.open({
directory,
host: localHost({ searchPaths: [path.join(process.resourcesPath, 'bin')] })
})
A host has launch(provider, settings), which returns the command and the executable it found, notInstalled(provider, source), environment({ provider, workspaceEnv, accountHome, envKeys }), mcpStartupMs and clientInfo, the name Codex's app-server gets for the app. Pass localHost({ clientInfo: { name: 'spool', title: 'Spool', version } }) to keep your app's name. Pass drivers to run a provider with another NativeDriverFactory.
Work on the package in Farero
Farero imports the TypeScript source through @farero/agent-runner/<path>, for example @farero/agent-runner/providers/claude. Its runtime passes Farero's own host, src/host/driver-host.ts, and its Docker sandboxes satisfy ClaudeSandbox and CodexSandbox. yarn build:node compiles the package into Farero's bundles.
The package reads Farero's protocol modules in src/ and nothing from src/host/ or web/. It holds no Farero app state: Farero's store stays in Farero, because it commits transcript rows in the same transaction as delivery receipts. yarn check:imports enforces the boundary. The tarball holds one ESM entry and its bundled types, with Farero's protocol modules compiled in. Its npm dependencies keep Farero's versions.