Skip to content
TypeScript

fix(server): avoid full shell read on resume (#23987)

Yash Singhd7a8d6f32001
diff --git a/apps/server/integration/transferBudgetV2.integration.test.ts b/apps/server/integration/transferBudgetV2.integration.test.ts
index 5064848571b..75b0be5ae57 100644
--- a/apps/server/integration/transferBudgetV2.integration.test.ts
+++ b/apps/server/integration/transferBudgetV2.integration.test.ts
@@ -89,6 +89,8 @@ const layerManagement = Layer.unwrap(
projections.getThreadSnapshotWindow(id, options).pipe(Effect.orDie),
getThreadShell: (id) => projections.getThreadShell(id).pipe(Effect.orDie),
getShellSnapshot: (options) => projections.getShellSnapshot(options).pipe(Effect.orDie),
+ listActiveSubagentThreadIds: () =>
+ projections.listActiveSubagentThreadIds().pipe(Effect.orDie),
readShellSnapshot: (options) =>
projections.readShellSnapshot(options).pipe(Effect.map(Effect.orDie), Effect.orDie),
streamStoredEventsFrom: (input) =>
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index 5dd48ba548d..4a3b101b494 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -371,6 +371,10 @@ export interface OrchestratorV2Shape {
readonly getShellSnapshot: (
options?: ShellSnapshotOptions,
) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, OrchestratorV2Error>;
+ readonly listActiveSubagentThreadIds: () => Effect.Effect<
+ ReadonlyArray<ThreadId>,
+ OrchestratorV2Error
+ >;
readonly readShellSnapshot: (
options?: ShellSnapshotOptions,
) => Effect.Effect<
@@ -12237,6 +12241,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause }))),
getShellSnapshot: (options) =>
projectionStore.getShellSnapshot(options).pipe(Effect.mapError(shellProjectionError)),
+ listActiveSubagentThreadIds: () =>
+ projectionStore.listActiveSubagentThreadIds().pipe(Effect.mapError(shellProjectionError)),
readShellSnapshot: (options) =>
projectionStore
.readShellSnapshot(options)
@@ -12382,6 +12388,13 @@ const layerUnavailable: Layer.Layer<OrchestratorV2> = Layer.succeed(
cause: "Orchestration V2 live runtime is not configured.",
}),
),
+ listActiveSubagentThreadIds: () =>
+ Effect.fail(
+ new OrchestratorProjectionError({
+ threadId: ThreadId.make("thread:shell"),
+ cause: "Orchestration V2 live runtime is not configured.",
+ }),
+ ),
readShellSnapshot: () =>
Effect.fail(
new OrchestratorProjectionError({
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts
index 6f87afb2eb2..fcadd8036b9 100644
--- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts
+++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts
@@ -1105,6 +1105,36 @@ const assertProjectScopedShells = Effect.fn("assertProjectScopedShells")(functio
return { projectId, activeThreadId, forkThreadId, otherThreadId };
});
+it.effect("lists only active subagent IDs without loading shell data", () =>
+ Effect.gen(function* () {
+ const store = yield* ProjectionStore.ProjectionStoreV2;
+ const sql = yield* SqlClient.SqlClient;
+ const createdAt = "2026-01-05T00:00:00.000Z";
+ for (const [id, relationship, archivedAt, deletedAt] of [
+ ["parent", null, null, null],
+ ["active-subagent", "subagent", null, null],
+ ["archived-subagent", "subagent", createdAt, null],
+ ["deleted-subagent", "subagent", null, createdAt],
+ ] as const) {
+ yield* sql`
+ INSERT INTO orchestration_v2_projection_threads (
+ thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
+ created_at, updated_at, archived_at, deleted_at, payload_json
+ ) VALUES (
+ ${id}, 'project:test', ${id}, 'codex', 'full-access', 'default',
+ ${createdAt}, ${createdAt}, ${archivedAt}, ${deletedAt},
+ ${JSON.stringify({ lineage: { relationshipToParent: relationship }, archivedAt })}
+ )
+ `;
+ }
+
+ assert.sameMembers(
+ [...(yield* store.listActiveSubagentThreadIds())],
+ [ThreadId.make("active-subagent")],
+ );
+ }).pipe(Effect.provide(layerTest)),
+);
+
it.layer(layerTest)("ProjectionStoreV2", (it) => {
// regression: #21969 keep corrupt plan JSON in the typed read error
it.effect("returns a typed read error for malformed stored plan JSON", () =>
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts
index 4f08378db52..00333529dd1 100644
--- a/apps/server/src/orchestration-v2/ProjectionStore.ts
+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts
@@ -424,6 +424,10 @@ export interface ProjectionStoreV2Shape {
readonly getShellSnapshot: (
options?: ShellSnapshotOptions,
) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, ProjectionStoreV2Error>;
+ readonly listActiveSubagentThreadIds: () => Effect.Effect<
+ ReadonlyArray<ThreadId>,
+ ProjectionStoreV2Error
+ >;
/**
* Callers that combine the shell with other reads run this inside their own
* transaction and the returned decode after it commits, so the shared
@@ -6430,6 +6434,20 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
.withTransaction(readShellSnapshotRows(options))
.pipe(Effect.mapError(shellSnapshotReadError), Effect.flatMap(decodeShellSnapshot));
+ const listActiveSubagentThreadIds = Effect.fn("ProjectionStore.listActiveSubagentThreadIds")(
+ function* () {
+ const rows = yield* sql<{ readonly thread_id: string }>`
+ SELECT thread_id
+ FROM orchestration_v2_projection_threads
+ WHERE deleted_at IS NULL
+ AND json_extract(payload_json, '$.archivedAt') IS NULL
+ AND json_extract(payload_json, '$.lineage.relationshipToParent') = 'subagent'
+ `;
+ return rows.map((row) => ThreadId.make(row.thread_id));
+ },
+ Effect.mapError(shellSnapshotReadError),
+ );
+
const getThreadShell: ProjectionStoreV2Shape["getThreadShell"] = (threadId) =>
sql
.withTransaction(
@@ -6522,6 +6540,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
return {
apply,
getShellSnapshot: readOnReader(getShellSnapshot),
+ listActiveSubagentThreadIds: readOnReader(listActiveSubagentThreadIds),
readShellSnapshot: readOnReader(readShellSnapshot),
getThreadShell: readOnReader(getThreadShell),
getThread: readOnReader(getThread),
@@ -6630,6 +6649,19 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
archivedThreads: visible.filter((thread) => thread.archivedAt !== null),
};
}),
+ listActiveSubagentThreadIds: Effect.fn("ProjectionStore.listActiveSubagentThreadIds")(
+ function* () {
+ const existing = (yield* Ref.get(replayState)).projections;
+ return [...existing.values()]
+ .filter(
+ ({ thread }) =>
+ thread.deletedAt === null &&
+ thread.archivedAt === null &&
+ thread.lineage.relationshipToParent === "subagent",
+ )
+ .map(({ thread }) => thread.id);
+ },
+ ),
readShellSnapshot: (options) =>
service.getShellSnapshot(options).pipe(Effect.map(Effect.succeed)),
getThreadShell: Effect.fn("ProjectionStore.getThreadShell")(function* (threadId) {
diff --git a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
index 178c6ec3dcc..3ae3a9974b4 100644
--- a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
+++ b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
@@ -211,6 +211,7 @@ it.effect(
apply: () => Effect.void,
getLimitRecoveryCandidates: () => Effect.die("unused getLimitRecoveryCandidates"),
getShellSnapshot: () => Effect.die("unused getShellSnapshot"),
+ listActiveSubagentThreadIds: () => Effect.die("unused listActiveSubagentThreadIds"),
readShellSnapshot: () => Effect.die("unused readShellSnapshot"),
getThreadShell: () => Effect.die("unused getThreadShell"),
getThread: () => Ref.get(projection).pipe(Effect.map((state) => state.thread)),
diff --git a/apps/server/src/orchestration-v2/ThreadManagementService.ts b/apps/server/src/orchestration-v2/ThreadManagementService.ts
index 543b3e2f89f..f2efb71637f 100644
--- a/apps/server/src/orchestration-v2/ThreadManagementService.ts
+++ b/apps/server/src/orchestration-v2/ThreadManagementService.ts
@@ -362,6 +362,7 @@ export interface ThreadManagementServiceShape {
readonly getShellSnapshot: (options?: {
readonly location?: "active" | "archive";
}) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, Orchestrator.OrchestratorV2Error>;
+ readonly listActiveSubagentThreadIds: Orchestrator.OrchestratorV2["Service"]["listActiveSubagentThreadIds"];
readonly readShellSnapshot: (options?: {
readonly location?: "active" | "archive";
}) => Effect.Effect<
@@ -1173,6 +1174,7 @@ const make = Effect.gen(function* () {
getProjectThreadRecords,
getProjectThread,
getShellSnapshot: orchestrator.getShellSnapshot,
+ listActiveSubagentThreadIds: orchestrator.listActiveSubagentThreadIds,
readShellSnapshot: orchestrator.readShellSnapshot,
getThreadShell: orchestrator.getThreadShell,
listProjectThreads,
diff --git a/apps/server/src/relay/AgentAwarenessRelay.test.ts b/apps/server/src/relay/AgentAwarenessRelay.test.ts
index 64b71140fdc..d1ad9e7a58a 100644
--- a/apps/server/src/relay/AgentAwarenessRelay.test.ts
+++ b/apps/server/src/relay/AgentAwarenessRelay.test.ts
@@ -187,6 +187,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
catchUp.shellSnapshotReads += 1;
return { schemaVersion: 2, snapshotSequence: 1, threads: [], archivedThreads: [] };
}),
+ listActiveSubagentThreadIds: unused,
readShellSnapshot: unused,
ensureLegacyTranscript: unused,
dispatch: unused,
diff --git a/apps/server/src/ws.test.ts b/apps/server/src/ws.test.ts
index 75da37ad3b6..787fdc268f4 100644
--- a/apps/server/src/ws.test.ts
+++ b/apps/server/src/ws.test.ts
@@ -252,7 +252,8 @@ it.live.each(["thread", "shell"] as const)(
const items = yield* subscription.pipe(
Effect.provideService(ThreadManagement.ThreadManagementService, {
ensureLegacyTranscript: () => Effect.void,
- getShellSnapshot: () => Effect.succeed({ threads: [] }),
+ getShellSnapshot: () => Effect.die("shell resume must not load a full snapshot"),
+ listActiveSubagentThreadIds: () => Effect.succeed([]),
streamStoredEventsFrom: (
input: Parameters<
ThreadManagement.ThreadManagementService["Service"]["streamStoredEventsFrom"]
diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts
index 556a023a01c..7656ddca925 100644
--- a/apps/server/src/ws.ts
+++ b/apps/server/src/ws.ts
@@ -1172,12 +1172,7 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
});
}
- const currentShell = yield* threadManagement.getShellSnapshot({ location: "active" });
- subagentThreadIds = new Set(
- currentShell.threads
- .filter((thread) => thread.lineage.relationshipToParent === "subagent")
- .map((thread) => thread.id),
- );
+ subagentThreadIds = new Set(yield* threadManagement.listActiveSubagentThreadIds());
const loaded = yield* loadProjectMetadataSnapshot(highWater);
const replay = toShellStream(