Prompt Pipeline

The prompt pipeline assembles a comprehensive system prompt for each agent session. It gathers environment information, role instructions, tool descriptions, pending messages, beads tasks, and session context into a structured multi-section prompt that guides the agent's behavior.

Overview

The build_prompt() function in prompt.rs orchestrates prompt assembly. Each time an agent transitions to BuildingPrompt, a fresh prompt is assembled from current state — there is no cached or incremental prompt. This ensures the agent always sees the latest messages, tasks, and environment.

Prompt Sections

The prompt is assembled from up to 14 numbered sections, each conditionally included based on available context:

#SectionContentConditional
1IdentityAgent name, swarm contextAlways
2Agent RoleThe agent's configured prompt textAlways
3Mode InstructionsBehavior rules for the agent's execution mode (code, delegate, etc.)When mode is set
4Workflow ContextCurrent workflow stage, inputs, and constraintsWhen in a workflow
5Project InstructionsContents of AGENTS.md from the project rootWhen file exists
6Core MandatesUniversal rules: commit discipline, branch hygiene, communication protocolAlways
7Doing TasksHow to approach coding tasks, use tools, handle errorsAlways
8Tool Usage PolicyRules for tool selection, permission handlingAlways
9Swarm WorkflowInter-agent communication protocol, when to message teammatesAlways
10Tone & StyleOutput formatting guidelinesAlways
11EnvironmentPlatform, OS, git status, recent commits, working directoryAlways
12MessagesPending messages from other agents (consumed from mailbox)When messages exist
13Beads TasksAvailable tasks from bd ready --jsonWhen beads is available
14Session ContextSession ID, session sequence, interrupt contextAlways

PromptContext

The PromptContext struct carries all the data needed for prompt assembly:

FieldDescription
agent_nameName of the agent being prompted
agent_promptThe agent's configured prompt text
modeAgent execution mode
session_idCurrent session ID
session_seqSession iteration number
db_pathPath to the SQLite mailbox database
worktree_pathAgent's worktree directory
agent_namesList of all agents in the session
workflow_contextOptional workflow stage context
interrupt_contextOptional interrupt reason

Environment Information

The EnvironmentInfo struct gathers runtime context:

FieldSource
platformstd::env::consts::OS
os_versionuname -r output
shell$SHELL environment variable
cwdCurrent working directory
git_statusgit status --short output
recent_commitsgit log --oneline -5 output
dateCurrent date

Message Formatting

Pending messages are consumed from the mailbox and formatted with urgency labels:

## Messages from teammates

[URGENT] From backend (2m ago):
The API endpoint /users is returning 500 errors, please check the database migration.

From frontend (5m ago):
I've finished the login page UI, ready for API integration.

Urgent messages are prefixed with [URGENT] to draw the agent's attention.

Beads Task Integration

When the bd CLI is available, the prompt pipeline runs bd ready --json with a timeout to discover available tasks:

## Available Tasks (from beads)

- SWARM-42: [open] Implement user authentication endpoint
- SWARM-43: [open] Add input validation to signup form
- SWARM-44: [in_progress] Write integration tests for login flow

This allows agents to autonomously pick up and work on tracked issues.

Interrupt Context

When an agent is interrupted (due to an urgent message), the interrupt context is included in the rebuilt prompt:

## Interrupt Context

You were interrupted by an urgent message. Your previous session was cancelled
so you could process this message. Review the messages section above and respond
to the urgent request.
  • Agent Lifecycle — When prompts are built in the state machine
  • Messaging — How messages are consumed into the prompt
  • Configuration — Agent prompt configuration
  • Skills — How skills inject into the prompt context