mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-10-04 00:03:59 +00:00
三层回退恢复机制(L0写前留档/L1恢复梯子/L2离线回滚)+ guard 父守护
- L0: files 插件写受保护系统路径(/etc 等)前自动留档,AbstractBeforeWrite 到 data/file_baseline - L1: failback 受限 worker 执行恢复梯子 probe→还原DNS/proxy→还原LLM配置+ReloadFromConfig→probe,N轮有界 - L2: tracker changeset 持久化原文 blob,guard 离线 RollbackFromDisk 回滚 agentfs;SystemSnapshot 支撑 - guard 父守护: 心跳 IPC(PING/ACK unix socket, 文件心跳回退)、失败计数、退出码协议(42/43/44)、最后手段 - 发行版路径适配: system.protected_paths/network_paths 可注入,默认面向主流 Linux - Windows 兼容: guard.go/failback.go 加 //go:build linux, guard_windows.go 提供 no-op 桩 - 修复: guard.yaml last_resort 键冲突、changeset Content 不落盘导致离线回滚丢原文 Build 全绿, vet 干净, system/recovery/ipc/tracker 单元测试全过
This commit is contained in:
@ -220,3 +220,73 @@ func TestGetHelpers(t *testing.T) {
|
||||
t.Fatalf("GetBool fallback: expected true, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotRestoreCoreLLM(t *testing.T) {
|
||||
r := NewConfigRegistry("")
|
||||
defer r.Close()
|
||||
|
||||
r.Set("core.llm.sources.main.base_url", "https://a")
|
||||
r.Set("core.llm.sources.main.model", "m1")
|
||||
r.Set("core.llm.sources.main.api_key", "k1")
|
||||
|
||||
snap := r.SnapshotCoreLLM()
|
||||
if len(snap) != 3 {
|
||||
t.Fatalf("expected 3 keys, got %d: %v", len(snap), snap)
|
||||
}
|
||||
|
||||
// 模拟写坏
|
||||
r.Set("core.llm.sources.main.base_url", "https://broken")
|
||||
r.Set("core.llm.sources.main.api_key", "hacked")
|
||||
r.Set("core.llm.sources.extra.model", "intruder")
|
||||
|
||||
if err := r.RestoreCoreLLM(snap); err != nil {
|
||||
t.Fatalf("RestoreCoreLLM: %v", err)
|
||||
}
|
||||
got := r.SnapshotCoreLLM()
|
||||
if len(got) != 3 {
|
||||
t.Fatalf("after restore expected 3 keys, got %d: %v", len(got), got)
|
||||
}
|
||||
for k, v := range snap {
|
||||
if got[k] != v {
|
||||
t.Errorf("key %s: want %q got %q", k, v, got[k])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLLMSnapshotFile(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "llm_snapshot.json")
|
||||
snap := map[string]string{"core.llm.sources.main.base_url": "https://a", "core.llm.sources.main.model": "m1"}
|
||||
if err := SaveLLMSnapshot(path, snap); err != nil {
|
||||
t.Fatalf("Save: %v", err)
|
||||
}
|
||||
got, err := LoadLLMSnapshot(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if got["core.llm.sources.main.base_url"] != "https://a" || got["core.llm.sources.main.model"] != "m1" {
|
||||
t.Fatalf("round-trip mismatch: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetLLMSnapshotFile(t *testing.T) {
|
||||
r := NewConfigRegistry("")
|
||||
defer r.Close()
|
||||
r.Set("core.llm.sources.main.base_url", "https://orig")
|
||||
r.Set("core.llm.sources.main.model", "m0")
|
||||
|
||||
path := filepath.Join(t.TempDir(), "llm_pre.json")
|
||||
r.SetLLMSnapshotFile(path)
|
||||
// 再次写入:写前自动留档应记录当前值 orig/m0,随后才被覆盖
|
||||
r.Set("core.llm.sources.main.base_url", "https://broken")
|
||||
|
||||
got, err := LoadLLMSnapshot(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Load snapshot: %v", err)
|
||||
}
|
||||
if got["core.llm.sources.main.base_url"] != "https://orig" {
|
||||
t.Fatalf("write-ahead snapshot should record pre-write value, got %q", got["core.llm.sources.main.base_url"])
|
||||
}
|
||||
if got["core.llm.sources.main.model"] != "m0" {
|
||||
t.Fatalf("snapshot missing untouched key model: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user