Skip to content

feat: report session user turns from the main agent log in rating survey events - #4144

Closed
Grapedge wants to merge 4 commits into
mainfrom
survey-new-user-turn
Closed

Grapedge wants to merge 4 commits into
mainfrom
survey-new-user-turn

Conversation

@Grapedge

@Grapedge Grapedge commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Resume 一个已有多轮对话的 session 后,评分埋点里的 user_turn_count 从 0 重新计数,同一段会话多次挂载上报的轮数对不上它实际经过的用户轮。本 PR 把该字段改为按主 agent 整本日志的会话累计口径上报;问卷的弹出轮数门禁保持挂载后口径不变。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

两个计数口径拆开,门禁不动,只换事件负载的来源:

 on(评分弹窗打开)
-  user_turn_count = mountUserTurnCount          # 本次 TUI 挂载以来的用户轮
+  user_turn_count = countSessionUserTurns(log)  # 主 agent 整本日志里的用户轮
 首次弹出 / 两次弹出间隔的轮数门槛仍读 mountUserTurnCount
turn.ended(用户轮)
  KimiTUI.refreshMainAgentLog
    Session.readMainAgentLog                    (node-sdk 新增 RPC)
      SDKRpcClientV2: 固定 main agent → IEventDispatcher.flush → IWireService.readJournal() 整本
    mainAgentLogCache = log                     (序号 + session 同一性防串会话;reset 时清零)
评分弹窗打开(SurveyController.open)
  captureSnapshot
    sessionUserTurns()
      mainAgentLog() → 缓存(不可用时回退挂载后计数,并 log.warn 留痕)
      countSessionUserTurns(log)                (isUserTurnOrigin:与引擎 undo anchor 同一套来源规则)
    appeared / responded / abandoned 共用同一份快照
 packages/node-sdk/src/
   rpc.ts                  # 抽象 readMainAgentLog(仅 SDKRpcClientV2 一个实现)
   sdk-rpc-client-v2.ts    # 实现:main agent 整本 journal → {type, origin} 投影
   session.ts              # Session.readMainAgentLog 透传
   types.ts                # SessionLogRecord
   context.ts              # isUserTurnOrigin(包引擎 isUndoAnchorOrigin,单一来源)
 apps/kimi-code/src/tui/
   controllers/survey-controller.ts  # user_turn_count 走会话轮次;挂载计数字段更名
   kimi-tui.ts                       # 日志缓存、用户轮结束触发重读、reset 清零
   utils/survey-policy.ts            # countSessionUserTurns

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
feedback_survey / long_context_survey 事件的 user_turn_count 本次 TUI 挂载以来的用户轮,resume 后从 0 重计 主 agent 整本日志的会话用户轮:含 resume 前历史;undo、压缩、/clear 前的轮仍计入(append-only 日志的必然结果);取消/失败的已开跑轮计入 消费这两个埋点事件的分析管道与看板 无——spec 明确以新口径为准;日志读失败时本次 appearance 自动回退旧口径并 log.warn 留痕
问卷弹出条件(首次轮数门槛、间隔轮数门槛、时间门槛、全局冷却、概率、长上下文 token 阈值) 挂载后用户轮 / token 不变 TUI 交互用户 —
SDK 导出面(契约文件 packages/node-sdk/src/index.ts) — 纯增量:Session.readMainAgentLog、SessionLogRecord、isUserTurnOrigin;无删除、无签名变更 SDK 调用方(code-app 的 desktop/web、VS Code 扩展、ACP、外部 SDK 用户) 旧版本客户端与调用方不受影响
日志读失败的可观测性 无此读取 失败时 log.warn('main agent log read failed') 并回退旧口径 排查埋点数据的人 —

受影响模块与测试覆盖:

  • survey-controller.test.ts 新增 6 个用例:日志计数口径、来源分类矩阵(普通 prompt / 夹带 skill / /skill / 插件命令各算 1;steer、cron、goal 续跑、系统提醒、shell、注入、压缩摘要不计)、门禁只用挂载轮(日志 20 轮时挂载 4 轮仍不弹)、同一 Appearance 三事件同数、长上下文臂带会话轮次且仍由 token 触发、20 轮会话跨 resume 的 worked example(23 → remount 后 26)。
  • kimi-tui-message-flow.test.ts 新增接线用例:假日志 8 轮 → 埋点报 8 而非挂载的 5,钉住"轮结束→重读→缓存→快照"链路。
  • sdk-rpc-client-v2.test.ts 新增用例:真引擎 2 个完成轮 + 1 个取消轮后整本日志数为 3;undo、resume 后不变。
  • 既有 TUI 假 Session 全部走"日志不可用 → 回退挂载计数"路径,旧断言(挂载口径)原样通过。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • 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.

Copilot AI balanced review requested due to automatic review settings October 8, 2026 06:21
@changeset-bot

changeset-bot Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c78cdd7

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 8, 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-09T04:53:17.476143Z c78cdd7 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 Oct 8, 2026 •

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

commit: c78cdd7

Copilot AI 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.

🔵 Needs a closer look

It changes the semantics of a telemetry metric (user_turn_count) consumed by external analytics dashboards and touches the public SDK export surface, so a maintainer should confirm the downstream contract impact.

1 open finding
What changed in this PR

This PR changes how the user_turn_count field is populated in rating-survey telemetry events (feedback_survey / long_context_survey). Previously it counted only user turns since the current TUI mount, which reset to 0 after resuming an existing multi-turn session. It now reports a session-cumulative count derived from the main agent's full append-only journal, while the survey pop-up gating thresholds continue to use the mount-relative count. This introduces a small additive SDK surface (Session.readMainAgentLog, SessionLogRecord, isUserTurnOrigin) and wires a per-turn log cache into the TUI.

Changes:

  • Added Session.readMainAgentLog (abstract on SDKRpcClientBase, implemented in SDKRpcClientV2 by flushing the dispatcher and reading the whole journal) plus the SessionLogRecord type and the isUserTurnOrigin predicate reusing the engine's undo-anchor rule.
  • TUI now caches the main-agent log (refreshed on every user-turn end, invalidated by a sequence counter and session identity, cleared on reset) and the survey controller reports the log-derived session count while renaming the mount-relative fields (userTurnCount → mountUserTurnCount, etc.).
  • Extracted sseChunk/sseBody test helpers and added comprehensive tests across the SDK, TUI wiring, and survey controller.
File Description
packages/​node-sdk/​src/​context.ts Adds isUserTurnOrigin wrapping engine isUndoAnchorOrigin.
packages/​node-sdk/​src/​types.ts Adds SessionLogRecord (type + optional origin).
packages/​node-sdk/​src/​rpc.ts Declares abstract readMainAgentLog.
packages/​node-sdk/​src/​sdk-rpc-client-v2.ts Implements readMainAgentLog via flush + readJournal projection.
packages/​node-sdk/​src/​session.ts Passthrough Session.readMainAgentLog.
packages/​node-sdk/​src/​index.ts Exports isUserTurnOrigin.
apps/​kimi-code/​src/​tui/​utils/​survey-policy.ts Adds countSessionUserTurns.
apps/​kimi-code/​src/​tui/​kimi-tui.ts Log cache, per-user-turn refresh, reset clearing.
apps/​kimi-code/​src/​tui/​controllers/​survey-controller.ts Payload uses session count; renames mount-relative fields; keeps gate on mount count.
packages/​node-sdk/​test/​*, apps/​kimi-code/​test/​tui/​* New tests + shared SSE helper extraction.
.changeset/​survey-session-user-turns.md Patch changeset describing the behavior change.

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Comment thread apps/kimi-code/src/tui/kimi-tui.ts
@Grapedge

Grapedge commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

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

ℹ️ 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
Comment thread packages/node-sdk/src/sdk-rpc-client-v2.ts
Comment thread packages/node-sdk/src/rpc.ts Outdated
Comment thread packages/node-sdk/src/sdk-rpc-client-v2.ts
@Grapedge

Grapedge commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 2fb6736366

ℹ️ 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
@Grapedge Grapedge closed this Oct 8, 2026
@Grapedge Grapedge reopened this Oct 8, 2026
@Grapedge
Grapedge force-pushed the survey-new-user-turn branch from 43c149a to 53913bd Compare October 8, 2026 07:35

@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: 53913bd0b8

ℹ️ 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/utils/survey-policy.ts
@Grapedge
Grapedge force-pushed the survey-new-user-turn branch from 53913bd to 1be2dc2 Compare October 8, 2026 07:41

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

ℹ️ 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/controllers/survey-controller.ts Outdated
@Grapedge

Grapedge commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@Grapedge

Grapedge commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: c78cdd725c

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

@Grapedge Grapedge closed this Oct 9, 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.

2 participants