Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(cli): say to pair from the desktop app when it owns the server

Bugfixer 24a56c1d87c6b
diff --git a/server/cli-pair.test.ts b/server/cli-pair.test.ts
index 99f04bf888..77d0e873f0 100644
--- a/server/cli-pair.test.ts
+++ b/server/cli-pair.test.ts
@@ -131,6 +131,26 @@ describe("openmausbot pair", () => {
expect(printed()).not.toMatch(/[▀▄█]/);
});
+ it("when the desktop app owns the server, says to pair from the app instead of showing the raw refusal", async () => {
+ fetchMock.mockImplementation(async (input, init) => {
+ const url = String(input);
+ if (url.endsWith("/api/health")) return Response.json({ app: "openmausbot", pid: 12345 });
+ if (url.endsWith("/api/auth/pairing")) {
+ return init?.method === "POST"
+ ? Response.json({ error: "forbidden: this change must come from the desktop app or a paired device" }, { status: 403 })
+ : Response.json({ pairings: [], publicUrl: null });
+ }
+ throw new Error(`Unexpected fixture request: ${url}`);
+ });
+ const failure = await runPair(options).then(() => null, (error: unknown) => error);
+ expect(failure).toBeInstanceOf(Error);
+ const message = (failure as Error).message;
+ expect(message).toContain("The OpenMausBot desktop app runs this server");
+ expect(message).toContain("Settings → Remote access");
+ expect(message).toContain("openmausbot serve");
+ expect(message).not.toContain("server refused to mint a pairing code");
+ });
+
it("says how to start a server when none runs", async () => {
fetchMock.mockImplementation(async () => { throw new Error("offline"); });
expect(await runPair(options)).toBe(1);
diff --git a/server/cli-pairing.ts b/server/cli-pairing.ts
index cb9c95bb1b..74bf4fd8da 100644
--- a/server/cli-pairing.ts
+++ b/server/cli-pairing.ts
@@ -5,6 +5,13 @@ import qrcode from "qrcode-terminal";
import { cliCommand } from "./cli-invocation.ts";
import type { PhoneRelayStatus } from "./phone-relay.ts";
+/** The desktop app owns this server, so only the app (or a paired device) may mint a code: say where pairing works instead. */
+export function desktopOwnedRefusal(status: number, body: unknown): string | null {
+ const error = (body as { error?: unknown } | null)?.error;
+ if (status !== 403 || typeof error !== "string" || !error.includes("must come from the desktop app")) return null;
+ return `The OpenMausBot desktop app runs this server, so it only pairs devices from the app itself: open OpenMausBot on this computer and use Settings → Remote access. To pair from this terminal instead, quit the desktop app and start the server with ${cliCommand("serve")}.`;
+}
+
/** A clock time a code works until: "11:22", or "tomorrow 11:22" / a date when it runs past today. */
export function untilText(expiresAt: number, now = Date.now()): string {
const end = new Date(expiresAt);
diff --git a/server/cli.ts b/server/cli.ts
index de9c2f3fae..a1636cc799 100644
--- a/server/cli.ts
+++ b/server/cli.ts
@@ -38,7 +38,7 @@ import { clearLeftoverTailscaleServe, explainTailscaleFailure, tailnetListenFall
import { defaultSetupIo, SetupCancelled, type SetupIo } from "./cli-prompts.ts";
import { HEADLESS_VERB_NAMES, usage } from "./cli-usage.ts";
import { normalizePhoneOrigin, phonePairingInstructions, runPhoneSetup } from "./cli-phone-setup.ts";
-import { browserSignInLines, formatSessions, pairingBlock, RELAY_NOT_WITH_TUNNEL, relayPairingBlock, showRelayPairing } from "./cli-pairing.ts";
+import { browserSignInLines, desktopOwnedRefusal, formatSessions, pairingBlock, RELAY_NOT_WITH_TUNNEL, relayPairingBlock, showRelayPairing } from "./cli-pairing.ts";
import { cliCommand, cliPrefix } from "./cli-invocation.ts";
import { checkReach, envPublicUrl, lanProblem, localLinkBase, noRouteLines, publicUrlProblem, reachLines, waitForReach, type Route } from "./cli-reach.ts";
import { lanUrl } from "./lan-address.ts";
@@ -360,8 +360,8 @@ async function mintPairing(port: number, options: { label?: string; client?: boo
const chatOnly = Boolean(options.client || options.phone);
const request = { ...(options.label ? { label: options.label } : {}), ...(chatOnly ? { scopes: ["client"] } : {}), ...(options.ttlMs ? { ttlMs: options.ttlMs } : {}) };
const { status, body } = await api(port, "/api/auth/pairing", { method: "POST", body: JSON.stringify(request) });
- if (refusedAsService(status, body)) throw new Error(SERVICE_TRUST_HELP);
- if (status !== 200) throw new Error(`server refused to mint a pairing code: ${typeof body?.error === "string" ? body.error : status}`);
+ const refusal = refusedAsService(status, body) ? SERVICE_TRUST_HELP : desktopOwnedRefusal(status, body);
+ if (refusal || status !== 200) throw new Error(refusal ?? `server refused to mint a pairing code: ${typeof body?.error === "string" ? body.error : status}`);
const reachable = options.publicUrl && !isLoopbackHost(new URL(options.publicUrl).hostname) ? options.publicUrl : null;
const link = reachable ? `${reachable}/pair#code=${body.code}` : typeof body.url === "string" && !isLoopbackHost(new URL(body.url).hostname) ? body.url : null;
const invite = (reachable && typeof body.credential === "string" ? phonePairingLink({ address: reachable, token: body.credential, name: typeof body.serverName === "string" ? body.serverName : undefined }) : null) ?? (typeof body.inviteUrl === "string" ? body.inviteUrl : null);