Source code for nemo_evaluator.sandbox.base
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from abc import ABC, abstractmethod
from dataclasses import dataclass
from pathlib import Path
from typing import ContextManager, Iterable, Protocol, runtime_checkable
[docs]
@dataclass(frozen=True)
class NemoSandboxCommand:
"""
TB-independent command model for driving an interactive terminal.
Mirrors the fields terminal-bench agents commonly use (but does not depend on TB).
"""
command: str
min_timeout_sec: float = 0.0
max_timeout_sec: float = 180.0
block: bool = False
append_enter: bool = True
[docs]
@runtime_checkable
class NemoSandboxSession(Protocol):
"""
Minimal session API used by agents/harnesses (tmux-like).
"""
self,
keys: str | list[str],
block: bool = False,
min_timeout_sec: float = 0.0,
max_timeout_sec: float = 180.0,
) -> None: ...
[docs]
def send_command(self, command: NemoSandboxCommand) -> None: ...
[docs]
def capture_pane(self, capture_entire: bool = False) -> str: ...
[docs]
def is_session_alive(self) -> bool: ...
[docs]
def get_incremental_output(self) -> str: ...
[docs]
def get_asciinema_timestamp(self) -> float: ...
self,
paths: list[Path] | Path,
container_dir: str | None = None,
container_filename: str | None = None,
) -> None: ...
[docs]
class NemoEvaluatorSandbox(ABC):
"""
Abstract factory for evaluator sandboxes.
Implementations are responsible for provisioning an isolated environment and exposing
a tmux-like session API for agents to interact with it.
"""
[docs]
@classmethod
@abstractmethod
def spin_up(
cls,
*,
task_id: str,
trial_name: str,
run_id: str,
pre_upload_paths: Iterable[Path] | None = None,
upload_dest_dir: str | None = None,
**kwargs,
) -> ContextManager["NemoEvaluatorSandbox"]:
raise NotImplementedError
[docs]
@abstractmethod
def create_session(
self,
session_name: str,
is_active_stream: bool = False,
as_configured_user: bool = True,
) -> NemoSandboxSession:
raise NotImplementedError
[docs]
@abstractmethod
def copy_to_sandbox(
self,
*,
paths: list[Path] | Path,
container_dir: str | None = None,
container_filename: str | None = None,
) -> None:
raise NotImplementedError
[docs]
@abstractmethod
def stop(self) -> None:
raise NotImplementedError