| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "log/slog" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // The tab's workspace chooses where new sessions are created, not where an |
| 10 | // already admitted historical source lives. Resolve compatibility reads by |
| 11 | // their captured path while retaining the known-root and symlink checks. |
| 12 | // Call outside App.mu: known roots can consult project metadata and tab state. |
| 13 | func (a *App) historyReadSource(sessionDir, sessionPath string) (string, string, error) { |
| 14 | if path, _, err := validateSessionPath(sessionDir, sessionPath); err == nil { |
| 15 | return sessionDir, path, nil |
| 16 | } else if !filepath.IsAbs(sessionPath) { |
| 17 | return "", "", err |
| 18 | } |
| 19 | return a.sessionDirForPath(sessionPath) |
| 20 | } |
| 21 | |
| 22 | // historySliceBeforeController keeps durable history observable when model |
| 23 | // configuration prevents the tab's execution controller from starting. |
| 24 | func (a *App) historySliceBeforeController(tabID, sessionDir, sessionPath, sessionID string, req HistorySliceRequest) HistorySlice { |
| 25 | if sessionID != "" { |
| 26 | query, ref, err := a.canonicalSessionQuery(tabID) |
| 27 | if err != nil { |
| 28 | return failedHistorySlice(err.Error()) |
| 29 | } |
| 30 | slice, err := a.canonicalHistorySlice(query, ref, sessionDir, sessionPath, req) |
| 31 | if err != nil { |
| 32 | slog.Debug("desktop: cold canonical history slice failed", "session", ref.SessionID, "err", err) |
| 33 | return failedHistorySlice(err.Error()) |
| 34 | } |
| 35 | return slice |
| 36 | } |
| 37 | if strings.TrimSpace(sessionPath) == "" { |
| 38 | return failedHistorySlice("session path unavailable before controller ready") |
| 39 | } |
| 40 | slice, err := a.coldHistorySlice(sessionDir, sessionPath, req) |
| 41 | if err != nil { |
| 42 | slog.Debug("desktop: cold history slice failed", "path", sessionPath, "err", err) |
| 43 | return failedHistorySlice(err.Error()) |
| 44 | } |
| 45 | return slice |
| 46 | } |
| 47 | |
| 48 | func (a *App) canonicalHistoryContentBeforeController( |
| 49 | tabID, sessionDir, sessionPath, sessionID string, |
| 50 | msgIndex, sub int, |
| 51 | ref HistoryContentRef, |
| 52 | chunkIndex int, |
| 53 | out HistoryContentChunk, |
| 54 | ) HistoryContentChunk { |
| 55 | query, sessionRef, err := a.canonicalSessionQuery(tabID) |
| 56 | if err != nil || entryIDSession(ref.EntryID) != sessionID || sessionRef.SessionID != sessionID { |
| 57 | out.Stale = true |
| 58 | return out |
| 59 | } |
| 60 | src, err := canonicalHistorySliceSource(query, sessionRef) |
| 61 | if err != nil || !src.identityMatches(ref.Revision, ref.RevKnown, ref.Digest) { |
| 62 | out.Stale = true |
| 63 | return out |
| 64 | } |
| 65 | value, found, stale := a.historyFieldValueForSource(src, msgIndex, sub, ref, sessionDisplayResolver(sessionDir, sessionPath), nil, nil) |
| 66 | if stale || !found || len(value) != ref.Size { |
| 67 | out.Stale = true |
| 68 | return out |
| 69 | } |
| 70 | out.Data, out.Chunks = historyContentChunkAt(value, chunkIndex) |
| 71 | out.Done = chunkIndex >= out.Chunks-1 |
| 72 | return out |
| 73 | } |
| 74 |