Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/novita_tbench2_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ async def main() -> int:
)
provider = NovitaSandboxProvider()
# First start builds a Novita template from the Dockerfile, which can take a
# few minutes; later starts reuse the cached template.
# few minutes; later starts reuse the cached template. Keep readiness inside
# the finally so a timeout still stops the sandbox.
base_url = provider.start_container(image=image)
provider.wait_for_ready(base_url, timeout_s=300)

try:
provider.wait_for_ready(base_url, timeout_s=300)
async with Tbench2Env(base_url=base_url, provider=provider) as env:
result = await env.reset(task_id=task_id)
print("Instruction head:")
Expand Down
13 changes: 10 additions & 3 deletions src/openenv/core/containers/runtime/novita_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def _resolve_from_references(content: str) -> str:
- The parser stores a ``FROM ${BASE_IMAGE}`` line verbatim, so the template
would be built from a literal image named ``${BASE_IMAGE}``. Only global
ARGs (declared before the first ``FROM``) are in scope for a ``FROM``
line, which is the form every in-repo Dockerfile uses.
line, which is the form every in-repo Dockerfile uses. Substitution is
longest-name-first with a word-boundary check on unbraced ``$NAME`` forms
so ``$BASE`` cannot corrupt ``$BASE_IMAGE``.
- ``FROM --platform=linux/amd64 python:3.10-slim`` likewise keeps the flag
as part of the name. Novita builds for its own platform, so the flag is
dropped rather than propagated.
Expand All @@ -147,9 +149,14 @@ def _resolve_from_references(content: str) -> str:

reference = match.group("rest").strip()
reference = re.sub(r"^(--platform=\S+\s*)+", "", reference).strip()
for name, value in arg_defaults.items():
# Longest names first so an unbraced `$BASE` cannot corrupt
# `$BASE_IMAGE` into `<BASE-value>_IMAGE`. Prefer word-boundary
# matching for the unbraced form for the same reason.
for name, value in sorted(
arg_defaults.items(), key=lambda item: len(item[0]), reverse=True
):
reference = reference.replace(f"${{{name}}}", value)
reference = reference.replace(f"${name}", value)
reference = re.sub(rf"\${re.escape(name)}(?!\w)", value, reference)
out.append(f"FROM {reference}")
return "\n".join(out)

Expand Down
24 changes: 24 additions & 0 deletions tests/test_core/test_novita_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,30 @@ def test_arg_after_first_from_not_in_scope(self):
out = _resolve_from_references("FROM python:3.11\nARG X=alpine\nRUN echo hi\n")
assert "FROM python:3.11" in out

def test_resolve_unbraced_arg_does_not_prefix_longer_name(self):
from openenv.core.containers.runtime.novita_provider import (
_resolve_from_references,
)

# Declaring a shorter ARG before a longer one must not turn
# `$BASE_IMAGE` into `<BASE-value>_IMAGE`.
out = _resolve_from_references(
"ARG BASE=python:3.12\n"
"ARG BASE_IMAGE=python:3.11-slim\n"
"FROM $BASE_IMAGE\n"
"RUN echo hi\n"
)
assert "FROM python:3.11-slim" in out
assert "python:3.12_IMAGE" not in out

braced = _resolve_from_references(
"ARG BASE=python:3.12\n"
"ARG BASE_IMAGE=python:3.11-slim\n"
"FROM ${BASE_IMAGE}\n"
)
assert "FROM python:3.11-slim" in braced
assert "python:3.12_IMAGE" not in braced

def test_flatten_drops_same_path_copy(self):
from openenv.core.containers.runtime.novita_provider import _flatten_multistage

Expand Down
Loading