Skip to content

fix(session): 剔除 Session 标题中的 System Instruction 噪声#246

Merged
ThreeFish-AI merged 1 commit into
feature/1.x.xfrom
ThreeFish-AI/session-title-from-user-input
May 25, 2026
Merged

fix(session): 剔除 Session 标题中的 System Instruction 噪声#246
ThreeFish-AI merged 1 commit into
feature/1.x.xfrom
ThreeFish-AI/session-title-from-user-input

Conversation

@ThreeFish-AI

Copy link
Copy Markdown
Owner

背景

PR #245 已实现「从首个用户消息提取 Session 标题并在 Dashboard 展示」,但 _extract_session_title 直接取首个 user 文本块前 30 字,未识别 Claude Code 在首条 user 消息 content 中拼接的 <system-reminder> / <user-preferences> 等系统注入块,导致 Dashboard ?tab=sessions 中所有 Session 标题都退化为同质的「MCP Server Instructions...」模板段,丧失辨识度。

变更要点

  • src/coding/proxy/routing/executor.py
    • 新增 _sanitize_user_text,基于白名单剥离 CC 注入标签 (system-reminderuser-preferenceslocal-command-stdout/stderrbash-input/stdout/stderride_selectionstdinsystem_instruction),正则使用 re.DOTALL | re.IGNORECASE 并容忍标签携带属性 (<tag attr="x">);
    • Slash command 优先识别:检测到 <command-name> 时合成「命令 + 参数」式标题 (例 /commit feat: 新增标题清洗),避免命令式会话标题为空;
    • 兜底剥离残留 <command-*> 包裹标签,最终折叠连续空白;
    • 重写 _extract_session_title:对清洗后为空的 user 文本 part 自动 fallback 到下一个非空 part。
  • tests/test_router_executor.py:新增 TestSanitizeUserText (12 例) 与 TestExtractSessionTitle (8 例) 共 20 个单元测试,覆盖噪声剥离、多行 / 带属性标签、slash command、空白折叠、30 字截断、跳过 assistant 角色、保留用户合法 XML 等场景。

设计取舍

  • 采用显式标签白名单而非「strip 任意 XML 标签」,避免误伤用户输入中合法的 <div>、Markdown 代码块等内容;
  • 清洗逻辑落在 routing/executor.py 而非 compat/canonical.py 解析阶段,保持代理透明性 (vendor 转发请求体不受影响),仅在生成 session 标题时应用;
  • session_meta 表使用 INSERT OR IGNORE历史数据无需迁移,老记录标题原样保留,仅新建 Session 写入清洗后标题。

验证

  • uv run pytest tests/test_router_executor.py — 121/121 通过 (101 既有 + 20 新增);
  • uv run pre-commit run --files src/coding/proxy/routing/executor.py tests/test_router_executor.py — 全绿。

🤖 Generated with Claude Code

Claude Code 会在首条 user 消息 content 中拼接多个 <system-reminder> /
<user-preferences> 等系统注入块,导致原 _extract_session_title 抽取出的
标题在不同会话间高度同质,丧失辨识度。

- 新增 _sanitize_user_text,基于白名单剥离 system-reminder、user-preferences、
  local-command-stdout/stderr、bash-input/stdout/stderr、ide_selection、
  stdin、system_instruction 等 CC 注入标签,折叠空白后返回真实用户输入;
- 优先识别 slash command (<command-name>/<command-args>),合成"命令 + 参数"
  式标题,避免命令式会话标题为空;
- 重写 _extract_session_title,跳过清洗后为空的 user 文本 part,自动 fallback
  到下一个有真实内容的 part;
- 补充 20 个单元测试覆盖噪声剥离、slash command、空白折叠、截断、边界场景。

🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist)
Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
@ThreeFish-AI ThreeFish-AI merged commit 799f5f9 into feature/1.x.x May 25, 2026
6 checks passed
@ThreeFish-AI ThreeFish-AI deleted the ThreeFish-AI/session-title-from-user-input branch May 25, 2026 08:54
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.

1 participant