Skip to content

feat: sort AI sessions by last prompt time - #24440

Merged
jeremyruppel merged 10 commits into
mainfrom
jeremyruppel/aigov-208-sessions-page-sorts-by-initial-interception-time
Apr 22, 2026
Merged

feat: sort AI sessions by last prompt time#24440
jeremyruppel merged 10 commits into
mainfrom
jeremyruppel/aigov-208-sessions-page-sorts-by-initial-interception-time

Conversation

@jeremyruppel

@jeremyruppel jeremyruppel commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Previously, the sessions list sorted by MIN(started_at) across interceptions, so sessions with old start times but recent activity would sink to the bottom of the list regardless of how recently they were used.

ListAIBridgeSessions now sorts by COALESCE(MAX(prompt.created_at), MIN(started_at)) DESC, exposed as the non-nullable last_active_at field. Sessions with prompts surface by last activity; sessions with no prompts fall back to their start time.

The original implementation used two separate columns (last_active_at as a nullable prompt timestamp and sort_at as the non-nullable cursor key). This revision collapses them into a single last_active_at that is always set — simplifying the SQL, the Go conversion, the API type, and the frontend.

🤖 Generated with Claude Code

Copilot AI 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.

Pull request overview

Updates AI Bridge session listing to sort sessions by most recent user prompt time (with a fallback to session start time), exposes this value as last_active_at in the API, and updates the UI/docs accordingly.

Changes:

  • Update ListAIBridgeSessions SQL to compute last_active_at (latest prompt timestamp) and sort by COALESCE(last_active_at, started_at) DESC.
  • Expose last_active_at through the Go SDK, generated TS types, Swagger, and docs; update the UI to display it.
  • Add a dedicated test case covering the new sort semantics.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
site/src/pages/AIBridgePage/ListSessionsPage/ListSessionsRow.tsx Display last_active_at (fallback to started_at) in the sessions list UI.
site/src/api/typesGenerated.ts Add last_active_at to the generated TS AIBridgeSession type.
enterprise/coderd/aibridge_test.go Update ordering comment and add a new test validating sort-by-last-active behavior.
docs/reference/api/schemas.md Document last_active_at in the schema examples/table.
docs/reference/api/aibridge.md Add last_active_at to the endpoint response example.
codersdk/aibridge.go Add LastActiveAt to codersdk.AIBridgeSession.
coderd/database/sqlc.yaml Add sqlc type override for session_page.last_active_at.
coderd/database/queries/aibridge.sql Implement prompt-based aggregation and sorting; return last_active_at.
coderd/database/queries.sql.go Regenerate SQL output for new columns/order.
coderd/database/modelqueries.go Scan the new last_active_at column.
coderd/database/db2sdk/db2sdk.go Map DB last_active_at into the SDK session response.
coderd/apidoc/swagger.json Add last_active_at to API schema.
coderd/apidoc/docs.go Update embedded Swagger template for last_active_at.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread coderd/database/queries/aibridge.sql Outdated
@jeremyruppel jeremyruppel changed the title feat(site): sort AI sessions by last prompt time feat: sort AI sessions by last prompt time Apr 16, 2026

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

Can you also please add a negative test case such that when a session's root interception has no associated prompt we still get the expected result?

Comment thread coderd/database/queries/aibridge.sql Outdated
jeremyruppel and others added 4 commits April 17, 2026 14:37
Previously, the sessions list was sorted by MIN(started_at) across
interceptions, so sessions with old start times but recent activity
would appear at the bottom.

ListAIBridgeSessions now joins aibridge_user_prompts (via a
pre-aggregated subquery to keep the join one-to-one) and sorts by
COALESCE(MAX(prompt.created_at), MIN(started_at)) DESC. Sessions with
prompts surface by last activity; sessions with no prompts fall back to
their start time. The sort key is also exposed as last_active_at in the
API response.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
…ions query

Replace the derived subquery (full-table GROUP BY over all prompts) with
a LEFT JOIN LATERAL that fetches the latest prompt per interception via
LIMIT 1. The existing (interception_id, created_at DESC) composite index
makes this an index-only lookup per interception row instead of a scan
over the entire aibridge_user_prompts table.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Existing test sessions were missing user prompts, which relied on the
COALESCE(MAX(latest_prompt_at), MIN(started_at)) fallback for sort_at
rather than exercising the prompt-based sort path.

Add a prompt to each test session so the sort exercises last_active_at
directly. Also add PromptlessSessionSortsByStartedAt to explicitly cover
the fallback: a session with no prompts must still appear in results and
sort by its started_at, not disappear due to a NULL sort_at in the HAVING
row-value comparison. Document both behaviors in the SQL query comments.
@jeremyruppel
jeremyruppel force-pushed the jeremyruppel/aigov-208-sessions-page-sorts-by-initial-interception-time branch from b24d723 to 714936a Compare April 17, 2026 15:37

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

Looking good! A few things to correct but directionally making sense 👌

Comment thread coderd/database/queries/aibridge.sql Outdated
Comment thread coderd/database/queries/aibridge.sql Outdated
Comment thread coderd/database/sqlc.yaml Outdated
Comment thread enterprise/coderd/aibridge_test.go Outdated
Comment thread enterprise/coderd/aibridge_test.go Outdated
Comment thread enterprise/coderd/aibridge_test.go
Comment thread enterprise/coderd/aibridge_test.go Outdated
jeremyruppel and others added 3 commits April 20, 2026 19:12
…ssions

Addresses reviewer feedback asking why we need both last_active_at and
sort_at. We don't — COALESCE(MAX(prompt.created_at), MIN(started_at))
produces a single non-nullable column that serves both purposes.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Comment thread coderd/database/sqlc.yaml Outdated
jeremyruppel and others added 2 commits April 21, 2026 18:34
…ml override

Explicit ::timestamptz cast lets sqlc infer time.Time directly, removing
the need for the manual column override in sqlc.yaml.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

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

LGTM, thanks a bunch @jeremyruppel!

I think we also discussed making the date a bit more clear in the table:

Image

"Timestamp" -> "Last prompt at" or something like that

Happy for this to be a follow-up

@jeremyruppel
jeremyruppel force-pushed the jeremyruppel/aigov-208-sessions-page-sorts-by-initial-interception-time branch from 040d869 to 4fece9d Compare April 22, 2026 15:04
@jeremyruppel
jeremyruppel merged commit c23abc6 into main Apr 22, 2026
31 checks passed
@jeremyruppel
jeremyruppel deleted the jeremyruppel/aigov-208-sessions-page-sorts-by-initial-interception-time branch April 22, 2026 16:06
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 22, 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.

3 participants