Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(desktop): Refuse an asleep phone removal while another companion holds the store
AdityaUmale51cb6540703f
diff --git a/electron/companion-revoke.node-test.mjs b/electron/companion-revoke.node-test.mjsindex d8086a2e5b..ec3796d325 100644--- a/electron/companion-revoke.node-test.mjs+++ b/electron/companion-revoke.node-test.mjs@@ -4,7 +4,7 @@ import test from "node:test";import vm from "node:vm";/** companionRevoke alone, with the sidecar, its store and the transition queue stubbed. */-function revokeWith({ running, removeAtRest = () => true, control = async () => ({}) }) {+function revokeWith({ running, removeAtRest = () => true, control = async () => ({}), portServed = false }) {const source = readFileSync(new URL("./companion.mjs", import.meta.url), "utf8");const start = source.indexOf("export async function companionRevoke");const end = source.indexOf("\n}\n", start);@@ -12,6 +12,8 @@ function revokeWith({ running, removeAtRest = () => true, control = async () =>const calls = [];const context = vm.createContext({proc: running ? {} : null,+ CONTROL_PORT: 8811,+ controlPortServed: async () => { calls.push(["port probe"]); return portServed; },serialize: (work) => { calls.push(["serialize"]); return Promise.resolve().then(work); },removePairedDeviceAtRest: (id) => { calls.push(["at rest", id]); return removeAtRest(id); },control: async (...args) => { calls.push(args); return control(...args); },@@ -25,7 +27,13 @@ function revokeWith({ running, removeAtRest = () => true, control = async () =>test("Remove with the phone link asleep takes the phone out of the store it loads at start", async () => {const { revoke, calls } = revokeWith({ running: false });await revoke("phone-1");- assert.deepEqual(calls, [["serialize"], ["at rest", "phone-1"]]);+ assert.deepEqual(calls, [["serialize"], ["port probe"], ["at rest", "phone-1"]]);+});++test("asleep, a companion this app did not start keeps the phone: the removal is refused, the store untouched", async () => {+ const { revoke, calls } = revokeWith({ running: false, portServed: true });+ await assert.rejects(revoke("phone-1"), /already serving another companion/);+ assert.deepEqual(calls, [["serialize"], ["port probe"]]);});test("a removal that could not be saved while asleep is an error, never a quiet success", async () => {diff --git a/electron/companion.mjs b/electron/companion.mjsindex 0df370a287..ddfe98d70e 100644--- a/electron/companion.mjs+++ b/electron/companion.mjs@@ -433,12 +433,29 @@ export async function companionPairing(open, expectedToken) {}}+/** True when something answers the control port; only a refused connection means no companion is there. */+async function controlPortServed() {+ try {+ await fetch(`http://127.0.0.1:${CONTROL_PORT}/state`, { signal: AbortSignal.timeout(4_000) });+ return true;+ } catch (error) {+ return error?.cause?.code !== "ECONNREFUSED";+ }+}+export async function companionRevoke(deviceId) {// the id came from the renderer, so it does not get to shape a pathif (!/^[\w-]{1,64}$/.test(String(deviceId ?? ""))) return companionState();// Asleep, nothing listens: the phone leaves the store the sidecar loads when it starts. In the transition queue, so// a start never loads the store halfway through; a start that got there first takes the removal itself.- if (!proc && await serialize(() => (proc ? false : (removePairedDeviceAtRest(deviceId), true)))) return companionState();+ const removedAtRest = !proc && await serialize(async () => {+ if (proc) return false;+ // A companion this app did not start keeps its phones in memory and would save the removed one back.+ if (await controlPortServed()) throw new Error(`port ${CONTROL_PORT} is already serving another companion — stop it and try again`);+ removePairedDeviceAtRest(deviceId);+ return true;+ });+ if (removedAtRest) return companionState();// A removal that failed is an error on the page, never a quiet "Removed".await control("DELETE", `/devices/${deviceId}`);e2eHooks.changed();