Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/claude_agent_sdk/_internal/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,10 +834,14 @@ def get_session_info(


def _try_read_session_file(project_dir: Path, file_name: str) -> str | None:
"""Tries to read a session JSONL file from a project directory."""
"""Tries to read a session JSONL file from a project directory.

A corrupt (invalid UTF-8) transcript degrades to ``None`` like a missing
or unreadable file instead of raising out of this best-effort helper.
"""
try:
return (project_dir / file_name).read_text(encoding="utf-8")
except OSError:
except (OSError, UnicodeDecodeError):
return None


Expand Down Expand Up @@ -1618,7 +1622,7 @@ def get_subagent_messages(

try:
content = match.read_text(encoding="utf-8")
except OSError:
except (OSError, UnicodeDecodeError):
return []
if not content:
return []
Expand Down
45 changes: 45 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2391,3 +2391,48 @@ def test_empty_agent_file(self, claude_config_dir: Path, tmp_path: Path):
(subagents_dir / "agent-empty.jsonl").write_text("")

assert get_subagent_messages(sid, "empty", directory=project_path) == []


# ---------------------------------------------------------------------------
# Invalid UTF-8 transcript degradation (regression for issue #1518)
# ---------------------------------------------------------------------------


class TestInvalidUtf8Transcripts:
"""Corrupt (invalid UTF-8) transcript files degrade instead of raising."""

def test_get_session_messages_invalid_utf8(
self, claude_config_dir: Path, tmp_path: Path
):
"""A session file with invalid UTF-8 returns [] instead of raising."""
project_path = str(tmp_path / "proj")
Path(project_path).mkdir(parents=True)
project_dir = _make_project_dir(
claude_config_dir, os.path.realpath(project_path)
)
sid = str(uuid.uuid4())
raw = (
b'{"type":"user","uuid":"u1","sessionId":"x"}\n'
b"\xff\xfe not utf-8 \x80\n"
b'{"type":"assistant","uuid":"a1","sessionId":"x"}\n'
)
(project_dir / f"{sid}.jsonl").write_bytes(raw)

assert get_session_messages(sid, directory=project_path) == []

def test_get_subagent_messages_invalid_utf8(
self, claude_config_dir: Path, tmp_path: Path
):
"""A subagent transcript with invalid UTF-8 returns [] instead of raising."""
project_path = str(tmp_path / "proj")
Path(project_path).mkdir(parents=True)
sid, subagents_dir = _make_session_with_subagents(
claude_config_dir, project_path, agent_ids=[]
)
raw = (
b'{"type":"user","uuid":"u1"}\n'
b"\xff\xfe not utf-8 \x80\n"
)
(subagents_dir / "agent-badutf.jsonl").write_bytes(raw)

assert get_subagent_messages(sid, "badutf", directory=project_path) == []