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(session): steer / interrupt / is_turn_active for live-turn input

smixsb680162ed5af
diff --git a/src/d_brain/services/claude_session.py b/src/d_brain/services/claude_session.py
index 0a74300..c2581c8 100644
--- a/src/d_brain/services/claude_session.py
+++ b/src/d_brain/services/claude_session.py
@@ -276,6 +276,31 @@ class ClaudeSession:
self._ready_flag.unlink(missing_ok=True)
self._inflight.unlink(missing_ok=True)
+ # ── steering (concurrent input into a live turn) ─────────────────
+
+ def steer(self, text: str) -> None:
+ """Type text into the pane WITHOUT taking the pane lock.
+
+ Used to inject guidance into an in-flight turn (the lock is held by
+ that turn's ask()). The interactive TUI feeds mid-turn input to the
+ model (steer/queue semantics) — VERIFY-LIVE per CLI version.
+ """
+ self._send_text(text)
+ self._send_enter()
+
+ def interrupt(self) -> None:
+ """Stop the current response (TUI-native Escape, no lock).
+
+ C-c is deliberately NOT used here: on an idle prompt a C-c starts the
+ double-C-c exit sequence; Escape only ever cancels the response.
+ """
+ self._tmux("send-keys", "-t", self._target, "Escape")
+
+ def is_turn_active(self) -> bool:
+ """True iff a turn is in flight (the pane lock is held)."""
+ with self._locked(blocking=False) as got:
+ return not got
+
def send_control(self, text: str) -> None:
"""Type a client-side Claude Code command verbatim, fire-and-forget.
diff --git a/tests/test_claude_session.py b/tests/test_claude_session.py
index a2f2648..3a50e40 100644
--- a/tests/test_claude_session.py
+++ b/tests/test_claude_session.py
@@ -362,3 +362,52 @@ def test_ask_wrap_true_still_appends_markers(tmp_path, clock):
res = s.ask("ping", timeout=30)
assert res.ok and res.reply == "PONG"
assert fake.texts and f"<<<R:{rid}>>>" in fake.texts[0]
+
+
+# ── steering: inject input into a LIVE turn (no exclusive lock) ─────────────
+
+
+def test_steer_sends_text_while_lock_is_held(tmp_path, clock):
+ """steer() must work WHILE an ask() holds the pane lock — it types into
+ the live turn, so it must not take the blocking lock (no deadlock)."""
+ import fcntl
+ import os
+
+ fake = FakeTmuxText([READY], exists=True)
+ s = make_session(tmp_path, fake, clock)
+ lock_fd = os.open(tmp_path / ".dbrain" / "pane.lock", os.O_CREAT | os.O_RDWR)
+ fcntl.flock(lock_fd, fcntl.LOCK_EX) # simulate an in-flight ask()
+ try:
+ s.steer("уточнение: пиши короче")
+ finally:
+ fcntl.flock(lock_fd, fcntl.LOCK_UN)
+ os.close(lock_fd)
+ assert fake.texts and "уточнение" in fake.texts[0]
+ assert fake.enter_count() >= 1
+
+
+def test_interrupt_sends_escape(tmp_path, clock):
+ """interrupt() uses the TUI-native Escape (stops the current response);
+ C-c is reserved for the stall path (double C-c would begin app exit)."""
+ fake = FakeTmux([READY], exists=True)
+ s = make_session(tmp_path, fake, clock)
+ s.interrupt()
+ assert any(c[-1] == "Escape" for c in fake.sent_keys())
+ assert not any(c[-1] == "C-c" for c in fake.sent_keys())
+
+
+def test_is_turn_active_reflects_lock_state(tmp_path, clock):
+ import fcntl
+ import os
+
+ fake = FakeTmux([READY], exists=True)
+ s = make_session(tmp_path, fake, clock)
+ assert s.is_turn_active() is False # lock free → no ask in flight
+
+ lock_fd = os.open(tmp_path / ".dbrain" / "pane.lock", os.O_CREAT | os.O_RDWR)
+ fcntl.flock(lock_fd, fcntl.LOCK_EX)
+ try:
+ assert s.is_turn_active() is True
+ finally:
+ fcntl.flock(lock_fd, fcntl.LOCK_UN)
+ os.close(lock_fd)