System Architecture
The system consists of four specialized agent types, each with a distinct role:Agent Roles
| Agent | Purpose | Writes Code | Scope |
|---|---|---|---|
| Root Planner | Decomposes the project into tasks through iterative discovery | No | Entire project |
| Subplanner | Breaks complex tasks into parallelizable subtasks | No | Delegated task slice |
| Worker | Implements individual tasks on isolated branches | Yes | 1-5 files |
| Reconciler | Monitors build/test health and creates fix tasks | No | Main branch |
Key Design Principles
1. Iterative Discovery Over Upfront Planning
Unlike traditional planning systems that attempt to enumerate all work upfront, Longshot agents use iterative discovery:- Sprint-based planning: Each planning iteration produces only what can be confidently specified given current knowledge
- Feedback-driven adaptation: Completed work informs the next planning iteration
- Progressive refinement: As foundations are built, agents can specify increasingly detailed tasks
2. Conversation-Based Memory
All planning agents operate as persistent conversations, not stateless function calls:- Agents maintain a scratchpad that survives across iterations
- Context includes conversation history, previous responses, and accumulated knowledge
- Agents reference their own prior analysis and build on it incrementally
3. Hierarchical Decomposition
Tasks flow through a multi-level decomposition pipeline:4. Scope Containment
Scope inheritance is strictly enforced:- Subplanner subtasks must be subsets of the parent scope
- Workers cannot modify files outside their task scope
- Scope violations break the merge pipeline and cause untraceable conflicts
The Handoff Protocol
Communication between agents uses a structured handoff format:Why Handoffs Matter
Handoffs are the system’s primary feedback mechanism:- Planners learn from worker experiences: Concerns about missing utilities become the next sprint’s tasks
- Quality signals propagate: Patterns workers establish inform subsequent task specifications
- Failures guide adaptation: Blocked tasks produce targeted follow-ups, not wholesale retries
Tools and Capabilities
Planning Agents (Root Planner & Subplanner)
Read-only exploration tools:read— Read file contents by pathgrep— Search file contents with regex patternsfind— Find files by glob patternls— List directory contentsbash— Execute read-only git commands (git log,git diff,git show, etc.)
Worker Agents
Full coding capabilities via pi-coding-agent:read— Read fileswrite— Create/overwrite filesedit— Apply precise edits to existing filesbash— Execute shell commands (git, npm, build tools)grep— Search content with ripgrepfind— Find files by globls— List directories
Reconciler Agent
Build/test execution:- Runs
tsc --noEmitto check TypeScript compilation - Runs
npm run buildto verify bundling/build pipeline - Runs
npm testto execute test suites - Scans for merge conflict markers
- Calls LLM to generate targeted fix tasks based on failure output
The Scratchpad System
The scratchpad is the agent’s durable working memory:Root Planner Scratchpad
Must track:- Goals & Specs: Full goal set from SPEC.md/FEATURES.json, coverage status
- Current State: Iteration number, phase, what’s built/broken/in-progress
- Sprint Reasoning: Why this batch of tasks, what’s deferred and why
- Worker Intelligence: Patterns from handoffs, unresolved concerns
Subplanner Scratchpad
Must track:- Parent goal: The parent task’s acceptance criteria
- Scope coverage: Which files are addressed/pending/deferred
- Subtask status: completed/failed/in-progress for each subtask
- Discoveries: Patterns learned from handoffs
- What’s deferred: Parts held back and why
- Concerns: Worker-reported issues needing follow-up
Why Scratchpads Matter
- Survive context compaction: When conversation history grows large, older messages are compacted but the scratchpad persists
- Prevent drift: Forces agents to maintain coherent understanding across iterations
- Enable introspection: Agents can reference their own prior reasoning
Execution Model
1. Planning Loop (Root Planner)
- Triggers replanning when:
- First iteration (initial planning)
- N handoffs received since last plan (default: 3)
- No active work remains but project incomplete
- Uses delta optimization: Only sends changed repo state in follow-ups (saves ~40K chars/iteration)
2. Decomposition (Subplanner)
Complex tasks (scope > 4 files) are routed through a subplanner:- Maximum depth: 3 levels of recursive decomposition
- Scope threshold: Tasks with < 4 files go directly to workers
- Atomic bailout: If LLM returns no subtasks on first iteration, task goes to worker as-is
3. Worker Execution (Sandbox)
Workers run in isolated Modal sandboxes:- Isolated environment: Each worker gets a fresh sandbox with cloned repo
- Full Pi capabilities: All 7 Pi tools (not just the limited 4-tool
codingToolsset) - Safety-net commit: Uncommitted changes are auto-committed before diff extraction
- Post-agent build check: Runs
tsc --noEmitto detect compile failures early
4. Reconciliation Loop
Reconciler runs periodically (default: 5 minutes, adaptive):- Starts at
maxIntervalMs(5 minutes) - Drops to
minIntervalMs(1 minute) when errors detected - Returns to
maxIntervalMsafter 3 consecutive green sweeps
Extension Points
1. Custom System Prompts
Each agent type accepts a custom system prompt:prompts/*.md files:
prompts/root-planner.mdprompts/subplanner.mdprompts/worker.mdprompts/reconciler.md
2. Agent Callbacks
All agents expose lifecycle hooks:3. Dependency Injection
Planner and Reconciler accept dependency injection for testing:Next Steps
- Root Planner — Detailed root planner behavior and prompt engineering
- Subplanner — Recursive decomposition and scope management
- Worker — Implementation agent design and sandbox execution
- Reconciler — Build/test monitoring and fix task generation