From 7e83b8da3cdefd5c0820017ff60e48eb489044fd Mon Sep 17 00:00:00 2001 From: Abhay Singh Date: Wed, 12 Aug 2026 10:07:37 +0530 Subject: [PATCH] fix(learn/gemini): detect the project path for JSONL sessions (#2229) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description The Gemini `learn` plugin can't detect the project path for JSONL sessions, so it writes its insights to the wrong project. `discover_projects` globs both `session-*.json` and `session-*.jsonl` and calls `_detect_project_path`, which reads the file with a single whole-file `json.load`: ```python def _detect_project_path(self, session_path: Path) -> Path | None: try: with open(session_path, encoding="utf-8", errors="replace") as f: data = json.load(f) except (OSError, json.JSONDecodeError): return None ... ``` A `.jsonl` session is one JSON object per line, so `json.load` on the whole file raises `json.JSONDecodeError` ("Extra data") on the second line. The method swallows that and returns `None`, and the caller falls back to `Path.cwd()`: ```python project_path = self._detect_project_path(session_files[0]) ... ProjectInfo( name=project_path.name if project_path else project_dir.name, project_path=project_path or Path.cwd(), # wrong project context_file=gemini_md, # None: GEMINI.md never found ... ) ``` So for the JSONL format (Gemini CLI's newer session format — the one that carries `type: "session_metadata"` records), detection never works: the learned tool/verbosity insights are attributed to the current working directory instead of the real project, and the project's `GEMINI.md` is never located. The sibling `_scan_jsonl_session` already reads this format line-by-line, and the Claude plugin recovers the project path from session `cwd` the same way. ## Fix Route `.jsonl` sessions through a line-by-line reader and share the field extraction (`projectPath` / `project_path` / `cwd` / `workingDirectory`) between both formats: ```python if session_path.suffix == ".jsonl": return self._detect_project_path_jsonl(session_path) ``` `_detect_project_path_jsonl` parses each line (skipping blanks and unparseable lines, exactly like `_scan_jsonl_session`) and returns the first record that yields an existing path. The JSON path is unchanged. Closes # ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Performance improvement - [ ] Code refactoring (no functional changes) ## Changes Made - `headroom/learn/plugins/gemini.py`: dispatch `.jsonl` sessions to a new line-by-line `_detect_project_path_jsonl`; factor the field extraction into `_project_path_from_entry` shared by both paths. - `tests/test_learn/test_gemini_scanner.py`: new test asserting a JSONL session's `cwd` is recovered. - `CHANGELOG.md`: Bug Fixes entry. ## Testing - [ ] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [ ] Manual testing performed ### Test Output ```text $ uvx ruff@0.15.17 check headroom/learn/plugins/gemini.py tests/test_learn/test_gemini_scanner.py All checks passed! $ uvx mypy@1.20.2 --ignore-missing-imports headroom/learn/plugins/gemini.py Success: no issues found in 1 source file ``` ## Real Behavior Proof - Environment: Windows 11, Python 3.12, `uvx ruff@0.15.17` / `uvx mypy@1.20.2`. A full `pytest` OOM-kills this box (ML stack import), so I reproduced the behavior with a dependency-free script mirroring both detection paths and left the full pytest to CI. - Exact command / steps: wrote a `.jsonl` session whose first record is `{"type":"session_metadata","cwd":""}`, then ran the OLD whole-file `json.load` reader and the NEW line-by-line reader; also checked a single-object `.json` session still resolves under both. - Observed result: OLD returns `None` for the JSONL file (the caller would fall back to cwd); NEW returns the project path; the `.json` case resolves identically under both. - Not tested: a full `learn` run over a real Gemini history; full local `pytest` deferred to CI (OOM). ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [x] I have updated the CHANGELOG.md if applicable ## Additional Notes The "unit tests pass locally" box is unchecked because the full suite imports the ML stack, which I can't run here. The new test uses the existing `GeminiScanner` harness in `tests/test_learn/test_gemini_scanner.py` so it runs under the normal CI pytest job; behaviour is additionally verified by the standalone proof above. --------- Co-authored-by: JerrettDavis --- headroom/learn/plugins/gemini.py | 47 +++++++++++++++++++++---- tests/test_learn/test_gemini_scanner.py | 18 ++++++++++ 2 files changed, 58 insertions(+), 7 deletions(-) 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,15 +329,39 @@ 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 + # Module-level instance for auto-discovery by the plugin registry plugin = GeminiPlugin() 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