| 1 | // Package historycatalog maintains a disposable FTS projection of saved |
| 2 | // session history. Session JSONL/event/meta files remain authoritative. |
| 3 | package historycatalog |
| 4 | |
| 5 | import ( |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "reasonix/internal/config" |
| 11 | "reasonix/internal/projectiondb" |
| 12 | ) |
| 13 | |
| 14 | const ( |
| 15 | SchemaVersion = 1 |
| 16 | TokenizerVersion = 1 |
| 17 | DefaultLimit = 50 |
| 18 | MaxLimit = 200 |
| 19 | ) |
| 20 | |
| 21 | type Status struct { |
| 22 | State string `json:"state"` |
| 23 | Mode projectiondb.Mode `json:"mode"` |
| 24 | Path string `json:"path,omitempty"` |
| 25 | Revision uint64 `json:"revision"` |
| 26 | Indexed int64 `json:"indexed"` |
| 27 | Total int64 `json:"total"` |
| 28 | Pending int64 `json:"pending"` |
| 29 | Failed int64 `json:"failed"` |
| 30 | LastError string `json:"lastError,omitempty"` |
| 31 | QuarantinedPath string `json:"quarantinedPath,omitempty"` |
| 32 | } |
| 33 | |
| 34 | type Options struct { |
| 35 | Path string |
| 36 | InMemory bool |
| 37 | QueueCapacity int |
| 38 | MissingGrace time.Duration |
| 39 | ReconcileInterval time.Duration |
| 40 | MaxBytes int64 // on-disk index cap; 0 = history_search.max_mb or DefaultMaxBytes |
| 41 | Now func() time.Time |
| 42 | OnRevision func(Status, []string, string) |
| 43 | } |
| 44 | |
| 45 | type Root struct { |
| 46 | Path string |
| 47 | Source string |
| 48 | Scope string |
| 49 | WorkspaceRoot string |
| 50 | Subagents bool |
| 51 | Archive bool |
| 52 | } |
| 53 | |
| 54 | type Candidate struct { |
| 55 | RowID int64 |
| 56 | SessionPath string |
| 57 | Root string |
| 58 | Source string |
| 59 | Scope string |
| 60 | WorkspaceRoot string |
| 61 | ContentDigest string |
| 62 | MessageIndex int |
| 63 | PartIndex int |
| 64 | Role string |
| 65 | Kind string |
| 66 | ToolName string |
| 67 | Rank float64 |
| 68 | Score float64 |
| 69 | SessionTitle string |
| 70 | TopicTitle string |
| 71 | LastActivityAt int64 |
| 72 | } |
| 73 | |
| 74 | type SearchRequest struct { |
| 75 | Query string |
| 76 | Scope string |
| 77 | WorkspaceRoot string |
| 78 | // SessionPath restricts results to one exact durable transcript. It is used |
| 79 | // by target-bound sidebar previews so a failed/empty lookup can never fall |
| 80 | // through to a different session in the same root. |
| 81 | SessionPath string |
| 82 | Kinds []string |
| 83 | ToolName string |
| 84 | Limit int |
| 85 | Roots []string |
| 86 | After *SearchCursor |
| 87 | } |
| 88 | |
| 89 | // SearchCursor is a stable keyset over SQLite's BM25 rank and deterministic |
| 90 | // tie-break keys. Lower BM25 ranks sort first. |
| 91 | type SearchCursor struct { |
| 92 | Rank float64 |
| 93 | SessionPath string |
| 94 | MessageIndex int |
| 95 | PartIndex int |
| 96 | RowID int64 |
| 97 | } |
| 98 | |
| 99 | type SearchResult struct { |
| 100 | Items []Candidate |
| 101 | Revision uint64 |
| 102 | Partial bool |
| 103 | } |
| 104 | |
| 105 | // DefaultPath returns the disposable history FTS path under CacheDir. |
| 106 | // Empty when the OS cache directory is unavailable so Open falls back to memory. |
| 107 | func DefaultPath() string { |
| 108 | cache := strings.TrimSpace(config.CacheDir()) |
| 109 | if cache == "" { |
| 110 | return "" |
| 111 | } |
| 112 | return filepath.Join(cache, "history-search", "v1.sqlite") |
| 113 | } |
| 114 |