Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
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
45 changes: 45 additions & 0 deletions src/lib/actions/sandbox/destroy-container-identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Row = {
managedBy?: string;
workspace?: string;
sandboxId?: string;
managedAlt?: string;
};

function observeRows(rows: Row[], malformedRows = 0) {
Expand All @@ -25,6 +26,7 @@ function observeRows(rows: Row[], malformedRows = 0) {
managedBy: row.managedBy ?? "",
workspace: row.workspace ?? "",
sandboxId: row.sandboxId ?? "",
managedAlt: row.managedAlt ?? "",
})),
malformedRows,
};
Expand All @@ -35,13 +37,15 @@ const MANAGED = {
managedBy: "openshell",
workspace: "default",
sandboxId: "sb-real",
managedAlt: "",
} as const;

const FOREIGN = {
id: "ffff000000000000",
managedBy: "",
workspace: "foreign",
sandboxId: "",
managedAlt: "",
} as const;

function expectAmbiguous(
Expand Down Expand Up @@ -79,6 +83,47 @@ describe("classifyDestroyContainerIdentity", () => {
).toEqual([podmanManaged]);
});

it("is clear for a container carrying only OpenShell's Podman-driver ownership marker (#11139)", () => {
// Verified against a live OpenShell 0.0.106 Podman-driver sandbox create:
// the container carries `openshell.managed=true` and never carries
// `openshell.ai/managed-by` at all.
const podmanManaged = { ...MANAGED, managedBy: "", managedAlt: "true" };

expect(classifyDestroyContainerIdentity("destroytest", observeRows([podmanManaged]))).toEqual({
status: "clear",
identity: podmanManaged,
});
});

it("still refuses a container whose Podman-driver marker has the wrong value", () => {
const podmanManaged = { ...MANAGED, managedBy: "", managedAlt: "false" };

expect(
expectAmbiguous(classifyDestroyContainerIdentity("destroytest", observeRows([podmanManaged])))
.foreign,
).toEqual([podmanManaged]);
});

it("refuses a foreign container even when it borrows the Podman marker's label key with an unmatched value", () => {
const spoofed = { ...FOREIGN, managedAlt: "TRUE" };

expect(
expectAmbiguous(classifyDestroyContainerIdentity("destroytest", observeRows([spoofed])))
.foreign,
).toEqual([spoofed]);
});

it("refuses when a foreign container shares the sandbox-name label with a Podman-managed sandbox", () => {
const podmanManaged = { ...MANAGED, managedBy: "", managedAlt: "true" };
const verdict = expectAmbiguous(
classifyDestroyContainerIdentity("destroytest", observeRows([podmanManaged, FOREIGN])),
);
expect(verdict.foreign).toHaveLength(1);
expect(verdict.foreign[0].id).toBe(FOREIGN.id);
expect(verdict.managed).toHaveLength(1);
expect(verdict.managed[0].id).toBe(podmanManaged.id);
});

it("refuses when a foreign container shares the sandbox-name label (#8999)", () => {
// The exact repro: a real managed sandbox plus a busybox that borrows the
// sandbox-name label with a different workspace and no managed marker.
Expand Down
79 changes: 59 additions & 20 deletions src/lib/actions/sandbox/destroy-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ describe("destroySandbox flow", () => {

it("revalidates schema-4 Portable identity at every destroy checkpoint without Docker preflight (#9189)", async () => {
const harness = createDestroyHarness({
dockerRunResult: { status: 0, stdout: `${"f".repeat(64)}\t\tforeign\t` },
dockerRunResult: { status: 0, stdout: `${"f".repeat(64)}\t\tforeign\t\t\tend` },
portableDestroyAuthority: true,
});

Expand Down Expand Up @@ -841,10 +841,46 @@ describe("destroySandbox flow", () => {
expect(harness.dockerRunSpy).toHaveBeenCalledOnce();
});

it("destroys a Podman-owned container end to end from a raw identity row (#11139)", async () => {
// The Podman driver stamps `openshell.managed=true` and leaves
// `openshell.ai/managed-by` empty, so this row exercises the whole handoff
// from raw `docker ps` output through identity classification to the
// sandbox delete and registry removal — not just the parser or the
// classifier in isolation.
const containerId = "aaaa000000000000";
const harness = createDestroyHarness({});
// The fake honours the identity-query contract: the Podman ownership value
// is visible only to a query that actually asks for that label. A caller
// that stops requesting it observes the same container with no ownership
// marker, which is foreign, so destroy fails closed through its own public
// behaviour instead of through a mock-shape assertion.
let removed = false;
harness.dockerRunSpy.mockImplementation(((args: unknown) => {
const argv = Array.isArray(args) ? args.map(String) : [];
const format = argv.at(-1) ?? "";
removed ||= argv[0] === "rm" && argv[1] === "-f" && argv[2] === containerId;
const podmanMarker = format.includes('{{.Label "openshell.managed"}}') ? "true" : "";
const row = `${containerId}\t\tdefault\tsb-alpha\t${podmanMarker}\tend`;
const listed = format === "{{.ID}}" ? containerId : row;
const visible = argv[0] === "ps" && !removed;
return { status: 0, stdout: visible ? listed : "", stderr: "" };
}) as never);

await expect(harness.destroySandbox("alpha", { yes: true })).resolves.toBeUndefined();

expect(harness.runOpenshellSpy).toHaveBeenCalledWith(
expect.arrayContaining(["sandbox", "delete"]),
expect.any(Object),
);
expect(harness.removeSandboxSpy).toHaveBeenCalledWith("alpha");
const errorOutput = harness.errorSpy.mock.calls.map((call) => String(call[0])).join("\n");
expect(errorOutput).not.toContain("could not verify one complete container identity");
});

it("refuses before destructive work when multiple Docker identities share the name", async () => {
const rows = [
"aaaa000000000000\topenshell\tdefault\tsb-real",
"ffff000000000000\t\tforeign\t",
"aaaa000000000000\topenshell\tdefault\tsb-real\t\tend",
"ffff000000000000\t\tforeign\t\t\tend",
].join("\n");
const harness = createDestroyHarness({
dockerRunResult: { status: 0, stdout: rows },
Expand Down Expand Up @@ -873,8 +909,8 @@ describe("destroySandbox flow", () => {
});

it("refuses identity drift after read-only destroy preflight (#8999)", async () => {
const managed = "aaaa000000000000\topenshell\tdefault\tsb-alpha";
const foreign = "ffff000000000000\t\tforeign\t";
const managed = "aaaa000000000000\topenshell\tdefault\tsb-alpha\t\tend";
const foreign = "ffff000000000000\t\tforeign\t\t\tend";
const harness = createDestroyHarness({
dockerRunResultSequence: [
{ status: 0, stdout: managed },
Expand Down Expand Up @@ -904,12 +940,12 @@ describe("destroySandbox flow", () => {
it.each([
[
"a replacement identity",
{ status: 0, stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta" },
{ status: 0, stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t\tend" },
],
["container absence", { status: 0, stdout: "" }],
["a failed revalidation probe", { status: 1, stderr: "daemon unavailable" }],
])("refuses %s before sandbox mutation", async (_scenario, changedIdentity) => {
const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha" };
const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha\t\tend" };
const harness = createDestroyHarness({
dockerRunResultSequence: [managed, managed, changedIdentity],
});
Expand All @@ -929,7 +965,7 @@ describe("destroySandbox flow", () => {
const absent = { status: 0, stdout: "" };
const replacement = {
status: 0,
stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta",
stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t\tend",
};
const harness = createDestroyHarness({
sandboxPresent: false,
Expand All @@ -945,10 +981,10 @@ describe("destroySandbox flow", () => {
});

it("restores MCP preparation when identity changes before wipe", async () => {
const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha" };
const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha\t\tend" };
const replacement = {
status: 0,
stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta",
stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t\tend",
};
const harness = createDestroyHarness({
mcpServers: ["github"],
Expand Down Expand Up @@ -994,7 +1030,7 @@ describe("destroySandbox flow", () => {
it.each([
[
"a replacement identity",
{ status: 0, stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta" },
{ status: 0, stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t\tend" },
"Container identity changed after managed inference cleanup",
],
[
Expand All @@ -1005,7 +1041,10 @@ describe("destroySandbox flow", () => {
])(
"restores MCP preparation and refuses workspace wipe after %s",
async (_scenario, changedIdentity, expectedMessage) => {
const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha" };
const managed = {
status: 0,
stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha\t\tend",
};
const harness = createDestroyHarness({
mcpServers: ["github"],
dockerRunResultSequence: [managed, managed, managed, managed, changedIdentity],
Expand All @@ -1028,10 +1067,10 @@ describe("destroySandbox flow", () => {
);

it("revalidates immediately before delete and reports partial provider preparation", async () => {
const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha" };
const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha\t\tend" };
const replacement = {
status: 0,
stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta",
stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t\tend",
};
const harness = createDestroyHarness({
detachedProviders: ["provider-a"],
Expand Down Expand Up @@ -1062,7 +1101,7 @@ describe("destroySandbox flow", () => {
});

it("keeps the final exact probe immediately adjacent to sandbox delete", async () => {
const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha" };
const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha\t\tend" };
const trace: string[] = [];
let identityProbeCalls = 0;
const harness = createDestroyHarness({
Expand Down Expand Up @@ -1192,7 +1231,7 @@ describe("destroySandbox flow", () => {
dockerOrphanIds: [containerId],
dockerRunResult: {
status: 0,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha`,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t\tend`,
},
});

Expand All @@ -1213,7 +1252,7 @@ describe("destroySandbox flow", () => {
dockerOrphanIds: [replacementContainerId],
dockerRunResult: {
status: 0,
stdout: `${expectedContainerId}\topenshell\tdefault\tsb-alpha`,
stdout: `${expectedContainerId}\topenshell\tdefault\tsb-alpha\t\tend`,
},
});

Expand All @@ -1235,7 +1274,7 @@ describe("destroySandbox flow", () => {
dockerRemoveStatus: 1,
dockerRunResult: {
status: 0,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha`,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t\tend`,
},
};
const harness = createDestroyHarness(options);
Expand Down Expand Up @@ -1264,7 +1303,7 @@ describe("destroySandbox flow", () => {
dockerOrphanQueryStatus: 1,
dockerRunResult: {
status: 0,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha`,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t\tend`,
},
});

Expand All @@ -1286,7 +1325,7 @@ describe("destroySandbox flow", () => {
dockerOrphanIds: [containerId],
dockerRunResult: {
status: 0,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha`,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t\tend`,
},
});

Expand Down
8 changes: 6 additions & 2 deletions src/lib/actions/sandbox/destroy-presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
OPENSHELL_SANDBOX_NAME_LABEL,
OPENSHELL_SANDBOX_WORKSPACE_LABEL,
inspectDockerSandboxNameLabeledContainers,
isOpenShellSandboxOwnershipMarker,
resolveOpenShellSandboxOwnershipLabel,
} from "../../onboard/openshell-docker-sandbox-containers";
import { fingerprintOpenShellSandboxId } from "../../adapters/openshell/sandbox-identity";
Expand All @@ -31,6 +32,7 @@ export type SandboxNameLabeledContainer = {
managedBy: string;
workspace: string;
sandboxId: string;
managedAlt: string;
};

/** Verdict for whether destroy resolved one complete managed container identity. */
Expand Down Expand Up @@ -99,8 +101,10 @@ export function classifyDestroyContainerIdentity(

const { malformedRows, rows } = observation;
const ownership = resolveOpenShellSandboxOwnershipLabel();
const managed = rows.filter((row) => row.managedBy === ownership.value);
const foreign = rows.filter((row) => row.managedBy !== ownership.value);
// Ownership policy is owned by the onboarding container module so destroy
// cannot drift from the rest of the lifecycle (#11139).
const managed = rows.filter((row) => isOpenShellSandboxOwnershipMarker(row));
const foreign = rows.filter((row) => !isOpenShellSandboxOwnershipMarker(row));

if (malformedRows > 0) {
return {
Expand Down
18 changes: 9 additions & 9 deletions src/lib/actions/sandbox/destroy-retained-recovery-flow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe("destroySandbox retained recovery flow", () => {
const sandboxContainerId = "a".repeat(64);
const bootstrapContainerId = "b".repeat(64);
const identityRows = [sandboxContainerId, bootstrapContainerId]
.map((id) => `${id}\topenshell\tdefault\tsb-alpha`)
.map((id) => `${id}\topenshell\tdefault\tsb-alpha\t\tend`)
.join("\n");
const harness = createDestroyHarness({
sandboxPresent: false,
Expand Down Expand Up @@ -173,7 +173,7 @@ describe("destroySandbox retained recovery flow", () => {
const bootstrapContainerId = "b".repeat(64);
const foreignContainerId = "e".repeat(64);
const identityRows = [sandboxContainerId, bootstrapContainerId]
.map((id) => `${id}\topenshell\tdefault\tsb-alpha`)
.map((id) => `${id}\topenshell\tdefault\tsb-alpha\t\tend`)
.join("\n");
const harness = createDestroyHarness({
sandboxPresent: false,
Expand Down Expand Up @@ -245,7 +245,7 @@ describe("destroySandbox retained recovery flow", () => {
const harness = createDestroyHarness({
dockerRunResult: {
status: 0,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha`,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t\tend`,
},
registryEntryOverrides: {
lifecycleGeneration: recovery.lifecycleGeneration!,
Expand Down Expand Up @@ -285,7 +285,7 @@ describe("destroySandbox retained recovery flow", () => {
const harness = createDestroyHarness({
dockerRunResult: {
status: 0,
stdout: `${containerId}\topenshell\tdefault\tsandbox-alpha`,
stdout: `${containerId}\topenshell\tdefault\tsandbox-alpha\t\tend`,
},
registryEntryOverrides: {
lifecycleGeneration: recovery.lifecycleGeneration!,
Expand Down Expand Up @@ -395,7 +395,7 @@ describe("destroySandbox retained recovery flow", () => {
sandboxPresent: false,
dockerRunResult: {
status: 0,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha`,
stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t\tend`,
},
registryEntryOverrides: {
lifecycleGeneration: recovery.lifecycleGeneration!,
Expand Down Expand Up @@ -441,7 +441,7 @@ describe("destroySandbox retained recovery flow", () => {
dockerOrphanIds: [bootstrapContainerId],
dockerRunResult: {
status: 0,
stdout: `${bootstrapContainerId}\topenshell\tdefault\tsb-alpha`,
stdout: `${bootstrapContainerId}\topenshell\tdefault\tsb-alpha\t\tend`,
},
registryEntryOverrides: {
lifecycleGeneration: recovery.lifecycleGeneration!,
Expand Down Expand Up @@ -473,7 +473,7 @@ describe("destroySandbox retained recovery flow", () => {
dockerOrphanIds: [bootstrapContainerId],
dockerRunResult: {
status: 0,
stdout: `${bootstrapContainerId}\topenshell\tdefault\tsb-alpha`,
stdout: `${bootstrapContainerId}\topenshell\tdefault\tsb-alpha\t\tend`,
},
registryEntryOverrides: {
lifecycleGeneration: recovery.lifecycleGeneration!,
Expand Down Expand Up @@ -551,7 +551,7 @@ describe("destroySandbox retained recovery flow", () => {
const sandboxContainerId = "a".repeat(64);
const bootstrapContainerId = "b".repeat(64);
const identityRows = [sandboxContainerId, bootstrapContainerId]
.map((id) => `${id}\topenshell\tdefault\tsb-alpha`)
.map((id) => `${id}\topenshell\tdefault\tsb-alpha\t\tend`)
.join("\n");
const harness = createDestroyHarness({
registryEntryPresent: false,
Expand Down Expand Up @@ -595,7 +595,7 @@ describe("destroySandbox retained recovery flow", () => {
registryEntryPresent: false,
dockerRunResult: {
status: 0,
stdout: `${"a".repeat(64)}\topenshell\tdefault\tsb-alpha`,
stdout: `${"a".repeat(64)}\topenshell\tdefault\tsb-alpha\t\tend`,
},
retainedRecoveryRecords: [firstRecovery, secondRecovery],
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/actions/sandbox/destroy-timeout-recovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("destroy timeout recovery", () => {
const harness = createDestroyHarness({
dockerRunResult: {
status: 0,
stdout: "aaaaaaaaaaaa\topenshell\tdefault\tsb-alpha\n",
stdout: "aaaaaaaaaaaa\topenshell\tdefault\tsb-alpha\t\tend\n",
},
mcpServers: ["github"],
registeredSandboxCount: 1,
Expand Down
Loading
Loading