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>
81 lines
3.3 KiB
Python
81 lines
3.3 KiB
Python
"""ExecutionWorkspace - the output folder vs. the scratch folder for one
|
|
turn, as two distinct properties instead of a name convention (R06-T03).
|
|
|
|
Today the ``.scratch`` subtree is a special case buried inside
|
|
``_flatten_rel`` (``infrastructure/filesystem/file_tools.py``): a generator
|
|
script writes there, the deliverable lands in the output root, and
|
|
``core/chat_agent.py`` cleans ``.scratch`` up after the turn — but nothing
|
|
NAMES "the scratch folder" as a thing; every call site re-derives
|
|
``workdir / ".scratch"`` (or checks ``Path(rel).parts[0] == ".scratch"``) by
|
|
hand. This class gives that convention one home.
|
|
|
|
It does not change WHERE files land - ``workspace_root/.scratch`` stays
|
|
exactly what it always was. It exists so a caller (an application service,
|
|
R06-T05's ``FileWorkspaceService``, or a future turn-cleanup step) can ask
|
|
for "the output dir" / "the scratch dir" instead of hand-building the path
|
|
and hoping the convention hasn't drifted.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import shutil
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
from cowork_local.domain.workspaces import WorkspaceSession
|
|
|
|
SCRATCH_DIRNAME = ".scratch"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ExecutionWorkspace:
|
|
"""The two folders a turn actually writes to, derived from a
|
|
:class:`WorkspaceSession`.
|
|
|
|
``output_dir`` is always the session's ``workspace_root`` itself, not a
|
|
per-turn subfolder - Cowork's whole design is that every deliverable lands
|
|
directly in the one configured Output folder (see
|
|
``infrastructure/filesystem/file_tools.py::_flatten_rel``'s docstring).
|
|
``scratch_dir`` is the SAME flat ``workspace_root/.scratch`` every turn on
|
|
that workspace already shares today (``core/chat_agent.py``'s
|
|
``_cleanup_cowork_intermediates`` operates on that exact path) - this
|
|
class does not introduce per-turn namespacing that doesn't exist in the
|
|
engine yet, only names the existing convention.
|
|
|
|
``turn_id`` is kept as metadata for callers that want to attribute a
|
|
workspace to the turn that used it (logging, future per-turn scratch
|
|
namespacing); it does not affect either path today.
|
|
"""
|
|
|
|
session: WorkspaceSession
|
|
turn_id: str
|
|
|
|
@property
|
|
def output_dir(self) -> Path:
|
|
return self.session.workspace_root
|
|
|
|
@property
|
|
def scratch_dir(self) -> Path:
|
|
return self.session.workspace_root / SCRATCH_DIRNAME
|
|
|
|
def ensure_dirs(self) -> None:
|
|
"""Create both folders if they don't exist yet. Callers that only
|
|
need one (most do) can skip this and let ``write_file`` create parents
|
|
on demand, same as today."""
|
|
self.output_dir.mkdir(parents=True, exist_ok=True)
|
|
self.scratch_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
def cleanup_scratch(self) -> None:
|
|
"""Unconditionally remove the scratch subtree.
|
|
|
|
Coarser than ``core/chat_agent.py::_cleanup_cowork_intermediates``,
|
|
which rescues any real deliverable a generator script wrote INSIDE
|
|
``.scratch`` before wiping it - that rescue logic stays there. This
|
|
is for callers that only need "make the scratch folder go away"
|
|
(e.g. before starting a fresh run) and know it holds nothing worth
|
|
saving.
|
|
"""
|
|
shutil.rmtree(self.scratch_dir, ignore_errors=True)
|
|
|
|
|
|
__all__ = ["ExecutionWorkspace", "SCRATCH_DIRNAME"]
|