Repository navigation
feat(coderd): add search_chat_messages builtin chatd tool - #29616
Closed
jscottmiller wants to merge 2 commits into
Closed
jscottmiller wants to merge 2 commits into
jscottmiller wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a
search_chat_messagesbuiltin chatd tool that searches a chat transcript in SQL (case-insensitive substring over text parts and tool name/args/result) and returns bounded excerpts newest first, paged onid. It defaults to the calling chat;chat_idmay name an ancestor or descendant chat with the same owner. Explore-mode subagents may only search their own chat. Model-only and deleted rows are never returned.Backend only, no HTTP route or frontend renderer. The tool is registered for root chats and subagents, allowed in plan mode, and included in the explore allowlist. Also adds a partial index
idx_chat_messages_chat_visible_id (chat_id, id DESC) WHERE deleted = false AND visibility IN ('user','both')so an unfiltered first page does not scan the whole chat.Plan
Plan: search_chat_messages builtin chatd tool
Branch:
scott/search-chat-messages-toolofforigin/main(38a17ec).Backend only:
coderd/databaseandcoderd/x/chatd. No HTTP route.Findings that shape the design
chat_messages.contentis a jsonb array of parts (typein text,reasoning, tool-call, tool-result, file, ...). Text lives in
part->>'text';tool rows carry
tool_name,args(jsonb) andresult(jsonb).search_tsvtsvector column already exists, but it is populatedasynchronously by a dbpurge background job and only for
role IN ('user','assistant')rows. Recent rows (the ones an orchestratormost often wants) can still be NULL, tool rows are never vectorized, and
stemming is imprecise for identifiers and paths.
visibility IN ('user','both') AND deleted = falseand page onid(
GetChatMessagesByChatIDDescPaginated). Query comments stateidis thedurable append order and
created_atcan disagree with it.wait_agent,message_agent,interrupt_agent) authorizecross-chat access with
isSubagentDescendant(ctx, db, caller, target)(target must be a descendant of the caller). There is no existing
child-to-parent helper. Tools run under
dbauthz.AsChatd.subagentToolsis only appended for root chats(
appendRootChatTools). The always-present tool list is built ingeneration_preparer.go(tools := []fantasy.AgentTool{...}), thenfiltered by
builtinPlanToolAllowed(plan mode) andallowedExploreToolNames(explore subagents), both name allowlists.Decisions (deviations from the overview are marked)
strpos(lower(search_text), lower(@query)) > 0over text assembled in SQLfrom
textparts plus, for tool rows,tool_name,args::textandresult::text. Reasons: exact and synchronous (no backfill lag), coverstool rows, no LIKE metacharacter escaping. Reasoning and file parts are
excluded. The scan is bounded to one
chat_id. Review finding chore: Initial GHA workflow #1 added apartial index
idx_chat_messages_chat_visible_id (chat_id, id DESC) WHERE deleted = false AND visibility IN ('user','both')(migration 000598) soan unfiltered first page does not scan the whole chat; it also serves
GetChatMessagesByChatIDDescPaginated.substring(search_text FROM GREATEST(hit - ctx, 1) FOR 2*ctx + char_length(query)), so only theexcerpt (not full message text) crosses the wire. Go adds leading and
trailing ellipses using the returned
hit_posandtext_length. Contextis 160 characters each side; the excerpt is additionally capped at 400
runes in Go.
id DESCwithbefore_id(deviation: overview saidcreated_at and id).
idis the append order the existing paginated queryand the API cursor use;
created_atis the transaction start time and candisagree with append order. Query fetches
limit + 1to sethas_more;next_before_idis the last returned id.visibility IN ('user','both') AND deleted = false,including when searching the calling chat. Model-only rows (compaction
summaries, hidden sentinels) are excluded; there is no argument to include
them. Rationale: keeps the result identical to what the transcript API
shows and avoids leaking compaction summaries into a model that then
re-quotes them.
the caller, when
isSubagentDescendant(caller, target)(existing helper,parent to child), or when
isSubagentDescendant(target, caller)(samehelper with arguments swapped, child to parent or any ancestor). In all
cross-chat cases
target.OwnerID == caller.OwnerIDis also required.Explore-mode subagent callers may search only their own chat (orchestrator
decision on review finding chore: Formatting - bring .prettierrc over from cdr/m #9).
Denied and not-found both return the same "chat not found or not
accessible" tool error. Archived targets are allowed (history recovery is
a motivating use). The dbauthz method authorizes
ActionReadon the chatlike its siblings, which the chatd actor satisfies.
generation_preparer.goso root chats and subagents (general, explore,computer_use) all get it. Added as allowed to
builtinPlanToolAllowed(read-only, for root and child) and
allowedExploreToolNames. Notadded to
unbilledSubagentToolNames, so it is billed and counted likeother local tools. No exclusivity.
query(required, trimmed, 1..200 runes),chat_id(optional UUID, default calling chat),
role(optional, one ofuser/assistant/tool/system),
since/until(optional RFC 3339),limit(default 10, clamped 1..50),before_id(optional int64 > 0).Result:
chat_id,query,matches(newest first:id,created_at,role,excerpt,tool_namewhen present),returned,has_more,next_before_id(omitted whenhas_moreis false).Affected files
coderd/database/queries/chats.sql:SearchChatMessages :many.coderd/database/migrations/000598_chat_messages_visible_id_index.{up,down}.sql.coderd/database/queries.sql.go,querier.go,dump.sql,dbmock/dbmock.go,dbmetrics/querymetrics.go.coderd/database/dbauthz/dbauthz.go(+dbauthz_test.go): methodauthorizing
ActionReadon the chat.coderd/database/querier_test.go: postgres tests for the query.coderd/x/chatd/search_chat_messages.go(new): args, tool, authz check,excerpt shaping.
coderd/x/chatd/search_chat_messages_internal_test.go(new).coderd/x/chatd/generation_preparer.go: registration.coderd/x/chatd/chatd.go: plan and explore allowlists.coderd/x/chatd/ARCHITECTURE.md: TODO item in the tools list only.Sequencing
feat(coderd/database): add SearchChatMessages query: SQL, gen,dbauthz method and test, querier tests.
feat(coderd/x/chatd): add search_chat_messages builtin tool:tool, registration, allowlists, tests, ARCHITECTURE TODO.
experimental), final report.Testing
querier_test.go(dbtestutil, postgres): substring match iscase-insensitive; role filter; since/until; excludes deleted and
model-only rows; tool rows match on tool_name/args/result and return
tool_name; excerpt window bounded and hit_pos correct;
before_idpagingand
limit; no match on reasoning-only text.dbauthz_test.go:SearchChatMessagesassertsActionReadon the chat.search_chat_messages_internal_test.go: own chat default; child chatfrom parent; parent chat from child; unrelated chat of same owner denied;
limit clamps (0 -> default 10, 500 -> 50); invalid role/timestamp/chat_id
errors; has_more and cursor; tool registered for root and child turns and
allowed in plan and explore allowlists.
make genno diff,make linton touched packages, targetedgo testruns with postgres.Review findings and dispositions
Findings and dispositions
ORDER BY id DESC LIMIT nunder the visibility/deleted predicate; at 20k messages a first page costs ~290 ms and grows linearly. A partial index(chat_id, id DESC) WHERE deleted = false AND visibility IN ('user','both')brought it to ~2 ms and also servesGetChatMessagesByChatIDDescPaginated.idx_chat_messages_chat_visible_idwith that definition.c.search_text IS NOT NULLis redundant (strpos(NULL, q) > 0is already not true) and doubles the subselect evaluation per row.args/resultare matched as serialized JSON, so queries containing quotes or newlines miss tool rows; excerpts are raw quoted text. Neither is stated.authorizeChatSearchis the sole gate: the chatd store actor has deployment-wide chat read, so dbauthz always passes. Consistent withwait_agent, but this tool returns content. Add a comment stating the invariant and tests for different owner, sibling, grandchild.builtinPlanToolAllowedwas extended butPlanModeIncludesOnlyAllowlistedBuiltInsandPlanModeChildChatsAllowExplorationOnlywere not updated.tool_namesubselect usesLIMIT 1withoutORDER BY.WITH ORDINALITY ... ORDER BY ordinality.hit_poscomputed onlower(search_text)indexessearch_text; correct only whilelower()preserves length (true for libc UTF-8, not guaranteed under ICU).LIMIT @limit_val::inthas no floor unlike siblings'COALESCE(NULLIF(...), 50).limit + 1 >= 2; a silent default of 50 would break the has_more arithmetic and hide a caller bug.authorizeChatSearchdenies any target other than the caller whencaller.Modeis explore; other callers keep the ancestor/descendant same-owner rule. TestTestSearchChatMessagesTool_ExploreSubagentSearchesOwnChatOnlycovers own chat (default and explicit), parent denied, and parent still reaching the explore child. Arg description and ARCHITECTURE TODO updated.time.Parse(time.RFC3339)rejects date-only and zone-less timestamps models often produce.untilon a date has ambiguous end-of-day semantics.created_atis second precision whilesince/untilcompare at full precision, so a returned value fed back asuntilis lossy.time.RFC3339Nano.PlanningSubagentOverlayPromptlists the exploration tools a plan-mode child may use and omits the new tool.role: "system"is accepted but every system row isvisibility = 'model', so it can never match.systemfrom accepted roles and description.ErrNoRowserrors fromGetChatByIDreturn a different message than the denied case.authorizeChatSearchrefers to a sibling function.parent_chat_id).SinceUntilmutates a shared row.Open items
search_chat_messagesis a separate follow-up (Priya chore: Add documentation of our phased approach to the UX #15).Corrections applied
All accepted dispositions above were applied after the checkpoint:
000598_chat_messages_visible_id_indexaddsidx_chat_messages_chat_visible_id ON chat_messages (chat_id, id DESC) WHERE deleted = false AND visibility IN ('user', 'both').chats.sqldrops theIS NOT NULLpredicate, orders thetool_namesubselect by ordinality, and documents the JSON serialization and thelower()length assumption.systemis rejected;created_atusestime.RFC3339Nano.authorizeChatSearchcomment describes the parent_chat_id walk and the chatd-actor invariant;TestSearchChatMessagesTool_LineageAuthorizationcovers grandchild (both directions), sibling denied, and different owner denied in both directions.PlanningSubagentOverlayPromptinclude the tool.MultibyteCharacterPositionsandHitStraddlingTextParts;SinceUntiluses dedicated rows.This PR was generated by Coder Agents on behalf of @jscottmiller.