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>
65 lines
2.3 KiB
Python
65 lines
2.3 KiB
Python
"""EPIC R06-T01: WorkspaceSession is a frozen snapshot, captured once, that a
|
|
turn keeps using regardless of what the UI does to the live project
|
|
selection afterwards - see the module docstring for the race this replaces.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from cowork_local.domain.workspaces import WorkspaceSession
|
|
|
|
|
|
class _FakeProject:
|
|
def __init__(self, project_id: str, root: Path):
|
|
self.project_id = project_id
|
|
self._root = root
|
|
|
|
def workspace_dir(self) -> Path:
|
|
return self._root
|
|
|
|
|
|
def test_from_project_derives_sandbox_dir_under_the_workspace_root(tmp_path):
|
|
project = _FakeProject("proj-a", tmp_path)
|
|
session = WorkspaceSession.from_project(project)
|
|
|
|
assert session.project_id == "proj-a"
|
|
assert session.workspace_root == tmp_path
|
|
assert session.sandbox_dir == tmp_path / ".scratch"
|
|
assert session.allowed_paths == (tmp_path,)
|
|
|
|
|
|
def test_is_allowed_true_for_the_root_and_descendants(tmp_path):
|
|
session = WorkspaceSession.from_project(_FakeProject("p", tmp_path))
|
|
nested = tmp_path / "sub" / "file.txt"
|
|
nested.parent.mkdir(parents=True)
|
|
nested.write_text("x", encoding="utf-8")
|
|
|
|
assert session.is_allowed(tmp_path) is True
|
|
assert session.is_allowed(nested) is True
|
|
|
|
|
|
def test_is_allowed_false_outside_the_workspace(tmp_path):
|
|
session = WorkspaceSession.from_project(_FakeProject("p", tmp_path / "a"))
|
|
outside = tmp_path / "b" / "secret.txt"
|
|
|
|
assert session.is_allowed(outside) is False
|
|
|
|
|
|
def test_two_sessions_from_different_projects_stay_independent(tmp_path):
|
|
"""The exact race this snapshot exists to prevent: a turn holding session
|
|
A must never start accepting paths that belong to session B, no matter
|
|
what the (mutable, shared) AppContext does after the snapshot was taken."""
|
|
session_a = WorkspaceSession.from_project(_FakeProject("a", tmp_path / "a"))
|
|
session_b = WorkspaceSession.from_project(_FakeProject("b", tmp_path / "b"))
|
|
|
|
assert session_a.is_allowed(tmp_path / "b" / "file.txt") is False
|
|
assert session_b.is_allowed(tmp_path / "a" / "file.txt") is False
|
|
|
|
|
|
def test_unscoped_session_has_no_project_id(tmp_path):
|
|
session = WorkspaceSession.unscoped(tmp_path)
|
|
assert session.project_id == ""
|
|
assert session.is_allowed(tmp_path / "code.py") is True
|