feat: add tag_session with Unicode sanitization#670
Merged
Conversation
Ports TS SDK renameSessionImpl from qing/sdk-rename-session (PR #21660).
Appends a {type:'custom-title',customTitle:<title>,sessionId:<id>} JSONL
entry to the session file. list_sessions reads the LAST custom-title from
the file tail, so repeated calls are safe — the most recent wins.
Key behaviors ported:
- Validates UUID; rejects invalid session_id with ValueError
- Trims title before storing; rejects empty/whitespace-only titles
- Uses os.open with O_WRONLY | O_APPEND (no O_CREAT) so missing files
fail with ENOENT — no TOCTOU pre-check. CPython handles O_APPEND
correctly on all platforms (no Bun/Windows workaround needed).
- Skips 0-byte stubs during search (matches reader search protocol)
- Worktree fallback when directory is provided
- Searches all project directories when directory is omitted
- Compact JSON output (separators=(',', ':')) matching CLI format
Tests: 281 passed. Ruff + mypy clean.
Ports TS SDK tagSessionImpl from qing/sdk-tag-session (PR #21662).
Appends a {type:'tag',tag:<tag>,sessionId:<id>} JSONL entry. list_sessions
reads the LAST tag from the file tail — most recent wins. Passing None
appends an empty-string tag entry (clears the tag).
Key behaviors ported:
- Validates UUID; rejects invalid session_id with ValueError
- Trims tag before storing; rejects empty/whitespace-only tags
- Unicode sanitization: strips zero-width chars, directional marks,
BOM, private-use characters, and applies NFKC normalization
(ported from TS partiallySanitizeUnicode) for CLI filter compat
- None appends empty-string tag entry which readers treat as cleared
- Compact JSON output matching CLI format
- Reuses _append_to_session (worktree fallback, 0-byte stub skip,
O_APPEND without O_CREAT)
Python note: uses unicodedata.category() for Cf/Co/Cn stripping since
Python's re module doesn't support \p{} escapes without the third-party
regex module.
Tests: 310 passed. Ruff + mypy clean.
1e2839d to
034bfbe
Compare
dicksontsai
previously approved these changes
Mar 12, 2026
# Conflicts: # src/claude_agent_sdk/__init__.py # src/claude_agent_sdk/_internal/session_mutations.py # tests/test_session_mutations.py
dicksontsai
approved these changes
Mar 12, 2026
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.
Stacked on #668 (
rename_session).Changes
tag_session(session_id, tag, directory=None)— appends a{type:'tag',tag:<tag>,sessionId:<id>}JSONL entry.list_sessions()reads the LAST tag from the file tail — most recent wins.Noneappends an empty-string tag entry ({"tag":""}) which readers treat as cleared.Port notes
Unicode sanitization (for CLI filter compat):
remodule doesn't support\p{Cf}etc. without the third-partyregexmodule, so_sanitize_unicode()usesunicodedata.category(c) not in {"Cf","Co","Cn"}per-character — semantically equivalent to TS's/[\p{Cf}\p{Co}\p{Cn}]/guregex (and more reliable than TS's own ES-engine fallback).Other behavior:
.strip()ed before storing; whitespace-only tags rejected (ValueError)._append_to_sessionfrom therename_sessionPR (worktree fallback, 0-byte stub skip,O_APPENDwithoutO_CREAT).tag_session()for tagging sessions (with Unicode sanitization)Tests
17 new tests (TestTagSession, TestSanitizeUnicode). 298 total pass. Ruff + mypy clean.