EPIC R06 (Team Hoa) - workspace/filesystem isolation, no cross-project
mutable state.
R06-T01 domain/workspaces/workspace_session.py
WorkspaceSession - project_id/workspace_root/sandbox_dir/allowed_paths
frozen snapshot + is_allowed(path), same "capture once at submit time"
shape as R04's ConversationExecutionRequest.
R06-T02 infrastructure/persistence/json/{atomic_write,workspace_repository_impl,conversation_repository_impl}.py
Real bug fixed: core/projects.py::save_project and core/history.py's
save_conversation/rename_conversation/set_pinned did a plain
path.write_text(json.dumps(...)) - two syscalls, no atomicity. A crash
between them leaves a half-written file that load_project/load_conversation
then silently treat as "missing". All four now write through
atomic_write.write_json (temp file + os.replace). WorkspaceRepository/
ConversationRepository are thin object-shaped facades over the same
(now-atomic) functions, for future application-layer callers.
NOTE: atomic_write.py is deliberately NOT named atomic_json_file.py -
R02-T01 (Team Nam) claims that filename for the same purpose app-wide;
see the checklist for the consolidation TODO.
R06-T03 infrastructure/filesystem/execution_workspace.py
ExecutionWorkspace names the output_dir/scratch_dir split that already
exists (core/chat_agent.py's flat workspace_root/.scratch) - does not
move anything.
R06-T04 ui/chat_panel.py
The actual race: ChatPanel._persist_session (saves a BACKGROUND turn's
conversation) resolved its save directory via a live
self.ctx.config.history_dir() read at save time. ui/workspace_tab.py::
_load_current mutates that same config field on every project switch, so
a turn still running when the user switched projects got saved into the
NEW project's history folder. Fixed by adding "home_history_dir" to the
per-turn ctx dict (same "home_*" snapshot convention already used for
session id/messages/title), captured at submit time. Verified with a real
offscreen-Qt test, not just a unit double:
tests/integration/test_history_dir_race.py.
R06-T05 application/workspaces/file_workspace_service.py
FileWorkspaceService - the File Explorer / AI Editor entry point for the
same safe read/write/edit operations the agent tool loop has, by calling
core/tools.py::execute_tool directly (same dispatch, same ToolContext
containment, same audit log) rather than reimplementing any of it.
New tests: tests/unit/test_workspace_session.py,
test_atomic_write_and_repositories.py, test_execution_workspace.py,
test_file_workspace_service.py, tests/integration/test_history_dir_race.py
(29 new tests, incl. 2 real offscreen-Qt integration tests).
Suite: 283 passed, 4 pre-existing failures unrelated to R05/R06 (see
checklist). check_imports: PASS. All new files < 400 LOC.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
82 lines
3.8 KiB
Python
82 lines
3.8 KiB
Python
"""FileWorkspaceService - the safe file operations File Explorer and the AI
|
|
File Editor need, outside the agent tool loop (R06-T05).
|
|
|
|
``ui/folder_tab.py`` (File Explorer) and the AI File Editor dialog need the
|
|
exact same guarantees the agent's tools already have — path containment
|
|
inside the workspace, precise context-anchored edits, syntax warnings on a
|
|
bad Python write — but today that logic only exists wired to a model's tool
|
|
call (``core/tools.py::execute_tool``). A UI action that isn't a tool call
|
|
(browsing the tree, applying an AI-suggested diff from a review dialog) has
|
|
no equivalent entry point of its own.
|
|
|
|
This service IS that entry point. It reuses ``core/tools.py::execute_tool``
|
|
verbatim - same dispatch table, same ``ToolContext`` containment check, same
|
|
audit-log entry, same Python-syntax warning on write/edit - rather than
|
|
re-implementing any of it, so a fix to one path fixes both. It only adds the
|
|
:class:`~domain.workspaces.workspace_session.WorkspaceSession` seam: which
|
|
workspace root a call is scoped to is decided by the session, not by
|
|
whichever folder a widget happens to have open.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Dict
|
|
|
|
|
|
class FileWorkspaceService:
|
|
"""File operations scoped to one :class:`WorkspaceSession`.
|
|
|
|
Read-only by name (``list_tree``/``read_preview``) vs. writing
|
|
(``write_file``/``apply_edit``) mirrors the same READ/WRITE split
|
|
``domain/tools/tool_registry.py`` uses for the agent's own tools - a
|
|
caller that only wants to browse never accidentally has write access.
|
|
"""
|
|
|
|
def __init__(self, session) -> None: # WorkspaceSession - see module docstring
|
|
self._session = session
|
|
|
|
def list_tree(self, rel: str = ".") -> Dict[str, Any]:
|
|
"""Entries at ``rel`` (default: the workspace root)."""
|
|
return self._execute("list_dir", {"path": rel})
|
|
|
|
def read_preview(self, rel: str) -> Dict[str, Any]:
|
|
"""A text file's content (truncated by
|
|
``infrastructure/filesystem/file_tools.py::MAX_READ_BYTES``, same as
|
|
the agent's ``read_file`` tool)."""
|
|
return self._execute("read_file", {"path": rel})
|
|
|
|
def write_file(self, rel: str, content: str) -> Dict[str, Any]:
|
|
"""Create or fully overwrite ``rel``."""
|
|
return self._execute("write_file", {"path": rel, "content": content})
|
|
|
|
def apply_edit(self, rel: str, old_string: str, new_string: str,
|
|
replace_all: bool = False) -> Dict[str, Any]:
|
|
"""Replace an exact snippet in an existing file - the same
|
|
context-anchored algorithm the agent's ``edit_file`` tool uses, so an
|
|
AI-suggested diff applies with the same precision and the same
|
|
"old_string not found / ambiguous" failure messages either path
|
|
would give the caller."""
|
|
return self._execute("edit_file", {
|
|
"path": rel, "old_string": old_string, "new_string": new_string,
|
|
"replace_all": replace_all,
|
|
})
|
|
|
|
# -- internals --------------------------------------------------------- #
|
|
def _tool_context(self):
|
|
"""A ``ToolContext`` scoped to this session's workspace root.
|
|
``flatten_writes=False`` (unlike Cowork's agent context) - File
|
|
Explorer must preserve whatever subfolder structure the user is
|
|
actually browsing, not collapse every write into the root."""
|
|
from cowork_local.infrastructure.filesystem.tool_context import ToolContext
|
|
|
|
return ToolContext(self._session.workspace_root, flatten_writes=False)
|
|
|
|
def _execute(self, name: str, args: Dict[str, Any]) -> Dict[str, Any]:
|
|
"""Dispatch through ``core/tools.py::execute_tool`` - see the module
|
|
docstring for why this delegates instead of reimplementing."""
|
|
from cowork_local.core.tools import execute_tool
|
|
|
|
return execute_tool(self._tool_context(), name, args)
|
|
|
|
|
|
__all__ = ["FileWorkspaceService"]
|