From 9972fbd14b6c76b439c4d944ff3ac4e56bf3ddad Mon Sep 17 00:00:00 2001 From: Orin Date: Wed, 18 Nov 2026 06:20:15 -0800 Subject: [PATCH 1/6] fix(server): agents can raise the access of an existing environment link When an agent asked t3_environment_link for more access than an existing link had, the server returned the existing link at once and showed no card, so the agent could only unlink and link again. The server now returns an existing link at once only when it has at least the requested access; otherwise the card asks the user, and linking again replaces the old link. The card offers "use this link" only when the link covers the requested access. A "use existing" answer for a link removed meanwhile now leaves the card open with an error instead of failing it, and the client refreshes its links after every answer, so the next try shows the link step. Co-Authored-By: Claude Opus 5.5 --- .../src/features/threads/LinkRequestCard.tsx | 25 ++++--- apps/mobile/src/i18n/translations/de.json | 1 + apps/mobile/src/i18n/translations/en.json | 1 + apps/mobile/src/i18n/translations/es.json | 1 + apps/mobile/src/i18n/translations/fr.json | 1 + apps/mobile/src/i18n/translations/ja.json | 1 + apps/mobile/src/i18n/translations/ko.json | 1 + apps/mobile/src/i18n/translations/pt-BR.json | 1 + apps/mobile/src/i18n/translations/ru.json | 1 + apps/mobile/src/i18n/translations/sv.json | 1 + apps/mobile/src/i18n/translations/zh.json | 1 + apps/server/src/peer/PeerLinkRequests.test.ts | 69 +++++++++++++++---- apps/server/src/peer/PeerLinkRequests.ts | 28 ++++---- .../src/components/chat/LinkRequestCard.tsx | 10 ++- .../components/settings/LinkMachineSelect.tsx | 2 +- .../settings/LinkedEnvironmentsSettings.tsx | 2 +- packages/client-runtime/src/linkBatch.test.ts | 4 +- packages/client-runtime/src/linkBatch.ts | 4 +- .../client-runtime/src/linkRequest.test.ts | 55 ++++++++++----- packages/client-runtime/src/linkRequest.ts | 34 ++++++--- .../client-runtime/src/state/peerLinks.ts | 3 +- packages/contracts/src/auth.ts | 7 ++ packages/contracts/src/peerLink.ts | 1 + 23 files changed, 183 insertions(+), 71 deletions(-) diff --git a/apps/mobile/src/features/threads/LinkRequestCard.tsx b/apps/mobile/src/features/threads/LinkRequestCard.tsx index 1ee34663431b8..bc59b741ea66c 100644 --- a/apps/mobile/src/features/threads/LinkRequestCard.tsx +++ b/apps/mobile/src/features/threads/LinkRequestCard.tsx @@ -9,6 +9,7 @@ import { linkRequestFailureMessage, linkRequestInitialMachine, type LinkRequestItem, + linkRequestKeepsLink, linkRequestTarget, rankMachinesByHint, } from "@t3tools/client-runtime/link-request"; @@ -243,7 +244,7 @@ function LinkRequestSheet(props: { Option.isSome(connected) && mayMint && !mintFailed.has(targetId); - const useExisting = target.kind === "machine" && target.linked; + const useExisting = linkRequestKeepsLink(item, target); const ready = target.kind !== "none" && (useExisting || @@ -251,7 +252,7 @@ function LinkRequestSheet(props: { const prepareAnswer = async () => { if (target.kind === "none") return null; - if (target.kind === "machine" && target.linked) { + if (useExisting && target.kind === "machine") { return linkRequestAnswerInput(item, { type: "use-existing", environmentId: target.environmentId, @@ -407,6 +408,11 @@ function LinkRequestSheet(props: { ) : target.kind === "none" ? null : ( <> + {target.kind === "machine" && target.linkedAccess !== null ? ( + + Already linked. Linking again renews it with the access you pick. + + ) : null} {canMint ? null : ( +it.live("naming a machine linked with enough access returns the link at once, with no card", () => withBoth((a, b) => Effect.gen(function* () { yield* seedThread(a); const pairing = yield* b.auth.issuePairingCredential(); yield* a.links.link({ url: b.url, pairingCode: pairing.credential, access: "auto" }); - const asked = yield* a.call({ environmentId: box.environmentId }); - expect(asked.structuredContent).toEqual({ - status: "linked", - environmentId: box.environmentId, - label: "Box", - access: "auto", - }); + for (const asks of [ + {}, + { requestedAccess: "approval-required" }, + { requestedAccess: "auto" }, + ]) { + const asked = yield* a.call({ environmentId: box.environmentId, ...asks }); + expect(asked.structuredContent).toEqual({ + status: "linked", + environmentId: box.environmentId, + label: "Box", + access: "auto", + }); + } expect(yield* cardOf(a)).toBeUndefined(); // Not both: the address could name another machine than the id. @@ -1088,6 +1094,41 @@ it.live("a retry naming a machine linked meanwhile closes the card it left as li ), ); +it.live("a machine linked with less access than asked goes through the card, which raises it", () => + withBoth((a, b) => + Effect.gen(function* () { + yield* seedThread(a); + yield* linkTo(a.links, b, "read-only"); + const asked = yield* a.call({ + environmentId: box.environmentId, + requestedAccess: "full-access", + }); + expect(asked.structuredContent).toMatchObject({ status: "pending" }); + const card = yield* pendingCard(a); + expect(card.requestedAccess).toBe("full-access"); + + // Linking again over the old link replaces it; nothing is unlinked first. + const pairing = yield* b.auth.issuePairingCredential(); + yield* a.requests.answer({ + threadId, + turnItemId: card.id, + answer: { + type: "link", + environmentId: box.environmentId, + label: "Box", + urls: [b.url], + access: "full-access", + pairingCode: pairing.credential, + }, + }); + expect(yield* cardOf(a)).toMatchObject({ linkStatus: "linked", linkedAccess: "full-access" }); + expect(Option.getOrUndefined(yield* a.links.get(box.environmentId))).toMatchObject({ + access: "full-access", + }); + }), + ), +); + it.live("a link that expired goes through the card, so the user can link it again", () => withBoth((a, b) => Effect.gen(function* () { @@ -1097,16 +1138,18 @@ it.live("a link that expired goes through the card, so the user can link it agai expect(asked.structuredContent).toMatchObject({ status: "pending" }); const card = yield* pendingCard(a); - // Keeping the expired link is no answer: the card fails and says why. - yield* a.requests + // Keeping the expired link is no answer, as when a client's list still shows one + // removed meanwhile; the card stays open for the user to link again. + const kept = yield* a.requests .answer({ threadId, turnItemId: card.id, answer: { type: "use-existing", environmentId: box.environmentId }, }) - .pipe(afterLinksExpire); - expect(yield* cardOf(a)).toMatchObject({ linkStatus: "failed" }); - expect((yield* outcomeMessages(a)).messages[0]!.text).toContain("expired"); + .pipe(afterLinksExpire, Effect.flip); + expect(kept.reason).toBe("link_gone"); + expect(yield* cardOf(a)).toMatchObject({ linkStatus: "pending" }); + expect((yield* outcomeMessages(a)).messages).toHaveLength(0); }), ), ); diff --git a/apps/server/src/peer/PeerLinkRequests.ts b/apps/server/src/peer/PeerLinkRequests.ts index a33bb47d7ec17..6f6f64668aed4 100644 --- a/apps/server/src/peer/PeerLinkRequests.ts +++ b/apps/server/src/peer/PeerLinkRequests.ts @@ -7,6 +7,7 @@ import { CommandId, type EnvironmentId, + mcpClientAccessCovers, MessageId, type NodeId, type OrchestrationV2Notification, @@ -15,7 +16,6 @@ import { type OrchestratorMcpEnvironmentLinkResult, OrchestratorMcpFailure, type PeerLink, - PeerLinkError, PeerLinkRequestError, type PeerLinkRequestAnswerInput, type RunId, @@ -209,7 +209,8 @@ const make = Effect.gen(function* () { "Pass either environmentId or url, not both. Pass neither to let the user pick the machine.", }); } - // A machine already linked needs no card: the agent can use it now. + // A machine already linked with enough access needs no card: the agent can + // use it now. One linked with less gets a card, and linking again raises it. if (input.environmentId !== undefined) { const linked = yield* usableLink(input.environmentId).pipe( Effect.mapError( @@ -217,7 +218,11 @@ const make = Effect.gen(function* () { new OrchestratorMcpFailure({ code: "orchestration_error", message: error.message }), ), ); - if (Option.isSome(linked)) { + if ( + Option.isSome(linked) && + (input.requestedAccess === undefined || + mcpClientAccessCovers(linked.value.access, input.requestedAccess)) + ) { if (input.clientRequestId !== undefined) { yield* closeAsLinked( threadId, @@ -300,28 +305,21 @@ const make = Effect.gen(function* () { /** Links the machine the user picked, or finds the link they chose to keep. */ const answerOutcome = ( answer: PeerLinkRequestAnswerInput["answer"], - ): Effect.Effect => { + ): Effect.Effect => { switch (answer.type) { case "decline": return Effect.succeed({ linkStatus: "declined" }); case "use-existing": + // A client's list of links can lag one removed meanwhile, by an agent say. + // The card stays open so the user can link the machine again. return usableLink(answer.environmentId).pipe( + Effect.mapError((cause) => new PeerLinkRequestError({ reason: "load_failed", cause })), Effect.flatMap( Option.match({ - onNone: () => - Effect.fail( - new PeerLinkError({ - reason: "unknown_link", - message: - "That environment is no longer linked here, or its link expired. Link it again.", - }), - ), + onNone: () => Effect.fail(new PeerLinkRequestError({ reason: "link_gone" })), onSome: (linked) => Effect.succeed(linkedOutcome(linked)), }), ), - Effect.catch((error) => - Effect.succeed({ linkStatus: "failed", failure: error.message }), - ), ); case "link": { const [url, ...alternateUrls] = answer.urls; diff --git a/apps/web/src/components/chat/LinkRequestCard.tsx b/apps/web/src/components/chat/LinkRequestCard.tsx index 81c25774a9000..86fd16626786e 100644 --- a/apps/web/src/components/chat/LinkRequestCard.tsx +++ b/apps/web/src/components/chat/LinkRequestCard.tsx @@ -9,6 +9,7 @@ import { linkRequestFailureMessage, linkRequestInitialMachine, type LinkRequestItem, + linkRequestKeepsLink, linkRequestTarget, linkRequestTargetName, rankMachinesByHint, @@ -232,7 +233,7 @@ function LinkRequestDialog(props: { !mintFailed.has(targetId); // An address the agent gave is already filled in; only one the user must type is asked for. const asksAddress = needsAddress && item.url === undefined; - const useExisting = target.kind === "machine" && target.linked; + const useExisting = linkRequestKeepsLink(item, target); // A named machine with an address it answers at has nothing to choose before access. const steps: ReadonlyArray = pickMachine || asksAddress ? ["machine", "access"] : ["access"]; @@ -259,7 +260,7 @@ function LinkRequestDialog(props: { // Settles to the answer to send, an error to show, or nothing to do. const prepareAnswer = async () => { if (target.kind === "none") return null; - if (target.kind === "machine" && target.linked) { + if (useExisting && target.kind === "machine") { return linkRequestAnswerInput(item, { type: "use-existing", environmentId: target.environmentId, @@ -407,6 +408,11 @@ function LinkRequestDialog(props: { ) : null} {useExisting ? null : ( <> + {target.kind === "machine" && target.linkedAccess !== null ? ( +

+ Already linked. Linking again renews it with the access you pick. +

+ ) : null} {needsAddress ? : null} {canMint ? null : (