Skip to content

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

desktop: relay status timing, and old-phone counts for the tunnel gate

milind-soni3f0747e2659e
diff --git a/electron/companion-account-service.mjs b/electron/companion-account-service.mjs
index b91d1f3999..70e7da7492 100644
--- a/electron/companion-account-service.mjs
+++ b/electron/companion-account-service.mjs
@@ -259,8 +259,8 @@ export function createCompanionAccountService({
firstEndpointCheckMs = DEFAULT_FIRST_ENDPOINT_CHECK_MS,
onDemand = false,
managedEndpointNeeded = async () => null,
- /** Reaches its phones through MausBot Relay: a "no" from managedEndpointNeeded then holds even right after sign-in. */
- relayCapable = () => false,
+ /** MausBot Relay: `capable` makes a "no" need hold even right after sign-in; `legacyDevices` counts old phones. */
+ relay = { capable: () => false, legacyDevices: async () => null },
releaseGraceMs = DEFAULT_RELEASE_GRACE_MS,
releaseJitterMs = DEFAULT_RELEASE_JITTER_MS,
// Only the code, HTTP status, request id and retry delay: never an email, token, credential or address.
@@ -550,15 +550,15 @@ export function createCompanionAccountService({
}
throw error;
}
- if (onDemand && (await currentNeed()) === false) {
+ const withoutTunnel = async () => {
await updateCredentials((document) => withAuthenticatedAccount(document, { accountToken, user, clientInstanceId }));
resetAutoRetry();
- phase = null;
- return settledState();
- }
- const endpoint = await client.ensureEndpoint(installation.credential, {
- appVersion: identity.appVersion,
- });
+ return (phase = null), settledState();
+ };
+ if (onDemand && (await currentNeed()) === false) return withoutTunnel();
+ const endpoint = await client.ensureEndpoint(installation.credential, { appVersion: identity.appVersion, ...(await relay.legacyDevices().then((count) => (count === null ? {} : { legacyDevices: count }))) })
+ .catch((error) => (error?.code === "relay_capable" ? null : Promise.reject(error)));
+ if (!endpoint) return withoutTunnel();
try {
await updateCredentials((document) =>
withProvisionedAccount(document, {
@@ -782,7 +782,7 @@ export function createCompanionAccountService({
const answer = await Promise.resolve().then(() => managedEndpointNeeded()).catch(() => null);
phonesUnused = answer === MANAGED_ENDPOINT_PHONES_UNUSED;
const need = phonesUnused ? false : typeof answer === "boolean" ? answer : null;
- if (need === false && relayCapable()) return false;
+ if (need === false && relay.capable()) return false;
if (requestedRecently()) return (phonesUnused = false), true;
return need;
};
diff --git a/electron/companion-account-service.test.mjs b/electron/companion-account-service.test.mjs
index c6c631b51d..a1576bb96a 100644
--- a/electron/companion-account-service.test.mjs
+++ b/electron/companion-account-service.test.mjs
@@ -1417,9 +1417,21 @@ describe("Companion account on demand", () => {
expect(managedEndpointNeed({ ...on([], { pairing }), e2e: { enabled: false } }, now)).toBe(true);
});
+ it("reports the old phones with a tunnel request, and takes 409 relay_capable as no tunnel needed", async () => {
+ const refused = Object.assign(new Error("relay capable"), { code: "relay_capable", status: 409 });
+ const client = readyClient({ ensureEndpoint: vi.fn(async () => Promise.reject(refused)) });
+ const f = onDemandFixture({ initial: {}, client, relay: { capable: () => false, legacyDevices: async () => 0 } });
+ f.companion.need = true;
+ await f.service.requestCode("[email protected]");
+ const state = await f.service.verifyCode("[email protected]", "12345678");
+ expect(client.ensureEndpoint).toHaveBeenCalledWith(expect.any(String), { appVersion: "1.2.3", legacyDevices: 0 });
+ expect(state).toEqual({ available: true, status: "ready", email: "[email protected]" });
+ f.service.dispose();
+ });
+
it("signs a relay-capable computer in without a tunnel when no phone needs one", async () => {
for (const relayCapable of [true, false]) {
- const f = onDemandFixture({ initial: {}, relayCapable: () => relayCapable });
+ const f = onDemandFixture({ initial: {}, relay: { capable: () => relayCapable, legacyDevices: async () => 0 } });
f.companion.need = false;
await f.service.requestCode("[email protected]");
const state = await f.service.verifyCode("[email protected]", "12345678");
diff --git a/electron/companion-e2e-desktop.mjs b/electron/companion-e2e-desktop.mjs
index b36e0b6182..ab8fd0a330 100644
--- a/electron/companion-e2e-desktop.mjs
+++ b/electron/companion-e2e-desktop.mjs
@@ -1,6 +1,7 @@
import { companionState, postCompanionMessage, setCompanionE2EHooks } from "./companion.mjs";
import { COMPANION_INSTALLATION_CREDENTIAL_FIELD, resolveCompanionControlPlaneURL } from "./companion-account-service.mjs";
import { createCompanionE2E } from "./companion-e2e.mjs";
+export { deviceCounts } from "./companion-e2e.mjs";
import { createControlPlaneClient } from "./control-plane-client.mjs";
const signedOut = () => Promise.reject(Object.assign(new Error("signed out"), { code: "signed_out" }));
diff --git a/electron/main.mjs b/electron/main.mjs
index 54ccb991f9..8bcc0a8b47 100644
--- a/electron/main.mjs
+++ b/electron/main.mjs
@@ -461,7 +461,7 @@ let logStream = null;
let desktopShutdownStarted = false;
import { companionAdvertisedHostedUrl, companionEnabledAtRest, companionOriginTarget, companionPairing, companionPairingRenew, companionDeviceRoutes, companionRefreshTailscale, companionCloudDesktopAccess,
companionBrowserControlAccess, companionRevoke, companionRunning, companionState, rememberCompanionEnabled, rememberCompanionKeepAwake, setCompanionHostedUrl, setCompanionLifecycleListener, startCompanion, stopCompanion } from "./companion.mjs";
-import { createDesktopCompanionE2E } from "./companion-e2e-desktop.mjs";
+import { createDesktopCompanionE2E, deviceCounts } from "./companion-e2e-desktop.mjs";
import { autoStartCompanion } from "./companion-autostart.mjs";
import { createRoutineWakeHold, rememberRoutineWake, routineWakeSettings } from "./routine-wake.mjs";
import { CREDENTIAL_PATCH, credentialSaveError } from "./credential-patch.mjs";
@@ -761,7 +761,7 @@ function ensureCompanionAccountService() {
onDemand: true,
managedEndpointNeeded: async () =>
companionRunning() ? managedEndpointNeed(await companionState().catch(() => null)) : null,
- relayCapable: () => companionE2E?.enabled === true,
+ relay: { capable: () => companionE2E?.enabled === true, legacyDevices: async () => (companionRunning() ? deviceCounts(await companionState().catch(() => null)).legacy : null) },
log: (line) => slog(line),
});
return companionAccountService;
diff --git a/src/components/CompanionSection.tsx b/src/components/CompanionSection.tsx
index 90fa61877b..d2bbada279 100644
--- a/src/components/CompanionSection.tsx
+++ b/src/components/CompanionSection.tsx
@@ -22,7 +22,7 @@ import {
import { companionPairingMode } from "../lib/phone-setup";
import { revealPhonePairing } from "../lib/phone-pairing";
import { PhonesList } from "./PhonesList";
-import { PairedPhones, relayStatusLine } from "./PairedPhones";
+import { PairedPhones, RelayLine } from "./PairedPhones";
import { ConnectionDetail } from "./ConnectionDetail";
import { Card, Switch } from "./SettingsPrimitives";
import { productName } from "../lib/brand";
@@ -241,7 +241,7 @@ export function CompanionSection({ profileEmail = "", focusRequest = 0 }: { prof
<div className="min-w-0">
<div className="text-[13px] text-ink">{t("remote.title")}</div>
<div className="mt-0.5 text-[12px] leading-relaxed text-ink-secondary">
- {relayStatusLine(state) ?? t("remote.toggleDetail")}
+ <RelayLine state={state} fallback={t("remote.toggleDetail")} />
</div>
</div>
<Switch
diff --git a/src/components/PairedPhones.test.ts b/src/components/PairedPhones.test.ts
index 0837ed109f..6a2c240583 100644
--- a/src/components/PairedPhones.test.ts
+++ b/src/components/PairedPhones.test.ts
@@ -2,7 +2,7 @@ import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vitest";
-import { PhoneRow, phoneSubtitle, relayStatusLine, sortPhones } from "./PairedPhones";
+import { CONNECTING_DELAY_MS, MIN_VISIBLE_MS, PhoneRow, phoneSubtitle, relayStatusLine, sortPhones, steadyAt } from "./PairedPhones";
import type { CompanionState, PhoneDevice, PhoneSetupController } from "./PhoneSetupFlow";
const NOW = Date.now();
@@ -45,6 +45,14 @@ describe("paired phones", () => {
expect(order).toEqual(["b", "a", "c"]);
});
+ it("shows Connecting only once it lasted 1.5 s, and keeps any line up at least 0.4 s", () => {
+ const shown = { text: "Reachable from anywhere · end-to-end encrypted", at: 1_000 };
+ expect(steadyAt(shown, 1_100, true)).toBe(1_100 + CONNECTING_DELAY_MS);
+ expect(steadyAt(shown, 1_100, false)).toBe(1_000 + MIN_VISIBLE_MS);
+ expect(steadyAt({ text: null, at: 0 }, 5_000, false)).toBe(5_000);
+ expect(steadyAt(shown, 9_000, false)).toBe(9_000);
+ });
+
it("names where phones can reach this computer from", () => {
const e2e = (relay: "connected" | "connecting" | "off") => ({ enabled: true, relay, node: null, descriptor: null });
expect(relayStatusLine(state())).toBeNull();
diff --git a/src/components/PairedPhones.tsx b/src/components/PairedPhones.tsx
index 678b5f5fb7..44b8a621f7 100644
--- a/src/components/PairedPhones.tsx
+++ b/src/components/PairedPhones.tsx
@@ -1,4 +1,4 @@
-import { useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { MoreHorizontal } from "lucide-react";
import { t } from "@/lib/i18n";
@@ -134,6 +134,32 @@ export function PairedPhones({ c, state }: { c: PhoneSetupController; state: Com
);
}
+/** "Connecting…" appears only once it has lasted this long, so a quick reconnect shows nothing. */
+export const CONNECTING_DELAY_MS = 1_500;
+/** A line, once shown, stays at least this long, so it never flickers. */
+export const MIN_VISIBLE_MS = 400;
+
+/** When `wanted` (wanted since `since`) may replace what is on screen. */
+export function steadyAt(shown: { text: string | null; at: number }, since: number, connecting: boolean): number {
+ return Math.max(connecting ? since + CONNECTING_DELAY_MS : since, shown.text === null ? 0 : shown.at + MIN_VISIBLE_MS);
+}
+
+/** The relay line with PAIRING-UX's timing; until anything shows, the section's own description. */
+export function RelayLine({ state, fallback }: { state: Pick<CompanionState, "enabled" | "e2e">; fallback: string }) {
+ const wanted = relayStatusLine(state);
+ const connecting = state.enabled && state.e2e?.enabled === true && state.e2e.relay === "connecting";
+ const since = useRef({ text: wanted, at: Date.now() });
+ if (since.current.text !== wanted) since.current = { text: wanted, at: Date.now() };
+ const [shown, setShown] = useState<{ text: string | null; at: number }>(() => ({ text: connecting ? null : wanted, at: Date.now() }));
+ useEffect(() => {
+ if (shown.text === wanted) return;
+ const wait = steadyAt(shown, since.current.at, connecting) - Date.now();
+ const timer = setTimeout(() => setShown({ text: wanted, at: Date.now() }), Math.max(0, wait));
+ return () => clearTimeout(timer);
+ }, [wanted, connecting, shown]);
+ return <>{shown.text ?? fallback}</>;
+}
+
/** The line under Remote access: where phones can reach this computer from, once it is end-to-end encrypted. */
export function relayStatusLine(state: Pick<CompanionState, "enabled" | "e2e">): string | null {
if (!state.enabled || state.e2e?.enabled !== true) return null;