| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "os" |
| 7 | |
| 8 | "reasonix/internal/agent" |
| 9 | ) |
| 10 | |
| 11 | // loadSessionTitles reads the basename→title map (missing/corrupt → empty). |
| 12 | func loadSessionTitles(dir string) map[string]string { |
| 13 | m, err := loadSessionTitlesWithError(dir) |
| 14 | if err != nil { |
| 15 | return map[string]string{} |
| 16 | } |
| 17 | return m |
| 18 | } |
| 19 | |
| 20 | // loadSessionTitlesWithError preserves sidecar read and decode failures for |
| 21 | // migrations that must not certify a fallback while a custom title may still |
| 22 | // be recoverable. A missing sidecar is the ordinary no-overrides case. |
| 23 | func loadSessionTitlesWithError(dir string) (map[string]string, error) { |
| 24 | m := map[string]string{} |
| 25 | b, err := readFileWithTimeout(sessionTitlesPath(dir), topicFileReadTimeout) |
| 26 | if err != nil { |
| 27 | if errors.Is(err, os.ErrNotExist) { |
| 28 | return m, nil |
| 29 | } |
| 30 | return nil, err |
| 31 | } |
| 32 | if err := json.Unmarshal(b, &m); err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | if m == nil { |
| 36 | m = map[string]string{} |
| 37 | } |
| 38 | // Older builds could persist titles polluted with internal wrappers |
| 39 | // (memory-compiler contracts, transient blocks) — clean at the read |
| 40 | // boundary; UserPreviewText is a no-op on clean titles (#5666). |
| 41 | for key, title := range m { |
| 42 | m[key] = agent.UserPreviewText(title) |
| 43 | } |
| 44 | return m, nil |
| 45 | } |
| 46 |