Skip to content

Commit 84a4f73

Browse files
fix(server): handle peer response and cleanup failures
1 parent e06c97f commit 84a4f73

4 files changed

Lines changed: 170 additions & 27 deletions

File tree

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

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ const serveB = (initial: ExecutionEnvironmentDescriptor) =>
9999
const redirects = yield* Ref.make<ReadonlyMap<string, string>>(new Map());
100100
const redirectedBodies = yield* Ref.make<ReadonlyArray<string>>([]);
101101
const toolResult = yield* Ref.make<McpSchema.CallToolResult | null>(null);
102+
const sse = yield* Ref.make(false);
103+
const replies = yield* Ref.make<ReadonlyMap<string, { status: number; body: unknown }>>(
104+
new Map(),
105+
);
102106
const layerEnvironment = Layer.succeed(ServerEnvironment.ServerEnvironment, {
103107
getEnvironmentId: Ref.get(descriptor).pipe(Effect.map((current) => current.environmentId)),
104108
getDescriptor: Ref.get(descriptor),
@@ -136,12 +140,30 @@ const serveB = (initial: ExecutionEnvironmentDescriptor) =>
136140
if (authorization !== undefined) {
137141
yield* Ref.update(bearers, (seen) => [...seen, authorization]);
138142
}
143+
const reply = (yield* Ref.get(replies)).get(request.url);
144+
if (reply !== undefined)
145+
return HttpServerResponse.jsonUnsafe(reply.body, { status: reply.status });
139146
const response = yield* httpEffect;
140147
// Keep real authentication and dispatch, but exercise richer peer wire results.
141148
const result = yield* Ref.get(toolResult);
142149
if (result !== null && request.url === "/mcp" && response.status === 200) {
143150
const rpc = yield* decodeMcpRequest(yield* request.json);
144151
if (rpc.method === "tools/call") {
152+
if (yield* Ref.get(sse)) {
153+
return HttpServerResponse.text(
154+
[
155+
'data: {"jsonrpc":"2.0","method":"notifications/progress","params":{"result":"notification"}}',
156+
"",
157+
'data: {"jsonrpc":"2.0","id":999,"error":{"code":-1,"message":"other request"}}',
158+
"",
159+
'data: {"jsonrpc":"2.0",',
160+
'data: "id":' + rpc.id + ',"result":' + JSON.stringify(result) + "}",
161+
"",
162+
"",
163+
].join("\r\n"),
164+
{ contentType: "text/event-stream" },
165+
);
166+
}
145167
return HttpServerResponse.jsonUnsafe({ jsonrpc: "2.0", id: rpc.id, result });
146168
}
147169
}
@@ -204,6 +226,8 @@ const serveB = (initial: ExecutionEnvironmentDescriptor) =>
204226
alternateDescriptor,
205227
bearers,
206228
toolResult,
229+
sse,
230+
replies,
207231
redirects,
208232
redirectedBodies,
209233
/** How often its descriptor was asked for, as a probe of whether it answers. */
@@ -215,6 +239,7 @@ const serveB = (initial: ExecutionEnvironmentDescriptor) =>
215239
const makeA = (
216240
descriptor: ExecutionEnvironmentDescriptor,
217241
afterSecretWrite: Effect.Effect<void> = Effect.void,
242+
beforeSecretRemove: Effect.Effect<void, ServerSecretStore.SecretStoreError> = Effect.void,
218243
) =>
219244
Effect.gen(function* () {
220245
const secretReads = yield* Ref.make(0);
@@ -225,6 +250,7 @@ const makeA = (
225250
return ServerSecretStore.ServerSecretStore.of({
226251
...secrets,
227252
set: (name, value) => secrets.set(name, value).pipe(Effect.andThen(afterSecretWrite)),
253+
remove: (name) => beforeSecretRemove.pipe(Effect.andThen(secrets.remove(name))),
228254
get: (name) =>
229255
Ref.update(secretReads, (count) => count + 1).pipe(Effect.andThen(secrets.get(name))),
230256
});
@@ -289,7 +315,29 @@ it.effect("links, reads and forgets a peer while preserving access limits and re
289315
Effect.scoped(
290316
Effect.gen(function* () {
291317
const b = yield* serveB(box);
292-
const a = yield* makeA(laptop);
318+
const removal = yield* Ref.make<"fail" | "pause" | null>(null);
319+
const removalEntered = yield* Deferred.make<void>();
320+
const allowRemoval = yield* Deferred.make<void>();
321+
const a = yield* makeA(
322+
laptop,
323+
Effect.void,
324+
Ref.get(removal).pipe(
325+
Effect.flatMap((action) =>
326+
action === "fail"
327+
? Effect.fail(
328+
new ServerSecretStore.SecretStoreRemoveError({
329+
resource: "test credential",
330+
cause: new Error("removal failed"),
331+
}),
332+
)
333+
: action === "pause"
334+
? Deferred.succeed(removalEntered, undefined).pipe(
335+
Effect.andThen(Deferred.await(allowRemoval)),
336+
)
337+
: Effect.void,
338+
),
339+
),
340+
);
293341

294342
const refused = yield* a.links
295343
.link({ url: b.url, pairingCode: "not-a-code", access: "auto" })
@@ -332,6 +380,31 @@ it.effect("links, reads and forgets a peer while preserving access limits and re
332380
const notFound = yield* probe(a, undefined, "refuse").pipe(Effect.flip);
333381
expect(notFound).toMatchObject({ code: "thread_not_found", message: "No such thread." });
334382

383+
const names = yield* a.secretNames;
384+
const beforeUnlink = yield* a.links.get(box.environmentId);
385+
yield* Ref.set(removal, "fail");
386+
expect((yield* a.links.unlink(box.environmentId).pipe(Effect.flip)).reason).toBe("storage");
387+
expect(yield* a.links.get(box.environmentId)).toEqual(beforeUnlink);
388+
expect(yield* a.secretNames).toEqual(names);
389+
expect(yield* probe(a)).toEqual({ caller: "T3 Code · Laptop", modes: "auto/default" });
390+
391+
yield* Ref.set(removal, "pause");
392+
const unlinking = yield* a.links.unlink(box.environmentId).pipe(Effect.forkScoped);
393+
yield* Deferred.await(removalEntered);
394+
yield* Ref.set(removal, null);
395+
const newPairing = yield* b.auth.issuePairingCredential();
396+
const relinked = yield* a.links.link({
397+
url: b.url,
398+
pairingCode: newPairing.credential,
399+
access: "approval-required",
400+
});
401+
yield* Deferred.succeed(allowRemoval, undefined);
402+
expect(yield* Fiber.join(unlinking)).toBe(false);
403+
expect(yield* a.links.get(box.environmentId)).toEqual(Option.some(relinked));
404+
expect(yield* probe(a)).toEqual({
405+
caller: "T3 Code · Laptop",
406+
modes: "approval-required/default",
407+
});
335408
expect(yield* a.links.unlink(box.environmentId)).toBe(true);
336409
expect(yield* a.links.unlink(box.environmentId)).toBe(false);
337410
expect(yield* a.links.get(box.environmentId)).toEqual(Option.none());
@@ -340,7 +413,7 @@ it.effect("links, reads and forgets a peer while preserving access limits and re
340413
yield* Ref.set(b.bearers, []);
341414
expect((yield* probe(a).pipe(Effect.flip)).code).toBe("invalid_request");
342415
expect(yield* Ref.get(b.bearers)).toHaveLength(0);
343-
expect(yield* linkedSessions(b)).toHaveLength(1);
416+
expect(yield* linkedSessions(b)).toHaveLength(2);
344417
}),
345418
).pipe(Effect.provide(Layer.merge(NodeHttpServer.layerTest, NodeServices.layer))),
346419
);
@@ -506,6 +579,51 @@ it.effect(
506579
.pipe(Effect.flip);
507580
expect(lanAlternate.message).toContain("plain http");
508581

582+
for (const url of [
583+
"file://username:[email protected]/path?token=private",
584+
"http://username:[email protected]:3773/path?token=private",
585+
]) {
586+
const invalid = yield* a.links
587+
.link({ url, pairingCode: "unused", access: "auto" })
588+
.pipe(Effect.flip);
589+
expect(invalid.message).not.toMatch(/username|password|private|example\.test/);
590+
}
591+
yield* Ref.set(b.descriptor, box);
592+
const peerText = "PRIVATE-PEER-RESPONSE\n".repeat(200);
593+
for (const [path, body, message] of [
594+
[
595+
"/oauth/mcp/register",
596+
{ error: "invalid_client_metadata", error_description: peerText },
597+
"The peer refused to register this environment. Check its connection settings.",
598+
],
599+
[
600+
"/oauth/mcp/decision",
601+
{ _tag: "AuthMcpApprovalError", message: peerText },
602+
"The peer refused link approval. Check the pairing code and try again.",
603+
],
604+
[
605+
"/oauth/mcp/token",
606+
{ error: "invalid_grant", error_description: peerText },
607+
"The peer refused the sign-in token. Create a new pairing code and try again.",
608+
],
609+
[
610+
"/oauth/mcp/decision",
611+
{ redirectTo: "http://localhost/?error_description=" + encodeURIComponent(peerText) },
612+
"The peer did not approve the link. Check the pairing code and try again.",
613+
],
614+
] as const) {
615+
yield* Ref.set(
616+
b.replies,
617+
new Map([[path, { status: "redirectTo" in body ? 200 : 400, body }]]),
618+
);
619+
const code = yield* b.auth.issuePairingCredential();
620+
const failed = yield* a.links
621+
.link({ url: b.url, pairingCode: code.credential, access: "auto" })
622+
.pipe(Effect.flip);
623+
expect(failed.reason).toBe("pairing_rejected");
624+
expect(failed.message).toBe(message);
625+
}
626+
yield* Ref.set(b.replies, new Map());
509627
yield* Ref.set(b.descriptor, laptop);
510628
const self = yield* a.links
511629
.link({ url: b.url, pairingCode: pairing.credential, access: "auto" })
@@ -545,6 +663,8 @@ it.effect(
545663
}),
546664
);
547665
expect(yield* probe(a)).toEqual(result);
666+
yield* Ref.set(b.sse, true);
667+
expect(yield* probe(a)).toEqual(result);
548668

549669
yield* Ref.set(
550670
b.toolResult,

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const peerOrigin = (url: string) => {
103103
return Effect.fail(
104104
new PeerLinkError({
105105
reason: "unreachable",
106-
message: `${url} is not an http or https address.`,
106+
message: "The peer address must use http or https.",
107107
}),
108108
);
109109
}
@@ -115,7 +115,8 @@ const peerOrigin = (url: string) => {
115115
return Effect.fail(
116116
new PeerLinkError({
117117
reason: "unreachable",
118-
message: `${url} is plain http, which would send the link's credentials unencrypted. Use HTTPS, or HTTP with loopback, a Tailscale .ts.net name, or Tailscale IPv6.`,
118+
message:
119+
"The peer address uses plain http, which would send the link's credentials unencrypted. Use HTTPS, or HTTP with loopback, a Tailscale .ts.net name, or Tailscale IPv6.",
119120
}),
120121
);
121122
}
@@ -190,7 +191,9 @@ const make = Effect.gen(function* () {
190191
Effect.fail(
191192
new PeerLinkError({
192193
reason: "pairing_rejected",
193-
message: `The peer refused to register this environment: ${error.message}`,
194+
message:
195+
"The peer refused to register this environment. Check its connection settings.",
196+
cause: error,
194197
}),
195198
),
196199
),
@@ -220,7 +223,11 @@ const make = Effect.gen(function* () {
220223
Effect.catchTags({
221224
AuthMcpApprovalError: (error) =>
222225
Effect.fail(
223-
new PeerLinkError({ reason: "pairing_rejected", message: error.message }),
226+
new PeerLinkError({
227+
reason: "pairing_rejected",
228+
message: "The peer refused link approval. Check the pairing code and try again.",
229+
cause: error,
230+
}),
224231
),
225232
}),
226233
);
@@ -235,9 +242,7 @@ const make = Effect.gen(function* () {
235242
if (code === null || redirect.searchParams.get("state") !== state) {
236243
return yield* new PeerLinkError({
237244
reason: "pairing_rejected",
238-
message:
239-
redirect.searchParams.get("error_description") ??
240-
"The peer did not approve the link. Check the pairing code and try again.",
245+
message: "The peer did not approve the link. Check the pairing code and try again.",
241246
});
242247
}
243248
return yield* client.mcpOAuth
@@ -256,7 +261,9 @@ const make = Effect.gen(function* () {
256261
Effect.fail(
257262
new PeerLinkError({
258263
reason: "pairing_rejected",
259-
message: `The peer refused the sign-in: ${error.message}`,
264+
message:
265+
"The peer refused the sign-in token. Create a new pairing code and try again.",
266+
cause: error,
260267
}),
261268
),
262269
),
@@ -422,13 +429,14 @@ const make = Effect.gen(function* () {
422429

423430
const unlink: PeerLinks["Service"]["unlink"] = (environmentId) =>
424431
Effect.gen(function* () {
432+
const row = yield* readRow(environmentId);
433+
if (Option.isNone(row)) return false;
434+
yield* secrets.remove(row.value.secret_name).pipe(Effect.catch(failStorage));
425435
const deleted = yield* sql<{ secret_name: string }>`
426-
DELETE FROM peer_environment_links WHERE environment_id = ${environmentId}
436+
DELETE FROM peer_environment_links
437+
WHERE environment_id = ${environmentId} AND secret_name = ${row.value.secret_name}
427438
RETURNING secret_name
428439
`.pipe(Effect.catch(failStorage));
429-
for (const row of deleted) {
430-
yield* secrets.remove(row.secret_name).pipe(Effect.catch(failStorage));
431-
}
432440
return deleted.length > 0;
433441
});
434442

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class PeerMcpClient extends Context.Service<
4545
>()("t3/peer/PeerMcpClient") {}
4646

4747
const ToolCallResponse = Schema.Struct({
48+
id: Schema.Number,
4849
result: Schema.optional(
4950
Schema.Struct({
5051
content: Schema.Array(
@@ -56,7 +57,9 @@ const ToolCallResponse = Schema.Struct({
5657
),
5758
error: Schema.optional(Schema.Struct({ code: Schema.Number, message: Schema.String })),
5859
});
59-
const decodeToolCallResponse = Schema.decodeUnknownEffect(Schema.fromJsonString(ToolCallResponse));
60+
const ToolCallResponseJson = Schema.fromJsonString(ToolCallResponse);
61+
const decodeToolCallResponse = Schema.decodeUnknownEffect(ToolCallResponseJson);
62+
const decodeToolCallResponseOption = Schema.decodeUnknownOption(ToolCallResponseJson);
6063
const Failure = Schema.Struct({ code: OrchestratorMcpFailure.fields.code, message: Schema.String });
6164
const decodeFailure = Schema.decodeUnknownOption(Failure);
6265
const decodeTextFailure = Schema.decodeUnknownOption(Schema.fromJsonString(Failure));
@@ -73,15 +76,25 @@ const fromLinkError = (error: PeerLinkError) =>
7376
message: error.message,
7477
});
7578

76-
/** The body of an MCP response: JSON, or a single server-sent event carrying JSON. */
77-
const responseJson = (contentType: string | undefined, text: string) =>
78-
contentType?.includes("text/event-stream") === true
79-
? text
80-
.split("\n")
81-
.filter((line) => line.startsWith("data:"))
82-
.map((line) => line.slice("data:".length).trim())
83-
.join("")
84-
: text;
79+
const responseJson = (contentType: string | undefined, text: string, requestId: number) => {
80+
if (contentType?.includes("text/event-stream") !== true) return text;
81+
for (const event of text.replace(/\r\n?/g, "\n").split("\n\n")) {
82+
const data = event
83+
.split("\n")
84+
.filter((line) => line.startsWith("data:"))
85+
.map((line) => line.slice("data:".length).replace(/^ /, ""))
86+
.join("\n");
87+
const decoded = decodeToolCallResponseOption(data);
88+
if (
89+
decoded._tag === "Some" &&
90+
decoded.value.id === requestId &&
91+
(decoded.value.result !== undefined || decoded.value.error !== undefined)
92+
) {
93+
return data;
94+
}
95+
}
96+
return "";
97+
};
8598

8699
const make = Effect.gen(function* () {
87100
const links = yield* PeerLinks.PeerLinks;
@@ -187,7 +200,7 @@ const make = Effect.gen(function* () {
187200
});
188201
}
189202
const body = yield* decodeToolCallResponse(
190-
responseJson(response.headers["content-type"], yield* response.text),
203+
responseJson(response.headers["content-type"], yield* response.text, 1),
191204
).pipe(
192205
Effect.mapError(
193206
() =>

‎apps/server/src/persistence/Migrations/064_CanonicalizeThreadPullRequestLinks.test.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ layer("064_CanonicalizeThreadPullRequestLinks", (it) => {
123123
'user', ${v1Payload}, '{}', 1)
124124
`;
125125

126-
assert.deepEqual(yield* runMigrations(), [[64, "CanonicalizeThreadPullRequestLinks"]]);
127-
assert.deepEqual(yield* runMigrations(), []);
126+
assert.deepEqual(yield* runMigrations({ toMigrationInclusive: 64 }), [
127+
[64, "CanonicalizeThreadPullRequestLinks"],
128+
]);
129+
assert.deepEqual(yield* runMigrations({ toMigrationInclusive: 64 }), []);
128130
const rows = yield* sql<{ readonly thread_id: string; readonly payload_json: string }>`
129131
SELECT thread_id, payload_json FROM orchestration_v2_projection_threads
130132
`;

0 commit comments

Comments
 (0)