Repository navigation
refactor: delegate workspace export #522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...g-agent/.opencode/prompts/tasks/runtime-workspace-export-service-task-packet.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| Goal: | ||
| Move workspace archive export orchestration out of `SessionManager` into a | ||
| runtime/workspace service boundary while preserving active-turn rejection, | ||
| cloud-binding validation, concurrent idle exports, export-in-progress guards, | ||
| and post-export owner revalidation. | ||
|
|
||
| Scope: | ||
| - Add `RuntimeWorkspaceExportService` under `coding_agent.runs`. | ||
| - Delegate `SessionManager.export_workspace_archive()` to the new service. | ||
| - Add focused service tests and update ADR-0058 follow-up status. | ||
|
|
||
| Out of scope: | ||
| - Do not change HTTP workspace transfer endpoints, archive generation, | ||
| workspace cleanup, checkpoint behavior, runtime execution, daemon CLI, | ||
| executor selection, or event streams. | ||
| - Do not change the `RuntimeTurnAdmissionService` export-in-progress guard. | ||
| - Do not touch untracked `webui/` or contract fixture workspaces. | ||
|
|
||
| Context: | ||
| - ADRs: | ||
| - `docs/adr/0058-local-daemon-control-plane-executor-architecture.md` | ||
| - Relevant files: | ||
| - `src/coding_agent/runs/workspace_export.py` | ||
| - `src/coding_agent/runs/__init__.py` | ||
| - `src/coding_agent/server/session_manager.py` | ||
| - `tests/coding_agent/test_runtime_workspace_export_service.py` | ||
| - `tests/ui/test_session_manager_public_api.py` | ||
| - `tests/ui/test_session_manager_runtime.py` | ||
| - Postmortem notes: | ||
| - `postmortem/patterns/PM-0022-revalidate-event-stream-ownership-after-queue-attach.md` | ||
| - `postmortem/patterns/PM-0023-make-event-stream-cleanup-and-teardown-idempotent.md` | ||
|
|
||
| Target tests: | ||
| - `uv run pytest tests/coding_agent/test_runtime_workspace_export_service.py -v` | ||
| - `uv run pytest tests/ui/test_session_manager_public_api.py -k "export_workspace_archive" -v` | ||
| - `uv run pytest tests/ui/test_session_manager_runtime.py -k "workspace_export" -v` | ||
| - `uv run pytest tests/ui/test_http_server_failover.py -k "events or event_queue or owner" -v` | ||
| - `uv run pytest tests/ui/test_session_manager_public_api.py -k "event_queue or close_session or clear_sessions" -v` | ||
| - `uv run ruff check src/coding_agent/runs/workspace_export.py src/coding_agent/runs/__init__.py src/coding_agent/server/session_manager.py tests/coding_agent/test_runtime_workspace_export_service.py` | ||
| - `uv run ruff format --check src/coding_agent/runs/workspace_export.py src/coding_agent/runs/__init__.py src/coding_agent/server/session_manager.py tests/coding_agent/test_runtime_workspace_export_service.py` | ||
|
|
||
| Loop policy: | ||
| - Engineer implements the smallest correct change and runs the target tests. | ||
| - Reviewer reviews only the resulting diff and affected tests. | ||
| - Reviewer reports only P1/P2 findings. | ||
| - Engineer fixes only accepted P1/P2 findings and reruns the same target tests. | ||
| - Verifier reruns the exact target tests and reports pass/fail only. | ||
|
|
||
| Stop conditions: | ||
| - At most one review/fix/retest cycle. | ||
| - Escalate architectural redirection or scope expansion to the human. | ||
| - Ignore non-blocking optimization suggestions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from collections.abc import Awaitable, Callable | ||
| from dataclasses import dataclass | ||
| from types import TracebackType | ||
| from typing import Protocol, TypeVar | ||
|
|
||
| from coding_agent.environment.execution_binding import CloudWorkspaceBinding | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
|
|
||
| class RuntimeWorkspaceExportLock(Protocol): | ||
| def locked(self) -> bool: ... | ||
|
|
||
| async def __aenter__(self) -> object: ... | ||
|
|
||
| async def __aexit__( | ||
| self, | ||
| exc_type: type[BaseException] | None, | ||
| exc: BaseException | None, | ||
| traceback: TracebackType | None, | ||
| ) -> bool | None: ... | ||
|
|
||
|
|
||
| class RuntimeWorkspaceExportTask(Protocol): | ||
| def done(self) -> bool: ... | ||
|
|
||
|
|
||
| class RuntimeWorkspaceExportSession(Protocol): | ||
| execution_binding: object | ||
| turn_in_progress: bool | ||
| task: RuntimeWorkspaceExportTask | None | ||
|
|
||
|
|
||
| RuntimeWorkspaceExportLockProvider = Callable[[str], RuntimeWorkspaceExportLock] | ||
| RuntimeWorkspaceExportOwnerAsserter = Callable[[str], Awaitable[None]] | ||
| RuntimeWorkspaceExportSessionLoader = Callable[ | ||
| [str], | ||
| Awaitable[RuntimeWorkspaceExportSession], | ||
| ] | ||
| RuntimeWorkspaceExportBegin = Callable[[str], None] | ||
| RuntimeWorkspaceExportEnd = Callable[[str], None] | ||
| RuntimeWorkspaceArchiveExporter = Callable[[CloudWorkspaceBinding], T] | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class RuntimeWorkspaceExportService: | ||
| turn_lock_for: RuntimeWorkspaceExportLockProvider | ||
| assert_owner: RuntimeWorkspaceExportOwnerAsserter | ||
| load_session: RuntimeWorkspaceExportSessionLoader | ||
| begin_export: RuntimeWorkspaceExportBegin | ||
| end_export: RuntimeWorkspaceExportEnd | ||
|
|
||
| async def export_archive( | ||
| self, | ||
| session_id: str, | ||
| export_archive: RuntimeWorkspaceArchiveExporter[T], | ||
| ) -> T: | ||
| turn_lock = self.turn_lock_for(session_id) | ||
| if turn_lock.locked(): | ||
| raise RuntimeError("turn already in progress") | ||
|
|
||
| await self.assert_owner(session_id) | ||
| session = await self.load_session(session_id) | ||
| if session.turn_in_progress or ( | ||
| session.task is not None and not session.task.done() | ||
| ): | ||
| raise RuntimeError("turn already in progress") | ||
| if not isinstance(session.execution_binding, CloudWorkspaceBinding): | ||
| raise ValueError("Workspace export requires cloud session") | ||
|
|
||
| self.begin_export(session_id) | ||
| try: | ||
| result = await asyncio.to_thread( | ||
| export_archive, | ||
| session.execution_binding, | ||
| ) | ||
| await self.assert_owner(session_id) | ||
| return result | ||
| finally: | ||
| self.end_export(session_id) | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "RuntimeWorkspaceArchiveExporter", | ||
| "RuntimeWorkspaceExportBegin", | ||
| "RuntimeWorkspaceExportEnd", | ||
| "RuntimeWorkspaceExportLock", | ||
| "RuntimeWorkspaceExportLockProvider", | ||
| "RuntimeWorkspaceExportOwnerAsserter", | ||
| "RuntimeWorkspaceExportService", | ||
| "RuntimeWorkspaceExportSession", | ||
| "RuntimeWorkspaceExportSessionLoader", | ||
| "RuntimeWorkspaceExportTask", | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Close the admission race between turn checks and
begin_export.Lines 61-74 validate turn state without actually holding the session turn lock. A turn can start in that window, so export can proceed concurrently with an admitted turn.
Proposed fix
async def export_archive( self, session_id: str, export_archive: RuntimeWorkspaceArchiveExporter[T], ) -> T: turn_lock = self.turn_lock_for(session_id) if turn_lock.locked(): raise RuntimeError("turn already in progress") - await self.assert_owner(session_id) - session = await self.load_session(session_id) - if session.turn_in_progress or ( - session.task is not None and not session.task.done() - ): - raise RuntimeError("turn already in progress") - if not isinstance(session.execution_binding, CloudWorkspaceBinding): - raise ValueError("Workspace export requires cloud session") - - self.begin_export(session_id) + async with turn_lock: + await self.assert_owner(session_id) + session = await self.load_session(session_id) + if session.turn_in_progress or ( + session.task is not None and not session.task.done() + ): + raise RuntimeError("turn already in progress") + if not isinstance(session.execution_binding, CloudWorkspaceBinding): + raise ValueError("Workspace export requires cloud session") + self.begin_export(session_id) try: result = await asyncio.to_thread( export_archive, session.execution_binding, )🤖 Prompt for AI Agents