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.
Under-scoped connection reconnect prompts — implementation plan
Spec / source: .ai/specs/implemented/2026-09-01-connection-reconnect-prompts.md
Branch: feat/active-pieces-integration · never git commit (user's rule)
Filters: @carbon/ee, @carbon/jobs, erp, @carbon/workflows (scope every typecheck)
Progress
- Task 1: ee —
grantedScopes/missingScopes; revive refreshesmetadata; tests - Task 2: jobs —
requiredScopesFor; barrel export; GCalmetadata.scopes; tests - Task 3: Runner — pre-check +
missing_scopemapping; tests - Task 4: Options provider
reconnectstate + node banner - Task 5: Accounts tab —
requiredScopesin loader, badge/copy, Reconnect button - Task 6: Card health includes the scope check
- Task 7: Regen catalog + Lingui; rule/spec sync
- Task 8: Verification + run log
Dependencies
1 → 3, 4, 5, 6 2 → 3, 4, 5, 6 7 after 2–6 8 last
Tasks 1 and 2 in parallel; then 3–6 in parallel (disjoint files).
Task 1: ee — scope predicates + revive metadata
Files: Modify packages/ee/src/integrations/connections.ts, packages/ee/src/integrations/connections.test.ts
Steps:
- Below
connectionsHealthy, add:/** * Scopes the vendor said it granted, recorded by the callback from the token * response (`metadata.scopes`; Slack separates with commas, the v2 backfill with * spaces). `null` when the connection predates recording or the vendor does not * report them — a caller must never flag what it cannot see. */ export function grantedScopes( connection: Pick<IntegrationConnection, "metadata"> ): string[] | null { const raw = connection.metadata.scopes; if (typeof raw !== "string") return null; const scopes = raw.split(/[\s,]+/).filter((s) => s.length > 0); return scopes.length === 0 ? null : scopes; } /** Required scopes the connection does not hold. Empty when grants are unknown. */ export function missingScopes( connection: Pick<IntegrationConnection, "metadata">, required: readonly string[] ): string[] { const granted = grantedScopes(connection); if (granted === null) return []; const have = new Set(granted); return required.filter((scope) => !have.has(scope)); } - In
createConnection's reviveupdate({...})add...(args.metadata === undefined ? {} : { metadata: args.metadata as never }),with a one-line comment: a re-consent's token response is authoritative (itsscopesis how "reconnect" clears). - Tests (new
describe("scopes")+ one revive case):grantedScopes({ metadata: { scopes: "a,b" } })→["a","b"];"a b"→ same;{}→null;""→null.missingScopes({ metadata: { scopes: "a" } }, ["a","c"])→["c"]; unknown →[].- Revive:
makeClient({ status: "Revoked" })withmetadata: { scopes: "old" }on the row;createConnectionsame name withmetadata: { scopes: "new" }→rows.get(CONNECTION)!.metadata.scopes === "new". (CheckmakeClient'supdatehandles ametadatakey; if not, extend the stub'supdateto assign it.)
Verify:
pnpm --filter @carbon/ee exec vitest run src/integrations/connections.test.ts # pass incl. new cases
pnpm exec turbo run typecheck --filter=@carbon/ee # no errors
Task 2: jobs — requiredScopesFor, GCal scopes map
Files: Modify packages/jobs/src/workflows/integrations/oauth.ts, index.ts, allowlist.ts, oauth.test.ts
Steps:
oauth.ts:/** The scopes a connection for this piece must hold — exactly what `buildConsentUrl` * requests, so "reconnect" always grants what the check demands. */ export async function requiredScopesFor(pieceName: string): Promise<readonly string[]> { const entry = PIECE_ALLOWLIST[pieceName]; if (entry === undefined) return []; return entry.oauth.scope ?? (await getPieceOAuth2Auth(pieceName)).scope; }index.ts: addrequiredScopesForto the./oauthexport list.allowlist.tsgoogle-calendar row: addmetadata: { scopes: "scope" }afteraccountLabel, comment: Google reports granted scopes too; recorded so a future scope change can be detected.oauth.test.ts:requiredScopesFor("slack")→ 16 unique, containschannels:read;requiredScopesFor("google-calendar")deep-equals(await getPieceOAuth2Auth("google-calendar")).scope;requiredScopesFor("nope")→[].
Verify:
pnpm --filter @carbon/jobs exec vitest run src/workflows/integrations/ # all pass
pnpm exec turbo run typecheck --filter=@carbon/jobs
Task 3: Runner — pre-check + vendor-error mapping
Files: Modify packages/jobs/src/workflows/actions/integration.ts, packages/jobs/src/workflows/actions/integration.test.ts
Steps:
- Import
missingScopesfrom@carbon/ee/integrations/connectionsandrequiredScopesForfrom../integrations/oauth. Add:const reconnectCopy = (label: string) => `The ${label} connection needs to be reconnected to grant the permissions this step uses — Settings → Integrations → ${label} → Accounts → Reconnect.`; const SCOPE_ERROR = /missing_scope|insufficient_scope|insufficient.?permission/i; - After the
ownedcheck (before resolving the token):// Deterministic first: a workspace connected before this piece needed a scope // fails here, without a vendor call, with words that name the fix. const missing = missingScopes(owned, await requiredScopesFor(pieceName)); if (missing.length > 0) { logger?.warn?.(...) — if the file has no logger, skip logging; do NOT add a logger import just for this. return { ok: false, error: reconnectCopy(label) }; } - In the final
catch:if (SCOPE_ERROR.test(message)) return { ok: false, error: reconnectCopy(label) };before the genericrejected thisreturn. - Tests: the module mock of
@carbon/ee/integrations/connectionslists exports explicitly — change it tovi.mock("@carbon/ee/integrations/connections", async (importOriginal) => ({ ...(await importOriginal<typeof import("@carbon/ee/integrations/connections")>()), readConnection: ..., resolveConnectionAuth: ... }))so the realmissingScopesruns. The../integrations/registrymock must also exposegetPieceOAuth2Auth: vi.fn().mockResolvedValue({ scope: [] })(google-calendar has nooauth.scopeoverride).readConnection's default mock row getsmetadata: {}. Add:- "refuses before calling the vendor when the connection lacks a required scope":
readConnection→{ id, pieceName: "slack", metadata: { scopes: "chat:write" } },pieceName: "slack"→ error matches/reconnected .* Accounts → Reconnect/, and the mockedrunwas not called; - "maps a vendor scope error to the reconnect copy":
runrejectsnew Error("An API error occurred: missing_scope")→ same copy.
- "refuses before calling the vendor when the connection lacks a required scope":
Verify:
pnpm --filter @carbon/jobs exec vitest run src/workflows/actions/integration.test.ts # pass incl. 2 new
pnpm exec turbo run typecheck --filter=@carbon/jobs
Task 4: Options provider reconnect + node banner
Files: Modify apps/erp/app/modules/workflows/options-providers.server.ts, apps/erp/app/modules/workflows/ui/Builder/config/forms/IntegrationNodeForm.tsx
Steps:
- Provider: import
missingScopes(ee) andrequiredScopesFor(jobs). InconnectionProvider.resolve:const usable = usableConnections(await readConnections(getCarbonServiceRole(), companyId, piece)); const required = await requiredScopesFor(piece); const ready = usable.filter((row) => missingScopes(row, required).length === 0); // Connected, but every account predates a scope this piece now needs: the fix is a // re-consent, not a new account — say so instead of offering an empty list. if (ready.length === 0 && usable.length > 0) { return { options: [], errorCode: "reconnect", errorHref: `${path.to.integration(piece)}?tab=connections` }; } return { options: ready.map(...same as today...), emptyHref: path.to.integrations }; IntegrationNodeForm: destructureerrorCode, errorHreffrom the existinguseWorkflowOptions(...)call. Where the "isn't connected yet" block renders (piece && checked && !connected), branch first:{piece && checked && !connected && errorCode === "reconnect" ? ( <div className="flex flex-col items-start gap-3 rounded-md border border-dashed p-4"> <p className="text-sm text-muted-foreground"> <Trans>{appName} is connected, but needs to be reconnected before workflow steps can use it.</Trans> </p> <Button asChild variant="secondary" isDisabled={isReadOnly}> <Link to={errorHref ?? `${path.to.integration(piece)}?tab=connections`} target="_blank" rel="noreferrer"> <Trans>Reconnect {appName}</Trans> </Link> </Button> </div> ) : piece && checked && !connected ? ( …existing Connect block… ) : null}
Verify:
pnpm exec turbo run typecheck --filter=erp
grep -n '"reconnect"' apps/erp/app/modules/workflows/options-providers.server.ts # ≥ 1 in connectionProvider
Task 5: Accounts tab — badge, copy, Reconnect
Files: Modify apps/erp/app/routes/x+/settings+/integrations.$id.tsx (~line 912), apps/erp/app/modules/settings/ui/Integrations/ConnectionsTab.tsx
Precedent: the existing Add-account button + connect fetcher effect in the same file.
Steps:
- Loader: import
requiredScopesForfrom@carbon/jobs/integrations;connectionsgainsrequiredScopes: await requiredScopesFor(integrationId)(as a plainstring[]). ConnectionsTab:ConnectionRowadds"metadata"; props addrequiredScopes: readonly string[]; the drawer passes it (integrations.$id.tsx~line 1698). Lift theconnectfetcher'sloadintoconst startConnect = (name: string) => connect.load(\${path.to.api.integrationConnect(pieceName)}?name=${encodeURIComponent(name)}`)and passonReconnect={() => startConnect(connection.name)}+reconnecting={connect.state !== "idle"}+requiredScopestoConnectionItem`.ConnectionItem:const needsScopes = connectionUsable(connection) && missingScopes(connection, requiredScopes).length > 0;(importmissingScopesfrom@carbon/ee/integrations/connections).- Beside the status badge:
{needsScopes && <Badge variant="yellow" className="shrink-0"><Trans>Reconnect needed</Trans></Badge>}. - Replace the "This account has stopped working. Add it again…" paragraph with:
{needsScopes ? ( <span className="text-xs text-muted-foreground"><Trans>Connected before workflows needed extra permissions. Reconnect to grant them — everything else keeps working meanwhile.</Trans></span> ) : !connectionUsable(connection) ? ( <span className="text-xs text-muted-foreground"><Trans>This account has stopped working. Reconnect it — workflow steps using it will fail until you do.</Trans></span> ) : null} - In the right-hand column, above the Disconnect form:
{(needsScopes || !connectionUsable(connection)) && (<Button size="sm" leftIcon={<LuPlug />} isLoading={reconnecting} onClick={onReconnect}><Trans>Reconnect</Trans></Button>)}; wrap the column in aVStack spacing={2} className="shrink-0 items-end"so both buttons stack.
Verify:
pnpm exec turbo run typecheck --filter=erp
grep -c "Reconnect" apps/erp/app/modules/settings/ui/Integrations/ConnectionsTab.tsx # ≥ 3
Task 6: Card health includes the scope check
Files: Modify apps/erp/app/modules/settings/settings.server.ts (getIntegrationHealth)
Steps:
- Imports:
connectionUsable, missingScopes, readConnectionsfrom@carbon/ee/integrations/connections;PIECE_ALLOWLIST, requiredScopesForfrom@carbon/jobs/integrations(this is a.server.tsmodule — server-only is fine). - After
const status = await (healthcheck …)(companyId, resolvedMetadata);and before theredis.set:
and use// A piece card is also unhealthy while any usable account predates a scope the // piece now needs — the step would fail with `missing_scope`. Checked here rather // than in the ee hook because only this side can see the allowlist's required list. let healthy = status; if (healthy && PIECE_ALLOWLIST[integration.id!] !== undefined) { const [rows, required] = await Promise.all([ readConnections(getCarbonServiceRole(), companyId, integration.id!), requiredScopesFor(integration.id!) ]); healthy = !rows.some((row) => connectionUsable(row) && missingScopes(row, required).length > 0); }healthyin theredis.setand the returnedhealth.
Verify:
pnpm exec turbo run typecheck --filter=erp
Task 7: Regen + docs
Steps:
pnpm run generate:workflow-catalog && pnpm run check:workflow-catalog && pnpm run lingui:extract(the GCal row change is metadata the generator ignores; Lingui picks up the new UI strings)..claude/rules/workflow-integrations.md: under "Connections" add a short "Scopes drift" paragraph:metadata.scopesrecords the grant;missingScopesvsrequiredScopesFor; the four surfaces; revive refreshes metadata.- Spec status →
implemented (2026-09-01)+ changelog line.
Verify:
pnpm run check:workflow-catalog # ok
grep -c "missingScopes\|requiredScopesFor" .claude/rules/workflow-integrations.md # ≥ 2
Task 8: Verification + run log
Files: Create .ai/runs/2026-09-01-connection-reconnect-prompts.md (format: the two earlier run logs today)
Steps:
- Gates: typecheck
@carbon/ee @carbon/jobs erp @carbon/workflows;pnpm --filter @carbon/ee test;pnpm --filter @carbon/jobs test(services.test.ts env-gated, pre-existing);pnpm --filter @carbon/workflows test; biome on touched files. - Data check on dev: the backfilled Slack row has 10 scopes →
missingScopes= the 6 piece-only scopes (compute with a one-offnode -eagainst the row'smetadata.scopesstring and the allowlist list, or via psql string ops). - Browser matrix (spec AC 4–6) — user-driven after the Slack manifest update; record UNVERIFIED honestly.