feat(memory): T1 灰区模型判定 —— D3 入库前审查补上 LLM 那一半
主人 2026-10-07 裁定「可以做 T1」。此前 D3 只有纯规则 T0,凡灰区(相似但 互不包含)一律保守 create,真机上「同一事实换措辞」类重复结构性治不住 (清完 92 条字面级重复后,active 里含「喵喵七号」的仍有 101 条)。 - 插入点:T0 判定内核只多吐一份结构化 Gray(惰性收集,恒零模型调用); T1 挂在异步审查层 Review,同步入库闸不碰它 ⇒ 写入路径零模型调用是 **数据流结构**而不是承诺 - 只许回 merge / modify / keep + 一句理由;坏 JSON、非法枚举、编号越界 一律回落 create(绝不猜、绝不挑最近的一条) - 禁产新内容:模型只给「动作 + 候选编号 + 一句理由」,正文一律取自库里 既有的两行 ⇒ T1 结构性地不可能把幻觉写进记忆库 - 不越硬门与红线:灰区已滤掉跨角色/作用域/项目/只读来源与时效/数值/冲突/ 低置信;「规则否决一票否决」不可走样,T1 不提供翻案入口 - 有界:每次审查至多一次调用(灰区打包,上限 8 条),子超时 8s 且不超过 审查总预算;失败只改 reason、决定恒 create、零 UPDATE - 开关 t1Judge(放 $DATA_DIR/config/memory-tidy.json,零新 env 键),默认开; 模型出口用闭包晚绑定既有模型栈(零新客户端、零新依赖) - 留痕 reason 结构化(t1_merge / t1_modify / t1_keep / t1_reply_invalid / t1_unavailable),rule_version d3.1 → d3.2;模型的自由文本理由一律丢弃 - preinsert_config.go 为纯搬移(为守 500 行上限,无新逻辑)
This commit is contained in:
1 parent
5b8c7a3ef3
commit
1b4d0a552f
11 files changed
+1233
-91
No files matched your search
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/embedding"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/history"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/memory"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
|
||||
)
|
||||
|
||||
// 本文件锁定 D3「入库前审查」(design/23 §15.1 D3)在**组合根**上的三件事:
|
||||
@@ -60,6 +62,12 @@ func TestMemoryReviewIsWired(t *testing.T) {
|
||||
// 的 `{"ingestGate": false}`(**零新 env 键**,`.env` 已冻结)。
|
||||
"a.memoryIngestGateOff()",
|
||||
"DisableIngestGate: gateOff",
|
||||
// T1 灰区模型判定(design/23 §15.1 D3-2):开关同样来自那个文件
|
||||
// (`{"t1Judge": false}`),模型出口**在调用时才解析**(本服务早于
|
||||
// 模型栈装配,与 a.gw 的晚组装同一手法)。
|
||||
"a.memoryT1JudgeOff()",
|
||||
"DisableT1: t1Off",
|
||||
"Judge: a.memoryT1Judge()",
|
||||
},
|
||||
"main.go": {
|
||||
"a.memoryReview.Start(ctx)",
|
||||
@@ -131,6 +139,98 @@ func TestMemoryIngestGate_ReadsDataDirConfig(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestMemoryT1Judge_ReadsDataDirConfig 钉住 **T1 灰区模型判定**的开关确实来自
|
||||
// `$DATA_DIR/config/memory-tidy.json` 的 `t1Judge`(它没有 env 键:`.env` 已冻结)
|
||||
// —— T1 是一处**会花掉模型额度**的动作,「一行配置关掉」必须真的成立。
|
||||
//
|
||||
// 口径与入库闸同一条:没配 = 开(主人 2026-10-07 要的就是「真的走记忆管理会话」);
|
||||
// 显式 false = 关(回落纯规则、零模型调用);坏文件 = 回落代码默认(开)并记
|
||||
// WARN,**绝不静默把一处防护关掉**。
|
||||
func TestMemoryT1Judge_ReadsDataDirConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
empty := &app{cfg: &config.Config{DataDir: t.TempDir()}, log: tidyTestLog()}
|
||||
if empty.memoryT1JudgeOff() {
|
||||
t.Fatal("没配过时 T1 应当保持代码默认(开)")
|
||||
}
|
||||
|
||||
dataDir := t.TempDir()
|
||||
dir := filepath.Join(dataDir, "config")
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
path := filepath.Join(dir, "memory-tidy.json")
|
||||
// 只写一个上限键:它不许把 T1 静默关掉(指针字段的存在理由)。
|
||||
if err := os.WriteFile(path, []byte(`{"autoApplyPerRound": 5}`), 0o600); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
partial := &app{cfg: &config.Config{DataDir: dataDir}, log: tidyTestLog()}
|
||||
if partial.memoryT1JudgeOff() {
|
||||
t.Fatal("只写了别的键时 T1 被静默关掉了(必须保持代码默认的「开」)")
|
||||
}
|
||||
|
||||
if err := os.WriteFile(path, []byte(`{"t1Judge": false}`), 0o600); err != nil {
|
||||
t.Fatalf("write: %v", err)
|
||||
}
|
||||
off := &app{cfg: &config.Config{DataDir: dataDir}, log: tidyTestLog()}
|
||||
if !off.memoryT1JudgeOff() {
|
||||
t.Fatal("t1Judge: false 没被读到(T1 没有一键关掉的路径)")
|
||||
}
|
||||
|
||||
if err := os.WriteFile(path, []byte(`{"t1Judge": "nope"}`), 0o600); err != nil {
|
||||
t.Fatalf("write broken: %v", err)
|
||||
}
|
||||
broken := &app{cfg: &config.Config{DataDir: dataDir}, log: tidyTestLog()}
|
||||
if broken.memoryT1JudgeOff() {
|
||||
t.Fatal("坏配置把 T1 静默关掉了:必须回落代码默认(开)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMemoryT1Judge_UsesTheWiredModelStack 钉住 T1 的模型出口**复用既有模型栈**
|
||||
// (不新造 LLM 客户端),且返回一个闭包而不是当场取值 —— 本服务在 wireMemory
|
||||
// 阶段装配,而 a.modelAdapter 要等更晚的 wireRuntime 才建好。
|
||||
func TestMemoryT1Judge_UsesTheWiredModelStack(t *testing.T) {
|
||||
t.Parallel()
|
||||
a := &app{log: tidyTestLog()}
|
||||
judge := a.memoryT1Judge()
|
||||
if judge == nil {
|
||||
t.Fatal("memoryT1Judge 返回了 nil")
|
||||
}
|
||||
// 模型栈还没建好:必须 fail-safe(返回错误),绝不 panic、绝不假装成功。
|
||||
if _, err := judge(context.Background(), model.Request{}, model.Options{}); !errors.Is(err, errT1ModelNotReady) {
|
||||
t.Fatalf("模型栈未就绪时 err = %v, want %v", err, errT1ModelNotReady)
|
||||
}
|
||||
// 接上模型栈后,同一个闭包就能发请求(晚解析的证据)。
|
||||
stub := &reviewAdapterStub{reply: "{}"}
|
||||
a.modelAdapter = stub
|
||||
if _, err := judge(context.Background(), model.Request{Purpose: "memory_t1_judge"}, model.Options{}); err != nil {
|
||||
t.Fatalf("接上模型栈后调用失败:%v", err)
|
||||
}
|
||||
if stub.calls != 1 {
|
||||
t.Fatalf("模型栈被调用 %d 次, want 1", stub.calls)
|
||||
}
|
||||
}
|
||||
|
||||
// reviewAdapterStub 是最小的 model.Adapter 桩(只为验「闭包解析到既有的那个
|
||||
// 模型栈」这件事,不参与判定正确性)。
|
||||
type reviewAdapterStub struct {
|
||||
calls int
|
||||
reply string
|
||||
}
|
||||
|
||||
func (s *reviewAdapterStub) Name() string { return "stub" }
|
||||
|
||||
func (s *reviewAdapterStub) Chat(context.Context, model.Request, model.Options) (model.Response, error) {
|
||||
s.calls++
|
||||
return model.Response{Content: s.reply}, nil
|
||||
}
|
||||
|
||||
func (s *reviewAdapterStub) ChatStream(
|
||||
ctx context.Context, req model.Request, onEvent func(model.StreamEvent),
|
||||
) (model.Response, error) {
|
||||
return s.Chat(ctx, req, model.Options{})
|
||||
}
|
||||
|
||||
// TestMemoryReviewSessionIsNotConversationLane 与整理同一条口径:审查会话不落在
|
||||
// 主人的对话车道("web" / "web:<uuid>")上,因此不改她的心情、不进主会话泳道。
|
||||
func TestMemoryReviewSessionIsNotConversationLane(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user