Skip to content

Commit 53c60d3

Browse files
fix(server): preserve discovery read error context
1 parent bfa053f commit 53c60d3

2 files changed

Lines changed: 59 additions & 4 deletions

File tree

‎apps/server/src/orchestration-v2/ProjectionPullRequestDiscovery.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,42 @@ it.effect("SQL discovery reads no run, message, item or fork history", () =>
157157
assert.isTrue(statements.some((query) => query.includes("MAX(sequence)")));
158158
}).pipe(Effect.provide(sqlLayer)),
159159
);
160+
161+
it.effect("SQL discovery identifies the thread whose metadata cannot decode", () =>
162+
Effect.gen(function* () {
163+
const store = yield* ProjectionStore.ProjectionStoreV2;
164+
const sql = yield* SqlClient.SqlClient;
165+
const id = yield* createThread("malformed-metadata");
166+
yield* sql`UPDATE orchestration_v2_projection_threads SET payload_json = '{}' WHERE thread_id = ${id}`;
167+
const error = yield* Effect.flip(
168+
store.getPullRequestDiscoverySnapshot({ unsettledOnly: false }),
169+
);
170+
assert.equal(error._tag, "ProjectionStoreReadError");
171+
if (error._tag === "ProjectionStoreReadError") {
172+
assert.equal(error.threadId, id);
173+
assert.isDefined(error.cause);
174+
}
175+
}).pipe(Effect.provide(sqlLayer)),
176+
);
177+
178+
it.effect("SQL discovery reports read failures with single-thread or sweep context", () =>
179+
Effect.gen(function* () {
180+
const store = yield* ProjectionStore.ProjectionStoreV2;
181+
const sql = yield* SqlClient.SqlClient;
182+
const id = yield* createThread("read-failure");
183+
yield* sql`DROP TABLE orchestration_v2_projection_threads`;
184+
for (const threadId of [undefined, id]) {
185+
const error = yield* Effect.flip(
186+
store.getPullRequestDiscoverySnapshot({
187+
...(threadId === undefined ? {} : { threadId }),
188+
unsettledOnly: false,
189+
}),
190+
);
191+
assert.equal(error._tag, "ProjectionStoreReadError");
192+
if (error._tag === "ProjectionStoreReadError") {
193+
assert.equal(error.threadId, threadId ?? ThreadId.make("thread:pull-request-discovery"));
194+
assert.isDefined(error.cause);
195+
}
196+
}
197+
}).pipe(Effect.provide(sqlLayer)),
198+
);

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5619,8 +5619,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56195619
threadId === undefined ? sql`` : sql` AND stream_id = ${threadId}`
56205620
}
56215621
`;
5622-
const rows = yield* sql<PayloadRow>`
5623-
SELECT payload_json FROM orchestration_v2_projection_threads
5622+
const rows = yield* sql<PayloadRow & { readonly thread_id: string }>`
5623+
SELECT thread_id, payload_json FROM orchestration_v2_projection_threads
56245624
WHERE deleted_at IS NULL
56255625
AND json_extract(payload_json, '$.archivedAt') IS NULL${
56265626
threadId === undefined ? sql`` : sql` AND thread_id = ${threadId}`
@@ -5635,8 +5635,25 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56355635
}),
56365636
)
56375637
.pipe(
5638+
Effect.mapError(
5639+
(cause) =>
5640+
new ProjectionStoreReadError({
5641+
threadId: threadId ?? ThreadId.make("thread:pull-request-discovery"),
5642+
cause,
5643+
}),
5644+
),
56385645
Effect.flatMap(({ rows, snapshotSequence }) =>
5639-
Effect.forEach(rows, (row) => decodeThreadPayload(row.payload_json)).pipe(
5646+
Effect.forEach(rows, (row) =>
5647+
decodeThreadPayload(row.payload_json).pipe(
5648+
Effect.mapError(
5649+
(cause) =>
5650+
new ProjectionStoreReadError({
5651+
threadId: ThreadId.make(row.thread_id),
5652+
cause,
5653+
}),
5654+
),
5655+
),
5656+
).pipe(
56405657
Effect.map((threads) => ({
56415658
snapshotSequence,
56425659
threads: threads.map((thread) => ({
@@ -5646,7 +5663,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56465663
})),
56475664
),
56485665
),
5649-
Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })),
56505666
);
56515667

56525668
const getThreadsWithPullRequests: ProjectionStoreV2Shape["getThreadsWithPullRequests"] = (

0 commit comments

Comments
 (0)