From a8825bb72672835908951b1950eeeeb1c7141e4b Mon Sep 17 00:00:00 2001 From: Yanyun Liao Date: Mon, 7 Sep 2026 18:38:17 +0800 Subject: [PATCH 1/8] fix(destroy): recognize Podman driver's sandbox ownership marker Destroy's container-identity check assumed every OpenShell-managed sandbox container carries `openshell.ai/managed-by=openshell`. That holds for the Docker driver, but a live OpenShell 0.0.106 Podman-driver sandbox create shows the container instead carries `openshell.managed =true` and never sets `openshell.ai/managed-by` at all. Every Podman-driver sandbox therefore looked like a foreign container borrowing the mutable sandbox name, and destroy refused to remove it even though it was the operator's own sandbox. classifyDestroyContainerIdentity() and hasOpenShellSandboxOwnership() now recognize either marker. inspectDockerSandboxIdentities() queries a second, driver-specific label alongside the existing three so the classifier can see it. The Docker-driver marker string itself is matched exactly as before (no fuzzy or truthy comparison), so a container that merely reuses the mutable sandbox name without either exact marker still fails closed. Note: this fix is verified against the destroy-time symptom only (container-identity misclassification), reproduced live on a DGX Spark host running OpenShell's Podman driver. It does not cover the separate post-create verification failure reported in the same issue; that failure's root cause needs its own investigation once a host with a newer Podman (the reporter used 5.8.4; our available test host only has 4.9.3) is available to reach it. Refs #11139 Signed-off-by: Yanyun Liao --- .../destroy-container-identity.test.ts | 45 +++++++++++++++++++ src/lib/actions/sandbox/destroy-presence.ts | 18 +++++++- .../adapters/docker/inspect-identity.test.ts | 33 +++++++++++++- src/lib/adapters/docker/inspect.ts | 27 +++++++++-- ...penshell-docker-sandbox-containers.test.ts | 1 + .../openshell-docker-sandbox-containers.ts | 13 +++++- 6 files changed, 128 insertions(+), 9 deletions(-) diff --git a/src/lib/actions/sandbox/destroy-container-identity.test.ts b/src/lib/actions/sandbox/destroy-container-identity.test.ts index 4c1db1a033f..f049d30ab8d 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-presence.ts b/src/lib/actions/sandbox/destroy-presence.ts index cb25a56b178..7054a3ee286 100644 --- a/src/lib/actions/sandbox/destroy-presence.ts +++ b/src/lib/actions/sandbox/destroy-presence.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { + OPENSHELL_PODMAN_MANAGED_VALUE, OPENSHELL_SANDBOX_ID_LABEL, OPENSHELL_SANDBOX_NAME_LABEL, OPENSHELL_SANDBOX_WORKSPACE_LABEL, @@ -33,8 +34,21 @@ export type SandboxNameLabeledContainer = { managedBy: string; workspace: string; sandboxId: string; + managedAlt: string; }; +/** + * A container is OpenShell-owned if it carries either the Docker driver's + * `managedBy` marker or the Podman driver's separate `managedAlt` marker + * (#11139) — the two drivers do not stamp the same ownership label. + */ +function isOpenShellOwnedContainer( + row: SandboxNameLabeledContainer, + ownershipValue: string, +): boolean { + return row.managedBy === ownershipValue || row.managedAlt === OPENSHELL_PODMAN_MANAGED_VALUE; +} + /** Verdict for whether destroy resolved one complete managed container identity. */ export type DestroyContainerIdentityVerdict = | { status: "clear"; identity: SandboxNameLabeledContainer | null } @@ -101,8 +115,8 @@ 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); + const managed = rows.filter((row) => isOpenShellOwnedContainer(row, ownership.value)); + const foreign = rows.filter((row) => !isOpenShellOwnedContainer(row, ownership.value)); if (malformedRows > 0) { return { diff --git a/src/lib/adapters/docker/inspect-identity.test.ts b/src/lib/adapters/docker/inspect-identity.test.ts index 7eff8c7afb8..a217c900bff 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", })); 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", + })); + + 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`, })), ).toEqual({ status: "observed", rows: [], malformedRows: 1 }); }); diff --git a/src/lib/adapters/docker/inspect.ts b/src/lib/adapters/docker/inspect.ts index 437af7ce009..d925a04c3e2 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 = @@ -37,14 +44,20 @@ 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}"}}`, ].join("\t"); const result = inspect( ["ps", "-a", "--no-trunc", "--filter", `label=${sandboxNameLabel}`, "--format", format], @@ -70,15 +83,21 @@ export function inspectDockerSandboxIdentities( if (rawLine.length === 0) continue; const fields = rawLine.split("\t"); if ( - fields.length !== 4 || + fields.length !== 5 || !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, + ]; + 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 66b4a65c46f..53efedd5016 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 190dab3f992..ef6e3708708 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"; @@ -31,7 +38,10 @@ export function hasOpenShellSandboxOwnership( env: NodeJS.ProcessEnv = process.env, ): boolean { const ownership = resolveOpenShellSandboxOwnershipLabel(env); - return labels[ownership.label] === ownership.value; + return ( + labels[ownership.label] === ownership.value || + labels[OPENSHELL_PODMAN_MANAGED_LABEL] === OPENSHELL_PODMAN_MANAGED_VALUE + ); } const DOCKER_SANDBOX_QUERY_TIMEOUT_MS = 30_000; @@ -148,6 +158,7 @@ export function inspectDockerSandboxNameLabeledContainers( managedBy: OPENSHELL_MANAGED_BY_LABEL, workspace: OPENSHELL_SANDBOX_WORKSPACE_LABEL, sandboxId: OPENSHELL_SANDBOX_ID_LABEL, + managedAlt: OPENSHELL_PODMAN_MANAGED_LABEL, }); } From 2fbea96688c76fad094d0e99a7ad653d6bd98bf7 Mon Sep 17 00:00:00 2001 From: Yanyun Liao Date: Wed, 9 Sep 2026 10:24:07 +0800 Subject: [PATCH 2/8] test(destroy): widen identity row fixtures for the Podman marker column The destroy identity probe now requests a fifth label column so a Podman-driver container's ownership marker is visible to the classifier. Fixtures that model `docker ps --format` output still emitted four columns, so every such row parsed as malformed and destroy failed closed in tests that expected a clean verdict. Add the empty trailing column to those fixtures. The parser keeps its exact-width check rather than accepting either width, because that guard is what stops a tab inside a label value from forging an adjacent field. Refs #11139 Signed-off-by: Yanyun Liao --- src/lib/actions/sandbox/destroy-flow.test.ts | 40 +++++++++---------- .../destroy-retained-recovery-flow.test.ts | 18 ++++----- .../sandbox/destroy-timeout-recovery.test.ts | 2 +- test/cli/destroy-gateway-cleanup.test.ts | 2 +- test/helpers/destroy-flow-test-harness.ts | 4 +- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/lib/actions/sandbox/destroy-flow.test.ts b/src/lib/actions/sandbox/destroy-flow.test.ts index d139ea9f436..3ec418230cc 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` }, portableDestroyAuthority: true, }); @@ -843,8 +843,8 @@ describe("destroySandbox flow", () => { 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", + "ffff000000000000\t\tforeign\t\t", ].join("\n"); const harness = createDestroyHarness({ dockerRunResult: { status: 0, stdout: rows }, @@ -873,8 +873,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"; + const foreign = "ffff000000000000\t\tforeign\t\t"; const harness = createDestroyHarness({ dockerRunResultSequence: [ { status: 0, stdout: managed }, @@ -904,12 +904,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" }, ], ["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" }; const harness = createDestroyHarness({ dockerRunResultSequence: [managed, managed, changedIdentity], }); @@ -929,7 +929,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", }; const harness = createDestroyHarness({ sandboxPresent: false, @@ -945,10 +945,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" }; const replacement = { status: 0, - stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta", + stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t", }; const harness = createDestroyHarness({ mcpServers: ["github"], @@ -994,7 +994,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" }, "Container identity changed after managed inference cleanup", ], [ @@ -1005,7 +1005,7 @@ 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" }; const harness = createDestroyHarness({ mcpServers: ["github"], dockerRunResultSequence: [managed, managed, managed, managed, changedIdentity], @@ -1028,10 +1028,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" }; const replacement = { status: 0, - stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta", + stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t", }; const harness = createDestroyHarness({ detachedProviders: ["provider-a"], @@ -1062,7 +1062,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" }; const trace: string[] = []; let identityProbeCalls = 0; const harness = createDestroyHarness({ @@ -1192,7 +1192,7 @@ describe("destroySandbox flow", () => { dockerOrphanIds: [containerId], dockerRunResult: { status: 0, - stdout: `${containerId}\topenshell\tdefault\tsb-alpha`, + stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t`, }, }); @@ -1213,7 +1213,7 @@ describe("destroySandbox flow", () => { dockerOrphanIds: [replacementContainerId], dockerRunResult: { status: 0, - stdout: `${expectedContainerId}\topenshell\tdefault\tsb-alpha`, + stdout: `${expectedContainerId}\topenshell\tdefault\tsb-alpha\t`, }, }); @@ -1235,7 +1235,7 @@ describe("destroySandbox flow", () => { dockerRemoveStatus: 1, dockerRunResult: { status: 0, - stdout: `${containerId}\topenshell\tdefault\tsb-alpha`, + stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t`, }, }; const harness = createDestroyHarness(options); @@ -1264,7 +1264,7 @@ describe("destroySandbox flow", () => { dockerOrphanQueryStatus: 1, dockerRunResult: { status: 0, - stdout: `${containerId}\topenshell\tdefault\tsb-alpha`, + stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t`, }, }); @@ -1286,7 +1286,7 @@ describe("destroySandbox flow", () => { dockerOrphanIds: [containerId], dockerRunResult: { status: 0, - stdout: `${containerId}\topenshell\tdefault\tsb-alpha`, + stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t`, }, }); 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 8fb160696ae..7290a384af3 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`) .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`) .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`, }, 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`, }, registryEntryOverrides: { lifecycleGeneration: recovery.lifecycleGeneration!, @@ -403,7 +403,7 @@ describe("destroySandbox retained recovery flow", () => { sandboxPresent: false, dockerRunResult: { status: 0, - stdout: `${containerId}\topenshell\tdefault\tsb-alpha`, + stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t`, }, registryEntryOverrides: { lifecycleGeneration: recovery.lifecycleGeneration!, @@ -449,7 +449,7 @@ describe("destroySandbox retained recovery flow", () => { dockerOrphanIds: [bootstrapContainerId], dockerRunResult: { status: 0, - stdout: `${bootstrapContainerId}\topenshell\tdefault\tsb-alpha`, + stdout: `${bootstrapContainerId}\topenshell\tdefault\tsb-alpha\t`, }, registryEntryOverrides: { lifecycleGeneration: recovery.lifecycleGeneration!, @@ -481,7 +481,7 @@ describe("destroySandbox retained recovery flow", () => { dockerOrphanIds: [bootstrapContainerId], dockerRunResult: { status: 0, - stdout: `${bootstrapContainerId}\topenshell\tdefault\tsb-alpha`, + stdout: `${bootstrapContainerId}\topenshell\tdefault\tsb-alpha\t`, }, registryEntryOverrides: { lifecycleGeneration: recovery.lifecycleGeneration!, @@ -559,7 +559,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`) .join("\n"); const harness = createDestroyHarness({ registryEntryPresent: false, @@ -603,7 +603,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`, }, 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..3f71e344ff6 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\n", }, mcpServers: ["github"], registeredSandboxCount: 1, diff --git a/test/cli/destroy-gateway-cleanup.test.ts b/test/cli/destroy-gateway-cleanup.test.ts index ed90393bab1..712f7b26b27 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\n' ;; esac fi ;; diff --git a/test/helpers/destroy-flow-test-harness.ts b/test/helpers/destroy-flow-test-harness.ts index 64e01811403..1e12d8aae88 100644 --- a/test/helpers/destroy-flow-test-harness.ts +++ b/test/helpers/destroy-flow-test-harness.ts @@ -579,12 +579,12 @@ 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" : "", }; 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`).join("\n"), stderr: "", }; const result = From a648f9277d39ebdcec42e2dd14ba30aa9bde7b4d Mon Sep 17 00:00:00 2001 From: Yanyun Liao Date: Wed, 9 Sep 2026 13:52:45 +0800 Subject: [PATCH 3/8] fix(destroy): keep the identity row width stable when a label is empty `run()` trims captured stdout, so a container identity row whose final column is empty loses that field and is rejected as malformed. The Podman ownership marker added for #11139 is empty for every container the Docker driver labels, which made destroy fail closed on the ordinary case. Terminate the row with a literal column so its width no longer depends on which labels a driver stamps, and require that terminator when parsing. The exact-width guard is unchanged: a tab inside a label value still cannot forge an adjacent field, because a forged row loses the terminator. Refs #11139 Signed-off-by: Yanyun Liao --- src/lib/actions/sandbox/destroy-flow.test.ts | 40 +++++++++---------- .../destroy-retained-recovery-flow.test.ts | 18 ++++----- .../sandbox/destroy-timeout-recovery.test.ts | 2 +- .../adapters/docker/inspect-identity.test.ts | 6 +-- src/lib/adapters/docker/inspect.ts | 14 ++++++- test/cli/destroy-gateway-cleanup.test.ts | 2 +- test/helpers/destroy-flow-test-harness.ts | 4 +- 7 files changed, 49 insertions(+), 37 deletions(-) diff --git a/src/lib/actions/sandbox/destroy-flow.test.ts b/src/lib/actions/sandbox/destroy-flow.test.ts index 3ec418230cc..724844a6fff 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\t` }, + dockerRunResult: { status: 0, stdout: `${"f".repeat(64)}\t\tforeign\t\t\tend` }, portableDestroyAuthority: true, }); @@ -843,8 +843,8 @@ describe("destroySandbox flow", () => { it("refuses before destructive work when multiple Docker identities share the name", async () => { const rows = [ - "aaaa000000000000\topenshell\tdefault\tsb-real\t", - "ffff000000000000\t\tforeign\t\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 +873,8 @@ describe("destroySandbox flow", () => { }); it("refuses identity drift after read-only destroy preflight (#8999)", async () => { - const managed = "aaaa000000000000\topenshell\tdefault\tsb-alpha\t"; - const foreign = "ffff000000000000\t\tforeign\t\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 +904,12 @@ describe("destroySandbox flow", () => { it.each([ [ "a replacement identity", - { status: 0, stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t" }, + { 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\t" }; + const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha\t\tend" }; const harness = createDestroyHarness({ dockerRunResultSequence: [managed, managed, changedIdentity], }); @@ -929,7 +929,7 @@ describe("destroySandbox flow", () => { const absent = { status: 0, stdout: "" }; const replacement = { status: 0, - stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t", + stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t\tend", }; const harness = createDestroyHarness({ sandboxPresent: false, @@ -945,10 +945,10 @@ describe("destroySandbox flow", () => { }); it("restores MCP preparation when identity changes before wipe", async () => { - const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha\t" }; + const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha\t\tend" }; const replacement = { status: 0, - stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t", + stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t\tend", }; const harness = createDestroyHarness({ mcpServers: ["github"], @@ -994,7 +994,7 @@ describe("destroySandbox flow", () => { it.each([ [ "a replacement identity", - { status: 0, stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t" }, + { status: 0, stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t\tend" }, "Container identity changed after managed inference cleanup", ], [ @@ -1005,7 +1005,7 @@ 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\t" }; + 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 +1028,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\t" }; + const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha\t\tend" }; const replacement = { status: 0, - stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t", + stdout: "bbbb000000000000\topenshell\tdefault\tsb-beta\t\tend", }; const harness = createDestroyHarness({ detachedProviders: ["provider-a"], @@ -1062,7 +1062,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\t" }; + const managed = { status: 0, stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha\t\tend" }; const trace: string[] = []; let identityProbeCalls = 0; const harness = createDestroyHarness({ @@ -1192,7 +1192,7 @@ describe("destroySandbox flow", () => { dockerOrphanIds: [containerId], dockerRunResult: { status: 0, - stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t`, + stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t\tend`, }, }); @@ -1213,7 +1213,7 @@ describe("destroySandbox flow", () => { dockerOrphanIds: [replacementContainerId], dockerRunResult: { status: 0, - stdout: `${expectedContainerId}\topenshell\tdefault\tsb-alpha\t`, + stdout: `${expectedContainerId}\topenshell\tdefault\tsb-alpha\t\tend`, }, }); @@ -1235,7 +1235,7 @@ describe("destroySandbox flow", () => { dockerRemoveStatus: 1, dockerRunResult: { status: 0, - stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t`, + stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t\tend`, }, }; const harness = createDestroyHarness(options); @@ -1264,7 +1264,7 @@ describe("destroySandbox flow", () => { dockerOrphanQueryStatus: 1, dockerRunResult: { status: 0, - stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t`, + stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t\tend`, }, }); @@ -1286,7 +1286,7 @@ describe("destroySandbox flow", () => { dockerOrphanIds: [containerId], dockerRunResult: { status: 0, - stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t`, + stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t\tend`, }, }); 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 7290a384af3..bc63ecf91c2 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\t`) + .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\t`) + .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\t`, + 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\t`, + stdout: `${containerId}\topenshell\tdefault\tsandbox-alpha\t\tend`, }, registryEntryOverrides: { lifecycleGeneration: recovery.lifecycleGeneration!, @@ -403,7 +403,7 @@ describe("destroySandbox retained recovery flow", () => { sandboxPresent: false, dockerRunResult: { status: 0, - stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t`, + stdout: `${containerId}\topenshell\tdefault\tsb-alpha\t\tend`, }, registryEntryOverrides: { lifecycleGeneration: recovery.lifecycleGeneration!, @@ -449,7 +449,7 @@ describe("destroySandbox retained recovery flow", () => { dockerOrphanIds: [bootstrapContainerId], dockerRunResult: { status: 0, - stdout: `${bootstrapContainerId}\topenshell\tdefault\tsb-alpha\t`, + stdout: `${bootstrapContainerId}\topenshell\tdefault\tsb-alpha\t\tend`, }, registryEntryOverrides: { lifecycleGeneration: recovery.lifecycleGeneration!, @@ -481,7 +481,7 @@ describe("destroySandbox retained recovery flow", () => { dockerOrphanIds: [bootstrapContainerId], dockerRunResult: { status: 0, - stdout: `${bootstrapContainerId}\topenshell\tdefault\tsb-alpha\t`, + stdout: `${bootstrapContainerId}\topenshell\tdefault\tsb-alpha\t\tend`, }, registryEntryOverrides: { lifecycleGeneration: recovery.lifecycleGeneration!, @@ -559,7 +559,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\t`) + .map((id) => `${id}\topenshell\tdefault\tsb-alpha\t\tend`) .join("\n"); const harness = createDestroyHarness({ registryEntryPresent: false, @@ -603,7 +603,7 @@ describe("destroySandbox retained recovery flow", () => { registryEntryPresent: false, dockerRunResult: { status: 0, - stdout: `${"a".repeat(64)}\topenshell\tdefault\tsb-alpha\t`, + 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 3f71e344ff6..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\t\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 a217c900bff..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\topenshell", + stdout: "aaaa000000000000\topenshell\tdefault\tsb-real\topenshell\tend", })); expect( @@ -41,7 +41,7 @@ describe("inspectDockerSandboxIdentities", () => { 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", + stdout: "aaaa000000000000\t\tdefault\tsb-real\ttrue\tend", })); expect( @@ -73,7 +73,7 @@ describe("inspectDockerSandboxIdentities", () => { expect( inspectDockerSandboxIdentities("openshell.ai/sandbox-name=alpha", LABELS, () => ({ status: 0, - stdout: `aaaa000000000000\t${managedBy}\tdefault\tsb-real\topenshell`, + 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 d925a04c3e2..b8d5d8513a1 100644 --- a/src/lib/adapters/docker/inspect.ts +++ b/src/lib/adapters/docker/inspect.ts @@ -26,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; @@ -58,6 +67,7 @@ export function inspectDockerSandboxIdentities( `{{.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], @@ -83,7 +93,8 @@ export function inspectDockerSandboxIdentities( if (rawLine.length === 0) continue; const fields = rawLine.split("\t"); if ( - fields.length !== 5 || + fields.length !== 6 || + fields[5] !== IDENTITY_ROW_TERMINATOR || !DOCKER_CONTAINER_ID_PATTERN.test(fields[0] ?? "") || !fields.every(isExactBoundedIdentityText) ) { @@ -96,6 +107,7 @@ export function inspectDockerSandboxIdentities( string, string, string, + string, ]; rows.push({ id, managedBy, workspace, sandboxId, managedAlt }); } diff --git a/test/cli/destroy-gateway-cleanup.test.ts b/test/cli/destroy-gateway-cleanup.test.ts index 712f7b26b27..74103c977e4 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\t\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 1e12d8aae88..a60e511dffd 100644 --- a/test/helpers/destroy-flow-test-harness.ts +++ b/test/helpers/destroy-flow-test-harness.ts @@ -579,12 +579,12 @@ export function createDestroyHarness(options: DestroyHarnessOptions = {}): Destr options.onDockerRun?.(identityProbeCall); const defaultIdentityResult = { status: 0, - stdout: sandboxPresent ? "aaaaaaaaaaaa\topenshell\tdefault\tsb-alpha\t" : "", + 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\t`).join("\n"), + stdout: dockerNameLabeledIds.map((id) => `${id}\topenshell\tdefault\tsb-alpha\t\tend`).join("\n"), stderr: "", }; const result = From 34f8783fe7670fcc3b95df21185977e27372b762 Mon Sep 17 00:00:00 2001 From: Yanyun Liao Date: Wed, 9 Sep 2026 18:23:13 +0800 Subject: [PATCH 4/8] refactor(destroy): keep one owner for OpenShell container ownership The Podman marker rule was added in two places: the onboarding container module and the destroy identity classifier. A new or changed driver marker would have needed synchronized edits, and the two layers could disagree about which containers OpenShell owns. Export one predicate from the onboarding container module and have destroy classification delegate to it, so destroy keeps only its own identity and ambiguity rules. Also drive a raw Podman-only identity row through the whole destroy flow. The existing tests proved the parser and the classifier separately, so a regression in the caller handoff could pass them while destroy still failed closed for the container this fix targets. The new case also pins that the identity query actually requests the Podman marker, because the mocked engine returns a canned row regardless of the requested format. Refs #11139 Signed-off-by: Yanyun Liao --- src/lib/actions/sandbox/destroy-flow.test.ts | 27 ++++++++++++++++++ src/lib/actions/sandbox/destroy-presence.ts | 20 ++++--------- .../openshell-docker-sandbox-containers.ts | 28 +++++++++++++++++-- 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/src/lib/actions/sandbox/destroy-flow.test.ts b/src/lib/actions/sandbox/destroy-flow.test.ts index 724844a6fff..0aed1ad66ca 100644 --- a/src/lib/actions/sandbox/destroy-flow.test.ts +++ b/src/lib/actions/sandbox/destroy-flow.test.ts @@ -841,6 +841,33 @@ 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 podmanOwned = "aaaa000000000000\t\tdefault\tsb-alpha\ttrue\tend"; + const harness = createDestroyHarness({ + dockerRunResult: { status: 0, stdout: podmanOwned }, + }); + + 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"); + // The mocked engine returns a canned row regardless of the requested + // format, so also pin the caller path: destroy must actually ask for the + // Podman marker, otherwise this row could never appear in production. + const identityArgv = (harness.dockerRunSpy.mock.calls[0]?.[0] ?? []) as string[]; + expect(identityArgv.at(-1)).toContain('{{.Label "openshell.managed"}}'); + }); + it("refuses before destructive work when multiple Docker identities share the name", async () => { const rows = [ "aaaa000000000000\topenshell\tdefault\tsb-real\t\tend", diff --git a/src/lib/actions/sandbox/destroy-presence.ts b/src/lib/actions/sandbox/destroy-presence.ts index a9c6fa8e1f5..198e9169980 100644 --- a/src/lib/actions/sandbox/destroy-presence.ts +++ b/src/lib/actions/sandbox/destroy-presence.ts @@ -2,11 +2,11 @@ // SPDX-License-Identifier: Apache-2.0 import { - OPENSHELL_PODMAN_MANAGED_VALUE, OPENSHELL_SANDBOX_ID_LABEL, OPENSHELL_SANDBOX_NAME_LABEL, OPENSHELL_SANDBOX_WORKSPACE_LABEL, inspectDockerSandboxNameLabeledContainers, + isOpenShellSandboxOwnershipMarker, resolveOpenShellSandboxOwnershipLabel, } from "../../onboard/openshell-docker-sandbox-containers"; import { fingerprintOpenShellSandboxId } from "../../adapters/openshell/sandbox-identity"; @@ -37,18 +37,6 @@ export type SandboxNameLabeledContainer = { managedAlt: string; }; -/** - * A container is OpenShell-owned if it carries either the Docker driver's - * `managedBy` marker or the Podman driver's separate `managedAlt` marker - * (#11139) — the two drivers do not stamp the same ownership label. - */ -function isOpenShellOwnedContainer( - row: SandboxNameLabeledContainer, - ownershipValue: string, -): boolean { - return row.managedBy === ownershipValue || row.managedAlt === OPENSHELL_PODMAN_MANAGED_VALUE; -} - /** Verdict for whether destroy resolved one complete managed container identity. */ export type DestroyContainerIdentityVerdict = | { status: "clear"; identity: SandboxNameLabeledContainer | null } @@ -115,8 +103,10 @@ export function classifyDestroyContainerIdentity( const { malformedRows, rows } = observation; const ownership = resolveOpenShellSandboxOwnershipLabel(); - const managed = rows.filter((row) => isOpenShellOwnedContainer(row, ownership.value)); - const foreign = rows.filter((row) => !isOpenShellOwnedContainer(row, 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/onboard/openshell-docker-sandbox-containers.ts b/src/lib/onboard/openshell-docker-sandbox-containers.ts index ef6e3708708..8118f98c283 100644 --- a/src/lib/onboard/openshell-docker-sandbox-containers.ts +++ b/src/lib/onboard/openshell-docker-sandbox-containers.ts @@ -33,14 +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 || - labels[OPENSHELL_PODMAN_MANAGED_LABEL] === OPENSHELL_PODMAN_MANAGED_VALUE + return isOpenShellSandboxOwnershipMarker( + { + managedBy: labels[ownership.label] ?? "", + managedAlt: labels[OPENSHELL_PODMAN_MANAGED_LABEL] ?? "", + }, + env, ); } From b56172067d3785f0fa2d2aa035a6be2981dd901b Mon Sep 17 00:00:00 2001 From: Yanyun Liao Date: Thu, 10 Sep 2026 11:05:18 +0800 Subject: [PATCH 5/8] test(destroy): prove the Podman handoff through the public boundary The identity fake now answers the query it is actually given: the Podman ownership value is visible only to a query that requests that label. A caller that stops requesting it observes the same container with no ownership marker, which is foreign, so destroy fails closed on its own public behaviour. That removes the mock-call assertion the previous version needed to detect the same regression. Refs #11139 Signed-off-by: Yanyun Liao --- src/lib/actions/sandbox/destroy-flow.test.ts | 27 +++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/lib/actions/sandbox/destroy-flow.test.ts b/src/lib/actions/sandbox/destroy-flow.test.ts index 0aed1ad66ca..1cdbf184307 100644 --- a/src/lib/actions/sandbox/destroy-flow.test.ts +++ b/src/lib/actions/sandbox/destroy-flow.test.ts @@ -847,10 +847,24 @@ describe("destroySandbox flow", () => { // 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 podmanOwned = "aaaa000000000000\t\tdefault\tsb-alpha\ttrue\tend"; - const harness = createDestroyHarness({ - dockerRunResult: { status: 0, stdout: podmanOwned }, - }); + 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(); @@ -861,11 +875,6 @@ describe("destroySandbox flow", () => { 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"); - // The mocked engine returns a canned row regardless of the requested - // format, so also pin the caller path: destroy must actually ask for the - // Podman marker, otherwise this row could never appear in production. - const identityArgv = (harness.dockerRunSpy.mock.calls[0]?.[0] ?? []) as string[]; - expect(identityArgv.at(-1)).toContain('{{.Label "openshell.managed"}}'); }); it("refuses before destructive work when multiple Docker identities share the name", async () => { From 9d41a90ada57925e683cb133c039e46c3ea8d68a Mon Sep 17 00:00:00 2001 From: Yanyun Liao Date: Thu, 10 Sep 2026 11:10:36 +0800 Subject: [PATCH 6/8] test(onboard): complete identity rows for the new handoff fixtures A fixture that landed on main builds DockerSandboxIdentityRow values, which now carry the Podman ownership column. Give those Docker-driver rows the empty marker so the suite type-checks against the widened row. Refs #11139 Signed-off-by: Yanyun Liao --- .../onboard/sandbox-create/orchestration-final-handoff.test.ts | 2 ++ 1 file changed, 2 insertions(+) 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: "", }, ], }, From 8394519a1f9c8f1e9d76eb76b6491004b244d04b Mon Sep 17 00:00:00 2001 From: Yanyun Liao Date: Thu, 10 Sep 2026 11:28:56 +0800 Subject: [PATCH 7/8] test(onboard): widen the shared docker identity mock to the terminated row The onboard script mock still emitted a four-column identity row, so every row it produced was malformed under the terminated contract and legacy compatibility recovery could not prove a replacement runtime. Give it the empty Podman marker and the literal terminator, matching what the Docker driver produces. Refs #11139 Signed-off-by: Yanyun Liao --- test/helpers/onboard-script-mocks.cjs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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) && From d7d3f8285364cc178df6643aad2546f60864a5cf Mon Sep 17 00:00:00 2001 From: Yanyun Liao Date: Fri, 11 Sep 2026 14:26:12 +0800 Subject: [PATCH 8/8] style(destroy): apply formatter wrapping after the terminator column The literal row terminator pushed three lines past the formatter's width, so the static-checks hook rewrote them. No behaviour change. Refs #11139 Co-Authored-By: Claude Opus 5 Signed-off-by: Yanyun Liao --- src/lib/actions/sandbox/destroy-flow.test.ts | 5 ++++- src/lib/onboard/openshell-docker-sandbox-containers.ts | 3 +-- test/helpers/destroy-flow-test-harness.ts | 4 +++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib/actions/sandbox/destroy-flow.test.ts b/src/lib/actions/sandbox/destroy-flow.test.ts index 1cdbf184307..3f2d1acd7ea 100644 --- a/src/lib/actions/sandbox/destroy-flow.test.ts +++ b/src/lib/actions/sandbox/destroy-flow.test.ts @@ -1041,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\t\tend" }; + const managed = { + status: 0, + stdout: "aaaa000000000000\topenshell\tdefault\tsb-alpha\t\tend", + }; const harness = createDestroyHarness({ mcpServers: ["github"], dockerRunResultSequence: [managed, managed, managed, managed, changedIdentity], diff --git a/src/lib/onboard/openshell-docker-sandbox-containers.ts b/src/lib/onboard/openshell-docker-sandbox-containers.ts index 43d26909193..b5c19e794e3 100644 --- a/src/lib/onboard/openshell-docker-sandbox-containers.ts +++ b/src/lib/onboard/openshell-docker-sandbox-containers.ts @@ -47,8 +47,7 @@ export function isOpenShellSandboxOwnershipMarker( ): boolean { const ownership = resolveOpenShellSandboxOwnershipLabel(env); return ( - observed.managedBy === ownership.value || - observed.managedAlt === OPENSHELL_PODMAN_MANAGED_VALUE + observed.managedBy === ownership.value || observed.managedAlt === OPENSHELL_PODMAN_MANAGED_VALUE ); } diff --git a/test/helpers/destroy-flow-test-harness.ts b/test/helpers/destroy-flow-test-harness.ts index df6ace2c621..32649591cee 100644 --- a/test/helpers/destroy-flow-test-harness.ts +++ b/test/helpers/destroy-flow-test-harness.ts @@ -584,7 +584,9 @@ export function createDestroyHarness(options: DestroyHarnessOptions = {}): Destr const sequencedResult = options.dockerRunResultSequence?.[identityProbeCall - 1]; const exactCleanupResult = { status: options.dockerOrphanQueryStatus ?? 0, - stdout: dockerNameLabeledIds.map((id) => `${id}\topenshell\tdefault\tsb-alpha\t\tend`).join("\n"), + stdout: dockerNameLabeledIds + .map((id) => `${id}\topenshell\tdefault\tsb-alpha\t\tend`) + .join("\n"), stderr: "", }; const result =