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>
97 lines
4.3 KiB
Python
97 lines
4.3 KiB
Python
"""WorkspaceSession - an immutable snapshot of which project a turn belongs
|
|
to and where it may touch the filesystem (R06-T01).
|
|
|
|
``state.py::AppContext.active_project_id`` is a single mutable field read by
|
|
every background worker thread. ``ui/workspace_tab.py::_load_current`` writes
|
|
it (and the related ``config._project_history_dir``) on the UI thread the
|
|
moment the user switches projects - while a turn already running on a
|
|
worker thread may read either field mid-switch and end up acting on the
|
|
OTHER project's workspace/history for the rest of its run (the race
|
|
R06-T04 fixes).
|
|
|
|
The fix, same shape as R04's ``ConversationExecutionRequest``: capture the
|
|
workspace facts a turn needs ONCE, on the thread that knows which project is
|
|
selected, into one frozen object. Whatever the user does to the UI afterwards,
|
|
the turn keeps using the workspace it was handed at submit time.
|
|
|
|
Pure domain code: stdlib only, no Qt, no network. It does touch ``Path`` (not
|
|
plain strings, unlike ``ConversationExecutionRequest``) because its whole job
|
|
is path-containment checking - a snapshot with no room to answer "is this
|
|
path mine" would not replace what ``ToolContext.resolve`` currently does
|
|
inline.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
from typing import Tuple
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class WorkspaceSession:
|
|
"""Everything a turn needs to know about ITS workspace, fixed at the
|
|
moment it was submitted.
|
|
|
|
Attributes:
|
|
project_id: the project this turn belongs to (``""`` when no project
|
|
is selected - e.g. the Code tab, which has no project concept).
|
|
workspace_root: the project's sandbox root (``Project.workspace_dir()``).
|
|
sandbox_dir: the ``.scratch`` subtree inside ``workspace_root`` used for
|
|
generator/helper scripts, never a final deliverable (see
|
|
``infrastructure/filesystem/file_tools.py::_flatten_rel``).
|
|
allowed_paths: every root a tool call may read/write under. Almost
|
|
always just ``(workspace_root,)``; a project with a custom
|
|
``output_dir`` outside the managed workspace tree still resolves
|
|
to exactly one root - the tuple exists so a future caller (e.g. a
|
|
step scoped to a shared input folder) can widen it without a
|
|
shape change.
|
|
"""
|
|
|
|
project_id: str
|
|
workspace_root: Path
|
|
sandbox_dir: Path
|
|
allowed_paths: Tuple[Path, ...] = field(default_factory=tuple)
|
|
|
|
def __post_init__(self) -> None:
|
|
if not self.allowed_paths:
|
|
object.__setattr__(self, "allowed_paths", (self.workspace_root,))
|
|
|
|
@classmethod
|
|
def from_project(cls, project) -> "WorkspaceSession":
|
|
"""Build a session from a ``core.projects.Project``. ``project`` is
|
|
typed loosely (not imported) so this module has no dependency on
|
|
``core/`` - the caller (``core/projects.py`` itself, or
|
|
``application/conversations``) already has the Project in hand."""
|
|
root = Path(project.workspace_dir())
|
|
return cls(project_id=project.project_id, workspace_root=root,
|
|
sandbox_dir=root / ".scratch", allowed_paths=(root,))
|
|
|
|
@classmethod
|
|
def unscoped(cls, workspace_root: Path) -> "WorkspaceSession":
|
|
"""A session for callers with no project concept (e.g. the Code tab,
|
|
which sandboxes to a plain folder rather than a ``Project``)."""
|
|
root = Path(workspace_root)
|
|
return cls(project_id="", workspace_root=root, sandbox_dir=root / ".scratch")
|
|
|
|
def is_allowed(self, path: Path) -> bool:
|
|
"""True when ``path`` resolves inside one of :attr:`allowed_paths`.
|
|
|
|
Same containment rule as ``ToolContext.resolve`` (an exact root match
|
|
or a real descendant), but side-effect-free: it reports the answer
|
|
instead of raising, so a caller (``FileWorkspaceService``, R06-T05)
|
|
can decide what "not allowed" means for its own UI instead of
|
|
catching a ``ToolError``.
|
|
"""
|
|
try:
|
|
resolved = Path(path).expanduser().resolve()
|
|
except OSError:
|
|
return False
|
|
for allowed in self.allowed_paths:
|
|
root = Path(allowed).resolve()
|
|
if resolved == root or root in resolved.parents:
|
|
return True
|
|
return False
|
|
|
|
|
|
__all__ = ["WorkspaceSession"]
|