Skip to content

feat: share session rating rules across CLI and server - #4272

Merged
Grapedge merged 20 commits into
mainfrom
session-rating-desktop
Oct 20, 2026
Merged

Grapedge merged 20 commits into
mainfrom
session-rating-desktop

Conversation

@Grapedge

Copy link
Copy Markdown
Collaborator

Requirement or Bug

让 CLI 和桌面端共用会话评分规则、云控和统计,同时由各客户端决定选择、撤销和结算,避免远程请求乱序改变用户答案。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

客户端
  回合结束后请求 evaluate(turn_id)
  本地处理展示、选择、撤销和致谢
  报告 shown / respond / abandon

kap-server
  核对当前 transcript 回合,再同步评估
  接收报告,不管理客户端交互状态

packages/product
  共用云控、门禁、抽样、计数和冷却
  保存有界的冻结快照,按首次结算上报

新回合、会话卸载和后续评估不会清掉旧回答快照。重复报告不重复结算,未显示的 offer 不产生 appeared。账本不跨进程重启保存,也不承诺报告持久送达。

Behavior Changes and Affected Users

行为 之前 之后 受影响用户 退出或迁移方式
评分开关 位于 tui.toml,只供 CLI 使用 位于全局 config.toml,CLI 和桌面共用 配置过评分开关的 CLI 用户 启动时迁移;全局已有值优先;失败保留旧值并警告
旧配置文件 启动时不因评分设置而重写 成功迁移后删除旧键,保留其他配置值,但不保留注释 tui.toml 含旧评分键的用户 迁移前可备份文件;后续使用全局设置
保存其他 TUI 偏好 自动标题关闭值可能未被保留 保留自动标题关闭值和尚未迁移的评分值 关闭自动标题或迁移失败后保存其他偏好的用户 无需操作
评分接口 无评分 REST 接口 新增 POST /sessions/{id}/rating 的四种操作 配套桌面客户端及远程连接用户 disable_feedback_survey = true 关闭提示;现有其他接口不变
用户轮次统计 评分事件使用挂载后的用户轮次 使用含历史的会话用户轮次;历史不可靠时省略 恢复会话的评分统计 预热和间隔仍按挂载计数,不改变触发门槛
CLI 评分交互 规则和交互均在 TUI 私有实现 共用产品规则,交互仍在 TUI,保留输入保护、撤销和致谢 CLI 评分用户 默认行为保持;可在 /settings 关闭评分

旧路径的功能归属如下。

  • survey_popup、长上下文优先级、模型门禁、抽样、全局冷却和事件属性移入 packages/product。
  • 命名云控客户端仍接受区域、访问令牌、缓存目录和 KIMI_CODE_BASE_URL。缓存提示、推荐思考强度和横幅改用共享实现。
  • TUI 的空闲等待、草稿和输入冲突、数字防抖、600ms 保护、方向键和 Esc 留在控制器。
  • 继续沿用遥测开关。关闭遥测时不展示评分;报告时再次检查开关。

服务端新增接口供配套桌面端使用,没有新增评分 WebSocket 事件或视图恢复接口。旧客户端不调用该接口,不需要降级分支。未修改模型提示词、provider 协议或其他客户端的 UI。

已运行产品包全量测试、服务端评分/API 快照/配置测试、CLI 全量测试,以及相关 typecheck 和 lint。CLI 的两项 native artifact 测试因环境缺少 zstd 失败,对应脚本未改。测试覆盖迟到、重复、跨回合、卸载后报告、历史计数、遥测关闭和配置迁移失败后的重试。配置与 API 文档已核对中英两版;文档流程所需的 changelog 同步脚本当前缺失,未执行该步骤。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve). 无关联 GitHub issue。
  • 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. 已核对双语文档;同步步骤因缺少脚本停止。

Move the session rating product logic out of the TUI into
@moonshot-ai/kimi-code-product so the CLI and kap-server share one engine:
survey_popup parsing and the effective config, the product gates, the
Appearance state machine, event properties, the kfc check, the global
cooldown file, and the named cloud config client.

The TUI survey controller becomes an adapter. It translates SDK events
into engine facts, reads its environment from appState, applies the
terminal-only UI vetoes before asking the engine to evaluate, and keeps
digit debounce, the 600ms entrance guard, draft detection, Esc, arrow
keys and rendering. The cloud config client now takes its region, base
URL override and cache directory from the host.

Behavior notes: the engine rereads the shared cooldown file before each
evaluation instead of once per mount, and a vetoed evaluation no longer
draws the session sample early.
Add the top-level disable_feedback_survey key to config.toml so the CLI
and the desktop app share one opt-out. The SDK config schema and TOML
writer, the kap-server config route and the config docs learn the key.

The CLI /settings toggle now writes config.toml. On startup the CLI moves
a legacy tui.toml value into config.toml and drops it from tui.toml; when
config.toml cannot be written it keeps honoring the legacy value, warns,
and retries on the next start. The engine reads the key before every
evaluation, so a change from either client applies without a restart.
Add a replayable userTurnTally state that counts main-agent prompts the
shared user turn classification accepts. It folds over the full journal,
so undo, context clears, and compaction never lower the count, and a fork
counts only the turns it copied. A prompt that cannot be classified, or a
user message recorded before the first prompt, turns the count unknown.

The restore pass that already replays every record builds the state, so
hosts read the session user turn count without a second read of the log.
The server host now feeds the engine everything the CLI reports: the
managed model route for the kfc check, busy state from the loop and
compaction, the session user turn count from core, resumed subagents from
session metadata, and tool call, subagent, and compaction facts from the
main agent. The consent gate reuses the server telemetry rule and is read
before every evaluation.

survey_popup comes from the cloud through the product client config
client with region, token, and cache directory taken from core; the first
evaluation request starts the fetch. Preview mode now needs debug
endpoints, so production builds ignore it. Server shutdown still ends
every rating silently.
Evaluate rating offers synchronously for a target turn and retain reporting snapshots independently of session lifetime. Settle selection and undo locally in each client and acknowledge actual display separately. Preserve existing preferences when migrating the survey opt-out.
@changeset-bot

changeset-bot Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c91d0ac

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T18:47:52.421991Z 6526ff3 New commits
ℹ️ 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 Oct 19, 2026 •

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

commit: c91d0ac

Separate common rating configuration from Node-backed feeds and migrate CLI and server imports. Derive build entries from package exports and add entry-boundary checks.

@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: 1cefbef44d

ℹ️ 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".

Comment thread apps/kimi-code/src/tui/config.ts Resolved
Share survey origin classification and historical counting across SDK
replay and the session rating host. Move survey event definitions into
product and retain generic telemetry dispatch in core.

Attach counters before normal restore and serialize late replay with
history restoration to preserve counts and queued events.

@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: 18237a0e7f

ℹ️ 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".

Comment thread apps/kimi-code/src/tui/kimi-tui.ts Resolved

@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: de1437a845

ℹ️ 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".

Comment thread apps/kimi-code/src/tui/kimi-tui.ts Resolved
Drops the tui.toml migration-history sentence added earlier on this
branch; public docs describe what the key does now, not its move.
@Grapedge
Grapedge merged commit ce0834a into main Oct 20, 2026
16 checks passed
@Grapedge
Grapedge deleted the session-rating-desktop branch October 20, 2026 06:20
@github-actions github-actions Bot mentioned this pull request Oct 20, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Oct 22, 2026
Ported from MoonshotAI/kimi-code 2.3.0 (ce0834a).

Introduces `packages/product` — the shared product layer (`client-configs/node`,
`session-rating`, `session-rating/node`) — and moves the session-rating engine,
survey gates, cooldown store and client-config cache into it so the CLI and
kap-server evaluate the same rules. The TUI survey controller and the server's
`POST /sessions/{id}/rating` route both drive that engine.

Fork adaptation of the new package:
- `packages/product` ships as a Bun workspace member: `test` runs
  `scripts/run-package-tests.mjs`, `vitest.config.ts` is gone, the nine test
  files are `bun:test`, and `tsconfig.json` maps `#/*` to `./src/*.ts` (the one
  thing Bun needs and vitest resolved through its own config).
- `bun install` links it; `flake.nix` and both `package.json` dependency lists
  keep the fork's `@moonshot-ai/kimi-i18n` alongside the new package.

Conflicts resolved:
- `klient/contract/global/events.ts`: the fork's root import for
  `ConfigChangedEvent` resolved to the bus-event type of the same name, so
  `config.sectionChanged` carried the wrong payload and the new
  `SDKRpcClientV2.onConfigSectionChanged` override stopped compiling. It now
  imports the section payload from `app/config/config` (as upstream does) and
  uses `ConfigSectionChangedEvent` for the section channel.
- `sessionLifecycleService.ts`: kept the fork's compact formatting and added
  upstream's `onDidCreateScope` seed plus `scopeCreated.fire(handle)`.
- `eventDispatcherService.ts`: added upstream's `replayFailure` rethrow.
- `kimi-tui.ts`: kept the fork file — it carries ~40 fork-only members (token
  statistics, transcript mounting, the status-surface focus fix), which taking
  upstream's rewrite would delete — and hand-ported its six survey deltas: the
  `Unsubscribe` import, `migrateLegacySurveySetting`, `removeLegacySurveySetting`,
  the `surveySettingSubscription` field/teardown, the `disableFeedbackSurvey`
  config-section listener (guarded with a `typeof` check because several TUI test
  doubles omit it) and `notifyTurnStarted(event.origin)`.
- `survey-panel.ts`, `survey-controller.ts`, `survey-policy.ts`,
  `client-configs.ts`: took upstream, then restored the fork's localisation
  (`surveyQuestion()`/`surveyOptionLabels()`, `t('tui.messages.surveyThanks')`).
- survey tests: `survey-controller.test.ts`, `survey-policy.test.ts` and
  `survey-preferences.test.ts` were rewritten upstream against the new engine and
  taken over with the `bun:test` conversion; `config.test.ts` likewise, plus the
  fork's `locale`/`disableFeedbackSurvey` fields in its expected objects.
  `kimi-tui-survey.test.ts` and `kimi-tui-message-flow.test.ts` waited on the
  controller's old `cooldownReady` flag, which the product mount resolves
  synchronously; those waits became a single macrotask yield.
- `rest-session-rating.ts`: the fork's `defineRoute` rejects non-strict request
  bodies, so the rating body became `z.strictObject` per union member.
- `rest-config.ts`: added `disable_feedback_survey` and kept the fork's
  `.strict()`.

Fixes surfaced by the port:
- `renderTuiConfig` wrote `locale = "undefined"` when the caller omitted the
  fork-only `locale` field, and the strict parser then rejected the file; it now
  falls back to `DEFAULT_TUI_CONFIG.locale`.
- `fullCompaction` pre-existing failure untouched (fails identically before).

Verification:
- bun run typecheck: exit 0, zero errors across every package and app
- packages/product: 272 pass / 0 fail
- apps/kimi-code: 4712 pass / 0 fail / 0 errors, 22 skip
- packages/kap-server: 1367 pass / 0 fail, 9 skip
- packages/node-sdk: 296 pass / 6 fail; the six are pre-existing in
  auth-facade.test.ts (the fixture omits `limit.context`, which
  packages/oauth already requires on the parent commit) and the file is
  untouched here
- packages/agent-core-v2: 7884 pass / 1 fail / 0 errors; the one failure is the
  fullCompaction pre-shrink case that fails identically on the parent commit
- check:no-comments, no-hardcoded-prose, t-coverage, locale-keys,
  catalog-versions, nix-workspace all OK
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.

2 participants