Coverage for little_loops/session_log.py: 24%

1"""Session log linking for issue files.

2

3 Links Claude Code JSONL session files to issue files by appending

4 session log entries with command name, timestamp, and file path.

5"""

6

7 from __future__ import annotations

8

9 import re

10 from datetime import UTC, datetime

11 from pathlib import Path

12

13 from little_loops.user_messages import get_project_folder

14

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:-]+)`")

21

22

23 def parse_session_log(content: str) -> list[str]:

24 """Extract distinct /ll:* command names from the ## Session Log section.

25

26 Returns commands in first-seen order, deduplicated (preserves insertion order).

27

28 Args:

29 content: Full text of an issue markdown file.

30

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))

40

41

42 def count_session_commands(content: str) -> dict[str, int]:

43 """Count occurrences of each /ll:* command in the ## Session Log section.

44

45 Unlike parse_session_log(), this does NOT deduplicate — each entry is counted.

46

47 Args:

48 content: Full text of an issue markdown file.

49

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

60

61

62 def get_current_session_jsonl(cwd: Path | None = None) -> Path | None:

63 """Resolve the active Claude Code session's JSONL file path.

64

65 Finds the most recently modified .jsonl file in the project's

66 Claude Code session directory, excluding agent session files.

67

68 Args:

69 cwd: Working directory to map. If None, uses current directory.

70

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

77

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

81

82 return max(jsonl_files, key=lambda f: f.stat().st_mtime)

83

84

85 def 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.

91

92 Creates or appends to the ``## Session Log`` section with command name,

93 ISO timestamp, and absolute path to the session JSONL file.

94

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.

99

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

107

108 timestamp = datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%S")

109 entry = f"- `{command}` - {timestamp} - `{session_jsonl}`"

110

111 content = issue_path.read_text()

112

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"

127

128 issue_path.write_text(content)

129 return True