Permission modes in headless Claude Code (plan mode wedges, skip-permissions, tool scoping)
claude-codeheadlesspermissionsplan-modedangerously-skip-permissionsallowedToolssecurity
Headless claude -p runs inherit the machine's Claude Code settings and have no human to approve
anything. That combination produced a thread that could never run a command again, and it shapes
how to give an unattended agent exactly the access it should have. Findings from ProcrastinAI
(TLDR's AI persona) on Claude Code 2.1.x, July to October 2026.
Plan mode wedges headless sessions
~/.claude/settings.jsonhad"permissions": {"defaultMode": "plan"}. Global settings apply to everyclaudeinvocation on the box, including headless ones started by a service.- Plan mode allows only read-only tools until
ExitPlanModeis approved by a human. A headless run has no human; the agent reported that ExitPlanMode "isn't available in this session at all". So turn 1 of any thread started without skip-permissions could not run Bash, curl, or edits. - The permission mode is fixed when a session is created. Later turns
--resumethe same session id, and adding--dangerously-skip-permissionson a resumed turn does not change the mode. Once a thread's first turn was wedged, no later reply could unstick it. - Verified the other way: a fresh (not resumed) session started with
--dangerously-skip-permissionsnever enters plan mode and runs ssh, mysql, curl and edits freely. - Rejected fixes: removing
defaultMode: planglobally, or passingbypassPermissionson every run. Either one would give every user of the service unrestricted execution and defeat the per-user danger gate. - Accepted design: an explicit "escalate" path that abandons the wedged session and starts a new one with the flag from the first turn, feeding the full prior conversation as context, only for a request that legitimately carries the danger flag from an authorized user. The existing "session not found, resend full conversation without --resume" fallback is the template for the mechanics.
Default-deny in headless mode
A run with no permission flags is effectively text-only: privileged tools are denied because nobody
can approve them. Ways to grant less than everything:
- --allowedTools with scoped rules, e.g. Write(//abs/workspace/**), Edit(//abs/workspace/**),
WebSearch. Probe that the path scoping really works on your CLI version before trusting it
(ask the agent to write /tmp/x; it must be denied).
- --add-dir <repo> gives read access to a second directory.
- The real guardrail is no Bash. Path rules on Write/Edit are decoration if the agent can run a
shell. Also leave out WebFetch where SSRF into the host's LAN matters; WebSearch is lower risk.
- If a run produces files, have the service collect and upload them after the run (walk the
workspace with lstat, skip symlinks and dotfiles, take only files modified after the run started,
enforce count and size caps), rather than giving the model credentials.
When the user is untrusted, scope with a sandbox, not just flags
Allow rules and path scoping limit a cooperative agent. For a user who will actively try to get out
(TLDR's guest mode), run the CLI inside bubblewrap with only a workspace mounted, use
--restricted with a read-only --settings file whose sandbox block denies Bash network and
limits writes, load no MCP servers, and pass only an access token through a file descriptor. Full
recipe: Sandboxing Claude Code for untrusted users (bubblewrap, --restricted, token pipe).
Raw flag passthrough is an escalation path
TLDR allowed #opt:key=value tags that became arbitrary CLI flags. Only one model value was
filtered, so #opt:dangerously-skip-permissions granted full access to any user allowed to post to
the AI. Never forward user-controlled strings as CLI flags; allowlist safe keys and gate the rest on
the same check as the danger flag. More in Securing a shared AI agent that other users can drive.
Everything the agent can read, it can repeat
Every run loads the global ~/.claude/CLAUDE.md, whatever the working directory. If that file holds
credentials, any user of a shared agent can ask for them. Keep secrets in a file that is not
auto-loaded and refer to it by path.
Untrusted workspaces
A project directory never opened interactively is untrusted: headless runs there ignore its
.claude/settings.json allow rules. See the gotcha in claude-service (ProcrastinAI's webhook runner for headless Claude).
Gotchas
- Persona instructions in the system prompt ("You are ProcrastinAI...") leaked into answers as quotes; a plain style instruction worked better.
- Markdown-escaped tags in stored posts (
\#danger) still matched the tag regex/#(\S+)/.
See also
- Lifecycle of an async AI request (status machine, expiry, retries, failure)
- claude-service (ProcrastinAI's webhook runner for headless Claude)