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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
import json
import os
import tempfile
import unittest
import unittest.mock
from claude_logbook import sessions as S
from .fixtures import ai_title, assistant, simple_tree, ts, user, write_session
class TempRoot(unittest.TestCase):
def setUp(self):
self._tmp = tempfile.TemporaryDirectory()
self.root = os.path.join(self._tmp.name, "projects")
os.makedirs(self.root)
self.cache = os.path.join(self._tmp.name, "cache.json")
self.addCleanup(self._tmp.cleanup)
def load(self, **kw):
kw.setdefault("cache_path", self.cache)
return S.load_sessions(root=self.root, **kw)
class TestReadSession(TempRoot):
def test_basic_conversation(self):
path = write_session(self.root, "-p", "11111111-1111-1111-1111-111111111111", [
user("why does it fail?", at=ts(0)),
assistant("Looking.", tools=[("Bash", {"command": "make test"})], at=ts(5)),
])
rec = S.read_session(path)
self.assertEqual(rec["u"], 1)
self.assertEqual(rec["a"], 1)
self.assertEqual(rec["d"], 5)
self.assertEqual(rec["p"], "/home/u/proj")
self.assertEqual(rec["b"], "main")
self.assertFalse(rec["e"])
self.assertFalse(rec["ai"])
self.assertEqual(rec["t"], "why does it fail?")
self.assertEqual([m["r"] for m in rec["c"]], ["u", "a", "t"])
self.assertEqual(rec["c"][2]["x"], "Bash: make test")
def test_claude_title_beats_the_first_message(self):
path = write_session(self.root, "-p", "22222222-0000-0000-0000-000000000000", [
user("fix this", at=ts(0)),
ai_title("First attempt"),
ai_title("Final title"),
])
rec = S.read_session(path)
self.assertEqual(rec["t"], "Final title")
self.assertTrue(rec["ai"])
def test_ignores_harness_noise(self):
path = write_session(self.root, "-p", "33333333-0000-0000-0000-000000000000", [
user("<command-name>/clear</command-name>", at=ts(0)),
user("<system-reminder>note</system-reminder>", at=ts(1)),
user("real text <system-reminder>note</system-reminder>", at=ts(2)),
user("meta", at=ts(2), isMeta=True),
user("from a subagent", at=ts(3), isSidechain=True),
assistant("subagent reply", at=ts(4), isSidechain=True),
])
rec = S.read_session(path)
self.assertEqual(rec["u"], 1)
self.assertEqual(rec["a"], 0)
self.assertEqual(rec["c"][0]["x"], "real text")
def test_tolerates_a_line_cut_in_half(self):
path = write_session(self.root, "-p", "44444444-0000-0000-0000-000000000000", [
user("first", at=ts(0)),
])
with open(path, "a", encoding="utf-8") as f:
f.write('{"type": "user", "message": {"content": [{"type": "te')
rec = S.read_session(path)
self.assertEqual(rec["u"], 1)
def test_session_without_messages(self):
path = write_session(self.root, "-p", "55555555-0000-0000-0000-000000000000", [
{"type": "system", "timestamp": ts(0), "cwd": "/home/u/proj"},
])
rec = S.read_session(path)
self.assertTrue(rec["e"])
self.assertIsNone(rec["t"])
self.assertEqual(rec["c"], [])
def test_single_huge_message_is_claude_p(self):
largo = "x" * (S.NONINTERACTIVE_CHARS + 1)
path = write_session(self.root, "-p", "66666666-0000-0000-0000-000000000000", [
user(largo, at=ts(0)),
])
self.assertTrue(S.read_session(path)["n"])
def test_short_chat_is_not_claude_p(self):
path = write_session(self.root, "-p", "77777777-0000-0000-0000-000000000000", [
user("hello", at=ts(0)),
])
self.assertFalse(S.read_session(path)["n"])
class TestTitlesCompactionDuration(TempRoot):
def test_rename_beats_the_claude_title(self):
path = write_session(self.root, "-p", "22222222-0000-0000-0000-000000000001", [
ai_title("What Claude thinks"),
user("hi there", at=ts(0)),
{"type": "custom-title", "customTitle": "Old name"},
{"type": "custom-title", "customTitle": "Lo grave"},
ai_title("A later Claude title"),
])
rec = S.read_session(path)
self.assertEqual(rec["t"], "Lo grave")
self.assertTrue(rec["ai"])
def test_compact_summary_is_not_a_message_of_yours(self):
path = write_session(self.root, "-p", "22222222-0000-0000-0000-000000000002", [
{"type": "system", "subtype": "compact_boundary", "timestamp": ts(0)},
user("This session is being continued from a previous conversation. "
"Summary: we fixed the build.", at=ts(0), isCompactSummary=True,
isVisibleInTranscriptOnly=True),
user("now the tests", at=ts(2)),
assistant("On it.", at=ts(3)),
])
rec = S.read_session(path)
self.assertEqual([m["r"] for m in rec["c"]], ["c", "u", "a"])
self.assertIn("we fixed the build", rec["c"][0]["x"])
self.assertEqual(rec["u"], 1)
self.assertEqual(rec["t"], "now the tests")
self.assertFalse(rec["n"])
def test_duration_leaves_out_long_pauses(self):
path = write_session(self.root, "-p", "22222222-0000-0000-0000-000000000003", [
user("start", at=ts(0)),
assistant("ok", at=ts(10)),
user("next day", at=ts(0, day=15)),
assistant("ok", at=ts(20, day=15)),
{"type": "system", "timestamp": ts(0, day=20)}, # not a message
])
self.assertEqual(S.read_session(path)["d"], 30)
def test_active_minutes(self):
self.assertIsNone(S.active_minutes([]))
self.assertEqual(S.active_minutes([ts(0)]), 0)
self.assertEqual(S.active_minutes([ts(0), ts(30), ts(31, hour=11)]), 30)
class TestBranchesAndDirectories(TempRoot):
ORIGIN = "33333333-0000-0000-0000-000000000001"
def branch(self, own=True):
fork = {"forkedFrom": {"sessionId": self.ORIGIN, "messageUuid": "x"}}
events = [
user("the original question", at=ts(0, hour=8), **fork),
assistant("the original answer", at=ts(5, hour=8), **fork),
{"type": "custom-title", "customTitle": "Try another way (Branch)"},
]
if own:
events += [user("and now this", at=ts(0)), assistant("ok", at=ts(10))]
return write_session(self.root, "-home-u-proj",
"33333333-0000-0000-0000-000000000002", events)
def test_the_copied_history_is_inherited(self):
rec = S.read_session(self.branch())
self.assertEqual(rec["o"], self.ORIGIN)
self.assertEqual(rec["h"], 2)
self.assertEqual([m["x"] for m in rec["c"][2:]], ["and now this", "ok"])
self.assertEqual((rec["u"], rec["a"], rec["d"]), (1, 1, 10))
self.assertEqual(rec["f"], ts(0))
self.assertEqual(rec["t"], "Try another way (Branch)")
def test_a_branch_with_nothing_of_its_own(self):
rec = S.read_session(self.branch(own=False))
self.assertEqual((rec["h"], rec["u"], rec["a"]), (2, 0, 0))
self.assertFalse(rec["e"])
self.assertEqual(rec["f"], ts(0, hour=8))
def test_not_a_branch(self):
path = write_session(self.root, "-p", "33333333-0000-0000-0000-000000000003", [
user("hello", at=ts(0))])
rec = S.read_session(path)
self.assertEqual((rec["h"], rec["o"]), (0, None))
def test_the_origin_title_is_filled_in(self):
self.branch()
write_session(self.root, "-home-u-proj", self.ORIGIN, [
ai_title("The original"), user("the original question", at=ts(0, hour=8))])
by_id = {s["id"]: s for s in S.load_sessions(self.root, use_cache=False)}
self.assertEqual(by_id["33333333-0000-0000-0000-000000000002"]["ot"], "The original")
self.assertIsNone(by_id[self.ORIGIN]["ot"])
def test_directory_changes_are_marked_and_filtered(self):
path = write_session(self.root, "-home-u-proj", "33333333-0000-0000-0000-000000000004", [
{"type": "system", "timestamp": ts(0), "cwd": "/home/u/proj"},
user("build it", at=ts(1)),
assistant("ok",
|