Skip to content

Commit 4aec277

Browse files
fix(server): t3_queue and handoff analytics no longer read a thread's whole history (#36130)
1 parent 08be782 commit 4aec277

7 files changed

Lines changed: 142 additions & 17 deletions

File tree

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,21 +1005,16 @@ const withToolAnalytics =
10051005
const scheduledRunOf = (threadId: string, runId: RunId | null) =>
10061006
Option.isNone(threads) || runId === null
10071007
? 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"], {
10101011
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));
10231018
const record =
10241019
/* Request instrumentation already owns the span; keep this helper in that context. */
10251020
Effect.fnUntraced(

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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";
25
import {
36
CommandId,
47
OrchestratorMcpFailure,
@@ -182,7 +185,7 @@ const resolveThreadId = (context: Caller, threadId: ThreadId | undefined) =>
182185

183186
export const readThread = Effect.fn("mcp.readThread")(function* <
184187
K extends ProjectionRecordField = never,
185-
>(threadId?: ThreadId, fields: ReadonlyArray<K> = []) {
188+
>(threadId?: ThreadId, fields: ReadonlyArray<K> = [], filter?: ProjectionRecordFilter) {
186189
const context = yield* readCaller();
187190
const targetId = yield* resolveThreadId(context, threadId);
188191
const shell = yield* context.threads.getThreadShell(targetId).pipe(Effect.mapError(unavailable));
@@ -192,6 +195,7 @@ export const readThread = Effect.fn("mcp.readThread")(function* <
192195
const projection = yield* context.threads
193196
.getProjectThreadRecords({ projectId: shell.projectId, threadId: targetId }, fields, {
194197
turnItemTypes: ["user_input_request"],
198+
...filter,
195199
})
196200
.pipe(
197201
Effect.mapError((error) =>

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import * as ProviderAdapterRegistry from "../../orchestration-v2/ProviderAdapter
4545
import * as ThreadLaunchService from "../../orchestration-v2/ThreadLaunchService.ts";
4646
import * as GitVcsDriver from "../../vcs/GitVcsDriver.ts";
4747
import * as ThreadManagement from "../../orchestration-v2/ThreadManagementService.ts";
48+
import type * as ProjectionStore from "../../orchestration-v2/ProjectionStore.ts";
4849
import * as ThreadSearch from "../../orchestration-v2/ThreadSearch.ts";
4950
import * as PreviewBrowser from "../../preview/PreviewBrowser.ts";
5051
import * as ProjectService from "../../project/ProjectService.ts";
@@ -467,6 +468,101 @@ it.effect("records only failed calls of tools that hand off no work", () => {
467468
);
468469
});
469470

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+
470566
it.effect("rejects an unknown organize target instead of archiving the calling thread", () => {
471567
const dispatched: Array<unknown> = [];
472568
return Effect.gen(function* () {

‎apps/server/src/mcp/toolkits/thread/handlers.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,16 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {
269269
(input) => input.action === "list" || input.action === "read",
270270
McpToolAccess.reads((input) =>
271271
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()] };
273282
if (input.action === "list") {
274283
const runs = queuedRunsInDeliveryOrder(projection);
275284
const cursor = input.cursor ?? 0;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ export interface OrchestratorV2Shape {
430430
options: ProjectionTimelinePageOptions,
431431
) => Effect.Effect<ProjectionTimelinePage, OrchestratorProjectionError>;
432432
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+
>;
433440
readonly getTranscriptByteFloor: (
434441
threadId: ThreadId,
435442
limit: number,
@@ -15765,6 +15772,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1576515772
.getMessageCount(threadId)
1576615773
.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
1576715774
}),
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+
}),
1576815780
getTranscriptByteFloor: Effect.fn("OrchestratorV2.getTranscriptByteFloor")(
1576915781
function (threadId, limit) {
1577015782
return projectionStore

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ export interface ThreadManagementServiceShape {
384384
readonly getThreadHistoryPage: Orchestrator.OrchestratorV2["Service"]["getThreadHistoryPage"];
385385
readonly getTimelinePage: Orchestrator.OrchestratorV2["Service"]["getTimelinePage"];
386386
readonly getMessageCount: Orchestrator.OrchestratorV2["Service"]["getMessageCount"];
387+
readonly getMessagesById: Orchestrator.OrchestratorV2["Service"]["getMessagesById"];
387388
readonly getTurnItem: (input: {
388389
readonly threadId: ThreadId;
389390
readonly itemId: TurnItemId;
@@ -1652,6 +1653,13 @@ const make = Effect.gen(function* () {
16521653
Effect.andThen(orchestrator.getMessageCount(threadId)),
16531654
);
16541655
}),
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+
),
16551663
getTurnItem: Effect.fn("ThreadManagementService.getTurnItem")(function (input) {
16561664
return ensureProjectionTranscript(input.threadId).pipe(
16571665
Effect.andThen(orchestrator.getTurnItem(input)),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
238238
getThreadHistoryPage: () => Effect.die("unused"),
239239
getTimelinePage: () => Effect.die("Unused timeline read"),
240240
getMessageCount: () => Effect.die("unused message count"),
241+
getMessagesById: () => Effect.die("unused messages by id"),
241242
getTurnItem: () => Effect.die("unused turn item read"),
242243
getThreadRecords: () => Effect.die("unused record read"),
243244
getThreadProjection: unused,

0 commit comments

Comments
 (0)