elliot-stack 1.0.18 → 1.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +11 -0
  2. package/bin/install.cjs +134 -49
  3. package/package.json +1 -1
  4. package/skills/estack-read-claude-session-history/SKILL.md +196 -0
  5. package/skills/estack-read-claude-session-history/references/jsonl-schema.md +126 -0
  6. package/skills/estack-read-claude-session-history/references/modes.md +366 -0
  7. package/skills/estack-read-claude-session-history/references/recipes.md +237 -0
  8. package/skills/estack-read-claude-session-history/scripts/lib/__init__.py +1 -0
  9. package/skills/estack-read-claude-session-history/scripts/lib/parser.py +460 -0
  10. package/skills/estack-read-claude-session-history/scripts/lib/paths.py +234 -0
  11. package/skills/estack-read-claude-session-history/scripts/lib/search.py +179 -0
  12. package/skills/estack-read-claude-session-history/scripts/lib/subagents.py +88 -0
  13. package/skills/estack-read-claude-session-history/scripts/lib/tools.py +144 -0
  14. package/skills/estack-read-claude-session-history/scripts/read_transcript.py +1448 -0
  15. package/skills/estack-read-claude-session-history/scripts/tests/conftest.py +40 -0
  16. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/README.md +20 -0
  17. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/all-noise.jsonl +4 -0
  18. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/basic-session.jsonl +2 -0
  19. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/interrupted.jsonl +2 -0
  20. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/multi-compact.jsonl +8 -0
  21. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/pending-user.jsonl +2 -0
  22. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-no-meta/subagents/agent-aaa.jsonl +2 -0
  23. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-no-meta.jsonl +2 -0
  24. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-parent/subagents/agent-xyz123.jsonl +2 -0
  25. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-parent/subagents/agent-xyz123.meta.json +1 -0
  26. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-parent.jsonl +4 -0
  27. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/time-spread.jsonl +6 -0
  28. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/timeline-day-test.jsonl +5 -0
  29. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/tool-zoo.jsonl +10 -0
  30. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/truncated.jsonl +3 -0
  31. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/unicode.jsonl +2 -0
  32. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/with-advisor.jsonl +3 -0
  33. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/with-compact.jsonl +5 -0
  34. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/with-thinking.jsonl +2 -0
  35. package/skills/estack-read-claude-session-history/scripts/tests/test_backup_roots.py +56 -0
  36. package/skills/estack-read-claude-session-history/scripts/tests/test_json_format.py +201 -0
  37. package/skills/estack-read-claude-session-history/scripts/tests/test_modes.py +199 -0
  38. package/skills/estack-read-claude-session-history/scripts/tests/test_parser.py +195 -0
  39. package/skills/estack-read-claude-session-history/scripts/tests/test_paths.py +133 -0
  40. package/skills/estack-read-claude-session-history/scripts/tests/test_search.py +78 -0
  41. package/skills/estack-read-claude-session-history/scripts/tests/test_subagents.py +43 -0
  42. package/skills/estack-read-claude-session-history/scripts/tests/test_timeline.py +175 -0
  43. package/skills/estack-read-claude-session-history/scripts/tests/test_timezone_and_project.py +212 -0
  44. package/skills/estack-read-claude-session-history/scripts/tests/test_tools.py +80 -0
@@ -0,0 +1,460 @@
1
+ """JSONL parsing primitives, message classification, and session summaries."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import re
7
+ import sys
8
+ from datetime import datetime, timedelta, timezone
9
+ from pathlib import Path
10
+ from typing import Iterator, Literal
11
+
12
+
13
+ NOISE_TYPES: set[str] = {
14
+ "permission-mode", "ai-title", "custom-title", "attachment",
15
+ "last-prompt", "queue-operation", "file-history-snapshot",
16
+ "system", "agent-name", "pr-link",
17
+ }
18
+
19
+ COMPACT_MARKER = "This session is being continued from a previous conversation"
20
+
21
+ # 5 MB — beyond this, dump mode auto-degrades unless --force-dump.
22
+ LARGE_FILE_THRESHOLD = 5 * 1024 * 1024
23
+
24
+ EntryType = Literal["user", "assistant", "title", "noise", "compact"]
25
+
26
+ _PARSE_CACHE: dict[Path, tuple[float, list[dict]]] = {}
27
+
28
+
29
+ def iter_lines(path: Path) -> Iterator[dict]:
30
+ """Yield parsed JSON objects from a .jsonl file, streaming.
31
+
32
+ A truncated (un-newline-terminated) trailing line is dropped silently with
33
+ a stderr note. Malformed JSON lines are also dropped silently.
34
+ """
35
+ truncated = False
36
+ try:
37
+ with open(path, encoding="utf-8") as f:
38
+ for line in f:
39
+ stripped = line.strip()
40
+ if not stripped:
41
+ continue
42
+ if not line.endswith("\n"):
43
+ # Last line, no terminator — could be partial. Try to parse,
44
+ # but if it fails, treat as truncation.
45
+ try:
46
+ yield json.loads(stripped)
47
+ except json.JSONDecodeError:
48
+ truncated = True
49
+ continue
50
+ try:
51
+ yield json.loads(stripped)
52
+ except json.JSONDecodeError:
53
+ continue
54
+ finally:
55
+ if truncated:
56
+ print(
57
+ f"[note: dropped truncated trailing line in {path.name}]",
58
+ file=sys.stderr,
59
+ )
60
+
61
+
62
+ def parse_lines(path: Path) -> list[dict]:
63
+ """Read all JSONL records from a file, with mtime-based caching."""
64
+ try:
65
+ mtime = path.stat().st_mtime
66
+ except OSError:
67
+ return list(iter_lines(path))
68
+ cached = _PARSE_CACHE.get(path)
69
+ if cached is not None and cached[0] == mtime:
70
+ return cached[1]
71
+ records = list(iter_lines(path))
72
+ _PARSE_CACHE[path] = (mtime, records)
73
+ return records
74
+
75
+
76
+ def extract_text_blocks(
77
+ content,
78
+ include_thinking: bool = False,
79
+ include_tool_use: bool = False,
80
+ ) -> list[str]:
81
+ """Pull human-readable text from a content field (string or block list)."""
82
+ if isinstance(content, str):
83
+ return [content] if content.strip() else []
84
+ if not isinstance(content, list):
85
+ return []
86
+ texts: list[str] = []
87
+ for block in content:
88
+ if not isinstance(block, dict):
89
+ continue
90
+ t = block.get("type")
91
+ if t == "text" and block.get("text", "").strip():
92
+ texts.append(block["text"])
93
+ elif t == "advisor_tool_result":
94
+ inner = block.get("content", {})
95
+ if isinstance(inner, dict) and inner.get("text"):
96
+ texts.append(f"[ADVISOR]\n{inner['text']}")
97
+ elif t == "thinking" and include_thinking:
98
+ think = block.get("thinking", "") or block.get("text", "")
99
+ if think.strip():
100
+ texts.append(f"[THINKING]\n{think}")
101
+ elif t == "tool_use" and include_tool_use:
102
+ name = block.get("name", "?")
103
+ tool_input = block.get("input", {})
104
+ try:
105
+ preview = json.dumps(tool_input)[:200]
106
+ except (TypeError, ValueError):
107
+ preview = str(tool_input)[:200]
108
+ texts.append(f"[TOOL_USE {name}] {preview}")
109
+ return texts
110
+
111
+
112
+ def is_compact_marker(text: str) -> bool:
113
+ return bool(text) and COMPACT_MARKER in text
114
+
115
+
116
+ def classify_entry(obj: dict) -> EntryType:
117
+ """Single source of truth for entry-type classification."""
118
+ t = obj.get("type", "")
119
+ if t == "ai-title" or t == "custom-title":
120
+ return "title"
121
+ if t in NOISE_TYPES:
122
+ return "noise"
123
+ msg = obj.get("message", {})
124
+ if not msg:
125
+ return "noise"
126
+ role = msg.get("role")
127
+ if role == "user":
128
+ content = msg.get("content", "")
129
+ text = (
130
+ content if isinstance(content, str)
131
+ else " ".join(
132
+ b.get("text", "") for b in content
133
+ if isinstance(b, dict) and b.get("type") == "text"
134
+ )
135
+ )
136
+ if is_compact_marker(text):
137
+ return "compact"
138
+ return "user"
139
+ if role == "assistant":
140
+ return "assistant"
141
+ return "noise"
142
+
143
+
144
+ def get_messages(lines: list[dict]) -> list[dict]:
145
+ """Filter to signal messages, returning {role, texts, line_index, is_compact, timestamp}."""
146
+ messages: list[dict] = []
147
+ for i, obj in enumerate(lines):
148
+ cls = classify_entry(obj)
149
+ if cls in ("noise", "title"):
150
+ continue
151
+ msg = obj.get("message", {})
152
+ if not msg:
153
+ continue
154
+ content = msg.get("content", "")
155
+ texts = extract_text_blocks(content)
156
+ timestamp = obj.get("timestamp")
157
+ messages.append({
158
+ "role": "user" if cls in ("user", "compact") else "assistant",
159
+ "texts": texts,
160
+ "line_index": i,
161
+ "is_compact": cls == "compact",
162
+ "timestamp": timestamp,
163
+ })
164
+ return messages
165
+
166
+
167
+ def filter_by_role(
168
+ messages: list[dict], role: Literal["user", "assistant", "both"]
169
+ ) -> list[dict]:
170
+ if role == "both":
171
+ return messages
172
+ return [m for m in messages if m["role"] == role]
173
+
174
+
175
+ # Display timezone. None → system local time. Set via set_timezone() (--tz flag).
176
+ # JSONL timestamps are UTC; every parsed timestamp is converted to this zone so
177
+ # all displayed times match the user's wall clock and compare cleanly against
178
+ # parse_timespec() values (which are local).
179
+ _TARGET_TZ: timezone | None = None
180
+
181
+ _TZ_OFFSET_RE = re.compile(r"^([+-])(\d{1,2})(?::?(\d{2}))?$")
182
+
183
+
184
+ def set_timezone(spec: str | None) -> None:
185
+ """Set the display timezone from a --tz spec.
186
+
187
+ Accepts:
188
+ - None / "local" → system local time (default)
189
+ - "UTC" → UTC
190
+ - fixed offsets → "+5", "-4", "+05:30", "UTC-4"
191
+ - IANA names → "America/New_York" (via zoneinfo)
192
+ """
193
+ global _TARGET_TZ
194
+ if not spec or spec.strip().lower() == "local":
195
+ _TARGET_TZ = None
196
+ return
197
+ s = spec.strip()
198
+ if s.upper().startswith("UTC"):
199
+ rest = s[3:].strip()
200
+ if not rest:
201
+ _TARGET_TZ = timezone.utc
202
+ return
203
+ s = rest # "UTC-4" → "-4"
204
+ m = _TZ_OFFSET_RE.match(s)
205
+ if m:
206
+ sign = 1 if m.group(1) == "+" else -1
207
+ hours = int(m.group(2))
208
+ mins = int(m.group(3) or 0)
209
+ _TARGET_TZ = timezone(sign * timedelta(hours=hours, minutes=mins))
210
+ return
211
+ try:
212
+ from zoneinfo import ZoneInfo
213
+ _TARGET_TZ = ZoneInfo(spec.strip())
214
+ except Exception as e:
215
+ raise ValueError(
216
+ f"Unrecognized timezone: {spec!r}. "
217
+ "Use an IANA name (America/New_York), 'UTC', or an offset (+5, -4, +05:30)."
218
+ ) from e
219
+
220
+
221
+ def to_display(dt: datetime) -> datetime:
222
+ """Convert an aware datetime to the display timezone, returned naive."""
223
+ return dt.astimezone(_TARGET_TZ).replace(tzinfo=None)
224
+
225
+
226
+ def epoch_to_display(epoch: float) -> datetime:
227
+ """Convert an epoch (e.g. st_mtime) to the display timezone, returned naive."""
228
+ return to_display(datetime.fromtimestamp(epoch, tz=timezone.utc))
229
+
230
+
231
+ def display_to_epoch(dt: datetime) -> float:
232
+ """Interpret a naive display-timezone datetime as an epoch.
233
+
234
+ Inverse of epoch_to_display. Needed because naive_dt.timestamp() assumes
235
+ *local* time, which is wrong under a --tz override.
236
+ """
237
+ if dt.tzinfo is None and _TARGET_TZ is not None:
238
+ dt = dt.replace(tzinfo=_TARGET_TZ)
239
+ return dt.timestamp()
240
+
241
+
242
+ def now_display() -> datetime:
243
+ """Current time as a naive datetime in the display timezone."""
244
+ import time as _time
245
+ return epoch_to_display(_time.time())
246
+
247
+
248
+ def _parse_timestamp(ts) -> datetime | None:
249
+ """Parse a JSONL timestamp → naive datetime in the display timezone."""
250
+ if not ts:
251
+ return None
252
+ if isinstance(ts, (int, float)):
253
+ try:
254
+ return epoch_to_display(float(ts))
255
+ except (ValueError, OSError, OverflowError):
256
+ return None
257
+ if isinstance(ts, str):
258
+ # ISO 8601 with possible Z
259
+ s = ts.replace("Z", "+00:00")
260
+ try:
261
+ dt = datetime.fromisoformat(s)
262
+ except ValueError:
263
+ return None
264
+ if dt.tzinfo is not None:
265
+ return to_display(dt)
266
+ return dt # naive — assume already local
267
+ return None
268
+
269
+
270
+ def filter_by_time(
271
+ messages: list[dict],
272
+ since: datetime | None,
273
+ until: datetime | None,
274
+ ) -> list[dict]:
275
+ if since is None and until is None:
276
+ return messages
277
+ out = []
278
+ for m in messages:
279
+ ts = _parse_timestamp(m.get("timestamp"))
280
+ if ts is None:
281
+ continue
282
+ # Strip tzinfo for naive comparison
283
+ if ts.tzinfo is not None:
284
+ ts = ts.replace(tzinfo=None)
285
+ if since is not None and ts < since:
286
+ continue
287
+ if until is not None and ts > until:
288
+ continue
289
+ out.append(m)
290
+ return out
291
+
292
+
293
+ def _truncate(s: str, n: int) -> str:
294
+ if not s:
295
+ return ""
296
+ s = s.replace("\n", " ").strip()
297
+ return s if len(s) <= n else s[: n - 1] + "…"
298
+
299
+
300
+ def infer_status(
301
+ lines: list[dict],
302
+ mtime: float,
303
+ current_session_id: str | None,
304
+ session_uuid: str | None,
305
+ ) -> Literal["clean", "interrupted", "pending-user", "active"]:
306
+ """Heuristic session status from the shape of the final entry."""
307
+ now = datetime.now().timestamp()
308
+ if (
309
+ current_session_id
310
+ and session_uuid
311
+ and current_session_id == session_uuid
312
+ and now - mtime < 300
313
+ ):
314
+ return "active"
315
+
316
+ if not lines:
317
+ return "clean"
318
+
319
+ # Walk backwards through non-noise entries
320
+ last_assistant = None
321
+ has_dangling_tool_use = False
322
+ pending_tool_use_ids: set[str] = set()
323
+ tool_result_ids: set[str] = set()
324
+ for obj in lines:
325
+ msg = obj.get("message", {})
326
+ if not isinstance(msg, dict):
327
+ continue
328
+ content = msg.get("content")
329
+ if not isinstance(content, list):
330
+ continue
331
+ for block in content:
332
+ if not isinstance(block, dict):
333
+ continue
334
+ bt = block.get("type")
335
+ if bt == "tool_use":
336
+ tid = block.get("id")
337
+ if tid:
338
+ pending_tool_use_ids.add(tid)
339
+ elif bt == "tool_result":
340
+ tid = block.get("tool_use_id")
341
+ if tid:
342
+ tool_result_ids.add(tid)
343
+
344
+ dangling = pending_tool_use_ids - tool_result_ids
345
+ if dangling:
346
+ has_dangling_tool_use = True
347
+
348
+ # Find the last assistant message
349
+ for obj in reversed(lines):
350
+ msg = obj.get("message", {})
351
+ if msg.get("role") == "assistant":
352
+ last_assistant = msg
353
+ break
354
+
355
+ if has_dangling_tool_use:
356
+ return "interrupted"
357
+
358
+ if last_assistant is not None:
359
+ content = last_assistant.get("content", "")
360
+ text = (
361
+ content if isinstance(content, str)
362
+ else " ".join(
363
+ b.get("text", "") for b in content
364
+ if isinstance(b, dict) and b.get("type") == "text"
365
+ )
366
+ )
367
+ if text.strip().endswith("?"):
368
+ return "pending-user"
369
+
370
+ return "clean"
371
+
372
+
373
+ def session_summary(path: Path, current_session_id: str | None = None) -> dict:
374
+ """Compact per-session metrics for brief / list / journal / count modes."""
375
+ from .tools import extract_tool_calls, files_touched # local import to avoid cycle
376
+ from .paths import decode_project_name, list_subagents
377
+ from .subagents import load_meta
378
+
379
+ try:
380
+ stat = path.stat()
381
+ except OSError:
382
+ return {
383
+ "path": path,
384
+ "uuid": path.stem,
385
+ "mtime": 0,
386
+ "size": 0,
387
+ "exists": False,
388
+ }
389
+
390
+ lines = parse_lines(path)
391
+ messages = get_messages(lines)
392
+ user_msgs = [m for m in messages if m["role"] == "user" and not m["is_compact"]]
393
+ assistant_msgs = [m for m in messages if m["role"] == "assistant"]
394
+
395
+ # Title
396
+ title = ""
397
+ for obj in lines:
398
+ if obj.get("type") in ("ai-title", "custom-title"):
399
+ title = obj.get("aiTitle") or obj.get("customTitle") or ""
400
+ if title:
401
+ break
402
+
403
+ first_prompt = ""
404
+ if user_msgs and user_msgs[0]["texts"]:
405
+ first_prompt = _truncate(user_msgs[0]["texts"][0], 200)
406
+
407
+ last_assistant = ""
408
+ if assistant_msgs and assistant_msgs[-1]["texts"]:
409
+ last_assistant = _truncate(assistant_msgs[-1]["texts"][-1], 200)
410
+
411
+ last_activity = epoch_to_display(stat.st_mtime).strftime("%Y-%m-%d %H:%M")
412
+
413
+ tool_calls = extract_tool_calls(lines)
414
+ tool_counts: dict[str, int] = {}
415
+ for tc in tool_calls:
416
+ tool_counts[tc["name"]] = tool_counts.get(tc["name"], 0) + 1
417
+
418
+ files = files_touched(lines)
419
+ edit_count = len(files)
420
+
421
+ subagents = list_subagents(path)
422
+ subagent_types: dict[str, int] = {}
423
+ for sa in subagents:
424
+ meta = load_meta(sa)
425
+ atype = meta.get("agentType", "unknown")
426
+ subagent_types[atype] = subagent_types.get(atype, 0) + 1
427
+
428
+ has_compact = any(m["is_compact"] for m in messages)
429
+ parent_dir_name = path.parent.name
430
+ decoded = decode_project_name(parent_dir_name)
431
+
432
+ status = infer_status(
433
+ lines, stat.st_mtime, current_session_id, path.stem
434
+ )
435
+
436
+ return {
437
+ "path": path,
438
+ "uuid": path.stem,
439
+ "mtime": stat.st_mtime,
440
+ "size": stat.st_size,
441
+ "exists": True,
442
+ "title": title,
443
+ "first_prompt": first_prompt,
444
+ "last_assistant": last_assistant,
445
+ "last_activity": last_activity,
446
+ "msg_count": len(messages),
447
+ "edit_count": edit_count,
448
+ "tool_counts": tool_counts,
449
+ "files_touched": list(files.keys()),
450
+ "subagent_count": len(subagents),
451
+ "subagent_types": subagent_types,
452
+ "has_compact": has_compact,
453
+ "has_subagents": bool(subagents),
454
+ "cwd": parent_dir_name,
455
+ "decoded_project": decoded,
456
+ "status": status,
457
+ "is_current": bool(
458
+ current_session_id and current_session_id == path.stem
459
+ ),
460
+ }
@@ -0,0 +1,234 @@
1
+ """Path resolution, project discovery, and time-spec parsing."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import re
7
+ from datetime import datetime, timedelta
8
+ from pathlib import Path
9
+
10
+
11
+ CLAUDE_DIR = Path.home() / ".claude"
12
+ DEFAULT_LIVE_PROJECTS = CLAUDE_DIR / "projects"
13
+ DEFAULT_BACKUPS_DIR = Path.home() / ".claude-backups"
14
+
15
+ KNOWN_ROOTS = {"live", "mirror", "snapshot-24h", "snapshot-1w", "snapshot-1mo"}
16
+
17
+
18
+ def encode_cwd(cwd: str) -> str:
19
+ """Convert an absolute path to the Claude project directory name.
20
+
21
+ Replaces colons, backslashes, forward slashes, and whitespace with hyphens.
22
+ Verified against the 34 real project dirs on this machine — no other chars
23
+ appear in encoded names.
24
+ """
25
+ return re.sub(r"[:\\/\s]", "-", cwd)
26
+
27
+
28
+ def decode_project_name(encoded: str) -> str:
29
+ """Best-effort reverse for display.
30
+
31
+ Strips the `C--Users-<user>-` drive/home prefix when present, replaces
32
+ remaining hyphens with spaces, and joins path-like segments with " > ".
33
+
34
+ Falls back to the raw encoded name if the heuristic fails. Display only —
35
+ never use this to look up a real directory.
36
+ """
37
+ if not encoded:
38
+ return encoded
39
+
40
+ # Strip leading drive prefix `C--Users-<name>-`
41
+ m = re.match(r"^([A-Z])--Users-([^-]+)-(.+)$", encoded)
42
+ if m:
43
+ remainder = m.group(3)
44
+ else:
45
+ remainder = encoded
46
+
47
+ # Heuristic: every run of single hyphens is a path separator. The encoder
48
+ # mapped one `-` per separator char, so a single `-` in the original path
49
+ # is impossible to recover. We split on single `-` between word characters
50
+ # and treat the result as path segments. Multiple consecutive hyphens
51
+ # indicate the original had spaces+hyphens fused together — collapse to one.
52
+ # In practice this gives readable output like "Other Claude Code > Personal Brand Project".
53
+ cleaned = re.sub(r"-{2,}", "-", remainder)
54
+ # Words are likely separated by hyphens; segments by capitalized starts.
55
+ # Simple approach: just replace hyphens with spaces.
56
+ return cleaned.replace("-", " ").strip() or encoded
57
+
58
+
59
+ def current_session_id() -> str | None:
60
+ """Return the current Claude Code session UUID from CLAUDE_SESSION_ID env var.
61
+
62
+ Returns None when called outside a Claude Code session.
63
+ """
64
+ val = os.environ.get("CLAUDE_SESSION_ID", "").strip()
65
+ return val or None
66
+
67
+
68
+ def resolve_root(name: str | None) -> Path:
69
+ """Resolve a root name to its absolute projects directory.
70
+
71
+ - "live" (default, None) -> ~/.claude/projects
72
+ - "mirror" -> ~/.claude-backups/mirror/projects
73
+ - "snapshot-24h" -> ~/.claude-backups/snapshot-24h/projects
74
+ - "snapshot-1w" / "snapshot-1mo" -> analogous
75
+ - <absolute path> -> passes through unchanged
76
+ """
77
+ if not name or name == "live":
78
+ return DEFAULT_LIVE_PROJECTS
79
+ if name in KNOWN_ROOTS:
80
+ return DEFAULT_BACKUPS_DIR / name / "projects"
81
+ p = Path(name)
82
+ if p.is_absolute():
83
+ return p
84
+ raise ValueError(
85
+ f"Unknown root: {name!r}. Expected one of {sorted(KNOWN_ROOTS)} or an absolute path."
86
+ )
87
+
88
+
89
+ def find_project_dir(cwd: str, root: Path | None = None) -> Path:
90
+ """Resolve a project directory under the given root.
91
+
92
+ Tries exact encoded match first, falls back to case-insensitive substring.
93
+ """
94
+ if root is None:
95
+ root = DEFAULT_LIVE_PROJECTS
96
+ encoded = encode_cwd(cwd)
97
+ candidate = root / encoded
98
+ if candidate.exists():
99
+ return candidate
100
+ if root.exists():
101
+ matches = [
102
+ d for d in root.iterdir()
103
+ if d.is_dir() and encoded.lower() in d.name.lower()
104
+ ]
105
+ if matches:
106
+ return matches[0]
107
+ raise FileNotFoundError(
108
+ f"No project directory found for cwd: {cwd}\nExpected: {candidate}"
109
+ )
110
+
111
+
112
+ def list_projects(root: Path | None = None) -> list[Path]:
113
+ """All encoded-cwd dirs under the given root."""
114
+ if root is None:
115
+ root = DEFAULT_LIVE_PROJECTS
116
+ if not root.exists():
117
+ return []
118
+ return sorted([d for d in root.iterdir() if d.is_dir()], key=lambda d: d.name)
119
+
120
+
121
+ def filter_projects(root: Path | None, name: str) -> list[Path]:
122
+ """Project dirs whose encoded or decoded name contains `name` (case-insensitive).
123
+
124
+ Matches against both forms so `--project "Keel Project"`, `--project
125
+ Keel-Project`, and `--project keel` all hit the same directory.
126
+ """
127
+ q = name.strip().lower()
128
+ q_encoded = q.replace(" ", "-")
129
+ out = []
130
+ for d in list_projects(root):
131
+ dname = d.name.lower()
132
+ decoded = decode_project_name(d.name).lower()
133
+ if q in dname or q_encoded in dname or q in decoded:
134
+ out.append(d)
135
+ return out
136
+
137
+
138
+ def list_transcripts(
139
+ project_dir: Path,
140
+ since: datetime | None = None,
141
+ until: datetime | None = None,
142
+ ) -> list[Path]:
143
+ """Return .jsonl files in the project dir, newest first.
144
+
145
+ Excludes subagent transcripts (files starting with `agent-`).
146
+ """
147
+ if not project_dir.exists():
148
+ return []
149
+ files = [f for f in project_dir.glob("*.jsonl") if not f.name.startswith("agent-")]
150
+ # display_to_epoch (not .timestamp()) — naive bounds are in the display
151
+ # timezone, which differs from local under a --tz override.
152
+ from . import parser as _parser
153
+ if since is not None:
154
+ since_ts = _parser.display_to_epoch(since)
155
+ files = [f for f in files if f.stat().st_mtime >= since_ts]
156
+ if until is not None:
157
+ until_ts = _parser.display_to_epoch(until)
158
+ files = [f for f in files if f.stat().st_mtime <= until_ts]
159
+ files.sort(key=lambda f: f.stat().st_mtime, reverse=True)
160
+ return files
161
+
162
+
163
+ def list_subagents(session_file: Path) -> list[Path]:
164
+ """Return subagent transcript files for a given parent session."""
165
+ uuid = session_file.stem
166
+ subagent_dir = session_file.parent / uuid / "subagents"
167
+ if not subagent_dir.exists():
168
+ return []
169
+ return sorted(
170
+ subagent_dir.glob("agent-*.jsonl"),
171
+ key=lambda f: f.stat().st_mtime,
172
+ reverse=True,
173
+ )
174
+
175
+
176
+ _RELATIVE_RE = re.compile(r"^(\d+)\s*(m|h|d|w|mo)$", re.IGNORECASE)
177
+
178
+
179
+ def parse_timespec(s: str) -> datetime:
180
+ """Parse a time spec into a naive datetime in the display timezone
181
+ (system local time unless --tz overrides it).
182
+
183
+ Accepts:
184
+ - ISO date: "2026-05-01"
185
+ - ISO datetime: "2026-05-01T14:30" or "2026-05-01 14:30"
186
+ - Relative: "30m", "24h", "7d", "1w", "1mo"
187
+ - Named: "today", "yesterday", "now"
188
+ """
189
+ if not s:
190
+ raise ValueError("Empty time spec")
191
+ s = s.strip()
192
+ lower = s.lower()
193
+ # "now" in the display timezone (== datetime.now() unless --tz is set),
194
+ # so that named/relative specs stay consistent with displayed times.
195
+ from . import parser as _parser
196
+ now = _parser.now_display()
197
+ if lower == "now":
198
+ return now
199
+ if lower == "today":
200
+ return now.replace(hour=0, minute=0, second=0, microsecond=0)
201
+ if lower == "yesterday":
202
+ return (now - timedelta(days=1)).replace(
203
+ hour=0, minute=0, second=0, microsecond=0
204
+ )
205
+ m = _RELATIVE_RE.match(s)
206
+ if m:
207
+ n = int(m.group(1))
208
+ unit = m.group(2).lower()
209
+ if unit == "m":
210
+ return now - timedelta(minutes=n)
211
+ if unit == "h":
212
+ return now - timedelta(hours=n)
213
+ if unit == "d":
214
+ return now - timedelta(days=n)
215
+ if unit == "w":
216
+ return now - timedelta(weeks=n)
217
+ if unit == "mo":
218
+ return now - timedelta(days=30 * n)
219
+ # ISO formats
220
+ for fmt in (
221
+ "%Y-%m-%dT%H:%M:%S",
222
+ "%Y-%m-%dT%H:%M",
223
+ "%Y-%m-%d %H:%M:%S",
224
+ "%Y-%m-%d %H:%M",
225
+ "%Y-%m-%d",
226
+ ):
227
+ try:
228
+ return datetime.strptime(s, fmt)
229
+ except ValueError:
230
+ continue
231
+ try:
232
+ return datetime.fromisoformat(s)
233
+ except ValueError as e:
234
+ raise ValueError(f"Unrecognized time spec: {s!r}") from e