Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(docker): copy MARS web bridge modules into the image (#18343)

Vasco Schiavo26f590d1f710
diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml
index eabe69d117..c2f69bf5b3 100644
--- a/.github/workflows/docker.yml
+++ b/.github/workflows/docker.yml
@@ -208,6 +208,11 @@ jobs:
provenance: true
sbom: true
+ # Boot the image before merge-manifests publishes the multi-arch tags
+ # (including `latest`): a missing COPY only shows up at runtime (#18342).
+ - name: Smoke test (${{ matrix.arch }})
+ run: docker/smoke-test.sh "${IMAGE}@${{ steps.build.outputs.digest }}"
+
- name: Summary (${{ matrix.arch }})
env:
TAGS: ${{ steps.prep.outputs.tags }}
diff --git a/__tests__/scripts/docker-script-imports.test.ts b/__tests__/scripts/docker-script-imports.test.ts
new file mode 100644
index 0000000000..5b780e33f3
--- /dev/null
+++ b/__tests__/scripts/docker-script-imports.test.ts
@@ -0,0 +1,81 @@
+// @vitest-environment node
+import { readFileSync } from "node:fs";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+import { describe, expect, it } from "vitest";
+
+const repoRoot = path.resolve(
+ path.dirname(fileURLToPath(import.meta.url)),
+ "../..",
+);
+const dockerfile = readFileSync(
+ path.join(repoRoot, "docker/Dockerfile"),
+ "utf-8",
+);
+const entrypoint = readFileSync(
+ path.join(repoRoot, "docker/entrypoint.sh"),
+ "utf-8",
+);
+
+// The image copies scripts/ one file at a time, so a new import in a script
+// the entrypoint runs must come with a COPY line, or the container crashes on
+// start with ERR_MODULE_NOT_FOUND (#18342).
+function imageScriptGraph(): { scripts: Set<string>; packages: Set<string> } {
+ const scripts = new Set<string>();
+ const packages = new Set<string>();
+ const queue = [
+ ...entrypoint.matchAll(/\bnode \/opt\/agent-canvas\/([\w.-]+\.mjs)/g),
+ ].map((match) => match[1]);
+
+ while (queue.length > 0) {
+ const script = queue.pop()!;
+ if (scripts.has(script)) continue;
+ scripts.add(script);
+
+ // Drop block comments so JSDoc `import("electron")` types don't count.
+ const source = readFileSync(
+ path.join(repoRoot, "scripts", script),
+ "utf-8",
+ ).replace(/\/\*[\s\S]*?\*\//g, "");
+ const specifiers = [
+ ...source.matchAll(/^\s*(?:import|export)\b[^;]*?\bfrom\s+"([^"]+)"/gm),
+ ...source.matchAll(/^\s*import\s+"([^"]+)"/gm),
+ ...source.matchAll(/\bimport\(\s*"([^"]+)"\s*\)/g),
+ ].map((match) => match[1]);
+
+ for (const specifier of specifiers) {
+ if (specifier.startsWith("./")) {
+ queue.push(specifier.slice(2));
+ } else if (!specifier.startsWith("node:")) {
+ const [scope, name] = specifier.split("/");
+ packages.add(scope.startsWith("@") ? `${scope}/${name}` : scope);
+ }
+ }
+ }
+
+ return { scripts, packages };
+}
+
+describe("Docker image script imports", () => {
+ const { scripts, packages } = imageScriptGraph();
+
+ it("finds the scripts the entrypoint runs", () => {
+ expect(scripts).toContain("static-server.mjs");
+ expect(scripts).toContain("runtime-services-info.mjs");
+ });
+
+ it.each([...scripts].sort())("copies %s into /opt/agent-canvas", (script) => {
+ expect(dockerfile).toContain(
+ `COPY scripts/${script} /opt/agent-canvas/${script}`,
+ );
+ });
+
+ it.each([...packages].sort())(
+ "copies the %s package into /opt/agent-canvas/node_modules",
+ (pkg) => {
+ expect(dockerfile).toContain(
+ `COPY --from=frontend-build /build/node_modules/${pkg} /opt/agent-canvas/node_modules/${pkg}`,
+ );
+ },
+ );
+});
diff --git a/docker/Dockerfile b/docker/Dockerfile
index f69b3639c8..f4e4e86a7e 100644
--- a/docker/Dockerfile
+++ b/docker/Dockerfile
@@ -142,6 +142,17 @@ COPY --from=frontend-build /build/build /opt/agent-canvas/frontend
COPY scripts/static-server.mjs /opt/agent-canvas/static-server.mjs
COPY scripts/bind-host.mjs /opt/agent-canvas/bind-host.mjs
COPY scripts/proxy-utils.mjs /opt/agent-canvas/proxy-utils.mjs
+# static-server.mjs mounts the MARS web bridge, which pulls in these modules.
+COPY scripts/http-body.mjs /opt/agent-canvas/http-body.mjs
+COPY scripts/mars-web-bridge.mjs /opt/agent-canvas/mars-web-bridge.mjs
+COPY scripts/mars-tunnel-bridge.mjs /opt/agent-canvas/mars-tunnel-bridge.mjs
+COPY scripts/mars-api.mjs /opt/agent-canvas/mars-api.mjs
+COPY scripts/mars-credentials.mjs /opt/agent-canvas/mars-credentials.mjs
+COPY scripts/mars-oauth.mjs /opt/agent-canvas/mars-oauth.mjs
+COPY scripts/mars-session.mjs /opt/agent-canvas/mars-session.mjs
+COPY scripts/tunnel-client.mjs /opt/agent-canvas/tunnel-client.mjs
+COPY scripts/tunnel-registry.mjs /opt/agent-canvas/tunnel-registry.mjs
+COPY --from=frontend-build /build/node_modules/ws /opt/agent-canvas/node_modules/ws
COPY --from=frontend-build /build/node_modules/httpxy /opt/agent-canvas/node_modules/httpxy
COPY --from=frontend-build /build/node_modules/sirv /opt/agent-canvas/node_modules/sirv
COPY --from=frontend-build /build/node_modules/@polka /opt/agent-canvas/node_modules/@polka
diff --git a/docker/smoke-test.sh b/docker/smoke-test.sh
new file mode 100755
index 0000000000..63e0f6c573
--- /dev/null
+++ b/docker/smoke-test.sh
@@ -0,0 +1,58 @@
+#!/usr/bin/env bash
+# Start an agent-canvas image with no mounts and fail unless the unified
+# entry point (static-server on :8000, proxying /health to the agent-server)
+# answers and the container is still up after a stability window.
+#
+# A unit test cannot catch a module that exists in scripts/ but was never
+# copied into the image (#18342): the frontend dies on startup and the
+# entrypoint takes the whole container down with it.
+#
+# Usage: docker/smoke-test.sh <image> [host-port]
+set -euo pipefail
+
+IMAGE="${1:?usage: docker/smoke-test.sh <image> [host-port]}"
+PORT="${2:-8000}"
+READY_TIMEOUT="${SMOKE_READY_TIMEOUT:-300}"
+STABLE_SECONDS="${SMOKE_STABLE_SECONDS:-30}"
+NAME="agent-canvas-smoke-$$"
+HEALTH_URL="http://127.0.0.1:${PORT}/health"
+
+cleanup() {
+ status=$?
+ if [ "$status" -ne 0 ]; then
+ echo "--- last 60 lines of container logs ---"
+ docker logs --tail 60 "$NAME" 2>&1 || true
+ fi
+ docker rm -f "$NAME" >/dev/null 2>&1 || true
+}
+trap cleanup EXIT
+
+fail() {
+ echo "::error::Smoke test failed for ${IMAGE}: $*"
+ exit 1
+}
+
+running() {
+ [ "$(docker inspect -f '{{.State.Running}}' "$NAME" 2>/dev/null)" = "true" ]
+}
+
+docker run -d --name "$NAME" -p "127.0.0.1:${PORT}:8000" "$IMAGE" >/dev/null
+echo "Started ${IMAGE}; waiting up to ${READY_TIMEOUT}s for ${HEALTH_URL}"
+
+deadline=$((SECONDS + READY_TIMEOUT))
+until curl -fs -o /dev/null --max-time 5 "$HEALTH_URL"; do
+ running || fail "container exited before serving :8000"
+ [ "$SECONDS" -lt "$deadline" ] || fail "no healthy response within ${READY_TIMEOUT}s"
+ sleep 2
+done
+echo "Healthy after ${SECONDS}s; checking it stays up for ${STABLE_SECONDS}s"
+
+stable_until=$((SECONDS + STABLE_SECONDS))
+while [ "$SECONDS" -lt "$stable_until" ]; do
+ running || fail "container exited after becoming healthy"
+ sleep 2
+done
+curl -fsS -o /dev/null --max-time 5 "$HEALTH_URL" ||
+ fail "${HEALTH_URL} stopped answering"
+
+echo "Smoke test passed for ${IMAGE}"