Skip to content
TypeScript

perf(server): read pull request metadata from a lightweight snapshot

Yash Singh0b5600b1c3df
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index 1059c882307..16256585dc5 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -438,6 +438,12 @@ export interface OrchestratorV2Shape {
},
OrchestratorV2Error
>;
+ readonly getPullRequestSnapshot: (
+ options: Parameters<ProjectionStoreV2["Service"]["getPullRequestSnapshot"]>[0],
+ ) => Effect.Effect<
+ Effect.Success<ReturnType<ProjectionStoreV2["Service"]["getPullRequestSnapshot"]>>,
+ OrchestratorV2Error
+ >;
readonly getShellSnapshot: (
options?: ShellSnapshotOptions,
) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, OrchestratorV2Error>;
@@ -14143,6 +14149,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
},
),
+ getPullRequestSnapshot: Effect.fn("OrchestratorV2.getPullRequestSnapshot")(function (options) {
+ return projectionStore
+ .getPullRequestSnapshot(options)
+ .pipe(Effect.mapError(shellProjectionError));
+ }),
getShellSnapshot: Effect.fn("OrchestratorV2.getShellSnapshot")(function (options) {
return projectionStore.getShellSnapshot(options).pipe(Effect.mapError(shellProjectionError));
}),
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts
index a79b2f37a1d..f453dec9567 100644
--- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts
+++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts
@@ -32,6 +32,7 @@ import {
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
+import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import * as Tracer from "effect/Tracer";
import * as SqlClient from "effect/sql/SqlClient";
@@ -64,6 +65,7 @@ const encodeUnknownJsonString = Schema.encodeSync(Schema.fromJsonString(Schema.U
const traceSqlStatements = () => {
const statements: Array<string> = [];
+ const queryTransactions: Array<string | null> = [];
const tracer = Tracer.make({
span(options) {
const span = new Tracer.NativeSpan(options);
@@ -71,12 +73,19 @@ const traceSqlStatements = () => {
span.end = (endTime, exit) => {
end(endTime, exit);
const query = span.attributes.get("db.query.text");
- if (typeof query === "string") statements.push(query);
+ if (typeof query === "string") {
+ statements.push(query);
+ let parent = Option.getOrUndefined(span.parent);
+ while (parent?._tag === "Span" && parent.name !== "sql.transaction") {
+ parent = Option.getOrUndefined(parent.parent);
+ }
+ queryTransactions.push(parent?._tag === "Span" ? parent.spanId : null);
+ }
};
return span;
},
});
- return { statements, tracer };
+ return { statements, tracer, queryTransactions };
};
/** The shell index flags every live shell and matches its shells on the fields subscriber trims read. */
@@ -3793,6 +3802,76 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
.filter((id) => id === readThreadId || id === laterThreadId);
yield* createThread(readThreadId);
+ yield* sql`UPDATE orchestration_v2_projection_threads
+ SET payload_json = json_set(payload_json, '$.linkedPullRequest', json(${JSON.stringify({
+ projectId: "project:shell-read",
+ repository: "acme/project",
+ number: 42,
+ url: "https://github.com/acme/project/pull/42",
+ })})) WHERE thread_id = ${readThreadId}`;
+ const trace = traceSqlStatements();
+ const discovery = yield* projectionStore
+ .getPullRequestSnapshot({})
+ .pipe(Effect.withTracer(trace.tracer));
+ assert.deepEqual(
+ discovery.threads
+ .map((thread) => thread.id)
+ .filter((id) => id === readThreadId || id === laterThreadId),
+ [readThreadId],
+ );
+ assert.strictEqual(discovery.snapshotSequence, 0);
+ assert.strictEqual(trace.queryTransactions.length, 2);
+ assert.isNotNull(trace.queryTransactions[0]);
+ assert.strictEqual(new Set(trace.queryTransactions).size, 1);
+ assert.isFalse(
+ trace.statements.some((query) =>
+ /orchestration_v2_projection_(runs|messages|turn_items|provider_threads)/.test(query),
+ ),
+ );
+ const fullShell = (yield* projectionStore.getShellSnapshot()).threads.find(
+ (thread) => thread.id === readThreadId,
+ )!;
+ const metadata = discovery.threads.find((thread) => thread.id === readThreadId)!;
+ for (const key of Object.keys(metadata) as Array<keyof typeof metadata>) {
+ assert.deepEqual(metadata[key], fullShell[key]);
+ }
+ const narrow = yield* projectionStore.getPullRequestSnapshot({ threadId: readThreadId });
+ assert.deepEqual(
+ narrow.threads,
+ discovery.threads.filter((thread) => thread.id === readThreadId),
+ );
+ const settledPayload = (settledAt: string | null, archivedAt: string | null) => sql`
+ UPDATE orchestration_v2_projection_threads
+ SET payload_json = json_set(payload_json, '$.settledAt', ${settledAt}, '$.archivedAt', ${archivedAt})
+ WHERE thread_id = ${readThreadId}
+ `;
+ yield* settledPayload(DateTime.formatIso(now), null);
+ assert.isFalse(
+ (yield* projectionStore.getPullRequestSnapshot({ unsettledOnly: true })).threads.some(
+ (thread) => thread.id === readThreadId,
+ ),
+ );
+ assert.strictEqual(
+ (yield* projectionStore.getPullRequestSnapshot({ threadId: readThreadId })).threads.length,
+ 1,
+ );
+ yield* settledPayload(null, DateTime.formatIso(now));
+ assert.isFalse(
+ (yield* projectionStore.getPullRequestSnapshot({})).threads.some(
+ (thread) => thread.id === readThreadId,
+ ),
+ );
+ assert.strictEqual(
+ (yield* projectionStore.getPullRequestSnapshot({ threadId: readThreadId })).threads.length,
+ 1,
+ );
+ yield* sql`UPDATE orchestration_v2_projection_threads SET deleted_at = '2026-11-03T00:00:00.000Z' WHERE thread_id = ${readThreadId}`;
+ assert.deepEqual(
+ (yield* projectionStore.getPullRequestSnapshot({ threadId: readThreadId })).threads,
+ [],
+ );
+ yield* sql`UPDATE orchestration_v2_projection_threads SET deleted_at = NULL WHERE thread_id = ${readThreadId}`;
+ yield* settledPayload(null, null);
const decode = yield* sql.withTransaction(projectionStore.readShellSnapshot());
yield* createThread(laterThreadId);
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts
index d7a5fc4a18d..8726799153c 100644
--- a/apps/server/src/orchestration-v2/ProjectionStore.ts
+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts
@@ -481,6 +481,16 @@ export interface ProjectionStoreV2Shape {
readonly apply: (
event: OrchestrationV2DomainEvent,
) => Effect.Effect<void, ProjectionStoreV2Error>;
+ readonly getPullRequestSnapshot: (options: {
+ readonly threadId?: ThreadId;
+ readonly unsettledOnly?: boolean;
+ }) => Effect.Effect<
+ {
+ readonly snapshotSequence: number;
+ readonly threads: ReadonlyArray<typeof PullRequestThread.Type>;
+ },
+ ProjectionStoreV2Error
+ >;
readonly getShellSnapshot: (
options?: ShellSnapshotOptions,
) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, ProjectionStoreV2Error>;
@@ -1097,6 +1107,37 @@ const encodeContextTransferPayload = Schema.encodeEffect(
Schema.fromJsonString(OrchestrationV2ContextTransferJsonSchema),
);
+const PullRequestThread = OrchestrationV2AppThreadJsonSchema.mapFields(
+ ({
+ id,
+ projectId,
+ lineage,
+ branch,
+ worktreePath,
+ archivedAt,
+ settledOverride,
+ settledAt,
+ linkedPullRequest,
+ branchPullRequest,
+ pullRequests,
+ }) => ({
+ id,
+ projectId,
+ lineage,
+ branch,
+ worktreePath,
+ archivedAt,
+ settledOverride,
+ settledAt,
+ linkedPullRequest,
+ branchPullRequest,
+ pullRequests,
+ }),
+);
+const decodePullRequestThread = Schema.decodeUnknownEffect(
+ Schema.fromJsonString(PullRequestThread),
+);
+
const decodeThreadPayload = Schema.decodeUnknownEffect(
Schema.fromJsonString(OrchestrationV2AppThreadJsonSchema),
);
@@ -6895,6 +6936,41 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
Effect.map(decodeShellSnapshot),
);
+ const getPullRequestSnapshot: ProjectionStoreV2Shape["getPullRequestSnapshot"] = Effect.fn(
+ "ProjectionStore.getPullRequestSnapshot",
+ )(function* ({ threadId, unsettledOnly = false }) {
+ const { rows, sequenceRows } = yield* sql.withTransaction(
+ Effect.gen(function* () {
+ const rows = yield* sql<PayloadRow>`
+ SELECT t.payload_json FROM orchestration_v2_projection_threads t
+ WHERE t.deleted_at IS NULL${
+ threadId === undefined
+ ? sql` AND json_extract(t.payload_json, '$.archivedAt') IS NULL`
+ : sql` AND t.thread_id = ${threadId}`
+ }${
+ unsettledOnly
+ ? sql` AND json_extract(t.payload_json, '$.settledAt') IS NULL
+ AND json_extract(t.payload_json, '$.settledOverride') IS NOT 'settled'`
+ : sql``
+ }
+ ORDER BY t.updated_at ASC, t.thread_id ASC
+ `;
+ const sequenceRows = yield* sql<{ readonly snapshot_sequence: number | null }>`
+ SELECT MAX(sequence) AS snapshot_sequence FROM orchestration_events
+ WHERE application_event_version = 2 AND aggregate_kind = 'thread'
+ `;
+ return { rows, sequenceRows };
+ }),
+ );
+ // Decode after releasing the reader, keeping rows and sequence from the same transaction.
+ const threads = yield* Effect.forEach(rows, (row) =>
+ decodePullRequestThread(row.payload_json).pipe(
+ Effect.map((thread) => ({ ...thread, pullRequests: threadPullRequestsOf(thread) })),
+ ),
+ );
+ return { snapshotSequence: sequenceRows[0]?.snapshot_sequence ?? 0, threads };
+ }, Effect.mapError(shellSnapshotReadError));
+
const getShellSnapshot: ProjectionStoreV2Shape["getShellSnapshot"] = (options) =>
sql
.withTransaction(readShellSnapshotRows(options))
@@ -7163,6 +7239,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
return {
apply,
getShellSnapshot: readOnReader(getShellSnapshot),
+ getPullRequestSnapshot: readOnReader(getPullRequestSnapshot),
listActiveSubagentThreadIds: readOnReader(listActiveSubagentThreadIds),
countThreads: readOnReader(countThreads),
listActiveShellIndex: readOnReader(listActiveShellIndex),
diff --git a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
index 77caf037125..bc565609444 100644
--- a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
+++ b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
@@ -271,6 +271,7 @@ it.effect(
getUnreadableThreadIds: () => Effect.die("unused getUnreadableThreadIds"),
getThreadSnapshot: () => Effect.die("unused getThreadSnapshot"),
getThreadSnapshotWindow: () => Effect.die("unused getThreadSnapshotWindow"),
+ getPullRequestSnapshot: () => Effect.die("unused getPullRequestSnapshot"),
}),
);
const layerSessionManager = Layer.succeed(
diff --git a/apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts b/apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts
index 9c2ff9ef18f..423d1cddd94 100644
--- a/apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts
+++ b/apps/server/src/orchestration-v2/ThreadPullRequestService.test.ts
@@ -28,6 +28,10 @@ import * as ProjectStore from "./ProjectStore.ts";
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
import * as ServerActivation from "../serverActivation.ts";
+import * as ProjectionStore from "./ProjectionStore.ts";
+import * as SqlClient from "effect/sql/SqlClient";
+import * as SqlitePersistence from "../persistence/Sqlite.ts";
+import { EventId } from "@t3tools/contracts";
import * as Orchestrator from "./Orchestrator.ts";
import * as ThreadPullRequestService from "./ThreadPullRequestService.ts";
@@ -105,15 +109,13 @@ describe("ThreadPullRequestServiceV2 reads", () => {
const dependencies = Layer.mergeAll(
Layer.mock(Orchestrator.OrchestratorV2)({
streamDomainEvents: Stream.fromQueue(domainEvents),
- getShellSnapshot: () =>
+ getPullRequestSnapshot: () =>
Effect.succeed({
schemaVersion: 2,
snapshotSequence: 1,
threads: [thread],
archivedThreads: [],
}),
- getThreadEventSequence: () => Effect.succeed(1),
- getThreadShell: () => Effect.succeed(thread),
}),
Layer.mock(ProjectStore.ProjectStoreV2)({
listShells: () => Effect.succeed([project]),
@@ -212,6 +214,8 @@ describe("ThreadPullRequestServiceV2 reads", () => {
Effect.gen(function* () {
const thread = {
...threadShell("worktree-thread"),
+ settledOverride: "settled" as const,
+ settledAt: NOW,
branch: "feature/worktree",
worktreePath: "/workspace/worktree",
};
@@ -230,7 +234,7 @@ describe("ThreadPullRequestServiceV2 reads", () => {
const dependencies = Layer.mergeAll(
Layer.mock(Orchestrator.OrchestratorV2)({
streamDomainEvents: Stream.never,
- getShellSnapshot: () =>
+ getPullRequestSnapshot: () =>
Deferred.succeed(sweepStarted, undefined).pipe(
Effect.as({
schemaVersion: 2,
@@ -335,18 +339,17 @@ describe("ThreadPullRequestServiceV2 reads", () => {
const dependencies = Layer.mergeAll(
Layer.mock(Orchestrator.OrchestratorV2)({
streamDomainEvents: Stream.fromQueue(domainEvents),
- getShellSnapshot: () =>
- Queue.offer(sweeps, undefined).pipe(
- Effect.as({
- schemaVersion: 2,
- snapshotSequence: 1,
- threads: [parent, child],
- archivedThreads: [],
- }),
- ),
- getThreadEventSequence: () => Effect.succeed(1),
- getThreadShell: (threadId) =>
- Effect.succeed([parent, child].find((thread) => thread.id === threadId) ?? null),
+ getPullRequestSnapshot: ({ threadId }) =>
+ threadId !== undefined
+ ? Effect.succeed({ snapshotSequence: 1, threads: [child] })
+ : Queue.offer(sweeps, undefined).pipe(
+ Effect.as({
+ schemaVersion: 2,
+ snapshotSequence: 1,
+ threads: [parent, child],
+ archivedThreads: [],
+ }),
+ ),
}),
Layer.mock(ProjectStore.ProjectStoreV2)({
listShells: () => Effect.succeed([project]),
@@ -457,14 +460,37 @@ describe("ThreadPullRequestServiceV2 reads", () => {
const dependencies = Layer.mergeAll(
Layer.mock(Orchestrator.OrchestratorV2)({
streamDomainEvents: Stream.never,
- getShellSnapshot: () =>
- Deferred.succeed(sweepStarted, undefined).pipe(
- Effect.as({
- schemaVersion: 2,
- snapshotSequence: 1,
- threads: [thread],
- archivedThreads: [],
- }),
+ getPullRequestSnapshot: (options) =>
+ Effect.gen(function* () {
+ const store = yield* ProjectionStore.ProjectionStoreV2;
+ yield* store.apply({
+ id: EventId.make(`event:${thread.id}:created`),
+ type: "thread.created",
+ threadId: thread.id,
+ occurredAt: NOW,
+ payload: { ...thread, lastVisitedAt: thread.lastVisitedAt ?? null },
+ });
+ const sql = yield* SqlClient.SqlClient;
+ yield* sql`INSERT INTO orchestration_events (
+ sequence, event_id, aggregate_kind, stream_id, stream_version, event_type,
+ occurred_at, actor_kind, payload_json, metadata_json, application_event_version
+ ) VALUES (7, 'pr-discovery', 'thread', ${thread.id}, 1, 'thread.created',
+ '2026-09-20T00:00:00.000Z', 'system', '{}', '{}', 2)`;
+ for (const table of [
+ "orchestration_v2_projection_turn_items",
+ "orchestration_v2_projection_messages",
+ "orchestration_v2_projection_provider_threads",
+ "orchestration_v2_projection_runs",
+ ])
+ yield* sql.unsafe(`DROP TABLE ${table}`);
+ const snapshot = yield* store.getPullRequestSnapshot(options);
+ yield* Deferred.succeed(sweepStarted, undefined);
+ return snapshot;
+ }).pipe(
+ Effect.provide(
+ ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
+ ),
+ Effect.orDie,
),
dispatch: (command) =>
Effect.sync(() => {
@@ -529,6 +555,14 @@ describe("ThreadPullRequestServiceV2 reads", () => {
expect(commands[0]).toMatchObject({
type: "thread.pull-request.sync",
threadId: thread.id,
+ snapshotSequence: 7,
+ expected: {
+ workspaceRoot: project.workspaceRoot,
+ branch: thread.branch,
+ worktreePath: thread.worktreePath,
+ linkedPullRequest: savedReference,
+ branchPullRequest: savedReference,
+ },
branchPullRequest: null,
});
expect(commands[0]).not.toHaveProperty("linkedPullRequest");
diff --git a/apps/server/src/orchestration-v2/ThreadPullRequestService.ts b/apps/server/src/orchestration-v2/ThreadPullRequestService.ts
index e221388a5b8..1cb01acd4ed 100644
--- a/apps/server/src/orchestration-v2/ThreadPullRequestService.ts
+++ b/apps/server/src/orchestration-v2/ThreadPullRequestService.ts
@@ -7,7 +7,6 @@ import {
type ThreadLinkedPullRequest,
} from "@t3tools/contracts";
import { makeDrainableWorker } from "@t3tools/shared/DrainableWorker";
-import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
import * as Cause from "effect/Cause";
import * as Context from "effect/Context";
import * as SecureRandom from "effect/crypto/SecureRandom";
@@ -120,19 +119,9 @@ export const make = Effect.gen(function* () {
};
const readThreadSnapshot = ({ threadId, backfill }: RefreshRequest) =>
- threadId === null
- ? orchestrator
- .getShellSnapshot({
- location: "active",
- unsettledOnly: !(backfill || pendingBackfill.size > 0),
- })
- .pipe(NodeSqliteClient.readOnly)
- : Effect.gen(function* () {
- // Read sequence first so guarded sync rejects intervening changes.
- const snapshotSequence = yield* orchestrator.getThreadEventSequence(threadId);
- const thread = yield* orchestrator.getThreadShell(threadId);
- return { snapshotSequence, threads: thread === null ? [] : [thread] };
- });
+ orchestrator.getPullRequestSnapshot(
+ threadId === null ? { unsettledOnly: !(backfill || pendingBackfill.size > 0) } : { threadId },
+ );
const synchronize = Effect.fn("ThreadPullRequestServiceV2.synchronize")(function* (
request: RefreshRequest,