diff --git a/apps/mobile/src/features/threads/LinkRequestCard.tsx b/apps/mobile/src/features/threads/LinkRequestCard.tsx index 1ee34663431b..f52057a121b5 100644 --- a/apps/mobile/src/features/threads/LinkRequestCard.tsx +++ b/apps/mobile/src/features/threads/LinkRequestCard.tsx @@ -9,6 +9,8 @@ import { linkRequestFailureMessage, linkRequestInitialMachine, type LinkRequestItem, + linkRequestKeepsLink, + linkRequestSuggestedAddress, linkRequestTarget, rankMachinesByHint, } from "@t3tools/client-runtime/link-request"; @@ -207,7 +209,8 @@ function LinkRequestSheet(props: { const listed = useLinkableMachines(props.environmentId, true); const machines = useMemo(() => rankMachinesByHint(listed, item.hint), [listed, item.hint]); const [choice, setChoice] = useState(null); - const [address, setAddress] = useState(item.url ?? ""); + // Null until the user types; until then the field holds the suggested address. + const [typedAddress, setAddress] = useState(null); const [access, setAccess] = useState(() => linkRequestDefaultAccess(item)); const [pairingCode, setPairingCode] = useState(""); const [busy, setBusy] = useState(false); @@ -223,6 +226,7 @@ function LinkRequestSheet(props: { }); const selected = choice ?? linkRequestInitialMachine(item, machines); const target = linkRequestTarget(item, machines, selected, m); + const address = typedAddress ?? linkRequestSuggestedAddress(item, target); const targetName = target.kind === "machine" ? target.label @@ -243,7 +247,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 +255,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, @@ -375,7 +379,7 @@ function LinkRequestSheet(props: { /> ) : null} - {needsAddress && item.url === undefined ? ( + {needsAddress && item.url === undefined && !useExisting ? ( ) : 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,64 @@ 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]); + + // Keeping a link with less access than asked is no answer, from a client whose list + // still shows it covering the request say; the card stays open to link again. + const kept = yield* a.requests + .answer({ + threadId, + turnItemId: card.id, + answer: { type: "use-existing", environmentId: box.environmentId }, + }) + .pipe(Effect.flip); + expect(kept.reason).toBe("link_too_weak"); + expect((yield* linkCards(a)).find((item) => item.id === card.id)).toMatchObject({ + linkStatus: "pending", + }); + expect((yield* outcomeMessages(a)).messages).toHaveLength(0); + + // 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 +1161,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 a33bb47d7ec1..2fea5fea9d59 100644 --- a/apps/server/src/peer/PeerLinkRequests.ts +++ b/apps/server/src/peer/PeerLinkRequests.ts @@ -5,8 +5,10 @@ * (provider CLIs abandon a long tool call): the answer reaches the agent as a notification. */ import { + type AuthMcpClientAccess, CommandId, type EnvironmentId, + mcpClientAccessCovers, MessageId, type NodeId, type OrchestrationV2Notification, @@ -15,7 +17,6 @@ import { type OrchestratorMcpEnvironmentLinkResult, OrchestratorMcpFailure, type PeerLink, - PeerLinkError, PeerLinkRequestError, type PeerLinkRequestAnswerInput, type RunId, @@ -209,7 +210,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 +219,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 +306,26 @@ 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 => { + requestedAccess: AuthMcpClientAccess | undefined, + ): 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 or replaced 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.", - }), - ), - onSome: (linked) => Effect.succeed(linkedOutcome(linked)), + onNone: () => Effect.fail(new PeerLinkRequestError({ reason: "link_gone" })), + onSome: (linked) => + requestedAccess === undefined || + mcpClientAccessCovers(linked.access, requestedAccess) + ? Effect.succeed(linkedOutcome(linked)) + : Effect.fail(new PeerLinkRequestError({ reason: "link_too_weak" })), }), ), - Effect.catch((error) => - Effect.succeed({ linkStatus: "failed", failure: error.message }), - ), ); case "link": { const [url, ...alternateUrls] = answer.urls; @@ -380,7 +384,7 @@ const make = Effect.gen(function* () { linkStatus: "failed", failure: "This request is from before an update. Ask for the link again.", } - : yield* answerOutcome(input.answer); + : yield* answerOutcome(input.answer, card.requestedAccess); // If Stop closed the card while linking, the card keeps that; the link // itself stands and Settings lists it. yield* lifecycleLocks.withLock( diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index 169fac6ce2a8..be2ee3583899 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { "claudeSystemAppendBytes": 6245, "codexAdditionalContextBytes": 7639, - "mcpToolListJsonBytes": 61724 + "mcpToolListJsonBytes": 61715 } diff --git a/apps/web/src/components/chat/LinkRequestCard.tsx b/apps/web/src/components/chat/LinkRequestCard.tsx index 81c25774a900..b2e8eb6bf7ff 100644 --- a/apps/web/src/components/chat/LinkRequestCard.tsx +++ b/apps/web/src/components/chat/LinkRequestCard.tsx @@ -9,6 +9,8 @@ import { linkRequestFailureMessage, linkRequestInitialMachine, type LinkRequestItem, + linkRequestKeepsLink, + linkRequestSuggestedAddress, linkRequestTarget, linkRequestTargetName, rankMachinesByHint, @@ -189,7 +191,8 @@ function LinkRequestDialog(props: { const machines = rankMachinesByHint(listed, item.hint); // Null until the user picks; until then the hint's best match is picked. const [choice, setChoice] = useState(null); - const [address, setAddress] = useState(item.url ?? ""); + // Null until the user types; until then the field holds the suggested address. + const [typedAddress, setAddress] = useState(null); const [access, setAccess] = useState(() => linkRequestDefaultAccess(item)); const [pairingCode, setPairingCode] = useState(""); const [step, setStep] = useState("machine"); @@ -207,6 +210,7 @@ function LinkRequestDialog(props: { }); const selected = choice ?? linkRequestInitialMachine(item, machines); const target = linkRequestTarget(item, machines, selected, messages); + const address = typedAddress ?? linkRequestSuggestedAddress(item, target); const targetName = target.kind === "machine" ? target.label : linkRequestTargetName(item, messages); // Without a catalog entry or an address, the name is the generic "a machine". @@ -230,9 +234,9 @@ function LinkRequestDialog(props: { Option.isSome(connected) && mayMint && !mintFailed.has(targetId); + const useExisting = linkRequestKeepsLink(item, target); // 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 asksAddress = needsAddress && item.url === undefined && !useExisting; // A named machine with an address it answers at has nothing to choose before access. const steps: ReadonlyArray = pickMachine || asksAddress ? ["machine", "access"] : ["access"]; @@ -243,7 +247,7 @@ function LinkRequestDialog(props: { const reset = () => { setChoice(null); - setAddress(item.url ?? ""); + setAddress(null); setAccess(linkRequestDefaultAccess(item)); setPairingCode(""); setStep("machine"); @@ -259,7 +263,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 +411,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 : (