Compare commits
30
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e21eef6da | ||
|
|
5b9f75cc7f | ||
|
|
ba81121efa | ||
|
|
5565ec8a9b | ||
|
|
95bb930bd6 | ||
|
|
e900e4073f | ||
|
|
66d8e32bc6 | ||
|
|
13ced12ce9 | ||
|
|
227b2979dd | ||
|
|
cb450e9c9d | ||
|
|
28f6951061 | ||
|
|
9b7b969cd3 | ||
|
|
dfc6f580eb | ||
|
|
a39e5c7e23 | ||
|
|
ce38465dbd | ||
|
|
a162e2769e | ||
|
|
13cf7d2088 | ||
|
|
c34178c3c3 | ||
|
|
cbbdd9a708 | ||
|
|
6e752aa904 | ||
|
|
63ceb4a11c | ||
|
|
e9fce11a1b | ||
|
|
6db0ffa7d4 | ||
|
|
0e4fb59e1a | ||
|
|
b551c55011 | ||
|
|
aa8f503ae0 | ||
|
|
82ef904257 | ||
|
|
886c0c20e2 | ||
|
|
31bdf3403b | ||
|
|
2a8fc6494e |
@@ -35,3 +35,6 @@ bin/
|
||||
# 构建产物(worker 曾把 7.9M 二进制留在工作区,2026-09-21)
|
||||
go/zhiyi-cli
|
||||
go/zhiyid
|
||||
|
||||
# 临时工件(cron list dump,随时可 `hermes cron list` 重现,不入库)
|
||||
cron_raw.txt
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
# ─── 变量 ────────────────────────────────────────────
|
||||
GO_CMD := go
|
||||
RUST_CMD := cargo
|
||||
GOFMT_CMD := gofmt
|
||||
GO_DIR := go
|
||||
RUST_DIR := rust
|
||||
BINARY_GO := $(GO_DIR)/cmd/zhiyid/zhiyid
|
||||
@@ -81,6 +82,15 @@ deploy-compose: docker-build
|
||||
test:
|
||||
cd $(GO_DIR) && $(GO_CMD) test ./... -v -count=1
|
||||
|
||||
# 格式校验:gofmt 不合规即报错(gofmt -l 列出文件 → 非空则 exit 1)
|
||||
fmt-check:
|
||||
@cd $(GO_DIR) && $(GOFMT_CMD) -l . | tee /tmp/.gofmt_files > /dev/null; \
|
||||
if [ -s /tmp/.gofmt_files ]; then \
|
||||
echo "❌ gofmt 不合规文件($(GOFMT_CMD) -l . 非空):"; \
|
||||
$(GOFMT_CMD) -l . ; \
|
||||
echo " 修复: cd $(GO_DIR) && $(GOFMT_CMD) -w ."; exit 1; \
|
||||
else echo "✅ gofmt 合规(0 文件)"; fi
|
||||
|
||||
test-go: test
|
||||
|
||||
test-rust:
|
||||
|
||||
@@ -426,7 +426,10 @@ python3 -c "from plugins.memory.zhiyi import HermesZhiYiMemoryProvider; \
|
||||
| POST | `/api/v1/recall` | 语义检索(支持 mode=hybrid\|keyword\|semantic) |
|
||||
| POST | `/api/v1/batch-commit` | 批量提交 |
|
||||
| GET | `/api/v1/memories` | 列出记忆(分页) |
|
||||
| POST | `/api/v1/feedback` | 反馈(useful / not-useful / deprecate) |
|
||||
| POST | `/api/v1/feedback/useful` | 标记记忆有用(GA-P0-1 写 human_useful) |
|
||||
| POST | `/api/v1/feedback/not-useful` | 标记记忆无用(写 human_not_useful_count) |
|
||||
| POST | `/api/v1/feedback/deprecate` | 标记过时 |
|
||||
| POST | `/api/v1/feedback/correct` | 提交修正 |
|
||||
|
||||
### 统计
|
||||
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
# hits+/hits− outcome 信号定义(GA-P0-4 · t_bb10e8bc)
|
||||
|
||||
> 一句话:hits+/hits− 是**独立、语义单一**的「真实 outcome」信号,供 P2 自适应阈值做
|
||||
> 有意义的拟合。它**不是** useful_count / not_useful_count 的别名 —— 那两个列混入了
|
||||
> 去重副作用与「只是被看到」的假信号(见下文「为什么不能直接用旧列」)。
|
||||
|
||||
## 1. 它是什么
|
||||
|
||||
`hit_events` 是 **append-only 事件表**(sqlite 表 / LanceDB 表 / 内存切片三种后端同步),
|
||||
每次**真实 outcome** 追加一行,带 `created_at` 时间戳,**不覆盖式累加**。字段:
|
||||
|
||||
| 字段 | 取值 | 含义 |
|
||||
|------|------|------|
|
||||
| `outcome` | `pos` / `neg` | hits+ / hits− |
|
||||
| `reason` | `human_feedback` / `contradiction` / `replacement` / `downstream_use` | 这条 outcome 由什么产生 |
|
||||
| `memory_id` | 记忆 ID | 这条 outcome 落在哪条记忆上 |
|
||||
| `created_at` | RFC3339(UTC) | 事件发生时刻 —— **按周窗口聚合的依据** |
|
||||
|
||||
P2 自适应阈值按周窗口聚合:`WHERE created_at >= 本周起点`,得到该记忆/该标签近一周的
|
||||
hits+ / hits− 数,再拟合阈值。因为事件带时间戳且 append-only,周窗口天然可算(老窗口
|
||||
自然滑出,不污染当前拟合)。
|
||||
|
||||
## 2. y 判据:什么算 hits+ / 什么算 hits− / 什么不算
|
||||
|
||||
铁律一句话:**只有内容被「消费」或「否定」,才产生 outcome;被搬运、被看到、被降权都不算。**
|
||||
|
||||
### hits+(`outcome="pos"`)—— 被真实使用/认可
|
||||
1. **human_feedback**:用户对召回结果点「有用」(`/feedback/useful`)、或「修正」(`/feedback/correct`)
|
||||
2. **downstream_use**:下游流程**实际引用**了这条记忆的内容(写代码/写文档/决策时真正用上)
|
||||
3. **contradiction(正面)**:后续对话中它被反复印证、未被推翻
|
||||
|
||||
### hits−(`outcome="neg"`)—— 被真实否定/取代
|
||||
1. **human_feedback**:用户对召回结果点「没用」(`/feedback/not-useful`)
|
||||
2. **contradiction**:后续对话中它与更可信的信息**冲突并被淘汰**(`delete_reason=contradiction`)
|
||||
3. **replacement**:后续对话中它被**更新的信息取代**(更新的内容登场,旧的作废)
|
||||
|
||||
### ✗ 明确**不**算(排除项 —— 这些是 P0-1/P0-2 拆出来的假信号)
|
||||
| 信号 | 为什么不算 | 落在哪 |
|
||||
|------|-----------|--------|
|
||||
| dedup 自动降权 | 只是去重副作用,不代表内容质量差 | `dedup_downweight_count`(GA-P0-1) |
|
||||
| 单纯出现在召回结果里 | 「被看到」≠「被用上」,不代表有用 | `recall_seen_count`(GA-P0-2) |
|
||||
| 只是被召回、无任何后续 | 没有消费、没有否定 → 无 outcome | 不产生事件 |
|
||||
|
||||
## 3. 为什么不能直接用旧列(等同不成立的证据)
|
||||
|
||||
方案文档(`已办.md:16623`)曾写 `hits+ = useful_count`、`hits− = not_useful_count`、
|
||||
`V = hits+ + hits−`。该等同**不成立**:
|
||||
|
||||
- `not_useful_count` 混入 **dedup 自动降权** —— 一次 dedup 合并把内容质量**尚可**的记忆
|
||||
降权,却被当成了「这条记忆内容差」的负反馈 → P2 拿它当负标签会系统性误判「合并整理」
|
||||
为「内容质量差」。
|
||||
- `recall_count` 记的是「出现在结果里」而非「被验证使用」—— 一次召回命中就 ++,
|
||||
被召回 500+ 次的记忆永不遗忘(虚高保命),却从未被真正用上 → 拿它当正向信号,
|
||||
阈值会拟合在「被看到」上而不是「被用上」上。
|
||||
|
||||
所以必须新建**只由真实 outcome 写入**的独立信号。本表就是它。
|
||||
|
||||
## 4. 写入点(只由真实 outcome 触发)
|
||||
|
||||
当前已接线的真实 outcome 写入点(`go/internal/api/routes/feedback.go`):
|
||||
|
||||
| 端点 | outcome | reason | 说明 |
|
||||
|------|---------|--------|------|
|
||||
| `/feedback/useful` | pos | human_feedback | 用户点「有用」 |
|
||||
| `/feedback/not-useful` | neg | human_feedback | 用户点「没用」 |
|
||||
|
||||
矛盾/取代/下游引用三类由 P2 阶段接入(本卡只定义语义 + 提供 API,不部署、不强行接
|
||||
对话处理器)。
|
||||
|
||||
## 5. 写入约束(代码强制,防污染)
|
||||
|
||||
- `AppendHitEvent(memoryID, outcome, reason)` 的 `outcome` 必须落在 `pos|neg`,
|
||||
**否则报错不写库**(`hit_event.go:NormalizeHitOutcome`)—— 非法/混合语义值在源头被拦。
|
||||
- `reason` 经 `NormalizeHitReason` 归一到固定枚举(`human_feedback|contradiction|
|
||||
replacement|downstream_use`)。
|
||||
- dedup 降权 / 单纯召回出现**禁止**调用本方法(它们在 P0-1/P0-2 已各有专属字段)。
|
||||
|
||||
## 6. 实现位置
|
||||
|
||||
| 层 | 文件 |
|
||||
|----|------|
|
||||
| Go 语义定义(枚举+归一化,单一真源) | `go/internal/storage/hit_event.go` |
|
||||
| Go 模型 | `go/internal/models/memory.go`(`HitEvent`) |
|
||||
| sqlite 后端 | `go/internal/storage/sqlite.go`(`hit_events` 表 + 3 方法) |
|
||||
| 内存后端 | `go/internal/storage/memvector.go`(`hitEvents` 切片 + 3 方法) |
|
||||
| Rust-LanceDB 后端 | `rust/src/lancedb_ops.rs`(`hit_events` 表 + 3 方法)· `rust/src/main.rs`(IPC) |
|
||||
| 真实 outcome 接线 | `go/internal/api/routes/feedback.go` |
|
||||
|
||||
**禁部署**:本卡只落代码 + 测试 + 文档,不更新运行中的二进制、不碰 live 库。
|
||||
+9
-9
@@ -71,7 +71,7 @@ type RegisterResponse struct {
|
||||
|
||||
// WSEvent WebSocket 推送事件
|
||||
type WSEvent struct {
|
||||
Type string `json:"type"` // prefetch.push / gap.detected / memory.updated / conflict.detected / ...
|
||||
Type string `json:"type"` // prefetch.push / gap.detected / memory.updated / conflict.detected / ...
|
||||
Payload interface{} `json:"payload"`
|
||||
}
|
||||
|
||||
@@ -88,14 +88,14 @@ type StatsResponse struct {
|
||||
|
||||
// ZhiYiClient 织忆 Go SDK(Hermes/OpenClaw/Cron Jobs 共用)
|
||||
type ZhiYiClient struct {
|
||||
baseURL string
|
||||
apiKey string
|
||||
agentID string
|
||||
http *http.Client
|
||||
wsConn *websocket.Conn
|
||||
wsMu sync.Mutex
|
||||
eventCh chan WSEvent
|
||||
done chan struct{}
|
||||
baseURL string
|
||||
apiKey string
|
||||
agentID string
|
||||
http *http.Client
|
||||
wsConn *websocket.Conn
|
||||
wsMu sync.Mutex
|
||||
eventCh chan WSEvent
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
// NewZhiYiClient 创建织忆客户端
|
||||
|
||||
@@ -0,0 +1,205 @@
|
||||
// 织忆 MemoryWeave — is_deleted 删除原因回填工具(GA-P0-3 t_f785d7f2)
|
||||
//
|
||||
// 背景:is_deleted=true 实测 11228 条,但删除原因不可分辨——「坏记忆被删」(contradiction/
|
||||
// manual/retention) 和「合并/整理被删」(merge_dup) 混在同一个布尔里。P2 要拿「被删」
|
||||
// 当负标签,必须先把原因分列。
|
||||
//
|
||||
// 本工具:
|
||||
// 1. 读 LanceDB 全部 is_deleted=true 行(走 Rust IPC lancedb_scan_deleted)。
|
||||
// 2. 以 tombstones 表为据尽力判定删除原因(sqlite tombstones.reason 列)。
|
||||
// tombstones 判不出的写 unknown。
|
||||
// 3. 对每行写 delete_reason(跳过往已回填的、行上 delete_reason 非空的行)。
|
||||
// 4. 输出 delete_reason 直方图。
|
||||
//
|
||||
// 用法(**dry-run 默认,apply 需显式 --apply,且须经小怡终审后才允许跑**):
|
||||
//
|
||||
// go run ./cmd/backfill_deletereason --socket /tmp/zhiyi-ipc.sock --tombstones <sqlite.db>
|
||||
// go run ./cmd/backfill_deletereason --socket /tmp/zhiyi-ipc.sock --tombstones <sqlite.db> --apply
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ipcReq struct {
|
||||
Type string `json:"type"`
|
||||
Table string `json:"table,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Fields string `json:"fields,omitempty"`
|
||||
}
|
||||
|
||||
type ipcResp struct {
|
||||
Status string `json:"status"`
|
||||
ReportJSON string `json:"report_json"`
|
||||
ErrorDetail string `json:"error_detail"`
|
||||
}
|
||||
|
||||
func rpc(sock string, req ipcReq) (*ipcResp, error) {
|
||||
conn, err := net.DialTimeout("unix", sock, 5e9)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ipc dial %s: %w", sock, err)
|
||||
}
|
||||
defer conn.Close()
|
||||
body, _ := json.Marshal(req)
|
||||
lenBuf := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(lenBuf, uint32(len(body)))
|
||||
conn.Write(lenBuf)
|
||||
conn.Write(body)
|
||||
io.ReadFull(conn, lenBuf)
|
||||
msgLen := binary.BigEndian.Uint32(lenBuf)
|
||||
respBuf := make([]byte, msgLen)
|
||||
io.ReadFull(conn, respBuf)
|
||||
var resp ipcResp
|
||||
if err := json.Unmarshal(respBuf, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.Status != "ok" {
|
||||
return nil, fmt.Errorf("ipc: %s", resp.ErrorDetail)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
// 枚举(与 internal/storage/delete_reason.go 保持一致)
|
||||
const (
|
||||
DRMergeDup = "merge_dup"
|
||||
DRContradiction = "contradiction"
|
||||
DRManual = "manual"
|
||||
DRRetention = "retention"
|
||||
DRUnknown = "unknown"
|
||||
)
|
||||
|
||||
// Normalize 把 free-form reason 归一到枚举(与 storage.NormalizeDeleteReason 同规则)。
|
||||
func Normalize(reason string) string {
|
||||
r := strings.ToLower(strings.TrimSpace(reason))
|
||||
if r == "" {
|
||||
return DRUnknown
|
||||
}
|
||||
switch {
|
||||
case strings.HasPrefix(r, "batch:"):
|
||||
return DRUnknown
|
||||
case strings.Contains(r, "manual"), strings.Contains(r, "用户"),
|
||||
strings.Contains(r, "user"), strings.Contains(r, "feedback"),
|
||||
strings.Contains(r, "human"), strings.Contains(r, "人工"):
|
||||
return DRManual
|
||||
case strings.Contains(r, "dedup"), strings.Contains(r, "wording"),
|
||||
strings.Contains(r, "duplicate"), strings.Contains(r, "merged"),
|
||||
strings.Contains(r, "merge"):
|
||||
return DRMergeDup
|
||||
case strings.Contains(r, "conflict"), strings.Contains(r, "contradict"),
|
||||
strings.Contains(r, "consolidate"):
|
||||
return DRContradiction
|
||||
case strings.Contains(r, "auto_forget"), strings.Contains(r, "ephemeral"),
|
||||
strings.Contains(r, "fragment"), strings.Contains(r, "expire"),
|
||||
strings.Contains(r, "retention"), strings.Contains(r, "过期"),
|
||||
strings.Contains(r, "自动"):
|
||||
return DRRetention
|
||||
default:
|
||||
return DRUnknown
|
||||
}
|
||||
}
|
||||
|
||||
type deletedRow struct {
|
||||
ID string `json:"id"`
|
||||
DeleteReason string `json:"delete_reason"`
|
||||
}
|
||||
|
||||
// loadTombstoneReasons 从 sqlite tombstones 表读取 original_id → reason 映射。
|
||||
// 文件不存在/表不存在 ⇒ 空映射(全部落 unknown)。
|
||||
func loadTombstoneReasons(path string) (map[string]string, error) {
|
||||
out := map[string]string{}
|
||||
if path == "" {
|
||||
return out, nil
|
||||
}
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
return out, nil // 无 tombstone 库 ⇒ 尽力判定为 0,全 unknown
|
||||
}
|
||||
// 复用系统 sqlite3 CLI 最简读法(避免 CGO 依赖);库通常只在 SQLite 后端写入。
|
||||
// 生产 LanceDB 后端 tombstones 表是空(软删直接写 memories.is_deleted),
|
||||
// 故此处判不出是常态 —— 符合「判不出的写 unknown」。
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
sock := flag.String("socket", "/tmp/zhiyi-ipc.sock", "rust sidecar unix socket")
|
||||
tombstones := flag.String("tombstones", "", "可选:sqlite tombstones 库路径(尽力判定用)")
|
||||
apply := flag.Bool("apply", false, "真正写库(默认 false = dry-run;未经终审禁止 true)")
|
||||
flag.Parse()
|
||||
|
||||
resp, err := rpc(*sock, ipcReq{Type: "lancedb_scan_deleted"})
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "scan_deleted 失败:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
var rows []deletedRow
|
||||
if err := json.Unmarshal([]byte(resp.ReportJSON), &rows); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "unmarshal 失败:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
tombReasons, _ := loadTombstoneReasons(*tombstones)
|
||||
N := len(rows)
|
||||
|
||||
// 分类:存量已回填(非空 reason)跳过;未回填的按 tombstone 尽力判定,判不出 unknown。
|
||||
hist := map[string]int{}
|
||||
skipFilled := 0
|
||||
toWrite := map[string]string{} // id -> reason
|
||||
for _, r := range rows {
|
||||
if r.DeleteReason != "" {
|
||||
skipFilled++ // 已回填,跳过
|
||||
continue
|
||||
}
|
||||
reason := DRUnknown
|
||||
if tr, ok := tombReasons[r.ID]; ok && tr != "" {
|
||||
reason = Normalize(tr)
|
||||
}
|
||||
toWrite[r.ID] = reason
|
||||
hist[reason]++
|
||||
}
|
||||
|
||||
fmt.Printf("已删除行总数 N = %d\n", N)
|
||||
fmt.Printf(" 已有 delete_reason(跳过) = %d\n", skipFilled)
|
||||
fmt.Printf(" 待回填 = %d\n", len(toWrite))
|
||||
fmt.Println("── delete_reason 直方图(value→行数,仅待回填)──")
|
||||
order := []string{DRMergeDup, DRContradiction, DRManual, DRRetention, DRUnknown}
|
||||
keys := make([]string, 0, len(hist))
|
||||
for k := range hist {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, k := range order {
|
||||
fmt.Printf("[%s] %d\n", k, hist[k])
|
||||
}
|
||||
|
||||
if *apply {
|
||||
if len(toWrite) == 0 {
|
||||
fmt.Println("apply: 无可回填行(全部已回填或已删除行为空)。")
|
||||
} else {
|
||||
fmt.Printf("apply: 将逐条 Update %d 行 delete_reason(id IN ... 分批)。—— 未经终审禁止。\n", len(toWrite))
|
||||
// 实际写入走 lancedb_update(每 id 一条,带 delete_reason + is_deleted 保持 true)。
|
||||
// 实现:对每 id 发 lancedb_update fields=[{column:delete_reason,value:'<reason>'}]
|
||||
// 注意:**只写 delete_reason,不碰其它字段**(禁改已删除记录其它字段)。
|
||||
var wrote int64
|
||||
for id, reason := range toWrite {
|
||||
fields := fmt.Sprintf(`[{"column":"delete_reason","value":"'%s'"}]`, reason)
|
||||
r, err := rpc(*sock, ipcReq{Type: "lancedb_update", Table: "memories", ID: id, Fields: fields})
|
||||
_ = r
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "update %s 失败: %v\n", id, err)
|
||||
continue
|
||||
}
|
||||
wrote++
|
||||
}
|
||||
fmt.Printf("apply: 已写 %d 行 delete_reason。\n", wrote)
|
||||
}
|
||||
} else {
|
||||
fmt.Println("(dry-run) 未写库。输出须先经小怡终审,批准后才可 --apply。")
|
||||
}
|
||||
}
|
||||
@@ -93,7 +93,7 @@ func main() {
|
||||
if err != nil || t.Before(epochThreshold) || t.Year() < 2024 {
|
||||
// 用确定性派生时间(避免所有epoch-0都用同一时间)
|
||||
// 基于 ID 哈希分配 2024-2026 之间的不同日期
|
||||
offsetDays := int(len(m.ID)%(365*2)) // 0-729天
|
||||
offsetDays := int(len(m.ID) % (365 * 2)) // 0-729天
|
||||
newTime := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC).AddDate(0, 0, offsetDays)
|
||||
ts := newTime.Format(time.RFC3339)
|
||||
if err := ipcUpdate(m.ID, "created_at", ts); err != nil {
|
||||
@@ -107,4 +107,4 @@ func main() {
|
||||
}
|
||||
|
||||
log.Printf("\n完成: 修复 %d/%d 条记忆时间戳\n", fixed, len(memories))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
// 织忆 MemoryWeave — not_useful_count 拆分迁移工具(GA-P0-1 t_718d8707)
|
||||
//
|
||||
// 背景:not_useful_count 被两种语义混写——用户人工点「没用」(feedback.go
|
||||
// MarkNotUseful)与 dedup/合并流程自动降权(core.go Dedup demote)都写同一个字段。
|
||||
// 本工具把存量 not_useful_count 按来源拆分到 human_not_useful_count /
|
||||
// dedup_downweight_count。
|
||||
//
|
||||
// 分类依据(可判 M 条):本库**无任何 per-row 来源 provenance**——feedback 与
|
||||
// dedup 都不在行上留下可辨识标记(source 只是「哪条管道进来」,非「人点还是 dedup」)。
|
||||
// 故对存量行无法按来源判定:M=0,全部归 unknown(K=N)。apply 在 M=0 时是 no-op,
|
||||
// 不会改动任何存量数据(保守安全)。
|
||||
//
|
||||
// 用法(**dry-run 默认,apply 需显式 --apply,且须经小怡终审后才允许跑**):
|
||||
//
|
||||
// go run ./cmd/split_notuseful --socket /tmp/zhiyi-ipc.sock # dry-run
|
||||
// go run ./cmd/split_notuseful --socket /tmp/zhiyi-ipc.sock --apply # 真写(禁未审批)
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type ipcReq struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type ipcResp struct {
|
||||
Status string `json:"status"`
|
||||
ReportJSON string `json:"report_json"`
|
||||
ErrorDetail string `json:"error_detail"`
|
||||
}
|
||||
|
||||
func rpc(sock string, req ipcReq) (*ipcResp, error) {
|
||||
conn, err := net.DialTimeout("unix", sock, 5e9)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ipc dial %s: %w", sock, err)
|
||||
}
|
||||
defer conn.Close()
|
||||
body, _ := json.Marshal(req)
|
||||
lenBuf := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(lenBuf, uint32(len(body)))
|
||||
conn.Write(lenBuf)
|
||||
conn.Write(body)
|
||||
io.ReadFull(conn, lenBuf)
|
||||
msgLen := binary.BigEndian.Uint32(lenBuf)
|
||||
respBuf := make([]byte, msgLen)
|
||||
io.ReadFull(conn, respBuf)
|
||||
var resp ipcResp
|
||||
if err := json.Unmarshal(respBuf, &resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.Status != "ok" {
|
||||
return nil, fmt.Errorf("ipc: %s", resp.ErrorDetail)
|
||||
}
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
type row struct {
|
||||
ID string `json:"id"`
|
||||
Source string `json:"source"`
|
||||
Category string `json:"category"`
|
||||
UsefulCount int `json:"useful_count"`
|
||||
NotUsefulCount int `json:"not_useful_count"`
|
||||
HumanNotUsefulCount int `json:"human_not_useful_count"`
|
||||
DedupDownweightCount int `json:"dedup_downweight_count"`
|
||||
}
|
||||
|
||||
// classifyBySource 按来源判定归属。返回 "human" / "dedup" / "unknown"。
|
||||
// 当前 schema 无 per-row 来源 provenance ⇒ 恒 unknown(M=0)。
|
||||
// 将来若某来源可明确标识「人工反馈」或「dedup 降权」,在此追加映射即可。
|
||||
func classifyBySource(_ row) string {
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func main() {
|
||||
sock := flag.String("socket", "/tmp/zhiyi-ipc.sock", "rust sidecar unix socket")
|
||||
apply := flag.Bool("apply", false, "真正写库(默认 false = dry-run;未经终审禁止 true)")
|
||||
flag.Parse()
|
||||
|
||||
resp, err := rpc(*sock, ipcReq{Type: "lancedb_scan_all"})
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "scan_all 失败:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
var rows []row
|
||||
if err := json.Unmarshal([]byte(resp.ReportJSON), &rows); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "unmarshal 失败:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var nu []row
|
||||
for _, r := range rows {
|
||||
if r.NotUsefulCount > 0 {
|
||||
nu = append(nu, r)
|
||||
}
|
||||
}
|
||||
N := len(nu)
|
||||
|
||||
// 分类
|
||||
humanIDs := map[string]int{} // id -> count
|
||||
dedupIDs := map[string]int{} // id -> count
|
||||
unknownIDs := map[string]int{}
|
||||
for _, r := range nu {
|
||||
switch classifyBySource(r) {
|
||||
case "human":
|
||||
humanIDs[r.ID] = r.NotUsefulCount
|
||||
case "dedup":
|
||||
dedupIDs[r.ID] = r.NotUsefulCount
|
||||
default:
|
||||
unknownIDs[r.ID] = r.NotUsefulCount
|
||||
}
|
||||
}
|
||||
M := len(humanIDs) + len(dedupIDs)
|
||||
K := len(unknownIDs)
|
||||
|
||||
// 3 字段直方图(当前存量值分布)
|
||||
hist := map[string]map[int]int{}
|
||||
hist["not_useful_count"] = map[int]int{}
|
||||
hist["human_not_useful_count"] = map[int]int{}
|
||||
hist["dedup_downweight_count"] = map[int]int{}
|
||||
for _, r := range rows {
|
||||
hist["not_useful_count"][r.NotUsefulCount]++
|
||||
hist["human_not_useful_count"][r.HumanNotUsefulCount]++
|
||||
hist["dedup_downweight_count"][r.DedupDownweightCount]++
|
||||
}
|
||||
|
||||
fmt.Printf("总行数(存活): %d\n", len(rows))
|
||||
fmt.Printf("not_useful_count>0 待改行数 N = %d\n", N)
|
||||
fmt.Printf(" 其中可按来源判定 M = %d (human=%d dedup=%d)\n", M, len(humanIDs), len(dedupIDs))
|
||||
fmt.Printf(" 无法判定归 unknown K = %d\n", K)
|
||||
fmt.Println("── 3 字段直方图(value→行数,仅列当前非零分布)──")
|
||||
order := []string{"not_useful_count", "human_not_useful_count", "dedup_downweight_count"}
|
||||
for _, f := range order {
|
||||
m := hist[f]
|
||||
keys := make([]int, 0, len(m))
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Ints(keys)
|
||||
fmt.Printf("[%s] ", f)
|
||||
if len(keys) == 1 && keys[0] == 0 {
|
||||
fmt.Println("全 0(0 行非零)")
|
||||
continue
|
||||
}
|
||||
parts := []string{}
|
||||
for _, k := range keys {
|
||||
if k == 0 {
|
||||
continue
|
||||
}
|
||||
parts = append(parts, fmt.Sprintf("%d×%d", k, m[k]))
|
||||
}
|
||||
fmt.Println(parts)
|
||||
}
|
||||
|
||||
if *apply {
|
||||
if M == 0 {
|
||||
fmt.Println("apply: 无可判定行(M=0),无需迁移,存量 not_useful_count 保持原样。")
|
||||
} else {
|
||||
fmt.Printf("apply: 将拆分 %d 行(human=%d dedup=%d)—— 需逐条 Update。\n", M, len(humanIDs), len(dedupIDs))
|
||||
}
|
||||
} else {
|
||||
fmt.Println("(dry-run) 未写库。输出须先经小怡终审,批准后才可 --apply。")
|
||||
}
|
||||
}
|
||||
+36
-30
@@ -28,6 +28,7 @@ const (
|
||||
// 3. /var/lib/memoryweave/.api-key 文件(**多行格式**:注释与 `legacy:` 行跳过,
|
||||
// 取第 1 个非注释非 legacy 行 = 主密钥)
|
||||
// 4. 均无 → 返回 error,调用方退出
|
||||
//
|
||||
// 🔴 2026-09-21 修:原实现 `TrimSpace(整份文件)` 在多行 key 文件下会把
|
||||
// 「注释头 + 主密钥 + legacy 行」整块当 key 发给服务端 → 必然 401。
|
||||
func resolveAPIKey(flagKey string) (string, error) {
|
||||
@@ -52,9 +53,9 @@ func resolveAPIKey(flagKey string) (string, error) {
|
||||
// ─── 全局参数 ──────────────────────────────────────────────
|
||||
|
||||
var (
|
||||
apiURL = flag.String("url", getEnv("ZHIYI_API_URL", apiURLDefault), "织忆 API 地址")
|
||||
apiKey = flag.String("key", getEnv("ZHIYI_API_KEY", apiKeyDefault), "API Key")
|
||||
ns = flag.String("n", getEnv("ZHIYI_NAMESPACE", namespaceDefault), "命名空间")
|
||||
apiURL = flag.String("url", getEnv("ZHIYI_API_URL", apiURLDefault), "织忆 API 地址")
|
||||
apiKey = flag.String("key", getEnv("ZHIYI_API_KEY", apiKeyDefault), "API Key")
|
||||
ns = flag.String("n", getEnv("ZHIYI_NAMESPACE", namespaceDefault), "命名空间")
|
||||
)
|
||||
|
||||
// ─── 入口 ─────────────────────────────────────────────────
|
||||
@@ -81,11 +82,16 @@ func main() {
|
||||
|
||||
var runErr error
|
||||
switch cmd {
|
||||
case "tree": runErr = runTree(args, apiKeyVal)
|
||||
case "graph": runErr = runGraph(args, apiKeyVal)
|
||||
case "recall": runErr = runRecall(args, apiKeyVal)
|
||||
case "stats": runErr = runStats(apiKeyVal)
|
||||
case "entity": runErr = runEntity(args, apiKeyVal)
|
||||
case "tree":
|
||||
runErr = runTree(args, apiKeyVal)
|
||||
case "graph":
|
||||
runErr = runGraph(args, apiKeyVal)
|
||||
case "recall":
|
||||
runErr = runRecall(args, apiKeyVal)
|
||||
case "stats":
|
||||
runErr = runStats(apiKeyVal)
|
||||
case "entity":
|
||||
runErr = runEntity(args, apiKeyVal)
|
||||
case "help", "--help", "-h":
|
||||
flag.Usage()
|
||||
os.Exit(0)
|
||||
@@ -188,11 +194,11 @@ func runTree(args []string, apiKey string) error {
|
||||
// 从图谱 export 获取节点(带 category),按 category 分组
|
||||
var r struct {
|
||||
Nodes []struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Category string `json:"category,omitempty"`
|
||||
Weight float64 `json:"weight,omitempty"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Category string `json:"category,omitempty"`
|
||||
Weight float64 `json:"weight,omitempty"`
|
||||
} `json:"nodes"`
|
||||
Edges []struct {
|
||||
Source string `json:"source"`
|
||||
@@ -235,12 +241,12 @@ func runTree(args []string, apiKey string) error {
|
||||
fmt.Println(strings.Repeat("─", 50))
|
||||
|
||||
catIcons := map[string]string{
|
||||
"人物": "👤",
|
||||
"项目": "📦",
|
||||
"事件": "📅",
|
||||
"概念": "💡",
|
||||
"位置": "📍",
|
||||
"组织": "🏢",
|
||||
"人物": "👤",
|
||||
"项目": "📦",
|
||||
"事件": "📅",
|
||||
"概念": "💡",
|
||||
"位置": "📍",
|
||||
"组织": "🏢",
|
||||
"distilled": "🔄",
|
||||
}
|
||||
for _, cat := range cats {
|
||||
@@ -289,14 +295,14 @@ func runGraph(args []string, apiKey string) error {
|
||||
}
|
||||
|
||||
type navigateResp struct {
|
||||
Entity string `json:"entity"`
|
||||
Count int `json:"count"`
|
||||
Entity string `json:"entity"`
|
||||
Count int `json:"count"`
|
||||
Paths []struct {
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
Relation string `json:"relation"`
|
||||
Hop int `json:"hop"`
|
||||
Weight float64 `json:"weight"`
|
||||
Hop int `json:"hop"`
|
||||
Weight float64 `json:"weight"`
|
||||
} `json:"paths"`
|
||||
}
|
||||
var resp navigateResp
|
||||
@@ -423,10 +429,10 @@ func runStats(apiKey string) error {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
var distStatus struct {
|
||||
QueueLen int `json:"queue_len"`
|
||||
DailyUsed int `json:"daily_used"`
|
||||
DailyLimit int `json:"daily_limit"`
|
||||
BatchSize int `json:"batch_size"`
|
||||
QueueLen int `json:"queue_len"`
|
||||
DailyUsed int `json:"daily_used"`
|
||||
DailyLimit int `json:"daily_limit"`
|
||||
BatchSize int `json:"batch_size"`
|
||||
}
|
||||
|
||||
apiGet("/api/v1/stats", &stats, apiKey)
|
||||
@@ -591,4 +597,4 @@ func box(style, s string) string {
|
||||
return "◉ " + s
|
||||
}
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,19 +16,19 @@ import (
|
||||
// ─── Per-Agent 令牌桶 ────────────────────────────────────────
|
||||
|
||||
type tokenBucket struct {
|
||||
tokens float64
|
||||
tokens float64
|
||||
lastRefill time.Time
|
||||
rate float64 // tokens/sec
|
||||
burst float64
|
||||
mu sync.Mutex
|
||||
rate float64 // tokens/sec
|
||||
burst float64
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func newTokenBucket(rate, burst float64) *tokenBucket {
|
||||
return &tokenBucket{
|
||||
tokens: burst,
|
||||
tokens: burst,
|
||||
lastRefill: time.Now(),
|
||||
rate: rate,
|
||||
burst: burst,
|
||||
rate: rate,
|
||||
burst: burst,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,10 +58,10 @@ type AgentRateConfig struct {
|
||||
}
|
||||
|
||||
var agentRates = map[string]AgentRateConfig{
|
||||
"hermes": {RecallQPS: 10, CommitQPS: 2, Burst: 20},
|
||||
"hermes-a06":{RecallQPS: 10, CommitQPS: 2, Burst: 20},
|
||||
"openclaw": {RecallQPS: 10, CommitQPS: 2, Burst: 20},
|
||||
"cron-job": {RecallQPS: 5, CommitQPS: 1, Burst: 10},
|
||||
"hermes": {RecallQPS: 10, CommitQPS: 2, Burst: 20},
|
||||
"hermes-a06": {RecallQPS: 10, CommitQPS: 2, Burst: 20},
|
||||
"openclaw": {RecallQPS: 10, CommitQPS: 2, Burst: 20},
|
||||
"cron-job": {RecallQPS: 5, CommitQPS: 1, Burst: 10},
|
||||
}
|
||||
|
||||
var defaultRate = AgentRateConfig{RecallQPS: 5, CommitQPS: 1, Burst: 10}
|
||||
@@ -69,14 +69,14 @@ var defaultRate = AgentRateConfig{RecallQPS: 5, CommitQPS: 1, Burst: 10}
|
||||
// ─── RateLimiter ─────────────────────────────────────────────
|
||||
|
||||
type RateLimiter struct {
|
||||
mu sync.Mutex
|
||||
buckets map[string]*tokenBucket // agentID → bucket
|
||||
maxPerMinute int // 全局兜底
|
||||
mu sync.Mutex
|
||||
buckets map[string]*tokenBucket // agentID → bucket
|
||||
maxPerMinute int // 全局兜底
|
||||
}
|
||||
|
||||
func NewRateLimiter(maxPerMinute int) *RateLimiter {
|
||||
return &RateLimiter{
|
||||
buckets: make(map[string]*tokenBucket),
|
||||
buckets: make(map[string]*tokenBucket),
|
||||
maxPerMinute: maxPerMinute,
|
||||
}
|
||||
}
|
||||
@@ -125,10 +125,14 @@ func init() {
|
||||
|
||||
// t_4fab32c1 AC2: 密钥强制(无硬编码回落)。
|
||||
// 🔑 2026-09-21 过渡期补丁(牧尘当晚批准上线):密钥文件为**多行**格式 ——
|
||||
// 第 1 个非注释行 = 主密钥;`legacy:` 前缀行 = 过渡期兼容密钥(放行但打 DEPRECATED 日志)。
|
||||
//
|
||||
// 第 1 个非注释行 = 主密钥;`legacy:` 前缀行 = 过渡期兼容密钥(放行但打 DEPRECATED 日志)。
|
||||
//
|
||||
// 为什么需要 legacy:hermes 记忆插件在 **import 时**读 `ZHIYI_API_KEY`,
|
||||
// 正在运行的网关不重启不会重读;另有 8 个本地脚本持旧密钥。
|
||||
// 若直接轮换值 → 瞬间打断记忆读写(recall/commit 全 401)。
|
||||
//
|
||||
// 正在运行的网关不重启不会重读;另有 8 个本地脚本持旧密钥。
|
||||
// 若直接轮换值 → 瞬间打断记忆读写(recall/commit 全 401)。
|
||||
//
|
||||
// 收口条件:客户端迁移完(网关重启后)删除 key 文件里的 legacy 行。
|
||||
const keyFile = "/var/lib/memoryweave/.api-key"
|
||||
|
||||
|
||||
@@ -24,4 +24,4 @@ func CORS() func(http.Handler) http.Handler {
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,9 @@ func (aa *AdminAPI) Forget(w http.ResponseWriter, r *http.Request) {
|
||||
forgotten := 0
|
||||
for _, mem := range results {
|
||||
lastAccess := parseTimeStr(mem["last_recalled_at"])
|
||||
recallCnt := intVal(mem["recall_count"])
|
||||
// GA-P0-2 (t_c6d41ac6):衰减/遗忘只认 recall_verified_count(正反馈才是 V 值)。
|
||||
// recall_count 已冻结(历史虚高保命来源),seen 只用于观测,不参与衰减。
|
||||
recallCnt := intVal(mem["recall_verified_count"])
|
||||
tier := strVal(mem["tier"])
|
||||
content := strVal(mem["content"])
|
||||
|
||||
@@ -121,7 +123,7 @@ func (aa *AdminAPI) Forget(w http.ResponseWriter, r *http.Request) {
|
||||
// POST /api/v1/admin/purge-invalid
|
||||
// P1-1 2026-10-08: 一次性清理 id 为空的垃圾行(DB 存在 id/content/source 全空的 2 行,
|
||||
// 命中 <30 字碎片快速道 → SoftDelete("") 被 Rust 拒绝 → 每轮刷错误日志 + 遗忘计数虚高)。
|
||||
// 这些行无 id 无法按 id 软删,只能按 id='' 条件一次性软删(is_deleted=true)。
|
||||
// 这些行无 id 无法按 id 软删,只能按 id=” 条件一次性软删(is_deleted=true)。
|
||||
// ⚠️ 由验收/决策者按需调用(本实现不自动触发),本端点只提供能力 + 返回受影响行数。
|
||||
func (aa *AdminAPI) PurgeInvalid(w http.ResponseWriter, r *http.Request) {
|
||||
n, err := aa.LanceDB.PurgeInvalid()
|
||||
|
||||
@@ -19,9 +19,9 @@ import (
|
||||
// forgetMockLance 为 Forget 计数测试提供的 LanceDB 桩:
|
||||
// GetCandidatesForForgetting 可配置候选列表;SoftDelete 可配置失败/成功。
|
||||
type forgetMockLance struct {
|
||||
candidates []map[string]interface{}
|
||||
candidates []map[string]interface{}
|
||||
softDeleteErr error
|
||||
deletedIDs []string
|
||||
deletedIDs []string
|
||||
}
|
||||
|
||||
func (m *forgetMockLance) InsertEpisode(agentID, namespace, content, category string) (string, error) {
|
||||
@@ -53,19 +53,26 @@ func (m *forgetMockLance) GetCandidatesForForgetting() ([]map[string]interface{}
|
||||
func (m *forgetMockLance) GetSkillCandidates(minRecalls, limit int) ([]models.MemoryRecord, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *forgetMockLance) Backup(path string) error { return nil }
|
||||
func (m *forgetMockLance) Backup(path string) error { return nil }
|
||||
func (m *forgetMockLance) GetAuditLog(limit int) ([]map[string]interface{}, error) { return nil, nil }
|
||||
func (m *forgetMockLance) IncrementUseful(id string) error { return nil }
|
||||
func (m *forgetMockLance) IncrementNotUseful(id string) error { return nil }
|
||||
func (m *forgetMockLance) IncrementUseful(id string) error { return nil }
|
||||
func (m *forgetMockLance) IncrementNotUseful(id string) error { return nil }
|
||||
|
||||
// GA-P0-1 (t_718d8707):拆分字段
|
||||
func (m *forgetMockLance) IncrementHumanNotUseful(id string) error { return nil }
|
||||
func (m *forgetMockLance) AppendHitEvent(memoryID, outcome, reason string) error { return nil }
|
||||
func (m *forgetMockLance) CountHitEvents(memoryID, outcome string, since time.Time) (int, error) { return 0, nil }
|
||||
func (m *forgetMockLance) GetHitEvents(memoryID string, limit int) ([]models.HitEvent, error) { return nil, nil }
|
||||
func (m *forgetMockLance) IncrementDedupDownweight(id string) error { return nil }
|
||||
func (m *forgetMockLance) UpdateMemoryContent(id, newContent, source string) error { return nil }
|
||||
func (m *forgetMockLance) InsertMemory(mem models.MemoryRecord) error { return nil }
|
||||
func (m *forgetMockLance) InsertMemory(mem models.MemoryRecord) error { return nil }
|
||||
func (m *forgetMockLance) Search(table string, vector []float32, topK int, namespaceFilter string) ([]models.MemoryRecord, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *forgetMockLance) Scan(limit int, namespaceFilter string) ([]models.MemoryRecord, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (m *forgetMockLance) Insert(table string, record any) error { return nil }
|
||||
func (m *forgetMockLance) Insert(table string, record any) error { return nil }
|
||||
func (m *forgetMockLance) Update(table, id string, fields map[string]any) error {
|
||||
return nil
|
||||
}
|
||||
@@ -111,7 +118,7 @@ func callForget(t *testing.T, db *forgetMockLance) forgetResp {
|
||||
// 修前:失败照样 forgotten++ ⇒ 遗忘计数虚高(每轮刷日志 + 计数失真)。
|
||||
func TestForget_SoftDeleteFailure_DoesNotIncrement(t *testing.T) {
|
||||
db := &forgetMockLance{
|
||||
candidates: []map[string]interface{}{fragmentCandidate("mem_fail_1")},
|
||||
candidates: []map[string]interface{}{fragmentCandidate("mem_fail_1")},
|
||||
softDeleteErr: fmt.Errorf("ipc: simulated soft delete failure"),
|
||||
}
|
||||
resp := callForget(t, db)
|
||||
@@ -151,4 +158,4 @@ func TestForget_EmptyIDSkipped(t *testing.T) {
|
||||
t.Fatalf("空 id 候选不得调用 SoftDelete,实得 %v", db.deletedIDs)
|
||||
}
|
||||
t.Logf("✓ 空 id 候选跳过:forgotten=%d, SoftDelete 调用 0 次", resp.Forgotten)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,14 +35,14 @@ func AutoDistillTrigger(episodeID string, content string, category string, names
|
||||
// 2. 使用蒸馏引擎
|
||||
if DistillEngineRef != nil {
|
||||
DistillEngineRef.Enqueue(distill.DistillInput{
|
||||
EpisodeID: episodeID,
|
||||
Content: content,
|
||||
Category: distill.Category(category),
|
||||
Namespace: namespace,
|
||||
AgentID: agentID,
|
||||
Source: meta.Source,
|
||||
EmbeddingModel: meta.EmbeddingModel,
|
||||
ScriptVersion: meta.ScriptVersion,
|
||||
EpisodeID: episodeID,
|
||||
Content: content,
|
||||
Category: distill.Category(category),
|
||||
Namespace: namespace,
|
||||
AgentID: agentID,
|
||||
Source: meta.Source,
|
||||
EmbeddingModel: meta.EmbeddingModel,
|
||||
ScriptVersion: meta.ScriptVersion,
|
||||
Confidence: meta.Confidence,
|
||||
ConfidenceSource: meta.ConfidenceSource,
|
||||
})
|
||||
@@ -54,6 +54,7 @@ func AutoDistillTrigger(episodeID string, content string, category string, names
|
||||
Facts: facts,
|
||||
Entities: extractEntities(content),
|
||||
Namespace: namespace,
|
||||
Source: meta.Source, // 2026-10-12 t_67d0279a 方案A:降级路径同样透传来源分档
|
||||
})
|
||||
}
|
||||
|
||||
@@ -107,8 +108,8 @@ func currentUnixMS() int64 {
|
||||
// ─── 全局引用(server.go 需要访问)─────────────────────
|
||||
|
||||
var (
|
||||
graphUpdater *governance.AutoGraphUpdater
|
||||
conflictScanner = governance.NewConflictDetector()
|
||||
graphUpdater *governance.AutoGraphUpdater
|
||||
conflictScanner = governance.NewConflictDetector()
|
||||
)
|
||||
|
||||
func SetGraphUpdater(gu *governance.AutoGraphUpdater) {
|
||||
@@ -116,6 +117,8 @@ func SetGraphUpdater(gu *governance.AutoGraphUpdater) {
|
||||
}
|
||||
|
||||
func minLen2(a, b int) int {
|
||||
if a < b { return a }
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@
|
||||
// Hermes / OpenClaw 的客户端库,零外部依赖
|
||||
//
|
||||
// 使用:
|
||||
// client := NewClient("http://localhost:7821", "mw-your-api-key")
|
||||
// mid, err := client.Commit("hermes", "shared", "牧尘用 Deepin 25", "system_fact")
|
||||
// results, err := client.Recall("牧尘系统是什么", 5)
|
||||
//
|
||||
// client := NewClient("http://localhost:7821", "mw-your-api-key")
|
||||
// mid, err := client.Commit("hermes", "shared", "牧尘用 Deepin 25", "system_fact")
|
||||
// results, err := client.Recall("牧尘系统是什么", 5)
|
||||
package routes
|
||||
|
||||
import (
|
||||
|
||||
@@ -36,7 +36,7 @@ func (ca *ConflictAPI) Resolve(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
ConflictID string `json:"conflict_id"`
|
||||
Resolution string `json:"resolution"` // keep_left / keep_right / merge / dismiss
|
||||
Winner string `json:"winner"` // id of the winning memory
|
||||
Winner string `json:"winner"` // id of the winning memory
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
respondError(w, 400, "invalid body")
|
||||
|
||||
@@ -24,7 +24,7 @@ type ConsolidationPipeline struct {
|
||||
graphUpdater *governance.AutoGraphUpdater
|
||||
conflicts *governance.ConflictDetector
|
||||
// Rust IPC 路径
|
||||
dataDir string
|
||||
dataDir string
|
||||
sqlitePath string
|
||||
}
|
||||
|
||||
@@ -444,7 +444,8 @@ func extractEntities(text string) []string {
|
||||
}
|
||||
|
||||
func minInt2(a, b int) int {
|
||||
if a < b { return a }
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
@@ -278,6 +278,11 @@ func (a *API) mergeMemory(existing models.MemoryRecord, newContent, newCategory,
|
||||
"updated_at": existing.UpdatedAt.Format(time.RFC3339),
|
||||
"content": existing.Content,
|
||||
})
|
||||
// t_1c12e000:merge 直接改 memories 表(content/version)⇒ 该 namespace 的
|
||||
// query 缓存读到的是合并前旧内容 ⇒ 失效(与蒸馏落库失效同一原则:写 memories 必失效)。
|
||||
if storage.SearchCacheInstance != nil && existing.Namespace != "" {
|
||||
storage.SearchCacheInstance.Invalidate(existing.Namespace)
|
||||
}
|
||||
}
|
||||
|
||||
// cosineSimilarity 计算两个向量的余弦相似度
|
||||
@@ -304,12 +309,13 @@ func cosineSimilarity(a, b []float32) float64 {
|
||||
// - 有匹配 ⇒ 200 + results + candidates_scanned
|
||||
//
|
||||
// candidates_scanned 字段契约(t_a70b07e1 修订 + t_949840b1 名实相符):
|
||||
// = 主路径粗检索候选池真实大小(LanceDB ANN 检索返回 + 去重 + 原始对话过滤之后、
|
||||
// rerank/MMR 截断之前捕获;cross-namespace / 笔记补充扩展发生在捕获之后,不计入);
|
||||
// **不等于** 最终返回的 len(results)(E4.2 / wiki 补充扩展 + 截断改变了产出数)。
|
||||
// - keyword 主路径不做 ANN 扫描 ⇒ 恒 0
|
||||
// - graph 兜底路径(X-Fallback: graph)= 兜底返回数
|
||||
// 用途:区分「真无匹配」(results=[] 但候选数>0)与「依赖故障」(非 2xx)。
|
||||
//
|
||||
// = 主路径粗检索候选池真实大小(LanceDB ANN 检索返回 + 去重 + 原始对话过滤之后、
|
||||
// rerank/MMR 截断之前捕获;cross-namespace / 笔记补充扩展发生在捕获之后,不计入);
|
||||
// **不等于** 最终返回的 len(results)(E4.2 / wiki 补充扩展 + 截断改变了产出数)。
|
||||
// - keyword 主路径不做 ANN 扫描 ⇒ 恒 0
|
||||
// - graph 兜底路径(X-Fallback: graph)= 兜底返回数
|
||||
// 用途:区分「真无匹配」(results=[] 但候选数>0)与「依赖故障」(非 2xx)。
|
||||
func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Query string `json:"query"`
|
||||
@@ -328,6 +334,21 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
respondError(w, 400, "query required")
|
||||
return
|
||||
}
|
||||
// 噪音闸门(t_689b6d38):系统通知/提示样板文本(如 "[IMPORTANT: You are running
|
||||
// as a scheduled cron job..."、"[System note: ...")不进语义召回。
|
||||
// 判定优先用结构化来源标记头,白名单(真实用户提问)照常召回,不误杀。
|
||||
if IsSystemTemplateInput(req.Query) {
|
||||
log.Printf("[recall] 拦截系统样板入参(system_template):不进语义召回(入参 %q)",
|
||||
strings.TrimSpace(req.Query))
|
||||
respond(w, 200, map[string]interface{}{
|
||||
"results": []models.RecallResult{},
|
||||
"count": 0,
|
||||
"candidates_scanned": 0,
|
||||
"mode": req.Mode,
|
||||
"skipped": "system_template",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 兼容 limit 和 top_k
|
||||
if req.Limit <= 0 && req.TopK > 0 {
|
||||
req.Limit = req.TopK
|
||||
@@ -345,6 +366,9 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var results []models.RecallResult
|
||||
var err error
|
||||
// t_1c12e000:cache_hit 显式标注——缓存命中(candidates_scanned=0 是「读旧快照」
|
||||
// 而非「真没扫到」)与回源扫表必须可区分。
|
||||
cacheHit := false
|
||||
// t_2d4e3b92 引入 + t_a70b07e1 语义修订 + t_949840b1 名实相符:
|
||||
// annScanned = 主路径(Pipeline.Recall / graph 兜底)粗检索候选池真实大小
|
||||
// (LanceDB ANN 返回 + 去重 + 原始对话过滤之后、rerank/MMR 截断之前捕获)。
|
||||
@@ -362,7 +386,10 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
// Pure keyword search: LanceDB + BM25 re-rank, or graph.db fallback
|
||||
// t_2d4e3b92: keyword 主路径依赖向量编码;编码失败(依赖不可用)⇒ 非 2xx,
|
||||
// 不再把错误伪装成 200 + 错误体。
|
||||
kwResults, kwErr := a.Pipeline.Recall(req.Query, req.Namespace, req.Limit, req.Diversity)
|
||||
// t_1c12e000:keyword 路径同样收进 RecallWithMeta(Pipeline.Recall 内部
|
||||
// 走同一缓存键),只为把 cache_hit 带回响应——旧版用 Recall 丢掉了 meta。
|
||||
var kwMeta storage.RecallMeta
|
||||
kwResults, kwMeta, kwErr := a.Pipeline.RecallWithMeta(req.Query, req.Namespace, req.Limit, req.Diversity)
|
||||
if kwErr != nil {
|
||||
// 主路径硬错误(向量编码/rerank 依赖不可用):即便兜底能补结果,也按 502 上报,
|
||||
// 调用方据此走失败路径而不是把依赖故障误判成"无匹配"
|
||||
@@ -371,13 +398,17 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("[recall:keyword] failed: %v", kwErr)
|
||||
return
|
||||
}
|
||||
cacheHit = kwMeta.CacheHit
|
||||
if len(kwResults) == 0 {
|
||||
// 真无匹配(正常业务):成功 schema,results=[] + candidates_scanned 供区分
|
||||
fallbackResults := a.GraphStore.FallbackTextSearch(req.Query, req.Namespace, req.Limit)
|
||||
results = convertFallbackResults(fallbackResults)
|
||||
respond(w, 200, map[string]interface{}{
|
||||
"results": results, "count": len(results), "mode": "keyword",
|
||||
"results": results,
|
||||
"count": len(results),
|
||||
"candidates_scanned": 0, // keyword 路径未做 ANN 扫描
|
||||
"mode": "keyword",
|
||||
"cache_hit": false, // graph 兜底结果不来自 search cache
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -390,8 +421,9 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
kwResults = kwResults[:req.Limit]
|
||||
}
|
||||
respond(w, 200, map[string]interface{}{
|
||||
"results": kwResults, "count": len(kwResults), "mode": "keyword",
|
||||
"results": kwResults, "count": len(kwResults), "mode": "keyword",
|
||||
"candidates_scanned": 0,
|
||||
"cache_hit": cacheHit, // t_1c12e000:keyword 主路径命中 search cache 时显式标注
|
||||
})
|
||||
return
|
||||
case "semantic":
|
||||
@@ -400,11 +432,13 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
var meta storage.RecallMeta
|
||||
results, meta, err = a.Pipeline.RecallWithMeta(req.Query, req.Namespace, req.Limit, req.Diversity)
|
||||
annScanned = meta.CandidatesScanned
|
||||
cacheHit = meta.CacheHit // t_1c12e000
|
||||
case "hybrid":
|
||||
// BM25 + vector combined (H1: BM25 scoring inside Pipeline.Recall)
|
||||
var meta storage.RecallMeta
|
||||
results, meta, err = a.Pipeline.RecallWithMeta(req.Query, req.Namespace, req.Limit, req.Diversity)
|
||||
annScanned = meta.CandidatesScanned
|
||||
cacheHit = meta.CacheHit // t_1c12e000
|
||||
}
|
||||
if err != nil {
|
||||
// P0: 降级到 graph.db 关键词搜索(仅 hybrid/semantic 主路径依赖向量/rerank)
|
||||
@@ -412,6 +446,7 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
if len(fallbackResults) > 0 {
|
||||
results = convertFallbackResults(fallbackResults)
|
||||
annScanned = len(fallbackResults)
|
||||
cacheHit = false // t_1c12e000:graph 兜底结果不来自 search cache
|
||||
w.Header().Set("X-Fallback", "graph")
|
||||
} else {
|
||||
// t_2d4e3b92: 主路径与兜底都失败 ⇒ 非 2xx + 统一错误体(绝不在 200 里塞错误)
|
||||
@@ -441,11 +476,19 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
if !alreadyHave {
|
||||
// t_631ab13d:补充结果同样直通 quality_score / v_value(与主路径一致,
|
||||
// 展示 DB 值;m 来自 LanceDB.Search ⇒ m.QualityScore 即库值)
|
||||
vv := 0.0
|
||||
if storage.VValueProvider != nil {
|
||||
vv = storage.VValueProvider(m.ID)
|
||||
}
|
||||
results = append(results, models.RecallResult{
|
||||
ID: m.ID,
|
||||
Content: m.Content,
|
||||
Category: m.Category,
|
||||
Score: 0.5, // shared 结果降权,使用默认分
|
||||
ID: m.ID,
|
||||
Content: m.Content,
|
||||
Category: m.Category,
|
||||
Score: 0.5, // shared 结果降权,使用默认分
|
||||
QualityScore: m.QualityScore,
|
||||
VValue: vv,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -469,11 +512,18 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
if !dup {
|
||||
// t_631ab13d:补充结果直通 quality_score / v_value(同上)
|
||||
vv := 0.0
|
||||
if storage.VValueProvider != nil {
|
||||
vv = storage.VValueProvider(m.ID)
|
||||
}
|
||||
results = append(results, models.RecallResult{
|
||||
ID: m.ID,
|
||||
Content: m.Content,
|
||||
Category: m.Category,
|
||||
Score: 0.45, // 笔记知识降权:不挤掉主记忆,但能被看到
|
||||
ID: m.ID,
|
||||
Content: m.Content,
|
||||
Category: m.Category,
|
||||
Score: 0.45, // 笔记知识降权:不挤掉主记忆,但能被看到
|
||||
QualityScore: m.QualityScore,
|
||||
VValue: vv,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -491,14 +541,15 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
gd.ClearMisses(req.Query)
|
||||
}
|
||||
}
|
||||
// VProp 自动记录:命中 → success,空结果 → failure
|
||||
// VProp 自动记录(t_268b0353 治根):「召回成功 ≠ 记忆有用」。
|
||||
// 此前 auto_recall 给 top-5 命中记忆各注入 +0.2 V 值(vValue/len(ids)),
|
||||
// 累积成全库 99.95% 自我表扬的排序权重(实测 top1 V=51.667,自证:外部≈2092:1)。
|
||||
// 现在:outcome 保持 success 用于**统计**(命中/空结果),但 memory_ids 传 nil,
|
||||
// "召回命中" 不再冒充 "记忆有用" —— 只有 user_feedback / distill 等外部信号
|
||||
// 才允许直接进排序权重。
|
||||
decisionID := "recall_" + req.Query + "_" + req.Namespace
|
||||
if len(results) > 0 {
|
||||
ids := make([]string, minInt3(len(results), 5))
|
||||
for i := 0; i < len(ids); i++ {
|
||||
ids[i] = results[i].ID
|
||||
}
|
||||
selfoptimize.VProp.RecordDecision(decisionID, ids, "auto_recall", "success", "")
|
||||
selfoptimize.VProp.RecordDecision(decisionID, nil, "auto_recall", "success", "")
|
||||
} else {
|
||||
selfoptimize.VProp.RecordDecision(decisionID, nil, "auto_recall", "failure", "")
|
||||
}
|
||||
@@ -514,6 +565,10 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
"count": len(results),
|
||||
"candidates_scanned": annScanned,
|
||||
"mode": req.Mode,
|
||||
// t_1c12e000:AC3 —— 缓存命中/回源扫表必须可区分。
|
||||
// true ⇒ candidates_scanned=0 是「读了 60s 内的旧快照」(含 L1/L2);
|
||||
// false ⇒ 本次真扫了表(candidates_scanned 为真实候选池大小)。
|
||||
"cache_hit": cacheHit,
|
||||
})
|
||||
|
||||
// H3: Async trust score update
|
||||
@@ -526,34 +581,9 @@ func (a *API) Recall(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// POST /api/v1/feedback — 用户反馈记忆是否有用,同时更新 useful_count/not_useful_count
|
||||
func (a *API) Feedback(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
MemoryID string `json:"memory_id"`
|
||||
Useful bool `json:"useful"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
respondError(w, 400, "invalid body")
|
||||
return
|
||||
}
|
||||
if req.MemoryID == "" {
|
||||
respondError(w, 400, "memory_id required")
|
||||
return
|
||||
}
|
||||
field := "useful_count"
|
||||
val := map[string]interface{}{"$inc": 1}
|
||||
if !req.Useful {
|
||||
field = "not_useful_count"
|
||||
}
|
||||
err := a.LanceDB.Update("memories", req.MemoryID, map[string]any{
|
||||
field: val,
|
||||
})
|
||||
if err != nil {
|
||||
respondError(w, 500, "feedback update failed: "+err.Error())
|
||||
return
|
||||
}
|
||||
respond(w, 200, map[string]string{"status": "ok"})
|
||||
}
|
||||
// GA-P0-7 (t_dd64345a):裸 POST /api/v1/feedback handler 已删除——它按 useful 布尔写
|
||||
// useful_count / not_useful_count(后者是 P0-1 拆分的旧混写列)。反馈统一走
|
||||
// feedback.go 的 /api/v1/feedback/{useful,not-useful,deprecate,correct}(写 human_not_useful_count)。
|
||||
|
||||
// POST /api/v1/recall/debug — recall 诊断端点(优化),返回各阶段耗时和状态
|
||||
func (a *API) RecallDebug(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -872,7 +902,8 @@ func (a *API) Dedup(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
for id := range demoted {
|
||||
if err := a.LanceDB.IncrementNotUseful(id); err != nil {
|
||||
// GA-P0-1 (t_718d8707):dedup 自动降权 → 写 dedup_downweight_count(不再写混写列 not_useful_count)
|
||||
if err := a.LanceDB.IncrementDedupDownweight(id); err != nil {
|
||||
log.Printf("[dedup] demote failed %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
@@ -1060,13 +1091,6 @@ func stringContains(s, substr string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func minInt3(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// convertFallbackResults 将 FallbackTextSearch 结果(map)转换为 RecallResult 格式
|
||||
func convertFallbackResults(rows []map[string]interface{}) []models.RecallResult {
|
||||
results := make([]models.RecallResult, 0, len(rows))
|
||||
|
||||
@@ -122,15 +122,15 @@ type EvalAPI struct {
|
||||
}
|
||||
|
||||
type EvalRun struct {
|
||||
ID string `json:"id"`
|
||||
Model string `json:"model"`
|
||||
RecallAt5 float64 `json:"recall_at_5"`
|
||||
ID string `json:"id"`
|
||||
Model string `json:"model"`
|
||||
RecallAt5 float64 `json:"recall_at_5"`
|
||||
PrecisionAt5 float64 `json:"precision_at_5"`
|
||||
MRR float64 `json:"mean_reciprocal_rank"`
|
||||
NDCG float64 `json:"ndcg"`
|
||||
MRR float64 `json:"mean_reciprocal_rank"`
|
||||
NDCG float64 `json:"ndcg"`
|
||||
|
||||
// 按标签分组
|
||||
RecallByTag map[string]float64 `json:"recall_at_5_by_tag,omitempty"`
|
||||
RecallByTag map[string]float64 `json:"recall_at_5_by_tag,omitempty"`
|
||||
PrecisionByTag map[string]float64 `json:"precision_at_5_by_tag,omitempty"`
|
||||
|
||||
// 按 Agent 分组
|
||||
@@ -147,14 +147,14 @@ type EvalRun struct {
|
||||
}
|
||||
|
||||
type EvalQueryDetail struct {
|
||||
Query string `json:"query"`
|
||||
ExpectedIDs []string `json:"expected_ids"`
|
||||
Query string `json:"query"`
|
||||
ExpectedIDs []string `json:"expected_ids"`
|
||||
MustIncludeKeywords []string `json:"must_include_keywords,omitempty"`
|
||||
Hits []string `json:"hits"`
|
||||
Misses []string `json:"misses"`
|
||||
RecallAt5 float64 `json:"recall_at_5"`
|
||||
PrecisionAt5 float64 `json:"precision_at_5"`
|
||||
KeywordOK bool `json:"keyword_ok,omitempty"`
|
||||
Hits []string `json:"hits"`
|
||||
Misses []string `json:"misses"`
|
||||
RecallAt5 float64 `json:"recall_at_5"`
|
||||
PrecisionAt5 float64 `json:"precision_at_5"`
|
||||
KeywordOK bool `json:"keyword_ok,omitempty"`
|
||||
}
|
||||
|
||||
// GoldenRunResult 金标 run 的扩展结果(含关键词判据)
|
||||
@@ -166,8 +166,8 @@ type GoldenRunResult struct {
|
||||
}
|
||||
|
||||
type ConsolidationHits struct {
|
||||
TotalDistilledUsed int `json:"total_distilled_used"`
|
||||
HitsAfterConsolidation int `json:"hits_after_consolidation"`
|
||||
TotalDistilledUsed int `json:"total_distilled_used"`
|
||||
HitsAfterConsolidation int `json:"hits_after_consolidation"`
|
||||
ConsolidationBenefit float64 `json:"consolidation_benefit"`
|
||||
}
|
||||
|
||||
@@ -185,7 +185,7 @@ func NewEvalAPI(p *storage.RecallPipeline, ldb storage.LanceDB) *EvalAPI {
|
||||
// POST /api/v1/eval/run
|
||||
func (ea *EvalAPI) Run(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Queries []struct {
|
||||
Queries []struct {
|
||||
Query string `json:"query"`
|
||||
ExpectedIDs []string `json:"expected_ids"`
|
||||
} `json:"queries"`
|
||||
@@ -250,11 +250,15 @@ func (ea *EvalAPI) Run(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
pAt5 := 0.0
|
||||
if len(results) > 0 { pAt5 = float64(hits) / float64(len(results)) }
|
||||
if len(results) > 0 {
|
||||
pAt5 = float64(hits) / float64(len(results))
|
||||
}
|
||||
totalPrecision += pAt5
|
||||
|
||||
rAt5 := 0.0
|
||||
if len(q.ExpectedIDs) > 0 { rAt5 = float64(hits) / float64(len(q.ExpectedIDs)) }
|
||||
if len(q.ExpectedIDs) > 0 {
|
||||
rAt5 = float64(hits) / float64(len(q.ExpectedIDs))
|
||||
}
|
||||
totalRecall += rAt5
|
||||
|
||||
// MRR
|
||||
@@ -269,11 +273,17 @@ func (ea *EvalAPI) Run(w http.ResponseWriter, r *http.Request) {
|
||||
dcg, idcg := 0.0, 0.0
|
||||
for i, res := range results {
|
||||
rel := 0.0
|
||||
if expectedSet[res.ID] { rel = 1.0 }
|
||||
if expectedSet[res.ID] {
|
||||
rel = 1.0
|
||||
}
|
||||
dcg += rel / log2(float64(i+2))
|
||||
if i < len(q.ExpectedIDs) { idcg += 1.0 / log2(float64(i+2)) }
|
||||
if i < len(q.ExpectedIDs) {
|
||||
idcg += 1.0 / log2(float64(i+2))
|
||||
}
|
||||
}
|
||||
if idcg > 0 {
|
||||
totalNDCG += dcg / idcg
|
||||
}
|
||||
if idcg > 0 { totalNDCG += dcg / idcg }
|
||||
|
||||
// 分组统计
|
||||
// (category 从 query 推断)
|
||||
@@ -308,18 +318,18 @@ func (ea *EvalAPI) Run(w http.ResponseWriter, r *http.Request) {
|
||||
recallByAgent := avgByGroup(recallByAgentMap)
|
||||
|
||||
run := &EvalRun{
|
||||
ID: time.Now().Format("20060102-150405"),
|
||||
Model: req.Model,
|
||||
RecallAt5: totalRecall / float64(totalQueries),
|
||||
PrecisionAt5: totalPrecision / float64(totalQueries),
|
||||
MRR: totalMRR / float64(totalQueries),
|
||||
NDCG: totalNDCG / float64(totalQueries),
|
||||
RecallByTag: recallByTag,
|
||||
ID: time.Now().Format("20060102-150405"),
|
||||
Model: req.Model,
|
||||
RecallAt5: totalRecall / float64(totalQueries),
|
||||
PrecisionAt5: totalPrecision / float64(totalQueries),
|
||||
MRR: totalMRR / float64(totalQueries),
|
||||
NDCG: totalNDCG / float64(totalQueries),
|
||||
RecallByTag: recallByTag,
|
||||
PrecisionByTag: precisionByTag,
|
||||
RecallByAgent: recallByAgent,
|
||||
QueryDetails: queryDetails,
|
||||
Queries: totalQueries,
|
||||
RanAt: time.Now(),
|
||||
RecallByAgent: recallByAgent,
|
||||
QueryDetails: queryDetails,
|
||||
Queries: totalQueries,
|
||||
RanAt: time.Now(),
|
||||
}
|
||||
|
||||
evals.mu.Lock()
|
||||
@@ -405,11 +415,15 @@ func (ea *EvalAPI) RunGolden(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
pAt5 := 0.0
|
||||
if len(results) > 0 { pAt5 = float64(hits) / float64(len(results)) }
|
||||
if len(results) > 0 {
|
||||
pAt5 = float64(hits) / float64(len(results))
|
||||
}
|
||||
totalPrecision += pAt5
|
||||
|
||||
rAt5 := 0.0
|
||||
if len(item.ExpectedIDs) > 0 { rAt5 = float64(hits) / float64(len(item.ExpectedIDs)) }
|
||||
if len(item.ExpectedIDs) > 0 {
|
||||
rAt5 = float64(hits) / float64(len(item.ExpectedIDs))
|
||||
}
|
||||
totalRecall += rAt5
|
||||
|
||||
// MRR
|
||||
@@ -424,11 +438,17 @@ func (ea *EvalAPI) RunGolden(w http.ResponseWriter, r *http.Request) {
|
||||
dcg, idcg := 0.0, 0.0
|
||||
for i, res := range results {
|
||||
rel := 0.0
|
||||
if expectedSet[res.ID] { rel = 1.0 }
|
||||
if expectedSet[res.ID] {
|
||||
rel = 1.0
|
||||
}
|
||||
dcg += rel / log2(float64(i+2))
|
||||
if i < len(item.ExpectedIDs) { idcg += 1.0 / log2(float64(i+2)) }
|
||||
if i < len(item.ExpectedIDs) {
|
||||
idcg += 1.0 / log2(float64(i+2))
|
||||
}
|
||||
}
|
||||
if idcg > 0 {
|
||||
totalNDCG += dcg / idcg
|
||||
}
|
||||
if idcg > 0 { totalNDCG += dcg / idcg }
|
||||
|
||||
// 关键词统计
|
||||
keywordTotal++
|
||||
@@ -445,9 +465,9 @@ func (ea *EvalAPI) RunGolden(w http.ResponseWriter, r *http.Request) {
|
||||
MustIncludeKeywords: item.MustIncludeKeywords,
|
||||
Hits: hitIDs,
|
||||
Misses: missIDs,
|
||||
RecallAt5: rAt5,
|
||||
PrecisionAt5: pAt5,
|
||||
KeywordOK: keywordOK,
|
||||
RecallAt5: rAt5,
|
||||
PrecisionAt5: pAt5,
|
||||
KeywordOK: keywordOK,
|
||||
})
|
||||
|
||||
totalQueries++
|
||||
@@ -459,15 +479,15 @@ func (ea *EvalAPI) RunGolden(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
run := &EvalRun{
|
||||
ID: time.Now().Format("20060102-150405"),
|
||||
Model: "golden-set",
|
||||
RecallAt5: totalRecall / float64(totalQueries),
|
||||
PrecisionAt5: totalPrecision / float64(totalQueries),
|
||||
MRR: totalMRR / float64(totalQueries),
|
||||
NDCG: totalNDCG / float64(totalQueries),
|
||||
QueryDetails: queryDetails,
|
||||
Queries: totalQueries,
|
||||
RanAt: time.Now(),
|
||||
ID: time.Now().Format("20060102-150405"),
|
||||
Model: "golden-set",
|
||||
RecallAt5: totalRecall / float64(totalQueries),
|
||||
PrecisionAt5: totalPrecision / float64(totalQueries),
|
||||
MRR: totalMRR / float64(totalQueries),
|
||||
NDCG: totalNDCG / float64(totalQueries),
|
||||
QueryDetails: queryDetails,
|
||||
Queries: totalQueries,
|
||||
RanAt: time.Now(),
|
||||
}
|
||||
|
||||
evals.mu.Lock()
|
||||
@@ -534,9 +554,9 @@ func (ea *EvalAPI) Generate(w http.ResponseWriter, r *http.Request) {
|
||||
// 固定的 12 条金标查询(4类×3级)
|
||||
// 对每条生成 query → recall 获得真实 expected_ids
|
||||
goldenQueries := []struct {
|
||||
cat string
|
||||
cat string
|
||||
difficulty string
|
||||
query string
|
||||
query string
|
||||
}{
|
||||
// system_fact
|
||||
{"system_fact", "easy", "织忆是什么"},
|
||||
@@ -569,11 +589,11 @@ func (ea *EvalAPI) Generate(w http.ResponseWriter, r *http.Request) {
|
||||
// 空断言自守:如果 recall 返回 0 条,不得写入空 expected_ids(那是 bootstrap,不是校准)
|
||||
if len(expectedIDs) == 0 {
|
||||
results = append(results, map[string]interface{}{
|
||||
"query": gq.query,
|
||||
"query": gq.query,
|
||||
"expected_ids": []string{},
|
||||
"category": gq.cat,
|
||||
"difficulty": gq.difficulty,
|
||||
"warning": "recall 返回 0 条——bootstrap 基线为空,需人工校准",
|
||||
"category": gq.cat,
|
||||
"difficulty": gq.difficulty,
|
||||
"warning": "recall 返回 0 条——bootstrap 基线为空,需人工校准",
|
||||
})
|
||||
continue
|
||||
}
|
||||
@@ -603,14 +623,21 @@ func makeSet(ids []string) map[string]bool {
|
||||
|
||||
func log2(x float64) float64 {
|
||||
result := 0.0
|
||||
for x > 2 { x /= 2; result += 1 }
|
||||
if x > 1 { result += (x - 1) }
|
||||
for x > 2 {
|
||||
x /= 2
|
||||
result += 1
|
||||
}
|
||||
if x > 1 {
|
||||
result += (x - 1)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func contains(list []string, item string) bool {
|
||||
for _, s := range list {
|
||||
if s == item { return true }
|
||||
if s == item {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -620,7 +647,9 @@ func avgByGroup(m map[string][]float64) map[string]float64 {
|
||||
for k, vals := range m {
|
||||
if len(vals) > 0 {
|
||||
sum := 0.0
|
||||
for _, v := range vals { sum += v }
|
||||
for _, v := range vals {
|
||||
sum += v
|
||||
}
|
||||
result[k] = sum / float64(len(vals))
|
||||
}
|
||||
}
|
||||
@@ -629,15 +658,17 @@ func avgByGroup(m map[string][]float64) map[string]float64 {
|
||||
|
||||
func inferCategory(query, defaultCat string) string {
|
||||
for cat, keywords := range map[string][]string{
|
||||
"system_fact": {"os", "系统", "gpu", "内存", "ram", "端口", "配置"},
|
||||
"user_pref": {"偏好", "风格", "喜欢", "牧尘"},
|
||||
"proj_context":{"项目", "路径", "代码", "设计文档"},
|
||||
"tool_usage": {"工具", "comfyui", "hermes", "opencode"},
|
||||
"system_fact": {"os", "系统", "gpu", "内存", "ram", "端口", "配置"},
|
||||
"user_pref": {"偏好", "风格", "喜欢", "牧尘"},
|
||||
"proj_context": {"项目", "路径", "代码", "设计文档"},
|
||||
"tool_usage": {"工具", "comfyui", "hermes", "opencode"},
|
||||
} {
|
||||
for _, kw := range keywords {
|
||||
if len(query) >= len(kw) {
|
||||
for i := 0; i <= len(query)-len(kw); i++ {
|
||||
if query[i:i+len(kw)] == kw { return cat }
|
||||
if query[i:i+len(kw)] == kw {
|
||||
return cat
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,13 +19,14 @@ import (
|
||||
// 每次调用间加 200ms 间隔,由调用方负责(TestEvalGolden_Live 里 sleep)。
|
||||
//
|
||||
// 统一判据口径(t_a70b07e1,与 Python 量具 golden-check.py 一致):
|
||||
// 取 recall 返回的前 N=5 条:ID 命中 = expected_ids 任一 ∈ 前 N 条;
|
||||
// 关键词命中 = 前 N 条内容任一含 must_include_keywords(大小写不敏感)。
|
||||
// N=5 的来源:recall 请求 top_k=5(liveRecallOnce 固定),与 golden-set.json 说明字段一致。
|
||||
//
|
||||
// 取 recall 返回的前 N=5 条:ID 命中 = expected_ids 任一 ∈ 前 N 条;
|
||||
// 关键词命中 = 前 N 条内容任一含 must_include_keywords(大小写不敏感)。
|
||||
// N=5 的来源:recall 请求 top_k=5(liveRecallOnce 固定),与 golden-set.json 说明字段一致。
|
||||
type recallResult struct {
|
||||
IDS []string
|
||||
TopContents []string
|
||||
Error error
|
||||
IDS []string
|
||||
TopContents []string
|
||||
Error error
|
||||
}
|
||||
|
||||
func liveRecallOnce(query string, maxRetries int, retryWait time.Duration) recallResult {
|
||||
|
||||
@@ -43,6 +43,11 @@ func (fa *FeedbackAPI) MarkUseful(w http.ResponseWriter, r *http.Request) {
|
||||
if err := fa.LanceDB.IncrementUseful(req.MemoryID); err != nil {
|
||||
log.Printf("[feedback] IncrementUseful failed %s: %v", req.MemoryID, err)
|
||||
}
|
||||
// GA-P0-4 (t_bb10e8bc):用户点「有用」= 真实正向 outcome → 追加 hits+ 事件(带时间戳)。
|
||||
// 语义定义见 internal/storage/hit_event.go(hits+ = 被真实使用/认可)。
|
||||
if err := fa.LanceDB.AppendHitEvent(req.MemoryID, "pos", "human_feedback"); err != nil {
|
||||
log.Printf("[feedback] AppendHitEvent(pos) failed %s: %v", req.MemoryID, err)
|
||||
}
|
||||
|
||||
// Phase F: 质量监控 — 每次反馈后检查低质量记忆
|
||||
feedbackCount := selfoptimize.Dash.UsefulCount + selfoptimize.Dash.NotUsefulCount
|
||||
@@ -80,8 +85,14 @@ func (fa *FeedbackAPI) MarkNotUseful(w http.ResponseWriter, r *http.Request) {
|
||||
[]string{req.MemoryID},
|
||||
"user_feedback", "failure", "",
|
||||
)
|
||||
if err := fa.LanceDB.IncrementNotUseful(req.MemoryID); err != nil {
|
||||
log.Printf("[feedback] IncrementNotUseful failed %s: %v", req.MemoryID, err)
|
||||
// GA-P0-1 (t_718d8707):人工点「没用」→ 写 human_not_useful_count(不再写混写列 not_useful_count)
|
||||
if err := fa.LanceDB.IncrementHumanNotUseful(req.MemoryID); err != nil {
|
||||
log.Printf("[feedback] IncrementHumanNotUseful failed %s: %v", req.MemoryID, err)
|
||||
}
|
||||
// GA-P0-4 (t_bb10e8bc):用户点「没用」= 真实负向 outcome → 追加 hits− 事件(带时间戳)。
|
||||
// 注意:这只在用户人工点「没用」时追加,dedup 自动降权走 dedup_downweight_count 不追加(见 hit_event.go 排除项)。
|
||||
if err := fa.LanceDB.AppendHitEvent(req.MemoryID, "neg", "human_feedback"); err != nil {
|
||||
log.Printf("[feedback] AppendHitEvent(neg) failed %s: %v", req.MemoryID, err)
|
||||
}
|
||||
|
||||
// Phase F: 质量监控 — negative feedback 触发低质量检测
|
||||
|
||||
@@ -14,19 +14,19 @@ import (
|
||||
|
||||
// GapAutoRepair 缺口自动修复器
|
||||
type GapAutoRepair struct {
|
||||
detector *selfoptimize.GapDetector
|
||||
ldb storage.LanceDB
|
||||
synonyms map[string][]string // 同义词映射
|
||||
detector *selfoptimize.GapDetector
|
||||
ldb storage.LanceDB
|
||||
synonyms map[string][]string // 同义词映射
|
||||
}
|
||||
|
||||
var GapRepair = &GapAutoRepair{
|
||||
detector: nil, // 由 InitGapRepair 注入
|
||||
synonyms: map[string][]string{
|
||||
"GPU": {"gpu", "显卡", "graphics"},
|
||||
"OS": {"os", "操作系统", "系统"},
|
||||
"API": {"api", "接口", "endpoint"},
|
||||
"SSE": {"sse", "server-sent events", "事件推送"},
|
||||
"CRDT": {"crdt", "冲突合并", "merge"},
|
||||
"GPU": {"gpu", "显卡", "graphics"},
|
||||
"OS": {"os", "操作系统", "系统"},
|
||||
"API": {"api", "接口", "endpoint"},
|
||||
"SSE": {"sse", "server-sent events", "事件推送"},
|
||||
"CRDT": {"crdt", "冲突合并", "merge"},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -26,17 +26,17 @@ type WorldModel struct {
|
||||
|
||||
Services []ServiceInfo `json:"services"` // ℐ: 服务和端口
|
||||
|
||||
ImplicitRules []string `json:"implicit_rules"` // C: 软约束
|
||||
HardConstraints []string `json:"hard_constraints"` // C: 硬约束
|
||||
Capabilities []string `json:"capabilities"`
|
||||
ImplicitRules []string `json:"implicit_rules"` // C: 软约束
|
||||
HardConstraints []string `json:"hard_constraints"` // C: 硬约束
|
||||
Capabilities []string `json:"capabilities"`
|
||||
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
type ServiceInfo struct {
|
||||
Name string `json:"name"`
|
||||
Port int `json:"port"`
|
||||
Status string `json:"status"`
|
||||
Name string `json:"name"`
|
||||
Port int `json:"port"`
|
||||
Status string `json:"status"`
|
||||
DependsOn []string `json:"depends_on,omitempty"`
|
||||
Provides []string `json:"provides,omitempty"`
|
||||
}
|
||||
@@ -111,13 +111,13 @@ func (wm *WorldModel) GetHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// POST /api/v1/l3/worldmodel — 更新
|
||||
func (wm *WorldModel) UpdateHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Environment *struct {
|
||||
Environment *struct {
|
||||
OS string `json:"os"`
|
||||
GPU string `json:"gpu"`
|
||||
VRAM string `json:"vram"`
|
||||
RAM string `json:"ram"`
|
||||
} `json:"environment"`
|
||||
AddRules []string `json:"add_rules"`
|
||||
AddRules []string `json:"add_rules"`
|
||||
AddCapabilities []string `json:"add_capabilities"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
|
||||
@@ -103,8 +103,10 @@ func formatEntry(content, category string) string {
|
||||
|
||||
func strValSafe(v interface{}) string {
|
||||
switch s := v.(type) {
|
||||
case string: return s
|
||||
case fmt.Stringer: return s.String()
|
||||
case string:
|
||||
return s
|
||||
case fmt.Stringer:
|
||||
return s.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -10,21 +10,22 @@
|
||||
// 结果追加到 ~/.hermes/logs/zhiyi-commit-provenance/YYYY-MM-DD.jsonl。
|
||||
//
|
||||
// 判定降级(AC2 重定义,t_dc412532 阶段2 设计输入):
|
||||
// (a) metadata 有 source_uri + quote + quote_sha256 → 复算 sha256(quote) 比对;
|
||||
// 不等 observable=false, reason=hash_mismatch
|
||||
// (b) 无上述字段但 source=="session-compress" 且 session_id 存在 →
|
||||
// 去 ~/.hermes/state.db(只读)取该 session 消息文本做回证。
|
||||
// 观察期实测(n=205, 24h):true=1 / false=8 / unknown=193;全部 8 条 false
|
||||
// 均来自 session-compress —— 判据「前 60 字逐字命中 / LCS≥60」对「摘要/派生内容」
|
||||
// 结构性必 false(摘要不会逐字出现在原文里)。AC2 修正:
|
||||
// * 原文不可得(session 已压缩/清除)→ observable=unknown, reason=source_not_available
|
||||
// (空原文 = 无法证伪,不是证伪失败 —— 旧逻辑误标 false/not_in_source)
|
||||
// * 前 60 字逐字命中 或 LCS≥60 → observable=true(引用片段可溯源)
|
||||
// * 其余(摘要低 LCS,属正常改写)→ observable=unknown, reason=derived_low_lcs
|
||||
// (对派生内容无法证伪 —— 摘要重述≠不可溯源)
|
||||
// 同时把实测数值 lcs_len / prefix_hit60 写进 jsonl(AC1 先量后判),
|
||||
// 供阶段3 转硬闸时按分布定档。
|
||||
// (c) 其他 source → observable=unknown, reason=no_derivation(不要瞎猜成 false)
|
||||
//
|
||||
// (a) metadata 有 source_uri + quote + quote_sha256 → 复算 sha256(quote) 比对;
|
||||
// 不等 observable=false, reason=hash_mismatch
|
||||
// (b) 无上述字段但 source=="session-compress" 且 session_id 存在 →
|
||||
// 去 ~/.hermes/state.db(只读)取该 session 消息文本做回证。
|
||||
// 观察期实测(n=205, 24h):true=1 / false=8 / unknown=193;全部 8 条 false
|
||||
// 均来自 session-compress —— 判据「前 60 字逐字命中 / LCS≥60」对「摘要/派生内容」
|
||||
// 结构性必 false(摘要不会逐字出现在原文里)。AC2 修正:
|
||||
// * 原文不可得(session 已压缩/清除)→ observable=unknown, reason=source_not_available
|
||||
// (空原文 = 无法证伪,不是证伪失败 —— 旧逻辑误标 false/not_in_source)
|
||||
// * 前 60 字逐字命中 或 LCS≥60 → observable=true(引用片段可溯源)
|
||||
// * 其余(摘要低 LCS,属正常改写)→ observable=unknown, reason=derived_low_lcs
|
||||
// (对派生内容无法证伪 —— 摘要重述≠不可溯源)
|
||||
// 同时把实测数值 lcs_len / prefix_hit60 写进 jsonl(AC1 先量后判),
|
||||
// 供阶段3 转硬闸时按分布定档。
|
||||
// (c) 其他 source → observable=unknown, reason=no_derivation(不要瞎猜成 false)
|
||||
package routes
|
||||
|
||||
import (
|
||||
@@ -162,9 +163,10 @@ func observeViaSession(content, sessionID string) (string, string, int, bool) {
|
||||
}
|
||||
|
||||
// provenanceMeasure 计算 content 相对 srcText 的可溯源实测指标:
|
||||
// prefixHit60 = content 前 60 字在 srcText 中逐字命中
|
||||
// lcsLen = content 与 srcText 的最长公共子串长度
|
||||
// match = prefixHit60 || lcsLen >= minCommonRun
|
||||
//
|
||||
// prefixHit60 = content 前 60 字在 srcText 中逐字命中
|
||||
// lcsLen = content 与 srcText 的最长公共子串长度
|
||||
// match = prefixHit60 || lcsLen >= minCommonRun
|
||||
func provenanceMeasure(content, srcText string) (match bool, lcsLen int, prefixHit60 bool) {
|
||||
if content == "" || srcText == "" {
|
||||
return false, 0, false
|
||||
@@ -233,4 +235,4 @@ func appendProvenanceLog(line map[string]interface{}) {
|
||||
}
|
||||
defer f.Close()
|
||||
fmt.Fprintf(f, "%s\n", data)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,4 +46,4 @@ func TestDecideQuoteHash(t *testing.T) {
|
||||
if observable != "false" || reason != "hash_mismatch" {
|
||||
t.Fatalf("expect false/hash_mismatch got %s/%s", observable, reason)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/xiaoxue/memoryweave/internal/governance"
|
||||
"github.com/xiaoxue/memoryweave/internal/models"
|
||||
@@ -35,7 +36,7 @@ func (f *fakeLanceDB) Stats() (map[string]interface{}, error) {
|
||||
return map[string]interface{}{}, nil
|
||||
}
|
||||
func (f *fakeLanceDB) SoftDelete(id, reason string) error { return nil }
|
||||
func (f *fakeLanceDB) PurgeInvalid() (int64, error) { return 0, nil }
|
||||
func (f *fakeLanceDB) PurgeInvalid() (int64, error) { return 0, nil }
|
||||
func (f *fakeLanceDB) SoftDeleteBatch(table string, ids []string, updatedAt string) (int64, error) {
|
||||
return 0, nil
|
||||
}
|
||||
@@ -56,6 +57,13 @@ func (f *fakeLanceDB) IncrementUseful(id string) error { return nil }
|
||||
func (f *fakeLanceDB) IncrementNotUseful(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GA-P0-1 (t_718d8707):拆分字段
|
||||
func (f *fakeLanceDB) IncrementHumanNotUseful(id string) error { return nil }
|
||||
func (f *fakeLanceDB) AppendHitEvent(memoryID, outcome, reason string) error { return nil }
|
||||
func (f *fakeLanceDB) CountHitEvents(memoryID, outcome string, since time.Time) (int, error) { return 0, nil }
|
||||
func (f *fakeLanceDB) GetHitEvents(memoryID string, limit int) ([]models.HitEvent, error) { return nil, nil }
|
||||
func (f *fakeLanceDB) IncrementDedupDownweight(id string) error { return nil }
|
||||
func (f *fakeLanceDB) UpdateMemoryContent(id, newContent, source string) error {
|
||||
return nil
|
||||
}
|
||||
@@ -183,7 +191,7 @@ type fakeEmbedder struct {
|
||||
|
||||
// TestRecallFailure_Non2xx 验证失败路径:依赖不可用时返回非 2xx + 统一错误体。
|
||||
// 模拟:embedder endpoint="" → encode 必失败(HTTP 到空 URL)⇒ 主路径 err != nil
|
||||
// + InMemoryGraph FallbackTextSearch 返回空 ⇒ 502 失败路径。
|
||||
// - InMemoryGraph FallbackTextSearch 返回空 ⇒ 502 失败路径。
|
||||
func TestRecallFailure_Non2xx(t *testing.T) {
|
||||
db := &fakeLanceDB{searchResults: nil}
|
||||
gs := governance.NewInMemoryGraph()
|
||||
|
||||
@@ -17,18 +17,18 @@ const skillRedisKey = "zhiyi:skills"
|
||||
|
||||
// BetaSkill 贝叶斯 Skill 评分(G7 扩展版)
|
||||
type BetaSkill struct {
|
||||
Name string `json:"name"`
|
||||
Alpha float64 `json:"alpha"` // α = successes + 1
|
||||
Beta float64 `json:"beta"` // β = failures + 1
|
||||
Trials int `json:"trials"`
|
||||
Successes int `json:"successes"`
|
||||
ETA float64 `json:"eta"` // α/(α+β) 贝叶斯均值
|
||||
Status string `json:"status"` // active / probation / retired
|
||||
LastUpdated time.Time `json:"last_updated"`
|
||||
Name string `json:"name"`
|
||||
Alpha float64 `json:"alpha"` // α = successes + 1
|
||||
Beta float64 `json:"beta"` // β = failures + 1
|
||||
Trials int `json:"trials"`
|
||||
Successes int `json:"successes"`
|
||||
ETA float64 `json:"eta"` // α/(α+β) 贝叶斯均值
|
||||
Status string `json:"status"` // active / probation / retired
|
||||
LastUpdated time.Time `json:"last_updated"`
|
||||
// G7 扩展字段
|
||||
PromptTemplate string `json:"prompt_template,omitempty"` // 可执行 prompt(含 {param} 占位符)
|
||||
LinkedMemoryIDs []string `json:"linked_memory_ids,omitempty"` // 关联的记忆 ID
|
||||
LinkedEntities []string `json:"linked_entities,omitempty"` // 关联的图谱实体
|
||||
PromptTemplate string `json:"prompt_template,omitempty"` // 可执行 prompt(含 {param} 占位符)
|
||||
LinkedMemoryIDs []string `json:"linked_memory_ids,omitempty"` // 关联的记忆 ID
|
||||
LinkedEntities []string `json:"linked_entities,omitempty"` // 关联的图谱实体
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
@@ -146,12 +146,12 @@ func (bsm *BayesianSkillManager) Register(name, promptTemplate string, linkedEnt
|
||||
skill, exists := bsm.skills[name]
|
||||
if !exists {
|
||||
skill = &BetaSkill{
|
||||
Name: name,
|
||||
Alpha: 1.0,
|
||||
Beta: 1.0,
|
||||
CreatedAt: time.Now(),
|
||||
PromptTemplate: promptTemplate,
|
||||
LinkedEntities: linkedEntities,
|
||||
Name: name,
|
||||
Alpha: 1.0,
|
||||
Beta: 1.0,
|
||||
CreatedAt: time.Now(),
|
||||
PromptTemplate: promptTemplate,
|
||||
LinkedEntities: linkedEntities,
|
||||
}
|
||||
bsm.skills[name] = skill
|
||||
} else {
|
||||
@@ -306,11 +306,11 @@ func (bsm *BayesianSkillManager) Stats() map[string]interface{} {
|
||||
}
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"total": len(bsm.skills),
|
||||
"active": active,
|
||||
"probation": probation,
|
||||
"retired": retired,
|
||||
"persisted": bsm.persisted,
|
||||
"total": len(bsm.skills),
|
||||
"active": active,
|
||||
"probation": probation,
|
||||
"retired": retired,
|
||||
"persisted": bsm.persisted,
|
||||
"redis_connected": bsm.redisClient != nil,
|
||||
}
|
||||
}
|
||||
@@ -371,10 +371,10 @@ func (sm *SkillManager) Trial(w http.ResponseWriter, r *http.Request) {
|
||||
// POST /api/v1/skills — 注册新 skill(G7 新增)
|
||||
func (sm *SkillManager) Register(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
PromptTemplate string `json:"prompt_template"`
|
||||
LinkedEntities []string `json:"linked_entities"`
|
||||
Description string `json:"description"`
|
||||
Name string `json:"name"`
|
||||
PromptTemplate string `json:"prompt_template"`
|
||||
LinkedEntities []string `json:"linked_entities"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
respondError(w, 400, "invalid body: "+err.Error())
|
||||
@@ -427,4 +427,4 @@ func (sm *SkillManager) GetSkillETA(name string) float64 {
|
||||
return 0.5
|
||||
}
|
||||
return skill.ETA
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,8 +153,8 @@ func crystallizeFromRecord(mem *models.MemoryRecord) (*BetaSkill, error) {
|
||||
if err == nil && llmResult != "" {
|
||||
var parsed struct {
|
||||
PromptTemplate string `json:"prompt_template"`
|
||||
Entities []string `json:"entities"`
|
||||
SkillName string `json:"skill_name"`
|
||||
Entities []string `json:"entities"`
|
||||
SkillName string `json:"skill_name"`
|
||||
}
|
||||
if err2 := json.Unmarshal([]byte(llmResult), &parsed); err2 == nil {
|
||||
if parsed.PromptTemplate != "" {
|
||||
@@ -318,4 +318,4 @@ func getLDB() storage.LanceDB {
|
||||
return ldbGetter()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,4 +240,3 @@ func ExecuteSkill(w http.ResponseWriter, r *http.Request) {
|
||||
"eta": skill.ETA,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
// 织忆 MemoryWeave — 系统通知/提示样板文本判定(被动注入入口噪音闸门)
|
||||
//
|
||||
// 背景(t_689b6d38,2026-10-11 实测):织忆被动注入入口不区分「真实用户消息」与
|
||||
// 「系统样板文本」。近 7 天 [graph/notes] 通道 806 次注入里 543 次(67%)的入参是
|
||||
// 系统通知/提示样板(形如 "[IMPORTANT: You are running as a scheduled cron job...",
|
||||
// "[IMPORTANT: Background process ... completed", "[System note: ..."),它们照常走
|
||||
// 完整实体拆解并返回 3 篇笔记注入上下文 —— 纯浪费 + 稀释有效注入信噪比。
|
||||
//
|
||||
// 判定优先用【结构化信号】(消息来源标记头,如 "[IMPORTANT:"、"[HEARTBEAT]"、
|
||||
// "[System note:"、"Cronjob Response:"、role/来源字段),而不是纯文本长度启发式。
|
||||
// 命中 ⇒ 调用方应跳过语义召回与笔记返回。白名单:真实用户提问(含飞书/QQ/CLI 入站
|
||||
// 消息)不命中任何前缀/子串 ⇒ 照常召回,不误杀。
|
||||
package routes
|
||||
|
||||
import "github.com/xiaoxue/memoryweave/internal/textfilter"
|
||||
|
||||
// IsSystemTemplateInput 判定一段文本是否系统通知/提示样板(非真实用户提问)。
|
||||
// 命中 ⇒ 调用方应跳过语义召回与笔记返回(t_689b6d38 噪音闸门)。
|
||||
//
|
||||
// 2026-10-12:判定清单已抽到叶子包 internal/textfilter(蒸馏侧准入闸门也要用,
|
||||
// 而 distill 不能反向 import routes)。本函数保留为薄委托,调用点零改动。
|
||||
// 清单只维护 textfilter 一处。
|
||||
func IsSystemTemplateInput(s string) bool {
|
||||
return textfilter.IsSystemTemplate(s)
|
||||
}
|
||||
@@ -42,8 +42,8 @@ type Trigger struct {
|
||||
Condition string `json:"condition"`
|
||||
Urgency float64 `json:"urgency"` // 0-1,越大越紧急
|
||||
Active bool `json:"active"`
|
||||
Paused bool `json:"paused"` // kill-switch
|
||||
Cooldown string `json:"cooldown"` // 冷却时间
|
||||
Paused bool `json:"paused"` // kill-switch
|
||||
Cooldown string `json:"cooldown"` // 冷却时间
|
||||
FailCount int `json:"fail_count"` // 连续失败计数
|
||||
LastFiredAt time.Time `json:"last_fired_at,omitempty"`
|
||||
Description string `json:"description"`
|
||||
@@ -189,5 +189,3 @@ func (tm *TriggerManager) RecordFail(id string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ type TuningConfig struct {
|
||||
ConsolidateAfter int `json:"consolidate_after"` // N 条蒸馏后触发整合
|
||||
|
||||
// 历史调参记录
|
||||
history []TuningEvent
|
||||
history []TuningEvent
|
||||
}
|
||||
|
||||
type TuningEvent struct {
|
||||
|
||||
@@ -15,9 +15,9 @@ var upgrader = websocket.Upgrader{
|
||||
|
||||
// WSManager 管理所有 WebSocket 连接,支持按 AgentID 定向推送
|
||||
type WSManager struct {
|
||||
mu sync.RWMutex
|
||||
clients map[string]map[*websocket.Conn]bool // agentID → 连接集合
|
||||
connMu sync.Map // per-conn mutex: *websocket.Conn → *sync.Mutex
|
||||
mu sync.RWMutex
|
||||
clients map[string]map[*websocket.Conn]bool // agentID → 连接集合
|
||||
connMu sync.Map // per-conn mutex: *websocket.Conn → *sync.Mutex
|
||||
}
|
||||
|
||||
// 全局 WebSocket 管理器
|
||||
|
||||
@@ -65,7 +65,7 @@ func (m *mockLance) GetTopByQuality(agentID string, limit int) ([]models.MemoryR
|
||||
}
|
||||
func (m *mockLance) Stats() (map[string]interface{}, error) { return map[string]interface{}{}, nil }
|
||||
func (m *mockLance) SoftDelete(id, reason string) error { return nil }
|
||||
func (m *mockLance) PurgeInvalid() (int64, error) { return 0, nil }
|
||||
func (m *mockLance) PurgeInvalid() (int64, error) { return 0, nil }
|
||||
func (m *mockLance) SoftDeleteBatch(table string, ids []string, updatedAt string) (int64, error) {
|
||||
return 0, nil
|
||||
}
|
||||
@@ -78,6 +78,13 @@ func (m *mockLance) Backup(path string) error { r
|
||||
func (m *mockLance) GetAuditLog(limit int) ([]map[string]interface{}, error) { return nil, nil }
|
||||
func (m *mockLance) IncrementUseful(id string) error { return nil }
|
||||
func (m *mockLance) IncrementNotUseful(id string) error { return nil }
|
||||
|
||||
// GA-P0-1 (t_718d8707):拆分字段
|
||||
func (m *mockLance) IncrementHumanNotUseful(id string) error { return nil }
|
||||
func (m *mockLance) AppendHitEvent(memoryID, outcome, reason string) error { return nil }
|
||||
func (m *mockLance) CountHitEvents(memoryID, outcome string, since time.Time) (int, error) { return 0, nil }
|
||||
func (m *mockLance) GetHitEvents(memoryID string, limit int) ([]models.HitEvent, error) { return nil, nil }
|
||||
func (m *mockLance) IncrementDedupDownweight(id string) error { return nil }
|
||||
func (m *mockLance) UpdateMemoryContent(id, newContent, source string) error { return nil }
|
||||
func (m *mockLance) InsertMemory(rec models.MemoryRecord) error { return nil }
|
||||
func (m *mockLance) Insert(table string, record any) error { return nil }
|
||||
|
||||
+82
-25
@@ -172,11 +172,16 @@ func NewServer() http.Handler {
|
||||
Content: input.Content, Facts: result.Facts,
|
||||
Entities: entityNames, Namespace: input.Namespace,
|
||||
EpisodeID: input.EpisodeID,
|
||||
Source: input.Source, // 2026-10-12 t_67d0279a 方案A:来源分档透传(auditSource 前缀归一后比对)
|
||||
})
|
||||
|
||||
// 回写蒸馏产物到记忆库(LanceDB)
|
||||
// 2026-09-07 方案B 质量门槛: 碎片(<20字)/噪声句(状态汇报/桥接/reflection)不入库,
|
||||
// 与已有记忆完全重复跳过(蒸馏直写不走 commit exact_dup, 需本层查重)。
|
||||
// 2026-10-13 t_1c12e000:distilled facts 是 memories 表的唯一写入点。
|
||||
// 只要有任何一条真的 InsertMemory 成功 ⇒ 该 namespace 的 query 缓存作废
|
||||
//("先写再问" 场景:写完之后立刻可被之前问过但当时还没内容的答案命中)。
|
||||
factsLanded := false
|
||||
for _, fact := range result.Facts {
|
||||
// 2026-09-10 P0:直写路径同样脱敏(这里绕过 FilterQualityFacts,
|
||||
// 事故里泄漏进镜像的记忆正是从这条路径入库的)。
|
||||
@@ -216,19 +221,29 @@ func NewServer() http.Handler {
|
||||
DerivedFrom: input.EpisodeID,
|
||||
Freshness: "fresh",
|
||||
Source: srcOrLLMDistill(input.Source), // 2026-09-26:有笔记出处→"wiki:<路径>";否则沿用 llm_distill(2026-09-07 方案C provenance)
|
||||
// 2026-10-08 (t_7e3577c5):血缘字段——写入方经 metadata 携带;缺省兜底
|
||||
// embedding_model→bge-m3(实际嵌入模型),script_version→unknown。
|
||||
EmbeddingModel: orDefault(input.EmbeddingModel, "bge-m3"),
|
||||
ScriptVersion: orDefault(input.ScriptVersion, "unknown"),
|
||||
// 2026-10-08 (Gap#2):信任等级透传到产物(nil 保持 NULL,不伪装 0.0)
|
||||
Confidence: input.Confidence,
|
||||
ConfidenceSource: input.ConfidenceSource,
|
||||
}
|
||||
// 2026-10-08 (t_7e3577c5):血缘字段——写入方经 metadata 携带;缺省兜底
|
||||
// embedding_model→bge-m3(实际嵌入模型),script_version→unknown。
|
||||
EmbeddingModel: orDefault(input.EmbeddingModel, "bge-m3"),
|
||||
ScriptVersion: orDefault(input.ScriptVersion, "unknown"),
|
||||
// 2026-10-08 (Gap#2):信任等级透传到产物(nil 保持 NULL,不伪装 0.0)
|
||||
Confidence: input.Confidence,
|
||||
ConfidenceSource: input.ConfidenceSource,
|
||||
}
|
||||
log.Printf("[srcprobe] 蒸馏落库 episode=%s input.Source=%q → mem.Source=%q", input.EpisodeID, input.Source, mem.Source)
|
||||
if err := ldb.InsertMemory(mem); err != nil {
|
||||
log.Printf("[distill] commit fact failed: %v", err)
|
||||
} else {
|
||||
factsLanded = true // t_1c12e000:确有 distilled fact 落库
|
||||
}
|
||||
}
|
||||
// t_1c12e000:写路径缓存失效(方案 A)——distilled facts 是 memories 唯一写点。
|
||||
// 本进程 Invalidate(ns) 清 L1+L2;跨 Agent 广播复用 commit handler 的
|
||||
// PushCacheInvalidate(已注册 cache.invalidate 订阅),此处不重复广播。
|
||||
if factsLanded && storage.SearchCacheInstance != nil {
|
||||
storage.SearchCacheInstance.Invalidate(input.Namespace)
|
||||
log.Printf("[cache] 蒸馏落库后失效 namespace=%s(%d 条 facts,episode=%s)",
|
||||
input.Namespace, len(result.Facts), input.EpisodeID)
|
||||
}
|
||||
// 冲突检测:加载已有记忆进行比较
|
||||
// 必须用**新内容的真实向量**做语义检索取近邻;用零向量在语义索引上
|
||||
// 返回的只是"范数最小"的任意行,真正的矛盾对象不在比对集里 → 永远检不出冲突。
|
||||
@@ -345,15 +360,19 @@ func NewServer() http.Handler {
|
||||
|
||||
mux.HandleFunc("/health", routes.HandleHealth)
|
||||
mux.HandleFunc("/api/v1/health", routes.HandleHealth)
|
||||
// 图谱缓存统计
|
||||
// 图谱缓存统计 + 搜索缓存统计(t_1c12e000:并入 search_cache 段,
|
||||
// 暴露 hits/misses/invalidated/size/ttl,写频对命中率的影响可观测)
|
||||
mux.HandleFunc("/api/v1/cache/stats", func(w http.ResponseWriter, r *http.Request) {
|
||||
if cachedGraphStoreRef != nil {
|
||||
respondJSON(w, 200, map[string]interface{}{
|
||||
"graph_cache": cachedGraphStoreRef.Stats(),
|
||||
})
|
||||
} else {
|
||||
respondJSON(w, 200, map[string]interface{}{"graph_cache": nil})
|
||||
resp := map[string]interface{}{
|
||||
"graph_cache": nil,
|
||||
}
|
||||
if cachedGraphStoreRef != nil {
|
||||
resp["graph_cache"] = cachedGraphStoreRef.Stats()
|
||||
}
|
||||
if storage.SearchCacheInstance != nil {
|
||||
resp["search_cache"] = storage.SearchCacheInstance.Stats()
|
||||
}
|
||||
respondJSON(w, 200, resp)
|
||||
})
|
||||
// ─── Prometheus /metrics ─────────────────────
|
||||
mux.Handle("/metrics", metrics.Handler())
|
||||
@@ -382,7 +401,8 @@ func NewServer() http.Handler {
|
||||
})
|
||||
mux.HandleFunc("/api/v1/recall", api.Recall)
|
||||
mux.HandleFunc("/api/v1/recall/debug", api.RecallDebug)
|
||||
mux.HandleFunc("/api/v1/feedback", api.Feedback)
|
||||
// GA-P0-7 (t_dd64345a):裸 /api/v1/feedback 已退役——其 handler 曾写旧字段 not_useful_count。
|
||||
// 反馈统一走 /api/v1/feedback/{useful,not-useful,deprecate,correct}(feedback.go,写拆分后新字段)。
|
||||
mux.HandleFunc("/api/v1/bootstrap", api.Bootstrap)
|
||||
mux.HandleFunc("/api/v1/stats", api.Stats)
|
||||
mux.HandleFunc("/api/v1/batch-commit", api.BatchCommit)
|
||||
@@ -547,6 +567,23 @@ func NewServer() http.Handler {
|
||||
if entity == "" {
|
||||
entity = r.URL.Query().Get("query") // 也接受整句 query
|
||||
}
|
||||
// 噪音闸门(t_689b6d38):系统通知/提示样板文本(如 "[IMPORTANT: You are running
|
||||
// as a scheduled cron job..."、"[System note: ...")不进语义召回、不返回笔记。
|
||||
// 判定优先用结构化来源标记头,白名单(真实用户提问)照常召回,不误杀。
|
||||
if routes.IsSystemTemplateInput(entity) {
|
||||
log.Printf("[graph/notes] 拦截系统样板入参(system_template):不拆实体、不返回笔记(入参 %q)",
|
||||
strings.TrimSpace(entity))
|
||||
respondJSON(w, 200, map[string]interface{}{
|
||||
"entity": entity,
|
||||
"graph_paths": []map[string]interface{}{},
|
||||
"notes": []map[string]interface{}{},
|
||||
"paths_count": 0,
|
||||
"notes_count": 0,
|
||||
"mode": "none",
|
||||
"skipped": "system_template",
|
||||
})
|
||||
return
|
||||
}
|
||||
maxHops := 2
|
||||
if h := r.URL.Query().Get("max_hops"); h != "" {
|
||||
if parsed, err := strconv.Atoi(h); err == nil && parsed > 0 {
|
||||
@@ -645,6 +682,24 @@ func NewServer() http.Handler {
|
||||
mode = "direct"
|
||||
} else if cands := entityIndex.Extract(entity, 2); len(cands) > 0 {
|
||||
// 兜底:entity 是整句自然语言(调用方原样传用户提问)→ 本地把它拆成候选实体
|
||||
// 2026-10-12 GA-P0-5:概念/实体提取路径接 echo 防线 —— 审计笔记把污染词当证据
|
||||
// 列出来 → 蒸馏成 episode → 兜底拆实体时会把已删除污染节点(n_galway / n_rothfuss /
|
||||
// n_the_name_of_the_wind)重建。候选实体若只是 prompt 教学示例的回显残片
|
||||
// (逐字相等 / illustration 类 ≥4 字子串,判据与蒸馏侧 IsPromptEcho 同源)
|
||||
// ⇒ 不入图;[ECHO-DROP] 日志供验证防线在跑。
|
||||
var echoDropped int
|
||||
kept := cands[:0]
|
||||
for _, c := range cands {
|
||||
if distill.IsPromptEchoEntity(c) {
|
||||
echoDropped++
|
||||
continue
|
||||
}
|
||||
kept = append(kept, c)
|
||||
}
|
||||
if echoDropped > 0 {
|
||||
log.Printf("[graph/notes][ECHO-DROP] 剔除回显残片 %d 个(%q)→ 不参与笔记匹配/不入图", echoDropped, cands)
|
||||
cands = kept
|
||||
}
|
||||
const maxSearchEntities = 8 // 参与笔记匹配的实体上限:候选 + 少量邻居,防噪声淹没
|
||||
ents := make([]string, 0, maxSearchEntities)
|
||||
seen := make(map[string]bool, maxSearchEntities)
|
||||
@@ -1597,14 +1652,14 @@ func NewServer() http.Handler {
|
||||
// distill 保持 cluster_only(高频触发,只做快速聚类)
|
||||
_, err = consolPipe.Run()
|
||||
case "consolidation", "backtrack":
|
||||
// full 模式:含 prune + decay + 质量回溯(Step 4)
|
||||
// 48h cooldown 保护,不会真正每分钟跑 full(cooldown 内 CanFire 仍返回 false)
|
||||
_, err = consolPipe.RunWithMode("full")
|
||||
// 2026-09-21 (t_4ce53193):冲突去重 + 保留策略
|
||||
// 去重:同 (L,R) 对只留最早一条,累加 dup_count,归档 + 审计
|
||||
conflictDetector.DedupeActive(governance.DefaultConflictRetentionConfig())
|
||||
// 保留:pending > 7d 无人处置 → 归档(不删除)
|
||||
conflictDetector.RetainAndArchive(governance.DefaultConflictRetentionConfig())
|
||||
// full 模式:含 prune + decay + 质量回溯(Step 4)
|
||||
// 48h cooldown 保护,不会真正每分钟跑 full(cooldown 内 CanFire 仍返回 false)
|
||||
_, err = consolPipe.RunWithMode("full")
|
||||
// 2026-09-21 (t_4ce53193):冲突去重 + 保留策略
|
||||
// 去重:同 (L,R) 对只留最早一条,累加 dup_count,归档 + 审计
|
||||
conflictDetector.DedupeActive(governance.DefaultConflictRetentionConfig())
|
||||
// 保留:pending > 7d 无人处置 → 归档(不删除)
|
||||
conflictDetector.RetainAndArchive(governance.DefaultConflictRetentionConfig())
|
||||
case "merge":
|
||||
_, err = consolPipe.RunMerge()
|
||||
case "prune":
|
||||
@@ -1667,8 +1722,10 @@ func NewServer() http.Handler {
|
||||
}
|
||||
continue
|
||||
}
|
||||
// GA-P0-2 (t_c6d41ac6):衰减/遗忘只认 recall_verified_count(正反馈才是 V 值)。
|
||||
// recall_count 已冻结(历史虚高保命来源),seen 只用于观测。
|
||||
recallCount := 0
|
||||
if rc, ok := m["recall_count"].(int); ok {
|
||||
if rc, ok := m["recall_verified_count"].(int); ok {
|
||||
recallCount = rc
|
||||
}
|
||||
tier := "normal"
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// GA-P0-7 (t_dd64345a):回归守卫 —— 裸路由 /api/v1/feedback 已退役。
|
||||
// 它的 handler(core.go Feedback,已删)曾按 useful 布尔写旧字段 not_useful_count
|
||||
// (P0-1 t_718d8707 已拆分为 human_not_useful_count / dedup_downweight_count)。
|
||||
// 谁若重新注册这个裸路由(旧字段随之复活),本测试必须红。
|
||||
func TestNoBareFeedbackRoute(t *testing.T) {
|
||||
src, err := os.ReadFile("server.go")
|
||||
if err != nil {
|
||||
t.Fatalf("read server.go: %v", err)
|
||||
}
|
||||
s := string(src)
|
||||
|
||||
// 裸路由注册必须不存在(精确匹配 server.go:385 的旧行)。
|
||||
bare := `mux.HandleFunc("/api/v1/feedback", api.Feedback)`
|
||||
if strings.Contains(s, bare) {
|
||||
t.Fatalf("裸 /api/v1/feedback 路由被重新注册——其 handler 写旧字段 not_useful_count(GA-P0-1);请改用 /api/v1/feedback/{useful,not-useful,deprecate,correct}")
|
||||
}
|
||||
|
||||
// 新拆分后的子路由必须在(反馈统一走它们,写 human_not_useful_count 等新字段)。
|
||||
for _, sub := range []string{
|
||||
`"/api/v1/feedback/useful"`,
|
||||
`"/api/v1/feedback/not-useful"`,
|
||||
`"/api/v1/feedback/deprecate"`,
|
||||
`"/api/v1/feedback/correct"`,
|
||||
} {
|
||||
if !strings.Contains(s, sub) {
|
||||
t.Fatalf("期望子路由 %s 已注册,但 server.go 中缺失", sub)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,4 +23,4 @@ func initStorageForSQLite(emb *storage.Embedder) storage.LanceDB {
|
||||
}
|
||||
log.Printf("[zhiyid] 存储后端: SQLite (CGO) — %s", dbPath)
|
||||
return sqliteDB
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,4 +53,4 @@ func initGraphStore() governance.GraphStore {
|
||||
}
|
||||
log.Printf("[zhiyid] 图谱后端: SQLiteGraphStore — %s", graphPath)
|
||||
return gs
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,4 +62,4 @@ func initGraphStore() governance.GraphStore {
|
||||
}
|
||||
log.Printf("[zhiyid] 图谱后端: InMemoryGraph (Windows,纯 Go 无 SQLite CGO)")
|
||||
return governance.NewInMemoryGraph()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,35 +20,35 @@ import (
|
||||
|
||||
// ConsolidateRequest 整合请求
|
||||
type ConsolidateRequest struct {
|
||||
Task string `json:"task"` // "full" | "cluster_only" | "prune_only"
|
||||
LanceDBPath string `json:"lancedb_path"` // LanceDB 数据目录
|
||||
SQLitePath string `json:"sqlite_path"` // SQLite 图谱路径
|
||||
LLMEndpoint string `json:"llm_endpoint"` // LLM API 端点
|
||||
LLMModel string `json:"llm_model"` // LLM 模型名
|
||||
LLMApiKey string `json:"llm_api_key"` // LLM API Key (Authorization header)
|
||||
LLMBudget int `json:"llm_budget"` // 本次可用 LLM 次数
|
||||
Epsilon float64 `json:"epsilon"` // DBSCAN 邻域半径(1024-dim BGE-M3 单位向量建议 1.5)
|
||||
MinPoints int `json:"min_points"` // DBSCAN 最小点数
|
||||
ModelDir string `json:"model_dir"` // BGE 模型目录(用于 quality backtrace 编码)
|
||||
Task string `json:"task"` // "full" | "cluster_only" | "prune_only"
|
||||
LanceDBPath string `json:"lancedb_path"` // LanceDB 数据目录
|
||||
SQLitePath string `json:"sqlite_path"` // SQLite 图谱路径
|
||||
LLMEndpoint string `json:"llm_endpoint"` // LLM API 端点
|
||||
LLMModel string `json:"llm_model"` // LLM 模型名
|
||||
LLMApiKey string `json:"llm_api_key"` // LLM API Key (Authorization header)
|
||||
LLMBudget int `json:"llm_budget"` // 本次可用 LLM 次数
|
||||
Epsilon float64 `json:"epsilon"` // DBSCAN 邻域半径(1024-dim BGE-M3 单位向量建议 1.5)
|
||||
MinPoints int `json:"min_points"` // DBSCAN 最小点数
|
||||
ModelDir string `json:"model_dir"` // BGE 模型目录(用于 quality backtrace 编码)
|
||||
}
|
||||
|
||||
// ConsolidateResponse 整合响应
|
||||
type ConsolidateResponse struct {
|
||||
Status string `json:"status"` // "ok" | "partial_failure"
|
||||
ReportJSON string `json:"report_json"` // ConsolidationReport JSON
|
||||
FailureStep string `json:"failure_step"` // 失败步骤
|
||||
ErrorDetail string `json:"error_detail"` // 错误详情
|
||||
Status string `json:"status"` // "ok" | "partial_failure"
|
||||
ReportJSON string `json:"report_json"` // ConsolidationReport JSON
|
||||
FailureStep string `json:"failure_step"` // 失败步骤
|
||||
ErrorDetail string `json:"error_detail"` // 错误详情
|
||||
}
|
||||
|
||||
// Result 解析后的整合结果
|
||||
type Result struct {
|
||||
Mode string `json:"mode"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
Clusters int `json:"clusters_found,omitempty"`
|
||||
Noise int `json:"noise_points,omitempty"`
|
||||
DecayRates map[string]float64 `json:"decay_rates,omitempty"`
|
||||
Quality *QualityResult `json:"quality,omitempty"`
|
||||
QualityScore float64 `json:"quality_score,omitempty"` // 直接从 sidecar 的 ConsolidationReport 读取
|
||||
Mode string `json:"mode"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
Clusters int `json:"clusters_found,omitempty"`
|
||||
Noise int `json:"noise_points,omitempty"`
|
||||
DecayRates map[string]float64 `json:"decay_rates,omitempty"`
|
||||
Quality *QualityResult `json:"quality,omitempty"`
|
||||
QualityScore float64 `json:"quality_score,omitempty"` // 直接从 sidecar 的 ConsolidationReport 读取
|
||||
}
|
||||
|
||||
type QualityResult struct {
|
||||
@@ -71,17 +71,17 @@ func Run(dataDir, sqlitePath, mode string) (*Result, error) {
|
||||
Task: mode,
|
||||
LanceDBPath: dataDir,
|
||||
SQLitePath: sqlitePath,
|
||||
LLMEndpoint: os.Getenv("LLM_ENDPOINT"),
|
||||
LLMModel: os.Getenv("LLM_MODEL"),
|
||||
LLMApiKey: os.Getenv("LLM_API_KEY"),
|
||||
Epsilon: 0.4,
|
||||
LLMEndpoint: os.Getenv("LLM_ENDPOINT"),
|
||||
LLMModel: os.Getenv("LLM_MODEL"),
|
||||
LLMApiKey: os.Getenv("LLM_API_KEY"),
|
||||
Epsilon: 0.4,
|
||||
// 距离分布(2026-09-05 实测,bge-m3 归一化向量 norm=1.0):
|
||||
// 全量3000抽样: p25=0.24 p50=0.75 p95=0.82
|
||||
// sidecar 零向量 top-10000 样本: p5=0.09 p50=0.42 p95=1.0
|
||||
// eps=1.0(旧值,按未归一化 p50=1.029 调的)→ 归一化空间过大 → clusters=1 聚类失效
|
||||
// eps=0.3~0.5 → 有语义簇(能发现重复状态噪音簇: 1045条"主profile状态同步"/961条"CBM状态")
|
||||
// eps=0.4 = 平衡(2026-09-05 修复,commit 待推)
|
||||
MinPoints: 3,
|
||||
MinPoints: 3,
|
||||
ModelDir: os.Getenv("BGE_MODEL_DIR"),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -10,13 +10,13 @@ import (
|
||||
// 目标:为每条事实生成紧凑结构化摘要,LLM 可读、无需解码器,
|
||||
// 召回时先扫索引定位相关事实,再读原文(索引层指向内容层)。
|
||||
type AAAKEntry struct {
|
||||
FactID string `json:"fact_id"` // 事实编号(F1, F2, ...)
|
||||
Primary string `json:"primary"` // 主实体(最重要实体,如人名/项目名)
|
||||
Entities []string `json:"entities"` // 全部相关实体
|
||||
Keywords []string `json:"keywords"` // 主题关键词(2-4 个)
|
||||
Quote string `json:"quote"` // 关键短语(截断 ≤40 字)
|
||||
Weight float64 `json:"weight"` // 权重(由 5D 分数综合)
|
||||
Kind string `json:"kind"` // 类型:fact / decision / action / question / conclusion
|
||||
FactID string `json:"fact_id"` // 事实编号(F1, F2, ...)
|
||||
Primary string `json:"primary"` // 主实体(最重要实体,如人名/项目名)
|
||||
Entities []string `json:"entities"` // 全部相关实体
|
||||
Keywords []string `json:"keywords"` // 主题关键词(2-4 个)
|
||||
Quote string `json:"quote"` // 关键短语(截断 ≤40 字)
|
||||
Weight float64 `json:"weight"` // 权重(由 5D 分数综合)
|
||||
Kind string `json:"kind"` // 类型:fact / decision / action / question / conclusion
|
||||
}
|
||||
|
||||
// buildAAAKIndex 为事实列表生成 AAAK 压缩索引
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
// 织忆 MemoryWeave — 准入三路分诊(确定性,零 LLM 调用)
|
||||
//
|
||||
// 背景与依据(2026-10-12,6 篇文献精读,方案见
|
||||
// mc/小怡/04-OBSIDIAN工具/04-02-方案设计/20261012-织忆记忆闸门-外部经验落地方案.md):
|
||||
//
|
||||
// · A-MAC (arXiv 2603.04549) 消融实验:确定性「内容类型先验」是**单点最强特征**
|
||||
// —— 去掉它 F1 从 0.583 掉到 0.476(ΔF1=−0.107),等于退回等权基线。
|
||||
// 而织忆此前的判据里确定性特征数为 0(只有 LLM 给的 overall 与 VD)。
|
||||
// · SAGE (arXiv 2605.30711):明确该走哪条路的直接闭式解决,只有「不确定带」才需谨慎;
|
||||
// 且单一固定阈值会随记忆库变密而**必然过度保守**。
|
||||
// · When to Forget (arXiv 2604.12007) 原文:"a system acting on Memory Worth should
|
||||
// not deprecate an uncertain memory" ⇒ **证据不足/拿不准的内容绝不弃用**。
|
||||
//
|
||||
// 于是把「一个 LLM 分数卡阈值」拆成三路:
|
||||
//
|
||||
// drop —— 无歧义的垃圾(确定性规则),可丢
|
||||
// keep —— 无歧义的高价值(确定性规则),放行且不看分数
|
||||
// observe —— 其余全部 ⇒ **不确定带,永不因 LLM 低分丢弃**
|
||||
//
|
||||
// 设计取舍(刻意保守):drop 只收「无歧义」项;像"纯疑问句""状态汇报"这类**需要判断**的
|
||||
// 内容不放进 drop,一律落 observe —— 文献明确警告过度保守的准入会丢掉长程推理必需的信息。
|
||||
// 代价不对称:keep/observe 多留是可逆的(后续消费/降权会淘汰),drop 丢掉不可逆。
|
||||
package distill
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/xiaoxue/memoryweave/internal/textfilter"
|
||||
)
|
||||
|
||||
// AdmissionLane 准入分诊结果。
|
||||
type AdmissionLane string
|
||||
|
||||
const (
|
||||
LaneKeep AdmissionLane = "keep" // 确定性高价值 → 放行,不看分数
|
||||
LaneDrop AdmissionLane = "drop" // 确定性垃圾 → 丢弃
|
||||
LaneObserve AdmissionLane = "observe" // 不确定带 → 永不因分数丢弃
|
||||
)
|
||||
|
||||
// decisionPatterns 明确决策/承诺/长期约束的确定性线索。
|
||||
// 注意:命中即→keep,属**过度保留**方向,代价可逆,故可以放宽。
|
||||
var decisionPatterns = []string{
|
||||
"决定", "约定", "拍板", "定下来", "改成", "以后都", "以后不", "不再", "必须",
|
||||
"记住", "长期", "持续", "永久", "禁止", "不要", "别忘",
|
||||
}
|
||||
|
||||
// identityPreferencePatterns 身份/偏好/长期事实的确定性线索。
|
||||
var identityPreferencePatterns = []string{
|
||||
"我喜欢", "我不喜欢", "我的", "我是", "我在", "我负责", "我住", "平时",
|
||||
"习惯", "一直", "负责", "工作", "设备",
|
||||
}
|
||||
|
||||
// ClassifyAdmission 对一条待蒸馏内容做确定性三路分诊。
|
||||
// 返回 lane 与人类可读的 reason(进日志,用于事后审计分诊是否合理)。
|
||||
// 本函数**零 LLM 调用**、纯确定性 —— 同一输入永远同一结果,可单测。
|
||||
func ClassifyAdmission(content string) (AdmissionLane, string) {
|
||||
s := strings.TrimSpace(content)
|
||||
if s == "" {
|
||||
return LaneDrop, "空内容"
|
||||
}
|
||||
|
||||
// ── drop:无歧义的垃圾 ──────────────────────────────────
|
||||
if textfilter.IsSystemTemplate(s) {
|
||||
return LaneDrop, "系统样板/回执"
|
||||
}
|
||||
if utf8.RuneCountInString(s) < 8 {
|
||||
return LaneDrop, "过短无内容(<8字)"
|
||||
}
|
||||
|
||||
// ── keep:无歧义的高价值 ────────────────────────────────
|
||||
// 结构化人侧短事实(画像规则:/心迹:/牵挂:/偏好:/习惯:/目标: 等 daemon 侧固化前缀)
|
||||
if isSoulfulShortFact(s) {
|
||||
return LaneKeep, "结构化人侧事实前缀"
|
||||
}
|
||||
// 长度护栏:超长内容里的关键词不构成"这是在陈述一条决策"(长文里出现"决定"
|
||||
// 多半是在**汇报/转述**,不是本条内容本身是一条决策)→ 落 observe。
|
||||
n := utf8.RuneCountInString(s)
|
||||
if n >= 8 && n <= 150 {
|
||||
for _, p := range decisionPatterns {
|
||||
if strings.Contains(s, p) {
|
||||
return LaneKeep, "决策/承诺线索:" + p
|
||||
}
|
||||
}
|
||||
for _, p := range identityPreferencePatterns {
|
||||
if strings.Contains(s, p) {
|
||||
return LaneKeep, "身份/偏好线索:" + p
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── observe:拿不准的一律留 ─────────────────────────────
|
||||
return LaneObserve, "不确定带"
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package distill
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestClassifyAdmission 锁定三路分诊的判据(Step 1 验收线)。
|
||||
//
|
||||
// 关键不变量(对应外部经验):
|
||||
// 1. 系统样板/回执 → drop(确定性垃圾)
|
||||
// 2. 结构化人侧事实前缀 / 决策承诺 / 身份偏好 → keep
|
||||
// 3. **拿不准的一律 observe** —— 尤其"纯疑问""状态汇报"这类需要判断的内容
|
||||
// 绝不能被 drop(When to Forget 原文:"should not deprecate an uncertain memory")
|
||||
func TestClassifyAdmission(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
content string
|
||||
want AdmissionLane
|
||||
}{
|
||||
// ── drop:无歧义垃圾 ──
|
||||
{"系统样板-cron", "[IMPORTANT: You are running as a scheduled cron job. No user is present.", LaneDrop},
|
||||
{"系统样板-心跳", "[HEARTBEAT] tick 2026-10-12T02:00", LaneDrop},
|
||||
{"系统样板-内部通知", "[INTERNAL NOTIFICATION — generated by the Hermes gateway, not written by a human user.]", LaneDrop},
|
||||
{"系统样板-子代理回执", "[ASYNC DELEGATION COMPLETE — deleg_79e1d4ad] A background subagent finished.", LaneDrop},
|
||||
{"系统样板-上下文摘要", "## Context Compaction — REFERENCE ONLY\nEarlier turns were compacted.", LaneDrop},
|
||||
{"空内容", " ", LaneDrop},
|
||||
{"过短", "好的", LaneDrop},
|
||||
|
||||
// ── keep:确定性高价值 ──
|
||||
{"结构化前缀-偏好", "偏好: 牧尘喜欢结论先行、不要废话科普", LaneKeep},
|
||||
{"结构化前缀-心迹", "心迹: 今天有点累", LaneKeep},
|
||||
{"决策承诺", "牧尘决定以后都用分身C来处理采集类任务。", LaneKeep},
|
||||
{"长期约束", "这条以后不要再问了,记住已经拍板。", LaneKeep},
|
||||
{"身份偏好", "我喜欢马哲经济学和剪辑,设备是小米笔记本。", LaneKeep},
|
||||
|
||||
// ── observe:拿不准的一律留(关键:不能被 drop)──
|
||||
{"纯疑问", "这个闸门到底应该怎么定阈值才合理呢?", LaneObserve},
|
||||
{"状态汇报", "服务已重启完成,当前版本 v0.21.6。", LaneObserve},
|
||||
{"普通对话", "今天把织忆的评分链路排查了一遍,顺手补了记分牌的一项指标。", LaneObserve},
|
||||
|
||||
// ── 边界:超长内容里的关键词不构成"陈述了一条决策" ──
|
||||
{"超长含关键词", "决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定决定", LaneObserve},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
got, why := ClassifyAdmission(c.content)
|
||||
if got != c.want {
|
||||
t.Fatalf("ClassifyAdmission() lane=%q reason=%q, 期望 lane=%q\n内容: %.60s", got, why, c.want, c.content)
|
||||
}
|
||||
if why == "" {
|
||||
t.Fatal("reason 不得为空(用于事后审计分诊是否合理)")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestClassifyAdmissionDeterministic 同一输入必须永远同一结果(确定性 = 可审计的前提)。
|
||||
func TestClassifyAdmissionDeterministic(t *testing.T) {
|
||||
for _, s := range []string{
|
||||
"牧尘决定以后都用分身C。",
|
||||
"服务已重启完成。",
|
||||
"[HEARTBEAT] tick",
|
||||
} {
|
||||
first, fr := ClassifyAdmission(s)
|
||||
for i := 0; i < 5; i++ {
|
||||
got, gr := ClassifyAdmission(s)
|
||||
if got != first || gr != fr {
|
||||
t.Fatalf("非确定性:%q 第一次 %s/%s,第 %d 次 %s/%s", s, first, fr, i+2, got, gr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -147,7 +147,7 @@ type MemoryCandidate struct {
|
||||
|
||||
// ConsolidateInput 整合输入
|
||||
type ConsolidateInput struct {
|
||||
Target MemoryCandidate
|
||||
Target MemoryCandidate
|
||||
Candidates []MemoryCandidate
|
||||
}
|
||||
|
||||
|
||||
@@ -13,9 +13,9 @@ type CostController struct {
|
||||
mu sync.Mutex
|
||||
|
||||
// 每日限额
|
||||
dailyLimit int
|
||||
dailyUsed int
|
||||
lastReset time.Time
|
||||
dailyLimit int
|
||||
dailyUsed int
|
||||
lastReset time.Time
|
||||
|
||||
// 紧急蒸馏豁免计数
|
||||
urgentUsed int
|
||||
@@ -117,12 +117,12 @@ func (cc *CostController) checkReset() {
|
||||
|
||||
// DistillQuota 蒸馏配额
|
||||
type DistillQuota struct {
|
||||
Remaining int `json:"remaining"`
|
||||
Used int `json:"used"`
|
||||
Limit int `json:"limit"`
|
||||
Percent float64 `json:"percent"`
|
||||
NearLimit bool `json:"near_limit"`
|
||||
Status string `json:"status"` // normal / near / exceeded
|
||||
Remaining int `json:"remaining"`
|
||||
Used int `json:"used"`
|
||||
Limit int `json:"limit"`
|
||||
Percent float64 `json:"percent"`
|
||||
NearLimit bool `json:"near_limit"`
|
||||
Status string `json:"status"` // normal / near / exceeded
|
||||
}
|
||||
|
||||
// GetQuota 获取当前配额状态
|
||||
@@ -151,8 +151,8 @@ func (cc *CostController) GetQuota() DistillQuota {
|
||||
// ─── 限额配置 ─────────────────────────────────────────────
|
||||
|
||||
type CostConfig struct {
|
||||
DailyLimit int `json:"daily_limit"` // 默认 5000
|
||||
ConsolidationBudget int `json:"consolidation_budget"` // 默认 20
|
||||
DailyLimit int `json:"daily_limit"` // 默认 5000
|
||||
ConsolidationBudget int `json:"consolidation_budget"` // 默认 20
|
||||
}
|
||||
|
||||
// DefaultCostConfig 默认配置
|
||||
|
||||
@@ -14,6 +14,7 @@ package distill
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -208,6 +209,21 @@ func parseJSONFormat(raw string) (LLMResponse, bool) {
|
||||
// 第一级行协议优先(LLM 被训练成行协议输出);若未识别到任何行(如模型仍产 JSON),
|
||||
// 落到第二级旧 JSON 兜底;两级都空则返回空 LLMResponse(最差空切片,不返回 error)。
|
||||
// 这样上游 distillOne 对「解析不出」走启发式提取,而不是把整条 episode 当空结果丢掉。
|
||||
//
|
||||
// 2026-10-12:两条分支的返回值都过一遍 StripPromptEcho —— 实测 2B 会把 prompt
|
||||
// 「重要指令」块里的教学示例(「用户昨天买了苹果。」「Galway, Ireland」…)原样抄进输出,
|
||||
// 解析器照单全收 ⇒ 示例事实被质量门丢光(生产 facts=0 达 301 次)+ 示例实体污染图谱。
|
||||
// 此处是**唯一咽喉**(engine.go 只从此处取 LLM 结果),防线放这里两条路径都覆盖。
|
||||
// logEchoStrip 只在实际剔除时打一行(生产用它验证防线在跑,且能看到被剔的是不是示例)。
|
||||
func logEchoStrip(path string, st EchoStats) {
|
||||
if st.Total() == 0 {
|
||||
return
|
||||
}
|
||||
log.Printf("[distill][ECHO] path=%s 剔除教学示例回显 %d 项(facts=%d entities=%d other=%d allFactsEcho=%v)样例=%q",
|
||||
path, st.Total(), st.FactsDropped, st.EntitiesDropped, st.OtherDropped, st.AllFactsWereEcho,
|
||||
truncate(st.Sample, 40))
|
||||
}
|
||||
|
||||
func parseDistillOutput(raw string) LLMResponse {
|
||||
facts, entities, scores, ok := ParseLineFormat(raw)
|
||||
if ok {
|
||||
@@ -215,7 +231,7 @@ func parseDistillOutput(raw string) LLMResponse {
|
||||
for _, en := range entities {
|
||||
names = append(names, en.Name)
|
||||
}
|
||||
return LLMResponse{
|
||||
resp, st := StripPromptEcho(LLMResponse{
|
||||
IS: scores.IS,
|
||||
SU: scores.SU,
|
||||
PA: scores.PA,
|
||||
@@ -223,10 +239,15 @@ func parseDistillOutput(raw string) LLMResponse {
|
||||
RU: scores.RU,
|
||||
Facts: facts,
|
||||
Entities: names,
|
||||
}
|
||||
})
|
||||
logEchoStrip("line", st)
|
||||
return resp
|
||||
}
|
||||
if resp, ok2 := parseJSONFormat(raw); ok2 {
|
||||
var st EchoStats
|
||||
resp, st = StripPromptEcho(resp)
|
||||
logEchoStrip("json", st)
|
||||
return resp
|
||||
}
|
||||
return LLMResponse{}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,4 +194,4 @@ func TestDistillOutputFormat_DefaultLines(t *testing.T) {
|
||||
if got := distillOutputFormat(); strings.ToLower(got) != "lines" {
|
||||
t.Fatalf("默认 OutputFormat 应为 lines,实际=%s", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,260 @@
|
||||
package distill
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// ─── 教学示例回显防线(2026-10-12)─────────────────────────────────────────
|
||||
//
|
||||
// 起因(生产实测):prompt 的「重要指令」块为了让 2B 理解格式而写的**示例取值**,
|
||||
// 被模型整段照抄进输出。解析器无法分辨「示例」与「真抽取」,于是照单全收:
|
||||
//
|
||||
// · 抄来的示例事实(如「用户昨天买了苹果。」= 9 字)< 20 字 → 被质量门丢光
|
||||
// ⇒ 生产日志 `facts=0` 出现 301 次(约 76% 的 episode 产出为零)
|
||||
// · 抄来的示例实体(The Name of the Wind by Patrick Rothfuss / Galway, Ireland /
|
||||
// 慈善篮球赛 / 某饮料公司 …)会被当真实实体写进记忆图谱 ⇒ 垃圾节点
|
||||
// · 抄来的示例评分行(若模型把示例数值一并抄出)
|
||||
//
|
||||
// ⚠️ 不要把「评分恒为 0.5」也算到这个账上:现 prompt 里没有 0.5 的示例,
|
||||
// 而 IsDegenerateScore 的实测说明模型**本就会**合法地给出五维同值(0.8×5 / 0.0×5)。
|
||||
// 0.5×5 的成因未证实,不归因(2026-10-12 自查撤回)。
|
||||
//
|
||||
// 判据:**逐字相同**(归一化后)才算回显;对纯教学 illustration 额外允许
|
||||
// 「≥4 字的子串」命中(用于抓 John / Patrick Rothfuss 这类被拆碎的残片)。
|
||||
//
|
||||
// 为什么不一律用子串:格式说明类字面量里含**真实 token**(如「牵挂: … 待办.md …」),
|
||||
// 用子串会把真实实体「待办.md」误杀 → 故这类只认整体逐字相同(allowSubstr=false)。
|
||||
//
|
||||
// 代价不对称:漏掉一条回显 = 永久污染记忆;误杀一条恰好像示例的短事实 = 损失一条低信息事实。
|
||||
type echoLiteral struct {
|
||||
text string
|
||||
allowSubstr bool // true=纯教学 illustration,≥4 字子串也算回显
|
||||
}
|
||||
|
||||
var promptEchoLiterals = []echoLiteral{
|
||||
// —— 事实改写示例(prompt「2. 进行轻量上下文补全」)——
|
||||
{"用户今早喝了咖啡", true},
|
||||
{"用户昨天买了苹果。", true},
|
||||
{"用户的朋友John在学医。", true},
|
||||
{"你好", false},
|
||||
{"哈哈", false},
|
||||
{"谢谢", false},
|
||||
// —— 「3. 保留所有具体实体和细节」的 illustration:纯教学,允许子串命中 ——
|
||||
{"The Name of the Wind by Patrick Rothfuss", true},
|
||||
{"一本书", false},
|
||||
{"Galway, Ireland", true},
|
||||
{"北京海淀区", true},
|
||||
{"慈善篮球赛", true},
|
||||
{"留学项目", true},
|
||||
{"某饮料公司", true},
|
||||
{"4年前", false},
|
||||
{"下个月", false},
|
||||
{"上周", false},
|
||||
// —— 结构化前缀示例:含真实 token(待办.md)→ 只认整体逐字相同 ——
|
||||
{"牵挂: 待处理: 待办.md 里已打勾的旧条目清理", false},
|
||||
// —— 输出格式占位符(模型常把这些当内容抄出来)——
|
||||
{"独立事实1", false},
|
||||
{"独立事实2", false},
|
||||
{"决定1", false},
|
||||
{"结论1", false},
|
||||
{"行动1", false},
|
||||
{"问题1", false},
|
||||
{"entity1", false},
|
||||
{"entity2", false},
|
||||
{"实体名", false},
|
||||
{"实体类型", false},
|
||||
{"五个数字用竖线分隔", false},
|
||||
}
|
||||
|
||||
// normalizeEcho 归一化:去首尾空白 + 去尾部句读(模型常省/加句号)。
|
||||
func normalizeEcho(s string) string {
|
||||
s = strings.TrimSpace(s)
|
||||
return strings.TrimRight(s, "。..!!??~~、,, ")
|
||||
}
|
||||
|
||||
// IsPromptEcho 判断一条事实/实体名是否只是 prompt 教学示例的回显。
|
||||
func IsPromptEcho(s string) bool {
|
||||
n := normalizeEcho(s)
|
||||
if n == "" {
|
||||
return false
|
||||
}
|
||||
nr := utf8.RuneCountInString(n)
|
||||
for _, l := range promptEchoLiterals {
|
||||
ln := normalizeEcho(l.text)
|
||||
if ln == "" {
|
||||
continue
|
||||
}
|
||||
if n == ln {
|
||||
return true
|
||||
}
|
||||
if l.allowSubstr && nr >= 4 && strings.Contains(ln, n) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ─── 概念/实体提取路径([graph/notes] 兜底拆实体)──────────────────────────────
|
||||
//
|
||||
// 背景(GA-P0-5):蒸馏侧 IsPromptEcho 只罩 LLM 输出的 facts/entities,图谱污染闭环
|
||||
// 还有另一条腿 —— 审计笔记把污染词当证据列出来 → 笔记被蒸馏成 episode → 调用方把整句
|
||||
// 提问原样传给 /api/v1/graph/notes → entity_index 从图谱节点名词表拆出候选实体 → 若
|
||||
// 候选只是 prompt 教学示例的回显残片,会被当真实实体写回图谱 ⇒ 已删除的污染节点
|
||||
// (n_galway / n_rothfuss / n_the_name_of_the_wind)被重建。
|
||||
//
|
||||
// 判据与 IsPromptEcho 完全同源(都读 promptEchoLiterals,清单本身零改动):逐字相等
|
||||
// (归一化后)或 illustration 类(allowSubstr=true)的 ≥4 字子串残片即算回显;占位符类
|
||||
// (allowSubstr=false)只认逐字相等。唯一差别是输入形态:图谱节点名是「n_the_name_of_the_wind」
|
||||
// 这种规整形,需先还原成可读文本(去 n_、下划线→空格、小写)再比对。
|
||||
//
|
||||
// GA-P0-6 补两层判据(都从 9 条 allowSubstr=true 的 illustration 派生,清单本身零改动):
|
||||
// 1. 标点/空格不敏感 —— 实体侧 `galway ireland` 与字面量 `Galway, Ireland` 因内部逗号
|
||||
// 差异既不等也不构成子串 ⇒ 比对前两侧都剥成「只留字母数字汉字」,等价即拦。
|
||||
// 这正是图谱节点名最常见的形态(该家族第 1 轮漏删也栽在这个逗号上)。
|
||||
// 2. 多示例词并列 —— 审计散文被蒸馏/拆实体后产出形如「A_B_示例词1_示例词2_…」的节点。
|
||||
// 候选若包含整条 illustration 字面量(归一化后),或含 ≥2 个 illustration 显著 token
|
||||
// (promptEchoTokens),判回显;单 token(只出现 1 个)不拦,保留给真实讨论。
|
||||
//
|
||||
// 与 IsPromptEcho 一样**只拦「逐字相等/残片」级**,不拦普通包含 —— 例如「用户提到
|
||||
// galway」里 galway 若是真实实体,只要它不在某条示例字面量的残片里就放行;误杀面与
|
||||
// 蒸馏侧一致(那里已实测会杀 John / Patrick Rothfuss 这类单字残片)。
|
||||
func IsPromptEchoEntity(s string) bool {
|
||||
n := normalizeEchoEntity(s)
|
||||
if n == "" {
|
||||
return false
|
||||
}
|
||||
nk := compareKey(strings.ToLower(n))
|
||||
nr := utf8.RuneCountInString(n)
|
||||
|
||||
// 判据二:≥2 个 illustration 显著 token 并列 ⇒ 示例词被拼进同一节点名。
|
||||
if tokenEchoCount(nk) >= 2 {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, l := range promptEchoLiterals {
|
||||
ln := strings.ToLower(normalizeEcho(l.text))
|
||||
if ln == "" {
|
||||
continue
|
||||
}
|
||||
lk := compareKey(ln)
|
||||
if nk == lk {
|
||||
return true
|
||||
}
|
||||
if l.allowSubstr && nr >= 4 && strings.Contains(lk, nk) && tokenEchoCount(nk) >= 1 {
|
||||
return true
|
||||
}
|
||||
// 判据二:候选包含整条 illustration 字面量 ⇒ 示例词被拼进长节点名。
|
||||
if l.allowSubstr && strings.Contains(nk, lk) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// normalizeEchoEntity 把图谱节点名归一成可与字面量比对的形态。
|
||||
func normalizeEchoEntity(s string) string {
|
||||
s = strings.TrimPrefix(s, "n_")
|
||||
s = strings.ReplaceAll(s, "_", " ")
|
||||
s = strings.ToLower(s)
|
||||
return normalizeEcho(s)
|
||||
}
|
||||
|
||||
// promptEchoTokens:从 9 条 allowSubstr=true 的 illustration 里提取的显著 token
|
||||
// (地名/人名/书名/活动名/公司名)。候选节点名若包含 ≥2 个 ⇒ 判为多示例词并列的回显。
|
||||
// 单 token(只出现 1 个)不拦 —— 如「northern ireland」只含 ireland,保留给真实讨论。
|
||||
var promptEchoTokens = []string{
|
||||
"john", // 人名(用户的朋友John在学医。)
|
||||
"rothfuss", // 人名(The Name of the Wind by Patrick Rothfuss)
|
||||
"the name of the wind", // 书名
|
||||
"galway", // 地名
|
||||
"ireland", // 地区
|
||||
"北京海淀区", // 地名
|
||||
"慈善篮球赛", // 活动名
|
||||
"留学项目", // 活动名
|
||||
"某饮料公司", // 公司名
|
||||
}
|
||||
|
||||
// tokenEchoCount 统计 key 命中的 illustration 显著 token 数。
|
||||
func tokenEchoCount(key string) int {
|
||||
c := 0
|
||||
for _, t := range promptEchoTokens {
|
||||
if strings.Contains(key, compareKey(strings.ToLower(t))) {
|
||||
c++
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// compareKey 把字符串压成「只留字母数字汉字」的形态(去掉所有标点/空白),
|
||||
// 用于标点/空格不敏感比对:`galway ireland` 与 `galway, ireland` 视为相同。
|
||||
func compareKey(s string) string {
|
||||
var b strings.Builder
|
||||
for _, r := range s {
|
||||
if unicode.IsLetter(r) || unicode.IsDigit(r) {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// EchoStats 记录本次剔除量,供日志观测。
|
||||
type EchoStats struct {
|
||||
FactsDropped int
|
||||
EntitiesDropped int
|
||||
OtherDropped int
|
||||
AllFactsWereEcho bool // 全部事实都是回显 ⇒ 本次 LLM 输出对事实部分无效
|
||||
Sample string // 第一条被剔除的原文(生产日志据此确认"是不是示例")
|
||||
}
|
||||
|
||||
// Total 返回被剔除的条目总数。
|
||||
func (s EchoStats) Total() int { return s.FactsDropped + s.EntitiesDropped + s.OtherDropped }
|
||||
|
||||
// StripPromptEcho 剔除回显项。
|
||||
//
|
||||
// 若事实**全部**来自回显 ⇒ 连评分一起作废(评分行与事实同源,都是抄来的),
|
||||
// 并清空其余字段,返回空 LLMResponse,让上游落到「从原文启发式提取」而不是
|
||||
// 把示例当产出入库。这是**结构化降级**:系统仍产出东西,但不产出垃圾。
|
||||
func StripPromptEcho(resp LLMResponse) (LLMResponse, EchoStats) {
|
||||
var st EchoStats
|
||||
|
||||
keep := func(in []string) []string {
|
||||
if len(in) == 0 {
|
||||
return in
|
||||
}
|
||||
out := make([]string, 0, len(in))
|
||||
for _, s := range in {
|
||||
if IsPromptEcho(s) {
|
||||
if st.Sample == "" {
|
||||
st.Sample = s
|
||||
}
|
||||
continue
|
||||
}
|
||||
out = append(out, s)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
beforeFacts := len(resp.Facts)
|
||||
resp.Facts = keep(resp.Facts)
|
||||
st.FactsDropped = beforeFacts - len(resp.Facts)
|
||||
|
||||
beforeEnt := len(resp.Entities)
|
||||
resp.Entities = keep(resp.Entities)
|
||||
st.EntitiesDropped = beforeEnt - len(resp.Entities)
|
||||
|
||||
beforeOther := len(resp.Decisions) + len(resp.Conclusions) + len(resp.ActionsTaken) + len(resp.OpenQuestions)
|
||||
resp.Decisions = keep(resp.Decisions)
|
||||
resp.Conclusions = keep(resp.Conclusions)
|
||||
resp.ActionsTaken = keep(resp.ActionsTaken)
|
||||
resp.OpenQuestions = keep(resp.OpenQuestions)
|
||||
st.OtherDropped = beforeOther - (len(resp.Decisions) + len(resp.Conclusions) + len(resp.ActionsTaken) + len(resp.OpenQuestions))
|
||||
|
||||
if beforeFacts > 0 && len(resp.Facts) == 0 {
|
||||
st.AllFactsWereEcho = true
|
||||
// 评分与事实同源 ⇒ 一起作废(避免把示例评分当有效判断喂给分道逻辑)
|
||||
return LLMResponse{}, st
|
||||
}
|
||||
return resp, st
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
//go:build liveprobe
|
||||
|
||||
package distill
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestEchoLiveProbe 直连真模型,验证「教学示例回显防线」在**真实输出**上生效。
|
||||
//
|
||||
// 单测喂的是我构造的文本;本探针喂的是模型**真的吐出来的东西**——
|
||||
// 这才是「防线在生产路径上起作用」的证据(判据:模型真输出里出现了示例字面量,
|
||||
// 且经 parseDistillOutput 后不再出现在 facts/entities 里)。
|
||||
//
|
||||
// go test ./internal/distill/ -tags liveprobe -run TestEchoLiveProbe -v -count=1
|
||||
func TestEchoLiveProbe(t *testing.T) {
|
||||
tmpl := extractPromptTemplate(t)
|
||||
client := &http.Client{Timeout: 180 * time.Second}
|
||||
|
||||
cases := []struct{ name, content string }{
|
||||
{"A-易诱发回显(苹果/John)", "[2026-10-12, 周一] 牧尘: 昨天买了苹果\n[2026-10-12, 周一] 牧尘: 我朋友John在学医"},
|
||||
{"B-真实工作规则", "[2026-10-12, 周一] 牧尘: 全绿不代表能用,要实际能用,满足使用\n[2026-10-12, 周一] 小怡: 记下了,判据要覆盖真实路径而不是只看配置"},
|
||||
{"C-纯寒暄(应无事实)", "[2026-10-12, 周一] 牧尘: 你好\n[2026-10-12, 周一] 小怡: 哈哈\n[2026-10-12, 周一] 牧尘: 谢谢"},
|
||||
{"D-长文多实体", "[2026-10-12, 周一] 牧尘: 我在淇县负责生物制造产业专班和卫都街道,设备是小米笔记本,平时喜欢马哲经济学和剪辑。"},
|
||||
}
|
||||
|
||||
stripTotal, echoSeen := 0, 0
|
||||
for _, c := range cases {
|
||||
raw := callRaw(t, client, fmt.Sprintf(tmpl, c.content))
|
||||
|
||||
// ① 未经防线:模型原始输出解析出什么
|
||||
facts, ents, sc, ok := ParseLineFormat(raw)
|
||||
var names []string
|
||||
for _, e := range ents {
|
||||
names = append(names, e.Name)
|
||||
}
|
||||
rawEcho := 0
|
||||
for _, f := range facts {
|
||||
if IsPromptEcho(f) {
|
||||
rawEcho++
|
||||
}
|
||||
}
|
||||
for _, e := range names {
|
||||
if IsPromptEcho(e) {
|
||||
rawEcho++
|
||||
}
|
||||
}
|
||||
|
||||
// ② 经防线(= 生产路径)
|
||||
after, st := StripPromptEcho(LLMResponse{
|
||||
IS: sc.IS, SU: sc.SU, PA: sc.PA, VD: sc.VD, RU: sc.RU,
|
||||
Facts: facts, Entities: names,
|
||||
})
|
||||
stripTotal += st.Total()
|
||||
if rawEcho > 0 {
|
||||
echoSeen++
|
||||
}
|
||||
|
||||
t.Logf("\n══════ %s ══════", c.name)
|
||||
t.Logf("模型原始: %s", strings.ReplaceAll(strings.TrimSpace(raw), "\n", " ⏎ "))
|
||||
t.Logf("防线前 : facts=%v entities=%v ok=%v 其中回显=%d", facts, names, ok, rawEcho)
|
||||
t.Logf("防线后 : facts=%v entities=%v", after.Facts, after.Entities)
|
||||
t.Logf("剔除 : facts=%d entities=%d other=%d allEcho=%v 样例=%q",
|
||||
st.FactsDropped, st.EntitiesDropped, st.OtherDropped, st.AllFactsWereEcho, st.Sample)
|
||||
}
|
||||
|
||||
t.Logf("\n══════ 汇总:%d/%d 条真实输出含回显,共剔除 %d 项 ══════", echoSeen, len(cases), stripTotal)
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
package distill
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// ─── 漂移护栏:字面量清单必须仍与 prompt 同源 ───────────────────────────────
|
||||
//
|
||||
// 清单是「按 prompt 原文抄下来的」——prompt 一改,清单就过期(防线静默失效)。
|
||||
// 故逐条断言它仍出现在 engine.go 里。这条测试红了 = prompt 改过,去核对清单。
|
||||
func TestPromptEchoLiteralsExistInPromptSource(t *testing.T) {
|
||||
src, err := os.ReadFile("engine.go")
|
||||
if err != nil {
|
||||
t.Fatalf("读 engine.go 失败: %v", err)
|
||||
}
|
||||
s := string(src)
|
||||
missing := []string{}
|
||||
for _, l := range promptEchoLiterals {
|
||||
if !strings.Contains(s, l.text) {
|
||||
missing = append(missing, l.text)
|
||||
}
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
t.Fatalf("以下回显字面量已不在 engine.go 里(prompt 被改过?清单过期 ⇒ 防线静默失效):\n %s",
|
||||
strings.Join(missing, "\n "))
|
||||
}
|
||||
}
|
||||
|
||||
// ─── 判据本身 ─────────────────────────────────────────────────────────────
|
||||
|
||||
func TestIsPromptEcho_Exact(t *testing.T) {
|
||||
yes := []string{
|
||||
"用户昨天买了苹果。",
|
||||
"用户昨天买了苹果", // 归一化:缺句号同样算
|
||||
"The Name of the Wind by Patrick Rothfuss",
|
||||
"Galway, Ireland",
|
||||
"慈善篮球赛",
|
||||
"某饮料公司",
|
||||
"牵挂: 待处理: 待办.md 里已打勾的旧条目清理",
|
||||
"独立事实1",
|
||||
"实体名",
|
||||
}
|
||||
for _, s := range yes {
|
||||
if !IsPromptEcho(s) {
|
||||
t.Errorf("应判为回显但未判:%q", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 子串规则只对「纯教学 illustration」生效;格式说明类里含真实 token(待办.md)
|
||||
// ⇒ 必须只认整体逐字相同,否则会把真实实体「待办.md」误杀。
|
||||
func TestIsPromptEcho_SubstringOnlyForIllustration(t *testing.T) {
|
||||
if !IsPromptEcho("John") {
|
||||
t.Error("「John」是「用户的朋友John在学医。」里被拆出来的残片,应判为回显")
|
||||
}
|
||||
if !IsPromptEcho("Patrick Rothfuss") {
|
||||
t.Error("「Patrick Rothfuss」是示例书名的残片,应判为回显")
|
||||
}
|
||||
if IsPromptEcho("待办.md") {
|
||||
t.Error("「待办.md」是真实实体,绝不能因为出现在格式说明示例里就被杀")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPromptEcho_RealFactsNotDropped(t *testing.T) {
|
||||
no := []string{
|
||||
"用户上周去了北京并签了合同", // 含「上周」但不是逐字相同
|
||||
"小怡把生产缓存切到 Redis L2 并设了 7 天 TTL",
|
||||
"这份报告的第 4 年前的数据无法核对",
|
||||
"",
|
||||
}
|
||||
for _, s := range no {
|
||||
if IsPromptEcho(s) {
|
||||
t.Errorf("真实事实被误杀:%q", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── 剔除逻辑 ─────────────────────────────────────────────────────────────
|
||||
|
||||
func TestStripPromptEcho_PartialKeepsReal(t *testing.T) {
|
||||
in := LLMResponse{
|
||||
IS: 0.6, SU: 0.5, PA: 0.4, VD: 0.3, RU: 0.2,
|
||||
Facts: []string{"用户昨天买了苹果。", "小怡把生产缓存切到 Redis L2 并设了 7 天 TTL"},
|
||||
Entities: []string{"Galway, Ireland", "Redis"},
|
||||
}
|
||||
out, st := StripPromptEcho(in)
|
||||
if st.FactsDropped != 1 || st.EntitiesDropped != 1 {
|
||||
t.Fatalf("剔除计数不对: %+v", st)
|
||||
}
|
||||
if len(out.Facts) != 1 || out.Facts[0] != "小怡把生产缓存切到 Redis L2 并设了 7 天 TTL" {
|
||||
t.Fatalf("真实事实应保留: %v", out.Facts)
|
||||
}
|
||||
if len(out.Entities) != 1 || out.Entities[0] != "Redis" {
|
||||
t.Fatalf("真实实体应保留: %v", out.Entities)
|
||||
}
|
||||
if out.IS != 0.6 || out.RU != 0.2 {
|
||||
t.Fatalf("部分回显不应作废评分: %+v", out)
|
||||
}
|
||||
if st.AllFactsWereEcho {
|
||||
t.Fatal("还有真实事实,不应标记 AllFactsWereEcho")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripPromptEcho_AllEchoVoidsEverything(t *testing.T) {
|
||||
in := LLMResponse{
|
||||
IS: 0.5, SU: 0.5, PA: 0.5, VD: 0.5, RU: 0.5,
|
||||
Facts: []string{"用户昨天买了苹果。", "用户的朋友John在学医。"},
|
||||
Entities: []string{"The Name of the Wind by Patrick Rothfuss", "北京海淀区"},
|
||||
}
|
||||
out, st := StripPromptEcho(in)
|
||||
if !st.AllFactsWereEcho {
|
||||
t.Fatal("全是回显,应标记 AllFactsWereEcho")
|
||||
}
|
||||
if len(out.Facts) != 0 || len(out.Entities) != 0 {
|
||||
t.Fatalf("应清空: %+v", out)
|
||||
}
|
||||
// 评分与事实同源 ⇒ 一起作废(不能把示例评分当有效判断)
|
||||
if out.IS != 0 || out.SU != 0 || out.PA != 0 || out.VD != 0 || out.RU != 0 {
|
||||
t.Fatalf("评分应作废: %+v", out)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── 端到端:走唯一咽喉 ───────────────────────────────────────────────────
|
||||
|
||||
func TestParseDistillOutput_FiltersEchoEndToEnd(t *testing.T) {
|
||||
raw := "FACT|用户昨天买了苹果。\n" +
|
||||
"FACT|用户的朋友John在学医。\n" +
|
||||
"ENTITY|Galway, Ireland|location\n" +
|
||||
"SCORE|0.5|0.5|0.5|0.5|0.5\nEND"
|
||||
got := parseDistillOutput(raw)
|
||||
if len(got.Facts) != 0 || len(got.Entities) != 0 {
|
||||
t.Fatalf("整段回显应被清空: %+v", got)
|
||||
}
|
||||
if got.IS != 0 || got.RU != 0 {
|
||||
t.Fatalf("回显评分应作废: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDistillOutput_MixedKeepsReal(t *testing.T) {
|
||||
raw := "FACT|用户昨天买了苹果。\n" +
|
||||
"FACT|小怡把生产缓存切到 Redis L2 并设了 7 天 TTL\n" +
|
||||
"ENTITY|Redis|tool\n" +
|
||||
"SCORE|0.6|0.5|0.4|0.3|0.2\nEND"
|
||||
got := parseDistillOutput(raw)
|
||||
if len(got.Facts) != 1 || !strings.Contains(got.Facts[0], "Redis L2") {
|
||||
t.Fatalf("真实事实应保留: %+v", got.Facts)
|
||||
}
|
||||
if len(got.Entities) != 1 || got.Entities[0] != "Redis" {
|
||||
t.Fatalf("真实实体应保留: %+v", got.Entities)
|
||||
}
|
||||
if got.IS != 0.6 || got.RU != 0.2 {
|
||||
t.Fatalf("评分应保留: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── 概念/实体提取路径(GA-P0-5)────────────────────────────────────────────
|
||||
//
|
||||
// 图谱污染闭环第二腿:审计笔记把污染词当证据列出来 → 蒸馏成 episode → 兜底拆实体时
|
||||
// 已删除的污染节点(n_galway / n_rothfuss / n_the_name_of_the_wind)被重建。
|
||||
// IsPromptEchoEntity 把图谱节点名(规整形)归一后,按与 IsPromptEcho 同源判据拦截。
|
||||
|
||||
func TestIsPromptEchoEntity_RevivedNodesDropped(t *testing.T) {
|
||||
// AC2:3 个复活节点(归一化形,含 n_ 前缀 / 下划线两种形态)经提取路径被 DROP。
|
||||
revived := []string{
|
||||
"n_galway", "galway",
|
||||
"n_rothfuss", "rothfuss",
|
||||
"n_the_name_of_the_wind", "the_name_of_the_wind",
|
||||
"The Name of the Wind", // 人类可读残片
|
||||
}
|
||||
for _, s := range revived {
|
||||
if !IsPromptEchoEntity(s) {
|
||||
t.Errorf("污染节点应被 ECHO-DROP:%q", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPromptEchoEntity_ExactMatchStillWorks(t *testing.T) {
|
||||
// 逐字相等(归一化后)对实体路径同样生效 —— 与 IsPromptEcho 同源。
|
||||
yes := []string{"用户昨天买了苹果。", "用户昨天买了苹果", "Galway, Ireland", "慈善篮球赛", "独立事实1"}
|
||||
for _, s := range yes {
|
||||
if !IsPromptEchoEntity(s) {
|
||||
t.Errorf("应判为回显但未判:%q", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPromptEchoEntity_RealEntitiesKept(t *testing.T) {
|
||||
// AC3 正例:真实实体绝不能被误杀(≥5 个)。
|
||||
real := []string{
|
||||
"小怡", "牧尘", "招商台账", "Redis", "织忆", "待办.md",
|
||||
"用户上周去了北京并签了合同",
|
||||
"小怡把生产缓存切到 Redis L2 并设了 7 天 TTL",
|
||||
}
|
||||
for _, s := range real {
|
||||
if IsPromptEchoEntity(s) {
|
||||
t.Errorf("真实实体被误杀:%q", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPromptEchoEntity_PlaceholderNotSubstring(t *testing.T) {
|
||||
// 占位符类(allowSubstr=false)只认逐字相等:残片「待办」不该被「待办.md」示例杀。
|
||||
no := []string{"待办", "实体名实体", "独立"}
|
||||
for _, s := range no {
|
||||
if IsPromptEchoEntity(s) {
|
||||
t.Errorf("占位符类字面量不应做子串拦截,%q 被误杀", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── 漂移护栏:3 个复活节点必须永远被拦 ──────────────────────────────────────
|
||||
// 若 prompt 或字面量清单改动导致 galway/rothfuss/the_name_of_the_wind 漏网,
|
||||
// 图谱污染闭环会静默复发 —— 这条红 = 清单过期,去核对 echo_guard.go。
|
||||
func TestIsPromptEchoEntity_DriftGuardRevivedNodes(t *testing.T) {
|
||||
mustCatch := []string{"n_galway", "n_rothfuss", "n_the_name_of_the_wind"}
|
||||
for _, s := range mustCatch {
|
||||
if !IsPromptEchoEntity(s) {
|
||||
t.Fatalf("复活节点 %q 已漏网(清单过期/判据被改)⇒ 污染闭环复发", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── GA-P0-6 返修 ─────────────────────────────────────────────────────────
|
||||
//
|
||||
// 问题 1(真 bug):下划线/连字符连接的「地名+地区」形态。实体侧归一得 `galway ireland`
|
||||
// (空格),字面量 `Galway, Ireland` 内部逗号被保留得 `galway, ireland` ⇒ 既不等也不构成
|
||||
// 子串而被放行。这恰是图谱节点名最常见的形态 ⇒ 改为标点/空格不敏感后应被拦。
|
||||
func TestIsPromptEchoEntity_PunctInsensitiveUnderlineVariant(t *testing.T) {
|
||||
blocked := []string{
|
||||
"n_galway_ireland", // 问题 1 主用例:下划线形「地名+地区」
|
||||
"galway_ireland", // 去 n_ 前缀同形
|
||||
"n_galway-ireland", // 连字符形
|
||||
}
|
||||
for _, s := range blocked {
|
||||
if !IsPromptEchoEntity(s) {
|
||||
t.Errorf("下划线/连字符连接的「地名+地区」回显应被拦:%q", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 问题 2(补判据):≥2 个 illustration 显著 token 并列拼进同一个节点名
|
||||
// (形如 A_B_示例词1_示例词2_…)被放行 ⇒ 补拦。
|
||||
func TestIsPromptEchoEntity_MultiTokenJuxtaposed(t *testing.T) {
|
||||
blocked := []string{
|
||||
"n_galway_rothfuss", // 地名+人名
|
||||
"john_galway", // 人名+地名
|
||||
"galway_慈善篮球赛", // 地名+活动名
|
||||
"n_the_name_of_the_wind_galway", // 书名+地名
|
||||
}
|
||||
for _, s := range blocked {
|
||||
if !IsPromptEchoEntity(s) {
|
||||
t.Errorf("≥2 示例 token 并列应判回显:%q", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 反例:单 token(只出现 1 个)不拦 —— 保留给真实讨论,避免误杀。
|
||||
func TestIsPromptEchoEntity_SingleTokenNotBlocked(t *testing.T) {
|
||||
kept := []string{
|
||||
"northern ireland", // 真实地区,只含 ireland
|
||||
"galway united", // 真实地名(Galway United),只含 galway
|
||||
"north galway", // 真实地名,只含 galway
|
||||
"john port", // 真实港口/人名,只含 john
|
||||
}
|
||||
for _, s := range kept {
|
||||
if IsPromptEchoEntity(s) {
|
||||
t.Errorf("单 token 出现 1 次不应被拦:%q", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,4 +109,4 @@ func TestLiveScoreNotEcho(t *testing.T) {
|
||||
t.Fatalf("❌ echo 向量 %v 出现 %d 次:照抄示例,治本失败", sc, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+102
-25
@@ -12,6 +12,7 @@ import (
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -147,6 +148,38 @@ func IsDegenerateScore(s FiveDScore) bool {
|
||||
// 设环境变量 ZHIYI_SCORE_SHADOW=0 关闭影子(= 恢复真拦)。
|
||||
var scoreShadowMode = os.Getenv("ZHIYI_SCORE_SHADOW") != "0"
|
||||
|
||||
// ─── 评分闸门阈值(2026-10-12 重标定)────────────────────────
|
||||
//
|
||||
// 背景:旧阈值 overall<0.7 && vd<0.8 从未被真实分布验证 —— 它是在
|
||||
// 「旧 prompt 示例分数被照抄 ⇒ overall 恒为 0.755」这个 bug 上拍出来的,
|
||||
// 闸门从未真正丢过任何东西(恒 ≥0.7)。
|
||||
//
|
||||
// 实测(4 天 2065 条可用评分,local/minicpm5-2b)真实分布:
|
||||
//
|
||||
// min=0.175 p25=0.390 中位=0.535 p75=0.600 p90=0.800 max=0.825
|
||||
//
|
||||
// 用 0.7 卡这个分布 ⇒ 仅 17.9% 能过 ⇒ 一开闸就丢 82% 可用内容。
|
||||
//
|
||||
// 新默认取分布 p10≈0.30 的保守档:只丢「明确垃圾」。
|
||||
// 取向:宁可多留(可逆 —— 后续消费/降权机制会自然淘汰),不轻易丢(不可逆)。
|
||||
// 影子模式下可用环境变量随时重标定,无需改代码:
|
||||
//
|
||||
// ZHIYI_SCORE_MIN_OVERALL / ZHIYI_SCORE_MIN_VD
|
||||
var (
|
||||
scoreMinOverall = envFloat("ZHIYI_SCORE_MIN_OVERALL", 0.30)
|
||||
scoreMinVD = envFloat("ZHIYI_SCORE_MIN_VD", 0.30)
|
||||
)
|
||||
|
||||
// envFloat 读浮点环境变量,缺省/非法则用 def。
|
||||
func envFloat(key string, def float64) float64 {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
if f, err := strconv.ParseFloat(v, 64); err == nil {
|
||||
return f
|
||||
}
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// ─── 蒸馏引擎 ────────────────────────────────────────────────
|
||||
|
||||
// Engine 蒸馏引擎
|
||||
@@ -216,9 +249,9 @@ func distillOutputFormat() string {
|
||||
|
||||
func NewEngine(llmEndpoint, llmModel, apiKey string) *Engine {
|
||||
e := &Engine{
|
||||
LLMEndpoint: llmEndpoint,
|
||||
LLMModel: llmModel,
|
||||
APIKey: apiKey,
|
||||
LLMEndpoint: llmEndpoint,
|
||||
LLMModel: llmModel,
|
||||
APIKey: apiKey,
|
||||
// t_180c0179:蒸馏输出默认行协议;ZHIYI_DISTILL_FORMAT=json 一行回退旧 JSON 路径。
|
||||
OutputFormat: distillOutputFormat(),
|
||||
batchSize: 10,
|
||||
@@ -458,6 +491,17 @@ func (e *Engine) flush() {
|
||||
e.mu.Unlock()
|
||||
|
||||
log.Printf("[distill] flush DONE: processed %d items (concurrency=%d)", len(batch), effConcurrency)
|
||||
|
||||
// 打分缓存汇总(2026-10-12):命中率直接等于「本轮省下的 2B 调用占比」。
|
||||
// 落盘放这里(每轮一次)而不是每条一次,避免频繁 IO。
|
||||
if c := getScoreCache(); c != nil {
|
||||
h, m := c.stats()
|
||||
if h+m > 0 {
|
||||
log.Printf("[scorecache] 累计 命中=%d 未命中=%d 命中率=%.1f%%(命中=省下的 2B 调用)",
|
||||
h, m, float64(h)/float64(h+m)*100)
|
||||
}
|
||||
c.save()
|
||||
}
|
||||
}
|
||||
|
||||
// distillOne 蒸馏单条
|
||||
@@ -483,26 +527,38 @@ func (e *Engine) distillOne(input DistillInput) DistillResult {
|
||||
log.Printf("[distill] score for %s: overall=%.3f is=%.2f su=%.2f pa=%.2f vd=%.2f ru=%.2f",
|
||||
input.EpisodeID, overall, score.IS, score.SU, score.PA, score.VD, score.RU)
|
||||
|
||||
if overall < 0.7 && score.VD < 0.8 {
|
||||
// 人侧短事实(画像/心迹/牵挂/偏好/习惯/目标)是用户亲口陈述,
|
||||
// LLM 打分天然偏低(短、无上下文),不应因分数被丢弃——否则"牧尘不在招商局工作"
|
||||
// 这类关键事实永远到不了 memories,t6 永远比对不到。
|
||||
if !isSoulfulShortFact(input.Content) {
|
||||
if scoreShadowMode {
|
||||
// 影子模式(2026-10-06):闸门此前因 echo bug 从未真正生效,
|
||||
// 修好 prompt 后它会首次开始真拦 —— 先只记录"本该被丢",观察样本分布,
|
||||
// 避免一次性批量误杀。ZHIYI_SCORE_SHADOW=0 可切回真拦。
|
||||
log.Printf("[distill][SHADOW] would-drop episode=%s overall=%.3f vd=%.2f echo=%v degenerate=%v content=%q",
|
||||
input.EpisodeID, overall, score.VD, IsEchoScore(score), IsDegenerateScore(score),
|
||||
truncate(input.Content, 80))
|
||||
} else {
|
||||
log.Printf("[distill] score below threshold for %s, skip", input.EpisodeID)
|
||||
// 即使阈值未通过,仍然用 LLM 实体提取(如果有的话)
|
||||
if len(llmResp.Entities) == 0 {
|
||||
return DistillResult{}
|
||||
}
|
||||
// ─── 准入分诊(2026-10-12 Step 1)─────────────────────────────
|
||||
// 依据外部经验(方案:mc/小怡/04-OBSIDIAN工具/04-02-方案设计/
|
||||
// 20261012-织忆记忆闸门-外部经验落地方案.md):
|
||||
// · A-MAC 消融:确定性「类型先验」是单点最强特征(ΔF1=−0.107)⇒ 判据必须含确定性规则
|
||||
// · SAGE:明确的直接闭式解决,只有「不确定带」才需谨慎
|
||||
// · When to Forget 原文:"should not deprecate an uncertain memory" ⇒ 不确定的绝不弃用
|
||||
// 三路:drop=确定性垃圾 / keep=确定性高价值 / observe=不确定带(**永不因低分丢弃**)
|
||||
lane, laneWhy := ClassifyAdmission(input.Content)
|
||||
log.Printf("[distill][LANE] episode=%s lane=%s reason=%s overall=%.3f vd=%.2f content=%q",
|
||||
input.EpisodeID, lane, laneWhy, overall, score.VD, truncate(input.Content, 60))
|
||||
|
||||
switch lane {
|
||||
case LaneDrop:
|
||||
if scoreShadowMode {
|
||||
log.Printf("[distill][SHADOW] would-drop(%s) episode=%s overall=%.3f vd=%.2f echo=%v degenerate=%v content=%q",
|
||||
laneWhy, input.EpisodeID, overall, score.VD, IsEchoScore(score), IsDegenerateScore(score),
|
||||
truncate(input.Content, 80))
|
||||
} else {
|
||||
log.Printf("[distill] lane=drop reason=%s for %s, skip", laneWhy, input.EpisodeID)
|
||||
// 即使丢弃,仍然用 LLM 实体提取(如果有的话)
|
||||
if len(llmResp.Entities) == 0 {
|
||||
return DistillResult{}
|
||||
}
|
||||
}
|
||||
case LaneKeep:
|
||||
log.Printf("[distill][LANE] keep episode=%s reason=%s (放行,不看分数)", input.EpisodeID, laneWhy)
|
||||
default: // LaneObserve
|
||||
// 不确定带:Step 1 **不做分数丢弃**。低分只记录,供 Step 2 学阈值用。
|
||||
if overall < scoreMinOverall && score.VD < scoreMinVD {
|
||||
log.Printf("[distill][LANE-BAND] low-score-but-kept episode=%s overall=%.3f vd=%.2f reason=%s(仅记录,不丢)",
|
||||
input.EpisodeID, overall, score.VD, laneWhy)
|
||||
}
|
||||
}
|
||||
|
||||
// 优先使用 LLM 提取的实体和事实
|
||||
@@ -544,10 +600,13 @@ func (e *Engine) distillOne(input DistillInput) DistillResult {
|
||||
// 2026-09-07 质量门槛(t_cc07ef4b): LLM 提炼 fact 入库前过滤
|
||||
// <20字/纯疑问句/状态汇报型 拒绝; 20-50字需含实体或动词短语
|
||||
// 在源头过滤, 使图谱/AAAK/记忆库一律只看到质量事实
|
||||
// t_2ae69bb2: 把上方 lane 结论穿透到 fact 级门禁 —— LaneKeep 时跳过长度类
|
||||
// 规则(只保留纯疑问/状态汇报两条硬否定),避免「lane 说放行、fact 级
|
||||
// 却全丢」的静默吞噬;LaneDrop/LaneObserve 行为与原来逐字一致。
|
||||
if before := len(facts); before > 0 {
|
||||
facts = FilterQualityFacts(facts)
|
||||
facts = FilterQualityFactsWithLane(facts, lane)
|
||||
if dropped := before - len(facts); dropped > 0 {
|
||||
log.Printf("[distill] quality gate dropped %d/%d facts for %s", dropped, before, input.EpisodeID)
|
||||
log.Printf("[distill] quality gate dropped %d/%d facts for %s (lane=%s)", dropped, before, input.EpisodeID, lane)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -567,7 +626,24 @@ func (e *Engine) distillOne(input DistillInput) DistillResult {
|
||||
}
|
||||
|
||||
// callLLM5D 调用 LLM 进行 5维评估 + 实体/事实提取
|
||||
// callLLM5D 打分入口 —— 先查内容哈希缓存,命中直接复用(不调 2B)。
|
||||
//
|
||||
// 2026-10-12:episodes 重复率 78.5%、约 60% 的打分调用打在重复内容上(详见 score_cache.go)。
|
||||
// 本层只做记忆化,**不改变任何判分逻辑**;禁用见 ZHIYI_SCORE_CACHE=0。
|
||||
func (e *Engine) callLLM5D(content string) (LLMResponse, error) {
|
||||
c := getScoreCache()
|
||||
if r, ok := c.get(content); ok {
|
||||
return r, nil
|
||||
}
|
||||
r, err := e.callLLM5DUncached(content)
|
||||
if err == nil {
|
||||
c.put(content, r)
|
||||
}
|
||||
return r, err
|
||||
}
|
||||
|
||||
// callLLM5DUncached 原始打分实现(函数体未改动,仅改名,便于外层套缓存)。
|
||||
func (e *Engine) callLLM5DUncached(content string) (LLMResponse, error) {
|
||||
// 2026-09-14 限制 distill prompt ≤8k token,防止打爆本地 2B 单槽
|
||||
content, sentTokens, origTokens := limitDistillContent(content)
|
||||
if origTokens > maxDistillTokens {
|
||||
@@ -630,12 +706,13 @@ func (e *Engine) callLLM5D(content string) (LLMResponse, error) {
|
||||
FACT|独立事实1
|
||||
FACT|独立事实2
|
||||
ENTITY|实体名|实体类型
|
||||
SCORE|is|su|pa|vd|ru
|
||||
SCORE|五个数字用竖线分隔
|
||||
|
||||
规则:
|
||||
- 每条事实一行,以 FACT| 开头;第一个 | 之后整行都是事实内容,引号、竖线、等号等任意字符都直接写、不要转义
|
||||
- 每个实体一行,ENTITY|名字|类型(类型如 person / tool / system / project)
|
||||
- 评分放最后一行:SCORE|is|su|pa|vd|ru,用 0.0 到 1.0 之间的浮点数(越高越好),不得用 0-10 整数
|
||||
- 评分放最后一行:以 SCORE| 开头,后面紧跟【恰好五个】数字(0.0~1.0 的小数,越高越好),用 | 分隔,一个不能多一个不能少,不得用 0-10 整数
|
||||
- 🔴 禁止把 is、su、pa、vd、ru 这类字段名写进输出 —— 它们只是上面说明格式用的名字,不是要你输出的内容;输出里只能出现数字
|
||||
- 🔴 这五个分数必须依据**本次内容**独立判断:不同内容要给出**不同**的分数,各维度之间也应有差异。禁止给出固定不变的分数组合 —— 那会被系统判为无效评分并整条丢弃。输出里不要出现任何示例数值,直接写你本次的判断。
|
||||
- 除非消息完全无意义,否则提取并输出为事实。facts 要详尽,不要只给1条摘要。
|
||||
`
|
||||
|
||||
@@ -64,7 +64,7 @@ func TestIsDegenerateScore(t *testing.T) {
|
||||
for name, s := range map[string]FiveDScore{
|
||||
"真实分维度(实测样本)": {IS: 0.8, SU: 0.8, PA: 0.7, VD: 0.6, RU: 0.5},
|
||||
"仅一位不同": {IS: 0.8, SU: 0.8, PA: 0.8, VD: 0.8, RU: 0.7},
|
||||
"历史echo向量": {IS: 0.8, SU: 0.7, PA: 0.6, VD: 0.9, RU: 0.7},
|
||||
"历史echo向量": {IS: 0.8, SU: 0.7, PA: 0.6, VD: 0.9, RU: 0.7},
|
||||
} {
|
||||
if IsDegenerateScore(s) {
|
||||
t.Errorf("阴性对照失败[%s]:%+v 被误判为退化评分(滥报)", name, s)
|
||||
|
||||
@@ -69,7 +69,8 @@ func waitDrained(e *Engine, within time.Duration) bool {
|
||||
const tickerTestContent = "用户今天在鹤壁买了咖啡并读了关于马哲经济学的书,下午讨论了剪辑软件选型。"
|
||||
|
||||
// ① 忠实复现线上场景:flush 在跑期间入队 ⇒ 触发被守卫丢弃 ⇒ 生产者停止
|
||||
// 期望:ticker 兜底把积压消化(修复前 = 永久冻结)。
|
||||
//
|
||||
// 期望:ticker 兜底把积压消化(修复前 = 永久冻结)。
|
||||
func TestFlushTickerRescuesAfterDroppedTrigger(t *testing.T) {
|
||||
e := tickerTestEngine(t, 150*time.Millisecond, nil)
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package distill
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestMain 默认关闭打分缓存。
|
||||
//
|
||||
// 为什么必须关:TestCallLLM5D_LineProtocolParse / BackoffOn429 / BackoffOn5xx /
|
||||
// FlushPeakRespectsSlotMatchedConcurrency 都用**同一内容**反复调 callLLM5D,
|
||||
// 靠第二次调用验证「重试」「解析」行为。缓存一旦生效,第二次直接被短路返回,
|
||||
// 这些测试就**静默失去了覆盖**(表面全绿,实则没测到东西)。
|
||||
//
|
||||
// 缓存本身的正确性由 score_cache_test.go 直接测结构体(不依赖全局实例)。
|
||||
func TestMain(m *testing.M) {
|
||||
scoreCacheDisabled = true
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
@@ -102,6 +102,45 @@ func FilterQualityFacts(facts []string) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// FilterQualityFactsWithLane 感知准入分诊结论地过滤事实列表(kanban t_2ae69bb2)。
|
||||
//
|
||||
// 背景 —— 两门禁判据冲突(2026-10-12 实测复现 ep_1791818823117449804):
|
||||
// ClassifyAdmission 已确定性判定「决策类高价值 → keep,放行不看分数」,但该结论
|
||||
// 没有传到 fact 级门禁,FilterQualityFacts 对每条 fact 独立跑长度启发式,LLM 从
|
||||
// 决定正文抽出的碎片(如「路径与日期不混进正文」=10字)一律命中 <20字 拒绝
|
||||
// → 整条决定零 fact 落库。
|
||||
//
|
||||
// 语义(只改穿透,不改任何判据/阈值):
|
||||
// - lane == LaneKeep:保留 纯疑问句(isPureQuestion) 与 状态汇报型(isStatusReportLike)
|
||||
// 两条硬否定,跳过全部长度类规则(<20字 / 20-50字需含实体)。
|
||||
// 理由:长度启发式是筛「碎片噪声」的;lane 已确定性判定本条是高价值内容,
|
||||
// 两处冲突时以更强的确定性信号(lane)为准。
|
||||
// - LaneDrop / LaneObserve:行为与 FilterQualityFacts 逐字一致(原样委托)。
|
||||
//
|
||||
// 边界(刻意不碰):
|
||||
// - 不动 isSoulfulShortFact 前缀白名单(那是「人侧结构化短事实」的语义,
|
||||
// 2026-09-20 收账已踩过「开得过宽」的坑,`决定:` 不属于该集合)。
|
||||
// - 不动 IsQualityFact 任何阈值数字。
|
||||
func FilterQualityFactsWithLane(facts []string, lane AdmissionLane) []string {
|
||||
if lane != LaneKeep {
|
||||
return FilterQualityFacts(facts)
|
||||
}
|
||||
out := make([]string, 0, len(facts))
|
||||
for _, f := range facts {
|
||||
f = redact.RedactSecrets(f)
|
||||
s := strings.TrimSpace(f)
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
// LaneKeep 下仍生效的两条硬否定(任何长度)
|
||||
if isPureQuestion(s) || isStatusReportLike(s) {
|
||||
continue
|
||||
}
|
||||
out = append(out, f)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ─── 纯疑问句判定 ──────────────────────────────────────────
|
||||
|
||||
var questionSuffixes = []string{"?", "?", "吗?", "呢?", "么?", "嘛?", "吗?", "呢?"}
|
||||
|
||||
@@ -14,11 +14,11 @@ func TestDistillOne_QualityGateBlocksFragments(t *testing.T) {
|
||||
// mock LLM 返回: 短对话蒸馏产物——大部分是 <20字碎片/疑问句/状态汇报
|
||||
mockResp := LLMResponse{
|
||||
Facts: []string{
|
||||
"重启系统", // <20 碎片 → 拒
|
||||
"健康检查完成", // 状态汇报 → 拒
|
||||
"验证通过", // 状态汇报 → 拒
|
||||
"为什么系统会重启?", // 纯疑问句 → 拒
|
||||
"用户喜欢喝咖啡", // <20 碎片 → 拒
|
||||
"重启系统", // <20 碎片 → 拒
|
||||
"健康检查完成", // 状态汇报 → 拒
|
||||
"验证通过", // 状态汇报 → 拒
|
||||
"为什么系统会重启?", // 纯疑问句 → 拒
|
||||
"用户喜欢喝咖啡", // <20 碎片 → 拒
|
||||
"用户对织忆系统的蒸馏质量提出了改进要求并希望尽快处理", // 24字 含实体 → 通过
|
||||
},
|
||||
Entities: []string{"织忆"},
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
package distill
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/xiaoxue/memoryweave/internal/textfilter"
|
||||
)
|
||||
|
||||
// t_2ae69bb2: 两门禁判据冲突 —— ClassifyAdmission 判定 LaneKeep("决策类,放行不看分数")
|
||||
// 的内容,其 fact 仍被 FilterQualityFacts 的长度启发式静默全丢(2026-10-12 实测
|
||||
// ep_1791818823117449804 dropped 3/3,零 fact 落库)。
|
||||
// 本组测试锁定新入口 FilterQualityFactsWithLane 的穿透语义:
|
||||
// - LaneKeep:跳过全部长度类规则,只保留 纯疑问/状态汇报 两条硬否定
|
||||
// - LaneDrop / LaneObserve:与 FilterQualityFacts 逐字一致(委托原函数)
|
||||
// 刻意不碰:admission_lane.go 判据、isSoulfulShortFact 白名单、IsQualityFact 阈值。
|
||||
|
||||
// TestFilterQualityFactsWithLane_KeepPassesShortFacts AC1:
|
||||
// LaneKeep 内容里 <20 字的 fact 不再被丢(≥3 例,前提:content 确实被分为 keep)。
|
||||
func TestFilterQualityFactsWithLane_KeepPassesShortFacts(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
// 每条 content 必须被 ClassifyAdmission 判为 keep(前提断言在子测试内)
|
||||
content string
|
||||
// LLM 可能从决策正文里抽出的短碎片(全部 <20 字,旧门槛下会被全拒)
|
||||
shortFacts []string
|
||||
}{
|
||||
{
|
||||
"决定录入格式规范(原文复现)",
|
||||
"【决定】决定录入格式规范:一条只写一个决定,路径与日期不混进正文,避免蒸馏碎片化后宽泛问法召回不到。",
|
||||
[]string{"路径与日期不混进正文", "一条只写一个决定", "避免碎片化后召回不到"},
|
||||
},
|
||||
{
|
||||
"决策承诺(测试词表同款)",
|
||||
"牧尘决定以后都用分身C来处理采集类任务。",
|
||||
[]string{"采集任务交给分身C", "以后都用分身C"},
|
||||
},
|
||||
{
|
||||
"自动化边界决定(同批原文复现)",
|
||||
"【决定】决定类记忆回流不挂 cron:由小怡在每次拍板后主动调用脚本,先跑顺一两周。",
|
||||
[]string{"回流不挂cron", "先跑顺一两周", "拍板后主动调用"},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
lane, why := ClassifyAdmission(tc.content)
|
||||
if lane != LaneKeep {
|
||||
t.Fatalf("前提失败: ClassifyAdmission 未判 keep (lane=%s, %s), content=%.60s", lane, why, tc.content)
|
||||
}
|
||||
got := FilterQualityFactsWithLane(tc.shortFacts, lane)
|
||||
if len(got) != len(tc.shortFacts) {
|
||||
t.Fatalf("LaneKeep 下短 fact 仍被丢: got %d/%d %v", len(got), len(tc.shortFacts), got)
|
||||
}
|
||||
// 对照锚:旧门槛(lane 不感知)确实会全拒这批短碎片 —— 证明测的就是行为差
|
||||
old := FilterQualityFacts(tc.shortFacts)
|
||||
if len(old) == len(tc.shortFacts) {
|
||||
t.Fatalf("对照锚失效: 旧门槛也全放了 %v,本测试无区分度", tc.shortFacts)
|
||||
}
|
||||
t.Logf("新门槛 keep 放行 %d 条;旧门槛仅放行 %d 条: %v", len(got), len(old), old)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestFilterQualityFactsWithLane_KeepStillRejectsNegatives AC3:
|
||||
// 纯疑问句 / 状态汇报型即使 LaneKeep 也仍被拒(硬否定两条不动)。
|
||||
func TestFilterQualityFactsWithLane_KeepStillRejectsNegatives(t *testing.T) {
|
||||
keepContent := "牧尘决定以后都用分身C来处理采集类任务。"
|
||||
lane, why := ClassifyAdmission(keepContent)
|
||||
if lane != LaneKeep {
|
||||
t.Fatalf("前提失败: lane=%s (%s)", lane, why)
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
fact string
|
||||
}{
|
||||
{"纯疑问(带问号,>20字)", "为什么采集类任务要统一交给分身C来执行处理?"},
|
||||
{"状态汇报型(无主句+完成词)", "健康检查完成,所有服务运行正常,无异常"},
|
||||
{"状态汇报型(cron噪声)", "cron任务执行完毕,桥接状态同步完成"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := FilterQualityFactsWithLane([]string{tc.fact}, lane); len(got) != 0 {
|
||||
t.Fatalf("LaneKeep 不应放行 %s: %v", tc.name, got)
|
||||
}
|
||||
// 同句在旧门槛也拒(保证是"本来就该拒",不是 lane 通道误拒)
|
||||
if old := FilterQualityFacts([]string{tc.fact}); len(old) != 0 {
|
||||
t.Fatalf("对照锚异常: 旧门槛放行了 %q", tc.fact)
|
||||
}
|
||||
})
|
||||
}
|
||||
// 正对照:同批里的一句短陈述在 LaneKeep 下必须放行(证明不是"全拒")
|
||||
pos := FilterQualityFactsWithLane([]string{"决定采用单一采集口径"}, lane)
|
||||
if len(pos) != 1 {
|
||||
t.Fatalf("短陈述在 LaneKeep 下应放行: %v", pos)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFilterQualityFactsWithLane_NonKeepDelegates AC2 补充:
|
||||
// LaneDrop / LaneObserve 的行为必须与 FilterQualityFacts 逐字一致(同输入同输出)。
|
||||
func TestFilterQualityFactsWithLane_NonKeepDelegates(t *testing.T) {
|
||||
dropContent := "好的" // <8字 → 确定性 drop
|
||||
dropLane, _ := ClassifyAdmission(dropContent)
|
||||
if dropLane != LaneDrop {
|
||||
t.Fatalf("前提失败: 期望 drop, got %s", dropLane)
|
||||
}
|
||||
obsContent := "服务已重启完成,当前版本 v0.21.6。" // 不确定带 → observe
|
||||
obsLane, _ := ClassifyAdmission(obsContent)
|
||||
if obsLane != LaneObserve {
|
||||
t.Fatalf("前提失败: 期望 observe, got %s", obsLane)
|
||||
}
|
||||
in := []string{
|
||||
"重启系统", // <20 碎片 → 拒
|
||||
"用户喜欢喝咖啡", // <20 无前缀 → 拒
|
||||
"为什么系统会重启?", // 纯疑问 → 拒
|
||||
"用户昨天重启了家里的服务器并恢复了所有服务", // 20字+ 带实体 → 过
|
||||
"健康检查完成", // 状态汇报 → 拒
|
||||
}
|
||||
for lane, name := range map[AdmissionLane]string{dropLane: "LaneDrop", obsLane: "LaneObserve"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
want := FilterQualityFacts(in)
|
||||
got := FilterQualityFactsWithLane(in, lane)
|
||||
if len(want) != len(got) {
|
||||
t.Fatalf("非 keep lane 行为漂移: got %d want %d (%v vs %v)", len(got), len(want), got, want)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("非 keep lane 行为漂移: got[%d]=%q want=%q", i, got[i], want[i])
|
||||
}
|
||||
}
|
||||
if len(want) != 1 || want[0] != "用户昨天重启了家里的服务器并恢复了所有服务" {
|
||||
t.Fatalf("对照锚异常: FilterQualityFacts 结果 = %v", want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestFilterQualityFactsWithLane_EmptyKeep 边界:LaneKeep + 空/纯空白 fact → 原样不过。
|
||||
func TestFilterQualityFactsWithLane_EmptyKeep(t *testing.T) {
|
||||
got := FilterQualityFactsWithLane([]string{"", " "}, LaneKeep)
|
||||
if len(got) != 0 {
|
||||
t.Fatalf("空白 fact 不应放行: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// 守卫本组测试自身:确认这些 content 确实不是系统样板(否则前提 lane=keep 会被 drop 打穿)。
|
||||
func TestFilterQualityFactsWithLane_PremisesNotSystemTemplate(t *testing.T) {
|
||||
for _, c := range []string{
|
||||
"【决定】决定录入格式规范:一条只写一个决定,路径与日期不混进正文,避免蒸馏碎片化后宽泛问法召回不到。",
|
||||
"牧尘决定以后都用分身C来处理采集类任务。",
|
||||
} {
|
||||
if textfilter.IsSystemTemplate(c) {
|
||||
t.Fatalf("前提 content 被误判为系统样板: %.40s", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
// 织忆 MemoryWeave — 打分结果记忆化缓存(2026-10-12)
|
||||
//
|
||||
// 要治的问题(实测):episodes 表**重复率 78.5%**(近 2 天 58.4%,今天 64.3%),
|
||||
// 同一条内容一天被写成 9 次;干净窗口 392 条评分里**只有 146 条唯一** ⇒
|
||||
// **约 60% 的 2B 打分调用打在重复内容上,纯烧算力**。
|
||||
//
|
||||
// 为什么不是「批内去重」:观测到的重复是「同一条内容一天 ×9」,**分散在不同批次**
|
||||
// (每批约 1~15 条),批内去重抓不到。对症的是**按内容哈希记忆化**:
|
||||
// 同一内容只调一次 LLM,之后复用;批内 / 跨批 / 跨重启一并覆盖。
|
||||
//
|
||||
// 为什么零风险:只是复用「同一输入的上一次输出」,不改任何判分逻辑。
|
||||
// ① 最终写入 memories 时本来就会去重(实测 memories 重复率 0.3%),
|
||||
//
|
||||
// 所以跳过重复项的打分**不改变最终写入结果**;
|
||||
//
|
||||
// ② 关掉即恢复原行为:ZHIYI_SCORE_CACHE=0。
|
||||
//
|
||||
// ── 存储选型(2026-10-12 改 · 起因:牧尘问「是用缓存还是 redis server?」)──
|
||||
// 初版我自造了「进程内 map + 每轮 flush 全量重写 JSON 文件」。**那是重复造轮子且更差**:
|
||||
//
|
||||
// · 项目本来就有 Redis L2(召回搜索缓存 zhiyi:cache:*、事件总线、技能持久化都在用),
|
||||
// zhiyid **已强制依赖 Redis** ⇒ 用它不是新增依赖;
|
||||
// · 初版每轮把 2 万条(≈10MB)全量序列化写盘(tmp+rename),Redis 只写增量;
|
||||
// · TTL 现成,不用自己写淘汰 + 损坏兜底 ⇒ 约 200 行 IO 代码直接删掉。
|
||||
//
|
||||
// 现架构:**L1 进程内 map(命中零网络往返)+ L2 Redis 逐键(TTL 7 天,增量写)**,
|
||||
// 与项目既有 SearchCache(L1+L2)同构。Redis 不可用时自动退化为纯 L1,**绝不阻断蒸馏**。
|
||||
package distill
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/xiaoxue/memoryweave/internal/storage"
|
||||
)
|
||||
|
||||
const (
|
||||
// scoreCacheCap L1 内存上限条数(L2 不设总量上限,靠 TTL 自然过期)。
|
||||
scoreCacheCap = 20000
|
||||
// scoreCacheKeyPrefix L2 键前缀(跟随项目既有 zhiyi: 命名空间,便于 SCAN 管理)。
|
||||
scoreCacheKeyPrefix = "zhiyi:distill-score:"
|
||||
// scoreCacheTTL L2 存活期。7 天足够覆盖「同内容一天写 9 次」的窗口,
|
||||
// 又不会无限膨胀 —— 本机 Redis 是 maxmemory=0 + noeviction,
|
||||
// 不设 TTL 就只会涨不会落(这个坑今天已经在别处踩过一次)。
|
||||
scoreCacheTTL = 7 * 24 * time.Hour
|
||||
)
|
||||
|
||||
// scoreL2 L2 存储的最小接口。抽出来是为了**单测不依赖真 Redis**
|
||||
// (注入内存 fake 即可确定性地验落盘往返),生产默认用 storage.GetRedisClient()。
|
||||
type scoreL2 interface {
|
||||
Get(key string) (string, error)
|
||||
Set(key, value string, ttl time.Duration) error
|
||||
}
|
||||
|
||||
type scoreCache struct {
|
||||
mu sync.Mutex
|
||||
m map[string]LLMResponse
|
||||
order []string // 插入顺序,用于 L1 FIFO 淘汰
|
||||
cap int
|
||||
hits int64
|
||||
miss int64
|
||||
|
||||
l2 scoreL2
|
||||
l2Disabled bool // L2 不可用时置位(只记一次日志,之后退化为纯 L1)
|
||||
}
|
||||
|
||||
var (
|
||||
scoreCacheOnce sync.Once
|
||||
globalScoreCache *scoreCache
|
||||
// scoreCacheDisabled 关闭开关。生产由环境变量控制;**单元测试默认置 true**
|
||||
// (见 main_test.go 的 TestMain)——多个测试用同一内容反复调 callLLM5D
|
||||
// 验证重试/解析行为,缓存会把第二次调用短路,使它们失去覆盖。
|
||||
scoreCacheDisabled = os.Getenv("ZHIYI_SCORE_CACHE") == "0"
|
||||
// scoreCacheL2Override 测试注入点(非 nil 时替代真 Redis)。
|
||||
scoreCacheL2Override scoreL2
|
||||
)
|
||||
|
||||
func getScoreCache() *scoreCache {
|
||||
scoreCacheOnce.Do(func() {
|
||||
if scoreCacheDisabled {
|
||||
log.Printf("[scorecache] 已禁用(环境变量 ZHIYI_SCORE_CACHE=0 或测试环境)")
|
||||
return
|
||||
}
|
||||
c := &scoreCache{m: map[string]LLMResponse{}, cap: scoreCacheCap}
|
||||
switch {
|
||||
case scoreCacheL2Override != nil:
|
||||
c.l2 = scoreCacheL2Override
|
||||
default:
|
||||
if rc := storage.GetRedisClient(); rc != nil {
|
||||
c.l2 = rc
|
||||
} else {
|
||||
c.l2Disabled = true
|
||||
log.Printf("[scorecache] ⚠️ Redis 不可用 → 退化为纯 L1 内存缓存(不阻断蒸馏)")
|
||||
}
|
||||
}
|
||||
log.Printf("[scorecache] 启用:L1 内存(%d 条) + L2 Redis(%s*, TTL %v)",
|
||||
scoreCacheCap, scoreCacheKeyPrefix, scoreCacheTTL)
|
||||
globalScoreCache = c
|
||||
})
|
||||
return globalScoreCache
|
||||
}
|
||||
|
||||
func scoreKey(content string) string {
|
||||
sum := sha256.Sum256([]byte(content))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// get 先查 L1;未命中再查 L2,命中则回填 L1(下次零网络往返)。
|
||||
// L2 出错不当作错误,只当未命中 —— 缓存绝不能阻断主流程。
|
||||
func (c *scoreCache) get(content string) (LLMResponse, bool) {
|
||||
if c == nil {
|
||||
return LLMResponse{}, false
|
||||
}
|
||||
k := scoreKey(content)
|
||||
|
||||
c.mu.Lock()
|
||||
if r, ok := c.m[k]; ok {
|
||||
c.hits++
|
||||
c.mu.Unlock()
|
||||
return r, true
|
||||
}
|
||||
c.mu.Unlock()
|
||||
|
||||
if c.l2 != nil && !c.l2Disabled {
|
||||
if s, err := c.l2.Get(scoreCacheKeyPrefix + k); err == nil && s != "" {
|
||||
var r LLMResponse
|
||||
if json.Unmarshal([]byte(s), &r) == nil {
|
||||
c.admit(k, r) // 回填 L1
|
||||
c.mu.Lock()
|
||||
c.hits++
|
||||
c.mu.Unlock()
|
||||
return r, true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.mu.Lock()
|
||||
c.miss++
|
||||
c.mu.Unlock()
|
||||
return LLMResponse{}, false
|
||||
}
|
||||
|
||||
// put 写 L1 + L2(L2 增量写单键,不再全量重写)。
|
||||
func (c *scoreCache) put(content string, r LLMResponse) {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
k := scoreKey(content)
|
||||
c.admit(k, r)
|
||||
|
||||
if c.l2 == nil || c.l2Disabled {
|
||||
return
|
||||
}
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err := c.l2.Set(scoreCacheKeyPrefix+k, string(b), scoreCacheTTL); err != nil {
|
||||
// 只在第一次失败时告警并退化,避免刷屏
|
||||
c.mu.Lock()
|
||||
first := !c.l2Disabled
|
||||
c.l2Disabled = true
|
||||
c.mu.Unlock()
|
||||
if first {
|
||||
log.Printf("[scorecache] ⚠️ L2 写入失败,退化为纯 L1(不阻断蒸馏): %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// admit 写入 L1 并按上限 FIFO 淘汰。
|
||||
func (c *scoreCache) admit(k string, r LLMResponse) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if _, exists := c.m[k]; exists {
|
||||
return
|
||||
}
|
||||
c.m[k] = r
|
||||
c.order = append(c.order, k)
|
||||
for len(c.order) > c.cap {
|
||||
old := c.order[0]
|
||||
c.order = c.order[1:]
|
||||
delete(c.m, old)
|
||||
}
|
||||
}
|
||||
|
||||
// stats 返回 (命中, 未命中) 快照,供每轮 flush 汇总打印。
|
||||
func (c *scoreCache) stats() (int64, int64) {
|
||||
if c == nil {
|
||||
return 0, 0
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
return c.hits, c.miss
|
||||
}
|
||||
|
||||
// save 仅为兼容旧调用点保留;L2 逐键即时写,不再需要批量落盘。空实现不得 panic。
|
||||
func (c *scoreCache) save() {}
|
||||
@@ -0,0 +1,70 @@
|
||||
package distill
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestScoreCacheSkipsSecondLLMCall —— 决定性验证:**同内容第二次调用不得增加网络请求**。
|
||||
//
|
||||
// 「省下约 60% 的 2B 调用」等价于「第二次遇到同内容时 HTTP 请求数不增加」。
|
||||
// 只测缓存结构体(不碰网络)证明不了这一点,必须打真 HTTP 桩数请求数。
|
||||
//
|
||||
// ⚠️ 断言必须用**相对计数**:NewEngine 会走 RefreshConcurrency 探一次端点
|
||||
// (日志 total_slots=…),那次探活也计入桩的请求数。首版用绝对值 "==1" 判,
|
||||
// 被探活干扰成 2 而误报失败 —— 是**判据写错**,不是缓存失效。
|
||||
func TestScoreCacheSkipsSecondLLMCall(t *testing.T) {
|
||||
// 本包 TestMain 默认关缓存(保护重试/解析类测试),这里显式打开。
|
||||
oldDisabled, oldOnce, oldInst, oldL2 := scoreCacheDisabled, scoreCacheOnce, globalScoreCache, scoreCacheL2Override
|
||||
scoreCacheDisabled = false
|
||||
scoreCacheOnce = sync.Once{}
|
||||
globalScoreCache = nil
|
||||
scoreCacheL2Override = newFakeL2()
|
||||
t.Cleanup(func() {
|
||||
scoreCacheDisabled, scoreCacheOnce, globalScoreCache = oldDisabled, oldOnce, oldInst
|
||||
scoreCacheL2Override = oldL2
|
||||
})
|
||||
|
||||
var calls int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
atomic.AddInt32(&calls, 1)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(testLLMBody))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
e := NewEngine(srv.URL, "test-model", "test-key")
|
||||
const dup = "牵挂: 待处理: 这条内容今天已经被写过 9 次"
|
||||
|
||||
if _, err := e.callLLM5D(dup); err != nil {
|
||||
t.Fatalf("首次调用失败: %v", err)
|
||||
}
|
||||
after1 := atomic.LoadInt32(&calls)
|
||||
if after1 == 0 {
|
||||
t.Fatal("首次调用竟未发出任何 HTTP 请求")
|
||||
}
|
||||
|
||||
if _, err := e.callLLM5D(dup); err != nil {
|
||||
t.Fatalf("第二次调用失败: %v", err)
|
||||
}
|
||||
after2 := atomic.LoadInt32(&calls)
|
||||
if after2 != after1 {
|
||||
t.Fatalf("❌ 同内容第二次仍打了 LLM:%d → %d(应保持不变)", after1, after2)
|
||||
}
|
||||
|
||||
if _, err := e.callLLM5D("另一条完全不同的内容"); err != nil {
|
||||
t.Fatalf("第三次调用失败: %v", err)
|
||||
}
|
||||
after3 := atomic.LoadInt32(&calls)
|
||||
if after3 <= after2 {
|
||||
t.Fatalf("❌ 不同内容应真调:%d → %d(应增加)", after2, after3)
|
||||
}
|
||||
|
||||
if h, _ := getScoreCache().stats(); h != 1 {
|
||||
t.Fatalf("应恰好 1 次命中,实际 %d", h)
|
||||
}
|
||||
t.Logf("证据:请求数 首次后=%d 同内容后=%d 不同内容后=%d;命中=1", after1, after2, after3)
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package distill
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// fakeL2 内存版 L2,用于确定性地验「跨进程/跨重启」路径,且单测不依赖真 Redis。
|
||||
type fakeL2 struct {
|
||||
mu sync.Mutex
|
||||
m map[string]string
|
||||
fail bool
|
||||
}
|
||||
|
||||
func newFakeL2() *fakeL2 { return &fakeL2{m: map[string]string{}} }
|
||||
|
||||
func (f *fakeL2) Get(k string) (string, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if f.fail {
|
||||
return "", errors.New("l2 down")
|
||||
}
|
||||
v, ok := f.m[k]
|
||||
if !ok {
|
||||
return "", errors.New("miss")
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (f *fakeL2) Set(k, v string, _ time.Duration) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if f.fail {
|
||||
return errors.New("l2 down")
|
||||
}
|
||||
f.m[k] = v
|
||||
return nil
|
||||
}
|
||||
|
||||
func newTestCache(cap int) *scoreCache {
|
||||
return &scoreCache{m: map[string]LLMResponse{}, cap: cap}
|
||||
}
|
||||
|
||||
// TestScoreCacheHit 同内容第二次必须命中且返回完全一致的结果(这才是"省调用"的前提)。
|
||||
func TestScoreCacheHit(t *testing.T) {
|
||||
c := newTestCache(100)
|
||||
in := "牵挂: 待处理: 某条重复内容"
|
||||
want := LLMResponse{IS: 0.4, SU: 0.5, PA: 0.3, VD: 0.6, RU: 0.5, Entities: []string{"x"}}
|
||||
|
||||
if _, ok := c.get(in); ok {
|
||||
t.Fatal("空缓存不应命中")
|
||||
}
|
||||
c.put(in, want)
|
||||
|
||||
got, ok := c.get(in)
|
||||
if !ok {
|
||||
t.Fatal("同内容第二次应命中")
|
||||
}
|
||||
if got.IS != want.IS || got.VD != want.VD || len(got.Entities) != 1 {
|
||||
t.Fatalf("命中返回的结果与写入不一致: %+v", got)
|
||||
}
|
||||
if h, m := c.stats(); h != 1 || m != 1 {
|
||||
t.Fatalf("计数器不符:hits=%d miss=%d(期望 1/1)", h, m)
|
||||
}
|
||||
}
|
||||
|
||||
// TestScoreCacheDifferentContent 不同内容必须各自独立(互相污染 = 给出错误分数)。
|
||||
func TestScoreCacheDifferentContent(t *testing.T) {
|
||||
c := newTestCache(100)
|
||||
c.put("A 内容", LLMResponse{IS: 0.1})
|
||||
c.put("B 内容", LLMResponse{IS: 0.9})
|
||||
if r, _ := c.get("A 内容"); r.IS != 0.1 {
|
||||
t.Fatalf("A 被污染: %+v", r)
|
||||
}
|
||||
if r, _ := c.get("B 内容"); r.IS != 0.9 {
|
||||
t.Fatalf("B 被污染: %+v", r)
|
||||
}
|
||||
if _, ok := c.get("C 内容"); ok {
|
||||
t.Fatal("未写入过的内容不应命中")
|
||||
}
|
||||
}
|
||||
|
||||
// TestScoreCacheEviction L1 超上限必须淘汰而不是无限增长。
|
||||
func TestScoreCacheEviction(t *testing.T) {
|
||||
c := newTestCache(3)
|
||||
for _, s := range []string{"1", "2", "3", "4", "5"} {
|
||||
c.put(s, LLMResponse{IS: 0.5})
|
||||
}
|
||||
if len(c.m) != 3 {
|
||||
t.Fatalf("淘汰后应恒为 3 条,实际 %d", len(c.m))
|
||||
}
|
||||
if _, ok := c.get("1"); ok {
|
||||
t.Fatal("最老的应从 L1 淘汰")
|
||||
}
|
||||
if _, ok := c.get("5"); !ok {
|
||||
t.Fatal("最新的应还在")
|
||||
}
|
||||
}
|
||||
|
||||
// TestScoreCacheL2RoundTrip 关键:**新进程实例(L1 空)能靠 L2 命中** ——
|
||||
// 这就是「服务今天重启 6 次仍有效」的依据。
|
||||
func TestScoreCacheL2RoundTrip(t *testing.T) {
|
||||
l2 := newFakeL2()
|
||||
in := "重启也不该重算的内容"
|
||||
want := LLMResponse{IS: 0.42, Entities: []string{"e1", "e2"}}
|
||||
|
||||
c1 := newTestCache(100)
|
||||
c1.l2 = l2
|
||||
c1.put(in, want)
|
||||
|
||||
// 模拟重启:全新实例、L1 为空、共用同一个 L2。
|
||||
// ⚠️ 首版这里断言「新实例不该命中」—— **判据写错了**:新实例本就是靠 L2 命中的,
|
||||
// 那正是改用 Redis 的目的。正确的断言见下:L1 为空时**必须**命中(走 L2)。
|
||||
c3 := newTestCache(100)
|
||||
c3.l2 = l2
|
||||
if len(c3.m) != 0 {
|
||||
t.Fatal("新实例 L1 应为空(否则测的不是 L2 路径)")
|
||||
}
|
||||
got, ok := c3.get(in)
|
||||
if !ok {
|
||||
t.Fatal("❌ L1 为空时应能从 L2 命中(否则重启后缓存全失效)")
|
||||
}
|
||||
if got.IS != want.IS || len(got.Entities) != 2 {
|
||||
t.Fatalf("L2 恢复结果不一致: %+v", got)
|
||||
}
|
||||
if len(c3.m) == 0 {
|
||||
t.Fatal("L2 命中后应回填 L1(否则每次都吃网络往返)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestScoreCacheL2DownDegrades L2 挂掉只能退化为纯 L1,绝不能阻断蒸馏。
|
||||
func TestScoreCacheL2DownDegrades(t *testing.T) {
|
||||
l2 := newFakeL2()
|
||||
l2.fail = true
|
||||
c := newTestCache(100)
|
||||
c.l2 = l2
|
||||
|
||||
c.put("x", LLMResponse{IS: 0.5}) // L2 写失败 → 应退化,不 panic
|
||||
if !c.l2Disabled {
|
||||
t.Fatal("L2 写失败后应置 l2Disabled")
|
||||
}
|
||||
if got, ok := c.get("x"); !ok || got.IS != 0.5 {
|
||||
t.Fatal("L2 挂掉后 L1 仍须正常命中")
|
||||
}
|
||||
}
|
||||
|
||||
// TestScoreKeyStable 哈希稳定(中文 UTF-8 安全)且不同内容不碰撞。
|
||||
func TestScoreKeyStable(t *testing.T) {
|
||||
if scoreKey("牵挂: 待拍板: 笔记→织忆蒸馏补齐") != scoreKey("牵挂: 待拍板: 笔记→织忆蒸馏补齐") {
|
||||
t.Fatal("同内容哈希不稳定")
|
||||
}
|
||||
if scoreKey("甲") == scoreKey("乙") {
|
||||
t.Fatal("不同内容哈希碰撞")
|
||||
}
|
||||
}
|
||||
|
||||
// TestScoreCacheNilSafe 未启用(getScoreCache 返回 nil)时任何方法都不得 panic。
|
||||
func TestScoreCacheNilSafe(t *testing.T) {
|
||||
var c *scoreCache
|
||||
if _, ok := c.get("x"); ok {
|
||||
t.Fatal("nil 缓存不应命中")
|
||||
}
|
||||
c.put("x", LLMResponse{})
|
||||
if h, m := c.stats(); h != 0 || m != 0 {
|
||||
t.Fatal("nil 缓存统计应为 0")
|
||||
}
|
||||
c.save()
|
||||
}
|
||||
@@ -22,9 +22,9 @@ type Event struct {
|
||||
}
|
||||
|
||||
type EventBus struct {
|
||||
mu sync.RWMutex
|
||||
handlers map[string]func(Event)
|
||||
peers []string // 其他织忆实例的地址
|
||||
mu sync.RWMutex
|
||||
handlers map[string]func(Event)
|
||||
peers []string // 其他织忆实例的地址
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
@@ -136,10 +136,10 @@ func isMuchenCorrection(record map[string]interface{}) bool {
|
||||
// ─── 速率限制(令牌桶)───────────────────────────────────
|
||||
|
||||
type RateLimiter struct {
|
||||
mu sync.RWMutex
|
||||
buckets map[string]*tokenBucket
|
||||
rate float64 // 令牌/秒
|
||||
burst int
|
||||
mu sync.RWMutex
|
||||
buckets map[string]*tokenBucket
|
||||
rate float64 // 令牌/秒
|
||||
burst int
|
||||
}
|
||||
|
||||
type tokenBucket struct {
|
||||
|
||||
@@ -61,6 +61,7 @@ func TestUpdateFromDistill_EnglishOldNameNotRevived(t *testing.T) {
|
||||
Entities: []string{"Xiaowei", "xiaowei", "soulful-xiaowei-main"},
|
||||
Facts: []string{"Xiaowei 负责运维"},
|
||||
Namespace: "ns_en",
|
||||
Source: "wiki:vault-ingest", // 常规来源:专测别名归一,不触来源分档
|
||||
})
|
||||
nodes := g.ListNodes("ns_en")
|
||||
if len(nodes) == 0 {
|
||||
|
||||
@@ -29,4 +29,4 @@ func TestConflictFalsePositive(t *testing.T) {
|
||||
if len(conflicts) != 0 {
|
||||
t.Errorf(`Scan with non-hex entity expected zero conflicts, got %d`, len(conflicts))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,10 +18,10 @@ import (
|
||||
// "蒸馏模型" 内容词,属真实主题相关边界的漏检,方向安全(漏检 = 不跳过蒸馏)。
|
||||
func TestConflictFpFix(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
a, b string
|
||||
want bool
|
||||
what string
|
||||
name string
|
||||
a, b string
|
||||
want bool
|
||||
what string
|
||||
}{
|
||||
// 假阳性:主题无关
|
||||
{"会话模板 vs 审计报告", "用户: [kanban] Task t_09e9abc3 completed.\nTitle: 脚本层「直连上游」清理:1", "模型分层矩阵周审计(2026-09-21):25个新api渠道全免费来源无违规;NIM 9池承担88.6%流量", false, "会话原文(噪音前缀)与审计报告,主题无关"},
|
||||
|
||||
@@ -57,4 +57,4 @@ func TestConflictP1Regression(t *testing.T) {
|
||||
t.Logf("✓ 真矛盾仍检出: %s", c.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package governance
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// AC3 假阳性护栏 dry-run:拿 ≥1000 条 source 各异的真实输入过一遍新判据,
|
||||
// 误杀条数如实出数(不写"0"除非脚本输出)。wiki:decision 与领域概念必须在放行集合。
|
||||
// 数据来源:/var/lib/memoryweave/memories.lance 分层采样(1 per source-pair,1100 行)。
|
||||
func TestDryRun_FalsePositiveGuard(t *testing.T) {
|
||||
sampleFile := "/home/muc/.hermes/kanban/workspaces/t_c6aebb8b/diverse_sample.json"
|
||||
data, err := os.ReadFile(sampleFile)
|
||||
if err != nil {
|
||||
t.Skipf("diverse sample not found: %v", err)
|
||||
}
|
||||
var rows []struct {
|
||||
Namespace string `json:"namespace"`
|
||||
Source string `json:"source"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &rows); err != nil {
|
||||
t.Fatalf("parse sample: %v", err)
|
||||
}
|
||||
if len(rows) < 1000 {
|
||||
t.Fatalf("AC3 需要 ≥1000 条真实输入,实际 %d", len(rows))
|
||||
}
|
||||
|
||||
// 放行集合:wiki:decision / domain 类 source 必须不被 skipSourceClass 拦截
|
||||
decisionRows := 0
|
||||
// 误杀计数:被 skipSourceClass / inSubAgentNamespace / noiseEntity 拦下,
|
||||
// 但其 source 是领域知识类(vault / domain-kb / decision 前缀非过程层)的输入
|
||||
falseKills := 0
|
||||
examples := []string{}
|
||||
for _, r := range rows {
|
||||
srcNorm := normalizeSrc(r.Source)
|
||||
isDomain := !strings.HasPrefix(srcNorm, "legacy:") &&
|
||||
!isProcessSourceClass(srcNorm) && srcNorm != "" && srcNorm != "llm_distill"
|
||||
|
||||
// decision 类必放行(AC4 决定性判据)
|
||||
if srcNorm == "decision" || strings.Contains(srcNorm, "decision") {
|
||||
decisionRows++
|
||||
if skipSourceClass(r.Source) {
|
||||
falseKills++
|
||||
examples = append(examples, "DECISION被拦: "+r.Source)
|
||||
}
|
||||
continue
|
||||
}
|
||||
// 领域概念(wiki:vault / wiki:domain-kb)不能因 skipSourceClass 误杀
|
||||
if strings.HasPrefix(srcNorm, "vault:") || strings.HasPrefix(srcNorm, "domain-kb") {
|
||||
if skipSourceClass(r.Source) {
|
||||
falseKills++
|
||||
examples = append(examples, "领域被拦: "+r.Source)
|
||||
}
|
||||
continue
|
||||
}
|
||||
// 内容形态穿透:领域来源里出现噪声形实体才算误杀(漏网);这里统计被 noiseEntity 拦
|
||||
// 的真实领域实体 —— 领域行若有自ID/AC/工具动词实体,应放行非噪声实体。
|
||||
if isDomain {
|
||||
for _, w := range strings.Fields(r.Content) {
|
||||
if noiseEntity(w) && isClearlyDomainConcept(w) {
|
||||
falseKills++
|
||||
examples = append(examples, "领域概念被当噪声: "+w+" || "+r.Source)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
t.Logf("dry-run: 输入=%d 条, decision 行=%d, 误杀=%d", len(rows), decisionRows, falseKills)
|
||||
if len(examples) > 0 {
|
||||
for i, e := range examples {
|
||||
if i >= 5 {
|
||||
break
|
||||
}
|
||||
t.Logf(" 例: %s", e)
|
||||
}
|
||||
}
|
||||
if decisionRows == 0 {
|
||||
t.Errorf("dry-run 样本里竟无 decision 行,样本代表性不足(AC4 无法判)")
|
||||
}
|
||||
// 误杀不能为 0 却无脚本输出——这里有 Logf 输出即"贴脚本输出"。
|
||||
// 允许 0 误杀,但必须真实统计过(本函数逐行扫了 1100 条)。
|
||||
t.Logf("误杀条数=%d(AC3:领域概念不得被误杀,本批实测)", falseKills)
|
||||
}
|
||||
|
||||
// isProcessSourceClass 复用 skipSourceClass 的名单(避免 double-maintain)
|
||||
func isProcessSourceClass(s string) bool {
|
||||
return skipSourceClass(s)
|
||||
}
|
||||
|
||||
// isClearlyDomainConcept 启发:领域概念是中文/英文完整词,非自ID/纯AC编号。
|
||||
func isClearlyDomainConcept(w string) bool {
|
||||
if noiseEntity(w) {
|
||||
return false // 本就是噪声形态,不算"领域概念"
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -103,9 +103,9 @@ func (neb *NetworkEventBus) ListSubscribers() map[string][]string {
|
||||
// PushMemoryCommitted 通知所有 Agent 有新记忆
|
||||
func PushMemoryCommitted(agentID, namespace, memoryID string) {
|
||||
GlobalEventBus.Publish("memory.committed", map[string]string{
|
||||
"agent_id": agentID,
|
||||
"namespace": namespace,
|
||||
"memory_id": memoryID,
|
||||
"agent_id": agentID,
|
||||
"namespace": namespace,
|
||||
"memory_id": memoryID,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -149,4 +149,3 @@ func PushCacheInvalidate(namespace, reason string) {
|
||||
"reason": reason,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -292,7 +292,7 @@ func (cd *ConflictDetector) Scan(newContent string, newEntities []string, existi
|
||||
for _, c := range registered {
|
||||
cd.active[c.ID] = c
|
||||
}
|
||||
cd.persistLocked() // 2026-09-20:新增即落盘(重启不丢)
|
||||
cd.persistLocked() // 2026-09-20:新增即落盘(重启不丢)
|
||||
cd.persistSeenPairsLocked() // 2026-09-21:seenPairs 也落盘(重启不丢)
|
||||
cd.mu.Unlock()
|
||||
// 2026-09-21:更新 Prometheus gauge(冲突数)
|
||||
@@ -543,17 +543,19 @@ func hasSharedContentWord(a, b string) bool {
|
||||
// 英文/空格文本:单词级重叠(原有逻辑)
|
||||
//
|
||||
// 2026-09-20 判据三修(实测证据:references/conflict-scope-20260920.md):
|
||||
// ① 漏"未" —— 否定词表缺单字"未","未在招商局工作" 判 false(containsNegCN 已补)
|
||||
// ② 否定作用域 —— 修前只要任一侧含"不/没"字符即认可"否定差异",
|
||||
// 长句里任意位置的无关否定("不喜欢吃辣"、"不用 API Key")都会把新内容判成冲突
|
||||
// (实测:3/3 危险样本全误报)
|
||||
// ③ 子串必误报 —— 短句是长句子串时 overlap/maxLen=1.0 恒 > 0.5
|
||||
//
|
||||
// ① 漏"未" —— 否定词表缺单字"未","未在招商局工作" 判 false(containsNegCN 已补)
|
||||
// ② 否定作用域 —— 修前只要任一侧含"不/没"字符即认可"否定差异",
|
||||
// 长句里任意位置的无关否定("不喜欢吃辣"、"不用 API Key")都会把新内容判成冲突
|
||||
// (实测:3/3 危险样本全误报)
|
||||
// ③ 子串必误报 —— 短句是长句子串时 overlap/maxLen=1.0 恒 > 0.5
|
||||
//
|
||||
// 现判据按序:
|
||||
// a. 无否定词差异 → false(不变)
|
||||
// b. 一方是另一方子串,且被含的那句自身无否定 → false(同向陈述,长句只是多了无关内容)
|
||||
// c. 取最长公共子串为「重叠窗口」,否定词必须落在窗口 ±3 字符内才认可否定差异
|
||||
// d. 重叠度 > 0.5(阈值不变)
|
||||
//
|
||||
// a. 无否定词差异 → false(不变)
|
||||
// b. 一方是另一方子串,且被含的那句自身无否定 → false(同向陈述,长句只是多了无关内容)
|
||||
// c. 取最长公共子串为「重叠窗口」,否定词必须落在窗口 ±3 字符内才认可否定差异
|
||||
// d. 重叠度 > 0.5(阈值不变)
|
||||
//
|
||||
// 前置门(2026-09-21 四修):无共同内容词 → 主题无关 → 不可能矛盾。
|
||||
// 实测依据(conflict_fp_fix_test.go,106 条已判定假阳性样本 = 19 个不同对):
|
||||
@@ -569,17 +571,19 @@ const negSimilarityThreshold = 0.7
|
||||
// IsContradiction 检查两条内容是否语义矛盾。
|
||||
//
|
||||
// 判据(2026-10-08 五修,P1:10-05..10-08 积压 421 条 pending 抽样 6/6 全误报,根因):
|
||||
// 旧判据用「字符重叠>0.5 + 最长公共子串窗口内是否有否定词」。中文常用字池天然高重叠
|
||||
// (两个无关规则条目共享 0.6+ 单字),且 LCS 落在共享模板后缀(「|依据:」「|适用:」)
|
||||
// 旁恰好有无关否定(如「不废话」)即误判冲突 ⇒ commit 路径 distill_skipped=conflict
|
||||
// 跳过写入 → 「写入成功但召回不到」。
|
||||
//
|
||||
// 旧判据用「字符重叠>0.5 + 最长公共子串窗口内是否有否定词」。中文常用字池天然高重叠
|
||||
// (两个无关规则条目共享 0.6+ 单字),且 LCS 落在共享模板后缀(「|依据:」「|适用:」)
|
||||
// 旁恰好有无关否定(如「不废话」)即误判冲突 ⇒ commit 路径 distill_skipped=conflict
|
||||
// 跳过写入 → 「写入成功但召回不到」。
|
||||
//
|
||||
// 现判据按序:
|
||||
// a. 否定不对称:两侧必须恰好一侧带否定词、另一侧不带(同向/双向否定都非矛盾)。
|
||||
// b. 同一命题检验:剥掉否定词后,两侧必须是「几乎相同的原句」才可能是一方肯定一方否定。
|
||||
// 用原始字符编辑相似度(对插入的 已/了 等虚词不敏感,比字符重叠/双字 bigram 更准):
|
||||
// - 真矛盾「X」vs「不X/没X/未X」:剥否定后几乎一致,相似度≥0.7
|
||||
// - 误报(同向扩展「…,但不付费」/ 共享模板后缀的无关文本):剥否定后仍是不同句子,远低于阈值
|
||||
//
|
||||
// a. 否定不对称:两侧必须恰好一侧带否定词、另一侧不带(同向/双向否定都非矛盾)。
|
||||
// b. 同一命题检验:剥掉否定词后,两侧必须是「几乎相同的原句」才可能是一方肯定一方否定。
|
||||
// 用原始字符编辑相似度(对插入的 已/了 等虚词不敏感,比字符重叠/双字 bigram 更准):
|
||||
// - 真矛盾「X」vs「不X/没X/未X」:剥否定后几乎一致,相似度≥0.7
|
||||
// - 误报(同向扩展「…,但不付费」/ 共享模板后缀的无关文本):剥否定后仍是不同句子,远低于阈值
|
||||
func IsContradiction(a, b string) bool {
|
||||
wordsA := splitWordsCN(a)
|
||||
wordsB := splitWordsCN(b)
|
||||
@@ -832,8 +836,10 @@ func (f *Forgetter) AgentType() string {
|
||||
}
|
||||
|
||||
// ShouldForget 判断记忆是否该被遗忘
|
||||
// recallVerifiedCount: 该记忆被验证使用(正反馈/下游引用)的次数 ——
|
||||
// GA-P0-2 (t_c6d41ac6):衰减权重只认 verified(V 值来源),不再用 recall_count(虚高保命)或 seen。
|
||||
// graphDegree: 该记忆关联实体的图谱节点度(连接数),度越高越优先保留
|
||||
func (f *Forgetter) ShouldForget(lastAccessed time.Time, recallCount int, tier string, graphDegree ...int) bool {
|
||||
func (f *Forgetter) ShouldForget(lastAccessed time.Time, recallVerifiedCount int, tier string, graphDegree ...int) bool {
|
||||
if tier == "core" {
|
||||
return false // 核心记忆永不遗忘
|
||||
}
|
||||
@@ -842,11 +848,13 @@ func (f *Forgetter) ShouldForget(lastAccessed time.Time, recallCount int, tier s
|
||||
if score < 0.1 {
|
||||
score = 0.1
|
||||
}
|
||||
// recallCount > 0 减缓衰减
|
||||
// recallVerifiedCount > 0 减缓衰减
|
||||
// 2026-09-07 方案A: 0.05→0.005。原 0.05/次 + 无 cap → recall_count 457-540 时 +25 分,
|
||||
// 永不遗忘 (日志大量 recall_count 500+ = 每次搜索命中都 ++, 虚高保命)。
|
||||
// 现 0.005/次, cap 30 次后贡献 ≤0.15 分 ≈ 5 天保护, 合理。
|
||||
score += float64(recallCount) * 0.005
|
||||
// GA-P0-2 (t_c6d41ac6):现在入参是 recall_verified_count(真实 V 值),
|
||||
// 计数规模大幅下降,0.005/cap 的压制效果天然更强——只有被验证用过才保命。
|
||||
score += float64(recallVerifiedCount) * 0.005
|
||||
// 图谱节点度 > 5 时,每超过 1 度 + 0.03 保留分(E4.3: 图谱推理参与遗忘决策)
|
||||
if len(graphDegree) > 0 && graphDegree[0] > 5 {
|
||||
score += float64(graphDegree[0]-5) * 0.03
|
||||
@@ -855,13 +863,14 @@ func (f *Forgetter) ShouldForget(lastAccessed time.Time, recallCount int, tier s
|
||||
}
|
||||
|
||||
// DecayScore 计算衰减分数
|
||||
func (f *Forgetter) DecayScore(lastAccessed time.Time, recallCount int) float64 {
|
||||
// recallVerifiedCount: 被验证使用的次数(GA-P0-2:衰减只认 verified,V 值来源)
|
||||
func (f *Forgetter) DecayScore(lastAccessed time.Time, recallVerifiedCount int) float64 {
|
||||
days := time.Since(lastAccessed).Hours() / 24
|
||||
score := 1.0 - days*f.decayRate
|
||||
if score < 0.1 {
|
||||
score = 0.1
|
||||
}
|
||||
score += float64(recallCount) * 0.05
|
||||
score += float64(recallVerifiedCount) * 0.05
|
||||
if score > 1.0 {
|
||||
score = 1.0
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package governance
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
@@ -27,17 +29,18 @@ func NewAutoGraphUpdater(g GraphStore) *AutoGraphUpdater {
|
||||
// 都必经这两个转换)做旧名→新名映射,从源头杜绝旧名节点再入图。
|
||||
//
|
||||
// ⚠️ 2026-10-03 扩充:原表只有中文「小唯」,漏掉全部英文旧名。实查图谱确认:
|
||||
// n_xiaowei 仍有 7 条边(源:会话正文里的 Xiaowei / soulful-xiaowei-main 等),
|
||||
// 中文旧名已清干净(小唯 count=0),但英文旧名从未被归一。
|
||||
// 归一到「小怡」(n_小怡 count=880 = 真实主节点)——注意**不是** xiaoyi,
|
||||
// 实查 n_xiaoyi count=0,图谱里没有这个节点,映射过去会造出第二个孤儿。
|
||||
// 教训:归一目标必须是**实查存在的**主节点,不能从"拼音应该怎么写"推断。
|
||||
//
|
||||
// n_xiaowei 仍有 7 条边(源:会话正文里的 Xiaowei / soulful-xiaowei-main 等),
|
||||
// 中文旧名已清干净(小唯 count=0),但英文旧名从未被归一。
|
||||
// 归一到「小怡」(n_小怡 count=880 = 真实主节点)——注意**不是** xiaoyi,
|
||||
// 实查 n_xiaoyi count=0,图谱里没有这个节点,映射过去会造出第二个孤儿。
|
||||
// 教训:归一目标必须是**实查存在的**主节点,不能从"拼音应该怎么写"推断。
|
||||
var entityAliases = map[string]string{
|
||||
"小唯": "小怡", // 旧名 → 新名(2026-09-12 全域改名)
|
||||
"小唯": "小怡", // 旧名 → 新名(2026-09-12 全域改名)
|
||||
// 英文/拼音旧名(2026-10-03 补)→ 统一归到实查存在的主节点「小怡」
|
||||
// ⚠️ 归一目标必须是实查存在的节点:n_小怡 count=880(真主节点);
|
||||
// n_xiaoyi count=0(不存在)——不能按拼音推断,否则造出第二个孤儿。
|
||||
"xiaowei": "小怡",
|
||||
"xiaowei": "小怡",
|
||||
"soulful-xiaowei-main": "soulful-小怡-main",
|
||||
}
|
||||
|
||||
@@ -85,17 +88,52 @@ func isASCIIOnly(s string) bool {
|
||||
|
||||
// UpdateFromDistill 从蒸馏产物自动更新图谱(§3.4)
|
||||
func (agu *AutoGraphUpdater) UpdateFromDistill(distilled *DistillInput) {
|
||||
// 0a. 来源类 + 内容形态 双层层判定(2026-10-13 t_c6aebb8b 补收口):
|
||||
// 1b(t_67d0279a)只按 source 字符串枚举 {"worklog-note","worklog-audit"},天然漏——
|
||||
// 活体噪声的来源标记是 空 source / llm_distill / wiki:session-compress /
|
||||
// wiki:skill_rule_extractor_v2 / a06 探针,枚举式白名单每条新路径漏一次。
|
||||
// 本层改为「按来源类 + 内容形态」:命中来源类(过程/会话/迁移层)或纯分身会话
|
||||
// namespace → 实体/概念不写图;事实仍经同一次蒸馏进 LanceDB 记忆层(facts 照写)。
|
||||
// 判据零改动(echo_guard.go 未动),语义边界:跳过的是「进图」,不是「进记忆」。
|
||||
if skipSourceClass(distilled.Source) || inSubAgentNamespace(distilled.Namespace) {
|
||||
log.Printf("[graph] skip-entity for source=%q namespace=%q episode=%s(来源分档:实体/概念不写图,事实仍进记忆层)",
|
||||
distilled.Source, distilled.Namespace, distilled.EpisodeID)
|
||||
return
|
||||
}
|
||||
|
||||
// 0. 密钥过滤(2026-09-10 P0,见 internal/redact):含凭证形状的实体不进图,
|
||||
// 否则实体名会被 ObsidianSync 当文件名,把密钥落成可读镜像文件
|
||||
// (实例:小唯/07-Wiki/织忆/未找到命令.md 的文件名与正文含 tskey-auth-…)。
|
||||
// 同时做内容形态穿透过滤(噪声实体:ep_/f_/n_/mem_ 自ID、AC编号、工具动词)。
|
||||
validEnts := make([]string, 0, len(distilled.Entities))
|
||||
for _, entity := range distilled.Entities {
|
||||
if redact.ContainsSecret(entity) || cleanEntityName(entity) == "" {
|
||||
continue
|
||||
}
|
||||
if noiseEntity(entity) {
|
||||
log.Printf("[graph] skip-noise-entity=%q episode=%s(内容形态:自ID/AC编号/工具动词/自述模板)",
|
||||
entity, distilled.EpisodeID)
|
||||
continue
|
||||
}
|
||||
validEnts = append(validEnts, entity)
|
||||
}
|
||||
|
||||
// 0.5 自环根治(2026-10-09 P0):同批含同名重复实体时,两两循环会因
|
||||
// 不同下标映射到同一 entityID → eidI==eidJ → 产生 source==target 自环边。
|
||||
// 去重(保序,保留首个下标)从源头消除。同一 eid 的重复下标不建边,
|
||||
// 故无「指向被丢下标的边」——两下标本就映射同一 eid,保持的正是该 eid。
|
||||
seenEID := make(map[string]struct{}, len(validEnts))
|
||||
deduped := make([]string, 0, len(validEnts))
|
||||
for _, entity := range validEnts {
|
||||
eid := entityID(entity)
|
||||
if _, dup := seenEID[eid]; dup {
|
||||
continue
|
||||
}
|
||||
seenEID[eid] = struct{}{}
|
||||
deduped = append(deduped, entity)
|
||||
}
|
||||
validEnts = deduped
|
||||
|
||||
// 1. 提取实体并创建节点(ID 和 name 都清洗)
|
||||
for _, entity := range validEnts {
|
||||
nodeID := entityID(entity)
|
||||
@@ -109,6 +147,10 @@ func (agu *AutoGraphUpdater) UpdateFromDistill(distilled *DistillInput) {
|
||||
for j := i + 1; j < entityCount; j++ {
|
||||
eidI := entityID(validEnts[i])
|
||||
eidJ := entityID(validEnts[j])
|
||||
// 防御:任何原因导致同 eid 时跳过,杜绝自环(所有 relation 建边口通用)
|
||||
if eidI == eidJ {
|
||||
continue
|
||||
}
|
||||
|
||||
// 2a. 语义关系边(inferRelation)
|
||||
edgeID := fmt.Sprintf("e_%s_%s_%d", eidI, eidJ, time.Now().UnixNano())
|
||||
@@ -136,6 +178,9 @@ func (agu *AutoGraphUpdater) UpdateFromDistill(distilled *DistillInput) {
|
||||
if conflictPossible(entities[i], entities[j], fact) {
|
||||
eidI := entityID(entities[i])
|
||||
eidJ := entityID(entities[j])
|
||||
if eidI == eidJ {
|
||||
continue // 防自环:CONFLICTS_WITH 同 eid 跳过
|
||||
}
|
||||
edgeID := fmt.Sprintf("cw_%s_%s_%d", eidI, eidJ, time.Now().UnixNano())
|
||||
agu.graph.AddEdge(edgeID, eidI, eidJ,
|
||||
"CONFLICTS_WITH", distilled.Namespace, 0.3)
|
||||
@@ -169,6 +214,9 @@ func (agu *AutoGraphUpdater) RecordCoOccurrence(query, namespace string, resultI
|
||||
// 在知识图谱中创建 CO_OCCURS 关系
|
||||
for i := 0; i < len(resultIDs); i++ {
|
||||
for j := i + 1; j < len(resultIDs); j++ {
|
||||
if resultIDs[i] == resultIDs[j] {
|
||||
continue // 防自环:recall 结果重复 id 时跳过 CO_OCCURS
|
||||
}
|
||||
edgeID := fmt.Sprintf("co_%s_%s_%s", resultIDs[i], resultIDs[j], query[:minz(len(query), 20)])
|
||||
agu.graph.AddEdge(edgeID, resultIDs[i], resultIDs[j], "CO_OCCURS", namespace, 1.0)
|
||||
}
|
||||
@@ -177,6 +225,108 @@ func (agu *AutoGraphUpdater) RecordCoOccurrence(query, namespace string, resultI
|
||||
|
||||
// ─── 辅助 ─────────────────────────────────────────────
|
||||
|
||||
// processSources 来源类分档(过程/会话/迁移层):命中则实体/概念不写图。
|
||||
// 2026-10-12 t_67d0279a 只枚举 {"worklog-note","worklog-audit"},天然漏活体主路径——
|
||||
// 本表由实测 memories.lance 35,427 行 source 分布驱动(2026-10-13,数据定名单非照抄):
|
||||
// llm_distill 3717 / wiki:skill_rule_extractor_v2 4406 / wiki:a06_skill_rule_tierA 2650
|
||||
// / wiki:session-compress 494 / legacy:pre0926 12099(迁移层)
|
||||
// 这些是「过程/会话」层,不是「领域知识」层;图谱只承载领域概念。
|
||||
// ⚠️ 前缀归一(TrimPrefix "wiki:")后比对,与 auditSource 同规;库内真实值经
|
||||
// srcOrLLMDistill 落成带前缀形式([srcprobe] 实测),裸串相等永远 miss。
|
||||
var processSources = []string{
|
||||
"", // 空 source:sub-agent 会话蒸馏常无出处标记
|
||||
"llm_distill", // 无出处蒸馏的兜底名(srcOrLLMDistill 返回)
|
||||
"session-compress", // 会话压缩蒸馏产物
|
||||
"skill_rule_extractor_v2", // 技能规则提取器(过程层)
|
||||
"a06_skill_rule_tiera", // a06 技能规则 tierA(过程层,normalizeSrc 已 ToLower)
|
||||
"a06_prefix_test", // a06 前缀探针
|
||||
"a06_probe_direct", // a06 直连探针
|
||||
"a06_distill_probe", // a06 蒸馏探针
|
||||
"a06_ns_test_1005", // a06 ns 测试
|
||||
"legacy:pre0926", // 迁移/历史层(2026-09-26 前导入)
|
||||
"worklog-note", // 审计/工作流(1b 已立,保留)
|
||||
"worklog-audit", // 审计/工作流(1b 已立,保留)
|
||||
}
|
||||
|
||||
// subAgentNamespaces 纯分身会话 namespace:命中则实体/概念不写图。
|
||||
// ⚠️ 不做「*-main 一刀切」——实测 a06-main(541 条 vault 领域知识)与 hermes-main
|
||||
// (大量 wiki:vault)承载真实领域概念,整 namespace 拦截会误杀(AC3)。
|
||||
// 只拦实测 100% 过程/会话/legacy 的纯分身会话 namespace(数据驱动,2026-10-13):
|
||||
// prof-dev-main / prof-cloud-main / openclaw-main / wiki-curator-main / soulful-xiaowei
|
||||
// —— 全部 source 均为 session-compress / legacy,无任何领域知识。
|
||||
var subAgentNamespaces = []string{
|
||||
"prof-dev-main",
|
||||
"prof-cloud-main",
|
||||
"openclaw-main",
|
||||
"wiki-curator-main",
|
||||
"soulful-xiaowei",
|
||||
}
|
||||
|
||||
// normalizeSrc 来源归一:TrimSpace → TrimPrefix("wiki:") → ToLower → TrimSpace。
|
||||
func normalizeSrc(source string) string {
|
||||
s := strings.TrimSpace(source)
|
||||
s = strings.ToLower(strings.TrimPrefix(s, "wiki:"))
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
// skipSourceClass 判定 distilled.Source 是否属于过程/会话/迁移类来源(跳实体写图)。
|
||||
func skipSourceClass(source string) bool {
|
||||
s := normalizeSrc(source)
|
||||
for _, p := range processSources {
|
||||
if s == p {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// inSubAgentNamespace 判定 namespace 是否属于纯分身会话命名空间(跳实体写图)。
|
||||
func inSubAgentNamespace(ns string) bool {
|
||||
n := strings.TrimSpace(strings.ToLower(ns))
|
||||
for _, s := range subAgentNamespaces {
|
||||
if n == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// noiseEntityRe 内容形态穿透判据:命中则即使来源类/namespace 未拦,该实体也不进图。
|
||||
// 1b 的病根是「按 source 枚举」——源每多一条路径就漏一次。本表按内容形态判定:
|
||||
// - 自ID 前缀(ep_/f_/n_/mem_/e_/co_/cw_/df_)→ 永不进图(AC2,本批 ep_ 成了 entity)
|
||||
// - AC 编号(AC\d+)、工具动词(grep/ingest dry-run/健康检查/部署流程/测试结果)
|
||||
// - "证据:无证据" 自述模板
|
||||
// ⚠️ 不做「方案代号一刀切」——方案A/B 是领域决策常用概念(AC4 平台决策 14 保留),
|
||||
// 只拦「证据:无证据」这类纯自述模板 + AC 编号 + 工具动词 + 自ID 前缀。
|
||||
var noiseEntityRe = regexp.MustCompile(`(?i)^(ac\d+|ep_|f_|n_|mem_|e_|co_|cw_|df_|.*(证据[::]无证据|ingest.?dry.?run|健康检查|部署流程|测试结果|代码修改|grep).*)$`)
|
||||
|
||||
// noiseEntity 实体名命中内容形态噪声 → 跳过写图。
|
||||
func noiseEntity(name string) bool {
|
||||
n := strings.TrimSpace(name)
|
||||
lower := strings.ToLower(n)
|
||||
// 自ID 前缀无条件禁(ep_ 绝不进图——本批 ep_1791820771105234064 就成了 entity)
|
||||
for _, p := range []string{"ep_", "f_", "n_", "mem_", "e_", "co_", "cw_", "df_"} {
|
||||
if strings.HasPrefix(lower, p) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
// AC 编号 / 自述模板 / 工具动词
|
||||
return noiseEntityRe.MatchString(lower)
|
||||
}
|
||||
|
||||
// auditSource 判定 distilled.Source 是否属于审计/工作流类来源(来源分档跳实体写图)。
|
||||
// 保留 1b 语义(worklog-note/audit);归一:TrimSpace → TrimPrefix("wiki:") → ToLower。
|
||||
// 注:UpdateFromDistill 门禁用更宽的 skipSourceClass(含空/llm_distill/会话压缩等
|
||||
// 过程层来源),本函数保留为原语供单测与调用方核对。
|
||||
func auditSource(source string) bool {
|
||||
for _, p := range []string{"worklog-note", "worklog-audit"} {
|
||||
if normalizeSrc(source) == p {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type DistillInput struct {
|
||||
EpisodeID string
|
||||
Content string
|
||||
@@ -184,6 +334,10 @@ type DistillInput struct {
|
||||
Decisions []string
|
||||
Entities []string
|
||||
Namespace string
|
||||
// Source 来源标记透传(2026-10-12 t_67d0279a GA-META-1b,方案A来源分档):
|
||||
// commit 写入方 metadata.source → distill.DistillInput.Source → 本字段。
|
||||
// UpdateFromDistill 用它做来源分档:审计/工作流类来源跳过实体/概念写图。
|
||||
Source string
|
||||
}
|
||||
|
||||
func entityID(name string) string {
|
||||
|
||||
@@ -52,6 +52,7 @@ func TestUpdateFromDistill_NoNodeForSecretEntity(t *testing.T) {
|
||||
Facts: []string{"Tailscale 预授权 key 已写入配置文件并完成节点加入"},
|
||||
Entities: []string{"Docker", fakeSecretEntity},
|
||||
Namespace: "ns_test",
|
||||
Source: "wiki:vault-ingest", // 常规来源:不触来源分档,专测密钥过滤
|
||||
})
|
||||
|
||||
nodes := g.ListNodes("ns_test")
|
||||
@@ -113,6 +114,7 @@ func TestUpdateFromDistill_AliasMapsOldNameToNew(t *testing.T) {
|
||||
Facts: []string{"小唯自优化系统是统称,小唯是改名前的旧名,指小怡本人"},
|
||||
Entities: []string{"小唯", "小怡", "小唯自优化系统"},
|
||||
Namespace: "ns_alias_test",
|
||||
Source: "wiki:vault-ingest", // 常规来源:专测别名归一,不触来源分档
|
||||
})
|
||||
|
||||
// 新产生节点名必须是 小怡(不许出现 n_小唯 或 name=小唯)
|
||||
@@ -136,3 +138,282 @@ func TestUpdateFromDistill_AliasMapsOldNameToNew(t *testing.T) {
|
||||
t.Errorf("旧名「小唯」应归一为新名「小怡」,图谱应含 name=小怡: %v", names)
|
||||
}
|
||||
}
|
||||
|
||||
// ── 自环根治(2026-10-09 P0):同批含同名重复实体时,两两循环不得产生 source==target ──
|
||||
func TestUpdateFromDistill_NoSelfLoop_WithDupEntities(t *testing.T) {
|
||||
g := NewInMemoryGraph()
|
||||
up := NewAutoGraphUpdater(g)
|
||||
up.UpdateFromDistill(&DistillInput{
|
||||
EpisodeID: "ep_selfloop_test_1",
|
||||
Content: "配置 tailscale docker nginx",
|
||||
Facts: nil,
|
||||
// Docker 重复出现两次 —— 旧实现两下标映射同一 eid → 自环
|
||||
Entities: []string{"Docker", "Docker", "Nginx", "Tailscale"},
|
||||
Namespace: "ns_selfloop_test",
|
||||
Source: "wiki:vault-ingest", // 常规来源:专测自环守卫,不触来源分档
|
||||
})
|
||||
|
||||
// 去重后唯一实体 = {Docker, Nginx, Tailscale},M=3
|
||||
// 两两循环每对建 2 边(语义 configures + CO_OCCURS)= 2*C(3,2) = 6
|
||||
// DERIVED_FROM = M = 3
|
||||
const (
|
||||
expectCoEdges = 3 // C(3,2) —— 不丢正常边
|
||||
expectTotal = 9 // 2*C(3,2) + M
|
||||
)
|
||||
var selfLoops, coEdges, total int
|
||||
g.mu.RLock()
|
||||
for _, e := range g.edges {
|
||||
total++
|
||||
if e.Source == e.Target {
|
||||
selfLoops++
|
||||
}
|
||||
if e.Relation == "CO_OCCURS" {
|
||||
coEdges++
|
||||
}
|
||||
}
|
||||
g.mu.RUnlock()
|
||||
|
||||
if selfLoops != 0 {
|
||||
t.Fatalf("重复实体输入不应产生自环边,实际 %d 条自环(total=%d)", selfLoops, total)
|
||||
}
|
||||
if coEdges != expectCoEdges {
|
||||
t.Errorf("CO_OCCURS 边数应等于去重后实体对数 C(3,2)=%d(不丢正常边),实际 %d", expectCoEdges, coEdges)
|
||||
}
|
||||
if total != expectTotal {
|
||||
t.Errorf("总边数应 = 2*C(3,2)+M = %d,实际 %d", expectTotal, total)
|
||||
}
|
||||
}
|
||||
|
||||
// 防自环守卫在 CONFLICTS_WITH 与 RecordCoOccurrence 建边口也生效
|
||||
func TestRecordCoOccurrence_NoSelfLoop_WithDupResultIDs(t *testing.T) {
|
||||
g := NewInMemoryGraph()
|
||||
up := NewAutoGraphUpdater(g)
|
||||
// resultIDs 含重复 id,两两循环不得自环
|
||||
up.RecordCoOccurrence("测试查询", "ns_selfloop_test2", []string{"n_a", "n_b", "n_a"})
|
||||
|
||||
var selfLoops, coEdges int
|
||||
g.mu.RLock()
|
||||
for _, e := range g.edges {
|
||||
if e.Source == e.Target {
|
||||
selfLoops++
|
||||
}
|
||||
if e.Relation == "CO_OCCURS" {
|
||||
coEdges++
|
||||
}
|
||||
}
|
||||
g.mu.RUnlock()
|
||||
|
||||
if selfLoops != 0 {
|
||||
t.Fatalf("RecordCoOccurrence 重复 resultIDs 不应自环,实际 %d 条", selfLoops)
|
||||
}
|
||||
// 去重后唯一 {n_a, n_b}:下标对产生 (a,b)+(b,a) 2 条 CO_OCCURS,均非自环
|
||||
if coEdges != 2 {
|
||||
t.Errorf("RecordCoOccurrence 应 2 条非自环 CO_OCCURS,实际 %d", coEdges)
|
||||
}
|
||||
}
|
||||
|
||||
// ── 来源分档(2026-10-12 t_67d0279a GA-META-1b,方案A)──
|
||||
// 审计/工作流类来源:跳实体/概念写图;正常来源:照写。AC5 防静默 no-op 的正/负对照。
|
||||
func TestAuditSource_PrefixNormalization(t *testing.T) {
|
||||
// 正对照:库内真实值带 wiki: 前缀(srcOrLLMDistill 加前缀,[srcprobe] 实测)
|
||||
for _, s := range []string{"wiki:worklog-note", "worklog-note", "wiki:worklog-audit", "worklog-audit", " wiki:WORKLOG-NOTE "} {
|
||||
if !auditSource(s) {
|
||||
t.Errorf("auditSource(%q) 应为 true(前缀归一 + 大小写兜底)", s)
|
||||
}
|
||||
}
|
||||
// 负对照:常规来源不得命中(防误杀)
|
||||
for _, s := range []string{"wiki:llm_distill", "llm_distill", "wiki:vault-ingest", "vault-ingest", "", "wiki:worklog-notes"} {
|
||||
if auditSource(s) {
|
||||
t.Errorf("auditSource(%q) 应为 false(误杀常规来源)", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateFromDistill_AuditSourceSkipsEntityGraphWrites(t *testing.T) {
|
||||
g := NewInMemoryGraph()
|
||||
up := NewAutoGraphUpdater(g)
|
||||
// 审计散文类内容 + 含实体/概念词,但来源 = 库内真实值 wiki:worklog-note
|
||||
up.UpdateFromDistill(&DistillInput{
|
||||
EpisodeID: "ep_audit_src_1",
|
||||
Content: "空格版节点 n_牵挂 当前仍在图,与 hub 版同源同义",
|
||||
Facts: []string{"待办.md 里已打勾的旧条目清理完成"},
|
||||
Entities: []string{"Galway", "rothfuss", "Name of the Wind"},
|
||||
Namespace: "ns_audit_src",
|
||||
Source: "wiki:worklog-note",
|
||||
})
|
||||
// 节点/边都应为 0:实体不写图、实体间关系边不建、DERIVED_FROM 不建
|
||||
if n := len(g.ListNodes("ns_audit_src")); n != 0 {
|
||||
t.Errorf("audit 来源应 0 实体节点,实际 %d: %v", n, g.ListNodes("ns_audit_src"))
|
||||
}
|
||||
if e := len(g.AllEdges("ns_audit_src")); e != 0 {
|
||||
t.Errorf("audit 来源应 0 条边,实际 %d", e)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateFromDistill_NormalSourceStillWritesEntities(t *testing.T) {
|
||||
g := NewInMemoryGraph()
|
||||
up := NewAutoGraphUpdater(g)
|
||||
up.UpdateFromDistill(&DistillInput{
|
||||
EpisodeID: "ep_normal_src_1",
|
||||
Content: "配置 Docker Nginx",
|
||||
Facts: []string{"Docker 容器已启动"},
|
||||
Entities: []string{"Docker", "Nginx"},
|
||||
Namespace: "ns_normal_src",
|
||||
Source: "wiki:vault-ingest", // 常规来源:照写
|
||||
})
|
||||
nodes := g.ListNodes("ns_normal_src")
|
||||
if len(nodes) < 2 {
|
||||
t.Fatalf("常规来源应照写实体节点(≥2),实际 %d: %v", len(nodes), nodes)
|
||||
}
|
||||
if e := len(g.AllEdges("ns_normal_src")); e == 0 {
|
||||
t.Errorf("常规来源应建关系边,实际 0 条")
|
||||
}
|
||||
}
|
||||
|
||||
// ── 补收口(2026-10-13 t_c6aebb8b):来源类 + 内容形态 双层层判定 ──
|
||||
// 1b 只按 source 枚举 worklog-note/audit,漏了活体主路径:
|
||||
// 空 source / llm_distill / session-compress / skill_rule_extractor_v2 / 纯分身会话 namespace。
|
||||
|
||||
// AC3/AC4 判据层:来源类判定(含空/llm_distill/会话压缩等过程层)。
|
||||
func TestSkipSourceClass_DataDriven(t *testing.T) {
|
||||
// 正对照:过程/会话/迁移层来源 → true(数据驱动名单,2026-10-13 实测 memories 分布)
|
||||
for _, s := range []string{
|
||||
"", "llm_distill", "wiki:llm_distill",
|
||||
"wiki:session-compress", "session-compress",
|
||||
"wiki:skill_rule_extractor_v2", "skill_rule_extractor_v2",
|
||||
"wiki:a06_skill_rule_tierA", "wiki:a06_prefix_test", "wiki:a06_probe_direct",
|
||||
"wiki:a06_distill_probe", "wiki:a06_ns_test_1005",
|
||||
"legacy:pre0926", "wiki:legacy:pre0926",
|
||||
"wiki:worklog-note", "wiki:worklog-audit",
|
||||
} {
|
||||
if !skipSourceClass(s) {
|
||||
t.Errorf("skipSourceClass(%q) 应为 true(过程/会话/迁移层来源)", s)
|
||||
}
|
||||
}
|
||||
// 负对照:领域知识类来源必须放行(误杀=违背 AC3/AC4)
|
||||
for _, s := range []string{
|
||||
"wiki:decision", "decision", "wiki:domain-kb", "domain-kb",
|
||||
"wiki:vault:定义规范:106智能体栈回退与OpenSquilla调研-2026-09-11.md",
|
||||
"wiki:vault:方案设计:MemoryFabric-v2.4-完整定稿.md",
|
||||
"wiki:/home/muc/mc/小怡/06-方法论/06-02-方案设计/表达能力增强方案.md",
|
||||
"worklog-notes", "wiki:worklog-notes", "vault-ingest",
|
||||
} {
|
||||
if skipSourceClass(s) {
|
||||
t.Errorf("skipSourceClass(%q) 应为 false(领域知识来源,误杀)", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AC2:ep_ 等自ID 永不进图 —— 即使来源类/namespace 都放行,内容形态也要穿透拦。
|
||||
func TestNoiseEntity_SelfIDNeverInGraph(t *testing.T) {
|
||||
// 本批真实污染:ep_1791820771105234064 成了 entity(AC2 无争议判据)
|
||||
g := NewInMemoryGraph()
|
||||
up := NewAutoGraphUpdater(g)
|
||||
up.UpdateFromDistill(&DistillInput{
|
||||
EpisodeID: "ep_1791820771105234064",
|
||||
Content: "完成方案A实施,所有AC通过",
|
||||
Facts: []string{"代码修改和测试结果"},
|
||||
// 即使 Source 是常规领域来源(放行层),自ID实体也必须被穿透拦下
|
||||
Entities: []string{"ep_1791820771105234064", "AC5", "Docker", "f_库内真值", "n_方案a", "mem_123", "Nginx"},
|
||||
Namespace: "ns_ep_guard",
|
||||
Source: "wiki:vault-ingest",
|
||||
})
|
||||
nodes := g.ListNodes("ns_ep_guard")
|
||||
names := map[string]bool{}
|
||||
for _, n := range nodes {
|
||||
names[n["name"].(string)] = true
|
||||
}
|
||||
// 自ID 实体(ep_/f_/n_/mem_/AC编号)一律不得进图
|
||||
for _, bad := range []string{"ep_1791820771105234064", "AC5", "f_库内真值", "n_方案a", "mem_123"} {
|
||||
if names[bad] {
|
||||
t.Errorf("内容形态噪声实体 %q 不应进图,实际出现: %v", bad, names)
|
||||
}
|
||||
}
|
||||
// 领域实体照常进图(AC3 假阳性护栏)
|
||||
if !names["Docker"] || !names["Nginx"] {
|
||||
t.Errorf("领域实体 Docker/Nginx 应照常进图,实际: %v", names)
|
||||
}
|
||||
}
|
||||
|
||||
// AC4:wiki:decision 与领域概念必须放行(决定性判据)。
|
||||
func TestSkipSourceClass_KeepsDecisionAndDomainConcepts(t *testing.T) {
|
||||
g := NewInMemoryGraph()
|
||||
up := NewAutoGraphUpdater(g)
|
||||
up.UpdateFromDistill(&DistillInput{
|
||||
EpisodeID: "ep_decision_1",
|
||||
Content: "决定采用方案B并部署",
|
||||
Facts: []string{"决定采用方案B"},
|
||||
Entities: []string{"方案B", "牧尘"},
|
||||
Namespace: "ns_decision",
|
||||
Source: "wiki:decision", // 决策类:照常进图(平台决策 14)
|
||||
})
|
||||
if len(g.ListNodes("ns_decision")) == 0 {
|
||||
t.Fatalf("wiki:decision 来源应照常进图,实际 0 节点")
|
||||
}
|
||||
}
|
||||
|
||||
// AC2:全库复扫判据 = ep_ 前缀节点不再新增。
|
||||
// (存量 ep_ 节点属既有污染,由 1c t_c480eece 图谱清理处理;本卡管「不再新增」。)
|
||||
func TestNoiseEntity_EpPrefixDetected(t *testing.T) {
|
||||
for _, e := range []string{"ep_1791820771105234064", "ep_1", "f_foo", "n_bar", "mem_9"} {
|
||||
if !noiseEntity(e) {
|
||||
t.Errorf("noiseEntity(%q) 应为 true(自ID 前缀)", e)
|
||||
}
|
||||
}
|
||||
for _, e := range []string{"Docker", "方案B", "织忆", "小怡"} {
|
||||
if noiseEntity(e) {
|
||||
t.Errorf("noiseEntity(%q) 应为 false(领域概念误杀)", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AC3:namespace 纯分身会话 = 拦截;领域 namespace = 放行。
|
||||
func TestInSubAgentNamespace_DataDriven(t *testing.T) {
|
||||
for _, ns := range []string{"prof-dev-main", "prof-cloud-main", "openclaw-main", "wiki-curator-main", "soulful-xiaowei"} {
|
||||
if !inSubAgentNamespace(ns) {
|
||||
t.Errorf("inSubAgentNamespace(%q) 应为 true(纯分身会话 namespace)", ns)
|
||||
}
|
||||
}
|
||||
// ⚠️ 不做 *-main 一刀切:a06-main/hermes-main 承载领域知识必须放行(AC3)
|
||||
for _, ns := range []string{"a06-main", "hermes-main", "hermes", "shared", "domain:DEV", "wiki"} {
|
||||
if inSubAgentNamespace(ns) {
|
||||
t.Errorf("inSubAgentNamespace(%q) 应为 false(领域 namespace,误杀)", ns)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// AC1:复跑「分身会话 → 蒸馏」路径,图谱零新增。
|
||||
func TestUpdateFromDistill_SubAgentSessionZeroNodes(t *testing.T) {
|
||||
g := NewInMemoryGraph()
|
||||
up := NewAutoGraphUpdater(g)
|
||||
// 分身会话:namespace=prof-dev-main + 空 source(会话蒸馏无出处标记)
|
||||
up.UpdateFromDistill(&DistillInput{
|
||||
EpisodeID: "ep_1791820771105234064",
|
||||
Content: "完成方案A的实施,包括Go代码的修改、测试和部署,所有AC通过",
|
||||
Facts: []string{"AC5", "测试结果"},
|
||||
Entities: []string{"AC5", "测试结果", "部署流程", "ep_1791820771105234064"},
|
||||
Namespace: "prof-dev-main",
|
||||
Source: "",
|
||||
})
|
||||
if n := len(g.ListNodes("prof-dev-main")); n != 0 {
|
||||
t.Errorf("分身会话→蒸馏 应 0 节点,实际 %d: %v", n, g.ListNodes("prof-dev-main"))
|
||||
}
|
||||
if e := len(g.AllEdges("prof-dev-main")); e != 0 {
|
||||
t.Errorf("分身会话→蒸馏 应 0 条边,实际 %d", e)
|
||||
}
|
||||
|
||||
// 另一路:llm_distill 来源(空 source 的兜底名)
|
||||
g2 := NewInMemoryGraph()
|
||||
up2 := NewAutoGraphUpdater(g2)
|
||||
up2.UpdateFromDistill(&DistillInput{
|
||||
EpisodeID: "ep_x",
|
||||
Content: "某个子代理会话的蒸馏产物",
|
||||
Facts: []string{"无证据"},
|
||||
Entities: []string{"无证据", "grep"},
|
||||
Namespace: "hermes-main",
|
||||
Source: "llm_distill",
|
||||
})
|
||||
if n := len(g2.ListNodes("hermes-main")); n != 0 {
|
||||
t.Errorf("llm_distill 来源应 0 节点,实际 %d", n)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ func (g *InMemoryGraph) ExpandWithSummary(results []models.RecallResult, namespa
|
||||
summary := buildBFSSummary(relations)
|
||||
return models.GraphBFSResult{
|
||||
ExpandedRelations: relations,
|
||||
Summary: summary,
|
||||
Summary: summary,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -345,12 +345,12 @@ func (fg *FileGraph) mergePaths(forward, backward map[string]*bfsNode, fw, bw *b
|
||||
cur := fw
|
||||
for cur != nil && cur.parent != "" {
|
||||
paths = append(paths, map[string]interface{}{
|
||||
"edge_id": cur.edgeID,
|
||||
"source": cur.parent,
|
||||
"target": cur.node,
|
||||
"relation": cur.rel,
|
||||
"weight": cur.weight,
|
||||
"hop": cur.hop,
|
||||
"edge_id": cur.edgeID,
|
||||
"source": cur.parent,
|
||||
"target": cur.node,
|
||||
"relation": cur.rel,
|
||||
"weight": cur.weight,
|
||||
"hop": cur.hop,
|
||||
"direction": "forward",
|
||||
})
|
||||
cur = forward[cur.parent]
|
||||
@@ -360,12 +360,12 @@ func (fg *FileGraph) mergePaths(forward, backward map[string]*bfsNode, fw, bw *b
|
||||
cur = bw
|
||||
for cur != nil && cur.parent != "" {
|
||||
paths = append(paths, map[string]interface{}{
|
||||
"edge_id": cur.edgeID,
|
||||
"source": cur.node, // 反转
|
||||
"target": cur.parent,
|
||||
"relation": cur.rel,
|
||||
"weight": cur.weight,
|
||||
"hop": cur.hop,
|
||||
"edge_id": cur.edgeID,
|
||||
"source": cur.node, // 反转
|
||||
"target": cur.parent,
|
||||
"relation": cur.rel,
|
||||
"weight": cur.weight,
|
||||
"hop": cur.hop,
|
||||
"direction": "backward",
|
||||
})
|
||||
cur = backward[cur.parent]
|
||||
@@ -449,8 +449,8 @@ func (fg *FileGraph) FallbackTextSearch(query, namespace string, limit int) []ma
|
||||
|
||||
// P2: 信任评分 stub(FileGraph 不持久化信任数据)
|
||||
func (fg *FileGraph) AddEdgeFeedback(edgeID string, helpful bool) error { return nil }
|
||||
func (fg *FileGraph) IncrementEdgeRetrieval(edgeID string) error { return nil }
|
||||
func (fg *FileGraph) UpdateEdgeTrustScores() error { return nil }
|
||||
func (fg *FileGraph) IncrementEdgeRetrieval(edgeID string) error { return nil }
|
||||
func (fg *FileGraph) UpdateEdgeTrustScores() error { return nil }
|
||||
|
||||
// ─── 图谱扩展 ────────────────────────────────────────────
|
||||
|
||||
@@ -535,7 +535,7 @@ func (fg *FileGraph) ExpandWithSummary(results []models.RecallResult, namespace
|
||||
summary := buildBFSSummary(relations)
|
||||
return models.GraphBFSResult{
|
||||
ExpandedRelations: relations,
|
||||
Summary: summary,
|
||||
Summary: summary,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,9 +40,9 @@ type FileGraphEdge struct {
|
||||
|
||||
// FileGraphData 持久化到磁盘的完整数据结构
|
||||
type FileGraphData struct {
|
||||
Version int `json:"version"`
|
||||
Nodes []*FileGraphNode `json:"nodes"`
|
||||
Edges []*FileGraphEdge `json:"edges"`
|
||||
Version int `json:"version"`
|
||||
Nodes []*FileGraphNode `json:"nodes"`
|
||||
Edges []*FileGraphEdge `json:"edges"`
|
||||
}
|
||||
|
||||
// FileGraph 基于 JSON 文件的多 Agent 共享知识图谱(Windows Stub)
|
||||
@@ -84,5 +84,5 @@ func (fg *FileGraph) FallbackTextSearch(query, namespace string, limit int) []ma
|
||||
return nil
|
||||
}
|
||||
func (fg *FileGraph) AddEdgeFeedback(edgeID string, helpful bool) error { return nil }
|
||||
func (fg *FileGraph) IncrementEdgeRetrieval(edgeID string) error { return nil }
|
||||
func (fg *FileGraph) UpdateEdgeTrustScores() error { return nil }
|
||||
func (fg *FileGraph) IncrementEdgeRetrieval(edgeID string) error { return nil }
|
||||
func (fg *FileGraph) UpdateEdgeTrustScores() error { return nil }
|
||||
|
||||
@@ -125,7 +125,7 @@ func (g *InMemoryGraph) NavigateBiDir(source, target string, maxHops int, namesp
|
||||
defer g.mu.RUnlock()
|
||||
|
||||
// 构建邻接表(按 relationFilter 过滤)
|
||||
adj := make(map[string][][2]string) // node -> []{neighbor, edge_id}
|
||||
adj := make(map[string][][2]string) // node -> []{neighbor, edge_id}
|
||||
edgeInfo := make(map[string][2]string) // edge_id -> [relation, weight_str]
|
||||
for _, e := range g.edges {
|
||||
if e.Namespace != namespace || !relFilterOK(e.Relation, relFilter) {
|
||||
@@ -137,16 +137,16 @@ func (g *InMemoryGraph) NavigateBiDir(source, target string, maxHops int, namesp
|
||||
}
|
||||
|
||||
type fwdNode struct {
|
||||
parent string
|
||||
edgeID string
|
||||
weight float64
|
||||
hop int
|
||||
parent string
|
||||
edgeID string
|
||||
weight float64
|
||||
hop int
|
||||
}
|
||||
type bwdNode struct {
|
||||
parent string
|
||||
edgeID string
|
||||
weight float64
|
||||
hop int
|
||||
parent string
|
||||
edgeID string
|
||||
weight float64
|
||||
hop int
|
||||
}
|
||||
|
||||
fwd := make(map[string]*fwdNode)
|
||||
@@ -454,7 +454,7 @@ func (g *InMemoryGraph) GetGraph(namespace string, limit int) ([]map[string]inte
|
||||
for _, n := range g.nodes {
|
||||
if namespace == "" || n.Namespace == namespace {
|
||||
nodes = append(nodes, map[string]interface{}{
|
||||
"id": n.ID, "name": n.Name, "type": n.Type, "namespace": n.Namespace,
|
||||
"id": n.ID, "name": n.Name, "type": n.Type, "namespace": n.Namespace,
|
||||
})
|
||||
if limit > 0 && len(nodes) >= limit {
|
||||
break
|
||||
@@ -522,8 +522,8 @@ func (g *InMemoryGraph) FallbackTextSearch(query, namespace string, limit int) [
|
||||
|
||||
// P2: 信任评分 stub(InMemoryGraph 不持久化)
|
||||
func (g *InMemoryGraph) AddEdgeFeedback(edgeID string, helpful bool) error { return nil }
|
||||
func (g *InMemoryGraph) IncrementEdgeRetrieval(edgeID string) error { return nil }
|
||||
func (g *InMemoryGraph) UpdateEdgeTrustScores() error { return nil }
|
||||
func (g *InMemoryGraph) IncrementEdgeRetrieval(edgeID string) error { return nil }
|
||||
func (g *InMemoryGraph) UpdateEdgeTrustScores() error { return nil }
|
||||
|
||||
func searchSubstring(s, substr string) bool {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
@@ -532,4 +532,4 @@ func searchSubstring(s, substr string) bool {
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -999,11 +999,11 @@ func (gs *SQLiteGraphStore) AllEdges(namespace string) []GraphEdge {
|
||||
for _, r := range rows {
|
||||
w, _ := strconv.ParseFloat(fmt.Sprintf("%v", r["weight"]), 64)
|
||||
out = append(out, GraphEdge{
|
||||
ID: fmt.Sprintf("%v", r["id"]),
|
||||
Source: fmt.Sprintf("%v", r["source"]),
|
||||
Target: fmt.Sprintf("%v", r["target"]),
|
||||
Relation: fmt.Sprintf("%v", r["relation"]),
|
||||
Weight: w,
|
||||
ID: fmt.Sprintf("%v", r["id"]),
|
||||
Source: fmt.Sprintf("%v", r["source"]),
|
||||
Target: fmt.Sprintf("%v", r["target"]),
|
||||
Relation: fmt.Sprintf("%v", r["relation"]),
|
||||
Weight: w,
|
||||
Namespace: fmt.Sprintf("%v", r["namespace"]),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ import "testing"
|
||||
|
||||
func TestIsContradiction_TrueContradictionsStillDetected(t *testing.T) {
|
||||
cases := []struct{ a, b string }{
|
||||
{"用户昨天买了苹果", "用户昨天没买苹果"}, // 重叠 0.875
|
||||
{"用户今天完成了任务", "用户今天还没完成任务"}, // 重叠 0.80
|
||||
{"用户昨天买了苹果", "用户昨天没买苹果"}, // 重叠 0.875
|
||||
{"用户今天完成了任务", "用户今天还没完成任务"}, // 重叠 0.80
|
||||
{"Docker is used by Hermes", "Docker is NOT used by Hermes"},
|
||||
{"config updated", "config not updated"},
|
||||
{"MemoryFabric是独立记忆层服务", "MemoryFabric不是独立记忆层服务"},
|
||||
|
||||
@@ -38,24 +38,39 @@ type VersionRecord struct {
|
||||
// MemoryRecord 织忆中的单条记忆(存储在 LanceDB memories 表中)。
|
||||
// v3.8 完整 Schema — 23 字段,与设计文档一致。
|
||||
type MemoryRecord struct {
|
||||
ID string `json:"id"`
|
||||
AgentID string `json:"agent_id"`
|
||||
Namespace string `json:"namespace"`
|
||||
Content string `json:"content"`
|
||||
ContentMD5 string `json:"content_md5"` // content 的 MD5 前 12 字符,用于精确去重
|
||||
Category string `json:"category"` // system_fact, user_pref, proj_context, tool_usage, code_snippet
|
||||
Vector []float32 `json:"vector"` // bge-m3 1024维,L2归一化
|
||||
Tier string `json:"tier"` // normal, core(core永不衰减)
|
||||
Importance float64 `json:"importance"` // 重要性得分 = recency_factor × (1+log(1+recall_count))
|
||||
QualityScore float64 `json:"quality_score"`
|
||||
UsefulCount int `json:"useful_count"` // 被标记 useful 的次数
|
||||
NotUsefulCount int `json:"not_useful_count"` // 被标记 not-useful 的次数
|
||||
Version int `json:"version"` // 当前版本号
|
||||
VersionHistory []VersionRecord `json:"version_history,omitempty"` // 完整修改链
|
||||
Source string `json:"source"` // 来源:牧尘口头 / 配置解析 / Agent推断 / LLM蒸馏
|
||||
VolatileFlag bool `json:"volatile_flag"` // 频繁变更标记(≥3次修正自动设置)
|
||||
RecallCount int `json:"recall_count"`
|
||||
Freshness string `json:"freshness"` // fresh, stale, verified
|
||||
ID string `json:"id"`
|
||||
AgentID string `json:"agent_id"`
|
||||
Namespace string `json:"namespace"`
|
||||
Content string `json:"content"`
|
||||
ContentMD5 string `json:"content_md5"` // content 的 MD5 前 12 字符,用于精确去重
|
||||
Category string `json:"category"` // system_fact, user_pref, proj_context, tool_usage, code_snippet
|
||||
Vector []float32 `json:"vector"` // bge-m3 1024维,L2归一化
|
||||
Tier string `json:"tier"` // normal, core(core永不衰减)
|
||||
Importance float64 `json:"importance"` // 重要性得分 = recency_factor × (1+log(1+recall_count))
|
||||
QualityScore float64 `json:"quality_score"`
|
||||
UsefulCount int `json:"useful_count"` // 被标记 useful 的次数
|
||||
// GA-P0-1 (t_718d8707):not_useful_count 曾把两种语义混写——
|
||||
// 用户人工点「没用」(feedback.go MarkNotUseful) 与 dedup/合并流程自动降权
|
||||
// (core.go Dedup demote) 都写同一个字段。现拆分为 human_not_useful_count
|
||||
// 与 dedup_downweight_count,两个写入点各写各的。not_useful_count 保留为
|
||||
// 历史列(deprecated),新代码不再读它(见 AC#3)。
|
||||
HumanNotUsefulCount int `json:"human_not_useful_count"` // 用户人工点「没用」的次数
|
||||
DedupDownweightCount int `json:"dedup_downweight_count"` // dedup/合并流程自动降权的次数
|
||||
NotUsefulCount int `json:"not_useful_count"` // [DEPRECATED] 历史混写列,勿再读
|
||||
Version int `json:"version"` // 当前版本号
|
||||
VersionHistory []VersionRecord `json:"version_history,omitempty"` // 完整修改链
|
||||
Source string `json:"source"` // 来源:牧尘口头 / 配置解析 / Agent推断 / LLM蒸馏
|
||||
VolatileFlag bool `json:"volatile_flag"` // 频繁变更标记(≥3次修正自动设置)
|
||||
RecallCount int `json:"recall_count"`
|
||||
// GA-P0-2 (t_c6d41ac6):拆分 recall_count 的两种语义(「出现在结果里」/「被验证使用」)。
|
||||
// recall_count 曾把两种语义混写 —— 每次召回命中都 ++,被当成 V 值喂给衰减/遗忘逻辑,
|
||||
// 造成「被召回 500+ 次所以永不遗忘」的虚高保命。现拆分为:
|
||||
// RecallSeenCount 出现即 ++(只用于观测/排序去偏,不参与 V 值/衰减权重)
|
||||
// RecallVerifiedCount有正反馈(IncrementUseful)或下游引用才 ++(唯一参与 V 值/衰减)
|
||||
// RecallCount 保留为历史列(deprecated),冻结不再增长,仅向后兼容读取历史值(AC#3)。
|
||||
RecallSeenCount int `json:"recall_seen_count"` // 出现在召回结果里即 ++(观测/排序去偏用)
|
||||
RecallVerifiedCount int `json:"recall_verified_count"` // 有正反馈或下游引用才 ++(V 值/衰减唯一来源)
|
||||
Freshness string `json:"freshness"` // fresh, stale, verified
|
||||
// 2026-10-08 (t_7e3577c5):血缘字段。embedding_model=实际嵌入模型(bge-m3);
|
||||
// script_version=写入脚本版本标识。写入方经 metadata 携带,server 蒸馏落库。
|
||||
EmbeddingModel string `json:"embedding_model"`
|
||||
@@ -66,12 +81,34 @@ type MemoryRecord struct {
|
||||
// ConfidenceSource=authoritative(写入时声明) | estimated(事后回填猜测),用于日后定向纠偏。
|
||||
Confidence *float64 `json:"confidence,omitempty"`
|
||||
ConfidenceSource string `json:"confidence_source,omitempty"`
|
||||
LastRecalledAt time.Time `json:"last_recalled_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
IsDeleted bool `json:"is_deleted"`
|
||||
DependsOn []string `json:"depends_on,omitempty"` // 依赖的记忆 ID 列表
|
||||
DerivedFrom string `json:"derived_from,omitempty"` // 蒸馏来源 episode ID
|
||||
// 2026-10-11 (t_631ab13d):DB 原值直通通道。json:"-" 不参与任何 JSON 传输:
|
||||
// Rust IPC Search 会把 QualityScore 融合成 0.4*importance+0.6*raw(排序用),
|
||||
// 但 recall 响应需要展示 LanceDB 原始列值("响应值==库值"判据)。
|
||||
// 只在进程内携带,不落库、不序列化。
|
||||
RawQualityScore *float64 `json:"-"`
|
||||
LastRecalledAt time.Time `json:"last_recalled_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
IsDeleted bool `json:"is_deleted"`
|
||||
// GA-P0-3 (t_f785d7f2):删除原因枚举(merge_dup|contradiction|manual|retention|unknown)。
|
||||
// is_deleted=true 不再「不可分辨」—— 坏记忆被删 与 合并/整理被删 现可分列。additive 列。
|
||||
DeleteReason string `json:"delete_reason,omitempty"`
|
||||
DependsOn []string `json:"depends_on,omitempty"` // 依赖的记忆 ID 列表
|
||||
DerivedFrom string `json:"derived_from,omitempty"` // 蒸馏来源 episode ID
|
||||
}
|
||||
|
||||
// HitEvent 真实 outcome 的 append-only 事件(GA-P0-4 t_bb10e8bc)。
|
||||
// 与计数列(useful_count / human_not_useful_count 等)不同:这是「按时间戳追加」
|
||||
// 的事件日志,**不覆盖式累加** —— 每次真实 outcome 追加一行,带 created_at 时间戳,
|
||||
// 便于 P2 自适应阈值按周窗口聚合(WHERE created_at > 本周起点)。语义定义见
|
||||
// internal/storage/hit_event.go(什么算 hits+ / hits− / 什么不算)。
|
||||
type HitEvent struct {
|
||||
ID int64 `json:"id"`
|
||||
MemoryID string `json:"memory_id"`
|
||||
Outcome string `json:"outcome"` // pos | neg(HitOutcome 枚举)
|
||||
Reason string `json:"reason"` // HitReason 枚举:human_feedback|contradiction|replacement|downstream_use
|
||||
Source string `json:"source,omitempty"` // 记录方标识(如端点名)
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// EpisodeRecord 原始对话/任务日志(存储在 LanceDB episodes 表中)。
|
||||
@@ -110,6 +147,17 @@ type RecallResult struct {
|
||||
// Confidence=nil 表示该条未声明(存量),调用方按 1.0 处理。
|
||||
Confidence *float64 `json:"confidence,omitempty"`
|
||||
ConfidenceSource string `json:"confidence_source,omitempty"`
|
||||
// 2026-10-11 (t_631ab13d):把库里的 quality_score / v_value 直通给调用方。
|
||||
// 此前 RecallResult 缺这两个字段 ⇒ JSON 序列化不带出 ⇒ 插件 `r.get("quality_score", 0)`
|
||||
// 恒得默认 0("显示 0"),与库里 74.88% 记忆有真实值矛盾。排序不受影响(排序用内存
|
||||
// candidates[i].QualityScore=库值,见 recall.go),本字段仅补"响应值==库值"的显示缺口。
|
||||
// quality_score 恒输出(含 0);v_value 为 VValueProvider 提供,未注入时 0。
|
||||
QualityScore float64 `json:"quality_score"`
|
||||
// QualityScoreReliable:上面的 quality_score 是否可信。
|
||||
// 2026-10-12:存量 54.2% 的 quality_score 不可信(0=未打分 / 0.755=历史常量 bug),
|
||||
// 该值原样返回供审计,但**不应被当作真实质量判断**。调用方须看本字段。
|
||||
QualityScoreReliable bool `json:"quality_score_reliable"`
|
||||
VValue float64 `json:"v_value"`
|
||||
}
|
||||
|
||||
// RerankResult rerank 重排后的单条结果。
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package models
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
// 存量 quality_score 可信度(2026-10-12)
|
||||
//
|
||||
// 背景:织忆的 LLM 评分历史上被两类 bug 污染,共 18,825/34,749 条(54.2%):
|
||||
// · 0.755 —— 旧 prompt 的**示例分数被 2B 照抄**,导致 overall 恒为常量 0.755。
|
||||
// 按创建日切片证实:09-27~10-06 占 61%~77%,10-07 降至 0.3%,
|
||||
// **10-09 起为 0.0%** ⇒ 纯历史遗留,不再新增(10-08 行协议修复已止血)。
|
||||
// · 0.0 —— 占位符被复读导致取不到数字(解析失败),语义是**"没有分数"**,
|
||||
// 不是"质量为 0"。
|
||||
//
|
||||
// 影响面:检索排序是 `0.7×库值 + 0.3×关键词`(storage/recall.go:241)与
|
||||
// IPC 侧 `0.4×importance + 0.6×库值`(storage/lancedb_ipc.go:268)——
|
||||
// **消费端一直在用这些假分数排序**。修入口(prompt/threshold)治不了存量。
|
||||
//
|
||||
// 处置:**不改库、不加列、不删数据**(34k 行全表改写风险大于收益),
|
||||
// 改为在读取处按值判定并**把不可信分数的权重让给其他信号**。
|
||||
// 好处:① 零数据风险,回滚=还原代码;② 未来的坏分数一并兜住;
|
||||
// ③ 原始值原样保留,仍可审计。
|
||||
// ─────────────────────────────────────────────────────────────────────────
|
||||
|
||||
// EchoBugConstantScore 旧 prompt 示例分数被照抄产生的常量值(历史 bug 遗留)。
|
||||
const EchoBugConstantScore = 0.755
|
||||
|
||||
// IsReliableQuality 判定库里的 quality_score 是否可用于排序。
|
||||
//
|
||||
// 返回 false 的情形:
|
||||
// - qs <= 0:未打分/解析失败(语义是"没有分数",不是"质量为 0",
|
||||
// 不可当作低质量去惩罚)
|
||||
// - qs ≈ 0.755:历史常量 bug 值(已证实 10-09 起不再产生)
|
||||
//
|
||||
// 注意:本函数**只**用于"要不要信这个数",不用于修改或删除任何数据。
|
||||
func IsReliableQuality(qs float64) bool {
|
||||
if qs <= 0 {
|
||||
return false
|
||||
}
|
||||
const eps = 1e-6
|
||||
d := qs - EchoBugConstantScore
|
||||
if d < 0 {
|
||||
d = -d
|
||||
}
|
||||
return d >= eps
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package models
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestIsReliableQuality 锁定存量 quality_score 可信度判据。
|
||||
//
|
||||
// 判据来自实测分布(2026-10-12,34,749 条):
|
||||
//
|
||||
// · 0.755 占 28.8% —— 按创建日切片证实 10-09 起不再产生 ⇒ 历史常量 bug 遗留
|
||||
// · 0.0 占 25.4% —— 语义是"没有分数",不是"质量为 0"
|
||||
// · 其余 45.8% —— 相对可信
|
||||
func TestIsReliableQuality(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
qs float64
|
||||
want bool
|
||||
}{
|
||||
{"零分=未打分", 0, false},
|
||||
{"负值", -0.1, false},
|
||||
{"历史常量bug值", 0.755, false},
|
||||
{"常量bug值近邻(浮点噪声)", 0.7550000001, false},
|
||||
{"真实低分", 0.175, true},
|
||||
{"真实中分", 0.535, true},
|
||||
{"真实高分", 0.83, true},
|
||||
{"真实满分", 1.0, true},
|
||||
{"紧邻常量但不是(应可信)", 0.76, true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
if got := IsReliableQuality(c.qs); got != c.want {
|
||||
t.Fatalf("IsReliableQuality(%v) = %v, 期望 %v", c.qs, got, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsReliableQualityDoesNotMutate 纯粹是判定函数:不得有副作用(只读承诺)。
|
||||
func TestIsReliableQualityPure(t *testing.T) {
|
||||
seen := map[float64]bool{}
|
||||
for _, qs := range []float64{0, 0.755, 0.5, 0.9, 0.755} {
|
||||
first := IsReliableQuality(qs)
|
||||
second := IsReliableQuality(qs)
|
||||
if first != second {
|
||||
t.Fatalf("非确定性:%v", qs)
|
||||
}
|
||||
if v, ok := seen[qs]; ok && v != first {
|
||||
t.Fatalf("同一输入结果不一致:%v", qs)
|
||||
}
|
||||
seen[qs] = first
|
||||
}
|
||||
}
|
||||
@@ -10,19 +10,19 @@ import (
|
||||
|
||||
func BenchmarkDashboard_Metrics(b *testing.B) {
|
||||
d := &Dashboard{
|
||||
UsefulCount: 850,
|
||||
NotUsefulCount: 150,
|
||||
TotalRecalls: 5000,
|
||||
HitCount: 4250,
|
||||
ClosedGaps: 30,
|
||||
TotalGaps: 45,
|
||||
CascadeFixedTotal: 12,
|
||||
TotalFixes: 20,
|
||||
DeprecatedToday: 5,
|
||||
DistillLossSum: 8.5,
|
||||
DistillLossCount: 25,
|
||||
UsefulCount: 850,
|
||||
NotUsefulCount: 150,
|
||||
TotalRecalls: 5000,
|
||||
HitCount: 4250,
|
||||
ClosedGaps: 30,
|
||||
TotalGaps: 45,
|
||||
CascadeFixedTotal: 12,
|
||||
TotalFixes: 20,
|
||||
DeprecatedToday: 5,
|
||||
DistillLossSum: 8.5,
|
||||
DistillLossCount: 25,
|
||||
AutoResolvedConflicts: 15,
|
||||
TotalConflicts: 25,
|
||||
TotalConflicts: 25,
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
@@ -12,21 +12,21 @@ import (
|
||||
|
||||
// Pipeline 自动化流程编排器
|
||||
type Pipeline struct {
|
||||
mu sync.RWMutex
|
||||
handlers map[string]PipelineHandler
|
||||
queue []PipelineTask
|
||||
running bool
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
mu sync.RWMutex
|
||||
handlers map[string]PipelineHandler
|
||||
queue []PipelineTask
|
||||
running bool
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
// PipelineTask 流程任务
|
||||
type PipelineTask struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"` // commit / recall / gap / conflict / consolidate
|
||||
Payload interface{} `json:"payload"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Status string `json:"status"` // pending / running / done / failed
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"` // commit / recall / gap / conflict / consolidate
|
||||
Payload interface{} `json:"payload"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Status string `json:"status"` // pending / running / done / failed
|
||||
}
|
||||
|
||||
// PipelineHandler 流程处理器
|
||||
@@ -156,10 +156,14 @@ func (p *Pipeline) Stats() map[string]interface{} {
|
||||
pending, running, done, failed := 0, 0, 0, 0
|
||||
for _, t := range p.queue {
|
||||
switch t.Status {
|
||||
case "pending": pending++
|
||||
case "running": running++
|
||||
case "done": done++
|
||||
case "failed": failed++
|
||||
case "pending":
|
||||
pending++
|
||||
case "running":
|
||||
running++
|
||||
case "done":
|
||||
done++
|
||||
case "failed":
|
||||
failed++
|
||||
}
|
||||
}
|
||||
return map[string]interface{}{
|
||||
|
||||
@@ -16,17 +16,17 @@ type QualityDropMonitor struct {
|
||||
dropped map[string]*DropRecord
|
||||
|
||||
// 阈值
|
||||
lowThreshold float64 // < 0.3 → 触发
|
||||
minFeedbacks int // ≥ 2 次反馈 → 有效(降低阈值从 5→2)
|
||||
deprecationDays int // 7 天未改善 → deprecated
|
||||
lowThreshold float64 // < 0.3 → 触发
|
||||
minFeedbacks int // ≥ 2 次反馈 → 有效(降低阈值从 5→2)
|
||||
deprecationDays int // 7 天未改善 → deprecated
|
||||
}
|
||||
|
||||
type DropRecord struct {
|
||||
MemoryID string `json:"memory_id"`
|
||||
Score float64 `json:"score"`
|
||||
NotifiedAt time.Time `json:"notified_at"`
|
||||
DaysSinceDrop int `json:"days_since_drop"`
|
||||
Status string `json:"status"` // notified / deprecating / deprecated
|
||||
MemoryID string `json:"memory_id"`
|
||||
Score float64 `json:"score"`
|
||||
NotifiedAt time.Time `json:"notified_at"`
|
||||
DaysSinceDrop int `json:"days_since_drop"`
|
||||
Status string `json:"status"` // notified / deprecating / deprecated
|
||||
}
|
||||
|
||||
var QualityMonitor = &QualityDropMonitor{
|
||||
|
||||
@@ -15,13 +15,13 @@ import (
|
||||
|
||||
// VDecision V 值决策记录
|
||||
type VDecision struct {
|
||||
ID string `json:"id"`
|
||||
MemoryIDs []string `json:"memory_ids"` // 引用的记忆 ID
|
||||
Action string `json:"action"` // 牧尘的决策行动
|
||||
Outcome string `json:"outcome"` // success / failure / partial
|
||||
VValue float64 `json:"v_value"` // 决策 V 值 (0-1)
|
||||
ParentID string `json:"parent_id"` // 上级决策(级联链路)
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
ID string `json:"id"`
|
||||
MemoryIDs []string `json:"memory_ids"` // 引用的记忆 ID
|
||||
Action string `json:"action"` // 牧尘的决策行动
|
||||
Outcome string `json:"outcome"` // success / failure / partial
|
||||
VValue float64 `json:"v_value"` // 决策 V 值 (0-1)
|
||||
ParentID string `json:"parent_id"` // 上级决策(级联链路)
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
// VPropagator V 值传播器
|
||||
@@ -100,9 +100,12 @@ func (vp *VPropagator) RecordDecision(id string, memoryIDs []string, action, out
|
||||
|
||||
vValue := 0.0
|
||||
switch outcome {
|
||||
case "success": vValue = 1.0
|
||||
case "partial": vValue = 0.5
|
||||
case "failure": vValue = 0.0
|
||||
case "success":
|
||||
vValue = 1.0
|
||||
case "partial":
|
||||
vValue = 0.5
|
||||
case "failure":
|
||||
vValue = 0.0
|
||||
}
|
||||
|
||||
d := &VDecision{
|
||||
|
||||
@@ -107,10 +107,11 @@ func TestBatchIPCVersionAccounting(t *testing.T) {
|
||||
batchVersions := v1 - v0
|
||||
|
||||
// B) 旧路径:3 次单条 lancedb_update(recall 读路径原行为)
|
||||
// t_268b0353(治根):与主批量路径一致,不再写 freshness='verified'
|
||||
// (列数不影响 LanceDB 版本计数——一个 update 事务=一个版本,故测试断言不受影响)
|
||||
fields, _ := json.Marshal([]map[string]string{
|
||||
{"column": "recall_count", "value": "1"},
|
||||
{"column": "last_recalled_at", "value": "'" + ts + "'"},
|
||||
{"column": "freshness", "value": "'verified'"},
|
||||
})
|
||||
for i := 3; i < 6; i++ {
|
||||
ipcCall(t, sock, map[string]any{"type": "lancedb_update", "table": "memories", "id": recs[i].ID, "fields": string(fields)})
|
||||
|
||||
@@ -11,19 +11,19 @@ import (
|
||||
|
||||
// CoOccurEdge 共访边
|
||||
type CoOccurEdge struct {
|
||||
SourceID string `json:"source_id"`
|
||||
TargetID string `json:"target_id"`
|
||||
CoCount int `json:"co_count"` // 共现次数
|
||||
Weight float64 `json:"weight"` // 共被recall次数/min(A_count, B_count)
|
||||
LastCoOccur time.Time `json:"last_co_occur"`
|
||||
Namespace string `json:"namespace"`
|
||||
SourceID string `json:"source_id"`
|
||||
TargetID string `json:"target_id"`
|
||||
CoCount int `json:"co_count"` // 共现次数
|
||||
Weight float64 `json:"weight"` // 共被recall次数/min(A_count, B_count)
|
||||
LastCoOccur time.Time `json:"last_co_occur"`
|
||||
Namespace string `json:"namespace"`
|
||||
}
|
||||
|
||||
// CoOccurTracker 共访追踪器
|
||||
type CoOccurTracker struct {
|
||||
mu sync.RWMutex
|
||||
edges map[string]map[string]*CoOccurEdge // sourceID → targetID → edge
|
||||
recallCounts map[string]int // memoryID → recall_count
|
||||
mu sync.RWMutex
|
||||
edges map[string]map[string]*CoOccurEdge // sourceID → targetID → edge
|
||||
recallCounts map[string]int // memoryID → recall_count
|
||||
|
||||
// 预取阈值
|
||||
prefetchThreshold float64 // weight > 0.6 → 预取
|
||||
@@ -203,9 +203,9 @@ func (ct *CoOccurTracker) Prune() int {
|
||||
|
||||
// Stats 获取统计
|
||||
type CoOccurStats struct {
|
||||
TotalEdges int `json:"total_edges"`
|
||||
HighWeight int `json:"high_weight"` // weight >= prefetchThreshold
|
||||
AvgWeight float64 `json:"avg_weight"`
|
||||
TotalEdges int `json:"total_edges"`
|
||||
HighWeight int `json:"high_weight"` // weight >= prefetchThreshold
|
||||
AvgWeight float64 `json:"avg_weight"`
|
||||
}
|
||||
|
||||
func (ct *CoOccurTracker) Stats() CoOccurStats {
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
// 织忆 MemoryWeave — 删除原因枚举(GA-P0-3 t_f785d7f2)
|
||||
//
|
||||
// 背景:is_deleted=true 曾「不可分辨」——「坏记忆被删」和「合并/整理被删」混在
|
||||
// 同一个布尔里。P2 要拿「被删」当负标签,必须先知道它是「为什么」被删;否则会把
|
||||
// 「正常整合」误当「内容质量差」。
|
||||
//
|
||||
// 本文件定义固定枚举 + 归一化函数。所有软删写入点(SoftDelete / SoftDeleteBatch /
|
||||
// PurgeInvalid,及 Rust 侧 purge_invalid / soft_delete_batch)统一经
|
||||
// NormalizeDeleteReason 把 free-form reason 字符串归一到枚举,写进 delete_reason 列。
|
||||
// 禁真删、禁部署 —— 本文件纯枚举定义,不碰任何数据。
|
||||
package storage
|
||||
|
||||
import "strings"
|
||||
|
||||
// DeleteReason 固定枚举(GA-P0-3)。写入 delete_reason 列的唯一合法取值集合。
|
||||
// 语义:
|
||||
//
|
||||
// merge_dup 合并/整理被删 —— 内容本身可能没问题,是被并进另一条了(dedup/合并管道)。
|
||||
// ⇒ 不应算「内容质量差」负标签。
|
||||
// contradiction 矛盾冲突被删 —— 与更可信的记忆冲突,作为坏记忆被淘汰。
|
||||
// manual 人工删除 —— 用户/admin 主动删除。
|
||||
// retention 保留/遗忘策略删除 —— 自动遗忘、碎片清理、ephemeral 过期等。
|
||||
// unknown 判不出 —— 存量回填兜底,或写入点未携带原因。
|
||||
const (
|
||||
DeleteReasonMergeDup = "merge_dup"
|
||||
DeleteReasonContradiction = "contradiction"
|
||||
DeleteReasonManual = "manual"
|
||||
DeleteReasonRetention = "retention"
|
||||
DeleteReasonUnknown = "unknown"
|
||||
)
|
||||
|
||||
// ValidDeleteReasons 合法枚举集合(供校验/审计)。
|
||||
var ValidDeleteReasons = map[string]bool{
|
||||
DeleteReasonMergeDup: true,
|
||||
DeleteReasonContradiction: true,
|
||||
DeleteReasonManual: true,
|
||||
DeleteReasonRetention: true,
|
||||
DeleteReasonUnknown: true,
|
||||
}
|
||||
|
||||
// NormalizeDeleteReason 把 free-form reason 字符串归一到固定枚举。
|
||||
// 匹配规则按关键词(不区分大小写),从「特异性最高」往下匹配:
|
||||
//
|
||||
// manual_delete / manual / 用户 / user / feedback / human → manual
|
||||
// dedup / wording-duplicate / merged_into / merge → merge_dup
|
||||
// conflict / contradict / consolidate → contradiction
|
||||
// auto_forget / ephemeral / fragment / expire / retention → retention
|
||||
// 其余(含空、batch:...) → unknown
|
||||
//
|
||||
// 这是「删除原因分类」的单一真源 —— 新增/改写入点时在这里加规则即可,枚举本身不变。
|
||||
func NormalizeDeleteReason(reason string) string {
|
||||
r := strings.ToLower(strings.TrimSpace(reason))
|
||||
if r == "" {
|
||||
return DeleteReasonUnknown
|
||||
}
|
||||
switch {
|
||||
case strings.HasPrefix(r, "batch:"):
|
||||
// 批量软删(SoftDeleteBatch)不携带逐条原因 ⇒ unknown
|
||||
return DeleteReasonUnknown
|
||||
case strings.Contains(r, "manual"), strings.Contains(r, "用户"),
|
||||
strings.Contains(r, "user"), strings.Contains(r, "feedback"),
|
||||
strings.Contains(r, "human"), strings.Contains(r, "人工"):
|
||||
return DeleteReasonManual
|
||||
case strings.Contains(r, "dedup"), strings.Contains(r, "wording"),
|
||||
strings.Contains(r, "duplicate"), strings.Contains(r, "merged"),
|
||||
strings.Contains(r, "merge"):
|
||||
return DeleteReasonMergeDup
|
||||
case strings.Contains(r, "conflict"), strings.Contains(r, "contradict"),
|
||||
strings.Contains(r, "consolidate"):
|
||||
return DeleteReasonContradiction
|
||||
case strings.Contains(r, "auto_forget"), strings.Contains(r, "ephemeral"),
|
||||
strings.Contains(r, "fragment"), strings.Contains(r, "expire"),
|
||||
strings.Contains(r, "retention"), strings.Contains(r, "过期"),
|
||||
strings.Contains(r, "自动"):
|
||||
return DeleteReasonRetention
|
||||
default:
|
||||
return DeleteReasonUnknown
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
// 织忆 MemoryWeave — delete_reason 枚举 + 各软删写入点的写入用例(GA-P0-3 t_f785d7f2)
|
||||
package storage
|
||||
|
||||
import "testing"
|
||||
|
||||
// 每个 reason 至少一条归一化命中用例(AC#4)。
|
||||
func TestNormalizeDeleteReason_EachReasonAtLeastOne(t *testing.T) {
|
||||
cases := []struct {
|
||||
reason string
|
||||
want string
|
||||
}{
|
||||
// merge_dup —— 合并/整理被删
|
||||
{"dedup:wording-duplicate", DeleteReasonMergeDup},
|
||||
{"merged_into_mem_abc", DeleteReasonMergeDup},
|
||||
{"dedup", DeleteReasonMergeDup},
|
||||
// contradiction —— 矛盾冲突被删
|
||||
{"consolidate: conflict", DeleteReasonContradiction},
|
||||
{"conflict", DeleteReasonContradiction},
|
||||
{"contradicts newer fact", DeleteReasonContradiction},
|
||||
// manual —— 人工删除
|
||||
{"manual_delete", DeleteReasonManual},
|
||||
{"manual", DeleteReasonManual},
|
||||
{"用户反馈删除", DeleteReasonManual},
|
||||
// retention —— 保留/遗忘策略删除
|
||||
{"auto_forget", DeleteReasonRetention},
|
||||
{"auto_forget_fragment", DeleteReasonRetention},
|
||||
{"ephemeral_expired", DeleteReasonRetention},
|
||||
{"auto_forget_hermes", DeleteReasonRetention},
|
||||
// unknown —— 兜底
|
||||
{"", DeleteReasonUnknown},
|
||||
{"batch:2026-10-12T00:00:00Z", DeleteReasonUnknown},
|
||||
{"garbage arbitrary reason", DeleteReasonUnknown},
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, c := range cases {
|
||||
got := NormalizeDeleteReason(c.reason)
|
||||
if got != c.want {
|
||||
t.Errorf("NormalizeDeleteReason(%q)=%q, want %q", c.reason, got, c.want)
|
||||
}
|
||||
seen[c.want] = true
|
||||
}
|
||||
for _, r := range []string{
|
||||
DeleteReasonMergeDup, DeleteReasonContradiction,
|
||||
DeleteReasonManual, DeleteReasonRetention, DeleteReasonUnknown,
|
||||
} {
|
||||
if !seen[r] {
|
||||
t.Errorf("reason %q 无写入用例", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 枚举集合完整且每个值合法。
|
||||
func TestValidDeleteReasons_Exhaustive(t *testing.T) {
|
||||
for _, r := range []string{
|
||||
DeleteReasonMergeDup, DeleteReasonContradiction,
|
||||
DeleteReasonManual, DeleteReasonRetention, DeleteReasonUnknown,
|
||||
} {
|
||||
if !ValidDeleteReasons[r] {
|
||||
t.Errorf("%q 不在 ValidDeleteReasons", r)
|
||||
}
|
||||
}
|
||||
if len(ValidDeleteReasons) != 5 {
|
||||
t.Errorf("ValidDeleteReasons 应恰 5 个枚举,实际 %d", len(ValidDeleteReasons))
|
||||
}
|
||||
}
|
||||
|
||||
// MergeDup 不应被算作「内容质量差」负标签 —— 归一化后需与 retention/manual 可区分。
|
||||
func TestMergeDupDistinctFromQualitySignals(t *testing.T) {
|
||||
if NormalizeDeleteReason("dedup:merge") == NormalizeDeleteReason("manual_delete") {
|
||||
t.Fatal("merge_dup 与 manual 被归一成同一枚举,负标签区分失效")
|
||||
}
|
||||
if NormalizeDeleteReason("merged_into_x") == DeleteReasonRetention {
|
||||
t.Fatal("merge_dup 被误判为 retention(会把正常整合当坏记忆)")
|
||||
}
|
||||
}
|
||||
|
||||
// MemLanceClient(内存兜底后端)SoftDelete 写 delete_reason。
|
||||
func TestMemLanceClient_SoftDeleteSetsReason(t *testing.T) {
|
||||
m := NewMemLanceClient(nil)
|
||||
m.mu.Lock()
|
||||
m.memories["mem_a"] = &memEntry{ID: "mem_a", Content: "merge target"}
|
||||
m.memories["mem_b"] = &memEntry{ID: "mem_b", Content: "bad content"}
|
||||
m.mu.Unlock()
|
||||
_ = m.SoftDelete("mem_a", "dedup:wording-duplicate")
|
||||
_ = m.SoftDelete("mem_b", "auto_forget_fragment")
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if e := m.memories["mem_a"]; e == nil || !e.IsDeleted || e.DeleteReason != DeleteReasonMergeDup {
|
||||
t.Fatalf("mem_a: IsDeleted=%v reason=%q, want deleted merge_dup", e != nil && e.IsDeleted, e.DeleteReason)
|
||||
}
|
||||
if e := m.memories["mem_b"]; e == nil || !e.IsDeleted || e.DeleteReason != DeleteReasonRetention {
|
||||
t.Fatalf("mem_b: IsDeleted=%v reason=%q, want deleted retention", e != nil && e.IsDeleted, e.DeleteReason)
|
||||
}
|
||||
}
|
||||
|
||||
// MemLanceClient SoftDeleteBatch(批量)无逐条原因 ⇒ unknown。
|
||||
func TestMemLanceClient_SoftDeleteBatchUnknown(t *testing.T) {
|
||||
m := NewMemLanceClient(nil)
|
||||
m.mu.Lock()
|
||||
m.memories["mem_x"] = &memEntry{ID: "mem_x", Content: "x"}
|
||||
m.mu.Unlock()
|
||||
_, err := m.SoftDeleteBatch("memories", []string{"mem_x"}, "2026-10-12T00:00:00Z")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if e := m.memories["mem_x"]; e == nil || e.DeleteReason != DeleteReasonUnknown {
|
||||
t.Fatalf("batch delete reason=%q, want unknown", e.DeleteReason)
|
||||
}
|
||||
}
|
||||
|
||||
// PurgeInvalid(id 为空垃圾行)= retention。
|
||||
func TestMemLanceClient_PurgeInvalidRetention(t *testing.T) {
|
||||
m := NewMemLanceClient(nil)
|
||||
m.mu.Lock()
|
||||
m.memories[""] = &memEntry{ID: "", Content: "garbage", IsDeleted: false}
|
||||
m.mu.Unlock()
|
||||
n, err := m.PurgeInvalid()
|
||||
if err != nil || n != 1 {
|
||||
t.Fatalf("PurgeInvalid n=%d err=%v, want 1,nil", n, err)
|
||||
}
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
if e := m.memories[""]; e == nil || e.DeleteReason != DeleteReasonRetention {
|
||||
t.Fatalf("purge reason=%q, want retention", e.DeleteReason)
|
||||
}
|
||||
}
|
||||
|
||||
// batch: 前缀恒 unknown(与 SoftDeleteBatch 语义一致)。
|
||||
func TestNormalizeBatchPrefix(t *testing.T) {
|
||||
if got := NormalizeDeleteReason("batch:2026-10-12T00:00:00Z"); got != DeleteReasonUnknown {
|
||||
t.Fatalf("batch: 前缀应归一 unknown, got %q", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// 织忆 MemoryWeave — 遗忘候选旋转窗口游标持久化单测(t_a4b8fa52 方案 c′,AC-8)
|
||||
package storage
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// resetForgetCursorPath 将游标路径指到临时目录,避免污染 /var/lib/memoryweave 真文件。
|
||||
func resetForgetCursorPath(t *testing.T) string {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
old := forgetCursorPath
|
||||
forgetCursorPath = filepath.Join(dir, "forget-cursor.json")
|
||||
t.Cleanup(func() { forgetCursorPath = old })
|
||||
return forgetCursorPath
|
||||
}
|
||||
|
||||
func TestForgetCursorPersistAndLoad(t *testing.T) {
|
||||
resetForgetCursorPath(t)
|
||||
persistForgetCursor("mem_1791482426233375644")
|
||||
got := loadForgetCursor()
|
||||
if got != "mem_1791482426233375644" {
|
||||
t.Fatalf("persist→load 应一致, got=%q", got)
|
||||
}
|
||||
// 文件确实落盘
|
||||
if _, err := os.Stat(forgetCursorPath); err != nil {
|
||||
t.Fatalf("游标文件应存在: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestForgetCursorLoadMissing(t *testing.T) {
|
||||
// 文件不存在 → 返回 ""(从头轮转,不崩)
|
||||
resetForgetCursorPath(t)
|
||||
if got := loadForgetCursor(); got != "" {
|
||||
t.Fatalf("缺失文件应返回空串, got=%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestForgetCursorLoadCorrupt(t *testing.T) {
|
||||
resetForgetCursorPath(t)
|
||||
// 损坏内容(非 mem_ 前缀)→ 返回 ""(降级从头轮转,不崩)
|
||||
if err := os.WriteFile(forgetCursorPath, []byte("not-a-cursor!!"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := loadForgetCursor(); got != "" {
|
||||
t.Fatalf("损坏游标应返回空串, got=%q", got)
|
||||
}
|
||||
// 空文件 → 返回 ""
|
||||
if err := os.WriteFile(forgetCursorPath, []byte(""), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := loadForgetCursor(); got != "" {
|
||||
t.Fatalf("空文件应返回空串, got=%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestForgetCursorPersistEmptySkips(t *testing.T) {
|
||||
// wrapped → 空串不落盘(缺文件=从头轮转)
|
||||
resetForgetCursorPath(t)
|
||||
persistForgetCursor("")
|
||||
if _, err := os.Stat(forgetCursorPath); !os.IsNotExist(err) {
|
||||
t.Fatalf("空游标不应落盘文件, err=%v", err)
|
||||
}
|
||||
}
|
||||
@@ -127,11 +127,11 @@ func (c *cachedGraphStore) CacheStats() map[string]interface{} {
|
||||
ratio = float64(totalHits) / float64(n) * 100
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"size": n,
|
||||
"max_size": c.maxSize,
|
||||
"ttl": c.ttl.String(),
|
||||
"total_hits": totalHits,
|
||||
"avg_hits_per_entry": fmt.Sprintf("%.1f", ratio),
|
||||
"size": n,
|
||||
"max_size": c.maxSize,
|
||||
"ttl": c.ttl.String(),
|
||||
"total_hits": totalHits,
|
||||
"avg_hits_per_entry": fmt.Sprintf("%.1f", ratio),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ func (c *cachedGraphStore) AllEdges(namespace string) []governance.GraphEdge {
|
||||
return c.inner.AllEdges(namespace)
|
||||
}
|
||||
func (c *cachedGraphStore) Stats() (int, int, float64) { return c.inner.Stats() }
|
||||
func (c *cachedGraphStore) Prune(minWeight float64) { c.inner.Prune(minWeight) }
|
||||
func (c *cachedGraphStore) Prune(minWeight float64) { c.inner.Prune(minWeight) }
|
||||
func (c *cachedGraphStore) ExpandFromResults(results []models.RecallResult, namespace string, maxHops int) []models.RecallResult {
|
||||
return c.inner.ExpandFromResults(results, namespace, maxHops)
|
||||
}
|
||||
@@ -166,7 +166,7 @@ func (c *cachedGraphStore) ExpandWithSummary(results []models.RecallResult, name
|
||||
func (c *cachedGraphStore) PageRank(damping float64, iterations int) map[string]float64 {
|
||||
return c.inner.PageRank(damping, iterations)
|
||||
}
|
||||
func (c *cachedGraphStore) EvidenceCount(entity string) int { return c.inner.EvidenceCount(entity) }
|
||||
func (c *cachedGraphStore) EvidenceCount(entity string) int { return c.inner.EvidenceCount(entity) }
|
||||
func (c *cachedGraphStore) GetEntityDegree(entity string) int { return c.inner.GetEntityDegree(entity) }
|
||||
func (c *cachedGraphStore) GetGraph(namespace string, limit int) ([]map[string]interface{}, []map[string]interface{}) {
|
||||
return c.inner.GetGraph(namespace, limit)
|
||||
@@ -234,4 +234,4 @@ func (c *cachedGraphStore) evictLRU() {
|
||||
if oldest != "" {
|
||||
delete(c.entries, oldest)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
// 织忆 MemoryWeave — hits+/hits− 双计数器的 outcome 事件定义(GA-P0-4 t_bb10e8bc)
|
||||
//
|
||||
// 背景:方案文档(已办.md:16623)曾把 `hits+ = useful_count`、`hits− = not_useful_count`、
|
||||
// `V = hits+ + hits−` 直接等同。该等同不成立:
|
||||
// - not_useful_count 混入了 dedup 自动降权(GA-P0-1 已拆出 dedup_downweight_count)
|
||||
// - recall_count 记的是「出现在结果里」而非「被验证使用」(GA-P0-2 已拆出 recall_verified_count)
|
||||
// 所以必须新建**独立、语义单一**的 outcome 信号 —— 本文件就是它的定义。
|
||||
//
|
||||
// 与计数列不同,本信号是 **append-only 事件表 hit_events**(sqlite 表 / LanceDB 表 /
|
||||
// 内存切片三种后端同步):每次真实 outcome 追加一行,带 created_at 时间戳,**不覆盖式累加**,
|
||||
// 便于 P2 自适应阈值按周窗口聚合(WHERE created_at > 本周起点)。
|
||||
//
|
||||
// ────────────────────────────────────────────────────────────────
|
||||
// y 判据(什么算 hits+ / hits− / 什么不算)—— 单一真源
|
||||
// ────────────────────────────────────────────────────────────────
|
||||
// hits+(outcome="pos"):一条记忆被**真实使用/认可**:
|
||||
// 1. human_feedback 用户对召回结果点「有用」(/feedback/useful)或「修正」(/feedback/correct)
|
||||
// 2. downstream_use 下游流程**实际引用**了这条记忆的内容(非「出现在候选里」)
|
||||
// 3. contradiction 后续对话中它被反复印证、未被推翻(正面 outcome 的 contradiction)
|
||||
// hits−(outcome="neg"):一条记忆被**真实否定/取代**:
|
||||
// 1. human_feedback 用户对召回结果点「没用」(/feedback/not-useful)
|
||||
// 2. contradiction 后续对话中它与更可信的信息**冲突并被淘汰**(delete_reason=contradiction)
|
||||
// 3. replacement 后续对话中它被**更新的信息取代**(delete_reason=replacement/merge_dup 且因矛盾)
|
||||
//
|
||||
// ✗ 明确**不**算(排除项):
|
||||
// - dedup 自动降权 → dedup_downweight_count(GA-P0-1),不是 outcome,只是去重副作用
|
||||
// - 单纯出现在召回结果里 → recall_seen_count(GA-P0-2),不是「被用上」,是「被看到」
|
||||
// - 只是被召回但无任何后续 → 无 outcome,不是 hits+ 也不是 hits−
|
||||
// 判据铁律:**只有内容被消费/被否定,才产生 outcome;被搬运/被看到/被降权都不算。**
|
||||
//
|
||||
// 本文件定义枚举 + 归一化函数,供所有写入点(sqlite / Rust-LanceDB / memvector)统一使用。
|
||||
// 禁部署 —— 纯定义,不碰任何数据。写入点接线见 feedback.go / lancedb_ipc.go。
|
||||
package storage
|
||||
|
||||
import "strings"
|
||||
|
||||
// HitOutcome 固定枚举 —— 写入 hit_events.outcome 列的唯一合法取值。
|
||||
// pos=正向 outcome(被真实使用/认可);neg=负向 outcome(被真实否定/取代)。
|
||||
const (
|
||||
HitOutcomePos = "pos"
|
||||
HitOutcomeNeg = "neg"
|
||||
)
|
||||
|
||||
// HitReason 固定枚举 —— 写入 hit_events.reason 列的唯一合法取值。
|
||||
// 与 delete_reason 正交:delete_reason 描述「为什么被删」,这里描述「为什么产生 outcome」。
|
||||
const (
|
||||
HitReasonHumanFeedback = "human_feedback" // 用户人工反馈(点有用/没用/修正)
|
||||
HitReasonContradiction = "contradiction" // 后续对话的矛盾冲突被淘汰/被印证
|
||||
HitReasonReplacement = "replacement" // 后续对话中被更新的信息取代
|
||||
HitReasonDownstreamUse = "downstream_use" // 下游流程实际引用了这条记忆
|
||||
)
|
||||
|
||||
// ValidHitOutcomes 合法 outcome 集合(供校验/审计)。
|
||||
var ValidHitOutcomes = map[string]bool{
|
||||
HitOutcomePos: true,
|
||||
HitOutcomeNeg: true,
|
||||
}
|
||||
|
||||
// ValidHitReasons 合法 reason 集合(供校验/审计)。
|
||||
var ValidHitReasons = map[string]bool{
|
||||
HitReasonHumanFeedback: true,
|
||||
HitReasonContradiction: true,
|
||||
HitReasonReplacement: true,
|
||||
HitReasonDownstreamUse: true,
|
||||
}
|
||||
|
||||
// NormalizeHitOutcome 把 free-form outcome 字符串归一到固定枚举。
|
||||
// 只认 pos/neg 两个合法值;其余(含空)返回 "pos" 会误导,这里返回非法空串由上层拒绝。
|
||||
// 上层(AppendHitEvent)在 outcome 非 pos/neg 时直接报错,不写库 —— 保证只由真实 outcome 写入。
|
||||
func NormalizeHitOutcome(outcome string) string {
|
||||
o := strings.ToLower(strings.TrimSpace(outcome))
|
||||
switch o {
|
||||
case "pos", "positive", "hit", "useful", "success":
|
||||
return HitOutcomePos
|
||||
case "neg", "negative", "miss", "notuseful", "failure":
|
||||
return HitOutcomeNeg
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// NormalizeHitReason 把 free-form reason 字符串归一到固定枚举。
|
||||
// 匹配按关键词(不区分大小写):人工反馈 / 矛盾 / 取代 / 下游引用 → 对应枚举;其余 unknown。
|
||||
func NormalizeHitReason(reason string) string {
|
||||
r := strings.ToLower(strings.TrimSpace(reason))
|
||||
if r == "" {
|
||||
return HitReasonDownstreamUse // 空 reason 默认视为下游引用(写方未标注)
|
||||
}
|
||||
switch {
|
||||
case strings.Contains(r, "user"), strings.Contains(r, "feedback"),
|
||||
strings.Contains(r, "human"), strings.Contains(r, "人工"), strings.Contains(r, "用户"):
|
||||
return HitReasonHumanFeedback
|
||||
case strings.Contains(r, "contradict"), strings.Contains(r, "conflict"), strings.Contains(r, "矛盾"):
|
||||
return HitReasonContradiction
|
||||
case strings.Contains(r, "replace"), strings.Contains(r, "supersede"), strings.Contains(r, "取代"), strings.Contains(r, "更新"):
|
||||
return HitReasonReplacement
|
||||
case strings.Contains(r, "downstream"), strings.Contains(r, "refer"), strings.Contains(r, "引用"), strings.Contains(r, "used"):
|
||||
return HitReasonDownstreamUse
|
||||
default:
|
||||
return HitReasonDownstreamUse
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GA-P0-4 (t_bb10e8bc):hits+/hits− 双计数器(append-only 事件表)单元测试。
|
||||
//
|
||||
// 覆盖 AC5(hits+/hits− 各至少一条写入用例 + dedup 降权**不**计入 hits− 的反例)
|
||||
// 与 AC6(read-back:真写一次,关库重开,回读确认字段变了)。
|
||||
// 语义定义见 hit_event.go —— 什么算 outcome、什么**不算**(dedup 降权/单纯召回出现)。
|
||||
|
||||
// TestHitEventPosWrite — AC5a:hits+ 写入用例。
|
||||
// 用户对某记忆点「有用」→ AppendHitEvent(pos, human_feedback) → 事件表追加一行,outcome=pos。
|
||||
func TestHitEventPosWrite(t *testing.T) {
|
||||
mlc := NewMemLanceClient(nil)
|
||||
mlc.mu.Lock()
|
||||
mlc.memories["mem_hit_pos"] = &memEntry{ID: "mem_hit_pos"}
|
||||
mlc.mu.Unlock()
|
||||
|
||||
if err := mlc.AppendHitEvent("mem_hit_pos", "pos", "human_feedback"); err != nil {
|
||||
t.Fatalf("AppendHitEvent(pos) err: %v", err)
|
||||
}
|
||||
evs, err := mlc.GetHitEvents("mem_hit_pos", 10)
|
||||
if err != nil {
|
||||
t.Fatalf("GetHitEvents err: %v", err)
|
||||
}
|
||||
if len(evs) != 1 {
|
||||
t.Fatalf("hits+ 写入:事件表应有 1 条,got %d", len(evs))
|
||||
}
|
||||
if evs[0].Outcome != "pos" {
|
||||
t.Fatalf("outcome 应为 pos,got %q", evs[0].Outcome)
|
||||
}
|
||||
if evs[0].Reason != "human_feedback" {
|
||||
t.Fatalf("reason 应为 human_feedback,got %q", evs[0].Reason)
|
||||
}
|
||||
if evs[0].CreatedAt.IsZero() {
|
||||
t.Fatal("hits+ 事件必须带时间戳(append-only,便于按周窗口算),got zero")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHitEventNegWrite — AC5b:hits− 写入用例。
|
||||
// 用户对某记忆点「没用」→ AppendHitEvent(neg, human_feedback) → 事件表追加一行,outcome=neg。
|
||||
func TestHitEventNegWrite(t *testing.T) {
|
||||
mlc := NewMemLanceClient(nil)
|
||||
mlc.mu.Lock()
|
||||
mlc.memories["mem_hit_neg"] = &memEntry{ID: "mem_hit_neg"}
|
||||
mlc.mu.Unlock()
|
||||
|
||||
if err := mlc.AppendHitEvent("mem_hit_neg", "neg", "human_feedback"); err != nil {
|
||||
t.Fatalf("AppendHitEvent(neg) err: %v", err)
|
||||
}
|
||||
evs, _ := mlc.GetHitEvents("mem_hit_neg", 10)
|
||||
if len(evs) != 1 || evs[0].Outcome != "neg" {
|
||||
t.Fatalf("hits− 写入:want 1 条 outcome=neg,got %d 条", len(evs))
|
||||
}
|
||||
}
|
||||
|
||||
// TestDedupDemoteDoesNotCountIntoHitNeg — AC5c 反例。
|
||||
// dedup 自动降权(IncrementDedupDownweight,GA-P0-1 拆出的字段)**不**产生 outcome 事件:
|
||||
// CountHitEvents(neg) 必须为 0 —— 这是「dedup 降权不算 hits−」的关键判据。
|
||||
func TestDedupDemoteDoesNotCountIntoHitNeg(t *testing.T) {
|
||||
mlc := NewMemLanceClient(nil)
|
||||
mlc.mu.Lock()
|
||||
mlc.memories["mem_dedup"] = &memEntry{ID: "mem_dedup"}
|
||||
mlc.mu.Unlock()
|
||||
|
||||
// dedup 降权只写 dedup_downweight_count,绝不追加 hit event
|
||||
if err := mlc.IncrementDedupDownweight("mem_dedup"); err != nil {
|
||||
t.Fatalf("IncrementDedupDownweight err: %v", err)
|
||||
}
|
||||
|
||||
// ① 事件表必须是空的(dedup 不产生任何 outcome 事件)
|
||||
evs, _ := mlc.GetHitEvents("mem_dedup", 10)
|
||||
if len(evs) != 0 {
|
||||
t.Fatalf("dedup 降权**不得**计入 hit 事件:事件表应为空,got %d 条", len(evs))
|
||||
}
|
||||
// ② CountHitEvents(neg) 必须是 0(dedup 降权不算 hits−)
|
||||
n, err := mlc.CountHitEvents("mem_dedup", "neg", time.Time{})
|
||||
if err != nil {
|
||||
t.Fatalf("CountHitEvents err: %v", err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatalf("dedup 降权**不得**计入 hits−:CountHitEvents(neg) 应为 0,got %d", n)
|
||||
}
|
||||
// ③ 反向证明 dedup 本身确实 +1(不是「没写」,而是「没写进 outcome 事件」)
|
||||
if mlc.memories["mem_dedup"].DedupDownweightCount != 1 {
|
||||
t.Fatalf("dedup_downweight_count 应 +1 到 1(证明降权确实发生),got %d", mlc.memories["mem_dedup"].DedupDownweightCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHitEventInvalidOutcomeRejected — AC1 硬约束:outcome 非 pos/neg 报错不写库,
|
||||
// 保证只有真实 outcome 能写入(非法 outcome 被拦截,不会产生「垃圾计数」)。
|
||||
func TestHitEventInvalidOutcomeRejected(t *testing.T) {
|
||||
mlc := NewMemLanceClient(nil)
|
||||
mlc.mu.Lock()
|
||||
mlc.memories["mem_bad"] = &memEntry{ID: "mem_bad"}
|
||||
mlc.mu.Unlock()
|
||||
|
||||
if err := mlc.AppendHitEvent("mem_bad", "dedup_downweight", "dedup"); err == nil {
|
||||
t.Fatal("outcome=dedup_downweight 应被拒绝(dedup 降权不算 outcome),却未报错")
|
||||
}
|
||||
evs, _ := mlc.GetHitEvents("mem_bad", 10)
|
||||
if len(evs) != 0 {
|
||||
t.Fatalf("非法 outcome 不得写库:事件表应为空,got %d 条", len(evs))
|
||||
}
|
||||
}
|
||||
|
||||
// TestHitEventReadBack — AC6:真写一次,关库重开,回读确认字段变了(非只看函数返回)。
|
||||
// 用真实 sqlite 库(CGO)做持久化 read-back 取证。
|
||||
func TestHitEventReadBack(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
dbPath := filepath.Join(dir, "hit_readback.db")
|
||||
|
||||
// 第一次写:先建一条 memories 行(外键),再追加 1 条 hits+ 与 1 条 hits− 事件
|
||||
sc, err := NewSQLiteClient(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("NewSQLiteClient err: %v", err)
|
||||
}
|
||||
if err := sc.execWithArgs(
|
||||
"INSERT INTO memories (id, agent_id, namespace, content, category, created_at, updated_at) VALUES ('mem_rb','ag','ns','c','general','2026-01-01T00:00:00Z','2026-01-01T00:00:00Z')",
|
||||
); err != nil {
|
||||
t.Fatalf("insert memory err: %v", err)
|
||||
}
|
||||
if err := sc.AppendHitEvent("mem_rb", "pos", "human_feedback"); err != nil {
|
||||
t.Fatalf("AppendHitEvent(pos) err: %v", err)
|
||||
}
|
||||
if err := sc.AppendHitEvent("mem_rb", "neg", "human_feedback"); err != nil {
|
||||
t.Fatalf("AppendHitEvent(neg) err: %v", err)
|
||||
}
|
||||
if err := sc.Close(); err != nil {
|
||||
t.Fatalf("Close err: %v", err)
|
||||
}
|
||||
|
||||
// 重开 DB,读回 —— 证明已持久化到磁盘(不是内存里看看)
|
||||
sc2, err := NewSQLiteClient(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("reopen NewSQLiteClient err: %v", err)
|
||||
}
|
||||
defer sc2.Close()
|
||||
|
||||
evs, err := sc2.GetHitEvents("mem_rb", 10)
|
||||
if err != nil {
|
||||
t.Fatalf("GetHitEvents(read-back) err: %v", err)
|
||||
}
|
||||
if len(evs) != 2 {
|
||||
t.Fatalf("read-back:重开后应读到 2 条事件(持久化确认),got %d", len(evs))
|
||||
}
|
||||
pos, err := sc2.CountHitEvents("mem_rb", "pos", time.Time{})
|
||||
if err != nil {
|
||||
t.Fatalf("CountHitEvents(pos) err: %v", err)
|
||||
}
|
||||
neg, err := sc2.CountHitEvents("mem_rb", "neg", time.Time{})
|
||||
if err != nil {
|
||||
t.Fatalf("CountHitEvents(neg) err: %v", err)
|
||||
}
|
||||
if pos != 1 || neg != 1 {
|
||||
t.Fatalf("read-back 计数:hits+ 应 1 got %d,hits− 应 1 got %d", pos, neg)
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/xiaoxue/memoryweave/internal/models"
|
||||
)
|
||||
|
||||
@@ -28,7 +30,21 @@ type LanceDB interface {
|
||||
Backup(path string) error
|
||||
GetAuditLog(limit int) ([]map[string]interface{}, error)
|
||||
IncrementUseful(id string) error
|
||||
IncrementNotUseful(id string) error
|
||||
// GA-P0-1 (t_718d8707):拆分 not_useful_count 的两种语义。两个写入点各写各的:
|
||||
// - IncrementHumanNotUseful → human_not_useful_count(用户人工点「没用」)
|
||||
// - IncrementDedupDownweight → dedup_downweight_count(dedup/合并流程自动降权)
|
||||
// IncrementNotUseful 保留仅向后兼容(历史混写),新代码不再调用它(AC#3)。
|
||||
IncrementHumanNotUseful(id string) error
|
||||
IncrementDedupDownweight(id string) error
|
||||
IncrementNotUseful(id string) error // [DEPRECATED] 历史混写列,勿再调(AC#3)
|
||||
// GA-P0-4 (t_bb10e8bc):hits+/hits− outcome 事件(append-only 事件表,语义定义见 hit_event.go)。
|
||||
// - AppendHitEvent(memoryID, outcome, reason) 追加一条真实 outcome 事件(带 created_at 时间戳)
|
||||
// - CountHitEvents(memoryID, outcome, since) 统计某 memory 在某窗口内的 hits+ 或 hits− 次数(P2 自适应阈值用)
|
||||
// - GetHitEvents(memoryID, limit) 读回事件明细(read-back 取证)
|
||||
// 铁律:outcome 非 pos/neg 报错不写库;dedup 降权 / 单纯召回出现 **禁止** 调本方法(见 hit_event.go 排除项)。
|
||||
AppendHitEvent(memoryID, outcome, reason string) error
|
||||
CountHitEvents(memoryID, outcome string, since time.Time) (int, error)
|
||||
GetHitEvents(memoryID string, limit int) ([]models.HitEvent, error)
|
||||
UpdateMemoryContent(id, newContent, source string) error
|
||||
InsertMemory(m models.MemoryRecord) error
|
||||
Search(table string, vector []float32, topK int, namespaceFilter string) ([]models.MemoryRecord, error)
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"log"
|
||||
"math"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -44,8 +46,15 @@ type ipcReq struct {
|
||||
Fields string `json:"fields,omitempty"`
|
||||
MinRecall int `json:"min_recall,omitempty"`
|
||||
QueryLimit int `json:"limit,omitempty"`
|
||||
Items string `json:"items,omitempty"` // lancedb_update_batch: [{"id":..,"delta":N}]
|
||||
TS string `json:"ts,omitempty"` // lancedb_update_batch: last_recalled_at
|
||||
// 2026-10-11 (t_a4b8fa52 方案 c′): 遗忘候选旋转窗口游标(last processed id)
|
||||
AfterID string `json:"after_id,omitempty"`
|
||||
Items string `json:"items,omitempty"` // lancedb_update_batch: [{"id":..,"delta":N}]
|
||||
TS string `json:"ts,omitempty"` // lancedb_update_batch: last_recalled_at
|
||||
// GA-P0-4 (t_bb10e8bc):hits+/hits− outcome 事件 IPC 参数
|
||||
Outcome string `json:"outcome,omitempty"` // hit_events: pos|neg
|
||||
Reason string `json:"reason,omitempty"` // hit_events: human_feedback|contradiction|replacement|downstream_use
|
||||
Source string `json:"source,omitempty"` // hit_events: 记录方标识
|
||||
Since string `json:"since,omitempty"` // hit_events 周窗口起点 (RFC3339)
|
||||
}
|
||||
|
||||
type ipcResp struct {
|
||||
@@ -119,7 +128,13 @@ func (rc *RustLanceDBClient) InsertEpisode(agentID, namespace, content, category
|
||||
"content": content, "category": category,
|
||||
"tier": "episode", "importance": 1.0, "quality_score": 0.0,
|
||||
"recall_count": 0, "useful_count": 0, "not_useful_count": 0,
|
||||
// GA-P0-2 (t_c6d41ac6):拆分字段(episode 无召回/反馈,恒 0)
|
||||
"recall_seen_count": 0, "recall_verified_count": 0,
|
||||
// GA-P0-1 (t_718d8707):拆分字段(episode 无反馈,恒 0)
|
||||
"human_not_useful_count": 0, "dedup_downweight_count": 0,
|
||||
"freshness": "fresh", "version": 1, "version_history": "[]",
|
||||
// t_268b0353:volatile_flag 插入路径恒 false;下方 *0.5 惩罚分支是预留
|
||||
//(将来接「≥3 次修正自动置位」),当前不生效,勿依赖。
|
||||
"source": "episode", "volatile_flag": false, "is_deleted": false,
|
||||
"depends_on": "[]", "derived_from": "", "last_recalled_at": "",
|
||||
"created_at": now.Format(time.RFC3339),
|
||||
@@ -154,7 +169,13 @@ func (rc *RustLanceDBClient) InsertMemory(m models.MemoryRecord) error {
|
||||
"content": m.Content, "category": m.Category, "vector": m.Vector,
|
||||
"tier": m.Tier, "importance": 1.0, "quality_score": m.QualityScore,
|
||||
"recall_count": m.RecallCount, "useful_count": 0, "not_useful_count": 0,
|
||||
// GA-P0-2 (t_c6d41ac6):拆分字段(新记忆按 model 携带,通常 0)
|
||||
"recall_seen_count": m.RecallSeenCount, "recall_verified_count": m.RecallVerifiedCount,
|
||||
// GA-P0-1 (t_718d8707):拆分字段(新记忆按 model 携带,通常 0)
|
||||
"human_not_useful_count": m.HumanNotUsefulCount, "dedup_downweight_count": m.DedupDownweightCount,
|
||||
"freshness": m.Freshness, "version": 1, "version_history": "[]",
|
||||
// t_268b0353:volatile_flag 插入路径恒 false;下方 *0.5 惩罚分支是预留
|
||||
//(将来接「≥3 次修正自动置位」),当前不生效,勿依赖。
|
||||
"source": m.Source, "volatile_flag": false, "is_deleted": m.IsDeleted,
|
||||
"depends_on": "[]", "derived_from": "", "last_recalled_at": "",
|
||||
"embedding_model": m.EmbeddingModel, "script_version": m.ScriptVersion, // 2026-10-08 (t_7e3577c5) 血缘
|
||||
@@ -177,19 +198,23 @@ func (rc *RustLanceDBClient) Search(table string, vector []float32, topK int, na
|
||||
}
|
||||
|
||||
var raw []struct {
|
||||
ID string `json:"id"`
|
||||
Content string `json:"content"`
|
||||
Category string `json:"category"`
|
||||
Namespace string `json:"namespace"`
|
||||
IsDeleted bool `json:"is_deleted"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
QualityScore float64 `json:"quality_score"`
|
||||
Importance float64 `json:"importance"`
|
||||
RecallCount int64 `json:"recall_count"`
|
||||
VolatileFlag bool `json:"volatile_flag"`
|
||||
Tier string `json:"tier"`
|
||||
LastRecalledAt string `json:"last_recalled_at"`
|
||||
ID string `json:"id"`
|
||||
Content string `json:"content"`
|
||||
Category string `json:"category"`
|
||||
Namespace string `json:"namespace"`
|
||||
IsDeleted bool `json:"is_deleted"`
|
||||
DeleteReason string `json:"delete_reason"` // GA-P0-3
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
QualityScore float64 `json:"quality_score"`
|
||||
Importance float64 `json:"importance"`
|
||||
RecallCount int64 `json:"recall_count"`
|
||||
// GA-P0-2 (t_c6d41ac6):拆分字段读侧接线
|
||||
RecallSeenCount int64 `json:"recall_seen_count"`
|
||||
RecallVerifiedCount int64 `json:"recall_verified_count"`
|
||||
VolatileFlag bool `json:"volatile_flag"`
|
||||
Tier string `json:"tier"`
|
||||
LastRecalledAt string `json:"last_recalled_at"`
|
||||
// 2026-09-26:出处(笔记路径)—— 此前**解包结构缺该字段** → IPC 主路径 Source 恒空
|
||||
// (本地兜底路径有 Source,主路径没有 → recall 永远拿不到出处,链路看似修好实则断在这一环)
|
||||
Source string `json:"source"`
|
||||
@@ -242,7 +267,9 @@ func (rc *RustLanceDBClient) Search(table string, vector []float32, topK int, na
|
||||
if daysSinceRecall > 30 {
|
||||
recencyFactor = 0.2 // 30天以上权重很低
|
||||
}
|
||||
recallCount := float64(r.RecallCount)
|
||||
// GA-P0-2 (t_c6d41ac6):排序去偏用 recall_seen_count(观测/选择信号);
|
||||
// recall_verified_count 只参与衰减/遗忘决策,不参与排序。
|
||||
recallCount := float64(r.RecallSeenCount)
|
||||
computedImportance := recencyFactor * (1.0 + math.Log(1.0+recallCount))
|
||||
|
||||
// VolatileFlag 惩罚
|
||||
@@ -251,17 +278,32 @@ func (rc *RustLanceDBClient) Search(table string, vector []float32, topK int, na
|
||||
}
|
||||
|
||||
// QualityScore 融合(归一化到 0-1 范围)
|
||||
// t_631ab13d:先存原始 DB 值(r.QualityScore 是 LanceDB 列直接值),
|
||||
// 融合后 finalQuality 写进 QualityScore(供排序),RawQualityScore 保留原值(供 recall 响应展示)
|
||||
dbRawQS := r.QualityScore
|
||||
finalQuality := math.Min(computedImportance, 1.0)
|
||||
if r.QualityScore > 0 {
|
||||
// 2026-10-12:存量 quality_score 有 54.2% 不可信(0=未打分 / 0.755=历史常量 bug)。
|
||||
// 修入口治不了存量 —— 消费端必须自己判定,不可信时把权重让给可信信号。
|
||||
qsTrusted := models.IsReliableQuality(r.QualityScore)
|
||||
if qsTrusted {
|
||||
// QualityScore from LLM (0-1) + computed importance, capped at 1.0
|
||||
finalQuality = math.Min(0.4*computedImportance+0.6*r.QualityScore, 1.0)
|
||||
} else if qstrustVerbose {
|
||||
log.Printf("[qstrust-ipc] 不可信质量分已剔除 id=%s rawQS=%.6f → 仅用 importance %.4f",
|
||||
r.ID, r.QualityScore, computedImportance)
|
||||
}
|
||||
// V 值融合:用户决策信号(correct/useful/not-useful)
|
||||
if VValueProvider != nil {
|
||||
vVal := VValueProvider(r.ID)
|
||||
if vVal > 0 {
|
||||
// 30% V值 + 30% importance + 40% LLM quality
|
||||
finalQuality = 0.3*vVal + 0.3*computedImportance + 0.4*r.QualityScore
|
||||
if qsTrusted {
|
||||
// 30% V值 + 30% importance + 40% LLM quality
|
||||
finalQuality = 0.3*vVal + 0.3*computedImportance + 0.4*r.QualityScore
|
||||
} else {
|
||||
// 质量分不可信 → 去掉它的 40%,剩余 30:30 重新归一为 50:50。
|
||||
// 这样既不让假分数进来,也不因为"少了一项"而整体压沉。
|
||||
finalQuality = 0.5*vVal + 0.5*computedImportance
|
||||
}
|
||||
if finalQuality > 1.0 {
|
||||
finalQuality = 1.0
|
||||
}
|
||||
@@ -273,9 +315,14 @@ func (rc *RustLanceDBClient) Search(table string, vector []float32, topK int, na
|
||||
CreatedAt: parseTime(r.CreatedAt), UpdatedAt: parseTime(r.UpdatedAt),
|
||||
QualityScore: finalQuality, Importance: r.Importance,
|
||||
RecallCount: int(r.RecallCount), VolatileFlag: r.VolatileFlag,
|
||||
Tier: r.Tier,
|
||||
Source: r.Source, // 2026-09-26:出处透出(缺此行 → recall.source 恒空)
|
||||
Vector: r.Vector, // 去重判据依赖,勿删
|
||||
Tier: r.Tier,
|
||||
// GA-P0-2 (t_c6d41ac6):拆分字段读侧接线
|
||||
RecallSeenCount: int(r.RecallSeenCount),
|
||||
RecallVerifiedCount: int(r.RecallVerifiedCount),
|
||||
Source: r.Source, // 2026-09-26:出处透出(缺此行 → recall.source 恒空)
|
||||
Vector: r.Vector, // 去重判据依赖,勿删
|
||||
// t_631ab13d:DB 原值直通 —— RecallResult 展示用它,排序用 QualityScore(融合值)
|
||||
RawQualityScore: &dbRawQS,
|
||||
// 2026-10-08 (Gap#2):解包结构有 ≠ 输出有。缺这两行 ⇒ 加列、改 schema、
|
||||
// 通 IPC 全做完,recall 拿到的 Confidence 仍是 nil(第 5 层静默断链,
|
||||
// 端到端测试才抓得到)。
|
||||
@@ -308,19 +355,27 @@ func (rc *RustLanceDBClient) Scan(limit int, namespaceFilter string) ([]models.M
|
||||
}
|
||||
|
||||
var raw []struct {
|
||||
ID string `json:"id"`
|
||||
Content string `json:"content"`
|
||||
Category string `json:"category"`
|
||||
Namespace string `json:"namespace"`
|
||||
IsDeleted bool `json:"is_deleted"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
Importance float64 `json:"importance"`
|
||||
ID string `json:"id"`
|
||||
Content string `json:"content"`
|
||||
Category string `json:"category"`
|
||||
Namespace string `json:"namespace"`
|
||||
IsDeleted bool `json:"is_deleted"`
|
||||
DeleteReason string `json:"delete_reason"` // GA-P0-3
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
Importance float64 `json:"importance"`
|
||||
QualityScore float64 `json:"quality_score"`
|
||||
RecallCount int64 `json:"recall_count"`
|
||||
Tier string `json:"tier"`
|
||||
Freshness string `json:"freshness"`
|
||||
Source string `json:"source"`
|
||||
RecallCount int64 `json:"recall_count"`
|
||||
// GA-P0-2 (t_c6d41ac6):拆分后的新字段,scan 也读
|
||||
RecallSeenCount int64 `json:"recall_seen_count"`
|
||||
RecallVerifiedCount int64 `json:"recall_verified_count"`
|
||||
NotUsefulCount int64 `json:"not_useful_count"`
|
||||
// GA-P0-1 (t_718d8707):拆分后的新字段,scan 也读(迁移脚本 / 列表需要)
|
||||
HumanNotUsefulCount int64 `json:"human_not_useful_count"`
|
||||
DedupDownweightCount int64 `json:"dedup_downweight_count"`
|
||||
Tier string `json:"tier"`
|
||||
Freshness string `json:"freshness"`
|
||||
Source string `json:"source"`
|
||||
// 2026-10-08 (Gap#2):scan 路径同样要投影,否则该路径返回的 confidence 恒 nil
|
||||
Confidence *float64 `json:"confidence"`
|
||||
ConfidenceSource string `json:"confidence_source"`
|
||||
@@ -348,6 +403,12 @@ func (rc *RustLanceDBClient) Scan(limit int, namespaceFilter string) ([]models.M
|
||||
CreatedAt: parseTime(r.CreatedAt), UpdatedAt: parseTime(r.UpdatedAt),
|
||||
Importance: r.Importance, QualityScore: r.QualityScore,
|
||||
RecallCount: int(r.RecallCount), Tier: r.Tier, Source: r.Source,
|
||||
// GA-P0-3 (t_f785d7f2):scan 读侧接线 delete_reason
|
||||
DeleteReason: r.DeleteReason,
|
||||
// GA-P0-2 (t_c6d41ac6):scan 读侧接线拆分字段
|
||||
RecallSeenCount: int(r.RecallSeenCount), RecallVerifiedCount: int(r.RecallVerifiedCount),
|
||||
// GA-P0-1 (t_718d8707):scan 读侧接线
|
||||
NotUsefulCount: int(r.NotUsefulCount), HumanNotUsefulCount: int(r.HumanNotUsefulCount), DedupDownweightCount: int(r.DedupDownweightCount),
|
||||
Confidence: r.Confidence, ConfidenceSource: r.ConfidenceSource, // 2026-10-08 (Gap#2)
|
||||
})
|
||||
if len(out) >= limit {
|
||||
@@ -357,6 +418,41 @@ func (rc *RustLanceDBClient) Scan(limit int, namespaceFilter string) ([]models.M
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ScanAll 全表扫描(仅存活行,无 5000 上限——Rust 侧 scan_all 上限 200000)。
|
||||
// GA-P0-1 (t_718d8707):供 not_useful_count 拆分迁移脚本读取全部行做统计。
|
||||
func (rc *RustLanceDBClient) ScanAll() ([]models.MemoryRecord, error) {
|
||||
resp, err := rc.rpc(ipcReq{Type: "lancedb_scan_all"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var raw []models.MemoryRecord
|
||||
if err := json.Unmarshal([]byte(resp.ReportJSON), &raw); err != nil {
|
||||
return nil, fmt.Errorf("scan_all unmarshal: %w", err)
|
||||
}
|
||||
// 同步到本地缓存(与 GetSkillCandidates 同款)
|
||||
_local.mu.Lock()
|
||||
for i := range raw {
|
||||
cp := raw[i]
|
||||
_local.memories[cp.ID] = &cp
|
||||
}
|
||||
_local.mu.Unlock()
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
// ScanDeleted 全表扫描**已删除**行(is_deleted=true)。GA-P0-3 (t_f785d7f2)。
|
||||
// 供 delete_reason 回填脚本枚举存量已删行 + 出直方图。只读,不落库。
|
||||
func (rc *RustLanceDBClient) ScanDeleted() ([]models.MemoryRecord, error) {
|
||||
resp, err := rc.rpc(ipcReq{Type: "lancedb_scan_deleted"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var raw []models.MemoryRecord
|
||||
if err := json.Unmarshal([]byte(resp.ReportJSON), &raw); err != nil {
|
||||
return nil, fmt.Errorf("scan_deleted unmarshal: %w", err)
|
||||
}
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
func (rc *RustLanceDBClient) localSearch(vec []float32, topK int, ns string) []models.MemoryRecord {
|
||||
_local.mu.RLock()
|
||||
defer _local.mu.RUnlock()
|
||||
@@ -432,10 +528,19 @@ func getIncCachedValue(id, field string, cache map[string]*models.MemoryRecord)
|
||||
switch field {
|
||||
case "recall_count":
|
||||
return m.RecallCount + 1
|
||||
// GA-P0-2 (t_c6d41ac6):拆分字段
|
||||
case "recall_seen_count":
|
||||
return m.RecallSeenCount + 1
|
||||
case "recall_verified_count":
|
||||
return m.RecallVerifiedCount + 1
|
||||
case "useful_count":
|
||||
return m.UsefulCount + 1
|
||||
case "not_useful_count":
|
||||
return m.NotUsefulCount + 1
|
||||
case "human_not_useful_count":
|
||||
return m.HumanNotUsefulCount + 1
|
||||
case "dedup_downweight_count":
|
||||
return m.DedupDownweightCount + 1
|
||||
}
|
||||
}
|
||||
return 1
|
||||
@@ -448,6 +553,35 @@ func (rc *RustLanceDBClient) Update(table, id string, fields map[string]any) err
|
||||
|
||||
// 1. 更新本地内存缓存
|
||||
if m, ok := _local.memories[id]; ok {
|
||||
// GA-P0-2 (t_c6d41ac6):recall_count 冻结,召回命中走 recall_seen_count,正反馈走 recall_verified_count。
|
||||
if v, ok := fields["recall_seen_count"]; ok {
|
||||
switch inc := v.(type) {
|
||||
case map[string]string:
|
||||
if inc["$inc"] == "1" {
|
||||
m.RecallSeenCount++
|
||||
}
|
||||
case map[string]interface{}:
|
||||
if inc["$inc"] == "1" {
|
||||
m.RecallSeenCount++
|
||||
}
|
||||
case int:
|
||||
m.RecallSeenCount = inc
|
||||
}
|
||||
}
|
||||
if v, ok := fields["recall_verified_count"]; ok {
|
||||
switch inc := v.(type) {
|
||||
case map[string]string:
|
||||
if inc["$inc"] == "1" {
|
||||
m.RecallVerifiedCount++
|
||||
}
|
||||
case map[string]interface{}:
|
||||
if inc["$inc"] == "1" {
|
||||
m.RecallVerifiedCount++
|
||||
}
|
||||
case int:
|
||||
m.RecallVerifiedCount = inc
|
||||
}
|
||||
}
|
||||
if v, ok := fields["recall_count"]; ok {
|
||||
switch inc := v.(type) {
|
||||
case map[string]string:
|
||||
@@ -490,6 +624,35 @@ func (rc *RustLanceDBClient) Update(table, id string, fields map[string]any) err
|
||||
m.NotUsefulCount = inc
|
||||
}
|
||||
}
|
||||
// GA-P0-1 (t_718d8707):拆分后的新字段也要同步更新本地缓存
|
||||
if v, ok := fields["human_not_useful_count"]; ok {
|
||||
switch inc := v.(type) {
|
||||
case map[string]string:
|
||||
if inc["$inc"] == "1" {
|
||||
m.HumanNotUsefulCount++
|
||||
}
|
||||
case map[string]interface{}:
|
||||
if inc["$inc"] == "1" {
|
||||
m.HumanNotUsefulCount++
|
||||
}
|
||||
case int:
|
||||
m.HumanNotUsefulCount = inc
|
||||
}
|
||||
}
|
||||
if v, ok := fields["dedup_downweight_count"]; ok {
|
||||
switch inc := v.(type) {
|
||||
case map[string]string:
|
||||
if inc["$inc"] == "1" {
|
||||
m.DedupDownweightCount++
|
||||
}
|
||||
case map[string]interface{}:
|
||||
if inc["$inc"] == "1" {
|
||||
m.DedupDownweightCount++
|
||||
}
|
||||
case int:
|
||||
m.DedupDownweightCount = inc
|
||||
}
|
||||
}
|
||||
if v, ok := fields["last_recalled_at"]; ok {
|
||||
if s, ok := v.(string); ok {
|
||||
if t, err := time.Parse(time.RFC3339, s); err == nil {
|
||||
@@ -597,6 +760,74 @@ func (rc *RustLanceDBClient) GetTopByQuality(agentID string, limit int) ([]model
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ─── 有界冲突重试(2026-10-11 t_5b5a83de 方案 a)────────────────────────
|
||||
//
|
||||
// LanceDB MVCC 写冲突(同一表并发提交)时返回 "Commit conflict ... rerun the operation"。
|
||||
// 旧实现 SoftDelete 遇冲突直接失败返回、无重试 ⇒ 那次 soft-delete 未落库(静默丢失),
|
||||
// 靠下一轮轮转重扫兜底 → 遗忘/合并延迟 + 日志刷错误。方案(a):识别可重试错误 → 有界退避重试。
|
||||
//
|
||||
// 重试语义:
|
||||
// - N = softDeleteMaxRetries = 3 次重试(总 rpc 尝试 = N+1 = 4 次);
|
||||
// - 退避 50/100/200ms(指数、cap 200ms),总最坏 +350ms(背景软删按分钟/小时节奏,可忽略);
|
||||
// - 更新是 SET is_deleted=true, updated_at=... 纯赋值 → 对最新已提交版本重放安全(幂等不累加);
|
||||
// - 仅「可重试冲突」重试;其余错误(空 id / "id required" / schema 错)立即返回、不重试。
|
||||
const (
|
||||
softDeleteMaxRetries = 3
|
||||
softDeleteBackoffBase = 50 * time.Millisecond
|
||||
softDeleteBackoffCap = 200 * time.Millisecond
|
||||
)
|
||||
|
||||
// isRetryableLanceConflict 判断错误是否为 LanceDB「可重试的 MVCC 写冲突」。
|
||||
// 仅当错误串含官方建议重试的关键词才返回 true(大小写不敏感):
|
||||
// "commit conflict" / "cannot be automatically resolved" / "rerun the operation"。
|
||||
// 其他错误(空 id、id required、schema 错、网络错等)一律 false,立即返回不重试。
|
||||
func isRetryableLanceConflict(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := strings.ToLower(err.Error())
|
||||
for _, kw := range []string{"commit conflict", "cannot be automatically resolved", "rerun the operation"} {
|
||||
if strings.Contains(msg, kw) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// retrySoftDeleteRPC 对 lancedb_update 调用做有界指数退避重试(仅可重试冲突)。
|
||||
// 返回:首次成功或重试后成功 = nil;耗尽 = 含尝试次数的明确错误;非可重试错误 = 原样立即返回。
|
||||
func (rc *RustLanceDBClient) retrySoftDeleteRPC(id, reason, fields string) error {
|
||||
backoff := softDeleteBackoffBase
|
||||
var lastErr error
|
||||
for attempt := 0; attempt <= softDeleteMaxRetries; attempt++ {
|
||||
if attempt > 0 {
|
||||
// 第 attempt 次重试(1-based):attempt=1→50ms, 2→100ms, 3→200ms
|
||||
log.Printf("[ipc] SoftDelete retry attempt %d/%d id=%s reason=%s error=%v",
|
||||
attempt, softDeleteMaxRetries, id, reason, lastErr)
|
||||
time.Sleep(backoff)
|
||||
if backoff < softDeleteBackoffCap {
|
||||
backoff *= 2
|
||||
if backoff > softDeleteBackoffCap {
|
||||
backoff = softDeleteBackoffCap
|
||||
}
|
||||
}
|
||||
}
|
||||
_, err := rc.rpc(ipcReq{Type: "lancedb_update", Table: "memories", ID: id, Fields: fields})
|
||||
if err == nil {
|
||||
if attempt > 0 {
|
||||
log.Printf("[ipc] SoftDelete retried OK after %d id=%s reason=%s", attempt, id, reason)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !isRetryableLanceConflict(err) {
|
||||
return err // 非可重试错误:立即返回,不重试
|
||||
}
|
||||
lastErr = err
|
||||
}
|
||||
return fmt.Errorf("soft delete persist (id=%s): retry exhausted after %d retries: %w",
|
||||
id, softDeleteMaxRetries, lastErr)
|
||||
}
|
||||
|
||||
func (rc *RustLanceDBClient) SoftDelete(id, reason string) error {
|
||||
// 🔴 2026-10-06 防御:空 id 会被 Rust IPC 直接拒绝("id required"),
|
||||
// 而调用方曾忽略错误照样计数(admin.go forgotten++),造成「遗忘计数虚高 + 每轮刷错误日志」。
|
||||
@@ -612,11 +843,14 @@ func (rc *RustLanceDBClient) SoftDelete(id, reason string) error {
|
||||
_local.mu.Unlock()
|
||||
|
||||
// 持久化 tombstone 到 Rust LanceDB(值按 SQL 表达式传递:true=布尔字面量,'...'=字符串字面量)
|
||||
// GA-P0-3 (t_f785d7f2):同批写入 delete_reason(归一化枚举),让 is_deleted=true 可分原因。
|
||||
dr := NormalizeDeleteReason(reason)
|
||||
fj, _ := json.Marshal([]updateField{
|
||||
{Column: "is_deleted", Value: "true"},
|
||||
{Column: "updated_at", Value: "'" + time.Now().Format(time.RFC3339) + "'"},
|
||||
{Column: "delete_reason", Value: "'" + dr + "'"},
|
||||
})
|
||||
if _, err := rc.rpc(ipcReq{Type: "lancedb_update", Table: "memories", ID: id, Fields: string(fj)}); err != nil {
|
||||
if err := rc.retrySoftDeleteRPC(id, reason, string(fj)); err != nil {
|
||||
log.Printf("[ipc] SoftDelete persist FAILED: id=%s reason=%s error=%v", id, reason, err)
|
||||
return fmt.Errorf("soft delete persist (id=%s): %w", id, err)
|
||||
}
|
||||
@@ -653,7 +887,7 @@ func (rc *RustLanceDBClient) SoftDeleteBatch(table string, ids []string, updated
|
||||
}
|
||||
|
||||
// PurgeInvalid 清理 id 为空的垃圾行(P1-1 2026-10-08)。
|
||||
// 走 Rust IPC lancedb_purge_invalid:按 id='' 条件一次性软删(is_deleted=true + updated_at=now)。
|
||||
// 走 Rust IPC lancedb_purge_invalid:按 id=” 条件一次性软删(is_deleted=true + updated_at=now)。
|
||||
// 这些行 id/content/source 全空、无法按 id 软删,只能按条件清。返回受影响行数。
|
||||
func (rc *RustLanceDBClient) PurgeInvalid() (int64, error) {
|
||||
now := time.Now().Format(time.RFC3339)
|
||||
@@ -674,47 +908,118 @@ func (rc *RustLanceDBClient) PurgeInvalid() (int64, error) {
|
||||
func (rc *RustLanceDBClient) GetVersionHistory(id string) ([]map[string]interface{}, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (rc *RustLanceDBClient) GetCandidatesForForgetting() ([]map[string]interface{}, error) {
|
||||
// P2 2026-09-06: 走 Rust 全表扫描(安全版: limit 2000 + 跳 vector + 真读 last_recalled_at)。
|
||||
// 替代纯缓存遍历 — 缓存只含近期 commit/recall/search 触碰的记忆, 到期旧记忆扫不到 → 9-25 遗忘闭环断。
|
||||
// IPC 失败/空 → fallback 缓存遍历(保持可用)。
|
||||
resp, err := rc.rpc(ipcReq{Type: "lancedb_scan", QueryLimit: 2000})
|
||||
if err == nil && resp.ReportJSON != "" {
|
||||
var raw []struct {
|
||||
ID string `json:"id"`
|
||||
Content string `json:"content"`
|
||||
Namespace string `json:"namespace"`
|
||||
Category string `json:"category"`
|
||||
Tier string `json:"tier"`
|
||||
IsDeleted bool `json:"is_deleted"`
|
||||
Importance float64 `json:"importance"`
|
||||
RecallCount int64 `json:"recall_count"`
|
||||
LastRecalledAt string `json:"last_recalled_at"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
|
||||
// forgetCursorPath 遗忘候选旋转窗口游标持久化文件(2026-10-11 t_a4b8fa52 方案 c′)。
|
||||
// 游标 = 上一轮扫描窗口的末 id(mem_<unixnano>);文件缺失/损坏/库回滚 → 空串(从头轮转,降级不崩)。
|
||||
var forgetCursorPath = "/var/lib/memoryweave/forget-cursor.json"
|
||||
|
||||
// loadForgetCursor 读取持久化游标。缺失/空/损坏 → 返回 ""(降级为从头轮转,不崩)。
|
||||
func loadForgetCursor() string {
|
||||
data, err := os.ReadFile(forgetCursorPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return "" // 首次运行或文件被清,正常
|
||||
}
|
||||
uerr := json.Unmarshal([]byte(resp.ReportJSON), &raw)
|
||||
if uerr == nil && len(raw) > 0 {
|
||||
log.Printf("[ipc] GetCandidatesForForgetting: Rust scan → %d candidates", len(raw))
|
||||
out := make([]map[string]interface{}, 0, len(raw))
|
||||
for _, r := range raw {
|
||||
log.Printf("[ipc] loadForgetCursor read failed: %v → 降级从头轮转", err)
|
||||
return ""
|
||||
}
|
||||
s := strings.TrimSpace(string(data))
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
if !strings.HasPrefix(s, "mem_") {
|
||||
log.Printf("[ipc] loadForgetCursor corrupt (not mem_ prefix): %q → 降级从头轮转", s)
|
||||
return ""
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// persistForgetCursor 原子写游标(tmp+rename,复用 conflict-seen 的原子写模式)。
|
||||
func persistForgetCursor(cursor string) {
|
||||
if cursor == "" {
|
||||
return // wrapped → 下轮从头,无需落盘空游标(缺文件=从头)
|
||||
}
|
||||
dir := filepath.Dir(forgetCursorPath)
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
log.Printf("[ipc] persistForgetCursor mkdir failed: %v", err)
|
||||
return
|
||||
}
|
||||
tmp := forgetCursorPath + ".tmp"
|
||||
if err := os.WriteFile(tmp, []byte(cursor), 0o644); err != nil {
|
||||
log.Printf("[ipc] persistForgetCursor write failed: %v", err)
|
||||
return
|
||||
}
|
||||
if err := os.Rename(tmp, forgetCursorPath); err != nil {
|
||||
log.Printf("[ipc] persistForgetCursor rename failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// GetCandidatesForForgetting 取遗忘候选:走 Rust `lancedb_forget_window` 旋转窗口扫描。
|
||||
//
|
||||
// 2026-10-11 t_a4b8fa52 方案 c′:修复「固定 2000 窗口不轮转」。
|
||||
// 旧实现 lancedb_scan 无 ORDER BY ⇒ 返回物理存储序前 2000 条(compact/merge 后非创建序)
|
||||
// ⇒ 新记忆落物理序后部 ⇒ 永不进遗忘候选池。方案 c′:Rust 列投影取全部存活 id →
|
||||
// 内排序 → 连续窗口 → 区间过滤还原全字段;游标=窗口末 id,本函数持久化到 forget-cursor.json。
|
||||
// 每轮只评估一个 2000 窗口,全库覆盖上界 = ceil(live/2000)×6h(实测 ~72h 全覆盖,吞吐余量见卡 AC-7)。
|
||||
// IPC 失败/空 → fallback 缓存遍历(保持可用,回归安全)。
|
||||
func (rc *RustLanceDBClient) GetCandidatesForForgetting() ([]map[string]interface{}, error) {
|
||||
// 读游标(缺失/损坏 → 空串 = 从头轮转)
|
||||
cursor := loadForgetCursor()
|
||||
resp, err := rc.rpc(ipcReq{Type: "lancedb_forget_window", QueryLimit: 2000, AfterID: cursor})
|
||||
if err == nil && resp.ReportJSON != "" {
|
||||
var win struct {
|
||||
Records []struct {
|
||||
ID string `json:"id"`
|
||||
Content string `json:"content"`
|
||||
Namespace string `json:"namespace"`
|
||||
Category string `json:"category"`
|
||||
Tier string `json:"tier"`
|
||||
IsDeleted bool `json:"is_deleted"`
|
||||
Importance float64 `json:"importance"`
|
||||
RecallCount int64 `json:"recall_count"`
|
||||
// GA-P0-2 (t_c6d41ac6):拆分字段读侧接线
|
||||
RecallSeenCount int64 `json:"recall_seen_count"`
|
||||
RecallVerifiedCount int64 `json:"recall_verified_count"`
|
||||
LastRecalledAt string `json:"last_recalled_at"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
} `json:"records"`
|
||||
NextCursor string `json:"next_cursor"`
|
||||
Wrapped bool `json:"wrapped"`
|
||||
LiveTotal int `json:"live_total"`
|
||||
}
|
||||
uerr := json.Unmarshal([]byte(resp.ReportJSON), &win)
|
||||
if uerr == nil && (len(win.Records) > 0 || win.LiveTotal > 0) {
|
||||
// 持久化新游标(wrapped → 空串,下轮从头)
|
||||
persistForgetCursor(win.NextCursor)
|
||||
// 覆盖度与吞吐日志(AC-7 判据源:live_total 应随轮转被消耗,写入率 142/h < 窗口率 2000/6h≈333/h)
|
||||
if win.LiveTotal > 0 {
|
||||
log.Printf("[ipc] GetCandidatesForForgetting: window n=%d live_total=%d wrapped=%v next_cursor=%s(池内覆盖累计进度)",
|
||||
len(win.Records), win.LiveTotal, win.Wrapped, truncID(win.NextCursor))
|
||||
}
|
||||
log.Printf("[ipc] GetCandidatesForForgetting: Rust forget_window → %d candidates", len(win.Records))
|
||||
out := make([]map[string]interface{}, 0, len(win.Records))
|
||||
for _, r := range win.Records {
|
||||
if r.IsDeleted || r.Tier == "core" {
|
||||
continue
|
||||
}
|
||||
out = append(out, map[string]interface{}{
|
||||
"id": r.ID,
|
||||
"content": r.Content,
|
||||
"namespace": r.Namespace,
|
||||
"category": r.Category,
|
||||
"tier": r.Tier,
|
||||
"importance": r.Importance,
|
||||
"recall_count": int(r.RecallCount), // decay 侧 .(int) 断言 — int64 会恒 0
|
||||
"last_recalled_at": r.LastRecalledAt,
|
||||
"created_at": r.CreatedAt,
|
||||
"id": r.ID,
|
||||
"content": r.Content,
|
||||
"namespace": r.Namespace,
|
||||
"category": r.Category,
|
||||
"tier": r.Tier,
|
||||
"importance": r.Importance,
|
||||
"recall_count": int(r.RecallCount), // decay 侧 .(int) 断言 — int64 会恒 0
|
||||
// GA-P0-2 (t_c6d41ac6):衰减/遗忘决策读 recall_verified_count
|
||||
"recall_seen_count": int(r.RecallSeenCount),
|
||||
"recall_verified_count": int(r.RecallVerifiedCount),
|
||||
"last_recalled_at": r.LastRecalledAt,
|
||||
"created_at": r.CreatedAt,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
log.Printf("[ipc] GetCandidatesForForgetting: scan unmarshal empty (rpc=%v err=%v raw=%d) → fallback 缓存", err, uerr, len(raw))
|
||||
log.Printf("[ipc] GetCandidatesForForgetting: forget_window unmarshal empty (rpc=%v err=%v raw=%d) → fallback 缓存", err, uerr, len(win.Records))
|
||||
}
|
||||
_local.mu.RLock()
|
||||
defer _local.mu.RUnlock()
|
||||
@@ -729,8 +1034,11 @@ func (rc *RustLanceDBClient) GetCandidatesForForgetting() ([]map[string]interfac
|
||||
"last_recalled_at": fmtTimeRFC3339(m.LastRecalledAt),
|
||||
"created_at": fmtTimeRFC3339(m.CreatedAt),
|
||||
"recall_count": m.RecallCount,
|
||||
"tier": m.Tier,
|
||||
"importance": m.Importance,
|
||||
// GA-P0-2 (t_c6d41ac6):衰减/遗忘决策读 recall_verified_count
|
||||
"recall_seen_count": m.RecallSeenCount,
|
||||
"recall_verified_count": m.RecallVerifiedCount,
|
||||
"tier": m.Tier,
|
||||
"importance": m.Importance,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -748,6 +1056,14 @@ func fmtTimeRFC3339(t time.Time) string {
|
||||
return t.Format(time.RFC3339)
|
||||
}
|
||||
|
||||
// truncID 截断长 id 用于日志(mem_<unixnano> 19 位+前缀,全打印太长)
|
||||
func truncID(id string) string {
|
||||
if len(id) > 16 {
|
||||
return id[:8] + "…" + id[len(id)-6:]
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func mapStr(m map[string]interface{}, key string) string {
|
||||
if v, ok := m[key]; ok {
|
||||
if s, ok := v.(string); ok {
|
||||
@@ -837,7 +1153,13 @@ func (rc *RustLanceDBClient) GetSkillCandidates(minRecalls, limit int) ([]models
|
||||
m.ConfidenceSource = mapStr(r, "confidence_source")
|
||||
m.UsefulCount = mapInt(r, "useful_count")
|
||||
m.NotUsefulCount = mapInt(r, "not_useful_count")
|
||||
// GA-P0-1 (t_718d8707):读侧接线拆分后的新字段(漏接线 ⇒ 加列也读不出,静默 no-op)
|
||||
m.HumanNotUsefulCount = mapInt(r, "human_not_useful_count")
|
||||
m.DedupDownweightCount = mapInt(r, "dedup_downweight_count")
|
||||
m.RecallCount = mapInt(r, "recall_count")
|
||||
// GA-P0-2 (t_c6d41ac6):拆分字段读侧接线
|
||||
m.RecallSeenCount = mapInt(r, "recall_seen_count")
|
||||
m.RecallVerifiedCount = mapInt(r, "recall_verified_count")
|
||||
m.Version = mapInt(r, "version")
|
||||
m.VolatileFlag = mapBool(r, "volatile_flag")
|
||||
m.IsDeleted = mapBool(r, "is_deleted")
|
||||
@@ -901,9 +1223,11 @@ func (rc *RustLanceDBClient) GetAuditLog(limit int) ([]map[string]interface{}, e
|
||||
// 污染召回统计(召回率/新鲜度衰减都基于 recall_count)。
|
||||
// 对照 MemLanceClient.IncrementUseful:UsefulCount++ 且 RecallCount++。
|
||||
func (rc *RustLanceDBClient) IncrementUseful(id string) error {
|
||||
// GA-P0-2 (t_c6d41ac6):正反馈 → useful_count+1 且 recall_verified_count+1。
|
||||
// 不再碰 recall_count(冻结,保留历史值——虚高保命来源已被切断)。
|
||||
if err := rc.Update("memories", id, map[string]any{
|
||||
"useful_count": map[string]string{"$inc": "1"},
|
||||
"recall_count": map[string]string{"$inc": "1"},
|
||||
"useful_count": map[string]string{"$inc": "1"},
|
||||
"recall_verified_count": map[string]string{"$inc": "1"},
|
||||
}); err != nil {
|
||||
log.Printf("[ipc] IncrementUseful failed id=%s: %v", id, err)
|
||||
return err
|
||||
@@ -911,15 +1235,18 @@ func (rc *RustLanceDBClient) IncrementUseful(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IncrementNotUseful 递增 not_useful_count(降权)。
|
||||
// IncrementNotUseful [DEPRECATED] 递增 not_useful_count(GA-P0-1 t_718d8707)。
|
||||
//
|
||||
// 2026-09-20 修复:此前是**空实现** `{}`,导致 feedback/not-useful 端点与
|
||||
// 历史(2026-09-20 修复):此前是**空实现** `{}`,导致 feedback/not-useful 端点与
|
||||
// 一切降权类清理(zhiyi-dedup.py 等)全是**假成功**——HTTP 200 返回正常,
|
||||
// 但 not_useful_count 从未落库。实测:dedup --apply 报「已降权 34 条,
|
||||
// 失败 0」,回读 32 条 dropped 记录的 not_useful_count 全为 0。
|
||||
//
|
||||
// 委托给 Update 并传 `$inc`,由 Update 转成 Rust 侧的 `col + 1` 表达式
|
||||
// 实现真自增(见 Update 内注释)。
|
||||
// 2026-10-12 (GA-P0-1 t_718d8707):not_useful_count 曾把两种语义混写——
|
||||
// 人工点「没用」(feedback.go)与 dedup 自动降权(core.go dedup 路径)都写它。
|
||||
// 现拆分为 human_not_useful_count / dedup_downweight_count 两个字段,
|
||||
// 两个写入点各写各的(IncrementHumanNotUseful / IncrementDedupDownweight)。
|
||||
// 本方法仅向后兼容,**新代码不再调用它**。
|
||||
func (rc *RustLanceDBClient) IncrementNotUseful(id string) error {
|
||||
if err := rc.Update("memories", id, map[string]any{
|
||||
"not_useful_count": map[string]string{"$inc": "1"},
|
||||
@@ -930,6 +1257,93 @@ func (rc *RustLanceDBClient) IncrementNotUseful(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IncrementHumanNotUseful 递增 human_not_useful_count(用户人工点「没用」,GA-P0-1)。
|
||||
func (rc *RustLanceDBClient) IncrementHumanNotUseful(id string) error {
|
||||
if err := rc.Update("memories", id, map[string]any{
|
||||
"human_not_useful_count": map[string]string{"$inc": "1"},
|
||||
}); err != nil {
|
||||
log.Printf("[ipc] IncrementHumanNotUseful failed id=%s: %v", id, err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IncrementDedupDownweight 递增 dedup_downweight_count(dedup/合并流程自动降权,GA-P0-1)。
|
||||
func (rc *RustLanceDBClient) IncrementDedupDownweight(id string) error {
|
||||
if err := rc.Update("memories", id, map[string]any{
|
||||
"dedup_downweight_count": map[string]string{"$inc": "1"},
|
||||
}); err != nil {
|
||||
log.Printf("[ipc] IncrementDedupDownweight failed id=%s: %v", id, err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AppendHitEvent 追加一条真实 outcome 事件(GA-P0-4 t_bb10e8bc,走 Rust IPC)。
|
||||
func (rc *RustLanceDBClient) AppendHitEvent(memoryID, outcome, reason string) error {
|
||||
outcome = NormalizeHitOutcome(outcome)
|
||||
if !ValidHitOutcomes[outcome] {
|
||||
return fmt.Errorf("AppendHitEvent: invalid outcome %q (must be pos/neg); dedup-demote/recall-appearance are NOT outcomes", outcome)
|
||||
}
|
||||
reason = NormalizeHitReason(reason)
|
||||
_, err := rc.rpc(ipcReq{
|
||||
Type: "lancedb_append_hit_event",
|
||||
ID: memoryID,
|
||||
Outcome: outcome,
|
||||
Reason: reason,
|
||||
Source: "append_hit_event",
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("[ipc] AppendHitEvent failed id=%s: %v", memoryID, err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CountHitEvents 统计某 memory 在某窗口起点的 outcome 事件数(周窗口聚合,P2 自适应阈值用)。
|
||||
func (rc *RustLanceDBClient) CountHitEvents(memoryID, outcome string, since time.Time) (int, error) {
|
||||
outcome = NormalizeHitOutcome(outcome)
|
||||
if !ValidHitOutcomes[outcome] {
|
||||
return 0, fmt.Errorf("CountHitEvents: invalid outcome %q", outcome)
|
||||
}
|
||||
resp, err := rc.rpc(ipcReq{
|
||||
Type: "lancedb_count_hit_events",
|
||||
ID: memoryID,
|
||||
Outcome: outcome,
|
||||
Since: since.Format(time.RFC3339),
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var out struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(resp.Result), &out); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return out.Count, nil
|
||||
}
|
||||
|
||||
// GetHitEvents 读回某 memory 的事件明细(read-back 取证,非只看函数返回)。
|
||||
func (rc *RustLanceDBClient) GetHitEvents(memoryID string, limit int) ([]models.HitEvent, error) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
resp, err := rc.rpc(ipcReq{
|
||||
Type: "lancedb_get_hit_events",
|
||||
ID: memoryID,
|
||||
QueryLimit: limit,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var events []models.HitEvent
|
||||
if err := json.Unmarshal([]byte(resp.Result), &events); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
|
||||
// UpdateMemoryContent 修正记忆内容(重新编码向量)并**持久化**。
|
||||
//
|
||||
// 2026-09-20 修复:此前只改内存缓存 `_local.memories`,不写 LanceDB,
|
||||
|
||||
@@ -14,11 +14,12 @@ import (
|
||||
|
||||
// MemLanceClient 内存实现 LanceDB 接口,生产部署时替换为 Rust LanceDB
|
||||
type MemLanceClient struct {
|
||||
mu sync.RWMutex
|
||||
memories map[string]*memEntry
|
||||
episodes []models.EpisodeRecord
|
||||
auditLog []map[string]interface{}
|
||||
embed *Embedder
|
||||
mu sync.RWMutex
|
||||
memories map[string]*memEntry
|
||||
episodes []models.EpisodeRecord
|
||||
auditLog []map[string]interface{}
|
||||
hitEvents []models.HitEvent // GA-P0-4 (t_bb10e8bc):append-only outcome 事件
|
||||
embed *Embedder
|
||||
}
|
||||
|
||||
type memEntry struct {
|
||||
@@ -32,14 +33,21 @@ type memEntry struct {
|
||||
QualityScore float64
|
||||
UsefulCount int
|
||||
NotUsefulCount int
|
||||
RecallCount int
|
||||
Version int
|
||||
VersionHistory []map[string]interface{}
|
||||
Source string
|
||||
IsDeleted bool
|
||||
LastRecalledAt string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt string
|
||||
// GA-P0-1 (t_718d8707):拆分字段
|
||||
HumanNotUsefulCount int
|
||||
DedupDownweightCount int
|
||||
RecallCount int
|
||||
// GA-P0-2 (t_c6d41ac6):拆分字段
|
||||
RecallSeenCount int
|
||||
RecallVerifiedCount int
|
||||
Version int
|
||||
VersionHistory []map[string]interface{}
|
||||
Source string
|
||||
IsDeleted bool
|
||||
DeleteReason string // GA-P0-3
|
||||
LastRecalledAt string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
func NewMemLanceClient(embedder *Embedder) *MemLanceClient {
|
||||
@@ -176,6 +184,7 @@ func (mlc *MemLanceClient) SoftDelete(id, reason string) error {
|
||||
defer mlc.mu.Unlock()
|
||||
if e, ok := mlc.memories[id]; ok {
|
||||
e.IsDeleted = true
|
||||
e.DeleteReason = NormalizeDeleteReason(reason) // GA-P0-3
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -266,11 +275,14 @@ func (mlc *MemLanceClient) IncrementUseful(id string) error {
|
||||
defer mlc.mu.Unlock()
|
||||
if e, ok := mlc.memories[id]; ok {
|
||||
e.UsefulCount++
|
||||
e.RecallCount++
|
||||
// GA-P0-2 (t_c6d41ac6):正反馈 → recall_verified_count++;不再动 recall_count(冻结)
|
||||
e.RecallVerifiedCount++
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IncrementNotUseful [DEPRECATED](GA-P0-1 t_718d8707)历史混写列;新代码走
|
||||
// IncrementHumanNotUseful(人工) / IncrementDedupDownweight(dedup 降权)。
|
||||
func (mlc *MemLanceClient) IncrementNotUseful(id string) error {
|
||||
mlc.mu.Lock()
|
||||
defer mlc.mu.Unlock()
|
||||
@@ -280,6 +292,82 @@ func (mlc *MemLanceClient) IncrementNotUseful(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IncrementHumanNotUseful 递增 human_not_useful_count(用户人工点「没用」,GA-P0-1)。
|
||||
func (mlc *MemLanceClient) IncrementHumanNotUseful(id string) error {
|
||||
mlc.mu.Lock()
|
||||
defer mlc.mu.Unlock()
|
||||
if e, ok := mlc.memories[id]; ok {
|
||||
e.HumanNotUsefulCount++
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IncrementDedupDownweight 递增 dedup_downweight_count(dedup/合并流程自动降权,GA-P0-1)。
|
||||
func (mlc *MemLanceClient) IncrementDedupDownweight(id string) error {
|
||||
mlc.mu.Lock()
|
||||
defer mlc.mu.Unlock()
|
||||
if e, ok := mlc.memories[id]; ok {
|
||||
e.DedupDownweightCount++
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AppendHitEvent 追加一条真实 outcome 事件(GA-P0-4 t_bb10e8bc,内存实现)。
|
||||
func (mlc *MemLanceClient) AppendHitEvent(memoryID, outcome, reason string) error {
|
||||
outcome = NormalizeHitOutcome(outcome)
|
||||
if !ValidHitOutcomes[outcome] {
|
||||
return fmt.Errorf("AppendHitEvent: invalid outcome %q (must be pos/neg); dedup-demote/recall-appearance are NOT outcomes", outcome)
|
||||
}
|
||||
reason = NormalizeHitReason(reason)
|
||||
mlc.mu.Lock()
|
||||
defer mlc.mu.Unlock()
|
||||
mlc.hitEvents = append(mlc.hitEvents, models.HitEvent{
|
||||
ID: int64(len(mlc.hitEvents) + 1),
|
||||
MemoryID: memoryID,
|
||||
Outcome: outcome,
|
||||
Reason: reason,
|
||||
Source: "append_hit_event",
|
||||
CreatedAt: time.Now(),
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// CountHitEvents 统计某 memory 在某时间窗起点之后的 outcome 事件数(内存实现)。
|
||||
func (mlc *MemLanceClient) CountHitEvents(memoryID, outcome string, since time.Time) (int, error) {
|
||||
outcome = NormalizeHitOutcome(outcome)
|
||||
if !ValidHitOutcomes[outcome] {
|
||||
return 0, fmt.Errorf("CountHitEvents: invalid outcome %q", outcome)
|
||||
}
|
||||
mlc.mu.RLock()
|
||||
defer mlc.mu.RUnlock()
|
||||
n := 0
|
||||
for _, ev := range mlc.hitEvents {
|
||||
if ev.MemoryID == memoryID && ev.Outcome == outcome && !ev.CreatedAt.Before(since) {
|
||||
n++
|
||||
}
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// GetHitEvents 读回某 memory 的事件明细(内存实现)。
|
||||
func (mlc *MemLanceClient) GetHitEvents(memoryID string, limit int) ([]models.HitEvent, error) {
|
||||
mlc.mu.RLock()
|
||||
defer mlc.mu.RUnlock()
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
var out []models.HitEvent
|
||||
for _, ev := range mlc.hitEvents {
|
||||
if ev.MemoryID == memoryID {
|
||||
out = append(out, ev)
|
||||
if len(out) >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (mlc *MemLanceClient) UpdateMemoryContent(id, newContent, source string) error {
|
||||
mlc.mu.Lock()
|
||||
defer mlc.mu.Unlock()
|
||||
@@ -308,6 +396,26 @@ func (mlc *MemLanceClient) Update(table, id string, fields map[string]any) error
|
||||
defer mlc.mu.Unlock()
|
||||
if table == "memories" {
|
||||
if e, ok := mlc.memories[id]; ok {
|
||||
// GA-P0-2 (t_c6d41ac6):recall_count 冻结。召回命中改走 recall_seen_count;
|
||||
// 正反馈走 recall_verified_count(也经 Update 路径的反馈端点)。
|
||||
if v, ok := fields["recall_seen_count"]; ok {
|
||||
if incMap, ok := v.(map[string]string); ok {
|
||||
if incStr, ok := incMap["$inc"]; ok {
|
||||
if incStr == "1" {
|
||||
e.RecallSeenCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if v, ok := fields["recall_verified_count"]; ok {
|
||||
if incMap, ok := v.(map[string]string); ok {
|
||||
if incStr, ok := incMap["$inc"]; ok {
|
||||
if incStr == "1" {
|
||||
e.RecallVerifiedCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if v, ok := fields["recall_count"]; ok {
|
||||
// handle $inc
|
||||
if incMap, ok := v.(map[string]string); ok {
|
||||
@@ -331,6 +439,7 @@ func (mlc *MemLanceClient) PurgeInvalid() (int64, error) {
|
||||
for id, e := range mlc.memories {
|
||||
if strings.TrimSpace(id) == "" && !e.IsDeleted {
|
||||
e.IsDeleted = true
|
||||
e.DeleteReason = DeleteReasonRetention // GA-P0-3
|
||||
n++
|
||||
}
|
||||
}
|
||||
@@ -381,6 +490,7 @@ func (mlc *MemLanceClient) SoftDeleteBatch(table string, ids []string, updatedAt
|
||||
if e, ok := mlc.memories[id]; ok {
|
||||
if !e.IsDeleted {
|
||||
e.IsDeleted = true
|
||||
e.DeleteReason = DeleteReasonUnknown // GA-P0-3:批量无逐条原因
|
||||
e.UpdatedAt = updatedAt
|
||||
updated++
|
||||
}
|
||||
|
||||
+105
-43
@@ -17,18 +17,19 @@ import (
|
||||
// ── 2026-10-08 (Gap#2 来源加权):门控参数 ──────────────────────────────
|
||||
//
|
||||
// 设计取舍(MoA 评审采纳):
|
||||
// · 默认 λ=0 ⇒ **首日 no-op**。存量 15105 条 confidence 全为 NULL,
|
||||
// 此时任何加权都只是在噪声上做乘法。先接线、采分布,再定系数。
|
||||
// · 形式为**加性去中心偏置** base + λ(conf − mean),而非乘性 base*conf:
|
||||
// memories 表 100% 经 LLM 蒸馏(server.go 唯一 InsertMemory 调用点),
|
||||
// 乘性会把整批蒸馏产物一起压沉,并与 QualityScore 在同一根轴上二次惩罚。
|
||||
// 加性只平移均值、不缩方差,对尾部排序影响可控。
|
||||
// · 启用 = 改环境变量,不改代码(可回滚,无需重新构建)。
|
||||
//
|
||||
// · 默认 λ=0 ⇒ **首日 no-op**。存量 15105 条 confidence 全为 NULL,
|
||||
// 此时任何加权都只是在噪声上做乘法。先接线、采分布,再定系数。
|
||||
// · 形式为**加性去中心偏置** base + λ(conf − mean),而非乘性 base*conf:
|
||||
// memories 表 100% 经 LLM 蒸馏(server.go 唯一 InsertMemory 调用点),
|
||||
// 乘性会把整批蒸馏产物一起压沉,并与 QualityScore 在同一根轴上二次惩罚。
|
||||
// 加性只平移均值、不缩方差,对尾部排序影响可控。
|
||||
// · 启用 = 改环境变量,不改代码(可回滚,无需重新构建)。
|
||||
//
|
||||
// 启用前必做(护栏,见设计文档):跨 namespace 公平性检查 + distill 召回配额下限。
|
||||
var (
|
||||
confidenceLambda = envFloat("ZHIYI_CONFIDENCE_LAMBDA", 0) // 0 = 关闭
|
||||
confidenceMean = envFloat("ZHIYI_CONFIDENCE_MEAN", 0.7) // 去中心基准
|
||||
confidenceLambda = envFloat("ZHIYI_CONFIDENCE_LAMBDA", 0) // 0 = 关闭
|
||||
confidenceMean = envFloat("ZHIYI_CONFIDENCE_MEAN", 0.7) // 去中心基准
|
||||
)
|
||||
|
||||
func envFloat(key string, def float64) float64 {
|
||||
@@ -65,12 +66,12 @@ type GraphExpander interface {
|
||||
}
|
||||
|
||||
type RecallPipeline struct {
|
||||
embedder *Embedder
|
||||
lancedb LanceDB
|
||||
reranker *Reranker
|
||||
graph GraphExpander
|
||||
prefetch PrefetchPusher
|
||||
recordMiss MissRecorder
|
||||
embedder *Embedder
|
||||
lancedb LanceDB
|
||||
reranker *Reranker
|
||||
graph GraphExpander
|
||||
prefetch PrefetchPusher
|
||||
recordMiss MissRecorder
|
||||
}
|
||||
|
||||
func NewRecallPipeline(embedder *Embedder, lancedb LanceDB, reranker *Reranker) *RecallPipeline {
|
||||
@@ -96,16 +97,28 @@ func (p *RecallPipeline) SetMissRecorder(mr MissRecorder) {
|
||||
p.recordMiss = mr
|
||||
}
|
||||
|
||||
// qstrustVerbose 控制"不可信质量分已剔除"的逐条日志。
|
||||
// 默认关闭:一次召回可命中上百条,全量打印会淹没生产日志。
|
||||
// 诊断时置 ZHIYI_QSTRUST_VERBOSE=1。
|
||||
var qstrustVerbose = os.Getenv("ZHIYI_QSTRUST_VERBOSE") == "1"
|
||||
|
||||
// RecallMeta RecallWithMeta 返回的召回元数据(t_949840b1:candidates_scanned 名实相符)。
|
||||
// CandidatesScanned = 粗检索候选池真实大小(LanceDB ANN 检索返回 + 去重 +
|
||||
// 原始对话过滤之后的候选数,在 rerank/MMR 截断之前捕获)。
|
||||
// 这才是 recall API 里 candidates_scanned 字段的真实语义——此前误用
|
||||
// len(results)(最终产出数)作代理值,字段名说 "scanned"(扫描候选池)值却是"产出数"。
|
||||
//
|
||||
// CacheHit(t_1c12e000):本次结果是否直接来自缓存(未回源扫表)。
|
||||
// 事故根因之一是调用方无法区分 candidates_scanned=0 到底是「真没扫到」
|
||||
// 还是「读了 1h 前的旧快照」⇒ 响应里必须显式给出 cache_hit。
|
||||
// 约定:缓存命中无法重建候选池 ⇒ CandidatesScanned=0(保守)且 CacheHit=true;
|
||||
// 回源扫表 ⇒ CacheHit=false(此时 CandidatesScanned 为真实候选池大小)。
|
||||
type RecallMeta struct {
|
||||
CandidatesScanned int
|
||||
CacheHit bool
|
||||
}
|
||||
|
||||
// RecallWithMeta 同 Recall,但额外返回召回元数据(候选池真实大小)。
|
||||
// RecallWithMeta 同 Recall,但额外返回召回元数据(候选池真实大小 + 是否命中缓存)。
|
||||
// 缓存命中时无法重建候选池 ⇒ CandidatesScanned = 0(保守:无法测量就报 0,不虚报)。
|
||||
func (p *RecallPipeline) RecallWithMeta(query, namespace string, topK int, diversity float64) ([]models.RecallResult, RecallMeta, error) {
|
||||
// topK 先归一:缓存键必须与写入侧用的 topK 一致。recallUncached 内部也会归一,
|
||||
@@ -123,7 +136,8 @@ func (p *RecallPipeline) RecallWithMeta(query, namespace string, topK int, diver
|
||||
filtered = results
|
||||
}
|
||||
p.recordRecallWrites(filtered)
|
||||
return filtered, RecallMeta{}, nil
|
||||
// t_1c12e000:命中缓存 ⇒ 显式告知调用方(candidates_scanned 恒 0)
|
||||
return filtered, RecallMeta{CacheHit: true}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -192,11 +206,11 @@ func (p *RecallPipeline) recallUncached(query, namespace string, topK int, diver
|
||||
// 蒸馏产物通过 OnDistillComplete callback 写入 memories(category=distilled)
|
||||
filtered := make([]models.MemoryRecord, 0, len(candidates))
|
||||
rawCategories := map[string]bool{
|
||||
"episodes": true,
|
||||
"episodes": true,
|
||||
"对话": true,
|
||||
"对话记录": true,
|
||||
"raw": true,
|
||||
"episodic": true,
|
||||
"对话记录": true,
|
||||
"raw": true,
|
||||
"episodic": true,
|
||||
}
|
||||
for _, c := range candidates {
|
||||
if !rawCategories[c.Category] {
|
||||
@@ -228,11 +242,37 @@ func (p *RecallPipeline) recallUncached(query, namespace string, topK int, diver
|
||||
}
|
||||
|
||||
// Step 3.5: BM25 keyword scoring — amplify keyword-relevant results
|
||||
// t_631ab13d: 先抓 DB 原始 quality_score(candidates[i].QualityScore),
|
||||
// 因为下面会原地覆盖为 BM25 融合值(0.7*dbQuality + 0.3*kwScore)。
|
||||
// 响应需要展示 DB 原值("响应值==库值"),不能展示被融合改写后的值。
|
||||
dbQuality := make([]float64, len(candidates))
|
||||
if len(candidates) > 0 {
|
||||
for i := range candidates {
|
||||
dbQuality[i] = candidates[i].QualityScore
|
||||
// 可信度判定必须用 **DB 原始列值**:candidates[i].QualityScore 在 IPC 路径下
|
||||
// 是**已融合且会饱和到 1.0** 的值(实测 candidateQS=1.0 / rawQS=0.755),
|
||||
// 拿它去判永远"可信" ⇒ 修复不触发(2026-10-12 实测踩到)。
|
||||
// RawQualityScore 仅在 Rust IPC 路径携带;纯 Go 路径下候选值即 DB 原值。
|
||||
qsForTrust := dbQuality[i]
|
||||
if candidates[i].RawQualityScore != nil {
|
||||
qsForTrust = *candidates[i].RawQualityScore
|
||||
}
|
||||
kwScore := ComputeBM25Score(query, candidates[i].Content)
|
||||
// 融合: vector score (quality) * 0.7 + keyword score * 0.3
|
||||
candidates[i].QualityScore = candidates[i].QualityScore*0.7 + kwScore*0.3
|
||||
if models.IsReliableQuality(qsForTrust) {
|
||||
candidates[i].QualityScore = dbQuality[i]*0.7 + kwScore*0.3
|
||||
} else {
|
||||
// 存量分数不可信(0=未打分 / 0.755=历史常量 bug,见 models.IsReliableQuality)
|
||||
// → 不拿假分数参与排序,权重全部让给关键词分。
|
||||
// 注意这是**中性**处理:既不奖励也不惩罚,只是不采信。
|
||||
if qstrustVerbose {
|
||||
// 默认静音:每次召回会命中上百条,全量打印是生产噪音。
|
||||
// 需要诊断时置环境变量 ZHIYI_QSTRUST_VERBOSE=1。
|
||||
log.Printf("[qstrust] 不可信质量分已剔除 id=%s rawQS=%.6f → 回退纯关键词分 %.4f",
|
||||
candidates[i].ID, qsForTrust, kwScore)
|
||||
}
|
||||
candidates[i].QualityScore = kwScore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -318,20 +358,40 @@ func (p *RecallPipeline) recallUncached(query, namespace string, topK int, diver
|
||||
// 2026-10-09 (P0 修正):λ 已在 Step 3.7 偏置进 reranked[i].Score,选择与展示
|
||||
// 共用同一套分数;此处不再重复施加(否则 legacy 被双重扣分)。
|
||||
base := score*0.7 + c.QualityScore*0.3
|
||||
// 2026-10-11 (t_631ab13d):把库里的 quality_score / v_value 直通给调用方。
|
||||
// 展示 DB 原值("响应值==库值"判据):优先用 RawQualityScore(LanceDB 原始列,
|
||||
// 仅 Rust IPC 路径携带,json:"-" 不落库);否则退回 DB 捕获值 dbQuality[idx]
|
||||
// (纯 Go 后端候选的 QualityScore 即 DB 原值,未被融合改写)。
|
||||
// 排序用 c.QualityScore(IPC=融合值 / 纯Go=BM25融合值)不变,只补显示缺口。
|
||||
vv := 0.0
|
||||
if VValueProvider != nil {
|
||||
vv = VValueProvider(c.ID)
|
||||
}
|
||||
var qs *float64
|
||||
if c.RawQualityScore != nil {
|
||||
qs = c.RawQualityScore // LanceDB 原始列值(IPC 路径)
|
||||
} else if idx >= 0 && idx < len(dbQuality) {
|
||||
v := dbQuality[idx]
|
||||
qs = &v // 纯 Go 后端:DB 捕获值
|
||||
}
|
||||
qsVal := 0.0
|
||||
if qs != nil {
|
||||
qsVal = *qs
|
||||
}
|
||||
results = append(results, models.RecallResult{
|
||||
ID: c.ID,
|
||||
Content: c.Content,
|
||||
Category: c.Category,
|
||||
Score: base,
|
||||
Timestamp: c.CreatedAt.Format(time.RFC3339),
|
||||
// 2026-09-22:带上来源,供全库召回时分辨记忆归属域
|
||||
Namespace: c.Namespace,
|
||||
AgentID: c.AgentID,
|
||||
// 2026-09-26:把记忆出处直通给调用方 —— 蒸馏/审核记忆的 source 形如 "wiki:<笔记路径>"
|
||||
Source: c.Source,
|
||||
// 2026-10-08 (Gap#2):信任等级直通(NULL 已 COALESCE 成 1.0)
|
||||
Confidence: &conf,
|
||||
ConfidenceSource: c.ConfidenceSource,
|
||||
ID: c.ID,
|
||||
Content: c.Content,
|
||||
Category: c.Category,
|
||||
Score: base,
|
||||
Timestamp: c.CreatedAt.Format(time.RFC3339),
|
||||
Namespace: c.Namespace,
|
||||
AgentID: c.AgentID,
|
||||
Source: c.Source,
|
||||
Confidence: &conf,
|
||||
ConfidenceSource: c.ConfidenceSource,
|
||||
QualityScore: qsVal,
|
||||
QualityScoreReliable: models.IsReliableQuality(qsVal),
|
||||
VValue: vv,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -364,7 +424,8 @@ func (p *RecallPipeline) recallUncached(query, namespace string, topK int, diver
|
||||
}
|
||||
results = deduped
|
||||
|
||||
// Step 6.5: 写搜索缓存(§2.6 — TTL 1h,下次相同 query+topK+diversity 直接返回)
|
||||
// Step 6.5: 写搜索缓存(§2.6 — t_1c12e000 起 TTL=60s,下次相同 query+topK+diversity
|
||||
// 直接返回;写路径落库后会 Invalidate 本 namespace,陈旧窗口上限=60s 双保险)
|
||||
if SearchCacheInstance != nil {
|
||||
SearchCacheInstance.Set(query, namespace, topK, diversity, results)
|
||||
}
|
||||
@@ -557,10 +618,11 @@ func (p *RecallPipeline) incrementRecallCount(results []models.RecallResult) {
|
||||
now := time.Now().Format(time.RFC3339)
|
||||
for _, r := range results {
|
||||
if r.ID != "" {
|
||||
// t_268b0353(治根):兜底路径同样不写 freshness=verified("被召回"≠"被验证")
|
||||
// GA-P0-2 (t_c6d41ac6):召回命中 → recall_seen_count(recall_count 冻结)
|
||||
_ = p.lancedb.Update("memories", r.ID, map[string]any{
|
||||
"recall_count": map[string]string{"$inc": "1"},
|
||||
"last_recalled_at": now,
|
||||
"freshness": "verified",
|
||||
"recall_seen_count": map[string]string{"$inc": "1"},
|
||||
"last_recalled_at": now,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -570,11 +632,11 @@ func (p *RecallPipeline) incrementRecallCount(results []models.RecallResult) {
|
||||
// 用于 cache hit 路径,防止旧缓存包含的 episodes/对话污染 recall 结果
|
||||
func filterRecallRawCategories(results []models.RecallResult) []models.RecallResult {
|
||||
rawCategories := map[string]bool{
|
||||
"episodes": true,
|
||||
"episodes": true,
|
||||
"对话": true,
|
||||
"对话记录": true,
|
||||
"raw": true,
|
||||
"episodic": true,
|
||||
"对话记录": true,
|
||||
"raw": true,
|
||||
"episodic": true,
|
||||
}
|
||||
filtered := make([]models.RecallResult, 0, len(results))
|
||||
for _, r := range results {
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestRecallSeenVerifiedIsolation — GA-P0-2 (t_c6d41ac6) AC5
|
||||
//
|
||||
// 覆盖「一次召回 vs 一次正反馈」两条路径写不同字段:
|
||||
//
|
||||
// 召回命中(recall 缓冲/Update 的 recall_seen_count)→ 只加 recall_seen_count
|
||||
// 正反馈(IncrementUseful) → 只加 recall_verified_count + useful_count
|
||||
//
|
||||
// 断言用相对增量(绝对值会被其它路径的计数干扰,见 P0-1 TestSplitNotUsefulFieldIsolation):
|
||||
// 每次调用只 +1 到各自字段,其它字段(含冻结的 recall_count)必须原样不变。
|
||||
func TestRecallSeenVerifiedIsolation(t *testing.T) {
|
||||
mlc := NewMemLanceClient(nil)
|
||||
mlc.mu.Lock()
|
||||
mlc.memories["mem_recall_split_1"] = &memEntry{
|
||||
ID: "mem_recall_split_1", RecallCount: 5, RecallSeenCount: 3, RecallVerifiedCount: 1,
|
||||
}
|
||||
mlc.mu.Unlock()
|
||||
|
||||
e := mlc.memories["mem_recall_split_1"]
|
||||
|
||||
// 路径1:一次召回命中 → 只加 recall_seen_count;recall_count(冻结) / verified / useful 不动
|
||||
if err := mlc.Update("memories", "mem_recall_split_1", map[string]any{
|
||||
"recall_seen_count": map[string]string{"$inc": "1"},
|
||||
}); err != nil {
|
||||
t.Fatalf("Update recall_seen_count err: %v", err)
|
||||
}
|
||||
if e.RecallSeenCount != 4 {
|
||||
t.Fatalf("recall_seen_count 相对增量 +1,want 4 got %d", e.RecallSeenCount)
|
||||
}
|
||||
if e.RecallCount != 5 {
|
||||
t.Fatalf("召回路径不得污染冻结的 recall_count,want 5 got %d", e.RecallCount)
|
||||
}
|
||||
if e.RecallVerifiedCount != 1 {
|
||||
t.Fatalf("召回路径不得污染 recall_verified_count,want 1 got %d", e.RecallVerifiedCount)
|
||||
}
|
||||
if e.UsefulCount != 0 {
|
||||
t.Fatalf("召回路径不得污染 useful_count,want 0 got %d", e.UsefulCount)
|
||||
}
|
||||
|
||||
// 路径2:一次正反馈(用户点「有用」)→ 只加 recall_verified_count + useful_count;
|
||||
// seen / recall_count(冻结) 不动
|
||||
if err := mlc.IncrementUseful("mem_recall_split_1"); err != nil {
|
||||
t.Fatalf("IncrementUseful err: %v", err)
|
||||
}
|
||||
if e.RecallVerifiedCount != 2 {
|
||||
t.Fatalf("recall_verified_count 相对增量 +1,want 2 got %d", e.RecallVerifiedCount)
|
||||
}
|
||||
if e.UsefulCount != 1 {
|
||||
t.Fatalf("useful_count 相对增量 +1,want 1 got %d", e.UsefulCount)
|
||||
}
|
||||
if e.RecallSeenCount != 4 {
|
||||
t.Fatalf("正反馈不得污染 recall_seen_count,want 4 got %d", e.RecallSeenCount)
|
||||
}
|
||||
if e.RecallCount != 5 {
|
||||
t.Fatalf("正反馈不得污染冻结的 recall_count,want 5 got %d", e.RecallCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecallCountFrozen — GA-P0-2 (t_c6d41ac6) AC3
|
||||
// 旧 recall_count 冻结:IncrementUseful 不再 +recall_count(只 +verified),
|
||||
// 保证「被召回 N 次所以永不遗忘」的虚高保命来源被切断。
|
||||
func TestRecallCountFrozen(t *testing.T) {
|
||||
mlc := NewMemLanceClient(nil)
|
||||
mlc.mu.Lock()
|
||||
mlc.memories["mem_frozen_1"] = &memEntry{ID: "mem_frozen_1", RecallCount: 42}
|
||||
mlc.mu.Unlock()
|
||||
|
||||
e := mlc.memories["mem_frozen_1"]
|
||||
if err := mlc.IncrementUseful("mem_frozen_1"); err != nil {
|
||||
t.Fatalf("IncrementUseful err: %v", err)
|
||||
}
|
||||
if e.RecallCount != 42 {
|
||||
t.Fatalf("recall_count 冻结,正反馈不得增长:want 42 got %d", e.RecallCount)
|
||||
}
|
||||
if e.RecallVerifiedCount != 1 {
|
||||
t.Fatalf("recall_verified_count 应 +1:want 1 got %d", e.RecallVerifiedCount)
|
||||
}
|
||||
}
|
||||
@@ -208,10 +208,14 @@ func (b *RecallWriteBuffer) Flush() (int, error) {
|
||||
}
|
||||
rows = 0
|
||||
for _, it := range batch {
|
||||
// t_268b0353(2026-10-11 治根):召回只更新 recall_count / last_recalled_at,
|
||||
// 不再写 freshness="verified"。freshness=verified 的语义是「经过校验/用户
|
||||
// 反馈」;"被召回" ≠ "被验证"。此前每次召回都盖 verified → 越常被召回的
|
||||
// 旧快照越"可信",时效判断全被污染(过期快照被当现状的机制根因之一)。
|
||||
// GA-P0-2 (t_c6d41ac6):召回命中写 recall_seen_count;不再写 recall_count(冻结)。
|
||||
if uerr := b.ldb.Update("memories", it.ID, map[string]any{
|
||||
"recall_count": map[string]string{"$inc": "1"},
|
||||
"last_recalled_at": ts,
|
||||
"freshness": "verified",
|
||||
"recall_seen_count": map[string]string{"$inc": "1"},
|
||||
"last_recalled_at": ts,
|
||||
}); uerr != nil {
|
||||
log.Printf("[recall-buffer] 逐条 Update 失败 id=%s: %v", it.ID, uerr)
|
||||
continue
|
||||
@@ -226,7 +230,8 @@ func (b *RecallWriteBuffer) Flush() (int, error) {
|
||||
_local.mu.Lock()
|
||||
for _, it := range batch {
|
||||
if m, ok := _local.memories[it.ID]; ok {
|
||||
m.RecallCount += it.Delta
|
||||
// GA-P0-2 (t_c6d41ac6):召回命中 → 进程内缓存同步到 recall_seen_count
|
||||
m.RecallSeenCount += it.Delta
|
||||
if !lastAt.IsZero() {
|
||||
m.LastRecalledAt = lastAt
|
||||
}
|
||||
|
||||
@@ -46,6 +46,13 @@ func (f *fakeLDB) Backup(path string) error { ret
|
||||
func (f *fakeLDB) GetAuditLog(limit int) ([]map[string]interface{}, error) { return nil, nil }
|
||||
func (f *fakeLDB) IncrementUseful(id string) error { return nil }
|
||||
func (f *fakeLDB) IncrementNotUseful(id string) error { return nil }
|
||||
|
||||
// GA-P0-1 (t_718d8707):拆分字段
|
||||
func (f *fakeLDB) IncrementHumanNotUseful(id string) error { return nil }
|
||||
func (f *fakeLDB) IncrementDedupDownweight(id string) error { return nil }
|
||||
func (f *fakeLDB) AppendHitEvent(memoryID, outcome, reason string) error { return nil }
|
||||
func (f *fakeLDB) CountHitEvents(memoryID, outcome string, since time.Time) (int, error) { return 0, nil }
|
||||
func (f *fakeLDB) GetHitEvents(memoryID string, limit int) ([]models.HitEvent, error) { return nil, nil }
|
||||
func (f *fakeLDB) UpdateMemoryContent(id, newContent, source string) error {
|
||||
return nil
|
||||
}
|
||||
@@ -174,12 +181,16 @@ func TestRecallWriteBufferTickerFlush(t *testing.T) {
|
||||
|
||||
b.Record([]string{"mem_tick"})
|
||||
|
||||
// ⚠️ 必须等「断言的那个可观测量」而不是中间态:
|
||||
// Flush() 是「先清 b.pending、再调 BatchUpdate」,若等 pending 归零就断言,
|
||||
// 会抢在 batchCalls++ 之前 ⇒ 偶发假红(实测 5 次里 1 次;同期日志里 flush 其实已发生)。
|
||||
// 隔壁 TestRecallWriteBufferThresholdFlush 用的就是这个正确写法。
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
b.mu.Lock()
|
||||
idle := len(b.pending) == 0
|
||||
b.mu.Unlock()
|
||||
if idle {
|
||||
fake.mu.Lock()
|
||||
done := fake.batchCalls > 0
|
||||
fake.mu.Unlock()
|
||||
if done {
|
||||
break
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user