Skip to content

The agent that grows with you

fix(delegate): child's dedicated SessionDB must follow the parent's db_path

kshitij70704b962ffa
diff --git a/tests/tools/test_delegate.py b/tests/tools/test_delegate.py
index b0d50c85b92..b71fd985969 100644
--- a/tests/tools/test_delegate.py
+++ b/tests/tools/test_delegate.py
@@ -364,6 +364,55 @@ class TestDelegateTask(unittest.TestCase):
_, kwargs = MockAgent.call_args
self.assertIsNone(kwargs["session_db"])
+ def test_child_dedicated_db_follows_parents_db_path(self):
+ """Per-profile parents: the child's dedicated handle must target the
+ parent's database FILE, not the launch profile's default state.db.
+
+ tui_gateway hands agents dedicated per-profile handles
+ (``SessionDB(db_path=<profile_home>/state.db)`` via
+ ``_transfer_db_to_agent``). A bare ``SessionDB()`` in
+ ``_build_child_agent`` would write the child's transcript into the
+ launch profile's db — cross-profile leakage that breaks
+ ``parent_session_id`` lineage and ``session_search``.
+ """
+ import tempfile
+ from pathlib import Path
+
+ with tempfile.TemporaryDirectory() as tmp:
+ profile_db_path = Path(tmp) / "profile-work" / "state.db"
+ profile_db_path.parent.mkdir(parents=True)
+ parent = _make_mock_parent(depth=0)
+ parent_db = SessionDB(db_path=profile_db_path)
+ parent._session_db = parent_db
+ child_db = None
+ try:
+ with patch("run_agent.AIAgent") as MockAgent:
+ MockAgent.return_value = MagicMock()
+
+ _build_child_agent(
+ task_index=0,
+ goal="test",
+ context=None,
+ toolsets=None,
+ model="test-model",
+ max_iterations=5,
+ parent_agent=parent,
+ task_count=1,
+ )
+
+ _, kwargs = MockAgent.call_args
+
+ child_db = kwargs["session_db"]
+ self.assertIsInstance(child_db, SessionDB)
+ self.assertIsNot(child_db, parent_db)
+ self.assertEqual(
+ str(child_db.db_path), str(parent_db.db_path)
+ )
+ finally:
+ if child_db is not None:
+ child_db.close()
+ parent_db.close()
+
def test_nous_child_rederives_api_mode_from_model(self):
"""Portal is dual-wire — same provider + different model prefix must
not inherit the parent's Messages/chat_completions mode verbatim."""
diff --git a/tools/delegate_tool.py b/tools/delegate_tool.py
index 92afe58e49c..fd47d15133e 100644
--- a/tools/delegate_tool.py
+++ b/tools/delegate_tool.py
@@ -1773,22 +1773,31 @@ def _build_child_agent(
child_optional_kwargs["max_tokens"] = child_max_tokens
# Each child gets a DEDICATED SessionDB connection instead of the parent's
- # live object. The parent's handle is owned by the parent's lifecycle:
- # cron run_job closes its per-job SessionDB in its finally block while a
- # fire-and-forget background delegation subagent is still flushing on a
- # daemon thread, and every subsequent flush then hits the closed handle
- # ('NoneType' object has no attribute 'execute') — the failure is
- # downgraded to a WARNING and the child's entire transcript is silently
- # dropped (#81267). The same teardown-while-child-alive shape exists for
- # gateway session end and /new mid-delegation, so the child must never
- # share the parent's handle. A dedicated handle is released by the
- # child's own close() via the owned flag set below.
+ # live object. The parent's handle is owned by the parent's lifecycle
+ # (cron run_job's finally block, gateway session end, /new) and can be
+ # closed while a fire-and-forget background child is still flushing on a
+ # daemon thread — every subsequent flush then hits the closed handle and
+ # the child's transcript is silently dropped (#81267). A dedicated handle
+ # can't be closed out from under the child; it is released by the child's
+ # own close() via the owned flag set below. It MUST point at the same
+ # database FILE as the parent's handle: parents can hold non-default
+ # per-profile handles (tui_gateway opens SessionDB(db_path=<profile>/
+ # state.db) for non-launch profiles), and a bare SessionDB() would write
+ # the child's transcript into the launch profile's db, breaking
+ # parent_session_id lineage and session_search. AsyncSessionDB wrappers
+ # (gateway) forward .db_path via __getattr__, so this works through them.
child_session_db = None
- if getattr(parent_agent, "_session_db", None) is not None:
+ parent_session_db = getattr(parent_agent, "_session_db", None)
+ if parent_session_db is not None:
try:
from hermes_state import SessionDB
- child_session_db = SessionDB()
+ _parent_db_path = getattr(parent_session_db, "db_path", None)
+ child_session_db = (
+ SessionDB(db_path=_parent_db_path)
+ if _parent_db_path is not None
+ else SessionDB()
+ )
except Exception:
logger.debug(
"subagent: failed to open dedicated SessionDB; child persistence disabled",