Skip to content

Commit e8123e1

Browse files
committed
perf(server): use one query for buffered provider events
1 parent 3bbbc1d commit e8123e1

10 files changed

Lines changed: 228 additions & 65 deletions

‎apps/server/src/checkpointing/CheckpointDiffQuery.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ describe("CheckpointDiffQuery.layer", () => {
107107
toCheckpointRef,
108108
});
109109
}),
110+
getThreadRuntimeContext: () => Effect.die("unused"),
110111
getThreadShellById: () => Effect.succeed(Option.none()),
111112
getThreadDetailById: () => Effect.succeed(Option.none()),
112113
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
@@ -203,6 +204,7 @@ describe("CheckpointDiffQuery.layer", () => {
203204
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
204205
getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
205206
getFullThreadDiffContext: () => Effect.die("unused"),
207+
getThreadRuntimeContext: () => Effect.die("unused"),
206208
getThreadShellById: () => Effect.succeed(Option.none()),
207209
getThreadDetailById: () => Effect.succeed(Option.none()),
208210
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
@@ -289,6 +291,7 @@ describe("CheckpointDiffQuery.layer", () => {
289291
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
290292
getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
291293
getFullThreadDiffContext: () => Effect.die("unused"),
294+
getThreadRuntimeContext: () => Effect.die("unused"),
292295
getThreadShellById: () => Effect.succeed(Option.none()),
293296
getThreadDetailById: () => Effect.succeed(Option.none()),
294297
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
@@ -360,6 +363,7 @@ describe("CheckpointDiffQuery.layer", () => {
360363
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
361364
getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
362365
getFullThreadDiffContext: () => Effect.die("unused"),
366+
getThreadRuntimeContext: () => Effect.die("unused"),
363367
getThreadShellById: () => Effect.succeed(Option.none()),
364368
getThreadDetailById: () => Effect.succeed(Option.none()),
365369
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
@@ -416,6 +420,7 @@ describe("CheckpointDiffQuery.layer", () => {
416420
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
417421
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
418422
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
423+
getThreadRuntimeContext: () => Effect.die("unused"),
419424
getThreadShellById: () => Effect.succeed(Option.none()),
420425
getThreadDetailById: () => Effect.succeed(Option.none()),
421426
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),

‎apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ describe("OrchestrationEngine", () => {
418418
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
419419
getThreadCheckpointContext: () => Effect.succeed(Option.none()),
420420
getFullThreadDiffContext: () => Effect.succeed(Option.none()),
421+
getThreadRuntimeContext: () => Effect.die("unused"),
421422
getThreadShellById: () => Effect.succeed(Option.none()),
422423
getThreadDetailById: () => Effect.succeed(Option.none()),
423424
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),

‎apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as ThreadPlanProgress from "../ThreadPlanProgress.ts";
2222
import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
2323
import { encodeThreadDetailPageCursor } from "../threadDetailCursor.ts";
2424
import { projectThreadDetailSnapshot } from "../ActivityPayloadProjection.ts";
25+
import { makeSqlStatementCounter } from "../../../integration/SqlStatementCounter.integration.ts";
2526

2627
const asProjectId = (value: string): ProjectId => ProjectId.make(value);
2728
const asTurnId = (value: string): TurnId => TurnId.make(value);
@@ -560,6 +561,38 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
560561
},
561562
]);
562563
}
564+
565+
const counter = makeSqlStatementCounter();
566+
const context = yield* snapshotQuery
567+
.getThreadRuntimeContext(ThreadId.make("thread-1"))
568+
.pipe(Effect.withTracer(counter.tracer));
569+
assert.equal(counter.count(), 1);
570+
assert.equal(context._tag, "Some");
571+
if (context._tag === "Some") {
572+
assert.deepEqual(context.value, {
573+
id: ThreadId.make("thread-1"),
574+
title: "Thread 1",
575+
session: snapshot.threads[0]?.session,
576+
});
577+
}
578+
579+
yield* sql`
580+
UPDATE projection_thread_sessions
581+
SET status = 'starting', active_turn_id = NULL, provider_name = 'claudeAgent',
582+
provider_instance_id = 'claude-secondary', last_error = 'Starting another session'
583+
WHERE thread_id = 'thread-1'
584+
`;
585+
const changedContext = yield* snapshotQuery.getThreadRuntimeContext(
586+
ThreadId.make("thread-1"),
587+
);
588+
assert.equal(changedContext._tag, "Some");
589+
if (changedContext._tag === "Some") {
590+
assert.equal(changedContext.value.session?.status, "starting");
591+
assert.equal(changedContext.value.session?.activeTurnId, null);
592+
assert.equal(changedContext.value.session?.providerName, "claudeAgent");
593+
assert.equal(changedContext.value.session?.providerInstanceId, "claude-secondary");
594+
assert.equal(changedContext.value.session?.lastError, "Starting another session");
595+
}
563596
}),
564597
);
565598

@@ -680,6 +713,22 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
680713
[ThreadId.make("thread-archived")],
681714
);
682715
assert.equal(archivedShellSnapshot.threads[0]?.archivedAt, "2026-04-06T00:00:06.000Z");
716+
const activeContext = yield* snapshotQuery.getThreadRuntimeContext(
717+
ThreadId.make("thread-active"),
718+
);
719+
assert.equal(activeContext._tag, "Some");
720+
if (activeContext._tag === "Some") assert.equal(activeContext.value.session, null);
721+
for (const threadId of ["thread-archived", "thread-missing"]) {
722+
assert.equal(
723+
(yield* snapshotQuery.getThreadRuntimeContext(ThreadId.make(threadId)))._tag,
724+
"None",
725+
);
726+
}
727+
yield* sql`UPDATE projection_threads SET deleted_at = '2026-04-06T00:00:08.000Z' WHERE thread_id = 'thread-active'`;
728+
assert.equal(
729+
(yield* snapshotQuery.getThreadRuntimeContext(ThreadId.make("thread-active")))._tag,
730+
"None",
731+
);
683732
}),
684733
);
685734

‎apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ const ProjectionThreadActivityIdRowSchema = Schema.Struct({
113113
activityId: ProjectionThreadActivity.fields.activityId,
114114
});
115115
const ProjectionThreadSessionDbRowSchema = ProjectionThreadSession;
116+
const ProjectionThreadRuntimeContextDbRowSchema = Schema.Struct({
117+
id: ThreadId,
118+
title: Schema.String,
119+
session: Schema.NullOr(ProjectionThreadSessionDbRowSchema),
120+
});
116121
const ProjectionCheckpointDbRowSchema = ProjectionCheckpoint.mapFields(
117122
Struct.assign({
118123
files: Schema.fromJsonString(Schema.Array(OrchestrationCheckpointFile)),
@@ -1038,6 +1043,40 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
10381043
`,
10391044
});
10401045

1046+
const getThreadRuntimeContextRow = SqlSchema.findOneOption({
1047+
Request: ThreadIdLookupInput,
1048+
Result: ProjectionThreadRuntimeContextDbRowSchema,
1049+
execute: ({ threadId }) =>
1050+
sql`
1051+
SELECT
1052+
threads.thread_id AS id,
1053+
threads.title,
1054+
sessions.thread_id AS "threadId",
1055+
sessions.status,
1056+
sessions.provider_name AS "providerName",
1057+
sessions.provider_instance_id AS "providerInstanceId",
1058+
sessions.runtime_mode AS "runtimeMode",
1059+
sessions.active_turn_id AS "activeTurnId",
1060+
sessions.last_error AS "lastError",
1061+
sessions.updated_at AS "updatedAt"
1062+
FROM projection_threads AS threads
1063+
LEFT JOIN projection_thread_sessions AS sessions
1064+
ON sessions.thread_id = threads.thread_id
1065+
WHERE threads.thread_id = ${threadId}
1066+
AND threads.deleted_at IS NULL
1067+
AND threads.archived_at IS NULL
1068+
LIMIT 1
1069+
`.pipe(
1070+
Effect.map((rows) =>
1071+
rows.map((row) => ({
1072+
id: row.id,
1073+
title: row.title,
1074+
session: row.threadId === null ? null : row,
1075+
})),
1076+
),
1077+
),
1078+
});
1079+
10411080
const listThreadMessageRowsByThread = SqlSchema.findAll({
10421081
Request: ThreadIdLookupInput,
10431082
Result: ProjectionThreadMessageDbRowSchema,
@@ -2764,6 +2803,23 @@ pending_approval_requests AS (
27642803
} satisfies OrchestrationThreadShell);
27652804
});
27662805

2806+
const getThreadRuntimeContext: ProjectionSnapshotQueryShape["getThreadRuntimeContext"] =
2807+
Effect.fn("ProjectionSnapshotQuery.getThreadRuntimeContext")(function* (threadId) {
2808+
const context = yield* getThreadRuntimeContextRow({ threadId }).pipe(
2809+
Effect.mapError(
2810+
toPersistenceSqlOrDecodeError(
2811+
"ProjectionSnapshotQuery.getThreadRuntimeContext:query",
2812+
"ProjectionSnapshotQuery.getThreadRuntimeContext:decodeRow",
2813+
),
2814+
),
2815+
);
2816+
return Option.map(context, (row) => ({
2817+
id: row.id,
2818+
title: row.title,
2819+
session: row.session === null ? null : mapSessionRow(row.session),
2820+
}));
2821+
});
2822+
27672823
// Contiguous turn range bounding a windowed detail read; undefined loads the
27682824
// full thread. Resolved from a window request inside the snapshot
27692825
// transaction (see getThreadDetailSnapshot).
@@ -3200,6 +3256,7 @@ pending_approval_requests AS (
32003256
getThreadCheckpointContext,
32013257
getFullThreadDiffContext,
32023258
getThreadShellById,
3259+
getThreadRuntimeContext,
32033260
getThreadDetailById,
32043261
getThreadDetailSnapshot,
32053262
} satisfies ProjectionSnapshotQueryShape;

0 commit comments

Comments
 (0)