From 74d99f87056f2acead4f04f77b557b23798321a9 Mon Sep 17 00:00:00 2001 From: qbot Date: Thu, 22 Oct 2026 10:22:25 +0000 Subject: [PATCH] fix(media): derive agent workspace root from glob-free prefix of cwd_whitelist cwd_whitelist entries are fnmatch patterns (`~/workspace/*`), but _agent_cwd() only ran expanduser() on the first entry, so media_in/ and the outbound media sentinel used a literal `*` directory (`~/workspace/*/media_in`). _glob_root() now strips the glob segment. Also covers sending files prematurely: sentinel scan of the literal dir never saw agent output. --- src/agenthub/session.py | 25 ++++++++++++-- tests/test_workspace_root.py | 66 ++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 tests/test_workspace_root.py diff --git a/src/agenthub/session.py b/src/agenthub/session.py index 0ed5ffb..8b5d0a9 100644 --- a/src/agenthub/session.py +++ b/src/agenthub/session.py @@ -519,10 +519,10 @@ class SessionManager: # -- media --------------------------------------------------------------- def _agent_cwd(self, agent_name: str) -> str | None: - """Agent workspace root: first whitelist entry (expanded) or None.""" + """Agent workspace root: base dir of the first whitelist entry, or None.""" spec = self.agents.get(agent_name) if spec and spec.cwd_whitelist: - return os.path.expanduser(spec.cwd_whitelist[0]) + return _glob_root(spec.cwd_whitelist[0]) return None def _media_dir(self, agent: str) -> str: @@ -960,6 +960,27 @@ class SessionManager: # --------------------------------------------------------------------------- +def _glob_root(pattern: str) -> str: + """Base directory of a cwd whitelist pattern (``~`` expanded). + + Whitelist entries are fnmatch patterns such as ``~/workspace/*``; the agent + workspace root must be the longest glob-free prefix, otherwise media files + land in a literal ``*`` directory and the media sentinel scans nothing. + """ + pattern = os.path.expanduser(pattern) + if not any(ch in pattern for ch in "*?["): + return pattern + parts: list[str] = [] + for part in pattern.split(os.sep): + if any(ch in part for ch in "*?["): + break + parts.append(part) + root = os.sep.join(parts) + if not root: + return os.sep if os.path.isabs(pattern) else "." + return root + + def _check_cwd(cwd: str, whitelist: list[str]) -> None: """Enforce the agent's cwd whitelist (fnmatch patterns, ~ expanded).""" if not whitelist: diff --git a/tests/test_workspace_root.py b/tests/test_workspace_root.py new file mode 100644 index 0000000..b21e8fb --- /dev/null +++ b/tests/test_workspace_root.py @@ -0,0 +1,66 @@ +"""Agent workspace root derivation from ``cwd_whitelist`` patterns. + +Regression: whitelist entries are fnmatch patterns (``~/workspace/*``), so a +naive ``expanduser`` of the first entry produced a literal ``*`` directory and +every inbound attachment landed in ``~/workspace/*/media_in``. +""" +from __future__ import annotations + +import os + +import pytest + +from agenthub.session import SessionManager, _glob_root + + +def test_glob_root_strips_glob_segment(): + assert _glob_root("/home/qbot/workspace/*") == "/home/qbot/workspace" + assert _glob_root("/home/qbot/projects/*") == "/home/qbot/projects" + + +def test_glob_root_keeps_plain_path(): + assert _glob_root("/home/qbot/workspace") == "/home/qbot/workspace" + + +def test_glob_root_expands_home(monkeypatch, tmp_path): + monkeypatch.setenv("HOME", str(tmp_path)) + assert _glob_root("~/workspace/*") == str(tmp_path / "workspace") + + +def test_glob_root_mid_path_glob(): + assert _glob_root("/home/*/workspace") == "/home" + + +def test_glob_root_edge_cases(): + assert _glob_root("/*") == os.sep + assert _glob_root("*.py") == "." + assert _glob_root("/home/qbot/workspace/??") == "/home/qbot/workspace" + + +@pytest.fixture() +def mgr(tmp_path, monkeypatch): + from agenthub.session import AgentSpec + + monkeypatch.setenv("HOME", str(tmp_path)) + (tmp_path / "workspace").mkdir() + return SessionManager( + { + "deepseek": AgentSpec( + name="deepseek", + cmd=["opencode acp"], + cwd_whitelist=[str(tmp_path / "workspace") + "/*", str(tmp_path / "projects") + "/*"], + ) + }, + db_path=str(tmp_path / "s.db"), + ) + + +def test_media_dir_not_literal_star(mgr, tmp_path): + d = mgr._media_dir("deepseek") + assert d == str(tmp_path / "workspace" / "media_in") + assert os.path.isdir(d) + assert not (tmp_path / "workspace" / "*").exists() + + +def test_agent_cwd_is_glob_base(mgr, tmp_path): + assert mgr._agent_cwd("deepseek") == str(tmp_path / "workspace")