Skip to content

Commit 82170e8

Browse files
samsam
authored andcommitted
fix(server): read peer summary inside detail transaction
1 parent abf7267 commit 82170e8

4 files changed

Lines changed: 52 additions & 38 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ const seed = Effect.gen(function* () {
132132
});
133133

134134
describe("peer thread MCP", () => {
135-
it.effect("retries a read when projection changes between shell and detail", () =>
135+
it.effect("reads shell and detail together after a concurrent projection change", () =>
136136
Effect.scoped(
137137
Effect.gen(function* () {
138138
const fs = yield* FileSystem.FileSystem;
@@ -153,7 +153,7 @@ describe("peer thread MCP", () => {
153153
const read = result(yield* call("read_thread", { threadId: callerId }));
154154
expect(read.thread.title).toBe("New title");
155155
expect(read.thread.hasPendingApprovals).toBe(false);
156-
expect(detailReads).toBe(2);
156+
expect(detailReads).toBe(1);
157157
}).pipe(
158158
Effect.provide(
159159
makeLayer(`${directory}/state.sqlite`, (query) => ({

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

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -156,43 +156,43 @@ const make = Effect.gen(function* () {
156156
read_thread: (input) =>
157157
Effect.gen(function* () {
158158
const caller = yield* source();
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, {
159+
const detail = yield* snapshots
160+
.getThreadDetailSnapshot(
161+
input.threadId,
162+
{
164163
turnLimit: input.turnLimit ?? 10,
165164
...(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-
};
165+
},
166+
true,
167+
)
168+
.pipe(Effect.mapError(failed));
169+
if (Option.isNone(detail) || detail.value.shell === undefined) return yield* failed();
170+
const thread = detail.value.shell;
171+
if (thread.projectId !== caller.projectId || thread.archivedAt !== null) {
172+
return yield* new ThreadToolError({ message: "Thread is unavailable in this project." });
194173
}
195-
return yield* failed();
174+
return {
175+
thread: summary(
176+
thread,
177+
detail.value.thread.activities
178+
.filter(
179+
(activity) =>
180+
activity.kind === "approval.requested" ||
181+
activity.kind === "user-input.requested",
182+
)
183+
.map((activity) => activity.id)
184+
.sort(),
185+
),
186+
snapshotSequence: detail.value.snapshotSequence,
187+
...(detail.value.page === undefined ? {} : { page: detail.value.page }),
188+
messages: detail.value.thread.messages.map((message) => ({
189+
id: message.id,
190+
role: message.role,
191+
text: message.text.slice(0, 8000),
192+
truncated: message.text.length > 8000,
193+
createdAt: message.createdAt,
194+
})),
195+
};
196196
}),
197197

198198
send_message_to_thread: (input) =>

‎apps/server/src/orchestration/Layers/ProjectionSnapshotQuery.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3496,6 +3496,7 @@ pending_approval_requests AS (
34963496
const getThreadDetailSnapshot: ProjectionSnapshotQueryShape["getThreadDetailSnapshot"] = (
34973497
threadId,
34983498
window,
3499+
includeShell = false,
34993500
) =>
35003501
// Read the thread detail and the snapshot sequence within a single
35013502
// transaction so the sequence is consistent with the returned state; a
@@ -3506,6 +3507,9 @@ pending_approval_requests AS (
35063507
sql
35073508
.withTransaction(
35083509
Effect.gen(function* () {
3510+
const shell = includeShell ? yield* getThreadShellById(threadId) : Option.none();
3511+
if (includeShell && Option.isNone(shell)) return Option.none();
3512+
const shellFields = Option.isSome(shell) ? { shell: shell.value } : {};
35093513
if (window?.turnLimit === undefined) {
35103514
const thread = yield* getThreadDetailByIdBounded(threadId, undefined, {
35113515
mode: "client",
@@ -3514,7 +3518,7 @@ pending_approval_requests AS (
35143518
return Option.none<OrchestrationThreadDetailSnapshot>();
35153519
}
35163520
const { snapshotSequence } = yield* getSnapshotSequence();
3517-
return Option.some({ snapshotSequence, thread: thread.value });
3521+
return Option.some({ snapshotSequence, thread: thread.value, ...shellFields });
35183522
}
35193523

35203524
// A malformed or foreign-thread cursor falls back to the first page
@@ -3606,6 +3610,7 @@ pending_approval_requests AS (
36063610
return Option.some({
36073611
snapshotSequence,
36083612
thread: thread.value,
3613+
...shellFields,
36093614
page: {
36103615
beforeCursor:
36113616
hasMore && oldest !== undefined

‎apps/server/src/orchestration/Services/ProjectionSnapshotQuery.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,23 @@ export interface ProjectionSnapshotQueryShape {
248248
* Without a window the full thread is returned with no `page` field —
249249
* pagination is strictly opt-in.
250250
*
251+
* `includeShell` reads shell-only attention and liveness fields in the same
252+
* transaction for callers that combine history with a thread summary.
253+
*
251254
* Activity payloads are projected for clients as they are read in small
252255
* sequential batches. Callers still apply the full snapshot projector for
253256
* collection-level activity pruning.
254257
*/
255258
readonly getThreadDetailSnapshot: (
256259
threadId: ThreadId,
257260
window?: OrchestrationThreadDetailWindow,
258-
) => Effect.Effect<Option.Option<OrchestrationThreadDetailSnapshot>, ProjectionRepositoryError>;
261+
includeShell?: boolean,
262+
) => Effect.Effect<
263+
Option.Option<
264+
OrchestrationThreadDetailSnapshot & { readonly shell?: OrchestrationThreadShell }
265+
>,
266+
ProjectionRepositoryError
267+
>;
259268
}
260269

261270
/**

0 commit comments

Comments
 (0)