Skip to content

feat(telemetry): stamp the rollout id on sandbox spans - #4752

Merged
iguehring-nvidia merged 8 commits into
mainfrom
iguehring/sandbox-span-correlation
Oct 20, 2026
Merged

iguehring-nvidia merged 8 commits into
mainfrom
iguehring/sandbox-span-correlation

Conversation

@iguehring-nvidia

@iguehring-nvidia iguehring-nvidia commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds nemo.gym.rollout.id to the three sandbox lifecycle spans (gym.sandbox.start, gym.sandbox.exec, gym.sandbox.stop) when they run inside a rollout, the same attribute the endpoint wrappers put on gym.rollout, gym.verify and the model-call spans.

Why: sandbox spans were only correlated to their rollout through the parent chain. With the id on the span, "all sandbox execs of rollout 3-0" is an attribute filter in a trace backend, and a rollout's sandbox time can be summed without walking the tree. Outside a rollout nothing is added, so standalone sandbox use is unchanged. The command text stays off the span, as before.

How, after review (2ee6bc3, moved to its own module in the follow-up commit): the sandbox package does not learn about rollouts. nemo_gym/sandbox/observability.py exposes set_span_attribute_provider(callable), whose attributes are added to every gym.sandbox.* span when it opens, and AsyncSandbox.start() and exec() take an optional span_attributes argument that wins over the provider key by key. Gym's telemetry setup registers the rollout-id provider once, next to the span-group registration. Every existing call site (about twenty resources servers and agents call AsyncSandbox directly) is covered without changes, and nemo_gym.sandbox imports nothing from rollout_correlation or global_config, which also removes the import-order hazard the first version had.

No issue; small follow-on to the sandbox instrumentation (#3633).

Validation

  • pytest tests/unit_tests/telemetry/test_call_sites.py tests/unit_tests/telemetry/test_setup.py: 61 passed (plus the pre-existing macOS-only gauge failure). Tests: all three spans carry the id inside a rollout through the registered provider; none outside; a failing create or exec keeps the id; no provider means no extra attributes even inside a rollout; explicit span_attributes win over the provider; init_telemetry registers the provider.

  • Previous validation of the earlier shape:

  • pytest tests/unit_tests/telemetry/test_call_sites.py tests/unit_tests/test_enroot_sandbox_facade.py tests/unit_tests/test_opensandbox_cleanup.py tests/unit_tests/test_opensandbox_compose.py: 48 passed; the one failure is the pre-existing macOS-only test_supervisor_is_the_only_writer_of_the_active_servers_gauge, identical on main.

  • New tests: inside rollout_context("7-0") all three spans carry the id; outside a rollout none of them does.

  • pre-commit run --files <changed files>: passed.

Rollout evidence

N/A: attribute-only change on existing spans; sandbox behaviour is untouched.

Review follow-up

The automated review caught a real import cycle: global_config imports the sandbox registry while initialising, and the new module-level import of rollout_correlation read constants from the half-initialised global_config, so a fresh import nemo_gym.global_config failed. The import now lives inside the helper, with a comment on why; verified by importing global_config in a fresh interpreter (no dedicated test, by maintainer preference). The rollout id is also attached when the start and exec spans open rather than after the provider call returns, so a failed start or exec keeps its correlation; tests cover a provider that raises in create and in exec. pytest tests/unit_tests/telemetry/test_call_sites.py: 25 passed plus the known macOS-only failure.

Sandboxed end-to-end run (added after opening)

Terminal-bench 4 miniswe on OpenSandbox sandboxes, 8 tasks, from a branch merging this PR with #4749, #4750 and #4751 on main, with capture (observability_enabled) on. Sandboxes are driven by the resources server (/seed_session), not the agent. All 363 sandbox spans (25 start, 313 exec, 25 stop) carry nemo.gym.rollout.id, and for every one the id matches the gym.rollout found through its parent chain. A first attempt with observability_enabled: false showed none, which is expected: that setting switches rollout correlation off entirely, so there is no id for any span to carry.

Sandboxed end-to-end run of the provider-based shape (2ee6bc3)

Same terminal-bench 4 miniswe setup, 8 tasks, OpenSandbox, capture on, with the provider registered by telemetry setup and no sandbox-side lookup: all 354 sandbox spans (25 start, 304 exec, 25 stop), from both the resources server and the agent, carry nemo.gym.rollout.id, and no span carries an id that disagrees with its rollout. Collector log clean, no token in the job directory.

`gym.sandbox.start`, `gym.sandbox.exec` and `gym.sandbox.stop` now carry
`nemo.gym.rollout.id` when they run inside a rollout context, the same
correlation attribute Gym's endpoint and model-call spans carry. A
rollout's sandbox time becomes filterable by attribute, not only through
the parent chain. Outside a rollout the attribute is absent, as before.

Signed-off-by: Ingo Guehring <[email protected]>
@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team October 19, 2026 16:51
@iguehring-nvidia iguehring-nvidia added feature New capabilities, enhancements, or enablement work area:sandbox Sandbox providers, isolation, and sandbox runtime contracts needs-review PR is ready for code review and waiting on a reviewer labels Oct 19, 2026
@iguehring-nvidia iguehring-nvidia self-assigned this Oct 19, 2026
@iguehring-nvidia

Copy link
Copy Markdown
Contributor Author

/review

@svcnvidia-nemo-ci svcnvidia-nemo-ci 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.

HOLD — 1 BLOCKER and 1 RISK.

The new import creates an import-order-dependent startup failure on normal CLI/server paths. Separately, failed sandbox starts and execs miss the rollout attribute because it is attached only after the awaited operation succeeds.

Reviewed both changed files and their directly affected import, correlation, and telemetry contracts. Findings are source-validated; tests could not be executed in this read-only review.

Note: formal approval is not available for this request, so this review is posted as a comment only.

Review generated by codex

Comment thread nemo_gym/sandbox/api.py Outdated
from pathlib import Path
from typing import Any, TypeVar

from nemo_gym.rollout_correlation import current_rollout_id

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.

BLOCKER — Defer the correlation import to avoid breaking startup

Failure / blast radius: In a fresh interpreter, import nemo_gym.global_config now encounters a circular import. global_config imports the sandbox registry, which initializes the sandbox package and its API. This new import then loads rollout_correlation, which imports constants from the still-initializing global_config; those constants are not defined until lines 263–272. The result is ImportError, not a usable module. Normal gym env start reaches this path through cli.env → setup_command → global_config, even with telemetry disabled. Importing the sandbox first can mask the problem in a shared test process.

Correction: Remove the module-level import and resolve it inside _rollout_attributes, below the existing telemetry gates. Suggested replacement at the helper, retaining its docstring:

def _rollout_attributes() -> dict[str, str]:
    from nemo_gym.rollout_correlation import current_rollout_id

    rollout_id = current_rollout_id()
    return {"nemo.gym.rollout.id": rollout_id} if rollout_id else {}

This restores the invariant that importing the sandbox API does not require rollout correlation while global configuration is initializing. Verify this suggestion before adopting it.

Preservation / proposed acceptance (not run): Add separate fresh-interpreter subprocess checks for import nemo_gym.global_config, import nemo_gym.rollout_correlation, import nemo_gym.server_utils, and import nemo_gym.sandbox.api; each must exit 0. Retain the correlation assertions: all three lifecycle spans carry 7-0 inside that context and omit the key outside it. Retain the disabled-group assertion: exec returns 0 and no spans are emitted. The local import must remain reachable only on enabled instrumentation paths.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed with a fresh interpreter: import nemo_gym.global_config failed on the branch and worked on main. Fixed in a15da3e: the import moved inside _rollout_attributes, with a comment on why. A fresh-interpreter import check guards the entry point that broke.

Comment thread nemo_gym/sandbox/api.py Outdated
"nemo.gym.sandbox.id": _sandbox_id(self._handle),
"nemo.gym.sandbox.return_code": result.return_code,
"nemo.gym.sandbox.error_type": result.error_type,
**_rollout_attributes(),

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.

RISK — Attach rollout correlation before awaiting start/exec

Failure / blast radius: With sandbox tracing enabled and rollout_context("7-0"), let a stub provider's create() or exec() raise RuntimeError("boom"). The lifecycle span records the failure, but lacks nemo.gym.rollout.id: both start and exec attach it only after the await succeeds. Consequently, the new per-rollout attribute filter excludes precisely these failed operations and their time. Expected: correlation is present independently of provider success, as it already is for stop. This affects diagnostics, not sandbox execution results.

Correction: In AsyncSandbox.start() and AsyncSandbox.exec(), replace the initial managed_span attribute argument with:

**{"nemo.gym.sandbox.provider": provider_name, **_rollout_attributes()},

Remove _rollout_attributes() from their success-only attribute updates. Keep the sandbox ID after successful creation, and keep return-code/error-type attributes after an exec result exists. The invariant is that call-context correlation is attached before fallible work, while result-dependent metadata is attached only when available. This also works with the deferred helper import proposed above; verify the suggestion before adopting it.

Preservation / proposed acceptance (not run): Add failing-create and failing-exec stub cases under rollout_context("7-0"); assert the corresponding finished span has ERROR status and rollout ID 7-0, and the original RuntimeError still propagates. Retain the current successful all-three-spans correlation case, plus the nonzero-exit case: returning code 3 remains a normal result and records code 3. Exercise start, exec and stop outside a rollout and assert the key is absent. Retain disabled-group behavior and command confidentiality: no spans when disabled, and no SECRET_FLAG in exec attributes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in a15da3e. The rollout id is now passed to managed_span for gym.sandbox.start and gym.sandbox.exec, so it is on the span before the awaited provider call; the sandbox id stays attached after a successful create, return code and error type after an exec result. Tests: a provider that raises in create and one that raises in exec, each under rollout_context("7-0"), assert ERROR status, rollout id 7-0 and the propagated RuntimeError; start, exec and stop outside a rollout assert the key is absent.

…and stamp the id before the awaited work

Importing it at module level created an import cycle: global_config imports
the sandbox registry while initialising, and rollout_correlation imports
constants from global_config, so a fresh `import nemo_gym.global_config`
failed. The import now lives inside the helper.

The rollout id is attached when the start and exec spans open, not after
the provider call returns, so a failed start or exec keeps its correlation.
Fresh-interpreter import checks and failing-provider cases are added to the
tests.

Signed-off-by: Ingo Guehring <[email protected]>
@iguehring-nvidia

Copy link
Copy Markdown
Contributor Author

/review

@iguehring-nvidia

Copy link
Copy Markdown
Contributor Author

/review

@svcnvidia-nemo-ci svcnvidia-nemo-ci 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.

SHIP — LGTM — no reliability concerns within the eligible follow-up scope.

Compared all four changed files with the previously reviewed head. The historical fixes remain intact: rollout lookup is deferred in telemetry setup, rather than imported during sandbox initialization, and start/exec attributes are still attached before awaiting provider work. The failure regressions retain assertions for propagated exceptions, ERROR status, and rollout correlation.

Reviewed the new registration path, disabled-instrumentation gates, attribute precedence, and directly affected import contracts. No unresolved prior findings or introduced regressions eligible above the historical high-impact ceiling remain.

Validation was source-only; tests and fresh-interpreter import checks were not executed.

Note: formal approval is not available for this request, so this review is posted as a comment only.

Review generated by codex

Comment thread nemo_gym/sandbox/api.py Outdated
)


#: Extra attributes for every sandbox span, supplied by whoever runs the sandbox. The sandbox

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.

this approach looks good. Can you just move this to nemo_gym/sandbox/observability.py?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved to nemo_gym/sandbox/observability.py in b029bcf; api.py only consumes it.

…ox/observability.py

The seam the embedding application registers its extra span attributes
through now lives in its own module, as asked in review; the sandbox API
only consumes it.

Signed-off-by: Ingo Guehring <[email protected]>
@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team October 19, 2026 21:33
@yaoyu-33 yaoyu-33 added the complexity:medium Single-domain change with interacting parts or a moderate review surface label Oct 19, 2026
@oyilmaz-nvidia

Copy link
Copy Markdown
Contributor

/ok to test 4dbfc8b

Comment thread nemo_gym/sandbox/api.py Outdated
from pathlib import Path
from typing import Any, TypeVar

from nemo_gym.sandbox.observability import span_attributes as _span_attributes

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.

can you rename this to from nemo_gym.sandbox.observability import span_attributes as default_span_attributes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Renamed in 9c542be: from nemo_gym.sandbox.observability import span_attributes as default_span_attributes, used at the three call sites.

@iguehring-nvidia
iguehring-nvidia added this pull request to the merge queue Oct 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/39492762972

Merged via the queue into main with commit c1582e1 Oct 20, 2026
49 of 54 checks passed
@iguehring-nvidia
iguehring-nvidia deleted the iguehring/sandbox-span-correlation branch October 20, 2026 20:47

This branch was previously deployed

1 inactive deployment
public — 9c542bee Deployed Oct 20, 2026 by copy-pr-bot[bot] via release / finalize / notify #6082
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:sandbox Sandbox providers, isolation, and sandbox runtime contracts complexity:medium Single-domain change with interacting parts or a moderate review surface feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants