Skip to content

The agent that grows with you

fix(delegation): type-validate the parent db_path before pinning the live home

Brooklyn Nicholson4f37880f3d84
diff --git a/tests/tools/test_delegation_live_log.py b/tests/tools/test_delegation_live_log.py
index 2946fa9ac73..1e166b56200 100644
--- a/tests/tools/test_delegation_live_log.py
+++ b/tests/tools/test_delegation_live_log.py
@@ -344,6 +344,38 @@ def test_parent_session_db_pins_home():
assert _parent_live_home(parent) == Path("/prof")
+def test_mock_parent_db_path_is_not_pinned(caplog):
+ """Regression (PR #131931 side-effect screen): a duck-typed parent whose
+ ``_session_db.db_path`` is auto-created by a Mock is not a real path —
+ pinning to it would send transcripts to ``str(<MagicMock>)`` paths. The
+ pin must fall back to None (ambient resolve), and the skip must leave a
+ trace. Note the check is for concrete ``str``/``Path``, NOT the
+ ``os.PathLike`` protocol: MagicMock registers ``__fspath__`` and so IS
+ PathLike, which is exactly how the garbage pin slipped through."""
+ import logging as _logging
+
+ from tools.delegate_tool import _parent_live_home
+
+ parent = MagicMock() # plain mock: _session_db.db_path auto-creates a Mock
+
+ with caplog.at_level(_logging.DEBUG, logger="tools.delegate_tool"):
+ assert _parent_live_home(parent) is None
+ assert any("home pinning skipped" in r.getMessage() for r in caplog.records), (
+ "the skipped pin must leave a log trace"
+ )
+
+
+def test_parent_db_path_accepts_str_paths():
+ """SessionDB always holds a Path, but a str db_path is a valid path value
+ too — it must pin, not be rejected by the concrete-type guard."""
+ from tools.delegate_tool import _parent_live_home
+
+ parent = MagicMock(spec=["_session_db"])
+ parent._session_db.db_path = "/prof/state.db"
+
+ assert _parent_live_home(parent) == Path("/prof")
+
+
def test_prune_sweeps_the_pinned_root_not_ambient(tmp_path, monkeypatch):
"""Retention must clean where the dispatch writes: stale dirs under the
pinned home are removed by that home's dispatches; a stale dir under the
diff --git a/tools/delegate_tool.py b/tools/delegate_tool.py
index bcf9c920d98..02cc4a44216 100644
--- a/tools/delegate_tool.py
+++ b/tools/delegate_tool.py
@@ -65,13 +65,25 @@ def _parent_live_home(parent_agent: Any) -> Optional[Path]:
The parent's per-profile SessionDB sits directly under its profile home
(``<home>/state.db``), so the db path's parent IS the home. Returns None
- when the parent exposes no SessionDB — the caller then falls back to the
- ambient resolve, which is exactly the #91996 failure mode, so the skip is
- logged rather than silent.
+ when the parent exposes no usable SessionDB — the caller then falls back
+ to the ambient resolve, which is exactly the #91996 failure mode, so the
+ skip is logged rather than silent.
"""
parent_db = getattr(getattr(parent_agent, "_session_db", None), "db_path", None)
+ # Concrete str/Path only — NOT the os.PathLike protocol: MagicMock (and any
+ # duck-typed test double) registers __fspath__ and so IS PathLike, which is
+ # how a Mock "home" slipped through and transcripts landed at
+ # str(<MagicMock>) paths (PR #131931 side-effect screen).
+ if isinstance(parent_db, (str, Path)):
+ return Path(parent_db).parent
if parent_db is not None:
- return parent_db.parent
+ logger.debug(
+ "delegate_task: parent _session_db.db_path is %r (not a str/Path); "
+ "live-transcript home pinning skipped, falling back to ambient "
+ "HERMES_HOME resolve (transcripts may land in a different profile, #91996)",
+ parent_db,
+ )
+ return None
logger.warning(
"delegate_task: parent agent exposes no _session_db; live-transcript "
"home pinning skipped, falling back to ambient HERMES_HOME resolve "