How one user message travels through reflect.
User prompt (TUI or reflect exec)
│
▼
┌───────────────────┐
│ buildSystemPrompt │ AGENTS.md + prompt templates (practices.md)
└─────────┬─────────┘
│
▼
┌───────────────────┐ ┌─────────────┐
│ local memory │◄────│ ~/.reflect/ │ conventions, decisions, lessons
│ recall │ │ memories │
└─────────┬─────────┘ └─────────────┘
│
▼
┌───────────────────┐ ┌─────────────┐
│ token budget │ │ embedded │ CountTokens, CompressForContext
│ (context sizing) │ │ library │
└─────────┬─────────┘ └─────────────┘
│
▼
┌───────────────────┐ ┌─────────────┐
│ Reflect ChatClient │────►│ relay/engine│ catalog, credentials, routing
│ port + adapter │ │ generate/ │────► provider API
└─────────┬─────────┘ │ stream │
└─────────────┘
│
▼
Tool calls (Read, Edit, Bash, …)
│
▼
Response to user
│
▼ (when context grows)
┌───────────────────┐
│ token Compress │ fast path before LLM summarization
│ + relay compact │
└───────────────────┘
- relay: The Reflect composition root creates an
relay/engine.Enginewith Relay-owned state paths, an injected secret store, and per-engine custom gateway metadata. The engine loads provider state and the model catalog, then builds transport behind Reflect'sChatClientport. - memory:
configureSessioninitializes the local memory manager (core, auto, evolving, zen, retrieval metrics, continuity). If the state directory is unavailable, reflect runs without persistent memory. - token: No startup step — embedded at compile time.
- Reflect templates (
internal/prompts/templates/*.md) define behavior, tools, and practices. - Project
AGENTS.mdis appended viareflectconfig.BuildContextWithDirs. - memory:
Memory.Recallinjects relevant stored memories into the system prompt.
Each turn:
- memory — recall memories matching the latest user message (token budget ~2000).
- relay — Reflect's adapter calls engine generate/stream with Reflect-owned tool definitions; Relay normalizes provider events and tool requests.
- Tools run with the session's memory service in context.
- memory — post-session bookkeeping records the session goal and outcome.
When messages exceed limits (internal/engine/compact.go):
- token —
token.Compress()tries a fast compression path for summaries. - relay — if token reduction is insufficient, reflect calls the LLM to summarize, then keeps recent messages.
internal/engine/token/tokenizer.gowraps the embedded token engine for precise and fast estimates used in budget UI and compaction decisions.
reflect doctor # ecosystem panel + relay preflight
./scripts/smoke-reflect.sh # build + quick tests| Module | Role in reflect | Required? |
|---|---|---|
| relay | LLM APIs, catalog, credentials, routing | Yes |
| internal/token | Token estimate + context compression + secrets + usage | Yes (embedded, no config) |
relay is an independent sibling checkout; the parent go.work wires the
local Go workspace.
Production Reflect code imports Relay only through relay/engine. Conversation
history, WAL/resume, permissions, tool execution, and memory remain in Reflect;
provider credentials, discovery, selection, transport, resilience, and
normalized streaming remain in Relay.