Skip to content

Commit 1b0f3dd

Browse files
cdeustclaude
andcommitted
fix(hooks): skip a transcript line that is not a JSON object (#591)
The review of PR #592 showed transcript_activity crashing with AttributeError on a line that parses as valid JSON but is not an object, which a whole-file JSON array produces. _build_session_entry runs inside process_event with no try/except, so that would have cost the session its log entry and its profile update, worse than the empty entry it replaces. The record is now skipped like an unparseable line, and the docstring says so. Two tests cover it: a whole-file JSON array and a JSONL holding a bare string and a bare array; both raise on the previous code. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01Cz7A6goxVzfKL9NT7k4zDH
1 parent 4370849 commit 1b0f3dd

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎mcp_server/infrastructure/transcript_activity.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ def transcript_activity(transcript_path: str | Path | None) -> dict[str, Any]:
4343
4444
An unreadable, missing or malformed transcript yields an empty sequence
4545
and a zero count: the caller keeps whatever the event supplied. A line
46-
that does not parse as JSON is skipped, the rest of the file still read.
46+
that does not parse as JSON, or parses as anything other than an object,
47+
is skipped and the rest of the file is still read — this runs inside the
48+
SessionEnd hook, where an exception would cost the session its log entry
49+
and its profile update.
4750
"""
4851
empty: dict[str, Any] = {"tool_sequence": [], "turn_count": 0}
4952
if not transcript_path:
@@ -60,7 +63,7 @@ def transcript_activity(transcript_path: str | Path | None) -> dict[str, Any]:
6063
record = json.loads(line)
6164
except ValueError:
6265
continue
63-
if record.get("type") != "assistant":
66+
if not isinstance(record, dict) or record.get("type") != "assistant":
6467
continue
6568
turns += 1
6669
if len(sequence) < MAX_TOOL_SEQUENCE:

‎tests_py/infrastructure/test_transcript_activity.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,24 @@ def test_the_sequence_is_capped(tmp_path) -> None:
9999

100100
assert len(activity["tool_sequence"]) == MAX_TOOL_SEQUENCE
101101
assert activity["turn_count"] == 1
102+
103+
104+
def test_a_json_array_file_is_not_a_crash(tmp_path) -> None:
105+
"""A whole-file JSON array parses line by line into lists, not dicts."""
106+
path = tmp_path / "s.json"
107+
path.write_text(
108+
json.dumps([_assistant("Read"), {"type": "assistant", "message": {}}]),
109+
encoding="utf-8",
110+
)
111+
112+
assert transcript_activity(str(path)) == {"tool_sequence": [], "turn_count": 0}
113+
114+
115+
def test_a_non_object_line_is_skipped(tmp_path) -> None:
116+
path = tmp_path / "s.jsonl"
117+
path.write_text(
118+
'"assistant"\n' + "[1, 2, 3]\n" + json.dumps(_assistant("Bash")) + "\n",
119+
encoding="utf-8",
120+
)
121+
122+
assert transcript_activity(str(path))["tool_sequence"] == ["Bash"]

0 commit comments

Comments
 (0)