diff --git a/src/claude_agent_sdk/_internal/session_resume.py b/src/claude_agent_sdk/_internal/session_resume.py index a50e578fd..d98600109 100644 --- a/src/claude_agent_sdk/_internal/session_resume.py +++ b/src/claude_agent_sdk/_internal/session_resume.py @@ -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, diff --git a/tests/test_session_resume.py b/tests/test_session_resume.py index ebe9c83b2..db5a348e7 100644 --- a/tests/test_session_resume.py +++ b/tests/test_session_resume.py @@ -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: