Repository navigation
Expand file tree
/
Copy pathRepositoryErrorCorrelation.test.ts
More file actions
261 lines (243 loc) · 9.27 KB
/
Copy pathRepositoryErrorCorrelation.test.ts
File metadata and controls
261 lines (243 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { AuthSessionId, ThreadId, type AuthEnvironmentScope } from "@t3tools/contracts";
import { assert, describe, it } from "@effect/vitest";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as SqlClient from "effect/sql/SqlClient";
import * as AuthPairingLinks from "./AuthPairingLinks.ts";
import * as AuthSessions from "./AuthSessions.ts";
import * as PersistenceErrors from "./Errors.ts";
import * as SqlitePersistence from "./Sqlite.ts";
import * as ProviderSessionRuntime from "./ProviderSessionRuntime.ts";
const issuedAt = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
const expiresAt = DateTime.makeUnsafe("2027-06-20T00:00:00.000Z");
const now = DateTime.makeUnsafe("2026-06-21T00:00:00.000Z");
const scopes: ReadonlyArray<AuthEnvironmentScope> = ["access:read"];
const layerAuthSession = AuthSessions.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory));
const layerAuthPairingLink = AuthPairingLinks.layer.pipe(
Layer.provideMerge(SqlitePersistence.layerMemory),
);
const layerProviderSessionRuntime = ProviderSessionRuntime.layer.pipe(
Layer.provideMerge(SqlitePersistence.layerMemory),
);
describe("persistence error correlation", () => {
it.effect("correlates auth session SQL and row-decode failures without sensitive fields", () =>
Effect.gen(function* () {
const sessions = yield* AuthSessions.AuthSessionRepository;
const sql = yield* SqlClient.SqlClient;
const sessionId = AuthSessionId.make("session-correlation");
const currentSessionId = AuthSessionId.make("current-session-correlation");
const subject = "session-subject-secret-sentinel";
yield* sessions.create({
sessionId,
subject,
scopes,
method: "browser-session-cookie",
client: {
label: null,
ipAddress: null,
userAgent: null,
deviceType: "desktop",
os: null,
browser: null,
},
issuedAt,
expiresAt,
});
yield* sql`
UPDATE auth_sessions
SET scopes = ${"session-scopes-secret-sentinel"}
WHERE session_id = ${sessionId}
`;
const decodeError = yield* Effect.flip(sessions.listActive({ now }));
assert.instanceOf(decodeError, PersistenceErrors.PersistenceDecodeError);
assert.deepStrictEqual(decodeError.correlation, { sessionId });
assert.equal(
decodeError.message,
`Decode error in AuthSessionRepository.listActive:decodeRows: ${decodeError.issue}`,
);
assert.notInclude(decodeError.issue, subject);
assert.notInclude(decodeError.issue, "session-scopes-secret-sentinel");
assert.notInclude(decodeError.message, subject);
yield* sql`DROP TABLE auth_sessions`;
const createError = yield* Effect.flip(
sessions.create({
sessionId,
subject,
scopes,
method: "browser-session-cookie",
client: {
label: null,
ipAddress: null,
userAgent: null,
deviceType: "desktop",
os: null,
browser: null,
},
issuedAt,
expiresAt,
}),
);
assert.instanceOf(createError, PersistenceErrors.PersistenceSqlError);
assert.deepStrictEqual(createError.correlation, { sessionId });
assert.equal(createError.message, "SQL error in AuthSessionRepository.create:query");
assert.notInclude(createError.message, subject);
assert.notInclude(createError.message, DateTime.formatIso(issuedAt));
const revokeOtherError = yield* Effect.flip(
sessions.revokeAllExcept({ currentSessionId, revokedAt: now }),
);
assert.instanceOf(revokeOtherError, PersistenceErrors.PersistenceSqlError);
assert.deepStrictEqual(revokeOtherError.correlation, { currentSessionId });
assert.equal(
revokeOtherError.message,
"SQL error in AuthSessionRepository.revokeAllExcept:query",
);
assert.notInclude(revokeOtherError.message, DateTime.formatIso(now));
}).pipe(Effect.provide(layerAuthSession)),
);
it.effect("correlates pairing-link create and revoke failures by id only", () =>
Effect.gen(function* () {
const pairingLinks = yield* AuthPairingLinks.AuthPairingLinkRepository;
const sql = yield* SqlClient.SqlClient;
const id = "pairing-link-correlation";
const credential = "pairing-credential-secret-sentinel";
const subject = "pairing-subject-secret-sentinel";
const scopesPayload = "pairing-scopes-secret-sentinel";
yield* sql`
INSERT INTO auth_pairing_links (
id,
credential,
method,
scopes,
subject,
label,
proof_key_thumbprint,
created_at,
expires_at,
consumed_at,
revoked_at
)
VALUES (
${id},
${credential},
${"one-time-token"},
${scopesPayload},
${subject},
NULL,
NULL,
${DateTime.formatIso(issuedAt)},
${DateTime.formatIso(expiresAt)},
NULL,
NULL
)
`;
const decodeError = yield* Effect.flip(pairingLinks.getByCredential({ credential }));
assert.instanceOf(decodeError, PersistenceErrors.PersistenceDecodeError);
assert.deepStrictEqual(decodeError.correlation, { pairingLinkId: id });
assert.equal(
decodeError.message,
`Decode error in AuthPairingLinkRepository.getByCredential:decodeRow: ${decodeError.issue}`,
);
assert.notInclude(decodeError.issue, credential);
assert.notInclude(decodeError.issue, subject);
assert.notInclude(decodeError.issue, scopesPayload);
assert.notInclude(decodeError.message, DateTime.formatIso(issuedAt));
yield* sql`DROP TABLE auth_pairing_links`;
const createError = yield* Effect.flip(
pairingLinks.create({
id,
credential,
method: "one-time-token",
scopes,
subject,
label: null,
proofKeyThumbprint: null,
createdAt: issuedAt,
expiresAt,
}),
);
assert.instanceOf(createError, PersistenceErrors.PersistenceSqlError);
assert.deepStrictEqual(createError.correlation, { pairingLinkId: id });
assert.notInclude(createError.message, credential);
assert.notInclude(createError.message, subject);
assert.notInclude(createError.message, DateTime.formatIso(issuedAt));
const revokeError = yield* Effect.flip(pairingLinks.revoke({ id, revokedAt: now }));
assert.instanceOf(revokeError, PersistenceErrors.PersistenceSqlError);
assert.deepStrictEqual(revokeError.correlation, { pairingLinkId: id });
assert.notInclude(revokeError.message, credential);
assert.notInclude(revokeError.message, DateTime.formatIso(now));
}).pipe(Effect.provide(layerAuthPairingLink)),
);
it.effect("skips undecodable provider runtime rows and correlates SQL failures by thread", () =>
Effect.gen(function* () {
const runtimes = yield* ProviderSessionRuntime.ProviderSessionRuntimeRepository;
const sql = yield* SqlClient.SqlClient;
const threadId = ThreadId.make("thread-correlation");
const runtimePayload = "runtime-payload-secret-sentinel";
const lastSeenAt = "2026-06-20T00:00:00.000Z";
yield* sql`
INSERT INTO provider_session_runtime (
thread_id,
provider_name,
provider_instance_id,
adapter_key,
runtime_mode,
status,
last_seen_at,
resume_cursor_json,
runtime_payload_json
)
VALUES (
${threadId},
${"codex"},
NULL,
${"codex"},
${"invalid-runtime-mode"},
${"running"},
${lastSeenAt},
NULL,
${`{"secret":"${runtimePayload}"}`}
)
`;
const validThreadId = ThreadId.make("thread-valid");
yield* runtimes.upsert({
threadId: validThreadId,
providerName: "codex",
providerInstanceId: null,
adapterKey: "codex",
runtimeMode: "full-access",
status: "running",
lastSeenAt,
resumeCursor: null,
runtimePayload: null,
});
const listed = yield* runtimes.list();
assert.deepStrictEqual(
listed.map((runtime) => runtime.threadId),
[validThreadId],
);
yield* sql`DROP TABLE provider_session_runtime`;
const sqlFailure = yield* Effect.flip(
runtimes.upsert({
threadId,
providerName: "codex",
providerInstanceId: null,
adapterKey: "codex",
runtimeMode: "full-access",
status: "running",
lastSeenAt,
resumeCursor: null,
runtimePayload: { secret: runtimePayload },
}),
);
assert.instanceOf(sqlFailure, PersistenceErrors.PersistenceSqlError);
assert.deepStrictEqual(sqlFailure.correlation, { threadId });
assert.equal(
sqlFailure.message,
"SQL error in ProviderSessionRuntimeRepository.upsert:query",
);
assert.notInclude(sqlFailure.message, runtimePayload);
assert.notInclude(sqlFailure.message, lastSeenAt);
}).pipe(Effect.provide(layerProviderSessionRuntime)),
);
});