Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,9 @@ ARG NEMOCLAW_EXTRA_AGENTS_JSON_B64=W10=
# since terminal-based pairing is impossible in those contexts.
# Default: "0" (device auth enabled for local deployments — secure by default).
ARG NEMOCLAW_DISABLE_DEVICE_AUTH=0
# Unique per build — busts the Docker cache for the token-injection layer
# so each image gets a fresh gateway auth token.
# Pass --build-arg NEMOCLAW_BUILD_ID=$(date +%s) to bust the cache.
# Compatibility build arg for older custom Dockerfiles and rebuild tooling.
# NemoClaw-managed images intentionally do not consume it; gateway auth tokens
# are generated at container startup and are never baked into image layers.
ARG NEMOCLAW_BUILD_ID=default
# macOS OpenShell VM backend imports the Docker image into a virtiofs rootfs
# where image uid/gid ownership is presented as the host user. The VM also
Expand Down
125 changes: 122 additions & 3 deletions src/lib/onboard/dockerfile-patch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "./dockerfile-patch";

const tmpRoots: string[] = [];
const REPO_ROOT = path.resolve(import.meta.dirname, "../../..");

beforeEach(() => {
delete process.env.NEMOCLAW_MESSAGING_PLAN_B64;
Expand Down Expand Up @@ -196,7 +197,7 @@ describe("dockerfile patch helpers", () => {
expect(patched).toContain("ARG NEMOCLAW_PRIMARY_MODEL_REF=inference/custom-model");
expect(patched).toContain("ARG CHAT_UI_URL=https://chat.example");
expect(patched).toContain("ARG NEMOCLAW_INFERENCE_COMPAT_B64=");
expect(patched).toContain("ARG NEMOCLAW_BUILD_ID=build-1");
expect(patched).toContain("ARG NEMOCLAW_BUILD_ID=old");
expect(patched).toContain("ARG NEMOCLAW_DARWIN_VM_COMPAT=1");
expect(patched).toContain("ARG NEMOCLAW_PROXY_HOST=host.docker.internal");
expect(patched).toContain("ARG NEMOCLAW_PROXY_PORT=3128");
Expand Down Expand Up @@ -393,7 +394,8 @@ describe("dockerfile patch helpers", () => {
expect(patched).not.toMatch(/\r|\nRUN touch/);
expect(patched).toContain("ARG NEMOCLAW_MODEL=modelRUN touch /tmp/model-pwn");
expect(patched).toContain("ARG CHAT_UI_URL=https://chat.exampleRUN touch /tmp/chat-pwn");
expect(patched).toContain("ARG NEMOCLAW_BUILD_ID=build-1RUN touch /tmp/build-pwn");
expect(patched).toContain("ARG NEMOCLAW_BUILD_ID=old");
expect(patched).not.toContain("build-1RUN touch /tmp/build-pwn");
expect(patched).toContain("ARG NEMOCLAW_INFERENCE_API=openai-responsesRUN touch /tmp/api-pwn");
expect(patched).toContain(
"ARG BASE_IMAGE=ghcr.io/nvidia/nemoclaw/sandbox-base@sha256:abcRUN touch /tmp/base-pwn",
Expand Down Expand Up @@ -429,13 +431,130 @@ describe("dockerfile patch helpers", () => {
assert.match(patched, /^ARG NEMOCLAW_PROVIDER_KEY=openai$/m);
assert.match(patched, /^ARG NEMOCLAW_PRIMARY_MODEL_REF=openai\/gpt-5\.4$/m);
assert.match(patched, /^ARG CHAT_UI_URL=http:\/\/127\.0\.0\.1:19999$/m);
assert.match(patched, /^ARG NEMOCLAW_BUILD_ID=build-123$/m);
assert.match(patched, /^ARG NEMOCLAW_BUILD_ID=default$/m);
assert.match(patched, /^ARG NEMOCLAW_DARWIN_VM_COMPAT=0$/m);
} finally {
fs.rmSync(tmpDir, { recursive: true, force: true });
}
});

it("leaves NEMOCLAW_BUILD_ID stable unless the Dockerfile consumes it (#4682)", () => {
const unconsumedDockerfilePath = dockerfileWith(
[
"ARG NEMOCLAW_MODEL=old",
"ARG NEMOCLAW_PROVIDER_KEY=old",
"ARG NEMOCLAW_PRIMARY_MODEL_REF=old",
"ARG CHAT_UI_URL=old",
"ARG NEMOCLAW_INFERENCE_BASE_URL=old",
"ARG NEMOCLAW_INFERENCE_API=old",
"ARG NEMOCLAW_INFERENCE_COMPAT_B64=old",
"# NEMOCLAW_BUILD_ID is documented here but not consumed.",
"ARG NEMOCLAW_BUILD_ID=default",
"ARG NEMOCLAW_DARWIN_VM_COMPAT=0",
].join("\n"),
);

patchStagedDockerfile(
unconsumedDockerfilePath,
"gpt-5.4",
"http://127.0.0.1:19999",
"build-cache-stable",
"openai-api",
);

expect(fs.readFileSync(unconsumedDockerfilePath, "utf8")).toMatch(
/^ARG NEMOCLAW_BUILD_ID=default$/m,
);

for (const [index, consumerLine] of [
"ENV LEGACY_BUILD_ID=${NEMOCLAW_BUILD_ID}",
"RUN printf '%s\\n' ${NEMOCLAW_BUILD_ID}",
"ARG LEGACY_LABEL=prefix-${NEMOCLAW_BUILD_ID}-suffix",
"LABEL legacy.build=${NEMOCLAW_BUILD_ID}",
].entries()) {
const consumedDockerfilePath = dockerfileWith(
[
"ARG NEMOCLAW_MODEL=old",
"ARG NEMOCLAW_PROVIDER_KEY=old",
"ARG NEMOCLAW_PRIMARY_MODEL_REF=old",
"ARG CHAT_UI_URL=old",
"ARG NEMOCLAW_INFERENCE_BASE_URL=old",
"ARG NEMOCLAW_INFERENCE_API=old",
"ARG NEMOCLAW_INFERENCE_COMPAT_B64=old",
"ARG NEMOCLAW_BUILD_ID=default",
consumerLine,
"ARG NEMOCLAW_DARWIN_VM_COMPAT=0",
].join("\n"),
);
patchStagedDockerfile(
consumedDockerfilePath,
"gpt-5.4",
"http://127.0.0.1:19999",
`build-cache-bust-${index}`,
"openai-api",
);

expect(fs.readFileSync(consumedDockerfilePath, "utf8")).toMatch(
new RegExp(`^ARG NEMOCLAW_BUILD_ID=build-cache-bust-${index}$`, "m"),
);
}
});

it.each([
["OpenClaw", path.join(REPO_ROOT, "Dockerfile")],
["Hermes", path.join(REPO_ROOT, "agents", "hermes", "Dockerfile")],
])("keeps the stock %s build context byte-identical when only the per-run build ID changes (#4682)", (_agent, stockDockerfile) => {
const stockSource = fs.readFileSync(stockDockerfile, "utf8");
const firstBuild = dockerfileWith(stockSource);
const secondBuild = dockerfileWith(stockSource);

patchStagedDockerfile(
firstBuild,
"gpt-5.4",
"http://127.0.0.1:19999",
"first-per-run-id",
"openai-api",
);
patchStagedDockerfile(
secondBuild,
"gpt-5.4",
"http://127.0.0.1:19999",
"second-per-run-id",
"openai-api",
);

expect(fs.readFileSync(firstBuild, "utf8")).toBe(fs.readFileSync(secondBuild, "utf8"));
});

it("sanitizes a per-run build ID when a custom Dockerfile consumes it (#4682)", () => {
const dockerfilePath = dockerfileWith(
[
"ARG NEMOCLAW_MODEL=old",
"ARG NEMOCLAW_PROVIDER_KEY=old",
"ARG NEMOCLAW_PRIMARY_MODEL_REF=old",
"ARG CHAT_UI_URL=old",
"ARG NEMOCLAW_INFERENCE_BASE_URL=old",
"ARG NEMOCLAW_INFERENCE_API=old",
"ARG NEMOCLAW_INFERENCE_COMPAT_B64=old",
"ARG NEMOCLAW_BUILD_ID=default",
"LABEL legacy.build=${NEMOCLAW_BUILD_ID}",
"ARG NEMOCLAW_DARWIN_VM_COMPAT=0",
].join("\n"),
);

patchStagedDockerfile(
dockerfilePath,
"gpt-5.4",
"http://127.0.0.1:19999",
"build-safe\nRUN touch /tmp/build-id-injection",
"openai-api",
);

const patched = fs.readFileSync(dockerfilePath, "utf8");
expect(patched).toContain("ARG NEMOCLAW_BUILD_ID=build-safeRUN touch /tmp/build-id-injection");
expect(patched).not.toMatch(/\nRUN touch \/tmp\/build-id-injection/);
});

it("patches the staged Dockerfile for macOS VM rootfs ownership compatibility", () => {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-onboard-darwin-vm-"));
const dockerfilePath = path.join(tmpDir, "Dockerfile");
Expand Down
21 changes: 17 additions & 4 deletions src/lib/onboard/dockerfile-patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ function encodeSanitizedDockerJsonArg(value: unknown): string {
return sanitizeDockerArg(encodeDockerJsonArg(value));
}

function dockerfileConsumesBuildId(dockerfile: string): boolean {
return dockerfile.split("\n").some((line) => {
const trimmed = line.trimStart();
return (
!trimmed.startsWith("#") &&
!/^ARG\s+NEMOCLAW_BUILD_ID(?:=.*)?$/.test(trimmed) &&
/\bNEMOCLAW_BUILD_ID\b/.test(trimmed)
);
});
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

export function isValidProxyHost(value: string): boolean {
return PROXY_HOST_RE.test(value);
}
Expand Down Expand Up @@ -171,10 +182,12 @@ export function patchStagedDockerfile(
/^ARG NEMOCLAW_INFERENCE_COMPAT_B64=.*$/m,
`ARG NEMOCLAW_INFERENCE_COMPAT_B64=${encodeSanitizedDockerJsonArg(inferenceCompat)}`,
);
dockerfile = dockerfile.replace(
/^ARG NEMOCLAW_BUILD_ID=.*$/m,
`ARG NEMOCLAW_BUILD_ID=${sanitizeDockerArg(buildId)}`,
);
if (dockerfileConsumesBuildId(dockerfile)) {
dockerfile = dockerfile.replace(
/^ARG NEMOCLAW_BUILD_ID=.*$/m,
`ARG NEMOCLAW_BUILD_ID=${sanitizeDockerArg(buildId)}`,
);
}
dockerfile = dockerfile.replace(
/^ARG NEMOCLAW_DARWIN_VM_COMPAT=.*$/m,
`ARG NEMOCLAW_DARWIN_VM_COMPAT=${sanitizeDockerArg(darwinVmCompat ? "1" : "0")}`,
Expand Down
74 changes: 74 additions & 0 deletions test/fixtures/warm-build-cache-evidence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!-- SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -->
<!-- SPDX-License-Identifier: Apache-2.0 -->

# Warm Sandbox Build Cache Evidence

This fixture records the manual cache validation for issue #4682. It is not a
user-facing guide; it gives reviewers an auditable command shape and expected
cache behavior for stabilizing the otherwise-unused per-run build ID.

## Method

The measurement keeps shared base images on the host and removes only generated
NemoClaw/OpenShell sandbox images (`openshell/sandbox-from:*`) for the cold run.
That isolates final-image layer reuse instead of measuring base-image pulls.

For each agent:

1. Delete the measurement sandbox if it exists:

```bash
openshell sandbox delete warm-cache-openclaw || true
openshell sandbox delete warm-cache-hermes || true
```

2. Delete the generated measurement image before the cold run:

```bash
docker image rm openshell/sandbox-from:<measurement-tag>
```

3. Run onboard with stable inputs and record the
`Sandbox image build completed in ...` line:

```bash
NEMOCLAW_NON_INTERACTIVE=1 \
NEMOCLAW_RECREATE_SANDBOX=1 \
NEMOCLAW_SANDBOX_NAME=warm-cache-openclaw \
NEMOCLAW_PROVIDER=custom \
NEMOCLAW_MODEL=test-model \
NEMOCLAW_ENDPOINT_URL=http://host.openshell.internal:11434/v1 \
COMPATIBLE_API_KEY=warm-cache-dummy-key \
node bin/nemoclaw.js onboard \
--non-interactive --yes --fresh --recreate-sandbox \
--name warm-cache-openclaw \
--yes-i-accept-third-party-software
```

For Hermes, add `--agent hermes` and use
`NEMOCLAW_SANDBOX_NAME=warm-cache-hermes` / `--name warm-cache-hermes`.

4. Stop the post-build readiness wait after the timing line, delete the sandbox,
keep the generated image, and rerun the same command for the warm run.

## Observed Results

| Agent | Cold build | Warm build | Expected warm-cache behavior |
| --- | ---: | ---: | --- |
| OpenClaw | `20.9s` | `0.1s` | Stable Dockerfile/build context reuses build-time config, plugin install, proxy, OTEL, permission, and hash layers. |
| Hermes | `21.5s` | `0.4s` | Stable Dockerfile/build context reuses runtime setup, config generation, agent-install, permission, and config-hash layers. |

Warm builds showed the derived-image Docker steps completing at `0.0s` or
`0.1s`. `ARG NEMOCLAW_BUILD_ID=default` remained stable in stock staged
Dockerfiles; custom Dockerfiles that reference `NEMOCLAW_BUILD_ID` still receive
the supplied build ID.

A separate BuildKit control probe confirmed that changing an in-scope `ARG`
invalidates a following `RUN` layer even when that instruction does not mention
the argument. This change therefore does not claim that moving `ENV` instructions
can protect layers from other changed build arguments. The automated regression
instead patches both stock Dockerfiles with two different per-run build IDs and
requires the resulting build contexts to remain byte-identical.

The post-build OpenShell GPU reconnect/readiness step is outside this cache
measurement and can be handled separately from Docker build-layer reuse.
Loading