Skip to content

Commit a843632

Browse files
fix(server): PR sync sweep and run-status checks read only what they use
The minute PR-sync sweep read a full shell snapshot (runs, turn items, provider threads, messages) for every active thread, and each run-start status check loaded the thread's whole runtime recovery projection to read one run's activeAttemptId and status. - ProjectionStore.getPullRequestSyncSnapshot reads active, non-deleted thread payloads (archived_at column, unsettledOnly as the shell read) and the shell snapshot sequence in one reader transaction. The single-thread refresh uses it with a thread filter, so its thread and sequence are now read atomically too. - ProjectionStore.getRun reads one run by primary key; makeRunControls' isCurrentAttemptInStatus and shouldFinalizeRun use it and keep their ThreadNotFound and read-error behaviour. Refs #24895 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent fec62ad commit a843632

8 files changed

Lines changed: 246 additions & 55 deletions

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ import {
138138
type ProjectionCheckpointContext,
139139
type OlderSettledThreadsListing,
140140
type OlderSettledThreadsQuery,
141+
type ProjectionPullRequestSyncSnapshot,
141142
type ShellSnapshotOptions,
142143
} from "./ProjectionStore.ts";
143144
import { ProviderAdapterRegistryV2 } from "./ProviderAdapterRegistry.ts";
@@ -389,6 +390,9 @@ export interface OrchestratorV2Shape {
389390
readonly getShellSnapshot: (
390391
options?: ShellSnapshotOptions,
391392
) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, OrchestratorV2Error>;
393+
readonly getPullRequestSyncSnapshot: (
394+
options: Parameters<ProjectionStoreV2["Service"]["getPullRequestSyncSnapshot"]>[0],
395+
) => Effect.Effect<ProjectionPullRequestSyncSnapshot, OrchestratorV2Error>;
392396
readonly listActiveSubagentThreadIds: () => Effect.Effect<
393397
ReadonlyArray<ThreadId>,
394398
OrchestratorV2Error
@@ -12697,6 +12701,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1269712701
getShellSnapshot: Effect.fn("OrchestratorV2.getShellSnapshot")(function (options) {
1269812702
return projectionStore.getShellSnapshot(options).pipe(Effect.mapError(shellProjectionError));
1269912703
}),
12704+
getPullRequestSyncSnapshot: Effect.fn("OrchestratorV2.getPullRequestSyncSnapshot")(
12705+
function (options) {
12706+
return projectionStore
12707+
.getPullRequestSyncSnapshot(options)
12708+
.pipe(Effect.mapError(shellProjectionError));
12709+
},
12710+
),
1270012711
listActiveSubagentThreadIds: Effect.fn("OrchestratorV2.listActiveSubagentThreadIds")(
1270112712
function () {
1270212713
return projectionStore

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,99 @@ it.effect.each([
11921192
}).pipe(Effect.provide(layer)),
11931193
);
11941194

1195+
it.effect("pull request sync reads the shell snapshot's threads and sequence", () =>
1196+
Effect.gen(function* () {
1197+
type SyncThread = ProjectionStore.ProjectionPullRequestSyncSnapshot["threads"][number];
1198+
const store = yield* ProjectionStore.ProjectionStoreV2;
1199+
const now = yield* DateTime.now;
1200+
const id = (name: string) => ThreadId.make(`thread:pr-sync:${name}`);
1201+
const payloadOf = (
1202+
name: string,
1203+
state: Partial<Pick<SyncThread, "archivedAt" | "settledOverride" | "settledAt">> = {},
1204+
) =>
1205+
({
1206+
createdBy: "user",
1207+
creationSource: "web",
1208+
id: id(name),
1209+
projectId: ProjectId.make("project:pr-sync"),
1210+
title: name,
1211+
providerInstanceId,
1212+
modelSelection,
1213+
runtimeMode: "full-access",
1214+
interactionMode: "default",
1215+
branch: `feature/${name}`,
1216+
worktreePath: null,
1217+
activeProviderThreadId: null,
1218+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: id(name) },
1219+
forkedFrom: null,
1220+
createdAt: now,
1221+
updatedAt: now,
1222+
archivedAt: null,
1223+
settledOverride: null,
1224+
settledAt: null,
1225+
lastVisitedAt: null,
1226+
deletedAt: null,
1227+
...state,
1228+
}) as const;
1229+
const create = (name: string, state?: Parameters<typeof payloadOf>[1]) =>
1230+
store.apply({
1231+
id: EventId.make(`event:pr-sync:${name}`),
1232+
type: "thread.created",
1233+
threadId: id(name),
1234+
occurredAt: now,
1235+
payload: payloadOf(name, state),
1236+
});
1237+
yield* create("open");
1238+
yield* create("settled", { settledOverride: "settled", settledAt: now });
1239+
yield* create("auto-settled", { settledAt: now });
1240+
yield* create("archived", { archivedAt: now });
1241+
yield* create("deleted");
1242+
yield* store.apply({
1243+
id: EventId.make("event:pr-sync:deleted:deleted"),
1244+
type: "thread.deleted",
1245+
threadId: id("deleted"),
1246+
occurredAt: now,
1247+
payload: { ...payloadOf("deleted"), deletedAt: now },
1248+
});
1249+
1250+
const pick = (thread: SyncThread) => ({
1251+
id: thread.id,
1252+
projectId: thread.projectId,
1253+
branch: thread.branch,
1254+
worktreePath: thread.worktreePath,
1255+
pullRequests: thread.pullRequests,
1256+
linkedPullRequest: thread.linkedPullRequest,
1257+
branchPullRequest: thread.branchPullRequest,
1258+
archivedAt: thread.archivedAt,
1259+
settledOverride: thread.settledOverride,
1260+
settledAt: thread.settledAt,
1261+
});
1262+
for (const options of [
1263+
{ location: "active" },
1264+
{ location: "active", unsettledOnly: true },
1265+
{ threadIds: [id("archived")] },
1266+
{ threadIds: [id("deleted")] },
1267+
] as const) {
1268+
const shell = yield* store.getShellSnapshot(options);
1269+
const sync = yield* store.getPullRequestSyncSnapshot(options);
1270+
assert.deepEqual(
1271+
{ snapshotSequence: sync.snapshotSequence, threads: sync.threads.map(pick) },
1272+
{
1273+
snapshotSequence: shell.snapshotSequence,
1274+
threads: [...shell.threads, ...shell.archivedThreads].map(pick),
1275+
},
1276+
);
1277+
}
1278+
assert.deepEqual(
1279+
(yield* store.getPullRequestSyncSnapshot({
1280+
location: "active",
1281+
unsettledOnly: true,
1282+
})).threads.map((thread) => thread.id),
1283+
[id("open")],
1284+
);
1285+
}).pipe(Effect.provide(layerTest)),
1286+
);
1287+
11951288
it.effect.each([
11961289
["memory", ProjectionStore.layerMemory],
11971290
["SQL", layerTest],

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

Lines changed: 111 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,25 @@ export type ProjectionThreadPullRequests = Pick<
189189
"id" | "projectId" | "lineage" | "settledOverride" | "settledAt" | "pullRequests"
190190
>;
191191

192+
export interface ProjectionPullRequestSyncSnapshot {
193+
readonly snapshotSequence: number;
194+
readonly threads: ReadonlyArray<
195+
Pick<
196+
OrchestrationV2ThreadShell,
197+
| "id"
198+
| "projectId"
199+
| "branch"
200+
| "worktreePath"
201+
| "pullRequests"
202+
| "linkedPullRequest"
203+
| "branchPullRequest"
204+
| "archivedAt"
205+
| "settledOverride"
206+
| "settledAt"
207+
>
208+
>;
209+
}
210+
192211
export type ProjectionSettlementCandidate = Pick<
193212
OrchestrationV2ThreadShell,
194213
| "id"
@@ -512,6 +531,12 @@ export interface ProjectionStoreV2Shape {
512531
readonly getThreadsWithPullRequests: (
513532
threadId?: ThreadId,
514533
) => Effect.Effect<ReadonlyArray<ProjectionThreadPullRequests>, ProjectionStoreV2Error>;
534+
/** The shell snapshot's threads and sequence without the run, item and message reads. */
535+
readonly getPullRequestSyncSnapshot: (
536+
options: Pick<ShellSnapshotOptions, "unsettledOnly" | "threadIds"> & {
537+
readonly location?: "active";
538+
},
539+
) => Effect.Effect<ProjectionPullRequestSyncSnapshot, ProjectionStoreV2Error>;
515540
readonly getThreadWorktreePaths: (options?: {
516541
readonly includeArchived?: boolean;
517542
}) => Effect.Effect<
@@ -610,6 +635,10 @@ export interface ProjectionStoreV2Shape {
610635
threadId: ThreadId,
611636
runId: RunId,
612637
) => Effect.Effect<OrchestrationV2ConversationMessage | undefined, ProjectionStoreV2Error>;
638+
readonly getRun: (
639+
threadId: ThreadId,
640+
runId: RunId,
641+
) => Effect.Effect<OrchestrationV2Run | undefined, ProjectionStoreV2Error>;
613642
readonly canStartQueuedRun: (
614643
threadId: ThreadId,
615644
) => Effect.Effect<boolean, ProjectionStoreV2Error>;
@@ -5265,6 +5294,21 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
52655294
)
52665295
.pipe(Effect.mapError(controlReadError(threadId)));
52675296

5297+
const getRun: ProjectionStoreV2Shape["getRun"] = (threadId, runId) =>
5298+
sql
5299+
.withTransaction(
5300+
Effect.gen(function* () {
5301+
yield* requireThread(threadId);
5302+
const rows = yield* sql<PayloadRow>`
5303+
SELECT payload_json FROM orchestration_v2_projection_runs
5304+
WHERE run_id = ${runId} AND thread_id = ${threadId}`;
5305+
return rows[0] === undefined
5306+
? undefined
5307+
: yield* decodeRunPayload(rows[0].payload_json);
5308+
}),
5309+
)
5310+
.pipe(Effect.mapError(controlReadError(threadId)));
5311+
52685312
const canStartQueuedRun: ProjectionStoreV2Shape["canStartQueuedRun"] = (threadId) =>
52695313
sql
52705314
.withTransaction(
@@ -6474,6 +6518,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
64746518
const shellSnapshotReadError = (cause: unknown) =>
64756519
new ProjectionStoreReadError({ threadId: ThreadId.make("thread:shell"), cause });
64766520

6521+
const selectSnapshotSequence = sql<{ readonly snapshot_sequence: number | null }>`
6522+
SELECT MAX(sequence) AS snapshot_sequence
6523+
FROM orchestration_events
6524+
WHERE application_event_version = 2
6525+
AND aggregate_kind = 'thread'
6526+
`;
6527+
64776528
const readShellSnapshotRows = Effect.fn("ProjectionStore.readShellSnapshotRows")(function* (
64786529
options: ShellSnapshotOptions | undefined,
64796530
) {
@@ -6515,12 +6566,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
65156566
readForThreadIds(selectShellRunRows, forkSourceIds),
65166567
readForThreadIds(selectShellRunItemCounts, forkSourceIds),
65176568
readForThreadIds(selectShellItemCounts),
6518-
sql<{ readonly snapshot_sequence: number | null }>`
6519-
SELECT MAX(sequence) AS snapshot_sequence
6520-
FROM orchestration_events
6521-
WHERE application_event_version = 2
6522-
AND aggregate_kind = 'thread'
6523-
`,
6569+
selectSnapshotSequence,
65246570
readForThreadIds(selectShellProviderThreadRows),
65256571
readForThreadIds((ids) =>
65266572
selectShellPendingTurnItemRows(
@@ -6605,6 +6651,50 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
66056651
.withTransaction(readShellSnapshotRows(options))
66066652
.pipe(Effect.mapError(shellSnapshotReadError), Effect.flatMap(decodeShellSnapshot));
66076653

6654+
const getPullRequestSyncSnapshot: ProjectionStoreV2Shape["getPullRequestSyncSnapshot"] =
6655+
Effect.fn("ProjectionStore.getPullRequestSyncSnapshot")(function* ({
6656+
location,
6657+
unsettledOnly = false,
6658+
threadIds,
6659+
}) {
6660+
const [rows, sequenceRows] = yield* sql.withTransaction(
6661+
Effect.all([
6662+
sql<PayloadRow>`
6663+
SELECT payload_json
6664+
FROM orchestration_v2_projection_threads
6665+
WHERE deleted_at IS NULL${location === "active" ? sql` AND archived_at IS NULL` : sql``}${
6666+
unsettledOnly
6667+
? sql` AND json_extract(payload_json, '$.settledAt') IS NULL AND json_extract(payload_json, '$.settledOverride') IS NOT 'settled'`
6668+
: sql``
6669+
}${threadIds === undefined ? sql`` : sql` AND thread_id IN ${sql.in(threadIds)}`}
6670+
ORDER BY updated_at ASC, thread_id ASC
6671+
`,
6672+
selectSnapshotSequence,
6673+
]),
6674+
);
6675+
const threads = yield* Effect.forEach(rows, (row) =>
6676+
decodeThreadPayload(row.payload_json).pipe(
6677+
Effect.map((thread) => ({
6678+
id: thread.id,
6679+
projectId: thread.projectId,
6680+
branch: thread.branch,
6681+
worktreePath: thread.worktreePath,
6682+
pullRequests: threadPullRequestsOf(thread),
6683+
...(thread.linkedPullRequest === undefined
6684+
? {}
6685+
: { linkedPullRequest: thread.linkedPullRequest }),
6686+
...(thread.branchPullRequest === undefined
6687+
? {}
6688+
: { branchPullRequest: thread.branchPullRequest }),
6689+
archivedAt: thread.archivedAt,
6690+
settledOverride: thread.settledOverride,
6691+
settledAt: thread.settledAt,
6692+
})),
6693+
),
6694+
);
6695+
return { snapshotSequence: sequenceRows[0]?.snapshot_sequence ?? 0, threads };
6696+
}, Effect.mapError(shellSnapshotReadError));
6697+
66086698
const listActiveSubagentThreadIds = Effect.fn("ProjectionStore.listActiveSubagentThreadIds")(
66096699
function* () {
66106700
const rows = yield* sql<{ readonly thread_id: string }>`
@@ -6780,6 +6870,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
67806870
getThread: readOnReader(getThread),
67816871
getSettlementCandidates: readOnReader(getSettlementCandidates),
67826872
getThreadsWithPullRequests: readOnReader(getThreadsWithPullRequests),
6873+
getPullRequestSyncSnapshot: readOnReader(getPullRequestSyncSnapshot),
67836874
getThreadWorktreePaths: readOnReader(getThreadWorktreePaths),
67846875
getThreadProjection,
67856876
getTurnStartContext: readOnReader(getTurnStartContext),
@@ -6792,6 +6883,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
67926883
getCheckpointCaptureContext: readOnReader(getCheckpointCaptureContext),
67936884
getCheckpointRollbackContext: readOnReader(getCheckpointRollbackContext),
67946885
getRunMessage: readOnReader(getRunMessage),
6886+
getRun: readOnReader(getRun),
67956887
canStartQueuedRun: readOnReader(canStartQueuedRun),
67966888
getPendingNativeUserInputs: readOnReader(getPendingNativeUserInputs),
67976889
hasUnpairedRunInterruptRequest: readOnReader(hasUnpairedRunInterruptRequest),
@@ -7060,6 +7152,13 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
70607152
})),
70617153
),
70627154
),
7155+
getPullRequestSyncSnapshot: (options) =>
7156+
service.getShellSnapshot(options).pipe(
7157+
Effect.map((snapshot) => ({
7158+
snapshotSequence: snapshot.snapshotSequence,
7159+
threads: [...snapshot.threads, ...snapshot.archivedThreads],
7160+
})),
7161+
),
70637162
getLimitRecoveryCandidates: (options) =>
70647163
Ref.get(replayState).pipe(
70657164
Effect.map((state) =>
@@ -7500,6 +7599,12 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
75007599
const run = projection.runs.find((candidate) => candidate.id === runId);
75017600
return projection.messages.find((message) => message.id === run?.userMessageId);
75027601
}),
7602+
getRun: Effect.fn("ProjectionStore.getRun")(function* (threadId, runId) {
7603+
const projection = (yield* Ref.get(replayState)).projections.get(threadId);
7604+
if (projection === undefined)
7605+
return yield* new ProjectionStoreThreadNotFoundError({ threadId });
7606+
return projection.runs.find((candidate) => candidate.id === runId);
7607+
}),
75037608
canStartQueuedRun: Effect.fn("ProjectionStore.canStartQueuedRun")(function* (threadId) {
75047609
const projection = (yield* Ref.get(replayState)).projections.get(threadId);
75057610
if (projection === undefined)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ it.effect(
219219
getThread: () => Ref.get(projection).pipe(Effect.map((state) => state.thread)),
220220
getSettlementCandidates: () => Effect.die("unused getSettlementCandidates"),
221221
getThreadsWithPullRequests: () => Effect.die("unused getThreadsWithPullRequests"),
222+
getPullRequestSyncSnapshot: () => Effect.die("unused getPullRequestSyncSnapshot"),
222223
getThreadWorktreePaths: () => Effect.die("unused getThreadWorktreePaths"),
223224
getThreadProjection: () => Effect.die("control effects must not load transcript"),
224225
getTurnStartContext: () => Effect.die("unused"),
@@ -258,6 +259,7 @@ it.effect(
258259
getCheckpointRollbackContext: () => Effect.die("unused rollback context"),
259260
getCheckpointCaptureContext: () => Effect.die("not used"),
260261
getRunMessage: () => Effect.die("not used"),
262+
getRun: () => Effect.die("not used"),
261263
isNodeStored: () => Effect.die("not used"),
262264
canStartQueuedRun: () => Effect.die("not used"),
263265
getRecoveryThreadIds: () => Effect.die("unused getRecoveryThreadIds"),

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,11 @@ function makeLocalCommandHarness(input: {
731731
getThreadRecords,
732732
getTurnStartContext,
733733
getRuntimeRecoveryProjection,
734+
getRun: (_threadId, id) =>
735+
Effect.as(
736+
failReadIfRunning,
737+
projection.runs.find((candidate) => candidate.id === id),
738+
),
734739
getTurnStartHistory: () =>
735740
Effect.fail(
736741
new ProjectionStore.ProjectionStoreReadError({
@@ -1362,10 +1367,11 @@ effectIt.effect("checks the run's status without loading the thread's history",
13621367
expect(yield* controls.shouldStartProviderTurn!()).toBe(true);
13631368
expect(yield* controls.shouldFinalizeRun!()).toBe(true);
13641369
// Each check needs one run row; the recovery projection reads every run and open turn item.
1370+
// The one read left is the start's inherited background items snapshot, not a status check.
13651371
expect(
13661372
harness.getRuntimeRecoveryProjection.mock.calls.length,
1367-
"run status checks that loaded the thread's recovery projection",
1368-
).toBe(0);
1373+
"recovery projection reads beyond the inherited background items snapshot",
1374+
).toBe(1);
13691375
}),
13701376
);
13711377

0 commit comments

Comments
 (0)