elliot-stack 1.0.18 → 1.0.20

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 (50) 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 +203 -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 +423 -0
  7. package/skills/estack-read-claude-session-history/references/recipes.md +271 -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 +1776 -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/engagement-gaps.jsonl +9 -0
  20. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/engagement-noise.jsonl +7 -0
  21. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/engagement-parallel-a.jsonl +3 -0
  22. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/engagement-parallel-b.jsonl +3 -0
  23. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/engagement-waiting.jsonl +5 -0
  24. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/interrupted.jsonl +2 -0
  25. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/multi-compact.jsonl +8 -0
  26. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/pending-user.jsonl +2 -0
  27. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-no-meta/subagents/agent-aaa.jsonl +2 -0
  28. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-no-meta.jsonl +2 -0
  29. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-parent/subagents/agent-xyz123.jsonl +2 -0
  30. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-parent/subagents/agent-xyz123.meta.json +1 -0
  31. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/subagent-parent.jsonl +4 -0
  32. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/time-spread.jsonl +6 -0
  33. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/timeline-day-test.jsonl +5 -0
  34. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/tool-zoo.jsonl +10 -0
  35. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/truncated.jsonl +3 -0
  36. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/unicode.jsonl +2 -0
  37. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/with-advisor.jsonl +3 -0
  38. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/with-compact.jsonl +5 -0
  39. package/skills/estack-read-claude-session-history/scripts/tests/fixtures/with-thinking.jsonl +2 -0
  40. package/skills/estack-read-claude-session-history/scripts/tests/test_backup_roots.py +56 -0
  41. package/skills/estack-read-claude-session-history/scripts/tests/test_engagement.py +239 -0
  42. package/skills/estack-read-claude-session-history/scripts/tests/test_json_format.py +201 -0
  43. package/skills/estack-read-claude-session-history/scripts/tests/test_modes.py +199 -0
  44. package/skills/estack-read-claude-session-history/scripts/tests/test_parser.py +195 -0
  45. package/skills/estack-read-claude-session-history/scripts/tests/test_paths.py +133 -0
  46. package/skills/estack-read-claude-session-history/scripts/tests/test_search.py +78 -0
  47. package/skills/estack-read-claude-session-history/scripts/tests/test_subagents.py +43 -0
  48. package/skills/estack-read-claude-session-history/scripts/tests/test_timeline.py +179 -0
  49. package/skills/estack-read-claude-session-history/scripts/tests/test_timezone_and_project.py +212 -0
  50. package/skills/estack-read-claude-session-history/scripts/tests/test_tools.py +80 -0
@@ -0,0 +1,40 @@
1
+ """Shared pytest fixtures and import-path setup."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import sys
6
+ from pathlib import Path
7
+
8
+ import pytest
9
+
10
+
11
+ # Ensure UTF-8 output regardless of console code page (Windows)
12
+ if sys.stdout.encoding and sys.stdout.encoding.lower() != "utf-8":
13
+ try:
14
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
15
+ except (AttributeError, OSError):
16
+ pass
17
+
18
+
19
+ THIS_DIR = Path(__file__).resolve().parent
20
+ SCRIPTS_DIR = THIS_DIR.parent
21
+ FIXTURES_DIR = THIS_DIR / "fixtures"
22
+
23
+ # Make `from lib...` work in tests
24
+ if str(SCRIPTS_DIR) not in sys.path:
25
+ sys.path.insert(0, str(SCRIPTS_DIR))
26
+
27
+
28
+ @pytest.fixture
29
+ def fixtures_dir() -> Path:
30
+ return FIXTURES_DIR
31
+
32
+
33
+ @pytest.fixture
34
+ def scripts_dir() -> Path:
35
+ return SCRIPTS_DIR
36
+
37
+
38
+ @pytest.fixture
39
+ def cli_path() -> Path:
40
+ return SCRIPTS_DIR / "read_transcript.py"
@@ -0,0 +1,20 @@
1
+ # Test fixtures
2
+
3
+ Hand-crafted minimal JSONL files, one scenario per file. Keep them small (≤50 lines) — they're easier to reason about than real session snapshots and don't carry PII.
4
+
5
+ | File | Purpose |
6
+ |---|---|
7
+ | `basic-session.jsonl` | One user + one assistant exchange. Sanity check for the parser. |
8
+ | `with-compact.jsonl` | Conversation interrupted by a single `/compact` marker. |
9
+ | `multi-compact.jsonl` | Two `/compact` markers — exercises "most recent" logic. |
10
+ | `with-advisor.jsonl` | Contains an `advisor_tool_result` block. |
11
+ | `with-thinking.jsonl` | Contains a `thinking` block plus normal text. |
12
+ | `all-noise.jsonl` | Only `ai-title` + `attachment` entries — should look empty to signal queries. |
13
+ | `subagent-parent.jsonl` | Parent session that spawns one subagent via the `Agent` tool. |
14
+ | `subagent-no-meta.jsonl` | Parent session with a sibling subagent file but no `.meta.json` sidecar — `load_meta` must fall back. |
15
+ | `tool-zoo.jsonl` | One call to each of Bash, Read, Edit, Write, Agent, Skill, Glob, Grep. |
16
+ | `time-spread.jsonl` | Six messages over a known time range — exercises `--since`/`--until`. |
17
+ | `truncated.jsonl` | Final line is missing its newline AND is malformed JSON — should be dropped silently. |
18
+ | `unicode.jsonl` | Contains emoji + CJK characters — exercises UTF-8 decoding. |
19
+ | `pending-user.jsonl` | Last assistant message ends with `?` — `infer_status` should return `pending-user`. |
20
+ | `interrupted.jsonl` | Final assistant message has a `tool_use` block with no matching `tool_result` — status `interrupted`. |
@@ -0,0 +1,4 @@
1
+ {"type":"ai-title","aiTitle":"Test session"}
2
+ {"type":"attachment","data":{"path":"foo.png"}}
3
+ {"type":"permission-mode","mode":"plan"}
4
+ {"type":"file-history-snapshot","content":{"file":"bar.py"}}
@@ -0,0 +1,2 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Hello Claude"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"Hi there. Here is help."}]}}
@@ -0,0 +1,9 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Start the analysis"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:01:00Z","message":{"role":"assistant","content":[{"type":"text","text":"On it."}]}}
3
+ {"type":"user","timestamp":"2026-05-01T10:05:00Z","message":{"role":"user","content":"Looks good, continue"}}
4
+ {"type":"assistant","timestamp":"2026-05-01T10:06:00Z","message":{"role":"assistant","content":[{"type":"text","text":"Done with phase 1."}]}}
5
+ {"type":"user","timestamp":"2026-05-01T10:08:00Z","message":{"role":"user","content":"Next phase"}}
6
+ {"type":"assistant","timestamp":"2026-05-01T10:09:00Z","message":{"role":"assistant","content":[{"type":"text","text":"Phase 2 complete."}]}}
7
+ {"type":"user","timestamp":"2026-05-01T10:40:00Z","message":{"role":"user","content":"Back from a break, keep going"}}
8
+ {"type":"assistant","timestamp":"2026-05-01T10:41:00Z","message":{"role":"assistant","content":[{"type":"text","text":"Resumed."}]}}
9
+ {"type":"user","timestamp":"2026-05-01T10:45:00Z","message":{"role":"user","content":"Wrap it up"}}
@@ -0,0 +1,7 @@
1
+ {"type":"user","timestamp":"2026-05-01T09:00:00Z","isMeta":true,"message":{"role":"user","content":[{"type":"text","text":"SessionStart hook output — not a human action"}]}}
2
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"This session is being continued from a previous conversation. Summary: we were doing things."}}
3
+ {"type":"user","timestamp":"2026-05-01T10:01:00Z","message":{"role":"user","content":"Real prompt one"}}
4
+ {"type":"assistant","timestamp":"2026-05-01T10:02:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Read","input":{"file_path":"x"},"id":"t9"}]}}
5
+ {"type":"user","timestamp":"2026-05-01T10:03:00Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"t9","content":"file contents"}]}}
6
+ {"type":"user","timestamp":"2026-05-01T10:05:00Z","isMeta":true,"message":{"role":"user","content":[{"type":"text","text":"Skill expansion injected text"}]}}
7
+ {"type":"user","timestamp":"2026-05-01T10:06:00Z","message":{"role":"user","content":"Real prompt two"}}
@@ -0,0 +1,3 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Chat A start"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:01:00Z","message":{"role":"assistant","content":[{"type":"text","text":"Working in A."}]}}
3
+ {"type":"user","timestamp":"2026-05-01T10:30:00Z","message":{"role":"user","content":"Chat A follow-up"}}
@@ -0,0 +1,3 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:10:00Z","message":{"role":"user","content":"Chat B start"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:11:00Z","message":{"role":"assistant","content":[{"type":"text","text":"Working in B."}]}}
3
+ {"type":"user","timestamp":"2026-05-01T10:20:00Z","message":{"role":"user","content":"Chat B follow-up"}}
@@ -0,0 +1,5 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Run the long migration"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:02:00Z","message":{"role":"assistant","content":[{"type":"tool_use","name":"Bash","input":{"command":"migrate"},"id":"t1"}]}}
3
+ {"type":"user","timestamp":"2026-05-01T10:15:00Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"t1","content":"step 1 done"}]}}
4
+ {"type":"assistant","timestamp":"2026-05-01T10:30:00Z","message":{"role":"assistant","content":[{"type":"text","text":"Migration finished."}]}}
5
+ {"type":"user","timestamp":"2026-05-01T10:32:00Z","message":{"role":"user","content":"Great, now verify it"}}
@@ -0,0 +1,2 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Run a tool"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"Running."},{"type":"tool_use","id":"toolu_999","name":"Bash","input":{"command":"sleep 999"}}]}}
@@ -0,0 +1,8 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"First"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"R1"}]}}
3
+ {"type":"user","timestamp":"2026-05-01T10:30:00Z","message":{"role":"user","content":"This session is being continued from a previous conversation. Round 1"}}
4
+ {"type":"user","timestamp":"2026-05-01T11:00:00Z","message":{"role":"user","content":"Mid"}}
5
+ {"type":"assistant","timestamp":"2026-05-01T11:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"R2"}]}}
6
+ {"type":"user","timestamp":"2026-05-01T11:30:00Z","message":{"role":"user","content":"This session is being continued from a previous conversation. Round 2"}}
7
+ {"type":"user","timestamp":"2026-05-01T12:00:00Z","message":{"role":"user","content":"Last"}}
8
+ {"type":"assistant","timestamp":"2026-05-01T12:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"R3"}]}}
@@ -0,0 +1,2 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Hello"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"Sure, do you want me to A, B, or C?"}]}}
@@ -0,0 +1,2 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:10Z","message":{"role":"user","content":"Do thing"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:20Z","message":{"role":"assistant","content":[{"type":"text","text":"Done."}]}}
@@ -0,0 +1,2 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Run an agent"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"Agent ran."}]}}
@@ -0,0 +1,2 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:10Z","message":{"role":"user","content":"Find the bug in foo.py"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:20Z","message":{"role":"assistant","content":[{"type":"text","text":"Found it: foo.py line 42 — off-by-one in loop bound."}]}}
@@ -0,0 +1 @@
1
+ {"agentType": "Explore", "description": "Find the bug in foo.py"}
@@ -0,0 +1,4 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Investigate the bug"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"toolu_01abc","name":"Agent","input":{"description":"Find the bug","prompt":"Look at file X","subagent_type":"Explore"}}]}}
3
+ {"type":"user","timestamp":"2026-05-01T10:00:30Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_01abc","content":"Found it in foo.py:42"}]}}
4
+ {"type":"assistant","timestamp":"2026-05-01T10:00:35Z","message":{"role":"assistant","content":[{"type":"text","text":"Got the report, patching foo.py."}]}}
@@ -0,0 +1,6 @@
1
+ {"type":"user","timestamp":"2026-04-15T08:00:00Z","message":{"role":"user","content":"Apr 15 morning"}}
2
+ {"type":"assistant","timestamp":"2026-04-15T08:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"Apr 15 reply"}]}}
3
+ {"type":"user","timestamp":"2026-05-01T12:00:00Z","message":{"role":"user","content":"May 1 noon"}}
4
+ {"type":"assistant","timestamp":"2026-05-01T12:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"May 1 reply"}]}}
5
+ {"type":"user","timestamp":"2026-05-15T20:00:00Z","message":{"role":"user","content":"May 15 evening"}}
6
+ {"type":"assistant","timestamp":"2026-05-15T20:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"May 15 reply"}]}}
@@ -0,0 +1,5 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Start the CRM cleanup"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:05:00Z","message":{"role":"assistant","content":[{"type":"text","text":"Working on it."}]}}
3
+ {"type":"assistant","timestamp":"2026-05-01T10:08:00Z","message":{"role":"assistant","content":[{"type":"text","text":"First pass done."}]}}
4
+ {"type":"user","timestamp":"2026-05-01T12:00:00Z","message":{"role":"user","content":"Back — continue"}}
5
+ {"type":"assistant","timestamp":"2026-05-01T12:02:00Z","message":{"role":"assistant","content":[{"type":"text","text":"Resumed and finished."}]}}
@@ -0,0 +1,10 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Run all the tools"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:01Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t1","name":"Bash","input":{"command":"ls -la /tmp"}}]}}
3
+ {"type":"assistant","timestamp":"2026-05-01T10:00:02Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t2","name":"PowerShell","input":{"command":"Get-ChildItem"}}]}}
4
+ {"type":"assistant","timestamp":"2026-05-01T10:00:03Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t3","name":"Read","input":{"file_path":"C:\\foo.py","offset":1,"limit":50}}]}}
5
+ {"type":"assistant","timestamp":"2026-05-01T10:00:04Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t4","name":"Edit","input":{"file_path":"C:\\foo.py","old_string":"a","new_string":"b"}}]}}
6
+ {"type":"assistant","timestamp":"2026-05-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t5","name":"Write","input":{"file_path":"C:\\bar.py","content":"print('hi')"}}]}}
7
+ {"type":"assistant","timestamp":"2026-05-01T10:00:06Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t6","name":"Agent","input":{"description":"Find X","subagent_type":"Explore"}}]}}
8
+ {"type":"assistant","timestamp":"2026-05-01T10:00:07Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t7","name":"Skill","input":{"skill":"using-superpowers"}}]}}
9
+ {"type":"assistant","timestamp":"2026-05-01T10:00:08Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t8","name":"Glob","input":{"pattern":"**/*.py"}}]}}
10
+ {"type":"assistant","timestamp":"2026-05-01T10:00:09Z","message":{"role":"assistant","content":[{"type":"tool_use","id":"t9","name":"Grep","input":{"pattern":"TODO"}}]}}
@@ -0,0 +1,3 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Valid line"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"Valid reply"}]}}
3
+ {"type":"user","timestamp":"2026-05-01T10:01:00Z","message":{"role":"user","content":"Trunc
@@ -0,0 +1,2 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Hello 🌍, 你好"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"Привет — 안녕하세요!"}]}}
@@ -0,0 +1,3 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Get advice"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"Calling advisor..."},{"type":"advisor_tool_result","content":{"text":"The advisor says do X then Y."}}]}}
3
+ {"type":"assistant","timestamp":"2026-05-01T10:00:10Z","message":{"role":"assistant","content":[{"type":"text","text":"OK, doing X."}]}}
@@ -0,0 +1,5 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"First question"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"text","text":"First answer"}]}}
3
+ {"type":"user","timestamp":"2026-05-01T11:00:00Z","message":{"role":"user","content":"This session is being continued from a previous conversation. Summary follows..."}}
4
+ {"type":"user","timestamp":"2026-05-01T11:00:10Z","message":{"role":"user","content":"After compact"}}
5
+ {"type":"assistant","timestamp":"2026-05-01T11:00:15Z","message":{"role":"assistant","content":[{"type":"text","text":"Post-compact reply"}]}}
@@ -0,0 +1,2 @@
1
+ {"type":"user","timestamp":"2026-05-01T10:00:00Z","message":{"role":"user","content":"Solve this"}}
2
+ {"type":"assistant","timestamp":"2026-05-01T10:00:05Z","message":{"role":"assistant","content":[{"type":"thinking","thinking":"Let me think step by step..."},{"type":"text","text":"The answer is 42."}]}}
@@ -0,0 +1,56 @@
1
+ """Tests for backup-root resolution (--root mirror|snapshot-*|<abs-path>)."""
2
+
3
+ from pathlib import Path
4
+
5
+ import pytest
6
+
7
+ from lib import paths as P
8
+
9
+
10
+ def test_root_live_default():
11
+ assert P.resolve_root(None) == P.DEFAULT_LIVE_PROJECTS
12
+ assert P.resolve_root("live") == P.DEFAULT_LIVE_PROJECTS
13
+
14
+
15
+ def test_root_mirror_path_shape():
16
+ r = P.resolve_root("mirror")
17
+ parts = r.parts
18
+ assert ".claude-backups" in parts
19
+ assert "mirror" in parts
20
+ assert parts[-1] == "projects"
21
+
22
+
23
+ def test_root_snapshot_24h_path_shape():
24
+ r = P.resolve_root("snapshot-24h")
25
+ assert "snapshot-24h" in r.parts
26
+ assert r.name == "projects"
27
+
28
+
29
+ def test_root_all_known():
30
+ for name in ("mirror", "snapshot-24h", "snapshot-1w", "snapshot-1mo"):
31
+ r = P.resolve_root(name)
32
+ assert name in r.parts
33
+
34
+
35
+ def test_root_absolute_path(tmp_path: Path):
36
+ fake = tmp_path / "weird-root"
37
+ fake.mkdir()
38
+ assert P.resolve_root(str(fake)) == fake
39
+
40
+
41
+ def test_root_unknown_relative_raises():
42
+ with pytest.raises(ValueError):
43
+ P.resolve_root("bogus")
44
+
45
+
46
+ def test_find_project_dir_uses_root(tmp_path: Path):
47
+ # Build a fake root with a fake project dir
48
+ proj = tmp_path / "C--Users-foo-bar"
49
+ proj.mkdir()
50
+ found = P.find_project_dir("C:\\Users\\foo\\bar", root=tmp_path)
51
+ assert found == proj
52
+
53
+
54
+ def test_find_project_dir_not_found(tmp_path: Path):
55
+ with pytest.raises(FileNotFoundError):
56
+ P.find_project_dir("C:\\does\\not\\exist", root=tmp_path)
@@ -0,0 +1,239 @@
1
+ """Tests for the engagement mode (attention-time accounting)."""
2
+
3
+ import json
4
+ import os
5
+ import shutil
6
+ import subprocess
7
+ import sys
8
+ from datetime import datetime, timedelta
9
+
10
+ import pytest
11
+
12
+ import read_transcript as RT
13
+ from lib import parser as PR
14
+
15
+
16
+ def _run_cli(cli_path, *args, env_overrides=None):
17
+ env = dict(os.environ)
18
+ env["PYTHONIOENCODING"] = "utf-8"
19
+ if env_overrides:
20
+ env.update(env_overrides)
21
+ return subprocess.run(
22
+ [sys.executable, str(cli_path), *args],
23
+ capture_output=True,
24
+ text=True,
25
+ encoding="utf-8",
26
+ env=env,
27
+ )
28
+
29
+
30
+ @pytest.fixture
31
+ def utc_tz():
32
+ PR.set_timezone("UTC")
33
+ yield
34
+ PR.set_timezone(None)
35
+
36
+
37
+ def _fake_root(tmp_path, **projects):
38
+ """Build a fake projects root: {project_dir_name: [(fixture, uuid), ...]}."""
39
+ root = tmp_path / "projects"
40
+ for proj_name, files in projects.items():
41
+ pd = root / proj_name
42
+ pd.mkdir(parents=True)
43
+ for src, uuid in files:
44
+ shutil.copy(src, pd / f"{uuid}.jsonl")
45
+ return root
46
+
47
+
48
+ def _build(root, **kw):
49
+ defaults = dict(
50
+ report_dirs=None,
51
+ report_file=None,
52
+ since=datetime(2026, 5, 1),
53
+ until=datetime(2026, 5, 2),
54
+ break_minutes=10,
55
+ current_uuid=None,
56
+ )
57
+ defaults.update(kw)
58
+ return RT.build_engagement(root, **defaults)
59
+
60
+
61
+ # ── unit: gap math ───────────────────────────────────────────────────────────
62
+
63
+ def test_gaps_straddle_threshold(fixtures_dir, tmp_path, utc_tz):
64
+ root = _fake_root(
65
+ tmp_path, **{"C--proj-a": [(fixtures_dir / "engagement-gaps.jsonl", "aaaa1111")]}
66
+ )
67
+ data = _build(root)
68
+ (s,) = data["sessions"].values()
69
+ # 5m + 3m active, 32m break (Claude finished 10:09, reply 10:40 — too late),
70
+ # then 5m active = 13m.
71
+ assert s["active"] == timedelta(minutes=13)
72
+ assert s["user_messages"] == 5
73
+ assert len(data["breaks"]) == 1
74
+ assert data["breaks"][0][0] == datetime(2026, 5, 1, 10, 8)
75
+ assert data["breaks"][0][1] == datetime(2026, 5, 1, 10, 40)
76
+
77
+
78
+ def test_waiting_on_claude_credit(fixtures_dir, tmp_path, utc_tz):
79
+ root = _fake_root(
80
+ tmp_path, **{"C--proj-a": [(fixtures_dir / "engagement-waiting.jsonl", "bbbb2222")]}
81
+ )
82
+ data = _build(root)
83
+ (s,) = data["sessions"].values()
84
+ # 32m gap, but Claude's last event was 10:30 and the user replied 10:32
85
+ # (2m ≤ 10m) — the whole gap counts as waiting-on-Claude.
86
+ assert s["active"] == timedelta(minutes=32)
87
+ assert data["breaks"] == []
88
+
89
+
90
+ def test_noise_exclusion(fixtures_dir, tmp_path, utc_tz):
91
+ root = _fake_root(
92
+ tmp_path, **{"C--proj-a": [(fixtures_dir / "engagement-noise.jsonl", "cccc3333")]}
93
+ )
94
+ data = _build(root)
95
+ (s,) = data["sessions"].values()
96
+ # Only the two real prompts (10:01, 10:06) count: compact continuation,
97
+ # isMeta injections, and tool results are all excluded from the user stream.
98
+ assert s["user_messages"] == 2
99
+ assert s["first"] == datetime(2026, 5, 1, 10, 1)
100
+ assert s["active"] == timedelta(minutes=5)
101
+
102
+
103
+ def test_parallel_sessions_never_double_count(fixtures_dir, tmp_path, utc_tz):
104
+ root = _fake_root(
105
+ tmp_path,
106
+ **{
107
+ "C--proj-a": [(fixtures_dir / "engagement-parallel-a.jsonl", "aaaa1111")],
108
+ "C--proj-b": [(fixtures_dir / "engagement-parallel-b.jsonl", "bbbb2222")],
109
+ },
110
+ )
111
+ data = _build(root)
112
+ by_uuid = {s["summary"]["uuid"]: s for s in data["sessions"].values()}
113
+ # Stream: 10:00 A, 10:10 B, 10:20 B, 10:30 A. Each segment goes to the
114
+ # session of the LATER prompt: B gets 10:00–10:20 (20m), A gets 10:20–10:30
115
+ # (10m). Total 30m == wall clock, not the naive 40m.
116
+ assert by_uuid["bbbb2222"]["active"] == timedelta(minutes=20)
117
+ assert by_uuid["aaaa1111"]["active"] == timedelta(minutes=10)
118
+ total = sum((s["active"] for s in data["sessions"].values()), timedelta())
119
+ assert total == timedelta(minutes=30)
120
+
121
+
122
+ def test_single_message_session(tmp_path, utc_tz):
123
+ root = tmp_path / "projects"
124
+ pd = root / "C--proj-a"
125
+ pd.mkdir(parents=True)
126
+ (pd / "dddd4444.jsonl").write_text(
127
+ '{"type":"user","timestamp":"2026-05-01T10:00:00Z",'
128
+ '"message":{"role":"user","content":"one and done"}}\n',
129
+ encoding="utf-8",
130
+ )
131
+ data = _build(root)
132
+ (s,) = data["sessions"].values()
133
+ assert s["active"] == timedelta(0)
134
+ assert s["user_messages"] == 1
135
+
136
+
137
+ def test_report_scope_filters_but_stream_stays_global(fixtures_dir, tmp_path, utc_tz):
138
+ root = _fake_root(
139
+ tmp_path,
140
+ **{
141
+ "C--proj-a": [(fixtures_dir / "engagement-parallel-a.jsonl", "aaaa1111")],
142
+ "C--proj-b": [(fixtures_dir / "engagement-parallel-b.jsonl", "bbbb2222")],
143
+ },
144
+ )
145
+ data = _build(root, report_dirs=[root / "C--proj-a"])
146
+ # Only A is reported, but B's prompts still split the stream — A gets its
147
+ # interval-correct 10m, not a naive 30m.
148
+ (s,) = data["sessions"].values()
149
+ assert s["summary"]["uuid"] == "aaaa1111"
150
+ assert s["active"] == timedelta(minutes=10)
151
+
152
+
153
+ # ── CLI ──────────────────────────────────────────────────────────────────────
154
+
155
+ def test_engagement_cli_text(cli_path, fixtures_dir, tmp_path):
156
+ root = _fake_root(
157
+ tmp_path, **{"C--proj-a": [(fixtures_dir / "engagement-gaps.jsonl", "aaaa1111")]}
158
+ )
159
+ r = _run_cli(
160
+ cli_path, "--root", str(root), "--tz", "UTC",
161
+ "--mode", "engagement", "--date", "2026-05-01",
162
+ )
163
+ assert r.returncode == 0
164
+ assert "13m" in r.stdout
165
+ assert "break=10m" in r.stdout
166
+ assert "Breaks >10m" in r.stdout
167
+
168
+
169
+ def test_engagement_cli_json(cli_path, fixtures_dir, tmp_path):
170
+ root = _fake_root(
171
+ tmp_path, **{"C--proj-a": [(fixtures_dir / "engagement-gaps.jsonl", "aaaa1111")]}
172
+ )
173
+ r = _run_cli(
174
+ cli_path, "--root", str(root), "--tz", "UTC",
175
+ "--mode", "engagement", "--date", "2026-05-01", "--format", "json",
176
+ )
177
+ assert r.returncode == 0
178
+ data = json.loads(r.stdout)
179
+ assert data["break_minutes"] == 10
180
+ assert data["totals"]["active_minutes"] == 13
181
+ assert data["totals"]["sessions"] == 1
182
+ s = data["sessions"][0]
183
+ assert s["uuid"] == "aaaa1111"
184
+ assert s["elapsed_minutes"] == 45
185
+ assert s["ratio"] == round(13 / 45, 2)
186
+ assert len(data["stream_breaks"]) == 1
187
+ assert data["stream_breaks"][0]["minutes"] == 32
188
+
189
+
190
+ def test_engagement_cli_file_window_derived(cli_path, fixtures_dir, tmp_path):
191
+ root = _fake_root(
192
+ tmp_path, **{"C--proj-a": [(fixtures_dir / "engagement-waiting.jsonl", "bbbb2222")]}
193
+ )
194
+ f = root / "C--proj-a" / "bbbb2222.jsonl"
195
+ r = _run_cli(
196
+ cli_path, "--root", str(root), "--tz", "UTC",
197
+ "--mode", "engagement", "--file", str(f),
198
+ )
199
+ assert r.returncode == 0
200
+ assert "32m" in r.stdout
201
+ assert "Prompt gaps" in r.stdout
202
+
203
+
204
+ def test_engagement_cli_custom_break(cli_path, fixtures_dir, tmp_path):
205
+ root = _fake_root(
206
+ tmp_path, **{"C--proj-a": [(fixtures_dir / "engagement-gaps.jsonl", "aaaa1111")]}
207
+ )
208
+ r = _run_cli(
209
+ cli_path, "--root", str(root), "--tz", "UTC",
210
+ "--mode", "engagement", "--date", "2026-05-01",
211
+ "--break", "1h", "--format", "json",
212
+ )
213
+ assert r.returncode == 0
214
+ data = json.loads(r.stdout)
215
+ # 1h threshold swallows the 32m gap — everything is active: 45m.
216
+ assert data["totals"]["active_minutes"] == 45
217
+ assert data["stream_breaks"] == []
218
+
219
+
220
+ def test_engagement_cli_invalid_break(cli_path, tmp_path):
221
+ root = tmp_path / "projects"
222
+ root.mkdir()
223
+ r = _run_cli(
224
+ cli_path, "--root", str(root),
225
+ "--mode", "engagement", "--break", "soon",
226
+ )
227
+ assert r.returncode == 1
228
+
229
+
230
+ def test_engagement_cli_empty_range(cli_path, fixtures_dir, tmp_path):
231
+ root = _fake_root(
232
+ tmp_path, **{"C--proj-a": [(fixtures_dir / "engagement-gaps.jsonl", "aaaa1111")]}
233
+ )
234
+ r = _run_cli(
235
+ cli_path, "--root", str(root), "--tz", "UTC",
236
+ "--mode", "engagement", "--date", "2020-01-01",
237
+ )
238
+ assert r.returncode == 0
239
+ assert "no user messages" in r.stdout