# Gas Town Mail Protocol > Reference for inter-agent mail communication in Gas Town ## Overview Gas Town agents coordinate via mail messages routed through the beads system. Mail uses `type=message` beads with routing handled by `gt mail`. ## Message Types ### POLECAT_DONE **Route**: Polecat → Witness **Purpose**: Signal work completion, trigger cleanup flow. **Subject format**: `POLECAT_DONE ` **Body format**: ``` Exit: MERGED|ESCALATED|DEFERRED Issue: MR: # if exit=MERGED Branch: ``` **Trigger**: `gt polecat done` command generates this automatically. **Handler**: Witness creates a cleanup wisp for the polecat. ### MERGE_READY **Route**: Witness → Refinery **Purpose**: Signal a branch is ready for merge queue processing. **Subject format**: `MERGE_READY ` **Body format**: ``` Branch: Issue: Polecat: Verified: clean git state, issue closed ``` **Trigger**: Witness sends after verifying polecat work is complete. **Handler**: Refinery adds to merge queue, processes when ready. ### MERGED **Route**: Refinery → Witness **Purpose**: Confirm branch was merged successfully, safe to nuke polecat. **Subject format**: `MERGED ` **Body format**: ``` Branch: Issue: Polecat: Rig: Target: Merged-At: Merge-Commit: ``` **Trigger**: Refinery sends after successful merge to main. **Handler**: Witness completes cleanup wisp, nukes polecat worktree. ### MERGE_FAILED **Route**: Refinery → Witness **Purpose**: Notify that merge attempt failed (tests, build, or other non-conflict error). **Subject format**: `MERGE_FAILED ` **Body format**: ``` Branch: Issue: Polecat: Rig: Target: Failed-At: Failure-Type: Error: ``` **Trigger**: Refinery sends when merge fails for non-conflict reasons. **Handler**: Witness notifies polecat, assigns work back for rework. ### REWORK_REQUEST **Route**: Refinery → Witness **Purpose**: Request polecat to rebase branch due to merge conflicts. **Subject format**: `REWORK_REQUEST ` **Body format**: ``` Branch: Issue: Polecat: Rig: Target: Requested-At: Conflict-Files: , , ... Please rebase your changes onto : git fetch origin git rebase origin/ # Resolve any conflicts git push -f The Refinery will retry the merge after rebase is complete. ``` **Trigger**: Refinery sends when merge has conflicts with target branch. **Handler**: Witness notifies polecat with rebase instructions. ### WITNESS_PING **Route**: Witness → Deacon (all witnesses send) **Purpose**: Second-order monitoring - ensure Deacon is alive. **Subject format**: `WITNESS_PING ` **Body format**: ``` Rig: Timestamp: Patrol: ``` **Trigger**: Each witness sends periodically (every N patrol cycles). **Handler**: Deacon acknowledges. If no ack, witnesses escalate to Mayor. ### HELP **Route**: Any → escalation target (usually Mayor) **Purpose**: Request intervention for stuck/blocked work. **Subject format**: `HELP: ` **Body format**: ``` Agent: Issue: # if applicable Problem: Tried: ``` **Trigger**: Agent unable to proceed, needs external help. **Handler**: Escalation target assesses and intervenes. ### HANDOFF **Route**: Agent → self (or successor) **Purpose**: Session continuity across context limits/restarts. **Subject format**: `🤝 HANDOFF: ` **Body format**: ``` attached_molecule: # if work in progress attached_at: ## Context ## Status ## Next ``` **Trigger**: `gt handoff` command, or manual send before session end. **Handler**: Next session reads handoff, continues from context. ## Format Conventions ### Subject Line - **Type prefix**: Uppercase, identifies message type - **Colon separator**: After type for structured info - **Brief context**: Human-readable summary Examples: ``` POLECAT_DONE nux MERGE_READY gastown/nux HELP: Polecat stuck on test failures 🤝 HANDOFF: Schema work in progress ``` ### Body Structure - **Key-value pairs**: For structured data (one per line) - **Blank line**: Separates structured data from freeform content - **Markdown sections**: For freeform content (##, lists, code blocks) ### Addresses Format: `/` or `//` Examples: ``` gastown/witness # Witness for gastown rig beads/refinery # Refinery for beads rig gastown/polecats/nux # Specific polecat mayor/ # Town-level Mayor deacon/ # Town-level Deacon ``` ## Protocol Flows ### Polecat Completion Flow ``` Polecat Witness Refinery │ │ │ │ POLECAT_DONE │ │ │─────────────────────────>│ │ │ │ │ │ (verify clean) │ │ │ │ │ │ MERGE_READY │ │ │─────────────────────────>│ │ │ │ │ │ (merge attempt) │ │ │ │ │ MERGED (success) │ │ │<─────────────────────────│ │ │ │ │ (nuke polecat) │ │ │ │ ``` ### Merge Failure Flow ``` Witness Refinery │ │ │ (merge fails) │ │ │ MERGE_FAILED │ ┌──────────────────────────│<─────────────────────────│ │ │ │ │ (failure notification) │ │ │<─────────────────────────│ │ │ │ │ Polecat (rework needed) ``` ### Rebase Required Flow ``` Witness Refinery │ │ │ (conflict detected) │ │ │ REWORK_REQUEST │ ┌──────────────────────────│<─────────────────────────│ │ │ │ │ (rebase instructions) │ │ │<─────────────────────────│ │ │ │ │ Polecat │ │ │ │ │ │ (rebases, gt done) │ │ │─────────────────────────>│ MERGE_READY │ │ │─────────────────────────>│ │ │ (retry merge) ``` ### Second-Order Monitoring ``` Witness-1 ──┐ │ WITNESS_PING Witness-2 ──┼────────────────> Deacon │ Witness-N ──┘ │ (if no response) │ <────────────────────┘ Escalate to Mayor ``` ## Implementation ### Sending Mail ```bash # Basic send gt mail send -s "Subject" -m "Body" # With structured body gt mail send gastown/witness -s "MERGE_READY nux" -m "Branch: feature-xyz Issue: gt-abc Polecat: nux Verified: clean" ``` ### Receiving Mail ```bash # Check inbox gt mail inbox # Read specific message gt mail read # Mark as read gt mail ack ``` ### In Patrol Formulas Formulas should: 1. Check inbox at start of each cycle 2. Parse subject prefix to route handling 3. Extract structured data from body 4. Take appropriate action 5. Mark mail as read after processing ## Extensibility New message types follow the pattern: 1. Define subject prefix (TYPE: or TYPE_SUBTYPE) 2. Document body format (key-value pairs + freeform) 3. Specify route (sender → receiver) 4. Implement handlers in relevant patrol formulas The protocol is intentionally simple - structured enough for parsing, flexible enough for human debugging. ## Related Documents - `docs/agent-as-bead.md` - Agent identity and slots - `.beads/formulas/mol-witness-patrol.formula.toml` - Witness handling - `internal/mail/` - Mail routing implementation - `internal/protocol/` - Protocol handlers for Witness-Refinery communication