Skip to content

Commit abf7267

Browse files
samsam
authored andcommitted
fix(server): keep peer thread reads on one projection version
1 parent 4ee2a76 commit abf7267

3 files changed

Lines changed: 91 additions & 23 deletions

File tree

‎apps/server/src/mcp/toolkits/threads/handlers.test.ts‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import * as DateTime from "effect/DateTime";
1313
import * as Effect from "effect/Effect";
1414
import * as Layer from "effect/Layer";
15+
import * as ProjectionSnapshotQuery from "../../../orchestration/Services/ProjectionSnapshotQuery.ts";
1516
import * as FileSystem from "effect/FileSystem";
1617
import { TestClock } from "effect/testing";
1718
import { McpSchema, McpServer } from "effect/unstable/ai";
@@ -53,7 +54,10 @@ const scope: McpInvocationContext.McpInvocationScope = {
5354
capabilities: new Set(["threads"]),
5455
issuedAt: 1,
5556
};
56-
function makeLayer(db: string) {
57+
function makeLayer(
58+
db: string,
59+
wrapQuery = (query: ProjectionSnapshotQuery.ProjectionSnapshotQueryShape) => query,
60+
) {
5761
const orchestration = Layer.mergeAll(
5862
OrchestrationEngineLive.pipe(
5963
Layer.provide(OrchestrationProjectionSnapshotQueryLive),
@@ -70,7 +74,12 @@ function makeLayer(db: string) {
7074
);
7175
return ThreadsToolkitRegistrationLive.pipe(
7276
Layer.provideMerge(McpServer.McpServer.layer),
73-
Layer.provideMerge(orchestration),
77+
Layer.provideMerge(
78+
Layer.effect(
79+
ProjectionSnapshotQuery.ProjectionSnapshotQuery,
80+
Effect.map(ProjectionSnapshotQuery.ProjectionSnapshotQuery, wrapQuery),
81+
).pipe(Layer.provideMerge(orchestration)),
82+
),
7483
Layer.provideMerge(ServerConfig.layerTest(process.cwd(), { prefix: "t3-peer-tools-" })),
7584
Layer.provideMerge(NodeServices.layer),
7685
);
@@ -123,6 +132,44 @@ const seed = Effect.gen(function* () {
123132
});
124133

125134
describe("peer thread MCP", () => {
135+
it.effect("retries a read when projection changes between shell and detail", () =>
136+
Effect.scoped(
137+
Effect.gen(function* () {
138+
const fs = yield* FileSystem.FileSystem;
139+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-peer-detail-" });
140+
let detailReads = 0;
141+
let rename = Effect.void;
142+
yield* Effect.gen(function* () {
143+
yield* seed;
144+
const engine = yield* OrchestrationEngineService;
145+
rename = engine
146+
.dispatch({
147+
type: "thread.meta.update",
148+
threadId: callerId,
149+
commandId: CommandId.make("rename-during-read"),
150+
title: "New title",
151+
})
152+
.pipe(Effect.asVoid, Effect.orDie);
153+
const read = result(yield* call("read_thread", { threadId: callerId }));
154+
expect(read.thread.title).toBe("New title");
155+
expect(read.thread.hasPendingApprovals).toBe(false);
156+
expect(detailReads).toBe(2);
157+
}).pipe(
158+
Effect.provide(
159+
makeLayer(`${directory}/state.sqlite`, (query) => ({
160+
...query,
161+
getThreadDetailSnapshot: (...args) =>
162+
Effect.gen(function* () {
163+
if (++detailReads === 1) yield* rename;
164+
return yield* query.getThreadDetailSnapshot(...args);
165+
}),
166+
})),
167+
),
168+
);
169+
}).pipe(Effect.provide(NodeServices.layer)),
170+
),
171+
);
172+
126173
it.effect("keeps retry IDs distinct across caller IDs containing delimiters", () =>
127174
Effect.scoped(
128175
Effect.gen(function* () {

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

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,45 @@ const make = Effect.gen(function* () {
156156
read_thread: (input) =>
157157
Effect.gen(function* () {
158158
const caller = yield* source();
159-
const thread = yield* target(input.threadId, caller);
160-
const detail = yield* snapshots
161-
.getThreadDetailSnapshot(thread.id, {
162-
turnLimit: input.turnLimit ?? 10,
163-
...(input.beforeCursor === undefined ? {} : { beforeCursor: input.beforeCursor }),
164-
})
165-
.pipe(Effect.mapError(failed));
166-
if (Option.isNone(detail)) return yield* failed();
167-
return {
168-
thread: yield* summarize(thread),
169-
snapshotSequence: detail.value.snapshotSequence,
170-
...(detail.value.page === undefined ? {} : { page: detail.value.page }),
171-
messages: detail.value.thread.messages.map((message) => ({
172-
id: message.id,
173-
role: message.role,
174-
text: message.text.slice(0, 8000),
175-
truncated: message.text.length > 8000,
176-
createdAt: message.createdAt,
177-
})),
178-
};
159+
for (let attempt = 0; attempt < 3; attempt++) {
160+
const before = yield* snapshots.getSnapshotSequence().pipe(Effect.mapError(failed));
161+
const thread = yield* target(input.threadId, caller);
162+
const detail = yield* snapshots
163+
.getThreadDetailSnapshot(thread.id, {
164+
turnLimit: input.turnLimit ?? 10,
165+
...(input.beforeCursor === undefined ? {} : { beforeCursor: input.beforeCursor }),
166+
})
167+
.pipe(Effect.mapError(failed));
168+
if (Option.isNone(detail)) return yield* failed();
169+
// Detail pages omit shell-only attention/liveness fields. A matching sequence
170+
// keeps those fields and the paginated history on the same projection version.
171+
if (before.snapshotSequence !== detail.value.snapshotSequence) continue;
172+
return {
173+
thread: summary(
174+
thread,
175+
detail.value.thread.activities
176+
.filter(
177+
(activity) =>
178+
activity.kind === "approval.requested" ||
179+
activity.kind === "user-input.requested",
180+
)
181+
.map((activity) => activity.id)
182+
.sort(),
183+
),
184+
snapshotSequence: detail.value.snapshotSequence,
185+
...(detail.value.page === undefined ? {} : { page: detail.value.page }),
186+
messages: detail.value.thread.messages.map((message) => ({
187+
id: message.id,
188+
role: message.role,
189+
text: message.text.slice(0, 8000),
190+
truncated: message.text.length > 8000,
191+
createdAt: message.createdAt,
192+
})),
193+
};
194+
}
195+
return yield* failed();
179196
}),
197+
180198
send_message_to_thread: (input) =>
181199
Effect.gen(function* () {
182200
const caller = yield* source();

‎apps/server/src/mcp/toolkits/threads/tools.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ const makeTool = <const N extends string, P extends Schema.Top, S extends Schema
6767
parameters: P,
6868
success: S,
6969
readonly: boolean,
70+
destructive = false,
7071
) =>
7172
Tool.make(name, { description, parameters, success, failure, dependencies })
7273
.annotate(Tool.Readonly, readonly)
73-
.annotate(Tool.Destructive, false)
74+
.annotate(Tool.Destructive, destructive)
7475
.annotate(Tool.Idempotent, readonly)
7576
.annotate(Tool.OpenWorld, false);
7677

@@ -158,12 +159,14 @@ export const ThreadsToolkit = Toolkit.make(
158159
Schema.Struct({ ...mutation, ...target, settled: Schema.Boolean }),
159160
receipt,
160161
false,
162+
true,
161163
),
162164
makeTool(
163165
"interrupt_thread",
164166
"Request interruption of a peer thread's current turn in this project. History remains available for a follow-up.",
165167
Schema.Struct({ ...mutation, ...target }),
166168
receipt,
167169
false,
170+
true,
168171
),
169172
);

0 commit comments

Comments
 (0)