Skip to content

Commit d7a8d6f

Browse files
authored
fix(server): avoid full shell read on resume (#23987)
1 parent 807319c commit d7a8d6f

9 files changed

Lines changed: 84 additions & 7 deletions

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ const layerManagement = Layer.unwrap(
8989
projections.getThreadSnapshotWindow(id, options).pipe(Effect.orDie),
9090
getThreadShell: (id) => projections.getThreadShell(id).pipe(Effect.orDie),
9191
getShellSnapshot: (options) => projections.getShellSnapshot(options).pipe(Effect.orDie),
92+
listActiveSubagentThreadIds: () =>
93+
projections.listActiveSubagentThreadIds().pipe(Effect.orDie),
9294
readShellSnapshot: (options) =>
9395
projections.readShellSnapshot(options).pipe(Effect.map(Effect.orDie), Effect.orDie),
9496
streamStoredEventsFrom: (input) =>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ export interface OrchestratorV2Shape {
371371
readonly getShellSnapshot: (
372372
options?: ShellSnapshotOptions,
373373
) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, OrchestratorV2Error>;
374+
readonly listActiveSubagentThreadIds: () => Effect.Effect<
375+
ReadonlyArray<ThreadId>,
376+
OrchestratorV2Error
377+
>;
374378
readonly readShellSnapshot: (
375379
options?: ShellSnapshotOptions,
376380
) => Effect.Effect<
@@ -12237,6 +12241,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1223712241
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause }))),
1223812242
getShellSnapshot: (options) =>
1223912243
projectionStore.getShellSnapshot(options).pipe(Effect.mapError(shellProjectionError)),
12244+
listActiveSubagentThreadIds: () =>
12245+
projectionStore.listActiveSubagentThreadIds().pipe(Effect.mapError(shellProjectionError)),
1224012246
readShellSnapshot: (options) =>
1224112247
projectionStore
1224212248
.readShellSnapshot(options)
@@ -12382,6 +12388,13 @@ const layerUnavailable: Layer.Layer<OrchestratorV2> = Layer.succeed(
1238212388
cause: "Orchestration V2 live runtime is not configured.",
1238312389
}),
1238412390
),
12391+
listActiveSubagentThreadIds: () =>
12392+
Effect.fail(
12393+
new OrchestratorProjectionError({
12394+
threadId: ThreadId.make("thread:shell"),
12395+
cause: "Orchestration V2 live runtime is not configured.",
12396+
}),
12397+
),
1238512398
readShellSnapshot: () =>
1238612399
Effect.fail(
1238712400
new OrchestratorProjectionError({

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,36 @@ const assertProjectScopedShells = Effect.fn("assertProjectScopedShells")(functio
11051105
return { projectId, activeThreadId, forkThreadId, otherThreadId };
11061106
});
11071107

1108+
it.effect("lists only active subagent IDs without loading shell data", () =>
1109+
Effect.gen(function* () {
1110+
const store = yield* ProjectionStore.ProjectionStoreV2;
1111+
const sql = yield* SqlClient.SqlClient;
1112+
const createdAt = "2026-01-05T00:00:00.000Z";
1113+
for (const [id, relationship, archivedAt, deletedAt] of [
1114+
["parent", null, null, null],
1115+
["active-subagent", "subagent", null, null],
1116+
["archived-subagent", "subagent", createdAt, null],
1117+
["deleted-subagent", "subagent", null, createdAt],
1118+
] as const) {
1119+
yield* sql`
1120+
INSERT INTO orchestration_v2_projection_threads (
1121+
thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
1122+
created_at, updated_at, archived_at, deleted_at, payload_json
1123+
) VALUES (
1124+
${id}, 'project:test', ${id}, 'codex', 'full-access', 'default',
1125+
${createdAt}, ${createdAt}, ${archivedAt}, ${deletedAt},
1126+
${JSON.stringify({ lineage: { relationshipToParent: relationship }, archivedAt })}
1127+
)
1128+
`;
1129+
}
1130+
1131+
assert.sameMembers(
1132+
[...(yield* store.listActiveSubagentThreadIds())],
1133+
[ThreadId.make("active-subagent")],
1134+
);
1135+
}).pipe(Effect.provide(layerTest)),
1136+
);
1137+
11081138
it.layer(layerTest)("ProjectionStoreV2", (it) => {
11091139
// regression: #21969 keep corrupt plan JSON in the typed read error
11101140
it.effect("returns a typed read error for malformed stored plan JSON", () =>

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,10 @@ export interface ProjectionStoreV2Shape {
424424
readonly getShellSnapshot: (
425425
options?: ShellSnapshotOptions,
426426
) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, ProjectionStoreV2Error>;
427+
readonly listActiveSubagentThreadIds: () => Effect.Effect<
428+
ReadonlyArray<ThreadId>,
429+
ProjectionStoreV2Error
430+
>;
427431
/**
428432
* Callers that combine the shell with other reads run this inside their own
429433
* transaction and the returned decode after it commits, so the shared
@@ -6430,6 +6434,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
64306434
.withTransaction(readShellSnapshotRows(options))
64316435
.pipe(Effect.mapError(shellSnapshotReadError), Effect.flatMap(decodeShellSnapshot));
64326436

6437+
const listActiveSubagentThreadIds = Effect.fn("ProjectionStore.listActiveSubagentThreadIds")(
6438+
function* () {
6439+
const rows = yield* sql<{ readonly thread_id: string }>`
6440+
SELECT thread_id
6441+
FROM orchestration_v2_projection_threads
6442+
WHERE deleted_at IS NULL
6443+
AND json_extract(payload_json, '$.archivedAt') IS NULL
6444+
AND json_extract(payload_json, '$.lineage.relationshipToParent') = 'subagent'
6445+
`;
6446+
return rows.map((row) => ThreadId.make(row.thread_id));
6447+
},
6448+
Effect.mapError(shellSnapshotReadError),
6449+
);
6450+
64336451
const getThreadShell: ProjectionStoreV2Shape["getThreadShell"] = (threadId) =>
64346452
sql
64356453
.withTransaction(
@@ -6522,6 +6540,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
65226540
return {
65236541
apply,
65246542
getShellSnapshot: readOnReader(getShellSnapshot),
6543+
listActiveSubagentThreadIds: readOnReader(listActiveSubagentThreadIds),
65256544
readShellSnapshot: readOnReader(readShellSnapshot),
65266545
getThreadShell: readOnReader(getThreadShell),
65276546
getThread: readOnReader(getThread),
@@ -6630,6 +6649,19 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
66306649
archivedThreads: visible.filter((thread) => thread.archivedAt !== null),
66316650
};
66326651
}),
6652+
listActiveSubagentThreadIds: Effect.fn("ProjectionStore.listActiveSubagentThreadIds")(
6653+
function* () {
6654+
const existing = (yield* Ref.get(replayState)).projections;
6655+
return [...existing.values()]
6656+
.filter(
6657+
({ thread }) =>
6658+
thread.deletedAt === null &&
6659+
thread.archivedAt === null &&
6660+
thread.lineage.relationshipToParent === "subagent",
6661+
)
6662+
.map(({ thread }) => thread.id);
6663+
},
6664+
),
66336665
readShellSnapshot: (options) =>
66346666
service.getShellSnapshot(options).pipe(Effect.map(Effect.succeed)),
66356667
getThreadShell: Effect.fn("ProjectionStore.getThreadShell")(function* (threadId) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ it.effect(
211211
apply: () => Effect.void,
212212
getLimitRecoveryCandidates: () => Effect.die("unused getLimitRecoveryCandidates"),
213213
getShellSnapshot: () => Effect.die("unused getShellSnapshot"),
214+
listActiveSubagentThreadIds: () => Effect.die("unused listActiveSubagentThreadIds"),
214215
readShellSnapshot: () => Effect.die("unused readShellSnapshot"),
215216
getThreadShell: () => Effect.die("unused getThreadShell"),
216217
getThread: () => Ref.get(projection).pipe(Effect.map((state) => state.thread)),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ export interface ThreadManagementServiceShape {
362362
readonly getShellSnapshot: (options?: {
363363
readonly location?: "active" | "archive";
364364
}) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, Orchestrator.OrchestratorV2Error>;
365+
readonly listActiveSubagentThreadIds: Orchestrator.OrchestratorV2["Service"]["listActiveSubagentThreadIds"];
365366
readonly readShellSnapshot: (options?: {
366367
readonly location?: "active" | "archive";
367368
}) => Effect.Effect<
@@ -1173,6 +1174,7 @@ const make = Effect.gen(function* () {
11731174
getProjectThreadRecords,
11741175
getProjectThread,
11751176
getShellSnapshot: orchestrator.getShellSnapshot,
1177+
listActiveSubagentThreadIds: orchestrator.listActiveSubagentThreadIds,
11761178
readShellSnapshot: orchestrator.readShellSnapshot,
11771179
getThreadShell: orchestrator.getThreadShell,
11781180
listProjectThreads,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
187187
catchUp.shellSnapshotReads += 1;
188188
return { schemaVersion: 2, snapshotSequence: 1, threads: [], archivedThreads: [] };
189189
}),
190+
listActiveSubagentThreadIds: unused,
190191
readShellSnapshot: unused,
191192
ensureLegacyTranscript: unused,
192193
dispatch: unused,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ it.live.each(["thread", "shell"] as const)(
252252
const items = yield* subscription.pipe(
253253
Effect.provideService(ThreadManagement.ThreadManagementService, {
254254
ensureLegacyTranscript: () => Effect.void,
255-
getShellSnapshot: () => Effect.succeed({ threads: [] }),
255+
getShellSnapshot: () => Effect.die("shell resume must not load a full snapshot"),
256+
listActiveSubagentThreadIds: () => Effect.succeed([]),
256257
streamStoredEventsFrom: (
257258
input: Parameters<
258259
ThreadManagement.ThreadManagementService["Service"]["streamStoredEventsFrom"]

‎apps/server/src/ws.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,12 +1172,7 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
11721172
});
11731173
}
11741174

1175-
const currentShell = yield* threadManagement.getShellSnapshot({ location: "active" });
1176-
subagentThreadIds = new Set(
1177-
currentShell.threads
1178-
.filter((thread) => thread.lineage.relationshipToParent === "subagent")
1179-
.map((thread) => thread.id),
1180-
);
1175+
subagentThreadIds = new Set(yield* threadManagement.listActiveSubagentThreadIds());
11811176

11821177
const loaded = yield* loadProjectMetadataSnapshot(highWater);
11831178
const replay = toShellStream(

0 commit comments

Comments
 (0)