Skip to content

fix: only handle turns initiated by this bridge (multi-bridge cross-posting)#3

Open
pg-ferrari wants to merge 1 commit into
siygle:mainfrom
pg-ferrari:fix/bridge-turn-ownership
Open

fix: only handle turns initiated by this bridge (multi-bridge cross-posting)#3
pg-ferrari wants to merge 1 commit into
siygle:mainfrom
pg-ferrari:fix/bridge-turn-ownership

Conversation

@pg-ferrari

Copy link
Copy Markdown

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 stale pendingChat from 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 globalThis key (__PI_BRIDGE_ACTIVE_OWNER):

  • A bridge claims ownership in forwardToPi() when it sends a user message.
  • Each bridge captures whether it owns the turn at turn_start (stable for the whole turn, even if the other bridge claims afterwards).
  • All output handlers early-return when the bridge does not own the current turn.
  • Ownership is released when the turn completes (turn_end with 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_OWNER key with its own id for the coordination to take effect.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant