Coverage for little_loops / session_log.py: 24%
46 statements
« prev ^ index » next coverage.py v7.12.0, created at 2026-03-18 16:18 -0500
« prev ^ index » next coverage.py v7.12.0, created at 2026-03-18 16:18 -0500
1"""Session log linking for issue files.
3Links Claude Code JSONL session files to issue files by appending
4session log entries with command name, timestamp, and file path.
5"""
7from __future__ import annotations
9import re
10from datetime import UTC, datetime
11from pathlib import Path
13from little_loops.user_messages import get_project_folder
15# Regex to isolate the ## Session Log section content
16_SESSION_LOG_SECTION_RE = re.compile(
17 r"^## Session Log\s*\n+(.*?)(?:\n##|\n---|\Z)", re.MULTILINE | re.DOTALL
18)
19# Regex to extract backtick-quoted /ll:* command names from session log entries
20_COMMAND_RE = re.compile(r"`(/[\w:-]+)`")
23def parse_session_log(content: str) -> list[str]:
24 """Extract distinct /ll:* command names from the ## Session Log section.
26 Returns commands in first-seen order, deduplicated (preserves insertion order).
28 Args:
29 content: Full text of an issue markdown file.
31 Returns:
32 List of distinct command names (e.g. ["/ll:refine-issue", "/ll:ready-issue"]).
33 """
34 matches = list(_SESSION_LOG_SECTION_RE.finditer(content))
35 if not matches:
36 return []
37 cmds = _COMMAND_RE.findall(matches[-1].group(1))
38 # Deduplicate while preserving insertion order
39 return list(dict.fromkeys(cmds))
42def count_session_commands(content: str) -> dict[str, int]:
43 """Count occurrences of each /ll:* command in the ## Session Log section.
45 Unlike parse_session_log(), this does NOT deduplicate — each entry is counted.
47 Args:
48 content: Full text of an issue markdown file.
50 Returns:
51 Mapping of command name to occurrence count (e.g. {"/ll:refine-issue": 3}).
52 """
53 matches = list(_SESSION_LOG_SECTION_RE.finditer(content))
54 if not matches:
55 return {}
56 counts: dict[str, int] = {}
57 for cmd in _COMMAND_RE.findall(matches[-1].group(1)):
58 counts[cmd] = counts.get(cmd, 0) + 1
59 return counts
62def get_current_session_jsonl(cwd: Path | None = None) -> Path | None:
63 """Resolve the active Claude Code session's JSONL file path.
65 Finds the most recently modified .jsonl file in the project's
66 Claude Code session directory, excluding agent session files.
68 Args:
69 cwd: Working directory to map. If None, uses current directory.
71 Returns:
72 Path to the most recent JSONL file, or None if not found.
73 """
74 project_folder = get_project_folder(cwd)
75 if project_folder is None:
76 return None
78 jsonl_files = [f for f in project_folder.glob("*.jsonl") if not f.name.startswith("agent-")]
79 if not jsonl_files:
80 return None
82 return max(jsonl_files, key=lambda f: f.stat().st_mtime)
85def append_session_log_entry(
86 issue_path: Path,
87 command: str,
88 session_jsonl: Path | None = None,
89) -> bool:
90 """Append a session log entry to an issue file.
92 Creates or appends to the ``## Session Log`` section with command name,
93 ISO timestamp, and absolute path to the session JSONL file.
95 Args:
96 issue_path: Path to the issue markdown file.
97 command: Command name (e.g., ``/ll:manage-issue``).
98 session_jsonl: Path to session JSONL file. If None, auto-detected.
100 Returns:
101 True if entry was appended, False if session could not be resolved.
102 """
103 if session_jsonl is None:
104 session_jsonl = get_current_session_jsonl()
105 if session_jsonl is None:
106 return False
108 timestamp = datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%S")
109 entry = f"- `{command}` - {timestamp} - `{session_jsonl}`"
111 content = issue_path.read_text()
113 if "## Session Log" in content:
114 # Insert entry after the last ## Session Log header (real section, not a fake in code block)
115 idx = content.rfind("## Session Log\n")
116 insert_pos = idx + len("## Session Log\n")
117 content = content[:insert_pos] + entry + "\n" + content[insert_pos:]
118 else:
119 # Add new section before --- Status footer if present, else at end
120 if "\n---\n\n## Status" in content:
121 content = content.replace(
122 "\n---\n\n## Status",
123 f"\n## Session Log\n{entry}\n\n---\n\n## Status",
124 )
125 else:
126 content += f"\n\n## Session Log\n{entry}\n"
128 issue_path.write_text(content)
129 return True