Skip to content

Commit f0f1ce4

Browse files
only21milclaude
andcommitted
perf(server): read only thread rows for PR discovery
PR discovery built full thread shells, which count tool items, look up run errors and pending work, and walk fork sources. It uses none of that. It needs the thread's project, branch, worktree, saved PR links and settled or archived state. Add a projection read that returns the thread rows and the thread event high-water mark in one transaction, then decodes after it ends. Legacy single-link payloads still read as one link, as they do in shells. The sweep keeps its active and unsettled filters, startup backfill and sequence guard; a one-thread refresh still reads that thread wherever it lives. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6111850 commit f0f1ce4

6 files changed

Lines changed: 161 additions & 83 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,46 @@ it.effect.each([
399399
}).pipe(Effect.provide(testLayer)),
400400
);
401401

402+
it.effect.each([
403+
["sql", layerSql],
404+
["memory", ProjectionStore.layerMemory],
405+
] as const)("%s: pull-request discovery reads match its shell scope", ([, testLayer]) =>
406+
Effect.gen(function* () {
407+
const store = yield* ProjectionStore.ProjectionStoreV2;
408+
const legacy = yield* createThread("discovery-legacy", {
409+
linkedPullRequest: {
410+
projectId: ProjectId.make("project:settlement"),
411+
repository: "acme/project",
412+
number: 5,
413+
url: "https://github.com/acme/project/pull/5",
414+
},
415+
});
416+
const settled = yield* createThread("discovery-settled", { settledAt: old });
417+
const archived = yield* createThread("discovery-archived", { archivedAt: old });
418+
419+
const unsettled = yield* store.getPullRequestDiscoverySnapshot({ unsettledOnly: true });
420+
assert.deepEqual(
421+
unsettled.threads.map((thread) => thread.id),
422+
[legacy],
423+
);
424+
// Older payloads carry only linkedPullRequest; discovery must see it as a link, as shells do.
425+
assert.deepEqual(
426+
unsettled.threads[0]?.pullRequests?.map((link) => link.number),
427+
[5],
428+
);
429+
const active = yield* store.getPullRequestDiscoverySnapshot({});
430+
assert.deepEqual(
431+
new Set(active.threads.map((thread) => thread.id)),
432+
new Set([legacy, settled]),
433+
);
434+
const single = yield* store.getPullRequestDiscoverySnapshot({ threadId: archived });
435+
assert.deepEqual(
436+
single.threads.map((thread) => thread.id),
437+
[archived],
438+
);
439+
}).pipe(Effect.provide(testLayer)),
440+
);
441+
402442
it.effect.each([
403443
["sql", layerSql],
404444
["memory", ProjectionStore.layerMemory],

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

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

192+
export interface ProjectionPullRequestDiscoverySnapshot {
193+
readonly snapshotSequence: number;
194+
readonly threads: ReadonlyArray<OrchestrationV2AppThread>;
195+
}
196+
192197
export type ProjectionSettlementCandidate = Pick<
193198
OrchestrationV2ThreadShell,
194199
| "id"
@@ -512,6 +517,11 @@ export interface ProjectionStoreV2Shape {
512517
readonly getThreadsWithPullRequests: (
513518
threadId?: ThreadId,
514519
) => Effect.Effect<ReadonlyArray<ProjectionThreadPullRequests>, ProjectionStoreV2Error>;
520+
/** Thread rows without run or item history. A thread id reads it even when archived. */
521+
readonly getPullRequestDiscoverySnapshot: (options: {
522+
readonly threadId?: ThreadId;
523+
readonly unsettledOnly?: boolean;
524+
}) => Effect.Effect<ProjectionPullRequestDiscoverySnapshot, ProjectionStoreV2Error>;
515525
readonly getThreadWorktreePaths: (options?: {
516526
readonly includeArchived?: boolean;
517527
}) => Effect.Effect<
@@ -6605,6 +6615,43 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
66056615
.withTransaction(readShellSnapshotRows(options))
66066616
.pipe(Effect.mapError(shellSnapshotReadError), Effect.flatMap(decodeShellSnapshot));
66076617

6618+
const getPullRequestDiscoverySnapshot: ProjectionStoreV2Shape["getPullRequestDiscoverySnapshot"] =
6619+
Effect.fn("ProjectionStore.getPullRequestDiscoverySnapshot")(function* ({
6620+
threadId,
6621+
unsettledOnly = false,
6622+
}) {
6623+
const [sequenceRows, rows] = yield* sql.withTransaction(
6624+
Effect.all([
6625+
sql<{ readonly snapshot_sequence: number | null }>`
6626+
SELECT MAX(sequence) AS snapshot_sequence
6627+
FROM orchestration_events
6628+
WHERE application_event_version = 2
6629+
AND aggregate_kind = 'thread'
6630+
`,
6631+
sql<PayloadRow>`
6632+
SELECT payload_json
6633+
FROM orchestration_v2_projection_threads
6634+
WHERE deleted_at IS NULL${
6635+
threadId === undefined
6636+
? sql` AND json_extract(payload_json, '$.archivedAt') IS NULL`
6637+
: sql` AND thread_id = ${threadId}`
6638+
}${
6639+
unsettledOnly
6640+
? sql` AND json_extract(payload_json, '$.settledAt') IS NULL AND json_extract(payload_json, '$.settledOverride') IS NOT 'settled'`
6641+
: sql``
6642+
}
6643+
ORDER BY updated_at ASC, thread_id ASC
6644+
`,
6645+
]),
6646+
);
6647+
const threads = yield* Effect.forEach(rows, (row) =>
6648+
decodeThreadPayload(row.payload_json).pipe(
6649+
Effect.map((thread) => ({ ...thread, pullRequests: threadPullRequestsOf(thread) })),
6650+
),
6651+
);
6652+
return { snapshotSequence: sequenceRows[0]?.snapshot_sequence ?? 0, threads };
6653+
}, Effect.mapError(shellSnapshotReadError));
6654+
66086655
const listActiveSubagentThreadIds = Effect.fn("ProjectionStore.listActiveSubagentThreadIds")(
66096656
function* () {
66106657
const rows = yield* sql<{ readonly thread_id: string }>`
@@ -6780,6 +6827,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
67806827
getThread: readOnReader(getThread),
67816828
getSettlementCandidates: readOnReader(getSettlementCandidates),
67826829
getThreadsWithPullRequests: readOnReader(getThreadsWithPullRequests),
6830+
getPullRequestDiscoverySnapshot: readOnReader(getPullRequestDiscoverySnapshot),
67836831
getThreadWorktreePaths: readOnReader(getThreadWorktreePaths),
67846832
getThreadProjection,
67856833
getTurnStartContext: readOnReader(getTurnStartContext),
@@ -7060,6 +7108,29 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
70607108
})),
70617109
),
70627110
),
7111+
getPullRequestDiscoverySnapshot: Effect.fn("ProjectionStore.getPullRequestDiscoverySnapshot")(
7112+
function* ({ threadId, unsettledOnly = false }) {
7113+
const state = yield* Ref.get(replayState);
7114+
const threads = [...state.projections.values()]
7115+
.map(({ thread }) => thread)
7116+
.filter(
7117+
(thread) =>
7118+
thread.deletedAt === null &&
7119+
(threadId === undefined ? thread.archivedAt === null : thread.id === threadId) &&
7120+
!(
7121+
unsettledOnly &&
7122+
(thread.settledAt !== null || thread.settledOverride === "settled")
7123+
),
7124+
)
7125+
.toSorted(
7126+
(left, right) =>
7127+
DateTime.toEpochMillis(left.updatedAt) - DateTime.toEpochMillis(right.updatedAt) ||
7128+
left.id.localeCompare(right.id),
7129+
)
7130+
.map((thread) => ({ ...thread, pullRequests: threadPullRequestsOf(thread) }));
7131+
return { snapshotSequence: yield* Ref.get(sequence), threads };
7132+
},
7133+
),
70637134
getLimitRecoveryCandidates: (options) =>
70647135
Ref.get(replayState).pipe(
70657136
Effect.map((state) =>

‎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,8 @@ 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+
getPullRequestDiscoverySnapshot: () =>
223+
Effect.die("unused getPullRequestDiscoverySnapshot"),
222224
getThreadWorktreePaths: () => Effect.die("unused getThreadWorktreePaths"),
223225
getThreadProjection: () => Effect.die("control effects must not load transcript"),
224226
getTurnStartContext: () => Effect.die("unused"),

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

Lines changed: 42 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
type OrchestrationProjectShell,
88
type OrchestrationV2AppThread,
99
type OrchestrationV2DomainEvent,
10-
type OrchestrationV2ThreadShell,
1110
type OrchestrationV2ServerCommand,
1211
} from "@t3tools/contracts";
1312
import { describe, expect, it } from "@effect/vitest";
@@ -51,7 +50,7 @@ describe("ThreadPullRequestServiceV2 project guard", () => {
5150

5251
describe("ThreadPullRequestServiceV2 reads", () => {
5352
const NOW = DateTime.makeUnsafe("2026-09-20T00:00:00.000Z");
54-
const threadShell = (id: string): OrchestrationV2ThreadShell => {
53+
const appThread = (id: string): OrchestrationV2AppThread => {
5554
const threadId = ThreadId.make(id);
5655
return {
5756
id: threadId,
@@ -68,15 +67,6 @@ describe("ThreadPullRequestServiceV2 reads", () => {
6867
forkedFrom: null,
6968
createdBy: "user",
7069
creationSource: "web",
71-
activeRunId: null,
72-
latestRunId: null,
73-
status: "idle",
74-
pendingRuntimeRequest: null,
75-
latestVisibleMessage: null,
76-
latestUserMessageAt: null,
77-
hasActionableProposedPlan: false,
78-
itemCount: 0,
79-
visibleItemCount: 0,
8070
createdAt: NOW,
8171
updatedAt: NOW,
8272
archivedAt: null,
@@ -91,7 +81,7 @@ describe("ThreadPullRequestServiceV2 reads", () => {
9181
Effect.scoped(
9282
Effect.gen(function* () {
9383
// Polling active branches without a connected client wastes GitHub requests.
94-
const thread = { ...threadShell("idle-branch-thread"), branch: "feature/idle" };
84+
const thread = { ...appThread("idle-branch-thread"), branch: "feature/idle" };
9585
const project: OrchestrationProjectShell = {
9686
id: thread.projectId,
9787
title: "Project",
@@ -108,15 +98,10 @@ describe("ThreadPullRequestServiceV2 reads", () => {
10898
const dependencies = Layer.mergeAll(
10999
Layer.mock(Orchestrator.OrchestratorV2)({
110100
streamDomainEvents: Stream.fromQueue(domainEvents),
111-
getShellSnapshot: () =>
112-
Effect.succeed({
113-
schemaVersion: 2,
114-
snapshotSequence: 1,
115-
threads: [thread],
116-
archivedThreads: [],
117-
}),
118-
getThreadEventSequence: () => Effect.succeed(1),
119-
getThreadShell: () => Effect.succeed(thread),
101+
}),
102+
Layer.mock(ProjectionStore.ProjectionStoreV2)({
103+
getPullRequestDiscoverySnapshot: () =>
104+
Effect.succeed({ snapshotSequence: 1, threads: [thread] }),
120105
}),
121106
Layer.mock(ProjectStore.ProjectStoreV2)({
122107
listShells: () => Effect.succeed([project]),
@@ -202,7 +187,7 @@ describe("ThreadPullRequestServiceV2 reads", () => {
202187
url: "https://github.com/acme/project/pull/42",
203188
};
204189
const thread = {
205-
...threadShell("branch-pr-thread"),
190+
...appThread("branch-pr-thread"),
206191
branch: "main",
207192
branchPullRequest: savedReference,
208193
linkedPullRequest: savedReference,
@@ -222,21 +207,18 @@ describe("ThreadPullRequestServiceV2 reads", () => {
222207
const dependencies = Layer.mergeAll(
223208
Layer.mock(Orchestrator.OrchestratorV2)({
224209
streamDomainEvents: Stream.never,
225-
getShellSnapshot: () =>
226-
Deferred.succeed(sweepStarted, undefined).pipe(
227-
Effect.as({
228-
schemaVersion: 2,
229-
snapshotSequence: 1,
230-
threads: [thread],
231-
archivedThreads: [],
232-
}),
233-
),
234210
dispatch: (command) =>
235211
Effect.sync(() => {
236212
commands.push(command);
237213
return { sequence: 2, storedEvents: [] };
238214
}),
239215
}),
216+
Layer.mock(ProjectionStore.ProjectionStoreV2)({
217+
getPullRequestDiscoverySnapshot: () =>
218+
Deferred.succeed(sweepStarted, undefined).pipe(
219+
Effect.as({ snapshotSequence: 1, threads: [thread] }),
220+
),
221+
}),
240222
Layer.mock(ProjectStore.ProjectStoreV2)({
241223
listShells: () => Effect.succeed([project]),
242224
getShell: () => Effect.succeedSome(project),
@@ -307,30 +289,7 @@ describe("ThreadPullRequestServiceV2 reads", () => {
307289
it.effect("discovers a branch pull request without reading thread history", () =>
308290
Effect.scoped(
309291
Effect.gen(function* () {
310-
const shell = threadShell("history-free-thread");
311-
const thread: OrchestrationV2AppThread = {
312-
createdBy: shell.createdBy,
313-
creationSource: shell.creationSource,
314-
id: shell.id,
315-
projectId: shell.projectId,
316-
title: shell.title,
317-
providerInstanceId: shell.providerInstanceId,
318-
modelSelection: shell.modelSelection,
319-
runtimeMode: shell.runtimeMode,
320-
interactionMode: shell.interactionMode,
321-
branch: "feature/discovered",
322-
worktreePath: null,
323-
activeProviderThreadId: null,
324-
lineage: shell.lineage,
325-
forkedFrom: null,
326-
createdAt: NOW,
327-
updatedAt: NOW,
328-
archivedAt: null,
329-
settledOverride: null,
330-
settledAt: null,
331-
lastVisitedAt: null,
332-
deletedAt: null,
333-
};
292+
const thread = { ...appThread("history-free-thread"), branch: "feature/discovered" };
334293
const project: OrchestrationProjectShell = {
335294
id: thread.projectId,
336295
title: "Project",
@@ -353,22 +312,26 @@ describe("ThreadPullRequestServiceV2 reads", () => {
353312
const activation = yield* Deferred.make<void>();
354313
const sweepStarted = yield* Deferred.make<void>();
355314
const commands: Array<OrchestrationV2ServerCommand> = [];
356-
const shellRead = <A, E>(
357-
read: (store: ProjectionStore.ProjectionStoreV2["Service"]) => Effect.Effect<A, E>,
358-
) =>
315+
const projectionStore = Layer.effect(
316+
ProjectionStore.ProjectionStoreV2,
359317
Effect.gen(function* () {
360-
return yield* read(yield* ProjectionStore.ProjectionStoreV2);
361-
}).pipe(Effect.orDie);
318+
const store = yield* ProjectionStore.ProjectionStoreV2;
319+
return {
320+
...store,
321+
getPullRequestDiscoverySnapshot: (options) =>
322+
Deferred.succeed(sweepStarted, undefined).pipe(
323+
Effect.andThen(store.getPullRequestDiscoverySnapshot(options)),
324+
),
325+
};
326+
}),
327+
).pipe(
328+
Layer.provide(ProjectionStore.layer),
329+
Layer.provideMerge(SqlitePersistence.layerMemory),
330+
);
362331
const dependencies = Layer.mergeAll(
363-
ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
332+
projectionStore,
364333
Layer.mock(Orchestrator.OrchestratorV2)({
365334
streamDomainEvents: Stream.never,
366-
getShellSnapshot: (options) =>
367-
Deferred.succeed(sweepStarted, undefined).pipe(
368-
Effect.andThen(shellRead((store) => store.getShellSnapshot(options))),
369-
),
370-
getThreadEventSequence: () => Effect.succeed(0),
371-
getThreadShell: (threadId) => shellRead((store) => store.getThreadShell(threadId)),
372335
dispatch: (command) =>
373336
Effect.sync(() => {
374337
commands.push(command);
@@ -417,8 +380,18 @@ describe("ThreadPullRequestServiceV2 reads", () => {
417380
occurredAt: NOW,
418381
payload: thread,
419382
});
420-
// Any read of run, item, message or provider history now fails.
421383
const sql = yield* SqlClient.SqlClient;
384+
yield* sql`
385+
INSERT INTO orchestration_events (
386+
sequence, event_id, aggregate_kind, stream_id, stream_version, event_type,
387+
occurred_at, actor_kind, payload_json, metadata_json, application_event_version
388+
)
389+
VALUES (
390+
42, ${`event:${thread.id}`}, 'thread', ${thread.id}, 1, 'thread.created',
391+
${DateTime.formatIso(NOW)}, 'server', '{}', '{}', 2
392+
)
393+
`;
394+
// Any read of run, item, message or provider history now fails.
422395
const historyTables = yield* sql<{ readonly name: string }>`
423396
SELECT name FROM sqlite_master
424397
WHERE type = 'table'
@@ -438,6 +411,7 @@ describe("ThreadPullRequestServiceV2 reads", () => {
438411
expect(commands[0]).toMatchObject({
439412
type: "thread.pull-request.sync",
440413
threadId: thread.id,
414+
snapshotSequence: 42,
441415
branchPullRequest: {
442416
projectId: project.id,
443417
repository: "acme/project",

0 commit comments

Comments
 (0)