Skip to content

Commit 869de80

Browse files
fix(server): t3_queue and handoff analytics look messages up by id
The messageRunIds and multi-message messageIds reads still walked the thread's message history (a thread-index scan, then the ordinal query over every user-message turn item). Both now fetch the run's own user messages through the keyed ProjectionStore.getMessagesById lookup. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7eaf451 commit 869de80

6 files changed

Lines changed: 37 additions & 20 deletions

File tree

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,22 +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-
messageRunIds: [runId],
1012-
messageRoles: ["user"],
1013-
})
1014-
.pipe(
1015-
Effect.map((records) => {
1016-
const run = records.runs.find((candidate) => candidate.id === runId);
1017-
return records.messages.some(
1018-
(message) =>
1019-
message.id === run?.userMessageId && message.scheduledTaskId !== undefined,
1020-
);
1021-
}),
1022-
Effect.orElseSucceed(() => false),
1023-
);
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));
10241018
const record =
10251019
/* Request instrumentation already owns the span; keep this helper in that context. */
10261020
Effect.fnUntraced(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,7 @@ const historyReads = (reads: Array<string>) => {
496496
Effect.sync(() => record(fields, filter)).pipe(Effect.as(projection as never)),
497497
getThreadRecords: (_id, fields, filter) =>
498498
Effect.sync(() => record(fields, filter)).pipe(Effect.as(projection as never)),
499+
getMessagesById: () => Effect.succeed(new Map()),
499500
dispatch: () => Effect.succeed({ sequence: 1 } as never),
500501
});
501502
};

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,13 @@ export const layer = McpToolAccess.toLayer(ThreadToolkit, {
272272
const { threads, projection: queued } = yield* readThread(input.threadId, ["runs"], {
273273
runStatuses: ["queued"],
274274
});
275-
const { messages } = yield* threads
276-
.getThreadRecords(queued.thread.id, ["messages"], {
277-
messageIds: queued.runs.map((run) => run.userMessageId),
278-
})
275+
const messages = yield* threads
276+
.getMessagesById(
277+
queued.thread.id,
278+
queued.runs.map((run) => run.userMessageId),
279+
)
279280
.pipe(Effect.mapError(unavailable));
280-
const projection = { runs: queued.runs, messages };
281+
const projection = { runs: queued.runs, messages: [...messages.values()] };
281282
if (input.action === "list") {
282283
const runs = queuedRunsInDeliveryOrder(projection);
283284
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)