[BUG] Agent in headless -p mode infinitely retries on permission denial instead of terminating
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
When running claude with -p flag (headless/non-interactive mode) and --dangerously-skip-permissions, if the agent encounters a permission denial for a "sensitive file" (e.g., writing to .claude/ directory), it does not terminate with an error. Instead, it enters an infinite retry loop — repeatedly attempting the same write operation and receiving the same denial.
In my case, the agent ran for 37+ minutes making 3 identical write attempts to .claude/docs/, consuming API tokens on each retry, until I manually killed the process (kill <pid>, exit code 143/SIGTERM).
What Should Happen?
In headless -p mode, when a permission denial occurs that cannot be resolved without user interaction, the agent should:
- Recognize that no user is available to grant permission
- Terminate with a non-zero exit code and a clear error message in the JSON output (e.g.,
"is_error": truewith details about the permission denial)
This is especially important for automated pipelines and orchestrated multi-agent workflows where a stuck process wastes resources and blocks the pipeline.
Error Messages/Logs
# The agent session log shows 3 identical permission denials:
[35] Write: .claude/docs/agents-md-quality-criteria.md
-> ERROR: "Claude requested permissions to edit ... which is a sensitive file." (is_error: true)
[37] Write: .claude/docs/agents-md-quality-criteria.md
-> ERROR: "Claude requested permissions to edit ... which is a sensitive file." (is_error: true)
[39] Write: .claude/docs/agents-md-quality-criteria.md
-> ERROR: "Claude requested permissions to edit ... which is a sensitive file." (is_error: true)
# Process continued running (37+ min uptime) until manually killed
# Session file grew from 354KB to 576KB during retries
Steps to Reproduce
- Run an agent in headless mode that needs to write to
.claude/directory:
``bash``
claude --dangerously-skip-permissions --output-format json --agent <agent-name> -p "Write a file to .claude/docs/test.md with content 'hello'"
- Observe that the process does not terminate — it retries the write indefinitely
- Check the session log at
~/.claude/projects/<project-path>/<session-id>.jsonlto see repeated permission denials - Process must be killed manually (
kill <pid>)
Claude Model
Opus
Is this a regression?
Yes, this worked in a previous version
Last Working Version
2.1.77
Claude Code Version
2.1.81
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Non-interactive/CI environment
Additional Information
The root cause of the permission denial itself is tracked in #35895 (.claude/ directory writes blocked even with --dangerously-skip-permissions since v2.1.78). However, this issue is about the agent behavior upon receiving the denial — regardless of whether the denial is correct, the agent should not retry infinitely in headless mode.
This affects any orchestrator pattern where claude -p is used to run agents as background processes (e.g., skill-based multi-agent systems).
This issue has 5 comments on GitHub. Read the full discussion on GitHub ↗