Skip to content

feat: add session title generation telemetry - #4699

Merged
7Sageer merged 1 commit into
MoonshotAI:mainfrom
7Sageer:feat/session-title-telemetry-pr
Nov 5, 2026
Merged

7Sageer merged 1 commit into
MoonshotAI:mainfrom
7Sageer:feat/session-title-telemetry-pr

Conversation

@7Sageer

@7Sageer 7Sageer commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

自动生成 Session 标题此前没有独立的可观测数据,无法从客户端遥测判断生成是否成功、耗时多久,以及失败集中在哪个阶段。本 PR 为实际生成过程增加匿名完成事件,同时不采集标题、对话或后端错误原文。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

在共享的标题生成服务中统一记录 session_title_generation_finished,CLI/TUI、Desktop、Web 和 SDK 的既有调用链都会覆盖到,不需要增加客户端埋点或修改请求契约。

generateTitle
├── 已有自定义/生成标题且未 force → 跳过,不计入生成统计
└── 实际生成并结束
    ├── success:trigger + source + duration_ms + title_chars + title_outcome
    ├── unavailable:trigger + source + duration_ms + 有限的 reason
    └── error:trigger + source + duration_ms + unexpected + error_type

关键行为:

  • trigger 沿用现有调用语义:force: true 记为 manual,其余记为 auto;现有 SDK 中 force 的含义和返回值均不改变。
  • source 保留 first_turn、digest、user_prompts 三种输入来源。
  • duration_ms 覆盖从提取标题输入、请求标题接口到本地标题写入结束的耗时。
  • 成功只记录应用后的标题字符数,以及是否因长度上限被截断;无有效标题、鉴权失败和请求失败归为 unavailable,意外异常归为 error 并继续按原逻辑抛出。
  • reason 使用有界枚举;error_type 只记录错误类名。标题内容、对话内容、后端响应和错误消息不会进入该事件。

阅读顺序:先看 packages/agent-core-v2/src/session/sessionTitle/sessionTitleService.ts 的生成与上报,再看 packages/agent-core-v2/src/app/telemetry/events.ts 的字段定义;测试集中在既有 sessionTitleService.test.ts。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
实际完成一次标题生成 无独立标题生成事件 上报结果、触发方式、来源和耗时 需要分析标题生成功能的遥测消费者 沿用 telemetry = false 和各入口既有的遥测开关
标题生成成功 只落库并广播标题 额外记录标题字符数和是否截断 CLI/TUI、Desktop、Web 的标题生成链路 不上传标题原文;关闭遥测则不上报
无标题、鉴权失败、请求失败 仅保留原标题,部分原因写 debug 日志 额外记录有限的失败原因和耗时 未登录、缺少有效对话、接口不可用的用户 错误消息和响应原文不上传;原降级行为不变
意外异常 原样抛出 记录错误类名后原样抛出 调用生成接口的 SDK/客户端 原异常类型与传播方式不变
已有自定义/生成标题且未强制生成 直接跳过 保持跳过且不计入成功率分母 已命名或已生成标题的 Session 既有 force 行为不变

受影响模块与覆盖:

  • 标题生成服务:真实 fetch 响应桩覆盖自动成功与固定 125ms 耗时、手动生成、201 字标题截断、请求失败、意外异常、无输入与已有标题跳过;同时断言标题及错误原文未进入事件。
  • 遥测注册表:字段名和事件元数据通过既有 registry 校验,事件沿 Session 级遥测上下文发送。
  • 兼容性:标题生成公开 REST 路径、CLI 自动标题、SDK 及 telemetry 既有用例通过;请求参数、配置、磁盘格式与 UI 均未改变。
验证结果
  • 最新 origin/main 上全量 Vitest:877 个测试文件通过、5 个跳过;17,605 个测试通过、21 个跳过,0 失败。
  • @moonshot-ai/agent-core-v2 TypeScript 类型检查及 tsdown 构建通过。
  • 完整 pnpm lint 通过,0 errors;仓库既有 warnings 保留。
  • 只读安全审查确认无密钥、内部标识、标题/对话/后端错误原文上报。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).(无关联 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.(用户调用方式、配置及标题行为未改变,无需更新文档)

Copilot AI balanced review requested due to automatic review settings November 5, 2026 06:14
@changeset-bot

changeset-bot Bot commented Nov 5, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7e3feb6

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

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@7Hanrui

7Hanrui commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 5, 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-11-05T06:27:07.177059Z 7e3feb6 Draft marked ready
ℹ️ 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.

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

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

const track = (
result: Omit<SessionTitleGenerationFinishedEvent, 'duration_ms' | 'trigger' | 'source'>,
): void => {
this.telemetry.track2('session_title_generation_finished', {

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 session context for late title telemetry

When a session is closed, reloaded, or deleted while the title HTTP request is pending, its telemetry binding is disposed before this call runs. BoundTelemetryService.ambient() then deliberately falls back to the App context, so session_title_generation_finished is sent without the session id; capture a withContext snapshot before the first await (or finish/cancel the attempt before teardown) so late completion remains attributable to the session.

Useful? React with 👍 / 👎.

const title = result.title.slice(0, MAX_GENERATED_TITLE_LENGTH);
const applied = await this.metadata.setGeneratedTitleIfUncustomized(title, { force });
if (!applied) return undefined;
if (!applied) return { reason: 'custom_title' };

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 Distinguish disposal from a custom-title race

When a session is closed, reloaded, or deleted while the title request is in flight, SessionMetadata.setGeneratedTitleIfUncustomized() can return false from prepareWrite() solely because the metadata service has been disposed, even though no custom title exists. This line maps every false result to reason: custom_title, corrupting the new reason breakdown; confirm titleKind before selecting that reason or add a lifecycle-specific reason.

Useful? React with 👍 / 👎.

@7Sageer
7Sageer marked this pull request as ready for review November 5, 2026 06:24
@pkg-pr-new

pkg-pr-new Bot commented Nov 5, 2026

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

commit: 7e3feb6

@7Sageer
7Sageer merged commit 2a6d1cf into MoonshotAI:main Nov 5, 2026
18 checks passed
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.

4 participants