Skip to content

Replay your coding agent sessions as a map and a timeline

fix(gemini): don't skip sessions whose first prompt is past the head

shubhook0d2b4c0d4b11
diff --git a/python/src/agentplay/_core/ingestion/coding_agent/_sources/local/source.py b/python/src/agentplay/_core/ingestion/coding_agent/_sources/local/source.py
index 6b8aa75..d17a336 100644
--- a/python/src/agentplay/_core/ingestion/coding_agent/_sources/local/source.py
+++ b/python/src/agentplay/_core/ingestion/coding_agent/_sources/local/source.py
@@ -1157,24 +1157,26 @@ def _gemini_user_counts(content: object) -> bool:
return any(text and not text.lstrip().startswith(_GEMINI_SESSION_CONTEXT_PREFIX) for text in texts)
-def _gemini_has_messages(head: bytes) -> bool:
- """True when the head holds a real prompt or a `gemini` message.
-
- A session opened and closed without a prompt is the header plus the CLI's
- `<session_context>` user record. That record is not a turn, so the file
- has nothing to map and is an `empty_events` skip, same as a header-only
- file and `_kiro_event_count == 0`.
+def _gemini_has_messages(path: Path) -> bool:
+ """True when the file holds a real prompt or a `gemini` message.
+
+ Streams one line at a time and stops at the first qualifying record, so a
+ prompt that sits past `_read_head` (after a multi-MB `<session_context>`
+ line) is still found. A file of only the header, that context block, or
+ empty user text is skipped at EOF, same as `_kiro_event_count == 0`.
+ The whole transcript is never held at once.
"""
- for line in head.split(b"\n"):
- try:
- obj = json.loads(line)
- except ValueError:
- continue
- if not isinstance(obj, dict):
- continue
- kind = obj.get("type")
- if kind == "gemini" or (kind == "user" and _gemini_user_counts(obj.get("content"))):
- return True
+ with path.open("rb") as handle:
+ for line in handle:
+ try:
+ obj = json.loads(line)
+ except ValueError:
+ continue
+ if not isinstance(obj, dict):
+ continue
+ kind = obj.get("type")
+ if kind == "gemini" or (kind == "user" and _gemini_user_counts(obj.get("content"))):
+ return True
return False
@@ -1963,7 +1965,12 @@ class LocalSource:
self._record_discovery("gemini-cli", "different_agent")
return None
- if not _gemini_has_messages(head):
+ try:
+ has_messages = _gemini_has_messages(jsonl_file)
+ except OSError:
+ self._record_discovery("gemini-cli", "empty_or_unreadable")
+ return None
+ if not has_messages:
logger.debug("gemini-cli: %s has no messages; session skipped", jsonl_file)
self._record_discovery("gemini-cli", "empty_events")
return None
diff --git a/python/tests/core/test_gemini_parser.py b/python/tests/core/test_gemini_parser.py
index 2f49554..2465922 100644
--- a/python/tests/core/test_gemini_parser.py
+++ b/python/tests/core/test_gemini_parser.py
@@ -449,6 +449,52 @@ def test_empty_text_user_record_is_skipped_as_empty_events(tmp_path):
assert source.discovery_stats()["gemini-cli"]["skipped"]["empty_events"] == 2
+def test_prompt_past_the_head_is_indexed_and_empty_sessions_are_skipped(tmp_path):
+ # `_read_head` returns at most 5MB plus one 64KB chunk once the header has
+ # identified the agent. A context record larger than that hides the first
+ # real prompt from a head-only check.
+ home = tmp_path / ".gemini"
+ chats = home / "tmp" / "project" / "chats"
+ chats.mkdir(parents=True)
+ (chats.parent / ".project_root").write_text(FIXTURE_ROOT, encoding="utf-8")
+ past_head = "<session_context>\n" + ("x" * (5_000_000 + 65_536)) + "\n</session_context>"
+ prompt = {
+ "id": "u1",
+ "timestamp": "2026-10-23T07:04:00Z",
+ "type": "user",
+ "content": [{"text": "PROMPT-PLACEHOLDER-1"}],
+ }
+ model = {
+ "id": "g1",
+ "timestamp": "2026-10-23T07:04:01Z",
+ "type": "gemini",
+ "content": "MODEL-TEXT-PLACEHOLDER",
+ "model": "gemini-3-flash-preview",
+ }
+ context = "<session_context>\nCONTEXT-PLACEHOLDER\n</session_context>"
+ files = {
+ "session-prompt-past-head.jsonl": (_context_user("u0", past_head), prompt, "prompt-past-head"),
+ "session-model-past-head.jsonl": (_context_user("u0", past_head), model, "model-past-head"),
+ "session-context-only.jsonl": (
+ _context_user("u0", context),
+ None,
+ "context-only",
+ ),
+ "session-empty-text.jsonl": (_context_user("u0", ""), None, "empty-text"),
+ }
+ for name, (first, second, session_id) in files.items():
+ records = (first,) if second is None else (first, second)
+ (chats / name).write_bytes(_session(*records, session_id=session_id))
+
+ source = LocalSource(claude_code_homes=[], codex_homes=[], gemini_homes=[home])
+ index = source._build_index()
+ assert "local:gemini-cli:prompt-past-head" in index
+ assert "local:gemini-cli:model-past-head" in index
+ assert "local:gemini-cli:context-only" not in index
+ assert "local:gemini-cli:empty-text" not in index
+ assert source.discovery_stats()["gemini-cli"]["skipped"]["empty_events"] == 2
+
+
def test_local_source_indexes_gemini_sessions(tmp_path):
source = LocalSource(claude_code_homes=[], codex_homes=[], gemini_homes=[_gemini_home(tmp_path)])
index = source._build_index()