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,49 @@ 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 card = yield* ask(a, { + environmentId: box.environmentId, + requestedAccess: "full-access", + }); + expect(card).toMatchObject({ linkStatus: "pending", requestedAccess: "full-access" }); + // Naming no access, or no more than the link has, takes the link as it is. + for (const asks of [{}, { requestedAccess: "read-only" }]) { + expect( + (yield* a.call({ environmentId: box.environmentId, ...asks })).structuredContent, + ).toMatchObject({ status: "linked", access: "read-only" }); + } + expect((yield* linkCards(a)).filter((item) => item.linkStatus === "pending")).toEqual([card]); + + // 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* linkCards(a)).find((item) => item.id === card.id)).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 +1146,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..7123d001d6ecf 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 needs no card: the agent can use it now. Only one + // asked for more access than the link has 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/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index 028cd6cdf1fdf..d75f8f60a3ffd 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { "claudeSystemAppendBytes": 6245, "codexAdditionalContextBytes": 7639, - "mcpToolListJsonBytes": 57751 + "mcpToolListJsonBytes": 57742 } 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 : (