The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 04:30 UTC
fix(server): a peer refusing one call no longer marks its link as failing (#29914)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Alex Southwell committed
Nov 3, 2026 at 23:38 UTC
b7fcb7c11122f216eabfc0ff61f80ac071cd2dfb
2 files changed
+93
-17
apps/server/src/peer/PeerMcpClient.test.ts
+71
-9
@@ -1,5 +1,6 @@
1
import { NodeHttpServer } from "@effect/platform-node";
2
import { expect, it } from "@effect/vitest";
3
+import { OrchestratorMcpFailure } from "@t3tools/contracts";
4
import * as Context from "effect/Context";
5
import * as DateTime from "effect/DateTime";
6
import * as Deferred from "effect/Deferred";
@@ -30,8 +31,19 @@ const toolkit = Toolkit.make(
31
success: Result,
32
dependencies: [McpInvocationContext.McpInvocationContext],
33
}),
34
+ Tool.make("refuse", {
35
+ description: "Refuses the call.",
36
+ success: Result,
37
+ failure: OrchestratorMcpFailure,
38
+ failureMode: "return",
39
+ }),
40
);
41
42
+const refusal = new OrchestratorMcpFailure({
43
+ code: "thread_not_found",
44
+ message: "Thread thread-gone was not found.",
45
+});
46
+
47
it.effect.each([false, true])(
48
"reuses an initialized session before its first call finishes (cancelled: %s)",
49
(cancelFirst) =>
@@ -47,6 +59,7 @@ it.effect.each([false, true])(
59
);
60
const handlers = McpToolAccess.toLayer(toolkit, {
61
probe: McpToolAccess.reads(() => modes),
62
+ refuse: McpToolAccess.reads(() => Effect.fail(refusal)),
63
hold: McpToolAccess.reads(() =>
64
Deferred.succeed(entered, undefined).pipe(
65
Effect.andThen(Deferred.await(release)),
@@ -91,10 +104,45 @@ it.effect.each([false, true])(
104
).pipe(Effect.provide(NodeHttpServer.layerTest)),
105
);
106
107
+it.effect("keeps a link healthy when the peer refuses a call", () =>
108
+ Effect.scoped(
109
+ Effect.gen(function* () {
110
+ const handlers = McpToolAccess.toLayer(toolkit, {
111
+ probe: McpToolAccess.reads(() => Effect.succeed({ modes: "" })),
112
+ hold: McpToolAccess.reads(() => Effect.succeed({ modes: "" })),
113
+ refuse: McpToolAccess.reads(() => Effect.fail(refusal)),
114
+ });
115
+ const box = descriptorOf("environment-box", "Box");
116
+ const b = yield* servePeer(box, McpHttpServer.toolkitRegistration(toolkit, handlers));
117
+ const context = yield* Layer.build(
118
+ layerLinkingEnvironment(descriptorOf("environment-a", "A")),
119
+ );
120
+ const links = Context.get(context, PeerLinks.PeerLinks);
121
+ const client = Context.get(context, PeerMcpClient.PeerMcpClient);
122
+ yield* linkTo(links, b);
123
+ yield* links.recordOutcome(box.environmentId, { error: "earlier outage" });
124
+ const refused = yield* client
125
+ .call({
126
+ environmentId: box.environmentId,
127
+ tool: "refuse",
128
+ arguments: {},
129
+ limits: { runtimeMode: "full-access", interactionMode: "default" },
130
+ success: Result,
131
+ })
132
+ .pipe(Effect.flip);
133
+ // The agent hears the peer's own refusal; the link answered, so it is not failing.
134
+ expect(refused).toMatchObject({ code: refusal.code, message: refusal.message });
135
+ const link = yield* links.get(box.environmentId);
136
+ expect(link._tag === "Some" ? link.value.lastError : "missing").toBeNull();
137
+ }),
138
+ ).pipe(Effect.provide(NodeHttpServer.layerTest)),
139
+);
140
+
141
it.effect("renews a missing peer session once and reuses its replacement", () =>
142
Effect.gen(function* () {
143
const sessionHeaders: Array<string | undefined> = [];
97
- const statuses = [200, 202, 404, 200, 202, 200, 200];
144
+ const statuses = [200, 202, 404, 200, 202, 200, 200, 200];
145
+ const outcomes: Array<string | null> = [];
146
const http = HttpClient.make((request) =>
147
Effect.sync(() => {
148
const index = sessionHeaders.length;
@@ -104,13 +152,15 @@ it.effect("renews a missing peer session once and reuses its replacement", () =>
152
return HttpClientResponse.fromWeb(
153
request,
154
new Response(
107
- index >= 5
108
- ? JSON.stringify({
109
- result: {
110
- content: [{ type: "text", text: '{"modes":"approval-required/plan"}' }],
111
- },
112
- })
113
- : "",
155
+ index === 7
156
+ ? "{}"
157
+ : index >= 5
158
+ ? JSON.stringify({
159
+ result: {
160
+ content: [{ type: "text", text: '{"modes":"approval-required/plan"}' }],
161
+ },
162
+ })
163
+ : "",
164
{
165
status,
166
headers:
@@ -146,7 +196,7 @@ it.effect("renews a missing peer session once and reuses its replacement", () =>
196
url: "https://peer.example",
197
token: "fixture",
198
}),
149
- recordOutcome: () => Effect.void,
199
+ recordOutcome: (_, outcome) => Effect.sync(() => outcomes.push(outcome.error)),
200
}),
201
),
202
),
@@ -164,6 +214,17 @@ it.effect("renews a missing peer session once and reuses its replacement", () =>
214
}),
215
).toEqual({ modes: "approval-required/plan" });
216
}
217
+ // An answer with neither a result nor an error is the link's problem, not the call's.
218
+ const empty = yield* client
219
+ .call({
220
+ environmentId: box.environmentId,
221
+ tool: "probe",
222
+ arguments: {},
223
+ limits: { runtimeMode: "approval-required", interactionMode: "plan" },
224
+ success: Result,
225
+ })
226
+ .pipe(Effect.flip);
227
+ expect(outcomes).toEqual([null, null, empty.message]);
228
expect(sessionHeaders).toEqual([
229
undefined,
230
"old",
@@ -172,6 +233,7 @@ it.effect("renews a missing peer session once and reuses its replacement", () =>
233
"renewed",
234
"renewed",
235
"renewed",
236
+ "renewed",
237
]);
238
}).pipe(Effect.scoped),
239
);
apps/server/src/peer/PeerMcpClient.ts
+22
-8
@@ -9,6 +9,7 @@ import * as Context from "effect/Context";
9
import * as Effect from "effect/Effect";
10
import * as Layer from "effect/Layer";
11
import * as Ref from "effect/Ref";
12
+import * as Result from "effect/Result";
13
import * as Schema from "effect/Schema";
14
import * as HttpBody from "effect/http/HttpBody";
15
import * as HttpClient from "effect/http/HttpClient";
@@ -210,25 +211,36 @@ const make = Effect.gen(function* () {
211
}),
212
),
213
);
213
- if (body.error !== undefined || body.result === undefined) {
214
+ if (body.error !== undefined) {
215
+ return Result.fail(
216
+ new OrchestratorMcpFailure({ code: "invalid_request", message: body.error.message }),
217
+ );
218
+ }
219
+ if (body.result === undefined) {
220
return yield* new OrchestratorMcpFailure({
221
code: "invalid_request",
216
- message: body.error?.message ?? "The linked environment returned no result.",
222
+ message: "The linked environment returned no result.",
223
});
224
}
225
const text = body.result.content.map((part) => part.text ?? "").join("");
226
if (body.result.isError === true) {
227
// A refusal keeps the peer's own code, so the agent sees why it was refused.
228
const refused = decodeFailure(text);
223
- return yield* refused._tag === "Some"
224
- ? new OrchestratorMcpFailure({ code: refused.value.code, message: refused.value.message })
225
- : new OrchestratorMcpFailure({
226
- code: "orchestration_error",
227
- message: text || "The linked environment refused the call.",
228
- });
229
+ return Result.fail(
230
+ refused._tag === "Some"
231
+ ? new OrchestratorMcpFailure({
232
+ code: refused.value.code,
233
+ message: refused.value.message,
234
+ })
235
+ : new OrchestratorMcpFailure({
236
+ code: "orchestration_error",
237
+ message: text || "The linked environment refused the call.",
238
+ }),
239
+ );
240
}
241
const success = Schema.fromJsonString(input.success);
242
return yield* Schema.decodeEffect(success)(text).pipe(
243
+ Effect.map(Result.succeed),
244
Effect.mapError(
245
() =>
246
new OrchestratorMcpFailure({
@@ -244,6 +256,7 @@ const make = Effect.gen(function* () {
256
Effect.tapError((error) =>
257
links.recordOutcome(input.environmentId, { error: error.message }),
258
),
259
+ // A refusal is still an answer: the link works, so only this call fails.
260
Effect.tap(() => links.recordOutcome(input.environmentId, { error: null })),
261
// Recorded above with the transport's own message; the agent hears it plainly.
262
Effect.catchTags({
@@ -255,6 +268,7 @@ const make = Effect.gen(function* () {
268
}),
269
),
270
}),
271
+ Effect.flatMap(Effect.fromResult),
272
),
273
);
274