| 1 | package workspacestate |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "reflect" |
| 10 | "slices" |
| 11 | |
| 12 | "reasonix/internal/fileutil" |
| 13 | ) |
| 14 | |
| 15 | // CommitHistoricalVersion adopts a validated, unregistered content_ready target |
| 16 | // as an explicitly requested version. The caller holds source/target ownership |
| 17 | // through validation and this commit. The original adoption remains immutable. |
| 18 | func (s *Store) CommitHistoricalVersion(ctx context.Context, observed Operation, versionKey string) error { |
| 19 | return s.mutate(ctx, func(state *State) error { |
| 20 | op, mapping, err := historicalVersionCandidate(state, observed, versionKey) |
| 21 | if err != nil { |
| 22 | return err |
| 23 | } |
| 24 | if err := validateHistoricalVersionDestination(state, op, mapping, versionKey); err != nil { |
| 25 | return err |
| 26 | } |
| 27 | if historicalVersionReservationConflict(state, op, mapping, versionKey) { |
| 28 | return ErrMutationConflict |
| 29 | } |
| 30 | if err := backupHistoricalVersionRegistry(s.path); err != nil { |
| 31 | return err |
| 32 | } |
| 33 | mapping.SourceKey = versionKey |
| 34 | op.Mapping = &mapping |
| 35 | state.PendingOperations[op.ID] = op |
| 36 | return commitOperation(state, op.ID, map[string]bool{}) |
| 37 | }) |
| 38 | } |
| 39 | |
| 40 | func historicalVersionCandidate(state *State, observed Operation, versionKey string) (Operation, SourceMapping, error) { |
| 41 | op, exists := state.PendingOperations[observed.ID] |
| 42 | if !exists || !reflect.DeepEqual(op, observed) || op.Kind != "import" || op.Phase != "content_ready" || |
| 43 | op.Lifecycle != Active || op.Mapping == nil || len(op.SessionIDs) != 1 || len(op.Dependencies) != 0 || op.RecoveryEntryID != "" { |
| 44 | return Operation{}, SourceMapping{}, ErrMutationConflict |
| 45 | } |
| 46 | mapping := *op.Mapping |
| 47 | if mapping.SourceKey == "" || mapping.Fingerprint == "" || versionKey != mapping.SourceKey+":review:"+mapping.Fingerprint || |
| 48 | mapping.SessionID != op.SessionIDs[0] || mapping.WorkspaceID != op.WorkspaceID { |
| 49 | return Operation{}, SourceMapping{}, ErrMutationConflict |
| 50 | } |
| 51 | return op, mapping, nil |
| 52 | } |
| 53 | |
| 54 | func validateHistoricalVersionDestination(state *State, op Operation, mapping SourceMapping, versionKey string) error { |
| 55 | original, adopted := state.SourceMappings[mapping.SourceKey] |
| 56 | if !adopted || original.SessionID == mapping.SessionID || original.Fingerprint == mapping.Fingerprint || |
| 57 | original.Path != mapping.Path || original.HeadID != mapping.HeadID || original.Format != mapping.Format || |
| 58 | original.WorkspaceID != op.WorkspaceID || state.SessionStates[original.SessionID].Lifecycle != Active { |
| 59 | return ErrMutationConflict |
| 60 | } |
| 61 | owner, attached := sessionOwner(*state, original.SessionID) |
| 62 | if !attached || owner != op.WorkspaceID { |
| 63 | return ErrMutationConflict |
| 64 | } |
| 65 | if _, owned := sessionOwner(*state, mapping.SessionID); owned { |
| 66 | return ErrMutationConflict |
| 67 | } |
| 68 | if _, known := state.SessionStates[mapping.SessionID]; known { |
| 69 | return ErrMutationConflict |
| 70 | } |
| 71 | if _, creating := state.PendingCreates[mapping.SessionID]; creating { |
| 72 | return ErrMutationConflict |
| 73 | } |
| 74 | if _, committed := state.SourceMappings[versionKey]; committed { |
| 75 | return ErrMutationConflict |
| 76 | } |
| 77 | return nil |
| 78 | } |
| 79 | |
| 80 | func historicalVersionReservationConflict(state *State, op Operation, mapping SourceMapping, versionKey string) bool { |
| 81 | for id, other := range state.PendingOperations { |
| 82 | if id == op.ID || other.Phase == "committed" { |
| 83 | continue |
| 84 | } |
| 85 | if other.Mapping != nil && other.Mapping.SourceKey == versionKey { |
| 86 | return true |
| 87 | } |
| 88 | if slices.Contains(other.SessionIDs, mapping.SessionID) { |
| 89 | return true |
| 90 | } |
| 91 | } |
| 92 | return false |
| 93 | } |
| 94 | |
| 95 | func backupHistoricalVersionRegistry(path string) error { |
| 96 | // Preserve the exact pre-repair registry, including unknown fields. |
| 97 | body, err := os.ReadFile(path) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | backup := filepath.Join(filepath.Dir(path), "historical-version-backups", fmt.Sprintf("%x.json", sha256.Sum256(body))) |
| 102 | if err := os.MkdirAll(filepath.Dir(backup), 0700); err != nil { |
| 103 | return err |
| 104 | } |
| 105 | return fileutil.AtomicWriteFileStrict(backup, body, 0600) |
| 106 | } |
| 107 |