feat: add tag/agent_name to SDKSessionInfo + get_session_info#667
Open
feat: add tag/agent_name to SDKSessionInfo + get_session_info#667
Conversation
…n_info
Ports TS SDK changes from qing/sdk-session-info-expand (PR #21659).
- Add tag and agent_name fields to SDKSessionInfo dataclass
- Refactor inline field extraction into _parse_session_info_from_lite
helper for reuse between list_sessions and get_session_info
- Extract tag from tail via last-occurrence scan (empty string = cleared)
- Extract agent_name scoped to {type:'agent-name'} entries only; bare
tail-scan would pick up per-message agentName from swarm sessions
(handles both compact and spaced JSON variants)
- Add get_session_info(session_id, directory=None) for single-session
metadata lookup without O(n) directory scan; includes worktree fallback
when directory is provided (matches get_session_messages semantics)
- Export get_session_info from package root
Tests: 75 passed. Ruff + mypy clean.
Extracts creation timestamp from first entry's ISO timestamp field in the head buffer. Returns epoch ms for consistency with last_modified. More reliable than stat().birthtime which is unsupported on some filesystems. Python 3.10 compatibility: datetime.fromisoformat() in 3.10 doesn't support trailing 'Z', so we replace it with '+00:00' before parsing. Mirrors claude-cli-internal commit 2470efe469. Tests: 288 passed. Ruff + mypy clean.
agent_name is tied to feature-gated swarm mode — not a general SDK concept. Removing to keep SDKSessionInfo focused on universal metadata. Mirrors TS change in claude-cli-internal#21659. Tests: 286 passed. Ruff + mypy clean.
file_size is a local-JSONL concept with no remote-storage equivalent. Making optional now for forward-compat with pluggable storage backends. Local _parse_session_info_from_lite still populates it from fstat size; this is a type loosening only. Existing tests pass unchanged. Mirrors claude-cli-internal PR #21659.
…-expand-py # Conflicts: # src/claude_agent_sdk/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mirrors TypeScript PR claude-cli-internal#21659.
Changes
SDKSessionInfodataclass +=tag: str | None,created_at: float | Nonelist_sessions()now extractstagfrom session tail (last occurrence wins; empty string → cleared)get_session_info(session_id, directory=None) -> SDKSessionInfo | None— single-session metadata lookup without O(n) directory scan_parse_session_info_from_lite()shared helper (matches TSparseSessionInfoFromLite)SDKSessionInfo.file_sizeis nowint | None(wasint) — forward-compat for pluggable storage backends where file size isn't meaningful. Local JSONL storage still populates it.Port notes
get_session_infoincludes worktree fallback per TS fix2b2d7b91f4, matchingget_session_messagessemantics.None(cleared) via existingor Nonepattern.created_atparsed from first entry's ISOtimestampfield → epoch ms. Python 3.10'sdatetime.fromisoformat()doesn't support trailingZ, so we replaceZ→+00:00before parsing. More reliable thanstat().birthtimewhich is unsupported on some filesystems.get_session_info()for single-session metadata lookuptagandcreated_atfields toSDKSessionInfo; makefile_sizeoptionalTests
20 new tests (TestTagExtraction, TestCreatedAtExtraction, TestGetSessionInfo). 286 total pass. Ruff + mypy clean.