From eb7e832293e18d71631a30c2cb4d477dc138eb8f Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 30 Nov 2026 02:21:29 +0000 Subject: [PATCH 1/2] feat: support credential-free base snapshot setup script --- README.md | 12 ++++++++++++ apps/server/src/vm/bases.ts | 28 ++++++++++++++++++++++++---- apps/server/src/vm/runtime.ts | 2 +- apps/server/test/bases.test.ts | 11 +++++++++-- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index fec6188e..702d6dc9 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,18 @@ secrets and never shown again. To add many at once, paste a `.env` file or a blo `export NAME=value` lines into the name field, check the list, and save them together. Names VM Bridge sets itself, such as `PATH`, `HOME`, `CLAUDE_CONFIG_DIR` or `GH_TOKEN`, are refused. +### Custom base setup + +Set `VMBRIDGE_BASE_SETUP_SCRIPT` to the path of a readable, credential-free Bash +script on the machine running VM Bridge. VM Bridge reads it at startup and runs +it inside each new base snapshot builder after installing T3 and the agent CLIs, +before saving the snapshot. The script's content is part of the snapshot stamp: +restart VM Bridge after editing it to trigger a fresh build. The script is not +run on existing VMs. Keep sign-ins, tokens, repositories, and personal files out +of it, since a provider snapshot includes the builder's entire disk. If the +script fails, the base build fails and the dashboard reports the error. Leave +the variable unset for the normal base setup. + ## Skills Under **Skills**, press **Add skill** to install one from skills.sh, or use the **This machine** tab diff --git a/apps/server/src/vm/bases.ts b/apps/server/src/vm/bases.ts index d979913e..4323c5eb 100644 --- a/apps/server/src/vm/bases.ts +++ b/apps/server/src/vm/bases.ts @@ -1,9 +1,11 @@ import { SandboxError } from "@opencoredev/sandbox-sdk"; +import { createHash } from "node:crypto"; +import { readFileSync } from "node:fs"; import type { ProviderId, ProviderSetup, VmSize } from "@vmbridge/contracts"; import { providers, type Driver } from "../providers/index.ts"; import type { BaseSnapshot, Store } from "../store.ts"; import type { Traffic } from "../traffic.ts"; -import { prepareBase, setupStamp, t3Port } from "./runtime.ts"; +import { check, prepareBase, runProgram, setupStamp, t3Port } from "./runtime.ts"; /** A base snapshot this old is rebuilt, so new VMs get recent agent CLIs, which setup installs at their latest. */ export const baseMaxAgeMs = 7 * 24 * 60 * 60_000; @@ -13,7 +15,20 @@ export const baseRetryMs = 10 * 60_000; * A build running longer than this is given up: its VM is deleted and it counts as failed, so a hung provider call * cannot hold its provider back, and Retry or a new key can start another. */ -export const baseSetupLimitMs = 15 * 60_000; +export const baseSetupLimitMs = 30 * 60_000; + +/** Optional host-side, credential-free script run only while building a new base snapshot. Restart to reload it. */ +export function baseSetupScript(path = process.env.VMBRIDGE_BASE_SETUP_SCRIPT): string | null { + if (!path) return null; + const script = readFileSync(path, "utf8"); + if (!script.trim()) throw new Error(`Base setup script is empty: ${path}`); + return script; +} + +export function baseStamp(t3Version: string, script: string | null) { + if (!script) return setupStamp(t3Version); + return createHash("sha256").update(setupStamp(t3Version)).update("\n").update(script).digest("hex"); +} /** * The snapshot a new VM starts from: the saved one when it holds what setup installs now (same stamp) and is @@ -30,6 +45,7 @@ export function chooseBase(base: BaseSnapshot | null, stamp: string, now: number * created without a current one. */ export class Bases { + private readonly setupScript = baseSetupScript(); private readonly building = new Map; abort: AbortController }>(); private readonly failures = new Map(); @@ -70,7 +86,7 @@ export class Bases { */ choose(provider: ProviderId, size: VmSize, t3Version: string): { from: string | null; rebuild: boolean } { if (!this.driver(provider).snapshot) return { from: null, rebuild: false }; - return chooseBase(this.store.base(provider, size), setupStamp(t3Version), Date.now()); + return chooseBase(this.store.base(provider, size), baseStamp(t3Version, this.setupScript), Date.now()); } /** Forgets a snapshot a VM could not start from, which may be gone or belong to another account. */ @@ -228,6 +244,10 @@ export class Bases { let snapshot: string; try { await prepareBase(box, t3Version); + if (this.setupScript) { + await runProgram(box, this.setupScript, "Base setup script", 15 * 60_000); + await check(box, "sync", "Syncing base setup tools"); + } signal.throwIfAborted(); if (!driver.snapshot) throw new Error(`${provider} cannot snapshot a VM.`); snapshot = await driver.snapshot(box); @@ -247,7 +267,7 @@ export class Bases { ); } const replaced = this.store.base(provider, size)?.snapshot; - this.store.saveBase(provider, size, { snapshot, stamp: setupStamp(t3Version), createdAt: Date.now() }); + this.store.saveBase(provider, size, { snapshot, stamp: baseStamp(t3Version, this.setupScript), createdAt: Date.now() }); if (replaced && replaced !== snapshot) this.store.retireSnapshot(provider, replaced); await this.sweep(provider); this.record(provider, `build ${size} base snapshot`, "ok", Math.round(performance.now() - started)); diff --git a/apps/server/src/vm/runtime.ts b/apps/server/src/vm/runtime.ts index 1ef2342b..f0947555 100644 --- a/apps/server/src/vm/runtime.ts +++ b/apps/server/src/vm/runtime.ts @@ -134,7 +134,7 @@ export function envValueProblem(value: string) { function envFile(credentials: VmCredentials, fingerprint: string) { const lines = [ `unset ${providerVars.join(" ")}`, - `export PATH="${root}/node/bin:${root}/npm/bin:${root}/gh/bin:$PATH"`, + `export PATH="${root}/node/bin:${root}/npm/bin:${root}/gh/bin:${root}/tools/bin:$PATH"`, // Not exported: only start-t3.sh reads it, to record what the server starts with. `VMBRIDGE_CREDENTIALS=${fingerprint}`, ]; diff --git a/apps/server/test/bases.test.ts b/apps/server/test/bases.test.ts index 5d3ebab5..e5bd1973 100644 --- a/apps/server/test/bases.test.ts +++ b/apps/server/test/bases.test.ts @@ -9,7 +9,7 @@ import { Secrets, Settings } from "../src/settings.ts"; import { Skills } from "../src/skills.ts"; import { Store } from "../src/store.ts"; import type { Traffic } from "../src/traffic.ts"; -import { baseMaxAgeMs, baseSetupLimitMs, chooseBase } from "../src/vm/bases.ts"; +import { baseMaxAgeMs, baseSetupLimitMs, baseStamp, chooseBase } from "../src/vm/bases.ts"; import { setupStamp } from "../src/vm/runtime.ts"; import { testEncryption } from "./key.ts"; @@ -28,6 +28,13 @@ describe("choosing a base snapshot for a new VM", () => { expect(chooseBase(base, stamp, now)).toEqual({ from: "snap1", rebuild: false }); }); + test("a changed base setup script invalidates the saved snapshot", () => { + expect(baseStamp("1.0.0", null)).toBe(stamp); + const withTools = baseStamp("1.0.0", "echo tools v1"); + expect(chooseBase({ ...base, stamp: withTools }, withTools, now).from).toBe("snap1"); + expect(chooseBase({ ...base, stamp: withTools }, baseStamp("1.0.0", "echo tools v2"), now).rebuild).toBe(true); + }); + test("creates the plain way and rebuilds when there is none, it is out of date, or it is a week old", () => { const plain = { from: null, rebuild: true }; expect(chooseBase(null, stamp, now)).toEqual(plain); @@ -373,7 +380,7 @@ describe("setting up a provider", () => { jest.useRealTimers(); } const builder = events.find((event) => event.startsWith("create"))!.slice("create ".length); - expect(fleet.setup("boxd")).toEqual({ state: "failed", reason: "Setup took longer than 15 minutes." }); + expect(fleet.setup("boxd")).toEqual({ state: "failed", reason: `Setup took longer than ${baseSetupLimitMs / 60_000} minutes.` }); await until(() => events.includes(`destroy ${builder}`)); expect(store.base("boxd", "medium")?.builder).toBeNull(); From b06a31fc157c65c7b92f961e159355be003df243 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 30 Nov 2026 02:24:59 +0000 Subject: [PATCH 2/2] fix: load base script without a Workers filesystem import --- apps/server/src/vm/bases.ts | 7 +++++-- apps/server/test/bases.test.ts | 14 +++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/apps/server/src/vm/bases.ts b/apps/server/src/vm/bases.ts index 4323c5eb..2bfb2d1d 100644 --- a/apps/server/src/vm/bases.ts +++ b/apps/server/src/vm/bases.ts @@ -1,6 +1,5 @@ import { SandboxError } from "@opencoredev/sandbox-sdk"; import { createHash } from "node:crypto"; -import { readFileSync } from "node:fs"; import type { ProviderId, ProviderSetup, VmSize } from "@vmbridge/contracts"; import { providers, type Driver } from "../providers/index.ts"; import type { BaseSnapshot, Store } from "../store.ts"; @@ -20,7 +19,11 @@ export const baseSetupLimitMs = 30 * 60_000; /** Optional host-side, credential-free script run only while building a new base snapshot. Restart to reload it. */ export function baseSetupScript(path = process.env.VMBRIDGE_BASE_SETUP_SCRIPT): string | null { if (!path) return null; - const script = readFileSync(path, "utf8"); + // This module is also bundled for Cloudflare Workers, which have no host filesystem. + if (typeof Bun === "undefined") throw new Error("A base setup script requires the local VMBridge server."); + const read = Bun.spawnSync({ cmd: ["cat", path], stdout: "pipe", stderr: "pipe" }); + if (read.exitCode !== 0) throw new Error(`Cannot read base setup script ${path}: ${read.stderr.toString().trim()}`); + const script = read.stdout.toString(); if (!script.trim()) throw new Error(`Base setup script is empty: ${path}`); return script; } diff --git a/apps/server/test/bases.test.ts b/apps/server/test/bases.test.ts index e5bd1973..faefa88a 100644 --- a/apps/server/test/bases.test.ts +++ b/apps/server/test/bases.test.ts @@ -1,5 +1,6 @@ import { afterEach, describe, expect, jest, spyOn, test } from "bun:test"; import { SandboxError } from "@opencoredev/sandbox-sdk"; +import { rmSync } from "node:fs"; import type { AgentAuth } from "../src/agent-auth/profiles.ts"; import { openSocket } from "../src/bun/socket.ts"; import { openDatabase } from "../src/bun/sqlite.ts"; @@ -9,7 +10,7 @@ import { Secrets, Settings } from "../src/settings.ts"; import { Skills } from "../src/skills.ts"; import { Store } from "../src/store.ts"; import type { Traffic } from "../src/traffic.ts"; -import { baseMaxAgeMs, baseSetupLimitMs, baseStamp, chooseBase } from "../src/vm/bases.ts"; +import { baseMaxAgeMs, baseSetupLimitMs, baseSetupScript, baseStamp, chooseBase } from "../src/vm/bases.ts"; import { setupStamp } from "../src/vm/runtime.ts"; import { testEncryption } from "./key.ts"; @@ -35,6 +36,17 @@ describe("choosing a base snapshot for a new VM", () => { expect(chooseBase({ ...base, stamp: withTools }, baseStamp("1.0.0", "echo tools v2"), now).rebuild).toBe(true); }); + test("reads the operator's script at startup and rejects a missing file", async () => { + const path = `/tmp/vmbridge-base-${crypto.randomUUID()}.sh`; + try { + await Bun.write(path, "echo installed\n"); + expect(baseSetupScript(path)).toBe("echo installed\n"); + } finally { + rmSync(path, { force: true }); + } + expect(() => baseSetupScript(path)).toThrow("Cannot read base setup script"); + }); + test("creates the plain way and rebuilds when there is none, it is out of date, or it is a week old", () => { const plain = { from: null, rebuild: true }; expect(chooseBase(null, stamp, now)).toEqual(plain);