Architecture
The layering
Section titled “The layering”Every arrow is a process or crate boundary that can be tested on its own:
Telegram / Slack / Discord / Web / CLI / APIs | ka agent runtime | memory + task scheduler + plugin host | stable Ka interface (KaPort) | Ka engineIn the MVP, concretely:
Terminal chat ──admin endpoint──┐ web UI ──loopback──┐Telegram / Matrix (long poll) ──┤ local admin CLI ───────┤Slack (Socket Mode) / Discord (gateway) ──────────────────┤ ▼ ▼ ┌──────── ka-agent daemon ──────────────────────────────────────────┐ │ channels: telegram, matrix, slack, discord adapters, admin │ │ endpoint (unix socket or Windows named pipe), web UI │ │ runtime: per-user sessions, worker pool, durable queue, │ │ approvals, budgets, supervisor, scheduler, hooks · memory · │ │ plugins (bwrap sandbox) · KaPort │ └───────────────────┬───────────────────────────┘ ▼ supervised worker process ka engine (one per session)The crates
Section titled “The crates”| Crate | Role |
|---|---|
ka-agent-core |
Domain types and state machines: ids, users and principals, sessions/turns, approvals, tasks, queue, outbox, budgets, audit, strict ka-agent.toml config. No I/O. |
ka-agent-store |
SQLite persistence: schema (v12 carries users, groups, web sessions, invites, knowledge bases and shared agents beside the sessions/turns core; v13 to v19 add durable allow-rules, checkpoints, skill usage, presets, archived sessions, plans and trigger events), migrations, audit chain, intents; bounded by retention (Store::prune trims terminal runs, settled approvals, finished intents, decided candidates, turned queue rows and sent outbox on 30/90-day windows; turns, FTS, sessions, audit, identities and crons are never pruned). |
ka-agent-port |
The KaPort trait and the crate-backed worker boundary. The only crate besides the binary that may depend on ka-*. |
ka-agent-runtime |
Per-user session manager (UserState map over a bounded worker pool), durable queue, single-flight submission, approvals with deny-on-expiry and policy routing, durable allow-rules, shadow-git checkpoints, plans with verify gates, budgets, worker supervisor with backoff and a crash-loop breaker, scheduler (reminders, cron, calendar, inbound triggers), skills provisioning, operator hooks. One AgentEvent model, emitted in sequenced envelopes so every surface replays the same stream. |
ka-agent-memory |
Stable memory: git-backed Markdown with provenance (including the persona), compiled into the engine’s MEMORY.md and IDENTITY.md under a budget. |
ka-agent-channels |
The Telegram, Matrix, Slack and Discord channels behind one Channel trait, the MCP bridge (kagent__* tools) with grants, identities, web search, knowledge-base sync connectors, and the secret store. |
ka-agent-plugins |
The plugin host: manifest v1, signed archives with TOFU pinning, the bubblewrap sandbox, install/enable lifecycle. |
ka-agent-web |
The loopback-only web UI: server-rendered HTML, argon2 accounts (/login, /invite) beside the one-shot operator login, live chat over SSE, the admin pages (users, groups, KBs, agents), settings and usage. |
apps/ka-agent |
The binary: setup, chat, run, status, dashboard, model, config, cron, approvals, briefing, mcp, profile, usage, identity, user, kb, plugin, grant, secret, memory, persona, notify, ask, hooks, backup, restore, update, import, export, purge, doctor, completions, plus the hidden __worker, __mcp, __guard and agent. |
A CI gate (scripts/check-ka-deps.sh) keeps every ka-* dependency inside ka-agent-port, so swapping the boundary implementation stays a one-crate change.
The engine boundary
Section titled “The engine boundary”ka agent talks to engines only through KaPort:
- Forwarded commands:
prompt,answer,abort,set_mode,set_model,set_name,set_effort,steer,undo_file,compact. A turn can also submit with a per-turn model override (that is how background turns usebackground_model);steerdelivers at the next tool-batch boundary, andundo_fileasks the engine to revert its own last file edit. Nothing else. - Never reachable from a channel:
shell,set_feature,set_sandbox,save_api_key,save_settings. The port API has no operation that maps to them; the invariant is enforced by construction and by contract tests. - No “always” from remote channels. Durable always lives in ka agent’s own rules layer, mode-scoped and revocable (
/y <id> always; see approvals). The engine’s own coarse per-tool always, which writes the project.ka/ka.toml, is never sent: every settle answers plain Allow.
The worker applies a security overlay over any engine configuration, and the overlay always wins:
mode = "ask" # worker sessions always ask[tools.bash]background_after_ms = 0 # bash jobs never detach (kill reaches them)[features]disable = ["tool:remember"] # memory goes through ka agent# [guards] spend_usd comes from the daemon budgetsThe overlay also appends the [[mcp]] block that spawns the kagent bridge (see connectors & grants), so the engine’s extra capabilities are ka agent processes under the same policy.
Workers run isolated from your interactive Ka state: their own KA_DATA_DIR, XDG_STATE_HOME, XDG_CONFIG_HOME and HOME, so the operator’s ka.toml, strands, skills and trust store are never shared.
Worker supervision
Section titled “Worker supervision”- Workers are separate processes, one per active session, spawned and stopped by the runtime.
- Crashes respawn with exponential backoff (1s to 60s, configurable) and a crash-loop breaker: too many crashes inside the window degrade the session, and resuming then needs an explicit
/resumeor/new. - Stopping escalates:
abort, wait the grace period (default 10s), then SIGTERM to the worker’s process group (so tool children die too), then SIGKILL. On Windows the same escalation is onetaskkill /T /Fof the child tree. - An engine channel that closes without a terminal
idleis treated as a crash: the turn is marked interrupted, you are notified, and nothing is auto-replayed.
Full contracts: docs/contract/ka-port.md and docs/contract/worker-protocol.md in the repository.