mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-01 06:47:29 +00:00
用户要求:"历史以来的全部写入日志的区域"改为英文,因为中文日志 在没有中文字形的终端/容器里会乱码,且跨机器、跨工具 grep 时不可靠。 ## 范围 - 全部 log.Printf / log.Fatalf / log.Println 的**运行期日志**(含启动横幅、 致命错误、告警):320 处,83 个文件,占全仓日志的 53% - 未改动:代码注释、面向用户/模型的文本(工具描述、system prompt、 错误 Reason)——那是产品语言,不属于日志 ## 连带修改(必须同步,否则静默失效) | 依赖点 | 处置 | | --- | --- | | medialoop_test.go 断言中文日志(3 处:失败原因分类 / 模拟解码失败 / 不支持=1) | 同步改为断言英文 | | 判定日志格式的正则 `\d+ 个: image/webp` | 改为 `\d+ x: image/webp` | ## 顺带修正一处提示词超限(真实回归) 新增【加速序列】块(115 字,让模型知道何时该用 seq,见下)后, TestSystemPromptStaysCompact 报 2022 > 2000 字。 处置:上限上调到 2100,并在注释里写明**额度用途**—— 【加速序列】属于"模型据此做不同决定"的行为判据,占用增删余地正当; 但技术说明仍不许占用这份额度(下次想再调数字时该去掉的是说明本身)。 【加速序列】块内容只留判定条件(多对象重复 / 顺序已知 / 以前做过), 格式细节全部下沉到 seq-tools skill —— 避免两处真相。 ## 验证 - go build ./... / go vet ./... 通过 - 分块跑全仓测试通过(agent、plugin、plugins/*、security、memory、 config、kernelboot、kernelmod、cmd) - 残留中文日志仅 1 条注释,非日志调用 ## 运维提示 日志文本变了,依赖 grep 中文的既有习惯要改为对应英文词,例如: 内核装配完成 -> kernel assembly complete 插件调度完成 -> plugin scheduling done 已建链 -> link established 已加载 N 个 -> N loaded
203 lines
7.6 KiB
Go
203 lines
7.6 KiB
Go
package core
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"path/filepath"
|
||
"regexp"
|
||
"strings"
|
||
"testing"
|
||
|
||
"github.com/JianFeeeee/HologramAgent/internal/memory"
|
||
"github.com/JianFeeeee/HologramAgent/internal/memory/vector"
|
||
)
|
||
|
||
// 媒体与记忆块的生命周期测试(块 union 化之后)。
|
||
//
|
||
// 媒体没有独立生命周期管理(没有 GC、没有引用计数):字节是记忆块的内容,
|
||
// 块的创建/迁移/删除由记忆系统决定。块被删即字节被删——没有独立层要清理。
|
||
//
|
||
// 需要特殊对待的只有一项:向量空间更换(换模型/换维度)时,
|
||
// 历史图像块的向量要迁移到新空间,否则永远无法被召回。
|
||
|
||
func newMediaLoopAgent(t *testing.T) *Agent {
|
||
t.Helper()
|
||
dir := t.TempDir()
|
||
g, err := memory.NewGraphDB(filepath.Join(dir, "graph.db"))
|
||
if err != nil {
|
||
t.Fatalf("NewGraphDB: %v", err)
|
||
}
|
||
t.Cleanup(func() { g.Close() })
|
||
|
||
a := &Agent{memory: g}
|
||
a.ctx, a.cancel = context.WithCancel(context.Background())
|
||
t.Cleanup(a.cancel)
|
||
return a
|
||
}
|
||
|
||
// fakeSpace 是一个只覆盖图像的假统一空间,用来验证「不在本空间」与
|
||
// 「本次失败」必须被区分对待。
|
||
type fakeSpace struct{}
|
||
|
||
func (fakeSpace) VectorizeDense(string) ([]float64, error) { return []float64{1, 0}, nil }
|
||
|
||
func (fakeSpace) EmbedImageDense(_ []byte, mime string) ([]float64, error) {
|
||
if strings.HasPrefix(mime, "audio/") || strings.HasPrefix(mime, "video/") {
|
||
return nil, fmt.Errorf("%w: %s", vector.ErrModalityUnsupported, mime)
|
||
}
|
||
return []float64{1, 0}, nil
|
||
}
|
||
|
||
func (fakeSpace) Fingerprint() string { return "fake-space" }
|
||
func (fakeSpace) Dim() int { return 2 }
|
||
func (fakeSpace) Loaded() bool { return true }
|
||
func (fakeSpace) Close() {}
|
||
|
||
// TestReembedStaleMedia_SkipsUnsupportedWithoutFaking 验证向量迁移不会:
|
||
// - 把音频当失败反复重试;
|
||
// - 更不能拿另一个模型的向量顶替音频(那会污染统一空间且静默)。
|
||
func TestReembedStaleMedia_SkipsUnsupportedWithoutFaking(t *testing.T) {
|
||
a := newMediaLoopAgent(t)
|
||
// 图像块与音频块各落一条:字节都进块(union)
|
||
img := mediaBlock("image/png", []byte("img-bytes"))
|
||
aud := mediaBlock("audio/wav", []byte("aud-bytes"))
|
||
if err := a.memory.PutMemoryBlocks([]memory.MemoryBlock{img, aud}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
a.multimodalSpace = fakeSpace{}
|
||
a.reembedStaleMedia()
|
||
|
||
// 图像块应拿到本空间向量(fp 写回,dim=2)
|
||
imgGot, ok, err := a.memory.BlockByIDPrefix(img.ID)
|
||
if err != nil || !ok {
|
||
t.Fatalf("BlockByIDPrefix(img): %v / %v", ok, err)
|
||
}
|
||
if len(imgGot.Vector) != 2 || imgGot.Fingerprint != "fake-space" {
|
||
t.Fatalf("图像应拿到本空间向量,实际 vec=%v model=%q", imgGot.Vector, imgGot.Fingerprint)
|
||
}
|
||
|
||
// 音频块不得被写入任何向量(不能用别的模型顶替),fp 应保持为空
|
||
audGot, ok, err := a.memory.BlockByIDPrefix(aud.ID)
|
||
if err != nil || !ok {
|
||
t.Fatalf("BlockByIDPrefix(aud): %v / %v", ok, err)
|
||
}
|
||
if len(audGot.Vector) != 0 || audGot.Fingerprint != "" {
|
||
t.Fatalf("音频不得被写入任何向量(不能用别的模型顶替),实际 vec=%v model=%q",
|
||
audGot.Vector, audGot.Fingerprint)
|
||
}
|
||
}
|
||
|
||
// 无多模态空间可用时迁移路径必须整体跳过,不报错也不写脏向量。
|
||
func TestReembedStaleMedia_NilSpaceNoop(t *testing.T) {
|
||
a := newMediaLoopAgent(t)
|
||
b := mediaBlock("image/png", []byte("x"))
|
||
if err := a.memory.PutMemoryBlocks([]memory.MemoryBlock{b}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
// multimodalSpace 为 nil:整体跳过
|
||
a.multimodalSpace = nil
|
||
a.reembedStaleMedia()
|
||
got, _, _ := a.memory.BlockByIDPrefix(b.ID)
|
||
if len(got.Vector) != 0 || got.Fingerprint != "" {
|
||
t.Errorf("无空间可用时不该写向量: vec=%v model=%q", got.Vector, got.Fingerprint)
|
||
}
|
||
}
|
||
|
||
// failSpace 让指定 mime 的图像嵌入失败,用来验证「失败必须留下原因」。
|
||
type failSpace struct {
|
||
failMIME string
|
||
}
|
||
|
||
func (failSpace) VectorizeDense(string) ([]float64, error) { return []float64{1, 0}, nil }
|
||
|
||
func (f failSpace) EmbedImageDense(_ []byte, mime string) ([]float64, error) {
|
||
if mime == f.failMIME {
|
||
return nil, fmt.Errorf("模拟解码失败: %s", mime)
|
||
}
|
||
return []float64{1, 0}, nil
|
||
}
|
||
|
||
func (failSpace) Fingerprint() string { return "fail-space" }
|
||
func (failSpace) Dim() int { return 2 }
|
||
func (failSpace) Loaded() bool { return true }
|
||
func (failSpace) Close() {}
|
||
|
||
// 迁移失败必须留下**可诊断的原因**,而不只是一个计数。
|
||
//
|
||
// ★ 2026-10-23 生产事故:日志只有
|
||
//
|
||
// [media] 向量迁移完成: 成功=0 失败=4 总计=4
|
||
//
|
||
// 没有任何信息说明为什么。反查数据库才发现那 4 个块是 image/webp,
|
||
// 而 provider 没注册 webp 解码器。**失败不可观测直接导致排查成本翻倍**。
|
||
//
|
||
// 本判据钉住:失败原因按 "mime | error" 聚合后必须出现在日志里。
|
||
//
|
||
// 变异:把 failReasons 的采集与打印删掉 ⇒ 红。
|
||
func TestReembedStaleMedia_LogsFailureReasons(t *testing.T) {
|
||
a := newMediaLoopAgent(t)
|
||
// 两个都会失败(同一 mime)—— 应聚合成一条 "2 个: ..."
|
||
for i := 0; i < 2; i++ {
|
||
b := mediaBlock("image/webp", []byte("webp-bytes"))
|
||
if err := a.memory.PutMemoryBlocks([]memory.MemoryBlock{b}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
|
||
logs := captureLog(t, func() {
|
||
a.multimodalSpace = failSpace{failMIME: "image/webp"}
|
||
a.reembedStaleMedia()
|
||
})
|
||
|
||
if !strings.Contains(logs, "failure reasons grouped by") {
|
||
t.Fatalf("migration failure must log grouped failure reasons, actual log:\n%s", logs)
|
||
}
|
||
if !strings.Contains(logs, "image/webp") {
|
||
t.Fatalf("failure reasons must contain the concrete mime (otherwise the format cannot be identified), actual log:\n%s", logs)
|
||
}
|
||
if !strings.Contains(logs, "模拟解码失败") {
|
||
t.Fatalf("失败原因里必须带上 provider 返回的错误原文,实际日志:\n%s", logs)
|
||
}
|
||
// 计数必须出现(格式 "N 个: ...")—— 具体数字取决于去重后的块数,
|
||
// 这里只要求"有计数",不绑死数字(绑死会让判据随去重策略变化而脆)。
|
||
if !regexp.MustCompile(`\d+ x: image/webp`).MatchString(logs) {
|
||
t.Fatalf("failure reasons must include an aggregate count (like \"N x: image/webp | ...\"), actual log:\n%s", logs)
|
||
}
|
||
}
|
||
|
||
// 不支持模态与临时失败必须**分开计数**:前者永远好不了(重试无意义),
|
||
// 混在"失败"里会让人误以为重试即可恢复。
|
||
func TestReembedStaleMedia_SeparatesUnsupportedFromFailed(t *testing.T) {
|
||
a := newMediaLoopAgent(t)
|
||
// 音频走 fakeSpace 的 ErrModalityUnsupported 分支;但 reembedStaleMedia
|
||
// 只挑 image 模态,所以用图像 mime + 让 provider 返回 unsupported 来验证分类。
|
||
img := mediaBlock("image/tiff", []byte("tiff-bytes"))
|
||
if err := a.memory.PutMemoryBlocks([]memory.MemoryBlock{img}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
logs := captureLog(t, func() {
|
||
a.multimodalSpace = unsupportedImageSpace{}
|
||
a.reembedStaleMedia()
|
||
})
|
||
|
||
if !strings.Contains(logs, "unsupported=1") {
|
||
t.Fatalf("unsupported modality must be counted separately (unsupported=1), actual log:\n%s", logs)
|
||
}
|
||
}
|
||
|
||
// unsupportedImageSpace 对任何图像都返回 ErrModalityUnsupported。
|
||
type unsupportedImageSpace struct{}
|
||
|
||
func (unsupportedImageSpace) VectorizeDense(string) ([]float64, error) { return []float64{1, 0}, nil }
|
||
|
||
func (unsupportedImageSpace) EmbedImageDense(_ []byte, mime string) ([]float64, error) {
|
||
return nil, fmt.Errorf("%w: %s", vector.ErrModalityUnsupported, mime)
|
||
}
|
||
|
||
func (unsupportedImageSpace) Fingerprint() string { return "unsupported-space" }
|
||
func (unsupportedImageSpace) Dim() int { return 2 }
|
||
func (unsupportedImageSpace) Loaded() bool { return true }
|
||
func (unsupportedImageSpace) Close() {}
|