Repository navigation
fix(hosts): sort unpaginated sessions before projection - #5015
Conversation
🦋 Changeset detectedLatest commit: 9a1327e The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9a1327e67c
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| } | ||
| } | ||
| items.sort((a, b) => b.updated_at.localeCompare(a.updated_at) || b.id.localeCompare(a.id)); | ||
| items.sort((a, b) => context.compare(context.position(a), context.position(b))); |
There was a problem hiding this comment.
Preserve the descending ID tie-break for timestamp ties
When two sessions have the same timestamp under updated_at_desc or created_at_desc, compareSessionPositions returns 0, so this stable sort now preserves local/host source order. The previous comparator explicitly ordered equal updated_at values by descending ID, matching the local route's comparator; consequently, an unpaginated hosts=include request can now return a different order from the paginated endpoint solely based on which host supplied each tied session. Retain the established ID tie-break when sorting the collected items.
AGENTS.md reference: AGENTS.md:L110-L112
Useful? React with 👍 / 👎.
| items: query.fields === undefined | ||
| ? items | ||
| : items.map((item) => project(item, false, item.host_id) as Session), |
There was a problem hiding this comment.
Preserve full responses for semantically empty fields
For an unpaginated request such as GET /sessions?hosts=include&fields=, the schema accepts the value and parseSessionFields treats it as no projection, so this request previously returned full session objects. The new check considers every defined string—including empty, whitespace-only, or comma-only values—to be a projection and silently returns only {id, archived, host_id}. Determine projection using the parsed, nonempty fields before clearing fields from the source query.
AGENTS.md reference: AGENTS.md:L110-L112
Useful? React with 👍 / 👎.
Requirement or Bug
桌面端和 Web 在跨 SSH Host 的无分页会话列表中,ID 投影请求会返回
50001,显式指定更新时间升序或创建时间降序时也会得到默认排序。本 PR 让这些请求按现有 API 契约返回完整结果与请求的顺序。Bug Reproduction Steps
updated_at,返回Cannot read properties of undefined (reading 'localeCompare')。sort=updated_at_asc或sort=created_at_desc,修复前转发和最终合并仍按updated_at_desc。Root Cause
无分页全量分支强制覆盖
sort,并在聚合阶段提前生成仅含 ID 与归档状态的条目。最后的硬编码降序排序因此读取缺失字段。本 PR 在全量分支内恢复“完整字段收集与排序,最后投影”的顺序,属于对既有聚合流程的根本修复。Code Changes
packages/kap-server/src/routes/sessionsAcrossHosts.ts:保留请求的排序,复用已有比较器;ID 投影移到合并排序之后。公共的分组及分页聚合逻辑未改。packages/kap-server/test/hostAggregation.test.ts:真实路由覆盖无分页 ID 投影;聚合测试精确覆盖升序、创建时间降序、默认降序及远端host_id。.changeset/fix-host-unpaginated-session-lists.md:记录用户可感知修复。Behavior Changes and Affected Users
50001code: 0、ID、归档状态与远端host_idsort=updated_at_asc和sort=created_at_desc被忽略受影响模块为 kap-server 的跨 Host 无分页列表。真实路由覆盖 ID 投影,聚合测试覆盖三种排序;既有分页与分组用例保留。跨 Host 的
before_id/after_id语义不在本次改动范围内。验证结果
50001。pnpm exec vitest run --maxWorkers=15 --project kap-server:69 个测试文件、1414 个测试全部通过。pnpm exec vitest run --maxWorkers=15 --project kap-server test/hostAggregation.test.ts:18 个测试通过。pnpm --filter @moonshot-ai/kap-server run typecheck:通过。pnpm exec oxlint --type-aware packages/kap-server/src/routes/sessionsAcrossHosts.ts packages/kap-server/test/hostAggregation.test.ts:0 warnings,0 errors。pnpm --filter @moonshot-ai/kap-server run build:通过。Checklist
/approve). 本 PR 修复 PR 4784 合并后确认的会话列表回归,问题与修复范围已与维护者确认。gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update. 接口契约未变,无需更新用户文档。