-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_recall.py
More file actions
executable file
·1973 lines (1684 loc) · 74.2 KB
/
session_recall.py
File metadata and controls
executable file
·1973 lines (1684 loc) · 74.2 KB
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""session-recall: Search Claude Code session transcripts to recover context after compaction."""
import argparse
import json
import os
import re
import sys
from collections import Counter, defaultdict
from datetime import datetime
from pathlib import Path
def find_project_dirs():
"""Find all Claude Code project directories."""
env_val = os.environ.get("CLAUDE_PROJECTS_DIR", "")
if env_val:
dirs = [Path(p).expanduser() for p in env_val.split(":") if p.strip()]
else:
dirs = [Path.home() / ".claude" / "projects"]
return [d for d in dirs if d.is_dir()]
def find_all_sessions(project_dirs):
"""Find all .jsonl session files across project dirs, sorted by mtime desc."""
sessions = []
for d in project_dirs:
for p in d.rglob("*.jsonl"):
try:
stat = p.stat()
sessions.append((stat.st_mtime, stat.st_size, p))
except OSError:
continue
sessions.sort(key=lambda x: x[0], reverse=True)
return sessions
def find_current_session(project_dirs):
"""Find the most recently modified .jsonl file."""
sessions = find_all_sessions(project_dirs)
if not sessions:
return None
return sessions[0][2]
def session_id_from_path(path):
"""Extract session ID (filename without extension) from path."""
return path.stem
def format_size(size_bytes):
"""Format file size in KB."""
kb = max(1, size_bytes // 1024)
return f"{kb}KB"
def has_human_text(content):
"""Check if a content field has actual human/assistant text (not just tool blocks)."""
if isinstance(content, str):
return bool(content.strip())
if isinstance(content, list):
for block in content:
if isinstance(block, dict):
if block.get("type") == "text" and block.get("text", "").strip():
return True
elif isinstance(block, str) and block.strip():
return True
return False
def extract_human_text(content):
"""Extract only human-readable text blocks (no tool_use/tool_result noise)."""
if isinstance(content, str):
return content
if isinstance(content, list):
parts = []
for block in content:
if isinstance(block, str):
parts.append(block)
elif isinstance(block, dict):
if block.get("type") == "text":
parts.append(block.get("text", ""))
return "\n".join(parts)
return str(content)
def extract_text_from_content(content):
"""Extract readable text from a message content field (string or list of blocks)."""
if isinstance(content, str):
return content
if isinstance(content, list):
parts = []
for block in content:
if isinstance(block, str):
parts.append(block)
elif isinstance(block, dict):
btype = block.get("type", "")
if btype == "text":
parts.append(block.get("text", ""))
elif btype == "tool_use":
name = block.get("name", "?")
inp = block.get("input", {})
inp_str = json.dumps(inp, ensure_ascii=False)
if len(inp_str) > 300:
inp_str = inp_str[:300] + "..."
parts.append(f"[tool_use] {name}: {inp_str}")
elif btype == "tool_result":
tc = block.get("content", "")
text = extract_text_from_content(tc)
if len(text) > 300:
text = text[:300] + "..."
is_err = block.get("is_error", False)
prefix = "[tool_result ERROR]" if is_err else "[tool_result]"
parts.append(f"{prefix} {text}")
elif btype == "thinking":
pass # skip thinking blocks
return "\n".join(parts)
return str(content)
def extract_tool_blocks(content):
"""Extract tool_use and tool_result blocks from content."""
blocks = []
if isinstance(content, list):
for block in content:
if isinstance(block, dict):
btype = block.get("type", "")
if btype == "tool_use":
name = block.get("name", "?")
inp = block.get("input", {})
inp_str = json.dumps(inp, ensure_ascii=False)
if len(inp_str) > 300:
inp_str = inp_str[:300] + "..."
blocks.append(("tool_use", f"{name}: {inp_str}"))
elif btype == "tool_result":
tc = block.get("content", "")
text = extract_text_from_content(tc)
if len(text) > 300:
text = text[:300] + "..."
is_err = block.get("is_error", False)
label = "tool_result ERROR" if is_err else "tool_result"
blocks.append((label, text))
return blocks
def truncate_around(text, pos, before=250, after=350):
"""Return a substring centered around pos with given context."""
start = max(0, pos - before)
end = min(len(text), pos + after)
prefix = "..." if start > 0 else ""
suffix = "..." if end < len(text) else ""
return prefix + text[start:end] + suffix
def print_session_header(session_path):
"""Print standard session header with pin status."""
sid = session_id_from_path(session_path)
size = format_size(session_path.stat().st_size)
pinned = get_pinned_session()
pin_info = " (pinned)" if pinned and pinned == session_path else ""
print(f"Session: {sid}{pin_info}")
print(f"Size: {size}")
print("---")
def cmd_search(session_path, keywords, max_results=15, tools_only=False):
"""Search session for keyword matches."""
patterns = [re.compile(re.escape(kw), re.IGNORECASE) for kw in keywords]
matches = []
with open(session_path, "r", errors="replace") as f:
for line_num, raw_line in enumerate(f):
try:
obj = json.loads(raw_line)
except (json.JSONDecodeError, ValueError):
continue
entry_type = obj.get("type", "")
if entry_type not in ("user", "assistant"):
continue
msg = obj.get("message", {})
role = msg.get("role", entry_type)
content = msg.get("content", "")
if tools_only:
tool_blocks = extract_tool_blocks(content)
if not tool_blocks:
continue
for label, text in tool_blocks:
full_text = f"[{label}] {text}"
if all(p.search(full_text) for p in patterns):
matches.append((line_num, label, full_text))
else:
text = extract_text_from_content(content)
if not text:
continue
if all(p.search(text) for p in patterns):
m = patterns[0].search(text)
pos = m.start() if m else 0
snippet = truncate_around(text, pos)
matches.append((line_num, role, snippet))
if not matches:
print(f"No matches for: {' '.join(keywords)}", file=sys.stderr)
return 1
show = matches[-max_results:]
print_session_header(session_path)
print(f"Found {len(matches)} matches (showing last {len(show)}):\n")
for line_num, role, snippet in show:
print(f"[{role}] (line {line_num})")
print(snippet)
print("---")
return 0
def cmd_recent(session_path, count=20):
"""Show last N messages with actual human/assistant text (skips tool-only messages)."""
entries = []
with open(session_path, "r", errors="replace") as f:
for raw_line in f:
try:
obj = json.loads(raw_line)
except (json.JSONDecodeError, ValueError):
continue
entry_type = obj.get("type", "")
if entry_type not in ("user", "assistant"):
continue
msg = obj.get("message", {})
role = msg.get("role", entry_type)
content = msg.get("content", "")
if not has_human_text(content):
continue
text = extract_human_text(content)
if not text or not text.strip():
continue
entries.append((role, text))
if len(entries) > count * 3:
entries = entries[-(count * 2):]
show = entries[-count:]
print_session_header(session_path)
if not show:
print("No human/assistant messages found.")
return 1
for role, text in show:
display = text.strip()
if len(display) > 400:
display = display[:400] + "..."
print(f"[{role}] {display}\n")
return 0
def cmd_list(project_dirs, count=20):
"""List recent session files."""
sessions = find_all_sessions(project_dirs)
if not sessions:
print("No session files found.", file=sys.stderr)
return 1
pinned = get_pinned_session()
for mtime, fsize, path in sessions[:count]:
ts = datetime.fromtimestamp(mtime).strftime("%Y-%m-%d %H:%M:%S")
sz = format_size(fsize).rjust(8)
marker = " *" if pinned and path == pinned else ""
print(f"{ts} {sz} {path.name}{marker}")
if pinned:
print(f"\n* = pinned session")
return 0
def cmd_decisions(session_path, max_results=15):
"""Find decision points in the session."""
decision_keywords = [
"decided", "chose", "going with", "approach:", "plan:",
"will use", "decision:", "agreed", "settled on", "picked",
"recommendation:", "let's go with", "the plan is"
]
pattern = re.compile("|".join(re.escape(kw) for kw in decision_keywords), re.IGNORECASE)
matches = []
with open(session_path, "r", errors="replace") as f:
for line_num, raw_line in enumerate(f):
try:
obj = json.loads(raw_line)
except (json.JSONDecodeError, ValueError):
continue
entry_type = obj.get("type", "")
if entry_type not in ("user", "assistant"):
continue
msg = obj.get("message", {})
role = msg.get("role", entry_type)
content = msg.get("content", "")
text = extract_text_from_content(content)
if not text:
continue
m = pattern.search(text)
if m:
snippet = truncate_around(text, m.start())
matches.append((line_num, role, snippet))
if not matches:
print("No decision points found.", file=sys.stderr)
return 1
show = matches[-max_results:]
print_session_header(session_path)
print(f"Found {len(matches)} decision points (showing last {len(show)}):\n")
for line_num, role, snippet in show:
print(f"[{role}] (line {line_num})")
print(snippet)
print("---")
return 0
# --- Session Report/Analysis ---
CORRECTION_PATTERNS = [
re.compile(r"\bno[,.]?\s+(?:that|this|it|you|don't|we|i)", re.I),
re.compile(r"\bthat'?s (?:not|wrong|incorrect)", re.I),
re.compile(r"\bactually[,]\s", re.I),
re.compile(r"\bi (?:said|asked|meant|wanted)\b", re.I),
re.compile(r"\byou should(?:n'?t| not| have)\b", re.I),
re.compile(r"\bwhy did you\b", re.I),
re.compile(r"\bi already (?:told|said|asked)\b", re.I),
re.compile(r"\bnot what i (?:asked|wanted|meant)\b", re.I),
re.compile(r"\bincorrect\b", re.I),
re.compile(r"\bdon'?t (?:do|use|add|change|remove|delete|create)\b", re.I),
re.compile(r"\bstop (?:doing|adding|changing|deleting|creating)\b", re.I),
re.compile(r"\bwrong (?:file|path|approach|way|method|port|url|name)\b", re.I),
# Soft correction patterns: user steering, not just pushback
re.compile(r"\blet'?s (?:use|try|go with|switch to)\b", re.I),
re.compile(r"\bi(?:'d)? prefer\b", re.I),
re.compile(r"\bcan we (?:do|use|try|go with)\b", re.I),
re.compile(r"\bactually,? let'?s\b", re.I),
re.compile(r"\bi'?d rather\b", re.I),
]
SCORE_PATTERN = re.compile(r'\b(\d{1,2})\s*/\s*10\b')
def parse_entries(session_path):
"""Parse all entries into structured dicts for analysis."""
entries = []
with open(session_path, "r", errors="replace") as f:
for line_num, raw_line in enumerate(f):
try:
obj = json.loads(raw_line)
except (json.JSONDecodeError, ValueError):
continue
entry_type = obj.get("type", "")
if entry_type not in ("user", "assistant"):
continue
msg = obj.get("message", {})
role = msg.get("role", entry_type)
content = msg.get("content", "")
entry = {
"line": line_num,
"type": entry_type,
"role": role,
"text": extract_human_text(content),
"full_text": extract_text_from_content(content),
"tool_uses": [],
"tool_results": [],
}
if isinstance(content, list):
for block in content:
if not isinstance(block, dict):
continue
btype = block.get("type", "")
if btype == "tool_use":
entry["tool_uses"].append({
"name": block.get("name", "?"),
"input": block.get("input", {}),
})
elif btype == "tool_result":
rc = block.get("content", "")
rc_text = extract_text_from_content(rc)
if len(rc_text) > 500:
rc_text = rc_text[:500]
entry["tool_results"].append({
"content": rc_text,
"is_error": block.get("is_error", False),
})
entries.append(entry)
return entries
def categorize_error(text):
"""Categorize an error message into a known type."""
t = text.lower()
if "not found" in t or "no such file" in t or "does not exist" in t:
return "FILE_NOT_FOUND"
if "permission denied" in t or "access denied" in t:
return "PERMISSION_DENIED"
if ("edit" in t or "old_string" in t) and ("unique" in t or "not found in" in t or "no match" in t):
return "EDIT_FAILED"
if "hook" in t and ("block" in t or "reject" in t):
return "HOOK_BLOCKED"
if "timeout" in t or "timed out" in t:
return "TIMEOUT"
if "connection refused" in t or "econnrefused" in t or "econnreset" in t:
return "NETWORK_ERROR"
if "syntax" in t or "parse error" in t or "unexpected token" in t or "syntaxerror" in t:
return "SYNTAX_ERROR"
if "too large" in t or "file_too_large" in t:
return "FILE_TOO_LARGE"
if "command failed" in t or "exit code" in t or "non-zero" in t or "exited with" in t:
return "COMMAND_FAILED"
if "import" in t and ("module" in t or "no module" in t or "cannot find" in t):
return "IMPORT_ERROR"
if "already exists" in t or "duplicate" in t:
return "ALREADY_EXISTS"
if "no space" in t or "disk full" in t or "quota" in t:
return "DISK_FULL"
if "killed" in t or "oom" in t or "out of memory" in t:
return "OOM"
return "OTHER"
def rpt_errors(entries):
"""Analyze errors from tool_result blocks."""
errors = []
for entry in entries:
for tr in entry["tool_results"]:
if tr["is_error"]:
cat = categorize_error(tr["content"])
errors.append({"line": entry["line"], "category": cat, "text": tr["content"][:200]})
by_cat = Counter(e["category"] for e in errors)
return {"total": len(errors), "by_category": dict(by_cat), "details": errors}
def rpt_retries(entries):
"""Find retry loops: same tool+similar input called multiple times in proximity.
Also captures error messages from tool_results to explain WHY retries failed.
"""
# Build a map of line -> error text from tool_results
error_by_line = {}
for entry in entries:
for tr in entry["tool_results"]:
if tr["is_error"] and tr["content"]:
error_by_line[entry["line"]] = tr["content"][:150]
calls = []
for entry in entries:
for tu in entry["tool_uses"]:
inp_str = json.dumps(tu["input"], ensure_ascii=False)
fingerprint = f"{tu['name']}:{inp_str[:150]}"
calls.append({"name": tu["name"], "input": inp_str[:200], "fp": fingerprint, "line": entry["line"]})
# Group by fingerprint
groups = defaultdict(list)
for c in calls:
groups[c["fp"]].append(c)
retries = []
for fp, occs in groups.items():
if len(occs) < 3:
continue
# Check proximity: at least 3 occurrences within 80 lines of each other
cluster = [occs[0]]
for occ in occs[1:]:
if occ["line"] - cluster[-1]["line"] < 80:
cluster.append(occ)
else:
if len(cluster) >= 3:
# Find first error near the cluster
err_text = _find_cluster_error(cluster, error_by_line)
retries.append({
"tool": cluster[0]["name"],
"input_preview": cluster[0]["input"][:100],
"count": len(cluster),
"first_line": cluster[0]["line"],
"last_line": cluster[-1]["line"],
"error_text": err_text,
})
cluster = [occ]
if len(cluster) >= 3:
err_text = _find_cluster_error(cluster, error_by_line)
retries.append({
"tool": cluster[0]["name"],
"input_preview": cluster[0]["input"][:100],
"count": len(cluster),
"first_line": cluster[0]["line"],
"last_line": cluster[-1]["line"],
"error_text": err_text,
})
return sorted(retries, key=lambda x: x["count"], reverse=True)
def _find_cluster_error(cluster, error_by_line):
"""Find the first error message near a retry cluster's lines."""
for occ in cluster:
# Error tool_result typically appears 1-3 lines after the tool_use
for offset in range(1, 5):
err = error_by_line.get(occ["line"] + offset)
if err:
return err
return ""
def rpt_corrections(entries):
"""Find user messages that are corrections or pushback."""
corrections = []
for entry in entries:
if entry["role"] != "user":
continue
text = entry["text"]
if not text or len(text.strip()) < 3:
continue
# Skip non-human content (compaction summaries, skill prompts, system reminders)
text_lower = text.lower()
if "continued from a previous conversation" in text_lower:
continue
if "base directory for this skill" in text_lower:
continue
if "<system-reminder>" in text_lower or "system-reminder" in text_lower:
continue
# Skip tool-result-only messages
if not has_human_text(entry.get("_raw_content", text)):
continue
for pat in CORRECTION_PATTERNS:
if pat.search(text):
corrections.append({"line": entry["line"], "text": text[:200].strip()})
break
return corrections
def rpt_scores(entries):
"""Find self-scoring (X/10) and check if user found issues after."""
scores = []
for i, entry in enumerate(entries):
if entry["role"] != "assistant":
continue
text = entry["full_text"]
if not text:
continue
for m in SCORE_PATTERN.finditer(text):
score = int(m.group(1))
if score > 10:
continue
# Check next few user messages for correction signals
correction_after = False
for j in range(i + 1, min(i + 6, len(entries))):
if entries[j]["role"] == "user":
ut = entries[j]["text"].lower()
if any(w in ut for w in ["wrong", "bug", "broken", "not working", "incorrect", "fix", "fail"]):
correction_after = True
break
scores.append({
"line": entry["line"],
"score": score,
"correction_after": correction_after,
"context": truncate_around(text, m.start(), before=50, after=100),
})
return scores
def rpt_tool_usage(entries):
"""Compute tool call counts and error rates per tool."""
calls = Counter()
errs = Counter()
for entry in entries:
for tu in entry["tool_uses"]:
calls[tu["name"]] += 1
# Match errors to their preceding tool_use
for i, entry in enumerate(entries):
if entry["type"] != "user":
continue
for tr in entry["tool_results"]:
if tr["is_error"]:
for j in range(i - 1, max(i - 5, -1), -1):
if entries[j]["type"] == "assistant" and entries[j]["tool_uses"]:
errs[entries[j]["tool_uses"][-1]["name"]] += 1
break
return {"calls": dict(calls), "errors": dict(errs)}
def rpt_compactions(entries):
"""Find context compaction events in the session."""
events = []
for entry in entries:
if entry["role"] != "user":
continue
text = entry["text"]
if text and "continued from a previous conversation" in text.lower():
events.append({"line": entry["line"]})
return events
def generate_lessons(retries, errors, corrections, scores, compactions):
"""Generate actionable CLAUDE.md rules and MEMORY.md entries from analysis.
Rules include specific context from the session (tool names, error text,
line numbers) rather than generic advice.
"""
rules = [] # CLAUDE.md-style rules
memories = [] # MEMORY.md-style facts
# From retries: include the actual command/input AND the error that caused failure
retry_tools = set()
for r in retries:
tool = r["tool"]
if tool not in retry_tools:
retry_tools.add(tool)
preview = r.get("input_preview", "")[:80]
line_info = f"lines {r['first_line']}-{r['last_line']}"
err = r.get("error_text", "")
if err:
# Include the actual error, making the rule actionable
err_short = err.split("\n")[0][:100]
rules.append(
f"{tool} retried {r['count']}x ({line_info}): `{preview}`. "
f"Error: {err_short}. Fix the root cause instead of retrying."
)
else:
rules.append(
f"{tool} command retried {r['count']}x ({line_info}): `{preview}`. "
f"After 2 failures with {tool}, switch approach instead of retrying."
)
# From errors: include specific error examples
cats = errors.get("by_category", {})
error_details = errors.get("details", [])
if cats.get("FILE_NOT_FOUND", 0) >= 2:
examples = [e["text"][:80] for e in error_details if e["category"] == "FILE_NOT_FOUND"][:2]
rules.append("Use Glob to verify file paths before reading. FILE_NOT_FOUND errors ({} total): {}".format(
cats["FILE_NOT_FOUND"], "; ".join(examples) if examples else "multiple paths missing"))
if cats.get("EDIT_FAILED", 0) >= 2:
rules.append("Re-read files immediately before editing. {} EDIT_FAILED errors in this session.".format(cats["EDIT_FAILED"]))
if cats.get("HOOK_BLOCKED", 0) >= 1:
examples = [e["text"][:80] for e in error_details if e["category"] == "HOOK_BLOCKED"][:1]
rules.append("Hook blocks are deterministic. Blocked: {}. Never retry, use a different approach.".format(
examples[0] if examples else "command blocked by hook"))
if cats.get("FILE_TOO_LARGE", 0) >= 1:
rules.append("Use offset/limit or Grep for large files. {} FILE_TOO_LARGE errors.".format(cats["FILE_TOO_LARGE"]))
if cats.get("TIMEOUT", 0) >= 1:
rules.append("{} TIMEOUT errors. Use timeout for long-running commands, investigate root cause.".format(cats["TIMEOUT"]))
# From corrections: include the actual correction text
if len(corrections) >= 3:
correction_summaries = [c["text"][:60] for c in corrections[:3]]
rules.append("Multiple user corrections ({}): {}. Read instructions more carefully before acting.".format(
len(corrections), "; ".join(correction_summaries)))
if len(corrections) >= 1:
for c in corrections[:3]:
text = c["text"][:150]
memories.append(f"User correction (line {c['line']}): {text}")
# From scores: include the actual score context
inflated = [s for s in scores if s["correction_after"]]
if inflated:
score_details = ", ".join(
f"{s['score']}/10 at line {s['line']}" for s in inflated[:3]
)
rules.append(f"List flaws BEFORE scoring. User found issues after self-scores of {score_details}. Adversarial review before claiming done.")
# From compactions
if len(compactions) >= 2:
rules.append("Session had {} compactions. Use workplan files as external brain. Re-read the plan after each compaction.".format(
len(compactions)))
return rules, memories
# --- Deep analysis via Gemini ---
GEMINI_MODEL = os.environ.get("GEMINI_MODEL", "gemini-3-flash-preview")
_GEMINI_KEY_FILE = Path.home() / ".config" / "session-recall" / "gemini-key"
def _get_gemini_key():
"""Resolve Gemini API key: env var > config file > error."""
key = os.environ.get("GEMINI_API_KEY", "")
if key:
return key
if _GEMINI_KEY_FILE.exists():
key = _GEMINI_KEY_FILE.read_text().strip()
if key:
return key
return ""
def build_deep_context(entries, retries, errors, corrections, scores, compactions):
"""Build a structured context string for Gemini analysis."""
parts = []
# Conversation flow: extract human/assistant text messages (no tool noise)
parts.append("=== CONVERSATION FLOW (human/assistant messages only) ===")
msg_count = 0
for entry in entries:
text = entry["text"].strip()
if not text:
continue
role = entry["role"]
# Truncate long messages but keep corrections in full
is_correction = any(c["line"] == entry["line"] for c in corrections)
max_len = 500 if is_correction else 250
display = text[:max_len] + "..." if len(text) > max_len else text
parts.append(f"[{role}] (line {entry['line']}): {display}")
msg_count += 1
if msg_count > 200:
parts.append(f"... ({len(entries)} total entries, truncated)")
break
# Retry loops
if retries:
parts.append("\n=== RETRY LOOPS ===")
for r in retries:
parts.append(f"- {r['tool']} x{r['count']} (lines {r['first_line']}-{r['last_line']}): {r['input_preview'][:150]}")
# Errors
if errors["total"] > 0:
parts.append(f"\n=== ERRORS ({errors['total']}) ===")
for cat, count in sorted(errors["by_category"].items(), key=lambda x: x[1], reverse=True):
parts.append(f"- {cat}: {count}")
parts.append("Details:")
for e in errors["details"][:10]:
parts.append(f" Line {e['line']} [{e['category']}]: {e['text'][:150]}")
# User corrections (FULL text, these are the gold signal)
if corrections:
parts.append("\n=== USER CORRECTIONS (critical - these are the most important signals) ===")
for c in corrections:
parts.append(f"- Line {c['line']}: {c['text']}")
# Self-scoring
inflated = [s for s in scores if s["correction_after"]]
if inflated:
parts.append("\n=== INFLATED SELF-SCORES ===")
for s in inflated:
parts.append(f"- {s['score']}/10 at line {s['line']}: {s['context'][:200]}")
# Compactions
if compactions:
parts.append(f"\n=== COMPACTION EVENTS ({len(compactions)}) ===")
parts.append(f"Lines: {', '.join(str(e['line']) for e in compactions)}")
return "\n".join(parts)
def call_gemini(prompt, context):
"""Call Gemini API and return the text response."""
import urllib.request
import urllib.error
api_key = _get_gemini_key()
if not api_key:
return (f"No Gemini API key found. Set GEMINI_API_KEY env var or write key to {_GEMINI_KEY_FILE}")
url = f"https://generativelanguage.googleapis.com/v1beta/models/{GEMINI_MODEL}:generateContent?key={api_key}"
full_prompt = f"{prompt}\n\n---\n\nSESSION DATA:\n{context}"
payload = json.dumps({
"contents": [{"parts": [{"text": full_prompt}]}],
"generationConfig": {"temperature": 0.3, "maxOutputTokens": 4000},
}).encode("utf-8")
req = urllib.request.Request(url, data=payload, headers={"Content-Type": "application/json"})
try:
with urllib.request.urlopen(req, timeout=120) as resp:
data = json.loads(resp.read().decode("utf-8"))
candidates = data.get("candidates", [])
if candidates:
parts = candidates[0].get("content", {}).get("parts", [])
if parts:
return parts[0].get("text", "")
return "No response from Gemini."
except urllib.error.HTTPError as e:
body = e.read().decode("utf-8", errors="replace")[:500]
return f"Gemini API error {e.code}: {body}"
except Exception as e:
return f"Gemini API error: {e}"
DEEP_PROMPT = """You are analyzing a Claude Code session transcript to extract project-specific lessons.
Your job is to find SPECIFIC, ACTIONABLE insights about THIS project and THIS session. NOT generic advice.
BAD (generic): "Don't retry commands too many times"
GOOD (specific): "When ElevenLabs TTS API returns 429, wait 30s before retrying. The agent wasted 20 calls without backoff."
BAD (generic): "Read instructions carefully"
GOOD (specific): "User wants video narration synced with visual transitions. Agent ignored timing requirements and focused only on content."
Analyze the session data below and produce:
1. **PROJECT CONTEXT** (1-2 sentences): What was this session about? What project/task?
2. **CLAUDE.MD RULES** (3-7 rules): Specific behavioral rules for THIS project's CLAUDE.md.
- Each rule must reference a concrete event from the session
- Rules must be imperative ("Always X", "Never Y", "When Z happens, do W")
- Rules must be specific enough that a different agent reading them would avoid the same mistake
3. **MEMORY ENTRIES** (2-5 entries): Facts to remember for future sessions on this project.
- User preferences discovered in this session
- Technical decisions made
- Pitfalls encountered and their solutions
- Things the user corrected
4. **BIGGEST TIME WASTER**: The single largest waste of time in this session and how to prevent it.
Output in plain text, no markdown headers, just numbered sections. Be concise and direct."""
DEEP_PROMPT_JSON = """You are analyzing a Claude Code session transcript to extract project-specific lessons.
Your job is to find SPECIFIC, ACTIONABLE insights about THIS project and THIS session. NOT generic advice.
BAD (generic): "Don't retry commands too many times"
GOOD (specific): "When ElevenLabs TTS API returns 429, wait 30s before retrying. The agent wasted 20 calls without backoff."
Analyze the session data below and return ONLY valid JSON with this structure:
{
"project_context": "1-2 sentence description of what this session was about",
"rules": [
"Imperative rule 1 referencing a concrete event",
"Imperative rule 2..."
],
"memories": [
"Fact or preference to remember 1",
"Fact or preference to remember 2..."
],
"biggest_time_waster": "Description of the biggest time waste and how to prevent it"
}
Rules (3-7): Must be imperative ("Always X", "Never Y"), specific to THIS project.
Memories (2-5): User preferences, technical decisions, pitfalls, corrections.
Return ONLY the JSON object, no markdown fences, no commentary."""
# --- --apply: HITL review + append to CLAUDE.md / MEMORY.md ---
def _find_claude_md():
"""Find CLAUDE.md in current directory or parents."""
cwd = Path.cwd()
for d in [cwd] + list(cwd.parents):
candidate = d / "CLAUDE.md"
if candidate.exists():
return candidate
# Stop at home or root
if d == Path.home() or d == Path("/"):
break
# Default: create in cwd
return cwd / "CLAUDE.md"
def _find_memory_md():
"""Find the auto-memory MEMORY.md for the current project."""
# Claude Code stores per-project memory in ~/.claude/projects/<hash>/memory/MEMORY.md
projects_dir = Path.home() / ".claude" / "projects"
if not projects_dir.is_dir():
return None
# Walk up cwd path trying each parent: /root/session-recall -> try -root-session-recall, then -root
cwd = Path.cwd()
candidates = [cwd] + list(cwd.parents)
try:
existing_dirs = {d.name: d for d in projects_dir.iterdir() if d.is_dir()}
except PermissionError:
return None
for path in candidates:
encoded = str(path).replace("/", "-")
if encoded in existing_dirs:
mem_dir = existing_dirs[encoded] / "memory"
mem_dir.mkdir(exist_ok=True)
return mem_dir / "MEMORY.md"
# Stop at root
if path == Path("/"):
break
# Fallback: most recently modified project dir
try:
project_dirs = sorted(projects_dir.iterdir(), key=lambda p: p.stat().st_mtime, reverse=True)
except (PermissionError, OSError):
return None
for d in project_dirs:
if d.is_dir() and not d.name.startswith("."):
mem_dir = d / "memory"
if mem_dir.exists():
return mem_dir / "MEMORY.md"
return None
def _hitl_review(items, item_type):
"""Interactive review of items. Returns list of approved items."""
if not items:
return []
approved = []
print(f"\n{'=' * 50}")
print(f"REVIEW {item_type.upper()} ({len(items)} items)")
print(f"{'=' * 50}")
print(f" [y] approve [n] skip [e] edit [a] approve all [q] quit\n")
for i, item in enumerate(items):
print(f" ({i + 1}/{len(items)}) {item}")
while True:
try:
choice = input(" > ").strip().lower()
except (EOFError, KeyboardInterrupt):
print()
return approved
if choice in ("y", "yes", ""):
approved.append(item)
break
elif choice in ("n", "no", "s", "skip"):
break
elif choice in ("e", "edit"):
try:
edited = input(" new text> ").strip()
except (EOFError, KeyboardInterrupt):
print()
return approved
if edited:
approved.append(edited)
break
elif choice in ("a", "all"):
approved.append(item)
approved.extend(items[i + 1:])
print(f" Approved all remaining ({len(items) - i} items)")
return approved
elif choice in ("q", "quit"):
return approved
else:
print(" [y/n/e/a/q]?")
print()
return approved
def _append_to_file(filepath, section_header, items):
"""Append items under a section header in a file.
Tracks code fence state (``` toggles) so that section headers
appearing inside fenced code blocks are not matched.
Uses flock to prevent concurrent writes from corrupting the file.
"""
import fcntl
filepath = Path(filepath)
# Acquire exclusive lock for the duration of read-modify-write
lock_path = filepath.parent / f".{filepath.name}.lock"
lock_fd = None
try:
lock_path.parent.mkdir(parents=True, exist_ok=True)
lock_fd = open(lock_path, "w")
fcntl.flock(lock_fd, fcntl.LOCK_EX)
except OSError:
pass # Best effort: proceed without lock if it fails
try:
existing = filepath.read_text() if filepath.exists() else ""
_append_to_file_inner(filepath, section_header, items, existing)
finally:
if lock_fd:
try:
fcntl.flock(lock_fd, fcntl.LOCK_UN)
lock_fd.close()
lock_path.unlink(missing_ok=True)
except OSError:
pass
def _append_to_file_inner(filepath, section_header, items, existing):
# Check if section already exists (outside code fences)
lines = existing.split("\n")
in_fence = False
section_line = None
for i, line in enumerate(lines):
stripped = line.strip()