Tokidoki
An alive agent that grows with the user.
- days
- 6
- crates
- 16
- lines of Rust
- ~24k
- tests
- 190
A personal agent with two properties most chat apps lack: work survives a crash and resumes where it stopped, and it learns from conversations without being told to. It is one Rust library, input in and output out. A macOS app and a terminal chat are just two things that embed it.
At a glance
- What
- A personal assistant with durable execution and automatic long-term memory
- Stack
- Rust (16 crates), SQLite with FTS5, Tauri v2, Svelte 5
- Size
- About 24,000 lines of Rust, 3,700 of front end
- Tests
- 190 in the workspace, plus scripted runs of the app
The shape of the system
Sixteen crates, each with one job, and dependencies only point down. state knows nothing of
the engine, memory knows nothing of the agent, and the protocol crate depends on nothing at
all.
embedders app (Tauri + Svelte) · terminal chat
──────── interface: the protocol, as plain types ────────
library tokidoki wires it all together, decides who may speak
control router which thread an input belongs to
agent runs, tool calls, workers, idle time
durability engine durable tasks on one line of atomic commits
state threads as append-only logs of checkpoints
memory attention cheap triggers: no model calls, no I/O
experience recall, retain, refine, and dreaming
memory semantic · procedural · episodic, versioned
notes the user's own words, never rewritten
models context prompt, tools and history, vendor-neutral
inference one provider type, many wire formats
jev fast structured decisions
edges exa (web search) · voice (on device) · telemetry
The library never decides where inputs come from or where outputs go. Whatever embeds it calls
send, follows events, and reads threads, history and memories, all as interface
types. The TypeScript protocol the app uses is generated from those Rust types, so the two
can't drift.
The engine: durable tasks
Everything the agent does runs as a task on one engine: a state machine whose every step is a commit.
pub trait Task {
type Input; type State; type Output;
const NAME: &'static str;
const VERSION: u32 = 1;
fn initial(&self, input: &Self::Input) -> Self::State;
async fn run(&self, rt: Runtime<Self>) -> Result<()>;
async fn abort(&self, rt: Runtime<Self>) -> Result<()>;
fn on_fault(&self, tx: &mut Tx, task: &TaskRecord) -> Result<()>;
}
A phase ends by committing what comes next: a new checkpoint to continue from, a wait on other tasks, a sleep until a stored deadline, or an outcome. Thread checkpoints and task states go in the same SQLite transaction, or neither does, and nothing is shown to the user before it is stored.
Opening the engine after a crash is the recovery path, not a special case:
- A task that was running is pending again, with its restarts counted, and repeats its phase from the last checkpoint. After too many restarts in a row it is faulted, so work that keeps crashing the process ends.
- Waiting and sleeping tasks stay parked. A sleep's deadline is in the database, so an hour-long timer survives a reboot.
- Tasks own tasks. A task waits on others without running any code. Aborts run bottom-up: owned work first, then the owner's own abort handler.
- Background work is a boundary. It neither holds its owner up nor dies with it, which is what lets a research worker outlive the tool call that started it.
- A faulted task's cleanup (
on_fault) runs inside the commit that ends it, so no crash can come between the two. - Each task stores its version. A stored task of an older version is orphaned rather than run with a state shape it no longer matches.
State: the log is the truth
A thread is an append-only log of checkpoints: a message, a tool result, a compaction, a turn boundary. What the model sees is never stored. It is rebuilt from the log every time.
Dropping something from the context to save space therefore never loses it from history, so compaction is safe to get wrong. A thread also never saves itself: the engine saves its new checkpoints in the same commit as the task step that produced them, and rolls the thread back in memory if that commit fails.
The life of one message
- Route. The router files the input into a thread: an existing one, or a new one.
- Queue. If that thread is idle, the input becomes the user's message and a run starts. If it's busy, the input waits in the thread's inbox: a follow-up starts the next run once this one answers, and a steer joins the current run after its tool round.
- Generate. A run is a
Generationtask. It places any steers, asks the model, runs each requested tool as its ownToolCalltask and waits on them, and repeats until the model answers without tools. - Call tools durably. A tool call's intent is committed before it runs. If a crash cuts it off, only a tool marked safe to replay runs again. Any other gives the model an "interrupted" result instead of doing something twice.
- Retry honestly. A model request cut off by a crash counts as a failed attempt and retries after a backoff that is itself stored. It is never silently sent again.
- Stream without storing. The reply streams to the screen as it is written but is committed whole, once finished. Nothing is stored by being shown, so a slow watcher or a retry loses nothing.
- Finish. The run settles its inputs, starts the next queued follow-up, and starts the thread's idle task.
A run that dies without settling its inputs is noticed by the next submission to that thread: its inputs are marked unanswered, its open tool calls get error results, and the queue moves on.
Routing: decide cheaply first
Every input has to be filed before anything else sees it, and a model call on every message would be slow and expensive. The router works in tiers and stops at the first one that is sure:
| Tier | How it decides |
|---|---|
| Cues | Pattern rules: "new topic", "yes, do it", or no threads yet |
| Classifier | Jev, a structured decision in a few hundred milliseconds, taken only when confident |
| Continuity | The user is in a hot thread and the input shares words with it |
| Search | Keyword coverage of thread cards, decisive by a margin, or two searches agreeing |
| Nothing in common | No shared words and nothing that looks back, so it's a new thread |
| Model | A fast model, only for the inputs still in doubt |
The router reads cards, not threads: short summaries of the ten most recently active threads, plus any older one the user names. Only the chosen thread is loaded in full. The user sees one continuous conversation, while each subject still gets a focused context underneath.
Memory: three crates, one rule
The memory work is split so the part that runs on every turn is nearly free:
- Attention finds triggers ("this may need a recall", "this may be worth saving"). It runs before every model call, so it is plain synchronous code: patterns, structure, counts. It never reads or writes memory, and never decides.
- Experience decides what to do about a trigger and carries it out. With Jev, every message gets one request asking two questions in parallel: which candidate memories would help answer it, and whether it says something lasting. Jev decides; the model writes. When a memory is worth keeping, the model sees the related memories too, so it corrects one instead of adding a duplicate.
- Memory is passive storage in three kinds: semantic (what is true), procedural (how things are done) and episodic (what happened, pointing back at the stretch of thread it summarises). Semantic and procedural items are versioned, so a correction adds a version rather than erasing the past.
Jev only ever sees candidates: up to 30 memories from keyword search and recency, never the whole store. The rule over all of it is that memory work may never break a turn. A decision that fails becomes "ignore", with the reason recorded.
Notes are kept apart on purpose. Memory is the agent's and gets tidied. A note is the user's, saved only when asked and changed only by them. A note saved from a tool call records that call, so a crash-and-replay returns the same note instead of a second one.
Idle time: dreaming and settling
Each finished run starts an idle task on its thread, and the next run aborts it. The idle task sleeps durably, then dreams: it summarises episodes, tidies memory and compacts the context. After a longer idle it settles the thread, resetting its context to a resume card that holds just enough to pick it up again. History keeps everything. Closing the app's window leaves it running, so idle conversations still dream.
Workers
Long work such as web research is handed to a worker so the main model can stay with the user. A worker is an ordinary run on a hidden thread of its own, with its own prompt and tools and no memory work, so it is exactly as durable as any other run. It starts once per tool call, however often a crash makes that call replay.
When it finishes, its report goes back to the thread that asked. If the user is still there, or Jev judges the report worth interrupting them for, it is delivered now. Otherwise it is held, written into the thread's context without starting a turn, for when they come back.
Inference: one context, many wire formats
context holds the system prompt, tools and history in a vendor-neutral shape. One
Provider<A> does the HTTP for every vendor, with A as the API format: OpenAI-style,
Anthropic, or AnyApi when the vendor is the user's setting at runtime rather than the
program's type. Adding a provider is a wire format, not a new code path.
Built to be inspected
Every model call (the exact body sent and the raw reply), routing decision, memory decision and task change is a structured event tagged with its thread and task, recorded to a searchable journal beside the database. A moment described in words ("it forgot I'm vegetarian") can be found and laid out step by step, and its model calls sent again. The journal can also stand in for the provider, so a whole run can be replayed without calling a model.
Decisions worth defending
- Reports are not the user. A worker's report travels as a message. The first version learned from reports as if the user had said them, and could lose a held report when its thread settled. The fix was one shared definition of a report, used everywhere the code asks what the user said. The shortcut was simple to write and wrong in five places.
- The small model is optional. Jev makes routing and memory decisions fast and cheap, but its version is pinned, every use has a fallback, and a stalled request times out after five seconds.
- Open asleep. Without a key the app still opens: threads, history and memory can be read and memory edited, unfinished work waits, and only sending is refused.
- Voice stays on the device. Speech to text and text to speech run locally, and long recordings are split at the quietest point near each boundary.
What it suggests
- Durability is cheaper designed in than added. Deciding what a crash means for each kind of work cost little on day two and would be a rewrite on day twenty.
- Recovery should be the normal path. Opening the engine is how crashes are handled, so the crash path runs every launch and stays tested.
- A cheap decision layer changes what is affordable. Judging every message for recall and retention only makes sense when the judge is fast, cheap and optional.