Commit aa3d023
authored
feat: SessionStore.list_session_summaries — batch summary fetch for list-all (#847)
## Problem
`list_sessions_from_store()` calls `store.list_sessions()` for IDs, then
`store.load()` **per session** to derive title/first-prompt/branch/etc.
With N sessions that's N full-transcript round-trips just to render a
list. #846 (`load_range`) cut bytes-per-session but not round-trips —
500 sessions still meant 500+ store calls.
## Public API change — additive only, no breaking changes
```python
# New TypedDict (3 fields; `data` is opaque, stores persist verbatim)
class SessionSummaryEntry(TypedDict):
session_id: str
mtime: int
data: dict[str, Any]
# New helper — adapters call this from append()
def fold_session_summary(
prev: SessionSummaryEntry | None,
key: SessionKey,
entries: list[SessionStoreEntry],
) -> SessionSummaryEntry: ...
# New optional Protocol method (default: raise NotImplementedError)
class SessionStore(Protocol):
async def list_session_summaries(self, project_key: str) -> list[SessionSummaryEntry]: ...
```
**Unchanged:**
`SessionStore.{append,load,list_sessions,delete,list_subkeys}`,
`list_sessions_from_store()` signature/return,
`get_session_info_from_store()`, `SDKSessionInfo`,
`SessionStoreListEntry`, `InMemorySessionStore` public methods. Stores
that don't implement `list_session_summaries` work exactly as before.
## Approach
Every field the list view needs is **append-incremental**: 4 are
set-once from the first entries (`is_sidechain`, `created_at`, `cwd`,
`first_prompt`), 7 are last-write-wins from the tail (`custom_title`,
`ai_title`, `last_prompt`, `summary_hint`, `git_branch`, `tag`,
`mtime`), 0 require a full scan. So a store can maintain a small summary
record alongside each session, updated inside `append()` with the
entries already in hand — no re-reads.
This PR adds:
- **`SessionSummaryEntry`** (TypedDict) — 3-field record (`session_id`,
`mtime`, opaque `data`). Stores persist it verbatim and never interpret
`data`.
- **`fold_session_summary(prev, key, entries)`** — pure helper that
folds new entries into the previous summary. Adapters call this from
`append()` so derivation logic lives in one place (no per-adapter
drift). `created_at` latches the first *parseable* entry timestamp — a
documented divergence from the lite-parse path only when the very first
entry lacks a timestamp (never happens in CLI-produced transcripts).
- **`SessionStore.list_session_summaries(project_key)`** — optional
Protocol method returning all summaries for a project in one call.
- **Fast path in `list_sessions_from_store()`** — when the store
implements `list_session_summaries`: build a unified slot list
(summary-derived slots + gap-fill placeholders for sessions present in
`list_sessions()` but lacking a sidecar), sort by `mtime`, apply
`offset`/`limit`, then `load()` only the placeholders that landed in the
page. Summary-backed sidechain/empty sessions are pre-filtered *before*
pagination so they don't consume page positions (matching disk/slow-path
filter-then-paginate); only gap-fill placeholders that resolve to `None`
after load can short-page, so a store with complete sidecars never
short-pages. `load()` count is bounded by page size, not total missing —
zero `load()` calls when sidecars are complete. Gap-fill is best-effort:
if the store lacks `list_sessions` it's skipped with a debug log.
Otherwise falls back to the existing per-session `load()` path (bounded
at 16 concurrent).
- **`InMemorySessionStore`** reference impl (entry-timestamp-derived
`mtime` so fast/slow paths sort on one clock) + conformance contract
#14.
`get_session_info_from_store()` (single session) is unchanged — full
`load()` is fine there.
## Correctness
`tests/test_session_summary.py::TestParityWithLiteParse` proves
`summary_entry_to_sdk_info(fold_session_summary(...))` produces the same
`SDKSessionInfo` as the existing `_parse_session_info_from_lite` batch
path on the same entry stream.
## For reviewers
- `_internal/session_summary.py` — fold logic, esp. first-prompt skip
filter (mirrors `_extract_first_prompt_from_head`)
- `_internal/sessions.py` fast-path block — fallback semantics
- `types.py` — public surface: `SessionSummaryEntry`,
`fold_session_summary`, `list_session_summaries`
Supersedes #846. TS port: anthropics/claude-cli-internal#30520.1 parent a05af6b commit aa3d023
8 files changed
Lines changed: 1481 additions & 62 deletions
File tree
- src/claude_agent_sdk
- _internal
- testing
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| |||
109 | 110 | | |
110 | 111 | | |
111 | 112 | | |
| 113 | + | |
112 | 114 | | |
113 | 115 | | |
114 | 116 | | |
| |||
602 | 604 | | |
603 | 605 | | |
604 | 606 | | |
| 607 | + | |
605 | 608 | | |
606 | 609 | | |
| 610 | + | |
607 | 611 | | |
608 | 612 | | |
609 | 613 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
| 17 | + | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
| |||
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
44 | 63 | | |
45 | 64 | | |
46 | 65 | | |
47 | 66 | | |
48 | | - | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
49 | 82 | | |
50 | 83 | | |
51 | 84 | | |
| |||
64 | 97 | | |
65 | 98 | | |
66 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
67 | 105 | | |
68 | 106 | | |
69 | 107 | | |
| |||
72 | 110 | | |
73 | 111 | | |
74 | 112 | | |
| 113 | + | |
75 | 114 | | |
76 | 115 | | |
77 | 116 | | |
| |||
103 | 142 | | |
104 | 143 | | |
105 | 144 | | |
| 145 | + | |
| 146 | + | |
106 | 147 | | |
107 | 148 | | |
108 | 149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
0 commit comments