Skip to content

Persistence

Felix keeps conversations in three places: hosted threads in the API Worker, session files beside a workspace for the terminal client, and records in the browser for the web app. Each one stores the same message shapes from @felix/ai and validates them again when it reads them back.

Where Code Holds
Hosted threads packages/storage/src/, apps/api/src/storage/ Thread logs, one writer at a time, behind a lease.
Session files apps/cli/src/storage/ One JSON-lines journal per terminal conversation.
Browser history apps/web/src/storage/ One localStorage record per web conversation.

Threads live in a Durable Object, ThreadDatabase, declared as a SQLite class in apps/api/wrangler.jsonc and bound as THREADS. By default every thread for a deployment is in one instance, named operator. With FELIX_THREAD_STORAGE=per-thread, each thread is in its own instance, thread:<id>, and operator keeps a summary row per thread for lists, search and the asking list, and the record of which threads use each workspace blob and image (see Thread storage). The Worker reaches the instances only through the bindings in packages/storage/src/ (thread-binding.ts, workspace-binding.ts, rating-binding.ts), which call the object’s internal paths and decode each reply with the @felix/protocol parsers.

With FELIX_THREAD_INDEX set to write or read, the instances also send each thread change to a Postgres index, and under read lists, search and account export come from it; the partitions stay authoritative either way (apps/api/src/storage/thread-index.ts). FELIX_STORES=postgres keeps the stores that are not threads in Postgres instead of the Durable Object. See Thread index and Stores in Postgres.

How a hosted thread is stored and how it becomes the model's messages againA hosted run request names a thread and the revision it last saw. acquire refuses it with 409 run_in_progress when another run holds an unexpired lease, or 409 stale_revision when the revision has moved; otherwise it gives the run a fifteen-minute lease and the thread reads as running. The run writes through a leased session that carries its run id and revision. Each append names the revision it expects and, in one transaction, writes an entry under entry:id:seq and moves the revision on by one in the thread's meta record, which also holds the leaf, the lease and the status. A compaction record is written under the same lease to branchcompaction:id:seq, anchored to an entry on the active branch. Entries form a tree: each has a parent, and a rewind moves the leaf and the revision forward without deleting anything. A read walks from the leaf to the root and keeps the message entries; a log whose entries are missing, out of order or miscounted fails with 503 corrupt_thread_log rather than returning part of a thread. The harness then renders the model's input with applyCompaction: the newest compaction anchored on the active branch becomes a summary message, followed by any tool result kept verbatim and then the messages from its firstKept index on. finish drops the lease and sets the status to idle or interrupted.lease held, orrevision moved15-minute leasefor the run idappend at theexpected revisionrevision + 1, same transaction;finish sets idle or interruptedwriteCompactionunder the leasemoves the leaf and therevision;deletes nothingnames the leafa gap fails as503 corrupt_thread_lognewest anchoredon the branchHosted runrequestthreadId +revisionacquire409run_in_progressor stale_revisionleasedSessionrun id + revisionentry:id:seqmessage, label,model change,instructionsmeta:idrevision, leaf,lease, statusbranchcompaction:id:seqsummary,firstKept, anchorrewindActive branchleaf to root,messages onlyapplyCompactionsummary, thenmessagesfrom firstKeptMessages themodel is sentHosted thread storage and the model's messages
How is a hosted thread stored, and how does it become the model's messages again?Drawn from docs/diagrams/session-state.mmd, which follows packages/storage/src/thread-store.ts:threadRepository (acquire, append, writeCompaction, readCompaction, rewind, read), packages/storage/src/thread-session.ts:leasedSession, packages/harness/src/compaction/compaction.ts:applyCompaction.

The storage logic is threadRepository in packages/storage/src/thread-store.ts. It depends only on a small transactional key-value interface, so the tests run it against in-memory storage (MemoryThreadStorage in packages/storage/src/memory-storage.ts) and the Durable Object passes its own storage straight in (apps/api/src/storage/thread-object.ts).

Key Value
meta:<id> The summary (title, createdAt, updatedAt, revision, status, and mode and agent when set), the total message bytes, the active branch’s leaf, the number of entries, and the lease if one is held.
entry:<id>:<seq> One entry of the tree (a message, a label, a model change, instructions, a script’s stored values, or a signal wait and its end) with its parent. The sequence is zero-padded to six digits, so keys sort in append order.
branchcompaction:<id>:<seq> A compaction record, anchored to the entry its summary reaches. At most 16 per thread.
steer:, clientcall:, asking:, run: (each <id>:…) Steers waiting for the running run, client tool calls it waits on, the index of threads asking a question, and run history.
recent:<inverted updatedAt>:<id> In operator only: the search order, newest-updated first. Kept in the same transaction as each meta: write (thread-recency.ts); recent:state says whether an older deployment’s index is still being built, during which search sorts every row instead.

Every write, and every read of a whole thread, runs inside one storage transaction, so a message and the metadata that counts it are committed together or not at all. The object validates a whole command before touching storage: a malformed command is a 400 invalid_storage_request, kept apart from a storage failure, which is a 503 storage_unavailable.

A thread’s messages are entries in a tree: each has a parent, and the thread’s leaf marks the active branch, which is what a read returns and what messageCount counts. Rewinding moves the leaf; nothing is deleted. A thread’s revision is a counter that moves forward by one on every change to the log (an append, a rewind, a compaction, a label or model-change entry) and never repeats, so it is a safe token for optimistic checks even after a rewind shortens the branch.

An append names the revision it expects. If the thread is there, the message is written after the leaf and the revision moves on by one. If the thread has moved on, the append is refused with stale_revision, unless it is a retry of the append just made (the revision moved exactly once and the leaf is that same message), in which case it returns without writing: a retried append is harmless. Threads stored before the tree (message:<id>:<revision> and compaction:<id> keys) read as one branch and are rewritten as entries on their next change, without losing data; see docs/runtime.md for how their counter starts.

A read checks the log against its metadata and fails with 503 corrupt_thread_log rather than returning a partial thread when entries are missing or out of order.

Limits, enforced on append with thread_limit: 1,000 messages, 128 KiB per serialized message, and 1 MiB in total. A compaction summary is at most 64 KiB.

A hosted run request first calls acquire with the revision the client sent and a fresh run id.

  • If an unexpired lease is held by another run, it is refused with 409 run_in_progress.
  • Otherwise, if the revision does not match, it is refused with 409 stale_revision.
  • Otherwise the thread gets a lease for that run id, expiring in fifteen minutes, and its status becomes running.

Every later write (append, writeCompaction, finish) passes the run id and is refused with run_expired unless that run holds the lease and it has not expired. That is how a stale owner is kept out: once its lease has lapsed, or another run has taken the thread after the lapse, nothing it sends is written, and its stream ends with run_failed. finish drops the lease and sets the status to idle or interrupted. A thread whose lease has expired without a finish reads as interrupted, and the next acquire at the current revision takes it.

The ten-minute run budget is shorter than the lease, so a lease normally runs out only when the Worker stopped before it could call finish.

When a context window is configured, older turns are summarized to keep the model’s input inside it. The record is { summary, firstKept, tokensBefore, madeAt? }, written under the run’s lease, where madeAt is the branch length it was made at. The store checks its shape and that firstKept falls within the log, and anchors it to that point on the active branch; a newer record for the same point replaces the older one. A read uses the newest record anchored on the active branch. The harness checks it against the transcript when it reads it.

The terminal client keeps each conversation in a file of JSON lines. The default is WORKSPACE/.felix/sessions/default.json, and the conversation picker lists every <name>.json in that directory. A new session is named by when it was started, for example 20261115-093000-1a2b. Names match [A-Za-z0-9][A-Za-z0-9._-]{0,127}. The file tools refuse any path inside .felix, so the model cannot read or write sessions.

{ "type": "session", "version": 3, "workspace": "/home/me/project" }
{ "type": "entry", "kind": "message", "message": { "role": "user", "text": "…" }, "id": "…", "parentId": null, "at": 1763198400000 }
{ "type": "leaf", "leaf": "…" }

The first line is the session header; the lines after it are entries of the tree, and leaf and compactions lines, each of which replaces the one of its kind before it. workspace records where the session was started, so opening it from another workspace warns that its paths meant that one. A file whose first line is not a usable header is read as an older single-document file: version 1 (a message list with one compaction record) and version 2 (the entry tree) are read and converted to the current format on the next change; anything else is refused when opened. In the journal, a line that cannot be used is skipped with a warning, and a torn last line is cut off.

A change, appended entries, a moved leaf or a new compaction record, is appended to the journal as lines and fsynced before the in-memory copy advances and the run continues. If the append fails, what it wrote is cut off again and the next change rewrites the file. A file that held a skipped line is only appended to, never rewritten, so the skipped line stays for someone to recover.

The whole file is rewritten as a snapshot only when it is new, converted from an older format, or mostly lines later ones replaced:

  1. Write the new snapshot to <file>.<uuid>.tmp, created exclusively with mode 0600.
  2. fsync it.
  3. Rename it over the session file.
  4. fsync the directory, so the rename is durable too.

If any step fails, the temporary file is removed, the in-memory copy stays where it was, and the error reaches the run. The session directory is created with mode 0700.

Opening a session creates <file>.lock exclusively, with mode 0600, and writes the process id and host name into it as JSON ({ "pid": …, "host": "…" }). A second process opening the same session is refused. The lock is removed when the client closes the session. A lock left by a process on the same host that has since stopped is reclaimed when the session is next opened. A lock from another host, or from a process this host cannot see, stays; remove it by hand only after checking that process has stopped.

The full-screen client has one session open at a time. Switching takes the next session’s lock first, then releases the current one, so a session another process holds is refused and the client stays where it was.

The picker reads each file for its first prompt and its modification time. A file that does not parse as a session is left out of the list rather than failing it.

The web app keeps conversations in localStorage, one key per conversation:

Key Storage Holds
felix.conversations.v1:<id> localStorage { version: 1, id, updatedAt, snapshot, remote? }
felix.active-conversation.v1 sessionStorage The id open in this tab.

snapshot holds the turns as drawn (prompt, text, tool steps, state) and the message history. remote is present for a server thread: its id and the last revision this browser saw, which the next hosted request sends. Server threads are cached under the id hosted-<thread id>.

A record is saved at each checkpoint of a conversation. A record is at most 524,288 characters of JSON; a larger one is not written, and a stored one over that size is not read.

On load, every record is decoded and checked: version, id matching its key, turn and tool states, messages, and the remote binding. A record that fails is skipped and counted, and the app says “Some saved conversations could not be opened.” The others still load. Other keys in localStorage are left alone.

Failure Behaviour
localStorage cannot be read “Browser storage is unavailable. New conversations will stay in this tab.”
A save fails, or the record is too large “This conversation could not be saved. It is still available in this tab.” The next successful save clears it.
The tab selection cannot be saved “Conversation selection could not be saved for this tab.”

Hosted threads share one namespace per deployment, and each is private to the person who owns it (admins can read every thread). The web app renames and deletes hosted and browser threads from a thread’s row menu. Terminal sessions are named with /rename (in .titles.json, so the session file is not rewritten) and deleted with Ctrl+D in the conversation picker.