-
Notifications
You must be signed in to change notification settings - Fork 14
fix: wait for Relay ATIF finalization #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rapids-bot
merged 6 commits into
NVIDIA:main
from
AjayThorve:fix/relay-atif-finalization-main
Aug 7, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8e9ba2
fix: wait for Relay ATIF finalization
AjayThorve e3488c5
fix: share Relay ATIF finalization wait
AjayThorve 7604115
test: align mock Relay with ATIF contract
AjayThorve be57ad6
fix: detect changed Relay ATIF files
AjayThorve b402bd1
fix: prevent late Codex artifact attribution
AjayThorve 98495ee
fix: omit unfinalized Relay artifacts
AjayThorve File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
109 changes: 109 additions & 0 deletions
109
adapters/common/src/nemo_fabric_adapters/common/relay_artifacts.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Readiness checks for artifacts written asynchronously by NeMo Relay.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| import json | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| from nemo_fabric_adapters.common import utils as common_utils | ||
|
|
||
| ATIF_FINALIZATION_TIMEOUT_SECONDS = 5.0 | ||
| ATIF_POLL_INTERVAL_SECONDS = 0.05 | ||
| AtifFileFingerprint = tuple[int, int, int, int] | ||
| AtifSnapshot = dict[Path, AtifFileFingerprint] | ||
|
|
||
|
|
||
| def expects_local_atif(plugin_config: dict[str, Any]) -> bool: | ||
| """Return whether Relay is configured to write ATIF to the local runtime. | ||
|
|
||
| Relay treats a non-empty storage list as remote-only, so there is no local | ||
| artifact for an adapter to await in that configuration. | ||
| """ | ||
|
|
||
| for component in plugin_config.get("components", []): | ||
| if ( | ||
| not isinstance(component, dict) | ||
| or component.get("kind") != "observability" | ||
| or component.get("enabled", True) is False | ||
| ): | ||
| continue | ||
| config = component.get("config") | ||
| if not isinstance(config, dict): | ||
| continue | ||
| atif = config.get("atif") | ||
| if isinstance(atif, dict) and atif.get("enabled") and not atif.get("storage"): | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def _atif_fingerprint(path: Path) -> AtifFileFingerprint | None: | ||
| try: | ||
| status = path.stat() | ||
| except (OSError, RuntimeError, ValueError): | ||
| return None | ||
| return (status.st_dev, status.st_ino, status.st_size, status.st_mtime_ns) | ||
|
|
||
|
|
||
| def snapshot_atif_files(plugin_config: dict[str, Any]) -> AtifSnapshot: | ||
| """Capture ATIF file metadata before an adapter invocation. | ||
|
|
||
| Relay requires ``{session_id}`` in the ATIF filename template and creates a | ||
| new session scope for each turn, so a new path is the normal case. Metadata | ||
| fingerprints also detect a writer that rewrites an existing path without | ||
| reading or hashing artifacts from prior turns. | ||
| """ | ||
|
|
||
| snapshot: AtifSnapshot = {} | ||
| for artifact in common_utils.collect_relay_artifacts(plugin_config): | ||
| if artifact.get("kind") != "atif": | ||
| continue | ||
| path = Path(artifact["path"]) | ||
| fingerprint = _atif_fingerprint(path) | ||
| if fingerprint is not None: | ||
| snapshot[path] = fingerprint | ||
| return snapshot | ||
|
|
||
|
|
||
| def _finalized_atif_path( | ||
| plugin_config: dict[str, Any], before: AtifSnapshot | ||
| ) -> Path | None: | ||
| """Find a new or changed ATIF path containing a complete JSON object.""" | ||
|
|
||
| current = snapshot_atif_files(plugin_config) | ||
| for path in sorted(current): | ||
| if before.get(path) == current[path]: | ||
| continue | ||
| try: | ||
| # Relay writes directly to the final path, so existence alone does | ||
| # not prove that the JSON payload has been written completely. | ||
| document = json.loads(path.read_bytes()) | ||
| except (OSError, UnicodeDecodeError, json.JSONDecodeError): | ||
| continue | ||
| if isinstance(document, dict): | ||
| return path | ||
| return None | ||
|
|
||
|
|
||
| async def wait_for_finalized_atif( | ||
| plugin_config: dict[str, Any], | ||
| before: AtifSnapshot, | ||
| *, | ||
| timeout_seconds: float = ATIF_FINALIZATION_TIMEOUT_SECONDS, | ||
| poll_interval_seconds: float = ATIF_POLL_INTERVAL_SECONDS, | ||
| ) -> Path | None: | ||
| """Wait for one new or changed, complete ATIF file until a deadline.""" | ||
|
|
||
| loop = asyncio.get_running_loop() | ||
| deadline = loop.time() + timeout_seconds | ||
| while True: | ||
| if path := _finalized_atif_path(plugin_config, before): | ||
| return path | ||
| remaining = deadline - loop.time() | ||
| if remaining <= 0: | ||
| return None | ||
| await asyncio.sleep(min(poll_interval_seconds, remaining)) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.