| 1515 | // recentPostedUUIDs) — the index cursor here is a pre-filter to avoid |
| 1516 | // O(n) re-scanning of already-sent messages on every call. |
| 1517 | function forwardMessagesToBridge(): void { |
| 1518 | if (!bridgeHandle) return |
| 1519 | // Guard against mutableMessages shrinking (compaction truncates it). |
| 1520 | const startIndex = Math.min( |
| 1521 | bridgeLastForwardedIndex, |
| 1522 | mutableMessages.length, |
| 1523 | ) |
| 1524 | const newMessages = mutableMessages |
| 1525 | .slice(startIndex) |
| 1526 | .filter(m => m.type === 'user' || m.type === 'assistant') |
| 1527 | bridgeLastForwardedIndex = mutableMessages.length |
| 1528 | if (newMessages.length > 0) { |
| 1529 | bridgeHandle.writeMessages(newMessages) |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | // Helper to apply MCP server changes - used by both mcp_set_servers control message |
| 1534 | // and background plugin installation. |