| 1532 | // recentPostedUUIDs) — the index cursor here is a pre-filter to avoid |
| 1533 | // O(n) re-scanning of already-sent messages on every call. |
| 1534 | function forwardMessagesToBridge(): void { |
| 1535 | if (!bridgeHandle) return |
| 1536 | // Guard against mutableMessages shrinking (compaction truncates it). |
| 1537 | const startIndex = Math.min( |
| 1538 | bridgeLastForwardedIndex, |
| 1539 | mutableMessages.length, |
| 1540 | ) |
| 1541 | const newMessages = mutableMessages |
| 1542 | .slice(startIndex) |
| 1543 | .filter(m => m.type === 'user' || m.type === 'assistant') |
| 1544 | bridgeLastForwardedIndex = mutableMessages.length |
| 1545 | if (newMessages.length > 0) { |
| 1546 | bridgeHandle.writeMessages(newMessages) |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | // Helper to apply MCP server changes - used by both mcp_set_servers control message |
| 1551 | // and background plugin installation. |