diff --git a/src/lib/actions/sandbox/destroy-container-identity.test.ts b/src/lib/actions/sandbox/destroy-container-identity.test.ts index 44fc8d6060e..24e1093ec78 100644 --- a/src/lib/actions/sandbox/destroy-container-identity.test.ts +++ b/src/lib/actions/sandbox/destroy-container-identity.test.ts @@ -15,6 +15,7 @@ type Row = { managedBy?: string; workspace?: string; sandboxId?: string; + managedAlt?: string; }; function observeRows(rows: Row[], malformedRows = 0) { @@ -25,6 +26,7 @@ function observeRows(rows: Row[], malformedRows = 0) { managedBy: row.managedBy ?? "", workspace: row.workspace ?? "", sandboxId: row.sandboxId ?? "", + managedAlt: row.managedAlt ?? "", })), malformedRows, }; @@ -35,6 +37,7 @@ const MANAGED = { managedBy: "openshell", workspace: "default", sandboxId: "sb-real", + managedAlt: "", } as const; const FOREIGN = { @@ -42,6 +45,7 @@ const FOREIGN = { managedBy: "", workspace: "foreign", sandboxId: "", + managedAlt: "", } as const; function expectAmbiguous( @@ -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. diff --git a/src/lib/actions/sandbox/destroy-flow.test.ts b/src/lib/actions/sandbox/destroy-flow.test.ts index d139ea9f436..3f2d1acd7ea 100644 --- a/src/lib/actions/sandbox/destroy-flow.test.ts +++ b/src/lib/actions/sandbox/destroy-flow.test.ts @@ -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, }); @@ -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 }, @@ -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 }, @@ -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], }); @@ -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, @@ -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"], @@ -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", ], [ @@ -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], @@ -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"], @@ -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({ @@ -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`, }, }); @@ -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`, }, }); @@ -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); @@ -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`, }, }); @@ -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`, }, }); diff --git a/src/lib/actions/sandbox/destroy-presence.ts b/src/lib/actions/sandbox/destroy-presence.ts index 606efff39a0..5626b57e036 100644 --- a/src/lib/actions/sandbox/destroy-presence.ts +++ b/src/lib/actions/sandbox/destroy-presence.ts @@ -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"; @@ -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. */ @@ -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 { diff --git a/src/lib/actions/sandbox/destroy-retained-recovery-flow.test.ts b/src/lib/actions/sandbox/destroy-retained-recovery-flow.test.ts index 15cbf5c4e95..75f33091149 100644 --- a/src/lib/actions/sandbox/destroy-retained-recovery-flow.test.ts +++ b/src/lib/actions/sandbox/destroy-retained-recovery-flow.test.ts @@ -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, @@ -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, @@ -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!, @@ -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!, @@ -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!, @@ -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!, @@ -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!, @@ -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, @@ -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], }); diff --git a/src/lib/actions/sandbox/destroy-timeout-recovery.test.ts b/src/lib/actions/sandbox/destroy-timeout-recovery.test.ts index 11fc849c295..4c5e95c1fec 100644 --- a/src/lib/actions/sandbox/destroy-timeout-recovery.test.ts +++ b/src/lib/actions/sandbox/destroy-timeout-recovery.test.ts @@ -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, diff --git a/src/lib/adapters/docker/inspect-identity.test.ts b/src/lib/adapters/docker/inspect-identity.test.ts index 7eff8c7afb8..6c421f4993e 100644 --- a/src/lib/adapters/docker/inspect-identity.test.ts +++ b/src/lib/adapters/docker/inspect-identity.test.ts @@ -15,7 +15,7 @@ describe("inspectDockerSandboxIdentities", () => { it("queries only the sandbox-name label and parses one exact row", () => { const inspect = vi.fn((_args: readonly string[]) => ({ status: 0, - stdout: "aaaa000000000000\topenshell\tdefault\tsb-real", + stdout: "aaaa000000000000\topenshell\tdefault\tsb-real\topenshell\tend", })); expect( @@ -29,6 +29,7 @@ describe("inspectDockerSandboxIdentities", () => { managedBy: "openshell", workspace: "default", sandboxId: "sb-real", + managedAlt: "openshell", }, ], }); @@ -37,6 +38,34 @@ describe("inspectDockerSandboxIdentities", () => { expect(inspect.mock.calls[0][0]).not.toContain("label=openshell.ai/managed-by=openshell"); }); + it("queries a separate driver-specific marker when managedAlt differs from managedBy", () => { + const inspect = vi.fn((_args: readonly string[]) => ({ + status: 0, + stdout: "aaaa000000000000\t\tdefault\tsb-real\ttrue\tend", + })); + + expect( + inspectDockerSandboxIdentities( + "openshell.ai/sandbox-name=alpha", + { ...LABELS, managedAlt: "openshell.managed" }, + inspect, + ), + ).toEqual({ + status: "observed", + malformedRows: 0, + rows: [ + { + id: "aaaa000000000000", + managedBy: "", + workspace: "default", + sandboxId: "sb-real", + managedAlt: "true", + }, + ], + }); + expect(inspect.mock.calls[0][0].join("\t")).toContain('{{.Label "openshell.managed"}}'); + }); + it.each([ ["trailing vertical tab", "openshell\u000b"], ["leading whitespace", " openshell"], @@ -44,7 +73,7 @@ describe("inspectDockerSandboxIdentities", () => { expect( inspectDockerSandboxIdentities("openshell.ai/sandbox-name=alpha", LABELS, () => ({ status: 0, - stdout: `aaaa000000000000\t${managedBy}\tdefault\tsb-real`, + stdout: `aaaa000000000000\t${managedBy}\tdefault\tsb-real\topenshell\tend`, })), ).toEqual({ status: "observed", rows: [], malformedRows: 1 }); }); diff --git a/src/lib/adapters/docker/inspect.ts b/src/lib/adapters/docker/inspect.ts index 437af7ce009..b8d5d8513a1 100644 --- a/src/lib/adapters/docker/inspect.ts +++ b/src/lib/adapters/docker/inspect.ts @@ -8,6 +8,13 @@ export type DockerSandboxIdentityRow = { managedBy: string; workspace: string; sandboxId: string; + /** + * Alternate driver-specific ownership marker (e.g. OpenShell's Podman + * driver labels containers `openshell.managed=true` instead of the Docker + * driver's `openshell.ai/managed-by=openshell`). Empty when the caller did + * not request an alternate marker. + */ + managedAlt: string; }; export type DockerSandboxIdentityObservation = @@ -19,6 +26,15 @@ type DockerSandboxIdentityInspect = ( opts?: DockerRunOptions, ) => Partial, "error" | "status" | "stderr" | "stdout">>; +/** + * Literal final column. `run()` trims captured stdout, so a row whose last + * column is empty would silently lose that field and be rejected as + * malformed. The alternate ownership marker is empty for every container the + * Docker driver labels, so without this terminator destroy would fail closed + * on the ordinary case. Keeping it non-empty makes the row width independent + * of which labels a driver happens to stamp. + */ +const IDENTITY_ROW_TERMINATOR = "end"; const DOCKER_IDENTITY_PROBE_TIMEOUT_MS = 30_000; const DOCKER_IDENTITY_PROBE_MAX_BUFFER_BYTES = 256 * 1024; const IDENTITY_VALUE_MAX_LENGTH = 256; @@ -37,14 +53,21 @@ function isExactBoundedIdentityText(value: string): boolean { /** Inspect and parse the exact Docker rows used by sandbox destroy. */ export function inspectDockerSandboxIdentities( sandboxNameLabel: string, - labelKeys: { managedBy: string; workspace: string; sandboxId: string }, + labelKeys: { managedBy: string; workspace: string; sandboxId: string; managedAlt?: string }, inspect: DockerSandboxIdentityInspect = dockerRun, ): DockerSandboxIdentityObservation { + // managedAlt queries a second, driver-specific label key so a container can + // still be recognized as owned when the requesting driver does not stamp + // labelKeys.managedBy (e.g. OpenShell's Podman driver labels containers + // `openshell.managed=true` instead of `openshell.ai/managed-by=openshell`). + const altLabelKey = labelKeys.managedAlt ?? labelKeys.managedBy; const format = [ "{{.ID}}", `{{.Label "${labelKeys.managedBy}"}}`, `{{.Label "${labelKeys.workspace}"}}`, `{{.Label "${labelKeys.sandboxId}"}}`, + `{{.Label "${altLabelKey}"}}`, + IDENTITY_ROW_TERMINATOR, ].join("\t"); const result = inspect( ["ps", "-a", "--no-trunc", "--filter", `label=${sandboxNameLabel}`, "--format", format], @@ -70,15 +93,23 @@ export function inspectDockerSandboxIdentities( if (rawLine.length === 0) continue; const fields = rawLine.split("\t"); if ( - fields.length !== 4 || + fields.length !== 6 || + fields[5] !== IDENTITY_ROW_TERMINATOR || !DOCKER_CONTAINER_ID_PATTERN.test(fields[0] ?? "") || !fields.every(isExactBoundedIdentityText) ) { malformedRows += 1; continue; } - const [id, managedBy, workspace, sandboxId] = fields as [string, string, string, string]; - rows.push({ id, managedBy, workspace, sandboxId }); + const [id, managedBy, workspace, sandboxId, managedAlt] = fields as [ + string, + string, + string, + string, + string, + string, + ]; + rows.push({ id, managedBy, workspace, sandboxId, managedAlt }); } return { status: "observed", rows, malformedRows }; } diff --git a/src/lib/onboard/openshell-docker-sandbox-containers.test.ts b/src/lib/onboard/openshell-docker-sandbox-containers.test.ts index 8c1cdf3c5fc..9e3044b754d 100644 --- a/src/lib/onboard/openshell-docker-sandbox-containers.test.ts +++ b/src/lib/onboard/openshell-docker-sandbox-containers.test.ts @@ -35,6 +35,7 @@ function observeContainerIds(ids: readonly string[], malformedRows = 0) { managedBy: "openshell", workspace: "default", sandboxId: "sb-alpha", + managedAlt: "", })), malformedRows, }; diff --git a/src/lib/onboard/openshell-docker-sandbox-containers.ts b/src/lib/onboard/openshell-docker-sandbox-containers.ts index 6de44934e3e..b5c19e794e3 100644 --- a/src/lib/onboard/openshell-docker-sandbox-containers.ts +++ b/src/lib/onboard/openshell-docker-sandbox-containers.ts @@ -10,6 +10,13 @@ import type { DockerGpuPatchDeps } from "./docker-gpu-patch-types"; export const OPENSHELL_MANAGED_BY_LABEL = "openshell.ai/managed-by"; export const OPENSHELL_MANAGED_BY_VALUE = "openshell"; +// OpenShell's Podman driver does not stamp OPENSHELL_MANAGED_BY_LABEL on the +// containers it creates; it stamps this driver-specific marker instead +// (verified against a live OpenShell 0.0.106 Podman-driver sandbox create, +// #11139). Treat either marker as proof of OpenShell ownership so destroy's +// identity check does not fail closed on every Podman-driver sandbox. +export const OPENSHELL_PODMAN_MANAGED_LABEL = "openshell.managed"; +export const OPENSHELL_PODMAN_MANAGED_VALUE = "true"; export const OPENSHELL_SANDBOX_NAME_LABEL = "openshell.ai/sandbox-name"; export const OPENSHELL_SANDBOX_ID_LABEL = "openshell.ai/sandbox-id"; export const OPENSHELL_SANDBOX_NAMESPACE_LABEL = "openshell.ai/sandbox-namespace"; @@ -26,12 +33,36 @@ export function resolveOpenShellSandboxOwnershipLabel( return { label: OPENSHELL_MANAGED_BY_LABEL, value: OPENSHELL_MANAGED_BY_VALUE }; } +/** + * Sole owner of the "is this container OpenShell's" policy. Callers that hold + * an observed label map and callers that hold a parsed identity row both route + * here, so a new or changed driver marker is defined once (#11139). + * + * The Docker driver stamps `openshell.ai/managed-by=openshell`; the Podman + * driver stamps `openshell.managed=true` and omits `managed-by` entirely. + */ +export function isOpenShellSandboxOwnershipMarker( + observed: { readonly managedBy: string; readonly managedAlt: string }, + env: NodeJS.ProcessEnv = process.env, +): boolean { + const ownership = resolveOpenShellSandboxOwnershipLabel(env); + return ( + observed.managedBy === ownership.value || observed.managedAlt === OPENSHELL_PODMAN_MANAGED_VALUE + ); +} + export function hasOpenShellSandboxOwnership( labels: Readonly>, env: NodeJS.ProcessEnv = process.env, ): boolean { const ownership = resolveOpenShellSandboxOwnershipLabel(env); - return labels[ownership.label] === ownership.value; + return isOpenShellSandboxOwnershipMarker( + { + managedBy: labels[ownership.label] ?? "", + managedAlt: labels[OPENSHELL_PODMAN_MANAGED_LABEL] ?? "", + }, + env, + ); } const DOCKER_SANDBOX_QUERY_TIMEOUT_MS = 30_000; @@ -228,6 +259,7 @@ export function inspectDockerSandboxNameLabeledContainers( managedBy: OPENSHELL_MANAGED_BY_LABEL, workspace: OPENSHELL_SANDBOX_WORKSPACE_LABEL, sandboxId: OPENSHELL_SANDBOX_ID_LABEL, + managedAlt: OPENSHELL_PODMAN_MANAGED_LABEL, }); } diff --git a/src/lib/onboard/sandbox-create/orchestration-final-handoff.test.ts b/src/lib/onboard/sandbox-create/orchestration-final-handoff.test.ts index 7d709457c2d..da297085b91 100644 --- a/src/lib/onboard/sandbox-create/orchestration-final-handoff.test.ts +++ b/src/lib/onboard/sandbox-create/orchestration-final-handoff.test.ts @@ -99,6 +99,7 @@ describe("durable final-handoff publication", () => { managedBy: "openshell", workspace: "alpha", sandboxId, + managedAlt: "", }, ], }, @@ -116,6 +117,7 @@ describe("durable final-handoff publication", () => { managedBy: "openshell", workspace: "alpha", sandboxId: "foreign-sandbox-id", + managedAlt: "", }, ], }, diff --git a/test/cli/destroy-gateway-cleanup.test.ts b/test/cli/destroy-gateway-cleanup.test.ts index befe1945b02..601dca60166 100644 --- a/test/cli/destroy-gateway-cleanup.test.ts +++ b/test/cli/destroy-gateway-cleanup.test.ts @@ -17,7 +17,7 @@ case "$1" in for arg do last_arg="$arg"; done case "$last_arg" in '{{.ID}}') printf 'aaaaaaaaaaaa\n' ;; - *) printf 'aaaaaaaaaaaa\topenshell\tdefault\tsb-alpha\n' ;; + *) printf 'aaaaaaaaaaaa\topenshell\tdefault\tsb-alpha\t\tend\n' ;; esac fi ;; diff --git a/test/helpers/destroy-flow-test-harness.ts b/test/helpers/destroy-flow-test-harness.ts index 9ea27539d02..32649591cee 100644 --- a/test/helpers/destroy-flow-test-harness.ts +++ b/test/helpers/destroy-flow-test-harness.ts @@ -579,12 +579,14 @@ export function createDestroyHarness(options: DestroyHarnessOptions = {}): Destr options.onDockerRun?.(identityProbeCall); const defaultIdentityResult = { status: 0, - stdout: sandboxPresent ? "aaaaaaaaaaaa\topenshell\tdefault\tsb-alpha" : "", + stdout: sandboxPresent ? "aaaaaaaaaaaa\topenshell\tdefault\tsb-alpha\t\tend" : "", }; const sequencedResult = options.dockerRunResultSequence?.[identityProbeCall - 1]; const exactCleanupResult = { status: options.dockerOrphanQueryStatus ?? 0, - stdout: dockerNameLabeledIds.map((id) => `${id}\topenshell\tdefault\tsb-alpha`).join("\n"), + stdout: dockerNameLabeledIds + .map((id) => `${id}\topenshell\tdefault\tsb-alpha\t\tend`) + .join("\n"), stderr: "", }; const result = diff --git a/test/helpers/onboard-script-mocks.cjs b/test/helpers/onboard-script-mocks.cjs index bf90333ca0d..90cb566731c 100644 --- a/test/helpers/onboard-script-mocks.cjs +++ b/test/helpers/onboard-script-mocks.cjs @@ -1230,10 +1230,14 @@ function mockDockerSandboxLifecycleReleaseFromRunner() { normalized.includes("label=openshell.ai/sandbox-name=my-assistant") && normalized.includes("openshell.ai/sandbox-id") ) { - const row = `${ONBOARD_SANDBOX_NEW_CONTAINER_ID}\topenshell\talpha\t${state.sandboxId || ONBOARD_READY_SANDBOX_ID}\n`; + // Docker-driver containers carry no Podman ownership marker, and the + // identity row ends with a literal terminator column (#11139). + const identity = (containerId) => + `${containerId}\topenshell\talpha\t${state.sandboxId || ONBOARD_READY_SANDBOX_ID}\t\tend\n`; + const row = identity(ONBOARD_SANDBOX_NEW_CONTAINER_ID); return state.finalCommitReleased || state.legacyRecoverySandboxId ? row - : `${ONBOARD_SANDBOX_OLD_CONTAINER_ID}\topenshell\talpha\t${state.sandboxId || ONBOARD_READY_SANDBOX_ID}\n${row}`; + : `${identity(ONBOARD_SANDBOX_OLD_CONTAINER_ID)}${row}`; } if ( (state.finalCommitReleased || state.legacyRecoverySandboxId) &&