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.
Skip corrupted rows in backups; purge before restore — implementation plan
Spec / source: .ai/specs/2026-09-01-skip-corrupted-rows-in-backups.md
Branch: feat/skip-corrupted-data-in-backups (worktree carbon-feat-skip-corrupted-data-in-backups)
Local DB with the corruption: postgresql://postgres:postgres@localhost:59795/postgres — companies dab44qldq0gg27p2p9e0 (Carbon Development) and JqvzHghaGpe7vTMU1Y27jP (test comp) each carry 1 cross-company pickMethod row + 3 cross-company jobOperationDependency rows. Do not delete them (Task 11 uses a rolled-back transaction); the user tests the purge in the UI.
Never git commit.
Progress
- Task 1: Extract scope SQL into
packages/jobs/src/backups/scope.ts(pure) and add exclusion/purge builders - Task 2: Unit-test the pure builders (compile-only Kysely)
- Task 3:
Manifest.excludedRows, eventskipCorrupted, edge function pass-through - Task 4:
buildCompanyBackup({ skipCorrupted })+ export job marker fields - Task 5: Restore job — classify snapshot failure, store
filePath/includeStorage - Task 6: ERP service types/readers +
exportCompanyBackuparg - Task 7: ERP server
purgeCorruptedRows - Task 8: Route actions
exportSkipCorruptedandpurgeAndRestore - Task 9: UI — banner retry button, "N rows excluded" row detail, restore-row CTA + confirm modal
- Task 10: Docs — rule file + spec status
- Task 11: Verification — typecheck, tests, lingui extract, DB-backed dry run
Dependencies
Task 2 needs 1 · Task 3 independent of 1–2 · Task 4 needs 1, 3 · Task 5 needs 1 · Task 6 needs 3 · Task 7 needs 1 · Task 8 needs 5, 6, 7 · Task 9 needs 6, 8 · Task 10 independent · Task 11 last. Parallelizable: {1, 3} then {2, 4, 5} then {6, 7} then 8 → 9. Task 10 any time.
Task 1: Extract scope SQL into packages/jobs/src/backups/scope.ts and add exclusion/purge builders
Depends on: none Files:
- Create:
packages/jobs/src/backups/scope.ts - Modify:
packages/jobs/src/inngest/functions/tasks/company-backup.ts— removemapWithConcurrency,RETAINED_REF_TABLES,buildScopeFilter,findExportScopeViolations; addexport * from "../../../backups/scope"; - Modify:
packages/jobs/src/backups/schema.ts— appendexport * from "./scope";at the bottom (so@carbon/jobs/backupsexposes it without a new package subpath; see.ai/lessons.md"A new package exports subpath 500s until every running dev server restarts")
Steps:
- Create
scope.ts. Header comment: "Company-scope SQL for backups: scope predicates, the export closure guard, and the opt-in exclusion/purge of rows whose NOT-NULL FK escapes scope. Keep free of runtime@carbon/*imports (type-only OK) — imported by the ERP via@carbon/jobs/backupsand by bare-tsxscripts." Imports:import type { KyselyDatabase } from "@carbon/database/client"; import { type Kysely, type RawBuilder, sql } from "kysely"; import type { TableInfo } from "./schema"; - Move verbatim from
company-backup.ts(delete there):mapWithConcurrency(lines184–201),410–415),RETAINED_REF_TABLES(buildScopeFilter(~463–491). Keep their doc comments. - Add types + error:
(Message text must stay byte-identical to today's — the prior spec and prod logs quote it.)export type ScopeViolation = { table: string; column: string; refTable: string; rows: number }; export class ExportScopeViolationError extends Error { readonly violations: ScopeViolation[]; constructor(companyId: string, violations: ScopeViolation[]) { super(formatScopeViolations(companyId, violations)); this.name = "ExportScopeViolationError"; this.violations = violations; } } export function formatScopeViolations(companyId: string, v: ScopeViolation[]): string { const lines = v.map((x) => `${x.table}.${x.column} → ${x.refTable} (${x.rows} row${x.rows === 1 ? "" : "s"})`); return `Refusing to export ${companyId}: ${v.length} NOT-NULL reference(s) escape company scope, so the backup could never be restored:\n ${lines.join("\n ")}`; } - Add the pure edge enumerator, used by the guard, the exclusion and the purge so all three agree:
Same filters as today's loop inexport type ScopeEdge = { table: TableInfo; column: string; parent: TableInfo }; /** NOT-NULL FKs (refColumn "id") from an exportable table to another exportable, non-retained table. */ export function scopeEdges(table: TableInfo, exportableNames: Set<string>, byName: Map<string, TableInfo>): ScopeEdge[]findExportScopeViolations:fk.refColumn === "id",!RETAINED_REF_TABLES.has(fk.refTable),exportableNames.has(fk.refTable), column exists and!isNullable. - Add
parentExistence(parent, byName, companyId, companyGroupId)— the existingparentScope OR <scope.column> IS NULL(direct) /parentScope(via) expression, extracted. - Add the pure predicate builder:
For each edge: violation term =/** SQL predicate selecting the rows of `table` that must be EXCLUDED: a NOT-NULL FK escaping scope * ("violation"), or pointing at a row already excluded from its parent ("cascade"). null = nothing to exclude. */ export function buildExclusionPredicate( table: TableInfo, exportableNames: Set<string>, byName: Map<string, TableInfo>, companyId: string, companyGroupId: string | null, excludedIds: Map<string, unknown[]> ): { predicate: RawBuilder<unknown>; terms: Array<{ edge: ScopeEdge; cause: "violation" | "cascade"; predicate: RawBuilder<unknown> }> } | nullsql`(${sql.id(col)} IS NOT NULL AND ${sql.id(col)} NOT IN (SELECT id FROM ${sql.id(parent.name)} WHERE ${parentExistence}))`; cascade term (only whenexcludedIds.get(parent.name)?.length) =sql`${sql.id(col)} IN (${sql.join(ids.map(v => sql`${v}`))})`.predicate= terms OR-joined viasql.join(terms, sqlOR). Return null when no terms. - Rewrite
findExportScopeViolationsto returnPromise<ScopeViolation[]>(not strings), built fromscopeEdges+ the violation term (count(*)per edge, concurrency 6, same as today). Preserve the doc comment; add: "Callers format withformatScopeViolationsor throwExportScopeViolationError." - Add the DB-backed exclusion computation:
Iterateexport type ScopeExclusions = { predicates: Map<string, RawBuilder<unknown>>; // table → predicate to AND-NOT into the dump excludedIds: Map<string, unknown[]>; // tables with an `id` column only summary: Array<{ table: string; column: string; refTable: string; rows: number; cause: "violation" | "cascade" }>; }; export async function computeScopeExclusions(db, exportable: TableInfo[], byName, companyId, companyGroupId): Promise<ScopeExclusions>exportablein the given (catalog/topological) order, sequentially (parents before children matters). For each table:buildExclusionPredicate; if null continue. For each term:SELECT count(*)::text AS n FROM t WHERE scope AND term→ push to summary if n>0. If the table has anidcolumn:SELECT id FROM t WHERE scope AND predicate→excludedIds.set(name, ids)when non-empty. Setpredicates.set(name, predicate)only if any term count > 0 (avoid needlessNOT (...)on clean tables). - Add the purge:
/** Permanently delete the rows `computeScopeExclusions` would exclude. Call INSIDE a transaction. * Deletes children first (reverse catalog order) so NO ACTION FKs never block. Throws if anything remains. */ export async function purgeScopeViolations(trx, exportable, byName, companyId, companyGroupId): Promise<{ deleted: Array<{ table: string; rows: number }> }>const ex = await computeScopeExclusions(trx, …); for tables in[...exportable].reverse()with a predicate:DELETE FROM t WHERE scope AND predicate→ collectnumDeletedRows(Kysely:const r = await sql….execute(trx); r.numAffectedRows). Thenconst left = await findExportScopeViolations(trx, …); ifleft.lengththrownew Error("Purge left scope violations behind: " + formatScopeViolations(companyId, left)). - In
company-backup.ts: delete the moved code, addexport * from "../../../backups/scope";next to the existingexport * from "../../../backups/schema";. Fix thetype RawBuilderimport if now unused.company-backup.transforms.tsimportsRETAINED_REF_TABLESfrom./company-backup— still resolves via the re-export; no change. - Append
export * from "./scope";toschema.ts.
Verify:
cd /Users/aashu/work/carbon/carbon-feat-skip-corrupted-data-in-backups && pnpm --filter @carbon/jobs typecheck
# Expected: exit 0, no errors
pnpm --filter @carbon/jobs test -- src/inngest/functions/tasks/company-backup.closure.test.ts src/backups
# Expected: all existing tests pass
Out of scope: changing what counts as a violation; touching company-backup.transforms.ts logic.
Task 2: Unit-test the pure builders
Depends on: 1 Files:
- Create:
packages/jobs/src/backups/scope.test.ts - Copy from (precedent):
packages/jobs/src/inngest/functions/scheduled/workflow-run-retention.test.ts(DummyDriver Kysely for compile-only SQL) andpackages/jobs/src/inngest/functions/tasks/company-backup.closure.test.ts(col/table/fkbuilders)
Steps:
- Build a compile-only
dbexactly as in the precedent. Buildcol,table,fkhelpers as in the closure test (copy them). - Tests for
scopeEdges: (a) nullable FK excluded; (b) FK touser(retained) excluded; (c) FK to non-exportable table excluded; (d) NOT-NULL FK to exportable → included. - Tests for
buildExclusionPredicate(...)compiled viasqlSELECT 1 WHERE ${predicate}.compile(db).sql:- violation term present: contains
"itemId" is not null and "itemId" not in (select id from "item" where; - cascade term only when parent has excluded ids: with
excludedIds = new Map([["jobOperation", ["op1"]]])→ contains"operationId" in (; with empty map → does not; - table without
idcolumn (likejobOperationDependency) still returns a predicate; - returns null when the table has no qualifying edges.
- violation term present: contains
- Test
formatScopeViolations("C", [{table:"pickMethod",column:"itemId",refTable:"item",rows:1}])equals the exact prod string (Refusing to export C: 1 NOT-NULL reference(s) escape company scope, so the backup could never be restored:\n pickMethod.itemId → item (1 row)). - Test
ExportScopeViolationErrorexposes.violationsand.name === "ExportScopeViolationError".
Verify:
pnpm --filter @carbon/jobs test -- src/backups/scope.test.ts
# Expected: all tests pass
Out of scope: DB-backed tests (Task 11 covers with a dry run).
Task 3: Manifest.excludedRows, event skipCorrupted, edge function pass-through
Depends on: none Files:
- Modify:
packages/jobs/src/backups/schema.ts—Manifesttype - Modify:
packages/lib/src/events.ts—"carbon/company-export"data - Modify:
packages/database/supabase/functions/export-company/index.ts— forward flag
Steps:
- In
Manifestadd afterexcludedTables:/** Rows deliberately left out because a NOT-NULL FK escaped company scope ("violation") * or pointed at such a row ("cascade"). Absent/empty = full export. Written only when * the export ran with `skipCorrupted`. */ excludedRows?: Array<{ table: string; column: string; refTable: string; rows: number; cause: "violation" | "cascade" }>; - In
events.ts"carbon/company-export".dataadd/** Opt-in: exclude rows whose NOT-NULL FK escapes company scope instead of refusing. */ skipCorrupted?: boolean;. - In the edge function destructure
skipCorruptedfrom the body and sendskipCorrupted: skipCorrupted === truein the event data.
Verify:
pnpm --filter @carbon/lib typecheck && pnpm --filter @carbon/jobs typecheck
# Expected: exit 0
Out of scope: bumping BACKUP_VERSION; validating manifests.
Task 4: buildCompanyBackup({ skipCorrupted }) + export job marker fields
Depends on: 1, 3 Files:
- Modify:
packages/jobs/src/inngest/functions/tasks/company-export.ts
Steps:
- Import
computeScopeExclusions,ExportScopeViolationError,type ScopeViolationfrom./company-backup(re-exported). Remove the now-unusedfindExportScopeViolationsimport if no longer used directly (it is still used — see step 3). - Add
skipCorrupted?: boolean;tobuildCompanyBackup'soptswith doc: "Exclude (and record inmanifest.excludedRows) rows whose NOT-NULL FK escapes company scope, plus rows depending on them. Default false = refuse (the guard is a tenant-leak detector; skipping is a user-confirmed recovery, see spec 2026-09-01)." - Replace the violation block:
let exclusions: ScopeExclusions | null = null; if (opts.skipCorrupted) { exclusions = await computeScopeExclusions(db, exportable, byName, companyId, companyGroupId); if (exclusions.summary.length > 0) { log.warn("Company export skipping out-of-scope rows", { companyId, excludedRows: exclusions.summary }); } } else { const scopeViolations = await findExportScopeViolations(db, exportable, byName, companyId, companyGroupId); if (scopeViolations.length > 0) throw new ExportScopeViolationError(companyId, scopeViolations); }company-export.tshas no logger today: addconst log = getLogger("jobs", "company-export");withimport { getLogger } from "@carbon/logger";(same ascompany-backup.ts). Keep the existing long comment about the refusal being deliberate; append one sentence: "skipCorruptedis the ONE sanctioned bypass — user-confirmed after a visible failure, and the manifest records what was dropped." - In the per-table dump:
const exclusion = exclusions?.predicates.get(table.name); const exclusionFilter = exclusion ? sqlAND NOT (${exclusion}): sql``;and append${exclusionFilter}to theWHEREright after${ledgerFilter}. - In the manifest literal add
excludedRows: exclusions?.summary ?? []. companyExportFunction: destructureskipCorruptedfromevent.data, passskipCorrupted: skipCorrupted === truetobuildCompanyBackup.- Extend
upsertExportMarker'smetadatatype withreason?: "scope-violations"; violations?: ScopeViolation[]; label?: string | null; includeStorage?: "none" | "all";. In thecatch:await upsertExportMarker(client, companyId, userId, { status: "failed", startedAt, error: message.slice(0, 2000), label: label ?? null, includeStorage, ...(err instanceof ExportScopeViolationError ? { reason: "scope-violations", violations: err.violations } : {}) });
Verify:
pnpm --filter @carbon/jobs typecheck
# Expected: exit 0
Out of scope: company-template.ts / company-restore.ts snapshot calls (they keep default skipCorrupted: false).
Task 5: Restore job — classify snapshot failure, store filePath/includeStorage
Depends on: 1 Files:
- Modify:
packages/jobs/src/inngest/functions/tasks/company-restore.ts
Steps:
- Import
ExportScopeViolationError, type ScopeViolationfrom./company-backup. - Extend
RestoreMetawith:/** The backup being restored + file choice — kept so a recovery action can restart the same restore. */ filePath?: string; includeStorage?: "none" | "all"; /** Set when the pre-restore snapshot refused because the LIVE data has out-of-scope rows. */ reason?: "scope-violations"; violations?: ScopeViolation[]; - In the first
writeRestoreMarker(status running) addfilePath, includeStorageto the patch. - In the main
catch (err)ofcompanyRestoreFunction(the one writingstatus: "failed"), spread...(err instanceof ExportScopeViolationError ? { reason: "scope-violations", violations: err.violations } : {})into the patch.
Verify:
pnpm --filter @carbon/jobs typecheck
# Expected: exit 0
Out of scope: the revert/finalize functions; company-template.ts.
Task 6: ERP service types/readers + exportCompanyBackup arg
Depends on: 3 Files:
- Modify:
apps/erp/app/modules/settings/backups.service.ts
Steps:
exportCompanyBackupargs: addskipCorrupted?: boolean;(passed through in the body as-is).- Add near the top:
export type ScopeViolationSummary = { table: string; column: string; refTable: string; rows: number };(typed locally — the app must not import job internals; mirror the comment onCompanyTemplateRun). CompanyBackupSummary: addexcludedRows: NonNullable<Manifest["excludedRows"]>;(default[]inlistCompanyBackupFolders, set fromm.excludedRows ?? []when the manifest parses).getCompanyRestoreRuns: extend themetacast and returned object withreason: meta.reason ?? null,violations: meta.violations ?? [],filePath: meta.filePath ?? null,includeStorage: meta.includeStorage ?? null.CompanyExportRun: addreason: "scope-violations" | null; violations: ScopeViolationSummary[]; label: string | null; includeStorage: "none" | "all" | null;and populate frommetaingetCompanyExportRun.
Verify:
pnpm --filter erp typecheck
# Expected: exit 0 (RestoreReviewRow's RestoreRun type is structurally satisfied by the wider object)
Out of scope: UI.
Task 7: ERP server purgeCorruptedRows
Depends on: 1 Files:
- Modify:
apps/erp/app/modules/settings/backups.server.ts
Steps:
- Import
purgeScopeViolations, selectExportableTablesfrom@carbon/jobs/backups(available via the Task 1 re-export). - Add:
If/** * Permanently delete this company's rows whose NOT-NULL FK escapes company scope (and rows * depending on them) — the data the export guard refuses on. Only ever called from the * user-confirmed "Remove corrupted data and restore" action. One transaction; children first. */ export async function purgeCorruptedRows(companyId: string): Promise<{ deleted: Array<{ table: string; rows: number }> }> { const db = getDatabaseClient(); const serviceRole = getCarbonServiceRole(); const company = await serviceRole.from("company").select("companyGroupId").eq("id", companyId).single(); if (company.error) throw new Error(company.error.message); const catalog = await getCompanyTableCatalog(db); const { exportable } = selectExportableTables(catalog); const byName = new Map(catalog.tables.map((t) => [t.name, t])); const result = await db.transaction().execute((trx) => purgeScopeViolations(trx, exportable, byName, companyId, company.data.companyGroupId ?? null) ); log.warn("Backups: purged out-of-scope rows before restore", { companyId, deleted: result.deleted }); return result; }getDatabaseClient()'s type is not assignable toKysely<KyselyDatabase>forpurgeScopeViolations, STOP and report — do not cast.
Verify:
pnpm --filter erp typecheck
# Expected: exit 0
Out of scope: any deletion outside purgeScopeViolations.
Task 8: Route actions exportSkipCorrupted and purgeAndRestore
Depends on: 5, 6, 7 Files:
- Modify:
apps/erp/app/routes/x+/settings+/backups.tsx—actionswitch only
Steps:
- Import
purgeCorruptedRowsfrom~/modules/settings/backups.server. - Add case
"exportSkipCorrupted":const run = await getCompanyExportRun(client, companyId); if (run.data?.status !== "failed" || run.data.reason !== "scope-violations") { return { success: false, message: "There is no failed backup to retry" }; } const result = await exportCompanyBackup(client, { companyId, userId, label: run.data.label ?? undefined, includeStorage: run.data.includeStorage ?? "none", skipCorrupted: true }); if (result.error) return { success: false, message: await getEdgeFunctionErrorMessage(result.error, "Failed to start backup") }; return { success: true, message: "Backup started", started: "export" as const }; - Add case
"purgeAndRestore":
(Action messages here are server strings like the existing ones in this file — the route already returns English messages fromconst restoreRunId = String(formData.get("restoreRunId") ?? ""); const runs = await getCompanyRestoreRuns(client, companyId); const failed = runs.data?.find((r) => r.restoreRunId === restoreRunId); if (!failed || failed.status !== "failed" || failed.reason !== "scope-violations" || !failed.filePath) { return { success: false, message: "This restore can't be retried this way" }; } if (runs.data?.some((r) => r.status !== "failed")) { return { success: false, message: "Finish your current restore — keep or revert it — first." }; } try { const { deleted } = await purgeCorruptedRows(companyId); await finalizeCompanyRestore({ companyId, restoreRunId }); const newRunId = await startCompanyRestore({ companyId, userId, filePath: failed.filePath, includeStorage: failed.includeStorage ?? "all", label: failed.label ?? failed.filePath }); const rows = deleted.reduce((s, d) => s + d.rows, 0); return { success: true, message: `Removed ${rows} row${rows === 1 ? "" : "s"} — restoring`, restoreRunId: newRunId, deleted }; } catch (err) { return { success: false, message: err instanceof Error ? err.message : "Failed to remove corrupted data" }; }action; keep consistent. The UI-rendered copy in Task 9 is Lingui-wrapped.) - Widen the
fetchergeneric in the component to includedeleted?: Array<{ table: string; rows: number }>.
Verify:
pnpm --filter erp typecheck
# Expected: exit 0
Out of scope: UI rendering (Task 9).
Task 9: UI — banner retry button, "N rows excluded" row detail, restore-row CTA + confirm modal
Depends on: 6, 8 Files:
- Modify:
apps/erp/app/routes/x+/settings+/backups.tsx— component - Modify:
apps/erp/app/modules/settings/ui/Backups/RestoreReviewRow.tsx - Create:
apps/erp/app/modules/settings/ui/Backups/ExcludedRowsInfo.tsx; export fromui/Backups/index.ts - Copy from (precedent):
apps/erp/app/modules/settings/ui/Backups/BackupContentsInfo.tsx(Popover "i" pattern);apps/erp/app/components/Modals/Confirm/Confirm.tsx(modal layout); existing DeleteConfirmusage inBackupRow - All new strings via
<Trans>/useLingui().t(@lingui/react/macro) — see.claude/rules/i18n-lingui-system.md; nevertfrom@lingui/core/macro.
Steps:
- Failed-export banner (
{exportFailed && (...)}block): whenexportRun?.reason === "scope-violations", render under the error text a<span className="text-xs text-muted-foreground">with<Trans>Retrying with skip leaves out {count} rows whose links point outside this company. They are listed in the backup's details.</Trans>wherecount = exportRun.violations.reduce((s, v) => s + v.rows, 0); and in the buttonHStackadd before Dismiss:<Button onClick={() => fetcher.submit({ intent: "exportSkipCorrupted" }, { method: "post" })} isLoading={fetcher.state !== "idle" && fetcher.formData?.get("intent") === "exportSkipCorrupted"}><Trans>Skip corrupted rows and retry</Trans></Button>. The existingfetcher.dataeffect already handlesstarted: "export"(drops the optimistic row). ExcludedRowsInfo.tsx: props{ excludedRows: Array<{ table; column; refTable; rows; cause }> }. Copy the Popover/trigger structure fromBackupContentsInfo(no fetcher). Trigger:LuInfobutton,aria-label={t\Excluded rows`}. Content: titleRows left out of this backup, one line per entry ``${e.table}.${e.column} → ${e.refTable}`` left,e.rows.toLocaleString()right, and a mutedcause === "violation" ? t`link outside company` : t`depends on an excluded row``.BackupRow: computeconst excluded = file.excludedRows.reduce((s, e) => s + e.rows, 0). In the ready-branch second line, after the size, append{excluded > 0 ? <> {" · "}<Trans>{excluded} rows excluded</Trans> <ExcludedRowsInfo excludedRows={file.excludedRows} /></> : null}(use Linguipluralmacro from@lingui/react/macroif available in the repo — grepplural(inapps/erp/app; otherwise the<Trans>with the number is acceptable).RestoreReviewRow.tsx: extendRestoreRunwithreason: "scope-violations" | null; violations: Array<{ table: string; column: string; refTable: string; rows: number }>;. Add proponPurgeAndRestore: () => void. In the failed branch, whenrun.reason === "scope-violations", render before Dismiss:<Button variant="destructive" onClick={onPurgeAndRestore}><Trans>Remove corrupted data and restore</Trans></Button>. Also switch the two existing hardcoded strings in this file ("Failed —", "Restore", "restoring…", "reverting…", "rows", "Keep", "Revert", "Dismiss") to<Trans>/twhile here.- Route component: add
const [purgeRun, setPurgeRun] = useState<(typeof restoreRuns)[number] | null>(null);. PassonPurgeAndRestore={() => setPurgeRun(run)}to eachRestoreReviewRow. Render, whenpurgeRun, aModalcopied fromConfirm.tsx's JSX (Modal/ModalOverlay/ModalContent/ModalHeader/ModalTitle/ModalBody/ModalFooter from@carbon/react) but with a plain destructiveButtoninstead offetcher.Form:- title:
t\Permanently delete ${total} rows?`wheretotal = purgeRun.violations.reduce(...)`; - body:
<Trans>These rows link to data outside this company, so a safety copy of your current data can't be made. Deleting them cannot be undone. The restore will start right after.</Trans>followed by a<ul>of`${v.table}.${v.column} → ${v.refTable}: ${v.rows}`; - confirm button
<Trans>Delete and restore</Trans>→fetcher.submit({ intent: "purgeAndRestore", restoreRunId: purgeRun.restoreRunId }, { method: "post" }); resolveRun(purgeRun.restoreRunId); setPurgeRun(null); - cancel
<Trans>Cancel</Trans>→setPurgeRun(null).
- title:
- In the existing
fetcher.dataeffect, change theresult.success && result.restoreRunIdbranch toif (result.deleted) toast.success(result.message);beforesetActive(...)so the removed-row count is shown, then open the modal as today. - Progress modal (
BackupProgressModal.tsx~line 293): replace the sentence "The system created an invalid backup — please contact Carbon support." with<Trans>This backup could not be completed.</Trans>(the error text below already names the cause; matches the banner copy and copy rule D9).
Verify:
pnpm --filter erp typecheck
# Expected: exit 0
cd /Users/aashu/work/carbon/carbon-feat-skip-corrupted-data-in-backups && pnpm lingui:extract
# Expected: exits 0; `git diff --stat packages/locale/locales/en/erp.po` shows the new msgids added
Out of scope: restyling existing rows; MES app.
Task 10: Docs — rule file + spec status
Depends on: none Files:
- Modify:
.claude/rules/company-backup-restore.md— the "Out-of-scope rows are FATAL" bullet - Modify:
.ai/specs/2026-09-01-skip-corrupted-rows-in-backups.md— status →in-progress, changelog line
Steps:
- In the rule, keep the first two paragraphs of the bullet. Replace the sentence starting "Do not rebuild it." through "…lowering the guard." with:
"The ONE sanctioned bypass (spec
2026-09-01-skip-corrupted-rows-in-backups.md): after a visible failure the user may click Skip corrupted rows and retry, which re-runs the export withskipCorrupted: true—computeScopeExclusions(src/backups/scope.ts) excludes the violating rows and their dependents, the job logs awarnwith the list, andmanifest.excludedRowsrecords it. A restore blocked by the same rows (its pre-restore snapshot runs the same guard) offers Remove corrupted data and restore, whichpurgeScopeViolationsdeletes in one transaction (children first) after a destructive confirm. Never make either the default, and never skip silently." - Add
scope.tsto the file'spaths:frontmatter is unnecessary (already covered bypackages/jobs/src/backups/**). Add one line under the module description mentioningsrc/backups/scope.tsholds the scope predicates/guard/exclusion/purge. - Spec: set
> Status: in-progress; changelog- 2026-09-01: Plan written (.ai/plans/…); implementation started.
Verify:
grep -n "Skip corrupted rows and retry" .claude/rules/company-backup-restore.md
# Expected: one match
Out of scope: docs/content/docs/platform/backups.mdx (user-facing docs; leave unless the user asks).
Task 11: Verification — typecheck, tests, lingui extract, DB-backed dry run
Depends on: all Files:
- Create (scratch, not committed):
/private/tmp/claude-501/-Users-aashu-work-carbon/bb6cf834-ab54-4876-94f2-28d9a88f9535/scratchpad/scope-dryrun.ts
Steps:
pnpm --filter @carbon/jobs testandpnpm --filter @carbon/lib typecheck && pnpm --filter @carbon/jobs typecheck && pnpm --filter erp typecheck.pnpm run lint(Biome) — fix anything in touched files only.- DB dry run (does NOT persist): write the scratch script that imports
getCompanyTableCatalog, selectExportableTablesfrom<worktree>/packages/jobs/src/backups/schema.tsandcomputeScopeExclusions, findExportScopeViolations, purgeScopeViolationsfrom<worktree>/packages/jobs/src/backups/scope.ts; builds a Kysely withPostgresDialect+pg.Pool({ connectionString: "postgresql://postgres:postgres@localhost:59795/postgres" }); for companyJqvzHghaGpe7vTMU1Y27jP(group fromSELECT "companyGroupId" FROM company):- prints
findExportScopeViolations→ expect the 4 edges (1/3/3/3); - prints
computeScopeExclusions(...).summary→ expectpickMethod.itemId→item 1 violation, and the threejobOperationDependencyedges at 3 each (allviolation, nocascade— neither table has dependents); - runs
db.transaction().execute(async trx => { const r = await purgeScopeViolations(trx, …); console.log(r); throw new Error("ROLLBACK"); })and catches the error → expectdeleted=[{ jobOperationDependency: 3 }, { pickMethod: 1 }](order: children first) and no "left behind" error; - afterwards re-run
findExportScopeViolations→ expect still 4 (rolled back). Run withcd <worktree> && pnpm --filter @carbon/jobs exec tsx <script>. Iftsxcannot importschema.ts(CJS interop error naming@carbon/database), STOP and report — do not flip any package'stypefield (see.ai/lessons.md).
- prints
- Report to the user the manual QA to run in the browser (they own the clicks): (a) Backups → Create backup on test comp → fails → banner shows the new button → click → backup becomes Ready with
4 rows excludedand the info popover lists the four edges; (b) Restore that backup → fails at snapshot → row shows Remove corrupted data and restore → confirm modal lists 4 rows → confirm → toast "Removed 4 rows — restoring" → progress modal → Keep/Revert. Note the jobs worker (packages/devup) must be restarted to pick up the@carbon/jobschanges if it does not hot-reload.
Verify:
pnpm --filter @carbon/jobs test && pnpm --filter erp typecheck
# Expected: both exit 0
Out of scope: committing; deleting the local corrupted rows.
Run log — 2026-09-01
- Tasks 1–11 done.
pnpm --filter @carbon/jobs test→ 38 files / 572 tests pass (8 new inscope.test.ts);@carbon/jobs,@carbon/lib,erptypecheck clean; Biome clean on touched files;pnpm lingui:extract+lingui:cleanadded the new msgids to all 13 locales. - DB dry run (rolled back) against
JqvzHghaGpe7vTMU1Y27jP:computeScopeExclusions.summary= pickMethod.itemId→item 1 + jobOperationDependency {dependsOnId, jobId, operationId} 3 each, allviolation;purgeScopeViolations.deleted=[jobOperationDependency 3, pickMethod 1](children first), zero violations inside the trx, all 4 still present after rollback. - Not done here (user-owned): browser QA of both flows; restart of the jobs worker (
packages/devup) if it does not pick up@carbon/jobschanges. Not committed (user rule).