Repository navigation
Conversation
Add PlimsollEnvironment, a BaseEnvironment over one session of a self-hosted Plimsoll sandbox. It opens at a caller-set minimum isolation tier and checks the tier the session states before any call, runs commands and file I/O in the kept sandbox, cuts a call in flight when its await is cancelled, and closes a session whose open was cancelled. Also adds the plimsoll extra, an experimental feature flag, unit tests with the client mocked, live tests that skip without PLIMSOLL_URL, a sample and a unit guide.
ADK initializes an environment before every tool call and closes it with any Runner that holds it, an AgentTool's nested Runner included. An environment shared by an agent and the agent it wraps was closed when the wrapped agent finished, and the wrapping agent's next tool call opened a new, empty sandbox with no error, its model holding the wrapped agent's answer about files that were gone. initialize() after close() now raises RuntimeError naming the cause and the remedy, and reopen_after_close=True keeps the old behavior. A close before any open, or after a cancelled open, is harmless. Also rejects NaN and infinite timeouts and an env_vars name with a trailing newline, and says which timeout a command without one gets.
execute() returned a timed-out result for a negative timeout instead of raising, and passed NaN and infinity to the client unchecked. It now raises ValueError for a negative, non-finite or non-numeric timeout, as the constructor does for its own timeout. A timeout of zero was answered with a made-up timed-out result, without sending the command. It now behaves as OpenSandboxEnvironment, which accepts zero and runs the command with a one-millisecond limit: the command is sent with the shortest budget the daemon takes, one millisecond.
close() dropped the session before deleting it, so a delete that failed (the daemon unreachable for a moment, say) could not be tried again: the sandbox kept one of the daemon's places until its lifetime ran out, and a second close() did nothing. close() now keeps the session until its delete succeeds. No call reaches the sandbox once a close has started, and a reopen is still refused, but is_initialized stays true, so the toolset's next close comes back and retries. With reopen_after_close, initialize() retries the failed close before it opens a new sandbox, so the old one does not hold a place beside it.
…down When initialize() was cancelled while its open ran, a done callback on the open's task was to close the session the open made. asyncio.run cancels every task when it ends, the shielded open included, so at shutdown the callback saw a cancelled task and returned, while the thread went on to open a sandbox no one closed: it held one of the daemon's places until its idle limit. The open now runs in a thread of its own that decides, under a lock, what to do with the session it opened: publish it, or close it when the caller that waited for it was cancelled. Nothing needs the event loop to be running when the open finishes. The environment pickles as before, without its lock.
The environment relied on ADK's toolsets to run initialize() and close() one at a time. Called directly, two concurrent initialize() calls each opened a sandbox and only the last was kept, so the other held one of the daemon's places until its idle limit; and a close() made while an open was in flight returned before that open published its sandbox, which then stayed open. Concurrent initialize() calls now wait for the one open in flight instead of starting another. A close() moves the environment to a new generation and waits for that open, whose thread closes what it opened instead of publishing it. The open is closed when no caller waits for it any more, not when one of several waiting callers is cancelled, and a failed close of an unwanted sandbox is kept for the next close() to retry.
A failed close is kept for the next close() to retry, but two failures are final: plimsoll-client raises ChainError when the daemon answered the close (the session is closed there, and its count shows a call the client did not see, as a cut call may be), and StaleOwnerError when another worker holds the session. The client raises either again on every later close without sending anything, so keeping the session made every close() raise, and with reopen_after_close every initialize() too. Such an error is now raised once and the session is dropped; any other failure, such as a close that got no answer, is still retried.
… call
When a tool call is cancelled after its request was sent, the environment
cuts the request, and plimsoll-client then sends nothing more on that
session: the call may have run, so the client can no longer follow the
daemon's chain. ADK cancels the other calls of a parallel batch when one
of them fails, and on abort. The environment kept the dead session, so
every later call of every agent run using it failed with the client's
refusal ("open a new one", which an agent cannot do) until the process
restarted, and the session held one of the daemon's places meanwhile. A
session the daemon ended (its lifetime, a restart) was kept the same way.
After each call, in the call's own worker thread (a cancelled await
returns before the client has cut the request), the environment now checks
the client session's public stopped and ended states. When either is set
it lets the sandbox go: it closes the session, which frees the daemon's
place, and from then on every initialize() and call raises RuntimeError
saying the sandbox and its files were lost and why. The call that found
the loss still raises its own error. A call the client refused before
sending it leaves the session as it was.
The environment does not replace the lost sandbox, even with
reopen_after_close=True, and this is the behavior chosen over a
replacement: nothing tells the agent its files are gone, so a new sandbox
handed to it on the next tool call would be taken for the one holding its
files, the failure the refusal to reopen after close() exists to prevent.
The replacement stays an explicit step of the application: close() ends
the lost state, after which the rule for a closed environment applies, and
initialize() opens a new, empty sandbox only if reopen_after_close is set.
is_initialized stays true after a loss, so a Runner's close still reaches
close(). The guide and the class docstring say so.
Uses only what plimsoll-client 0.29.0 publishes (Session.stopped and
Session.ended).
`adk eval` runs each eval case in a Runner of its own over the sample's one agent, up to four at once, and each Runner closes the agent's toolsets when it ends. With one shared PlimsollEnvironment, the first case to end closed it, and since a closed environment refuses to reopen, every later case raised RuntimeError. The sample now wraps the environment tools in a small toolset that keeps an EnvironmentToolset and a PlimsollEnvironment per ADK session, marks a session as running from before_agent_callback to after_agent_callback, and closes only the sandboxes of sessions that are not running. The class is unchanged: it still refuses to reopen after close. A test runs two cases through their own InMemoryRunners over the sample's agent, with the Plimsoll client mocked, and closes one Runner while the other case is running; the running case keeps its sandbox and its file. The sample README and the guide say how adk eval meets a shared environment, the limits of the per-session toolset, what happens after a cut call, and how command and file timeouts are set.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
None of ADK's environments lets the app set a minimum isolation level, so a self-hosted sandbox that comes up on plain Docker runs the agent's code anyway. And a shared environment closed by one Runner leaves the next run an empty sandbox with no error (#8368).
Solution:
Adds an experimental
PlimsollEnvironmentundergoogle.adk.integrations.plimsoll, implementingBaseEnvironmentover one Plimsoll session:kernel) and checks the tier the session states before anything runs.reopen_after_close=Trueopts out), so an agent never takes an empty sandbox for the one holding its files.0sends 1 ms).adk eval's Runners don't close each other's sandboxes.Also adds the
plimsollextra, an experimental feature flag, unit tests, live tests that skip without a daemon, the sample and a unit guide. A sandbox per ADK session inside the environment (EnvironmentScope.SESSION) is a follow-up PR.Draft until 2026-12-02: ADK resolves dependencies with a 7-day cooldown and plimsoll-client 0.29.0 came out on 2026-11-25, so the constraint files can't be regenerated before then. I'll add them and mark this ready that day.
Testing Plan
Unit Tests:
I have added or updated unit tests for my change.
All unit tests pass locally.
pytest tests/unittests/integrations/plimsoll: 61 passed, 2 skipped (the live tests), against plimsoll-client 0.29.0 as published. Client mocked, no daemon, no network.The sample's tests in
tests/unittests/test_samples.pypass, including one Runner closing while another case is mid-run.Each fix came with a test that failed before it.
pyink, isort, ruff, codespell and mdformat pass on the changed files.
The full
tests/unittestsrun andtoxwill be on the ready version, with the constraint files.Manual End-to-End (E2E) Tests:
Against a Plimsoll daemon on Docker under gVisor (
kerneltier), started as in the sample's README, with a scripted model (no model calls):adk evalon the sample at--parallelism 1and at the default 4, twice in a row each: every case passed, and the daemon showed no sandbox left open after any run.--parallelism 1: every case failed with the refuse-reopen error, which is why the sample changed.sleep 20: the next call and the nextinitialize()raised the lost-sandbox error, and the daemon closed the session.Checklist
Additional context
Two limits of the sample, written in its README: a run that raises keeps its sandbox until the daemon's session lifetime, and
adk web's Eval tab closes a chat's sandbox between turns. The follow-up PR's session scope fixes the second: in a test against a real daemon, a chat inadk webkept its files while the Eval tab ran.