claude-code - 💡(How to fix) Fix [BUG] --dangerously-skip-permissions and PreToolUse hooks both bypassed after background task completion [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#47810Fetched 2026-04-15 06:41:40
View on GitHub
Comments
1
Participants
2
Timeline
6
Reactions
0
Timeline (top)
labeled ×5commented ×1

Error Message

Error Messages/Logs

Code Example

## Actual Behavior

After the background task completes:
- The prompt "Permission rule **Bash** requires confirmation for this command" appears
- The hook log shows **no entry** for the blocked command — the hook was never called
- Earlier commands in the same session have hook log entries with `decision=allow`

---

# Hook is being called and returning allow for all commands:
2026-04-14 15:53:33 tool=Bash decision=allow input_bytes=951 elapsed_ms=23 reason=none
2026-04-14 15:53:39 tool=Bash decision=allow input_bytes=849 elapsed_ms=23 reason=none
2026-04-14 15:58:35 tool=Agent decision=allow input_bytes=1203 elapsed_ms=24 reason=none
2026-04-14 16:00:39 tool=Bash decision=allow input_bytes=427 elapsed_ms=21 reason=none
2026-04-14 16:01:01 tool=Bash decision=allow input_bytes=484 elapsed_ms=17 reason=none
# ^^^ last hook entry before the blocked command
# The blocked `cat ... | tail -1` has NO log entry — hook was never invoked

---

{
  "hooks": {
    "PreToolUse": [{
      "matcher": ".*",
      "hooks": [{
        "type": "command",
        "command": "/path/to/hook.sh",
        "timeout": 30
      }]
    }]
  }
}
RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • I am using the latest version of Claude Code

What's Wrong?

When running Claude Code with --dangerously-skip-permissions --permission-mode bypassPermissions, both the CLI flag AND configured PreToolUse hooks stop taking effect after certain events during a session — specifically after background tasks complete.

This is related to #40241 (subagent permission propagation) and #1498 (intermittent permission prompts), but more severe: not only does the CLI flag stop working, but PreToolUse hooks are not even called.

What Should Happen?

Expected Behavior

All tool calls should proceed without permission prompts for the entire session lifetime, since:

  • --dangerously-skip-permissions is set ("Bypass all permission checks")
  • --permission-mode bypassPermissions is set
  • PreToolUse hook returns permissionDecision: "allow" for all tools

Error Messages/Logs

## Actual Behavior

After the background task completes:
- The prompt "Permission rule **Bash** requires confirmation for this command" appears
- The hook log shows **no entry** for the blocked command — the hook was never called
- Earlier commands in the same session have hook log entries with `decision=allow`

Steps to Reproduce

  1. Configure a PreToolUse hook in ~/.claude/settings.json with matcher: ".*" that logs every invocation and returns permissionDecision: "allow"
  2. Start Claude with: claude --dangerously-skip-permissions --permission-mode bypassPermissions
  3. Run several tool calls — they all go through without prompts, and hook log confirms they are being called
  4. Start a background task (e.g., Bash with run_in_background: true)
  5. Wait for the background task to complete
  6. Run any subsequent tool call (e.g., a simple cat file | tail -1)

Evidence from Hook Log

# Hook is being called and returning allow for all commands:
2026-04-14 15:53:33 tool=Bash decision=allow input_bytes=951 elapsed_ms=23 reason=none
2026-04-14 15:53:39 tool=Bash decision=allow input_bytes=849 elapsed_ms=23 reason=none
2026-04-14 15:58:35 tool=Agent decision=allow input_bytes=1203 elapsed_ms=24 reason=none
2026-04-14 16:00:39 tool=Bash decision=allow input_bytes=427 elapsed_ms=21 reason=none
2026-04-14 16:01:01 tool=Bash decision=allow input_bytes=484 elapsed_ms=17 reason=none
# ^^^ last hook entry before the blocked command
# The blocked `cat ... | tail -1` has NO log entry — hook was never invoked

Additional Observations

  • The same pattern occurs after extended idle periods (e.g., leaving terminal for 30+ minutes)
  • Once the permission state "degrades," it does not recover — all subsequent tool calls prompt
  • settings.json also has defaultMode: "bypassPermissions", skipDangerousModePermissionPrompt: true, and sandbox.autoAllowBashIfSandboxed: true — none of these prevent the issue
  • This affects real interactive sessions, not just sandboxed/CI environments

Hook Configuration

{
  "hooks": {
    "PreToolUse": [{
      "matcher": ".*",
      "hooks": [{
        "type": "command",
        "command": "/path/to/hook.sh",
        "timeout": 30
      }]
    }]
  }
}

The hook script logs every invocation to a file and returns {"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"allow"}} for all non-dangerous commands.

Claude Model

Sonnet (default)

Is this a regression?

No, this never worked

Last Working Version

No response

Claude Code Version

v2.1.107

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

Terminal.app (macOS)

Additional Information

No response

extent analysis

TL;DR

The issue can be mitigated by investigating and potentially fixing the permission state degradation after background tasks complete, ensuring that the --dangerously-skip-permissions flag and PreToolUse hooks continue to take effect throughout the session.

Guidance

  1. Review the Claude Code source: Examine the code handling background tasks and permission states to identify why the permission state "degrades" after these tasks complete.
  2. Verify hook configuration: Double-check the PreToolUse hook configuration in ~/.claude/settings.json to ensure it is correctly set up to log every invocation and return permissionDecision: "allow" for all tools.
  3. Test with simplified hooks: Try simplifying the hook script to just log invocations and return allow without any conditions to see if the issue persists, helping to isolate if the problem is with the hook logic or the Claude Code handling of hooks.
  4. Investigate idle period impact: Since the issue also occurs after extended idle periods, investigate how Claude Code handles session state during such periods and if there's a timeout or other mechanism that could be causing the permission state to degrade.

Example

No specific code example can be provided without modifying the existing hook script or Claude Code itself, but a simplified version of the hook script for testing could look like:

{
  "hooks": {
    "PreToolUse": [{
      "matcher": ".*",
      "hooks": [{
        "type": "command",
        "command": "/path/to/simplified_hook.sh",
        "timeout": 30
      }]
    }]
  }
}

With simplified_hook.sh containing minimal logic to log and always return allow.

Notes

The issue seems to be related to how Claude Code manages permission states, especially after background tasks and during idle periods. Without access to the Claude Code source or more detailed logs, it's challenging to provide a definitive fix. The suggestions above aim to help diagnose and potentially mitigate the issue.

Recommendation

Apply a workaround by regularly restarting the Claude Code session or implementing a custom mechanism to periodically refresh the permission state, as the root cause seems to be related to the internal state management of Claude Code, which may require a fix in the codebase itself.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING