Commit 95413fd
authored
feat(session_store): add import_session_to_store() for local→store replay (#858)
## Summary
Ports the TypeScript SDK's `importSessionToStore()` (`@alpha`,
`agentSdk.ts`) to Python as `import_session_to_store()`.
Streams a local `~/.claude/projects/<dir>/<sessionId>.jsonl`
line-by-line into `SessionStore.append()` in batches (default 500
entries / 1 MiB), optionally including subagent transcripts under
`<sessionId>/subagents/**` and their `.meta.json` sidecars (appended as
`{"type": "agent_metadata", ...}` so `materialize_resume_session()` can
recreate them).
This is the inverse of `materialize_resume_session()` (store → temp
disk). Key derivation uses `project_key_for_directory()` and the subpath
construction matches `file_path_to_session_key()`, so an imported
session is indistinguishable from one that was live-mirrored via
`TranscriptMirrorBatcher` and is resumable via
`query(options=ClaudeAgentOptions(session_store=store,
resume=session_id))` from the same cwd.
Useful for migrating existing local sessions to a remote store, or
catching a store up after a `MirrorErrorMessage` indicated a live-mirror
gap. Pairs with #857's uuid-idempotency docs — adapters should treat
`entry["uuid"]` as an idempotency key so re-import is duplicate-safe.
## API
```python
async def import_session_to_store(
session_id: str,
store: SessionStore,
*,
directory: str | None = None,
include_subagents: bool = True,
batch_size: int = 500,
) -> None:
```
(Uses `directory=` to match
`list_sessions`/`get_session_info`/`fork_session` rather than TS's
`dir`, which shadows a Python builtin.)
## Tests
12 tests in `tests/test_session_import.py`:
- main transcript import → `InMemorySessionStore`, entries match
- `batch_size=2` with 5 entries → 3 `append()` calls (2+2+1),
spy-verified
- blank lines skipped; `batch_size<=0` falls back to default
- subagent transcripts imported with correct `subpath` (flat + nested
`workflows/run-1/`)
- `.meta.json` sidecar imported as `agent_metadata` entry
- `include_subagents=False` skips subagent files
- missing subagents dir is a no-op
- invalid UUID → `ValueError`; missing session → `FileNotFoundError`
- key parity with `file_path_to_session_key()` (round-trip with
`TranscriptMirrorBatcher`)
`pytest` 12/12, `ruff` clean, `mypy src/` clean.
<!-- CHANGELOG:START -->
- Added `import_session_to_store()` to replay a local `~/.claude`
session transcript into a `SessionStore` (parity with TypeScript SDK
`importSessionToStore`)
<!-- CHANGELOG:END -->1 parent a7a5ead commit 95413fd
4 files changed
Lines changed: 494 additions & 22 deletions
File tree
- e2e-tests
- src/claude_agent_sdk
- _internal
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | 3 | | |
5 | | - | |
6 | 4 | | |
7 | 5 | | |
8 | 6 | | |
| |||
154 | 152 | | |
155 | 153 | | |
156 | 154 | | |
157 | | - | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | 210 | | |
215 | 211 | | |
216 | 212 | | |
217 | 213 | | |
218 | 214 | | |
219 | | - | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
220 | 218 | | |
221 | 219 | | |
222 | 220 | | |
| |||
245 | 243 | | |
246 | 244 | | |
247 | 245 | | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | 246 | | |
253 | 247 | | |
254 | 248 | | |
255 | 249 | | |
256 | 250 | | |
257 | | - | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
258 | 254 | | |
259 | 255 | | |
260 | 256 | | |
| |||
289 | 285 | | |
290 | 286 | | |
291 | 287 | | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | 288 | | |
297 | 289 | | |
298 | 290 | | |
299 | 291 | | |
300 | 292 | | |
301 | | - | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
302 | 296 | | |
303 | 297 | | |
304 | 298 | | |
| |||
327 | 321 | | |
328 | 322 | | |
329 | 323 | | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | 324 | | |
335 | 325 | | |
336 | 326 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| |||
616 | 617 | | |
617 | 618 | | |
618 | 619 | | |
| 620 | + | |
619 | 621 | | |
620 | 622 | | |
621 | 623 | | |
| |||
| 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 | + | |
0 commit comments