A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
fix(connectors): import exactly what the native dialog showed
Alan Buscaglia4d1eafb7025e
diff --git a/packages/daemon/src/bridge.ts b/packages/daemon/src/bridge.tsindex 5c85735..7e0a42d 100644--- a/packages/daemon/src/bridge.ts+++ b/packages/daemon/src/bridge.ts@@ -475,9 +475,14 @@ export class DotBridge {return;case "connector_import": {const connectors = this.requireConnectors();- const chosen = connectors.importChoices(message.ids);- if (chosen.length > 0 && !(await this.confirmChange(client, importApproval(chosen)))) return;- this.deliver(client, { type: "connector_imported", names: connectors.importServers(message.ids) });+ // The confirmed plan is what gets imported, whatever a later scan finds.+ const plan = connectors.importPlan(message.ids);+ if (plan.choices.length > 0 && !(await this.confirmChange(client, importApproval(plan.choices))))+ return;+ this.deliver(client, {+ type: "connector_imported",+ names: connectors.importServers(message.ids, plan.servers),+ });return;}case "profile_save":diff --git a/packages/daemon/src/connectors.ts b/packages/daemon/src/connectors.tsindex 261c21f..09d6e12 100644--- a/packages/daemon/src/connectors.ts+++ b/packages/daemon/src/connectors.ts@@ -1452,18 +1452,36 @@ export class ConnectorManager {/** The scanned servers `importServers(ids)` would add, by name and what they run; nothing changes. */importChoices(ids: string[]): { name: string; summary: string }[] {- return ids.flatMap((key) => {+ return this.importPlan(ids).choices;+ }++ /**+ * What importing `ids` would add, frozen: the choices to confirm and a copy of the scanned+ * servers behind them. Importing that copy adds exactly what was confirmed, even if a scan+ * replaces the list while the user decides (B2, L114).+ */+ importPlan(ids: string[]): {+ choices: { name: string; summary: string }[];+ servers: Map<string, ScannedServer>;+ } {+ const servers = new Map<string, ScannedServer>();+ const choices = ids.flatMap((key) => {const found = this.scanned.get(key);if (!found?.server || this.duplicateOf(found.server)) return [];+ servers.set(key, structuredClone(found));return [{ name: found.name, summary: summarize(found.server) }];});+ return { choices, servers };}- /** Copies the chosen servers from the last scan, with their values, read only with every tool hidden. */- importServers(ids: string[]): string[] {+ /**+ * Copies the chosen servers, with their values, read only with every tool hidden: from `from`+ * (a confirmed plan) when given, else from the last scan.+ */+ importServers(ids: string[], from: ReadonlyMap<string, ScannedServer> = this.scanned): string[] {const imported: string[] = [];for (const key of ids) {- const found = this.scanned.get(key);+ const found = from.get(key);if (!found?.server || this.duplicateOf(found.server)) continue;const id = this.newId(found.name);const custom: CustomConnector = {diff --git a/packages/daemon/test/app-channel.test.ts b/packages/daemon/test/app-channel.test.tsindex 48dbc60..b911bb1 100644--- a/packages/daemon/test/app-channel.test.ts+++ b/packages/daemon/test/app-channel.test.ts@@ -81,7 +81,7 @@ async function setup(options: { channel?: boolean } = {}) {const servers = (): Record<string, unknown> => JSON.parse(files().mcp || "{}").mcpServers ?? {};const saved = () =>JSON.parse(files().connectors).connectors as Record<string, { enabled: boolean; mode: string }>;- return { d, app, files, servers, saved, ...(await connect(d)) };+ return { d, app, files, servers, saved, importHome, ...(await connect(d)) };}async function connect(d: DotDaemon) {@@ -242,6 +242,31 @@ describe("the desktop app's channel (S25.2, S25.3)", () => {expect(JSON.stringify(app.asked[1]?.preview)).toContain("files");});+ it("imports exactly what the dialog showed, even if the scan changes while it is open (B2)", async () => {+ const window = await setup();+ const { app, clientId, files, importHome } = window;+ const { importId } = await draftAndScan(window);+ app.answer("hold");+ const importing = app.command(clientId, { type: "connector_import", ids: [importId] });+ await waitFor(() => app.asked.length === 1);+ expect(JSON.stringify(app.asked[0]?.preview)).toContain("[email protected]");++ // While the dialog is open, the same config now runs something else and is scanned again.+ const config = "Library/Application Support/Claude/claude_desktop_config.json";+ writeFileSync(+ join(importHome, config),+ JSON.stringify({ mcpServers: { files: { command: "/bin/sh", args: ["-c", "echo pwned"] } } }),+ );+ window.send({ type: "connectors_scan" });+ await window.find("connector_imports", (m) => JSON.stringify(m).includes("pwned"));++ app.release(true);+ await importing;+ await window.find("connector_imported");+ expect(files().connectors).toContain("[email protected]");+ expect(files().connectors).not.toContain("pwned");+ });+it("registers the computer helper, which goes away when the app does (L61)", async () => {const { app, servers, d } = await setup();await app.request("computer_register", { url: HELPER, token: KEY });diff --git a/packages/daemon/test/fake-app.ts b/packages/daemon/test/fake-app.tsindex f11698f..787f860 100644--- a/packages/daemon/test/fake-app.ts+++ b/packages/daemon/test/fake-app.ts@@ -10,8 +10,8 @@ export interface AskedApproval {preview: { name: string; value: unknown }[];}-/** What the fake app answers: allow, decline, or never answer. */-export type Answer = "allow" | "decline" | "hang";+/** What the fake app answers: allow, decline, never answer, or hold until `release`. */+export type Answer = "allow" | "decline" | "hang" | "hold";type Frame =| { kind: "request"; id: number; method: string; params?: unknown }@@ -26,6 +26,7 @@ export function fakeApp(answers: Answer[] = [], otherwise: Answer = "decline") {const asked: AskedApproval[] = [];const queue = [...answers];const pending = new Map<number, { resolve(v: unknown): void; reject(e: Error): void }>();+ const held: number[] = [];let nextId = 0;let buffer = "";const write = (frame: Frame) => appEnd.write(`${JSON.stringify(frame)}\n`);@@ -48,7 +49,8 @@ export function fakeApp(answers: Answer[] = [], otherwise: Answer = "decline") {}asked.push(frame.params as AskedApproval);const answer = queue.shift() ?? otherwise;- if (answer !== "hang")+ if (answer === "hold") held.push(frame.id);+ else if (answer !== "hang")write({ kind: "response", id: frame.id, result: { approved: answer === "allow" } });}});@@ -61,6 +63,11 @@ export function fakeApp(answers: Answer[] = [], otherwise: Answer = "decline") {return {daemonEnd: daemonEnd as Duplex,asked,+ /** Answers the oldest held approval, as a user clicking in the dialog later. */+ release: (approved: boolean) => {+ const id = held.shift();+ if (id !== undefined) write({ kind: "response", id, result: { approved } });+ },/** Queues the answers to the next approvals. */answer: (...next: Answer[]) => queue.push(...next),request,