|
| 1 | +# Future: Async Message Visibility in Debug-Agent |
| 2 | + |
| 3 | +## Current Limitation |
| 4 | + |
| 5 | +The debug-agent (and any pi session) operates in a **synchronous turn-based model**: |
| 6 | +- When processing a user prompt, the LLM generates a response |
| 7 | +- During this generation, incoming `send_to_session` messages are queued |
| 8 | +- These queued messages are **invisible** until the current turn completes |
| 9 | +- Only then does pi process the queue and present them as new user prompts |
| 10 | + |
| 11 | +This creates confusion in debug/observability scenarios where admins expect real-time interaction. |
| 12 | + |
| 13 | +## Short-term Mitigation (this PR) |
| 14 | + |
| 15 | +- Document the limitation clearly |
| 16 | +- Add `/ready` command to signal completion |
| 17 | +- Advise keeping responses short |
| 18 | +- Provide socket checking workarounds |
| 19 | + |
| 20 | +## Long-term Solutions |
| 21 | + |
| 22 | +### Option 1: Pi Core Enhancement - Message Queue API |
| 23 | + |
| 24 | +Extend pi's `ExtensionAPI` to expose pending message count: |
| 25 | + |
| 26 | +```typescript |
| 27 | +interface ExtensionAPI { |
| 28 | + // ... existing methods |
| 29 | + |
| 30 | + /** |
| 31 | + * Get count of pending send_to_session messages in queue |
| 32 | + * Returns 0 if no messages pending, N if messages queued |
| 33 | + */ |
| 34 | + getPendingMessageCount(): number; |
| 35 | + |
| 36 | + /** |
| 37 | + * Get preview of pending messages (if available) |
| 38 | + * Useful for dashboard indicators |
| 39 | + */ |
| 40 | + getPendingMessagePreviews(): Array<{ |
| 41 | + from: string; // sender session ID or name |
| 42 | + preview: string; // first 50 chars |
| 43 | + timestamp: Date; |
| 44 | + }>; |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +Then the dashboard could show: |
| 49 | +``` |
| 50 | +┌─────────────────────────────────────────────────────┐ |
| 51 | +│ 📬 3 messages queued (complete turn to process) │ |
| 52 | +│ • control-agent: "can you check..." │ |
| 53 | +│ • user: "status update?" │ |
| 54 | +└─────────────────────────────────────────────────────┘ |
| 55 | +``` |
| 56 | + |
| 57 | +**Implementation**: Requires changes to pi's session control layer (WebSocket/message routing) |
| 58 | + |
| 59 | +### Option 2: Streaming Response with Message Interruption |
| 60 | + |
| 61 | +More ambitious - allow interrupting mid-generation: |
| 62 | + |
| 63 | +```typescript |
| 64 | +pi.on("message_received_during_turn", async (event, ctx) => { |
| 65 | + // Opportunity to abort current generation |
| 66 | + // Present queued message immediately |
| 67 | + // Or queue for "after this sentence" |
| 68 | +}); |
| 69 | +``` |
| 70 | + |
| 71 | +**Challenges**: |
| 72 | +- Requires streaming/incremental generation support |
| 73 | +- Complex state management (partial responses) |
| 74 | +- May confuse conversation context |
| 75 | + |
| 76 | +### Option 3: Parallel Session Mode |
| 77 | + |
| 78 | +Create a "monitor" mode for debug-agent that spawns a parallel session: |
| 79 | + |
| 80 | +```typescript |
| 81 | +// Main session: normal turn-based interaction |
| 82 | +// Monitor session: async-only, polls for messages every 2s |
| 83 | + |
| 84 | +pi.registerExtension("async-monitor", { |
| 85 | + async init(ctx) { |
| 86 | + if (ctx.sessionRole === "debug-observer") { |
| 87 | + setInterval(() => { |
| 88 | + // Poll session control for pending messages |
| 89 | + // Display in dashboard without blocking main turn |
| 90 | + }, 2000); |
| 91 | + } |
| 92 | + } |
| 93 | +}); |
| 94 | +``` |
| 95 | + |
| 96 | +**Pros**: No pi core changes needed |
| 97 | +**Cons**: Complex dual-session architecture, more resources |
| 98 | + |
| 99 | +### Option 4: Event-Driven Dashboard Widget |
| 100 | + |
| 101 | +The dashboard widget already runs outside the LLM turn. Enhance it to: |
| 102 | +1. Listen on the session socket directly (bypassing pi's queue) |
| 103 | +2. Display pending messages in the widget itself (not as user prompts) |
| 104 | +3. Use `/accept-message <id>` command to pull from queue into conversation |
| 105 | + |
| 106 | +**Pros**: Clean separation of monitoring vs. conversation |
| 107 | +**Cons**: Two different interaction modes (in-chat vs. dashboard commands) |
| 108 | + |
| 109 | +## Recommendation |
| 110 | + |
| 111 | +**Phase 1** (this PR): Documentation + workarounds |
| 112 | +**Phase 2**: Implement Option 1 (message queue API in pi core) |
| 113 | +**Phase 3**: Consider Option 4 if Option 1 proves insufficient |
| 114 | + |
| 115 | +Option 1 is the cleanest long-term solution as it: |
| 116 | +- Preserves pi's turn-based model |
| 117 | +- Adds visibility without changing core behavior |
| 118 | +- Minimal API surface area |
| 119 | +- Useful for all session types, not just debug-agent |
| 120 | + |
| 121 | +## Related Pi Issues |
| 122 | + |
| 123 | +- (TODO: check if @mariozechner/pi-coding-agent has issue tracker) |
| 124 | +- Consider proposing this enhancement upstream |
0 commit comments