Skip to content
Merged
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
111 changes: 111 additions & 0 deletions src/lib/sandbox-create-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,117 @@ describe("sandbox-create-stream", () => {
expect((await promise).forcedReady).toBeUndefined();
});

it("announces the pull phase when base image download progress appears (classic docker)", async () => {
const child = new FakeChild();
const logLine = vi.fn();
const promise = streamSandboxCreate("echo create", process.env, {
logLine,
spawnImpl: () => child as never,
heartbeatIntervalMs: 1_000,
silentPhaseMs: 10_000,
});

child.stdout.emit(
"data",
Buffer.from(
"latest: Pulling from nvidia/nemoclaw/sandbox-base\n" +
"abc123def: Pulling fs layer\n" +
"abc123def: Downloading 12MB/50MB\n" +
"abc123def: Pull complete\n" +
"Digest: sha256:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef\n" +
"Status: Downloaded newer image for ghcr.io/nvidia/nemoclaw/sandbox-base:latest\n" +
" Step 1/45 : FROM ghcr.io/nvidia/nemoclaw/sandbox-base:latest\n",
),
);
child.emit("close", 0);

await expect(promise).resolves.toMatchObject({ status: 0, sawProgress: true });
expect(logLine).toHaveBeenCalledWith(" Pulling base image from registry...");
expect(logLine).toHaveBeenCalledWith("latest: Pulling from nvidia/nemoclaw/sandbox-base");
expect(logLine).toHaveBeenCalledWith(
"Status: Downloaded newer image for ghcr.io/nvidia/nemoclaw/sandbox-base:latest",
);
});

it("announces the pull phase for BuildKit pull progress", async () => {
const child = new FakeChild();
const logLine = vi.fn();
const promise = streamSandboxCreate("echo create", process.env, {
logLine,
spawnImpl: () => child as never,
heartbeatIntervalMs: 1_000,
silentPhaseMs: 10_000,
});

child.stdout.emit(
"data",
Buffer.from(
"#3 resolve ghcr.io/nvidia/nemoclaw/sandbox-base:latest\n" +
"#3 sha256:aa11bb22 12.34MB / 45.67MB 3.2s\n",
),
);
child.emit("close", 0);

await expect(promise).resolves.toMatchObject({ status: 0, sawProgress: true });
expect(logLine).toHaveBeenCalledWith(" Pulling base image from registry...");
// Lock in that BuildKit progress lines actually reach the user — guards against
// silent regressions where shouldShowLine drops the BuildKit pull patterns.
expect(logLine).toHaveBeenCalledWith(
expect.stringContaining("#3 resolve ghcr.io/nvidia/nemoclaw/sandbox-base:latest"),
);
expect(logLine).toHaveBeenCalledWith(
expect.stringContaining("#3 sha256:aa11bb22 12.34MB / 45.67MB 3.2s"),
);
});

it("recognizes non-lowercase image tag prefixes in 'Pulling from' lines", async () => {
const child = new FakeChild();
const logLine = vi.fn();
const promise = streamSandboxCreate("echo create", process.env, {
logLine,
spawnImpl: () => child as never,
heartbeatIntervalMs: 1_000,
silentPhaseMs: 10_000,
});

child.stdout.emit(
"data",
Buffer.from(
"v1.2.3: Pulling from nvidia/nemoclaw/sandbox-base\n" +
"cuda-12.5: Pulling from nvidia/cuda\n" +
"12.4: Pulling from library/python\n",
),
);
child.emit("close", 0);

await expect(promise).resolves.toMatchObject({ status: 0 });
expect(logLine).toHaveBeenCalledWith(" Pulling base image from registry...");
expect(logLine).toHaveBeenCalledWith("v1.2.3: Pulling from nvidia/nemoclaw/sandbox-base");
expect(logLine).toHaveBeenCalledWith("cuda-12.5: Pulling from nvidia/cuda");
expect(logLine).toHaveBeenCalledWith("12.4: Pulling from library/python");
});

it("emits a pull-phase heartbeat instead of a build-phase one during base image download", async () => {
vi.useFakeTimers();
const child = new FakeChild();
const logLine = vi.fn();
const promise = streamSandboxCreate("echo create", process.env, {
logLine,
spawnImpl: () => child as never,
heartbeatIntervalMs: 100,
silentPhaseMs: 50,
});

child.stdout.emit("data", Buffer.from("abc123def: Pulling fs layer\n"));
await vi.advanceTimersByTimeAsync(200);
child.emit("close", 0);
await promise;

const calls = logLine.mock.calls.map((c) => c[0] as string);
expect(calls.some((l) => /Still pulling base image from registry\.\.\./.test(l))).toBe(true);
expect(calls.some((l) => /Still building sandbox image\.\.\./.test(l))).toBe(false);
});

it("reports spawn errors cleanly", async () => {
const child = new FakeChild();
const promise = streamSandboxCreate("echo create", process.env, {
Expand Down
55 changes: 38 additions & 17 deletions src/lib/sandbox-create-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ const UPLOAD_PROGRESS_PATTERNS: readonly RegExp[] = [
/^ {2}Image .*available in the gateway/,
];

// Pull-phase indicators. Detect classic Docker pull output (`<tag>: Pulling
// from <ref>`, `<id>: Pulling fs layer / Downloading / Extracting / Pull
// complete`, `Status: Downloaded`, `Digest:`) plus BuildKit pull progress
// (`#N resolve <ref>`, `#N sha256:<id> <size> / <total>`). The tag prefix
// regex uses [^:\s]+ so non-lowercase tags (`v1.2.3`, `cuda-12.5`, `12.4`)
// also match. See #1829.
const PULL_PROGRESS_PATTERNS: readonly RegExp[] = [
/^\s*(?:[^:\s]+:\s+)?Pulling from \S+/,
/^\s*[a-f0-9]{6,}: (?:Pulling fs layer|Waiting|Downloading|Extracting|Pull complete|Verifying Checksum|Download complete)\b/,
/^\s*Status: (?:Downloaded|Image is up to date)/,
/^\s*Digest: sha256:[a-f0-9]{8,}/,
/^\s*#\d+\s+(?:resolve\s+\S+|sha256:[a-f0-9]+\s+[\d.]+\s*(?:B|KB|MB|GB)\s*\/)/,
];

const VISIBLE_PROGRESS_PATTERNS: readonly RegExp[] = [
...BUILD_PROGRESS_PATTERNS,
/^ {2}Context: /,
Expand All @@ -69,6 +83,7 @@ const VISIBLE_PROGRESS_PATTERNS: readonly RegExp[] = [
/^Successfully tagged /,
/^ {2}Built image /,
...UPLOAD_PROGRESS_PATTERNS,
...PULL_PROGRESS_PATTERNS,
/^Created sandbox: /,
/^✓ /,
];
Expand Down Expand Up @@ -100,7 +115,7 @@ export function streamSandboxCreate(
const silentPhaseMs = options.silentPhaseMs || 15000;
const startedAt = Date.now();
let lastOutputAt = startedAt;
type CreatePhase = "build" | "upload" | "create" | "ready";
type CreatePhase = "pull" | "build" | "upload" | "create" | "ready";

let currentPhase: CreatePhase | null = null;
let lastHeartbeatPhase: CreatePhase | null = null;
Expand Down Expand Up @@ -136,15 +151,17 @@ export function streamSandboxCreate(
lastHeartbeatPhase = null;
lastHeartbeatBucket = -1;
const phaseLine =
nextPhase === "build"
? " Building sandbox image..."
: nextPhase === "upload"
? " Uploading image into OpenShell gateway..."
: nextPhase === "create"
? " Creating sandbox in gateway..."
: nextPhase === "ready"
? " Waiting for sandbox to become ready..."
: null;
nextPhase === "pull"
? " Pulling base image from registry..."
: nextPhase === "build"
? " Building sandbox image..."
: nextPhase === "upload"
? " Uploading image into OpenShell gateway..."
: nextPhase === "create"
? " Creating sandbox in gateway..."
: nextPhase === "ready"
? " Waiting for sandbox to become ready..."
: null;
if (phaseLine) printProgressLine(phaseLine);
}

Expand All @@ -155,6 +172,8 @@ export function streamSandboxCreate(
lastOutputAt = Date.now();
if (matchesAny(line, BUILD_PROGRESS_PATTERNS)) {
setPhase("build");
} else if (matchesAny(line, PULL_PROGRESS_PATTERNS)) {
setPhase("pull");
} else if (matchesAny(line, UPLOAD_PROGRESS_PATTERNS)) {
setPhase("upload");
} else if (/^Created sandbox: /.test(line)) {
Expand Down Expand Up @@ -246,13 +265,15 @@ export function streamSandboxCreate(
return;
}
const heartbeatLine =
currentPhase === "upload"
? ` Still uploading image into OpenShell gateway... (${elapsed}s elapsed)`
: currentPhase === "create"
? ` Still creating sandbox in gateway... (${elapsed}s elapsed)`
: currentPhase === "ready"
? ` Still waiting for sandbox to become ready... (${elapsed}s elapsed)`
: ` Still building sandbox image... (${elapsed}s elapsed)`;
currentPhase === "pull"
? ` Still pulling base image from registry... (${elapsed}s elapsed)`
: currentPhase === "upload"
? ` Still uploading image into OpenShell gateway... (${elapsed}s elapsed)`
: currentPhase === "create"
? ` Still creating sandbox in gateway... (${elapsed}s elapsed)`
: currentPhase === "ready"
? ` Still waiting for sandbox to become ready... (${elapsed}s elapsed)`
: ` Still building sandbox image... (${elapsed}s elapsed)`;
if (trimDisplayLine(heartbeatLine) !== lastPrintedLine) {
printProgressLine(heartbeatLine);
lastHeartbeatPhase = currentPhase;
Expand Down
Loading