Repository navigation
Description
Summary
_entries_to_session_messages slices with an unguarded negative offset when a positive limit is given: messages[offset: offset + limit] with offset=-1, limit=5 is messages[-1:4], which is [] for any list. The no-limit branch, by contrast, treats the same negative offset as absent (if offset > 0), so the two branches disagree about what a negative offset means. The subagent-messages helper has the identical unguarded slice.
This is a correctness/robustness bug, not a security vulnerability: it only affects paging results for the caller's own session reads.
Version
- Repo:
anthropics/claude-agent-sdk-python - Package:
claude-agent-sdk0.2.182 - Commit:
4720d367f7db9d5df8a166c74d7f36a538ac8c34(main, 2026-11-13)
Found during an audit of 0.2.181 (commit 22b8eb9c19adfa78759b79e22bd664a57843bb06); re-confirmed present at the commit above.
Location
src/claude_agent_sdk/_internal/sessions.py:1355-1358 (_entries_to_session_messages):
if limit is not None and limit > 0:
return messages[offset: offset + limit]
if offset > 0:
return messages[offset:]Identical unguarded slice at :1729-1732 in _entries_to_subagent_messages. The listing convention in _apply_sort_limit_offset (:565-577) guards both values, and the comment at :2014-2017 says the message helpers should mirror those guards.
Minimal repro
from claude_agent_sdk._internal import sessions
messages = list(range(10)) # stand-in for 10 parsed messages
# Via the real helper on a 10-message session:
result = sessions._entries_to_session_messages(entries, limit=5, offset=-1)
print(result) # [] — expected the first 5 messagesEquivalently through the public API: get_session_messages(session_id, limit=5, offset=-1) on a 10-message session returns [], while get_session_messages(session_id, offset=-1) (no limit) returns all 10 — the same negative offset is treated as absent in one branch and as a from-the-end slice in the other.
Observed: limit=5, offset=-1 on 10 messages returns [].
Expected: a negative offset is normalized to 0 (or guarded by offset > 0 before slicing), returning the first 5 messages — consistent with the no-limit branch.
Impact
Callers passing a computed offset that can go negative (e.g. page * size - overlap) get silently empty or wrong pages instead of the first page. Local correctness impact only.
Suggested fix
Normalize a negative offset to 0, or apply the offset > 0 guard before slicing, in both _entries_to_session_messages and _entries_to_subagent_messages, mirroring _apply_sort_limit_offset.
Environment
- Python 3.12.3, Linux
- Verified against the pinned commit above (file byte-identical to the audited 0.2.181 pin).
Activity
doge-th commentedon Nov 22, 2026
Taking this — the fix will guard both slices (
_entries_to_session_messagesand_entries_to_subagent_messages) to treat a non-positiveoffsetas absent, mirroring the no-limit branch and the_apply_sort_limit_offsetconvention, with regression tests for both helpers.doge-th commentedon Nov 22, 2026
Fix up in #1568 — both helpers now mirror
_apply_sort_limit_offset's guards, with store- and disk-path regression tests that fail on unpatchedmain.