The Context Compiler is the engine that makes Gemstack work. It performs JIT (Just-In-Time) prompt assembly — dynamically stitching together exactly the right combination of project context, role definitions, phase instructions, and topology guardrails for any workflow step.
When you run gemstack compile step3-build (or when gemstack run calls it internally), the compiler executes 8 stages in sequence:
┌─────────────────────────────────────────────────────────┐
│ CONTEXT COMPILER │
│ │
│ 1. Parse Workflow → Extract required roles/phases │
│ 2. Load Roles → Bundle expert personas │
│ 3. Load Phases → Attach behavioral rules │
│ 4. Load Guardrails → Inject topology constraints │
│ 5. Compile .agent/ → Read project context files │
│ 6. Include Sources → Pull in referenced source code │
│ 7. Fire Hooks → Let plugins modify sections │
│ 8. Apply Budget → Truncate if over token limit │
│ │
│ Output: A single, self-contained prompt string │
└─────────────────────────────────────────────────────────┘
The compiler reads the workflow definition file (e.g., step3-build.md) and extracts:
- Required roles — Which expert personas the step needs (e.g.,
Principal Backend Engineer,Principal Frontend Engineer) - Required phases — Which behavioral phase rules apply (e.g.,
build) - Workflow goal — The step's purpose and acceptance criteria
Each role is a markdown file containing an expert persona definition. For step3-build, the compiler loads:
principal-backend-engineer.md— Server-side implementation expertiseprincipal-frontend-engineer.md— Client-side implementation expertise
Roles define the AI's professional identity, responsibilities, decision-making principles, and behavioral boundaries.
Phase instructions are behavioral rules for the current phase. The build phase enforces:
- "Loop until Exit Code 0" — run tests in the terminal, fix errors, repeat
- Static analysis first — run the type-checker before the test suite
- No shortcuts — never hardcode values or mutate tests
- Bounded Reflexion — maximum 3 attempts before circuit breaker
The compiler reads your topology declaration from ARCHITECTURE.md:
**Topology:** [frontend, backend]And loads the corresponding guardrail documents. For [frontend, backend], it injects:
backend.md— Data integrity, anti-mocking discipline, N+1 prevention, deterministic testingfrontend.md— Component state matrix, hydration safety, accessibility, no raw fetch loops
Multiple topologies are combined additively — all guardrails from all declared topologies apply simultaneously.
Your project's 5 context files are read and included:
ARCHITECTURE.md— Tech stack, data models, API contractsSTYLE.md— Coding conventions, forbidden anti-patternsTESTING.md— Test strategy, commands, coverage matricesPHILOSOPHY.md— Product soul, core beliefsSTATUS.md— Current lifecycle state, active feature, relevant files
Files listed in STATUS.md under "Relevant Files" are read from disk and included as raw source code. This gives the AI direct visibility into the files it needs to modify:
<!-- In STATUS.md -->
## Relevant Files
- src/api/routes/users.ts
- src/components/UserSearch.tsx
- tests/api/users.test.tsThese files are included verbatim in the compiled prompt.
Two plugin hooks fire during compilation:
gemstack_pre_compile(step, sections)— Plugins can add, remove, or reorder sections before assemblygemstack_post_compile(step, compiled)— Plugins can modify the final compiled string
See Building Custom Plugins for hook implementation details.
If you specified a --token-budget, the compiler truncates sections by priority (removing lowest-priority first):
| Priority | Section | Behavior |
|---|---|---|
| 1 (never removed) | Workflow Goal | Always included |
| 2 | Role Definitions | Removed last — the AI needs its persona |
| 3 | Phase Instructions | Behavioral rules for the current phase |
| 4 | .agent/ Files |
Your project context |
| 5 | Topology Guardrails | Domain-specific constraints |
| 6 | Workflow Protocol / Source Files | Removed first if space is tight |
This ensures your most critical context — the workflow goal, the AI's expert persona, and your project architecture — is always preserved, even in small context windows.
# View the compiled prompt for any step
gemstack compile step1-spec
gemstack compile step3-build
gemstack compile step4-auditThe output is the complete prompt string that would be sent to the Gemini API.
# Compile with a tight budget — see what gets truncated
gemstack compile step3-build --token-budget 50000
# Compile with a generous budget
gemstack compile step3-build --token-budget 200000# Save to a file
gemstack compile step3-build > /tmp/context.md
# Copy to clipboard (macOS)
gemstack compile step3-build | pbcopy
# Export as structured JSON
gemstack export --format json > context.jsonA compiled prompt has this general structure:
═══════════════════════════════════════════
WORKFLOW: Step 3 — Build
═══════════════════════════════════════════
## Goal
Implement the feature code and loop against the terminal
until every test passes with Exit Code 0.
═══════════════════════════════════════════
ROLES
═══════════════════════════════════════════
### Principal Backend Engineer
[Full role definition...]
### Principal Frontend Engineer
[Full role definition...]
═══════════════════════════════════════════
PHASE: Build
═══════════════════════════════════════════
[Build phase behavioral rules...]
═══════════════════════════════════════════
TOPOLOGY GUARDRAILS
═══════════════════════════════════════════
### Backend Guardrails
[Anti-mocking, data integrity, N+1 prevention...]
### Frontend Guardrails
[Component state matrix, hydration safety...]
═══════════════════════════════════════════
PROJECT CONTEXT
═══════════════════════════════════════════
[ARCHITECTURE.md, STYLE.md, TESTING.md, STATUS.md content...]
═══════════════════════════════════════════
RELEVANT SOURCE FILES
═══════════════════════════════════════════
[Raw source code of files listed in STATUS.md...]
-
Populate "Relevant Files" in
STATUS.md— This is how the compiler knows which source files to include. Be specific — list only the files relevant to your current feature. -
Use
--token-budgetto control costs — Larger contexts cost more but give the AI more information. Start with100000tokens and adjust. -
Check compilation before running — Always
gemstack compile step3-build | head -20beforegemstack runto verify the context looks correct. -
Use plugins for company-wide standards — If your organization has coding standards, inject them via
gemstack_pre_compileso every developer gets them automatically.
- 🤖 Learn how compiled context is used in Autonomous Execution
- 📐 Understand what guardrails are loaded in Topology-Aware Guardrails
- 🧩 Extend the compiler via Building Custom Plugins