Skip to content

Commit 83d2b8e

Browse files
fix(server): a linked retry closes the waiting link card it names (pingdotgg#28387)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 5f2c27e commit 83d2b8e

2 files changed

Lines changed: 91 additions & 16 deletions

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,35 @@ it.live("naming a machine that is already linked returns the link at once, with
951951
),
952952
);
953953

954+
it.live("a retry naming a machine linked meanwhile closes the card it left as linked", () =>
955+
withBoth((a, b) =>
956+
Effect.gen(function* () {
957+
yield* seedThread(a);
958+
yield* a.call({ url: b.url, clientRequestId: "link-box" });
959+
yield* pendingCard(a);
960+
// Linked from Settings meanwhile, not through the card.
961+
const pairing = yield* b.auth.issuePairingCredential();
962+
yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" });
963+
964+
const retried = yield* a.call({
965+
environmentId: box.environmentId,
966+
clientRequestId: "link-box",
967+
});
968+
expect(retried.structuredContent).toEqual({
969+
status: "linked",
970+
environmentId: box.environmentId,
971+
label: "Box",
972+
access: "auto",
973+
});
974+
expect(yield* cardOf(a)).toMatchObject({
975+
linkStatus: "linked",
976+
linkedEnvironmentId: box.environmentId,
977+
});
978+
expect((yield* a.orchestrator.getThreadShell(threadId))?.pendingRuntimeRequest).toBeNull();
979+
}),
980+
),
981+
);
982+
954983
it.live("a link that expired goes through the card, so the user can link it again", () =>
955984
withBoth((a, b) =>
956985
Effect.gen(function* () {

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

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,42 @@ const make = Effect.gen(function* () {
136136
...(wake === undefined ? {} : { wake }),
137137
});
138138

139+
const linkedOutcome = (linked: PeerLink): LinkRequestOutcome => ({
140+
linkStatus: "linked",
141+
linkedEnvironmentId: linked.environmentId,
142+
linkedLabel: linked.label,
143+
linkedAccess: linked.access,
144+
});
145+
146+
// One answer per card at a time: two answers racing on one card must not
147+
// both link. Other cards go on while a slow peer holds one.
148+
const answerLocks = yield* KeyedLock.make<TurnItemId>();
149+
150+
/**
151+
* Closes a card still waiting when its agent retries naming a machine linked
152+
* meanwhile. Nothing wakes the agent: the retry itself returns the link.
153+
*/
154+
const closeAsLinked = (threadId: ThreadId, turnItemId: TurnItemId, linked: PeerLink) =>
155+
answerLocks.withLock(
156+
turnItemId,
157+
Effect.gen(function* () {
158+
const card = yield* readCard(threadId, turnItemId);
159+
if (
160+
card === undefined ||
161+
card.linkStatus !== "pending" ||
162+
card.runId === null ||
163+
card.nodeId === null
164+
) {
165+
return;
166+
}
167+
yield* record(
168+
{ ...card, runId: card.runId, nodeId: card.nodeId },
169+
linkedOutcome(linked),
170+
CommandId.make(`link-request:${card.id}:linked-on-retry`),
171+
);
172+
}),
173+
);
174+
139175
const request: PeerLinkRequests["Service"]["request"] = Effect.fn("PeerLinkRequests.request")(
140176
function* (scope, input) {
141177
// The card is shown in, and answered from, the caller's own thread.
@@ -171,6 +207,21 @@ const make = Effect.gen(function* () {
171207
),
172208
);
173209
if (Option.isSome(linked)) {
210+
if (input.clientRequestId !== undefined) {
211+
yield* closeAsLinked(
212+
threadId,
213+
cardId(threadId, scope.requestNamespace, input.clientRequestId),
214+
linked.value,
215+
).pipe(
216+
Effect.mapError(
217+
(error) =>
218+
new OrchestratorMcpFailure({
219+
code: "orchestration_error",
220+
message: `Could not close the link request: ${errorMessage(error)}`,
221+
}),
222+
),
223+
);
224+
}
174225
return {
175226
status: "linked",
176227
environmentId: linked.value.environmentId,
@@ -180,12 +231,8 @@ const make = Effect.gen(function* () {
180231
}
181232
}
182233
const key = input.clientRequestId ?? (yield* crypto.randomUUIDv4().pipe(Effect.orDie));
183-
// Turn item ids are global; scope the key to this thread and provider
184-
// session, since a later session may reuse it. A retry with the same
185-
// clientRequestId finds this card and records nothing.
186-
const turnItemId = TurnItemId.make(
187-
`turn-item:link-request:${encodeURIComponent(threadId)}:${encodeURIComponent(scope.requestNamespace)}:${encodeURIComponent(key)}`,
188-
);
234+
// A retry with the same clientRequestId finds this card and records nothing.
235+
const turnItemId = cardId(threadId, scope.requestNamespace, key);
189236
if (input.clientRequestId !== undefined) {
190237
const existing = yield* readCard(threadId, turnItemId).pipe(Effect.mapError(unreadable));
191238
if (existing !== undefined) return resultOf(existing);
@@ -244,13 +291,6 @@ const make = Effect.gen(function* () {
244291
Effect.withSpan("PeerLinkRequests.request"),
245292
);
246293

247-
const linkedOutcome = (linked: PeerLink): LinkRequestOutcome => ({
248-
linkStatus: "linked",
249-
linkedEnvironmentId: linked.environmentId,
250-
linkedLabel: linked.label,
251-
linkedAccess: linked.access,
252-
});
253-
254294
/** Links the machine the user picked, or finds the link they chose to keep. */
255295
const answerOutcome = (
256296
answer: PeerLinkRequestAnswerInput["answer"],
@@ -300,9 +340,6 @@ const make = Effect.gen(function* () {
300340
}
301341
};
302342

303-
// One answer per card at a time: two answers racing on one card must not
304-
// both link. Other cards go on while a slow peer holds one.
305-
const answerLocks = yield* KeyedLock.make<TurnItemId>();
306343
const answer: PeerLinkRequests["Service"]["answer"] = Effect.fn("PeerLinkRequests.answer")(
307344
function* (input) {
308345
yield* Effect.annotateCurrentSpan({
@@ -345,6 +382,15 @@ const make = Effect.gen(function* () {
345382
return PeerLinkRequests.of({ request, answer });
346383
});
347384

385+
/**
386+
* Turn item ids are global; scope the key to this thread and provider session,
387+
* since a later session may reuse it.
388+
*/
389+
const cardId = (threadId: ThreadId, requestNamespace: string, key: string) =>
390+
TurnItemId.make(
391+
`turn-item:link-request:${encodeURIComponent(threadId)}:${encodeURIComponent(requestNamespace)}:${encodeURIComponent(key)}`,
392+
);
393+
348394
const pendingResult = (turnItemId: TurnItemId): OrchestratorMcpEnvironmentLinkResult => ({
349395
status: "pending",
350396
turnItemId,

0 commit comments

Comments
 (0)