Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(phones): recheck job grants and ownership
Sebastian B00f2725c4c0c
diff --git a/server/phones/host-revocation.test.ts b/server/phones/host-revocation.test.tsnew file mode 100644index 0000000000..79c9cb9b04--- /dev/null+++ b/server/phones/host-revocation.test.ts@@ -0,0 +1,92 @@+import { mkdtempSync } from "node:fs";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { afterEach, describe, expect, it } from "vitest";++import { flushDecisionLog } from "../decision-log.ts";+import type { BotRecord } from "../store.ts";+import { removeTempDir } from "../testing/cleanup.ts";+import { createPhoneHost } from "./host.ts";+import { PhoneJobs } from "./jobs.ts";+import { PhoneRegistry, type PhoneRegistration } from "./registry.ts";++const KEY = "session:phone-1";+const TURN = { botId: "helper", threadId: "thread-1", generation: "gen-1" };+const registration: PhoneRegistration = {+ installId: "install-0001", name: "Fixture phone", platform: "ios", appVersion: "1.0.0",+ timeZone: "UTC", capabilities: { calendar: "full" },+};+const cleanups: Array<() => Promise<void>> = [];+afterEach(async () => { for (const cleanup of cleanups.splice(0)) await cleanup(); });++function fixture() {+ const dir = mkdtempSync(join(tmpdir(), "phone-host-revocation-"));+ const registry = new PhoneRegistry(join(dir, "phones.json"));+ const jobs = new PhoneJobs();+ cleanups.push(async () => { jobs.close(); await flushDecisionLog(dir); await removeTempDir(dir); });+ registry.register(KEY, "owner", registration);+ const state = {+ bot: { id: "helper", name: "Helper", phoneApps: { calendar: "write" } } as BotRecord | null,+ owner: "owner" as string | null,+ };+ const messages: unknown[] = [];+ const host = createPhoneHost({+ registry, jobs, dataDir: dir, bot: () => state.bot, turnOwner: () => state.owner,+ inRoom: () => false, appendMessage: (_thread, message) => { messages.push(message); }, active: () => true,+ });+ const request = () => host.handle({ method: "tools/call", params: { name: "PHONE_DELETE_EVENT", arguments: { id: "event-1" } } }, TURN, () => {});+ return { registry, jobs, state, messages, request };+}++type Fixture = ReturnType<typeof fixture>;+const changes: Array<[string, (h: Fixture) => void]> = [+ ["the bot's calendar grant is removed", h => { h.state.bot!.phoneApps = {}; }],+ ["the bot's calendar grant becomes read only", h => { h.state.bot!.phoneApps = { calendar: "read" }; }],+ ["the bot is deleted", h => { h.state.bot = null; }],+ ["the turn loses ownership", h => { h.state.owner = null; }],+ ["the turn changes owner", h => { h.state.owner = "another-owner"; }],+ ["the selected phone changes owner", h => { h.registry.register(KEY, "another-owner", registration); }],+ ["the selected phone loses Calendar", h => { h.registry.register(KEY, "owner", { ...registration, capabilities: {} }); }],+ ["the selected phone is removed", h => { h.registry.remove(KEY); }],+];++describe.each(changes)("phone job revocation when %s", (_name, revoke) => {+ it("does not dequeue the queued deletion", async () => {+ const h = fixture();+ const blocking = h.jobs.request(KEY, "PHONE_LIST_EVENTS", {}, { active: () => true, timeoutMs: 45_000, offlineText: "offline", online: () => true });+ const first = (await h.jobs.poll(KEY))!;+ expect(h.jobs.lease(KEY, first.id)).toBe(true);+ const pending = h.request();+ revoke(h);+ h.jobs.complete(KEY, first.id, { ok: true, result: { events: [] } });+ await blocking;+ const gone = new AbortController();+ const poll = h.jobs.poll(KEY, gone.signal);+ gone.abort();+ expect(await poll).toBeNull();+ expect(await pending).toMatchObject({ isError: true });+ expect(h.messages).toEqual([]);+ });++ it("refuses a lease for the delivered deletion", async () => {+ const h = fixture();+ const pending = h.request();+ const job = (await h.jobs.poll(KEY))!;+ revoke(h);+ expect(h.jobs.lease(KEY, job.id)).toBe(false);+ expect(await pending).toMatchObject({ isError: true });+ expect(h.messages).toEqual([]);+ });++ it("refuses a result for the leased deletion", async () => {+ const h = fixture();+ const pending = h.request();+ const job = (await h.jobs.poll(KEY))!;+ expect(h.jobs.lease(KEY, job.id)).toBe(true);+ revoke(h);+ expect(() => h.jobs.complete(KEY, job.id, { ok: true, result: { deleted: true } })).toThrow(/expired/);+ expect(await pending).toMatchObject({ isError: true });+ expect(h.messages).toEqual([]);+ expect(h.registry.get(KEY)?.lastUsedAt ?? null).toBeNull();+ });+});diff --git a/server/phones/host.ts b/server/phones/host.tsindex 6ad6b46b9b..b47fad85b3 100644--- a/server/phones/host.ts+++ b/server/phones/host.ts@@ -94,7 +94,6 @@ export function createPhoneHost(deps: PhoneHostDeps): PhoneHost {withheld: (bot, owner) => (owner !== null && mounts(bot, owner) ? phoneWithheldPrompt(bot, phonesOf(owner).map(({ registration }) => registration)) : ""),toolListing: () => (registry.list(null, () => false).length === 0 ? null : { phone: PHONE_TOOLS.map(({ name, description }) => ({ name, description })) }),async handle(body, capability, assertActive) {- const active = () => deps.active(capability);return phoneRpc(body as { method?: unknown; params?: unknown }, {assertActive,// Re-read the live bot and the turn's owner on every call, so a permission change wins over a request@@ -127,6 +126,13 @@ export function createPhoneHost(deps: PhoneHostDeps): PhoneHost {return allows(match, phoneToolCapability(name)) ? target(match) : notAllowed(match.registration.name, name);},run: async (phone, name, args) => {+ const owner = deps.turnOwner(capability);+ const active = () => {+ if (!deps.active(capability) || owner === null || deps.turnOwner(capability) !== owner) return false;+ const bot = deps.bot(capability.botId);+ const current = registry.get(phone.key);+ return Boolean(bot && current && current.owner === owner && phoneAccess(bot, current.registration.capabilities, name).ok);+ };const outcome = await jobs.request(phone.key, name, args, {active, timeoutMs: isOutboundTool(name) ? PHONE_SEND_TIMEOUT_MS : PHONE_JOB_TIMEOUT_MS,offlineText: `${phone.name} is offline. Open the MausBot app on it.`, online: () => registry.online(phone.key),