Skip to content

Commit 1d6f23b

Browse files
t3dotggclaude
andauthored
perf(server): background sweeps only read threads that can still settle (pingdotgg#13765)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 294dd12 commit 1d6f23b

6 files changed

Lines changed: 349 additions & 30 deletions

File tree

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3643,6 +3643,85 @@ it.effect("reads one sweep thread and its projects like the shell snapshot", ()
36433643
}).pipe(Effect.provide(layer));
36443644
});
36453645

3646+
it.effect("reads a full sweep from unsettled threads and every project", () => {
3647+
const resolved: string[] = [];
3648+
const layer = OrchestrationProjectionSnapshotQueryLive.pipe(
3649+
Layer.provide(ThreadBackgroundLiveness.layer),
3650+
Layer.provide(ThreadPlanProgress.layer),
3651+
Layer.provide(
3652+
Layer.succeed(RepositoryIdentityResolver.RepositoryIdentityResolver, {
3653+
resolve: (root) =>
3654+
Effect.sync(() => {
3655+
resolved.push(root);
3656+
return null;
3657+
}),
3658+
}),
3659+
),
3660+
Layer.provideMerge(SqlitePersistenceMemory),
3661+
);
3662+
return Effect.gen(function* () {
3663+
const sql = yield* SqlClient.SqlClient;
3664+
const query = yield* ProjectionSnapshotQuery;
3665+
yield* sql`INSERT INTO projection_projects (project_id, title, workspace_root, scripts_json, created_at, updated_at)
3666+
VALUES ('p1', 'One', '/one', '[]', '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z'),
3667+
('p2', 'Two', '/two', '[]', '2026-09-02T00:00:00Z', '2026-09-02T00:00:00Z'),
3668+
('p3', 'Three', '/three', '[]', '2026-09-03T00:00:00Z', '2026-09-03T00:00:00Z'),
3669+
('p4', 'Four', '/four', '[]', '2026-09-04T00:00:00Z', '2026-09-04T00:00:00Z')`;
3670+
yield* sql`INSERT INTO projection_threads (thread_id, project_id, title, model_selection_json, runtime_mode, interaction_mode, branch_pull_request_json, latest_turn_id, created_at, updated_at, archived_at, settled_override, settled_at)
3671+
VALUES
3672+
('t-open', 'p1', 'Open', '{"provider":"codex","model":"gpt-5"}', 'full-access', 'default', NULL, 'turn-open', '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', NULL, NULL, NULL),
3673+
('t-resumed', 'p1', 'Resumed', '{"provider":"codex","model":"gpt-5"}', 'full-access', 'default', NULL, NULL, '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', NULL, 'active', NULL),
3674+
('t-branch', 'p1', 'Branch', '{"provider":"codex","model":"gpt-5"}', 'full-access', 'default',
3675+
'{"projectId":"p2","repository":"acme/web","number":8,"url":"https://github.com/acme/web/pull/8"}',
3676+
NULL, '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', NULL, NULL, NULL),
3677+
('t-settled', 'p3', 'Settled', '{"provider":"codex","model":"gpt-5"}', 'full-access', 'default', NULL, 'turn-settled', '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', NULL, 'settled', '2026-09-03T00:00:00Z'),
3678+
('t-archived', 'p4', 'Archived', '{"provider":"codex","model":"gpt-5"}', 'full-access', 'default', NULL, NULL, '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', '2026-09-04T00:00:00Z', NULL, NULL)`;
3679+
// The open and the settled thread both have a row in each joined table. The
3680+
// settled thread's turn and session are the newest rows, so updatedAt shows
3681+
// whether those two reads skip it.
3682+
yield* sql`INSERT INTO projection_thread_pull_requests (thread_id, host, repository, number, url, source, linked_at)
3683+
VALUES ('t-open', 'github.com', 'acme/web', 7, 'https://github.com/acme/web/pull/7', 'agent', '2026-09-02T00:00:00Z'),
3684+
('t-settled', 'github.com', 'acme/web', 9, 'https://github.com/acme/web/pull/9', 'agent', '2026-09-02T00:00:00Z')`;
3685+
yield* sql`INSERT INTO projection_turns (thread_id, turn_id, state, requested_at, checkpoint_files_json)
3686+
VALUES ('t-open', 'turn-open', 'completed', '2026-09-02T00:00:00Z', '[]'),
3687+
('t-settled', 'turn-settled', 'completed', '2026-09-09T00:00:00Z', '[]')`;
3688+
yield* sql`INSERT INTO projection_thread_sessions (thread_id, status, provider_name, active_turn_id, last_error, updated_at)
3689+
VALUES ('t-open', 'ready', 'codex', NULL, NULL, '2026-09-02T00:00:00Z'),
3690+
('t-settled', 'stopped', 'codex', NULL, NULL, '2026-09-10T00:00:00Z')`;
3691+
3692+
const full = yield* query.getShellSnapshot();
3693+
// The settled thread's rows must reach the full read, or skipping them proves nothing.
3694+
const settled = full.threads.find((thread) => thread.id === ThreadId.make("t-settled"));
3695+
assert.strictEqual(settled?.pullRequests[0]?.number, 9);
3696+
assert.strictEqual(settled?.latestTurn?.turnId, asTurnId("turn-settled"));
3697+
assert.strictEqual(settled?.session?.status, "stopped");
3698+
assert.strictEqual(full.updatedAt, "2026-09-10T00:00:00Z");
3699+
resolved.length = 0;
3700+
3701+
const sweep = yield* readSweepSnapshot(query, null);
3702+
assert.strictEqual(sweep.snapshotSequence, full.snapshotSequence);
3703+
assert.deepStrictEqual(
3704+
sweep.threads,
3705+
full.threads.filter((thread) => thread.id !== ThreadId.make("t-settled")),
3706+
);
3707+
assert.deepStrictEqual(
3708+
sweep.threads.map((thread) => thread.id),
3709+
["t-branch", "t-open", "t-resumed"],
3710+
);
3711+
// Like the full read, the sweep resolves every project, so it keeps the
3712+
// repository identity cache warm for client connects.
3713+
assert.deepStrictEqual(sweep.projects, full.projects);
3714+
assert.deepStrictEqual(resolved.toSorted(), ["/four", "/one", "/three", "/two"]);
3715+
// A settled thread's link that no longer decodes breaks the full read, but
3716+
// not the sweep, which never reads it.
3717+
yield* sql`UPDATE projection_thread_pull_requests SET snapshot_json = 'invalid-json' WHERE thread_id = 't-settled'`;
3718+
assert.strictEqual((yield* Effect.exit(query.getShellSnapshot()))._tag, "Failure");
3719+
const unsettled = yield* query.getShellSnapshot({ unsettledOnly: true });
3720+
assert.deepStrictEqual(unsettled.threads, sweep.threads);
3721+
assert.strictEqual(unsettled.updatedAt, "2026-09-04T00:00:00Z");
3722+
}).pipe(Effect.provide(layer));
3723+
});
3724+
36463725
projectionSnapshotLayer("ProjectionSnapshotQuery activities by kind", (it) => {
36473726
it.effect("lists one kind across active threads only, without hydrating the threads", () =>
36483727
Effect.gen(function* () {

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ const EventReplayStatsRowSchema = Schema.Struct({
185185
eventCount: Schema.Number,
186186
payloadBytes: Schema.Number,
187187
});
188+
const ActiveThreadRowsRequest = Schema.Struct({ unsettledOnly: Schema.Boolean });
188189
const ProjectionThreadSearchRequest = Schema.Struct({
189190
pattern: Schema.String,
190191
limit: Schema.Int,
@@ -600,10 +601,16 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
600601
`,
601602
});
602603

604+
// Background sweeps skip settled threads, the same check PR discovery makes.
605+
const unsettledThreadsFilter = (unsettledOnly: boolean) =>
606+
unsettledOnly
607+
? sql`AND threads.settled_at IS NULL AND threads.settled_override IS NOT 'settled'`
608+
: sql``;
609+
603610
const listActiveThreadRows = SqlSchema.findAll({
604-
Request: Schema.Void,
611+
Request: ActiveThreadRowsRequest,
605612
Result: ProjectionThreadDbRowSchema,
606-
execute: () =>
613+
execute: (request) =>
607614
sql`
608615
SELECT
609616
thread_id AS "threadId",
@@ -637,9 +644,10 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
637644
pending_user_input_count AS "pendingUserInputCount",
638645
has_actionable_proposed_plan AS "hasActionableProposedPlan",
639646
deleted_at AS "deletedAt"
640-
FROM projection_threads
647+
FROM projection_threads threads
641648
WHERE deleted_at IS NULL
642649
AND archived_at IS NULL
650+
${unsettledThreadsFilter(request.unsettledOnly)}
643651
ORDER BY project_id ASC, created_at ASC, thread_id ASC
644652
`,
645653
});
@@ -779,9 +787,9 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
779787
});
780788

781789
const listActiveThreadPullRequestRows = SqlSchema.findAll({
782-
Request: Schema.Void,
790+
Request: ActiveThreadRowsRequest,
783791
Result: ProjectionThreadPullRequestDbRowSchema,
784-
execute: () =>
792+
execute: (request) =>
785793
sql`
786794
SELECT
787795
links.thread_id AS "threadId",
@@ -798,6 +806,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
798806
ON threads.thread_id = links.thread_id
799807
WHERE threads.deleted_at IS NULL
800808
AND threads.archived_at IS NULL
809+
${unsettledThreadsFilter(request.unsettledOnly)}
801810
ORDER BY links.thread_id ASC, links.linked_at ASC, links.number ASC
802811
`,
803812
});
@@ -907,9 +916,9 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
907916
});
908917

909918
const listActiveThreadSessionRows = SqlSchema.findAll({
910-
Request: Schema.Void,
919+
Request: ActiveThreadRowsRequest,
911920
Result: ProjectionThreadSessionDbRowSchema,
912-
execute: () =>
921+
execute: (request) =>
913922
sql`
914923
SELECT
915924
sessions.thread_id AS "threadId",
@@ -927,6 +936,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
927936
ON threads.thread_id = sessions.thread_id
928937
WHERE threads.deleted_at IS NULL
929938
AND threads.archived_at IS NULL
939+
${unsettledThreadsFilter(request.unsettledOnly)}
930940
ORDER BY sessions.thread_id ASC
931941
`,
932942
});
@@ -1001,9 +1011,9 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
10011011
});
10021012

10031013
const listActiveLatestTurnRows = SqlSchema.findAll({
1004-
Request: Schema.Void,
1014+
Request: ActiveThreadRowsRequest,
10051015
Result: ProjectionLatestTurnDbRowSchema,
1006-
execute: () =>
1016+
execute: (request) =>
10071017
sql`
10081018
SELECT
10091019
turns.thread_id AS "threadId",
@@ -1022,6 +1032,7 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
10221032
WHERE threads.deleted_at IS NULL
10231033
AND threads.archived_at IS NULL
10241034
AND threads.latest_turn_id IS NOT NULL
1035+
${unsettledThreadsFilter(request.unsettledOnly)}
10251036
ORDER BY turns.thread_id ASC
10261037
`,
10271038
});
@@ -2654,8 +2665,9 @@ pending_approval_requests AS (
26542665
}),
26552666
);
26562667

2657-
const getShellSnapshot: ProjectionSnapshotQueryShape["getShellSnapshot"] = () =>
2658-
sql
2668+
const getShellSnapshot: ProjectionSnapshotQueryShape["getShellSnapshot"] = (options) => {
2669+
const unsettledOnly = options?.unsettledOnly === true;
2670+
return sql
26592671
.withTransaction(
26602672
Effect.all([
26612673
listProjectRows(undefined).pipe(
@@ -2666,31 +2678,31 @@ pending_approval_requests AS (
26662678
),
26672679
),
26682680
),
2669-
listActiveThreadRows(undefined).pipe(
2681+
listActiveThreadRows({ unsettledOnly }).pipe(
26702682
Effect.mapError(
26712683
toPersistenceSqlOrDecodeError(
26722684
"ProjectionSnapshotQuery.getShellSnapshot:listThreads:query",
26732685
"ProjectionSnapshotQuery.getShellSnapshot:listThreads:decodeRows",
26742686
),
26752687
),
26762688
),
2677-
listActiveThreadSessionRows(undefined).pipe(
2689+
listActiveThreadSessionRows({ unsettledOnly }).pipe(
26782690
Effect.mapError(
26792691
toPersistenceSqlOrDecodeError(
26802692
"ProjectionSnapshotQuery.getShellSnapshot:listThreadSessions:query",
26812693
"ProjectionSnapshotQuery.getShellSnapshot:listThreadSessions:decodeRows",
26822694
),
26832695
),
26842696
),
2685-
listActiveThreadPullRequestRows(undefined).pipe(
2697+
listActiveThreadPullRequestRows({ unsettledOnly }).pipe(
26862698
Effect.mapError(
26872699
toPersistenceSqlOrDecodeError(
26882700
"ProjectionSnapshotQuery.getShellSnapshot:listThreadPullRequests:query",
26892701
"ProjectionSnapshotQuery.getShellSnapshot:listThreadPullRequests:decodeRows",
26902702
),
26912703
),
26922704
),
2693-
listActiveLatestTurnRows(undefined).pipe(
2705+
listActiveLatestTurnRows({ unsettledOnly }).pipe(
26942706
Effect.mapError(
26952707
toPersistenceSqlOrDecodeError(
26962708
"ProjectionSnapshotQuery.getShellSnapshot:listLatestTurns:query",
@@ -2812,6 +2824,7 @@ pending_approval_requests AS (
28122824
return toPersistenceSqlError("ProjectionSnapshotQuery.getShellSnapshot:query")(error);
28132825
}),
28142826
);
2827+
};
28152828

28162829
const listThreadsWithPullRequests: ProjectionSnapshotQueryShape["listThreadsWithPullRequests"] =
28172830
() =>

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,15 @@ export interface ProjectionSnapshotQueryShape {
120120
*
121121
* Returns only projects and thread shell summaries so clients can bootstrap
122122
* lightweight navigation state without hydrating every thread body.
123+
*
124+
* `unsettledOnly` is for background sweeps, not clients. It skips settled
125+
* threads and their sessions, PR links, and turns, and its `updatedAt`
126+
* ignores those rows. It still resolves every project, which keeps
127+
* repository identities cached for client connects.
123128
*/
124-
readonly getShellSnapshot: () => Effect.Effect<
125-
OrchestrationShellSnapshot,
126-
ProjectionRepositoryError
127-
>;
129+
readonly getShellSnapshot: (options?: {
130+
readonly unsettledOnly?: boolean;
131+
}) => Effect.Effect<OrchestrationShellSnapshot, ProjectionRepositoryError>;
128132

129133
/**
130134
* Read archived thread shell summaries for the archive page.

0 commit comments

Comments
 (0)