Coverage for little_loops / session_log.py: 100%

44 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2026-03-04 01:17 -0600

1"""Session log linking for issue files. 

2 

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

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

5""" 

6 

7from __future__ import annotations 

8 

9import re 

10from datetime import UTC, datetime 

11from pathlib import Path 

12 

13from 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 

23def 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 log_match = _SESSION_LOG_SECTION_RE.search(content) 

35 if not log_match: 

36 return [] 

37 cmds = _COMMAND_RE.findall(log_match.group(1)) 

38 # Deduplicate while preserving insertion order 

39 return list(dict.fromkeys(cmds)) 

40 

41 

42def 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 log_match = _SESSION_LOG_SECTION_RE.search(content) 

54 if not log_match: 

55 return {} 

56 counts: dict[str, int] = {} 

57 for cmd in _COMMAND_RE.findall(log_match.group(1)): 

58 counts[cmd] = counts.get(cmd, 0) + 1 

59 return counts 

60 

61 

62def 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 

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. 

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 # Append entry after existing section header (count=1 to only replace first occurrence) 

115 content = content.replace( 

116 "## Session Log\n", 

117 f"## Session Log\n{entry}\n", 

118 1, 

119 ) 

120 else: 

121 # Add new section before --- Status footer if present, else at end 

122 if "\n---\n\n## Status" in content: 

123 content = content.replace( 

124 "\n---\n\n## Status", 

125 f"\n## Session Log\n{entry}\n\n---\n\n## Status", 

126 ) 

127 else: 

128 content += f"\n\n## Session Log\n{entry}\n" 

129 

130 issue_path.write_text(content) 

131 return True