@devries-cameron20 / t3code-1 / commits / 57d25c934b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

[codex] Structure persistence error correlation (#3439)

Co-authored-by: codex <codex@users.noreply.github.com>

Julius Marminge committed Jun 20, 2026 at 16:15 UTC 57d25c934b74986423896a7dc433595ed09b9c8d
5 files changed +451 -32
apps/server/src/persistence/AuthPairingLinks.ts
+78 -6
@@ -11,6 +11,7 @@ import { AuthEnvironmentScopes } from "@t3tools/contracts";
11 import {
12 type AuthPairingLinkRepositoryError,
13 PersistenceDecodeError,
14 + type PersistenceErrorCorrelation,
15 PersistenceSqlError,
16 } from "./Errors.ts";
17
@@ -66,6 +67,22 @@ export const GetAuthPairingLinkByCredentialInput = Schema.Struct({
67 });
68 export type GetAuthPairingLinkByCredentialInput = typeof GetAuthPairingLinkByCredentialInput.Type;
69
70 +const AuthPairingLinkRawDbRow = Schema.Struct({
71 + id: Schema.String,
72 + credential: Schema.Unknown,
73 + method: Schema.Unknown,
74 + scopes: Schema.Unknown,
75 + subject: Schema.Unknown,
76 + label: Schema.Unknown,
77 + proofKeyThumbprint: Schema.Unknown,
78 + createdAt: Schema.Unknown,
79 + expiresAt: Schema.Unknown,
80 + consumedAt: Schema.Unknown,
81 + revokedAt: Schema.Unknown,
82 +});
83 +
84 +const decodeAuthPairingLinkDbRow = Schema.decodeUnknownEffect(AuthPairingLinkRecord);
85 +
86 export class AuthPairingLinkRepository extends Context.Service<
87 AuthPairingLinkRepository,
88 {
@@ -87,11 +104,19 @@ export class AuthPairingLinkRepository extends Context.Service<
104 }
105 >()("t3/persistence/AuthPairingLinks/AuthPairingLinkRepository") {}
106
90 -function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
107 +function toPersistenceSqlOrDecodeError(
108 + sqlOperation: string,
109 + decodeOperation: string,
110 + correlation?: PersistenceErrorCorrelation,
111 +) {
112 return (cause: unknown): AuthPairingLinkRepositoryError =>
113 Schema.isSchemaError(cause)
93 - ? PersistenceDecodeError.fromSchemaError(decodeOperation, cause)
94 - : new PersistenceSqlError({ operation: sqlOperation, cause });
114 + ? PersistenceDecodeError.fromSchemaError(decodeOperation, cause, correlation)
115 + : new PersistenceSqlError({
116 + operation: sqlOperation,
117 + ...(correlation === undefined ? {} : { correlation }),
118 + cause,
119 + });
120 }
121
122 export const make = Effect.gen(function* () {
@@ -132,7 +157,7 @@ export const make = Effect.gen(function* () {
157
158 const consumeAvailablePairingLinkRow = SqlSchema.findOneOption({
159 Request: ConsumeAuthPairingLinkInput,
135 - Result: AuthPairingLinkRecord,
160 + Result: AuthPairingLinkRawDbRow,
161 execute: ({ credential, proofKeyThumbprint, consumedAt, now }) =>
162 sql`
163 UPDATE auth_pairing_links
@@ -162,7 +187,7 @@ export const make = Effect.gen(function* () {
187
188 const listActivePairingLinkRows = SqlSchema.findAll({
189 Request: ListActiveAuthPairingLinksInput,
165 - Result: AuthPairingLinkRecord,
190 + Result: AuthPairingLinkRawDbRow,
191 execute: ({ now }) =>
192 sql`
193 SELECT
@@ -201,7 +226,7 @@ export const make = Effect.gen(function* () {
226
227 const getPairingLinkRowByCredential = SqlSchema.findOneOption({
228 Request: GetAuthPairingLinkByCredentialInput,
204 - Result: AuthPairingLinkRecord,
229 + Result: AuthPairingLinkRawDbRow,
230 execute: ({ credential }) =>
231 sql`
232 SELECT
@@ -227,6 +252,7 @@ export const make = Effect.gen(function* () {
252 toPersistenceSqlOrDecodeError(
253 "AuthPairingLinkRepository.create:query",
254 "AuthPairingLinkRepository.create:encodeRequest",
255 + { pairingLinkId: input.id },
256 ),
257 ),
258 );
@@ -239,6 +265,22 @@ export const make = Effect.gen(function* () {
265 "AuthPairingLinkRepository.consumeAvailable:decodeRow",
266 ),
267 ),
268 + Effect.flatMap((rowOption) =>
269 + Option.match(rowOption, {
270 + onNone: () => Effect.succeed(Option.none()),
271 + onSome: (row) =>
272 + decodeAuthPairingLinkDbRow(row).pipe(
273 + Effect.mapError((cause) =>
274 + PersistenceDecodeError.fromSchemaError(
275 + "AuthPairingLinkRepository.consumeAvailable:decodeRow",
276 + cause,
277 + { pairingLinkId: row.id },
278 + ),
279 + ),
280 + Effect.map(Option.some),
281 + ),
282 + }),
283 + ),
284 );
285
286 const listActive: AuthPairingLinkRepository["Service"]["listActive"] = (input) =>
@@ -249,6 +291,19 @@ export const make = Effect.gen(function* () {
291 "AuthPairingLinkRepository.listActive:decodeRows",
292 ),
293 ),
294 + Effect.flatMap((rows) =>
295 + Effect.forEach(rows, (row) =>
296 + decodeAuthPairingLinkDbRow(row).pipe(
297 + Effect.mapError((cause) =>
298 + PersistenceDecodeError.fromSchemaError(
299 + "AuthPairingLinkRepository.listActive:decodeRows",
300 + cause,
301 + { pairingLinkId: row.id },
302 + ),
303 + ),
304 + ),
305 + ),
306 + ),
307 );
308
309 const revoke: AuthPairingLinkRepository["Service"]["revoke"] = (input) =>
@@ -257,6 +312,7 @@ export const make = Effect.gen(function* () {
312 toPersistenceSqlOrDecodeError(
313 "AuthPairingLinkRepository.revoke:query",
314 "AuthPairingLinkRepository.revoke:decodeRows",
315 + { pairingLinkId: input.id },
316 ),
317 ),
318 Effect.map((rows) => rows.length > 0),
@@ -270,6 +326,22 @@ export const make = Effect.gen(function* () {
326 "AuthPairingLinkRepository.getByCredential:decodeRow",
327 ),
328 ),
329 + Effect.flatMap((rowOption) =>
330 + Option.match(rowOption, {
331 + onNone: () => Effect.succeed(Option.none()),
332 + onSome: (row) =>
333 + decodeAuthPairingLinkDbRow(row).pipe(
334 + Effect.mapError((cause) =>
335 + PersistenceDecodeError.fromSchemaError(
336 + "AuthPairingLinkRepository.getByCredential:decodeRow",
337 + cause,
338 + { pairingLinkId: row.id },
339 + ),
340 + ),
341 + Effect.map(Option.some),
342 + ),
343 + }),
344 + ),
345 );
346
347 return {
apps/server/src/persistence/AuthSessions.ts
+63 -7
@@ -16,6 +16,7 @@ import {
16 import {
17 type AuthSessionRepositoryError,
18 PersistenceDecodeError,
19 + type PersistenceErrorCorrelation,
20 PersistenceSqlError,
21 } from "./Errors.ts";
22
@@ -122,6 +123,25 @@ const AuthSessionDbRow = Schema.Struct({
123 revokedAt: Schema.NullOr(Schema.DateTimeUtcFromString),
124 });
125
126 +const AuthSessionRawDbRow = Schema.Struct({
127 + sessionId: Schema.String,
128 + subject: Schema.Unknown,
129 + scopes: Schema.Unknown,
130 + method: Schema.Unknown,
131 + clientLabel: Schema.Unknown,
132 + clientIpAddress: Schema.Unknown,
133 + clientUserAgent: Schema.Unknown,
134 + clientDeviceType: Schema.Unknown,
135 + clientOs: Schema.Unknown,
136 + clientBrowser: Schema.Unknown,
137 + issuedAt: Schema.Unknown,
138 + expiresAt: Schema.Unknown,
139 + lastConnectedAt: Schema.Unknown,
140 + revokedAt: Schema.Unknown,
141 +});
142 +
143 +const decodeAuthSessionDbRow = Schema.decodeUnknownEffect(AuthSessionDbRow);
144 +
145 function toAuthSessionRecord(row: typeof AuthSessionDbRow.Type): AuthSessionRecord {
146 return {
147 sessionId: row.sessionId,
@@ -143,11 +163,19 @@ function toAuthSessionRecord(row: typeof AuthSessionDbRow.Type): AuthSessionReco
163 };
164 }
165
146 -function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
166 +function toPersistenceSqlOrDecodeError(
167 + sqlOperation: string,
168 + decodeOperation: string,
169 + correlation?: PersistenceErrorCorrelation,
170 +) {
171 return (cause: unknown): AuthSessionRepositoryError =>
172 Schema.isSchemaError(cause)
149 - ? PersistenceDecodeError.fromSchemaError(decodeOperation, cause)
150 - : new PersistenceSqlError({ operation: sqlOperation, cause });
173 + ? PersistenceDecodeError.fromSchemaError(decodeOperation, cause, correlation)
174 + : new PersistenceSqlError({
175 + operation: sqlOperation,
176 + ...(correlation === undefined ? {} : { correlation }),
177 + cause,
178 + });
179 }
180
181 export const make = Effect.gen(function* () {
@@ -192,7 +220,7 @@ export const make = Effect.gen(function* () {
220
221 const getSessionRowById = SqlSchema.findOneOption({
222 Request: GetAuthSessionByIdInput,
195 - Result: AuthSessionDbRow,
223 + Result: AuthSessionRawDbRow,
224 execute: ({ sessionId }) =>
225 sql`
226 SELECT
@@ -217,7 +245,7 @@ export const make = Effect.gen(function* () {
245
246 const listActiveSessionRows = SqlSchema.findAll({
247 Request: ListActiveAuthSessionsInput,
220 - Result: AuthSessionDbRow,
248 + Result: AuthSessionRawDbRow,
249 execute: ({ now }) =>
250 sql`
251 SELECT
@@ -285,6 +313,7 @@ export const make = Effect.gen(function* () {
313 toPersistenceSqlOrDecodeError(
314 "AuthSessionRepository.create:query",
315 "AuthSessionRepository.create:encodeRequest",
316 + { sessionId: input.sessionId },
317 ),
318 ),
319 );
@@ -295,12 +324,23 @@ export const make = Effect.gen(function* () {
324 toPersistenceSqlOrDecodeError(
325 "AuthSessionRepository.getById:query",
326 "AuthSessionRepository.getById:decodeRow",
327 + { sessionId: input.sessionId },
328 ),
329 ),
330 Effect.flatMap((rowOption) =>
331 Option.match(rowOption, {
332 onNone: () => Effect.succeed(Option.none()),
303 - onSome: (row) => Effect.succeed(Option.some(toAuthSessionRecord(row))),
333 + onSome: (row) =>
334 + decodeAuthSessionDbRow(row).pipe(
335 + Effect.mapError((cause) =>
336 + PersistenceDecodeError.fromSchemaError(
337 + "AuthSessionRepository.getById:decodeRow",
338 + cause,
339 + { sessionId: input.sessionId },
340 + ),
341 + ),
342 + Effect.map((decodedRow) => Option.some(toAuthSessionRecord(decodedRow))),
343 + ),
344 }),
345 ),
346 );
@@ -313,7 +353,20 @@ export const make = Effect.gen(function* () {
353 "AuthSessionRepository.listActive:decodeRows",
354 ),
355 ),
316 - Effect.flatMap((rows) => Effect.succeed(rows.map((row) => toAuthSessionRecord(row)))),
356 + Effect.flatMap((rows) =>
357 + Effect.forEach(rows, (row) =>
358 + decodeAuthSessionDbRow(row).pipe(
359 + Effect.mapError((cause) =>
360 + PersistenceDecodeError.fromSchemaError(
361 + "AuthSessionRepository.listActive:decodeRows",
362 + cause,
363 + { sessionId: row.sessionId },
364 + ),
365 + ),
366 + Effect.map(toAuthSessionRecord),
367 + ),
368 + ),
369 + ),
370 );
371
372 const revoke: AuthSessionRepository["Service"]["revoke"] = (input) =>
@@ -322,6 +375,7 @@ export const make = Effect.gen(function* () {
375 toPersistenceSqlOrDecodeError(
376 "AuthSessionRepository.revoke:query",
377 "AuthSessionRepository.revoke:decodeRows",
378 + { sessionId: input.sessionId },
379 ),
380 ),
381 Effect.map((rows) => rows.length > 0),
@@ -333,6 +387,7 @@ export const make = Effect.gen(function* () {
387 toPersistenceSqlOrDecodeError(
388 "AuthSessionRepository.revokeAllExcept:query",
389 "AuthSessionRepository.revokeAllExcept:decodeRows",
390 + { currentSessionId: input.currentSessionId },
391 ),
392 ),
393 Effect.map((rows) => rows.map((row) => row.sessionId)),
@@ -344,6 +399,7 @@ export const make = Effect.gen(function* () {
399 toPersistenceSqlOrDecodeError(
400 "AuthSessionRepository.setLastConnectedAt:query",
401 "AuthSessionRepository.setLastConnectedAt:encodeRequest",
402 + { sessionId: input.sessionId },
403 ),
404 ),
405 );
apps/server/src/persistence/Errors.ts
+16 -1
@@ -19,11 +19,20 @@ function summarizeSchemaIssue(issue: SchemaIssue.Issue): string {
19 // Core Persistence Errors
20 // ===============================
21
22 +export const PersistenceErrorCorrelation = Schema.Union([
23 + Schema.Struct({ sessionId: Schema.String }),
24 + Schema.Struct({ currentSessionId: Schema.String }),
25 + Schema.Struct({ pairingLinkId: Schema.String }),
26 + Schema.Struct({ threadId: Schema.String }),
27 +]);
28 +export type PersistenceErrorCorrelation = typeof PersistenceErrorCorrelation.Type;
29 +
30 export class PersistenceSqlError extends Schema.TaggedErrorClass<PersistenceSqlError>()(
31 "PersistenceSqlError",
32 {
33 operation: Schema.String,
34 detail: Schema.optional(Schema.String),
35 + correlation: Schema.optional(PersistenceErrorCorrelation),
36 cause: Schema.optional(Schema.Defect()),
37 },
38 ) {
@@ -39,13 +48,19 @@ export class PersistenceDecodeError extends Schema.TaggedErrorClass<PersistenceD
48 {
49 operation: Schema.String,
50 issue: Schema.String,
51 + correlation: Schema.optional(PersistenceErrorCorrelation),
52 cause: Schema.optional(Schema.Defect()),
53 },
54 ) {
45 - static fromSchemaError(operation: string, cause: Schema.SchemaError): PersistenceDecodeError {
55 + static fromSchemaError(
56 + operation: string,
57 + cause: Schema.SchemaError,
58 + correlation?: PersistenceErrorCorrelation,
59 + ): PersistenceDecodeError {
60 return new PersistenceDecodeError({
61 operation,
62 issue: summarizeSchemaIssue(cause.issue),
63 + ...(correlation === undefined ? {} : { correlation }),
64 cause,
65 });
66 }
apps/server/src/persistence/ProviderSessionRuntime.ts
+41 -18
@@ -17,6 +17,7 @@ import {
17
18 import {
19 PersistenceDecodeError,
20 + type PersistenceErrorCorrelation,
21 PersistenceSqlError,
22 type ProviderSessionRuntimeRepositoryError,
23 } from "./Errors.ts";
@@ -106,7 +107,19 @@ const ProviderSessionRuntimeDbRowSchema = ProviderSessionRuntime.mapFields(
107 }),
108 );
109
109 -const decodeRuntime = Schema.decodeUnknownEffect(ProviderSessionRuntime);
110 +const ProviderSessionRuntimeRawDbRowSchema = Schema.Struct({
111 + threadId: Schema.String,
112 + providerName: Schema.Unknown,
113 + providerInstanceId: Schema.Unknown,
114 + adapterKey: Schema.Unknown,
115 + runtimeMode: Schema.Unknown,
116 + status: Schema.Unknown,
117 + lastSeenAt: Schema.Unknown,
118 + resumeCursor: Schema.Unknown,
119 + runtimePayload: Schema.Unknown,
120 +});
121 +
122 +const decodeRuntimeRow = Schema.decodeUnknownEffect(ProviderSessionRuntimeDbRowSchema);
123
124 const GetRuntimeRequestSchema = Schema.Struct({
125 threadId: ThreadId,
@@ -114,11 +127,19 @@ const GetRuntimeRequestSchema = Schema.Struct({
127
128 const DeleteRuntimeRequestSchema = GetRuntimeRequestSchema;
129
117 -function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) {
130 +function toPersistenceSqlOrDecodeError(
131 + sqlOperation: string,
132 + decodeOperation: string,
133 + correlation?: PersistenceErrorCorrelation,
134 +) {
135 return (cause: unknown): ProviderSessionRuntimeRepositoryError =>
136 Schema.isSchemaError(cause)
120 - ? PersistenceDecodeError.fromSchemaError(decodeOperation, cause)
121 - : new PersistenceSqlError({ operation: sqlOperation, cause });
137 + ? PersistenceDecodeError.fromSchemaError(decodeOperation, cause, correlation)
138 + : new PersistenceSqlError({
139 + operation: sqlOperation,
140 + ...(correlation === undefined ? {} : { correlation }),
141 + cause,
142 + });
143 }
144
145 export const make = Effect.gen(function* () {
@@ -165,7 +186,7 @@ export const make = Effect.gen(function* () {
186
187 const getRuntimeRowByThreadId = SqlSchema.findOneOption({
188 Request: GetRuntimeRequestSchema,
168 - Result: ProviderSessionRuntimeDbRowSchema,
189 + Result: ProviderSessionRuntimeRawDbRowSchema,
190 execute: ({ threadId }) =>
191 sql`
192 SELECT
@@ -185,7 +206,7 @@ export const make = Effect.gen(function* () {
206
207 const listRuntimeRows = SqlSchema.findAll({
208 Request: Schema.Void,
188 - Result: ProviderSessionRuntimeDbRowSchema,
209 + Result: ProviderSessionRuntimeRawDbRowSchema,
210 execute: () =>
211 sql`
212 SELECT
@@ -218,6 +239,7 @@ export const make = Effect.gen(function* () {
239 toPersistenceSqlOrDecodeError(
240 "ProviderSessionRuntimeRepository.upsert:query",
241 "ProviderSessionRuntimeRepository.upsert:encodeRequest",
242 + { threadId: runtime.threadId },
243 ),
244 ),
245 );
@@ -228,17 +250,19 @@ export const make = Effect.gen(function* () {
250 toPersistenceSqlOrDecodeError(
251 "ProviderSessionRuntimeRepository.getByThreadId:query",
252 "ProviderSessionRuntimeRepository.getByThreadId:decodeRow",
253 + { threadId: input.threadId },
254 ),
255 ),
256 Effect.flatMap((runtimeRowOption) =>
257 Option.match(runtimeRowOption, {
258 onNone: () => Effect.succeed(Option.none()),
259 onSome: (row) =>
237 - decodeRuntime(row).pipe(
260 + decodeRuntimeRow(row).pipe(
261 Effect.mapError((cause) =>
262 PersistenceDecodeError.fromSchemaError(
240 - "ProviderSessionRuntimeRepository.getByThreadId:rowToRuntime",
263 + "ProviderSessionRuntimeRepository.getByThreadId:decodeRow",
264 cause,
265 + { threadId: input.threadId },
266 ),
267 ),
268 Effect.map((runtime) => Option.some(runtime)),
@@ -256,18 +280,16 @@ export const make = Effect.gen(function* () {
280 ),
281 ),
282 Effect.flatMap((rows) =>
259 - Effect.forEach(
260 - rows,
261 - (row) =>
262 - decodeRuntime(row).pipe(
263 - Effect.mapError((cause) =>
264 - PersistenceDecodeError.fromSchemaError(
265 - "ProviderSessionRuntimeRepository.list:rowToRuntime",
266 - cause,
267 - ),
283 + Effect.forEach(rows, (row) =>
284 + decodeRuntimeRow(row).pipe(
285 + Effect.mapError((cause) =>
286 + PersistenceDecodeError.fromSchemaError(
287 + "ProviderSessionRuntimeRepository.list:decodeRows",
288 + cause,
289 + { threadId: row.threadId },
290 ),
291 ),
270 - { concurrency: "unbounded" },
292 + ),
293 ),
294 ),
295 );
@@ -280,6 +302,7 @@ export const make = Effect.gen(function* () {
302 (cause) =>
303 new PersistenceSqlError({
304 operation: "ProviderSessionRuntimeRepository.deleteByThreadId:query",
305 + correlation: { threadId: input.threadId },
306 cause,
307 }),
308 ),
apps/server/src/persistence/RepositoryErrorCorrelation.test.ts new
+253
@@ -0,0 +1,253 @@
1 +import { AuthSessionId, ThreadId, type AuthEnvironmentScope } from "@t3tools/contracts";
2 +import { assert, describe, it } from "@effect/vitest";
3 +import * as DateTime from "effect/DateTime";
4 +import * as Effect from "effect/Effect";
5 +import * as Layer from "effect/Layer";
6 +import * as SqlClient from "effect/unstable/sql/SqlClient";
7 +
8 +import * as AuthPairingLinks from "./AuthPairingLinks.ts";
9 +import * as AuthSessions from "./AuthSessions.ts";
10 +import * as PersistenceErrors from "./Errors.ts";
11 +import { SqlitePersistenceMemory } from "./Layers/Sqlite.ts";
12 +import * as ProviderSessionRuntime from "./ProviderSessionRuntime.ts";
13 +
14 +const issuedAt = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
15 +const expiresAt = DateTime.makeUnsafe("2027-06-20T00:00:00.000Z");
16 +const now = DateTime.makeUnsafe("2026-06-21T00:00:00.000Z");
17 +const scopes: ReadonlyArray<AuthEnvironmentScope> = ["access:read"];
18 +
19 +const authSessionLayer = AuthSessions.layer.pipe(Layer.provideMerge(SqlitePersistenceMemory));
20 +const authPairingLinkLayer = AuthPairingLinks.layer.pipe(
21 + Layer.provideMerge(SqlitePersistenceMemory),
22 +);
23 +const providerSessionRuntimeLayer = ProviderSessionRuntime.layer.pipe(
24 + Layer.provideMerge(SqlitePersistenceMemory),
25 +);
26 +
27 +describe("persistence error correlation", () => {
28 + it.effect("correlates auth session SQL and row-decode failures without sensitive fields", () =>
29 + Effect.gen(function* () {
30 + const sessions = yield* AuthSessions.AuthSessionRepository;
31 + const sql = yield* SqlClient.SqlClient;
32 + const sessionId = AuthSessionId.make("session-correlation");
33 + const currentSessionId = AuthSessionId.make("current-session-correlation");
34 + const subject = "session-subject-secret-sentinel";
35 +
36 + yield* sessions.create({
37 + sessionId,
38 + subject,
39 + scopes,
40 + method: "browser-session-cookie",
41 + client: {
42 + label: null,
43 + ipAddress: null,
44 + userAgent: null,
45 + deviceType: "desktop",
46 + os: null,
47 + browser: null,
48 + },
49 + issuedAt,
50 + expiresAt,
51 + });
52 + yield* sql`
53 + UPDATE auth_sessions
54 + SET scopes = ${"session-scopes-secret-sentinel"}
55 + WHERE session_id = ${sessionId}
56 + `;
57 +
58 + const decodeError = yield* Effect.flip(sessions.listActive({ now }));
59 + assert.instanceOf(decodeError, PersistenceErrors.PersistenceDecodeError);
60 + assert.deepStrictEqual(decodeError.correlation, { sessionId });
61 + assert.equal(
62 + decodeError.message,
63 + `Decode error in AuthSessionRepository.listActive:decodeRows: ${decodeError.issue}`,
64 + );
65 + assert.notInclude(decodeError.issue, subject);
66 + assert.notInclude(decodeError.issue, "session-scopes-secret-sentinel");
67 + assert.notInclude(decodeError.message, subject);
68 +
69 + yield* sql`DROP TABLE auth_sessions`;
70 + const createError = yield* Effect.flip(
71 + sessions.create({
72 + sessionId,
73 + subject,
74 + scopes,
75 + method: "browser-session-cookie",
76 + client: {
77 + label: null,
78 + ipAddress: null,
79 + userAgent: null,
80 + deviceType: "desktop",
81 + os: null,
82 + browser: null,
83 + },
84 + issuedAt,
85 + expiresAt,
86 + }),
87 + );
88 + assert.instanceOf(createError, PersistenceErrors.PersistenceSqlError);
89 + assert.deepStrictEqual(createError.correlation, { sessionId });
90 + assert.equal(createError.message, "SQL error in AuthSessionRepository.create:query");
91 + assert.notInclude(createError.message, subject);
92 + assert.notInclude(createError.message, DateTime.formatIso(issuedAt));
93 +
94 + const revokeOtherError = yield* Effect.flip(
95 + sessions.revokeAllExcept({ currentSessionId, revokedAt: now }),
96 + );
97 + assert.instanceOf(revokeOtherError, PersistenceErrors.PersistenceSqlError);
98 + assert.deepStrictEqual(revokeOtherError.correlation, { currentSessionId });
99 + assert.equal(
100 + revokeOtherError.message,
101 + "SQL error in AuthSessionRepository.revokeAllExcept:query",
102 + );
103 + assert.notInclude(revokeOtherError.message, DateTime.formatIso(now));
104 + }).pipe(Effect.provide(authSessionLayer)),
105 + );
106 +
107 + it.effect("correlates pairing-link create and revoke failures by id only", () =>
108 + Effect.gen(function* () {
109 + const pairingLinks = yield* AuthPairingLinks.AuthPairingLinkRepository;
110 + const sql = yield* SqlClient.SqlClient;
111 + const id = "pairing-link-correlation";
112 + const credential = "pairing-credential-secret-sentinel";
113 + const subject = "pairing-subject-secret-sentinel";
114 + const scopesPayload = "pairing-scopes-secret-sentinel";
115 +
116 + yield* sql`
117 + INSERT INTO auth_pairing_links (
118 + id,
119 + credential,
120 + method,
121 + scopes,
122 + subject,
123 + label,
124 + proof_key_thumbprint,
125 + created_at,
126 + expires_at,
127 + consumed_at,
128 + revoked_at
129 + )
130 + VALUES (
131 + ${id},
132 + ${credential},
133 + ${"one-time-token"},
134 + ${scopesPayload},
135 + ${subject},
136 + NULL,
137 + NULL,
138 + ${DateTime.formatIso(issuedAt)},
139 + ${DateTime.formatIso(expiresAt)},
140 + NULL,
141 + NULL
142 + )
143 + `;
144 +
145 + const decodeError = yield* Effect.flip(pairingLinks.getByCredential({ credential }));
146 + assert.instanceOf(decodeError, PersistenceErrors.PersistenceDecodeError);
147 + assert.deepStrictEqual(decodeError.correlation, { pairingLinkId: id });
148 + assert.equal(
149 + decodeError.message,
150 + `Decode error in AuthPairingLinkRepository.getByCredential:decodeRow: ${decodeError.issue}`,
151 + );
152 + assert.notInclude(decodeError.issue, credential);
153 + assert.notInclude(decodeError.issue, subject);
154 + assert.notInclude(decodeError.issue, scopesPayload);
155 + assert.notInclude(decodeError.message, DateTime.formatIso(issuedAt));
156 +
157 + yield* sql`DROP TABLE auth_pairing_links`;
158 + const createError = yield* Effect.flip(
159 + pairingLinks.create({
160 + id,
161 + credential,
162 + method: "one-time-token",
163 + scopes,
164 + subject,
165 + label: null,
166 + proofKeyThumbprint: null,
167 + createdAt: issuedAt,
168 + expiresAt,
169 + }),
170 + );
171 + assert.instanceOf(createError, PersistenceErrors.PersistenceSqlError);
172 + assert.deepStrictEqual(createError.correlation, { pairingLinkId: id });
173 + assert.notInclude(createError.message, credential);
174 + assert.notInclude(createError.message, subject);
175 + assert.notInclude(createError.message, DateTime.formatIso(issuedAt));
176 +
177 + const revokeError = yield* Effect.flip(pairingLinks.revoke({ id, revokedAt: now }));
178 + assert.instanceOf(revokeError, PersistenceErrors.PersistenceSqlError);
179 + assert.deepStrictEqual(revokeError.correlation, { pairingLinkId: id });
180 + assert.notInclude(revokeError.message, credential);
181 + assert.notInclude(revokeError.message, DateTime.formatIso(now));
182 + }).pipe(Effect.provide(authPairingLinkLayer)),
183 + );
184 +
185 + it.effect("correlates provider runtime SQL and per-row decode failures by thread", () =>
186 + Effect.gen(function* () {
187 + const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;
188 + const sql = yield* SqlClient.SqlClient;
189 + const threadId = ThreadId.make("thread-correlation");
190 + const runtimePayload = "runtime-payload-secret-sentinel";
191 + const lastSeenAt = "2026-06-20T00:00:00.000Z";
192 +
193 + yield* sql`
194 + INSERT INTO provider_session_runtime (
195 + thread_id,
196 + provider_name,
197 + provider_instance_id,
198 + adapter_key,
199 + runtime_mode,
200 + status,
201 + last_seen_at,
202 + resume_cursor_json,
203 + runtime_payload_json
204 + )
205 + VALUES (
206 + ${threadId},
207 + ${"codex"},
208 + NULL,
209 + ${"codex"},
210 + ${"invalid-runtime-mode"},
211 + ${"running"},
212 + ${lastSeenAt},
213 + NULL,
214 + ${`{"secret":"${runtimePayload}"}`}
215 + )
216 + `;
217 +
218 + const decodeError = yield* Effect.flip(runtimes.list());
219 + assert.instanceOf(decodeError, PersistenceErrors.PersistenceDecodeError);
220 + assert.deepStrictEqual(decodeError.correlation, { threadId });
221 + assert.equal(
222 + decodeError.message,
223 + `Decode error in ProviderSessionRuntimeRepository.list:decodeRows: ${decodeError.issue}`,
224 + );
225 + assert.notInclude(decodeError.issue, runtimePayload);
226 + assert.notInclude(decodeError.message, runtimePayload);
227 + assert.notInclude(decodeError.message, lastSeenAt);
228 +
229 + yield* sql`DROP TABLE provider_session_runtime`;
230 + const sqlFailure = yield* Effect.flip(
231 + runtimes.upsert({
232 + threadId,
233 + providerName: "codex",
234 + providerInstanceId: null,
235 + adapterKey: "codex",
236 + runtimeMode: "full-access",
237 + status: "running",
238 + lastSeenAt,
239 + resumeCursor: null,
240 + runtimePayload: { secret: runtimePayload },
241 + }),
242 + );
243 + assert.instanceOf(sqlFailure, PersistenceErrors.PersistenceSqlError);
244 + assert.deepStrictEqual(sqlFailure.correlation, { threadId });
245 + assert.equal(
246 + sqlFailure.message,
247 + "SQL error in ProviderSessionRuntimeRepository.upsert:query",
248 + );
249 + assert.notInclude(sqlFailure.message, runtimePayload);
250 + assert.notInclude(sqlFailure.message, lastSeenAt);
251 + }).pipe(Effect.provide(providerSessionRuntimeLayer)),
252 + );
253 +});