Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/claude_agent_sdk/_internal/session_resume.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,20 @@ def _copy_auth_files(tmp_base: Path, opt_env: dict[str, str]) -> None:
)

creds_bytes = _read_if_present(source_config_dir / ".credentials.json")
creds_json = creds_bytes.decode("utf-8") if creds_bytes is not None else None
# An undecodable file (e.g. a truncated write from an interrupted login) is
# treated like a missing one: auth seeding is best-effort, and failing here
# would abort resume before the CLI is even spawned — including for callers
# that authenticate via ANTHROPIC_API_KEY and never read this file.
creds_json: str | None = None
if creds_bytes is not None:
try:
creds_json = creds_bytes.decode("utf-8")
except UnicodeDecodeError as e:
logger.warning(
"[SessionStore] resume: skipping %s (%s)",
source_config_dir / ".credentials.json",
e,
)

# macOS default setup keeps OAuth tokens in the Keychain, not a file.
# Redirecting CLAUDE_CONFIG_DIR changes the Keychain service-name suffix,
Expand Down
20 changes: 20 additions & 0 deletions tests/test_session_resume.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,26 @@ async def test_credentials_from_keychain_fallback(
assert "refreshToken" not in creds["claudeAiOauth"]
await m.cleanup()

@pytest.mark.anyio
async def test_corrupt_credentials_file_does_not_abort_resume(
self, cwd: Path, project_key: str, isolated_home: Path
) -> None:
"""An undecodable .credentials.json (e.g. a truncated write from an
interrupted login) is skipped like a missing one: no credentials file
is seeded, and resume proceeds so env/API-key auth still works."""
config = isolated_home / ".claude"
config.mkdir()
(config / ".credentials.json").write_bytes(b"\xff\xfe{not valid utf-8")

m = await self._materialize(cwd, project_key)

assert not (m.config_dir / ".credentials.json").exists()
# The transcript itself was still materialized.
assert (
m.config_dir / "projects" / project_key / f"{SESSION_ID}.jsonl"
).exists()
await m.cleanup()

async def _materialize(
self, cwd: Path, project_key: str, **kw: Any
) -> MaterializedResume:
Expand Down