Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8df9d0d5db |
@@ -16,7 +16,7 @@ func standingStore() *storage.StandingStore { return storage.StandingStoreInstan
|
||||
|
||||
// HandleStandingAnswers GET/POST /api/v1/standing-answers
|
||||
// GET ?id=<question_id> → 直读一条;无 id → 列全部
|
||||
// POST {"question_id","question","answer","op":{...},"source_memories":[]} → 写入/改写
|
||||
// POST {"question_id","question","answer","source_memories":[]} → 写入/改写
|
||||
func HandleStandingAnswers(w http.ResponseWriter, r *http.Request) {
|
||||
s := standingStore()
|
||||
if s == nil {
|
||||
@@ -47,11 +47,10 @@ func HandleStandingAnswers(w http.ResponseWriter, r *http.Request) {
|
||||
respond(w, http.StatusOK, a)
|
||||
case http.MethodPost:
|
||||
var in struct {
|
||||
QuestionID string `json:"question_id"`
|
||||
Question string `json:"question"`
|
||||
Answer string `json:"answer"`
|
||||
Op *storage.OpCard `json:"op,omitempty"`
|
||||
SourceMemories []string `json:"source_memories"`
|
||||
QuestionID string `json:"question_id"`
|
||||
Question string `json:"question"`
|
||||
Answer string `json:"answer"`
|
||||
SourceMemories []string `json:"source_memories"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
||||
respondError(w, http.StatusBadRequest, "bad json: "+err.Error())
|
||||
@@ -61,7 +60,7 @@ func HandleStandingAnswers(w http.ResponseWriter, r *http.Request) {
|
||||
respondError(w, http.StatusBadRequest, "question_id and answer required")
|
||||
return
|
||||
}
|
||||
a, err := s.Upsert(in.QuestionID, in.Question, in.Answer, in.SourceMemories, in.Op)
|
||||
a, err := s.Upsert(in.QuestionID, in.Question, in.Answer, in.SourceMemories)
|
||||
if err != nil {
|
||||
respondError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
// t_132cfa76 — 自动反馈样本账(AC5 判读门槛 + AC3 负样本消费侧入账)
|
||||
//
|
||||
// 背景:recall_usefulness_rate 长期只有 ~632 样本人工/自动反馈,覆盖率 1.36%,
|
||||
// 排序几乎没被校准过。本卡确立三条**机械可判**(零 LLM 自评、零人工点击)的
|
||||
// 自动样本来源,并把「多少样本才许判读」写死:
|
||||
//
|
||||
// ────────────────────────────────────────────────────────────
|
||||
// 🔴 判读门槛(牧尘 2026-11-10 卡 t_132cfa76 AC5 拍板):
|
||||
// rail_usefulness_rate(= recall_usefulness_rate)**样本 ≥ 5000 才判读**。
|
||||
// 在此之前该指标只能看趋势方向,不得作为排序改动 A/B 的裁决依据。
|
||||
// 判读时必须连同 Wilson 95% 区间一起看(区间宽度 > 效应量 ⇒ 无结论)。
|
||||
// 机械执行:metrics 输出 recall_usefulness_judgeable = samples ≥ 5000。
|
||||
// ────────────────────────────────────────────────────────────
|
||||
//
|
||||
// 自动信号定义(判定代码回放见卡交付 replay_auto_labels.py,7 天回放数据 out/):
|
||||
//
|
||||
// 正样本(已有链路,不新增写入点):
|
||||
// P1 downstream_use —— usage_track.go 判「被召回记忆的 ≥8 rune 独特内容逐字
|
||||
// 出现在后续写入」→ AppendHitEvent(pos)。回放 7d 命中 2043 条唯一记忆。
|
||||
// P2 inject_pass —— 插件 _auto_feedback_for_reply(token+语义双闸)→
|
||||
// /feedback/useful(source=auto)。回放 7d 唯一 105 条。
|
||||
//
|
||||
// 负样本(本卡新增消费侧自动上报,堵住「插件永不标 not_useful」的空白):
|
||||
// N1 expired_unconsumed —— usage_recall 行 consumed=2(召回后 48h 窗口内
|
||||
// 无任何下游逐字引用 = 过期未消费)。由消费扫描(CheckCommittedForUse 的
|
||||
// sweep 分支,与写侧同节拍、零常驻进程)逐条上报。
|
||||
// N2 injected_not_used —— 插件侧:注入且双闸 passes=False 且同 session 后续
|
||||
// 不再注入该 mid(回放 7d 唯一 268 条)。
|
||||
// N3 repeat_replaced —— 同 session 归一化 query 重复且该 mid 从 top-k 掉出
|
||||
// (回放 7d 唯一 43 条)。
|
||||
//
|
||||
// 可信性依据(AC4,100 例独立源交叉复跑,见 out/neg_error_rate.json):
|
||||
// 负样本判定错误率 6%(6/100,判据=同 7d 他处存在 passes=True 引用或
|
||||
// consumed=1 行 ⇒ 与负标签矛盾)< 10% 门槛。
|
||||
// ⚠️ 已知语义边界:N1-N3 是「该查询/该回合未被使用」的**上下文负样本**,
|
||||
// 不是「这条记忆全局无用」——聚合排序特征时必须按 (query_pair, mem) 计,
|
||||
// 不得把单条记忆的负样本直接当全局质量降权(hit_event.go 排除项铁律同源)。
|
||||
// 因此本文件只把自动负样本计入**观测指标** auto_*,不接进 RecordFeedback
|
||||
// (人评口径 useful_count/not_useful_count 保持纯净,人机可辨)。
|
||||
package selfoptimize
|
||||
|
||||
import "math"
|
||||
|
||||
// 自动反馈样本账:与人工 UsefulCount/NotUsefulCount 分开计数、分开判读。
|
||||
//
|
||||
// 持久化走 dashboardRedisKey / 本地 JSON 双通道,字段
|
||||
// auto_feedback_positive / auto_feedback_negative(与 useful_count 同款语义:
|
||||
// 单调累加器,跨重启存活;不参与跨天归零)。
|
||||
//
|
||||
// 门槛常量给审计/面板脚本 import 用;改门槛必须同步改本注释 + 卡 t_132cfa76。
|
||||
const (
|
||||
// UsefulnessReadGate —— rail_usefulness_rate 判读门槛(AC5 写死)。
|
||||
UsefulnessReadGate = 5000
|
||||
|
||||
// AutoNegSource* —— auto_feedback_negative 的机械来源枚举(审计分桶用)。
|
||||
AutoNegSourceExpiredUnconsumed = "expired_unconsumed" // N1
|
||||
AutoNegSourceInjectNotUsed = "inject_not_used" // N2
|
||||
AutoNegSourceRepeatReplaced = "repeat_replaced" // N3
|
||||
)
|
||||
|
||||
// RecordAutoFeedback 自动信号入账(useful=true 仅 downstream_use / inject_pass;
|
||||
// useful=false 仅 N1/N3 两类机械负样本)。
|
||||
//
|
||||
// 与 RecordFeedback 的差异:
|
||||
// - 不写 useful_count/not_useful_count(人评口径不掺机器信号)
|
||||
// - 同步追加 hit_events(pos→downstream_use;neg→replacement),
|
||||
// 由调用方负责(routes/feedback.go 已对 useful 做;not-useful auto 分支新增)
|
||||
func (d *Dashboard) RecordAutoFeedback(useful bool) {
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
if useful {
|
||||
d.AutoFeedbackPositive++
|
||||
} else {
|
||||
d.AutoFeedbackNegative++
|
||||
}
|
||||
d.persist()
|
||||
}
|
||||
|
||||
// WilsonInterval95 —— Wilson score 区间(95%,z=1.96)。
|
||||
// 任何对外报 recall_usefulness_rate / auto_usefulness_rate 的地方必须配它输出。
|
||||
func WilsonInterval95(pos, n int) (lo, hi float64) {
|
||||
if n <= 0 {
|
||||
return 0, 0
|
||||
}
|
||||
const z = 1.96
|
||||
p := float64(pos) / float64(n)
|
||||
zz := z * z
|
||||
denom := 1 + zz/float64(n)
|
||||
centre := (p + zz/(2*float64(n))) / denom
|
||||
half := z * math.Sqrt(p*(1-p)/float64(n)+zz/(4*float64(n*n))) / denom
|
||||
lo, hi = centre-half, centre+half
|
||||
if lo < 0 {
|
||||
lo = 0
|
||||
}
|
||||
if hi > 1 {
|
||||
hi = 1
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package selfoptimize
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// t_132cfa76 AC5 —— 判读门槛 + Wilson 区间的回归测试。
|
||||
|
||||
func TestUsefulnessReadGateConst(t *testing.T) {
|
||||
if UsefulnessReadGate != 5000 {
|
||||
t.Fatalf("UsefulnessReadGate = %d, want 5000 (AC5 写死,改需同步卡 t_132cfa76)", UsefulnessReadGate)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWilsonInterval95(t *testing.T) {
|
||||
// 已知锚点:n=100, pos=30 → 30% 的 Wilson 95% ≈ (0.2181, 0.3978)
|
||||
lo, hi := WilsonInterval95(30, 100)
|
||||
if math.Abs(lo-0.2181) > 0.005 || math.Abs(hi-0.3978) > 0.005 {
|
||||
t.Fatalf("Wilson(30,100) = (%.4f, %.4f), want ~(0.2181, 0.3978)", lo, hi)
|
||||
}
|
||||
// n=0 → (0,0)
|
||||
lo, hi = WilsonInterval95(0, 0)
|
||||
if lo != 0 || hi != 0 {
|
||||
t.Fatalf("Wilson(0,0) = (%.4f, %.4f), want (0,0)", lo, hi)
|
||||
}
|
||||
// 全正 → 上界 1
|
||||
lo, hi = WilsonInterval95(100, 100)
|
||||
if hi > 1+1e-9 || lo < 0.96 {
|
||||
t.Fatalf("Wilson(100,100) = (%.4f, %.4f), 上界应≈1, 下界应>0.96", lo, hi)
|
||||
}
|
||||
// 全负 → 下界 0
|
||||
lo, hi = WilsonInterval95(0, 100)
|
||||
if lo < -1e-9 || hi > 0.04 {
|
||||
t.Fatalf("Wilson(0,100) = (%.4f, %.4f), 上界应<0.04", lo, hi)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecordAutoFeedbackSeparateFromHuman(t *testing.T) {
|
||||
// 自动反馈账与人工账必须分账:RecordAutoFeedback 不碰 useful/not_useful。
|
||||
Dash.mu.Lock()
|
||||
Dash.UsefulCount, Dash.NotUsefulCount = 5, 3
|
||||
Dash.AutoFeedbackPositive, Dash.AutoFeedbackNegative = 0, 0
|
||||
Dash.mu.Unlock()
|
||||
before := Dash.UsefulCount + Dash.NotUsefulCount
|
||||
Dash.RecordAutoFeedback(true)
|
||||
Dash.RecordAutoFeedback(false)
|
||||
Dash.RecordAutoFeedback(false)
|
||||
if Dash.UsefulCount+Dash.NotUsefulCount != before {
|
||||
t.Fatalf("RecordAutoFeedback 污染了人工账: useful+not=%d (应仍=%d)",
|
||||
Dash.UsefulCount+Dash.NotUsefulCount, before)
|
||||
}
|
||||
if Dash.AutoFeedbackPositive != 1 || Dash.AutoFeedbackNegative != 2 {
|
||||
t.Fatalf("auto 账不对: pos=%d neg=%d (want 1/2)", Dash.AutoFeedbackPositive, Dash.AutoFeedbackNegative)
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,9 @@ type dashboardState struct {
|
||||
DistillSuccessCount int `json:"distill_success_count"`
|
||||
AutoResolvedConflicts int `json:"auto_resolved_conflicts"`
|
||||
TotalConflicts int `json:"total_conflicts"`
|
||||
// t_132cfa76:自动反馈样本账持久化字段(单调累加器,跨重启存活)。
|
||||
AutoFeedbackPositive int `json:"auto_feedback_positive"`
|
||||
AutoFeedbackNegative int `json:"auto_feedback_negative"`
|
||||
// t_e5997ca4:计数器版本标记(见 dashboardSchemaV2 注释)。
|
||||
SchemaVersion string `json:"dashboard_schema_version"`
|
||||
}
|
||||
@@ -114,6 +117,8 @@ func loadDashboardLocal() error {
|
||||
Dash.DistillSuccessCount = st.DistillSuccessCount
|
||||
Dash.AutoResolvedConflicts = st.AutoResolvedConflicts
|
||||
Dash.TotalConflicts = st.TotalConflicts
|
||||
Dash.AutoFeedbackPositive = st.AutoFeedbackPositive
|
||||
Dash.AutoFeedbackNegative = st.AutoFeedbackNegative
|
||||
// t_e5997ca4 (b):版本化一次性重置。本地 JSON 无 v2 标记(旧版/缺失)⇒ 丢弃被
|
||||
// 事件式自增污染的 gap 双计数器,回写标记;v2 标记在位 ⇒ 保真(新口径累计值)。
|
||||
if st.SchemaVersion != dashboardSchemaV2 {
|
||||
@@ -144,6 +149,8 @@ func (d *Dashboard) persistLocalLocked() {
|
||||
DistillSuccessCount: d.DistillSuccessCount,
|
||||
AutoResolvedConflicts: d.AutoResolvedConflicts,
|
||||
TotalConflicts: d.TotalConflicts,
|
||||
AutoFeedbackPositive: d.AutoFeedbackPositive,
|
||||
AutoFeedbackNegative: d.AutoFeedbackNegative,
|
||||
SchemaVersion: dashboardSchemaV2, // t_e5997ca4:v2 常驻回写(新安装/旧版升级都自动打标)
|
||||
}
|
||||
b, err := json.Marshal(st)
|
||||
@@ -179,6 +186,8 @@ func (d *Dashboard) persist() {
|
||||
_ = redisPersistence.HSet(dashboardRedisKey, "distill_success_count", strconv.Itoa(d.DistillSuccessCount))
|
||||
_ = redisPersistence.HSet(dashboardRedisKey, "auto_resolved_conflicts", strconv.Itoa(d.AutoResolvedConflicts))
|
||||
_ = redisPersistence.HSet(dashboardRedisKey, "total_conflicts", strconv.Itoa(d.TotalConflicts))
|
||||
_ = redisPersistence.HSet(dashboardRedisKey, "auto_feedback_positive", strconv.Itoa(d.AutoFeedbackPositive))
|
||||
_ = redisPersistence.HSet(dashboardRedisKey, "auto_feedback_negative", strconv.Itoa(d.AutoFeedbackNegative))
|
||||
_ = redisPersistence.HSet(dashboardRedisKey, "dashboard_schema_version", dashboardSchemaV2) // t_e5997ca4:v2 标记常驻回写
|
||||
_ = redisPersistence.Expire(dashboardRedisKey, 90*24*time.Hour) // 90 天 TTL
|
||||
}
|
||||
@@ -237,6 +246,12 @@ func loadDashboard() error {
|
||||
if v, err := strconv.Atoi(data["total_conflicts"]); err == nil {
|
||||
Dash.TotalConflicts = v
|
||||
}
|
||||
if v, err := strconv.Atoi(data["auto_feedback_positive"]); err == nil {
|
||||
Dash.AutoFeedbackPositive = v
|
||||
}
|
||||
if v, err := strconv.Atoi(data["auto_feedback_negative"]); err == nil {
|
||||
Dash.AutoFeedbackNegative = v
|
||||
}
|
||||
// t_e5997ca4 (b):版本化一次性重置(Redis 路径)。HASH 无 v2 标记(旧版遗留/缺失)⇒
|
||||
// 丢弃被事件式自增污染的 gap 双计数器(线上 9 vs 2 实例),回写标记;v2 在位 ⇒ 保真。
|
||||
if data["dashboard_schema_version"] != dashboardSchemaV2 {
|
||||
@@ -268,6 +283,10 @@ type Dashboard struct {
|
||||
DistillSuccessCount int `json:"distill_success_count"`
|
||||
AutoResolvedConflicts int `json:"auto_resolved_conflicts"`
|
||||
TotalConflicts int `json:"total_conflicts"`
|
||||
// t_132cfa76:自动反馈样本账(机械信号,与人工 useful/not_useful 分账)。
|
||||
// 判读门槛见 auto_feedback_calibration.go UsefulnessReadGate。
|
||||
AutoFeedbackPositive int `json:"auto_feedback_positive"`
|
||||
AutoFeedbackNegative int `json:"auto_feedback_negative"`
|
||||
}
|
||||
|
||||
var Dash = &Dashboard{}
|
||||
@@ -392,6 +411,21 @@ func (d *Dashboard) Metrics() map[string]float64 {
|
||||
"recall_usefulness_positive": float64(d.UsefulCount),
|
||||
"recall_usefulness_valid": usefulValid,
|
||||
|
||||
// t_132cfa76 AC5 —— 自动反馈样本账 + 判读门槛(写死,改需同步卡)。
|
||||
// 🔴 rail_usefulness_rate 样本 ≥ UsefulnessReadGate(5000) 才判读:
|
||||
// recall_usefulness_judgeable = 1 才允许用于排序裁决;=0 时只看方向,
|
||||
// 且判读必须配 Wilson 区间(recall_usefulness_wilson_lo/hi)一起看。
|
||||
"recall_usefulness_judgeable": func() float64 {
|
||||
if usefulSamples >= UsefulnessReadGate {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}(),
|
||||
"recall_usefulness_read_gate": float64(UsefulnessReadGate),
|
||||
"auto_feedback_positive": float64(d.AutoFeedbackPositive),
|
||||
"auto_feedback_negative": float64(d.AutoFeedbackNegative),
|
||||
"auto_feedback_samples": float64(d.AutoFeedbackPositive + d.AutoFeedbackNegative),
|
||||
|
||||
"gap_closure_rate": gapVal,
|
||||
"gap_closure_samples": float64(d.TotalGaps),
|
||||
"gap_closure_valid": gapValid,
|
||||
|
||||
@@ -439,21 +439,6 @@ func (p *RecallPipeline) RecallWithMetaTraced(ctx context.Context, query, namesp
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
// 常设答案精确命中 ⇒ 零检索零 LLM 直接返回,不进入 rerank/LLM:
|
||||
// 省 LLM 配额,且程序性记忆层在 LLM 过载(429)时仍可用。
|
||||
// 命中口径是归一化后整句相等(question + op.trigger 变体),无模糊面,
|
||||
// 不会误吞泛化查询;未命中时走下面的完整召回,行为逐位不变。
|
||||
if sa := standingAnswerFor(query); sa != nil {
|
||||
return []models.RecallResult{{
|
||||
ID: "standing:" + sa.QuestionID,
|
||||
Content: sa.Answer,
|
||||
Category: "standing_answer",
|
||||
Score: 1.0,
|
||||
Timestamp: sa.UpdatedAt.Format(time.RFC3339),
|
||||
Source: "standing_answer",
|
||||
Channel: "standing",
|
||||
}}, RecallMeta{StandingAnswerHit: sa.QuestionID, Status: "ok"}, nil
|
||||
}
|
||||
results, meta, err := p.recallWithMetaInner(ctx, query, namespace, topK, diversity, allowNSFallback, tr)
|
||||
if tr != nil {
|
||||
meta.TraceID = tr.TraceID
|
||||
@@ -465,6 +450,15 @@ func (p *RecallPipeline) RecallWithMetaTraced(ctx context.Context, query, namesp
|
||||
if err != nil {
|
||||
return results, meta, err
|
||||
}
|
||||
if StandingAnswerEnabled() {
|
||||
if sa := LookupStandingAnswer(query); sa != nil {
|
||||
results = PrependStandingAnswer(results, sa, topK)
|
||||
meta.StandingAnswerHit = sa.QuestionID
|
||||
if tr != nil {
|
||||
tr.StandingAnswerHit = sa.QuestionID
|
||||
}
|
||||
}
|
||||
}
|
||||
return results, meta, err
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -35,26 +34,12 @@ type StandingAnswer struct {
|
||||
QuestionID string `json:"question_id"`
|
||||
Question string `json:"question"`
|
||||
Answer string `json:"answer"`
|
||||
// Op(2026-10-28, t_3636b872 程序性记忆层):「怎么做 X」的结构化步骤卡。
|
||||
// 可空。携带 trigger 变体 + 可执行 steps + 前置检查/验证/回滚/风险。
|
||||
// 召回时除精确 Question 匹配外,还按 op.trigger 变体匹配(见 standing_match.go)。
|
||||
Op *OpCard `json:"op,omitempty"` // 可空:存量常设答案无结构卡,行为默认
|
||||
SourceMemories []string `json:"source_memories,omitempty"`
|
||||
Version int `json:"version"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// OpCard 程序性步骤卡(MoA 建议形态,2026-10-28)。
|
||||
type OpCard struct {
|
||||
Trigger []string `json:"trigger,omitempty"` // 用户会怎么问,3-5 个变体
|
||||
Steps []string `json:"steps"` // 可执行命令/操作序列,逐条
|
||||
PreCheck string `json:"pre_check,omitempty"` // 前置检查:怎么确认该不该做
|
||||
Verify string `json:"verify"` // 做完怎么验证成功
|
||||
Rollback string `json:"rollback,omitempty"` // 怎么回退
|
||||
Risk string `json:"risk,omitempty"` // 风险级别与不可逆点
|
||||
}
|
||||
|
||||
// StandingStore 常设答案存储(SQLite 单文件)。
|
||||
type StandingStore struct {
|
||||
mu sync.RWMutex
|
||||
@@ -79,7 +64,6 @@ func NewStandingStore(path string) (*StandingStore, error) {
|
||||
question_id TEXT PRIMARY KEY,
|
||||
question TEXT NOT NULL,
|
||||
answer TEXT NOT NULL,
|
||||
op TEXT DEFAULT NULL,
|
||||
source_memories TEXT DEFAULT '[]',
|
||||
version INTEGER DEFAULT 1,
|
||||
created_at TEXT NOT NULL,
|
||||
@@ -88,41 +72,9 @@ func NewStandingStore(path string) (*StandingStore, error) {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("init standing_answers: %w", err)
|
||||
}
|
||||
// 2026-10-28 (t_3636b872) 结构步骤卡列:存量库 ALTER 补齐;新库已含。
|
||||
// 幂等:重复执行看到列已存在则跳过(SQLite 无 IF NOT EXISTS for column)。
|
||||
if col, err := hasColumn(db, "standing_answers", "op"); err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("check op column: %w", err)
|
||||
} else if !col {
|
||||
if _, err := db.Exec(`ALTER TABLE standing_answers ADD COLUMN op TEXT DEFAULT NULL`); err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("add op column: %w", err)
|
||||
}
|
||||
}
|
||||
return &StandingStore{db: db, path: path}, nil
|
||||
}
|
||||
|
||||
// hasColumn 判断 SQLite 表是否存在某列(PRAGMA table_info)。
|
||||
func hasColumn(db *sql.DB, table, col string) (bool, error) {
|
||||
rows, err := db.Query(`PRAGMA table_info(` + table + `)`)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var cid, notnull, pk int
|
||||
var name, ctype string
|
||||
var dflt *string
|
||||
if err := rows.Scan(&cid, &name, &ctype, ¬null, &dflt, &pk); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if name == col {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, rows.Err()
|
||||
}
|
||||
|
||||
func dirOf(p string) string {
|
||||
for i := len(p) - 1; i >= 0; i-- {
|
||||
if p[i] == '/' {
|
||||
@@ -137,8 +89,7 @@ func (s *StandingStore) Close() error { return s.db.Close() }
|
||||
func (s *StandingStore) Path() string { return s.path }
|
||||
|
||||
// Upsert 写入/改写一条常设答案。已有则 version+1 并保留 source_memories;新则 version=1。
|
||||
// op 可空:nil 时不改既有 op 列(改答案不丢结构卡);写入保留调用方传入的 op。
|
||||
func (s *StandingStore) Upsert(qid, question, answer string, source []string, op *OpCard) (*StandingAnswer, error) {
|
||||
func (s *StandingStore) Upsert(qid, question, answer string, source []string) (*StandingAnswer, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
now := time.Now()
|
||||
@@ -146,10 +97,6 @@ func (s *StandingStore) Upsert(qid, question, answer string, source []string, op
|
||||
if sm == nil {
|
||||
sm = []byte("[]")
|
||||
}
|
||||
opJSON, _ := json.Marshal(op)
|
||||
if opJSON == nil {
|
||||
opJSON = nil
|
||||
}
|
||||
|
||||
// 查已有
|
||||
var ver int
|
||||
@@ -166,57 +113,39 @@ func (s *StandingStore) Upsert(qid, question, answer string, source []string, op
|
||||
if exists {
|
||||
newVer = ver + 1
|
||||
_, err = s.db.Exec(
|
||||
`UPDATE standing_answers SET answer=?, op=COALESCE(?,op), source_memories=?, version=?, updated_at=? WHERE question_id=?`,
|
||||
answer, opJSON, string(sm), newVer, now.Format(time.RFC3339), qid,
|
||||
`UPDATE standing_answers SET answer=?, source_memories=?, version=?, updated_at=? WHERE question_id=?`,
|
||||
answer, string(sm), newVer, now.Format(time.RFC3339), qid,
|
||||
)
|
||||
} else {
|
||||
newVer = 1
|
||||
created = now.Format(time.RFC3339)
|
||||
_, err = s.db.Exec(
|
||||
`INSERT INTO standing_answers(question_id,question,answer,op,source_memories,version,created_at,updated_at)
|
||||
VALUES(?,?,?,?,?,?,?,?)`,
|
||||
qid, question, answer, opJSON, string(sm), newVer, created, now.Format(time.RFC3339),
|
||||
`INSERT INTO standing_answers(question_id,question,answer,source_memories,version,created_at,updated_at)
|
||||
VALUES(?,?,?,?,?,?,?)`,
|
||||
qid, question, answer, string(sm), newVer, created, now.Format(time.RFC3339),
|
||||
)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("upsert: %w", err)
|
||||
}
|
||||
// 写后丢弃缓存,否则新卡/改卡在 standingCacheTTL(30s)内对召回不可见。
|
||||
refreshStandingAnswers()
|
||||
|
||||
return &StandingAnswer{
|
||||
QuestionID: qid, Question: question, Answer: answer, Op: op,
|
||||
QuestionID: qid, Question: question, Answer: answer,
|
||||
SourceMemories: source, Version: newVer, CreatedAt: now, UpdatedAt: now,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// parseOpCard 解析 op 列 JSON;空/坏返回 nil。
|
||||
func parseOpCard(raw sql.NullString) *OpCard {
|
||||
if !raw.Valid {
|
||||
return nil
|
||||
}
|
||||
s := strings.TrimSpace(raw.String)
|
||||
if s == "" || s == "null" {
|
||||
return nil
|
||||
}
|
||||
var oc OpCard
|
||||
if err := json.Unmarshal([]byte(s), &oc); err != nil {
|
||||
return nil
|
||||
}
|
||||
return &oc
|
||||
}
|
||||
|
||||
// Get 直读一条(主键查询,零检索零 LLM)。无则 (nil, nil)。
|
||||
func (s *StandingStore) Get(qid string) (*StandingAnswer, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
var a StandingAnswer
|
||||
var sm, created, updated string
|
||||
var opRaw sql.NullString
|
||||
var sm string
|
||||
var created, updated string
|
||||
err := s.db.QueryRow(
|
||||
`SELECT question_id,question,answer,op,source_memories,version,created_at,updated_at
|
||||
`SELECT question_id,question,answer,source_memories,version,created_at,updated_at
|
||||
FROM standing_answers WHERE question_id=?`, qid,
|
||||
).Scan(&a.QuestionID, &a.Question, &a.Answer, &opRaw, &sm, &a.Version, &created, &updated)
|
||||
).Scan(&a.QuestionID, &a.Question, &a.Answer, &sm, &a.Version, &created, &updated)
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -224,7 +153,6 @@ func (s *StandingStore) Get(qid string) (*StandingAnswer, error) {
|
||||
return nil, fmt.Errorf("standing get: %w", err)
|
||||
}
|
||||
a.SourceMemories = parseSourceEvents(sm)
|
||||
a.Op = parseOpCard(opRaw)
|
||||
a.CreatedAt, _ = time.Parse(time.RFC3339, created)
|
||||
a.UpdatedAt, _ = time.Parse(time.RFC3339, updated)
|
||||
return &a, nil
|
||||
@@ -235,7 +163,7 @@ func (s *StandingStore) List() ([]StandingAnswer, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
rows, err := s.db.Query(
|
||||
`SELECT question_id,question,answer,op,source_memories,version,created_at,updated_at
|
||||
`SELECT question_id,question,answer,source_memories,version,created_at,updated_at
|
||||
FROM standing_answers ORDER BY question_id`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("standing list: %w", err)
|
||||
@@ -245,12 +173,10 @@ func (s *StandingStore) List() ([]StandingAnswer, error) {
|
||||
for rows.Next() {
|
||||
var a StandingAnswer
|
||||
var sm, created, updated string
|
||||
var opRaw sql.NullString
|
||||
if err := rows.Scan(&a.QuestionID, &a.Question, &a.Answer, &opRaw, &sm, &a.Version, &created, &updated); err != nil {
|
||||
if err := rows.Scan(&a.QuestionID, &a.Question, &a.Answer, &sm, &a.Version, &created, &updated); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.SourceMemories = parseSourceEvents(sm)
|
||||
a.Op = parseOpCard(opRaw)
|
||||
a.CreatedAt, _ = time.Parse(time.RFC3339, created)
|
||||
a.UpdatedAt, _ = time.Parse(time.RFC3339, updated)
|
||||
out = append(out, a)
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestStandingUpsertGet(t *testing.T) {
|
||||
}
|
||||
|
||||
// 首次写入 → version=1 + source_memories
|
||||
a, err := s.Upsert("q_mu_pref", "牧尘的偏好是什么", "答案A", []string{"mem_1", "mem_2"}, nil)
|
||||
a, err := s.Upsert("q_mu_pref", "牧尘的偏好是什么", "答案A", []string{"mem_1", "mem_2"})
|
||||
if err != nil {
|
||||
t.Fatalf("Upsert: %v", err)
|
||||
}
|
||||
@@ -39,7 +39,7 @@ func TestStandingUpsertGet(t *testing.T) {
|
||||
}
|
||||
|
||||
// 改写 → version=2
|
||||
a2, err := s.Upsert("q_mu_pref", "牧尘的偏好是什么", "答案B(更新)", []string{"mem_1", "mem_2", "mem_3"}, nil)
|
||||
a2, err := s.Upsert("q_mu_pref", "牧尘的偏好是什么", "答案B(更新)", []string{"mem_1", "mem_2", "mem_3"})
|
||||
if err != nil {
|
||||
t.Fatalf("Upsert2: %v", err)
|
||||
}
|
||||
@@ -62,8 +62,8 @@ func TestStandingUpsertGet(t *testing.T) {
|
||||
// TestStandingListDelete 2026-10-13 t_e9a4d18f P2:列表 + 删除。
|
||||
func TestStandingListDelete(t *testing.T) {
|
||||
s := newTestStandingStore(t)
|
||||
_, _ = s.Upsert("q_sys", "当前系统状态", "答1", nil, nil)
|
||||
_, _ = s.Upsert("q_serv", "哪些服务在跑", "答2", []string{"mem_9"}, nil)
|
||||
_, _ = s.Upsert("q_sys", "当前系统状态", "答1", nil)
|
||||
_, _ = s.Upsert("q_serv", "哪些服务在跑", "答2", []string{"mem_9"})
|
||||
|
||||
all, err := s.List()
|
||||
if err != nil {
|
||||
|
||||
@@ -47,25 +47,6 @@ var (
|
||||
// LookupStandingHitCount 返回本进程启动以来常设答案命中次数(观测用)。
|
||||
func LookupStandingHitCount() int64 { return standingLookupHit.Load() }
|
||||
|
||||
// standingAnswerFor 若常设答案精确通道命中则返回该答案,否则 nil(零检索零 LLM)。
|
||||
// 命中口径 = 归一化后整句相等(question + op.trigger 变体),变体是人工写入的精确问法,
|
||||
// 无模糊面 ⇒ 可安全作为短路的唯一依据。
|
||||
func standingAnswerFor(query string) *StandingAnswer {
|
||||
if !StandingAnswerEnabled() {
|
||||
return nil
|
||||
}
|
||||
return LookupStandingAnswer(query)
|
||||
}
|
||||
|
||||
// refreshStandingAnswers 丢弃常设答案缓存,让下一次召回重新读库。
|
||||
// 写入(Upsert)后必须调用,否则新卡/改卡在 standingCacheTTL 内对召回不可见。
|
||||
func refreshStandingAnswers() {
|
||||
standingCacheMu.Lock()
|
||||
standingCache = nil
|
||||
standingCacheAt = time.Time{}
|
||||
standingCacheMu.Unlock()
|
||||
}
|
||||
|
||||
// normalizeForMatch 归一化:小写 + 去掉所有非字母数字/非 CJK 字符(空格、标点、符号)。
|
||||
func normalizeForMatch(s string) string {
|
||||
var b strings.Builder
|
||||
@@ -151,10 +132,6 @@ func loadStandingAnswers() []StandingAnswer {
|
||||
// Dice≈0.82),把 ComfyUI 的 8188 当成织忆端口置顶。模板句 + 换主语 = 必错。
|
||||
// 结论:常设答案只认**同一句话**;同义问法靠多行变体(question_id 不同)覆盖。
|
||||
// 近似命中(Dice ≥ nearMissLogThreshold)只打日志不返回,用来发现该补哪些变体。
|
||||
//
|
||||
// 2026-10-28 (t_3636b872):若常设答案带 op 结构卡,则其 op.trigger 变体也参与
|
||||
// **归一化后完全相等**匹配——变体是人工/脚本写入的精确问法(含评测集真实提问措辞),
|
||||
// 与 question 同等可信,无谓词注入面 ⇒ 不引入 fuzzy 误命中。card_trigger_match 标出命中路径。
|
||||
func LookupStandingAnswer(query string) *StandingAnswer {
|
||||
q := normalizeForMatch(query)
|
||||
if len([]rune(q)) < 2 {
|
||||
@@ -167,25 +144,16 @@ func LookupStandingAnswer(query string) *StandingAnswer {
|
||||
var near *StandingAnswer
|
||||
nearScore := 0.0
|
||||
for i := range all {
|
||||
a := &all[i]
|
||||
cand := normalizeForMatch(a.Question)
|
||||
if cand != "" {
|
||||
if cand == q {
|
||||
return a
|
||||
}
|
||||
if s := diceSimilarity(q, cand); s > nearScore {
|
||||
nearScore = s
|
||||
near = a
|
||||
}
|
||||
cand := normalizeForMatch(all[i].Question)
|
||||
if cand == "" {
|
||||
continue
|
||||
}
|
||||
// op 结构卡的 trigger 变体:归一化后完全相等也算命中(提示词精确句)。
|
||||
if a.Op != nil {
|
||||
for _, t := range a.Op.Trigger {
|
||||
tn := normalizeForMatch(t)
|
||||
if tn != "" && tn == q {
|
||||
return a
|
||||
}
|
||||
}
|
||||
if cand == q {
|
||||
return &all[i]
|
||||
}
|
||||
if s := diceSimilarity(q, cand); s > nearScore {
|
||||
nearScore = s
|
||||
near = &all[i]
|
||||
}
|
||||
}
|
||||
// 近似但不等:只记日志(便于补变体),绝不返回——错答比不答更贵。
|
||||
|
||||
@@ -63,76 +63,6 @@ func TestLookupStandingAnswerThreshold(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestLookupStandingAnswer_OpCardTrigger 2026-10-28 (t_3636b872):
|
||||
// op 结构卡的 trigger 变体参与归一化精确匹配;命中后返回带 op 的整条,供调用方展示步骤。
|
||||
func TestLookupStandingAnswer_OpCardTrigger(t *testing.T) {
|
||||
seedStanding(t, []StandingAnswer{
|
||||
{
|
||||
QuestionID: "state-db-fix-1",
|
||||
Question: "state.db 损坏了怎么修",
|
||||
Answer: "七步硬顺序:① 停 zhiyid ② 过门禁 db-op-gate ③ 备份 ④ cp 修复脚本用 systemd-run ⑤ 起 ⑥ 验",
|
||||
Op: &OpCard{
|
||||
Trigger: []string{"state.db 损坏怎么修", "state.db 坏了修复步骤是什么"},
|
||||
Steps: []string{"systemctl --user stop zhiyid", "source ~/.hermes/scripts/db-op-gate.sh && require_db_writable"},
|
||||
PreCheck: "pgrep -f zhiyid 确认为 0 且 journalctl 近 3 分钟写流量为 0",
|
||||
Verify: "sqlite3 /var/lib/memoryweave/memoryweave.db 'PRAGMA integrity_check' 返回 ok",
|
||||
Rollback: "停写后从备份 cp 回 memoryweave.db + status.db",
|
||||
Risk: "高:运行期 cp 活库会双写撕裂,必须先停 zhiyid",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// ① trigger 变体命中(原句不命中)
|
||||
if sa := LookupStandingAnswer("state.db 损坏怎么修"); sa == nil || sa.QuestionID != "state-db-fix-1" {
|
||||
t.Fatalf("trigger 变体应命中,实得 %+v", sa)
|
||||
}
|
||||
// ② 命中后整条带回,op 字段非空
|
||||
if sa := LookupStandingAnswer("state.db 损坏怎么修"); sa == nil || sa.Op == nil {
|
||||
t.Fatalf("命中后 op 应为非空,实得 op=%v", sa.Op)
|
||||
}
|
||||
// ③ 多个变体均可命中
|
||||
if sa := LookupStandingAnswer("state.db 坏了修复步骤是什么"); sa == nil || sa.QuestionID != "state-db-fix-1" {
|
||||
t.Fatalf("第二个 trigger 变体也应命中,实得 %+v", sa)
|
||||
}
|
||||
// ④ 原句仍走 Question 匹配(两条路径并存)
|
||||
if sa := LookupStandingAnswer("state.db 损坏了怎么修"); sa == nil || sa.QuestionID != "state-db-fix-1" {
|
||||
t.Fatalf("原句 Question 匹配仍应命中,实得 %+v", sa)
|
||||
}
|
||||
// ⑤ 不在 trigger 列表里的问法仍然不命中(无 fuzzy 引入)
|
||||
if sa := LookupStandingAnswer("state.db 怎么修复"); sa != nil {
|
||||
t.Fatalf("非精确 trigger 不应命中(无 fuzzy),实得 %+v", sa)
|
||||
}
|
||||
}
|
||||
|
||||
// TestLookupStandingAnswer_UpsertInvalidatesCache 写入后新卡必须立即对召回可见,
|
||||
// 不能等 standingCacheTTL(30s)——这是 t_3636b872 阶段 1 的写入-召回一致性判据。
|
||||
func TestLookupStandingAnswer_UpsertInvalidatesCache(t *testing.T) {
|
||||
seedStanding(t, []StandingAnswer{
|
||||
{QuestionID: "old-card", Question: "旧卡的问题", Answer: "旧卡的答案"},
|
||||
})
|
||||
store := StandingStoreInstance
|
||||
// 先查一次把缓存暖起来(此时缓存只含 old-card),模拟"已有 30s 内缓存"的真实场景
|
||||
if sa := LookupStandingAnswer("旧卡的问题"); sa == nil || sa.QuestionID != "old-card" {
|
||||
t.Fatalf("暖缓存失败,实得 %+v", sa)
|
||||
}
|
||||
oc := &OpCard{Trigger: []string{"新问题"}, Steps: []string{"做这个"}, Risk: "LOW"}
|
||||
if _, err := store.Upsert("new-card", "新问题", "新答案", []string{"x"}, oc); err != nil {
|
||||
t.Fatalf("写入新卡失败: %v", err)
|
||||
}
|
||||
// 写后不手工清缓存,新卡必须立即可见 —— 没有 refreshStandingAnswers 时这里会取到 30s 前的旧缓存
|
||||
if sa := LookupStandingAnswer("新问题"); sa == nil || sa.QuestionID != "new-card" {
|
||||
t.Fatalf("写后新卡应立即可见(调用方不应等 30s TTL),实得 %+v", sa)
|
||||
}
|
||||
// trigger 变体同样立即可见
|
||||
if sa := LookupStandingAnswer("新问题变体"); sa != nil {
|
||||
t.Fatalf("不在 trigger 列表的变体不应命中,实得 %+v", sa)
|
||||
}
|
||||
// 旧卡不被新写影响
|
||||
if sa := LookupStandingAnswer("旧卡的问题"); sa == nil || sa.QuestionID != "old-card" {
|
||||
t.Fatalf("旧卡仍应命中,实得 %+v", sa)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrependStandingAnswer(t *testing.T) {
|
||||
sa := &StandingAnswer{QuestionID: "cron-scheduler", Question: "cron 任务的调度者是谁", Answer: "kanban-daemon.service"}
|
||||
in := []models.RecallResult{
|
||||
@@ -164,7 +94,7 @@ func seedStanding(t *testing.T, rows []StandingAnswer) {
|
||||
t.Fatalf("建临时常设答案库失败: %v", err)
|
||||
}
|
||||
for _, r := range rows {
|
||||
if _, err := store.Upsert(r.QuestionID, r.Question, r.Answer, r.SourceMemories, r.Op); err != nil {
|
||||
if _, err := store.Upsert(r.QuestionID, r.Question, r.Answer, nil); err != nil {
|
||||
t.Fatalf("写入常设答案失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,3 @@ a2be088cba59c95901b22d855dea9b6df3bf3a80
|
||||
4341fac4c28a4f2825f7373ea0ec94d07c6452f0
|
||||
b101b464471f6283c438f089e9ec269c8f2b1644
|
||||
01d8f34
|
||||
a8e15c9ceee7978272d57e9dc7bf8f29c1b7970c
|
||||
6952bdb2138bb7b14479f64553fb911b24e9dda4
|
||||
ebe6384b733e52a910cd72aaf7afdb94128c72a1
|
||||
dbdb2ed08e0ba4a93cf43127e0e739eee114b4f6
|
||||
|
||||
Reference in New Issue
Block a user