Skip to content

fix: pre-create project log dir before launching CLI#737

Open
anthony-maio wants to merge 2 commits intoanthropics:mainfrom
anthony-maio:wt/issue-513-precreate-project-log-dir
Open

fix: pre-create project log dir before launching CLI#737
anthony-maio wants to merge 2 commits intoanthropics:mainfrom
anthony-maio:wt/issue-513-precreate-project-log-dir

Conversation

@anthony-maio
Copy link
Copy Markdown

Summary

  • pre-create the Claude project log directory before launching the CLI subprocess
  • resolve the config root from CLAUDE_CONFIG_DIR, HOME, or USERPROFILE in the child environment
  • add regression tests covering both HOME and CLAUDE_CONFIG_DIR overrides

Problem

When callers override HOME or CLAUDE_CONFIG_DIR per agent, the CLI can try to write ~/.claude/projects/<sanitized-cwd>/<session>.jsonl before that per-project directory exists. In multi-agent environments this shows up as ENOENT on startup.

Test Plan

  • python -m pytest tests/test_transport.py -k "not concurrent_writes"
  • python -m pytest tests/test_transport.py -k "project_log_dir or CLAUDE_CONFIG_DIR"
  • python -m ruff check src/claude_agent_sdk/_internal/transport/subprocess_cli.py tests/test_transport.py
  • python -m mypy src/

Notes

Two existing tests/test_transport.py cases still require the optional trio backend in this environment, so I excluded -k concurrent_writes from the broader pytest run.

Fixes #513

Copilot AI review requested due to automatic review settings March 25, 2026 23:16
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses Claude Code CLI startup failures in multi-agent setups by ensuring the per-project log directory is created before the CLI subprocess is launched, using the child process’s resolved config root.

Changes:

  • Add child-environment-aware resolution of the Claude config directory (CLAUDE_CONFIG_DIR, HOME, USERPROFILE) and pre-create the CLI project log directory before spawning the subprocess.
  • Add regression tests covering directory creation for both HOME and CLAUDE_CONFIG_DIR overrides.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/claude_agent_sdk/_internal/transport/subprocess_cli.py Resolves config directory from child env and ensures projects/<sanitized-cwd> exists before launching the CLI.
tests/test_transport.py Adds tests asserting the project log directory is created under overridden HOME and CLAUDE_CONFIG_DIR.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

Claude Code CLI fails to create project log path; ENOENT + lock contention + OOM in multi-agent environment

2 participants