Skip to content

fix(message): restore indexed reserved session request lookup - #1391

Merged
ding113 merged 4 commits into
devfrom
session-requests-pagination
Aug 3, 2026
Merged

fix(message): restore indexed reserved session request lookup#1391
ding113 merged 4 commits into
devfrom
session-requests-pagination

Conversation

@ding113

@ding113 ding113 commented Aug 3, 2026

Copy link
Copy Markdown
Owner

Summary

  • restore the COALESCE(session_identity, session_id) predicate for reserved pfx: / sid: session request timelines so PostgreSQL can use idx_message_request_session_identity_created_at
  • retain an explicit session_identity = identity guard for unscoped lookups, preserving reserved namespace isolation from same-named physical Session IDs
  • retain owner-scoped compatibility for legacy rows with session_identity IS NULL
  • add compiled-SQL regression coverage for both reserved prefixes and route coverage for URL-encoded identities

Production symptom

GET /api/v1/sessions/{encoded-pfx-identity}/requests?page=1&pageSize=20&order=desc returned 400 session.action_failed.

The route and query parameters are valid. The action wraps internal failures as this 400 response, while the underlying failure is the session request query timing out on a large message_request table.

Root cause

The reserved-identity request-list path generated only:

session_identity = $1

The existing index starts with:

COALESCE(session_identity, session_id)

The bare predicate could not become the expression index's Index Cond, so an admin/unscoped request could scan the large table until statement_timeout.

The repaired predicate is:

COALESCE(session_identity, session_id) = $1
AND session_identity = $2

For owner-scoped legacy compatibility, the second term is:

(session_identity = $2 OR session_identity IS NULL)
AND user_id = $3

Local PostgreSQL EXPLAIN changed from a filter-only plan to an index scan with:

Index Cond: (COALESCE(session_identity, session_id) = ...)
Filter: (session_identity = ...)

Validation

  • focused regression after rebase: 3 files, 23 tests passed
  • full Vitest suite before the final rebase: 858 files passed, 8370 tests passed, 13 skipped
  • bun run build: passed before the final rebase
  • bun run lint: passed after rebase
  • bun run typecheck: passed after rebase
  • git diff --check origin/dev...HEAD: passed
  • independent Standards and Spec reviews: no remaining findings

The intervening dev commit only changes dashboard usage-log time-filter UI and tests. The focused session tests, lint, and typecheck were rerun after rebase.

Deployment note

This PR does not change the action's broad error classification. Production recovery requires this PR to be merged and deployed; the reported remote URL has not been re-tested against a deployment from this branch.

Greptile Summary

This PR restores the expression-index predicate for reserved session request lookups while preserving owner-scoped legacy-row support and preventing reserved identities from aliasing physical sessions.

  • Rewrites the reserved canonical lookup into an index-compatible conjunction without changing its result set.
  • Adds SQL-shape tests for owner-scoped and unscoped pfx: and sid: identities.
  • Adds API coverage for URL-encoded reserved session identities.

Confidence Score: 5/5

The PR appears safe to merge, with no actionable correctness, security, or compatibility regressions identified.

The new predicate is logically equivalent to the prior lookup for all nullable column combinations, matches the existing PostgreSQL expression index, and preserves the deliberate distinction between owner-scoped legacy fallback and unscoped canonical lookup.

Important Files Changed

Filename Overview
src/repository/message.ts Restores the expression-index-compatible reserved-session predicate while retaining the existing scoped and unscoped result semantics.
tests/unit/repository/message-session-request-query.test.ts Verifies compiled SQL and parameter ordering for scoped and unscoped pfx: and sid: lookups.
tests/api/v1/sessions/sessions.test.ts Confirms URL-encoded reserved identities are decoded and forwarded correctly by the session request endpoint.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Reserved session identity] --> B[Match COALESCE of session identity and session ID]
  B --> C{Owner scoped?}
  C -->|Yes| D[Require matching identity or legacy NULL identity]
  C -->|No| E[Require explicit matching session identity]
  D --> F[Apply owner and request filters]
  E --> F
  F --> G[Count and return session requests]
Loading

Reviews (1): Last reviewed commit: "style(message): normalize comment punctu..." | Re-trigger Greptile

Context used:

ding113 added 4 commits August 3, 2026 15:40
…identities

The canonical session lookup for reserved identities had drifted away
from the messageSessionIdentity expression index, causing queries to
miss the optimised index path. The condition now always anchors on
the expression-index column while still allowing owner-scoped
lookups to match legacy null-identity rows via the session_id
fallback.

Unscoped reserved lookups remain narrow and do not pick up
unrelated null-identity rows, preserving reserved identity
isolation.
…lback

Extend the reserved identity SQL contract test to verify that
the generated where clause uses coalesce when resolving
owner-scoped session requests for legacy clients.
Add encoded-identity regression cases to the v1 session requests route
test, verifying that URL-encoded session identities (pfx: and sid:
prefixed) resolve correctly through the endpoint.

Rewrite the repository-level session request query tests to compile
the generated SQL via PgDialect instead of string matching, and
parameterize them across both pfx: and sid: identity prefixes. The
assertions now verify the exact coalesce expression, identity guards,
and parameter bindings for both owner-scoped legacy fallback and
unscoped canonical lookups.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

本次变更更新会话 identity 查询条件。指定用户时允许空 sessionIdentity 记录。未指定用户时要求精确匹配。测试覆盖 PostgreSQL SQL 生成和 URL 编码会话标识符路由。

Changes

会话 identity 查询流程

Layer / File(s) Summary
Identity 查询条件与 SQL 验证
src/repository/message.ts, tests/unit/repository/message-session-request-query.test.ts
messageCanonicalSessionLookup 使用 messageSessionIdentity 表达式索引条件。指定用户时支持空 sessionIdentity fallback。测试验证生成 SQL、参数顺序和 session_id 别名排除规则。
编码会话标识符路由测试
tests/api/v1/sessions/sessions.test.ts
新增 pfx:scope:fingerprintsid:canonical-session 的 URL 编码请求测试。测试验证原始标识符、默认分页 1/20 和降序参数。

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed 标题准确概括了恢复保留会话请求查询索引兼容性的主要变更。
Description check ✅ Passed 描述说明了查询性能根因、兼容性要求、测试覆盖和验证结果,且与变更内容直接相关。
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch session-requests-pagination

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ding113
ding113 merged commit 8baef8b into dev Aug 3, 2026
13 of 14 checks passed
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Claude Code Hub Roadmap Aug 3, 2026
@github-actions github-actions Bot added the size/S Small PR (< 200 lines) label Aug 3, 2026

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

Code Review Summary

No significant issues identified in this PR.

PR Size: S

  • Lines changed: 97
  • Files changed: 3

Review Coverage

  • Logic and correctness - Clean
  • Security (OWASP Top 10) - Clean
  • Error handling - Clean
  • Type safety - Clean
  • Documentation accuracy - Clean
  • Test coverage - Adequate
  • Code clarity - Good

Automated review by Codex AI

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

  • Reviewed PR #1391 end-to-end against the diff, repo standards, and the six requested review perspectives.
  • Applied the size/S label and posted the GitHub review summary on the PR.
  • No issues cleared the reporting threshold after context/false-positive validation, so I did not add inline review comments.
  • I could not run the targeted Vitest commands in this runner because bun/bunx are not installed, so the review outcome is based on static diff and code-path analysis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Small PR (< 200 lines)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant