Skip to content

fix(tools): derive seeded fault injection per eval case run - #8307

Open
Fruiterer333 wants to merge 3 commits into
google:mainfrom
Fruiterer333:fix/env-sim-seeded-injection-per-case
Open

Fruiterer333 wants to merge 3 commits into
google:mainfrom
Fruiterer333:fix/env-sim-seeded-injection-per-case

Conversation

@Fruiterer333

Copy link
Copy Markdown
Contributor

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):

Depends on #8041. This branch builds on #8041 (per-session simulated state), which changes the adjacent code in environment_simulation_engine.py. Until #8041 merges, this PR's diff also includes its commit (fix(tools): keep environment simulation state per session); the change for #8288 is the last commit only.

Problem:

InjectionConfig.random_seed seeded one generator per injection config when the plugin was created, and every session the plugin served drew from it. In an eval, one plugin serves every eval case and every num_runs repetition, so whether a case's run got the injected fault depended on how many draws other runs made first: which cases were selected, their order, and parallelism. Same seed, same case, different outcomes (repro in #8288).

Solution:

  • LocalEvalService._perform_inference_single_eval_item runs inference inside a new private, task-local eval run context (utils/_eval_run_context.py, a ContextVar), next to the existing client_label_context. It carries (app_name, eval_set_id, eval_case_id, repetition_index). Child tasks and every turn of the run see it, and it is reset on exit, including on failure or cancellation. It stays in process: nothing is added to client headers or session state.
  • In an eval run, EnvironmentSimulationEngine._draw uses a generator for that run, seeded from a SHA-256 digest of a JSON encoding of (seed, app_name, eval_set_id, eval_case_id, repetition_index, tool_name, injection_config_index), not Python's process-salted hash(). The run's generators are kept in a WeakKeyDictionary keyed by the run context object (identity-hashed), so concurrent runs never share a stream, a later eval invocation with the same plugin starts the case's stream from the beginning, and the generators go away with the run.
  • Outside an eval (no run context), behavior is unchanged: one seeded sequence per plugin, shared by its sessions. Unseeded configs are unchanged everywhere.
  • The random_seed docstring now states the guarantee: within a case run, the same ordered matching calls get the same draws regardless of other cases, order, or parallelism; a run whose agent trajectory changes can still consume different draws. Repetitions of a case get different streams, so num_runs still samples different fault patterns.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added focused tests for the core guarantee:

  • test_seeded_injection_in_eval_run_ignores_other_runs: one engine serving a case alone, again, after another case, and concurrently with other cases gives the case the same draws; other cases and repetitions get their own streams; the engine-wide sequence outside eval is untouched.
  • test_eval_run_seed_is_a_stable_sha256_of_the_run_key: pins the derived seed to a fixed SHA-256 value.
  • test_perform_inference_single_eval_item_sets_eval_run_context (success and failure): inference sees its run's context, and it is reset afterwards.

Broader regression coverage (case selection/order, parallelism, repeated evaluation with the same plugin, multi-turn continuity, context cleanup on failure/cancellation) is being added by @paulraj, as discussed in #8288.

$ pytest tests/unittests/tools/environment_simulation
78 passed
$ pytest tests/unittests/evaluation
1343 passed
$ pytest tests/unittests/tools/environment_simulation tests/unittests/evaluation tests/unittests/utils
2265 passed, 4 skipped

Manual End-to-End (E2E) Tests:

The offline script from #8288 (fake model, no API key), with num_runs=6 and an extra check that reuses one plugin for two evals:

Before (main + #8041):

case_a alone,          parallelism=1: case_a runs 0..5 -> ['OK', 'FAILED', 'FAILED', 'OK', 'FAILED', 'OK']
case_a alone,          parallelism=1 (again): case_a runs 0..5 -> ['OK', 'FAILED', 'FAILED', 'OK', 'FAILED', 'OK']
case_a + case_b,       parallelism=1: case_a runs 0..5 -> ['OK', 'FAILED', 'FAILED', 'FAILED', 'OK', 'OK']
case_b + case_a order, parallelism=1: case_a runs 0..5 -> ['FAILED', 'FAILED', 'FAILED', 'FAILED', 'FAILED', 'OK']
case_a + case_b,       parallelism=4: case_a runs 0..5 -> ['OK', 'OK', 'FAILED', 'FAILED', 'OK', 'OK']
case_a alone, same plugin, eval #1: case_a runs 0..5 -> ['OK', 'FAILED', 'FAILED', 'OK', 'FAILED', 'OK']
case_a alone, same plugin, eval #2: case_a runs 0..5 -> ['OK', 'FAILED', 'FAILED', 'FAILED', 'FAILED', 'OK']

After:

case_a alone,          parallelism=1: case_a runs 0..5 -> ['OK', 'OK', 'OK', 'OK', 'FAILED', 'FAILED']
case_a alone,          parallelism=1 (again): case_a runs 0..5 -> ['OK', 'OK', 'OK', 'OK', 'FAILED', 'FAILED']
case_a + case_b,       parallelism=1: case_a runs 0..5 -> ['OK', 'OK', 'OK', 'OK', 'FAILED', 'FAILED']
case_b + case_a order, parallelism=1: case_a runs 0..5 -> ['OK', 'OK', 'OK', 'OK', 'FAILED', 'FAILED']
case_a + case_b,       parallelism=4: case_a runs 0..5 -> ['OK', 'OK', 'OK', 'OK', 'FAILED', 'FAILED']
case_a alone, same plugin, eval #1: case_a runs 0..5 -> ['OK', 'OK', 'OK', 'OK', 'FAILED', 'FAILED']
case_a alone, same plugin, eval #2: case_a runs 0..5 -> ['OK', 'OK', 'OK', 'OK', 'FAILED', 'FAILED']

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. (Depends on fix(tools): keep environment simulation state per session #8041.)

Additional context

I used Grok Bot to help find the issue and draft the change; I reproduced the bug and verified the fix and tests locally.

Fruiterer333 pushed a commit to Fruiterer333/adk-python that referenced this pull request Nov 23, 2026
Exercise seeded environment simulation through LocalEvalService with a
stateless offline model. Cover case selection, case order, parallelism,
same-plugin reruns, multi-turn continuity and repetition diversity.

Extend live and non-live context lifecycle coverage, including real task
cancellation. Check nested context restoration and child-task isolation.

Related: google#8288
Based on: google#8307
@Fruiterer333

Copy link
Copy Markdown
Contributor Author

Thanks @paulraj for the regression tests. I merged them into this branch, and they pass locally (1440 tests across evaluation and environment simulation). Without the fix, the case-selection, case-order and plugin-reuse tests fail, as expected. The cla/google check now flags your commit as missing a CLA. Could you sign it at https://cla.developers.google.com/ (or make sure your CLA covers your users.noreply.github.com commit email), then trigger a rescan? Thanks!

Fruiterer333 and others added 3 commits December 2, 2026 09:46
EnvironmentSimulationEngine kept a single state store per engine, so
every session served by one plugin, including every eval case and
num_runs repetition, shared the same simulated state. Key the store by
(app_name, user_id, session_id) so each session starts empty while
turns of one session still share their state.

Closes google#8040
A seeded InjectionConfig drew from one generator per plugin, so in an eval
the draws a case saw depended on which other cases ran, their order and the
parallelism. LocalEvalService now runs each eval case run inside a private,
task-local eval run context, and the environment simulation engine seeds
that run's generators from a SHA-256 key of (seed, app_name, eval_set_id,
eval_case_id, repetition_index, tool_name, injection_config_index).
Behavior outside an eval is unchanged.

Fixes google#8288
Exercise seeded environment simulation through LocalEvalService with a
stateless offline model. Cover case selection, case order, parallelism,
same-plugin reruns, multi-turn continuity and repetition diversity.

Extend live and non-live context lifecycle coverage, including real task
cancellation. Check nested context restoration and child-task isolation.

Related: google#8288
Based on: google#8307
@Fruiterer333
Fruiterer333 force-pushed the fix/env-sim-seeded-injection-per-case branch from fa7d05c to de988f6 Compare December 2, 2026 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants