// Runner: a small daemon on one of the owner's computers that executes the // computer tool calls the service assigns to it. // // Outbound only: the runner long-polls the service over HTTPS for jobs // (ai.languid.runner.poll) and posts each result back (ai.languid.runner.result), // so it needs no open port, no public address and no VPN. It authenticates with // the token it got when it joined; the service keeps only a hash of it. import crypto from 'node:crypto' import fs from 'node:fs/promises' import os from 'node:os' import path from 'node:path' import { xrpc } from './atproto/xrpc.ts' import { discoverSkills, type InstalledSkill } from './skills.ts' import { JobManager, JobYielded, jobBashOperations, type JobReport } from './jobs.ts' import { PI_TOOLS, type PiToolName } from './tools.ts' export interface RunRequest { tool: string args: Record /** Working directory for the call; the runner falls back to home if it doesn't exist here. */ cwd?: string /** Which agent and call this is for; a background job echoes it back when it finishes. */ origin?: Record } export interface RunResponse { output: string isError: boolean content?: ToolContent[] details?: unknown } /** A computer tool's output: text for records, plus pi's content and details for pi's renderers. */ export interface ToolContent { type: 'text' | 'image' text?: string data?: string mimeType?: string } /** The directory tools run in: the session's working directory if it exists here, else home. */ async function workingDir(cwd?: string) { if (cwd && path.isAbsolute(cwd) && (await fs.stat(cwd).catch(() => null))?.isDirectory()) return cwd return os.homedir() } let defaultJobs: JobManager | undefined const jobManager = () => (defaultJobs ??= new JobManager()) /** * Run one of pi's built-in tools on this computer, with pi's own implementation. bash runs its * command as a job (see jobs.ts): a command still running after a few seconds keeps running in * the background, and the call returns its job id. */ export async function execute( { tool, args, cwd, origin }: RunRequest, signal?: AbortSignal, jobs: JobManager = jobManager(), ): Promise { if (tool === 'job_stop') return stopJob(jobs, String(args?.job ?? '')) const t = PI_TOOLS[tool as PiToolName] if (!t) return { output: `runner does not implement tool "${tool}"`, isError: true } try { const { computer: _c, machine: _m, ...params } = args ?? {} const dir = await workingDir(cwd) const def = tool === 'bash' ? PI_TOOLS.bash.create(dir, { operations: jobBashOperations(jobs, { origin }) }) : t.create(dir) const r: any = await def.execute(crypto.randomUUID(), params as any, signal, undefined, undefined as any) const content = (r.content ?? []) as ToolContent[] return { output: textOf(content), isError: !!r.isError, content, details: r.details } } catch (e: any) { if (e instanceof JobYielded) return yielded(jobs, e) // pi's tools throw for failures (missing file, aborted command); the message is the output. return { output: e?.message ?? String(e), isError: true } } } /** The result of a bash call whose command is still running. */ function yielded(jobs: JobManager, e: JobYielded): RunResponse { const logPath = jobs.logPath(e.job.id) const shown = e.output.length > 4_000 ? `[...]\n${e.output.slice(-4_000)}` : e.output const text = (shown.trim() ? `${shown.trimEnd()}\n\n` : '') + `Still running as background job ${e.job.id}. Its full output is going to ${logPath} (read it with the read tool). ` + `It appears in until it finishes; stop it with job_stop.` return { output: text, isError: false, content: [{ type: 'text', text }], details: { job: { id: e.job.id, command: e.job.command, logPath, startedAt: e.job.startedAt } }, } } function stopJob(jobs: JobManager, id: string): RunResponse { const before = jobs.check(id) if (!before) return { output: `no job ${id} on this computer`, isError: true } if (before.status !== 'running') { jobs.update(id, { reported: true }) return { output: `job ${id} already ${describeEnd(before.status, before.exitCode)}`, isError: false, details: { job: jobs.report(before) } } } const after = jobs.kill(id)! // A deliberate stop needs no completion report. jobs.update(id, { reported: true }) return { output: `stopped job ${id}`, isError: false, details: { job: jobs.report(after) } } } export const describeEnd = (status: string, exitCode?: number) => status === 'exited' ? `exited with code ${exitCode}` : status === 'timeout' ? 'timed out' : status === 'lost' ? 'was lost (its process vanished)' : 'was stopped' const textOf = (content: ToolContent[]) => content.map((c) => (c.type === 'text' ? c.text : '[image]')).join('\n') /** A job as the service hands it out. */ export interface Job extends RunRequest { id: string } export interface PollResponse { jobs: Job[] /** Jobs the service gave up on (timed out, or the caller went away): stop them. */ cancel: string[] } export class RunnerRevoked extends Error {} export interface ConnectOptions { serviceUrl: string token: string log?: (s: string) => void /** Stops the loop (tests, shutdown). */ signal?: AbortSignal /** Where background jobs live (tests); default /jobs. */ jobs?: JobManager /** How often to look for finished background jobs to report. */ jobCheckMs?: number /** Return after this long with no tool calls and no background jobs running (a cloud sandbox that sleeps when idle). */ idleExitMs?: number } /** Join a service with a one-time invite code (`languid computer invite`). Returns this computer's runner token. */ export function register(serviceUrl: string, code: string, name?: string) { return xrpc<{ name: string; token: string; agents: string[] }>(serviceUrl, 'ai.languid.computer.register', { body: { code, name, hostname: os.hostname(), os: `${os.type()} ${os.release()} ${os.arch()}`, capabilities: ['posix-shell', `os:${process.platform}`], }, }) } /** * Serve jobs from the service until stopped. Network failures are retried with backoff; a rejected * token (the computer was removed) ends the loop with RunnerRevoked. */ export async function connect(opts: ConnectOptions): Promise { const log = opts.log ?? console.log const headers = { authorization: `Bearer ${opts.token}`, 'content-type': 'application/json' } // A fresh id per process: when it changes, the service knows jobs given to the previous process are lost. const session = crypto.randomUUID() const running = new Map() const jobs = opts.jobs ?? jobManager() const post = (nsid: string, body: unknown, timeoutMs: number) => fetch(new URL(`/xrpc/${nsid}`, opts.serviceUrl), { method: 'POST', headers, body: JSON.stringify(body), signal: opts.signal ? AbortSignal.any([opts.signal, AbortSignal.timeout(timeoutMs)]) : AbortSignal.timeout(timeoutMs), }) let lastActive = Date.now() const background = jobs const idle = () => !!opts.idleExitMs && running.size === 0 && background.running().length === 0 && Date.now() - lastActive > opts.idleExitMs const run = async (job: Job) => { lastActive = Date.now() const ctl = new AbortController() running.set(job.id, ctl) const summary = job.tool === 'bash' ? job.args.command : (job.args.path ?? job.args.pattern) log(`→ ${job.tool} ${summary ?? ''}`) const result = await execute(job, ctl.signal, jobs) log(`← ${job.tool} ${ctl.signal.aborted ? 'cancelled' : result.isError ? 'error' : 'ok'}`) // Keep trying to deliver the result; the job stays "running" until the service has it. for (let attempt = 0; attempt < 10 && !opts.signal?.aborted; attempt++) { try { const res = await post('ai.languid.runner.result', { id: job.id, ...result }, 30_000) if (res.ok || res.status === 404) break // 404: the service already gave up on this job } catch {} await sleep(Math.min(1000 * 2 ** attempt, 30_000), opts.signal) } running.delete(job.id) lastActive = Date.now() } // Report background jobs as they finish, including ones that finished while the runner was down. const reportJobs = async () => { for (const meta of jobs.unreported()) { const report: JobReport = jobs.report(meta) try { const res = await post('ai.languid.runner.jobDone', report, 30_000) // 404: the service doesn't know the job (the call that started it was never recorded). if (res.ok || res.status === 404) { jobs.update(meta.id, { reported: true }) log(`job ${meta.id} ${describeEnd(meta.status, meta.exitCode)}`) } } catch {} // the service is unreachable: try again on the next pass } } let stopped = false void (async () => { while (!opts.signal?.aborted && !stopped) { await reportJobs().catch((e) => log(`job reports failed: ${e?.message ?? e}`)) try { jobs.prune() } catch {} await sleep(opts.jobCheckMs ?? 2_000, opts.signal) } })() let backoff = 1000 let connected = false let skills: InstalledSkill[] = [] let skillsCheckedAt = 0 while (!opts.signal?.aborted) { if (background.running().length) lastActive = Date.now() if (idle()) { log(`idle for ${Math.round(opts.idleExitMs! / 60_000)} min; stopping`) stopped = true return } let res: Response try { if (Date.now() - skillsCheckedAt > 60_000) { skills = await discoverSkills() skillsCheckedAt = Date.now() } res = await post('ai.languid.runner.poll', { session, running: [...running.keys()], skills }, 60_000) } catch (e: any) { if (opts.signal?.aborted) break if (connected) log(`lost the service (${e.cause?.code ?? e.message}); retrying`) connected = false await sleep(backoff, opts.signal) backoff = Math.min(backoff * 2, 30_000) continue } if (res.status === 401) throw new RunnerRevoked('the service no longer accepts this runner\'s token; join again with a new invite') if (!res.ok) { log(`poll failed: HTTP ${res.status}; retrying`) await sleep(backoff, opts.signal) backoff = Math.min(backoff * 2, 30_000) continue } if (!connected) log(`connected to ${opts.serviceUrl}`) connected = true backoff = 1000 const { jobs, cancel } = (await res.json()) as PollResponse for (const id of cancel) running.get(id)?.abort() for (const job of jobs) if (!running.has(job.id)) void run(job) } } function sleep(ms: number, signal?: AbortSignal) { return new Promise((resolve) => { const t = setTimeout(resolve, ms) signal?.addEventListener('abort', () => (clearTimeout(t), resolve()), { once: true }) }) }