Skip to content

feat(integrations): add Plimsoll environment - #8381

Draft
carrollco wants to merge 9 commits into
google:mainfrom
carrollco:feat/plimsoll-environment
Draft

carrollco wants to merge 9 commits into
google:mainfrom
carrollco:feat/plimsoll-environment

Conversation

@carrollco

Copy link
Copy Markdown

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 PlimsollEnvironment under google.adk.integrations.plimsoll, implementing BaseEnvironment over one Plimsoll session:

  • It opens at a caller-set isolation floor (default kernel) and checks the tier the session states before anything runs.
  • Once closed it refuses to reopen (reopen_after_close=True opts out), so an agent never takes an empty sandbox for the one holding its files.
  • A call cut off mid-flight lets the sandbox go, and every later call says it was lost.
  • Per-call timeouts are validated (negative or non-finite raises, 0 sends 1 ms).
  • A failed close can be retried, and opens and closes are serialized.
  • The sample gives each ADK session its own environment, so adk eval's Runners don't close each other's sandboxes.

Also adds the plimsoll extra, 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.py pass, 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/unittests run and tox will be on the ready version, with the constraint files.

Manual End-to-End (E2E) Tests:

Against a Plimsoll daemon on Docker under gVisor (kernel tier), started as in the sample's README, with a scripted model (no model calls):

  1. adk eval on the sample at --parallelism 1 and at the default 4, twice in a row each: every case passed, and the daemon showed no sandbox left open after any run.
  2. The sample as it was before the per-session change, same daemon, --parallelism 1: every case failed with the refuse-reopen error, which is why the sample changed.
  3. A call cancelled 2 s into sleep 20: the next call and the next initialize() raised the lost-sandbox error, and the daemon closed the session.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules. (plimsoll-client 0.29.0 is published; the constraint files wait for the cooldown.)

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 in adk web kept its files while the Eval tab ran.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(integrations): Add PlimsollEnvironment for self-hosted sandboxes with an isolation floor

1 participant