Skip to content

feat(coderd/database): add chat_context_resources table#26430

Merged
kylecarbs merged 4 commits into
mainfrom
kylecarbs/chat-context-resources-table
Jun 16, 2026
Merged

feat(coderd/database): add chat_context_resources table#26430
kylecarbs merged 4 commits into
mainfrom
kylecarbs/chat-context-resources-table

Conversation

@kylecarbs

@kylecarbs kylecarbs commented Jun 16, 2026

Copy link
Copy Markdown
Member

What

Adds the chat_context_resources table: a per-chat pinned copy of the agent context resources a chat is hydrated against.

The agent-side table workspace_agent_context_resources is last-writer-wins (overwritten on every PushContextState, no history). For a chat to keep a stable view of the resources it was pinned to while the agent drifts, it must take its own copy at hydration / context refresh. This PR is the schema foundation only.

Schema

  • Mirrors workspace_agent_context_resources columns and reuses its enum types (workspace_agent_context_body_kind, workspace_agent_context_resource_status). The down migration leaves those types in place since 000522 owns them.
  • chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE for retention / cleanup parity with the other chat-scoped tables. No agent FK so the pin survives agent replacement and workspace rebuilds.
  • PK (chat_id, source).
  • Regenerated dump.sql, models.go, and the FK/unique constraint enumerations via make gen. No queries, dbauthz, or audit entries yet (agent-derived copy; same precedent as the agent context tables and boundary_logs).
  • Adds a migration fixture; uses the established "first existing chat" pattern (cf. 000519) rather than hard-coding a chat id.

Out of scope (follow-ups)

  • Copy/list/delete queries + dbauthz wrappers, hooked into hydrate/refresh.
  • prepareGeneration consuming pinned instructions + skills (gated on a new chat-context-sources experiment).
  • Growing codersdk.ChatContext (resources + changed diff) and the frontend indicator/refresh.
  • Eventual removal of last_injected_context.

Testing

  • make gen clean (no drift beyond this table).
  • go test ./coderd/database/migrations/...: TestMigrate, TestMigrateUpWithFixtures (fixture + up/down round trip), TestCheckLatestVersion.
  • TestAuditableResources passes without an audit entry.
  • Full make pre-commit green.
Decision log

Dedicated table (Option A) over reusing chat_messages parts. Message context-file/skill parts get compacted away and re-derived (ReloadMessages); a dedicated table is compaction-immune and later powers the SDK resources view. Heavy content (can reach ~2 MiB) also stays off the hot chats row (list/watch paths), unlike a jsonb column.

Cleanup parity. All chat-scoped child tables (chat_messages, chat_debug_runs/steps, chat_diff_statuses, chat_file_links, chat_heartbeats, chat_queued_messages) use chat_id ... ON DELETE CASCADE. dbpurge purgeChatsInTx calls DeleteOldChats, which cascades to children, so CASCADE alone covers retention purge; no separate purge query needed.

No agent FK. The pin must outlive the agent that produced it (agent replacement / workspace rebuild), so there is intentionally no FK to workspace_agents.


This PR was created by Coder Agents on behalf of @kylecarbs.

Add chat_context_resources: a per-chat pinned copy of the agent context
resources a chat is hydrated against. The agent-side table
(workspace_agent_context_resources) is last-writer-wins and keeps no
history, so a chat takes its own copy at hydration and context refresh
to keep a stable view of its pinned resources while the agent drifts.

This is the schema foundation only. The copy/list queries,
prepareGeneration wiring, and SDK/frontend exposure land in follow-up
changes.

- Mirrors workspace_agent_context_resources columns and reuses its enum
  types (workspace_agent_context_body_kind,
  workspace_agent_context_resource_status); the down migration leaves
  those types in place since 000522 owns them.
- chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE for
  retention/cleanup parity with other chat-scoped tables. No agent FK so
  the pin survives agent replacement and workspace rebuilds.
- PK (chat_id, source).
@kylecarbs

Copy link
Copy Markdown
Member Author

/coder-agents-review

@coder-agents-review

coder-agents-review Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Chat: Review posted | View chat
Requested: 2026-06-16 19:38 UTC by @kylecarbs
Spend: $31.80 / $100.00

Review history
  • R1 (2026-06-16): 10 reviewers, 2 Nit, 1 Note, 1 P3, COMMENT. Review
  • R2 (2026-06-16): 7 reviewers, 2 Nit, 1 Note, 1 P3, APPROVE. Review

deep-review v0.7.1 | Round 2 | bca0ce0..3b483ea

Last posted: Round 2, 4 findings (1 P3, 2 Nit, 1 Note), APPROVE. Review

Finding inventory

Findings

# Sev Status Location Summary Round Reviewer Posted
CRF-1 P3 Author fixed (3b483ea) 000524_chat_context_resources.up.sql:1 Header comment largely duplicates COMMENT ON TABLE R1 Gon Yes
CRF-2 Nit Dropped by orchestrator (verbatim from source table 000522; fixing only here creates cross-table inconsistency) 000524_chat_context_resources.up.sql:30 Reserved variant casing mismatch in comment R1 Gon No
CRF-3 Note Author fixed (3b483ea) testdata/fixtures/000524_chat_context_resources.up.sql:1 "v1 body kind" is undefined shorthand R1 Gon Yes
CRF-4 Nit Author fixed (3b483ea) coderd/database/queries/chats.sql:2554 Stale DeleteOldChats cascaded table enumeration R1 Knuckle Yes
CRF-5 Nit Author fixed (3b483ea) PR description Three emdashes (U+2014) in PR body R1 Leorio, Mafu-san Yes

Round log

Round 1

Panel. 1 P3, 2 Nit, 1 Note new. 1 dropped (cross-table inconsistency). Reviewed against bca0ce0..4b84e39.

Round 2

Panel. CRF-1, CRF-3, CRF-4, CRF-5 all addressed. 0 new findings. Reviewed against bca0ce0..3b483ea.

About deep-review

CRF = Coder Review Finding (P0-P4, Nit, Note)

Reviewer Focus
Bisky tests
Chopper ops/errors
Churn-guard change verification
Ging language modernization
Gon naming
Hisoka edge cases
Killua perf
Kite change integrity
Knov contracts
Knuckle SQL
Kurapika security
Law decomposition
Leorio docs
Luffy product
Mafu-san process
Mafuuu contracts
Melody dispatch/pairing
Meruem structural
Nami frontend
Netero mechanical checks
Pariston premise testing
Pen-botter product gaps
Razor verification
Robin duplication
Ryosuke Go arch
Takumi concurrency
Zoro shape

🤖 Managed by Coder Agents.

@kylecarbs kylecarbs marked this pull request as ready for review June 16, 2026 19:19

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clean, well-documented schema foundation. The table design is sound: composite PK mirrors the source table, CASCADE follows the chat child table convention, the intentional absence of an agent FK is correct for survivability, and enum reuse avoids type duplication. Pariston explored four alternative framings (versioned agent resources, inline JSONB, derive-on-demand) and couldn't build a case against the copy-table approach. Hisoka probed PK collision potential and CASCADE chains with no findings. Knuckle confirmed the schema, fixture, and generated code are all consistent.

Severity count: 1 P3, 2 Nit, 1 Note. No blocking issues.

"Boring, correct DDL." — Hisoka


coderd/database/queries/chats.sql:32

Nit [CRF-4] This comment enumerates cascaded child tables but the list is stale. It says "Related chat_messages, chat_diff_statuses, and chat_queued_messages are removed via ON DELETE CASCADE" but was already missing chat_debug_runs, chat_debug_steps, chat_file_links, and chat_heartbeats. This PR adds chat_context_resources, widening the gap.

A partial list is worse than no list because it implies completeness. Either enumerate all cascaded tables or replace with: "All chat-scoped child tables are removed via ON DELETE CASCADE."

Not introduced by this PR, but widened by it. (Knuckle)

🤖

In reply to IC_kwDOGkVX1s8AAAABGXgOGQ:

Nit [CRF-5] Three emdashes (U+2014) in the PR description body. Project convention (AGENTS.md) bans emdash/endash in documentation, and PR descriptions count. Occurrences: (1) after the enum type list, (2) after the test command, (3) in the cleanup parity paragraph. Replace with periods or semicolons. The commit message and all SQL comments are clean. (Leorio, Mafu-san)

🤖

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/database/migrations/000524_chat_context_resources.up.sql Outdated
Comment thread coderd/database/migrations/testdata/fixtures/000524_chat_context_resources.up.sql Outdated
- Condense the migration header comment to defer table semantics to
  COMMENT ON TABLE, keeping only the migration-specific notes (no agent
  FK rationale, enum reuse from 000522). [CRF-1]
- Reword the fixture comment to "non-reserved body kind" instead of the
  undefined "v1 body kind" shorthand. [CRF-3]
- Replace the stale partial cascade list in DeleteOldChats with "All
  chat-scoped child tables are removed via ON DELETE CASCADE". [CRF-4]
@kylecarbs

Copy link
Copy Markdown
Member Author

Addressed the Round 1 findings in 3b483ea:

  • CRF-1 (P3, Gon): condensed the migration header comment to defer table semantics to COMMENT ON TABLE and keep only the migration-specific notes (no agent FK, enum reuse from 000522). Resolved inline.
  • CRF-3 (Note, Gon): reworded "v1 body kind" to "non-reserved body kind". Resolved inline.
  • CRF-4 (Nit, Knuckle): replaced the stale partial cascade list in DeleteOldChats with "All chat-scoped child tables are removed via ON DELETE CASCADE", so it cannot drift again.
  • CRF-5 (Nit, Leorio / Mafu-san): removed the three em-dashes from the PR description.
  • CRF-2 (Nit): left as-is, matching the orchestrator's own call: the wording is verbatim from 000522, and fixing only here would create cross-table inconsistency.

Coder Agents, on behalf of @kylecarbs

@kylecarbs

Copy link
Copy Markdown
Member Author

/coder-agents-review

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All R1 findings addressed in 3b483ea. CRF-1 header condensed (verified by Gon, Razor, Pariston). CRF-3 "v1" reworded to "non-reserved" (verified by Gon, Razor). CRF-4 stale cascade list generalized to future-proof phrasing (verified by Razor against all 8 child table FKs). CRF-5 emdashes removed from PR description. No new findings from the R2 panel (7 reviewers).

"The agent fixed the class, not just the instance." — Mafu-san, on the CRF-4 fix

🤖 This review was automatically generated with Coder Agents.

…0525

000524 was taken by 000524_aibridge_token_usage_cost after merging the
latest main. Renumbered the migration and its fixture from 000524 to
000525; regeneration confirms no schema drift and the migration tests
pass against the merged set.
@kylecarbs kylecarbs merged commit 53a6459 into main Jun 16, 2026
29 checks passed
@kylecarbs kylecarbs deleted the kylecarbs/chat-context-resources-table branch June 16, 2026 21:28
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 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