Manifest parity · team-rocket-sop-manifest.json
Alloy platform · Team Rocket Grant
Team Rocket Bot — Standard Operating Structure
End-to-end flow: user input → entitlement → context → agentic loop → final output. Production runtime is Python in serverpilot-discord-bot/; the TypeScript stack in packages/bot/ mirrors the same pipeline. Team Rocket Grant (Discord persona Grunt).
1. Identity Layer (Always On)
Before any message is processed, Grunt carries a fixed Team Rocket persona:
| Surface | Path |
|---|---|
| Live system prompt | data/persona.md |
| Master template | templates/team-rocket-master-prompt.md |
| Hardcoded fallback | cogs/chatops.py → _PERSONA_DEFAULT |
Operating rule: Grunt is a Team Rocket operative — theatrical, in-character, not a corporate assistant. Admin work still happens via tools, but voice stays in character. Persona is injected during Phase 7 — Context build as the base system layer.
2. Input Surfaces (How a Run Starts)
A run starts when Discord delivers a signal the bot is configured to accept.
flowchart TD
U[User signal] --> D[Discord Gateway]
D --> B[bot.py → ChatOps]
B --> G{Listen gate}
G -->|no match| X[Ignore]
G -->|match| P[Pipeline]
Guild chat (on_message)
The bot responds only when at least one trigger matches:
| Trigger | Condition |
|---|---|
| @mention | Bot user is mentioned |
| Reply | User replies to a bot message |
| Name-drop | BOT_NAME in text (default: Grunt) |
| Watched user | Author ID in WATCHED_USER_IDS |
Also accepted: text + image attachments, or reply-chain images.
Other entry points
| Entry | Behavior |
|---|---|
| /ask slash | Same pipeline as guild chat; no listen gate |
| DMs (no guild) | Consulting discovery handler — not main harness path |
Dropped signals
- Bot messages
- Empty prompts with no images (except watched-user rules)
- Non-members when entitlements apply
- Messages that fail the listen gate
3. End-to-End Pipeline (Happy Path)
sequenceDiagram
participant User
participant Discord
participant Listen
participant Entitle
participant Respond
participant Context
participant Harness
participant Agentic
participant LLM
participant Tools
participant Output
User->>Discord: message / attachment / /ask
Discord->>Listen: on_message
Listen->>Entitle: tier + quota
Entitle->>Respond: _respond_inner
Respond->>Respond: image fast path? (optional)
Respond->>Context: build_context + pick_tools
Respond->>Harness: session + trace route
Respond->>Agentic: _run_agentic
loop until done
Agentic->>LLM: chat + tool schemas
LLM-->>Agentic: text and/or tool_calls
Agentic->>Tools: _dispatch
Tools-->>Agentic: results
end
Agentic->>Output: plain-text reply + trace embeds
4. Phase Map
| # | Phase | What happens | Stop condition |
|---|---|---|---|
| 0 | Gateway ingest | discord.py delivers message to ChatOps.on_message | Bot author → stop |
| 1 | Listen gate | Check mention / reply / name-drop / watched | No trigger → ignore |
| 2 | Entitlement gate | Resolve tier from roles; check daily/monthly quota | Quota exceeded → quota embed |
| 3 | Prompt extraction | Strip mentions; merge reply-chain image URLs | Empty + no images → stop |
| 4 | Session key | (guild_id, channel_id, user_id); thread reply may bind existing harness session (I2) | — |
| 5 | Image fast path | Regex detects explicit generate/edit → OpenAI image API | Handled → image embed, skip LLM |
| 6 | Tool selection | Keyword router picks tools; tier / plan-mode / owner filters | Watched → zero tools |
| 7 | Context build | Assemble system prompt layers (see §5) | — |
| 8 | Harness bind | Trace route; grunt-work + mirror threads; run_started to control plane | Skipped for watched users |
| 9 | Agentic loop | Multi-turn LLM ↔ tool cycle | Final text or max turns |
| 10 | Output | Plain-text reply + progressive trace embeds | — |
| 11 | Persistence | Session history, harness JSON, usage metering, CP events | — |
5. Permission Branching
flowchart LR
M[Message] --> W{Watched?}
W -->|yes| WC[OpenAI chat-only
no tools, no harness]
W -->|no| E[Entitlement gate]
E --> T{Tier}
T --> O[Owner: all tools, bypass quota]
T --> A[Admin: 2/day]
T --> B[Booster: 10/day]
T --> D[Default: 1/week]
O & A & B & D --> F[filter_tools_for_tier]
F --> DIS[_dispatch gates]
| Tier | Quota | Tools |
|---|---|---|
| Owner | Bypass | All groups + host control + harness full mode |
| Admin | 2/day | Messaging, discord_admin, … |
| Booster | 10/day | Knowledge, media, utility |
| Default | 1/week | Knowledge, media only |
- Harness full mode (orchestrator, host tools, /harness) → owner-only, regardless of tier
- Watched users (I3) → forced chat-only: OpenAI, no tools, no harness, no trace threads
6. Context Assembly (What the LLM Sees)
build_context() in cogs/_context.py stacks layers in order:
- Persona — Team Rocket Grunt system prompt (persona.md)
- Provider awareness — which model/provider is active
- Library blocks — topical recall from configured library channels (I4)
- Long-term memory — facts from bot-remember / memory file
- Session history — prior turns (compacted if near context limit)
- Current user message — text + optional vision image blocks
Tool schemas are attached only when use_tools is on and the user is not on the watched path.
7. Agentic Loop (Core Reasoning Engine)
When use_agentic_loop + tools are enabled:
┌─────────────────────────────────────────┐ │ 1. LLM call (system + history + tools) │ │ 2. Parse response: text + tool_calls? │ │ 3. If tool_calls: │ │ a. Optional confirmation gate │ │ b. _dispatch each tool │ │ c. Append results to message history│ │ d. Stream progress to trace embeds │ │ 4. Repeat until no tool_calls or cap │ │ 5. Return final_text │ └─────────────────────────────────────────┘
Dispatch enforcement stack (bottom → top)
| Layer | Check |
|---|---|
| 1 | Entitlement tier filter |
| 2 | Owner gate (host / MCP / destructive ops) |
| 3 | tool_config.json (enabled, locked params, needsApproval) |
| 4 | Harness lane policy (specialist allowlists) |
| 5 | Confirmation gate (_ConfirmView) |
| 6 | Execute (Discord admin · host · project · MCP · web · images) |
| 7 | Audit log + control-plane step event |
LLM routing (LLMRouter)
| Need | Default provider |
|---|---|
| Chat | DeepSeek |
| Vision (images in message) | OpenAI (auto-reroute) |
| Image generate/edit (fast path or tool) | OpenAI |
Sub-agent branch (owner harness)
When orchestrator is active:
flowchart LR
COORD[Coordinator _run_agentic]
ORCH[SubAgentOrchestrator.plan_lanes]
L1[network-engineer lane]
L2[security-expert lane]
SYN[synthesize]
COORD --> ORCH
ORCH --> L1
ORCH --> L2
L1 --> SYN
L2 --> SYN
SYN --> COORD
Each lane: read-only tools, ~1400 char output cap, optional forum trace thread via trace_lanes.py.
8. Trace & Observability (Parallel to the Answer)
For non-watched agentic runs, _resolve_trace_route (I1 — sole thread factory) creates:
| Artifact | Purpose |
|---|---|
| Grunt-work thread | Forum post with full reasoning + tool progress |
| Mirror thread | Condensed copy on the invocation message |
| Inline fallback | If forum routing disabled |
_TraceSession streams embeds: plan → tool_start / tool_done → lane_spawn / lane_done → synthesis → final.
Side channel (non-blocking): HarnessManager POSTs run events to control plane at 127.0.0.1:8787 for Agent Studio / dashboard. Failure does not block the Discord reply.
9. Output Surfaces (What the User Receives)
| Output type | When | Where |
|---|---|---|
| Plain-text reply | Always (primary answer) | Invocation channel — reply to user |
| Trace embeds | Agentic run with trace route | Grunt-work forum + mirror thread |
| Image embed | Fast path or image tools | Invocation channel |
| Quota embed | Entitlement block | Invocation channel |
| Confirmation prompt | Destructive / approval-gated tools | Invocation channel (approve/deny buttons) |
Final answer rule: Grunt's in-character text is posted separately from trace machinery — users see the personality; operators see the work log.
10. Alternate Paths (Branch Summary)
Branch summary — all optional routes from user input to Discord output:
flowchart TD
IN[User input] --> L{Listen?}
L -->|no| DROP[Ignore]
L -->|yes| W{Watched?}
W -->|yes| OAI[OpenAI single-turn chat]
W -->|no| Q{Quota OK?}
Q -->|no| QE[Quota embed]
Q -->|yes| IMG{Image fast path?}
IMG -->|yes| IE[OpenAI image API → embed]
IMG -->|no| AG{Agentic + tools?}
AG -->|yes| LOOP[Agentic loop]
AG -->|no| CHAT[Single-turn chat]
OAI & LOOP & CHAT & IE --> OUT[Discord output]
11. Persistence After Output
| Store | What | Path |
|---|---|---|
| Session history | Last N turns per (guild, channel, user) | In-memory SessionStore |
| Harness sessions | Thread bindings, run IDs, lane state | data/harness/sessions.json |
| Usage metering | Tier quota counters | Entitlements store |
| Host audit | Shell/file commands | data/host_audit.json |
| Tool config | Per-tool enable/lock/approval | data/tool_config.json |
| Persona | Live Grunt voice | data/persona.md |
Thread continuity (I2): Replying inside a bound harness thread continues the same session without resetting context.
12. One-Page Operator Checklist
Normal @mention in guild chat
- User addresses Grunt (mention, reply, or name-drop)
- Bot checks tier/quota
- Bot picks tools by intent + tier
- Bot loads Team Rocket persona + library + memory + history
- Bot opens trace threads (if agentic)
- LLM thinks in character, calls tools as needed
- User gets Grunt's plain-text reply
- Operators see full trace in grunt-work forum
- Session + harness state saved for next turn
Chat-only (no admin)
User asks a casual question → keyword router sends minimal/no tools → single or short agentic loop → text reply only.
Admin action
User asks to create channel / assign role / etc. → full tool schemas → confirmation if destructive → tool executes → Grunt complains in character, reports result.
13. Key Files (Source of Truth)
| Role | Path |
|---|---|
| Entry + listen gate | serverpilot-discord-bot/cogs/chatops.py |
| Entitlements | serverpilot-discord-bot/cogs/_entitlements.py |
| Context / persona | serverpilot-discord-bot/cogs/_context.py |
| LLM routing | serverpilot-discord-bot/cogs/_llm_router.py |
| Harness | serverpilot-discord-bot/cogs/_harness/manager.py |
| Team Rocket prompt | serverpilot-discord-bot/templates/team-rocket-master-prompt.md |
| Dev walkthrough | brain/01-Developer-Guide/02 — Message Flow Walkthrough.md |
| Signal pipeline spec | docs/investor/TrainerLabs-Signal-Pipeline.md |