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.

fix(session): close blind-review residuals on the steering gate

smixs80b2fc80fa1b
diff --git a/scripts/notify.sh b/scripts/notify.sh
index 149b705..7070435 100755
--- a/scripts/notify.sh
+++ b/scripts/notify.sh
@@ -19,7 +19,7 @@ if [ -f "$ENV_FILE" ]; then
fi
MSG="${1:-d-brain alert}"
-CHAT_ID="${ALLOWED_USER_IDS//[\[\]]/}"
+CHAT_ID="${ALLOWED_USER_IDS//[\[\] ]/}" # strip brackets/spaces
CHAT_ID="${CHAT_ID%%,*}" # first id only
if [ -z "${TELEGRAM_BOT_TOKEN:-}" ] || [ -z "$CHAT_ID" ]; then
diff --git a/src/d_brain/services/claude_session.py b/src/d_brain/services/claude_session.py
index 20d542e..831afb2 100644
--- a/src/d_brain/services/claude_session.py
+++ b/src/d_brain/services/claude_session.py
@@ -104,7 +104,14 @@ class ClaudeSession:
self.runtime_dir.mkdir(parents=True, exist_ok=True)
# The dir holds the full pane transcript and turn state — owner-only,
# enforced on every start so pre-existing installs get repaired too.
- os.chmod(self.runtime_dir, 0o700)
+ # A foreign-owned dir (e.g. once created via sudo) must degrade to a
+ # loud warning, not a constructor crash-loop across all services.
+ try:
+ os.chmod(self.runtime_dir, 0o700)
+ except OSError as exc:
+ logger.warning(
+ "could not restrict %s to owner-only: %s", self.runtime_dir, exc
+ )
self.mcp_config = Path(mcp_config) if mcp_config else None
self.system_prompt_file = (
Path(system_prompt_file) if system_prompt_file else None
@@ -400,19 +407,25 @@ class ClaudeSession:
with self._locked() as got:
if not got: # only happens with non-blocking; blocking=True here
return AskResult("error", detail="could not acquire pane lock")
+ # Claim the turn IMMEDIATELY: a stale inflight from a timed-out
+ # earlier turn must not misrepresent this holder to the steering
+ # gate while _ensure_locked() spends up to startup_timeout.
+ self._inflight.write_text(f"{log_id}\n{self._clock()}\n")
try:
self._ensure_locked()
except Exception as exc: # noqa: BLE001 — must never escape ask()
logger.error("ensure_session failed for %s: %s", log_id, exc)
+ self._inflight.unlink(missing_ok=True)
return AskResult("error", detail=f"session start failed: {exc}")
pre = classify_state(self._capture())
if pre == PaneState.RATE_LIMITED:
+ self._inflight.unlink(missing_ok=True)
return AskResult("rate_limited")
if pre == PaneState.LOGGED_OUT:
+ self._inflight.unlink(missing_ok=True)
return AskResult("logged_out")
- self._inflight.write_text(f"{log_id}\n{self._clock()}\n")
self._send_prompt(prompt, rid, wrap=wrap)
last_active = self._clock()
diff --git a/tests/test_claude_session.py b/tests/test_claude_session.py
index 98c9c07..34b10b8 100644
--- a/tests/test_claude_session.py
+++ b/tests/test_claude_session.py
@@ -427,12 +427,22 @@ def test_runtime_dir_is_owner_only(tmp_path, clock):
def test_pane_log_precreated_owner_only_before_pipe(tmp_path, clock):
# pipe-pane appends the FULL Claude transcript via `cat >>` under the
- # tmux server's umask — the file must already exist as 0600.
- fake = FakeTmux([READY], exists=False)
- s = make_session(tmp_path, fake, clock)
- s.ensure_session()
+ # tmux server's umask — the file must ALREADY be 0600 when pipe-pane
+ # starts, not fixed up afterwards.
log = tmp_path / ".dbrain" / "pane.log"
- assert log.exists()
+ seen = {}
+
+ class Spy(FakeTmux):
+ def __call__(self, args, **kwargs): # noqa: ANN001
+ if len(args) > 1 and args[1] == "pipe-pane":
+ seen["mode_at_pipe"] = (
+ log.stat().st_mode & 0o777 if log.exists() else None
+ )
+ return super().__call__(args, **kwargs)
+
+ s = make_session(tmp_path, Spy([READY], exists=False), clock)
+ s.ensure_session()
+ assert seen["mode_at_pipe"] == 0o600
assert (log.stat().st_mode & 0o777) == 0o600
@@ -463,3 +473,28 @@ def test_is_steerable_turn_distinguishes_chat_from_maintenance(tmp_path, clock):
assert s.is_steerable_turn() is False # the nightly pipeline
finally:
fh.close()
+
+
+def test_inflight_claimed_before_session_startup(tmp_path, clock):
+ # A stale inflight left by a timed-out chat turn must not misrepresent
+ # the new holder to the steering gate while session startup (up to
+ # startup_timeout) is still running.
+ inflight = tmp_path / ".dbrain" / "inflight"
+ seen = {}
+
+ class Spy(FakeTmux):
+ def __call__(self, args, **kwargs): # noqa: ANN001
+ if len(args) > 1 and args[1] == "new-session":
+ seen["at_startup"] = (
+ inflight.read_text() if inflight.exists() else None
+ )
+ return super().__call__(args, **kwargs)
+
+ fake = Spy([READY, READY, _complete("rid00001")], exists=False)
+ s = make_session(tmp_path, fake, clock)
+ inflight.write_text("stale-chat-rid\n0.0\n") # leftover from a timeout
+
+ s.ask("nightly run", request_id="maint-process")
+
+ assert seen["at_startup"] is not None
+ assert seen["at_startup"].startswith("maint-process")