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(cron): second brain session factory

smixs62ec812c1877
diff --git a/src/d_brain/services/runtime.py b/src/d_brain/services/runtime.py
index 25392a3..9c2114b 100644
--- a/src/d_brain/services/runtime.py
+++ b/src/d_brain/services/runtime.py
@@ -9,20 +9,23 @@ just avoids piling up blocked worker threads).
import asyncio
import uuid
+from pathlib import Path
from d_brain.config import Settings
from d_brain.services.claude_session import ClaudeSession
from d_brain.services.processor import ClaudeProcessor
_session: ClaudeSession | None = None
+_cron_session: ClaudeSession | None = None
_processor: ClaudeProcessor | None = None
_ask_lock = asyncio.Lock()
def reset() -> None:
"""Drop the singletons (tests only)."""
- global _session, _processor
+ global _session, _cron_session, _processor
_session = None
+ _cron_session = None
_processor = None
@@ -40,11 +43,9 @@ def _persisted_name(settings: Settings) -> str:
return name
-def get_session(settings: Settings) -> ClaudeSession:
- """Return the shared interactive ClaudeSession singleton."""
- global _session
- if _session is not None:
- return _session
+def _build_session(
+ settings: Settings, *, session_name: str, runtime_dir: Path
+) -> ClaudeSession:
project_root = settings.vault_path.parent
mcp = project_root / "mcp-config.json"
brain_prompt = project_root / "deploy" / "brain-system.md"
@@ -58,17 +59,45 @@ def get_session(settings: Settings) -> ClaudeSession:
f"persona file missing or invalid: {brain_prompt} — "
"refusing to start a personality-less brain"
)
- _session = ClaudeSession(
- session_name=_persisted_name(settings),
+ return ClaudeSession(
+ session_name=session_name,
work_dir=settings.vault_path,
- runtime_dir=settings.runtime_dir,
+ runtime_dir=runtime_dir,
mcp_config=mcp if mcp.exists() else None,
system_prompt_file=brain_prompt,
model=settings.claude_model or None,
)
+
+
+def get_session(settings: Settings) -> ClaudeSession:
+ """Return the shared interactive ClaudeSession singleton."""
+ global _session
+ if _session is None:
+ _session = _build_session(
+ settings,
+ session_name=_persisted_name(settings),
+ runtime_dir=settings.runtime_dir,
+ )
return _session
+def get_cron_session(settings: Settings) -> ClaudeSession:
+ """Return the cron brain — a second, isolated ClaudeSession.
+
+ Same persona and vault as the main brain, but its own tmux session and
+ its own runtime dir (pane.lock / pane.log / ready), so scheduled jobs
+ never block or pollute the user's conversation.
+ """
+ global _cron_session
+ if _cron_session is None:
+ _cron_session = _build_session(
+ settings,
+ session_name=f"{_persisted_name(settings)}_cron",
+ runtime_dir=settings.cron_dir,
+ )
+ return _cron_session
+
+
def get_processor(settings: Settings) -> ClaudeProcessor:
global _processor
if _processor is None:
diff --git a/tests/test_runtime.py b/tests/test_runtime.py
index 476bb1e..e5ba745 100644
--- a/tests/test_runtime.py
+++ b/tests/test_runtime.py
@@ -50,6 +50,39 @@ def test_explicit_session_name_used(tmp_path):
assert rt.get_session(s).session_name == "dbrain_fixed"
+def test_get_cron_session_is_isolated_sibling(tmp_path):
+ """The cron brain is a SECOND ClaudeSession: same persona and vault,
+ but its own tmux session name and its own runtime dir, so pane.lock /
+ pane.log / ready never collide with the main brain's."""
+ rt.reset()
+ s = _settings(tmp_path)
+ main = rt.get_session(s)
+ cron = rt.get_cron_session(s)
+ assert cron is not main
+ assert cron.session_name == f"{main.session_name}_cron"
+ assert cron.runtime_dir == s.cron_dir
+ assert cron.runtime_dir != main.runtime_dir
+ assert cron.work_dir == main.work_dir
+
+
+def test_get_cron_session_is_singleton_and_reset_clears(tmp_path):
+ rt.reset()
+ s = _settings(tmp_path)
+ c1 = rt.get_cron_session(s)
+ assert rt.get_cron_session(s) is c1
+ rt.reset()
+ assert rt.get_cron_session(s) is not c1
+
+
+def test_get_cron_session_refuses_without_persona(tmp_path):
+ import pytest
+
+ rt.reset()
+ s = _settings(tmp_path, persona=False)
+ with pytest.raises(RuntimeError, match="persona"):
+ rt.get_cron_session(s)
+
+
def test_get_session_refuses_without_persona(tmp_path):
"""runtime.py used to silently pass system_prompt_file=None when the
persona file is missing — booting a personality-less vanilla agent.