Skip to content

Commit 33dc284

Browse files
cdeustclaude
andcommitted
fix(hooks): a transcript record's message field can hold anything (#591)
The previous commit guarded the record and left the field below it unguarded: `(record.get("message") or {}).get("content")` falls back only when message is falsy, so a truthy non-object (a string, a list) reached .get() and raised AttributeError in the same unguarded SessionEnd hook path. Reproduced on the branch with {"type": "assistant", "message": "not a dict"} before fixing. _message_content reads the content only when message is an object, and _tool_names already ignores a content that is not a list of blocks. Four parametrized cases (string, list, number, bool) cover it; each raises on the previous code, and the record still counts as a turn. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01Cz7A6goxVzfKL9NT7k4zDH
1 parent 1b0f3dd commit 33dc284

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

‎mcp_server/infrastructure/transcript_activity.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
MAX_TOOL_SEQUENCE = 2000
2727

2828

29+
def _message_content(record: dict) -> Any:
30+
"""The record's message content, or None when the record is not shaped
31+
the way a transcript record is. Nothing here trusts a field's type:
32+
every level of a hand-written or truncated transcript can hold anything.
33+
"""
34+
message = record.get("message")
35+
return message.get("content") if isinstance(message, dict) else None
36+
37+
2938
def _tool_names(content: Any) -> list[str]:
3039
if not isinstance(content, list):
3140
return []
@@ -67,8 +76,7 @@ def transcript_activity(transcript_path: str | Path | None) -> dict[str, Any]:
6776
continue
6877
turns += 1
6978
if len(sequence) < MAX_TOOL_SEQUENCE:
70-
content = (record.get("message") or {}).get("content")
71-
sequence.extend(_tool_names(content))
79+
sequence.extend(_tool_names(_message_content(record)))
7280
except OSError:
7381
return empty
7482
return {"tool_sequence": sequence[:MAX_TOOL_SEQUENCE], "turn_count": turns}

‎tests_py/infrastructure/test_transcript_activity.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import json
1313

14+
import pytest
15+
1416
from mcp_server.infrastructure.transcript_activity import (
1517
MAX_TOOL_SEQUENCE,
1618
transcript_activity,
@@ -120,3 +122,23 @@ def test_a_non_object_line_is_skipped(tmp_path) -> None:
120122
)
121123

122124
assert transcript_activity(str(path))["tool_sequence"] == ["Bash"]
125+
126+
127+
@pytest.mark.parametrize(
128+
"message",
129+
["not a dict", [1, 2, 3], 7, True],
130+
ids=["string", "list", "number", "bool"],
131+
)
132+
def test_a_record_whose_message_is_not_an_object_is_counted_not_a_crash(
133+
tmp_path, message
134+
) -> None:
135+
"""Every level of a transcript record can hold anything (issue #591)."""
136+
transcript = _write(
137+
tmp_path / "s.jsonl",
138+
[{"type": "assistant", "message": message}, _assistant("Bash")],
139+
)
140+
141+
activity = transcript_activity(transcript)
142+
143+
assert activity["tool_sequence"] == ["Bash"]
144+
assert activity["turn_count"] == 2

0 commit comments

Comments
 (0)