Repository navigation
feat(a2a-agent): an atif/1 copy's media files wait on the agent's disk until the worker pulls them #834
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
Open
earakely-scale
wants to merge
3
commits into
main
Choose a base branch
from
edgararakelyan/fd-6077-y3-media
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+777
−24
Open
feat(a2a-agent): an atif/1 copy's media files wait on the agent's disk until the worker pulls them #834
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
2ca0386
feat(a2a-agent): an atif/1 copy's media files wait on the agent's dis…
earakely-scale c7b8dc8
fix(a2a_agent): media pushes survive overrides and restores, and chec…
earakely-scale 2343d57
refactor(a2a_agent): name the media hash block size
earakely-scale 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
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
80 changes: 80 additions & 0 deletions
80
packages/agentenv-protocol/src/agentenv_protocol/a2a_agent/_media_spool.py
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,80 @@ | ||
| """The media files of an agent's atif/1 copies, spooled to its disk as the adapter names them: one file per sha256, | ||
| held by the contexts whose copies reference it, until a push confirms it for a context or that context goes. The worker | ||
| pulls them at each turn's end (``TRAJECTORY_V1.media``), so the bytes never wait in memory.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import itertools | ||
| import os | ||
| import shutil | ||
| import tempfile | ||
| import threading | ||
| from collections.abc import Callable | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| class MediaSpool: | ||
| """Each context's media files on disk. Safe to fill from the agent's thread while the loop pushes them; the folder | ||
| is made on the first file and removed by ``close``. Each hold has its own number, so an upload that outlives its | ||
| context's copy (a restore, an eviction) releases only the hold it began with, never a later copy's.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| self._lock = threading.Lock() | ||
| self._folder: Path | None = None | ||
| #: Each context's held paths, ``media/<sha256>.<ext>``, each with its hold's number. | ||
| self._held: dict[str, dict[str, int]] = {} | ||
| self._holds = itertools.count(1) | ||
|
|
||
| def sink(self, context_id: str) -> Callable[[str, bytes], None]: | ||
| """Where a context's copy writes each media file once, as its builder names it.""" | ||
|
|
||
| def write(path: str, data: bytes) -> None: | ||
| with self._lock: | ||
| file = self._file(path) | ||
| if not file.exists(): | ||
| part = file.with_name(f"{file.name}.{os.getpid()}.part") | ||
| part.write_bytes(data) | ||
| os.replace(part, file) | ||
| self._held.setdefault(context_id, {}).setdefault(path, next(self._holds)) | ||
|
|
||
| return write | ||
|
|
||
| def held(self, context_id: str, path: str) -> tuple[Path, int] | None: | ||
| """The file a context holds at ``path`` and its hold's number, or None when it holds none there.""" | ||
| with self._lock: | ||
| hold = self._held.get(context_id, {}).get(path) | ||
| return None if hold is None else (self._file(path), hold) | ||
|
|
||
| def release(self, context_id: str, path: str, hold: int) -> None: | ||
| """Hold ``hold`` on ``path`` is the context's no longer: a push confirmed it. A later hold stays, and the file | ||
| goes once no context holds it.""" | ||
| with self._lock: | ||
| held = self._held.get(context_id) | ||
| if held is None or held.get(path) != hold: | ||
| return | ||
| del held[path] | ||
| if not held: | ||
| del self._held[context_id] | ||
| self._remove_unheld(path) | ||
|
|
||
| def drop(self, context_id: str) -> None: | ||
| """The context is gone, evicted or restored over: its files go, but those another context still holds.""" | ||
| with self._lock: | ||
| for path in self._held.pop(context_id, {}): | ||
| self._remove_unheld(path) | ||
|
|
||
| def close(self) -> None: | ||
| with self._lock: | ||
| if self._folder is not None: | ||
| shutil.rmtree(self._folder, ignore_errors=True) | ||
| self._folder = None | ||
| self._held.clear() | ||
|
|
||
| def _file(self, path: str) -> Path: | ||
| if self._folder is None: | ||
| self._folder = Path(tempfile.mkdtemp(prefix="agentenv-atif-media-")) | ||
| return self._folder / path.removeprefix("media/") | ||
|
|
||
| def _remove_unheld(self, path: str) -> None: | ||
| if self._folder is not None and not any(path in held for held in self._held.values()): | ||
| (self._folder / path.removeprefix("media/")).unlink(missing_ok=True) |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.