package main import ( "context" "time" "knowledge-governance/internal/governance" "knowledge-governance/internal/librarysnapshot" postgresstore "knowledge-governance/internal/platform/postgres" ) // takeoverBaselineMaxBlobBytes 与其它归档读取点保持一致:基线清单只校验自身与 // 分区哈希,不把整份快照读进内存。 const takeoverBaselineMaxBlobBytes = 256 * 1024 * 1024 // loadTakeoverResumeFacts 收集"能否安全续写"的持久事实。 // // 任何一项读不到都按**不成立**处理(fail closed):这条路径的用途是允许一次 // 恢复覆盖"已被更新版本顶替"的历史快照,证据缺一条就必须退回拒绝,不能把 // "查不到"当成"没问题"。 func loadTakeoverResumeFacts(ctx context.Context, store libraryResumeFactsStore, archiveRoot string, job governance.LibraryJob, now time.Time) takeoverResumeFacts { facts := takeoverResumeFacts{} // 授权必须绑定本作业的请求摘要,且**签发于顶替之后**:顶替前签的旧授权 // 不知道这个快照已成历史,不能当作"恢复历史版本"的新意图。 if job.RequestDigest != "" && job.TargetSnapshotID != "" { if grant, found, err := store.ActiveLibraryAuthorization(ctx, job.Scope, job.TargetSnapshotID, job.RequestDigest, now); err == nil && found { facts.AuthorizedAt = grant.CreatedAt } } if job.SnapshotID != "" { // 基线必须先有本作业自己的预备份登记,再证明清单字节仍是那个摘要。 if _, found, err := store.LibraryVersionByRef(ctx, job.Scope, governance.LibraryVersionProductionPrebackup, job.ID, job.SnapshotID); err == nil && found { facts.BaselineRecorded = true } facts.BaselineVerified = snapshotManifestIntact(archiveRoot, job.SnapshotID) } facts.MaintenanceOwned = takeoverWindowOwnedBy(ctx, store, job) return facts } // libraryResumeFactsStore 是接管判定需要的只读面(writer 与裸 Store 都满足)。 type libraryResumeFactsStore interface { ActiveLibraryAuthorization(ctx context.Context, scope, snapshotID, paramsDigest string, now time.Time) (governance.LibraryReleaseAuthorization, bool, error) LibraryVersionByRef(ctx context.Context, scope string, kind governance.LibraryVersionKind, sourceJobID, snapshotID string) (governance.LibraryVersion, bool, error) LibraryMaintenanceOf(ctx context.Context, scope string) (*postgresstore.LibraryMaintenance, error) HeldInstanceMaintenance(ctx context.Context) ([]postgresstore.LibraryInstanceMaintenance, error) } // takeoverWindowOwnedBy 要求 scope 与 instance 两把锁都由本作业持有且未释放, // 且两行的目标指纹一致:只有本作业自己开的那扇窗口才允许续写,别人的窗口 // 即使作业号碰巧相同也进不来(Acquire* 的互斥语义在接管判定里同样成立)。 func takeoverWindowOwnedBy(ctx context.Context, store libraryResumeFactsStore, job governance.LibraryJob) bool { scopeRow, err := store.LibraryMaintenanceOf(ctx, job.Scope) if err != nil || scopeRow == nil || scopeRow.OwnerJobID != job.ID || scopeRow.ReleasedAt != nil { return false } held, err := store.HeldInstanceMaintenance(ctx) if err != nil { return false } for _, row := range held { if row.OwnerJobID == job.ID && row.OwnerScope == job.Scope && row.ReleasedAt == nil && scopeRow.TargetFingerprint != "" && row.TargetFingerprint == scopeRow.TargetFingerprint { return true } } return false } // snapshotManifestIntact 校验预备份清单未被改动:归档的清单文件名就是内容摘要, // LoadManifest 会重算比对,字节被换过就失败。分区 blob 的完整复核留给恢复流程, // 这里只回答"基线定义是否还是那一份"。 func snapshotManifestIntact(root, snapshotID string) bool { if root == "" || snapshotID == "" { return false } archive, err := librarysnapshot.Open(root, takeoverBaselineMaxBlobBytes) if err != nil { return false } defer archive.Close() if _, err := archive.LoadManifest(snapshotID); err != nil { return false } return true }