claude-agent-sdk-python icon indicating copy to clipboard operation
claude-agent-sdk-python copied to clipboard

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

Open jamiequint opened this issue 10 months ago • 2 comments

Environment

  • Running Claude CLI via claude_agent_sdk subprocess.
  • Each agent sets HOME to a per-agent directory (e.g. /home/myapp/{agent-id}).
  • Working directory is a workspace path (e.g. /tmp/myapp-agents/{agent-id} or /home/myapp/{agent-id}/{workspace}).
  • Multiple concurrent agents/processes on same host.

Observed Behavior

  • CLI attempts to open a project log file under:

    $HOME/.claude/projects/{normalized_cwd}/{session_id}.jsonl

  • When {normalized_cwd} includes slashified paths like -tmp-myapp-agents-{agent-id}, the CLI does not create the intermediate directory.

  • Results in:

    Error opening file .../.claude/projects/-tmp-myapp-agents-{agent-id}/{session}.jsonl ENOENT: no such file or directory

  • In parallel runs, CLI also logs:

    Failed to save config with lock: Lock file is already being held

  • In some cases the process is OOM-killed (exit code -9) shortly after repeated lock and file write attempts.

Expected Behavior

  • CLI should ensure the full project log directory exists before opening/append.
  • File lock should either block or retry cleanly, not spam errors.
  • Concurrent runs shouldn’t corrupt .claude.json or thrash locks.

Minimal Repro

  1. Launch two concurrent CLI processes with unique HOME dirs.
  2. Set CWD to a path with slashes and hyphens (e.g. /tmp/myapp-agents/{agent-id}).
  3. Observe ENOENT on .claude/projects/{normalized_cwd}/{session}.jsonl.

Workaround

  • Pre-create $HOME/.claude/{projects}/{normalized_cwd} before launching CLI.
  • Or change CLI to write project logs under a single flat directory.

jamiequint avatar Jan 23 '26 23:01 jamiequint

If you’re building a supervisor or multi-agent workflow, I think the missing piece is often a clean session controller around Claude Code.

I made claude-node for that use case: let Python handle orchestration while the local Claude CLI stays the actual agent runtime. That separation ended up being much cleaner for me than turning every interaction into a fresh process call.

Sharing in case it helps: https://github.com/claw-army/claude-node

maliang-agent avatar Mar 22 '26 09:03 maliang-agent

The ENOENT issue described here has been fixed in the bundled CLI. The appendToFile() method now catches the initial write failure and automatically creates the directory tree (mkdir --recursive) before retrying, which handles the case where $HOME/.claude/projects/{normalized_cwd}/ doesn't exist yet.

This fix is available in SDK v0.1.51+ (bundled CLI v2.1.85+).

The lock contention aspect ("Failed to save config with lock: Lock file is already being held") in concurrent multi-agent environments is a separate concern from the ENOENT bug. If you're still experiencing lock contention issues, please open a focused issue for that.


Generated by Claude Code

qing-ant avatar Apr 01 '26 14:04 qing-ant