Skip to content

Sessions & strands

  • A strand is Ka’s session: one append-only JSONL file whose replay reproduces engine state. ka agent never writes strand files; only the engine does.
  • A ka agent session is the durable handle for a conversation thread: a ka agent id bound to exactly one strand at a time (by id and path), plus a workspace and a status.

The lineage moves to a new strand only on /new, fork, or transfer.

Ka accepts one prompt per session at a time; a prompt sent while a cycle is running is silently dropped. ka agent therefore never submits directly:

  1. An inbound message (Telegram or console) is written to SQLite first, then enqueued.
  2. The queue drains one item at a time; the next item is submitted only after the engine reports idle.
  3. The span from submit to idle is one ka agent turn, with usage and cost recorded at the end.

The boundary is precise: the turn ends at turn_finished (its own terminal status, not an ask resolving), while idle is the delivery boundary where output flushes, the episodic index writes, and the queue drains; the queue never drains into a live cycle. Consequences you can observe: a crash between message arrival and dispatch re-delivers at least once (consumers are idempotent), and a restart never loses queued messages.

Sessions resume lazily: the first inbound message after a daemon, worker, or machine restart spawns a worker pointed at the session’s recorded strand path. A session whose strand file does not exist yet (Ka appends it only after the first completed turn) is reopened as new.

One subtlety makes resume safe: a resumed strand restores model and mode only from mid-session change records, so it can come back model-less or in a looser mode than configured. After every attach, the worker re-asserts set_mode and set_model before the first submit. Policy cannot loosen across a restart.

The core is multi-user on the team edition (v0.14.0, ADR-0003): each user owns one session (sessions.username, schema v12), adopted at their first message, and each session holds its own conversation state. There is no global switch and no head-of-line blocking: one user’s turn never waits on another user’s.

Live engines are bounded by runtime.max_workers (default 8, 1..=64; applies at restart). Over the cap the scheduler evicts the least-recently-active idle worker first (a deliberate close: the session persists and its strand reopens on the next message). If every live worker is mid-turn, the message queues on its own user’s session, and a round-robin drain hands slots over as turns finish, so a long turn elsewhere never blocks anyone. A crash degrades one user’s session: the crash-loop breaker counts per user, and idle workers are still reaped per worker_idle_timeout_secs. The spike behind ADR-0003 measured 8 idle engines at roughly 230 MiB before any engine work, which is what sizes the default. The optional per-worker bubblewrap profile (runtime.sandbox_engine) stays off by default; see roles.

/mode [m] switches the permission mode live: ask (asks; the default), accept_edits, allow and readonly (read-only). The old names still parse: guarded means ask, free means allow, plan means readonly, and an alias in the config normalizes at load. From the console and the web you can move either way; from a phone you can only tighten (allow < accept_edits < ask < readonly). Whenever the mode is not ask, a badge shows in the prompt, /status and the web header, so a loosened session never looks like an unrestricted one by accident.

/new private starts an incognito conversation on every surface: the terminal, Telegram, Matrix, and the 🕶 button in the web composer. A private conversation records usage but no content:

  • Turn rows carry no text, and nothing is indexed into the episodic history; usage accounting (model, tokens, cost) still records.
  • Memory, skill, and notes proposals are refused for the session’s turns; /remember and every scheduling path (/remind, /cron) answer with a refusal.
  • Inbound media lands in an ephemeral attachments/.private-<short-id>/ directory instead of the shared inbox.
  • The worker runs on a scratch engine home (a per-session KA_DATA_DIR and XDG_STATE_HOME under <root>/tmp/private-engine-<short-id>/), so strands, waypoints and spilled tool outputs stay out of the shared engine directories.

Ending the conversation (any /new) and a sweep at daemon boot destroy the scratch home, the media directory, queued messages and pending approval cards (which embed tool arguments), with secure_delete and a WAL checkpoint so the text leaves the database file. Usage accounting and the audit chain persist. One boundary stays: the in-memory event ring (256 envelopes) may re-deliver a just-ended private conversation’s last frames to a reconnecting surface until they age out; process memory only, never on disk.

While a private conversation runs, 🕶 private shows in the terminal status line, /status, and the web composer toggle keeps every surface in sync. Schema v10 carries the flag (sessions.private).

A message that arrives mid-turn has two destinations, set by busy_messages (default steer):

  • steer: the text rides the running reply. /steer <thought> does the same on demand, and the thought reaches the engine at the next tool-batch boundary, before the next tool call.
  • queue: the text waits durably for the next turn.

The queue is editable, not just visible: /queue lists it, and subcommands manage one item by row number: /queue rm <n>, /queue edit <n> <text>, /queue top <n>, /queue up <n>, /queue down <n>, and /queue now <n>, which promotes the item and aborts the running turn exactly like /stop (the engine never sees a mid-cycle prompt; the promoted item is simply the next submission). /queue clear empties the list. On Telegram the queue card grows a ↑ promote button per row beside the ✕ remove button.

Interrupt keywords. With interrupts on (the default), a bare short message (“stop”, “wait”) aborts the running cycle like /stop and becomes the next turn: ka agent answers with a stopped notice and puts the text at the queue’s front. The matcher is deliberately narrow: a trimmed message of at most 20 characters, case-insensitive, exactly one high-weight word plus optional trailing punctuation. Medium words (“no”, “actually”) are not acted on: a mid-cycle correction rides the steer path, and anything longer queues or steers normally. interrupts = "off" restores plain busy_messages behavior.

/undo reverts the last file change of the session, and every allowed write also lands in a shadow-git checkpoint: /rewind <n> restores the workspace to before an earlier approved write (see checkpoints & rewind). Background turns (crons, the briefing, ka-agent ask --background) run on background_model when one is set, and the main model otherwise.

/preset save <1-3> captures the live model + mode + effort triple into a numbered slot owned by your user (schema v16); /preset <n> applies it through the same setters as /model, /mode and /effort, so group model policy and role mode ceilings still bind: a preset cannot lift either. Bare /preset lists the slots. Local surfaces only.

/archive hides your live session from every list without deleting anything: the transcript, its turns and its usage rows stay, and @turn: references still resolve. /archive undo <id-prefix> restores it, /archive list shows what is hidden, and another user’s prefix is refused. The web session list and retention bookkeeping read the filtered list (archived included only on request), so an always-on daemon’s list stays navigable. Schema v17 carries the flag. Local surfaces only.

A shared agent is a definition several users can run: persona, skills, knowledge bundles and a model, owned by an account and shared with a group at a level (viewer, editor, owner; at least one owner per agent). The definition is shared; the run state is not:

Terminal window
ka-agent agent create reviewer --persona "You review changes" --group dev
ka-agent agent share reviewer --group dev --level viewer
ka-agent agent list · agent unshare reviewer --group dev · agent transfer reviewer ana · agent rm reviewer

/agent <name> runs the conversation as that agent (persona, skills and knowledge come from it); a bare /agent switches back to the user’s own assistant (agent: off). Only a user with access can switch (no shared agent "x" you can use otherwise), the session and usage stay the caller’s, and approvals and budgets follow the caller’s role and quota exactly as in their own turns. Access is re-checked while the conversation runs as the agent, so a revoked share ends the run-in. The panel’s /agents page lists the agents a caller can see, with the definition form for editors and owners.

The transient state of an active session (the in-flight cycle, a pending ask, streaming handles, the worker pid) lives in memory; the parts needed for recovery (queue, turn status, pending approvals) are persisted. The rest is rebuilt or declared lost on restart: unknown state is reported, never guessed.