| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | |
| 7 | "reasonix/internal/agent" |
| 8 | ) |
| 9 | |
| 10 | // A source proven by background discovery is a pending user choice, not a |
| 11 | // damaged canonical session. Never read its content or acquire its writer lock |
| 12 | // while restoring presentation. Durable recovery/mapping evidence wins. |
| 13 | func (a *App) savedTabHistoricalSource(entry desktopTabEntry, evidence savedTabReconcileEvidence) *SessionSourceRef { |
| 14 | if entry.SessionID != "" || entry.SessionPath == "" || evidence.registryErr != nil || evidence.draftErr != nil { |
| 15 | return nil |
| 16 | } |
| 17 | if _, found, _ := savedTabPendingSessionIdentity(entry, evidence); found { |
| 18 | return nil |
| 19 | } |
| 20 | if savedTabHasRecoveryOwner(entry, evidence) { |
| 21 | return nil |
| 22 | } |
| 23 | path := agent.CanonicalSessionPath(entry.SessionPath) |
| 24 | c := &a.historicalImports |
| 25 | c.mu.Lock() |
| 26 | defer c.mu.Unlock() |
| 27 | for key, source := range c.sources { |
| 28 | if !sameDesktopPath(source.path, path) || source.scope != entry.Scope || |
| 29 | source.scope == "project" && !sameDesktopPath(source.root, entry.WorkspaceRoot) { |
| 30 | continue |
| 31 | } |
| 32 | if _, adopted := historicalMappingForSource(evidence.registry, key); adopted { |
| 33 | return nil |
| 34 | } |
| 35 | // A path-only saved tab selects the source's current head, not whichever |
| 36 | // indexed branch happens to appear first in this map iteration. |
| 37 | return &SessionSourceRef{HostID: localDesktopHostID, Path: path} |
| 38 | } |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | func hasHistoricalSessionArtifacts(path string) bool { |
| 43 | for _, name := range []string{"manifest.json", "events.frames", "events.jsonl"} { |
| 44 | if info, err := os.Lstat(filepath.Join(path, name)); err == nil && info.Mode().IsRegular() { |
| 45 | return true |
| 46 | } |
| 47 | } |
| 48 | return false |
| 49 | } |
| 50 |