Skip to content

feat(chatd): store the answered state of descendant questions - #332

Merged
ibetitsmike merged 6 commits into
mainfrom
mike/issue-300-answered-state
Nov 14, 2026
Merged

ibetitsmike merged 6 commits into
mainfrom
mike/issue-300-answered-state

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Bottom of a two PR stack for #300. The top PR, #333, wakes wait_agent on the child's state channel.

What changed

  • New chatdmig node 01a1da95-..._chat_descendant_questions_answered adds root_chat_id, answered_at and answered_by to chatd.chat_descendant_questions, a CHECK on the pair (an answered row must name what answered it), and an index on root_chat_id.
  • The node backfills. It deletes no row. root_chat_id is COALESCE(chats.root_chat_id, chats.id), because roots have a NULL root_chat_id and children name the family root. A row that the old SQL rule treats as answered gets answered_at = now() and answered_by = 'migrated'. A pending row stays pending.
  • chatstate.(*Tx).insertMessages gets one hook line. forkRetireDescendantQuestion (in chatstate/fork_descendant_questions.go) runs RetireChatDescendantQuestion in the same transaction as the inserted user messages. It sets answered_by to message (a visible user message, question not secret), secret_answer (a secret question answered by a visible message whose text starts with Answer to ask_user_question call <id>:), or context_boundary (a compressed message: clear or compaction). Model-only messages and messages of a rolled back transaction leave the question pending.
  • UpsertChatDescendantQuestion takes the root from chats and clears the answered state, so a new question is pending again.
  • GetPendingChatDescendantQuestion and ListPendingChatDescendantQuestionsByRoot read only chat_descendant_questions and chats. They no longer scan chat_messages.
  • dbauthz and dbmetrics wrappers for the new query. coderd/x/chatd/ARCHITECTURE.md describes the stored state.

Why

Pending was derived from later chat messages on every read. That cost a chat_messages scan per poll and per questions endpoint call, and the rule was hard to reason about. Storing the state in the transaction of the retiring event keeps it exact.

Tests

All run locally with flock ~/.fork-go.lock:

  • go test ./coderd/x/chatd/chatstate/ -run TestRetireDescendantQuestion -v: one case per retiring event through real chatstate transitions (visible message, promoted queued message, secret message stays pending, secret answer, clear context, compaction, model-only message, rolled back message) plus "new question resets". Each asserts answered_at and answered_by. Pass.
  • go test ./coderd/database/forkdb/ -run Descendant -v: query table test, cascade delete, list by root. Pass.
  • CHATDMIG_TEST_DATABASE_URL=... go test ./coderd/database/migrations/chatdmig/...: full suite including TestUpgradeMatchesFresh and the new TestDescendantQuestionsAnsweredBackfill (a root and one child per branch of the old rule: visible, before plus model-only, deleted, compressed, secret visible, secret answer; rows kept, root_chat_id is the root, answered_by is migrated or NULL as the old rule says, created_at kept, and the CHECK rejects an answered row without answered_by). Pass. fork-ci sets this variable, so it runs there too.
  • go test ./coderd/ -run 'Question|Secret': includes the new NoMessageReads subtest, which holds LOCK TABLE chatd.chat_messages IN ACCESS EXCLUSIVE MODE, proves a chat_messages read blocks, and still gets the question from the endpoint. Pass.
  • go test ./coderd/database/dbauthz/, go test ./coderd/x/chatd/... : pass.
  • golangci-lint run --fix=false on the changed packages: no finding on changed lines. New-code config with --new-from-merge-base=origin/main: 0 issues. make gen: no diff (it stops at the helm golden step because helm is not installed here).

Not verified

Part of #300. The stack continues in #333.

chat_descendant_questions now stores the family root and whether the
question is answered. The chatstate message insert marks the question
answered in the same transaction as the user message that answers or
retires it: a visible user message, the secret answer message, or a
compressed context boundary. A new question clears the state.

Reads of the pending question and the questions endpoint no longer scan
chat_messages. The chatdmig node backfills root_chat_id from the family
root and marks rows that the old rule treats as answered with
answered_by = 'migrated'. Pending rows stay pending.

Part of #300.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 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-14T18:05:02.733745Z 1490c74 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: efca0cbbb9

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

Require answered_by on an answered row; a NULL passed the CHECK. Label
a prefixed answer to a question that is not secret as message, so
secret_answer means a secret question. Index root_chat_id fully, so the
cascade from a deleted chat can use it. Say in ARCHITECTURE.md that the
chatstate insert writes this table through a fork hook. Cover deleted,
compressed and secret rows in the backfill test.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 3aea38c6a5

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

# Conflicts:
#	coderd/database/forkdb/sqlc.yaml
#	coderd/x/chatd/ARCHITECTURE.md
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged origin/main (9308bf6, #330) into this branch. The node now also requires the token budget node. @codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 0a85921a5f

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

Coder Agents acted on behalf of @ibetitsmike.

Merged origin/main (9f99450, #331, site only) into this branch. @codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: 1ce7f63639

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

Coder Agents acted on behalf of @ibetitsmike.

fork-ci run 43619866320 on 1ce7f63 failed only in TestInterruptChatPersistsPartialResponse, an existing test this PR does not touch. Go and SQL are identical to 0a85921, where chat-tests passed. The test passed 30 of 30 runs locally. This empty commit triggers fork-ci again. @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: befc479c03

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

Coder Agents acted on behalf of @ibetitsmike.

Addressed the P2 on root purges in 1490c74. @codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create an environment for this repo.

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 1490c74d64

ℹ️ 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 5f049c1 into main Nov 14, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-300-answered-state branch November 14, 2026 18:06
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged as 5f049c1 (squash, --match-head-commit).

Gates on head 1490c74:

  • All 9 check runs passed. fork-ci run 43621632505 succeeded on this head.
  • Codex: "Didn't find any major issues" for 1490c74: feat(chatd): store the answered state of descendant questions #332 (comment)
  • Unresolved review threads: 0. Behind main: 0. Main was 9f99450 and the server ran 9f99450 at merge time.
  • Codex P1 on rolling deploys was dismissed: the fork runs one server process and a deploy replaces it.
  • Tests I ran on this exact head (go test -count=1 -v): TestDescendantQuestionsAnsweredBackfill (chatdmig, real Postgres), TestRetireDescendantQuestion (chatstate), TestRetireChatDescendantQuestion, TestListPendingChatDescendantQuestionsByRoot, TestChatDescendantQuestionDeletedWithChat (forkdb), TestForkChatDescendantQuestions, TestForkChatSecretAnswer*, TestForkChatSecretQuestionQueuedMessage (coderd). All pass.
  • Negative controls I ran: removing the forkRetireDescendantQuestion hook fails 5 of 8 TestRetireDescendantQuestion subtests (on 0a85921); restoring the root_chat_id foreign key fails TestChatDescendantQuestionDeletedWithChat (on this head).
  • Opus review of this head: mergeable, no P1 or P2. Nits kept as notes: the retire hook is not gated by fork-nested-subagents (one UPDATE by primary key per user message, writes nothing while the experiment is off); a question can keep a stale root_chat_id after its root is purged, as before.

Not exercised: a live family on the daily server. The migration backfill ran only in the chatdmig test database.

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