Skip to content

feat: add tag/agent_name to SDKSessionInfo + get_session_info#667

Open
qing-ant wants to merge 5 commits intomainfrom
qing/sdk-session-info-expand-py
Open

feat: add tag/agent_name to SDKSessionInfo + get_session_info#667
qing-ant wants to merge 5 commits intomainfrom
qing/sdk-session-info-expand-py

Conversation

@qing-ant
Copy link
Contributor

@qing-ant qing-ant commented Mar 10, 2026

Mirrors TypeScript PR claude-cli-internal#21659.

Changes

  • SDKSessionInfo dataclass += tag: str | None, created_at: float | None
  • list_sessions() now extracts tag from session tail (last occurrence wins; empty string → cleared)
  • NEW: get_session_info(session_id, directory=None) -> SDKSessionInfo | None — single-session metadata lookup without O(n) directory scan
  • Extracted _parse_session_info_from_lite() shared helper (matches TS parseSessionInfoFromLite)
  • SDKSessionInfo.file_size is now int | None (was int) — forward-compat for pluggable storage backends where file size isn't meaningful. Local JSONL storage still populates it.

Port notes

  • get_session_info includes worktree fallback per TS fix 2b2d7b91f4, matching get_session_messages semantics.
  • Empty-string tag resolves to None (cleared) via existing or None pattern.
  • created_at parsed from first entry's ISO timestamp field → epoch ms. Python 3.10's datetime.fromisoformat() doesn't support trailing Z, so we replace Z+00:00 before parsing. More reliable than stat().birthtime which is unsupported on some filesystems.
  • Add get_session_info() for single-session metadata lookup
  • Add tag and created_at fields to SDKSessionInfo; make file_size optional

Tests

20 new tests (TestTagExtraction, TestCreatedAtExtraction, TestGetSessionInfo). 286 total pass. Ruff + mypy clean.

…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
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.

1 participant