| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | |
| 7 | "reasonix/internal/agent" |
| 8 | "reasonix/internal/store" |
| 9 | ) |
| 10 | |
| 11 | // A source proven by background discovery is a pending user choice, not a |
| 12 | // damaged canonical session. Never read its content or acquire its writer lock |
| 13 | // while restoring presentation. Durable recovery/mapping evidence wins. |
| 14 | func (a *App) savedTabHistoricalSource(entry desktopTabEntry, evidence savedTabReconcileEvidence) *SessionSourceRef { |
| 15 | if entry.SessionID != "" || entry.SessionPath == "" || evidence.registryErr != nil { |
| 16 | return nil |
| 17 | } |
| 18 | if _, found, _ := savedTabPendingSessionIdentity(entry, evidence); found { |
| 19 | return nil |
| 20 | } |
| 21 | if savedTabHasRecoveryOwner(entry, evidence) { |
| 22 | return nil |
| 23 | } |
| 24 | path := agent.CanonicalSessionPath(entry.SessionPath) |
| 25 | key := desktopSourceKey(path, entry.SessionHeadID) |
| 26 | if _, adopted, err := historicalMappingForSource(evidence.registry, key); adopted || err != nil { |
| 27 | return nil |
| 28 | } |
| 29 | // The saved identity is already known. Stat just this source, without |
| 30 | // waiting for (or iterating over) the background discovery map. |
| 31 | info, err := os.Lstat(path) |
| 32 | if err != nil || info.Mode()&os.ModeSymlink != 0 { |
| 33 | return nil |
| 34 | } |
| 35 | valid := info.Mode().IsRegular() && store.IsSessionTranscriptName(filepath.Base(path)) |
| 36 | if info.IsDir() { |
| 37 | valid = hasHistoricalSessionArtifacts(path) |
| 38 | } |
| 39 | if valid { |
| 40 | return &SessionSourceRef{HostID: localDesktopHostID, Path: path, HeadID: entry.SessionHeadID, SourceKey: key} |
| 41 | } |
| 42 | return nil |
| 43 | } |
| 44 | |
| 45 | func hasHistoricalSessionArtifacts(path string) bool { |
| 46 | for _, name := range []string{"manifest.json", "events.frames", "events.jsonl"} { |
| 47 | if info, err := os.Lstat(filepath.Join(path, name)); err == nil && info.Mode().IsRegular() { |
| 48 | return true |
| 49 | } |
| 50 | } |
| 51 | return false |
| 52 | } |
| 53 |