Skip to content

Commit 5c828e3

Browse files
perf(server): internal callers read only the threads they need
Startup, the awareness relay, project deletion, and branch renames each read every thread's shell to use a few of them. The startup heartbeat now counts rows, bootstrap and awareness use the shell index to skip subagents, project deletion reads one project's shells, and branch renames read one worktree's. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0dac673 commit 5c828e3

11 files changed

Lines changed: 45 additions & 36 deletions

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ export interface OrchestratorV2Shape {
393393
ReadonlyArray<ThreadId>,
394394
OrchestratorV2Error
395395
>;
396+
readonly countThreads: () => Effect.Effect<number, OrchestratorV2Error>;
396397
readonly listActiveShellIndex: () => Effect.Effect<
397398
ReadonlyArray<ActiveShellIndexEntry>,
398399
OrchestratorV2Error
@@ -13439,6 +13440,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1343913440
.pipe(Effect.mapError(shellProjectionError));
1344013441
},
1344113442
),
13443+
countThreads: Effect.fn("OrchestratorV2.countThreads")(function () {
13444+
return projectionStore.countThreads().pipe(Effect.mapError(shellProjectionError));
13445+
}),
1344213446
listActiveShellIndex: Effect.fn("OrchestratorV2.listActiveShellIndex")(function () {
1344313447
return projectionStore.listActiveShellIndex().pipe(Effect.mapError(shellProjectionError));
1344413448
}),

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@ export interface ProjectionStoreV2Shape {
459459
ReadonlyArray<ThreadId>,
460460
ProjectionStoreV2Error
461461
>;
462+
/** Threads that are not deleted, archived ones included. */
463+
readonly countThreads: () => Effect.Effect<number, ProjectionStoreV2Error>;
462464
/** Every active thread, in shell order. */
463465
readonly listActiveShellIndex: () => Effect.Effect<
464466
ReadonlyArray<ActiveShellIndexEntry>,
@@ -6628,6 +6630,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
66286630
Effect.mapError(shellSnapshotReadError),
66296631
);
66306632

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+
66316640
const listActiveShellIndex = Effect.fn("ProjectionStore.listActiveShellIndex")(function* () {
66326641
const rows = yield* sql<{
66336642
readonly thread_id: string;
@@ -6867,6 +6876,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
68676876
apply,
68686877
getShellSnapshot: readOnReader(getShellSnapshot),
68696878
listActiveSubagentThreadIds: readOnReader(listActiveSubagentThreadIds),
6879+
countThreads: readOnReader(countThreads),
68706880
listActiveShellIndex: readOnReader(listActiveShellIndex),
68716881
readShellSnapshot: readOnReader(readShellSnapshot),
68726882
listOlderSettledThreads: readOnReader(listOlderSettledThreads),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ 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"),
216217
listActiveShellIndex: () => Effect.die("unused listActiveShellIndex"),
217218
listOlderSettledThreads: () => Effect.die("unused listOlderSettledThreads"),
218219
readShellSnapshot: () => Effect.die("unused readShellSnapshot"),

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,9 @@ const make = Effect.gen(function* () {
514514
// Update parents first so children inherit names; reread each pass for newly updated descendants.
515515
const visited = new Set([threadId]);
516516
while (true) {
517-
const snapshot = yield* threads.getShellSnapshot();
517+
const snapshot = yield* threads.getShellSnapshot({ worktreePath: worktreeCwd });
518518
const sharing = [...snapshot.threads, ...snapshot.archivedThreads].filter(
519-
(thread) =>
520-
!visited.has(thread.id) &&
521-
thread.worktreePath === worktreeCwd &&
522-
thread.branch === oldBranch,
519+
(thread) => !visited.has(thread.id) && thread.branch === oldBranch,
523520
);
524521
if (sharing.length === 0) break;
525522
for (const thread of sharing) {

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,14 @@ export interface ThreadManagementServiceShape {
394394
ProjectionRecords<K>,
395395
ThreadManagementProjectionLoadError | ThreadManagementThreadNotFoundError
396396
>;
397-
readonly getShellSnapshot: (options?: {
398-
readonly location?: "active" | "archive";
399-
}) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, Orchestrator.OrchestratorV2Error>;
397+
readonly getShellSnapshot: (
398+
options?: Pick<
399+
ShellSnapshotOptions,
400+
"location" | "projectId" | "unsettledOnly" | "worktreePath" | "threadIds"
401+
>,
402+
) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, Orchestrator.OrchestratorV2Error>;
400403
readonly listActiveSubagentThreadIds: Orchestrator.OrchestratorV2["Service"]["listActiveSubagentThreadIds"];
404+
readonly countThreads: Orchestrator.OrchestratorV2["Service"]["countThreads"];
401405
readonly listActiveShellIndex: Orchestrator.OrchestratorV2["Service"]["listActiveShellIndex"];
402406
readonly listOlderSettledThreads: Orchestrator.OrchestratorV2["Service"]["listOlderSettledThreads"];
403407
readonly readShellSnapshot: (
@@ -1334,6 +1338,7 @@ const make = Effect.gen(function* () {
13341338
getProjectThreadRecords,
13351339
getShellSnapshot: orchestrator.getShellSnapshot,
13361340
listActiveSubagentThreadIds: orchestrator.listActiveSubagentThreadIds,
1341+
countThreads: orchestrator.countThreads,
13371342
listActiveShellIndex: orchestrator.listActiveShellIndex,
13381343
listOlderSettledThreads: orchestrator.listOlderSettledThreads,
13391344
readShellSnapshot: orchestrator.readShellSnapshot,

‎apps/server/src/project/ProjectService.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,15 +457,13 @@ export const make = Effect.gen(function* () {
457457
) {
458458
const { projectId } = input;
459459
const snapshot = yield* threadProjections
460-
.getShellSnapshot()
460+
.getShellSnapshot({ projectId })
461461
.pipe(
462462
Effect.mapError(
463463
(cause) => new ProjectOperationError({ operation: "list-threads", projectId, cause }),
464464
),
465465
);
466-
const projectThreads = [...snapshot.threads, ...snapshot.archivedThreads].filter(
467-
(thread) => thread.projectId === projectId,
468-
);
466+
const projectThreads = [...snapshot.threads, ...snapshot.archivedThreads];
469467
if (projectThreads.length > 0 && input.force !== true) {
470468
return yield* new ProjectNotEmptyError({ projectId });
471469
}

‎apps/server/src/relay/AgentAwarenessRelay.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ const makeTestRelay = Effect.fnUntraced(function* (
190190
return { schemaVersion: 2, snapshotSequence: 1, threads: [], archivedThreads: [] };
191191
}),
192192
listActiveSubagentThreadIds: unused,
193-
listActiveShellIndex: unused,
193+
countThreads: unused,
194+
listActiveShellIndex: () => Effect.succeed([]),
194195
listOlderSettledThreads: unused,
195196
readShellSnapshot: unused,
196197
ensureLegacyTranscript: unused,

‎apps/server/src/relay/AgentAwarenessRelay.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -726,10 +726,15 @@ export const make = Effect.gen(function* () {
726726
return "unlinked" as const;
727727
}
728728
const environmentId = yield* serverEnvironment.getEnvironmentId;
729-
const [projectSnapshot, shellSnapshot] = yield* Effect.all([
730-
projects.snapshot,
731-
threads.getShellSnapshot(),
732-
]);
729+
const projectSnapshot = yield* projects.snapshot;
730+
// Subagents never publish awareness, and they are most threads.
731+
const topLevelThreadIds = (yield* threads.listActiveShellIndex()).flatMap((thread) =>
732+
thread.lineage.relationshipToParent === "subagent" ? [] : [thread.id],
733+
);
734+
const shellSnapshot = yield* threads.getShellSnapshot({
735+
location: "active",
736+
threadIds: topLevelThreadIds,
737+
});
733738
const activeThreadIds = resolveAgentAwarenessRelayActiveThreadIds({
734739
environmentId,
735740
startedAt,

‎apps/server/src/serverRuntimeStartup.autoPull.test.ts‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,7 @@ it.effect("parks automatic pull until activation without delaying command readin
141141
}),
142142
Layer.mock(ProjectService.ProjectService)({ snapshot: Effect.succeed(snapshot) }),
143143
Layer.mock(ThreadManagement.ThreadManagementService)({
144-
getShellSnapshot: () =>
145-
Effect.succeed({
146-
schemaVersion: 1,
147-
snapshotSequence: 0,
148-
threads: [],
149-
archivedThreads: [],
150-
}),
144+
countThreads: () => Effect.succeed(0),
151145
}),
152146
Layer.mock(ThreadLaunch.ThreadLaunchService)({}),
153147
Layer.mock(ServiceLauncherClient.ServiceLauncherClient)({

‎apps/server/src/serverRuntimeStartup.test.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,8 @@ it.effect.each([
176176
}),
177177
}),
178178
Layer.mock(ThreadManagement.ThreadManagementService)({
179-
getShellSnapshot: () =>
180-
Effect.succeed({
181-
threads: [],
182-
archivedThreads: [],
183-
schemaVersion: 1,
184-
snapshotSequence: 0,
185-
}),
179+
countThreads: () => Effect.succeed(0),
180+
listActiveShellIndex: () => Effect.succeed([]),
186181
}),
187182
Layer.mock(ThreadLaunch.ThreadLaunchService)({
188183
launch: (input) => {

0 commit comments

Comments
 (0)