Skip to content

feat(server): Retrieval Trace 基盤を追加する - #1111

Draft
owk-owk130 wants to merge 6 commits into
developfrom
feat/hil-1-retrieval-trace
Draft

feat(server): Retrieval Trace 基盤を追加する#1111
owk-owk130 wants to merge 6 commits into
developfrom
feat/hil-1-retrieval-trace

Conversation

@owk-owk130

@owk-owk130 owk-owk130 commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

概要

ナレッジ HIL 基盤(計画: .brain/plans/knowledge-hil-foundation.md)の PR 1。
ナレッジ検索 1 回ごとの実行記録を retrieval_runs に保存し、「なぜこの回答になったか」を後から再現できるようにする。後続の HIL レビュー画面(質問・回答・根拠 chunk の表示)の観測基盤。

変更内容

  • retrieval_runs テーブルと migration 0028 を追加
  • searchKnowledge に trace 記録を追加(0 件時も記録、best-effort で応答をブロックしない)
  • createRequestContextanswerRunId を払い出し、1 回答内の複数検索を束ねる
  • chat の onFinish で assistant メッセージ ID を retrieval run へ後埋め(message_feedback との join キー)
  • chunk metadata に contentHash を付与(以後の再 index 分から有効)
  • 保持期間 90 日を data retention に追加
  • cloudflare:workers の動的 import + waitUntil を lib/wait-until.ts に共通化

設計メモ

  • Mastra AI Tracing は不採用: @mastra/cloudflare-d1 が observability ドメイン未対応(公式 docs 明記)で、対応には ClickHouse 等の新インフラが必要なため
  • trace の INSERT と messageId 後埋め UPDATE の競合は、retrievalTracePending で INSERT 完了を待ってから UPDATE する形で解消(codex レビュー指摘)
  • web fallback の判定は trace + llm_usage から事後導出する設計のため本 PR にカラムは無い

未検証

  • onFinish の event.response.uiMessages の assistant ID が feedback の targetMessage.id と一致することは型と実装からの推定で、ローカル実機での突合は未実施。不一致でも messageId が NULL に留まるだけで他機能への影響は無い

🤖 Generated with Claude Code

https://claude.ai/code/session_018BjNFvQjDzMwF8HaZHnJjP

@owk-owk130 owk-owk130 changed the title feat/hil 1 retrieval trace feat(server): Retrieval Trace 基盤を追加する Sep 1, 2026
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Coverage Report for server

Status Category Percentage Covered / Total
🟢 Lines 98.21% (🎯 97%) 3465 / 3528
🟢 Statements 97.63% (🎯 96%) 3629 / 3717
🟢 Functions 98.61% (🎯 97%) 640 / 649
🟢 Branches 89.5% (🎯 87%) 1415 / 1581
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
server/src/db/schema.ts 100% 100% 100% 100%
server/src/lib/wait-until.ts 100% 100% 66.66% 100%
server/src/mastra/request-context.ts 90.32% 87.5% 100% 90.32%
server/src/repository/retrieval-run-repository.ts 100% 100% 100% 100%
server/src/routes/threads/chat.ts 100% 93.1% 100% 100%
server/src/services/data-retention.ts 100% 100% 100% 100%
server/src/services/analytics/llm-usage.ts 83.78% 94.44% 90% 81.81%
server/src/services/knowledge/embedding.ts 96% 83.33% 100% 95.6%
server/src/services/knowledge/retrieval-trace.ts 100% 100% 100% 100%
server/src/services/knowledge/search.ts 93.93% 100% 85.71% 93.54%
Generated in workflow #2107 for commit 1e28fdb by the Vitest Coverage Report Action

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Coverage Report for web

Status Category Percentage Covered / Total
🟢 Lines 97.41% (🎯 96%) 1658 / 1702
🟢 Statements 96.28% (🎯 95%) 1764 / 1832
🟢 Functions 96.08% (🎯 95%) 711 / 740
🟢 Branches 89.93% (🎯 88%) 1117 / 1242
File CoverageNo changed files found.
Generated in workflow #2107 for commit 1e28fdb by the Vitest Coverage Report Action

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Coverage Report for shared

Status Category Percentage Covered / Total
🟢 Lines 99.15% (🎯 98%) 235 / 237
🟢 Statements 80.96% (🎯 80%) 251 / 310
🟢 Functions 100% (🎯 98%) 79 / 79
🟢 Branches 62.87% (🎯 62%) 105 / 167
File CoverageNo changed files found.
Generated in workflow #2107 for commit 1e28fdb by the Vitest Coverage Report Action

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Coverage Report for widget

Status Category Percentage Covered / Total
🟢 Lines 99.62% (🎯 98%) 265 / 266
🟢 Statements 97.94% (🎯 96%) 286 / 292
🟢 Functions 98.11% (🎯 98%) 52 / 53
🟢 Branches 93.28% (🎯 92%) 139 / 149
File CoverageNo changed files found.
Generated in workflow #2107 for commit 1e28fdb by the Vitest Coverage Report Action

retrieval_runs の保持期間削除を repository 経由に揃える

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014kkNoEdkn1kVK4La79XFpc
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