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.
Company Private Buckets — implementation plan
Spec / source: .ai/specs/2026-09-17-company-private-buckets.md
Branch: feat/supabase-bucket-migration (worktree carbon-feat-supabase-bucket-migration)
Progress
- Task 1: Storage contract in
@carbon/utils(storage.ts+ tests) — 14 tests pass - Task 2: Bucket-provisioning migration (trigger + backfill) — file written;
pnpm db:migrateBLOCKED locally (worktree has no provisioned stack;storage.bucketsmissing in postgres-only stack) — apply from the main checkout - Task 3: Preview routes,
getPrivateUrl, path helpers (ERP + MES) — gate amended to allow sharedpublic/temp-stagingbuckets (behavior preservation; spec AC#4 adjusted) - Task 4: ERP module services + MCP signed-upload wrappers — 34 sites across 10 files
- Task 5: ERP UI components sweep — 75 sites across 48 files
- Task 6: ERP routes sweep (x+, api+, file+, share+) — 20 files incl. share-route TODO resolved
- Task 7: MES app sweep — mes typecheck green
- Task 8: packages/jobs + ee + lib sweep — added
@carbon/utilsdep to@carbon/lib; jobs/ee/lib typecheck green - Task 9: Edge function
import-csv— inline fallback; deno not installed locally, CI checks - Task 10: Copy script
scripts/migrate-private-buckets.ts— standalone tsc green,--dry-runsupported - Task 11: Final verification sweep — all six typechecks green, 245 utils tests pass (14 new), lint 35/35, leftover sweep clean (only helper internals + import-csv inline fallback + copy-script source remain; scripts/model-upload.ts also migrated).
.claude/rulesmentions of the private bucket deliberately NOT updated yet — they document committed code only; sync post-commit.
Dependencies
Task 1 first (everything imports it). Task 2 independent. Tasks 3–9 depend on 1 and are mutually independent (parallel subagents OK, but 4/5/6 touch adjacent ERP files — run 4 before 5/6 to avoid conflicts in service files imported by components). Task 10 independent of 3–9. Task 11 last.
Shared conventions for every sweep task (3–9)
- Import from
@carbon/utils:getCompanyPrivateBucket,LEGACY_PRIVATE_BUCKET,downloadCompanyPrivateObject,createCompanyPrivateSignedUrl,listCompanyPrivateObjects,removeCompanyPrivateObjects. - Writes (
upload,createSignedUploadUrl,copy,move):client.storage.from(getCompanyPrivateBucket(companyId))— no fallback. Object paths unchanged. - Reads (
download,createSignedUrl): use the fallback helpers, passingclient.storage. - Lists:
listCompanyPrivateObjects(unions company + legacy, de-dup by name, company wins). - Removes:
removeCompanyPrivateObjects(both buckets, ignore missing). - Where no
companyIdis in scope but the object path starts with it, derive:const companyId = objectPath.split("/")[0]with a comment. Prefer real companyId when available. - Never touch:
temp-staging,company-templates,public,avatars,feedbackbuckets;getDatasetAssetUrlbranches; zstd logic in preview routes. - If a call site doesn't fit these patterns, STOP and report — do not improvise.
Task 1: Storage contract in @carbon/utils
Depends on: none Files:
- Create:
packages/utils/src/storage.ts - Create:
packages/utils/src/storage.test.ts - Modify:
packages/utils/src/index.ts— addexport * from "./storage";
Steps:
- Implement in
storage.ts(no supabase-js dependency — structural typing):
Fallback helpers take a minimalexport const LEGACY_PRIVATE_BUCKET = "private"; export const COMPANY_BUCKET_FILE_SIZE_LIMIT = 52428800; export const normalizeStorageSegment = (value: string) => value.trim().replace(/[\\/]+/g, "-").replace(/^[-/]+|[-/]+$/g, ""); export const getCompanyPrivateBucket = (companyId: string) => normalizeStorageSegment(companyId); export const hasCompanyPrivateObjectPathPrefix = (companyId: string, objectPath: string) => objectPath.startsWith(`${getCompanyPrivateBucket(companyId)}/`); export const buildCompanyPrivateStorageTarget = ({ companyId, logicalFolder, entityId, fileName }: {...}) // => { physicalBucket, logicalFolder, objectPath: [bucket, folder, entityId?, fileName].filter(Boolean).join("/") }StorageClientLikeinterface (from(bucket) => { download, createSignedUrl, list, remove }with supabase-shaped{ data, error }returns):downloadCompanyPrivateObject({ storage, companyId, objectPath })— try company bucket; on error/null data, tryLEGACY_PRIVATE_BUCKET; return{ data, physicalBucket, errors: { bucket, error }[] }.createCompanyPrivateSignedUrl({ storage, companyId, objectPath, expiresIn })— same two-step; returns{ signedUrl, physicalBucket, errors }.listCompanyPrivateObjects({ storage, companyId, prefix, options? })— list both buckets (company first), union de-duped byname, company entries win; collect per-bucket errors; returns{ data, errors }.removeCompanyPrivateObjects({ storage, companyId, objectPaths })— remove from company bucket AND legacy bucket; a "not found"/empty result on either is NOT an error; return combined{ errors }only for real failures.
- Tests in
storage.test.ts(vitest, mirrorpackages/utils/src/*.test.tsstyle): normalization edge cases; target path shape with/withoutentityId; download fallback order (company hit → no legacy call; company miss → legacy tried); list de-dup preference; remove tolerates missing on one bucket.
Verify:
pnpm --filter @carbon/utils test && pnpm --filter @carbon/utils typecheck
# Expected: storage.test.ts passes, tsgo exits 0
Out of scope: packages/utils/src/storage-rules.ts (warehouse rules, unrelated).
Task 2: Bucket-provisioning migration
Depends on: none Files:
- Create: via
pnpm db:migrate:new company-bucket-provisioning(timestamp HHMMSS must not be 000000)
Steps:
- Migration SQL:
(Precedent for AFTER INSERT ON company:-- Backfill buckets for companies created after 20250827181005, 50MB cap INSERT INTO storage.buckets (id, name, public, file_size_limit) SELECT id, id, false, 52428800 FROM company ON CONFLICT (id) DO NOTHING; -- Normalize limit on the 2025 backfill rows (created with NULL) UPDATE storage.buckets SET file_size_limit = 52428800 WHERE id IN (SELECT id FROM company) AND file_size_limit IS NULL; CREATE OR REPLACE FUNCTION public.create_company_private_bucket() RETURNS TRIGGER AS $$ BEGIN INSERT INTO storage.buckets (id, name, public, file_size_limit) VALUES (NEW.id, NEW.id, false, 52428800) ON CONFLICT (id) DO NOTHING; RETURN NEW; END; $$ LANGUAGE plpgsql SECURITY DEFINER SET search_path = public, storage; CREATE TRIGGER create_company_private_bucket_trigger AFTER INSERT ON "company" FOR EACH ROW EXECUTE FUNCTION public.create_company_private_bucket();20240908100622_terms.sql:25. SECURITY DEFINER needed — authenticated role can't write storage.buckets.) pnpm db:migrate(applies + regenerates types; storage schema isn't in generated public types, so no TS diff expected).
Verify:
pnpm db:migrate
# Expected: migration applies cleanly; then `pnpm db:check:datasets` passes (company inserts still work)
Out of scope: RLS changes (20250827181005 already covers company buckets); dropping the "Shared Private Bucket" policy.
Task 3: Preview routes, getPrivateUrl, path helpers
Depends on: 1 Files:
- Modify:
apps/erp/app/routes/file+/preview+/$bucket.$.tsx— bucket gate + fallback download - Modify:
apps/mes/app/routes/file+/preview+/$bucket.$.tsx— same - Modify:
apps/erp/app/utils/path.ts—getPrivateUrl(line ~2388) - Modify:
apps/mes/app/utils/path.ts—getPrivateUrl(line ~227)
Steps:
- Preview routes: keep zstd streaming and the existing segment-bounded ownership check EXACTLY as-is. Add after
requirePermissions: allow onlyparams.bucket === getCompanyPrivateBucket(companyId) || params.bucket === LEGACY_PRIVATE_BUCKET, else 403. Replace the direct.download(...)withdownloadCompanyPrivateObjectwhen the requested bucket is company/legacy (single code path — the helper tries both). getPrivateUrl(path, companyId?)in both apps: keep thegetDatasetAssetUrl(path)branch first. When acompanyIdargument is provided emit/file/preview/${getCompanyPrivateBucket(companyId)}/${path}; the no-arg legacy form derives the bucket frompath.split("/")[0](paths start with companyId) and falls back toprivateonly if the first segment is empty. Update call sites ofgetPrivateUrlONLY where the compiler forces it (signature stays backward compatible).
Verify:
pnpm --filter erp typecheck && pnpm --filter mes typecheck
# Expected: exit 0
Out of scope: file+/model+/public.$.tsx (Task 6); dataset-asset resolution.
Task 4: ERP module services + MCP signed-upload wrappers
Depends on: 1 (do before 5/6) Files (Modify, apply shared conventions):
apps/erp/app/modules/documents/documents.service.tsapps/erp/app/modules/inventory/inventory.service.tsapps/erp/app/modules/items/items.service.tsapps/erp/app/modules/production/production.service.tsapps/erp/app/modules/purchasing/purchasing.service.tsapps/erp/app/modules/quality/quality.service.tsapps/erp/app/modules/sales/sales.service.tsapps/erp/app/modules/sales/sales.import.server.tsapps/erp/app/modules/shared/shared.server.tsapps/erp/app/modules/shared/shared.service.tsapps/erp/app/modules/documents/documents.models.ts—buildDocumentUploadPathgains no path change (path keeps companyId prefix) but its consumers'createSignedUploadUrlcalls move to the company bucket
Steps:
- Sweep every
from("private")per shared conventions. Services already receivecompanyIdin almost every signature; where one genuinely lacks it, derive from path first segment. shared.server.ts(~L191,265,342,474 generated-PDF upload/copy + signed URLs): uploads/copies to company bucket; signed URLs viacreateCompanyPrivateSignedUrl.- MCP signed-upload wrappers (added in e0f8265ea4) in documents/items/production/purchasing/sales services: mint on company bucket.
Verify:
pnpm --filter erp typecheck
# Expected: exit 0
Out of scope: UI components (Task 5), routes (Task 6).
Task 5: ERP UI components sweep
Depends on: 1, 4
Files (Modify, apply shared conventions — all have useUser()/company.id available; precedent for the pattern: PR #682's CadModel/Documents.tsx changes, i.e. const bucket = getCompanyPrivateBucket(company.id) once per component):
apps/erp/app/components/:AttachmentsList.tsx,DefaultAttachmentsPanel.tsx,Documents.tsx,Form/PdfExtractor.tsx,ImportCSVModal/UploadCSV.tsx,ItemThumnailUpload.tsx,Layout/Topbar/Suggestion.tsx,SlidesEditor.tsxapps/erp/app/modules/**/ui/:account/.../UserAttributesForm.tsx,accounting/.../FixedAssetNotes.tsx,documents/.../DocumentCreateForm.tsx,documents/.../useDocument.ts,inventory/.../PickingListNotes.tsx,inventory/.../ReceiptLines.tsx,inventory/.../ShipmentNotes.tsx,inventory/.../StockTransferNotes.tsx,items/.../ChangeNoticeActions.tsx,items/.../ChangeNoticeContent.tsx,items/.../BillOfProcess.tsx,items/.../ItemDocuments.tsx,items/.../ItemNotes.tsx,production/.../AssemblyInstructionProperties.tsx,production/.../AssemblyStepSlides.tsx,production/.../InspectionDocumentEditor.tsx,production/.../JobBillOfProcess.tsx,production/.../JobDocuments.tsx,production/.../JobNotes.tsx,production/.../ProcedureExplorer.tsx,purchasing/.../SupplierTaxForm.tsx,purchasing/.../SupplierInteractionDocuments.tsx,purchasing/.../SupplierInteractionLineDocuments.tsx,purchasing/.../SupplierInteractionLineNotes.tsx,purchasing/.../SupplierInteractionNotes.tsx,quality/.../GaugeCalibrationRecordForm.tsx,quality/.../QualityDocumentEditor.tsx,quality/.../IssueContent.tsx,quality/.../IssueTask.tsx,quality/.../IssueWorkflowForm.tsx,quality/.../RiskRegisterForm.tsx,resources/.../MaintenanceDispatchForm.tsx,resources/.../MaintenanceDispatchNotes.tsx,sales/.../CustomerTaxForm.tsx,sales/.../OpportunityDocuments.tsx,sales/.../OpportunityLineDocuments.tsx,sales/.../OpportunityLineNotes.tsx,sales/.../OpportunityNotes.tsx,sales/.../QuoteBillOfProcess.tsx
Steps: apply shared conventions (uploads → company bucket; downloads/signed URLs → fallback helpers; lists → union helper; removes → both-bucket helper).
Verify:
pnpm --filter erp typecheck
# Expected: exit 0
Out of scope: styling/behavior changes; getPrivateUrl internals (done in Task 3).
Task 6: ERP routes sweep
Depends on: 1, 4 Files (Modify, apply shared conventions):
apps/erp/app/routes/api+/sales.digital-quote.$id.tsxapps/erp/app/routes/file+/model+/public.$.tsxapps/erp/app/routes/share+/customer.$id.$.tsx— resolve the// TODOat ~L82: bucket =getCompanyPrivateBucket(customerPortal.companyId); requirehasCompanyPrivateObjectPathPrefix(customerPortal.companyId, decodedPath)else 404; download via fallback helperapps/erp/app/routes/x+/:inspection-document+/$id.delete.tsx,maintenance+/$dispatchId.tsx,procedure+/$id.tsx,purchase-invoice+/new.tsx,purchase-order+/$orderId.finalize.tsx,purchase-order+/$orderId.tsx,purchasing-rfq+/$rfqId.finalize.tsx,quote+/$quoteId.drag.tsx,quote+/$quoteId.finalize.tsx,sales-invoice+/$invoiceId.post.tsx,sales-rfq+/$rfqId.drag.tsx,sales-rfq+/new.tsx,settings+/purchasing.tsx,shipment+/$shipmentId.post.tsx,supplier+/$supplierId.default-attachments.tsx,supplier-quote+/$id.send.tsx,training+/$id.tsx
Steps:
- Sweep per shared conventions. Finalize/send routes (
purchasing-rfq,purchase-order,quote,supplier-quote,sales-invoice): email-attachment signed URLs viacreateCompanyPrivateSignedUrl; log the returnederrorsarray before failing. file+/model+/public.$.tsxdownloads a model by path with no session — derive companyId from the path's first segment for the fallback helper.
Verify:
pnpm --filter erp typecheck
# Expected: exit 0
Out of scope: send-email.ts internals (stays SMTP-only, attachments arrive as URLs — no change needed there unless the compiler disagrees; if it does, STOP and report).
Task 7: MES app sweep
Depends on: 1 Files (Modify, apply shared conventions):
apps/mes/app/services/operations.service.ts(4 lists)apps/mes/app/components/Suggestion.tsx,.../JobOperation/components/Step.tsx,.../JobOperation/components/MaintenanceDispatch.tsx(uploads)
Verify:
pnpm --filter mes typecheck
# Expected: exit 0
Task 8: packages/jobs + ee + lib sweep
Depends on: 1 Files (Modify, apply shared conventions; all run server-side with serviceRole clients):
packages/jobs/src/inngest/functions/:extraction/extract-document.ts(download),scheduled/cleanup.ts(list/info),tasks/assembly-convert.ts(signed upload URLs),tasks/assembly-plan.ts,tasks/model-optimize.ts(list + signed upload URL),tasks/model-thumbnail.ts(upload/remove),tasks/print-job/renderers.tsx(download)packages/ee/src/paperless-parts/lib/lib.ts(3 uploads)packages/lib/src/slack.server.ts:118(createSignedUrl → fallback helper)
Steps: event payloads already carry companyId in most of these; where only a path exists, derive from first segment with a comment.
Verify:
pnpm --filter @carbon/jobs typecheck && pnpm --filter @carbon/ee typecheck && pnpm --filter @carbon/lib typecheck
# Expected: exit 0 for all three
Task 9: Edge function import-csv
Depends on: none (can't import @carbon/utils — Deno)
Files:
- Modify:
packages/database/supabase/functions/import-csv/index.ts(~L971)
Steps: inline two-step download: try client.storage.from(companyId).download(filePath) (companyId is in the validated payload), on error fall back to from("private"). No shared-lib extraction for one call site.
Verify:
cd packages/database/supabase/functions && deno check import-csv/index.ts
# Expected: exit 0 (if deno isn't installed locally, STOP and note it in the run log; CI checks it)
Task 10: Copy script
Depends on: none Files:
- Create:
scripts/migrate-private-buckets.ts - Copy from (precedent):
scripts/model-upload.ts(env/client setup style), PR #682'sscripts/migrate-private-buckets.ts(shape)
Steps:
- For each
company.id(service-role client):createBucket(companyId, { public: false, fileSizeLimit: 52428800 })tolerating "already exists"; recursively listprivateundercompanyId/(paginate, recurse folders);copy(key, key, { destinationBucket: companyId }); count 409/"already exists" as skipped. Print per-company{ copied, skipped, failed }and a final summary. Never delete. - Add a
--dry-runflag that lists what would be copied.
Verify:
pnpm exec tsx scripts/migrate-private-buckets.ts --dry-run
# Expected: runs against local stack, prints per-company summary, exits 0
Out of scope: deleting legacy objects (follow-up per spec).
Task 11: Final verification sweep
Depends on: all Steps:
rg 'from\("private"\)' apps packages scripts --type ts— expected hits ONLY in:packages/utils/src/storage.ts(helper internals),packages/database/supabase/functions/import-csv/index.ts(inline fallback),scripts/migrate-private-buckets.ts(source bucket), contrib examples. Anything else = missed site, go fix.rg 'LEGACY_PRIVATE_BUCKET' packages/utils/src/storage.ts— constant exists.- Typechecks:
pnpm exec turbo run typecheck --filter=@carbon/utils --filter=erp --filter=mes --filter=@carbon/jobs --filter=@carbon/ee --filter=@carbon/lib. - Tests:
pnpm --filter @carbon/utils test. pnpm run lint.- Update
.claude/rules// AGENTS.md if any documented storage claim went stale (checkrg -l 'private.*bucket' .claude/rules packages/*/AGENTS.md -iand fix only real staleness).
Verify: all commands above exit 0; report actual outputs.