Skip to content

Commit bba0b92

Browse files
committed
fix(server): return definite remote launch refusals
Retry an initial launch only when the peer call has an uncertain outcome. Preserve uncertainty for a replay of an earlier send, and complete definite refusals with their original error. Cover receiver refusals, HTTP and protocol errors, and an accepted launch with a lost receipt.
1 parent f491a64 commit bba0b92

4 files changed

Lines changed: 150 additions & 17 deletions

File tree

‎apps/server/src/peer/PeerMcpClient.test.ts‎

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as Fiber from "effect/Fiber";
99
import * as Layer from "effect/Layer";
1010
import * as Ref from "effect/Ref";
1111
import * as Schema from "effect/Schema";
12+
import * as HttpClientError from "effect/http/HttpClientError";
1213
import { Tool, Toolkit } from "effect/ai";
1314
import { HttpClient, HttpClientResponse } from "effect/http";
1415

@@ -138,22 +139,58 @@ it.effect("keeps a link healthy when the peer refuses a call", () =>
138139
).pipe(Effect.provide(NodeHttpServer.layerTest)),
139140
);
140141

141-
it.effect("renews a missing peer session once and reuses its replacement", () =>
142+
it.effect.each([
143+
{ name: "empty result", status: 200, body: "{}", uncertain: false },
144+
{ name: "HTTP refusal", status: 403, body: "", uncertain: false },
145+
{ name: "HTTP failure", status: 503, body: "", uncertain: false },
146+
{
147+
name: "tool refusal",
148+
status: 200,
149+
body: JSON.stringify({
150+
result: {
151+
isError: true,
152+
content: [
153+
{
154+
type: "text",
155+
text: JSON.stringify({
156+
code: "orchestration_error",
157+
message: "The operation could not be completed.",
158+
}),
159+
},
160+
],
161+
},
162+
}),
163+
uncertain: false,
164+
},
165+
{
166+
name: "version mismatch",
167+
status: 200,
168+
body: JSON.stringify({ result: { content: [{ type: "text", text: "{}" }] } }),
169+
uncertain: false,
170+
},
171+
{ name: "unreadable answer", status: 200, body: "not JSON", uncertain: true },
172+
{ name: "lost response", status: 200, body: "", uncertain: true },
173+
])("renews a missing peer session and distinguishes $name from an uncertain call", (answer) =>
142174
Effect.gen(function* () {
143175
const sessionHeaders: Array<string | undefined> = [];
144-
const statuses = [200, 202, 404, 200, 202, 200, 200, 200];
176+
const statuses = [200, 202, 404, 200, 202, 200, 200, answer.status];
145177
const outcomes: Array<string | null> = [];
146178
const http = HttpClient.make((request) =>
147-
Effect.sync(() => {
179+
Effect.gen(function* () {
148180
const index = sessionHeaders.length;
149181
sessionHeaders.push(request.headers["mcp-session-id"]);
182+
if (index === 7 && answer.name === "lost response") {
183+
return yield* new HttpClientError.HttpClientError({
184+
reason: new HttpClientError.TransportError({ request }),
185+
});
186+
}
150187
const status = statuses[index];
151188
if (status === undefined) throw new Error("Unexpected retry");
152189
return HttpClientResponse.fromWeb(
153190
request,
154191
new Response(
155192
index === 7
156-
? "{}"
193+
? answer.body
157194
: index >= 5
158195
? JSON.stringify({
159196
result: {
@@ -225,7 +262,16 @@ it.effect("renews a missing peer session once and reuses its replacement", () =>
225262
success: Result,
226263
})
227264
.pipe(Effect.flip);
228-
expect(outcomes).toEqual([null, null, empty.message]);
265+
expect(PeerMcpClient.isUncertainCall(empty)).toBe(answer.uncertain);
266+
expect(outcomes).toEqual([
267+
null,
268+
null,
269+
answer.name === "tool refusal"
270+
? null
271+
: answer.name === "lost response"
272+
? "Transport error (POST https://peer.example/mcp)"
273+
: empty.message,
274+
]);
229275
expect(sessionHeaders).toEqual([
230276
undefined,
231277
"old",

‎apps/server/src/peer/PeerMcpClient.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ import * as HttpClientRequest from "effect/http/HttpClientRequest";
1818
import * as McpInvocationContext from "../mcp/McpInvocationContext.ts";
1919
import * as PeerLinks from "./PeerLinks.ts";
2020

21+
const uncertainCall = Symbol("PeerMcpClient.UncertainCall");
22+
23+
/** No usable answer arrived, so a keyed launch may already have been accepted. */
24+
export class UncertainCall extends OrchestratorMcpFailure {
25+
readonly [uncertainCall] = true;
26+
}
27+
28+
export const isUncertainCall = (error: OrchestratorMcpFailure) => uncertainCall in error;
29+
2130
/** The only MCP protocol version a T3 Code `/mcp` registers. */
2231
const PROTOCOL_VERSION = "2025-06-18";
2332

@@ -69,7 +78,7 @@ const decodeFailure = Schema.decodeUnknownOption(
6978

7079
/** A peer link problem, as the agent that asked hears it. */
7180
const fromLinkError = (error: PeerLinkError) =>
72-
new OrchestratorMcpFailure({
81+
new (error.reason === "unreachable" ? UncertainCall : OrchestratorMcpFailure)({
7382
code:
7483
error.reason === "unknown_link"
7584
? "invalid_request"
@@ -217,7 +226,7 @@ const make = Effect.gen(function* () {
217226
).pipe(
218227
Effect.mapError(
219228
() =>
220-
new OrchestratorMcpFailure({
229+
new UncertainCall({
221230
code: "orchestration_error",
222231
message: "The linked environment answered in an unexpected shape.",
223232
}),
@@ -274,7 +283,7 @@ const make = Effect.gen(function* () {
274283
Effect.catchTags({
275284
HttpClientError: () =>
276285
Effect.fail(
277-
new OrchestratorMcpFailure({
286+
new UncertainCall({
278287
code: "orchestration_error",
279288
message: "The linked environment stopped answering.",
280289
}),

‎apps/server/src/peer/RemoteDelegation.test.ts‎

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import * as CommandReceiptStore from "../orchestration-v2/CommandReceiptStore.ts
6161
import * as SourceControlRepositoryService from "../sourceControl/SourceControlRepositoryService.ts";
6262
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
6363
import * as PeerForwarding from "./PeerForwarding.ts";
64+
import * as PeerMcpClient from "./PeerMcpClient.ts";
6465
import * as PeerLinks from "./PeerLinks.ts";
6566
import { descriptorOf, layerLinkingEnvironment, linkTo, servePeer } from "./PeerLinks.testkit.ts";
6667
import * as RemoteDelegation from "./RemoteDelegation.ts";
@@ -121,6 +122,7 @@ interface BoxThread {
121122
readonly stops: Ref.Ref<ReadonlyArray<"interrupt" | "thread.stop" | "delegated-tasks">>;
122123
/** The launch starts no run, as one recorded before its run was kept. */
123124
readonly runless: Ref.Ref<boolean>;
125+
readonly launchError: Ref.Ref<ThreadLaunch.ThreadLaunchError | null>;
124126
/** Completed on the first interrupt there. */
125127
readonly interruptedOnce: Deferred.Deferred<void>;
126128
readonly finished: Deferred.Deferred<void>;
@@ -164,6 +166,7 @@ const makeBoxThread = Effect.gen(function* () {
164166
interrupted: yield* Ref.make(0),
165167
stops: yield* Ref.make<ReadonlyArray<"interrupt" | "thread.stop" | "delegated-tasks">>([]),
166168
runless: yield* Ref.make(false),
169+
launchError: yield* Ref.make<ThreadLaunch.ThreadLaunchError | null>(null),
167170
interruptedOnce: yield* Deferred.make<void>(),
168171
finished: yield* Deferred.make<void>(),
169172
holdRead: yield* Ref.make(false),
@@ -440,7 +443,9 @@ const boxToolkitLayer = (thread: BoxThread) => {
440443
});
441444
const layerBoxLaunches = Layer.mock(ThreadLaunch.ThreadLaunchService)({
442445
launch: (input) =>
443-
Ref.get(thread.runless).pipe(
446+
Ref.get(thread.launchError).pipe(
447+
Effect.flatMap((error) => (error === null ? Effect.void : Effect.fail(error))),
448+
Effect.andThen(Ref.get(thread.runless)),
444449
Effect.flatMap((runless) =>
445450
Ref.set(thread.launched, input.threadId!).pipe(
446451
Effect.andThen(Ref.set(thread.linkOrigin, input.linkOrigin ?? null)),
@@ -821,10 +826,19 @@ const taskResult = (laptopEnv: Effect.Success<ReturnType<typeof makeLaptop>>, ta
821826
),
822827
);
823828

824-
it.effect("returns the recorded task when an accepted launch loses its response", () =>
829+
it.effect("returns a definite launch refusal instead of an indefinitely launching task", () =>
825830
Effect.scoped(
826831
Effect.gen(function* () {
827832
const thread = yield* makeBoxThread;
833+
yield* Ref.set(
834+
thread.launchError,
835+
new ThreadLaunch.ThreadLaunchError({
836+
operation: "create-thread",
837+
commandId: CommandId.make("command:refused-launch"),
838+
projectId: boxProject,
839+
cause: new Error("The destination cannot create the thread."),
840+
}),
841+
);
828842
const b = yield* serveBox(thread);
829843
const offers = yield* SubscriptionRef.make<
830844
ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>
@@ -833,12 +847,67 @@ it.effect("returns the recorded task when an accepted launch loses its response"
833847
const a = yield* makeLaptop(database, offers);
834848
yield* linkTo(a.links, b, "full-access");
835849
yield* seedParent(a);
836-
yield* Ref.set(
837-
thread.afterLaunch,
838-
Ref.set(thread.afterLaunch, Effect.void).pipe(
839-
Effect.andThen(Effect.die("The accepted launch response was lost.")),
850+
const refused = yield* a.call("delegate_task", {
851+
task: "Run the checks there.",
852+
target: {
853+
environmentId: box.environmentId,
854+
projectId: boxProject,
855+
providerInstanceId: instanceId,
856+
model: "gpt-5.4",
857+
},
858+
mode: "async",
859+
clientRequestId: "refused-launch",
860+
});
861+
expect(refused.isError).toBe(true);
862+
expect(JSON.stringify(refused.content)).toContain("The operation could not be completed.");
863+
const task = (yield* a.orchestrator.getThreadProjection(parentThreadId)).subagents[0]!;
864+
expect(task.status).toBe("failed");
865+
expect(task.result).toBe("The operation could not be completed.");
866+
expect(task.completionDelivery?.state).toBe("disposed");
867+
expect(task.remoteChild?.stoppedThere).toBe(true);
868+
expect(task.remoteChild?.launchError).toBeUndefined();
869+
expect(yield* Ref.get(thread.launchIds)).toEqual([]);
870+
}),
871+
).pipe(Effect.provide(NodeHttpServer.layerTest)),
872+
);
873+
874+
it.effect("returns the recorded task when an accepted launch loses its response", () =>
875+
Effect.scoped(
876+
Effect.gen(function* () {
877+
const thread = yield* makeBoxThread;
878+
const b = yield* serveBox(thread);
879+
const offers = yield* SubscriptionRef.make<
880+
ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>
881+
>([]);
882+
const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build);
883+
const linking = yield* layerLinkingEnvironment(laptop).pipe(Layer.build);
884+
const client = Context.get(linking, PeerMcpClient.PeerMcpClient);
885+
let lost = false;
886+
const a = yield* makeLaptop(
887+
database,
888+
offers,
889+
Context.add(
890+
linking,
891+
PeerMcpClient.PeerMcpClient,
892+
PeerMcpClient.PeerMcpClient.of({
893+
call: (input) =>
894+
client.call(input).pipe(
895+
Effect.flatMap((result) => {
896+
if (input.tool !== "t3_thread_launch" || lost) return Effect.succeed(result);
897+
lost = true;
898+
return Effect.fail(
899+
new PeerMcpClient.UncertainCall({
900+
code: "orchestration_error",
901+
message: "The linked environment stopped answering.",
902+
}),
903+
);
904+
}),
905+
),
906+
}),
840907
),
841908
);
909+
yield* linkTo(a.links, b, "full-access");
910+
yield* seedParent(a);
842911
const delegated = yield* delegateToBox(a);
843912
expect(delegated.taskId).toBeDefined();
844913
const recorded = yield* a.orchestrator.getThreadProjection(parentThreadId);

‎apps/server/src/peer/RemoteDelegation.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolv
4242
import { forkParked } from "../serverActivation.ts";
4343
import * as PeerForwarding from "./PeerForwarding.ts";
4444
import * as PeerLinks from "./PeerLinks.ts";
45+
import * as PeerMcpClient from "./PeerMcpClient.ts";
4546

4647
/** How long one follow of a remote task waits on its thread there before checking again. */
4748
const FOLLOW_WAIT_MS = 10 * 60 * 1_000;
@@ -355,7 +356,7 @@ const make = Effect.gen(function* () {
355356
}
356357
yield* launchPending(scope.thread.threadId, taskId).pipe(
357358
Effect.catchIf(
358-
(error) => !isFinal(error) && !isModeRefusal(error) && error.code !== "invalid_request",
359+
(error) => PeerMcpClient.isUncertainCall(error),
359360
(error) =>
360361
Effect.logWarning("remote-delegation.launch-deferred", {
361362
taskId,
@@ -506,7 +507,12 @@ const make = Effect.gen(function* () {
506507
.pipe(
507508
Effect.catchTags({
508509
OrchestratorMcpFailure: (error) => {
509-
if (isFinal(error) || isModeRefusal(error) || error.code === "invalid_request") {
510+
if (
511+
isFinal(error) ||
512+
isModeRefusal(error) ||
513+
error.code === "invalid_request" ||
514+
(!sentBefore && !PeerMcpClient.isUncertainCall(error))
515+
) {
510516
const uncertain = `${error.message} The remote launch could not be confirmed. If it was already accepted, work may still be running on ${remote.label}.`;
511517
return complete(
512518
parentThreadId,
@@ -518,7 +524,10 @@ const make = Effect.gen(function* () {
518524
sentBefore ? uncertain : undefined,
519525
).pipe(Effect.andThen(Effect.fail(error)));
520526
}
521-
return Effect.fail(error);
527+
// A prior send may have launched work; a later refusal cannot disprove acceptance.
528+
return Effect.fail(
529+
new PeerMcpClient.UncertainCall({ code: error.code, message: error.message }),
530+
);
522531
},
523532
}),
524533
);

0 commit comments

Comments
 (0)