| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "path/filepath" |
| 6 | "reasonix/desktop/internal/workspacestate" |
| 7 | "reasonix/internal/session" |
| 8 | ) |
| 9 | |
| 10 | func (a *App) resolveHistoricalArchiveSource(ctx context.Context, state workspacestate.State, id string, source historicalSource, fingerprint string) (desktopMigrationSource, workspacestate.SourceMapping, bool, error) { |
| 11 | mapping, mapped, err := state.ResolveSource(id) |
| 12 | if err != nil { |
| 13 | return desktopMigrationSource{}, workspacestate.SourceMapping{}, false, err |
| 14 | } |
| 15 | migration := desktopMigrationSource{scope: source.scope, workspaceRoot: source.root, headID: source.head, registeredSourceKey: id, deferArchive: true} |
| 16 | changed := mapped && mapping.Fingerprint != fingerprint |
| 17 | if !mapped && source.format == "canonical" { |
| 18 | cp, err := newDesktopMigrationCheckpoint(migration, desktopCanonicalMigrationKey(filepath.Dir(source.path), filepath.Base(source.path)), canonicalMigrationSourceFiles(filepath.Dir(source.path), filepath.Base(source.path))) |
| 19 | if err != nil { |
| 20 | return desktopMigrationSource{}, workspacestate.SourceMapping{}, false, err |
| 21 | } |
| 22 | changed = cp.completed() && !cp.unchanged() |
| 23 | } |
| 24 | if changed { |
| 25 | // A source selector addresses the retained file; canonical selectors |
| 26 | // continue to address the adopted conversation independently of it. |
| 27 | migration.versionFingerprint = fingerprint |
| 28 | migration.registeredSourceKey = id + ":review:" + fingerprint |
| 29 | mapped = false |
| 30 | } |
| 31 | if mapped && source.format == "canonical" { |
| 32 | // The full fingerprint just matched the durable mapping. Refresh the |
| 33 | // cheap discovery stamp after an identical file copy/metadata rewrite. |
| 34 | cp, err := newDesktopMigrationCheckpoint(migration, desktopCanonicalMigrationKey(filepath.Dir(source.path), filepath.Base(source.path)), canonicalMigrationSourceFiles(filepath.Dir(source.path), filepath.Base(source.path))) |
| 35 | if err != nil { |
| 36 | return desktopMigrationSource{}, workspacestate.SourceMapping{}, false, err |
| 37 | } |
| 38 | if cp.completed() && cp.record.TargetSessionID == mapping.SessionID && !cp.unchanged() { |
| 39 | if err := cp.complete(mapping.SessionID, cp.record.ContentDigest); err != nil { |
| 40 | return desktopMigrationSource{}, workspacestate.SourceMapping{}, false, err |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | if changed && source.format == "canonical" && (state.SessionStates[mapping.SessionID].Lifecycle == workspacestate.Deleted || state.SessionStates[mapping.SessionID].Lifecycle == workspacestate.Archived) { |
| 45 | if adopted, handled, err := a.reconcileRetiredArchiveVersion(ctx, source, migration); handled || err != nil { |
| 46 | return migration, adopted, handled && err == nil, err |
| 47 | } |
| 48 | } |
| 49 | return migration, mapping, mapped, nil |
| 50 | } |
| 51 | |
| 52 | func (a *App) reconcileRetiredArchiveVersion(ctx context.Context, source historicalSource, migration desktopMigrationSource) (workspacestate.SourceMapping, bool, error) { |
| 53 | root, id := filepath.Dir(source.path), filepath.Base(source.path) |
| 54 | cp, err := newDesktopMigrationCheckpoint(migration, desktopCanonicalMigrationKey(root, id)+":review:"+migration.versionFingerprint, canonicalMigrationSourceFiles(root, id)) |
| 55 | if err != nil { |
| 56 | return workspacestate.SourceMapping{}, true, err |
| 57 | } |
| 58 | old, err := session.NewService("archive-proof", session.NewFilesystemPersistence(root)) |
| 59 | if err != nil { |
| 60 | return workspacestate.SourceMapping{}, true, err |
| 61 | } |
| 62 | defer shutdownHistoricalProofService(old) |
| 63 | digest, err := canonicalMigrationDigest(ctx, old.Query(), session.SessionRef{HostID: "archive-proof", SessionID: id}) |
| 64 | if err != nil { |
| 65 | return workspacestate.SourceMapping{}, true, err |
| 66 | } |
| 67 | if handled, err := a.reconcileCanonicalConversion(ctx, migration, cp, digest); !handled || err != nil { |
| 68 | return workspacestate.SourceMapping{}, handled, err |
| 69 | } |
| 70 | state, err := a.workspaceRegistry().Load(ctx) |
| 71 | if err != nil { |
| 72 | return workspacestate.SourceMapping{}, true, err |
| 73 | } |
| 74 | mapping, found, err := state.ResolveSource(migration.mappingKey(source.path)) |
| 75 | if err == nil && !found { |
| 76 | // The matching target may itself be active. Its source mapping was |
| 77 | // staged for the parent archive rather than committed by reconciliation. |
| 78 | return workspacestate.SourceMapping{}, false, nil |
| 79 | } |
| 80 | return mapping, true, err |
| 81 | } |
| 82 |