Repository navigation
feat(task): record each sandbox a run creates, by step and provider, as evidence that is never drift #1007
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/run-evidence
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.
+739
−70
Open
feat(task): record each sandbox a run creates, by step and provider, as evidence that is never drift #1007
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
1efc725
feat(task): record each sandbox a run creates, by step and provider, …
earakely-scale 90e313e
fix(a2a_agent): an agent's deploy that fails after its create lists t…
earakely-scale b0c13cf
fix(task): a type only one attempt used is a difference though neithe…
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
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,144 @@ | ||
| """The sandboxes a run creates, noted as each is created: the step whose work created it, its provider and whether it | ||
| served the deploy that created it. Every provider's ``create_sandbox``, ``create_vm`` and ``create_container`` note what | ||
| they return (``SandboxProvider.__init_subclass__``), and a run lists them as evidence of where it ran | ||
| (``agent_env.task.run_lock``). Only a ``recording`` block's context notes, so a sandbox made outside a run, or on a | ||
| thread that doesn't carry its context, as a computer composer's build VMs, isn't listed. | ||
|
|
||
| A sandbox is listed once a create call returns it, as created by the outermost one: a container a VM backend makes is a | ||
| container, though its ``create_vm`` noted it first. One a provider allocates and terminates itself inside a create that | ||
| then raises, as a VM whose setup fails, isn't listed: no create handed it to the run, and nothing of the run ran in it. | ||
| A listed sandbox didn't serve when a create call it was noted in raised, when a block of work that created it raised | ||
| (``discarded_if_raises``, as a deploy on one link of a chain), when its caller discarded it (``discarded``, as a chain | ||
| does one that can't run its image, or an agent's deploy that failed after its create returned), or when a probe created | ||
| it (``probing``).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import contextlib | ||
| import threading | ||
| from contextvars import ContextVar | ||
| from dataclasses import dataclass, replace | ||
| from typing import TYPE_CHECKING, Iterator, Optional | ||
|
|
||
| if TYPE_CHECKING: | ||
| from agent_env.providers.sandbox_providers.sandbox import Sandbox | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Created: | ||
| """A sandbox a run created: the step whose work created it, None outside a step; its provider's type | ||
| (``Sandbox.type``), its mode and platform as it was returned, and its id; and whether it ``served`` the deploy that | ||
| created it.""" | ||
|
|
||
| step: Optional[str] | ||
| type: str | ||
| mode: Optional[str] | ||
| platform: Optional[str] | ||
| sandbox_id: Optional[str] | ||
| served: bool = True | ||
|
|
||
|
|
||
| class CreatedSandboxes: | ||
| """The sandboxes a ``recording`` block's run created, in the order each was first noted. Notes come from threads a | ||
| step's work runs on with its context, too.""" | ||
|
|
||
| def __init__(self) -> None: | ||
| self._lock = threading.Lock() | ||
| # Each sandbox by its object, held so its id isn't reused while the run lasts. | ||
| self._by_object: dict[int, tuple[Sandbox, Created]] = {} | ||
|
|
||
| def note(self, sandbox: Sandbox, step: Optional[str]) -> None: | ||
| with self._lock: | ||
| held = self._by_object.get(id(sandbox)) | ||
| created = Created(held[1].step if held else step, str(sandbox.type), getattr(sandbox, "mode", None), | ||
| getattr(sandbox, "platform", None), getattr(sandbox, "sandbox_id", None), | ||
| held[1].served if held else True) | ||
| self._by_object[id(sandbox)] = (sandbox, created) | ||
|
|
||
| def discard(self, sandbox: Sandbox) -> None: | ||
| with self._lock: | ||
| if (held := self._by_object.get(id(sandbox))) is not None: | ||
| self._by_object[id(sandbox)] = (sandbox, replace(held[1], served=False)) | ||
|
|
||
| def __iter__(self) -> Iterator[Created]: | ||
| with self._lock: | ||
| return iter([created for _, created in self._by_object.values()]) | ||
|
|
||
|
|
||
| class _Scope: | ||
| """The sandboxes noted within a block, which it discards when it raises, or always for a probe's.""" | ||
|
|
||
| def __init__(self, always: bool) -> None: | ||
| self.always = always | ||
| self.sandboxes: list[Sandbox] = [] | ||
|
|
||
|
|
||
| _RECORDING: ContextVar[Optional[CreatedSandboxes]] = ContextVar("agentenv_created_sandboxes", default=None) | ||
| _STEP: ContextVar[Optional[str]] = ContextVar("agentenv_creating_step", default=None) | ||
| _SCOPES: ContextVar[tuple[_Scope, ...]] = ContextVar("agentenv_creating_scopes", default=()) | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def recording() -> Iterator[CreatedSandboxes]: | ||
| """The sandboxes created in the block, by its work and the tasks and threads that carry its context.""" | ||
| created = CreatedSandboxes() | ||
| token, scopes = _RECORDING.set(created), _SCOPES.set(()) | ||
| try: | ||
| yield created | ||
| finally: | ||
| _SCOPES.reset(scopes) | ||
| _RECORDING.reset(token) | ||
|
|
||
|
|
||
| def in_step(step_id: Optional[str]) -> None: | ||
| """Attribute the sandboxes created from here on, in this task and those it starts, to ``step_id``; None ends a | ||
| step's.""" | ||
| _STEP.set(step_id) | ||
|
|
||
|
|
||
| def note(sandbox: Sandbox) -> None: | ||
| """Note ``sandbox`` as created, in the block ``recording`` lists, if any.""" | ||
| if (created := _RECORDING.get()) is None: | ||
| return | ||
| created.note(sandbox, _STEP.get()) | ||
| for scope in _SCOPES.get(): | ||
| scope.sandboxes.append(sandbox) | ||
| if scope.always: | ||
| created.discard(sandbox) | ||
|
|
||
|
|
||
| def discarded(sandbox: Sandbox) -> None: | ||
| """Note that ``sandbox`` didn't serve: its caller dropped it, as a chain does one that can't run its image.""" | ||
| if (created := _RECORDING.get()) is not None: | ||
| created.discard(sandbox) | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def discarded_if_raises() -> Iterator[None]: | ||
| """Discard every sandbox created in the block if it raises, as its work failed and they served nothing.""" | ||
| with _scope(always=False): | ||
| yield | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def probing() -> Iterator[None]: | ||
| """Discard every sandbox created in the block: a probe's serve no deploy.""" | ||
| with _scope(always=True): | ||
| yield | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def _scope(always: bool) -> Iterator[None]: | ||
| if (created := _RECORDING.get()) is None: | ||
| yield | ||
| return | ||
| scope = _Scope(always) | ||
| token = _SCOPES.set((*_SCOPES.get(), scope)) | ||
| try: | ||
| yield | ||
| except BaseException: | ||
| for sandbox in list(scope.sandboxes): | ||
| created.discard(sandbox) | ||
| raise | ||
| finally: | ||
| _SCOPES.reset(token) |
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.