mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-04 05:12:38 +02:00
feat: updated file management for main agent
This commit is contained in:
parent
8d50f90060
commit
05ca4c0b9f
27 changed files with 5054 additions and 1803 deletions
|
|
@ -36,11 +36,18 @@ def test_backend_resolver_returns_multi_root_backend_for_single_root(tmp_path: P
|
|||
def test_backend_resolver_uses_cloud_mode_by_default():
|
||||
resolver = build_backend_resolver(FilesystemSelection())
|
||||
backend = resolver(_RuntimeStub())
|
||||
# StateBackend class name check keeps this test decoupled
|
||||
# from internal deepagents runtime class identity.
|
||||
# When no search_space_id is provided we fall back to StateBackend so
|
||||
# sub-agents / tests without DB access still work.
|
||||
assert backend.__class__.__name__ == "StateBackend"
|
||||
|
||||
|
||||
def test_backend_resolver_uses_kb_postgres_in_cloud_with_search_space():
|
||||
resolver = build_backend_resolver(FilesystemSelection(), search_space_id=42)
|
||||
backend = resolver(_RuntimeStub())
|
||||
assert backend.__class__.__name__ == "KBPostgresBackend"
|
||||
assert backend.search_space_id == 42
|
||||
|
||||
|
||||
def test_backend_resolver_returns_multi_root_backend_for_multiple_roots(tmp_path: Path):
|
||||
root_one = tmp_path / "resume"
|
||||
root_two = tmp_path / "notes"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,204 @@
|
|||
"""Unit tests for the SurfSense filesystem middleware new behaviors.
|
||||
|
||||
Covers:
|
||||
* cloud cwd defaults to ``/documents`` and relative paths resolve under it
|
||||
* cloud writes outside ``/documents/`` are rejected unless basename starts
|
||||
with ``temp_``
|
||||
* cloud writes/edits to the anonymous document are rejected (read-only)
|
||||
* helper methods on the middleware (``_resolve_relative``,
|
||||
``_check_cloud_write_namespace``, ``_default_cwd``)
|
||||
|
||||
These tests use ``__new__`` to bypass the heavy ``__init__`` and exercise
|
||||
the helper methods directly so the test surface stays narrow and fast.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from app.agents.new_chat.filesystem_selection import FilesystemMode
|
||||
from app.agents.new_chat.middleware.filesystem import (
|
||||
SurfSenseFilesystemMiddleware,
|
||||
_build_filesystem_system_prompt,
|
||||
_build_tool_descriptions,
|
||||
)
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
def _make_middleware(mode: FilesystemMode = FilesystemMode.CLOUD):
|
||||
middleware = SurfSenseFilesystemMiddleware.__new__(SurfSenseFilesystemMiddleware)
|
||||
middleware._filesystem_mode = mode
|
||||
return middleware
|
||||
|
||||
|
||||
def _runtime(state: dict | None = None) -> SimpleNamespace:
|
||||
return SimpleNamespace(state=state or {})
|
||||
|
||||
|
||||
class TestCloudCwdDefaults:
|
||||
def test_default_cwd_in_cloud_is_documents_root(self):
|
||||
m = _make_middleware()
|
||||
assert m._default_cwd() == "/documents"
|
||||
|
||||
def test_default_cwd_in_desktop_is_root(self):
|
||||
m = _make_middleware(FilesystemMode.DESKTOP_LOCAL_FOLDER)
|
||||
assert m._default_cwd() == "/"
|
||||
|
||||
def test_current_cwd_uses_state_when_set(self):
|
||||
m = _make_middleware()
|
||||
runtime = _runtime({"cwd": "/documents/notes"})
|
||||
assert m._current_cwd(runtime) == "/documents/notes"
|
||||
|
||||
def test_current_cwd_falls_back_to_default(self):
|
||||
m = _make_middleware()
|
||||
runtime = _runtime({})
|
||||
assert m._current_cwd(runtime) == "/documents"
|
||||
|
||||
def test_current_cwd_ignores_invalid(self):
|
||||
m = _make_middleware()
|
||||
runtime = _runtime({"cwd": "not-absolute"})
|
||||
assert m._current_cwd(runtime) == "/documents"
|
||||
|
||||
|
||||
class TestRelativePathResolution:
|
||||
def test_relative_path_resolves_against_cwd(self):
|
||||
m = _make_middleware()
|
||||
runtime = _runtime({"cwd": "/documents/projects"})
|
||||
assert (
|
||||
m._resolve_relative("notes.md", runtime) == "/documents/projects/notes.md"
|
||||
)
|
||||
|
||||
def test_relative_path_with_dotdot(self):
|
||||
m = _make_middleware()
|
||||
runtime = _runtime({"cwd": "/documents/a/b"})
|
||||
assert m._resolve_relative("../c.md", runtime) == "/documents/a/c.md"
|
||||
|
||||
def test_absolute_path_is_kept(self):
|
||||
m = _make_middleware()
|
||||
runtime = _runtime({"cwd": "/documents"})
|
||||
assert m._resolve_relative("/other/x.md", runtime) == "/other/x.md"
|
||||
|
||||
def test_empty_path_returns_cwd(self):
|
||||
m = _make_middleware()
|
||||
runtime = _runtime({"cwd": "/documents/projects"})
|
||||
assert m._resolve_relative("", runtime) == "/documents/projects"
|
||||
|
||||
|
||||
class TestCloudWriteNamespacePolicy:
|
||||
def test_documents_path_allowed(self):
|
||||
m = _make_middleware()
|
||||
runtime = _runtime()
|
||||
assert m._check_cloud_write_namespace("/documents/foo.md", runtime) is None
|
||||
|
||||
def test_documents_root_allowed(self):
|
||||
m = _make_middleware()
|
||||
runtime = _runtime()
|
||||
assert m._check_cloud_write_namespace("/documents", runtime) is None
|
||||
|
||||
def test_temp_basename_anywhere_allowed(self):
|
||||
m = _make_middleware()
|
||||
runtime = _runtime()
|
||||
assert m._check_cloud_write_namespace("/temp_scratch.md", runtime) is None
|
||||
assert m._check_cloud_write_namespace("/foo/temp_x.md", runtime) is None
|
||||
assert m._check_cloud_write_namespace("/documents/temp_x.md", runtime) is None
|
||||
|
||||
def test_other_paths_rejected(self):
|
||||
m = _make_middleware()
|
||||
runtime = _runtime()
|
||||
err = m._check_cloud_write_namespace("/foo/bar.md", runtime)
|
||||
assert err is not None
|
||||
assert "must target /documents" in err
|
||||
|
||||
def test_anon_doc_path_is_read_only(self):
|
||||
m = _make_middleware()
|
||||
runtime = _runtime(
|
||||
{
|
||||
"kb_anon_doc": {
|
||||
"path": "/documents/uploaded.xml",
|
||||
"title": "uploaded",
|
||||
"content": "",
|
||||
"chunks": [],
|
||||
}
|
||||
}
|
||||
)
|
||||
err = m._check_cloud_write_namespace("/documents/uploaded.xml", runtime)
|
||||
assert err is not None
|
||||
assert "read-only" in err
|
||||
|
||||
def test_desktop_mode_skips_namespace_policy(self):
|
||||
m = _make_middleware(FilesystemMode.DESKTOP_LOCAL_FOLDER)
|
||||
runtime = _runtime()
|
||||
assert m._check_cloud_write_namespace("/random/path.md", runtime) is None
|
||||
|
||||
|
||||
class TestModeSpecificPrompts:
|
||||
"""The prompt and tool descriptions must only describe the active mode.
|
||||
|
||||
Cross-mode noise wastes tokens and confuses the model with rules it
|
||||
cannot use this session.
|
||||
"""
|
||||
|
||||
def test_cloud_prompt_omits_desktop_section(self):
|
||||
prompt = _build_filesystem_system_prompt(
|
||||
FilesystemMode.CLOUD, sandbox_available=False
|
||||
)
|
||||
assert "Local Folder Mode" not in prompt
|
||||
assert "mount-prefixed" not in prompt
|
||||
assert "Persistence Rules" in prompt
|
||||
assert "/documents" in prompt
|
||||
assert "temp_" in prompt
|
||||
|
||||
def test_desktop_prompt_omits_cloud_persistence_rules(self):
|
||||
prompt = _build_filesystem_system_prompt(
|
||||
FilesystemMode.DESKTOP_LOCAL_FOLDER, sandbox_available=False
|
||||
)
|
||||
assert "Persistence Rules" not in prompt
|
||||
assert "Workspace Tree" not in prompt
|
||||
assert "<priority_documents>" not in prompt
|
||||
assert "Local Folder Mode" in prompt
|
||||
assert "mount-prefixed" in prompt
|
||||
|
||||
def test_cloud_tool_descs_omit_desktop_phrases(self):
|
||||
descs = _build_tool_descriptions(FilesystemMode.CLOUD)
|
||||
for name in (
|
||||
"write_file",
|
||||
"edit_file",
|
||||
"move_file",
|
||||
"mkdir",
|
||||
"list_tree",
|
||||
"grep",
|
||||
):
|
||||
text = descs[name]
|
||||
assert "Desktop" not in text, f"{name} leaks desktop hints"
|
||||
assert "Cloud mode:" not in text, f"{name} qualifies a cloud-only desc"
|
||||
|
||||
def test_desktop_tool_descs_omit_cloud_phrases(self):
|
||||
descs = _build_tool_descriptions(FilesystemMode.DESKTOP_LOCAL_FOLDER)
|
||||
for name in (
|
||||
"write_file",
|
||||
"edit_file",
|
||||
"move_file",
|
||||
"mkdir",
|
||||
"list_tree",
|
||||
"grep",
|
||||
):
|
||||
text = descs[name]
|
||||
assert "Cloud" not in text, f"{name} leaks cloud hints"
|
||||
assert "/documents/" not in text, f"{name} mentions cloud namespace"
|
||||
assert "temp_" not in text, f"{name} mentions cloud temp_ semantics"
|
||||
|
||||
def test_sandbox_addendum_appended_when_available(self):
|
||||
prompt = _build_filesystem_system_prompt(
|
||||
FilesystemMode.CLOUD, sandbox_available=True
|
||||
)
|
||||
assert "execute_code" in prompt
|
||||
assert "Code Execution" in prompt
|
||||
|
||||
def test_sandbox_addendum_absent_when_unavailable(self):
|
||||
prompt = _build_filesystem_system_prompt(
|
||||
FilesystemMode.CLOUD, sandbox_available=False
|
||||
)
|
||||
assert "execute_code" not in prompt
|
||||
|
|
@ -11,25 +11,6 @@ from app.agents.new_chat.middleware.multi_root_local_folder_backend import (
|
|||
pytestmark = pytest.mark.unit
|
||||
|
||||
|
||||
class _BackendWithRawRead:
|
||||
def __init__(self, content: str) -> None:
|
||||
self._content = content
|
||||
|
||||
def read(self, file_path: str, offset: int = 0, limit: int = 200000) -> str:
|
||||
del file_path, offset, limit
|
||||
return " 1\tline1\n 2\tline2"
|
||||
|
||||
async def aread(self, file_path: str, offset: int = 0, limit: int = 200000) -> str:
|
||||
return self.read(file_path, offset, limit)
|
||||
|
||||
def read_raw(self, file_path: str) -> str:
|
||||
del file_path
|
||||
return self._content
|
||||
|
||||
async def aread_raw(self, file_path: str) -> str:
|
||||
return self.read_raw(file_path)
|
||||
|
||||
|
||||
class _RuntimeNoSuggestedPath:
|
||||
state = {"file_operation_contract": {}}
|
||||
|
||||
|
|
@ -39,40 +20,19 @@ class _RuntimeWithSuggestedPath:
|
|||
self.state = {"file_operation_contract": {"suggested_path": suggested_path}}
|
||||
|
||||
|
||||
def test_verify_written_content_prefers_raw_sync() -> None:
|
||||
middleware = SurfSenseFilesystemMiddleware.__new__(SurfSenseFilesystemMiddleware)
|
||||
expected = "line1\nline2"
|
||||
backend = _BackendWithRawRead(expected)
|
||||
|
||||
verify_error = middleware._verify_written_content_sync(
|
||||
backend=backend,
|
||||
path="/note.md",
|
||||
expected_content=expected,
|
||||
)
|
||||
|
||||
assert verify_error is None
|
||||
|
||||
|
||||
def test_contract_suggested_path_falls_back_to_notes_md() -> None:
|
||||
def test_contract_suggested_path_falls_back_to_documents_notes_md() -> None:
|
||||
middleware = SurfSenseFilesystemMiddleware.__new__(SurfSenseFilesystemMiddleware)
|
||||
middleware._filesystem_mode = FilesystemMode.CLOUD
|
||||
suggested = middleware._get_contract_suggested_path(_RuntimeNoSuggestedPath()) # type: ignore[arg-type]
|
||||
assert suggested == "/notes.md"
|
||||
# Cloud default cwd is /documents so the fallback lands in the KB.
|
||||
assert suggested == "/documents/notes.md"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_verify_written_content_prefers_raw_async() -> None:
|
||||
def test_contract_suggested_path_falls_back_to_root_notes_md_in_desktop() -> None:
|
||||
middleware = SurfSenseFilesystemMiddleware.__new__(SurfSenseFilesystemMiddleware)
|
||||
expected = "line1\nline2"
|
||||
backend = _BackendWithRawRead(expected)
|
||||
|
||||
verify_error = await middleware._verify_written_content_async(
|
||||
backend=backend,
|
||||
path="/note.md",
|
||||
expected_content=expected,
|
||||
)
|
||||
|
||||
assert verify_error is None
|
||||
middleware._filesystem_mode = FilesystemMode.DESKTOP_LOCAL_FOLDER
|
||||
suggested = middleware._get_contract_suggested_path(_RuntimeNoSuggestedPath()) # type: ignore[arg-type]
|
||||
assert suggested == "/notes.md"
|
||||
|
||||
|
||||
def test_normalize_local_mount_path_prefixes_default_mount(tmp_path: Path) -> None:
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ import json
|
|||
import pytest
|
||||
from langchain_core.messages import AIMessage, HumanMessage
|
||||
|
||||
from app.agents.new_chat.document_xml import build_document_xml as _build_document_xml
|
||||
from app.agents.new_chat.middleware.knowledge_search import (
|
||||
KBSearchPlan,
|
||||
KnowledgeBaseSearchMiddleware,
|
||||
_build_document_xml,
|
||||
_normalize_optional_date_range,
|
||||
_parse_kb_search_plan_response,
|
||||
_render_recent_conversation,
|
||||
|
|
@ -248,17 +248,10 @@ class TestKnowledgeBaseSearchMiddlewarePlanner:
|
|||
captured.update(kwargs)
|
||||
return []
|
||||
|
||||
async def fake_build_scoped_filesystem(**kwargs):
|
||||
return {}, {}
|
||||
|
||||
monkeypatch.setattr(
|
||||
"app.agents.new_chat.middleware.knowledge_search.search_knowledge_base",
|
||||
fake_search_knowledge_base,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"app.agents.new_chat.middleware.knowledge_search.build_scoped_filesystem",
|
||||
fake_build_scoped_filesystem,
|
||||
)
|
||||
|
||||
llm = FakeLLM(
|
||||
json.dumps(
|
||||
|
|
@ -298,17 +291,10 @@ class TestKnowledgeBaseSearchMiddlewarePlanner:
|
|||
captured.update(kwargs)
|
||||
return []
|
||||
|
||||
async def fake_build_scoped_filesystem(**kwargs):
|
||||
return {}, {}
|
||||
|
||||
monkeypatch.setattr(
|
||||
"app.agents.new_chat.middleware.knowledge_search.search_knowledge_base",
|
||||
fake_search_knowledge_base,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"app.agents.new_chat.middleware.knowledge_search.build_scoped_filesystem",
|
||||
fake_build_scoped_filesystem,
|
||||
)
|
||||
|
||||
middleware = KnowledgeBaseSearchMiddleware(
|
||||
llm=FakeLLM("not json"),
|
||||
|
|
@ -334,17 +320,10 @@ class TestKnowledgeBaseSearchMiddlewarePlanner:
|
|||
captured.update(kwargs)
|
||||
return []
|
||||
|
||||
async def fake_build_scoped_filesystem(**kwargs):
|
||||
return {}, {}
|
||||
|
||||
monkeypatch.setattr(
|
||||
"app.agents.new_chat.middleware.knowledge_search.search_knowledge_base",
|
||||
fake_search_knowledge_base,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"app.agents.new_chat.middleware.knowledge_search.build_scoped_filesystem",
|
||||
fake_build_scoped_filesystem,
|
||||
)
|
||||
|
||||
middleware = KnowledgeBaseSearchMiddleware(
|
||||
llm=FakeLLM(
|
||||
|
|
@ -386,9 +365,6 @@ class TestKnowledgeBaseSearchMiddlewarePlanner:
|
|||
search_called = True
|
||||
return []
|
||||
|
||||
async def fake_build_scoped_filesystem(**kwargs):
|
||||
return {}, {}
|
||||
|
||||
monkeypatch.setattr(
|
||||
"app.agents.new_chat.middleware.knowledge_search.browse_recent_documents",
|
||||
fake_browse_recent_documents,
|
||||
|
|
@ -397,10 +373,6 @@ class TestKnowledgeBaseSearchMiddlewarePlanner:
|
|||
"app.agents.new_chat.middleware.knowledge_search.search_knowledge_base",
|
||||
fake_search_knowledge_base,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"app.agents.new_chat.middleware.knowledge_search.build_scoped_filesystem",
|
||||
fake_build_scoped_filesystem,
|
||||
)
|
||||
|
||||
llm = FakeLLM(
|
||||
json.dumps(
|
||||
|
|
@ -440,9 +412,6 @@ class TestKnowledgeBaseSearchMiddlewarePlanner:
|
|||
search_captured.update(kwargs)
|
||||
return []
|
||||
|
||||
async def fake_build_scoped_filesystem(**kwargs):
|
||||
return {}, {}
|
||||
|
||||
monkeypatch.setattr(
|
||||
"app.agents.new_chat.middleware.knowledge_search.browse_recent_documents",
|
||||
fake_browse_recent_documents,
|
||||
|
|
@ -451,10 +420,6 @@ class TestKnowledgeBaseSearchMiddlewarePlanner:
|
|||
"app.agents.new_chat.middleware.knowledge_search.search_knowledge_base",
|
||||
fake_search_knowledge_base,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"app.agents.new_chat.middleware.knowledge_search.build_scoped_filesystem",
|
||||
fake_build_scoped_filesystem,
|
||||
)
|
||||
|
||||
llm = FakeLLM(
|
||||
json.dumps(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue