Skip to content

Commit 71d8526

Browse files
fix(server): thread list builds metadata only for the page it returns
t3_thread_list (and `t3 thread list`) built a full shell for every active thread in scope before filtering and slicing the page, so a one-row list cost grew with the total thread count and history. Listing now reads a light index of active threads (id, project, title, lineage, settlement, snooze end, updatedAt), applies the lineage, settled, title and page filters to it, and builds shells only for the page. Status and snoozed filters still need shell state, so they build shells only for the candidates left after the cheap filters. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 36f011d commit 71d8526

8 files changed

Lines changed: 181 additions & 51 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3975,6 +3975,7 @@ describe("OrchestratorMcpService thread list", () => {
39753975
],
39763976
},
39773977
];
3978+
const shellRequests: Array<ReadonlyArray<ThreadId>> = [];
39783979
const service = layerOrchestratorService.pipe(
39793980
Layer.provide(
39803981
Layer.mergeAll(
@@ -3987,6 +3988,13 @@ describe("OrchestratorMcpService thread list", () => {
39873988
(thread) => input.projectId === undefined || thread.projectId === input.projectId,
39883989
),
39893990
),
3991+
getShellSnapshot: (options) => {
3992+
const threadIds = options?.threadIds ?? shells.map((thread) => thread.id);
3993+
shellRequests.push(threadIds);
3994+
return Effect.succeed({
3995+
threads: shells.filter((thread) => threadIds.includes(thread.id)),
3996+
} as never);
3997+
},
39903998
}),
39913999
ServerSettings.layerTest(),
39924000
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
@@ -4080,6 +4088,22 @@ describe("OrchestratorMcpService thread list", () => {
40804088
}).pipe(Effect.provide(service)),
40814089
);
40824090

4091+
it.effect("builds shells only for the threads on the page", () =>
4092+
Effect.gen(function* () {
4093+
const mcp = yield* OrchestratorMcpService.OrchestratorMcpService;
4094+
shellRequests.length = 0;
4095+
const listed = yield* mcp.listThreads(client, {
4096+
projectId: ProjectId.make("project:a"),
4097+
limit: 1,
4098+
});
4099+
assert.deepStrictEqual(
4100+
[listed.threads.map((thread) => thread.threadId), listed.total, listed.nextCursor],
4101+
[["thread:a-snoozed"], 3, 1],
4102+
);
4103+
assert.deepStrictEqual(shellRequests, [[ThreadId.make("thread:a-snoozed")]]);
4104+
}).pipe(Effect.provide(service)),
4105+
);
4106+
40834107
it.effect("points a caller outside any thread at allProjects when it omits projectId", () =>
40844108
Effect.gen(function* () {
40854109
const mcp = yield* OrchestratorMcpService.OrchestratorMcpService;

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3104,46 +3104,73 @@ const make = Effect.gen(function* () {
31043104
if (!input.allProjects && input.projectId !== undefined) {
31053105
yield* requireProject(input.projectId);
31063106
}
3107+
const listFailure = (error: unknown) =>
3108+
failure("orchestration_error", `Unable to list threads: ${errorMessage(error)}`);
31073109
const projectThreads = yield* threadManagement
31083110
.listProjectThreads({
31093111
projectId: projectId ?? undefined,
31103112
includeSubagents: input.includeSubagents !== false,
31113113
})
3112-
.pipe(
3113-
Effect.mapError((error) =>
3114-
failure("orchestration_error", `Unable to list threads: ${errorMessage(error)}`),
3115-
),
3116-
);
3114+
.pipe(Effect.mapError(listFailure));
31173115
const nowMs = yield* Clock.currentTimeMillis;
31183116
// An empty list is no filter: clients that fill every optional field send `[]`.
31193117
const statuses =
31203118
input.statuses === undefined || input.statuses.length === 0
31213119
? null
31223120
: new Set(input.statuses);
31233121
const titleContains = input.titleContains?.toLocaleLowerCase();
3124-
const filtered = projectThreads
3125-
.filter((thread) => statuses === null || statuses.has(listedThreadStatus(thread)))
3122+
const candidates = projectThreads
31263123
.filter(
31273124
(thread) =>
31283125
input.settled === undefined || threadSettlement(thread).settled === input.settled,
31293126
)
31303127
.filter(
3131-
(thread) => input.snoozed === undefined || isSnoozed(thread, nowMs) === input.snoozed,
3128+
(thread) =>
3129+
titleContains === undefined || thread.title.toLocaleLowerCase().includes(titleContains),
31323130
)
3131+
// Only a snooze that ends later can hold, so the rest skip building a shell.
31333132
.filter(
31343133
(thread) =>
3135-
titleContains === undefined || thread.title.toLocaleLowerCase().includes(titleContains),
3134+
input.snoozed !== true ||
3135+
(thread.snoozedUntil != null && DateTime.toEpochMillis(thread.snoozedUntil) > nowMs),
31363136
);
3137+
// Shells are the costly part, so build them only for the threads the page needs.
3138+
const shellsOf = (threads: ReadonlyArray<{ readonly id: ThreadId }>) =>
3139+
threads.length === 0
3140+
? Effect.succeed([])
3141+
: threadManagement
3142+
.getShellSnapshot({ location: "active", threadIds: threads.map(({ id }) => id) })
3143+
.pipe(
3144+
Effect.map((snapshot) => {
3145+
const shellsById = new Map(snapshot.threads.map((shell) => [shell.id, shell]));
3146+
return threads.flatMap(({ id }) => shellsById.get(id) ?? []);
3147+
}),
3148+
Effect.mapError(listFailure),
3149+
);
31373150
const cursor = input.cursor ?? 0;
31383151
const limit = input.limit ?? DEFAULT_THREAD_LIST_LIMIT;
3139-
const page = filtered.slice(cursor, cursor + limit);
3140-
const nextCursor = cursor + page.length < filtered.length ? cursor + page.length : null;
3152+
let page: ReadonlyArray<OrchestrationV2ThreadShell>;
3153+
let total: number;
3154+
if (statuses === null && input.snoozed === undefined) {
3155+
total = candidates.length;
3156+
page = yield* shellsOf(candidates.slice(cursor, cursor + limit));
3157+
} else {
3158+
// Status and snooze state are derived from the shell.
3159+
const filtered = (yield* shellsOf(candidates))
3160+
.filter((thread) => statuses === null || statuses.has(listedThreadStatus(thread)))
3161+
.filter(
3162+
(thread) => input.snoozed === undefined || isSnoozed(thread, nowMs) === input.snoozed,
3163+
);
3164+
total = filtered.length;
3165+
page = filtered.slice(cursor, cursor + limit);
3166+
}
3167+
const nextCursor = cursor + limit < total ? cursor + limit : null;
31413168
return {
31423169
projectId,
31433170
currentThreadId: parent?.thread.id ?? null,
31443171
threads: page.map((shell) => listItemFromShell(scope, shell, nowMs)),
31453172
nextCursor,
3146-
total: filtered.length,
3173+
total,
31473174
} satisfies OrchestratorMcpThreadListResult;
31483175
}),
31493176
readThread: Effect.fn("OrchestratorMcpService.readThread")(function* (scope, input) {

‎apps/server/src/mcp/toolkits/core.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,7 @@ it.effect(
13281328
Layer.provide(
13291329
Layer.mock(ThreadManagement.ThreadManagementService)({
13301330
listProjectThreads: () => Effect.succeed([unsettled, settled]),
1331+
getShellSnapshot: () => Effect.succeed({ threads: [unsettled, settled] } as never),
13311332
}),
13321333
),
13331334
Layer.provide(ServerSettings.layerTest()),

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
OrchestrationV2SearchThreadInput,
55
OrchestrationV2SearchThreadResult,
66
OrchestrationV2ThreadHistoryPage,
7+
ProjectId,
78
} from "@t3tools/contracts";
89
import {
910
latestExecutedRun,
@@ -173,6 +174,7 @@ import {
173174
type ProjectionCheckpointContext,
174175
type OlderSettledThreadsListing,
175176
type ActiveShellIndexEntry,
177+
type ActiveThreadListEntry,
176178
type OlderSettledThreadsQuery,
177179
type ShellSnapshotOptions,
178180
} from "./ProjectionStore.ts";
@@ -504,6 +506,9 @@ export interface OrchestratorV2Shape {
504506
ReadonlyArray<ActiveShellIndexEntry>,
505507
OrchestratorV2Error
506508
>;
509+
readonly listActiveThreads: (
510+
projectId?: ProjectId,
511+
) => Effect.Effect<ReadonlyArray<ActiveThreadListEntry>, OrchestratorV2Error>;
507512
readonly listOlderSettledThreads: (
508513
query: OlderSettledThreadsQuery,
509514
) => Effect.Effect<OlderSettledThreadsListing, OrchestratorV2Error>;
@@ -15997,6 +16002,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1599716002
listActiveShellIndex: Effect.fn("OrchestratorV2.listActiveShellIndex")(function () {
1599816003
return projectionStore.listActiveShellIndex().pipe(Effect.mapError(shellProjectionError));
1599916004
}),
16005+
listActiveThreads: Effect.fn("OrchestratorV2.listActiveThreads")(function (projectId) {
16006+
return projectionStore
16007+
.listActiveThreads(projectId)
16008+
.pipe(Effect.mapError(shellProjectionError));
16009+
}),
1600016010
listOlderSettledThreads: Effect.fn("OrchestratorV2.listOlderSettledThreads")(function (query) {
1600116011
return projectionStore
1600216012
.listOlderSettledThreads(query)

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

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,19 @@ export interface ActiveShellIndexEntry extends Pick<
467467
readonly hasRemoteTasks: boolean;
468468
}
469469

470+
/** What thread listings filter and order an active thread by, without building its shell. */
471+
export type ActiveThreadListEntry = Pick<
472+
OrchestrationV2ThreadShell,
473+
| "id"
474+
| "projectId"
475+
| "title"
476+
| "lineage"
477+
| "settledOverride"
478+
| "settledAt"
479+
| "snoozedUntil"
480+
| "updatedAt"
481+
>;
482+
470483
export interface OlderSettledThreadsQuery {
471484
readonly before: OrchestrationV2OlderSettledCursor;
472485
readonly projectIds?: ReadonlyArray<ProjectId> | undefined;
@@ -567,6 +580,10 @@ export interface ProjectionStoreV2Shape {
567580
ReadonlyArray<ActiveShellIndexEntry>,
568581
ProjectionStoreV2Error
569582
>;
583+
/** Every active thread, or one project's. */
584+
readonly listActiveThreads: (
585+
projectId?: ProjectId,
586+
) => Effect.Effect<ReadonlyArray<ActiveThreadListEntry>, ProjectionStoreV2Error>;
570587
/** Older settled thread ids below a cursor, newest settle first, and per-project counts. */
571588
readonly listOlderSettledThreads: (
572589
query: OlderSettledThreadsQuery,
@@ -1150,6 +1167,21 @@ const encodeTimelineSources = Schema.encodeEffect(
11501167
Schema.Array(Schema.Struct({ threadId: Schema.String, id: Schema.NullOr(Schema.String) })),
11511168
),
11521169
);
1170+
const shellIndexLineage = (row: {
1171+
readonly relationship_to_parent: string | null;
1172+
readonly parent_thread_id: string | null;
1173+
readonly root_thread_id: string;
1174+
readonly side_chat: number | null;
1175+
}): OrchestrationV2ThreadShell["lineage"] => ({
1176+
parentThreadId: row.parent_thread_id === null ? null : ThreadId.make(row.parent_thread_id),
1177+
relationshipToParent:
1178+
row.relationship_to_parent === "fork" || row.relationship_to_parent === "subagent"
1179+
? row.relationship_to_parent
1180+
: null,
1181+
rootThreadId: ThreadId.make(row.root_thread_id),
1182+
...(row.side_chat === null ? {} : { sideChat: row.side_chat === 1 }),
1183+
});
1184+
11531185
const encodeIdList = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema.String)));
11541186

11551187
// Empty filters still scan SQLite rows; short-circuit them, including turnItemTypes: [].
@@ -8770,16 +8802,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
87708802
return rows.map((row): ActiveShellIndexEntry => ({
87718803
id: ThreadId.make(row.thread_id),
87728804
projectId: ProjectId.make(row.project_id),
8773-
lineage: {
8774-
parentThreadId:
8775-
row.parent_thread_id === null ? null : ThreadId.make(row.parent_thread_id),
8776-
relationshipToParent:
8777-
row.relationship_to_parent === "fork" || row.relationship_to_parent === "subagent"
8778-
? row.relationship_to_parent
8779-
: null,
8780-
rootThreadId: ThreadId.make(row.root_thread_id),
8781-
...(row.side_chat === null ? {} : { sideChat: row.side_chat === 1 }),
8782-
},
8805+
lineage: shellIndexLineage(row),
87838806
settledOverride:
87848807
row.settled_override === "settled" || row.settled_override === "active"
87858808
? row.settled_override
@@ -8792,6 +8815,53 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
87928815
}));
87938816
}, Effect.mapError(shellSnapshotReadError));
87948817

8818+
const listActiveThreads = Effect.fn("ProjectionStore.listActiveThreads")(function* (
8819+
projectId?: ProjectId,
8820+
) {
8821+
const rows = yield* sql<{
8822+
readonly thread_id: string;
8823+
readonly project_id: string;
8824+
readonly title: string;
8825+
readonly updated_at: string;
8826+
readonly relationship_to_parent: string | null;
8827+
readonly parent_thread_id: string | null;
8828+
readonly root_thread_id: string;
8829+
readonly side_chat: number | null;
8830+
readonly settled_override: string | null;
8831+
readonly settled_at: string | null;
8832+
readonly snoozed_until: string | null;
8833+
}>`
8834+
SELECT
8835+
t.thread_id,
8836+
t.project_id,
8837+
t.title,
8838+
t.updated_at,
8839+
json_extract(t.payload_json, '$.lineage.relationshipToParent') AS relationship_to_parent,
8840+
json_extract(t.payload_json, '$.lineage.parentThreadId') AS parent_thread_id,
8841+
json_extract(t.payload_json, '$.lineage.rootThreadId') AS root_thread_id,
8842+
json_extract(t.payload_json, '$.lineage.sideChat') AS side_chat,
8843+
json_extract(t.payload_json, '$.settledOverride') AS settled_override,
8844+
json_extract(t.payload_json, '$.settledAt') AS settled_at,
8845+
json_extract(t.payload_json, '$.snoozedUntil') AS snoozed_until
8846+
FROM orchestration_v2_projection_threads t
8847+
WHERE t.deleted_at IS NULL
8848+
AND t.archived_at IS NULL${projectId === undefined ? sql`` : sql` AND t.project_id = ${projectId}`}
8849+
`;
8850+
return rows.map((row): ActiveThreadListEntry => ({
8851+
id: ThreadId.make(row.thread_id),
8852+
projectId: ProjectId.make(row.project_id),
8853+
title: row.title,
8854+
lineage: shellIndexLineage(row),
8855+
settledOverride:
8856+
row.settled_override === "settled" || row.settled_override === "active"
8857+
? row.settled_override
8858+
: null,
8859+
settledAt: row.settled_at === null ? null : DateTime.makeUnsafe(row.settled_at),
8860+
snoozedUntil: row.snoozed_until === null ? null : DateTime.makeUnsafe(row.snoozed_until),
8861+
updatedAt: DateTime.makeUnsafe(row.updated_at),
8862+
}));
8863+
}, Effect.mapError(shellSnapshotReadError));
8864+
87958865
const getThreadLinkMetadata = Effect.fn("ProjectionStore.getThreadLinkMetadata")(function* (
87968866
threadIds: ReadonlyArray<ThreadId>,
87978867
) {
@@ -8981,6 +9051,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
89819051
listActiveSubagentThreadIds: readOnReader(listActiveSubagentThreadIds),
89829052
countThreads: readOnReader(countThreads),
89839053
listActiveShellIndex: readOnReader(listActiveShellIndex),
9054+
listActiveThreads: readOnReader(listActiveThreads),
89849055
readShellSnapshot: readOnReader(readShellSnapshot),
89859056
listOlderSettledThreads: readOnReader(listOlderSettledThreads),
89869057
getThreadShell: readOnReader(getThreadShell),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ it.effect(
218218
listActiveSubagentThreadIds: () => Effect.die("unused listActiveSubagentThreadIds"),
219219
countThreads: () => Effect.die("unused countThreads"),
220220
listActiveShellIndex: () => Effect.die("unused listActiveShellIndex"),
221+
listActiveThreads: () => Effect.die("unused listActiveThreads"),
221222
listOlderSettledThreads: () => Effect.die("unused listOlderSettledThreads"),
222223
readShellSnapshot: () => Effect.die("unused readShellSnapshot"),
223224
getThreadShell: () => Effect.die("unused getThreadShell"),

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

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
OrchestrationV2SearchThreadResult,
44
} from "@t3tools/contracts";
55
import type {
6+
ActiveThreadListEntry,
67
ProjectionRecordField,
78
ProjectionRecordFilter,
89
ProjectionRecords,
@@ -30,7 +31,6 @@ import {
3031
type OrchestrationV2ThreadShellSnapshot,
3132
type OrchestrationV2ThreadProjection,
3233
type OrchestrationV2ThreadTranscript,
33-
type OrchestrationV2ThreadShell,
3434
type OrchestrationV2TurnItem,
3535
ProjectId,
3636
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
@@ -451,7 +451,7 @@ export interface ThreadManagementServiceShape {
451451
readonly listProjectThreads: (input: {
452452
readonly projectId?: ProjectId | undefined;
453453
readonly includeSubagents: boolean;
454-
}) => Effect.Effect<ReadonlyArray<OrchestrationV2ThreadShell>, ThreadManagementError>;
454+
}) => Effect.Effect<ReadonlyArray<ActiveThreadListEntry>, ThreadManagementError>;
455455
readonly sendToThread: (
456456
input: ThreadManagementSendInput,
457457
) => Effect.Effect<ThreadManagementSendResult, ThreadManagementFailure>;
@@ -835,33 +835,28 @@ const make = Effect.gen(function* () {
835835
);
836836

837837
const listProjectThreads: ThreadManagementServiceShape["listProjectThreads"] = (input) =>
838-
orchestrator
839-
.getShellSnapshot({
840-
location: "active",
841-
...(input.projectId === undefined ? {} : { projectId: input.projectId }),
842-
})
843-
.pipe(
844-
Effect.mapError(
845-
(cause) =>
846-
new ThreadManagementProjectThreadsListError({
847-
projectId: input.projectId,
848-
cause,
849-
}),
850-
),
851-
Effect.map((snapshot) =>
852-
snapshot.threads
853-
.filter((thread) =>
854-
input.includeSubagents
855-
? thread.lineage.sideChat !== true
856-
: isListedThreadLineage(thread.lineage),
857-
)
858-
.toSorted(
859-
(left, right) =>
860-
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt) ||
861-
right.id.localeCompare(left.id),
862-
),
863-
),
864-
);
838+
orchestrator.listActiveThreads(input.projectId).pipe(
839+
Effect.mapError(
840+
(cause) =>
841+
new ThreadManagementProjectThreadsListError({
842+
projectId: input.projectId,
843+
cause,
844+
}),
845+
),
846+
Effect.map((threads) =>
847+
threads
848+
.filter((thread) =>
849+
input.includeSubagents
850+
? thread.lineage.sideChat !== true
851+
: isListedThreadLineage(thread.lineage),
852+
)
853+
.toSorted(
854+
(left, right) =>
855+
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt) ||
856+
right.id.localeCompare(left.id),
857+
),
858+
),
859+
);
865860

866861
const sendToThread: ThreadManagementServiceShape["sendToThread"] = Effect.fn(
867862
"ThreadManagementService.sendToThread",

0 commit comments

Comments
 (0)