@devries-cameron20 / t3code-1 / commits / 4aec277f79
Fork 0

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

fix(server): t3_queue and handoff analytics no longer read a thread's whole history (#36130)

Julius Marminge committed Nov 13, 2026 at 03:21 UTC 4aec277f79b2d4dabd88f13cfab264d9f80cbcef
7 files changed +142 -17
apps/server/src/mcp/McpHttpServer.ts
+9 -14
@@ -1005,21 +1005,16 @@ const withToolAnalytics =
1005 const scheduledRunOf = (threadId: string, runId: RunId | null) =>
1006 Option.isNone(threads) || runId === null
1007 ? Effect.succeed(false)
1008 - : threads.value
1009 - .getThreadRecords(ThreadId.make(threadId), ["runs", "messages"], {
1008 + : Effect.gen(function* () {
1009 + const id = ThreadId.make(threadId);
1010 + const { runs } = yield* threads.value.getThreadRecords(id, ["runs"], {
1011 runIds: [runId],
1011 - messageRoles: ["user"],
1012 - })
1013 - .pipe(
1014 - Effect.map((records) => {
1015 - const run = records.runs.find((candidate) => candidate.id === runId);
1016 - return records.messages.some(
1017 - (message) =>
1018 - message.id === run?.userMessageId && message.scheduledTaskId !== undefined,
1019 - );
1020 - }),
1021 - Effect.orElseSucceed(() => false),
1022 - );
1012 + });
1013 + const run = runs.find((candidate) => candidate.id === runId);
1014 + if (run === undefined) return false;
1015 + const messages = yield* threads.value.getMessagesById(id, [run.userMessageId]);
1016 + return messages.get(run.userMessageId)?.scheduledTaskId !== undefined;
1017 + }).pipe(Effect.orElseSucceed(() => false));
1018 const record =
1019 /* Request instrumentation already owns the span; keep this helper in that context. */
1020 Effect.fnUntraced(
apps/server/src/mcp/threadAccess.ts
+6 -2
@@ -1,4 +1,7 @@
1 -import type { ProjectionRecordField } from "../orchestration-v2/ProjectionStore.ts";
1 +import type {
2 + ProjectionRecordField,
3 + ProjectionRecordFilter,
4 +} from "../orchestration-v2/ProjectionStore.ts";
5 import {
6 CommandId,
7 OrchestratorMcpFailure,
@@ -182,7 +185,7 @@ const resolveThreadId = (context: Caller, threadId: ThreadId | undefined) =>
185
186 export const readThread = Effect.fn("mcp.readThread")(function* <
187 K extends ProjectionRecordField = never,
185 ->(threadId?: ThreadId, fields: ReadonlyArray<K> = []) {
188 +>(threadId?: ThreadId, fields: ReadonlyArray<K> = [], filter?: ProjectionRecordFilter) {
189 const context = yield* readCaller();
190 const targetId = yield* resolveThreadId(context, threadId);
191 const shell = yield* context.threads.getThreadShell(targetId).pipe(Effect.mapError(unavailable));
@@ -192,6 +195,7 @@ export const readThread = Effect.fn("mcp.readThread")(function* <
195 const projection = yield* context.threads
196 .getProjectThreadRecords({ projectId: shell.projectId, threadId: targetId }, fields, {
197 turnItemTypes: ["user_input_request"],
198 + ...filter,
199 })
200 .pipe(
201 Effect.mapError((error) =>
apps/server/src/mcp/toolkits/core.test.ts
+96
@@ -45,6 +45,7 @@ import * as ProviderAdapterRegistry from "../../orchestration-v2/ProviderAdapter
45 import * as ThreadLaunchService from "../../orchestration-v2/ThreadLaunchService.ts";
46 import * as GitVcsDriver from "../../vcs/GitVcsDriver.ts";
47 import * as ThreadManagement from "../../orchestration-v2/ThreadManagementService.ts";
48 +import type * as ProjectionStore from "../../orchestration-v2/ProjectionStore.ts";
49 import * as ThreadSearch from "../../orchestration-v2/ThreadSearch.ts";
50 import * as PreviewBrowser from "../../preview/PreviewBrowser.ts";
51 import * as ProjectService from "../../project/ProjectService.ts";
@@ -467,6 +468,101 @@ it.effect("records only failed calls of tools that hand off no work", () => {
468 );
469 });
470
471 +// A thread read whose runs or messages are bounded by nothing but the thread.
472 +const historyReads = (reads: Array<string>) => {
473 + const record = (
474 + fields: ReadonlyArray<string>,
475 + filter: ProjectionStore.ProjectionRecordFilter | undefined,
476 + ) => {
477 + if (
478 + fields.includes("runs") &&
479 + filter?.runIds === undefined &&
480 + filter?.runStatuses === undefined
481 + )
482 + reads.push("every run");
483 + if (
484 + fields.includes("messages") &&
485 + filter?.messageIds === undefined &&
486 + filter?.messageRunIds === undefined
487 + )
488 + reads.push("every message");
489 + };
490 + const projection = McpToolAccessTestkit.idleThreadProjection(
491 + McpToolAccessTestkit.liveThreadShell(threadId),
492 + );
493 + return Layer.mock(ThreadManagement.ThreadManagementService)({
494 + getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)),
495 + getProjectThreadRecords: (_target, fields, filter) =>
496 + Effect.sync(() => record(fields, filter)).pipe(Effect.as(projection as never)),
497 + getThreadRecords: (_id, fields, filter) =>
498 + Effect.sync(() => record(fields, filter)).pipe(Effect.as(projection as never)),
499 + getMessagesById: () => Effect.succeed(new Map()),
500 + dispatch: () => Effect.succeed({ sequence: 1 } as never),
501 + });
502 +};
503 +
504 +it.effect("t3_queue list and read load only the queued runs and their messages", () => {
505 + const reads: Array<string> = [];
506 + return Effect.gen(function* () {
507 + const server = yield* McpServer.McpServer;
508 + const call = (args: Record<string, unknown>) =>
509 + server
510 + .callTool({ name: "t3_queue", arguments: args })
511 + .pipe(
512 + Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
513 + Effect.provideService(McpSchema.McpServerClient, client),
514 + );
515 + yield* call({ action: "list" });
516 + yield* call({ action: "read", queuedRunId: "run:queued" });
517 + expect(reads).toEqual([]);
518 + }).pipe(
519 + Effect.provide(
520 + layerThreadToolkit.pipe(
521 + Layer.provideMerge(McpHttpServer.layerInProcessMcpServer),
522 + Layer.provide(NodeCrypto.layer),
523 + Layer.provide(historyReads(reads)),
524 + ),
525 + ),
526 + );
527 +});
528 +
529 +it.effect("handoff analytics read only the caller's active run and its message", () => {
530 + const reads: Array<string> = [];
531 + const recorded = Deferred.makeUnsafe<void>();
532 + return Effect.gen(function* () {
533 + const server = yield* McpServer.McpServer;
534 + yield* server
535 + .callTool({
536 + name: "t3_queue",
537 + arguments: { action: "edit", queuedRunId: "run:queued", text: "Edited" },
538 + })
539 + .pipe(
540 + Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
541 + Effect.provideService(McpSchema.McpServerClient, client),
542 + );
543 + yield* Deferred.await(recorded);
544 + expect(reads).toEqual([]);
545 + }).pipe(
546 + Effect.provide(
547 + layerThreadToolkit.pipe(
548 + Layer.provideMerge(McpHttpServer.layerInProcessMcpServer),
549 + Layer.provide(NodeCrypto.layer),
550 + Layer.provide(historyReads(reads)),
551 + Layer.provide(
552 + Layer.succeed(
553 + AnalyticsService.AnalyticsService,
554 + AnalyticsService.AnalyticsService.of({
555 + record: () => Deferred.succeed(recorded, undefined).pipe(Effect.asVoid),
556 + flush: Effect.void,
557 + recordOwners: () => Effect.void,
558 + }),
559 + ),
560 + ),
561 + ),
562 + ),
563 + );
564 +});
565 +
566 it.effect("rejects an unknown organize target instead of archiving the calling thread", () => {
567 const dispatched: Array<unknown> = [];
568 return Effect.gen(function* () {
apps/server/src/mcp/toolkits/thread/handlers.ts
+10 -1
@@ -269,7 +269,16 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {
269 (input) => input.action === "list" || input.action === "read",
270 McpToolAccess.reads((input) =>
271 Effect.gen(function* () {
272 - const { projection } = yield* readThread(input.threadId, ["runs", "messages"]);
272 + const { threads, projection: queued } = yield* readThread(input.threadId, ["runs"], {
273 + runStatuses: ["queued"],
274 + });
275 + const messages = yield* threads
276 + .getMessagesById(
277 + queued.thread.id,
278 + queued.runs.map((run) => run.userMessageId),
279 + )
280 + .pipe(Effect.mapError(unavailable));
281 + const projection = { runs: queued.runs, messages: [...messages.values()] };
282 if (input.action === "list") {
283 const runs = queuedRunsInDeliveryOrder(projection);
284 const cursor = input.cursor ?? 0;
apps/server/src/orchestration-v2/Orchestrator.ts
+12
@@ -430,6 +430,13 @@ export interface OrchestratorV2Shape {
430 options: ProjectionTimelinePageOptions,
431 ) => Effect.Effect<ProjectionTimelinePage, OrchestratorProjectionError>;
432 readonly getMessageCount: (threadId: ThreadId) => Effect.Effect<number, OrchestratorV2Error>;
433 + readonly getMessagesById: (
434 + threadId: ThreadId,
435 + messageIds: ReadonlyArray<MessageId>,
436 + ) => Effect.Effect<
437 + ReadonlyMap<MessageId, OrchestrationV2ConversationMessage>,
438 + OrchestratorV2Error
439 + >;
440 readonly getTranscriptByteFloor: (
441 threadId: ThreadId,
442 limit: number,
@@ -15765,6 +15772,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
15772 .getMessageCount(threadId)
15773 .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
15774 }),
15775 + getMessagesById: Effect.fn("OrchestratorV2.getMessagesById")(function (threadId, messageIds) {
15776 + return projectionStore
15777 + .getMessagesById(threadId, messageIds)
15778 + .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
15779 + }),
15780 getTranscriptByteFloor: Effect.fn("OrchestratorV2.getTranscriptByteFloor")(
15781 function (threadId, limit) {
15782 return projectionStore
apps/server/src/orchestration-v2/ThreadManagementService.ts
+8
@@ -384,6 +384,7 @@ export interface ThreadManagementServiceShape {
384 readonly getThreadHistoryPage: Orchestrator.OrchestratorV2["Service"]["getThreadHistoryPage"];
385 readonly getTimelinePage: Orchestrator.OrchestratorV2["Service"]["getTimelinePage"];
386 readonly getMessageCount: Orchestrator.OrchestratorV2["Service"]["getMessageCount"];
387 + readonly getMessagesById: Orchestrator.OrchestratorV2["Service"]["getMessagesById"];
388 readonly getTurnItem: (input: {
389 readonly threadId: ThreadId;
390 readonly itemId: TurnItemId;
@@ -1652,6 +1653,13 @@ const make = Effect.gen(function* () {
1653 Effect.andThen(orchestrator.getMessageCount(threadId)),
1654 );
1655 }),
1656 + getMessagesById: Effect.fn("ThreadManagementService.getMessagesById")(
1657 + function (threadId, messageIds) {
1658 + return ensureProjectionTranscript(threadId).pipe(
1659 + Effect.andThen(orchestrator.getMessagesById(threadId, messageIds)),
1660 + );
1661 + },
1662 + ),
1663 getTurnItem: Effect.fn("ThreadManagementService.getTurnItem")(function (input) {
1664 return ensureProjectionTranscript(input.threadId).pipe(
1665 Effect.andThen(orchestrator.getTurnItem(input)),
apps/server/src/relay/AgentAwarenessRelay.test.ts
+1
@@ -238,6 +238,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
238 getThreadHistoryPage: () => Effect.die("unused"),
239 getTimelinePage: () => Effect.die("Unused timeline read"),
240 getMessageCount: () => Effect.die("unused message count"),
241 + getMessagesById: () => Effect.die("unused messages by id"),
242 getTurnItem: () => Effect.die("unused turn item read"),
243 getThreadRecords: () => Effect.die("unused record read"),
244 getThreadProjection: unused,