fix: only handle turns initiated by this bridge (multi-bridge cross-posting)#3
Open
pg-ferrari wants to merge 1 commit into
Open
fix: only handle turns initiated by this bridge (multi-bridge cross-posting)#3pg-ferrari wants to merge 1 commit into
pg-ferrari wants to merge 1 commit into
Conversation
When multiple bridge extensions (e.g. Telegram + Slack) run in the same pi process, they share one conversation and listen to the same global pi events. Each bridge gated its output only on its own module-level pendingChat, so a stale pendingChat on one bridge caused it to cross-post replies that were meant for the other platform. Track the owner of the current turn via a shared globalThis key. Each bridge claims ownership when it forwards a message and only reacts to turn/message events for turns it initiated, releasing ownership when the turn completes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When more than one bridge extension runs in the same pi process (for example this Telegram bridge alongside a Slack bridge), they share a single conversation and all listen to the same global pi events (
turn_start,message_start,message_update,turn_end,tool_execution_end).Each bridge only gated its output on its own module-level
pendingChat. If one bridge had a stalependingChatfrom an earlier conversation, it would also fire on a turn that the other bridge initiated, cross-posting a reply to the wrong platform.Fix
Coordinate turn ownership through a shared
globalThiskey (__PI_BRIDGE_ACTIVE_OWNER):forwardToPi()when it sends a user message.turn_start(stable for the whole turn, even if the other bridge claims afterwards).turn_endwith no pending tool calls) and on/abort.This is backwards-compatible: a single bridge running alone always owns its own turns, so behavior is unchanged.
Note: the cooperating bridge needs to use the same
__PI_BRIDGE_ACTIVE_OWNERkey with its own id for the coordination to take effect.