|
| 1 | +# ARCHITECTURE.md |
| 2 | + |
| 3 | +zerostack supports an optional `ARCHITECTURE.md` file that gives both the main |
| 4 | +agent and exploration subagents high-level design context about your project. |
| 5 | + |
| 6 | +## What It Does |
| 7 | + |
| 8 | +When `ARCHITECTURE.md` files exist (at the project root and/or in parent |
| 9 | +directories), their content is appended to the agent's system prompt preamble |
| 10 | +— right after `AGENTS.md` context and before the custom prompt. This means |
| 11 | +every LLM call carries awareness of your project's architecture. |
| 12 | + |
| 13 | +**All subagents also receive the same architecture context**, so they can |
| 14 | +explore the codebase with an understanding of the overall design. |
| 15 | + |
| 16 | +## Why Use It |
| 17 | + |
| 18 | +### For the Agent |
| 19 | + |
| 20 | +Without `ARCHITECTURE.md`, an agent exploring a large codebase starts with |
| 21 | +zero architectural knowledge. It reads `AGENTS.md` for conventions, then |
| 22 | +begins probing files one by one. This works but costs tokens and turns as |
| 23 | +the agent builds a mental model from scratch. |
| 24 | + |
| 25 | +With `ARCHITECTURE.md`, the agent enters the conversation already knowing: |
| 26 | +- How the project is organized (directory layout, module responsibilities) |
| 27 | +- Key types, traits, and data structures |
| 28 | +- Where control flow and data flow live |
| 29 | +- Design decisions and constraints |
| 30 | +- External dependencies and their roles |
| 31 | +- Entry points and how things boot |
| 32 | + |
| 33 | +This front-loads understanding, reducing the number of read probes needed and |
| 34 | +making the agent's first responses more accurate. |
| 35 | + |
| 36 | +### For the User |
| 37 | + |
| 38 | +- **Consistency across sessions** — the agent stays aligned with your design |
| 39 | + intent across different conversations |
| 40 | +- **Better subagent delegation** — when using the `task` tool, subagents |
| 41 | + understand the architecture without querying the main agent |
| 42 | +- **Onboarding** — new contributors (human or AI) get a structured overview |
| 43 | +- **Living documentation** — the agent can (and is prompted to) update |
| 44 | + `ARCHITECTURE.md` when significant changes are made |
| 45 | + |
| 46 | +## Discovery and Loading |
| 47 | + |
| 48 | +zerostack loads `ARCHITECTURE.md` files using the same recursive upward search |
| 49 | +as `AGENTS.md`: |
| 50 | + |
| 51 | +1. **Global**: `~/.local/share/zerostack/agent/ARCHITECTURE.md` (XDG data dir) |
| 52 | +2. **Project**: `ARCHITECTURE.md` in the current working directory and all |
| 53 | + parent directories up to the filesystem root |
| 54 | + |
| 55 | +Files from all levels are concatenated, with source-path headers indicating |
| 56 | +where each block came from. This lets you define organization-wide conventions |
| 57 | +in the global file while having project-specific architecture in each repo. |
| 58 | + |
| 59 | +At startup, if no `ARCHITECTURE.md` is found anywhere in the directory tree, |
| 60 | +zerostack offers to create one: |
| 61 | + |
| 62 | +``` |
| 63 | +No ARCHITECTURE.md found in /home/you/project. Create one? [y/N] |
| 64 | +``` |
| 65 | + |
| 66 | +If you answer yes, a template is written to the project root. The template |
| 67 | +includes sections for directory layout, key types/traits, control flow, data |
| 68 | +flow, design decisions, dependencies, and entry points. |
| 69 | + |
| 70 | +When you accept and the template is created, zerostack automatically injects |
| 71 | +a system message instructing the agent to explore the codebase and populate |
| 72 | +the file with a thorough architectural overview. |
| 73 | + |
| 74 | +### Template Contents |
| 75 | + |
| 76 | +The generated template contains: |
| 77 | + |
| 78 | +```markdown |
| 79 | +# Architecture Overview |
| 80 | + |
| 81 | +## Directory Layout |
| 82 | +<!-- Describe the top-level directory structure and responsibilities --> |
| 83 | + |
| 84 | +## Key Types / Traits |
| 85 | +<!-- List the primary data structures, traits, and their relationships --> |
| 86 | + |
| 87 | +## Control Flow |
| 88 | +<!-- How does execution flow through the system? --> |
| 89 | + |
| 90 | +## Data Flow |
| 91 | +<!-- How does data move through the system? --> |
| 92 | + |
| 93 | +## Design Decisions |
| 94 | +<!-- Notable architectural choices and tradeoffs --> |
| 95 | + |
| 96 | +## Dependencies |
| 97 | +<!-- Key external dependencies and their roles --> |
| 98 | + |
| 99 | +## Entry Points |
| 100 | +<!-- How does the application start and accept input? --> |
| 101 | +``` |
| 102 | + |
| 103 | +## Disabling |
| 104 | + |
| 105 | +Pass `--no-context-files` (or `-n`) to suppress loading of both `AGENTS.md` |
| 106 | +and `ARCHITECTURE.md`. You can also set `no_context_files = true` in your |
| 107 | +config file. |
| 108 | + |
| 109 | +## How It Integrates |
| 110 | + |
| 111 | +| Layer | Behavior | |
| 112 | +|---|---| |
| 113 | +| **System prompt** | Architecture content appended after `AGENTS.md`, before custom prompt | |
| 114 | +| **Subagents** | Each subagent receives the architecture context in its preamble | |
| 115 | +| **`task` tool** | Exploration subagents instructed to read `ARCHITECTURE.md` first | |
| 116 | +| **TUI status** | Displays `loaded ARCHITECTURE.md` when architecture content exists | |
| 117 | +| **Prompts** | Built-in prompts reference architecture-aware workflows | |
| 118 | + |
| 119 | +## Writing a Good ARCHITECTURE.md |
| 120 | + |
| 121 | +A well-written `ARCHITECTURE.md` should be **concise** (aim for 200-500 words |
| 122 | +for small projects, 500-2000 for larger ones) and **actionable** — think of it |
| 123 | +as a cheat sheet the agent can reference when making decisions. Avoid |
| 124 | +reproducing code; focus on structure, relationships, and rationale. |
| 125 | + |
| 126 | +### Recommended Sections |
| 127 | + |
| 128 | +1. **Directory Layout** — one-line summaries of each top-level directory |
| 129 | +2. **Key Types/Traits** — the 5-10 most important data structures |
| 130 | +3. **Control Flow** — request lifecycle, main loops, async boundaries |
| 131 | +4. **Data Flow** — how data enters, transforms, and exits the system |
| 132 | +5. **Design Decisions** — "why X instead of Y" for critical choices |
| 133 | +6. **Dependencies** — key libraries and what they're used for |
| 134 | +7. **Entry Points** — binary entry, API handlers, CLI parsing |
| 135 | + |
| 136 | +### Example |
| 137 | + |
| 138 | +```markdown |
| 139 | +# Architecture Overview |
| 140 | + |
| 141 | +## Directory Layout |
| 142 | +- `src/agent/` — Agent building, prompt construction, tool execution |
| 143 | +- `src/ui/` — TUI event loop, renderer, input handling, slash commands |
| 144 | +- `src/provider/` — LLM provider abstraction (OpenAI, Anthropic, etc.) |
| 145 | +- `src/config/` — Config parsing, validation, resolution |
| 146 | +- `src/extras/` — Optional features gated behind Cargo features |
| 147 | + |
| 148 | +## Key Types |
| 149 | +- `AnyAgent` / `AnyClient` — type-erased agent and LLM client |
| 150 | +- `Session` — conversation state, messages, tokens, compactions |
| 151 | +- `ContextFiles` — loaded AGENTS.md, ARCHITECTURE.md, prompts, themes |
| 152 | + |
| 153 | +## Control Flow |
| 154 | +1. CLI args parsed → config loaded → context files discovered |
| 155 | +2. Agent built with system prompt (agents + architecture + prompt) |
| 156 | +3. TUI event loop: user input → agent runner → streaming events → renderer |
| 157 | +4. Slash commands intercept input starting with `/` |
| 158 | + |
| 159 | +## Data Flow |
| 160 | +- User input → InputEditor → event loop → agent.spawn_runner() |
| 161 | +- Runner streams AgentEvent (Token, Reasoning, ToolCall, ToolResult, Done) |
| 162 | +- Events rendered incrementally via Renderer::write_line() |
| 163 | + |
| 164 | +## Design Decisions |
| 165 | +- Type-erased client/agent via trait objects for provider flexibility |
| 166 | +- Tokio for async I/O; crossterm for TUI |
| 167 | +- mpsc channels for agent events, user events, and permission requests |
| 168 | + |
| 169 | +## Dependencies |
| 170 | +- `rig` / `rig-core` — LLM client abstraction |
| 171 | +- `crossterm` — cross-platform terminal manipulation |
| 172 | +- `tokio` — async runtime |
| 173 | +- `serde` / `toml` — config parsing |
| 174 | + |
| 175 | +## Entry Points |
| 176 | +- `main.rs` — binary entry, CLI parsing, session init |
| 177 | +- `run_interactive()` — TUI main loop |
| 178 | +``` |
| 179 | + |
| 180 | +## Comparison with AGENTS.md |
| 181 | + |
| 182 | +| Aspect | AGENTS.md | ARCHITECTURE.md | |
| 183 | +|---|---|---| |
| 184 | +| **Purpose** | Coding conventions, instructions, project-specific procedures | High-level design: structure, relationships, rationale | |
| 185 | +| **Scope** | "How to work in this codebase" | "How this codebase is built" | |
| 186 | +| **Update frequency** | Rare (conventions change slowly) | With significant refactors or new modules | |
| 187 | +| **Typical length** | Short to medium | Medium (200-2000 words) | |
| 188 | +| **Loaded together** | Yes, both concatenated into system prompt preamble | | |
| 189 | + |
| 190 | +Both files complement each other: `AGENTS.md` tells the agent how to operate; |
| 191 | +`ARCHITECTURE.md` tells it what it's operating on. |
0 commit comments