Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: repair stale chat agent bindings after workspace rebuild - #28152

Merged
ibetitsmike merged 10 commits into
mainfrom
mike/fix-stale-chat-agent-binding
Aug 16, 2026
Merged

fix: repair stale chat agent bindings after workspace rebuild#28152
ibetitsmike merged 10 commits into
mainfrom
mike/fix-stale-chat-agent-binding

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Problem

When a chat is bound to a workspace, chatd persists chats.agent_id pointing at a specific workspace agent, and it only rebinds on the next chat turn. A workspace stop/start creates a new agent with a new ID in the latest build, so the chat page resolves the stale agent ID to undefined and the right sidebar silently drops Terminal, Desktop, Browser, apps, and ports even though the workspace is running. The existing read-time enrichment only filled nil agent IDs and skipped stale non-nil ones, so refreshing did not help until the user sent another message.

Fix

  • coderd/exp_chats.go: single-chat reads now repair agent IDs that no longer resolve in the workspace's latest build, using the same agentselect.FindChatAgent selection chatd uses. A repaired binding also carries the latest build's ID so the response never pairs the new agent with the previous build. Bindings that still resolve are preserved, and repair stays best-effort and response-only (no write-on-read). List reads keep the previous nil-fill-only behavior because validating existing bindings would cost a per-workspace authorization lookup per listed chat.
  • site/src/pages/AgentsPage/AgentChatPage.tsx: the workspace watch update handler detects when a running workspace's latest build no longer contains the chat's bound agent and invalidates the chat query once per chat/build/binding key for immediate recovery, and the chat query polls every 30 seconds while the binding remains unresolved so a transiently failed repair retries even when an idle workspace publishes no further watch events. The watch stream replays the current workspace on every (re)connect, so this covers rebuilds that happen while the page is open or disconnected; page loads are covered by the server-side repair. The workspace-watcher bailout now also keys on latest_build.id so a rebuild propagates while the page is open.
  • site/src/api/queries/chats.ts: chat watch events replay the persisted (pre-repair) binding, so the summary merge adopts a snapshot's build_id only when the snapshot agrees on agent_id, keeping the repaired agent/build pair atomic in the caches.

Testing

  • go test ./coderd -run TestEnrichChatAgentIDs covering repair, keep-valid, selection-error, list-mode-skips-bound, and no-workspaces cases.
  • Storybook interaction story RecoversSidebarAfterWorkspaceRebuild exercising the watch-event to chat-refetch to sidebar-recovery flow (verified red without the invalidation, green with it).
  • pnpm test AgentChatPage.test.ts covering the binding-resolution predicate.

Mux created this PR on Mike's behalf.

After an attached workspace is rebuilt, the chat's persisted agent_id
references an agent from the previous build until the next turn rebinds
it. The chat UI resolves the agent by exact ID against the latest
build, so the right panel lost its Terminal, Desktop, Browser, and app
tabs even though the workspace was running.

Repair stale bindings in chat read responses using the same agent
selection chatd uses, and refetch the chat once per build when the
binding no longer resolves in a running workspace.
Reword comments that overclaimed chatd authorship and persisted-binding
repair, drop a test comment that restated its assertions, and flatten
workspace agents once in isChatAgentBindingUnresolved.
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 99295034b2

ℹ️ 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/exp_chats.go Outdated
Comment thread site/src/pages/AgentsPage/AgentChatPage.tsx Outdated
Comment thread site/src/pages/AgentsPage/AgentChatPage.tsx Outdated
Scope binding repair to single-chat reads so chat-list reads keep the
nil-fill-only behavior and avoid a per-workspace authorization lookup
per listed chat. Move the frontend stale-binding detection from an
effect into the workspace watch update handler per FE8, and add a
Storybook interaction story covering rebuild recovery.
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 6c61c74a9f

ℹ️ 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

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: ad482a67f7

ℹ️ 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 site/src/pages/AgentsPage/AgentChatPage.tsx
The query client disables retries, so a transient refetch failure
permanently latched the dedupe key and blocked sidebar recovery until
a reload or another rebuild. Clear the key when the refetch errors so
the next workspace watch event retries.
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 2d22d804fa

ℹ️ 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 site/src/pages/AgentsPage/AgentChatPage.tsx Outdated
Server-side repair is best-effort, so a transient failure returns
HTTP 200 with the stale binding and no query error, which permanently
latched the dedupe key. Replace the latch and the error-state check
with a 30s cooldown per chat/build/binding key so any failed repair
retries on a later watch event without refetching on every event.
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 7396f12876

ℹ️ 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

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 1c48be9c91

ℹ️ 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 site/src/pages/AgentsPage/AgentChatPage.tsx Outdated
An idle workspace publishes no watch events (the stats reporter
returns early without publishing when there are no active sessions),
so event-driven retries alone cannot recover from a transiently
failed repair. Give the chat query a conditional refetchInterval
that polls every 30s only while the binding is unresolved, and
simplify the watch handler back to a once-per-key latch that only
provides immediate repair after a rebuild.
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: fd6e1b5131

ℹ️ 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 marked this pull request as ready for review August 14, 2026 17:34
@ethanndickson
ethanndickson self-requested a review August 14, 2026 17:38

@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: fd6e1b5131

ℹ️ 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/exp_chats.go

@ethanndickson ethanndickson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm fine with this approach if we fix the build id mismatch, and update an existing test to catch it.

Comment thread coderd/exp_chats.go
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 17b88f070d

ℹ️ 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/exp_chats.go
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 74cd0979e8

ℹ️ 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 521c383 into main Aug 16, 2026
32 checks passed
@ibetitsmike
ibetitsmike deleted the mike/fix-stale-chat-agent-binding branch August 16, 2026 18:30
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants