这批是 10-26~27 已在生产二进制(zhiyid-protectrails-20261027)里运行、 但一直留在工作树未提交的改动。收口原因:三件改造要开分支,工作树脏 = 分支基准与回滚点都不存在。 主要内容: - api/routes/contract.go + trace.go:召回响应契约字段(trace_id/status/scope/ continuation/channel)与 /api/v1/trace 回溯端点 - governance/entity_quality.go:实体质量闸 - storage/trace.go + recall.go/recall_write_buffer.go:召回写入缓冲与轨迹 - 测试:contract/recall_contract_fields/recall_lifecycle/stale_lifecycle/ entity_quality/trace 同时修一个 flaky:recall_lifecycle_test 原本硬编码「今天 11:00 已过」, 凌晨跑就会把「已失效」fixture 当成仍有效。改成相对时间(过期=now-1h), 生命周期判据与运行时刻无关。修完 go test ./internal/api/routes ./internal/storage 全绿。
50 lines
2.1 KiB
Go
50 lines
2.1 KiB
Go
// 织忆 MemoryWeave — 召回链路追踪端点(P0-1,2026-10-26)
|
||
//
|
||
// GET /api/v1/trace/<trace_id>
|
||
//
|
||
// 返回该次召回的:各阶段耗时(encode/coarse_search/fts_recall/bm25/rerank_fusion/mmr_select/
|
||
// graph_expand/dedupe)、各通道贡献条数(channels)、每条结果的来源归因(items)、
|
||
// status/partial_reason、scope、cache_hit/truncated。
|
||
//
|
||
// 数据来源:内存环形缓冲(最近 2000 条)+ JSONL 落盘(/var/lib/memoryweave/traces/),
|
||
// 服务重启后仍可按 trace_id 回溯(落盘扫描)。
|
||
package routes
|
||
|
||
import (
|
||
"context"
|
||
"net/http"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/xiaoxue/memoryweave/internal/storage"
|
||
)
|
||
|
||
// ── P0-3 召回硬超时:取消函数类型 + context.WithTimeout 包装 ──────────────
|
||
// core.go Recall() 在 req.DeadlineMS>0 时需要带类型标记的取消函数,以便 defer 取消。
|
||
// 2026-10-26 (t_e98cb1d6):此处补齐使 routes 包可编译(否则 routes 全包不可构建,
|
||
// capRecallResults 的单测无法跑通)。这是缺失的声明,不是功能实现;P0-3 的 trace 完整
|
||
// 接线(tr.Stage 埋点、tr 落盘)仍待 P0-3 本任务补齐。
|
||
type ctxDeadlineFunc context.CancelFunc
|
||
|
||
func contextWithTimeout(ctx context.Context, timeout time.Duration) (context.Context, ctxDeadlineFunc) {
|
||
c, cancel := context.WithTimeout(ctx, timeout)
|
||
return c, ctxDeadlineFunc(cancel)
|
||
}
|
||
|
||
// Trace 查询单条召回链路。路径形如 /api/v1/trace/tr_xxxx。
|
||
func (a *API) Trace(w http.ResponseWriter, r *http.Request) {
|
||
id := strings.TrimSpace(strings.TrimPrefix(r.URL.Path, "/api/v1/trace/"))
|
||
if id == "" {
|
||
respondErrorSchema(w, 400, "trace_id_required", "用法:GET /api/v1/trace/<trace_id>", 0)
|
||
return
|
||
}
|
||
tr, ok := storage.GetTrace(id)
|
||
if !ok {
|
||
// 404 与「trace 过期/被环形缓冲覆盖」同义:调用方据此走「重新召回」而不是当成系统故障。
|
||
respondErrorSchema(w, 404, "trace_not_found",
|
||
"trace 不存在或已被环形缓冲覆盖(内存保留最近 2000 条,落盘按天分文件)", 0)
|
||
return
|
||
}
|
||
respond(w, 200, tr)
|
||
}
|