Skip to content

Your team of AI agents, on your Mac, Windows or Linux. Agents that talk in channels, write code, review each other and use your tools, on the Claude Code, Codex, OpenCode or Pi you already have. Anything that touches the real world waits for your yes.

Start Claude Code for whoever will answer while the person types (#148)

Dias3e71b64d0d41
diff --git a/CHANGELOG.md b/CHANGELOG.md
index cbe1480..fbddfa5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ What changed in each version of Tucano. The format follows [Keep a Changelog](ht
> - **O navegador dos agentes abre páginas num servidor Linux que é um PC.** O Chrome esperava o chaveiro bloqueado da área de trabalho e nenhuma página carregava; no servidor ele agora usa o próprio armazenamento.
> - **A página do servidor abre por HTTP num endereço privado,** como o Tailscale ou a rede de casa. Ela ficava em branco.
> - **Os agentes do Claude respondem mais rápido e gastam menos do plano em conversas longas.** Cada agente continua a mesma sessão do Claude Code na conversa e manda só o que é novo; entre uma resposta e outra o processo fica aberto por 5 minutos. O primeiro trecho da resposta chega quase 1 segundo antes, e uma conversa longa não é mandada inteira de novo a cada resposta.
+> - **A resposta começa enquanto você digita.** O Tucano já abre o Claude Code de quem vai responder: o router escolhe quem responde num canal em 1,5 s em vez de 2,8 s, e numa DM parada há mais de 5 minutos a resposta começa meio segundo antes.
> - **Um agente só acorda outro quando precisa.** Quando um agente menciona outro na resposta, o router lê e chama só quem tem algo a fazer: um agradecimento ou um “combinado” não acordam ninguém. Na dúvida, chama.
> - **O app abre fora do instalador mesmo com uma versão estranha.** O atualizador, que nem é usado ali, conferia a versão ao abrir e derrubava o app.
> - **Agentes no Grok.** Um agente pode rodar no Grok CLI, com a assinatura do grok.com, como roda no Cursor CLI. Cada resposta usa uma pasta própria: os seus plugins, skills e hooks não carregam, e só as ferramentas do Tucano ficam à mão.
@@ -22,6 +23,7 @@ What changed in each version of Tucano. The format follows [Keep a Changelog](ht
### Changed
- **Claude agents reply faster, and long conversations spend less of the plan.** An agent now keeps its Claude Code session for each conversation and thread: its first reply sends the conversation, and the next ones only what's new since (other people's and agents' messages, and the task). The process stays open for five minutes after a reply (two at most at a time), so the next reply skips starting Claude Code. Measured with Haiku in a DM of about 16k tokens, the first words of a reply come about 0.9 s sooner (1.45 s instead of 2.33 s). Past the conversation's window, where each reply used to write the whole history to the prompt cache again (about 16,600 tokens), it now writes about 200. The session starts over when the agent's instructions, memories or tools change, a message it saw is edited, its last reply was stopped or failed, or after an hour idle. Thanks [@rjgdias](https://github.com/rjgdias)! ([#147](https://github.com/eduwxyz/tucano/pull/147))
+- **Replies get going while you type.** As you write, Tucano starts Claude Code for whoever will answer, so the reply doesn't wait the second it takes to start: in a channel of several agents, a process for the router, which then picks who answers in about 1.5 s instead of 2.8 s; in a DM whose agent's process closed after its five idle minutes, that process again, and the first words come about 0.6 s sooner. A process started for the router that goes unused (you mentioned someone, or didn't send) closes after two minutes. Thanks [@rjgdias](https://github.com/rjgdias)! ([#148](https://github.com/eduwxyz/tucano/pull/148))
- **A reply's steps say what each call did, as it runs.** A connector call shows what it asked (the SQL, a path, a search) from the moment it starts, and what came back once it ends, in a few words: the rows of a table or a SQL answer, the items of a list, the lines of a long text. A command shows whole, or as **Ran SQL** with the SQL taken out of it, and ends with what it printed or its exit code and the last lines of stderr; reading a skill says which skill and file. Tokens, passwords, keys and auth headers are masked on the screen and in the log (the agent still gets the real output). The steps list stays open while the agent works, unless closed, and folds when the reply is done. Thanks [@rjgdias](https://github.com/rjgdias)! ([#135](https://github.com/eduwxyz/tucano/pull/135))
- **An agent wakes another only when it needs to.** When an agent's reply mentions other agents, the router reads it first and hands the turn only to those asked to do something: a request, a question, a task, a decision. A thank-you, an "okay" or a credit wakes no one, and the decision shows as a step on the reply. In doubt, or if the router fails, everyone mentioned is called, as before; a mention in your own message still goes straight to the agent. Thanks [@eialanjones](https://github.com/eialanjones)! ([#139](https://github.com/eduwxyz/tucano/pull/139))
diff --git a/docs/spec-mvp.md b/docs/spec-mvp.md
index 0dd668c..1dbccda 100644
--- a/docs/spec-mvp.md
+++ b/docs/spec-mvp.md
@@ -88,6 +88,7 @@ Expostas pelo gateway, como qualquer outra. Todas as demais vêm de conectores M
- **Claude Code:** `claude -p --output-format stream-json --verbose --include-partial-messages`, com `--model`, `--system-prompt`, `--tools ""` (nenhuma ferramenta embutida: sem shell, sem arquivos), `--strict-mcp-config --mcp-config <gateway>`, `--setting-sources ""` (ignora hooks, plugins e configurações do usuário) e `--no-session-persistence` (fora as respostas de agente, que guardam sessão). Não usar `--bare`, que desliga o login por assinatura.
- **Sessão guardada (Claude Code):** a primeira resposta de um agente numa conversa ou thread começa uma sessão (`--session-id`) com a conversa inteira; as próximas a retomam (`--resume`) mandando só as mensagens novas desde a última resposta dele e a tarefa. O processo fica aberto 5 min depois de uma resposta (no máximo 2 esperando, ~140 MB cada), e a próxima é escrita nele; depois disso, a próxima abre outro processo e retoma do disco. O token do gateway fica o mesmo enquanto a sessão vive (entre respostas, as chamadas são recusadas). Começa outra sessão quando: o system prompt ou as ferramentas mudam (o `--resume` mantém o primeiro system prompt), uma mensagem que ela viu foi editada ou apagada, aparece uma mensagem no meio, a última resposta não terminou bem (parada, travada, limite, reserva), em "Tentar de novo", passou de ~60k tokens de contexto ou de 55 min parada (o cache dura 1 h), ou o app reabriu. Enquanto outro agente ainda escreve uma mensagem que ela veria pela metade, a resposta vai sem sessão. Os arquivos das sessões largadas são apagados.
+- **Começo antecipado (Claude Code):** enquanto a pessoa digita (no máximo um aviso a cada 20 s por conversa ou thread), o app já abre o Claude Code de quem vai responder, pra resposta não esperar o segundo que ele leva pra subir: numa DM (ou canal de um agente só), o processo da sessão guardada, se ele já tinha fechado; num canal de vários agentes, um processo do router. O do router só serve pra próxima decisão e fecha depois de 2 min sem uso (no máximo 2 esperando). Não abre nada antes da primeira resposta numa conversa, nem pra uma mensagem que abre thread nova, nem com os agentes pausados ou com o Jev de router.
- **Codex:** `codex exec --json --ephemeral --ignore-user-config --ignore-rules --skip-git-repo-check -s read-only -C <diretório vazio do run>`, com `-m`, `-c model_reasoning_effort=…`, skills e docs de projeto desligados, busca na web só com a Web ligada (§16), `--disable` em cada recurso embutido (shell, exec, plugins, apps, navegador, computer use, multiagente, imagens, memórias, hooks) e o gateway como único servidor MCP via `-c mcp_servers…` com `default_tools_approval_mode="approve"` (quem decide é o gateway).
- **O Codex roda com um home próprio do app** (`HOME` e `CODEX_HOME` numa pasta do Tucano). Medido com `codex debug prompt-input`: com o home do usuário, mesmo com `--ignore-user-config`, ele carrega o `~/.codex/AGENTS.md` e as skills do usuário, e num teste real usou o shell pra rodar uma ferramenta pessoal. Por isso o usuário faz um login próprio do Codex pro Tucano, pelo fluxo do próprio Codex, uma vez.
- O app **nunca** toca em credenciais: não lê, não guarda, não repassa token. O login acontece no fluxo da própria CLI (`claude auth login` / `codex login`), que o app abre num Terminal.
diff --git a/scripts/bench-warm-start.ts b/scripts/bench-warm-start.ts
new file mode 100644
index 0000000..8c84125
--- /dev/null
+++ b/scripts/bench-warm-start.ts
@@ -0,0 +1,96 @@
+// What starting Claude Code while the person types saves, through the real CLI (Haiku, the
+// person's own login):
+// router how long the router takes to pick who answers in a channel, started cold or while typing
+// DM a reply after the agent's kept process closed (five minutes idle), started cold or while typing
+// "While typing" means 3 s before the message is sent. Spends a little of the plan.
+// npx -y tsx scripts/bench-warm-start.ts [rounds, default 4]
+import { mkdirSync, mkdtempSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { randomUUID } from 'node:crypto'
+import { Db } from '../src/core/db'
+import { claudeEngine } from '../src/core/engines/claude'
+import { detectClaude } from '../src/core/engines/detect'
+import { fakeEngine } from '../src/core/engines/fake'
+import type { Engine } from '../src/core/engines/types'
+import { Gateway } from '../src/core/gateway'
+import { route, routerTurn } from '../src/core/router'
+import { Runner } from '../src/core/runs'
+import type { CoreEvent } from '../src/shared/rpc'
+import type { Agent, EngineId, Message, Settings } from '../src/shared/types'
+
+const rounds = Number(process.argv[2] ?? 4)
+const bin = process.env['CLAUDE_BIN'] || (await detectClaude()).path
+if (!bin) throw new Error('Claude Code not found')
+const wait = (ms: number) => new Promise((r) => setTimeout(r, ms))
+const median = (xs: number[]): number => [...xs].sort((a, b) => a - b)[Math.floor(xs.length / 2)]!
+const s = (ms: number): string => `${(ms / 1000).toFixed(2)} s`
+const agent = (id: string, name: string, role: string, instructions: string): Agent => ({ id, name, handle: id, role, color: 'sky', instructions, modelId: 'haiku', effort: 'low', limit: 100_000_000, used: 0, grants: {}, memory: 'auto', computer: false, archived: false })
+const lia = agent('lia', 'Lia', 'Dados', 'Você é a Lia, de engenharia de dados. Responda em uma frase curta.')
+const theo = agent('theo', 'Theo', 'Engenharia', 'Você é o Theo, de engenharia de software. Responda em uma frase curta.')
+
+// ---------- router ----------
+const work = join(tmpdir(), 'bench-warm')
+const routerDir = join(work, '_router')
+mkdirSync(routerDir, { recursive: true })
+const engine = claudeEngine(() => bin)
+const ask = (i: number): Message[] => [{ id: randomUUID(), conversationId: 'c', author: { kind: 'me' }, text: `A tabela vendas_diarias veio com ${1000 + i} linhas, o dobro de ontem. Alguém olha?`, at: Date.now() }]
+const routerMs: Record<'cold' | 'typing', number[]> = { cold: [], typing: [] }
+for (let i = 0; i < rounds; i++) {
+ for (const way of ['cold', 'typing'] as const) {
+ if (way === 'typing') {
+ engine.warm!(routerTurn('haiku', routerDir))
+ await wait(3000)
+ }
+ const r = await route({ engine, model: 'haiku', messages: ask(i), agents: [lia, theo], me: 'Gabriel', cwd: routerDir })
+ if ('error' in r) throw new Error(`router: ${r.error}`)
+ routerMs[way].push(r.ms)
+ await wait(1000)
+ }
+}
+
+// ---------- DM after the kept process closed ----------
+const ready = { installed: true, loggedIn: true }
+const settings = { workspaceName: 'Gabriel', context: '', engines: { claude: ready, codex: ready, opencode: ready, pi: ready, gemini: ready, cursor: ready }, routerModel: 'haiku', memoryModel: 'haiku', memoryLearning: 'approve', defaultLimit: 1000, approvalTtlHours: 24, theme: 'system', language: 'pt' } as Settings
+const db = new Db(join(mkdtempSync(join(tmpdir(), 'bench-db-')), 'bench.db'))
+db.saveAgent(lia)
+db.createConversation({ id: 'dm', kind: 'dm', name: 'lia', members: ['lia'] })
+const dmEngine = claudeEngine(() => bin)
+const engines: Record<EngineId, Engine> = { claude: dmEngine, codex: fakeEngine('codex'), opencode: fakeEngine('opencode'), pi: fakeEngine('pi'), gemini: fakeEngine('gemini'), cursor: fakeEngine('cursor') }
+const gateway = new Gateway()
+await gateway.start()
+let firstAt = 0
+const runner = new Runner({ db, engines, settings: async () => settings, emit: (e: CoreEvent) => void (e.type === 'delta' && !firstAt && (firstAt = Date.now())), workDir: work, gateway })
+const say = async (text: string): Promise<number> => {
+ firstAt = 0
+ const sent = Date.now()
+ await runner.send('dm', text)
+ await runner.idle()
+ const reply = db.messages().filter((m) => m.author.kind === 'agent').at(-1)!
+ if (reply.run?.status !== 'done') throw new Error(`DM: ${reply.run?.status} ${reply.run?.error ?? ''}`)
+ return firstAt - sent
+}
+await say('Oi Lia, tudo certo com o job de hoje?')
+const dmMs: Record<'cold' | 'typing', number[]> = { cold: [], typing: [] }
+for (let i = 0; i < rounds; i++) {
+ for (const way of ['cold', 'typing'] as const) {
+ dmEngine.dispose!() // its five minutes are over: the process closed
+ await wait(1000)
+ if (way === 'typing') {
+ await runner.typing('dm')
+ await wait(3000)
+ }
+ dmMs[way].push(await say(`E o job das ${10 + i}h, passou?`))
+ }
+}
+dmEngine.dispose!()
+engine.dispose!()
+await gateway.stop()
+
+console.log('| | cold | started while typing |')
+console.log('|---|---|---|')
+console.log(`| router picks who answers (median) | ${s(median(routerMs.cold))} | ${s(median(routerMs.typing))} |`)
+console.log(`| DM reply's first token after the process closed (median) | ${s(median(dmMs.cold))} | ${s(median(dmMs.typing))} |`)
+console.log(`\nrouter cold ${routerMs.cold.map(s).join(', ')} · typing ${routerMs.typing.map(s).join(', ')}`)
+console.log(`DM cold ${dmMs.cold.map(s).join(', ')} · typing ${dmMs.typing.map(s).join(', ')}`)
+process.exit(0)
diff --git a/src/core/engines/claude.ts b/src/core/engines/claude.ts
index 2a3d142..bb6268f 100644
--- a/src/core/engines/claude.ts
+++ b/src/core/engines/claude.ts
@@ -2,7 +2,7 @@ import { readdirSync, readFileSync, rmdirSync, rmSync } from 'node:fs'
import { homedir } from 'node:os'
import { dirname, join } from 'node:path'
import { lastLines, LiveCli, num, parseJson, runCli, type Parser } from './cli'
-import type { Engine, EngineEvent, Turn } from './types'
+import type { Engine, EngineEvent, Turn, TurnShape } from './types'
import { L } from '../../shared/lang'
export const GATEWAY_NAME = 'tucano'
@@ -189,6 +189,14 @@ const configDirOf = (account?: string): string => account || process.env['CLAUDE
const SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
export const KEEP_IDLE_MS = 5 * 60_000
export const MAX_IDLE = 2
+// A process started ahead of a turn with no session (the router's) waits this long, and only this
+// many wait: it's started while the person types, so the message comes soon or not at all.
+export const SPARE_MS = 2 * 60_000
+export const MAX_SPARES = 2
+
+// The flags of a turn with no session, reading its message as JSON: a process started ahead of
+// time has to read it that way, so the turn that uses it does too.
+const oneShot = (t: TurnShape, apiKeyHelper?: string): string[] => claudeArgs({ ...t, blocks: ['.'] }, apiKeyHelper)
interface Kept {
cli: LiveCli
@@ -200,6 +208,8 @@ interface Kept {
export function claudeEngine(bin: () => string | undefined, provider: () => Promise<Provider> = async () => userProvider()): Engine {
const kept = new Map<string, Kept>() // by session id
const files = new Map<string, string>() // session id → its file
+ const shapes = new Map<string, TurnShape>() // session id → the settings of its last turn, to start it again
+ const spares = new Map<string, { cli: LiveCli; timer: NodeJS.Timeout; at: number }>() // by flags and provider
const close = (id: string): void => {
const k = kept.get(id)
@@ -213,6 +223,24 @@ export function claudeEngine(bin: () => string | undefined, provider: () => Prom
const idle = [...kept].filter(([, k]) => !k.cli.busy).sort((a, b) => a[1].usedAt - b[1].usedAt)
for (const [id] of idle.slice(0, Math.max(0, idle.length - MAX_IDLE))) close(id)
}
+ // An open process waits for the next turn, for a while.
+ const park = (id: string, k: Kept): void => {
+ if (k.timer) clearTimeout(k.timer)
+ k.usedAt = Date.now()
+ k.timer = setTimeout(() => kept.get(id) === k && close(id), KEEP_IDLE_MS)
+ k.timer.unref()
+ trim()
+ }
+ // The same flags and provider as an open process: then it can take this turn.
+ const keyOf = (path: string, t: TurnShape, args: string[], p: Provider): string => JSON.stringify([path, t.cwd, args, p.env, t.account ?? ''])
+ const keptKey = (path: string, t: TurnShape, id: string, p: Provider): string => keyOf(path, t, claudeArgs({ ...t, session: { id, resume: true } }, p.apiKeyHelper), p)
+ const dropSpare = (key: string): void => {
+ const s = spares.get(key)
+ if (!s) return
+ spares.delete(key)
+ clearTimeout(s.timer)
+ s.cli.end()
+ }
return {
id: 'claude',
@@ -228,13 +256,27 @@ export function claudeEngine(bin: () => string | undefined, provider: () => Prom
const env = claudeEnv(p, turn.account)
const session = turn.session
if (!session) {
+ // A process started for it ahead of time takes the turn, then goes.
+ const key = keyOf(path, turn, oneShot(turn, p.apiKeyHelper), p)
+ const spare = spares.get(key)
+ if (spare?.cli.alive) {
+ spares.delete(key)
+ clearTimeout(spare.timer)
+ try {
+ yield* spare.cli.turn(claudeInput({ ...turn, blocks: turn.blocks?.length ? turn.blocks : [turn.prompt] }), claudeParser(), turn.signal)
+ } finally {
+ spare.cli.end()
+ }
+ return
+ }
yield* runCli({ bin: path, args: claudeArgs(turn, p.apiKeyHelper), stdin: claudeInput(turn), cwd: turn.cwd, signal: turn.signal, parser: claudeParser(), env })
return
}
+ const { prompt: _p, blocks: _b, images: _i, signal: _s, session: _n, ...shape } = turn
+ shapes.set(session.id, shape)
files.set(session.id, join(claudeProjectDir(configDirOf(turn.account), turn.cwd), `${session.id}.jsonl`))
- // The same flags and provider as the open process: then it can take this turn.
- const key = JSON.stringify([path, turn.cwd, claudeArgs({ ...turn, session: { id: session.id, resume: true } }, p.apiKeyHelper), p.env, turn.account ?? ''])
+ const key = keptKey(path, turn, session.id, p)
let k = kept.get(session.id)
if (k && (k.key !== key || !k.cli.alive || k.cli.busy || !session.resume)) {
close(session.id)
@@ -255,18 +297,54 @@ export function claudeEngine(bin: () => string | undefined, provider: () => Prom
} finally {
// Kept for the next reply only after a clean one; anything else and it goes.
if (kept.get(session.id) === current) {
- if (ended && session.keep && current.cli.alive && !turn.signal.aborted) {
- current.usedAt = Date.now()
- current.timer = setTimeout(() => kept.get(session.id) === current && close(session.id), KEEP_IDLE_MS)
- current.timer.unref()
- trim()
- } else close(session.id)
+ if (ended && session.keep && current.cli.alive && !turn.signal.aborted) park(session.id, current)
+ else close(session.id)
}
}
},
+ warm(turn) {
+ const path = bin()
+ if (!path) return
+ void provider().then((p) => {
+ const args = oneShot(turn, p.apiKeyHelper)
+ const key = keyOf(path, turn, args, p)
+ const had = spares.get(key)
+ if (had?.cli.alive) {
+ // Still waiting: it waits a while longer.
+ clearTimeout(had.timer)
+ had.timer = setTimeout(() => spares.get(key) === had && dropSpare(key), SPARE_MS)
+ had.timer.unref()
+ return
+ }
+ if (had) dropSpare(key)
+ const cli = new LiveCli({ bin: path, args, cwd: turn.cwd, env: claudeEnv(p, turn.account) })
+ const spare = { cli, at: Date.now(), timer: setTimeout(() => spares.get(key)?.cli === cli && dropSpare(key), SPARE_MS) }
+ spare.timer.unref()
+ spares.set(key, spare)
+ const oldest = [...spares].sort((a, b) => a[1].at - b[1].at)
+ for (const [k] of oldest.slice(0, Math.max(0, oldest.length - MAX_SPARES))) dropSpare(k)
+ })
+ },
+ rewarm(id) {
+ const path = bin()
+ const open = kept.get(id)
+ if (open?.cli.alive) {
+ if (!open.cli.busy) park(id, open) // open already: it waits a while longer
+ return
+ }
+ const shape = shapes.get(id)
+ if (!path || !shape) return
+ void provider().then((p) => {
+ if (kept.has(id) || !shapes.has(id)) return // a turn or a release got there first
+ const k: Kept = { cli: new LiveCli({ bin: path, args: claudeArgs({ ...shape, session: { id, resume: true } }, p.apiKeyHelper), cwd: shape.cwd, env: claudeEnv(p, shape.account) }), key: keptKey(path, shape, id, p), usedAt: Date.now() }
+ kept.set(id, k)
+ park(id, k)
+ })
+ },
release(id) {
const open = kept.get(id)?.cli
close(id)
+ shapes.delete(id)
const file = files.get(id)
files.delete(id)
if (!file) return
@@ -314,7 +392,9 @@ export function claudeEngine(bin: () => string | undefined, provider: () => Prom
},
dispose() {
for (const [, k] of kept) k.cli.killNow()
+ for (const [, s] of spares) s.cli.killNow()
kept.clear()
+ spares.clear()
}
}
}
diff --git a/src/core/engines/cli.ts b/src/core/engines/cli.ts
index 46483ec..bd190da 100644
--- a/src/core/engines/cli.ts
+++ b/src/core/engines/cli.ts
@@ -157,6 +157,13 @@ ${err.message}`
setTimeout(() => this.alive && this.child.kill('SIGKILL'), 5_000).unref()
}
+ // No more turns: it exits on its own once stdin closes (killed if it doesn't).
+ end(): void {
+ if (!this.alive) return
+ this.child.stdin!.end()
+ setTimeout(() => this.kill(), 10_000).unref()
+ }
+
// The app is quitting: nothing async, the process may be gone before taskkill would run.
killNow(): void {
if (this.alive) this.child.kill()
diff --git a/src/core/engines/fake.ts b/src/core/engines/fake.ts
index c18c7fb..c49302f 100644
--- a/src/core/engines/fake.ts
+++ b/src/core/engines/fake.ts
@@ -9,12 +9,20 @@ import type { Engine, EngineEvent, Turn } from './types'
// Deterministic engine for tests and screenshots. Never spawns anything, but
// behaves like the real ones where it matters: it routes when given the
// router prompt, and calls ask_user through the gateway when asked to ask.
-// `sessions`: it takes kept sessions like Claude Code does, and `released` gets the ones let go.
-export function fakeEngine(id: Engine['id'], opts: { delayMs?: number; reply?: (turn: Turn) => string; sessions?: boolean; released?: string[] } = {}): Engine {
+// `sessions`: it takes kept sessions like Claude Code does, and `released` gets the ones let go;
+// `warmed` gets what was started ahead ("warm <model>", "rewarm <session id>").
+export function fakeEngine(id: Engine['id'], opts: { delayMs?: number; reply?: (turn: Turn) => string; sessions?: boolean; released?: string[]; warmed?: string[] } = {}): Engine {
const delay = opts.delayMs ?? 25
return {
id,
- ...(opts.sessions ? { sessions: true, release: (sessionId: string) => void opts.released?.push(sessionId) } : {}),
+ ...(opts.sessions
+ ? {
+ sessions: true,
+ release: (sessionId: string) => void opts.released?.push(sessionId),
+ warm: (t: { model: string }) => void opts.warmed?.push(`warm ${t.model}`),
+ rewarm: (sessionId: string) => void opts.warmed?.push(`rewarm ${sessionId}`)
+ }
+ : {}),
async *run(turn): AsyncIterable<EngineEvent> {
const last = lastMessage(turn.prompt)
// "acabou o limite": Claude's usual login is out, to see an agent's reserve take over (another
diff --git a/src/core/engines/types.ts b/src/core/engines/types.ts
index 5de12d7..9d330de 100644
--- a/src/core/engines/types.ts
+++ b/src/core/engines/types.ts
@@ -44,6 +44,9 @@ export type EngineEvent =
| { type: 'done'; text: string; usage: Usage; context?: number }
| { type: 'error'; message: string }
+// A turn's settings without what it says: enough to start its process ahead of time.
+export type TurnShape = Omit<Turn, 'prompt' | 'blocks' | 'images' | 'signal' | 'session'>
+
export interface Engine {
id: EngineId
run(turn: Turn): AsyncIterable<EngineEvent>
@@ -51,4 +54,7 @@ export interface Engine {
release?(sessionId: string): void // a session that won't be resumed: its process and its file go
clean?(workDir: string, accountDirs?: string[]): void // sessions left from an earlier run of the app, by agents under workDir
dispose?(): void // the app is quitting: processes kept open close
+ // Starting the CLI takes about a second before any model call; these do it ahead of time.
+ warm?(turn: TurnShape): void // a process ready for the next turn with these settings and no session (the router's)
+ rewarm?(sessionId: string): void // the session's process again, if it closed, ready for its next turn
}
diff --git a/src/core/index.ts b/src/core/index.ts
index 9fe3a78..465cf4f 100644
--- a/src/core/index.ts
+++ b/src/core/index.ts
@@ -716,6 +716,10 @@ const api: Api = {
void runner.send(conversationId, text, images, threadId)
},
+ typing: ({ conversationId, threadId }) => {
+ void runner.typing(conversationId, threadId).catch((e: unknown) => console.warn('[typing]', e))
+ },
+
stopRun: ({ messageId }) => runner.stop(messageId),
retryRun: ({ messageId }) => runner.retry(messageId),
diff --git a/src/core/router.ts b/src/core/router.ts
index bddee82..d351c20 100644
--- a/src/core/router.ts
+++ b/src/core/router.ts
@@ -5,7 +5,7 @@
// "thanks, @bia" doesn't wake her up.
import { L } from '../shared/lang'
import type { Agent, Connector, Message } from '../shared/types'
-import type { Engine, Usage } from './engines/types'
+import type { Engine, TurnShape, Usage } from './engines/types'
import type { ThreadDigest } from './memory'
export const ROUTER_HISTORY = 10
@@ -155,6 +155,9 @@ export function parseRoute(text: string, agents: Agent[], max = MAX_ROUTED): { a
return { agents: picked.slice(0, max), reason: why }
}
+// The router's turn without its prompt: the same for every message, so its process can start ahead.
+export const routerTurn = (model: string, cwd: string): TurnShape => ({ model, effort: 'low', system: routerSystem(), cwd })
+
export async function route(opts: {
engine: Engine
model: string
@@ -170,14 +173,7 @@ export async function route(opts: {
let text = ''
let usage: Usage | undefined
let error: string | undefined
- for await (const ev of opts.engine.run({
- model: opts.model,
- effort: 'low',
- system: routerSystem(),
- prompt: routerPrompt(opts),
- cwd: opts.cwd,
- signal: AbortSignal.timeout(60_000)
- })) {
+ for await (const ev of opts.engine.run({ ...routerTurn(opts.model, opts.cwd), prompt: routerPrompt(opts), signal: AbortSignal.timeout(60_000) })) {
if (ev.type === 'text') text += ev.delta
else if (ev.type === 'done') {
text = ev.text || text
diff --git a/src/core/runs.ts b/src/core/runs.ts
index 88c60f7..f24a989 100644
--- a/src/core/runs.ts
+++ b/src/core/runs.ts
@@ -23,7 +23,7 @@ import { outOfQuota } from './engines/cli'
import { textResult, type Gateway, type Session, type ToolDef } from './gateway'
import { sessionConfig, Sessions, type SessionPlan, type Shown } from './sessions'
import { JEV_MODEL, routeJev } from './jev'
-import { ROUTER_HISTORY, route, screenHandoff } from './router'
+import { ROUTER_HISTORY, route, routerTurn, screenHandoff } from './router'
import { allowed, approvalHash, canonical } from './policy'
import { callError, contentText, errorReason } from './connectors'
import { commandStep, execSummary, keyArg, maskSecrets, resultSummary } from './step-text'
@@ -188,6 +188,35 @@ export class Runner {
this.paused = paused
}
+ // The person is writing here. What will answer gets its Claude Code started now, so the reply
+ // doesn't wait the second that takes: in a DM (or a channel of one agent), the agent's kept
+ // session, if its process closed; in a channel of several, the router. A first reply has no
+ // session to start yet, and a mention or a pending question skips the router: then the
+ // process started waits a couple of minutes and goes.
+ async typing(conversationId: string, threadId?: string): Promise<void> {
+ const { db, engines } = this.deps
+ const conv = db.conversation(conversationId)
+ if (!conv || this.paused) return
+ const members = this.members(conv)
+ const settings = await this.deps.settings()
+ const ready = (engine: EngineId): boolean => !!settings.engines[engine]?.installed && !!settings.engines[engine]?.loggedIn
+ if (conv.kind === 'dm' || members.length === 1) {
+ const agent = members[0]
+ // A message outside threads in a conversation that answers in them opens a new thread: nothing is kept there yet.
+ if (!agent || (conv.threads && !threadId)) return
+ const engine = modelInfo(agent.modelId)?.engine ?? 'claude'
+ const id = this.sessions.idOf(`${agent.id}:${conv.id}:${threadId ?? ''}`)
+ if (id && ready(engine)) engines[engine].rewarm?.(id)
+ return
+ }
+ if (members.length < 2 || settings.routerModel === JEV_MODEL) return
+ const model = usable(modelInfo(settings.routerModel)) ?? modelInfo('haiku')!
+ if (!ready(model.engine)) return
+ const cwd = join(this.deps.workDir, '_router')
+ mkdirSync(cwd, { recursive: true })
+ engines[model.engine].warm?.(routerTurn(model.id, cwd))
+ }
+
// Saves the human message and decides who answers it: mentioned agents
// first, then whoever asked a pending question, then the DM agent or the
// only agent in the channel, and otherwise the router. The answers, and
diff --git a/src/core/sessions.ts b/src/core/sessions.ts
index 4d70154..0196aac 100644
--- a/src/core/sessions.ts
+++ b/src/core/sessions.ts
@@ -115,6 +115,12 @@ export class Sessions {
return this.entries.has(key)
}
+ // The session a reply here would go on from, if it's still good for that (as far as time goes).
+ idOf(key: string): string | undefined {
+ const e = this.entries.get(key)
+ return e && this.now() - e.usedAt <= (this.opts.ttlMs ?? SESSION_TTL_MS) ? e.id : undefined
+ }
+
// What's new since the session's last reply, or undefined when it can't go on.
private since(e: Entry, input: { config: string; shown: Shown[] }): string[] | undefined {
if (e.config !== input.config) return undefined
diff --git a/src/renderer/src/components/Conversation.tsx b/src/renderer/src/components/Conversation.tsx
index 153e4c1..32f2142 100644
--- a/src/renderer/src/components/Conversation.tsx
+++ b/src/renderer/src/components/Conversation.tsx
@@ -30,6 +30,7 @@ import {
ACCESS_REQUEST,
countedTokens,
sendMessage,
+ typing,
setTab,
stopRun,
summarizeNow,
@@ -1708,6 +1709,7 @@ export function Composer({ conversationId, threadId, members, placeholder }: { c
onChange={(e) => {
setText(e.target.value)
updateMention(e.target.value, e.target.selectionStart)
+ if (e.target.value.trim()) typing(conversationId, threadId)
}}
onPaste={(e) => {
const files = [...e.clipboardData.files].filter((f) => f.type.startsWith('image/'))
diff --git a/src/renderer/src/store.ts b/src/renderer/src/store.ts
index ce0d74f..48d56aa 100644
--- a/src/renderer/src/store.ts
+++ b/src/renderer/src/store.ts
@@ -519,6 +519,16 @@ export const sendMessage = (conversationId: string, text: string, images: ImageU
if (text.trim() || images.length) void fire(L('Não consegui enviar a mensagem.', "I couldn't send the message."), 'sendMessage', { conversationId, text, ...(images.length ? { images } : {}), ...(threadId ? { threadId } : {}) })
}
+// The person is writing: the core gets what will answer ready. Once in a while per place, and
+// nobody needs to hear if it didn't work.
+const typed = new Map<string, number>()
+export const typing = (conversationId: string, threadId?: string): void => {
+ const where = `${conversationId}:${threadId ?? ''}`
+ if (Date.now() - (typed.get(where) ?? 0) < 20_000) return
+ typed.set(where, Date.now())
+ void call('typing', { conversationId, ...(threadId ? { threadId } : {}) }).catch(() => {})
+}
+
export const stopRun = (messageId: string): void => void fire(L('Não consegui parar a resposta.', "I couldn't stop the reply."), 'stopRun', { messageId })
export const retryRun = (messageId: string): void => void fire(L('Não consegui tentar de novo.', "I couldn't try again."), 'retryRun', { messageId })
export const continueRun = (messageId: string): void => void fire(L('Não consegui continuar a resposta.', "I couldn't continue the reply."), 'continueRun', { messageId })
diff --git a/src/shared/rpc.ts b/src/shared/rpc.ts
index 9d6effd..8c558ca 100644
--- a/src/shared/rpc.ts
+++ b/src/shared/rpc.ts
@@ -24,6 +24,7 @@ export interface Api {
snapshot(): Snapshot
artifactText(p: { id: string }): string | null // a text file an agent showed (Markdown, CSV, code), for the side panel
sendMessage(p: { conversationId: string; text: string; images?: ImageUpload[]; threadId?: string }): void // threadId: a reply in the thread that message started
+ typing(p: { conversationId: string; threadId?: string }): void // the person is writing there: what will answer gets ready
stopRun(p: { messageId: string }): void
retryRun(p: { messageId: string }): void
continueRun(p: { messageId: string }): void // a reply paused at its budget goes on
diff --git a/test/fixtures/fake-claude.mjs b/test/fixtures/fake-claude.mjs
index a78b406..8bddc50 100644
--- a/test/fixtures/fake-claude.mjs
+++ b/test/fixtures/fake-claude.mjs
@@ -1,6 +1,7 @@
// Stands in for Claude Code reading `--input-format stream-json`: one reply per message on stdin,
// for as long as stdin stays open. The reply says which process and which turn it was, and whether
-// it was started to resume a session. "demore" in the message: it takes 5 s before answering.
+// it was started to resume a session, and how long the process had been up. "demore" in the
+// message: it takes 5 s before answering. Plain text on stdin (no --input-format) is one message.
import { createInterface } from 'node:readline'
const resume = process.argv.includes('--resume')
@@ -9,13 +10,18 @@ const out = (ev) => process.stdout.write(`${JSON.stringify(ev)}\n`)
createInterface({ input: process.stdin }).on('line', async (line) => {
if (!line.trim()) return
- const msg = JSON.parse(line)
+ let msg
+ try {
+ msg = JSON.parse(line)
+ } catch {
+ msg = { message: { content: [{ text: line }] } }
+ }
const text = (msg.message?.content ?? []).map((c) => c.text ?? '').join('\n')
turn++
out({ type: 'system', subtype: 'init' })
out({ type: 'stream_event', event: { type: 'message_start', message: { usage: { input_tokens: 10, cache_read_input_tokens: 0 } } } })
if (/demore/.test(text)) await new Promise((r) => setTimeout(r, 5000))
- const reply = `pid=${process.pid} turn=${turn} resume=${resume}`
+ const reply = `pid=${process.pid} turn=${turn} resume=${resume} up=${Math.round(process.uptime() * 1000)}`
out({ type: 'stream_event', event: { type: 'content_block_delta', delta: { type: 'text_delta', text: reply } } })
out({ type: 'result', subtype: 'success', result: reply, usage: { input_tokens: 10, output_tokens: 5 } })
})
diff --git a/test/warm-start.test.ts b/test/warm-start.test.ts
new file mode 100644
index 0000000..a0fb96b
--- /dev/null
+++ b/test/warm-start.test.ts
@@ -0,0 +1,127 @@
+import { chmodSync, copyFileSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { beforeEach, describe, expect, it } from 'vitest'
+import type { Agent, EngineId, Settings } from '../src/shared/types'
+import { Db } from '../src/core/db'
+import { claudeEngine } from '../src/core/engines/claude'
+import { fakeEngine } from '../src/core/engines/fake'
+import type { Engine, EngineEvent, Turn } from '../src/core/engines/types'
+import { routerTurn } from '../src/core/router'
+import { Runner } from '../src/core/runs'
+
+const ready = { installed: true, loggedIn: true }
+const isWin = process.platform === 'win32'
+
+function agent(id: string, handle: string, extra: Partial<Agent> = {}): Agent {
+ return { id, name: handle, handle, role: '', color: 'sky', instructions: '', modelId: 'sonnet', effort: 'low', limit: 1_000_000, used: 0, grants: {}, memory: 'auto', computer: false, archived: false, ...extra }
+}
+
+describe('while the person types', () => {
+ let db: Db
+ let settings: Settings
+ let warmed: string[]
+ let turns: Turn[]
+
+ function runner(): Runner {
+ const fake = fakeEngine('claude', { delayMs: 1, sessions: true, warmed })
+ const claude: Engine = { ...fake, run: (t) => (turns.push(t), fake.run(t)) }
+ const engines: Record<EngineId, Engine> = { claude, codex: fakeEngine('codex', { delayMs: 1 }), opencode: fakeEngine('opencode'), pi: fakeEngine('pi'), gemini: fakeEngine('gemini'), cursor: fakeEngine('cursor'), grok: fakeEngine('grok') }
+ return new Runner({ db, engines, settings: async () => settings, emit: () => {}, workDir: mkdtempSync(join(tmpdir(), 'ws-work-')) })
+ }
+
+ beforeEach(() => {
+ db = new Db(join(mkdtempSync(join(tmpdir(), 'ws-db-')), 'test.db'))
+ warmed = []
+ turns = []
+ settings = { workspaceName: 'Edu', context: '', engines: { claude: ready, codex: ready, opencode: ready, pi: ready, gemini: ready, cursor: ready, grok: ready }, routerModel: 'haiku', memoryModel: 'haiku', memoryLearning: 'approve', defaultLimit: 1000, approvalTtlHours: 24, theme: 'system', language: 'pt' }
+ db.saveAgent(agent('a1', 'lia'))
+ db.saveAgent(agent('a2', 'theo'))
+ db.createConversation({ id: 'd1', kind: 'dm', name: 'a1', members: ['a1'] })
+ db.createConversation({ id: 'c1', kind: 'channel', name: 'engenharia', members: ['a1', 'a2'] })
+ db.createConversation({ id: 'c2', kind: 'channel', name: 'dados', members: ['a1'], threads: true })
+ })
+
+ it('in a DM, starts the agent’s kept session again, once it has one', async () => {
+ const r = runner()
+ await r.typing('d1')
+ expect(warmed).toEqual([]) // nothing kept before the first reply
+ await r.send('d1', 'oi')
+ await r.idle()
+ await r.typing('d1')
+ expect(warmed).toEqual([`rewarm ${turns[0]!.session!.id}`])
+ })
+
+ it('in a channel of several agents, starts the router', async () => {
+ const r = runner()
+ await r.typing('c1')
+ expect(warmed).toEqual(['warm haiku'])
+ })
+
+ it('starts nothing for a new thread, when paused, or with the router’s harness signed out', async () => {
+ const r = runner()
+ await r.typing('c2') // answers in threads: a new message opens a new one
+ r.setPaused(true)
+ await r.typing('c1')
+ r.setPaused(false)
+ settings.engines.claude = { installed: true, loggedIn: false }
+ await r.typing('c1')
+ expect(warmed).toEqual([])
+ })
+})
+
+describe('a Claude Code process started ahead', () => {
+ let dir: string
+ let bin: string
+
+ beforeEach(() => {
+ dir = mkdtempSync(join(tmpdir(), 'ws-cli-'))
+ const script = join(dir, 'fake-claude.mjs')
+ copyFileSync(join(__dirname, 'fixtures', 'fake-claude.mjs'), script)
+ if (isWin) {
+ bin = join(dir, 'claude.cmd')
+ writeFileSync(bin, '@"%~dp0\\fake-claude.mjs" %*\r\n')
+ } else {
+ bin = join(dir, 'claude')
+ writeFileSync(bin, `#!/usr/bin/env node\n${readFileSync(script, 'utf8')}`)
+ chmodSync(bin, 0o755)
+ }
+ mkdirSync(join(dir, 'config'))
+ })
+
+ const reply = async (events: AsyncIterable<EngineEvent>): Promise<string> => {
+ let text = ''
+ for await (const e of events) if (e.type === 'done') text = e.text
+ else if (e.type === 'error') throw new Error(e.message)
+ return text
+ }
+ const upOf = (text: string): number => Number(/up=(\d+)/.exec(text)?.[1])
+ const wait = (ms: number) => new Promise((r) => setTimeout(r, ms))
+
+ it('takes the router’s next turn, already up, and only that one', async () => {
+ const engine = claudeEngine(() => bin, async () => ({ env: {} }))
+ const turn = (): Turn => ({ ...routerTurn('haiku', dir), prompt: 'quem responde?', signal: new AbortController().signal })
+ engine.warm!(routerTurn('haiku', dir))
+ await wait(1500)
+ const warm = await reply(engine.run(turn()))
+ expect(upOf(warm)).toBeGreaterThanOrEqual(1000)
+ const cold = await reply(engine.run(turn())) // the spare is used up: a new process, as before
+ expect(cold.split(' ')[0]).not.toBe(warm.split(' ')[0])
+ expect(upOf(cold)).toBeLessThan(upOf(warm)) // started for this turn (a busy machine may take a second to start it)
+ engine.dispose!()
+ }, 30_000)
+
+ it('starts a kept session’s process again, resuming it, for its next turn', async () => {
+ const engine = claudeEngine(() => bin, async () => ({ env: {} }))
+ const turn = (resume: boolean): Turn => ({ model: 'haiku', effort: 'low', system: 'x', prompt: 'oi', cwd: dir, account: join(dir, 'config'), signal: new AbortController().signal, session: { id: 's1', resume, keep: true } })
+ await reply(engine.run(turn(false)))
+ engine.dispose!() // as if it had closed after its five minutes
+ engine.rewarm!('s1')
+ await wait(1500)
+ const next = await reply(engine.run(turn(true)))
+ expect(next).toMatch(/turn=1 resume=true/)
+ expect(upOf(next)).toBeGreaterThanOrEqual(1000)
+ engine.rewarm!('unknown') // never seen: nothing to start
+ engine.dispose!()
+ }, 30_000)
+})