Skip to content

Commit 58a0b9e

Browse files
perf(server): narrow watched reads and release settlement transactions
Co-authored-by: maria-rcks <[email protected]>
1 parent eb84f0c commit 58a0b9e

3 files changed

Lines changed: 173 additions & 67 deletions

File tree

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

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,28 +364,48 @@ const pullRequestLink = (number: number) => ({
364364
stack: null,
365365
});
366366

367+
const pullRequestWatch = {
368+
startedAt: DateTime.formatIso(old),
369+
headSha: null,
370+
failedChecks: [],
371+
passed: false,
372+
passedChecks: [],
373+
remarksThrough: DateTime.formatIso(old),
374+
remarkIds: [],
375+
conflicting: false,
376+
wakes: 0,
377+
};
378+
367379
it.effect.each([
368380
["sql", layerSql],
369381
["memory", ProjectionStore.layerMemory],
370382
] as const)(
371-
"%s: lists only active threads with pull request links, oldest first",
383+
"%s: lists active threads with pull request links, optionally watched, oldest first",
372384
([, testLayer]) =>
373385
Effect.gen(function* () {
374386
const store = yield* ProjectionStore.ProjectionStoreV2;
375387
yield* createThread("no-links");
376388
yield* createThread("empty-links", { pullRequests: [] });
377389
yield* createThread("archived-link", {
378390
archivedAt: old,
379-
pullRequests: [pullRequestLink(1)],
391+
pullRequests: [{ ...pullRequestLink(1), watch: pullRequestWatch }],
392+
});
393+
yield* createThread("deleted-link", {
394+
deletedAt: old,
395+
pullRequests: [{ ...pullRequestLink(5), watch: pullRequestWatch }],
380396
});
381397
const settled = yield* createThread("settled-link", {
382398
settledOverride: "settled",
383399
settledAt: old,
384400
updatedAt: DateTime.subtract(now, { days: 12 }),
385-
pullRequests: [pullRequestLink(2)],
401+
pullRequests: [{ ...pullRequestLink(2), watch: pullRequestWatch }],
386402
});
387403
const open = yield* createThread("open-link", {
388-
pullRequests: [pullRequestLink(3), pullRequestLink(4)],
404+
pullRequests: [pullRequestLink(3), { ...pullRequestLink(4), watch: pullRequestWatch }],
405+
});
406+
const unwatched = yield* createThread("unwatched-link", {
407+
updatedAt: now,
408+
pullRequests: [pullRequestLink(6)],
389409
});
390410

391411
const threads = yield* store.getThreadsWithPullRequests();
@@ -394,15 +414,64 @@ it.effect.each([
394414
[
395415
[settled, "settled", 1],
396416
[open, null, 2],
417+
[unwatched, null, 1],
397418
],
398419
);
399420
assert.deepEqual(
400421
(yield* store.getThreadsWithPullRequests(open)).map((thread) => thread.id),
401422
[open],
402423
);
424+
assert.deepEqual(
425+
yield* store.getThreadsWithPullRequests(undefined, { watchedOnly: true }),
426+
threads.filter((thread) => thread.id !== unwatched),
427+
);
428+
assert.deepEqual(
429+
yield* store.getThreadsWithPullRequests(open, { watchedOnly: true }),
430+
threads.filter((thread) => thread.id === open),
431+
);
432+
assert.deepEqual(
433+
yield* store.getThreadsWithPullRequests(unwatched, { watchedOnly: true }),
434+
[],
435+
);
403436
}).pipe(Effect.provide(testLayer)),
404437
);
405438

439+
it.effect(
440+
"watch reads exclude unwatched payloads before decoding and follow stored watch changes",
441+
() =>
442+
Effect.gen(function* () {
443+
const store = yield* ProjectionStore.ProjectionStoreV2;
444+
const sql = yield* SqlClient.SqlClient;
445+
const watched = yield* createThread("watch-decode", {
446+
pullRequests: [{ ...pullRequestLink(1), watch: pullRequestWatch }],
447+
});
448+
const unwatched = yield* createThread("unwatched-decode", {
449+
pullRequests: [pullRequestLink(2)],
450+
});
451+
yield* sql`UPDATE orchestration_v2_projection_threads
452+
SET payload_json = json_set(payload_json, '$.title', 42)
453+
WHERE thread_id = ${unwatched}`;
454+
455+
assert.deepEqual(
456+
(yield* store.getThreadsWithPullRequests(undefined, { watchedOnly: true })).map(
457+
(thread) => thread.id,
458+
),
459+
[watched],
460+
);
461+
yield* sql`UPDATE orchestration_v2_projection_threads
462+
SET payload_json = json_remove(payload_json, '$.pullRequests[0].watch')
463+
WHERE thread_id = ${watched}`;
464+
assert.deepEqual(
465+
yield* store.getThreadsWithPullRequests(undefined, { watchedOnly: true }),
466+
[],
467+
);
468+
assert.equal(
469+
(yield* store.getThreadsWithPullRequests().pipe(Effect.flip))._tag,
470+
"ProjectionStoreSetupError",
471+
);
472+
}).pipe(Effect.provide(layerSql)),
473+
);
474+
406475
it.effect.each([
407476
["sql", layerSql],
408477
["memory", ProjectionStore.layerMemory],

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

Lines changed: 99 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ export interface ProjectionStoreV2Shape {
421421
) => Effect.Effect<ReadonlyArray<ProjectionSettlementCandidate>, ProjectionStoreV2Error>;
422422
readonly getThreadsWithPullRequests: (
423423
threadId?: ThreadId,
424+
options?: { readonly watchedOnly?: boolean },
424425
) => Effect.Effect<ReadonlyArray<ProjectionThreadPullRequests>, ProjectionStoreV2Error>;
425426
readonly getThreadWorktreePaths: () => Effect.Effect<
426427
ReadonlyArray<{ readonly threadId: ThreadId; readonly worktreePath: string }>,
@@ -1207,6 +1208,20 @@ const decodeRows =
12071208
),
12081209
);
12091210

1211+
// Full thread schemas can exhaust the fiber budget slowly; yield between small
1212+
// batches so background sweeps let other event-loop work run during bulk decoding.
1213+
const decodeBackgroundRows = Effect.fnUntraced(function* <A, B, E, R>(
1214+
rows: ReadonlyArray<A>,
1215+
decode: (row: A) => Effect.Effect<B, E, R>,
1216+
) {
1217+
const values: Array<B> = [];
1218+
for (let index = 0; index < rows.length; index++) {
1219+
if (index > 0 && index % 64 === 0) yield* Effect.yieldNow;
1220+
values.push(yield* decode(rows[index]!));
1221+
}
1222+
return values;
1223+
});
1224+
12101225
function messageIdForTurnItem(item: OrchestrationV2TurnItem): string | null {
12111226
switch (item.type) {
12121227
case "user_message":
@@ -5650,71 +5665,83 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56505665
)
56515666
ORDER BY t.updated_at ASC, t.thread_id ASC
56525667
`;
5653-
if (rows.length === 0) return [];
5668+
if (rows.length === 0) {
5669+
return { rows, providerThreadRows: [], pendingTurnItemRows: [] };
5670+
}
56545671
const threadIds = rows.map((row) => ThreadId.make(row.thread_id));
5655-
const { providerThreadsByThreadId, pendingTurnItemsByThreadId } =
5656-
yield* pendingBackgroundDataByThreadId({
5657-
providerThreadRows: yield* selectShellProviderThreadRows(threadIds),
5658-
pendingTurnItemRows: yield* selectShellPendingTurnItemRows(threadIds),
5659-
});
5660-
return yield* Effect.forEach(rows, (row) =>
5661-
Effect.gen(function* () {
5662-
const thread = yield* decodeThreadPayload(row.payload_json);
5663-
const status = shellStatusFromStoredRunStatus(row.latest_run_status);
5664-
const latestRunId =
5665-
row.latest_run_id === null ? null : RunId.make(row.latest_run_id);
5666-
return {
5667-
...thread,
5668-
pinnedAt: thread.pinnedAt ?? null,
5669-
5670-
autoSettleDisabledAt: thread.autoSettleDisabledAt ?? null,
5671-
snoozedUntil: thread.snoozedUntil ?? null,
5672-
snoozedAt: thread.snoozedAt ?? null,
5673-
status,
5674-
latestRunId,
5675-
latestRunRequestedAt:
5676-
row.latest_run_requested_at === null
5677-
? null
5678-
: DateTime.makeUnsafe(row.latest_run_requested_at),
5679-
latestRunStartedAt:
5680-
row.latest_run_started_at === null
5681-
? null
5682-
: DateTime.makeUnsafe(row.latest_run_started_at),
5683-
latestRunCompletedAt:
5684-
row.latest_run_completed_at === null
5685-
? null
5686-
: DateTime.makeUnsafe(row.latest_run_completed_at),
5687-
latestUserMessageAt:
5688-
row.latest_user_message_at === null
5689-
? null
5690-
: DateTime.makeUnsafe(row.latest_user_message_at),
5691-
latestUserAuthoredMessageAt:
5692-
row.latest_user_authored_message_at === null
5693-
? null
5694-
: DateTime.makeUnsafe(row.latest_user_authored_message_at),
5695-
activityRunStatus: null,
5696-
activityRunStartedAt: null,
5697-
pendingRuntimeRequest: null,
5698-
pendingBackgroundTasks: derivePendingBackgroundWork({
5699-
latestRun:
5700-
latestRunId === null || status === "idle"
5701-
? null
5702-
: { id: latestRunId, ordinal: 0, status },
5703-
providerThreads: providerThreadsByThreadId.get(thread.id) ?? [],
5704-
turnItems: pendingTurnItemsByThreadId.get(thread.id) ?? [],
5705-
activeProviderThreadId: thread.activeProviderThreadId,
5706-
hasActiveRun: false,
5707-
pullRequests: thread.pullRequests,
5708-
}),
5709-
} satisfies ProjectionSettlementCandidate;
5710-
}),
5711-
);
5672+
return {
5673+
rows,
5674+
providerThreadRows: yield* selectShellProviderThreadRows(threadIds),
5675+
pendingTurnItemRows: yield* selectShellPendingTurnItemRows(threadIds),
5676+
};
57125677
}),
57135678
)
5714-
.pipe(Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })));
5679+
.pipe(
5680+
// Release the shared connection before schema decoding and background derivation.
5681+
Effect.flatMap(({ rows, providerThreadRows, pendingTurnItemRows }) =>
5682+
Effect.gen(function* () {
5683+
const { providerThreadsByThreadId, pendingTurnItemsByThreadId } =
5684+
yield* pendingBackgroundDataByThreadId({ providerThreadRows, pendingTurnItemRows });
5685+
return yield* decodeBackgroundRows(rows, (row) =>
5686+
Effect.gen(function* () {
5687+
const thread = yield* decodeThreadPayload(row.payload_json);
5688+
const status = shellStatusFromStoredRunStatus(row.latest_run_status);
5689+
const latestRunId =
5690+
row.latest_run_id === null ? null : RunId.make(row.latest_run_id);
5691+
return {
5692+
...thread,
5693+
pinnedAt: thread.pinnedAt ?? null,
5694+
5695+
autoSettleDisabledAt: thread.autoSettleDisabledAt ?? null,
5696+
snoozedUntil: thread.snoozedUntil ?? null,
5697+
snoozedAt: thread.snoozedAt ?? null,
5698+
status,
5699+
latestRunId,
5700+
latestRunRequestedAt:
5701+
row.latest_run_requested_at === null
5702+
? null
5703+
: DateTime.makeUnsafe(row.latest_run_requested_at),
5704+
latestRunStartedAt:
5705+
row.latest_run_started_at === null
5706+
? null
5707+
: DateTime.makeUnsafe(row.latest_run_started_at),
5708+
latestRunCompletedAt:
5709+
row.latest_run_completed_at === null
5710+
? null
5711+
: DateTime.makeUnsafe(row.latest_run_completed_at),
5712+
latestUserMessageAt:
5713+
row.latest_user_message_at === null
5714+
? null
5715+
: DateTime.makeUnsafe(row.latest_user_message_at),
5716+
latestUserAuthoredMessageAt:
5717+
row.latest_user_authored_message_at === null
5718+
? null
5719+
: DateTime.makeUnsafe(row.latest_user_authored_message_at),
5720+
activityRunStatus: null,
5721+
activityRunStartedAt: null,
5722+
pendingRuntimeRequest: null,
5723+
pendingBackgroundTasks: derivePendingBackgroundWork({
5724+
latestRun:
5725+
latestRunId === null || status === "idle"
5726+
? null
5727+
: { id: latestRunId, ordinal: 0, status },
5728+
providerThreads: providerThreadsByThreadId.get(thread.id) ?? [],
5729+
turnItems: pendingTurnItemsByThreadId.get(thread.id) ?? [],
5730+
activeProviderThreadId: thread.activeProviderThreadId,
5731+
hasActiveRun: false,
5732+
pullRequests: thread.pullRequests,
5733+
}),
5734+
} satisfies ProjectionSettlementCandidate;
5735+
}),
5736+
);
5737+
}),
5738+
),
5739+
Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })),
5740+
);
57155741

57165742
const getThreadsWithPullRequests: ProjectionStoreV2Shape["getThreadsWithPullRequests"] = (
57175743
threadId,
5744+
options,
57185745
) =>
57195746
Effect.gen(function* () {
57205747
const rows = yield* sql<PayloadRow>`
@@ -5723,9 +5750,17 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
57235750
WHERE deleted_at IS NULL${threadId === undefined ? sql`` : sql` AND thread_id = ${threadId}`}
57245751
AND json_extract(payload_json, '$.archivedAt') IS NULL
57255752
AND json_array_length(payload_json, '$.pullRequests') > 0
5753+
${
5754+
options?.watchedOnly
5755+
? sql`AND EXISTS (
5756+
SELECT 1 FROM json_each(payload_json, '$.pullRequests') AS link
5757+
WHERE json_type(link.value, '$.watch') IS NOT NULL
5758+
)`
5759+
: sql``
5760+
}
57265761
ORDER BY updated_at ASC, thread_id ASC
57275762
`;
5728-
return yield* Effect.forEach(rows, (row) =>
5763+
return yield* decodeBackgroundRows(rows, (row) =>
57295764
decodeThreadPayload(row.payload_json).pipe(
57305765
Effect.map((thread): ProjectionThreadPullRequests => ({
57315766
id: thread.id,
@@ -6252,7 +6287,7 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
62526287
),
62536288
),
62546289
),
6255-
getThreadsWithPullRequests: (threadId) =>
6290+
getThreadsWithPullRequests: (threadId, options) =>
62566291
Ref.get(replayState).pipe(
62576292
Effect.map((state) =>
62586293
[...state.projections.values()]
@@ -6262,7 +6297,9 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
62626297
(threadId === undefined || thread.id === threadId) &&
62636298
thread.deletedAt === null &&
62646299
thread.archivedAt === null &&
6265-
(thread.pullRequests ?? []).length > 0,
6300+
(thread.pullRequests ?? []).length > 0 &&
6301+
(!options?.watchedOnly ||
6302+
thread.pullRequests?.some((link) => link.watch !== undefined)),
62666303
)
62676304
.toSorted(
62686305
(left, right) =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ export const make = Effect.gen(function* () {
501501
});
502502

503503
const sweep = Effect.gen(function* () {
504-
const threads = yield* projections.getThreadsWithPullRequests();
504+
const threads = yield* projections.getThreadsWithPullRequests(undefined, { watchedOnly: true });
505505
const targets = threads.flatMap((thread) =>
506506
visibleThreadPullRequests(thread.pullRequests ?? []).flatMap((link) =>
507507
link.watch === undefined ? [] : [{ thread, link, watch: link.watch }],

0 commit comments

Comments
 (0)