Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
fix: Inngest event key on registration, tracing for server functions and errors, 415 for non-form bodies (#1831)
Sid93c609e2308e
diff --git a/.claude/rules/event-system.md b/.claude/rules/event-system.mdindex 48549c90c5..acfbb30d3c 100644--- a/.claude/rules/event-system.md+++ b/.claude/rules/event-system.md@@ -31,7 +31,7 @@ pg_cron 'event-queue-sweeper' (* * * * *, in-DB): if visible messages exist →The wake path (`20260721184852_event-queue-wake.sql`, rewired by `20261002170250_send-inngest-events-from-postgres.sql` and the managed files in `packages/database/src/event-system/functions/`) — the helpers live in the internal `util` schema, NOT `public`:- `util.send_inngest_event(name, data)` — SECURITY DEFINER; `net.http_post`s `{ name, data }` to the Vault secret `inngest_event_url` (`<base>/e/<eventKey>`). Error-swallowed and no-ops when the secret is unset — OLTP writes never fail on push failure. pg_net queues the request transactionally, so the event fires only after commit. Postgres calls no edge function.- `util.wake_event_queue()` — sends `carbon/event-queue.process` through it.-- `public.set_inngest_event_url(url)` writes the secret (refuses `anon`/`authenticated`). The URL carries the event key, so it lives in Vault, not in `config` (which has a SELECT policy). A no-op when the URL is unchanged (`20261003134512`). The ERP keeps the KEY in it current on boot in production (`apps/erp/app/utils/inngest-event-url.server.ts` → `public.set_inngest_event_config(key, base)`, `20261003142107`): it creates the URL from its own `INNGEST_BASE_URL` when none is stored, and otherwise rewrites only the `<eventKey>` segment — a stored address is never changed, since the database may reach Inngest somewhere the app does not. So a deployment needs no separate step. `ci/src/migrations.ts` calls the same function on every deploy for a workspace with an `inngest_event_key` (retried while PostgREST reloads its schema), so a changed `inngest_base_url` there needs `set_inngest_event_url` by hand. Set in full by `packages/database/src/seed.ts` (`resolveInngestEventUrl`), locally by `ensureConfigRow` in `packages/dev/src/services/migrations.ts` (`http://inngest:8288/e/NO_EVENT_KEY_SET`) and by `scripts/restore-database.sh` after it clears Vault on a local restore, and on self-host by `contrib/deploying/simple-docker-caddy/deploy.sh migrate` (`set_inngest_event_url`, reading the key from the erp task). In CI a workspace missing `inngest_event_key` or the service role key logs an error and fails the run, after seeding and scripts, which do not depend on it.+- `public.set_inngest_event_url(url)` writes the secret (refuses `anon`/`authenticated`). The URL carries the event key, so it lives in Vault, not in `config` (which has a SELECT policy). A no-op when the URL is unchanged (`20261003134512`). Both setters take one advisory lock (`20261003191518`), so concurrent writers (app registration, deploy, operator) serialize instead of failing on the Vault's unique name. The ERP keeps the KEY in it current whenever it registers with Inngest: a `PUT /api/inngest` (`apps/erp/app/routes/api+/inngest.ts`) calls `public.set_inngest_event_config(key, base)` before the registration handshake, in production. That PUT happens on every deploy (Vercel's Inngest integration, `ci/src/jobs.ts`) and on every boot of a long-lived host (`inngest-self-sync.server.ts`), inside a request, once per registration rather than once per instance. Not at module load: a serverless host froze those calls mid-connect. (`20261003142107`): it creates the URL from its own `INNGEST_BASE_URL` when none is stored, and otherwise rewrites only the `<eventKey>` segment — a stored address is never changed, since the database may reach Inngest somewhere the app does not. So a deployment needs no separate step. `ci/src/migrations.ts` calls the same function on every deploy for a workspace with an `inngest_event_key` (retried while PostgREST reloads its schema), so a changed `inngest_base_url` there needs `set_inngest_event_url` by hand. Set in full by `packages/database/src/seed.ts` (`resolveInngestEventUrl`), locally by `ensureConfigRow` in `packages/dev/src/services/migrations.ts` (`http://inngest:8288/e/NO_EVENT_KEY_SET`) and by `scripts/restore-database.sh` after it clears Vault on a local restore, and on self-host by `contrib/deploying/simple-docker-caddy/deploy.sh migrate` (`set_inngest_event_url`, reading the key from the erp task). In CI a workspace missing `inngest_event_key` or the service role key logs an error and fails the run, after seeding and scripts, which do not depend on it.- `dispatch_event_batch()` calls `util.wake_event_queue()` at most **once per transaction** via the txn-local GUC `carbon.event_wake_sent` (`set_config(..., true)`).- `util.sweep_event_queue()` — pg_cron job `event-queue-sweeper` re-wakes every minute while *visible* messages (`vt <= clock_timestamp()`) sit in `pgmq.q_event_system`.- **Why `util`, not `public`:** a public function is auto-exposed as a PostgREST RPC, and a non-superuser reference to a function that transitively calls `net.http_post` segfaults the backend (pg_net 0.20 / PG15) — a remote-DoS surface a `REVOKE` can't close because the crash precedes the privilege check. anon/authenticated have no `USAGE` on `util`, so the API can't reach it. The trigger and pg_cron call it as the owner (superuser), where the pg_net path is safe.diff --git a/apps/academy/app/entry.server.tsx b/apps/academy/app/entry.server.tsxindex d2dd413e3c..891313f71c 100644--- a/apps/academy/app/entry.server.tsx+++ b/apps/academy/app/entry.server.tsx@@ -3,6 +3,7 @@// including ports, remain AGPLv3; serving them over a network requires releasing their source.import { POSTHOG_API_HOST, SUPABASE_URL } from "@carbon/auth";+import { installFormBodyGuard } from "@carbon/auth/middleware/form-body.server";import {getNonce,setStrictContentSecurityPolicy@@ -12,6 +13,7 @@ import { handleRequest as vercelHandleRequest } from "@vercel/react-router/entryimport type { EntryContext, RouterContextProvider } from "react-router";ensureLoggingConfigured();+installFormBodyGuard();export const streamTimeout = 5_000;diff --git a/apps/academy/app/root.tsx b/apps/academy/app/root.tsxindex f6d62e185a..3e376137a0 100644--- a/apps/academy/app/root.tsx+++ b/apps/academy/app/root.tsx@@ -10,6 +10,7 @@ import {flashMiddleware,flashResultContext} from "@carbon/auth/middleware/flash.server";+import { formBodyMiddleware } from "@carbon/auth/middleware/form-body.server";import { securityMiddleware } from "@carbon/auth/middleware/security.server";import { getOrRefreshAuthSession } from "@carbon/auth/session.server";import { requestIdMiddleware } from "@carbon/logger/middleware.server";@@ -49,6 +50,7 @@ import { path } from "./utils/path";export const middleware = [requestIdMiddleware,securityMiddleware,+ formBodyMiddleware,flashMiddleware];export const clientMiddleware = [flashClientMiddleware];diff --git a/apps/erp/app/components/ActionTasks/syncNotes.ts b/apps/erp/app/components/ActionTasks/syncNotes.tsindex 665c984f7f..a31f8ce0ec 100644--- a/apps/erp/app/components/ActionTasks/syncNotes.ts+++ b/apps/erp/app/components/ActionTasks/syncNotes.ts@@ -51,7 +51,8 @@ export async function syncActionTaskNotes(: null;if (!result?.success) {- logger.error(`Failed to sync notes to ${target}`, {+ logger.error("Failed to sync notes to {target}", {+ target,actionId,entityType,status: response.status,@@ -59,7 +60,8 @@ export async function syncActionTaskNotes(});}} catch (e) {- logger.error(`Failed to sync notes to ${target}`, {+ logger.error("Failed to sync notes to {target}", {+ target,actionId,entityType,error: ediff --git a/apps/erp/app/entry.server.tsx b/apps/erp/app/entry.server.tsxindex 5780cdc8ec..cf6d19955d 100644--- a/apps/erp/app/entry.server.tsx+++ b/apps/erp/app/entry.server.tsx@@ -3,6 +3,7 @@// including ports, remain AGPLv3; serving them over a network requires releasing their source.import { POSTHOG_API_HOST, SUPABASE_URL } from "@carbon/auth";+import { installFormBodyGuard } from "@carbon/auth/middleware/form-body.server";import {getNonce,setStrictContentSecurityPolicy@@ -19,10 +20,10 @@ import { handleRequest as vercelHandleRequest } from "@vercel/react-router/entryimport { InngestSpanProcessor } from "inngest/experimental";import type { EntryContext, RouterContextProvider } from "react-router";import { isRouteErrorResponse } from "react-router";-import { scheduleInngestEventUrlSync } from "./utils/inngest-event-url.server";import { scheduleInngestSelfSync } from "./utils/inngest-self-sync.server";ensureLoggingConfigured();+installFormBodyGuard();// Vercel freezes an instance once its response is sent: keep it up until idle// database connections have closed and background work has finished.@@ -42,7 +43,6 @@ export const instrumentations = createTracing({spanProcessors: [new InngestSpanProcessor(inngest)]});scheduleInngestSelfSync();-scheduleInngestEventUrlSync();const log = getLogger("erp");diff --git a/apps/erp/app/modules/shared/shared.server.ts b/apps/erp/app/modules/shared/shared.server.tsindex 444f319bc8..c17d56bddf 100644--- a/apps/erp/app/modules/shared/shared.server.ts+++ b/apps/erp/app/modules/shared/shared.server.ts@@ -639,7 +639,8 @@ export async function requireCompanyRecord(.maybeSingle();if (error) {- logger.error(`Failed to verify ${table} for company`, {+ logger.error("Failed to verify {table} for company", {+ table,companyId,match,error@@ -647,7 +648,7 @@ export async function requireCompanyRecord(throw new Response("Not found", { status: 404 });}if (!data) {- logger.error(`${table} not found for company`, { companyId, match });+ logger.error("{table} not found for company", { table, companyId, match });throw new Response("Not found", { status: 404 });}}diff --git a/apps/erp/app/root.tsx b/apps/erp/app/root.tsxindex fbcc57dc9d..a97e547277 100644--- a/apps/erp/app/root.tsx+++ b/apps/erp/app/root.tsx@@ -10,6 +10,7 @@ import {flashMiddleware,flashResultContext} from "@carbon/auth/middleware/flash.server";+import { formBodyMiddleware } from "@carbon/auth/middleware/form-body.server";import { securityMiddleware } from "@carbon/auth/middleware/security.server";import { validator } from "@carbon/form";import { LocaleProvider, resolveLanguage } from "@carbon/locale";@@ -66,6 +67,7 @@ export const middleware = timedMiddleware({// First: the request scope (context, request id, access log).request: requestMiddleware,security: securityMiddleware,+ formBody: formBodyMiddleware,flash: flashMiddleware});export const clientMiddleware = [flashClientMiddleware];diff --git a/apps/erp/app/routes/api+/inngest.ts b/apps/erp/app/routes/api+/inngest.tsindex e09e1c43a5..35aa2d832a 100644--- a/apps/erp/app/routes/api+/inngest.ts+++ b/apps/erp/app/routes/api+/inngest.ts@@ -2,7 +2,9 @@// 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 { getCarbonServiceRole } from "@carbon/auth/client.server";import { functions, inngest, setWorkflowDispatch } from "@carbon/jobs/inngest";+import { getLogger } from "@carbon/logger";import { describeRequest } from "@carbon/logger/middleware.server";import {annotateRequestSpan,@@ -12,6 +14,8 @@ import { serve } from "inngest/remix";import type { ActionFunctionArgs, LoaderFunctionArgs } from "react-router";import { callOperation } from "./v1+/lib/call.server";+const logger = getLogger("erp", "inngest");+const handler = serve({client: inngest,functions,@@ -48,8 +52,37 @@ export function loader(args: LoaderFunctionArgs) {return handler(args);}-export function action(args: ActionFunctionArgs) {+// Postgres sends its own events to Inngest (the event-queue wake-up,+// embeddings, job notifications) at a URL kept in its Vault. A PUT here is the+// app registering with Inngest, which happens on every deploy and on every+// boot of a long-lived host, so that is when the database is told too: the+// event key is kept current, and an address already stored is left alone (the+// database may reach Inngest somewhere the app does not). Never fails the+// registration.+async function setDatabaseEventConfig() {+ const eventKey = process.env.INNGEST_EVENT_KEY;+ // Local dev: `crbn` points the database at its own Inngest server.+ if (!eventKey || process.env.NODE_ENV !== "production") return;+ const baseUrl =+ process.env.INNGEST_EVENT_API_BASE_URL ||+ process.env.INNGEST_BASE_URL ||+ "https://inn.gs/";+ // One call per registration: the REST client, not a pooled connection.+ const { error } = await getCarbonServiceRole().rpc(+ "set_inngest_event_config",+ { p_key: eventKey, p_base_url: baseUrl }+ );+ if (error) {+ logger.error(+ "Could not set the database's Inngest event key: database events will not reach Inngest",+ { error }+ );+ }+}++export async function action(args: ActionFunctionArgs) {wireWorkflowDispatch();+ if (args.request.method === "PUT") await setDatabaseEventConfig();// Every function and step arrives on this one route, so without these the// trace cannot say which function the time went to.const { searchParams } = new URL(args.request.url);diff --git a/apps/erp/app/routes/x+/acknowledge.tsx b/apps/erp/app/routes/x+/acknowledge.tsxindex e674fc12b0..ed7a0a6cd9 100644--- a/apps/erp/app/routes/x+/acknowledge.tsx+++ b/apps/erp/app/routes/x+/acknowledge.tsx@@ -232,10 +232,10 @@ export async function action({ request }: ActionFunctionArgs) {.single();if (readError) {- logger.error(- `[acknowledge] Failed to read flags for user ${userId}:`,- readError- );+ logger.error("[acknowledge] Failed to read flags for user {userId}", {+ userId,+ error: readError+ });return { success: false, message: "Failed to read user flags" };}@@ -249,8 +249,8 @@ export async function action({ request }: ActionFunctionArgs) {if (updateResult.error) {logger.error(- `[acknowledge] Failed to write flag "${flag}" for user ${userId}:`,- updateResult.error+ '[acknowledge] Failed to write flag "{flag}" for user {userId}',+ { flag, userId, error: updateResult.error });return { success: false, message: "Failed to update flag" };}diff --git a/apps/erp/app/routes/x+/sales-rfq+/$rfqId.convert.tsx b/apps/erp/app/routes/x+/sales-rfq+/$rfqId.convert.tsxindex 49b2bffe9a..e2a16a2dee 100644--- a/apps/erp/app/routes/x+/sales-rfq+/$rfqId.convert.tsx+++ b/apps/erp/app/routes/x+/sales-rfq+/$rfqId.convert.tsx@@ -93,54 +93,64 @@ export async function action({ request, params }: ActionFunctionArgs) {const quoteId = convert.data?.convertedId!;- // Seed `quoteLinePrice` rows for every new line. The convert function- // creates the `quoteLine` records (and, for Make to Order, kicks off- // `get-method itemToQuoteLine` to populate methods/materials), but it- // never writes any prices — so the new quote opens with empty pricing.- // The standard "add quote line" path in `$quoteId.new.tsx` calls these- // same helpers per methodType; mirror that here.- const newLines = await serviceRole- .from("quoteLine")- .select("id, methodType, quantity")- .eq("quoteId", quoteId);+ // Seed `quoteLinePrice` rows for the new lines that have none, so the quote+ // does not open with empty pricing. A Make to Order line whose method was+ // copied (`get-method itemToQuoteLine`) is priced already; seeding it again+ // was a duplicate insert the database refused. The "add quote line" path in+ // `$quoteId.new.tsx` calls these same helpers per methodType.+ const [newLines, priced] = await Promise.all([+ serviceRole+ .from("quoteLine")+ .select("id, methodType, quantity")+ .eq("quoteId", quoteId)+ .eq("companyId", companyId),+ serviceRole+ .from("quoteLinePrice")+ .select("quoteLineId")+ .eq("quoteId", quoteId)+ .eq("companyId", companyId)+ ]);+ const pricedLineIds = new Set(priced.data?.map((p) => p.quoteLineId));if (!newLines.error && newLines.data) {await Promise.all(- newLines.data.map((line) => {- const quantities = line.quantity ?? [1];- if (quantities.length === 0) return null;+ newLines.data+ .filter((line) => !pricedLineIds.has(line.id))+ .map((line) => {+ const quantities = line.quantity ?? [1];+ if (quantities.length === 0) return null;- switch (line.methodType) {- case "Make to Order":- return calculatePricesForQuantities(- serviceRole,- quoteId,- line.id,- quantities,- userId- );- case "Pull from Inventory":- return resolveQuoteLinePrices(- serviceRole,- companyId,- quoteId,- line.id,- quantities,- userId- );- case "Purchase to Order":- return resolvePurchaseToOrderPrices(- serviceRole,- companyId,- quoteId,- line.id,- quantities,- userId- );- default:- return null;- }- })+ switch (line.methodType) {+ case "Make to Order":+ return calculatePricesForQuantities(+ serviceRole,+ quoteId,+ line.id,+ quantities,+ userId+ );+ case "Pull from Inventory":+ return resolveQuoteLinePrices(+ serviceRole,+ companyId,+ quoteId,+ line.id,+ quantities,+ userId+ );+ case "Purchase to Order":+ return resolvePurchaseToOrderPrices(+ serviceRole,+ companyId,+ quoteId,+ line.id,+ quantities,+ userId+ );+ default:+ return null;+ }+ }));}diff --git a/apps/erp/app/utils/inngest-event-url.server.ts b/apps/erp/app/utils/inngest-event-url.server.tsdeleted file mode 100644index 344e44f01b..0000000000--- a/apps/erp/app/utils/inngest-event-url.server.ts+++ /dev/null@@ -1,72 +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 { sleep } from "@carbon/lib/async";-import { getLogger } from "@carbon/logger";-import { async } from "@carbon/utils";-import { sql } from "kysely";-import { getDatabaseClient } from "~/services/database.server";--const log = getLogger("erp", "inngest-event-url");--const RETRY_DELAY_MS = 60_000;--// Postgres sends its own events to Inngest (the event-queue wake-up, embeddings,-// job notifications) at a URL kept in its Vault. The app already holds the key-// that URL needs, so it keeps the key current on boot. It never changes an-// address that is already stored: the database may reach Inngest somewhere the-// app does not. With nothing stored, the URL starts from the app's own base.-async function setInngestEventConfig(- eventKey: string,- baseUrl: string,- attempts: number-) {- for (let attempt = 1; ; attempt++) {- try {- await sql`SELECT public.set_inngest_event_config(${eventKey}, ${baseUrl})`.execute(- getDatabaseClient()- );- return;- } catch (error) {- // The function arrives with a migration, which can land after the app.- if (attempt >= attempts) throw error;- await sleep(RETRY_DELAY_MS);- }- }-}--const globalForEventUrl = globalThis as typeof globalThis & {- __carbonInngestEventUrlStarted?: boolean;-};--/**- * Fire-and-forget, production only: local dev's database reaches Inngest at a- * docker-network address `crbn` sets. A serverless instance tries once (its- * next cold start tries again); a long-lived one retries for a few minutes.- */-export function scheduleInngestEventUrlSync() {- if (process.env.NODE_ENV !== "production") return;- if (globalForEventUrl.__carbonInngestEventUrlStarted) return;- globalForEventUrl.__carbonInngestEventUrlStarted = true;-- const eventKey = process.env.INNGEST_EVENT_KEY;- if (!eventKey) {- log.warn(- "INNGEST_EVENT_KEY is not set: database events will not reach Inngest"- );- return;- }- const baseUrl =- process.env.INNGEST_EVENT_API_BASE_URL ||- process.env.INNGEST_BASE_URL ||- "https://inn.gs/";- async.background(- () => setInngestEventConfig(eventKey, baseUrl, process.env.VERCEL ? 1 : 5),- (error) =>- log.error(- "Could not set the Inngest event key: database events will not reach Inngest until it is set",- { error }- )- );-}diff --git a/apps/mes/app/entry.server.tsx b/apps/mes/app/entry.server.tsxindex 86f4722fe6..b8afd08def 100644--- a/apps/mes/app/entry.server.tsx+++ b/apps/mes/app/entry.server.tsx@@ -3,6 +3,7 @@// including ports, remain AGPLv3; serving them over a network requires releasing their source.import { POSTHOG_API_HOST, SUPABASE_URL } from "@carbon/auth";+import { installFormBodyGuard } from "@carbon/auth/middleware/form-body.server";import {getNonce,setStrictContentSecurityPolicy@@ -19,6 +20,7 @@ import type { EntryContext, RouterContextProvider } from "react-router";import { isRouteErrorResponse } from "react-router";ensureLoggingConfigured();+installFormBodyGuard();// Vercel freezes an instance once its response is sent: keep it up until idle// database connections have closed and background work has finished.diff --git a/apps/mes/app/root.tsx b/apps/mes/app/root.tsxindex 0a807822d4..ac65fb1fe3 100644--- a/apps/mes/app/root.tsx+++ b/apps/mes/app/root.tsx@@ -10,6 +10,7 @@ import {flashMiddleware,flashResultContext} from "@carbon/auth/middleware/flash.server";+import { formBodyMiddleware } from "@carbon/auth/middleware/form-body.server";import { securityMiddleware } from "@carbon/auth/middleware/security.server";import { validator } from "@carbon/form";import { LocaleProvider, resolveLanguage } from "@carbon/locale";@@ -65,6 +66,7 @@ export const middleware = timedMiddleware({// First: the request scope (context, request id, access log).request: requestMiddleware,security: securityMiddleware,+ formBody: formBodyMiddleware,flash: flashMiddleware});export const clientMiddleware = [flashClientMiddleware];diff --git a/apps/starter/app/entry.server.tsx b/apps/starter/app/entry.server.tsxindex df24b3ee18..6b1cda5639 100644--- a/apps/starter/app/entry.server.tsx+++ b/apps/starter/app/entry.server.tsx@@ -3,6 +3,7 @@// including ports, remain AGPLv3; serving them over a network requires releasing their source.import { POSTHOG_API_HOST, SUPABASE_URL } from "@carbon/auth";+import { installFormBodyGuard } from "@carbon/auth/middleware/form-body.server";import {getNonce,setStrictContentSecurityPolicy@@ -12,6 +13,7 @@ import { handleRequest as vercelHandleRequest } from "@vercel/react-router/entryimport type { EntryContext, RouterContextProvider } from "react-router";ensureLoggingConfigured();+installFormBodyGuard();export const streamTimeout = 5_000;diff --git a/apps/starter/app/root.tsx b/apps/starter/app/root.tsxindex bb584d2df4..213aa4d973 100644--- a/apps/starter/app/root.tsx+++ b/apps/starter/app/root.tsx@@ -10,6 +10,7 @@ import {flashMiddleware,flashResultContext} from "@carbon/auth/middleware/flash.server";+import { formBodyMiddleware } from "@carbon/auth/middleware/form-body.server";import { securityMiddleware } from "@carbon/auth/middleware/security.server";import { validator } from "@carbon/form";import { requestIdMiddleware } from "@carbon/logger/middleware.server";@@ -46,6 +47,7 @@ import { getTheme } from "./services/theme.server";export const middleware = [requestIdMiddleware,securityMiddleware,+ formBodyMiddleware,flashMiddleware];export const clientMiddleware = [flashClientMiddleware];diff --git a/docs/app/api/authentication/page.tsx b/docs/app/api/authentication/page.tsxindex 8e39fea0df..9e294f6101 100644--- a/docs/app/api/authentication/page.tsx+++ b/docs/app/api/authentication/page.tsx@@ -18,7 +18,7 @@ import { ContentFooter } from "@/components/api/page-footer";import { pageSeo, SEO } from "@/lib/seo";export const metadata = pageSeo({- title: `${SEO.carbonApi.auth.title} — Carbon`,+ title: `${SEO.carbonApi.auth.title} | Carbon`,ogTitle: SEO.carbonApi.auth.title,description: SEO.carbonApi.auth.description,path: "/api/authentication",diff --git a/docs/app/api/data/[module]/[resource]/page.tsx b/docs/app/api/data/[module]/[resource]/page.tsxindex 667b173de8..42e60df34d 100644--- a/docs/app/api/data/[module]/[resource]/page.tsx+++ b/docs/app/api/data/[module]/[resource]/page.tsx@@ -22,7 +22,7 @@ export async function generateMetadata(props: Params): Promise<Metadata> {const { module, resource } = await props.params;const found = getResource(module, resource);return pageSeo({- title: found ? `${found.resource.name} — Carbon Data API` : "Carbon Data API",+ title: found ? `${found.resource.name} | Carbon Data API` : "Carbon Data API",ogTitle: found?.resource.name ?? "Carbon Data API",description: found?.resource.description,path: `/api/data/${module}/${resource}`,diff --git a/docs/app/api/data/authentication/page.tsx b/docs/app/api/data/authentication/page.tsxindex 57c45d9e50..d4b5452d93 100644--- a/docs/app/api/data/authentication/page.tsx+++ b/docs/app/api/data/authentication/page.tsx@@ -20,7 +20,7 @@ import { highlight } from "@/lib/highlight";import { pageSeo, SEO } from "@/lib/seo";export const metadata = pageSeo({- title: `${SEO.api.auth.title} — Carbon`,+ title: `${SEO.api.auth.title} | Carbon`,ogTitle: SEO.api.auth.title,description: SEO.api.auth.description,path: "/api/data/authentication",diff --git a/docs/app/api/data/page.tsx b/docs/app/api/data/page.tsxindex 670ef8eafe..f2e2f78834 100644--- a/docs/app/api/data/page.tsx+++ b/docs/app/api/data/page.tsx@@ -22,7 +22,7 @@ import { highlight } from "@/lib/highlight";import { pageSeo, SEO } from "@/lib/seo";export const metadata = pageSeo({- title: `${SEO.api.intro.title} — Carbon`,+ title: `${SEO.api.intro.title} | Carbon`,ogTitle: SEO.api.intro.title,description: SEO.api.intro.description,path: "/api/data",diff --git a/docs/app/api/mcp/page.tsx b/docs/app/api/mcp/page.tsxindex 81cf74d93a..bd9a65a7a5 100644--- a/docs/app/api/mcp/page.tsx+++ b/docs/app/api/mcp/page.tsx@@ -24,7 +24,7 @@ import { pageSeo, SEO } from "@/lib/seo";import { toolCounts } from "@/lib/tools-data";export const metadata = pageSeo({- title: `${SEO.carbonApi.mcp.title} — Carbon`,+ title: `${SEO.carbonApi.mcp.title} | Carbon`,ogTitle: SEO.carbonApi.mcp.title,description: SEO.carbonApi.mcp.description,path: "/api/mcp",diff --git a/docs/app/api/operations/[operation]/page.tsx b/docs/app/api/operations/[operation]/page.tsxindex 6f658abbfd..1f540a51ec 100644--- a/docs/app/api/operations/[operation]/page.tsx+++ b/docs/app/api/operations/[operation]/page.tsx@@ -37,12 +37,14 @@ export function generateStaticParams() {export async function generateMetadata(props: Params): Promise<Metadata> {const { operation } = await props.params;const found = getTool(operation);- // The full, callable name stays in the page title — that is the string a reader- // pastes into a search box — while the heading drops the module prefix the- // breadcrumb already carries.+ // "Purchasing - finalizePurchaseOrder": the underscored name is one unbreakable+ // word, which a link preview clips at both ends.+ const name = found+ ? `${found.module.name} - ${operationLabel(found.tool.name, found.module.slug)}`+ : "Carbon API";return pageSeo({- title: found ? `${found.tool.name} — Carbon API` : "Carbon API",- ogTitle: found?.tool.name ?? "Carbon API",+ title: found ? `${name} | Carbon API` : name,+ ogTitle: name,description: found?.tool.description,path: `/api/operations/${operation}`,eyebrow: found ? `Carbon API · ${found.module.name}` : "Carbon API"diff --git a/docs/app/api/page.tsx b/docs/app/api/page.tsxindex 7723cccfc2..573c3a7d9e 100644--- a/docs/app/api/page.tsx+++ b/docs/app/api/page.tsx@@ -19,7 +19,7 @@ import { pageSeo, SEO } from "@/lib/seo";import { toolCounts } from "@/lib/tools-data";export const metadata = pageSeo({- title: `${SEO.carbonApi.intro.title} — Carbon`,+ title: `${SEO.carbonApi.intro.title} | Carbon`,ogTitle: SEO.carbonApi.intro.title,description: SEO.carbonApi.intro.description,path: "/api",diff --git a/docs/app/api/sdks/page.tsx b/docs/app/api/sdks/page.tsxindex f3770eafd8..a3f88c4738 100644--- a/docs/app/api/sdks/page.tsx+++ b/docs/app/api/sdks/page.tsx@@ -24,7 +24,7 @@ import { pageSeo, SEO } from "@/lib/seo";import { toolCounts } from "@/lib/tools-data";export const metadata = pageSeo({- title: `${SEO.carbonApi.sdks.title} — Carbon`,+ title: `${SEO.carbonApi.sdks.title} | Carbon`,ogTitle: SEO.carbonApi.sdks.title,description: SEO.carbonApi.sdks.description,path: "/api/sdks",diff --git a/docs/app/docs/[[...slug]]/page.tsx b/docs/app/docs/[[...slug]]/page.tsxindex 38b10ec923..b09e1ad5d4 100644--- a/docs/app/docs/[[...slug]]/page.tsx+++ b/docs/app/docs/[[...slug]]/page.tsx@@ -86,7 +86,7 @@ export async function generateMetadata(props: Params): Promise<Metadata> {if (!page) notFound();return pageSeo({- title: `${page.data.title} — Carbon`,+ title: `${page.data.title} | Carbon`,ogTitle: page.data.title,description: page.data.description,path: page.url,diff --git a/docs/app/guides/[chapter]/page.tsx b/docs/app/guides/[chapter]/page.tsxindex cd53ba5da3..8cc787994a 100644--- a/docs/app/guides/[chapter]/page.tsx+++ b/docs/app/guides/[chapter]/page.tsx@@ -69,7 +69,7 @@ export async function generateMetadata(props: Params): Promise<Metadata> {// unfurls as "Make to order", not a stray line. Description is the guide's blurb.return pageSeo({title: page- ? `${page.data.label} ${page.data.flowName} — Carbon`+ ? `${page.data.label} ${page.data.flowName} | Carbon`: "Carbon Docs",ogTitle: page?.data.flowName ?? "Carbon Docs",description: pagediff --git a/docs/app/not-found.tsx b/docs/app/not-found.tsxindex b9b43b5502..84873eb23c 100644--- a/docs/app/not-found.tsx+++ b/docs/app/not-found.tsx@@ -7,7 +7,7 @@ import Link from "next/link";import { MainHeader } from "@/components/main-header";export const metadata: Metadata = {- title: "Page not found — Carbon",+ title: "Page not found | Carbon",description: "The page you’re looking for doesn’t exist or has moved."};diff --git a/docs/content/changelog/2026-10-03-server-side-posting.mdx b/docs/content/changelog/2026-10-03-server-side-posting.mdxdeleted file mode 100644index 46488785a1..0000000000--- a/docs/content/changelog/2026-10-03-server-side-posting.mdx+++ /dev/null@@ -1,33 +0,0 @@-----title: "Posting and job creation run in the app, not edge functions"-description: "Receipts, shipments, invoices and method copies now run in the Carbon app with fewer database round trips."-date: "2026-10-03"-tags: ["api", "self-hosting", "accounting"]-image: "/changelog/2026-10-03-server-side-posting.svg"------Posting a document or building a job from a method used to leave the app for a Supabase edge function, which then made its database calls one HTTP request at a time. That work now runs inside the Carbon app, on a direct database connection.--## One place for business logic--Posting [receipts](/docs/reference/receipts), [shipments](/docs/reference/shipments) and invoices, copying a method onto a [job](/docs/reference/jobs) or quote line, issuing material and recalculating job requirements all run in the app server.--The same operations stay available to integrations through the Carbon API with an [API key](/docs/building/api-keys). If you called a Supabase function URL directly, call the Carbon API instead.--## Less to run when self-hosting--A [self-hosted](/docs/platform/self-hosting) install no longer deploys a function per operation. One edge function remains, for search embeddings. The [architecture](/docs/building/architecture) page shows the new layout.--<Accordion title="Improvements">--- Copying a method onto a job or quote line makes about a third fewer database calls.-- Model thumbnails are rendered by the assembler service instead of a headless browser.--</Accordion>--<Accordion title="Fixes">--- A post refused because the document was already posted no longer sets it back to Draft.-- A failed receipt, shipment or purchase invoice post no longer leaves a fixed asset's cost or status changed.--</Accordion>diff --git a/docs/content/docs/platform/self-hosting/environment-variables.mdx b/docs/content/docs/platform/self-hosting/environment-variables.mdxindex c9c5152f76..67cd7260db 100644--- a/docs/content/docs/platform/self-hosting/environment-variables.mdx+++ b/docs/content/docs/platform/self-hosting/environment-variables.mdx@@ -65,7 +65,7 @@ Outside Vercel, set both `CLOUDFLARE_TURNSTILE_SITE_KEY` and `CLOUDFLARE_TURNSTI`INNGEST_SIGNING_KEY` and `INNGEST_EVENT_KEY` are optional only when `INNGEST_DEV=1`. In other environments, the application does not start if either is missing. A self-hosted Inngest server supplies both; the Docker with Caddy recipe generates them.</Callout>-The database also sends events to Inngest directly, for the event queue, search embeddings and job notifications. On startup the ERP stores the address for that in the database, built from `INNGEST_EVENT_KEY` and `INNGEST_BASE_URL`, so no extra step is needed.+The database also sends events to Inngest directly, for the event queue, search embeddings and job notifications. Whenever the ERP registers its functions with Inngest (on each deploy, and on each start of a long-running server), it also stores the address for that in the database, built from `INNGEST_EVENT_KEY` and `INNGEST_BASE_URL`, so no extra step is needed.If the database reaches Inngest at a different address than the application does, set that address once:@@ -73,7 +73,7 @@ If the database reaches Inngest at a different address than the application doesSELECT public.set_inngest_event_url('https://inngest.example.com/e/<event key>');```-The ERP keeps the address you set and only refreshes the event key in it, so a rotated key still reaches the database on the next startup.+The ERP keeps the address you set and only refreshes the event key in it, so a rotated key still reaches the database on the next deploy.## Email & billingdiff --git a/docs/public/changelog/2026-10-03-server-side-posting.svg b/docs/public/changelog/2026-10-03-server-side-posting.svgdeleted file mode 100644index ad55d95c16..0000000000--- a/docs/public/changelog/2026-10-03-server-side-posting.svg+++ /dev/null@@ -1,28 +0,0 @@-<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1200 675" fill="none">- <rect width="1200" height="675" fill="#09090B"/>- <rect x="390" y="158" width="420" height="360" rx="20" fill="#141518" stroke="#26272B" stroke-width="1.5"/>- <path d="M390 204 H810" stroke="#26272B" stroke-width="1.5"/>- <circle cx="418" cy="181" r="4" fill="#2E3036"/>- <circle cx="436" cy="181" r="4" fill="#2E3036"/>- <circle cx="454" cy="181" r="4" fill="#2E3036"/>- <circle cx="600" cy="300" r="34" fill="none" stroke="#00B0FF" stroke-width="1.5"/>- <circle cx="600" cy="300" r="62" fill="none" stroke="#26272B" stroke-width="1.5"/>- <circle cx="600" cy="300" r="90" fill="none" stroke="#26272B" stroke-width="1.5"/>- <circle cx="600" cy="300" r="10" fill="#00B0FF"/>- <rect x="444" y="404" width="312" height="10" rx="5" fill="#2E3036"/>- <rect x="444" y="436" width="236" height="10" rx="5" fill="#2E3036"/>- <rect x="444" y="470" width="312" height="30" rx="8" fill="#0B3C50"/>- <rect x="460" y="478" width="120" height="14" rx="7" fill="#00B0FF"/>- <rect x="160" y="236" width="190" height="204" rx="14" fill="#1B1C20" stroke="#26272B" stroke-width="1.5"/>- <rect x="186" y="270" width="94" height="10" rx="5" fill="#43454D"/>- <rect x="186" y="298" width="130" height="8" rx="4" fill="#2E3036"/>- <rect x="186" y="334" width="97" height="8" rx="4" fill="#212329"/>- <rect x="186" y="362" width="109" height="8" rx="4" fill="#212329"/>- <rect x="186" y="390" width="121" height="8" rx="4" fill="#212329"/>- <rect x="850" y="236" width="190" height="204" rx="14" fill="#1B1C20" stroke="#26272B" stroke-width="1.5"/>- <rect x="876" y="270" width="82" height="10" rx="5" fill="#43454D"/>- <rect x="876" y="298" width="126" height="8" rx="4" fill="#2E3036"/>- <rect x="876" y="334" width="111" height="8" rx="4" fill="#212329"/>- <rect x="876" y="362" width="109" height="8" rx="4" fill="#212329"/>- <rect x="876" y="390" width="100" height="8" rx="4" fill="#212329"/>-</svg>diff --git a/packages/auth/package.json b/packages/auth/package.jsonindex 073bdc6dbc..0b07d0a0cb 100644--- a/packages/auth/package.json+++ b/packages/auth/package.json@@ -26,6 +26,7 @@"./passkey.server": "./src/services/passkey.server.ts","./verification.server": "./src/services/verification.server.ts","./middleware/flash.server": "./src/middleware/flash.server.ts",+ "./middleware/form-body.server": "./src/middleware/form-body.server.ts","./middleware/security.server": "./src/middleware/security.server.ts","./middleware/flash.client": "./src/middleware/flash.client.ts"},diff --git a/packages/auth/src/middleware/form-body.server.test.ts b/packages/auth/src/middleware/form-body.server.test.tsnew file mode 100644index 0000000000..4b3d3cfafe--- /dev/null+++ b/packages/auth/src/middleware/form-body.server.test.ts@@ -0,0 +1,62 @@+// 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 { describe, expect, it } from "vitest";+import { installFormBodyGuard } from "./form-body.server";++installFormBodyGuard();++const post = (type: string | null, body: string) =>+ new Request("https://app.test/x/receipt/1/post", {+ method: "POST",+ headers: type ? { "content-type": type } : {},+ body+ });++async function thrown(request: Request): Promise<unknown> {+ try {+ await request.formData();+ } catch (error) {+ return error;+ }+ return undefined;+}++describe("installFormBodyGuard", () => {+ it("reads a form body as before", async () => {+ const form = await post(+ "application/x-www-form-urlencoded",+ "a=1&b=two"+ ).formData();+ expect(form.get("b")).toBe("two");+ });++ it("answers JSON sent to a form action with a 415 that says what to send", async () => {+ const error = await thrown(post("application/json", '{"a":1}'));+ expect(error).toBeInstanceOf(Response);+ const response = error as Response;+ expect(response.status).toBe(415);+ expect(response.headers.get("Accept-Post")).toBe(+ "application/x-www-form-urlencoded, multipart/form-data"+ );+ expect(response.headers.get("Link")).toBe(+ '</api/v1/openapi.json>; rel="service-desc"'+ );+ expect((await response.json()).hint).toContain("/api/v1/openapi.json");+ });++ it("answers a form body it cannot parse with a 400", async () => {+ const error = await thrown(+ post("multipart/form-data; boundary=x", "not a multipart body")+ );+ expect((error as Response).status).toBe(400);+ });++ it("leaves a body read twice as the server bug it is", async () => {+ const request = post("application/json", "{}");+ await request.text();+ const error = await thrown(request);+ expect(error).toBeInstanceOf(TypeError);+ });+});diff --git a/packages/auth/src/middleware/form-body.server.ts b/packages/auth/src/middleware/form-body.server.tsnew file mode 100644index 0000000000..31aec98e01--- /dev/null+++ b/packages/auth/src/middleware/form-body.server.ts@@ -0,0 +1,87 @@+// 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 type { MiddlewareFunction } from "react-router";++const FORM_TYPES = "application/x-www-form-urlencoded, multipart/form-data";+const API_DESCRIPTION = "/api/v1/openapi.json";+const HINT = {+ error: "This endpoint takes form data, not the content type that was sent.",+ accepts: FORM_TYPES.split(", "),+ hint: `This is a browser form endpoint. Programs should use the Carbon API, described at ${API_DESCRIPTION}.`+};+const HINT_HEADERS = {+ "Accept-Post": FORM_TYPES,+ Link: `<${API_DESCRIPTION}>; rel="service-desc"`+};++const INSTALLED = Symbol.for("carbon.formBodyGuard");++/**+ * Makes a form action answer a body it cannot read with a 4xx that says what to+ * send, instead of a 500.+ *+ * `request.formData()` throws a TypeError for a body that is not form data. The+ * browser never sends one, so the callers that hit this are scripts and agents+ * posting JSON to a page's form action. They get a 415 with `Accept-Post`+ * naming the accepted types and a `Link` to the API description, which is+ * where such a caller belongs. A form body that is malformed is a 400.+ *+ * Patched on the prototype, once per process, because React Router hands each+ * action its own `Request`: nothing set on the one a middleware sees reaches+ * it. The decision uses the request's own facts (was the body already read,+ * what is its content type), never the error's text, and a body read twice+ * (a server bug) is rethrown untouched.+ */+export function installFormBodyGuard(): void {+ const proto = Request.prototype as Request & { [INSTALLED]?: boolean };+ if (proto[INSTALLED]) return;+ proto[INSTALLED] = true;++ const readForm = Request.prototype.formData;+ Request.prototype.formData = async function formData(this: Request) {+ const alreadyRead = this.bodyUsed;+ try {+ return await readForm.call(this);+ } catch (error) {+ if (alreadyRead || !(error instanceof TypeError)) throw error;+ const type = this.headers.get("content-type") ?? "";+ const isForm = FORM_TYPES.split(", ").some((form) =>+ type.toLowerCase().startsWith(form)+ );+ if (isForm) {+ throw Response.json(+ { error: "The form body could not be read." },+ { status: 400 }+ );+ }+ throw Response.json(HINT, { status: 415, headers: HINT_HEADERS });+ }+ };+}++/**+ * Root middleware, paired with {@link installFormBodyGuard}. A response thrown+ * from an action of a page request is rendered as the HTML error page and loses+ * its headers, which tells a script nothing. Every 415 leaves with the+ * `Accept-Post` and `Link` hints, and as JSON unless the caller asked for HTML.+ * List it after the security middleware, so its response gets those headers.+ */+export const formBodyMiddleware: MiddlewareFunction<Response> = async (+ { request },+ next+) => {+ const response = await next();+ if (response.status !== 415) return response;+ const headers = new Headers(response.headers);+ for (const [name, value] of Object.entries(HINT_HEADERS)) {+ headers.set(name, value);+ }+ if ((request.headers.get("accept") ?? "").includes("text/html")) {+ return new Response(response.body, { status: 415, headers });+ }+ headers.delete("content-length");+ headers.set("content-type", "application/json");+ return new Response(JSON.stringify(HINT), { status: 415, headers });+};diff --git a/packages/auth/src/services/auth.server.ts b/packages/auth/src/services/auth.server.tsindex e857b45eef..c5b053068f 100644--- a/packages/auth/src/services/auth.server.ts+++ b/packages/auth/src/services/auth.server.ts@@ -10,6 +10,7 @@ import {import { redis } from "@carbon/kv";import { getLogger } from "@carbon/logger";import { oncePerRequest } from "@carbon/logger/middleware.server";+import { annotateRequestSpan } from "@carbon/logger/tracing.server";import { Edition, getClientIp, Plan } from "@carbon/utils";import type {AuthSession as SupabaseAuthSession,@@ -228,6 +229,8 @@ export async function requirePermissions(throw new Response("Invalid API key", { status: 401 });}if (company.data) {+ // Lets traces tell a script from a person at the keyboard.+ annotateRequestSpan({ "carbon.caller": "apiKey" });const apiKeyData = company.data as unknown as ApiKeyRecord;const companyId = apiKeyData.companyId;const companyGroupId = apiKeyData.companyGroupId;diff --git a/packages/checks/AGENTS.md b/packages/checks/AGENTS.mdindex a2ca093d81..8e03bdf7cc 100644--- a/packages/checks/AGENTS.md+++ b/packages/checks/AGENTS.md@@ -34,6 +34,7 @@ pnpm --filter @carbon/checks baseline # regenerate baseline (careful!)- **Conformance checks**: `noNumericPrecision`, `noLegacyRls`, `noDerivedPercentColumn`, `noRequiredColumnWithoutDefault`, `noViewWithoutInvoker` — scan SQL migrations for anti-patterns (`noDerivedPercentColumn` flags a `*Percent`/`*Rate` column `GENERATED ALWAYS AS` an expression that divides — a percentage is an entered fact, never a lossy echo of a rounded amount; `noViewWithoutInvoker` fails a `CREATE [OR REPLACE] VIEW` that does not state `security_invoker` — `CREATE OR REPLACE` replaces a view's options, so omitting the clause turns it into an owner-rights view that bypasses RLS for every PostgREST caller, and `openJobMaterialLines` leaked every company's rows that way. `security_invoker = false` passes, so an owner-rights view is a visible decision; materialized views are not matched. It reads one file's text, so an `EXECUTE format(...)` view or a later `ALTER VIEW … RESET` escapes it — the invariant below is the backstop); `noLocalTimezone` (in `SERVER_CHECKS`) scans server TS (`sources/server-files.ts`) for `getLocalTimeZone()`, UTC day-slicing, local date-parts of now (`new Date().getDay()` etc.), and `setHours(0,0,0,0)` midnights. Scanned files: MES services, `packages/jobs`, edge functions, `packages/database/src`, `packages/server-functions`, ERP module `.service.ts`/`.server.ts`, **plus both apps' route trees** — route modules are server AND client in one file, so `maskClientCode` blanks the browser half (default export, `clientLoader`/`clientAction`, PascalCase/`use*` declarations) while keeping loaders, actions, and the module-level helpers they call- **Authz ownership** (`CONFORMANCE_CHECKS` via `collectFindings`): `noAuthzDdlInMigrations(functions)` fails a migration newer than `20260927000000` that runs `CREATE`/`ALTER POLICY` on a public table (storage bucket policies stay allowed), or defines, alters or drops a managed function — policies live in `packages/database/src/authz/manifest.ts`, RLS helpers in `packages/database/src/authz/helpers/<name>.sql`, event-system functions in `packages/database/src/event-system/functions/<name>.sql` (`util.<name>.sql` outside `public`; checked from `20261002114954`, the migration that took them over), and `authz sync` applies all of them after migrations. The function list is read from those directories (`loadManagedFunctions`, one cutoff per directory in `MANAGED_FUNCTION_SETS`), so it cannot drift from the files. `DROP POLICY` is allowed (dropping a column a policy uses). Files starting with the `authz migration` header are exempt; `@carbon/database`'s `migration.test.ts` verifies their contents. Older migrations are history, so there are no baseline entries- **Forwarded headers** (own source, `REQUEST_HANDLING_ROOTS`: every app + auth, server-functions, ee, jobs, lib, utils, edge functions): `noRawForwardedHeaders` fails a raw read of `X-Forwarded-For/-Host/-Proto` or `X-Real-IP` outside `packages/utils/src/headers.ts` — use `getClientIp` / `getRequestProtocol` / `getRequestHost` / `getRequestOrigin`. Hand-rolled reads keyed login rate limits and ITAR audit records on the forgeable first `X-Forwarded-For` entry. No baseline entries+- **Error log messages** (same source as forwarded headers, `REQUEST_HANDLING_ROOTS`): `noInterpolatedErrorLog` fails `logger.error(`…${value}`)` / `.fatal` — the message is exported as a span attribute and is what errors group by, so values are named (`"Failed to upload {fileName}", { fileName }`). One baseline entry: the `packages/auth/src/utils/result.ts` pass-through, whose message is its caller's- **Numeric-precision checks** (in `TS_CHECKS`, over `sources/typescript.ts`): `noRawRounding` flags `Math.round/ceil/floor` and `.toFixed` on value-bearing numbers (use `round`/`scrapAllowance`/`applyRate`/`deriveRate` from `@carbon/utils`); `noInlineFractionDigits` flags `minimum`/`maximumFractionDigits` at a call site (pick a named kind from `@carbon/utils` `format.ts`, and `INPUT_FORMAT.*` for anything editable); `noUnroundedTrackedQuantity` flags inline unrounded arithmetic inside a `.updateTable("trackedEntity")` statement's `quantity:` set, and a raw `===`/`!==`/`<` compare against a `*[Ee]ntity*.quantity` read (use `round`/`settleQuantity` at the write, `isFullDraw` at a split gate — see `.claude/rules/traceability-model.md`). It is scoped to what one file can prove: a `quantity:` set from an already-rounded variable is unflaggable either way, and a compare of two locals is presumed rounded where they were defined. It has **no baseline entries** — the whole tree was fixed in the pass that added it, so any finding is new. `sources/typescript.ts` walks `apps/erp/app/{components,hooks,modules,routes}`, `apps/mes/app`, `packages/database/supabase/functions`, `packages/database/src`, `packages/{server-functions,ee,jobs}/src`, `packages/documents/src/{pdf,utils}` and the shared packages `packages/{utils,files,form,react,printing}/src` — tests (`*.test.ts(x)`, `*.spec.ts(x)`) and `*-test-fixture.ts` live-database fixtures excluded; a fixture is test support code, so it is held to the tests' rules, not shipped code's. The shared packages matter: `@carbon/utils` is where the standard's own helpers live, and `form`/`react` own the number inputs whose `formatOptions` are part of the storage round-trip. See `.claude/rules/numeric-precision.md`- **Missing audit columns** (built in `collectFindings`, not `TS_CHECKS`): `noMissingAuditColumn(columns)` fails a write that names `createdBy`/`updatedBy` on a relation that has no such column — PostgREST rejects the whole statement (`PGRST204 Could not find the 'createdBy' column of '<table>'`) and TypeScript does not flag it, because supabase-js widens the insert/upsert argument enough for the excess property to survive. That is how the `customerPartToItem` upsert in `sales-rfq.$rfqId.map-lines.ts` shipped with an unchecked result, so every RFQ-line → customer-part mapping silently failed. Roughly a third of Carbon's tables have no audit columns (favourites, link tables, the lean material lookups, `companySettings`) while golden rule 3 says to include them, so it is an easy assumption to carry into the wrong table. It takes the column map as an ARGUMENT — `loadDbTableColumns(root)` (`sources/db-columns.ts`) reads the generated `packages/database/src/types.ts` `Row:` blocks, so the I/O stays in `sources/` and `scan` stays pure, the same shape as `noAuthzDdlInMigrations(loadManagedFunctions(root))`. Scope is the LITERAL argument of the write (`.from`/`insertInto`/`updateTable` + `.insert`/`.upsert`/`.update`/`.values`/`.set`); a payload object spread into the row is invisible here, and for the MCP/API layer that same generated schema drives `withoutAbsentAuditColumns` in `scripts/lib/service-metadata.ts`. No baseline entries — the one hit was fixed when it was added- **Tenant-scoped Kysely writes** (in `TS_CHECKS`): `noUnscopedKyselyWrite` fails any `.updateTable(`/`.deleteFrom(` under `apps/erp/app/{modules,routes}`, `apps/mes/app`, `packages/jobs/src` or `packages/server-functions/src` whose statement (up to `.execute…(`/`.compile(`/`;`/`,` at depth 0, comments blanked) has no `.where("companyId", …)` / `.where("alias.companyId", …)`. Kysely is the superuser connection, and the public API hands a service's `db` param that same client, so the statement's own predicate is the only tenant boundary — ten drag-sort services (`updateQuoteLineOrder` and friends) were a reported cross-tenant write because they keyed on `id` alone. `company` is exempt (its `id` is the tenant); a `companyId` in `.set()` does not count; a link table with no `companyId` column passes by scoping through its parent in a subquery. `packages/ee` is out of scope (see the file header), and so are writes made through the `@carbon/database/rows` helpers (`updateRows` / `deleteRows`): the check only reads Kysely's own builders. One baseline entry: the `companyGroup` delete in `packages/jobs/.../scheduled/purge-company.ts` (a group has no `companyId` column; its `id` is the tenant-group key). Every other hit was fixed when it was addeddiff --git a/packages/checks/src/conformance/baseline.json b/packages/checks/src/conformance/baseline.jsonindex dfd5f1306a..b1040ae21e 100644--- a/packages/checks/src/conformance/baseline.json+++ b/packages/checks/src/conformance/baseline.json@@ -293,6 +293,7 @@"no-numeric-precision::20260604120000_invoice-totals-computed-in-views.sql::NUMERIC(10,2)","no-numeric-precision::20260604120000_invoice-totals-computed-in-views.sql::numeric(10,2)","no-numeric-precision::20260618171234_material-supersession.sql::NUMERIC(10,2)",+ "no-interpolated-error-log::packages/auth/src/utils/result.ts::log.error(`${","no-raw-rounding::apps/erp/app/components/CadModel.tsx::Math.round(artifacts.optimizedSize / 1024)","no-raw-rounding::apps/erp/app/components/CadModel.tsx::{convertKbToString(Math.ceil(file.size / 1024))}","no-raw-rounding::apps/erp/app/components/CadModel.tsx::{convertKbToString(Math.round(artifacts.size / 1024))}",diff --git a/packages/checks/src/conformance/no-interpolated-error-log.test.ts b/packages/checks/src/conformance/no-interpolated-error-log.test.tsnew file mode 100644index 0000000000..2391402a61--- /dev/null+++ b/packages/checks/src/conformance/no-interpolated-error-log.test.ts@@ -0,0 +1,29 @@+// 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 { describe, expect, it } from "vitest";+import { noInterpolatedErrorLog } from "./no-interpolated-error-log";++const scan = (contents: string) =>+ noInterpolatedErrorLog.scan("apps/erp/app/routes/x.tsx", contents);++describe("noInterpolatedErrorLog", () => {+ it("flags a value interpolated into an error message, on one line or several", () => {+ expect(+ scan(`logger.error(\`Failed to upload \${file.name}\`);+log.fatal(+ \`Lost \${id}\`,+ { error }+);`).map((v) => v.line)+ ).toEqual([1, 2]);+ });++ it("allows placeholders, plain templates and lower levels", () => {+ expect(+ scan(`logger.error("Failed to upload {fileName}", { fileName });+logger.error(\`Failed to upload\`);+logger.warn(\`Slow \${route}\`);`)+ ).toEqual([]);+ });+});diff --git a/packages/checks/src/conformance/no-interpolated-error-log.ts b/packages/checks/src/conformance/no-interpolated-error-log.tsnew file mode 100644index 0000000000..a3bcf2dfdd--- /dev/null+++ b/packages/checks/src/conformance/no-interpolated-error-log.ts@@ -0,0 +1,34 @@+// 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 type { ConformanceCheck, Violation } from "../check";++// An error log's message is exported as a span attribute and is what errors are+// grouped by. A value interpolated into it (`Failed to upload ${file.name}`)+// makes every occurrence a different error and puts the value in the trace.+// Name it instead: "Failed to upload {fileName}", { fileName: file.name }.+const INTERPOLATED = /\b\w*[lL]og(?:ger)?\.(?:error|fatal)\(\s*`[^`]*\$\{/g;++export const noInterpolatedErrorLog: ConformanceCheck = {+ id: "no-interpolated-error-log",+ description:+ "Error log messages name their values as {placeholders}, never interpolate them",+ provenance: {+ deprecates: "logger.error(`… ${value}`)",+ replacedBy: 'logger.error("… {value}", { value })',+ since: "2026-10-04"+ },+ scan(file: string, contents: string): Violation[] {+ return [...contents.matchAll(INTERPOLATED)].map((match) => {+ const line = contents.slice(0, match.index).split("\n").length;+ return {+ file,+ line,+ snippet: match[0].replace(/\s+/g, " "),+ message:+ 'Use a {placeholder} in the message and pass the value as a property: logger.error("Failed to upload {fileName}", { fileName })'+ };+ });+ }+};diff --git a/packages/checks/src/index.ts b/packages/checks/src/index.tsindex 2c6e513beb..c6509c2c20 100644--- a/packages/checks/src/index.ts+++ b/packages/checks/src/index.ts@@ -18,6 +18,7 @@ export { noDefaultOnEffects } from "./conformance/no-default-on-effects";export { noDerivedPercentColumn } from "./conformance/no-derived-percent-column";export { noInlineFractionDigits } from "./conformance/no-inline-fraction-digits";export { noIntegrationIdBranching } from "./conformance/no-integration-id-branching";+export { noInterpolatedErrorLog } from "./conformance/no-interpolated-error-log";export { noLegacyRls } from "./conformance/no-legacy-rls";export { noLocalTimezone } from "./conformance/no-local-timezone";export { noMissingAuditColumn } from "./conformance/no-missing-audit-column";diff --git a/packages/checks/src/run.ts b/packages/checks/src/run.tsindex 9f82d783a8..f0fa884784 100644--- a/packages/checks/src/run.ts+++ b/packages/checks/src/run.ts@@ -25,6 +25,7 @@ import { noDefaultOnEffects } from "./conformance/no-default-on-effects";import { noDerivedPercentColumn } from "./conformance/no-derived-percent-column";import { noInlineFractionDigits } from "./conformance/no-inline-fraction-digits";import { noIntegrationIdBranching } from "./conformance/no-integration-id-branching";+import { noInterpolatedErrorLog } from "./conformance/no-interpolated-error-log";import { noLegacyRls } from "./conformance/no-legacy-rls";import { noLocalTimezone } from "./conformance/no-local-timezone";import { noMissingAuditColumn } from "./conformance/no-missing-audit-column";@@ -156,7 +157,8 @@ export function collectFindings(root: string = repoRoot()): Finding[] {noMissingAuditColumn(loadDbTableColumns(root))]),...scanAll(loadTypescriptFiles(root, REQUEST_HANDLING_ROOTS), [- noRawForwardedHeaders+ noRawForwardedHeaders,+ noInterpolatedErrorLog]),...scanAll(loadTypescriptFiles(root, ROUTE_ROOTS), [indexRedirectBeforeLoadersdiff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.tsindex c399524d18..03d9829088 100644--- a/packages/database/src/swagger-docs-schema.ts+++ b/packages/database/src/swagger-docs-schema.ts@@ -34680,6 +34680,72 @@ export default {tags: ["intercompanyTransaction"]}},+ "/itemLedgerSnapshot": {+ get: {+ parameters: [+ {+ $ref: "#/parameters/rowFilter.itemLedgerSnapshot.itemId"+ },+ {+ $ref: "#/parameters/rowFilter.itemLedgerSnapshot.companyId"+ },+ {+ $ref: "#/parameters/rowFilter.itemLedgerSnapshot.locationId"+ },+ {+ $ref: "#/parameters/rowFilter.itemLedgerSnapshot.quantity"+ },+ {+ $ref: "#/parameters/rowFilter.itemLedgerSnapshot.consumed30"+ },+ {+ $ref: "#/parameters/rowFilter.itemLedgerSnapshot.consumed90"+ },+ {+ $ref: "#/parameters/rowFilter.itemLedgerSnapshot.storageUnitIds"+ },+ {+ $ref: "#/parameters/rowFilter.itemLedgerSnapshot.snapshotCutoff"+ },+ {+ $ref: "#/parameters/select"+ },+ {+ $ref: "#/parameters/order"+ },+ {+ $ref: "#/parameters/range"+ },+ {+ $ref: "#/parameters/rangeUnit"+ },+ {+ $ref: "#/parameters/offset"+ },+ {+ $ref: "#/parameters/limit"+ },+ {+ $ref: "#/parameters/preferCount"+ }+ ],+ responses: {+ "200": {+ description: "OK",+ schema: {+ items: {+ $ref: "#/definitions/itemLedgerSnapshot"+ },+ type: "array"+ }+ },+ "206": {+ description: "Partial Content"+ }+ },+ tags: ["itemLedgerSnapshot"]+ }+ },"/job": {get: {parameters: [@@ -47481,6 +47547,141 @@ export default {tags: ["storageUnit"]}},+ "/employeePin": {+ get: {+ parameters: [+ {+ $ref: "#/parameters/rowFilter.employeePin.employeeId"+ },+ {+ $ref: "#/parameters/rowFilter.employeePin.companyId"+ },+ {+ $ref: "#/parameters/rowFilter.employeePin.pinHash"+ },+ {+ $ref: "#/parameters/rowFilter.employeePin.updatedBy"+ },+ {+ $ref: "#/parameters/rowFilter.employeePin.updatedAt"+ },+ {+ $ref: "#/parameters/select"+ },+ {+ $ref: "#/parameters/order"+ },+ {+ $ref: "#/parameters/range"+ },+ {+ $ref: "#/parameters/rangeUnit"+ },+ {+ $ref: "#/parameters/offset"+ },+ {+ $ref: "#/parameters/limit"+ },+ {+ $ref: "#/parameters/preferCount"+ }+ ],+ responses: {+ "200": {+ description: "OK",+ schema: {+ items: {+ $ref: "#/definitions/employeePin"+ },+ type: "array"+ }+ },+ "206": {+ description: "Partial Content"+ }+ },+ tags: ["employeePin"]+ },+ post: {+ parameters: [+ {+ $ref: "#/parameters/body.employeePin"+ },+ {+ $ref: "#/parameters/select"+ },+ {+ $ref: "#/parameters/preferPost"+ }+ ],+ responses: {+ "201": {+ description: "Created"+ }+ },+ tags: ["employeePin"]+ },+ delete: {+ parameters: [+ {+ $ref: "#/parameters/rowFilter.employeePin.employeeId"+ },+ {+ $ref: "#/parameters/rowFilter.employeePin.companyId"+ },+ {+ $ref: "#/parameters/rowFilter.employeePin.pinHash"+ },+ {+ $ref: "#/parameters/rowFilter.employeePin.updatedBy"+ },+ {+ $ref: "#/parameters/rowFilter.employeePin.updatedAt"+ },+ {+ $ref: "#/parameters/preferReturn"+ }+ ],+ responses: {+ "204": {+ description: "No Content"+ }+ },+ tags: ["employeePin"]+ },+ patch: {+ parameters: [+ {+ $ref: "#/parameters/rowFilter.employeePin.employeeId"+ },+ {+ $ref: "#/parameters/rowFilter.employeePin.companyId"+ },+ {+ $ref: "#/parameters/rowFilter.employeePin.pinHash"+ },+ {+ $ref: "#/parameters/rowFilter.employeePin.updatedBy"+ },+ {+ $ref: "#/parameters/rowFilter.employeePin.updatedAt"+ },+ {+ $ref: "#/parameters/body.employeePin"+ },+ {+ $ref: "#/parameters/preferReturn"+ }+ ],+ responses: {+ "204": {+ description: "No Content"+ }+ },+ tags: ["employeePin"]+ }+ },"/implementationCheckState": {get: {parameters: [@@ -98217,6 +98418,45 @@ export default {tags: ["(rpc) get_quote_methods"]}},+ "/rpc/set_inngest_event_config": {+ post: {+ parameters: [+ {+ in: "body",+ name: "args",+ required: true,+ schema: {+ properties: {+ p_base_url: {+ format: "text",+ type: "string"+ },+ p_key: {+ format: "text",+ type: "string"+ }+ },+ required: ["p_key", "p_base_url"],+ type: "object"+ }+ },+ {+ $ref: "#/parameters/preferParams"+ }+ ],+ produces: [+ "application/json",+ "application/vnd.pgrst.object+json;nulls=stripped",+ "application/vnd.pgrst.object+json"+ ],+ responses: {+ "200": {+ description: "OK"+ }+ },+ tags: ["(rpc) set_inngest_event_config"]+ }+ },"/rpc/get_completion_jobs": {get: {parameters: [@@ -123502,6 +123742,50 @@ export default {},type: "object"},+ itemLedgerSnapshot: {+ properties: {+ itemId: {+ description:+ "Note:\nThis is a Foreign Key to `item.id`.<fk table='item' column='id'/>",+ format: "text",+ type: "string"+ },+ companyId: {+ description:+ "Note:\nThis is a Foreign Key to `company.id`.<fk table='company' column='id'/>",+ format: "text",+ type: "string"+ },+ locationId: {+ format: "text",+ type: "string"+ },+ quantity: {+ format: "numeric",+ type: "number"+ },+ consumed30: {+ format: "numeric",+ type: "number"+ },+ consumed90: {+ format: "numeric",+ type: "number"+ },+ storageUnitIds: {+ format: "text[]",+ items: {+ type: "string"+ },+ type: "array"+ },+ snapshotCutoff: {+ format: "timestamp with time zone",+ type: "string"+ }+ },+ type: "object"+ },job: {required: ["id",@@ -129502,6 +129786,38 @@ export default {},type: "object"},+ employeePin: {+ required: ["employeeId", "companyId", "pinHash", "updatedAt"],+ properties: {+ employeeId: {+ description: "Note:\nThis is a Primary Key.<pk/>",+ format: "text",+ type: "string"+ },+ companyId: {+ description:+ "Note:\nThis is a Primary Key.<pk/>\nThis is a Foreign Key to `company.id`.<fk table='company' column='id'/>",+ format: "text",+ type: "string"+ },+ pinHash: {+ format: "text",+ type: "string"+ },+ updatedBy: {+ description:+ "Note:\nThis is a Foreign Key to `user.id`.<fk table='user' column='id'/>",+ format: "text",+ type: "string"+ },+ updatedAt: {+ default: "now()",+ format: "timestamp with time zone",+ type: "string"+ }+ },+ type: "object"+ },implementationCheckState: {required: ["id",@@ -170364,6 +170680,63 @@ export default {in: "query",type: "string"},+ "body.itemLedgerSnapshot": {+ name: "itemLedgerSnapshot",+ description: "itemLedgerSnapshot",+ required: false,+ in: "body",+ schema: {+ $ref: "#/definitions/itemLedgerSnapshot"+ }+ },+ "rowFilter.itemLedgerSnapshot.itemId": {+ name: "itemId",+ required: false,+ in: "query",+ type: "string"+ },+ "rowFilter.itemLedgerSnapshot.companyId": {+ name: "companyId",+ required: false,+ in: "query",+ type: "string"+ },+ "rowFilter.itemLedgerSnapshot.locationId": {+ name: "locationId",+ required: false,+ in: "query",+ type: "string"+ },+ "rowFilter.itemLedgerSnapshot.quantity": {+ name: "quantity",+ required: false,+ in: "query",+ type: "string"+ },+ "rowFilter.itemLedgerSnapshot.consumed30": {+ name: "consumed30",+ required: false,+ in: "query",+ type: "string"+ },+ "rowFilter.itemLedgerSnapshot.consumed90": {+ name: "consumed90",+ required: false,+ in: "query",+ type: "string"+ },+ "rowFilter.itemLedgerSnapshot.storageUnitIds": {+ name: "storageUnitIds",+ required: false,+ in: "query",+ type: "string"+ },+ "rowFilter.itemLedgerSnapshot.snapshotCutoff": {+ name: "snapshotCutoff",+ required: false,+ in: "query",+ type: "string"+ },"body.job": {name: "job",description: "job",@@ -176876,6 +177249,45 @@ export default {in: "query",type: "string"},+ "body.employeePin": {+ name: "employeePin",+ description: "employeePin",+ required: false,+ in: "body",+ schema: {+ $ref: "#/definitions/employeePin"+ }+ },+ "rowFilter.employeePin.employeeId": {+ name: "employeeId",+ required: false,+ in: "query",+ type: "string"+ },+ "rowFilter.employeePin.companyId": {+ name: "companyId",+ required: false,+ in: "query",+ type: "string"+ },+ "rowFilter.employeePin.pinHash": {+ name: "pinHash",+ required: false,+ in: "query",+ type: "string"+ },+ "rowFilter.employeePin.updatedBy": {+ name: "updatedBy",+ required: false,+ in: "query",+ type: "string"+ },+ "rowFilter.employeePin.updatedAt": {+ name: "updatedAt",+ required: false,+ in: "query",+ type: "string"+ },"body.implementationCheckState": {name: "implementationCheckState",description: "implementationCheckState",diff --git a/packages/database/src/types.ts b/packages/database/src/types.tsindex b603e6ca65..64a5222692 100644--- a/packages/database/src/types.ts+++ b/packages/database/src/types.ts@@ -85178,6 +85178,10 @@ export type Database = {}Returns: undefined}+ set_inngest_event_config: {+ Args: { p_base_url: string; p_key: string }+ Returns: undefined+ }set_inngest_event_url: { Args: { p_url: string }; Returns: undefined }set_shelf_life_for_operation: {Args: {diff --git a/packages/database/supabase/migrations/20261003191518_inngest-event-url-single-writer.sql b/packages/database/supabase/migrations/20261003191518_inngest-event-url-single-writer.sqlnew file mode 100644index 0000000000..641ad61946--- /dev/null+++ b/packages/database/supabase/migrations/20261003191518_inngest-event-url-single-writer.sql@@ -0,0 +1,73 @@+-- The Inngest event URL has several writers (the app when it registers with+-- Inngest, the deploy, an operator), and two of them can run at once. Both+-- setters now take one transaction-scoped advisory lock, so a second caller+-- waits and then sees what the first stored: on a database with no URL yet it+-- no longer fails on the Vault's unique name. Bodies copied forward from+-- 20261003134512 and 20261003142107.+CREATE OR REPLACE FUNCTION public.set_inngest_event_url(p_url TEXT)+RETURNS VOID+LANGUAGE plpgsql+SECURITY DEFINER+SET search_path = public, vault+AS $$+DECLARE+ secret_id UUID;+ current_url TEXT;+BEGIN+ IF current_setting('role', true) IN ('anon', 'authenticated') THEN+ RAISE EXCEPTION 'Not authorized' USING ERRCODE = 'insufficient_privilege';+ END IF;++ PERFORM pg_advisory_xact_lock(hashtext('inngest_event_url'));++ SELECT id, decrypted_secret INTO secret_id, current_url+ FROM vault.decrypted_secrets WHERE name = 'inngest_event_url';+ IF secret_id IS NULL THEN+ PERFORM vault.create_secret(p_url, 'inngest_event_url');+ ELSIF current_url IS DISTINCT FROM p_url THEN+ PERFORM vault.update_secret(secret_id, p_url);+ END IF;+END;+$$;++CREATE OR REPLACE FUNCTION public.set_inngest_event_config(p_key TEXT, p_base_url TEXT)+RETURNS VOID+LANGUAGE plpgsql+SECURITY DEFINER+SET search_path = public, vault+AS $$+DECLARE+ secret_id UUID;+ current_url TEXT;+ new_url TEXT;+BEGIN+ IF current_setting('role', true) IN ('anon', 'authenticated') THEN+ RAISE EXCEPTION 'Not authorized' USING ERRCODE = 'insufficient_privilege';+ END IF;+ IF COALESCE(p_key, '') = '' THEN+ RAISE EXCEPTION 'An event key is required';+ END IF;++ PERFORM pg_advisory_xact_lock(hashtext('inngest_event_url'));++ SELECT id, decrypted_secret INTO secret_id, current_url+ FROM vault.decrypted_secrets WHERE name = 'inngest_event_url';++ IF secret_id IS NULL THEN+ PERFORM vault.create_secret(+ rtrim(p_base_url, '/') || '/e/' || p_key,+ 'inngest_event_url'+ );+ RETURN;+ END IF;++ -- Only a URL of the expected shape (…/e/<key>) is rewritten.+ IF current_url !~ '/e/[^/]*$' THEN+ RETURN;+ END IF;+ new_url := substring(current_url FROM '^(.*/e/)[^/]*$') || p_key;+ IF new_url IS DISTINCT FROM current_url THEN+ PERFORM vault.update_secret(secret_id, new_url);+ END IF;+END;+$$;diff --git a/packages/ee/src/paperless-parts/lib/lib.ts b/packages/ee/src/paperless-parts/lib/lib.tsindex 510b3fd91b..4a190d0141 100644--- a/packages/ee/src/paperless-parts/lib/lib.ts+++ b/packages/ee/src/paperless-parts/lib/lib.ts@@ -102,9 +102,10 @@ async function downloadFileFromUrl(const response = await fetch(url);if (!response.ok) {- logger.error(- `Failed to download file from ${url}: ${response.statusText}`- );+ logger.error("Failed to download file from {url}: {responseStatusText}", {+ url,+ responseStatusText: response.statusText+ });return null;}@@ -326,7 +327,7 @@ async function getCachedMaterialProperties(}if (!substance.data) {- logger.error(`Substance not found for ID: ${substanceId}`);+ logger.error("Substance not found for ID: {substanceId}", { substanceId });return null;}@@ -811,10 +812,10 @@ async function uploadModelFile(.single();if (existingItem.error) {- logger.error(- `Failed to read item ${itemId} before model upload:`,- existingItem.error- );+ logger.error("Failed to read item {itemId} before model upload", {+ itemId,+ error: existingItem.error+ });return false;}@@ -852,12 +853,17 @@ async function uploadModelFile(});if (modelUpload.error) {- logger.error(`Failed to upload model ${file.name}:`, modelUpload.error);+ logger.error("Failed to upload model {fileName}", {+ fileName: file.name,+ error: modelUpload.error+ });return false;}if (!modelUpload.data?.path) {- logger.error(`No path returned for uploaded model ${file.name}`);+ logger.error("No path returned for uploaded model {fileName}", {+ fileName: file.name+ });return false;}@@ -872,10 +878,10 @@ async function uploadModelFile(});if (modelRecord.error) {- logger.error(- `Failed to create model record for ${file.name}:`,- modelRecord.error- );+ logger.error("Failed to create model record for {fileName}", {+ fileName: file.name,+ error: modelRecord.error+ });return false;}@@ -950,7 +956,9 @@ async function uploadFileToItem(}if (!fileUpload.data?.path) {- logger.error(`No path returned for uploaded file ${file.name}`);+ logger.error("No path returned for uploaded file {fileName}", {+ fileName: file.name+ });return false;}@@ -1006,7 +1014,8 @@ async function processSupportingFiles(if (!file) {logger.error(- `Failed to download supporting file: ${supportingFile.filename}`+ "Failed to download supporting file: {supportingFileFilename}",+ { supportingFileFilename: supportingFile.filename });continue;}@@ -1025,9 +1034,9 @@ async function processSupportingFiles(if (uploadSuccess) {hasModel = true;} else {- logger.error(- `Failed to upload CAD model: ${supportingFile.filename}`- );+ logger.error("Failed to upload CAD model: {supportingFileFilename}", {+ supportingFileFilename: supportingFile.filename+ });}} else {logger.info(`Processing ${file.name} as document`);@@ -1041,7 +1050,8 @@ async function processSupportingFiles(if (!uploadSuccess) {logger.error(- `Failed to upload supporting file: ${supportingFile.filename}`+ "Failed to upload supporting file: {supportingFileFilename}",+ { supportingFileFilename: supportingFile.filename });}}@@ -1268,8 +1278,8 @@ export async function getCustomerIdAndContactId(// If we still haven't found an account, log the error but continue without throwingif (!existingPaperlessAccount) {logger.error(- `Could not create or find account in Paperless Parts for: ${customerName}. Error:`,- newPaperlessPartsAccount.error+ "Could not create or find account in Paperless Parts for: {customerName}",+ { customerName, error: newPaperlessPartsAccount.error });// Use a fallback approach - we'll create the customer in Carbon without a Paperless Parts account IDpaperlessPartsAccountId = 0; // Use 0 as a fallback to indicate no Paperless Parts account@@ -1957,7 +1967,9 @@ async function downloadAndUploadThumbnail(// Download the thumbnail from the URLconst response = await fetch(thumbnailUrl);if (!response.ok) {- logger.error(`Failed to download thumbnail: ${response.statusText}`);+ logger.error("Failed to download thumbnail: {responseStatusText}", {+ responseStatusText: response.statusText+ });return null;}@@ -2243,10 +2255,10 @@ export async function createPartFromComponent(.single();if (itemCostUpdate.error) {- logger.error(- `Failed to update itemCost for existing ${partId}:`,- itemCostUpdate.error- );+ logger.error("Failed to update itemCost for existing {partId}", {+ partId,+ error: itemCostUpdate.error+ });// Don't throw here, just log the error and continue} else {logger.info(`Successfully updated itemCost for existing ${partId}`);@@ -2319,10 +2331,10 @@ export async function createPartFromComponent(.single();if (itemCostUpdate.error) {- logger.error(- `Failed to update itemCost for ${partId}:`,- itemCostUpdate.error- );+ logger.error("Failed to update itemCost for {partId}", {+ partId,+ error: itemCostUpdate.error+ });// Don't throw here, just log the error and continue} else {logger.info(`Successfully updated itemCost for ${partId}`);@@ -2525,8 +2537,11 @@ export async function getOrCreatePart(if (itemCostUpdate.error) {logger.error(- `Failed to update itemCost for existing (external ID) ${existingPart.partId}:`,- itemCostUpdate.error+ "Failed to update itemCost for existing (external ID) {existingPartPartId}",+ {+ existingPartPartId: existingPart.partId,+ error: itemCostUpdate.error+ });// Don't throw here, just log the error and continue} else {@@ -2758,8 +2773,8 @@ export async function insertOrderLines(if (lineResult.error) {logger.error(- `Failed to insert sales order line for component ${component.part_uuid}:`,- lineResult.error+ "Failed to insert sales order line for component {componentPart_uuid}",+ { componentPart_uuid: component.part_uuid, error: lineResult.error });continue;}@@ -3010,8 +3025,8 @@ export async function insertQuoteLines(if (lineResult.error) {logger.error(- `Failed to insert quote line for component ${component.part_uuid}:`,- lineResult.error+ "Failed to insert quote line for component {componentPart_uuid}",+ { componentPart_uuid: component.part_uuid, error: lineResult.error });continue;}@@ -3042,8 +3057,11 @@ export async function insertQuoteLines(if (priceResult.error) {logger.error(- `Failed to insert quote line prices for component ${component.part_uuid}:`,- priceResult.error+ "Failed to insert quote line prices for component {componentPart_uuid}",+ {+ componentPart_uuid: component.part_uuid,+ error: priceResult.error+ });}}@@ -3132,8 +3150,8 @@ export async function insertQuoteLines(if (opResult.error) {logger.error(- `Failed to insert quote operation ${operationName}:`,- opResult.error+ "Failed to insert quote operation {operationName}",+ { operationName, error: opResult.error });}}diff --git a/packages/jobs/src/inngest/functions/events/audit.ts b/packages/jobs/src/inngest/functions/events/audit.tsindex 17184259a8..c0e55d651a 100644--- a/packages/jobs/src/inngest/functions/events/audit.ts+++ b/packages/jobs/src/inngest/functions/events/audit.ts@@ -468,7 +468,8 @@ async function applyFkSnapshots(// A rejected query (e.g. a tenancy filter on a table without// companyId) silently degrades the affected diffs to raw ids —// make that visible.- log.error(`FK snapshot lookup failed for table "${table}"`, {+ log.error('FK snapshot lookup failed for table "{table}"', {+ table,error,tenantScoped});@@ -476,7 +477,8 @@ async function applyFkSnapshots(}return data as Array<Record<string, unknown>>;} catch (err) {- log.error(`FK snapshot lookup failed for table "${table}"`, {+ log.error('FK snapshot lookup failed for table "{table}"', {+ table,error: err});return null;diff --git a/packages/jobs/src/inngest/functions/events/embedding.ts b/packages/jobs/src/inngest/functions/events/embedding.tsindex 67ae9b8354..b5b5cbcbdd 100644--- a/packages/jobs/src/inngest/functions/events/embedding.ts+++ b/packages/jobs/src/inngest/functions/events/embedding.ts@@ -353,7 +353,11 @@ export const embeddingFunction = inngest.createFunction(results.processed = embedded.length;results.failed = failed.length;for (const { record, error } of failed) {- logger.error(`Embedding ${record.table} ${record.id} failed: ${error}`);+ logger.error("Embedding {recordTable} {recordId} failed: {error}", {+ recordTable: record.table,+ recordId: record.id,+ error+ });}logger.info(diff --git a/packages/jobs/src/inngest/functions/events/sync.ts b/packages/jobs/src/inngest/functions/events/sync.tsindex 8a25fc5bf7..bee862a1ba 100644--- a/packages/jobs/src/inngest/functions/events/sync.ts+++ b/packages/jobs/src/inngest/functions/events/sync.ts@@ -218,7 +218,8 @@ export const syncFunction = inngest.createFunction(refs});} catch (error) {- logger.error(`Failed to reconcile sync events for ${key}`, {+ logger.error("Failed to reconcile sync events for {key}", {+ key,error});stepSummary.aborted = true;diff --git a/packages/jobs/src/inngest/functions/integrations/paperless-parts.ts b/packages/jobs/src/inngest/functions/integrations/paperless-parts.tsindex ef1c165fd7..e713ea50f9 100644--- a/packages/jobs/src/inngest/functions/integrations/paperless-parts.ts+++ b/packages/jobs/src/inngest/functions/integrations/paperless-parts.ts@@ -764,7 +764,9 @@ export const paperlessPartsFunction = inngest.createFunction(};break;default:- logger.error(`Unsupported event type: ${payload.payload}`);+ logger.error("Unsupported event type: {payload}", {+ payload: payload.payload+ });result = {success: false,message: `Unsupported event type`@@ -775,9 +777,10 @@ export const paperlessPartsFunction = inngest.createFunction(if (result.success) {logger.info(`Successfully processed ${payload.payload.type} event`);} else {- logger.error(- `Failed to process ${payload.payload.type} event: ${result.message}`- );+ logger.error("Failed to process {payloadType} event: {resultMessage}", {+ payloadType: payload.payload.type,+ resultMessage: result.message+ });}return result;diff --git a/packages/jobs/src/inngest/functions/integrations/slack-document-sync.ts b/packages/jobs/src/inngest/functions/integrations/slack-document-sync.tsindex 4e5453320f..299c57123e 100644--- a/packages/jobs/src/inngest/functions/integrations/slack-document-sync.ts+++ b/packages/jobs/src/inngest/functions/integrations/slack-document-sync.ts@@ -82,7 +82,10 @@ export const slackDocumentCreatedFunction = inngest.createFunction(return { success: true };} catch (error) {- logger.error(`Error posting ${documentType} to Slack`, { error });+ logger.error("Error posting {documentType} to Slack", {+ documentType,+ error+ });throw error;}}@@ -177,7 +180,8 @@ export const slackDocumentStatusUpdateFunction = inngest.createFunction(return { success: true };} catch (error) {- logger.error(`Error posting ${documentType} status update to Slack`, {+ logger.error("Error posting {documentType} status update to Slack", {+ documentType,error});throw error;@@ -277,7 +281,8 @@ export const slackDocumentTaskUpdateFunction = inngest.createFunction(return { success: true };} catch (error) {- logger.error(`Error posting ${documentType} task update to Slack`, {+ logger.error("Error posting {documentType} task update to Slack", {+ documentType,error});throw error;@@ -371,7 +376,8 @@ export const slackDocumentAssignmentUpdateFunction = inngest.createFunction(return { success: true };} catch (error) {- logger.error(`Error posting ${documentType} assignment update to Slack`, {+ logger.error("Error posting {documentType} assignment update to Slack", {+ documentType,error});throw error;diff --git a/packages/jobs/src/inngest/functions/integrations/stripe-connect-pull-sweep.ts b/packages/jobs/src/inngest/functions/integrations/stripe-connect-pull-sweep.tsindex e5b15459a4..dfa716ec0d 100644--- a/packages/jobs/src/inngest/functions/integrations/stripe-connect-pull-sweep.ts+++ b/packages/jobs/src/inngest/functions/integrations/stripe-connect-pull-sweep.ts@@ -220,8 +220,8 @@ export const stripeConnectPullSweepFunction = inngest.createFunction(summary.errors++;anyError = true;logger.error(- `[stripe-connect-pull-sweep] ${companyId}: error processing ${invoice.id}`,- { error: err }+ "[stripe-connect-pull-sweep] {companyId}: error processing {invoiceId}",+ { companyId, invoiceId: invoice.id, error: err });}}@@ -244,8 +244,8 @@ export const stripeConnectPullSweepFunction = inngest.createFunction();} catch (err) {logger.error(- `[stripe-connect-pull-sweep] ${target.companyId}: sweep failed`,- { error: err }+ "[stripe-connect-pull-sweep] {targetCompanyId}: sweep failed",+ { targetCompanyId: target.companyId, error: err });result = {companyId: target.companyId,diff --git a/packages/jobs/src/inngest/functions/integrations/timecard-auto-close.ts b/packages/jobs/src/inngest/functions/integrations/timecard-auto-close.tsindex 6559d9a8eb..02ba61142b 100644--- a/packages/jobs/src/inngest/functions/integrations/timecard-auto-close.ts+++ b/packages/jobs/src/inngest/functions/integrations/timecard-auto-close.ts@@ -45,8 +45,8 @@ export const timeCardAutoCloseFunction = inngest.createFunction(if (entriesError) {logger.error(- `Failed to fetch open entries for company ${company.id}`,- { error: entriesError }+ "Failed to fetch open entries for company {companyId}",+ { companyId: company.id, error: entriesError });continue;}@@ -116,7 +116,8 @@ export const timeCardAutoCloseFunction = inngest.createFunction(.eq("id", entry.id);if (updateError) {- logger.error(`Failed to auto-close entry ${entry.id}`, {+ logger.error("Failed to auto-close entry {entryId}", {+ entryId: entry.id,error: updateError});} else {diff --git a/packages/jobs/src/inngest/functions/scheduled/audit-archive.ts b/packages/jobs/src/inngest/functions/scheduled/audit-archive.tsindex 3e3bac299d..e0f33c0874 100644--- a/packages/jobs/src/inngest/functions/scheduled/audit-archive.ts+++ b/packages/jobs/src/inngest/functions/scheduled/audit-archive.ts@@ -121,7 +121,8 @@ async function archiveCompanyLogs();if (deleteError) {- log.error(`Failed to delete archived records for ${companyId}`, {+ log.error("Failed to delete archived records for {companyId}", {+ companyId,error: deleteError});// Don't throw - archive was successful, just couldn't clean up@@ -179,7 +180,8 @@ export const auditArchiveFunction = inngest.createFunction(results.recordsArchived += archived.recordsArchived;results.recordsDeleted += archived.recordsDeleted;} catch (error) {- logger.error(`Failed to archive logs for company ${company.id}`, {+ logger.error("Failed to archive logs for company {companyId}", {+ companyId: company.id,error});results.errors++;diff --git a/packages/jobs/src/inngest/functions/scheduled/mrp.ts b/packages/jobs/src/inngest/functions/scheduled/mrp.tsindex b23fd4e9ee..cb23f478c9 100644--- a/packages/jobs/src/inngest/functions/scheduled/mrp.ts+++ b/packages/jobs/src/inngest/functions/scheduled/mrp.ts@@ -114,7 +114,8 @@ export const mrpFunction = inngest.createFunction(logger.info(`Successfully ran MRP for company ${company.name}`);});} catch (error) {- logger.error(`Failed to run MRP for company ${company.name}`, {+ logger.error("Failed to run MRP for company {companyName}", {+ companyName: company.name,error});failed.push(company.id);diff --git a/packages/jobs/src/inngest/functions/scheduled/weekly.ts b/packages/jobs/src/inngest/functions/scheduled/weekly.tsindex 5cc09e9eb7..83465ea68c 100644--- a/packages/jobs/src/inngest/functions/scheduled/weekly.ts+++ b/packages/jobs/src/inngest/functions/scheduled/weekly.ts@@ -479,7 +479,8 @@ export const weeklyFunction = inngest.createFunction();if (failures.length > 0) {for (const failure of failures) {- logger.error(`Failed to remove company ${failure.part}`, {+ logger.error("Failed to remove company {failurePart}", {+ failurePart: failure.part,...company,error: failure.error});diff --git a/packages/jobs/src/inngest/functions/tasks/print-job/index.tsx b/packages/jobs/src/inngest/functions/tasks/print-job/index.tsxindex 70c4797bc0..e012082161 100644--- a/packages/jobs/src/inngest/functions/tasks/print-job/index.tsx+++ b/packages/jobs/src/inngest/functions/tasks/print-job/index.tsx@@ -313,7 +313,7 @@ async function processDocumentType(renderError instanceof Error? renderError.message: String(renderError);- log.error(`Rendering failed for job ${jobId}`, { error: message });+ log.error("Rendering failed for job {jobId}", { jobId, error: message });await updatePrintJobStatus(client, jobId, companyId, "failed", {error: `Rendering failed: ${message}`});diff --git a/packages/jobs/src/inngest/functions/tasks/recalculate.ts b/packages/jobs/src/inngest/functions/tasks/recalculate.tsindex 70d83853a1..65ed8c6a2b 100644--- a/packages/jobs/src/inngest/functions/tasks/recalculate.ts+++ b/packages/jobs/src/inngest/functions/tasks/recalculate.ts@@ -61,7 +61,9 @@ export const recalculateFunction = inngest.createFunction(if (result.success) {logger.info(`Success ${payload.id}`);} else {- logger.error(`Recalculation ${payload.type} failed for ${payload.id}`, {+ logger.error("Recalculation {payloadType} failed for {payloadId}", {+ payloadType: payload.type,+ payloadId: payload.id,message: result.message});}diff --git a/packages/jobs/src/inngest/functions/tasks/update-permissions.ts b/packages/jobs/src/inngest/functions/tasks/update-permissions.tsindex d8b6c2b308..6fab6091d6 100644--- a/packages/jobs/src/inngest/functions/tasks/update-permissions.ts+++ b/packages/jobs/src/inngest/functions/tasks/update-permissions.ts@@ -26,7 +26,8 @@ export const updatePermissionsFunction = inngest.createFunction(if (success) {logger.info(`Permission Update for ${payload.id} succeeded`);} else {- logger.error(`Permission Update for ${payload.id} failed`, {+ logger.error("Permission Update for {payloadId} failed", {+ payloadId: payload.id,message});}diff --git a/packages/jobs/src/inngest/functions/tasks/user-admin.ts b/packages/jobs/src/inngest/functions/tasks/user-admin.tsindex a652e7fe95..086a5cd75a 100644--- a/packages/jobs/src/inngest/functions/tasks/user-admin.ts+++ b/packages/jobs/src/inngest/functions/tasks/user-admin.ts@@ -135,7 +135,9 @@ export const userAdminFunction = inngest.createFunction(if (result.success) {logger.info(`Success ${payload.id}`);} else {- logger.error(`Admin action ${payload.type} failed for ${payload.id}`, {+ logger.error("Admin action {payloadType} failed for {payloadId}", {+ payloadType: payload.type,+ payloadId: payload.id,message: result.message});}diff --git a/packages/jobs/src/inngest/functions/workflows/run.ts b/packages/jobs/src/inngest/functions/workflows/run.tsindex c7e4c870a8..86d1793449 100644--- a/packages/jobs/src/inngest/functions/workflows/run.ts+++ b/packages/jobs/src/inngest/functions/workflows/run.ts@@ -32,7 +32,10 @@ export const workflowRunFunction = inngest.createFunction(idempotency: "event.data.runId",onFailure: async ({ event, logger }) => {const { runId, companyId } = event.data.event.data;- logger.error(`Workflow run ${runId} failed`, event.data.error);+ logger.error("Workflow run {runId} failed", {+ runId,+ error: event.data.error+ });await failCrashedRun(getJobDatabaseClient(),diff --git a/packages/logger/package.json b/packages/logger/package.jsonindex 5e58cdaf86..4a8c3efd50 100644--- a/packages/logger/package.json+++ b/packages/logger/package.json@@ -35,6 +35,7 @@"./config.client": "./src/config.client.ts","./middleware.server": "./src/middleware.server.ts","./tracing.server": "./src/tracing.server.ts",+ "./span": "./src/span.ts","./inngest": "./src/inngest.ts"},"private": true,diff --git a/packages/logger/src/config.server.ts b/packages/logger/src/config.server.tsindex c9f3231524..e901ec90d2 100644--- a/packages/logger/src/config.server.ts+++ b/packages/logger/src/config.server.ts@@ -6,9 +6,11 @@ import { AsyncLocalStorage } from "node:async_hooks";import {configureSync,getConsoleSink,- getJsonLinesFormatter+ getJsonLinesFormatter,+ type Sink} from "@logtape/logtape";import { redactByField } from "@logtape/redaction";+import { SpanStatusCode, trace } from "@opentelemetry/api";import { isAbandonedRead } from "./context.server";import { devFormatter } from "./dev-formatter";import { readEnv } from "./env";@@ -55,6 +57,33 @@ export type ConfigureLoggingOptions = {* Idempotent: a `globalThis` flag survives Vite SSR module re-evaluation, and* `reset: true` means a re-eval race reconfigures instead of throwing.*/+/**+ * Every error-level log also becomes a span in the trace of the request that+ * logged it, so the trace store can count and alert on them. Console logs stay+ * on the host; without this an error the app handled (a refusal it turned into+ * a flash message, a failed background step) was visible nowhere searchable.+ * Only the message template is recorded (`"Failed to post {document}"`), never+ * the values in it or the record's properties, so one message groups as one+ * error whatever it was about. That holds only while messages name their+ * values as placeholders: the `no-interpolated-error-log` check enforces it.+ */+const errorSpans: Sink = (record) => {+ if (record.level !== "error" && record.level !== "fatal") return;+ const message = (+ typeof record.rawMessage === "string"+ ? record.rawMessage+ : record.rawMessage.join("{}")+ ).slice(0, 300);+ const span = trace.getTracer("carbon").startSpan(`log ${record.level}`, {+ attributes: {+ "log.category": record.category.join("."),+ "log.message": message+ }+ });+ span.setStatus({ code: SpanStatusCode.ERROR, message });+ span.end();+};+export function ensureLoggingConfigured(options: ConfigureLoggingOptions = {}): void {@@ -87,7 +116,7 @@ export function ensureLoggingConfigured(configureSync({reset: true,contextLocalStorage: new AsyncLocalStorage(),- sinks: { console: sink, httpConsole: httpSink },+ sinks: { console: sink, httpConsole: httpSink, errorSpans },filters: {// Once the client of a read has gone, its database reads are cancelled// and each comes back as an AbortError. Those are not failures, the same@@ -103,7 +132,7 @@ export function ensureLoggingConfigured({category: [CARBON_ROOT_CATEGORY],lowestLevel: level,- sinks: ["console"],+ sinks: ["console", "errorSpans"],filters: ["liveRequest"]},{diff --git a/packages/logger/src/error-spans.test.ts b/packages/logger/src/error-spans.test.tsnew file mode 100644index 0000000000..ddf70a5f3d--- /dev/null+++ b/packages/logger/src/error-spans.test.ts@@ -0,0 +1,39 @@+// 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 { SpanStatusCode, trace } from "@opentelemetry/api";+import {+ InMemorySpanExporter,+ NodeTracerProvider,+ SimpleSpanProcessor+} from "@opentelemetry/sdk-trace-node";+import { afterAll, expect, it } from "vitest";+import { ensureLoggingConfigured } from "./config.server";+import { getLogger } from "./logger";++const exporter = new InMemorySpanExporter();+new NodeTracerProvider({+ spanProcessors: [new SimpleSpanProcessor(exporter)]+}).register();+afterAll(() => trace.disable());++it("records an error log as a span, by template, without its values", () => {+ ensureLoggingConfigured({ level: "info", pretty: false });+ const logger = getLogger("erp", "receipts");+ logger.info("Posting {receiptId}", { receiptId: "rec_1" });+ logger.error("Failed to post {receiptId} for {email}", {+ receiptId: "rec_1",+ email: "[email protected]"+ });++ const spans = exporter.getFinishedSpans();+ expect(spans).toHaveLength(1);+ expect(spans[0]?.name).toBe("log error");+ expect(spans[0]?.status.code).toBe(SpanStatusCode.ERROR);+ expect(spans[0]?.attributes["log.message"]).toBe(+ "Failed to post {receiptId} for {email}"+ );+ expect(String(spans[0]?.attributes["log.category"])).toContain("receipts");+ expect(JSON.stringify(spans[0]?.attributes)).not.toContain("someone@");+});diff --git a/packages/logger/src/span.test.ts b/packages/logger/src/span.test.tsnew file mode 100644index 0000000000..703e0eaacc--- /dev/null+++ b/packages/logger/src/span.test.ts@@ -0,0 +1,59 @@+// 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 { SpanStatusCode, trace } from "@opentelemetry/api";+import {+ InMemorySpanExporter,+ NodeTracerProvider,+ SimpleSpanProcessor+} from "@opentelemetry/sdk-trace-node";+import { afterAll, beforeEach, describe, expect, it } from "vitest";+import { failSpan, inSpan } from "./span";++const exporter = new InMemorySpanExporter();+const provider = new NodeTracerProvider({+ spanProcessors: [new SimpleSpanProcessor(exporter)]+});+provider.register();++beforeEach(() => exporter.reset());+afterAll(() => trace.disable());++describe("inSpan", () => {+ it("records a span around the work, nested under the active one", async () => {+ const result = await inSpan(+ "function outer",+ { "carbon.function": "outer" },+ () => inSpan("function inner", {}, async () => 42)+ );+ expect(result).toBe(42);+ const [inner, outer] = exporter.getFinishedSpans();+ expect(outer?.name).toBe("function outer");+ expect(outer?.attributes["carbon.function"]).toBe("outer");+ expect(inner?.parentSpanContext?.spanId).toBe(outer?.spanContext().spanId);+ });++ it("marks a throw as an error and rethrows it", async () => {+ await expect(+ inSpan("function boom", {}, async () => {+ throw new Error("nope");+ })+ ).rejects.toThrow("nope");+ const [span] = exporter.getFinishedSpans();+ expect(span?.status).toEqual({+ code: SpanStatusCode.ERROR,+ message: "nope"+ });+ });++ it("lets the work mark a failure it returns instead of throwing", async () => {+ await inSpan("function refused", {}, async (span) => {+ failSpan(span, "database unavailable");+ return { error: true };+ });+ expect(exporter.getFinishedSpans()[0]?.status.code).toBe(+ SpanStatusCode.ERROR+ );+ });+});diff --git a/packages/logger/src/span.ts b/packages/logger/src/span.tsnew file mode 100644index 0000000000..3c8e7cb442--- /dev/null+++ b/packages/logger/src/span.ts@@ -0,0 +1,37 @@+// 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 { type Span, SpanStatusCode, trace } from "@opentelemetry/api";++type Attributes = Record<string, string | number | boolean | undefined>;++/**+ * Runs `run` in a child span of whatever is active. Only the OpenTelemetry API+ * is imported, which does nothing until a tracer is registered, so this is safe+ * in code that is also reachable from the browser bundle (`tracing.server` is+ * not). A throw is recorded on the span and rethrown; `run` gets the span to+ * mark a failure it returns instead of throwing.+ */+export function inSpan<T>(+ name: string,+ attributes: Attributes,+ run: (span: Span) => Promise<T>+): Promise<T> {+ return trace+ .getTracer("carbon")+ .startActiveSpan(name, { attributes }, async (span) => {+ try {+ return await run(span);+ } catch (error) {+ failSpan(span, error instanceof Error ? error.message : String(error));+ throw error;+ } finally {+ span.end();+ }+ });+}++export function failSpan(span: Span, message: string): void {+ span.setStatus({ code: SpanStatusCode.ERROR, message });+}diff --git a/packages/server-functions/src/define-server-fn.ts b/packages/server-functions/src/define-server-fn.tsindex 312f3d5e27..50c685a679 100644--- a/packages/server-functions/src/define-server-fn.ts+++ b/packages/server-functions/src/define-server-fn.ts@@ -2,6 +2,7 @@// 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 { failSpan, inSpan } from "@carbon/logger/span";import type { z } from "zod";import { type ServerFnError, toServerFnError } from "./errors";import {@@ -55,18 +56,29 @@ export function defineServerFn<S extends z.ZodType, R>({defaultStatus = 500,run}: ServerFnDefinition<S, R>): ServerFn<S, R> {- const call = async (+ // One span per call, named as the edge function's was (`function <name>`),+ // so a trace says which function the SQL under it belongs to.+ const call = (ctx: ServerFnContext,input: z.input<S>- ): Promise<ServerFnResult<R>> => {- try {- const parsed = schema.parse(input);- await authorize(ctx, resolvePermissions(permissions, parsed));- return { data: await run(ctx, parsed), error: null };- } catch (err) {- return { data: null, error: toServerFnError(name, err, defaultStatus) };- }- };+ ): Promise<ServerFnResult<R>> =>+ inSpan(+ `function ${name}`,+ { "carbon.function": name, "carbon.function.actor": ctx.actor },+ async (span) => {+ try {+ const parsed = schema.parse(input);+ await authorize(ctx, resolvePermissions(permissions, parsed));+ return { data: await run(ctx, parsed), error: null };+ } catch (err) {+ const error = toServerFnError(name, err, defaultStatus);+ span.setAttribute("carbon.function.status", error.status);+ // A refusal the caller caused is not a failed span.+ if (error.status >= 500) failSpan(span, error.message);+ return { data: null, error };+ }+ }+ );return Object.assign(call, {serverFnName: name,diff --git a/packages/server-functions/src/errors.ts b/packages/server-functions/src/errors.tsindex e042c33a36..ca1c40eebf 100644--- a/packages/server-functions/src/errors.ts+++ b/packages/server-functions/src/errors.ts@@ -88,7 +88,7 @@ export function toServerFnError(const error = toError(err, defaultStatus);// A refusal the caller caused (bad input, missing record, no permission) is// expected traffic; only a server failure is an error.- if (error.status >= 500) logger.error(`${name} failed`, { error: err });+ if (error.status >= 500) logger.error("{name} failed", { name, error: err });else logger.warn(`${name} refused`, { status: error.status, error: err });return error;}