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
loop(1081): [t1.s1] resilient.ts logUnavailable/logReconnected now emit structured JSON events (redis.degraded via console.error, redis.recovered via console.info), keeping the existing one-log-per-transition throttle
Carbon Agentaa408834a3d2
diff --git a/.ai/runs/1081/binding.loop.md b/.ai/runs/1081/binding.loop.mdnew file mode 100644index 0000000000..48a30a69d1--- /dev/null+++ b/.ai/runs/1081/binding.loop.md@@ -0,0 +1,49 @@+---+id: "1081"+issue: 1081+kind: feature+risk: low+title: "Redis resilience: health endpoint + observability"+acceptance:+ - "GET /health returns { status: 'healthy', redis: 'up' } (HTTP 200) when Redis is running"+ - "GET /health returns { status: 'degraded', redis: 'down' } (HTTP 200) when Redis returns null from ping()"+ - "Health route is unauthenticated — no session or auth cookie required"+ - "packages/kv/src/resilient.ts emits structured { event: 'redis.degraded' } log on transition to unavailable (once per transition, not per command)"+ - "packages/kv/src/resilient.ts emits structured { event: 'redis.recovered' } log on reconnect"+ - "Unit test: mocking redis.ping() -> null returns degraded shape; mocking truthy returns healthy shape"+ - "TypeScript clean: pnpm --filter @carbon/erp tsc --noEmit and pnpm --filter @carbon/kv tsc --noEmit"+ - "Biome lint clean"+---++## Context++PR #1083 (merged to main) added `withResilience()` in `packages/kv/src/resilient.ts`. The wrapper already emits throttled `logUnavailable` / `logReconnected` console calls (one per transition). This issue surfaces that signal via a health endpoint and promotes the log events to structured observability.++`@carbon/kv` exports a `redis` client (check `packages/kv/src/index.ts` for the exact export name). `redis.ping()` returns `'PONG'` when Redis is up, or `null` when the resilience wrapper intercepts a down state.++## Task++### 1. Health endpoint (`apps/erp`)+Add a Remix resource route at `apps/erp/app/routes/health.ts` (or `health.tsx` — resource route, no default component export):+- Import the redis client from `@carbon/kv`+- Call `redis.ping()` — if result is `null` or falsy, Redis is down+- Return `Response.json({ status: 'healthy' | 'degraded', redis: 'up' | 'down' }, { status: 200 })`+- No auth required — this is a probe endpoint for infrastructure health checks+- The `REDIS_TIMEOUT_MS` (2s) from the wrapper is sufficient; no extra timeout logic needed++### 2. Structured logging (`packages/kv/src/resilient.ts`)+Find the existing `logUnavailable` / `logReconnected` inline calls. Update them to emit structured JSON:+- On unavailable transition: `console.error(JSON.stringify({ event: 'redis.degraded', message: 'Redis is unavailable, running in degraded mode' }))`+- On recovery: `console.info(JSON.stringify({ event: 'redis.recovered', message: 'Redis reconnected' }))`+Keep the throttle (one log per transition — already implemented, don't break that).++### 3. Unit test+Add `apps/erp/app/routes/health.test.ts`:+- Mock `@carbon/kv` so `redis.ping` returns `null` → loader returns `{ status: 'degraded', redis: 'down' }`+- Mock `@carbon/kv` so `redis.ping` returns `'PONG'` → loader returns `{ status: 'healthy', redis: 'up' }`++## Proof methods+- AC[1][2][3]: Unit test+- AC[4][5]: Static verification of the updated resilient.ts+- AC[6]: Vitest run for the health test+- AC[7][8]: tsc + biomediff --git a/packages/kv/src/resilient.ts b/packages/kv/src/resilient.tsindex 7b5b6f8f89..bbcb567f6b 100644--- a/packages/kv/src/resilient.ts+++ b/packages/kv/src/resilient.ts@@ -9,8 +9,13 @@ let unavailable = false;export function logUnavailable(err: unknown): void {const now = Date.now();if (!unavailable || now - lastLoggedAt >= LOG_THROTTLE_MS) {- const message = err instanceof Error ? err.message : String(err);- console.warn(`Redis unavailable, falling back: ${message}`);+ console.error(+ JSON.stringify({+ event: "redis.degraded",+ message: "Redis is unavailable, running in degraded mode",+ cause: err instanceof Error ? err.message : String(err)+ })+ );lastLoggedAt = now;}unavailable = true;@@ -18,7 +23,12 @@ export function logUnavailable(err: unknown): void {function logReconnected(): void {if (unavailable) {- console.info("Redis reconnected");+ console.info(+ JSON.stringify({+ event: "redis.recovered",+ message: "Redis reconnected"+ })+ );unavailable = false;}}