Skip to content

feat: wire chat lifecycle hooks into chatd#27429

Open
ibetitsmike wants to merge 1 commit into
mike/chat-hooks/chatstatefrom
mike/chat-hooks/chatd
Open

feat: wire chat lifecycle hooks into chatd#27429
ibetitsmike wants to merge 1 commit into
mike/chat-hooks/chatstatefrom
mike/chat-hooks/chatd

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Stack Context

Chat lifecycle hooks stack: #27427 (DB schema) -> #27401 (dispatch backend) -> #27428 (chatstate transitions) -> #27429 (this) -> #27430 (API + UI).

This is intentionally one PR: response application, allowed-tools narrowing, and the generation commit path are shared between lifecycle and tool/prompt hooks, so splitting them would force an artificial intermediate rewrite of commitGenerationStep.

What

Grouped by area:

Lifecycle engine (hooks.go, generation.go, options.go)

  • session_start on startup/resume/clear (once per process via sessionStartTracker), stop with one nudge-continue per turn (stopNudgeTracker), pre_compact/post_compact around compaction with summary hints (chatloop.SummaryHint).
  • Hook responses apply transactionally with the step commit: prefix messages, allowed-tools narrowing, fail-closed dispatch errors via FinishError in the same transaction.

Tool and prompt hooks (hooks.go, chatd.go, generation_preparer.go)

  • pre_tool_use preflight for model-issued, pending-dynamic, and local tool calls: allow/deny/input-override decisions, duplicate tool-use ID rejection, denied results synthesized without execution.
  • post_tool_use after execution with tool responses.
  • user_prompt_submit on send/edit/queued promotion: prompt overrides, deny with the hook reason (UserPromptDeniedError.Error() carries the hook's user message so non-HTTP callers such as subagent tools surface it too), queue-time deferral of hook effects, and title recomputation after overrides so redacted prompts never leak into titles.

HTTP error mapping (coderd/exp_chats.go)

  • Create/send/edit/tool-result handlers map UserPromptDeniedError to 403 with the hook's user message, and dispatch failures to a 502 ChatHookDispatchFailedResponse carrying kind: hook_dispatch_failed.

Subagent hooks (subagent.go): user_prompt_submit for child chat creation, fallback titles when hooks rewrite prompts, and spawn-tool dispatch failures propagate as errors (fail closed) instead of degrading into tool responses.

File links after overrides (coderd/exp_chats.go): create/send/edit link only file IDs still present in the persisted or queued content, so attachments dropped by a prompt override are never linked to the chat.

Deployment config and wiring (codersdk/deployment.go, coderd/coderd.go)

  • --ai-chat-hook-url/secret/timeout/enabled behind the agent-lifecycle-hooks experiment, consumed here by the dispatcher construction in coderd.go (experiment gate warning included).
  • Admin setup doc (docs/admin/setup/chat-lifecycle-hooks.md) plus generated CLI/API/type artifacts.
  • ARCHITECTURE.md documents the hook flow and invariants.

Why

This is the behavioral core of the feature: every hook event chatd emits, every response effect it honors, and the client-facing error semantics. The deployment configuration lives here rather than in #27401 so the options ship in the same PR as the code that reads them. Most of the diff is tests (dispatch ordering, deny/override matrices, compaction, stop nudges, subagent spawn, endpoint error mapping).

This PR was prepared by Mux (AI agent) on Mike's behalf.

Copy link
Copy Markdown
Collaborator Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@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: 953ead293d

ℹ️ 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/chatd/hooks.go
Comment thread coderd/x/chatd/chatd.go Outdated
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatstate branch from 9fefafe to e0b7344 Compare July 22, 2026 19:59
@ibetitsmike
ibetitsmike requested a review from Emyrk as a code owner July 22, 2026 19:59
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatd branch from 953ead2 to bbc1484 Compare July 22, 2026 19:59
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatstate branch from e0b7344 to a05cf3c Compare July 22, 2026 20:12
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatd branch from bbc1484 to d0f7244 Compare July 22, 2026 20:12
@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: d0f7244efd

ℹ️ 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/chatd/chatd.go
Comment thread coderd/x/chatd/hooks.go
Comment thread coderd/x/chatd/hooks.go
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatstate branch from a05cf3c to 841eefb Compare July 22, 2026 20:24
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatd branch from d0f7244 to 5250a86 Compare July 22, 2026 20:24
@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: 5250a861a5

ℹ️ 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/chatd/chatd.go
Comment thread coderd/x/chatd/hooks.go
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatd branch from 5250a86 to e62369a Compare July 22, 2026 20:37
@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: e62369a8af

ℹ️ 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/chatd/hooks.go
@Emyrk
Emyrk removed their request for review July 22, 2026 20:47
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatstate branch from 841eefb to e08f7af Compare July 22, 2026 20:51
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatd branch from e62369a to d835b06 Compare July 22, 2026 20:52
@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: d835b06b87

ℹ️ 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/chatd/chatd.go
Comment thread coderd/x/chatd/chatd.go
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatd branch from d835b06 to a3855c0 Compare July 22, 2026 21:27
@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: a3855c04aa

ℹ️ 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/chatd/hooks.go Outdated
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatstate branch from e08f7af to c6e5dff Compare July 22, 2026 22:00
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatd branch from a3855c0 to 08fb016 Compare July 22, 2026 22:00
@github-actions

Copy link
Copy Markdown

Docs preview

Check off each page once it's been reviewed. If a page changes in a later push, its checkbox clears automatically so it gets a fresh look. Pages not yet wired into the docs navigation aren't listed here.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@ibetitsmike ibetitsmike changed the title feat(coderd): wire chat lifecycle hooks into chatd feat: wire chat lifecycle hooks into chatd Jul 22, 2026
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatstate branch from c6e5dff to bc056cc Compare July 22, 2026 22:20
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatd branch from 08fb016 to 8e776d7 Compare July 22, 2026 22:20
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatstate branch from bc056cc to af39f14 Compare July 22, 2026 22:26
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatd branch from 8e776d7 to cb5540c Compare July 22, 2026 22:27
@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: cb5540c9e0

ℹ️ 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/chatd/subagent.go
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatstate branch from af39f14 to f9b2900 Compare July 22, 2026 22:41
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatd branch from cb5540c to 4c02cf5 Compare July 22, 2026 22:41
@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: 4c02cf59e8

ℹ️ 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/chatd/chatd.go
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatd branch from 4c02cf5 to 265d881 Compare July 22, 2026 23:11
@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: 265d88162c

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

Dispatches session_start, user_prompt_submit, pre/post_tool_use,
stop, pre/post_compact, and subagent spawn hooks from chatd, applies
hook responses (prompt overrides, prefix notices, allowed-tools
narrowing, stop nudges, compaction summary hints), persists dispatch
effects transactionally with each step, and injects the dispatcher
from coderd based on the new deployment configuration (hook URL,
secret, timeout, experiment) documented in the admin setup doc.
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatstate branch from f9b2900 to 4e9961b Compare July 22, 2026 23:32
@ibetitsmike
ibetitsmike force-pushed the mike/chat-hooks/chatd branch from 265d881 to 8f4a766 Compare July 22, 2026 23:32
@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: 8f4a7665e0

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

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