Skip to content

fix(coderd/x/plugins): deliver plugin events only to the active link - #197

Merged
ibetitsmike merged 14 commits into
mainfrom
mike/plugins-followups-link-routing
Nov 11, 2026
Merged

ibetitsmike merged 14 commits into
mainfrom
mike/plugins-followups-link-routing

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Sends a plugin chat event only to the link that follows that chat, not to every link the chat owner has.

Before this change, deliverChat sent each event to every plugin user link of the chat owner. Then SetPluginUserLinksActiveChatByUser pointed all of the owner's links at the delivered chat. With two links on one plugin, both links got every event, and a message in one chat took over the other link's active chat.

Now an event for chat X goes to the link whose active chat is X:

  • Routing. No caller of the by-user update remains, because new chats already set each link's own active chat. The query, its sqlc output, RBAC and metrics wrappers, mock method and test case are removed, so chore(coderd/database/forkdb): three fork queries have no production caller #167 gets no new orphan query.
  • Chats no link follows. A chat that no link follows (for example, one the user started in Coder) goes to the owner's only idle link: delivery points that link at the chat, then sends the event. A plugin without the chats create grant can reach a link only this way. Known gap: if the owner has several idle links, the chat isn't delivered. Delivery logs at debug level and the cursor advances, because picking one link could show the chat to the wrong identity. A chat a plugin started is never moved to another link: if its own link stops following it, delivery skips it. Bootstrapping happens only before a chat's first delivery: once an event reached a link, the new plugin_chat_bindings.delivered flag stops the chat from going to any other link, because one owner's links can reach different people. Every binding that exists at upgrade starts as delivered.
  • Legacy data. The chatd migration dedup_plugin_link_active_chats clears the active chat of every link in a group that shares one active chat. The by-user update could leave groups like that. The migration also adds the partial unique index plugin_user_links_active_chat_idx (plugin_id, active_chat_id), so at most one link follows a chat. A second migration, plugin_chat_binding_delivered, adds the delivered flag. Bindings that exist at upgrade start as delivered through the column default, so the table isn't rewritten. New bindings start undelivered.
  • Concurrency. Delivery holds callMu from the link lookup through the event call. Without it, a concurrent webhook host call could move the link between the two steps.

The test plugin wasm is also built before the test deadline starts. plugintest.BuildWasm runs go build and ignores the context, so a cold CI build outlasts WaitSuperLong before Load starts. In chat-tests that showed up as context deadline exceeded.

Testing:

  • TestDeliverChatRoutesToActiveLink loads the bridge plugin in-process. Its table cases cover: only the active link receives the event, a single idle link gets the chat and the event, several idle links get nothing, a plugin-started chat that no link follows gets nothing, and a chat that already reached a link gets nothing once no link follows it. Each case checks whether the cursor update records a delivery. TestPluginChatBindings checks that the delivered flag survives a later update that reached no link. Breaking the idle-link count check, the plugin-label check, the delivered check, the delivery record, or the flag's persistence in SQL makes the matching test fail.
  • TestPluginUserLinkActiveChatUnique (forkdb) checks the unique index.
  • go test ./coderd/x/plugins/ passes.

Builds on #194 (merged). Part of #172.

Xum opened this PR on behalf of @ibetitsmike.

DeleteChatWakeupByID, UpdateChatWakeupRunStatus and
DeleteExpiredPluginLinkCodes have no production callers. Regenerate the
sqlc output and drop the RBAC, metrics, mock and test cases that only
existed for them. Part of #167.
TestChatWakeupRuns reached a delivered run through
UpdateChatWakeupRunStatus and counted runs after DeleteChatWakeupByID;
TestPluginLinkCodes counted rows to verify DeleteExpiredPluginLinkCodes.
Both queries have no production callers and are removed (#167), so insert
the delivered run directly and drop the purge-count assertion.
One Coder user may link several external identities to the same plugin,
and each identity follows its own active chat. deliverChat broadcast
every assistant reply and status change to all of the owner's links and
then overwrote every link's active chat with the delivered chat, so a
message from either external account continued the other account's
conversation. Deliver only to the links whose active chat is the chat
being delivered, and drop the by-user overwrite: the active chat is set
per link when a chat is created.

Remove the now-unused SetPluginUserLinksActiveChatByUser query and its
RBAC, metrics, mock and test plumbing. Part of #172 (thread 3).
…eadline

plugintest.BuildWasm shells out to go build and does not observe the
context; a cold build on CI can outlast WaitSuperLong, so Load received
an already-expired deadline. Build first, then start the clock.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T14:25:08.148901Z 723e45d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d078523087

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/plugins/delivery.go Resolved
The by-user active-chat update pointed every link of a user at the chat
it last delivered to, so upgraded deployments keep several links of one
user on the same chat. Delivery now selects links by active_chat_id
equality and that only reaches a single identity while duplicates
remain. Keep the first link by external_id per chat and clear the rest;
a cleared link re-points at the next chat started from it.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum requested this review on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: da7b0f6131

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

…hema

TestSQLCSchemaOrder requires sqlc to read every fork node in graph
order.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum requested this review on behalf of @ibetitsmike.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8cef50262e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/plugins/delivery.go Resolved
Comment thread coderd/x/plugins/delivery.go Resolved
Clear every link of a group that shares an active chat instead of
keeping one: nothing records which identity opened a shared chat. A
partial unique index on (plugin_id, active_chat_id) holds the invariant,
including against a replica still on the previous release during a
rolling upgrade. Delivery reads the links and calls the plugin under
callMu, the lock host calls run under when a webhook moves a link to a
new chat, so a concurrent move cannot leave delivery on a stale link.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum requested this review on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 93bf23c8f9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Base automatically changed from mike/plugins-followups-unused-queries to main November 11, 2026 13:43
…s-link-routing

# Conflicts:
#	coderd/database/forkdb/plugins_test.go
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum requested this review on behalf of @ibetitsmike.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b88c501976

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/plugins/delivery.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum acted for Mike on this comment.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c844db5fe4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/plugins/delivery.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum acted for Mike on this comment.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9df0731db2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/plugins/delivery.go Resolved
Comment thread coderd/x/plugins/delivery.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum acted for Mike on this comment.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 723e45d415

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike merged commit 7fda649 into main Nov 11, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/plugins-followups-link-routing branch November 11, 2026 15:22
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum merged this PR on behalf of @ibetitsmike.

Merged as a squash of head 7c6ac202d9, on top of main cd278dc131 (#156).

  • CI: fork-ci run 43045404372 passed on the exact head. The lead merged main into the branch with update-branch; this PR's changed lines match the UAT'd head 723e45d415 exactly.
  • UAT: UAT197 passed at 723e45d415 on two fresh branch servers. On the first, S1 through S5 delivered every plugin event only to its rightful link, including the sticky and ambiguous-chat cases. On the second, the old build reproduced the shared active chat bug; after upgrading the same database, existing bindings counted as delivered, the duplicate pointers were cleared, and new chats bootstrapped correctly. Leak audits were clean.
  • Codex: the verdict on 723e45d415 had no findings. Two remaining P1 threads covered abnormal paths that fall outside the fork's scope rule; both are replied and resolved. 0 unresolved threads.

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