Compare commits
3
Commits
0f7adb7966
...
e07f37fbb4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e07f37fbb4 | ||
|
|
7ba5d75f3a | ||
|
|
74d99f8705 |
No files matched your search
@@ -5,3 +5,8 @@ dist/
|
|||||||
.pytest_cache/
|
.pytest_cache/
|
||||||
config.local.yaml
|
config.local.yaml
|
||||||
node_modules/
|
node_modules/
|
||||||
|
# runtime state + local config backups (never committed)
|
||||||
|
agenthub.db
|
||||||
|
agenthub.db-shm
|
||||||
|
agenthub.db-wal
|
||||||
|
config.json.bak.*
|
||||||
+23
-2
@@ -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:
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
"""Shared test isolation.
|
||||||
|
|
||||||
|
Several tests drive ``SessionManager.prompt(files=...)`` / ``_media_dir()`` with
|
||||||
|
agents that have no ``cwd_whitelist``, which makes the fallback path write to
|
||||||
|
``~/agenthub-media``. Pointing ``HOME`` at a per-test temp dir keeps the suite
|
||||||
|
from littering the real home directory.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def isolated_home(tmp_path, monkeypatch):
|
||||||
|
"""Per-test ``$HOME`` so nothing is written to the real home directory."""
|
||||||
|
home = tmp_path / "home"
|
||||||
|
home.mkdir()
|
||||||
|
monkeypatch.setenv("HOME", str(home))
|
||||||
|
return home
|
||||||
@@ -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")
|
||||||
Reference in new issue
Block a user