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
refactor: remove the retired shared private bucket from code
aashuf499a2d23a8e
diff --git a/.claude/rules/audit-log-system.md b/.claude/rules/audit-log-system.mdindex ad05b0fbdd..9c5441a2fe 100644--- a/.claude/rules/audit-log-system.md+++ b/.claude/rules/audit-log-system.md@@ -147,8 +147,7 @@ lookup per target table), and writes via `client.rpc("insert_audit_log_batch", {`auditArchiveFunction` in `packages/jobs/src/inngest/functions/scheduled/audit-archive.ts`(Inngest id `audit-log-archive`, cron `0 2 * * *`): per company, fetch rows older than `retentionDays`,gzip to JSONL, upload to the company's own bucket, record an `auditLogArchive` row, then delete the rows.-`getArchiveDownloadUrl` reads the company bucket only. Archives written to the retired shared `private`-bucket were moved there, and their `archivePath` rewritten, by `scripts/one-off/relocate-legacy-audit-archives.ts`.+`getArchiveDownloadUrl` reads the company bucket only.GOTCHA: the runtime path is `{companyId}/audit-logs/{year}/{month}/{YYYY-MM-DD}.jsonl.gz`, which does**not** match `auditConfig.archivePath` (`.../{month}.jsonl.gz`) — the job builds the path inline ratherdiff --git a/.claude/rules/billing-system.md b/.claude/rules/billing-system.mdindex 8e0c1dd654..ea5fb717e9 100644--- a/.claude/rules/billing-system.md+++ b/.claude/rules/billing-system.md@@ -237,9 +237,8 @@ whom it deleted.documents then fails, and is logged and skipped. So does the last company in a group,because its group's system accounts refuse deletion and the group would be stranded.- BEFORE the transaction, `removeCompanyFiles` drains and deletes the per-company- bucket (already missing counts as done), legacy files under `private/<companyId>/` and- legacy audit-log archives under `private/audit-logs/<companyId>/` (listed strictly, so a- listing error is a failure). Never inside it: storage deletes+ bucket (already missing counts as done; listed strictly, so a listing error is a+ failure). Never inside it: storage deletesits object rows on its own connection, `delete_orphaned_documents` then deletes thematching `document` rows, and when the open purge transaction has already deletedthose rows storage waits on it until it times out (HTTP 544) and the purge rollsdiff --git a/.claude/rules/company-backup-restore.md b/.claude/rules/company-backup-restore.mdindex 0f27251b2e..45607dff1f 100644--- a/.claude/rules/company-backup-restore.md+++ b/.claude/rules/company-backup-restore.md@@ -207,8 +207,6 @@ Buckets (important, two different things):under a `{companyId}/` prefix, one folder per backup (`exports/<name>/` — its`manifest.json`, `tables/`, `assets/{companyId}/…`), and pre-restore snapshots underthe same prefix — see `client.storage.from(companyId)`.-- `LEGACY_SHARED_BUCKET = "private"` — the retired shared bucket. Export, backup and- restore never read it; only the company purge still deletes what a company left there.- `TEMPLATE_BUCKET = "company-templates"`, `TEMPLATE_ASSET_PREFIX = "_templates"`.## Export — `company-export.ts`diff --git a/apps/erp/app/modules/documents/documents.paths.ts b/apps/erp/app/modules/documents/documents.paths.tsindex 13d6b59d1b..286cfa0d9e 100644--- a/apps/erp/app/modules/documents/documents.paths.ts+++ b/apps/erp/app/modules/documents/documents.paths.ts@@ -6,7 +6,7 @@ import { stripSpecialCharacters } from "@carbon/utils";/*** Build the canonical storage path for an uploaded document:- * `${companyId}/${folder}/${entityId}/${sanitizedName}` in the `"private"` bucket.+ * `${companyId}/${folder}/${entityId}/${sanitizedName}` in the company's own bucket.* Single source of truth for the path convention every `use*Documents` hook* inlines client-side; shared so the server-side (MCP) upload methods produce* paths the entity document panels list from.diff --git a/apps/erp/app/modules/documents/documents.server.ts b/apps/erp/app/modules/documents/documents.server.tsindex 2deb5584dc..3178e29ce4 100644--- a/apps/erp/app/modules/documents/documents.server.ts+++ b/apps/erp/app/modules/documents/documents.server.ts@@ -488,7 +488,7 @@ export const updateDocument = defineServiceFn({});/**- * Create a presigned upload URL for a document in the "private" bucket+ * Create a presigned upload URL for a document in the company's bucket** @mcp*/diff --git a/apps/erp/app/modules/documents/documents.service.ts b/apps/erp/app/modules/documents/documents.service.tsindex 5cfbd5fd5f..ceced41bbe 100644--- a/apps/erp/app/modules/documents/documents.service.ts+++ b/apps/erp/app/modules/documents/documents.service.ts@@ -171,7 +171,7 @@ export async function upsertDocument(}/**- * Create a presigned upload URL for a document in the "private" bucket. First step+ * Create a presigned upload URL for a document in the company's bucket. First step* of the two-step upload flow: PUT the file bytes to the returned `signedUrl` (or* use supabase-js `uploadToSignedUrl(path, token, file)`), then call* `insertUploadedDocument` with the returned `path` to register the metadata row.diff --git a/apps/erp/app/routes/download.$token.tsx b/apps/erp/app/routes/download.$token.tsxindex 97368036a7..712f6b7149 100644--- a/apps/erp/app/routes/download.$token.tsx+++ b/apps/erp/app/routes/download.$token.tsx@@ -51,8 +51,8 @@ export async function loader({ params }: LoaderFunctionArgs) {if (doc.error || !doc.data?.path) return fail("unavailable");// Access already proven above; fetch the bytes with the service role.- // Documents live in the company's private bucket (legacy `private`- // fallback), keyed directly by `document.path` (mirrors useDocument.ts).+ // Documents live in the company's own bucket, keyed directly by+ // `document.path` (mirrors useDocument.ts).const file = await storage(getCarbonServiceRole()).company(companyId).download(doc.data.path);diff --git a/contrib/building/examples/upload-3d-model/app/lib/carbon.server.ts b/contrib/building/examples/upload-3d-model/app/lib/carbon.server.tsindex 59baba4c27..62c7570b92 100644--- a/contrib/building/examples/upload-3d-model/app/lib/carbon.server.ts+++ b/contrib/building/examples/upload-3d-model/app/lib/carbon.server.ts@@ -38,7 +38,7 @@ class CarbonClient {const fileName = `${this.companyId}/models/${modelId}.${fileExtension}`;const [fileUpload, recordInsert] = await Promise.all([- this.client.storage.from("private").upload(fileName, file),+ this.client.storage.from(this.companyId).upload(fileName, file),this.client.from("modelUpload").insert({id: modelId,modelPath: fileName,@@ -78,7 +78,7 @@ class CarbonClient {const thumbnailPath = `${this.companyId}/thumbnails/${thumbnailId}.png`;const thumbnailUpload = await this.client.storage- .from("private")+ .from(this.companyId).upload(thumbnailPath, file, {upsert: true,contentType: "image/png"diff --git a/packages/database/supabase/migrations/20261020051356_drop-shared-private-bucket-policy.sql b/packages/database/supabase/migrations/20261020051356_drop-shared-private-bucket-policy.sqlnew file mode 100644index 0000000000..6c5661c263--- /dev/null+++ b/packages/database/supabase/migrations/20261020051356_drop-shared-private-bucket-policy.sql@@ -0,0 +1,10 @@+-- The legacy shared `private` bucket is retired: every company's files live in+-- its own bucket, and nothing reads or writes `private` any more. Drop its+-- storage policy so the bucket itself can be emptied and deleted.+--+-- The bucket is not deleted here: Storage owns its objects, so it is emptied+-- and removed from the dashboard (or the Storage API) after this ships.+--+-- `company_id_not_reserved_bucket_id` still reserves the id `private`, so no+-- company can ever be created with that id and claim a bucket of that name.+DROP POLICY IF EXISTS "Shared Private Bucket" ON storage.objects;diff --git a/packages/files/AGENTS.md b/packages/files/AGENTS.mdindex ba73150ed1..30da847945 100644--- a/packages/files/AGENTS.md+++ b/packages/files/AGENTS.md@@ -6,7 +6,7 @@ in the codebase, shared by the browser and Node.| Subpath | Provides ||---------|----------|-| `.` (root) | Cross-type helpers: `MEDIA_CONTENT_TYPES` + `getContentType` (the one extension→MIME map every file-serving route uses), `getFileExtension` / `effectiveExtension` (`.zst` unwrap), `getDocumentType` / `documentTypes` / `isPreviewableDocumentType` (file classification for the Documents UI), `convertKbToString`, `downloadUrl(url, filename)` (fetches first and throws on a failed response, so an expired session never saves an error page under the file's name), and `downloadBlob` / `downloadText` — THE browser "save this as a file" sequence (object URL → anchor click → revoke); never hand-roll it. **`storage(client)`** — the Carbon storage client, supabase-shaped: `.from(bucket)` is plain supabase for `public` / `temp-staging`; `.company(companyId)` is that company's private bucket (bucket id = companyId, THROWS on an empty id). Every read and write goes to the company bucket only. The retired shared `private` bucket is not read: the one-off copies (`scripts/one-off/migrate-private-buckets.ts`, `recopy-private-buckets.ts`, `relocate-legacy-audit-archives.ts`) moved every live company's files out of it, and a read check of production on 2026-10-17 found no live file left there. `list(folder)` takes no options: it reads the cursor-paged `listV2` endpoint to the end and returns every entry directly inside the folder, sorted by name, in the old `list` shape (`name` is the entry's own name; a sub-folder has a null `id`). Every key must sit under `${companyId}/` and pass `isUnsafeStoragePath`; anything else gets `{ data: null, error }` without touching storage. **`isUnsafeStoragePath(path)`** is THE storage-key traversal guard (dot segments, `\`, `?`, `#`, control characters, checked as given and after every decode so `%2e%2e` / `%252e%252e` are caught; malformed escapes refused) — every route that takes a key from a URL or form calls it rather than a local copy; **`safeStorageFileName(name)`** turns a caller-supplied file name into a safe last key segment (basename, no `%`) or null. This is the contract for document keys (`${companyId}/parts/…`, `/job/…`, `/tmp/…`, everything `getPrivateUrl` serves); the backup and audit-archive layouts (`exports/…`, `audit/…`) share the bucket under their own keys and keep using plain `client.storage.from(companyId)`. `getCompanyPrivateBucket` / `hasCompanyPrivateObjectPathPrefix` are exported for the few places that need the bucket *name* (MediaUploader staging, route authorization, the assembler's bucket resolver). `PRIVATE_PREVIEW_ALIAS` (`"private"`) is a stored URL segment, not a bucket: `/file/preview/private/<path>` links in rich text and inspection documents are served from the caller's own company bucket |+| `.` (root) | Cross-type helpers: `MEDIA_CONTENT_TYPES` + `getContentType` (the one extension→MIME map every file-serving route uses), `getFileExtension` / `effectiveExtension` (`.zst` unwrap), `getDocumentType` / `documentTypes` / `isPreviewableDocumentType` (file classification for the Documents UI), `convertKbToString`, `downloadUrl(url, filename)` (fetches first and throws on a failed response, so an expired session never saves an error page under the file's name), and `downloadBlob` / `downloadText` — THE browser "save this as a file" sequence (object URL → anchor click → revoke); never hand-roll it. **`storage(client)`** — the Carbon storage client, supabase-shaped: `.from(bucket)` is plain supabase for `public` / `temp-staging`; `.company(companyId)` is that company's private bucket (bucket id = companyId, THROWS on an empty id). Every read and write goes to the company bucket only; there is no shared `private` bucket. `list(folder)` takes no options: it reads the cursor-paged `listV2` endpoint to the end and returns every entry directly inside the folder, sorted by name, in the old `list` shape (`name` is the entry's own name; a sub-folder has a null `id`). Every key must sit under `${companyId}/` and pass `isUnsafeStoragePath`; anything else gets `{ data: null, error }` without touching storage. **`isUnsafeStoragePath(path)`** is THE storage-key traversal guard (dot segments, `\`, `?`, `#`, control characters, checked as given and after every decode so `%2e%2e` / `%252e%252e` are caught; malformed escapes refused) — every route that takes a key from a URL or form calls it rather than a local copy; **`safeStorageFileName(name)`** turns a caller-supplied file name into a safe last key segment (basename, no `%`) or null. This is the contract for document keys (`${companyId}/parts/…`, `/job/…`, `/tmp/…`, everything `getPrivateUrl` serves); the backup and audit-archive layouts (`exports/…`, `audit/…`) share the bucket under their own keys and keep using plain `client.storage.from(companyId)`. `getCompanyPrivateBucket` / `hasCompanyPrivateObjectPathPrefix` are exported for the few places that need the bucket *name* (MediaUploader staging, route authorization, the assembler's bucket resolver). `PRIVATE_PREVIEW_ALIAS` (`"private"`) is a stored URL segment, not a bucket: `/file/preview/private/<path>` links in rich text and inspection documents are served from the caller's own company bucket || `./csv` | `encodeCsv` (object rows; `header: false` encodes a later page of a streamed file — pages join with `\r\n`, see the Sync Activity export) / `encodeCsvTable` (header + cells) — the ONE CSV encoder, injection-safe by construction (`stripCsvFormulaPrefix` on every string cell; `formulaSafe: false` turns that off for CSV a program reads and no spreadsheet opens, which today is only the MCP server's tool results); `parseCsv` (text) / `parseCsvFile` (browser File, streamed) / `parseCsvLines` (text → records with the line each starts on, blank and delimiter-only records left out; for messages that name a line); `downloadCsv`; `CSV_CONTENT_TYPE`. papaparse underneath, in every runtime. The Table export button, report exports, import templates, import-error re-exports and the sales CSV import all go through here — before this there were two libraries (`json-2-csv` + papaparse) and two hand-rolled encoders, and only ONE of the five export sites protected against formula injection || `./media` | Images. **`MediaUploader`** — the configured client (constructor takes the storage client + HEIC staging location once, files-sdk style): `prepareForUpload(files)` (HEIC → JPEG sequentially + duplicate-name refusal, throws `DuplicateFileNameError`), `convertHeic(file)`, `prepareImage(file, shape)`. Underneath: `processImage` (the pipeline), `prepareImageUpload` (pipeline → native-decode fallback → imgproxy fallback), `convertHeicToJpeg` / `convertHeicFiles` / `findDuplicateFileName`, `transformImageViaStorage` (imgproxy round-trip), `isHeic`, `IMAGE_UPLOAD_MIME_TYPES`, plus the storage-path helpers `getPrivateUrl` / `getRawModelUrl` / `parseJobFilePath` || `./media/node` | `initNodeImageCodecs()` — Node-only wasm pre-instantiation; call once before the pipeline in Node (jobs, paperless, vitest). The wasm comes from the BUNDLER (`?inline` data URIs), never off disk — so this module is consumable only from a Vite pipeline, and that pipeline needs `assetsInclude: ["**/*.wasm"]` (already set in `apps/erp/vite.config.ts` and `@carbon/config/vitest`). `renderLabelLogo(bytes, ext, { widthDots, threshold?, crop? })` — a logo as a mono PNG data URL + ZPL `^GFA` field (inits the codecs itself; `@carbon/documents/labels` calls it). Never import from client code |diff --git a/packages/files/src/storage.ts b/packages/files/src/storage.tsindex f9ef4d2a69..9369c50525 100644--- a/packages/files/src/storage.ts+++ b/packages/files/src/storage.ts@@ -26,8 +26,8 @@ import {/*** The URL segment `/file/preview/private/<path>` — a stored link format* (rich text, inspection documents) that the preview routes serve from the- * caller's own company bucket. The shared `private` bucket it once named is- * no longer read.+ * caller's own company bucket. The shared `private` bucket it once named no+ * longer exists.*/export const PRIVATE_PREVIEW_ALIAS = "private";// Ephemeral staging for uploads too big for a company bucket's per-object capdiff --git a/packages/jobs/manifests/schema.json b/packages/jobs/manifests/schema.jsonindex de7c108b31..5fd82b1077 100644--- a/packages/jobs/manifests/schema.json+++ b/packages/jobs/manifests/schema.json@@ -1,11 +1,11 @@{"kind": "carbon-company-backup","version": 1,- "schemaVersion": "20261019185808",+ "schemaVersion": "20261020051356","sourceCompanyId": "schema-baseline","sourceCompanyGroupId": null,"sourceCompanyName": null,- "exportedAt": "2026-10-19T17:59:10.246Z",+ "exportedAt": "2026-10-20T05:29:25.062Z","exportedBy": "schema-baseline","label": "schema baseline","includeStorage": "none",@@ -479,10 +479,10 @@"accountingCutoverDate","accountingActivatedAt","accountingActivatedBy",- "requireBackflushStorageUnit","leaseIncrementalBorrowingRate","leaseShortTermExpedient","leaseReminderDays",+ "requireBackflushStorageUnit","preventNegativeBackflush","receiptQuantitiesStartAtZero"]diff --git a/packages/jobs/src/inngest/functions/scheduled/cleanup.ts b/packages/jobs/src/inngest/functions/scheduled/cleanup.tsindex f5038d102a..f7fd73cd2f 100644--- a/packages/jobs/src/inngest/functions/scheduled/cleanup.ts+++ b/packages/jobs/src/inngest/functions/scheduled/cleanup.ts@@ -406,13 +406,11 @@ export const cleanupFunction = inngest.createFunction(return;}- // Rule 1 — RELOCATED: the compact pipeline copies survivors to `private`- // under the same key (copy, not move — a move races concurrent jobs- // holding a temp-staging source pointer). Once the durable copy exists,- // the staged one is redundant regardless of size or references: every- // reader probes/falls back to `private`.- // A durable copy may live in the company's own bucket (current pipeline)- // or the legacy shared `private` bucket (pre-migration relocations).+ // Rule 1 — RELOCATED: the compact pipeline copies survivors to the+ // company's own bucket under the same key (copy, not move — a move races+ // concurrent jobs holding a temp-staging source pointer). Once the durable+ // copy exists, the staged one is redundant regardless of size or+ // references: every reader looks in the company bucket first.// Object keys start with the companyId segment, and a key without one// can't have a durable copy anywhere.let relocated: Set<string>;@@ -486,10 +484,8 @@ export const cleanupFunction = inngest.createFunction(Date.now() - TMP_STAGING_TTL_HOURS * 60 * 60 * 1000).toISOString();- // Staging now lives in each company's own bucket (bucket id = companyId),- // with the legacy shared `private` bucket still holding pre-migration- // objects — so select the bucket alongside the name and prune per bucket- // rather than assuming one shared bucket.+ // Staging lives in each company's own bucket (bucket id = companyId), so+ // select the bucket alongside the name and prune per bucket.const stale = await listStorageObjects(sql`name LIKE '%/tmp/%' AND created_at < ${cutoff}`);diff --git a/packages/jobs/src/inngest/functions/scheduled/company-cleanup.ts b/packages/jobs/src/inngest/functions/scheduled/company-cleanup.tsindex 00e82f218e..219bacd62c 100644--- a/packages/jobs/src/inngest/functions/scheduled/company-cleanup.ts+++ b/packages/jobs/src/inngest/functions/scheduled/company-cleanup.ts@@ -243,15 +243,12 @@ export async function deleteCompanies(continue;}// Outside the transaction: see `removeCompanyFiles`.- const failures = await removeCompanyFiles(serviceRole, company.id);- if (failures.length > 0) {- for (const failure of failures) {- logger.error("Failed to remove company {failurePart}", {- failurePart: failure.part,- ...company,- error: failure.error- });- }+ const filesError = await removeCompanyFiles(serviceRole, company.id);+ if (filesError) {+ logger.error("Failed to remove company bucket", {+ ...company,+ error: filesError+ });results.push({ id: company.id, deleted: false });continue;}diff --git a/packages/jobs/src/inngest/functions/scheduled/purge-company.test.ts b/packages/jobs/src/inngest/functions/scheduled/purge-company.test.tsindex 430f0f796a..c8d126d939 100644--- a/packages/jobs/src/inngest/functions/scheduled/purge-company.test.ts+++ b/packages/jobs/src/inngest/functions/scheduled/purge-company.test.ts@@ -58,37 +58,26 @@ function fakeStorage(}describe("removeCompanyFiles", () => {- it("drains the company bucket before deleting it, and the legacy folders", async () => {+ it("drains the company bucket before deleting it", async () => {const { serviceRole, buckets } = fakeStorage({co1: ["parts/a.png", "job/j1/b.pdf"],- private: [- "co1/docs/c.pdf",- "co12/docs/keep.pdf",- "audit-logs/co1/2026/01/2026-01-31.jsonl.gz",- "audit-logs/co12/2026/01/keep.jsonl.gz"- ]- });- expect(await removeCompanyFiles(serviceRole, "co1")).toEqual([]);- expect(buckets).toEqual({- private: ["co12/docs/keep.pdf", "audit-logs/co12/2026/01/keep.jsonl.gz"]+ co12: ["docs/keep.pdf"]});+ expect(await removeCompanyFiles(serviceRole, "co1")).toBeNull();+ expect(buckets).toEqual({ co12: ["docs/keep.pdf"] });});it("treats a bucket that is already gone as done", async () => {- const { serviceRole } = fakeStorage({ private: [] });- expect(await removeCompanyFiles(serviceRole, "co1")).toEqual([]);+ const { serviceRole } = fakeStorage({});+ expect(await removeCompanyFiles(serviceRole, "co1")).toBeNull();});- it("reports each part that failed, and leaves the bucket", async () => {+ it("reports a failed drain, and leaves the bucket", async () => {const { serviceRole, calls } = fakeStorage(- { co1: ["a.png"], private: ["co1/b.pdf"] },+ { co1: ["a.png"] },{ failRemove: true });- const failures = await removeCompanyFiles(serviceRole, "co1");- expect(failures.map((f) => f.part)).toEqual([- "company bucket",- "legacy private files"- ]);+ expect(await removeCompanyFiles(serviceRole, "co1")).toBeInstanceOf(Error);expect(calls).not.toContain("deleteBucket co1");});});diff --git a/packages/jobs/src/inngest/functions/scheduled/purge-company.ts b/packages/jobs/src/inngest/functions/scheduled/purge-company.tsindex 0b9a3e65a6..7cfd1f0685 100644--- a/packages/jobs/src/inngest/functions/scheduled/purge-company.ts+++ b/packages/jobs/src/inngest/functions/scheduled/purge-company.ts@@ -8,11 +8,7 @@ import { getCompanyPrivateBucket, isStorageNotFound } from "@carbon/files";import { chunkArray } from "@carbon/utils";import { type Kysely, sql } from "kysely";import { listBucketFilesRecursive } from "../../../backups/storage";-import {- type Catalog,- LEGACY_SHARED_BUCKET,- wipeScopedData-} from "../tasks/company-backup";+import { type Catalog, wipeScopedData } from "../tasks/company-backup";/*** Delete one company and everything it owns, inside the caller's transaction.@@ -158,9 +154,8 @@ export async function dropOrphanCompanyTables(}/**- * A company's files: its own storage bucket, and pre-bucket-migration files- * under `<companyId>/` in the shared private bucket. Returns the failures; each- * part is attempted regardless.+ * A company's files: its own storage bucket, drained and then deleted. Returns+ * the error that stopped it, or null once the bucket is gone.** Run BEFORE the purge transaction, never inside it. Storage deletes its object* rows on its own connection, and `delete_orphaned_documents` then deletes the@@ -175,9 +170,7 @@ export async function dropOrphanCompanyTables(export async function removeCompanyFiles(serviceRole: ReturnType<typeof getCarbonServiceRole>,companyId: string-): Promise<{ part: string; error: unknown }[]> {- const failures: { part: string; error: unknown }[] = [];-+): Promise<unknown> {// Drained by listing, not `emptyBucket`: that only queues the deletes, so the// `deleteBucket` after it is refused as "not empty" for any bucket with files.// A bucket already gone (an earlier attempt removed it, then failed) is done.@@ -186,25 +179,8 @@ export async function removeCompanyFiles(if (!bucketError) {bucketError = (await serviceRole.storage.deleteBucket(bucket)).error;}- if (bucketError && !(await isBucketGone(bucketError))) {- failures.push({ part: "company bucket", error: bucketError });- }-- // The retired shared bucket held a company's files under `<companyId>/` and- // its audit-log archives under `audit-logs/<companyId>/`.- for (const [part, prefix] of [- ["legacy private files", companyId],- ["legacy audit-log archives", `audit-logs/${companyId}`]- ] as const) {- const legacyError = await drainFolder(- serviceRole,- LEGACY_SHARED_BUCKET,- prefix- );- if (legacyError) failures.push({ part, error: legacyError });- }-- return failures;+ if (bucketError && !(await isBucketGone(bucketError))) return bucketError;+ return null;}const isBucketGone = async (error: unknown) =>diff --git a/packages/jobs/src/inngest/functions/tasks/company-backup.ts b/packages/jobs/src/inngest/functions/tasks/company-backup.tsindex 3760af259e..ce079fe06b 100644--- a/packages/jobs/src/inngest/functions/tasks/company-backup.ts+++ b/packages/jobs/src/inngest/functions/tasks/company-backup.ts@@ -46,13 +46,6 @@ export const EXPORTS_PREFIX = "exports";/** Shared, env-agnostic bucket holding the onboarding backup templates. */export const TEMPLATE_BUCKET = "company-templates";-/**- * The retired shared bucket that held every company's files under a- * `{companyId}/` prefix before each company got its own bucket. Nothing reads- * or writes it; a company purge still deletes what that company left there.- */-export const LEGACY_SHARED_BUCKET = "private";-/*** TEXT columns holding a storage path (`{companyId}/…/{id}.ext`). A cross-company* restore changes both the companyId and the embedded ids, so these are rewrittendiff --git a/packages/jobs/src/inngest/functions/tasks/company-import.ts b/packages/jobs/src/inngest/functions/tasks/company-import.tsindex ae1df192e6..e005a4eecc 100644--- a/packages/jobs/src/inngest/functions/tasks/company-import.ts+++ b/packages/jobs/src/inngest/functions/tasks/company-import.ts@@ -440,7 +440,7 @@ export const companyImportFunction = inngest.createFunction(// Storage files travel outside the transaction — failures here leave the// imported rows intact and are surfaced as warnings. Copy them server-side- // from the artifact's `.assets/` folder into the shared `private` bucket,+ // from the artifact's `.assets/` folder into the company's own bucket,// with the path rewritten for the target company (and remapped ids on// reseed) to match the path columns rewritten above.//diff --git a/scripts/one-off/migrate-private-buckets.ts b/scripts/one-off/migrate-private-buckets.tsdeleted file mode 100644index 9c1f6a124e..0000000000--- a/scripts/one-off/migrate-private-buckets.ts+++ /dev/null@@ -1,201 +0,0 @@-// SPDX-License-Identifier: AGPL-3.0-only-// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,-// including ports, remain AGPLv3; serving them over a network requires releasing their source.--import { createRequire } from "node:module";-import { readLocalScriptConfig } from "../lib/local-script-config";--// One-off, idempotent migration: copy the legacy shared `private` bucket's-// objects into each company's own bucket (bucket id = companyId). Object keys-// are identical in both buckets (they start with `${companyId}/`).-// Nothing is ever deleted. Re-running skips objects that already exist.-//-// COVERAGE: only objects under a LIVE company's `${companyId}/` prefix are-// copied. Pre-migration audit archives at `audit-logs/{companyId}/...` are moved-// by `relocate-legacy-audit-archives.ts`; prefixes of deleted companies are not-// read by anything.-//-// Usage:-// pnpm exec tsx scripts/one-off/migrate-private-buckets.ts # migrate-// pnpm exec tsx scripts/one-off/migrate-private-buckets.ts --dry-run # read-only--const LEGACY_BUCKET = "private";-const BUCKET_FILE_SIZE_LIMIT = 52428800; // 50 MB-const LIST_PAGE_SIZE = 1000;--const isDryRun = process.argv.includes("--dry-run");--const { SUPABASE_URL: supabaseUrl, SUPABASE_SERVICE_ROLE_KEY: serviceRoleKey } =- readLocalScriptConfig(- ["SUPABASE_URL", "SUPABASE_SERVICE_ROLE_KEY"],- process.env- );--function createServiceRoleClient(url: string, serviceRole: string) {- // The database workspace declares and pins this dependency; root scripts do not.- const fromDatabase = createRequire(- new URL("../../packages/database/package.json", import.meta.url)- );- const { createClient } = fromDatabase("@supabase/supabase-js");- return createClient(url, serviceRole, {- auth: { autoRefreshToken: false, persistSession: false },- });-}--const client = createServiceRoleClient(supabaseUrl, serviceRoleKey);--type CompanySummary = { copied: number; skipped: number; failed: number };--function isAlreadyExistsError(error: {- message?: string;- statusCode?: string | number;-}) {- const message = error.message ?? "";- return (- /already exists/i.test(message) ||- /duplicate/i.test(message) ||- String(error.statusCode) === "409"- );-}--async function getCompanyIds(): Promise<string[]> {- const ids: string[] = [];- for (let offset = 0; ; offset += LIST_PAGE_SIZE) {- const { data, error } = await client- .from("company")- .select("id")- .order("id")- .range(offset, offset + LIST_PAGE_SIZE - 1);- if (error)- throw new Error(`Failed to read company table: ${error.message}`);- ids.push(...data.map((row: { id: string }) => row.id));- if (data.length < LIST_PAGE_SIZE) break;- }- return ids;-}--// Recursively list every object key under `prefix` in `bucket`.-// Storage list returns folders as entries with id === null; recurse into them.-async function listObjectKeys(- bucket: string,- prefix: string-): Promise<string[]> {- const keys: string[] = [];- for (let offset = 0; ; offset += LIST_PAGE_SIZE) {- const { data, error } = await client.storage- .from(bucket)- .list(prefix, { limit: LIST_PAGE_SIZE, offset });- if (error) {- throw new Error(`Failed to list ${bucket}/${prefix}: ${error.message}`);- }- for (const entry of data as Array<{ name: string; id: string | null }>) {- if (entry.id === null) {- keys.push(...(await listObjectKeys(bucket, `${prefix}/${entry.name}`)));- } else {- keys.push(`${prefix}/${entry.name}`);- }- }- if (data.length < LIST_PAGE_SIZE) break;- }- return keys;-}--// Look before creating: Postgres logs every rejected duplicate insert as an-// error, so creating unconditionally logged one `buckets_pkey` error per-// company on every run.-async function bucketExists(companyId: string): Promise<boolean> {- const { error } = await client.storage.getBucket(companyId);- if (!error) return true;- if (String(error.statusCode) === "404") return false;- throw new Error(`Failed to read bucket ${companyId}: ${error.message}`);-}--async function createBucket(companyId: string) {- const { error } = await client.storage.createBucket(companyId, {- public: false,- fileSizeLimit: BUCKET_FILE_SIZE_LIMIT,- });- if (error && !isAlreadyExistsError(error)) {- throw new Error(`Failed to create bucket ${companyId}: ${error.message}`);- }-}--async function migrateCompany(companyId: string): Promise<CompanySummary> {- const summary: CompanySummary = { copied: 0, skipped: 0, failed: 0 };-- const exists = await bucketExists(companyId);- if (!exists && !isDryRun) {- await createBucket(companyId);- }-- const keys = await listObjectKeys(LEGACY_BUCKET, companyId);- // Keys already in the company bucket are skipped rather than copied again:- // a duplicate copy is another rejected insert, and another logged error.- // A bucket that did not exist yet has nothing in it to list.- const alreadyMigrated = new Set(- exists ? await listObjectKeys(companyId, companyId) : []- );-- for (const key of keys) {- if (alreadyMigrated.has(key)) {- summary.skipped += 1;- continue;- }-- if (isDryRun) {- process.stdout.write(- ` would copy ${LEGACY_BUCKET}/${key} -> ${companyId}/${key}\n`- );- summary.copied += 1;- continue;- }-- const { error } = await client.storage- .from(LEGACY_BUCKET)- .copy(key, key, { destinationBucket: companyId });-- if (!error) {- summary.copied += 1;- } else if (isAlreadyExistsError(error)) {- summary.skipped += 1;- } else {- summary.failed += 1;- process.stderr.write(- ` FAILED ${companyId}: ${key} — ${error.message}\n`- );- }- }-- return summary;-}--(async () => {- if (isDryRun) {- process.stdout.write("Dry run — no buckets created, no objects copied.\n");- }-- const companyIds = await getCompanyIds();- process.stdout.write(`Found ${companyIds.length} companies.\n`);-- const total: CompanySummary = { copied: 0, skipped: 0, failed: 0 };-- for (const companyId of companyIds) {- process.stdout.write(`\nCompany ${companyId}\n`);- const summary = await migrateCompany(companyId);- total.copied += summary.copied;- total.skipped += summary.skipped;- total.failed += summary.failed;- process.stdout.write(` ${JSON.stringify(summary)}\n`);- }-- process.stdout.write(- `\nTotal${isDryRun ? " (dry run)" : ""}: ${JSON.stringify(total)}\n`- );-- if (total.failed > 0) {- process.exitCode = 1;- }-})().catch((err) => {- process.stderr.write(`Migration failed: ${(err as Error).message}\n`);- process.exitCode = 1;-});diff --git a/scripts/one-off/recopy-private-buckets.ts b/scripts/one-off/recopy-private-buckets.tsdeleted file mode 100644index a64a253971..0000000000--- a/scripts/one-off/recopy-private-buckets.ts+++ /dev/null@@ -1,16 +0,0 @@-// SPDX-License-Identifier: AGPL-3.0-only-// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,-// including ports, remain AGPLv3; serving them over a network requires releasing their source.--// Second pass of migrate-private-buckets.ts. The first pass ran while the-// legacy `private` bucket was still writable, so files written there after it-// exist only in `private`. 20261002022403_private-bucket-read-only.sql closes the-// bucket to sessions and API keys in the same deploy, before this runs. The-// service role bypasses that policy, so this pass is final only while no-// service-role code writes to `private`.-//-// Same copy, new ledger name: the script is idempotent and skips objects that-// are already in the company bucket.-//-// Safe to remove once every deployment has run it.-import "./migrate-private-buckets";diff --git a/scripts/one-off/relocate-legacy-audit-archives.ts b/scripts/one-off/relocate-legacy-audit-archives.tsdeleted file mode 100644index f481377996..0000000000--- a/scripts/one-off/relocate-legacy-audit-archives.ts+++ /dev/null@@ -1,168 +0,0 @@-// SPDX-License-Identifier: AGPL-3.0-only-// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,-// including ports, remain AGPLv3; serving them over a network requires releasing their source.--import { createRequire } from "node:module";-import { readLocalScriptConfig } from "../lib/local-script-config";--// One-off, idempotent: move the audit-log archives written before each company-// had its own bucket. They live in the retired shared `private` bucket at-// `audit-logs/{companyId}/…`, and `auditLogArchive.archivePath` still points-// there. Each is copied to `{companyId}/audit-logs/…` in the company's own-// bucket (the layout new archives use), then its row is repointed. Nothing is-// deleted. Re-running skips rows that already point at the company bucket.-//-// The apps stopped reading `private` in the same change that added this-// script, so an archive this script has not moved cannot be downloaded.-// Safe to remove once every deployment has run it. CI runs it once per-// database after the migrations (scripts/one-off/README.md); `--dry-run`-// lists what it would move.-//-// An archive whose file is gone is reported and left as it is: nothing can-// bring it back, and failing on it would fail every later deploy. Any other-// failure exits ONE_OFF_SCRIPT_DEFERRED, so the next deploy retries.--/** ONE_OFF_SCRIPT_DEFERRED in ci/src/one-off-scripts.ts. */-const DEFERRED = 75;-const LEGACY_BUCKET = "private";-const LEGACY_PREFIX = "audit-logs/";-const PAGE_SIZE = 1000;--const isDryRun = process.argv.includes("--dry-run");--const { SUPABASE_URL: supabaseUrl, SUPABASE_SERVICE_ROLE_KEY: serviceRoleKey } =- readLocalScriptConfig(- ["SUPABASE_URL", "SUPABASE_SERVICE_ROLE_KEY"],- process.env- );--function createServiceRoleClient(url: string, serviceRole: string) {- // The database workspace declares and pins this dependency; root scripts do not.- const fromDatabase = createRequire(- new URL("../../packages/database/package.json", import.meta.url)- );- const { createClient } = fromDatabase("@supabase/supabase-js");- return createClient(url, serviceRole, {- auth: { autoRefreshToken: false, persistSession: false }- });-}--const client = createServiceRoleClient(supabaseUrl, serviceRoleKey);--type Archive = { id: string; companyId: string; archivePath: string };--/** `audit-logs/{companyId}/rest` → `{companyId}/audit-logs/rest`, or null. */-function relocatedArchivePath(archive: Archive): string | null {- const [root, companyId, ...rest] = archive.archivePath.split("/");- if (`${root}/` !== LEGACY_PREFIX) return null;- if (companyId !== archive.companyId || rest.length === 0) return null;- return `${archive.companyId}/audit-logs/${rest.join("/")}`;-}--function isAlreadyExistsError(error: {- message?: string;- statusCode?: string | number;-}) {- return (- /already exists|duplicate/i.test(error.message ?? "") ||- String(error.statusCode) === "409"- );-}--function isNotFoundError(error: {- message?: string;- statusCode?: string | number;-}) {- return (- /not found/i.test(error.message ?? "") ||- String(error.statusCode) === "404"- );-}--async function legacyArchives(): Promise<Archive[]> {- const rows: Archive[] = [];- for (let offset = 0; ; offset += PAGE_SIZE) {- const { data, error } = await client- .from("auditLogArchive")- .select("id, companyId, archivePath")- .like("archivePath", `${LEGACY_PREFIX}%`)- .order("id")- .range(offset, offset + PAGE_SIZE - 1);- if (error) {- throw new Error(`Failed to read auditLogArchive: ${error.message}`);- }- rows.push(...(data as Archive[]));- if (data.length < PAGE_SIZE) break;- }- return rows;-}--async function relocate(- archive: Archive-): Promise<"moved" | "missing" | "failed"> {- const target = relocatedArchivePath(archive);- if (!target) {- process.stderr.write(- ` FAILED ${archive.id}: ${archive.archivePath} is not under audit-logs/${archive.companyId}/\n`- );- return "failed";- }- if (isDryRun) {- process.stdout.write(- ` would move ${LEGACY_BUCKET}/${archive.archivePath} -> ${archive.companyId}/${target}\n`- );- return "moved";- }-- const copied = await client.storage- .from(LEGACY_BUCKET)- .copy(archive.archivePath, target, { destinationBucket: archive.companyId });- if (copied.error && isNotFoundError(copied.error)) {- process.stderr.write(- ` MISSING ${archive.id}: ${LEGACY_BUCKET}/${archive.archivePath} is gone; left as it is\n`- );- return "missing";- }- if (copied.error && !isAlreadyExistsError(copied.error)) {- process.stderr.write(- ` FAILED ${archive.id}: copy — ${copied.error.message}\n`- );- return "failed";- }-- // Compare-and-set on the old path, scoped to the company.- const updated = await client- .from("auditLogArchive")- .update({ archivePath: target })- .eq("id", archive.id)- .eq("companyId", archive.companyId)- .eq("archivePath", archive.archivePath);- if (updated.error) {- process.stderr.write(- ` FAILED ${archive.id}: repoint — ${updated.error.message}\n`- );- return "failed";- }- return "moved";-}--(async () => {- if (isDryRun) {- process.stdout.write("Dry run — nothing copied, no row changed.\n");- }- const archives = await legacyArchives();- process.stdout.write(`Found ${archives.length} legacy audit archives.\n`);-- const total = { moved: 0, missing: 0, failed: 0 };- for (const archive of archives) {- total[await relocate(archive)] += 1;- }-- process.stdout.write(- `Total${isDryRun ? " (dry run)" : ""}: ${JSON.stringify(total)}\n`- );- if (total.failed > 0) process.exitCode = DEFERRED;-})().catch((err) => {- process.stderr.write(`Relocation failed: ${(err as Error).message}\n`);- process.exitCode = 1;-});diff --git a/scripts/restore-database.sh b/scripts/restore-database.shindex 8035429e0c..2f0db291f0 100644--- a/scripts/restore-database.sh+++ b/scripts/restore-database.sh@@ -489,7 +489,6 @@ $PSQL_SA -v ON_ERROR_STOP=0 -c "INSERT INTO storage.buckets (id, name, public) VALUES('public', 'public', true),('avatars', 'avatars', true),- ('private', 'private', false),('feedback', 'feedback', true),('company-templates', 'company-templates', false)ON CONFLICT (id) DO NOTHING;@@ -501,9 +500,9 @@ cat "$WORK_DIR/buckets.err" >> "$RESTORE_LOG"# Every company needs its bucket (uploads go there), so a short count is a# broken restore, not a number to eyeball.BUCKET_COUNT=$($PSQL_PG -At -c "SELECT count(*) FROM storage.buckets;" 2>/dev/null || echo 0)-BUCKETS_EXPECTED=$($PSQL_PG -At -c "SELECT 5 + count(*) FROM public.company;" 2>/dev/null || echo 5)+BUCKETS_EXPECTED=$($PSQL_PG -At -c "SELECT 4 + count(*) FROM public.company;" 2>/dev/null || echo 4)if [[ "$BUCKET_COUNT" -ge "$BUCKETS_EXPECTED" ]]; then- echo " ✓ $BUCKET_COUNT storage buckets present (5 fixed + one per company)"+ echo " ✓ $BUCKET_COUNT storage buckets present (4 fixed + one per company)"elseRESTORE_UNVERIFIED=1echo " ✗ $BUCKET_COUNT of $BUCKETS_EXPECTED storage buckets present — the re-seed failed:"