Skip to content

An always-on second brain you talk to. Voice notes in Telegram → typed, linked knowledge in your Obsidian vault. Runs 24/7 on the Claude subscription you already have.

feat(tmux): auto-accept the bypass-permissions prompt on fresh installs

smixsf85e11f3646e
diff --git a/src/d_brain/services/claude_session.py b/src/d_brain/services/claude_session.py
index cfd150a..760cfb6 100644
--- a/src/d_brain/services/claude_session.py
+++ b/src/d_brain/services/claude_session.py
@@ -254,6 +254,16 @@ class ClaudeSession:
last_state = state
self._sleep(self._poll_interval)
continue
+ # Bypass-permissions accept screen (fresh config dir): unlike TRUST,
+ # the safe default ❯ sits on "1. No, exit", so we must actively pick
+ # "2. Yes, I accept". Debounced to the transition like TRUST.
+ if state == PaneState.BYPASS_PROMPT:
+ if last_state != PaneState.BYPASS_PROMPT:
+ self._tmux("send-keys", "-t", self._target, "2")
+ self._send_enter()
+ last_state = state
+ self._sleep(self._poll_interval)
+ continue
if state == PaneState.READY:
self._ready_flag.write_text("ready\n")
logger.info("Claude session %s is ready", self.session_name)
diff --git a/src/d_brain/services/tmux_parse.py b/src/d_brain/services/tmux_parse.py
index 40a5ab7..3e4a4de 100644
--- a/src/d_brain/services/tmux_parse.py
+++ b/src/d_brain/services/tmux_parse.py
@@ -30,6 +30,7 @@ class PaneState(str, Enum):
"""Coarse state of the interactive session, read from the pane text."""
TRUST_PROMPT = "trust_prompt" # "Is this a project you trust?" — needs Enter
+ BYPASS_PROMPT = "bypass_prompt" # "Bypass Permissions mode" accept — needs "2"
STARTING = "starting" # welcome box visible, not yet idle
READY = "ready" # idle prompt / bypass-permissions footer
RATE_LIMITED = "rate_limited" # usage limit hit — do NOT kill, wait for reset
@@ -98,6 +99,19 @@ def is_complete(text: str, rid: str) -> bool:
# TRUST anchors on the numbered menu (structural), not the prose sentence,
# so the model describing the trust prompt cannot trigger it.
_TRUST_MENU_RE = re.compile(r"(?m)^\s*(?:❯\s*)?1\.\s+Yes, I trust this folder")
+# First run with --dangerously-skip-permissions on a fresh config dir shows a
+# full-screen "WARNING: Claude Code running in Bypass Permissions mode" with a
+# numbered menu (1. No, exit / 2. Yes, I accept). classify_state knew nothing
+# of it → UNKNOWN → "not ready in 90s" on every fresh install. Accepting needs
+# the active choice "2" (the safe default ❯ sits on "1. No, exit").
+# DOUBLE anchor: the unique warning TITLE *and* the numbered accept line must
+# BOTH be present. "Yes, I accept" alone is reply-natural (a model could emit a
+# numbered consent list), so unlike TRUST's single unique line we also require
+# the verbatim title — no reply reproduces both at once.
+_BYPASS_TITLE_RE = re.compile(
+ r"WARNING: Claude Code running in Bypass Permissions mode"
+)
+_BYPASS_MENU_RE = re.compile(r"(?m)^\s*(?:❯\s*)?2\.\s+Yes, I accept")
_RATE_RE = re.compile(
r"usage limit|rate limit|limit reached|resets at|5-hour limit|weekly limit",
re.I,
@@ -212,6 +226,22 @@ def classify_state(text: str) -> PaneState:
# reproduce verbatim in a reply.
if _TRUST_MENU_RE.search(text):
return PaneState.TRUST_PROMPT
+ # BYPASS is also a full-screen modal at the TOP — match over the whole pane
+ # (chrome below is blank). Require BOTH the unique title and the numbered
+ # accept line, AND the ABSENCE of the idle footer: the real modal appears
+ # BEFORE the normal TUI (no "bypass permissions on" footer yet), while a
+ # working/idle session always shows that footer. Without this guard a reply
+ # that quotes the warning from scrollback (capture spans -S -200) would be
+ # taken for the modal — and the watchdog, which does not list BYPASS_PROMPT
+ # as serviceable, would force-recover a perfectly healthy session.
+ # Checked before READY so the modal's `❯ 1. No, exit` selector (which
+ # matches _IDLE_RE) can't be mistaken for an idle prompt.
+ if (
+ _BYPASS_TITLE_RE.search(text)
+ and _BYPASS_MENU_RE.search(text)
+ and not _FOOTER_RE.search(text)
+ ):
+ return PaneState.BYPASS_PROMPT
chrome = _chrome(text)
if _RATE_RE.search(chrome):
return PaneState.RATE_LIMITED
diff --git a/tests/test_claude_session.py b/tests/test_claude_session.py
index 926e058..7b29b46 100644
--- a/tests/test_claude_session.py
+++ b/tests/test_claude_session.py
@@ -22,6 +22,11 @@ TRUST = (
" Quick safety check: Is this a project you created or one you trust?\n"
" ❯ 1. Yes, I trust this folder\n 2. No, exit\n"
)
+BYPASS = (
+ " WARNING: Claude Code running in Bypass Permissions mode\n\n"
+ " In Bypass Permissions mode, Claude Code will not ask for your approval.\n"
+ " ❯ 1. No, exit\n 2. Yes, I accept\n"
+)
THINKING = " ✻ Working… (esc to interrupt)\n"
RATE = " You've reached your usage limit. Your limit resets at 3:00 PM.\n❯\n"
LOGGED_OUT = " Invalid API key · Please run /login to authenticate.\n❯\n"
@@ -134,6 +139,29 @@ def test_ensure_session_does_not_enter_spam_on_trust(tmp_path, clock):
assert fake.enter_count() == 1
+def _digit_sends(fake: FakeTmux, digit: str) -> int:
+ return sum(1 for c in fake.sent_keys() if c[-1] == digit)
+
+
+def test_ensure_session_accepts_bypass_prompt(tmp_path, clock):
+ """Fresh config dir under --dangerously-skip-permissions shows the bypass
+ accept screen; the session must actively pick "2. Yes, I accept" (the safe
+ default sits on "1. No, exit") and then reach READY."""
+ fake = FakeTmux([BYPASS, READY], exists=False)
+ s = make_session(tmp_path, fake, clock)
+ s.ensure_session()
+ assert _digit_sends(fake, "2") == 1
+ assert (tmp_path / ".dbrain" / "ready").exists()
+
+
+def test_ensure_session_does_not_spam_bypass(tmp_path, clock):
+ """Bypass persists for several captures; "2" debounced to the transition."""
+ fake = FakeTmux([BYPASS, BYPASS, BYPASS, READY], exists=False)
+ s = make_session(tmp_path, fake, clock)
+ s.ensure_session()
+ assert _digit_sends(fake, "2") == 1
+
+
def test_ensure_session_raises_if_never_ready(tmp_path, clock):
fake = FakeTmux([THINKING], exists=False)
s = make_session(tmp_path, fake, clock)
diff --git a/tests/test_tmux_parse.py b/tests/test_tmux_parse.py
index a874e87..b92a26f 100644
--- a/tests/test_tmux_parse.py
+++ b/tests/test_tmux_parse.py
@@ -51,6 +51,20 @@ TRUST_CAPTURE = """\
Enter to confirm · Esc to cancel
"""
+# Verbatim from the binary (v2.1.179): title, body and the numbered menu shown
+# on first run with --dangerously-skip-permissions on a fresh config dir.
+BYPASS_CAPTURE = """\
+ WARNING: Claude Code running in Bypass Permissions mode
+
+ In Bypass Permissions mode, Claude Code will not ask for your approval before
+ running potentially dangerous commands.
+ This mode should only be used in a sandboxed container/VM that has restricted
+ internet access and can easily be restored if damaged.
+
+ ❯ 1. No, exit
+ 2. Yes, I accept
+"""
+
STARTING_CAPTURE = """\
╭─── Claude Code v2.1.168 ──────────────────────────────────────╮
│ Loading... │
@@ -183,6 +197,58 @@ def test_classify_trust_prompt():
assert classify_state(TRUST_CAPTURE) == PaneState.TRUST_PROMPT
+def test_classify_bypass_prompt():
+ assert classify_state(BYPASS_CAPTURE) == PaneState.BYPASS_PROMPT
+
+
+def test_classify_bypass_not_triggered_by_prose():
+ # A model REPLY that merely describes bypass mode must not be classified as
+ # the accept screen — the title anchor isn't present.
+ prose = (
+ "────────────────────\n"
+ "⏺ Bypass Permissions mode means Claude Code will not ask for approval.\n"
+ " To accept it you would choose 'Yes, I accept'.\n"
+ "────────────────────\n❯\n"
+ " ⏵⏵ bypass permissions on (shift+tab to cycle)\n"
+ )
+ assert classify_state(prose) == PaneState.READY
+
+
+def test_classify_bypass_not_triggered_by_numbered_list_reply():
+ # The dangerous false-positive: a reply that emits a numbered consent list
+ # whose line 2 reads "2. Yes, I accept …". Without the verbatim warning
+ # TITLE it must NOT be taken for the accept screen (else the watchdog's
+ # current_state() would inject "2" into a live session).
+ numbered = (
+ "────────────────────\n"
+ "⏺ Pick one:\n"
+ " 1. No, decline\n"
+ " 2. Yes, I accept the terms\n"
+ "────────────────────\n❯\n"
+ " ⏵⏵ bypass permissions on (shift+tab to cycle)\n"
+ )
+ assert classify_state(numbered) == PaneState.READY
+
+
+def test_classify_bypass_ignored_when_idle_footer_present():
+ # Codex scenario: a completed reply quotes BOTH the verbatim warning title
+ # AND a "2. Yes, I accept" line, sitting in scrollback while the session is
+ # idle (capture spans -S -200). The live idle footer is present, so this is
+ # a healthy READY session, NOT the modal — the real modal never co-exists
+ # with the footer. Misclassifying it would let the watchdog (BYPASS_PROMPT
+ # is not serviceable) force-recover a healthy session.
+ quoted = (
+ "⏺ The first-run screen reads:\n"
+ " WARNING: Claude Code running in Bypass Permissions mode\n"
+ " 1. No, exit\n"
+ " 2. Yes, I accept\n"
+ "────────────────────\n❯\n────────────────────\n"
+ " hello | Opus 4.8 (1M context) | ~/p\n"
+ " ⏵⏵ bypass permissions on (shift+tab to cycle)\n"
+ )
+ assert classify_state(quoted) == PaneState.READY
+
+
def test_classify_ready_with_bypass_footer():
assert classify_state(READY_CAPTURE) == PaneState.READY