Skip to content

fix(hosts): sort unpaginated sessions before projection - #5015

Merged
7Sageer merged 1 commit into
mainfrom
fix/host-session-list-all
Nov 17, 2026
Merged

7Sageer merged 1 commit into
mainfrom
fix/host-session-list-all

Conversation

@7Sageer

@7Sageer 7Sageer commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

桌面端和 Web 在跨 SSH Host 的无分页会话列表中,ID 投影请求会返回 50001,显式指定更新时间升序或创建时间降序时也会得到默认排序。本 PR 让这些请求按现有 API 契约返回完整结果与请求的顺序。

Bug Reproduction Steps

  1. 配置一台包含托管项目会话的 SSH Host,本机也有会话。
  2. 请求:
    GET /api/v1/sessions?hosts=include&fields=id,archived
  3. 修复前,排序读取已经投影掉的 updated_at,返回 Cannot read properties of undefined (reading 'localeCompare')。
  4. 将查询改为 sort=updated_at_asc 或 sort=created_at_desc,修复前转发和最终合并仍按 updated_at_desc。

Root Cause

无分页全量分支强制覆盖 sort,并在聚合阶段提前生成仅含 ID 与归档状态的条目。最后的硬编码降序排序因此读取缺失字段。本 PR 在全量分支内恢复“完整字段收集与排序,最后投影”的顺序,属于对既有聚合流程的根本修复。

Code Changes

 listAllSessionsAcrossHosts
-  context: force updated_at_desc, project IDs while collecting
+  context: preserve requested sort, collect full sessions
   merge full sessions from local and remote sources
-  sort by updated_at descending
+  sort with the existing session-position comparator
+  project IDs after sorting when fields=id,archived
  • 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

Behavior Before After Who relies on the old behavior Escape hatch
跨 Host 无分页 ID 投影 返回 50001 返回 code: 0、ID、归档状态与远端 host_id 使用轻量会话列表的 desktop、Web 与 API 调用方 无;修复失败的现有请求
跨 Host 无分页显式排序 sort=updated_at_asc 和 sort=created_at_desc 被忽略 按请求排序后返回 指定会话排序的 API 调用方 无;恢复既有参数语义
默认排序、分页列表、项目分组及完整 Session 返回 既有行为 保持不变 现有客户端与 API 调用方 无需迁移

受影响模块为 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:通过。
  • 文档检查:API 参数、响应契约与公开用法均未改变,无需更新用户文档。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve). 本 PR 修复 PR 4784 合并后确认的会话列表回归,问题与修复范围已与维护者确认。
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update. 接口契约未变,无需更新用户文档。

@changeset-bot

changeset-bot Bot commented Nov 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9a1327e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

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

@7Hanrui

7Hanrui commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T08:21:17.133593Z 9a1327e Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 17, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@9a1327e
npx https://pkg.pr.new/@moonshot-ai/kimi-code@9a1327e

commit: 9a1327e

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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)));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Comment on lines +335 to +337
items: query.fields === undefined
? items
: items.map((item) => project(item, false, item.host_id) as Session),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@7Sageer
7Sageer merged commit de1fdaa into main Nov 17, 2026
17 checks passed
@7Sageer
7Sageer deleted the fix/host-session-list-all branch November 17, 2026 12:05
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.

3 participants