Skip to content

feat: filter chats by diff URL via the q search parameter - #24970

Merged
kylecarbs merged 2 commits into
mainfrom
kylecarbs/chats-diff-url-filter
May 13, 2026
Merged

kylecarbs merged 2 commits into
mainfrom
kylecarbs/chats-diff-url-filter

Conversation

@kylecarbs

Copy link
Copy Markdown
Member

Adds a diff_url: term to the q search parameter on GET /api/experimental/chats so callers can look up the chat associated with a particular pull request, merge request, or any other URL persisted on the chat's diff status.

q=diff_url:"https://github.com/coder/coder/pull/123"

Match is case-insensitive. When the URL lives on a delegated sub-agent's diff status, the parent chat is returned so the relationship surfaces from a single lookup.

Design notes
  • Forge-agnostic. Reuses the existing chat_diff_statuses.url column rather than introducing a pr: vocabulary, since the SDK already documents the URL as "may point to a pull request or a branch page depending on whether a PR has been opened." Works for GitHub PRs, GitLab MRs, branch pages, etc.
  • Composes with archived:. The two terms can be combined: q=archived:true diff_url:"...".
  • Case handling. The parser used to lowercase the entire q string up front, which would mangle URL path segments. Switched to lowercasing only the field key inside searchTerms (already happens there) and keeping the value as the caller typed it. The SQL comparison lowercases on both sides.
  • Validation. diff_url must be a syntactically valid HTTP(S) URL with a non-empty host. No forge-specific validation.
  • Index. Adds idx_chat_diff_statuses_url_lower on LOWER(url) so the lookup is cheap even on large datasets.
  • Sub-agent fan-in. EXISTS clause matches when the URL lives on the chat itself or any chat with root_chat_id equal to the chat's id, so a delegated sub-agent's PR pulls in its parent.
  • Deferred. Sentinels like pr:any / pr:none and a forge-agnostic state filter (diff_state:open|merged|closed) were intentionally left out of this change. They couple cleanly to a second forge or a clearer product call, and shipping them now would lock in vocabulary we may want to revisit.

Tests

  • coderd/searchquery: parser tests for valid URLs, case handling (key insensitive, value preserved), composition with archived:, and validation errors (non-HTTP scheme, missing host, malformed URL).
  • coderd/exp_chats_test.go: end-to-end coverage hitting ListChats. Verifies a root chat matches its own URL, a parent chat surfaces when only a sub-agent has the URL, lookups are case-insensitive, non-matching URLs return empty, and invalid URLs return 400.

This PR was authored by a Coder Agent on behalf of @kylecarbs.

@github-actions

github-actions Bot commented May 5, 2026

Copy link
Copy Markdown
Contributor

Docs preview

📖 View docs preview for docs/ai-coder/agents/chats-api.md

@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.

Clean feature addition. The EXISTS subquery with fan-in through root_chat_id is the right shape for this, the partial index aligns with the query predicates, and the test suite covers root match, sub-agent fan-in, case insensitivity, no-match, and invalid input. Good use of existing patterns (requireSDKError, dbgen.Chat, the subtest structure in TestListChats).

Six reviewers independently noted that Chats() is now the only search function that does not pre-lowercase the entire query string. The change is correct (URL path segments are case-meaningful, and strconv.ParseBool handles standard casings), but the asymmetry with the other six search functions is a maintenance consideration for future filters.

Nit [DEREM-3] coderd/database/sqlc.yaml: DiffUrl breaks the project's naming convention for URL-suffixed identifiers. The sqlc rename section maps every other _url column to the Go-idiomatic URL suffix (callback_url: CallbackURL, avatar_url: AvatarURL, troubleshooting_url: TroubleshootingURL, icon_url: IconURL, oauth2_auth_url: OAuth2AuthURL, oauth2_token_url: OAuth2TokenURL). The new diff_url named parameter is missing from this map, so sqlc generates DiffUrl instead of DiffURL. This propagates into modelqueries.go, search.go, exp_chats.go, and test files. Fix: add diff_url: DiffURL to the rename section and regenerate. (Mafuuu)

3 P3, 1 Nit.

"I tried to build a case against this change and could not." Pariston

🤖 This review was automatically generated with Coder Agents.

@@ -1266,6 +1266,65 @@ func TestSearchChats(t *testing.T) {
Query: "archived:",
ExpectedErrorContains: "cannot start or end with ':'",
},

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.

P3 [DEREM-1] Removing the blanket strings.ToLower(query) from Chats() narrows the set of accepted casings for archived values. Before: any mixed-case like tRuE resolved to "true" via lowering, then strconv.ParseBool succeeded. After: the value passes through in original case, and ParseBool only accepts a fixed set (true, TRUE, True, 1, t, T and their false equivalents).

Realistic impact is low (nobody types tRuE), but the behavioral change has no regression test. The existing ArchivedTrue test uses "archived:true" (already lowercase), so it passes regardless. Adding a row for archived:TRUE would document the new behavior boundary and catch regressions if someone re-adds the blanket lowercase or if a future filter requires different casing rules.

{
    Name:  "ArchivedTrueUpperCase",
    Query: "archived:TRUE",
    Expected: database.GetChatsParams{
        Archived: sql.NullBool{Bool: true, Valid: true},
    },
},

(Bisky P3, Hisoka P3)

🤖

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added an ArchivedTrueUpperCase parser case in c182a93 with a comment explaining the boundary. Locks in archived:TRUE so a regression to blanket lowercasing would fail loudly.

Comment thread coderd/exp_chats_test.go
}
})

t.Run("DiffURLFilter", func(t *testing.T) {

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.

P3 [DEREM-2] No E2E test for archived + diff_url composition at the SQL layer. The parser test DiffURLWithArchived proves both fields populate GetChatsParams, but the SQL query has two independent AND CASE clauses whose interaction is never tested against the database. Every chat in the DiffURLFilter fixture is unarchived.

If one chat were archived with a matching diff URL, the default query (archived:false) should exclude it, and archived:true diff_url:"..." should surface it. Neither path is tested. Sketch: archive rootWithPR, then assert the filter excludes it by default and includes it when archived:true is explicit.

(Bisky)

🤖

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in c182a93. Added an archived fixture chat with its own diff URL plus two subtests: ArchivedFilteredOut confirms the default archived:false excludes it, and ArchivedTrueComposes confirms archived:true diff_url:"..." surfaces it.

Comment thread coderd/exp_chats_test.go Outdated
_, err := client.ListChats(ctx, &codersdk.ListChatsOptions{
Query: `diff_url:"ftp://example.com/x"`,
})
requireSDKError(t, err, http.StatusBadRequest)

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.

P3 [DEREM-4] InvalidURL test checks only the HTTP status code, not which validation path produced the 400. requireSDKError(t, err, http.StatusBadRequest) confirms a 400 but discards the returned *codersdk.Error without inspecting Validations. The 400 could come from validateDiffURL (the intended path) or from searchTerms rejecting the URL's colons if quote handling changes. Since requireSDKError already returns the error, asserting on the validation field is one line:

sdkErr := requireSDKError(t, err, http.StatusBadRequest)
require.Contains(t, sdkErr.Validations[0].Field, "diff_url")

(Chopper)

🤖

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in c182a93. The InvalidURL test now captures the returned *codersdk.Error and asserts Validations[0].Field == "diff_url", so the test fails loudly if the 400 ever stops coming from validateDiffURL.

Copy link
Copy Markdown
Member Author

Thanks for the review. Addressed all four items in c182a93:

  • DEREM-3 (Nit): Added diff_url: DiffURL to coderd/database/sqlc.yaml and regenerated; the field is now DiffURL everywhere.
  • DEREM-1 (P3): Added ArchivedTrueUpperCase parser test.
  • DEREM-2 (P3): Added ArchivedFilteredOut and ArchivedTrueComposes E2E subtests covering the SQL-level interaction between archived and diff_url.
  • DEREM-4 (P3): InvalidURL now asserts on Validations[0].Field.

On the broader observation about Chats() being the only search function that does not pre-lowercase the query: agreed it's an asymmetry worth noting. Left it as-is for this PR because URL path segments are case-meaningful, but the new test pins the boundary so future filters with case requirements have an explicit signal to follow.

The commit also picks up regenerated apidoc/swagger output for stdlib net/url doc strings; that drift is already present on main and make gen insists on regenerating it.

Reply authored by a Coder Agent on behalf of @kylecarbs.

@kylecarbs
kylecarbs force-pushed the kylecarbs/chats-diff-url-filter branch from c182a93 to 1d18602 Compare May 11, 2026 18:07

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 four R1 findings addressed in 1d18602. Four reviewers (Bisky, Mafuuu, Pariston, Meruem) independently verified each fix against the current code.

DEREM-1: ArchivedTrueUpperCase test added, documents the strconv.ParseBool acceptance boundary. DEREM-2: ArchivedFilteredOut and ArchivedTrueComposes confirm the SQL-layer composition. DEREM-3: diff_url: DiffURL in sqlc.yaml, generated code is clean. DEREM-4: InvalidURL now asserts Validations[0].Field == "diff_url".

No new findings. The feature is well-scoped, the SQL and index design is correct, and the test suite covers the contract thoroughly.

"I traced the root_chat_id semantics through subagent.go:937-940... the SQL condition correctly resolves multi-level hierarchies." Pariston

🤖 This review was automatically generated with Coder Agents.

@kylecarbs
kylecarbs requested a review from johnstcn May 11, 2026 18:43
@kylecarbs
kylecarbs marked this pull request as ready for review May 11, 2026 18:43
@kylecarbs
kylecarbs force-pushed the kylecarbs/chats-diff-url-filter branch from 1d18602 to d36b0fd Compare May 12, 2026 16:45
kylecarbs added 2 commits May 13, 2026 14:09
Adds a 'diff_url:' term to the chats list endpoint's q search
parameter so callers can look up chats associated with a pull
request, merge request, or any other URL stored on the chat's
diff status. The match is case-insensitive and surfaces a root
chat when the URL lives on a delegated sub-agent's diff status.
- Rename DiffUrl to DiffURL via sqlc.yaml so the generated field
  follows the project's URL-suffix convention.
- Add ArchivedTrueUpperCase parser test to lock in the behavior
  boundary now that the Chats parser no longer pre-lowercases the
  full query string.
- Assert the validation error field on the InvalidURL E2E test so
  the assertion exercises validateDiffURL specifically.
- Add ArchivedFilteredOut and ArchivedTrueComposes E2E subtests to
  cover the SQL-level interaction between the archived and diff_url
  predicates.

Also includes regenerated apidoc/swagger output that was already
drifted on main from the stdlib net/url documentation update.
@kylecarbs
kylecarbs force-pushed the kylecarbs/chats-diff-url-filter branch from d36b0fd to dcb2b64 Compare May 13, 2026 14:09
@kylecarbs
kylecarbs merged commit 5040ab6 into main May 13, 2026
28 checks passed
@kylecarbs
kylecarbs deleted the kylecarbs/chats-diff-url-filter branch May 13, 2026 15:06
@github-actions github-actions Bot locked and limited conversation to collaborators May 13, 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