@devries-cameron20 / t3code-1 / commits / 83d2b8e347
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): a linked retry closes the waiting link card it names (#28387)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 1, 2026 at 21:26 UTC 83d2b8e3478b85cbe9691b7e4941e06b81856be7
2 files changed +91 -16
apps/server/src/peer/PeerLinkRequests.test.ts
+29
@@ -951,6 +951,35 @@ it.live("naming a machine that is already linked returns the link at once, with
951 ),
952 );
953
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 +
983 it.live("a link that expired goes through the card, so the user can link it again", () =>
984 withBoth((a, b) =>
985 Effect.gen(function* () {
apps/server/src/peer/PeerLinkRequests.ts
+62 -16
@@ -136,6 +136,42 @@ const make = Effect.gen(function* () {
136 ...(wake === undefined ? {} : { wake }),
137 });
138
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 +
175 const request: PeerLinkRequests["Service"]["request"] = Effect.fn("PeerLinkRequests.request")(
176 function* (scope, input) {
177 // The card is shown in, and answered from, the caller's own thread.
@@ -171,6 +207,21 @@ const make = Effect.gen(function* () {
207 ),
208 );
209 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 + }
225 return {
226 status: "linked",
227 environmentId: linked.value.environmentId,
@@ -180,12 +231,8 @@ const make = Effect.gen(function* () {
231 }
232 }
233 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);
236 if (input.clientRequestId !== undefined) {
237 const existing = yield* readCard(threadId, turnItemId).pipe(Effect.mapError(unreadable));
238 if (existing !== undefined) return resultOf(existing);
@@ -244,13 +291,6 @@ const make = Effect.gen(function* () {
291 Effect.withSpan("PeerLinkRequests.request"),
292 );
293
247 - const linkedOutcome = (linked: PeerLink): LinkRequestOutcome => ({
248 - linkStatus: "linked",
249 - linkedEnvironmentId: linked.environmentId,
250 - linkedLabel: linked.label,
251 - linkedAccess: linked.access,
252 - });
253 -
294 /** Links the machine the user picked, or finds the link they chose to keep. */
295 const answerOutcome = (
296 answer: PeerLinkRequestAnswerInput["answer"],
@@ -300,9 +340,6 @@ const make = Effect.gen(function* () {
340 }
341 };
342
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>();
343 const answer: PeerLinkRequests["Service"]["answer"] = Effect.fn("PeerLinkRequests.answer")(
344 function* (input) {
345 yield* Effect.annotateCurrentSpan({
@@ -345,6 +382,15 @@ const make = Effect.gen(function* () {
382 return PeerLinkRequests.of({ request, answer });
383 });
384
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 +
394 const pendingResult = (turnItemId: TurnItemId): OrchestratorMcpEnvironmentLinkResult => ({
395 status: "pending",
396 turnItemId,