Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(learn/gemini): detect the project path for JSONL sessions (#2229)

Abhay Singh7e83b8da3cde
diff --git a/headroom/learn/plugins/gemini.py b/headroom/learn/plugins/gemini.py
index 15e589a68..9aac4116e 100644
--- a/headroom/learn/plugins/gemini.py
+++ b/headroom/learn/plugins/gemini.py
@@ -312,7 +312,16 @@ class GeminiPlugin(LearnPlugin, ConversationScanner):
)
def _detect_project_path(self, session_path: Path) -> Path | None:
- """Try to detect the project path from a session file."""
+ """Try to detect the project path from a session file (JSON or JSONL)."""
+ # A `.jsonl` session is a stream of one JSON object per line, so
+ # `json.load` on the whole file raises JSONDecodeError on the second
+ # line and detection silently fell back to cwd — writing the learned
+ # insights to the wrong project and missing its GEMINI.md. Read JSONL
+ # line-by-line like the sibling `_scan_jsonl_session` (and the Claude
+ # plugin's `_project_path_from_session_cwd`) do.
+ if session_path.suffix == ".jsonl":
+ return self._detect_project_path_jsonl(session_path)
+
try:
with open(session_path, encoding="utf-8", errors="replace") as f:
data = json.load(f)
@@ -320,13 +329,37 @@ class GeminiPlugin(LearnPlugin, ConversationScanner):
return None
if isinstance(data, dict):
- project_path = data.get("projectPath", data.get("project_path", ""))
- if project_path and Path(project_path).exists():
- return Path(project_path)
- cwd = data.get("cwd", data.get("workingDirectory", ""))
- if cwd and Path(cwd).exists():
- return Path(cwd)
+ return self._project_path_from_entry(data)
+
+ return None
+
+ def _detect_project_path_jsonl(self, session_path: Path) -> Path | None:
+ try:
+ with open(session_path, encoding="utf-8", errors="replace") as f:
+ for line in f:
+ if not line.strip():
+ continue
+ try:
+ entry = json.loads(line)
+ except json.JSONDecodeError:
+ continue
+ if not isinstance(entry, dict):
+ continue
+ found = self._project_path_from_entry(entry)
+ if found is not None:
+ return found
+ except (OSError, UnicodeDecodeError):
+ return None
+ return None
+ @staticmethod
+ def _project_path_from_entry(entry: dict) -> Path | None:
+ project_path = entry.get("projectPath", entry.get("project_path", ""))
+ if project_path and Path(project_path).exists():
+ return Path(project_path)
+ cwd = entry.get("cwd", entry.get("workingDirectory", ""))
+ if cwd and Path(cwd).exists():
+ return Path(cwd)
return None
diff --git a/tests/test_learn/test_gemini_scanner.py b/tests/test_learn/test_gemini_scanner.py
index 0e0ebb34e..441d0b9a5 100644
--- a/tests/test_learn/test_gemini_scanner.py
+++ b/tests/test_learn/test_gemini_scanner.py
@@ -105,6 +105,24 @@ class TestProjectDiscovery:
assert len(projects) == 1
assert projects[0].data_path == chats_dir
+ def test_detects_project_path_from_jsonl_cwd(self, tmp_path):
+ # A JSONL session must not be read with a whole-file json.load (which
+ # raises on the 2nd line and silently fell back to cwd). The project
+ # cwd carried in the session_metadata line must be recovered.
+ gemini_dir, chats_dir = _setup_gemini_dir(tmp_path)
+ project = tmp_path / "myproject"
+ project.mkdir()
+ session_path = _write_jsonl_session(
+ chats_dir,
+ [
+ {"type": "session_metadata", "id": "s1", "cwd": str(project)},
+ {"type": "user", "parts": [{"text": "hi"}]},
+ ],
+ )
+
+ scanner = GeminiScanner(gemini_dir=gemini_dir)
+ assert scanner._detect_project_path(session_path) == project
+
# =============================================================================
# JSON Session Parsing