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.
This commit is contained in:
qbot committed 2026-10-22 18:54:58 +08:00
1 parent 0f7adb7966
commit 74d99f8705
2 files changed
+89 -2

No files matched your search

+23 -2
View File
@@ -519,10 +519,10 @@ class SessionManager:
# -- media --------------------------------------------------------------- # -- media ---------------------------------------------------------------
def _agent_cwd(self, agent_name: str) -> str | None: 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) spec = self.agents.get(agent_name)
if spec and spec.cwd_whitelist: if spec and spec.cwd_whitelist:
return os.path.expanduser(spec.cwd_whitelist[0]) return _glob_root(spec.cwd_whitelist[0])
return None return None
def _media_dir(self, agent: str) -> str: 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: def _check_cwd(cwd: str, whitelist: list[str]) -> None:
"""Enforce the agent's cwd whitelist (fnmatch patterns, ~ expanded).""" """Enforce the agent's cwd whitelist (fnmatch patterns, ~ expanded)."""
if not whitelist: if not whitelist:
+66
View File
@@ -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")