三层回退恢复机制(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:
root
2026-08-05 16:00:08 +08:00
parent 33e97f2a99
commit bc9ef15eb0
28 changed files with 3675 additions and 167 deletions

View File

@ -40,6 +40,9 @@ func fileHash(path string) (string, int64, error) {
return hex.EncodeToString(h[:]), int64(len(data)), nil
}
// maxCapturedContent 回滚内容捕获上限:超大文件不保存原文(回滚时跳过并告警)。
const maxCapturedContent = 8 << 20
func fileInfo(path string) (size int64, modTime time.Time, err error) {
info, err := os.Stat(path)
if err != nil {
@ -53,6 +56,7 @@ type FSState struct {
Root string `json:"root"`
}
// captureFSState 仅记录哈希/尺寸(用于"之后"快照,省内存)。
func captureFSState(root string) (*FSState, error) {
state := &FSState{
Files: make(map[string]FileChange),
@ -77,6 +81,27 @@ func captureFSState(root string) (*FSState, error) {
return state, err
}
// captureFSStateWithContent 额外捕获文件原文(用于"之前"基线,供回滚还原被改/被删文件)。
func captureFSStateWithContent(root string) (*FSState, error) {
state, err := captureFSState(root)
if err != nil {
return nil, err
}
for rel := range state.Files {
path := filepath.Join(root, rel)
info, err := os.Stat(path)
if err != nil || info.Size() > maxCapturedContent {
continue
}
if data, err := os.ReadFile(path); err == nil {
fc := state.Files[rel]
fc.Content = data
state.Files[rel] = fc
}
}
return state, nil
}
func diffStates(before, after *FSState) []FileChange {
var changes []FileChange
if before == nil || after == nil {
@ -95,6 +120,7 @@ func diffStates(before, after *FSState) []FileChange {
HashAfter: afterFile.HashAfter,
SizeBefore: beforeFile.SizeAfter,
SizeAfter: afterFile.SizeAfter,
Content: beforeFile.Content, // 原始内容,供回滚还原
})
}
} else {
@ -114,6 +140,7 @@ func diffStates(before, after *FSState) []FileChange {
Type: ChangeFileDeleted,
HashBefore: before.Files[path].HashAfter,
SizeBefore: before.Files[path].SizeAfter,
Content: before.Files[path].Content, // 原始内容,供回滚还原
})
}
}