Architecture
8 KB - הצגת הקובץ המקורי
This page describes the finished assistant. Parts marked with a lab number arrive in that lab; the model client and the turn loop exist from lab 01, and the session log from lab 02.
The shape
קוד המקור של התרשים
flowchart TB
subgraph Sources[Event sources]
T[Terminal channel]
W["Web channel (lab 08)"]
S["Scheduler (lab 09)"]
A["Sub-agent results (lab 10)"]
end
Sources --> G["Gateway (lab 08): identity links, session keys, one queue per session"]
G --> AS[Assistant.reply]
AS --> L[Turn loop]
CTX["Context assembly: persona, rules, caching (lab 03); memory via the memory tool (lab 06)"] --> L
L <--> M[ModelClient]
M --- API[Anthropic API]
M --- FAKE[Scripted fake for tests]
L --> TOOLS["Tools (lab 04)"]
TOOLS --> POL["Policy and approvals (lab 05)"]
L --> LOG[(Session event log, JSONL)]Three ideas carry the whole design:
- Everything is an event. A message you type, a timer firing, and a sub-agent finishing are all events that enter the same per-session queue. The turn loop does not care where an event came from.
- The log is the source of truth. Each session is an append-only file of typed events. The message list sent to the model is rebuilt from it, so a restart continues exactly where the conversation stopped. The model keeps nothing between requests: every Messages API request carries the whole conversation.
- The model sits behind an interface. The turn loop talks to a
ModelClient. In production that is the Anthropic SDK; in tests it is a scripted fake, so every behavior can be tested offline.
One turn, step by step
קוד המקור של התרשים
sequenceDiagram
participant U as Channel
participant A as Assistant
participant S as Session log
participant L as Turn loop
participant M as Model
participant T as Tools
U->>A: reply(text)
A->>S: turn_started, user message
A->>L: runTurn(history, message)
L->>M: complete(system, messages, tools)
M-->>L: tool_use
L->>S: assistant message (exactly as returned)
L->>T: run tools (validated input)
T-->>L: results
L->>S: one user message with all tool results
L->>M: complete(...)
M-->>L: end_turn + text
L->>S: assistant message
A->>S: turn_completed (stop reason, usage)
A-->>U: textSession events
Each line of a session log is one JSON object:
| Type | Written when | Main fields |
|---|---|---|
session_started |
The log is created | sessionId, key |
turn_started |
A message arrives | turnId, channel |
message |
Any message is added to the history | turnId, message (API format) |
turn_completed |
The turn finished normally | turnId, stop, model, usage |
turn_interrupted |
An error, Ctrl+C, or a crash | turnId, reason |
approval_requested |
A tool call needs the owner (lab 05) | turnId, approvalId, toolUseId, tool, summary, reason |
approval_resolved |
The owner (or the system) answered | approvalId, decision, by, note (optional) |
Every event also has v (format version) and ts (timestamp).
Session keys and scope
A session key names a conversation slot; a session id names one log file.
| Key | Used for |
|---|---|
steward:main |
All direct messages from the owner (default scope main) |
steward:channel:<name> |
One conversation per channel (scope per-channel) |
steward:cron:<job> |
A scheduled job's own conversation (lab 09) |
steward:sub:<task> |
A sub-agent's isolated conversation (lab 10) |
/new keeps the key and starts a new log. Old logs stay on disk unchanged.
Where the code lives
| Concern | File |
|---|---|
| Config | src/config/config.ts |
| Wiring everything together | src/app.ts |
| Model interface | src/core/model.ts, src/core/anthropic-model.ts |
| Turn loop, approvals, resume | src/core/turn-loop.ts |
| Assistant (session glue) | src/core/assistant.ts |
| System prompt, per-turn context | src/context/system-prompt.ts, src/context/turn-context.ts |
| Usage and cost | src/context/usage.ts |
| Tool contract | src/tools/tool.ts |
| Workspace and file tools | src/tools/workspace.ts, src/tools/file-tools.ts |
| Programs without a shell | src/tools/program-tool.ts |
| Policy and audit log | src/security/policy.ts, src/security/audit.ts |
| Long-term memory | src/memory/memory-store.ts, src/memory/memory-tool.ts |
| Sessions | src/sessions/*.ts |
| Terminal channel | src/channels/terminal.ts |
| Web channel (lab 08) | src/channels/web.ts, src/channels/web-page.ts |
| Gateway and per-session queue (lab 08) | src/gateway/gateway.ts, src/gateway/queue.ts |
| Scheduler (lab 09) | src/scheduler/*.ts |
| Sub-agents (lab 10) | src/agents/subagent.ts |
| Record, replay, and evals (lab 11) | src/core/recording-model.ts, src/evals/runner.ts |
| Spending cap (lab 12) | src/security/budget.ts |
| Atomic file writes (lab 02) | src/runtime/atomic.ts |
| Process lock and backups (lab 12) | src/runtime/lock.ts, src/runtime/backup.ts |
Data on disk
Path (under ~/.steward/) |
Contents | Lab |
|---|---|---|
sessions/index.json |
Session key to current session id | 02 |
sessions/<id>.jsonl |
One event log per session | 02 |
audit.jsonl |
One line per tool call decision and outcome | 05 |
policy-overrides.json |
The owner's "always" answers | 05 |
memory/ |
Long-term memory files (/memories to the model) |
06 |
outbox.jsonl |
Messages the assistant started itself | 08 |
jobs.json |
Scheduled jobs | 09 |
spend.jsonl |
The cost of every turn, for the daily cap | 12 |
steward.pid |
The process lock: one Steward per data folder | 12 |