Skip to content

fix: add session_id fallback conversation index#379

Merged
jalehman merged 3 commits into
mainfrom
schlick-53bdc498-impl-session-id-index
Apr 10, 2026
Merged

fix: add session_id fallback conversation index#379
jalehman merged 3 commits into
mainfrom
schlick-53bdc498-impl-session-id-index

Conversation

@jalehman

@jalehman jalehman commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

What

Adds the missing composite conversations(session_id, active, created_at) index so the session_id fallback lookup can satisfy its ordered LIMIT 1 query from an index instead of scanning and sorting. The migration regression test now also verifies the planner uses that index for the runtime query shape.

Why

Lossless Claw already indexed the analogous session_key lookup path, but the session_id fallback still depended on a scan/sort plan. This change fixes that hot-path asymmetry without adding broader or redundant indexes.

Changes

  • Add session_id, active, created_at index
  • Validate planner uses composite index
  • Keep runtime query shape unchanged

Testing

  • npm test -- --run test/migration.test.ts
  • npm test -- --run test/regression-2026-03-17.test.ts
  • Both commands pass locally

No changeset added because this is an internal query-planning/performance fix with no intended user-facing behavior change.

Add the conversations(session_id, active, created_at) composite index used by the session_id fallback lookup so SQLite can satisfy the ORDER BY/LIMIT path without a scan and temp sort. Extend the migration regression test to assert the index exists and that EXPLAIN QUERY PLAN uses it for the lookup shape.

Regeneration-Prompt: |\n  Improve the session_id-based conversation fallback lookup in lossless-claw without broadening index coverage beyond the hot query shape. The runtime path selects the latest conversation for a given session_id ordered by active DESC then created_at DESC, and the existing migration only created the analogous session_key composite index. Add the matching conversations(session_id, active, created_at) index in the migration path, then update targeted tests to prove the index is present and that SQLite uses it for that ORDER BY/LIMIT lookup without a temp B-tree sort. Keep the change additive and avoid unrelated schema or query changes.
Adjust the migration planner regression to use the same SELECT list as getConversationBySessionId so the assertion validates the real lookup shape instead of a narrower covering-index variant.

Regeneration-Prompt: |
  The new session_id composite index regression should prove the real runtime fallback lookup uses the index, not a simplified proxy query. Update the planner test to run EXPLAIN QUERY PLAN against the same SELECT columns used by getConversationBySessionId and relax the assertion from COVERING INDEX to INDEX while still rejecting any temp B-tree sort.
Add the missing patch changeset for PR #379 so the session_id fallback conversation index fix is release-ready without changing the code or tests in the branch.

Regeneration-Prompt: |
  PR #379 already adds the conversations(session_id, active, created_at) index and targeted tests for the session_id fallback lookup, but it was missing the required Changesets entry before merge. Do not broaden scope or alter the implementation. Add a single patch changeset for @martian-engineering/lossless-claw that briefly describes the performance improvement: the session_id fallback latest-conversation query now uses the matching composite index instead of a scan and temporary sort. Verify only the targeted migration and regression tests relevant to the PR, then commit the changeset addition.
@jalehman jalehman merged commit 7f42703 into main Apr 10, 2026
2 checks passed
@jalehman jalehman deleted the schlick-53bdc498-impl-session-id-index branch April 10, 2026 22:43
@github-actions github-actions Bot mentioned this pull request Apr 10, 2026
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