@devries-cameron20 / t3code Mirror · read-only / commits / b537916099
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 16:00 UTC

fix(server): thread list builds metadata only for the page it returns (#36434)

Julius Marminge committed Nov 13, 2026 at 15:13 UTC b5379160993f49edbc4458e091356da3bcd47c4c
8 files changed +181 -51
apps/server/src/mcp/OrchestratorMcpService.test.ts
+24
@@ -3975,6 +3975,7 @@ describe("OrchestratorMcpService thread list", () => {
3975 ],
3976 },
3977 ];
3978 + const shellRequests: Array<ReadonlyArray<ThreadId>> = [];
3979 const service = layerOrchestratorService.pipe(
3980 Layer.provide(
3981 Layer.mergeAll(
@@ -3987,6 +3988,13 @@ describe("OrchestratorMcpService thread list", () => {
3988 (thread) => input.projectId === undefined || thread.projectId === input.projectId,
3989 ),
3990 ),
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 + },
3998 }),
3999 ServerSettings.layerTest(),
4000 Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
@@ -4080,6 +4088,22 @@ describe("OrchestratorMcpService thread list", () => {
4088 }).pipe(Effect.provide(service)),
4089 );
4090
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 +
4107 it.effect("points a caller outside any thread at allProjects when it omits projectId", () =>
4108 Effect.gen(function* () {
4109 const mcp = yield* OrchestratorMcpService.OrchestratorMcpService;
apps/server/src/mcp/OrchestratorMcpService.ts
+39 -12
@@ -3104,16 +3104,14 @@ const make = Effect.gen(function* () {
3104 if (!input.allProjects && input.projectId !== undefined) {
3105 yield* requireProject(input.projectId);
3106 }
3107 + const listFailure = (error: unknown) =>
3108 + failure("orchestration_error", `Unable to list threads: ${errorMessage(error)}`);
3109 const projectThreads = yield* threadManagement
3110 .listProjectThreads({
3111 projectId: projectId ?? undefined,
3112 includeSubagents: input.includeSubagents !== false,
3113 })
3112 - .pipe(
3113 - Effect.mapError((error) =>
3114 - failure("orchestration_error", `Unable to list threads: ${errorMessage(error)}`),
3115 - ),
3116 - );
3114 + .pipe(Effect.mapError(listFailure));
3115 const nowMs = yield* Clock.currentTimeMillis;
3116 // An empty list is no filter: clients that fill every optional field send `[]`.
3117 const statuses =
@@ -3121,29 +3119,58 @@ const make = Effect.gen(function* () {
3119 ? null
3120 : new Set(input.statuses);
3121 const titleContains = input.titleContains?.toLocaleLowerCase();
3124 - const filtered = projectThreads
3125 - .filter((thread) => statuses === null || statuses.has(listedThreadStatus(thread)))
3122 + const candidates = projectThreads
3123 .filter(
3124 (thread) =>
3125 input.settled === undefined || threadSettlement(thread).settled === input.settled,
3126 )
3127 .filter(
3131 - (thread) => input.snoozed === undefined || isSnoozed(thread, nowMs) === input.snoozed,
3128 + (thread) =>
3129 + titleContains === undefined || thread.title.toLocaleLowerCase().includes(titleContains),
3130 )
3131 + // Only a snooze that ends later can hold, so the rest skip building a shell.
3132 .filter(
3133 (thread) =>
3135 - titleContains === undefined || thread.title.toLocaleLowerCase().includes(titleContains),
3134 + input.snoozed !== true ||
3135 + (thread.snoozedUntil != null && DateTime.toEpochMillis(thread.snoozedUntil) > nowMs),
3136 );
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 + );
3150 const cursor = input.cursor ?? 0;
3151 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;
3168 return {
3169 projectId,
3170 currentThreadId: parent?.thread.id ?? null,
3171 threads: page.map((shell) => listItemFromShell(scope, shell, nowMs)),
3172 nextCursor,
3146 - total: filtered.length,
3173 + total,
3174 } satisfies OrchestratorMcpThreadListResult;
3175 }),
3176 readThread: Effect.fn("OrchestratorMcpService.readThread")(function* (scope, input) {
apps/server/src/mcp/toolkits/core.test.ts
+1
@@ -1328,6 +1328,7 @@ it.effect(
1328 Layer.provide(
1329 Layer.mock(ThreadManagement.ThreadManagementService)({
1330 listProjectThreads: () => Effect.succeed([unsettled, settled]),
1331 + getShellSnapshot: () => Effect.succeed({ threads: [unsettled, settled] } as never),
1332 }),
1333 ),
1334 Layer.provide(ServerSettings.layerTest()),
apps/server/src/orchestration-v2/Orchestrator.ts
+10
@@ -4,6 +4,7 @@ import type {
4 OrchestrationV2SearchThreadInput,
5 OrchestrationV2SearchThreadResult,
6 OrchestrationV2ThreadHistoryPage,
7 + ProjectId,
8 } from "@t3tools/contracts";
9 import {
10 latestExecutedRun,
@@ -173,6 +174,7 @@ import {
174 type ProjectionCheckpointContext,
175 type OlderSettledThreadsListing,
176 type ActiveShellIndexEntry,
177 + type ActiveThreadListEntry,
178 type OlderSettledThreadsQuery,
179 type ShellSnapshotOptions,
180 } from "./ProjectionStore.ts";
@@ -504,6 +506,9 @@ export interface OrchestratorV2Shape {
506 ReadonlyArray<ActiveShellIndexEntry>,
507 OrchestratorV2Error
508 >;
509 + readonly listActiveThreads: (
510 + projectId?: ProjectId,
511 + ) => Effect.Effect<ReadonlyArray<ActiveThreadListEntry>, OrchestratorV2Error>;
512 readonly listOlderSettledThreads: (
513 query: OlderSettledThreadsQuery,
514 ) => Effect.Effect<OlderSettledThreadsListing, OrchestratorV2Error>;
@@ -16040,6 +16045,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16045 listActiveShellIndex: Effect.fn("OrchestratorV2.listActiveShellIndex")(function () {
16046 return projectionStore.listActiveShellIndex().pipe(Effect.mapError(shellProjectionError));
16047 }),
16048 + listActiveThreads: Effect.fn("OrchestratorV2.listActiveThreads")(function (projectId) {
16049 + return projectionStore
16050 + .listActiveThreads(projectId)
16051 + .pipe(Effect.mapError(shellProjectionError));
16052 + }),
16053 listOlderSettledThreads: Effect.fn("OrchestratorV2.listOlderSettledThreads")(function (query) {
16054 return projectionStore
16055 .listOlderSettledThreads(query)
apps/server/src/orchestration-v2/ProjectionStore.ts
+81 -10
@@ -467,6 +467,19 @@ export interface ActiveShellIndexEntry extends Pick<
467 readonly hasRemoteTasks: boolean;
468 }
469
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 +
483 export interface OlderSettledThreadsQuery {
484 readonly before: OrchestrationV2OlderSettledCursor;
485 readonly projectIds?: ReadonlyArray<ProjectId> | undefined;
@@ -567,6 +580,10 @@ export interface ProjectionStoreV2Shape {
580 ReadonlyArray<ActiveShellIndexEntry>,
581 ProjectionStoreV2Error
582 >;
583 + /** Every active thread, or one project's. */
584 + readonly listActiveThreads: (
585 + projectId?: ProjectId,
586 + ) => Effect.Effect<ReadonlyArray<ActiveThreadListEntry>, ProjectionStoreV2Error>;
587 /** Older settled thread ids below a cursor, newest settle first, and per-project counts. */
588 readonly listOlderSettledThreads: (
589 query: OlderSettledThreadsQuery,
@@ -1150,6 +1167,21 @@ const encodeTimelineSources = Schema.encodeEffect(
1167 Schema.Array(Schema.Struct({ threadId: Schema.String, id: Schema.NullOr(Schema.String) })),
1168 ),
1169 );
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 +
1185 const encodeIdList = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema.String)));
1186
1187 // Empty filters still scan SQLite rows; short-circuit them, including turnItemTypes: [].
@@ -8770,16 +8802,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
8802 return rows.map((row): ActiveShellIndexEntry => ({
8803 id: ThreadId.make(row.thread_id),
8804 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),
8806 settledOverride:
8807 row.settled_override === "settled" || row.settled_override === "active"
8808 ? row.settled_override
@@ -8792,6 +8815,53 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
8815 }));
8816 }, Effect.mapError(shellSnapshotReadError));
8817
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 +
8865 const getThreadLinkMetadata = Effect.fn("ProjectionStore.getThreadLinkMetadata")(function* (
8866 threadIds: ReadonlyArray<ThreadId>,
8867 ) {
@@ -8981,6 +9051,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
9051 listActiveSubagentThreadIds: readOnReader(listActiveSubagentThreadIds),
9052 countThreads: readOnReader(countThreads),
9053 listActiveShellIndex: readOnReader(listActiveShellIndex),
9054 + listActiveThreads: readOnReader(listActiveThreads),
9055 readShellSnapshot: readOnReader(readShellSnapshot),
9056 listOlderSettledThreads: readOnReader(listOlderSettledThreads),
9057 getThreadShell: readOnReader(getThreadShell),
apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
+1
@@ -218,6 +218,7 @@ it.effect(
218 listActiveSubagentThreadIds: () => Effect.die("unused listActiveSubagentThreadIds"),
219 countThreads: () => Effect.die("unused countThreads"),
220 listActiveShellIndex: () => Effect.die("unused listActiveShellIndex"),
221 + listActiveThreads: () => Effect.die("unused listActiveThreads"),
222 listOlderSettledThreads: () => Effect.die("unused listOlderSettledThreads"),
223 readShellSnapshot: () => Effect.die("unused readShellSnapshot"),
224 getThreadShell: () => Effect.die("unused getThreadShell"),
apps/server/src/orchestration-v2/ThreadManagementService.ts
+24 -29
@@ -3,6 +3,7 @@ import type {
3 OrchestrationV2SearchThreadResult,
4 } from "@t3tools/contracts";
5 import type {
6 + ActiveThreadListEntry,
7 ProjectionRecordField,
8 ProjectionRecordFilter,
9 ProjectionRecords,
@@ -30,7 +31,6 @@ import {
31 type OrchestrationV2ThreadShellSnapshot,
32 type OrchestrationV2ThreadProjection,
33 type OrchestrationV2ThreadTranscript,
33 - type OrchestrationV2ThreadShell,
34 type OrchestrationV2TurnItem,
35 ProjectId,
36 PROVIDER_SEND_TURN_MAX_FILE_BYTES,
@@ -451,7 +451,7 @@ export interface ThreadManagementServiceShape {
451 readonly listProjectThreads: (input: {
452 readonly projectId?: ProjectId | undefined;
453 readonly includeSubagents: boolean;
454 - }) => Effect.Effect<ReadonlyArray<OrchestrationV2ThreadShell>, ThreadManagementError>;
454 + }) => Effect.Effect<ReadonlyArray<ActiveThreadListEntry>, ThreadManagementError>;
455 readonly sendToThread: (
456 input: ThreadManagementSendInput,
457 ) => Effect.Effect<ThreadManagementSendResult, ThreadManagementFailure>;
@@ -835,33 +835,28 @@ const make = Effect.gen(function* () {
835 );
836
837 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 + );
860
861 const sendToThread: ThreadManagementServiceShape["sendToThread"] = Effect.fn(
862 "ThreadManagementService.sendToThread",
apps/server/src/peer/PeerForwarding.test.ts
+1
@@ -95,6 +95,7 @@ const boxThreads = (seen: Ref.Ref<Seen>, linkSession: Ref.Ref<string>) => {
95 getProjectThreadRecords: (input) => projectionOf(input.threadId),
96 getThreadRecords: (threadId) => projectionOf(threadId),
97 listProjectThreads: () => linked.pipe(Effect.map((shell) => [shell] as never)),
98 + getShellSnapshot: () => linked.pipe(Effect.map((shell) => ({ threads: [shell] }) as never)),
99 sendToThread: (input) =>
100 Ref.update(seen, (current) => ({ ...current, sends: [...current.sends, input] })).pipe(
101 Effect.as({ run: run("running"), delivery: "started" } as never),