How I built background jobs for my desktop AI companion: 32 at once, 64 KB of output, and the beginning you can never read back
Ankita AI is a desktop AI assistant by Krish Verma, and the moment it could run a command, it had a problem no chat UI can hide: a command that takes ten minutes cannot hold the conversation for ten minutes, and a dev server never exits at all.
So run_command does not wait for the process. It waits for one second, hands back a job ID, and lets the conversation move on - the job keeps running, and four small tools (job_status, job_wait, job_input, job_stop) are the whole interface to it afterwards.
This is how that interface works, and the one place where it quietly loses the output you most need.
Ankita is open source at https://github.com/akyourowngames/A.N.K.I.T.A, and the site is https://ankita-sable.vercel.app.
The yield, not the exit
run_command in tools/process/run-command.mjs spawns the process and then waits at most yield_ms - 1,000 ms by default, 10,000 ms maximum - before returning.
If the process finished inside the yield, you get the exit code and the output inline. If it did not, you get a job ID and a snapshot, plus the instruction to use the job tools and not to wait for servers to exit. background: true skips the wait entirely.
The caps are the design. At most 32 jobs may run at once - the 33rd gets "32 jobs are already running. Stop one first." The jobs map prunes finished jobs once it passes 100 entries. An optional timeout_ms is an execution deadline, capped at 600,000 ms (10 minutes); zero means no automatic kill.
Every run is written to the append-only audit log with the command, working directory, exit code or signal, and duration - a background job that fails at 3 a.m. still leaves a receipt.
The spawn is platform-split on purpose. On Windows the command runs in PowerShell - pwsh first, powershell.exe as fallback - with $ErrorActionPreference='Stop' by default, and the native spawn happens in a worker thread (tools/shared/_job-launcher.mjs) so a slow process lookup never blocks the assistant's event loop. On POSIX it runs under /bin/sh with detached: true, so the child owns its own process group.
That group is what makes Stop possible, and what makes it imperfect - more on that below.
Output is a ring with absolute cursors
A job's output lives in JobOutput (tools/shared/_jobs.mjs), a bounded buffer with a default of 65,536 bytes and a maximum of 4 MiB.
job_status reads it incrementally:
since_offsetreplays from a byte cursortailreturns the last N lines (up to 1,000)- every read advances a stored
readOffset, so the next poll resumes where the last one stopped
The cursors are absolute - they count bytes that are no longer retained. When output rolls over the buffer, the read reports dropped: how many bytes between the offset you asked for and the oldest byte still held.
The test pins this exactly: 3,000 bytes into a 1,024-byte buffer, a read from offset 0 starts at 1,979 and reports dropped: 1979.
Reads are also UTF-8 safe: a multi-byte character split across two data events is never returned half-decoded.
Stdin is bounded the other way: job_input sends at most 65,536 bytes per call with a 1-second write deadline, and refuses a closed pipe. job_wait waits at most 10 seconds, then returns whatever output exists with the job still alive.
Stop kills the tree - usually
job_stop calls killTree and then waits up to 5 seconds for the exit. If the process is still there after that, it says so honestly: "stop requested for job X, still exiting."
On POSIX, killTree sends SIGKILL to the whole process group (process.kill(-pid, 'SIGKILL')) and then to the child itself. On Windows it runs taskkill /PID <pid> /T /F, with a 5-second bound on taskkill itself; if that fails, it falls back to walking the process table for tracked descendants.
The tracking is careful about the classic trap: PIDs get reused, so descendants are matched by creation-time identity, not PID alone, and if the root process's identity has changed since launch, cleanup refuses outright rather than risk killing a stranger's process.
The Windows launcher has one more guard: terminateTree() waits for the launch to resolve before killing, so a Stop that arrives while the process is still starting cannot orphan a child that has no PID yet.
The flaw I already found: the beginning is kept and never shown
Here is the part I would change. The output buffer is not a simple tail - BoundedOutput in tools/shared/_shared.mjs keeps the first half and the last half. When a foreground job finishes, toString() returns both with a marker between them: [output truncated: N bytes total]. The beginning survives.
But job_status never serves the beginning. Its read path works only from recent - the newest window - and reports the rest as dropped, a number. For a background job, the first 32 KB is retained in memory and unreachable through any tool: the model learns exactly how many bytes it lost and nothing about what they said.
And the bytes a long job loses are not random ones. A build that fails after an hour of logging prints the fatal error near the start; a dev server that crashes on boot prints the reason in its first lines. The ring keeps the newest chatter and drops the diagnosis, then tells you the byte count of the diagnosis.
The same shape of gap exists in Stop. SIGKILL to a process group kills the group - unless a grandchild called setsid() and detached itself, in which case it survives and keeps the port. On Windows, the fallback walk can miss a process born after its snapshot.
So the design question I keep coming back to: should job_status serve the same head-plus-tail view the foreground path already builds - the first 32 KB, the marker, the last 32 KB - or should output past the cap spill to a file on disk that the job tools can read back in full?
The head-plus-tail fix is a day's work and keeps everything in memory; the disk spill is the honest one for a job that logs for hours, but it turns a bounded buffer into a file the assistant has to clean up, permission, and eventually rotate.
Tell me I'm wrong: is a truncated view with a byte count good enough, or does a background job owe you its whole log?
- Krish Verma, creator of Ankita AI
Comments
No comments yet. Start the discussion.