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
22 changes: 17 additions & 5 deletions src/lib/onboard/managed-bootstrap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ records containing the provider and sandbox identities, plan and profile
fingerprints, exact original and replacement IDs, rollback target, and phase.
Exact commit and cleanup receipts are durable terminal records, so adapter
recreation does not depend on process-local transaction sets or tombstone maps.
Rollback retains an `owner-cleanup-required` phase only after image-owned shared
state is restored and the exact replacement is absent. That phase keeps the
restored original quiescent and preserves the journal without a terminal
receipt until the owning sandbox service removes the exact runtime and the
provider proves its absence. Unknown runtime presence is a retryable durable
cleanup failure, never evidence of absence.
The image-owned shared-state transaction uses the same identity-bound model: a
commit atomically moves its pending manifest and backups into a durable receipt
namespace, compacts that state to an exact commit receipt, and rejects rollback
Expand All @@ -73,11 +79,17 @@ image-owned commit forward or durably authorizes rollback; rollback-authorized
work completes exact restore and cleanup; and shared-state-committed work
completes exact backup cleanup and commit. Recovery persists an identity-bound
finalization receipt before removing the active journal, is idempotent across
another interruption, and returns normalized, provider-owned receipts in stable
identity order. The code reads mutable OpenShell names only to detect ownership
reuse, and unsafe name-only deletion returns a typed retention error. Multi-process
lease/arbitration remains an explicit production-activation gate. Activation
must also inject the selected gateway's canonical state root.
another interruption, and enumerates durable identities before loading each
record so one unreadable transaction does not hide other results. The provider
returns bounded `{ receipts, failures }` evidence; the coordinator validates,
copies, freezes, and orders both arrays without routing on provider phases or
failure codes. A failure for the requested sandbox name, or one whose sandbox
identity cannot be proven, blocks create. An exact failure for another sandbox
is warned and retained without blocking the requested create. The code reads
mutable OpenShell names only to detect ownership reuse, and unsafe name-only
deletion returns a typed retention error. Multi-process lease/arbitration
remains an explicit production-activation gate. Activation must also inject the
selected gateway's canonical state root.

The runtime-provider bundle is the only bootstrap registration boundary. The
candidate Docker surface owns create routing, replacement construction,
Expand Down
169 changes: 151 additions & 18 deletions src/lib/onboard/managed-bootstrap/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { createManagedStartupRootApplyRequest } from "../managed-startup/root-apply";
import {
activateManagedBootstrapSequence,
enforceManagedBootstrapRecoveryForSandbox,
finalizeManagedBootstrapSequence,
MANAGED_BOOTSTRAP_SCHEMA_VERSION,
type ManagedBootstrapAdapter,
Expand All @@ -24,6 +25,7 @@ import {
type ManagedBootstrapHeldWorkloadHandle,
type ManagedBootstrapObservedSnapshot,
type ManagedBootstrapPreparedReplacementHandle,
ManagedBootstrapRecoveryBlockedError,
type ManagedBootstrapReplacementHandle,
prepareManagedBootstrapSequence,
recoverManagedBootstrapTransactions,
Expand Down Expand Up @@ -232,7 +234,7 @@ function adapterFor(agent: ManagedStartupAgent): Fixture {
const order: string[] = [];
const raw: Fixture["raw"] = { handle: null, snapshot: null, prepared: null };
const adapter: ManagedBootstrapAdapter = {
recoverUnfinishedTransactions: vi.fn(async () => []),
recoverUnfinishedTransactions: vi.fn(async () => ({ receipts: [], failures: [] })),
createHeldWorkload: vi.fn(async (input) => {
order.push("create");
const receipt = await input.launch({
Expand Down Expand Up @@ -714,41 +716,172 @@ describe("managed bootstrap adapter contract", () => {
outcome: "rolled-back" as const,
finalization: { ...receipt, bootstrapIdentity },
});
vi.mocked(fixture.adapter.recoverUnfinishedTransactions).mockResolvedValueOnce([
candidate("b".repeat(64)),
candidate("a".repeat(64)),
]);
vi.mocked(fixture.adapter.recoverUnfinishedTransactions).mockResolvedValueOnce({
receipts: [candidate("b".repeat(64)), candidate("a".repeat(64))],
failures: [],
});

const recovered = await recoverManagedBootstrapTransactions(fixture.adapter);

expect(recovered.map(({ bootstrapIdentity }) => bootstrapIdentity)).toEqual([
expect(recovered.receipts.map(({ bootstrapIdentity }) => bootstrapIdentity)).toEqual([
"a".repeat(64),
"b".repeat(64),
]);
expect(Object.isFrozen(recovered)).toBe(true);
expect(recovered.every((entry) => Object.isFrozen(entry.finalization))).toBe(true);
expect(Object.isFrozen(recovered.receipts)).toBe(true);
expect(recovered.receipts.every((entry) => Object.isFrozen(entry.finalization))).toBe(true);
});

it("rejects recovery evidence whose provider does not own the durable sandbox", async () => {
const fixture = adapterFor("openclaw");
const receipt = cleanupReceipt();
vi.mocked(fixture.adapter.recoverUnfinishedTransactions).mockResolvedValueOnce([
{
schemaVersion: MANAGED_BOOTSTRAP_SCHEMA_VERSION,
providerId: "mxc",
sourcePhase: "cutover",
sandbox: receipt.sandbox,
bootstrapIdentity: IDENTITY,
outcome: "rolled-back",
finalization: receipt,
},
]);
vi.mocked(fixture.adapter.recoverUnfinishedTransactions).mockResolvedValueOnce({
receipts: [
{
schemaVersion: MANAGED_BOOTSTRAP_SCHEMA_VERSION,
providerId: "mxc",
sourcePhase: "cutover",
sandbox: receipt.sandbox,
bootstrapIdentity: IDENTITY,
outcome: "rolled-back",
finalization: receipt,
},
],
failures: [],
});

await expect(recoverManagedBootstrapTransactions(fixture.adapter)).rejects.toThrow(
"recovery provider does not own",
);
});

it("normalizes provider-neutral failures and preserves bounded MXC-style diagnostics", async () => {
const fixture = adapterFor("hermes");
const failure = (bootstrapIdentity: string, sandboxName: string | null) => ({
schemaVersion: MANAGED_BOOTSTRAP_SCHEMA_VERSION,
providerId: "mxc",
sourcePhase: "provider-owned-cleanup",
sandbox:
sandboxName === null
? null
: { sandboxName, sandboxId: `mxc-${sandboxName}`, driverId: "mxc" },
bootstrapIdentity,
code: "provider-owned-retry",
retryable: true,
detail: "opaque MXC recovery evidence",
});
vi.mocked(fixture.adapter.recoverUnfinishedTransactions).mockResolvedValueOnce({
receipts: [],
failures: [failure("b".repeat(64), "bravo"), failure("a".repeat(64), null)],
});

const recovered = await recoverManagedBootstrapTransactions(fixture.adapter);

expect(recovered.failures.map(({ bootstrapIdentity }) => bootstrapIdentity)).toEqual([
"a".repeat(64),
"b".repeat(64),
]);
expect(recovered.failures[0]).toMatchObject({ sandbox: null, providerId: "mxc" });
expect(Object.isFrozen(recovered.failures)).toBe(true);
expect(recovered.failures.every(Object.isFrozen)).toBe(true);
});

it("rejects duplicate identities across recovered receipts and failures", async () => {
const fixture = adapterFor("openclaw");
const receipt = cleanupReceipt();
vi.mocked(fixture.adapter.recoverUnfinishedTransactions).mockResolvedValueOnce({
receipts: [
{
schemaVersion: MANAGED_BOOTSTRAP_SCHEMA_VERSION,
providerId: receipt.sandbox.driverId,
sourcePhase: "cutover",
sandbox: receipt.sandbox,
bootstrapIdentity: IDENTITY,
outcome: "rolled-back",
finalization: receipt,
},
],
failures: [
{
schemaVersion: MANAGED_BOOTSTRAP_SCHEMA_VERSION,
providerId: receipt.sandbox.driverId,
sourcePhase: "cleanup",
sandbox: receipt.sandbox,
bootstrapIdentity: IDENTITY,
code: "retry",
retryable: true,
detail: "retained",
},
],
});

await expect(recoverManagedBootstrapTransactions(fixture.adapter)).rejects.toThrow(
"duplicate bootstrap identities",
);
});

it("rejects an unbounded provider recovery result before normalizing records", async () => {
const fixture = adapterFor("hermes");
const candidate = {
schemaVersion: MANAGED_BOOTSTRAP_SCHEMA_VERSION,
providerId: "mxc",
sourcePhase: "provider-owned-cleanup",
sandbox: null,
bootstrapIdentity: IDENTITY,
code: "provider-owned-retry",
retryable: true,
detail: "opaque MXC recovery evidence",
} as const;
vi.mocked(fixture.adapter.recoverUnfinishedTransactions).mockResolvedValueOnce({
receipts: [],
failures: Array.from({ length: 4097 }, () => candidate),
});

await expect(recoverManagedBootstrapTransactions(fixture.adapter)).rejects.toThrow(
"provider recovery returned too many records",
);
});

it("blocks same-name and identity-unknown failures while warning for unrelated sandboxes", () => {
const failure = (bootstrapIdentity: string, sandboxName: string | null) =>
Object.freeze({
schemaVersion: MANAGED_BOOTSTRAP_SCHEMA_VERSION,
providerId: "mxc",
sourcePhase: "cleanup",
sandbox:
sandboxName === null
? null
: Object.freeze({ sandboxName, sandboxId: `mxc-${sandboxName}`, driverId: "mxc" }),
bootstrapIdentity,
code: "provider-owned-retry",
retryable: true,
detail: "opaque provider detail",
});
const warn = vi.fn();
const unrelated = failure("a".repeat(64), "bravo");
const sameName = failure("b".repeat(64), "alpha");
const identityUnknown = failure("c".repeat(64), null);

expect(
enforceManagedBootstrapRecoveryForSandbox(
Object.freeze({ receipts: Object.freeze([]), failures: Object.freeze([unrelated]) }),
"alpha",
warn,
),
).toMatchObject({ failures: [unrelated] });
expect(warn).toHaveBeenCalledWith(expect.stringContaining("unrelated sandbox 'bravo'"));

for (const blocking of [sameName, identityUnknown]) {
expect(() =>
enforceManagedBootstrapRecoveryForSandbox(
Object.freeze({ receipts: Object.freeze([]), failures: Object.freeze([blocking]) }),
"alpha",
warn,
),
).toThrow(ManagedBootstrapRecoveryBlockedError);
}
});

it.each([
"BASH_ENV=/sandbox/attacker",
"ENV=/sandbox/attacker",
Expand Down
Loading
Loading