Skip to content

Commit 8bf4822

Browse files
perf(server): sidebar shell loads 3x faster and startup thread counts 80x faster (#27106)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 7f39a86 commit 8bf4822

17 files changed

Lines changed: 436 additions & 154 deletions

‎apps/server/integration/transferBudgetV2.integration.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ const layerManagement = Layer.unwrap(
117117
.pipe(Effect.orDie),
118118
getThreadShell: (id) => projections.getThreadShell(id).pipe(Effect.orDie),
119119
getShellSnapshot: (options) => projections.getShellSnapshot(options).pipe(Effect.orDie),
120+
listActiveShellIndex: () => projections.listActiveShellIndex().pipe(Effect.orDie),
120121
listActiveSubagentThreadIds: () =>
121122
projections.listActiveSubagentThreadIds().pipe(Effect.orDie),
122123
readShellSnapshot: (options) =>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ import {
141141
type ProjectionRecords,
142142
type ProjectionCheckpointContext,
143143
type OlderSettledThreadsListing,
144+
type ActiveShellIndexEntry,
144145
type OlderSettledThreadsQuery,
145146
type ShellSnapshotOptions,
146147
} from "./ProjectionStore.ts";
@@ -392,6 +393,11 @@ export interface OrchestratorV2Shape {
392393
ReadonlyArray<ThreadId>,
393394
OrchestratorV2Error
394395
>;
396+
readonly countThreads: () => Effect.Effect<number, OrchestratorV2Error>;
397+
readonly listActiveShellIndex: () => Effect.Effect<
398+
ReadonlyArray<ActiveShellIndexEntry>,
399+
OrchestratorV2Error
400+
>;
395401
readonly listOlderSettledThreads: (
396402
query: OlderSettledThreadsQuery,
397403
) => Effect.Effect<OlderSettledThreadsListing, OrchestratorV2Error>;
@@ -13434,6 +13440,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1343413440
.pipe(Effect.mapError(shellProjectionError));
1343513441
},
1343613442
),
13443+
countThreads: Effect.fn("OrchestratorV2.countThreads")(function () {
13444+
return projectionStore.countThreads().pipe(Effect.mapError(shellProjectionError));
13445+
}),
13446+
listActiveShellIndex: Effect.fn("OrchestratorV2.listActiveShellIndex")(function () {
13447+
return projectionStore.listActiveShellIndex().pipe(Effect.mapError(shellProjectionError));
13448+
}),
1343713449
listOlderSettledThreads: Effect.fn("OrchestratorV2.listOlderSettledThreads")(function (query) {
1343813450
return projectionStore
1343913451
.listOlderSettledThreads(query)

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
ThreadId,
2727
TurnItemId,
2828
type ThreadPullRequestLink,
29+
isLiveSubagentShell,
2930
} from "@t3tools/contracts";
3031
import * as DateTime from "effect/DateTime";
3132
import * as Effect from "effect/Effect";
@@ -76,6 +77,33 @@ const traceSqlStatements = () => {
7677
return { statements, tracer };
7778
};
7879

80+
/** The shell index flags every live shell and matches its shells on the fields subscriber trims read. */
81+
const assertShellIndexCoversShells = Effect.fnUntraced(function* () {
82+
const store = yield* ProjectionStore.ProjectionStoreV2;
83+
const shells = (yield* store.getShellSnapshot({ location: "active" })).threads;
84+
const index = yield* store.listActiveShellIndex();
85+
const iso = (value: DateTime.Utc | null | undefined) =>
86+
value == null ? null : DateTime.formatIso(value);
87+
const trimmed = (
88+
row: Pick<
89+
ProjectionStore.ActiveShellIndexEntry,
90+
"id" | "projectId" | "lineage" | "settledOverride" | "settledAt" | "pinnedAt" | "snoozedUntil"
91+
>,
92+
) => ({
93+
id: row.id,
94+
projectId: row.projectId,
95+
lineage: row.lineage,
96+
settledOverride: row.settledOverride,
97+
settledAt: iso(row.settledAt),
98+
pinnedAt: iso(row.pinnedAt),
99+
snoozedUntil: iso(row.snoozedUntil),
100+
});
101+
assert.deepEqual(index.map(trimmed), shells.map(trimmed));
102+
shells.forEach((shell, position) => {
103+
if (isLiveSubagentShell(shell)) assert.isTrue(index[position]?.mayBeLive, shell.id);
104+
});
105+
});
106+
79107
const addRolledBackRecoveryCandidate = Effect.fn("addRolledBackRecoveryCandidate")(function* (
80108
suffix: string,
81109
) {
@@ -272,6 +300,7 @@ it.effect("SQL shells flag only an active turn blocked solely on T3 thread waits
272300
assert.equal(shell?.status, "running");
273301
assert.equal(shell?.blockedOnThreadWait ?? false, expected);
274302
}
303+
yield* assertShellIndexCoversShells();
275304
});
276305
const wait = {
277306
...itemBase,
@@ -531,6 +560,7 @@ it.effect("SQL shells use the latest subagent owner for duplicate provider-threa
531560
const listed = (yield* store.getShellSnapshot()).threads.find(
532561
(thread) => thread.id === targetThreadId,
533562
);
563+
yield* assertShellIndexCoversShells();
534564
for (const shell of [yield* store.getThreadShell(targetThreadId), listed]) {
535565
assert.equal(shell?.providerInstanceId, otherInstanceId);
536566
assert.equal(shell?.activeRunId, null);
@@ -1336,6 +1366,7 @@ it.effect("SQL list older settled threads below a cursor", () =>
13361366
const next = yield* list({ settledAt: DateTime.formatIso(at(4)), threadId: id("b") }, 10);
13371367
assert.deepEqual(next.threadIds, [id("c"), id("d"), id("snoozed")]);
13381368
assert.deepEqual(yield* list(before, 25, []), { threadIds: [], counts: [] });
1369+
yield* assertShellIndexCoversShells();
13391370
const scoped = yield* list(before, 0, [ProjectId.make("project:b")]);
13401371
assert.deepEqual(scoped, {
13411372
threadIds: [],
@@ -3798,6 +3829,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
37983829
const sqlShell = (yield* projectionStore.getShellSnapshot()).threads.find(
37993830
(row) => row.id === threadId,
38003831
)!;
3832+
yield* assertShellIndexCoversShells();
38013833
for (const questionShell of [
38023834
sqlShell,
38033835
ProjectionStore.threadShellFromProjection(projection),
@@ -4860,6 +4892,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
48604892
SET status = 'running', payload_json = json_set(payload_json, '$.status', 'running')
48614893
WHERE run_id = ${runId}
48624894
`;
4895+
yield* assertShellIndexCoversShells();
48634896
const secretOnlyShell = (yield* projectionStore.getShellSnapshot()).threads.find(
48644897
(thread) => thread.id === threadId,
48654898
);
@@ -6466,6 +6499,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
64666499
(candidate) => candidate.id === threadId,
64676500
);
64686501
assert.deepEqual(listed?.pendingBackgroundTasks, shell?.pendingBackgroundTasks);
6502+
yield* assertShellIndexCoversShells();
64696503
return shell && projectThreadAwarenessV2({ environmentId, project, thread: shell })?.phase;
64706504
});
64716505

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,15 @@ export interface ShellSnapshotOptions {
393393
readonly threadIds?: ReadonlyArray<ThreadId>;
394394
}
395395

396+
/** What a shell subscriber's trims read from an active thread, without building its shell. */
397+
export interface ActiveShellIndexEntry extends Pick<
398+
OrchestrationV2ThreadShell,
399+
"id" | "projectId" | "lineage" | "settledOverride" | "settledAt" | "pinnedAt" | "snoozedUntil"
400+
> {
401+
/** False only when no run, request, work item, or watch can make the shell live. */
402+
readonly mayBeLive: boolean;
403+
}
404+
396405
export interface OlderSettledThreadsQuery {
397406
readonly before: OrchestrationV2OlderSettledCursor;
398407
readonly projectIds?: ReadonlyArray<ProjectId> | undefined;
@@ -450,6 +459,13 @@ export interface ProjectionStoreV2Shape {
450459
ReadonlyArray<ThreadId>,
451460
ProjectionStoreV2Error
452461
>;
462+
/** Threads that are not deleted, archived ones included. */
463+
readonly countThreads: () => Effect.Effect<number, ProjectionStoreV2Error>;
464+
/** Every active thread, in shell order. */
465+
readonly listActiveShellIndex: () => Effect.Effect<
466+
ReadonlyArray<ActiveShellIndexEntry>,
467+
ProjectionStoreV2Error
468+
>;
453469
/** Older settled thread ids below a cursor, newest settle first, and per-project counts. */
454470
readonly listOlderSettledThreads: (
455471
query: OlderSettledThreadsQuery,
@@ -6614,6 +6630,98 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
66146630
Effect.mapError(shellSnapshotReadError),
66156631
);
66166632

6633+
const countThreads = Effect.fn("ProjectionStore.countThreads")(function* () {
6634+
const rows = yield* sql<{ readonly count: number }>`
6635+
SELECT COUNT(*) AS count FROM orchestration_v2_projection_threads WHERE deleted_at IS NULL
6636+
`;
6637+
return rows[0]?.count ?? 0;
6638+
}, Effect.mapError(shellSnapshotReadError));
6639+
6640+
const listActiveShellIndex = Effect.fn("ProjectionStore.listActiveShellIndex")(function* () {
6641+
const rows = yield* sql<{
6642+
readonly thread_id: string;
6643+
readonly project_id: string;
6644+
readonly relationship_to_parent: string | null;
6645+
readonly parent_thread_id: string | null;
6646+
readonly root_thread_id: string;
6647+
readonly settled_override: string | null;
6648+
readonly settled_at: string | null;
6649+
readonly pinned_at: string | null;
6650+
readonly snoozed_until: string | null;
6651+
readonly may_be_live: number;
6652+
}>`
6653+
SELECT
6654+
t.thread_id,
6655+
t.project_id,
6656+
json_extract(t.payload_json, '$.lineage.relationshipToParent') AS relationship_to_parent,
6657+
json_extract(t.payload_json, '$.lineage.parentThreadId') AS parent_thread_id,
6658+
json_extract(t.payload_json, '$.lineage.rootThreadId') AS root_thread_id,
6659+
json_extract(t.payload_json, '$.settledOverride') AS settled_override,
6660+
json_extract(t.payload_json, '$.settledAt') AS settled_at,
6661+
json_extract(t.payload_json, '$.pinnedAt') AS pinned_at,
6662+
json_extract(t.payload_json, '$.snoozedUntil') AS snoozed_until,
6663+
-- A superset of isLiveSubagentShell: the sources of its run status, pending request,
6664+
-- and pending background work.
6665+
(
6666+
EXISTS (
6667+
SELECT 1 FROM orchestration_v2_projection_runs r
6668+
WHERE r.thread_id = t.thread_id
6669+
AND r.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
6670+
)
6671+
OR EXISTS (
6672+
SELECT 1 FROM orchestration_v2_projection_runtime_requests request
6673+
WHERE request.thread_id = t.thread_id AND request.status = 'pending'
6674+
)
6675+
-- Same predicates as the shell's pending item and link reads, for their partial indexes.
6676+
OR EXISTS (
6677+
SELECT 1 FROM orchestration_v2_projection_turn_items item
6678+
WHERE item.thread_id = t.thread_id
6679+
AND item.type IN ('command_execution', 'dynamic_tool', 'subagent')
6680+
AND item.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled')
6681+
)
6682+
OR EXISTS (
6683+
SELECT 1 FROM orchestration_v2_projection_turn_items link_request
6684+
WHERE link_request.thread_id = t.thread_id
6685+
AND link_request.type = 'link_request' AND link_request.status = 'waiting'
6686+
)
6687+
OR EXISTS (
6688+
SELECT 1 FROM orchestration_v2_projection_provider_threads provider_thread
6689+
WHERE provider_thread.thread_id = t.thread_id
6690+
AND json_array_length(provider_thread.payload_json, '$.pendingBackgroundTasks') > 0
6691+
)
6692+
OR EXISTS (
6693+
SELECT 1 FROM json_each(t.payload_json, '$.pullRequests') link
6694+
WHERE json_type(link.value, '$.watch') IS NOT NULL
6695+
)
6696+
) AS may_be_live
6697+
FROM orchestration_v2_projection_threads t
6698+
WHERE t.deleted_at IS NULL
6699+
AND json_extract(t.payload_json, '$.archivedAt') IS NULL
6700+
ORDER BY t.updated_at ASC, t.thread_id ASC
6701+
`;
6702+
return rows.map((row): ActiveShellIndexEntry => ({
6703+
id: ThreadId.make(row.thread_id),
6704+
projectId: ProjectId.make(row.project_id),
6705+
lineage: {
6706+
parentThreadId:
6707+
row.parent_thread_id === null ? null : ThreadId.make(row.parent_thread_id),
6708+
relationshipToParent:
6709+
row.relationship_to_parent === "fork" || row.relationship_to_parent === "subagent"
6710+
? row.relationship_to_parent
6711+
: null,
6712+
rootThreadId: ThreadId.make(row.root_thread_id),
6713+
},
6714+
settledOverride:
6715+
row.settled_override === "settled" || row.settled_override === "active"
6716+
? row.settled_override
6717+
: null,
6718+
settledAt: row.settled_at === null ? null : DateTime.makeUnsafe(row.settled_at),
6719+
pinnedAt: row.pinned_at === null ? null : DateTime.makeUnsafe(row.pinned_at),
6720+
snoozedUntil: row.snoozed_until === null ? null : DateTime.makeUnsafe(row.snoozed_until),
6721+
mayBeLive: row.may_be_live === 1,
6722+
}));
6723+
}, Effect.mapError(shellSnapshotReadError));
6724+
66176725
const findGuestShareThreadId = Effect.fn("ProjectionStore.findGuestShareThreadId")(function* (
66186726
shareId: GuestShareId,
66196727
) {
@@ -6768,6 +6876,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
67686876
apply,
67696877
getShellSnapshot: readOnReader(getShellSnapshot),
67706878
listActiveSubagentThreadIds: readOnReader(listActiveSubagentThreadIds),
6879+
countThreads: readOnReader(countThreads),
6880+
listActiveShellIndex: readOnReader(listActiveShellIndex),
67716881
readShellSnapshot: readOnReader(readShellSnapshot),
67726882
listOlderSettledThreads: readOnReader(listOlderSettledThreads),
67736883
getThreadShell: readOnReader(getThreadShell),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ it.effect(
213213
getOpenRemoteDelegatedTasks: Effect.die("unused getOpenRemoteDelegatedTasks"),
214214
getShellSnapshot: () => Effect.die("unused getShellSnapshot"),
215215
listActiveSubagentThreadIds: () => Effect.die("unused listActiveSubagentThreadIds"),
216+
countThreads: () => Effect.die("unused countThreads"),
217+
listActiveShellIndex: () => Effect.die("unused listActiveShellIndex"),
216218
listOlderSettledThreads: () => Effect.die("unused listOlderSettledThreads"),
217219
readShellSnapshot: () => Effect.die("unused readShellSnapshot"),
218220
getThreadShell: () => Effect.die("unused getThreadShell"),

0 commit comments

Comments
 (0)