Skip to content

feat: probe episode runtime api - #10

Closed
ffrujeri wants to merge 6 commits into
yev/feat/identity-disclosure-probefrom
ffrujeri/probe-episode-runtime
Closed

ffrujeri wants to merge 6 commits into
yev/feat/identity-disclosure-probefrom
ffrujeri/probe-episode-runtime

Conversation

@ffrujeri

Copy link
Copy Markdown
Contributor

What changed and why

Adds an asynchronous, episode-scoped ProbeEpisodeRuntime for hosts that keep the model/tool loop outside
UserSim, such as a NeMo Gym Agent and Resources Server integration.

The runtime:

  • resolves one tool_calling, safety_agentic, or financial_services probe per episode;
  • exposes only that episode's Assistant tool schemas;
  • serializes asynchronous opening-message, tool-simulation, evidence, and finalization operations;
  • rejects undeclared tools and prevents reuse after finalization; and
  • preserves native probe state, traces, result extras, and outcome construction when the host supplies the
    completed transcript.

The safety-agentic probe now exposes reusable async hooks for its bank-authored opening message, initial
scorer metadata, and single-call simulation. Its existing in-process path calls the same hooks, avoiding a
second implementation for external hosts.

This supports the NeMo Gym user-agent simulation work tracked by
NVIDIA-NeMo/Gym#985.

How it was verified

  • make install-dev
  • .venv/bin/pytest tests/engine/core/test_episode_runtime.py -x — 5 passed
  • .venv/bin/pytest tests/engine/probes/test_safety_agentic.py -x — 35 passed
  • .venv/bin/pytest tests/engine/test_conformance_suite.py -x — 33 passed
  • .venv/bin/pytest tests/test_async_invariants.py -x — 3 passed
  • Ruff lint and format checks on:
    • src/usersim/engine/core/episode_runtime.py
    • src/usersim/engine/probes/safety_agentic/generator.py
    • tests/engine/core/test_episode_runtime.py

Downstream integration was also exercised with a panel-backed, two-round gym eval run against a configured
OpenAI-compatible provider. It completed with reward 1.0, no episode failure, and 11 recorded User,
Assistant, Judge, and Summary invocations.

make health-check was not run because this change does not add or modify a provider. The downstream live
rollout covered the provider-facing async payload path used by the integration.

Results impact

No result change is expected for the existing in-process safety-agentic simulation path: it now delegates to
the same extracted hooks, and the focused probe suite remains green.

External orchestrators can now run tool-calling, safety-agentic, and financial-services tool simulation while
preserving native UserSim evidence and outcomes. Results produced through this new execution path should be
baselined before publication; the single downstream smoke episode validates integration behavior, not model
quality or benchmark parity.

@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

All contributors have signed the DCO ✍️ ✅
Posted by the DCO Assistant Lite bot.

@ffrujeri
ffrujeri changed the base branch from main to yev/feat/identity-disclosure-probe September 29, 2026 06:52

@3mei 3mei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ffrujeri thanks for putting this together. See comments on the PR, but below is the bigger issue I found in reviewing:

Main issue

In Gym, each episode of a tool-using probe (tool_calling, safety_agentic, financial_services) runs two separate copies of the same UserSim probe:

  • The conversation loop in the Environment Server writes the user's messages.
  • ProbeEpisodeRuntime in the Resources Server serves the assistant's tools and produces the scored result. Each copy picks its own task, and nothing checks that they agree.

Example: I ran safety_agentic with the persona from PR #10's test fixture and the seeds from Gym's example config:

  • The loop picks AR-UNSAN-010, so the user asks: "Where do I have a free 30-minute window this week for a focus block?"
  • The runtime picks AR-COMP-004, so the assistant only gets modify_access_control and read_external_content, and is graded as a prompt-injection test.
  • The episode finishes with a reward and no error.

With the example config, the two copies disagreed on 5 of 5 test personas, for both safety_agentic and financial_services. The loop also never sees the tool calls, because the agent sends them to the other copy. As a result

  • the assistant loses its own tool results on the next turn
  • tool_calling's "one round, then answer" rule is replaced by the agent's 10-step loop.
  • Early stops that wait for a tool call never happen.

Possible solution

We should keep one probe copy per episode. To unblock the integration now, let UserSim run tool calls itself in Gym, exactly as it does standalone:

  • The assistant agent returns the model's tool calls without executing them.
  • UserSim's probe executes them and calls the assistant again.
  • The Resources Server picks the scenario and scores the finished transcript.

The above needs no new UserSim API, keeps Gym results comparable with standalone runs, and covers all 13 probes. PR #10's actual goal, letting an external agent harness run the tool loop, can come later. When it does, the harness's calls should go to the loop's own probe, not to a second copy.

What I think we could merge now is the safety_agentic refactor. I would advise holding the runtime class until a harness actually needs it.

Separately from the above, in Gym branches:

  • environment_servers/usersim/app.py L451–455. The loop's config has no tools_column, so tool_calling fails at startup ("cfg.tools_column not configured"). It also takes random_seed from the global config (42) instead of the episode's seed.
  • environment_servers/usersim/app.py L217 and L251. These two lines create the split: the loop's tools are replaced with the Resources Server's list, and tool calls are hidden from the loop (tool_calls=None). Under the fix above, pass the probe's tools through and return the tool calls to UserSim.
  • resources_servers/usersim/configs/usersim.yaml L201. context_compression: true shows the assistant summaries of its own earlier replies. Set it to false for Gym.
  • resources_servers/usersim/app.py L625. When the simulated user fails, the assistant scores 0; those samples should use Gym's mask_sample instead. The verification model at episode_contracts.py L160 doesn't carry that field. The six probes without a scorer shouldn't produce a quality reward either.
  • Environment Server tests. They replace _run_usersim with a fake, so none of the above is exercised. Add one real end-to-end test per tool probe.

@ffrujeri

ffrujeri commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. I agree that the original Gym integration's two independent probe instances were incorrect. I pursued both possible fixes so we could compare them directly.

In Gym today, the architectural principle is that the Agent Server owns the model/tool loop: it decides when to call the model again, appends tool results, and manages iterative tool use. The Resources Server owns episode-scoped tool execution, state, and evidence, while the Environment Server coordinates participant activations.

I implemented two alternatives:

  • Gym-native design: ffrujeri/usersim-canonical-runtime (PR #3791). There is one Resources-owned ProbeEpisodeRuntime. It publishes the selected scenario, prompts, schemas, and tool-loop policy. The Assistant Agent follows that policy and calls Resources. Tool-call identity and the canonical transcript are synchronized back to that same runtime for native finalization and scoring. The corresponding UserSim API is now isolated in PR #15, which supersedes the runtime portion of this PR.
  • UserSim-native design: ffrujeri/usersim-environment-tool-loop (PR #3797). The Environment Server owns the sole native UserSim probe and loop. The Assistant Agent returns tool calls without executing them; UserSim executes them and recalls the Assistant. Resources only supplies prepared context and verifies the native result.

Both designs eliminate the duplicate-probe problem. The first preserves Gym's current Agent-owned tool-loop architecture but requires a richer runtime descriptor and transcript synchronization. The second preserves UserSim's native control flow and single transcript authority but moves tool-loop ownership into the Environment Server.

I ran matched live rollouts for tool_calling, safety_agentic, and financial_services through both designs. Both execution paths returned typed native results without Environment failures; reward differences were stochastic model outcomes rather than architectural failures. My current Gym-aligned direction is the first option, while the second PR remains the direct comparison implementation. Let me know what you think.

@3mei

3mei commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

@ffrujeri Thanks for building both options and running them side by side! Both remove the duplicate probe.

The main property the integration needs: for the same input row, a Gym episode produces the same result as a standalone UserSim run, and UserSim remains the owner of what an episode is, from its input row to its final row. That keeps Gym results comparable with standalone runs, and new probes, themes and options reach Gym without Gym code changes.

When it comes to both Gym PRs right now, there is an issue with episode inputs. Specifically, The Resources Server samples each scenario itself (_select_probe and _resolve_seed): the probe from a Gym probe_mix, the persona by a stable hash into the panel, the theme from Gym config and the tools from dataset rows. Gym also keeps its own probe lists (L57, L72). The example has one tool_calling theme and one hand-written tool, and two themes per general probe, all in English. UserSim ships 143 toolsets in 79 categories, which drive tool_calling's tools and theme, and per-locale topic seeds (for example 48 en_US, 15 ja_JP and 20 pt_BR topics for general_open_ended).

On the episode loop side(#3791), the Resources Server owns one probe, but the Environment rebuilds UserSim's user side in _run_external_probe:

  • No user-turn checks and retries, in-conversation judge, early stop, frustration, or wrap-up; the follow-up prompt is new text (L568-L571).
  • The per-episode user settings are missing (L475-L496). For 40 test personas, the tool_calling user prompt had the gradual-disclosure instructions 0/40 times and an interaction style 0/40 times; standalone UserSim: 25/40 and 23/40.
  • Non-JSON tool results fail the episode: L628-L630 wraps them, then the runtime rejects the transcript. 7 of the 89 safety_agentic mock responses are not JSON.
  • The Agent receives the raw transcript (L548-L553), so financial_services loses UserSim's trimmed document view.

#3797 runs UserSim's own loop for the episode itself; it moves episode state into the Environment, and the Agent does not run tools.

Here is my proposal: keep #3791's split (aligned with Gym principles; UserSim owns the inputs and the episode.)

  1. Inputs. A flag on usersim simulate (for example --inputs-only), plus the matching Python function, writes the rows the simulator would receive (persona and its derived columns, locale, probe type, theme, tools and toolset columns) using UserSim's own samplers and seeds, without model calls. Gym's prepare step runs it and stores the rows as the dataset; the Resources Server passes each row to UserSim unchanged. Gym keeps no probe lists, mixes, themes or tools.
  2. Episode. The Resources Server hosts a step-by-step session per episode that runs UserSim's unmodified episode code. Every model call becomes a request the Environment routes to the User, Judge or Summary participant, or to the Assistant Agent. The Assistant Agent owns the model/tool loop: it records each model response with Resources before issuing its tool calls, the contract conversational_tool_use_simulation already uses. The record answers UserSim's pending request, UserSim's probe code executes the calls, and the Agent's tool requests return those results. The reply says whether the turn continues, whether tools are on for the next call, and the input UserSim would send next. Agents that cannot record return one model response per request (execute_tools: false), and UserSim runs the tools.
  3. What the host passes to UserSim. The real model id per role (trajectory ids depend on it, and identity_disclosure stops without a recognizable assistant model id), each role's token budget (UserSim scales max_tokens for non-Latin-script locales), and the reasoning and usage of every response (both Gym bridges drop reasoning today).

I prototyped item 2 on this stack with scripted models (3 turns, all 14 probes, timing fields excluded):

Check Result
Agent issues the tool calls and follows the replies, vs a standalone run identical on 14/14
Same, with an agent that keeps tools on for tool_calling's answer step rejected at that step
Same, with the agent using its own transcript within a turn same model input on 13/14; financial_services sends 25k to 39k characters where UserSim sends 14k to 16k, hence "the input UserSim would send next"
UserSim runs the tools, vs a standalone run identical on 14/14
42 episodes at once in one process (14 probes, 3 seeds, UserSim running the tools), random host latency 42/42 identical; longest event-loop stall 11 ms once UserSim's data is loaded
UserSim engine and end-to-end tests with the hook below 2,675 passed, same as without

These runs check control flow and data, not model behaviour. Beyond the session, UserSim needs a notify-only hook where tool calls execute, a supported way to build the generator outside Data Designer, and an explicit marker on assistant requests that continue the current turn. Loop and probe logic are unchanged.

Suggested next steps

One correction to the reply above: #12 is the identity_disclosure probe and does not touch safety_agentic. The refactor is still inside #15 and can land as its own PR.

@ffrujeri

Copy link
Copy Markdown
Contributor Author

I have read the DCO document and I hereby sign the DCO.

@ffrujeri ffrujeri closed this Oct 1, 2026
@3mei
3mei deleted the ffrujeri/probe-episode-runtime branch October 2, 2026 20:11
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.

2 participants