Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix(dev): report teardown failure when containers survive the sweep
Raul735024f3e739
diff --git a/packages/dev/src/services/compose.test.ts b/packages/dev/src/services/compose.test.tsindex 7ed8b2c8e0..46d1d09dd2 100644--- a/packages/dev/src/services/compose.test.ts+++ b/packages/dev/src/services/compose.test.ts@@ -1,14 +1,10 @@import { describe, expect, it } from "vitest";-import { buildDownArgs, buildUpArgs } from "./compose.js";+import { buildDownArgs, buildUpArgs, teardownExitCode } from "./compose.js";-// The teardown itself talks to the Docker daemon and isn't unit-testable. What-// IS testable — and what a mistake here actually costs — is the profile set on-// the `down` argv. Compose treats a profile-gated service as "defined but not-// enabled" rather than an orphan, so a `down` missing a profile exits 0 having-// silently left those containers running. They accumulate per worktree until a-// Docker restart recreates the project network, at which point the stale-// containers hold a dead network id and the next `crbn up` fails with-// "network <id> not found".+// The teardown itself talks to Docker and isn't unit-testable. The argv is.+// A `down` missing a profile exits 0 having left those containers running;+// they accumulate until a Docker restart turns them into a stale-network boot+// failure ("network <id> not found").const ROOT = "/tmp/carbon-worktree";const SLUG = "carbon-test";@@ -23,7 +19,6 @@ function profilesIn(args: string[]): string[] {describe("buildDownArgs", () => {it("enables every profile that buildUpArgs can enable", () => {- // Union of the profiles reachable from any `crbn up` invocation.const bootable = new Set([...profilesIn(buildUpArgs(ROOT, SLUG)),...profilesIn(buildUpArgs(ROOT, SLUG, { chrome: true })),@@ -40,16 +35,15 @@ describe("buildDownArgs", () => {});it("covers the profile-gated services by name", () => {- // Guards against the union test passing vacuously if buildUpArgs ever- // stops pushing profiles at all. `full` = studio/meta/inbucket,- // `chrome` = the opt-in thumbnail Chromium (`crbn up --thumbnails`).+ // Stops the union test passing vacuously. full = studio/meta/inbucket,+ // chrome = the opt-in thumbnail Chromium.expect(profilesIn(buildDownArgs(ROOT, SLUG, false))).toEqual(expect.arrayContaining(["full", "chrome"]));});it("preserves volumes unless explicitly asked to remove them", () => {- // A stray -v here silently destroys a developer's local database.+ // A stray -v destroys a developer's local database.expect(buildDownArgs(ROOT, SLUG, false)).not.toContain("-v");expect(buildDownArgs(ROOT, SLUG, true)).toContain("-v");});@@ -59,8 +53,31 @@ describe("buildDownArgs", () => {});});+describe("teardownExitCode", () => {+ it("reports success once nothing remains, even if compose failed", () => {+ // The sweep cleaned up — warning that containers may still be running+ // would be false.+ expect(teardownExitCode(1, 0)).toBe(0);+ expect(teardownExitCode(0, 0)).toBe(0);+ });++ it("reports failure when containers survive a compose success", () => {+ // destroyProject ignores docker errors, so a failed sweep would otherwise+ // be indistinguishable from a clean teardown.+ expect(teardownExitCode(0, 2)).toBe(1);+ });++ it("preserves compose's code when it failed and containers remain", () => {+ expect(teardownExitCode(137, 1)).toBe(137);+ });++ it("never returns 0 with containers remaining on an unknown exit", () => {+ expect(teardownExitCode(undefined, 1)).toBe(1);+ });+});+describe("buildUpArgs", () => {- // Locking in the pre-existing boot behavior, which the extraction preserved.+ // Locks in the pre-existing boot behavior the extraction preserved.it("enables the full profile by default", () => {expect(profilesIn(buildUpArgs(ROOT, SLUG))).toEqual(["full"]);});@@ -77,8 +94,8 @@ describe("buildUpArgs", () => {});it("activates no profiles when specific services are named", () => {- // Compose starts named services plus dependencies regardless of profiles;- // activating them here would pull in unrelated containers.+ // Compose starts named services + deps regardless; enabling profiles here+ // would pull in unrelated containers.const args = buildUpArgs(ROOT, SLUG, {services: ["postgres"],chrome: truediff --git a/packages/dev/src/services/compose.ts b/packages/dev/src/services/compose.tsindex 2ff6b1dd21..3052dd5833 100644--- a/packages/dev/src/services/compose.ts+++ b/packages/dev/src/services/compose.ts@@ -53,15 +53,12 @@ export type Container = {// Lifecycle// ----------------------------------------------------------------------------// Every profile any `bootStack` call can enable. Teardown must activate all of-// them: compose treats a profile-gated service as "defined but not enabled"-// rather than an orphan, so a `down` without the profile skips those-// containers entirely — `--remove-orphans` does not catch them either.-// Keep this in sync when adding a profile to the compose file; the residual-// sweep in `stopStack` is the backstop if it ever falls behind.+// Every profile `bootStack` can enable. Compose treats profile-gated services+// as "not enabled" rather than orphans, so a `down` missing one silently+// leaves those containers running. `stopStack`'s sweep backstops drift here.const COMPOSE_PROFILES = ["full", "chrome"] as const;-// Exported for tests: the exact `docker compose … up -d` argv.+// Exported for tests: the `docker compose … up -d` argv.export function buildUpArgs(root: string,slug: string,@@ -78,10 +75,8 @@ export function buildUpArgs(return args;}-// Exported for tests: the exact `docker compose … down` argv. Enables every-// profile unconditionally — a profile that was never booted simply matches no-// running containers, so over-enabling on teardown is free, while-// under-enabling silently leaks containers.+// Exported for tests. Enables every profile unconditionally — one that was+// never booted matches nothing, while a missing one leaks containers.export function buildDownArgs(root: string,slug: string,@@ -210,12 +205,8 @@ export async function allImagesPresentLocally(// failures (missing .env.local, profile issues, renamed compose file), falls// back to raw `docker rm -f` via destroyProject so the stack is always torn// down even when compose can't parse the project to find its containers.-//-// The fallback is keyed on containers actually remaining, NOT on the exit code-// alone: compose exits 0 after a teardown that skipped services it had no-// profile for, so a partial teardown looks like a success and would otherwise-// leave those containers running until the next Docker restart turned them-// into a stale-network boot failure.+// Keyed on containers actually remaining, not the exit code alone: compose+// exits 0 after skipping services it had no profile for.export async function stopStack(root: string,slug: string,@@ -226,15 +217,25 @@ export async function stopStack(reject: false});const project = projectName(slug);- const leftovers = await projectContainerIds(project);- if (r.exitCode !== 0 || leftovers.length > 0) {+ let remaining = await projectContainerIds(project);+ if (r.exitCode !== 0 || remaining.length > 0) {await destroyProject(project, withVolumes);+ // destroyProject swallows docker errors — re-read rather than assume.+ remaining = await projectContainerIds(project);}- return r.exitCode ?? 0;+ return teardownExitCode(r.exitCode, remaining.length);}-// Container ids belonging to a compose project, found by label so it works-// even when the compose file or worktree directory is gone.+// Describes the end state, not the compose invocation: `down` uses this only+// to warn that containers may still be running.+export function teardownExitCode(+ composeExit: number | undefined,+ remaining: number+): number {+ return remaining > 0 ? composeExit || 1 : 0;+}++// Container ids for a compose project, by label — works with no compose file.async function projectContainerIds(project: string): Promise<string[]> {const r = await execa("docker",