| 1 | package agent |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | func (s *Session) prepareRecoveryBranchMetaLocked(path string, opts RecoveryBranchOptions, preview string, turns int, digest string, depth int, contentUnchanged bool) (BranchMeta, error) { |
| 6 | existing, ok, err := LoadBranchMeta(path) |
| 7 | if err != nil { |
| 8 | return BranchMeta{}, err |
| 9 | } |
| 10 | meta := opts.BranchMeta |
| 11 | meta.ID = BranchID(path) |
| 12 | if strings.TrimSpace(meta.Name) == "" { |
| 13 | meta.Name = firstNonEmpty(strings.TrimSpace(opts.Name), RecoveryBranchDefaultName) |
| 14 | } |
| 15 | if strings.TrimSpace(meta.ParentID) == "" { |
| 16 | meta.ParentID = recoveryRootID(opts.OriginalPath) |
| 17 | } |
| 18 | meta.ForkTurn = -1 |
| 19 | meta.ForkMessageIndex = len(s.Snapshot()) |
| 20 | meta.Preview = preview |
| 21 | meta.Turns = turns |
| 22 | meta.SchemaVersion = BranchMetaCountsVersion |
| 23 | meta.Recovered = true |
| 24 | meta.VersionKind = VersionRecovery |
| 25 | meta.VersionState = VersionActive |
| 26 | meta.ParentConversationID = firstNonEmpty(meta.ParentConversationID, meta.TopicID) |
| 27 | meta.ParentVersionID = firstNonEmpty(meta.ParentVersionID, meta.ParentID) |
| 28 | meta.BaseRevision = opts.BaseRevision |
| 29 | meta.DiskRevision = opts.DiskRevision |
| 30 | meta.RecoveryReason = firstNonEmpty(strings.TrimSpace(opts.Reason), "session snapshot conflict") |
| 31 | meta.RecoveryDigest = digest |
| 32 | meta.RecoveryDepth = depth |
| 33 | meta.Revision = 1 |
| 34 | ledgerCurrent := false |
| 35 | if ok { |
| 36 | if meta.BaseRevision == 0 { |
| 37 | meta.BaseRevision = existing.BaseRevision |
| 38 | } |
| 39 | if meta.DiskRevision == 0 { |
| 40 | meta.DiskRevision = existing.DiskRevision |
| 41 | } |
| 42 | if meta.ParentConversationID == "" { |
| 43 | meta.ParentConversationID = existing.ParentConversationID |
| 44 | } |
| 45 | if meta.ParentVersionID == "" { |
| 46 | meta.ParentVersionID = existing.ParentVersionID |
| 47 | } |
| 48 | ledgerCurrent = contentUnchanged && existing.Revision > 0 && |
| 49 | strings.TrimSpace(existing.ContentDigest) == digest && |
| 50 | strings.TrimSpace(existing.RecoveryDigest) == digest |
| 51 | if ledgerCurrent { |
| 52 | meta.Revision = existing.Revision |
| 53 | } else { |
| 54 | meta.Revision = max(int64(1), existing.Revision+1) |
| 55 | } |
| 56 | meta.InFlightTurn = existing.InFlightTurn |
| 57 | if ledgerCurrent && strings.TrimSpace(existing.WriterID) != "" { |
| 58 | meta.WriterID = existing.WriterID |
| 59 | } |
| 60 | } |
| 61 | meta.ContentDigest = digest |
| 62 | stampSessionListingProjection(&meta) |
| 63 | if !ledgerCurrent || strings.TrimSpace(meta.WriterID) == "" { |
| 64 | meta.WriterID = SessionWriterID() |
| 65 | } |
| 66 | return meta, nil |
| 67 | } |
| 68 | |
| 69 | func (s *Session) saveRecoveryBranchMetaLocked(path string, opts RecoveryBranchOptions, preview string, turns int, digest string, depth int, contentUnchanged bool) (BranchMeta, error) { |
| 70 | meta, err := s.prepareRecoveryBranchMetaLocked(path, opts, preview, turns, digest, depth, contentUnchanged) |
| 71 | if err != nil { |
| 72 | return BranchMeta{}, err |
| 73 | } |
| 74 | if err := saveBranchMeta(path, meta, true); err != nil { |
| 75 | return BranchMeta{}, err |
| 76 | } |
| 77 | return meta, nil |
| 78 | } |
| 79 |