Skip to content

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.

loop(1078): [t2.s2] sendVerificationCode now fails closed (returns false, no email) when redis.set resolves null under withResilience; tightened test case 2b from a tautology to assert false + sendEmail not called

Carbon Agent084ad1c72102
diff --git a/.ai/runs/1078/ledger.jsonl b/.ai/runs/1078/ledger.jsonl
index b94fda4e4f..d660d2d838 100644
--- a/.ai/runs/1078/ledger.jsonl
+++ b/.ai/runs/1078/ledger.jsonl
@@ -7,3 +7,4 @@
{"iteration":7,"change":"Verified Task-1 @carbon/auth Redis null-handling hardening + intent comments are in place (getUserClaims claims-cache set try/catch, verifyEmailCode fail-closed doc, both passkey verify* callers null-guard getAndDelete*, session.del fire-and-forget); typecheck and lint pass clean","gates":{"lint":true,"conformance":true,"clobbers":true,"typecheck:@carbon/auth":true,"correctness":true},"decision":"checkpoint","reason":"slice complete; remaining: Task 2 of plan: add vitest tests covering all 6 Redis-down cases (users.test.ts + auth-redis-resilience.test.ts) mocking @carbon/kv to return null, then run pnpm --filter @carbon/auth test","task":"1/2: Harden auth service Redis null-handling + document intent","at":"2026-07-06T04:44:42.981Z"}
{"iteration":8,"change":"Verified Task-1 @carbon/auth Redis null-handling hardening + intent comments are fully in place (getUserClaims claims-cache set try/catch, verifyEmailCode fail-closed doc, both passkey verify* callers null-guard getAndDelete*, session.del fire-and-forget); typecheck and lint pass clean. No edits needed.","gates":{"lint":true,"conformance":true,"clobbers":true,"typecheck:@carbon/auth":true,"correctness":true},"decision":"checkpoint","reason":"fixing forward: criteria still unmet: 2","task":"1/2: Harden auth service Redis null-handling + document intent","at":"2026-07-06T04:47:57.162Z"}
{"iteration":9,"change":"Added packages/auth/src/services/auth-redis-resilience.test.ts covering all 6 Redis-down auth cases (getUserClaims DB fallback, verifyEmailCode/sendVerificationCode false, passkey getAndDelete* null, updateCompanySession cookie); Task-1 hardening already landed in prior slices","gates":{"lint":true,"conformance":true,"clobbers":true,"typecheck:@carbon/auth":true,"correctness":true},"decision":"keep","reason":"gates green; judge approved","task":"1/2: Harden auth service Redis null-handling + document intent","assumptions":["Task 1 (code hardening + comments) was verifiably complete across 8 prior committed slices, so I completed Task 2 (the tests) this session — the actual blocker for the flagged criteria [2]/[5]/[8] — rather than re-verify Task 1 a 9th time with no progress.","Combined both required test files into one auth-redis-resilience.test.ts (grooming context explicitly permits combining), covering all 6 cases plus a null-resolve no-throw variant.","Mocked env-validation (../config/env), @carbon/kv, and heavy deps (resend/email/simplewebauthn/supabase) so tests run without a live environment; used clearAllMocks (keeps factory implementations) plus per-test mockRejectedValueOnce for the set-fails case."],"at":"2026-07-06T04:56:16.669Z"}
+{"iteration":10,"change":"Verified packages/auth/src/services/auth-redis-resilience.test.ts covers all 6 Redis-down cases (getUserClaims DB fallback, sendVerificationCode false, verifyEmailCode false, both passkey getAndDelete* null, updateCompanySession cookie); auth test/typecheck/lint all pass","gates":{"lint":true,"conformance":true,"clobbers":true,"typecheck:@carbon/auth":true,"correctness":true},"decision":"checkpoint","reason":"fixing forward: judge rejected: Test task's own gates pass (typecheck/lint/7 tests green) and cases 1,3,4,5,6 are real proofs. But criterion [2] is unmet: under withResilience redis.set resolves null (no throw) when Redis is down, so sendVerificationCode (verification.server.ts:15) skips the catch, sends the email, and returns TRUE — emailing a code that was never stored. Grooming explicitly requires returning false ('verification code unsendable'). The case-2b test (auth-redis-resilience.test.ts:106) asserts only expect.any(Boolean), a tautology that would pass either way, so it doesn't verify the required behavior. Fix: in sendVerificationCode check redis.set's return (`if (!(await redis.set(...))) return false;`) and tighten case 2b to assert false on the null-set path. This is the final task, so no later task will cover [2].","task":"2/2: Add auth Redis-down resilience tests","at":"2026-07-06T05:00:34.839Z"}
diff --git a/packages/auth/src/services/auth-redis-resilience.test.ts b/packages/auth/src/services/auth-redis-resilience.test.ts
index 9a43e3a50a..421b1dbab2 100644
--- a/packages/auth/src/services/auth-redis-resilience.test.ts
+++ b/packages/auth/src/services/auth-redis-resilience.test.ts
@@ -69,6 +69,7 @@ vi.mock("./company.server", () => ({
}));
import { redis } from "@carbon/kv";
+import { sendEmail } from "@carbon/lib/resend.server";
import type { AuthSession } from "../types";
import {
getAndDeleteAuthChallenge,
@@ -103,10 +104,12 @@ describe("auth Redis-down resilience", () => {
await expect(sendVerificationCode("[email protected]")).resolves.toBe(false);
});
- it("[case 2b] sendVerificationCode does not throw when redis.set resolves null", async () => {
- await expect(sendVerificationCode("[email protected]")).resolves.toEqual(
- expect.any(Boolean)
- );
+ it("[case 2b] sendVerificationCode fails closed (returns false, no email) when redis.set resolves null", async () => {
+ // Under withResilience, a Redis-down `set` resolves null (not a throw). The code
+ // must NOT send an unverifiable email in that case — it returns false without
+ // ever calling sendEmail.
+ await expect(sendVerificationCode("[email protected]")).resolves.toBe(false);
+ expect(sendEmail).not.toHaveBeenCalled();
});
it("[case 3] verifyEmailCode returns false (fail-closed) when redis.get is null", async () => {
diff --git a/packages/auth/src/services/verification.server.ts b/packages/auth/src/services/verification.server.ts
index 0d6be9ca69..d7c7de9f39 100644
--- a/packages/auth/src/services/verification.server.ts
+++ b/packages/auth/src/services/verification.server.ts
@@ -11,13 +11,21 @@ export async function sendVerificationCode(email: string) {
100000 + Math.random() * 900000
).toString();
- // Store in Redis with 10-minute expiration
- await redis.set(
+ // Store in Redis with 10-minute expiration. @carbon/kv (withResilience) fails
+ // soft: on a Redis-down condition `set` resolves null instead of "OK". If we
+ // can't persist the code we must NOT send an unverifiable email — fail closed.
+ const stored = await redis.set(
`verification:${email.toLowerCase()}`,
verificationCode,
"EX",
600
);
+ if (!stored) {
+ console.error(
+ "Failed to store verification code (Redis unavailable); not sending email"
+ );
+ return false;
+ }
// Send email with verification code using React template
const html = await render(