Skip to content

fix: degrade corrupt session transcripts like unreadable files - #1570

Open
doge-th wants to merge 2 commits into
anthropics:mainfrom
doge-th:fix/session-file-utf8-tolerance
Open

doge-th wants to merge 2 commits into
anthropics:mainfrom
doge-th:fix/session-file-utf8-tolerance

Conversation

@doge-th

@doge-th doge-th commented Nov 22, 2026

Copy link
Copy Markdown

Summary

Fixes #1518.

_try_read_session_file and the subagent transcript read in get_subagent_messages decode with strict UTF-8 but catch only OSError. UnicodeDecodeError inherits from ValueError, so a single invalid byte in a transcript escaped get_session_messages as an uncaught exception instead of degrading to [] the way missing or unreadable files do.

Both sites now catch (OSError, ValueError) and return their empty fallback — matching _read_agent_metadata_sidecar (which already catches ValueError); _read_session_lite tolerates undecodable bytes via errors="replace".

Test plan

  • TestGetSessionMessages::test_undecodable_transcript_degrades_to_empty: a transcript with invalid UTF-8 followed by a valid JSONL line returns [] from get_session_messages, and a corrupt subagent transcript returns [] from get_subagent_messages instead of raising.
  • Verified the test fails on unpatched main with UnicodeDecodeError and passes with the fix.
  • pytest tests/: 1756 passed, 6 skipped (this branch). ruff check, mypy src/ scripts/ clean.

The two message-page helpers sliced with the raw offset when a positive
limit was given: messages[offset : offset + limit] with a negative
offset returned an empty/wrong page, while the no-limit branch treated
the same offset as absent. get_session_messages(sid, limit=5, offset=-1)
on a 10-message session returned [].

Mirror _apply_sort_limit_offset's guards in _entries_to_session_messages
and _entries_to_subagent_messages, which the SessionStore path already
follows: non-positive offsets are treated as absent and a positive limit
takes the first entries after the offset.

Fixes anthropics#1519
_try_read_session_file and the subagent transcript read caught only
OSError, but UnicodeDecodeError is a ValueError subclass, so a single
invalid byte in a transcript escaped get_session_messages as an uncaught
exception instead of degrading to [] like a missing or unreadable file.

Catch ValueError at both sites, matching _read_agent_metadata_sidecar;
_read_session_lite already tolerates undecodable bytes via
errors="replace".

Fixes anthropics#1518

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid UTF-8 in a session file escapes get_session_messages as UnicodeDecodeError

1 participant