Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Routines: say why a changed routine can't use the lent Mac, and how to fix it
AdityaUmalec8f6e38a9585
diff --git a/server/cloud-lending.ts b/server/cloud-lending.tsindex ec93ed62c9..1ffaa1c07e 100644--- a/server/cloud-lending.ts+++ b/server/cloud-lending.ts@@ -68,6 +68,11 @@ export function cloudLendingRefusalText(why: CloudLendingRefusal | "memory-changreturn `Only conversations you start with a bot, and routines you wrote, can use ${yours}. Start a new conversation with this bot to use it.`;}+/** A routine's run that someone else's change made no longer the owner's: saving it again in its editor renews it. */+export function changedRoutineRefusalText(name: string, device: "Mac" | "PC" | "computer"): string {+ return `"${name}" was changed outside your own chats, so it can't use your ${device}. Open it and save it to make it yours again.`;+}+/** On a team nobody's own computer is lent: the team's bots use the team's cloud computer. */export function teamLendingRefusal(team: string): string {return `${team}'s bots never use anyone's own computer; they use ${team}'s cloud computer.`;diff --git a/server/host/cloud-lending-turns.test.ts b/server/host/cloud-lending-turns.test.tsindex 01cff4d820..ba46fc18f7 100644--- a/server/host/cloud-lending-turns.test.ts+++ b/server/host/cloud-lending-turns.test.ts@@ -188,3 +188,47 @@ describe("a card lifts a lock only for the owner's own line (server/host/cloud-lexpect(withLifts([webhookPost, own, allowed]).host.cloudLendingView(turn)).toBe(false);});});++describe("a routine someone else changed (email 1a1bca40)", () => {+ const routinePrompt = { id: "m1", role: "user", automation: { routineId: "r1" } };+ function routineRun({ trigger = "schedule" as "schedule" | "manual" | "webhook" | "startup", ownerStarted = false, ownerAuthored = false, flagged = false } = {}) {+ const run = { id: "run1", routineId: "r1", threadId: "t1", triggerSource: trigger };+ return createCloudLendingTurns({+ store: {+ groupByThread: () => undefined,+ messagesFor: () => [routinePrompt],+ botByThread: () => ({ id: "b1" }),+ taskByThread: () => ({ threadId: "t1", routineRunId: "run1" }),+ } as never,+ cloudHome: true,+ directRequest: () => ({ messageId: "m1", generations: new Set(["g1"]), automation: trigger }),+ threadStarter: () => undefined,+ ownerPerson: (person) => person === OWNER,+ cardAnswerer: () => undefined,+ activeRoutineRun: () => run as never,+ ownerStartedRun: () => ownerStarted,+ routineRunIsOwners: () => ownerAuthored,+ memoryChangedBySomeoneElse: () => flagged,+ routines: () => ({ listRoutines: () => [{ id: "r1", name: "Morning inbox" }] }) as never,+ routineAuthors: { authored: () => ownerAuthored },+ lentPlatform: () => "darwin",+ });+ }+ const changed = "\"Morning inbox\" was changed outside your own chats, so it can't use your Mac. Open it and save it to make it yours again.";++ it("its scheduled run is told, by name, how to make it theirs again", () => {+ const host = routineRun();+ expect(host.cloudLendingRefusal(turn)).toBe("not-owner");+ expect(host.cloudLendingRefusalNotice(turn)).toBe(changed);+ expect(host.cloudLendingRefusalForBot(turn)).toBe(`${changed} ${ONLY_THE_PERSON}`);+ expect(routineRun({ trigger: "startup" }).cloudLendingRefusalNotice(turn)).toBe(changed);+ expect(routineRun({ trigger: "manual", ownerStarted: true }).cloudLendingRefusalNotice(turn)).toBe(changed);+ });+ it("the owner's own routine still runs, and every other refusal keeps its words", () => {+ expect(routineRun({ ownerAuthored: true }).cloudLendingRefusalNotice(turn)).toBeNull();+ for (const host of [routineRun({ trigger: "webhook" }), routineRun({ trigger: "manual" }), routineRun({ flagged: true })]) {+ expect(host.cloudLendingRefusalNotice(turn)).not.toBeNull();+ expect(host.cloudLendingRefusalNotice(turn)).not.toContain("was changed outside your own chats");+ }+ });+});diff --git a/server/host/cloud-lending-turns.ts b/server/host/cloud-lending-turns.tsindex 35247ea427..0231adc648 100644--- a/server/host/cloud-lending-turns.ts+++ b/server/host/cloud-lending-turns.ts@@ -1,7 +1,7 @@// Which turns on a Cloud home may use the Mac lent to it, read from the harness's own records (server/cloud-lending.ts// decides), and the plain words a turn that can't is told. A room member turn acts for the person's line that started// its run, for that turn generation alone, while the run lasts.-import { answersOwnersLine, cloudHomeLendingRefusal, cloudLendingRefusalText, ON_THE_CARD, ONLY_THE_PERSON, teamLendingRefusal, type CloudLendingRefusal, type CloudLendingTurn } from "../cloud-lending.ts";+import { answersOwnersLine, changedRoutineRefusalText, cloudHomeLendingRefusal, cloudLendingRefusalText, ON_THE_CARD, ONLY_THE_PERSON, teamLendingRefusal, type CloudLendingRefusal, type CloudLendingTurn } from "../cloud-lending.ts";import { unstamped } from "../cloud-owner-lines.ts";import { deviceWord } from "../lent-computers.ts";import type { Routine, RoutineManager, RoutineRun } from "../routines.ts";@@ -105,6 +105,8 @@ export function createCloudLendingTurns(deps: CloudLendingTurnsDeps) {const { conversation, memoryChanged } = shut;if (!conversation && !memoryChanged) return null;if (deps.team) return teamLendingRefusal(deps.team);+ const changed = conversation === "not-owner" && !memoryChanged ? changedRoutine(capability) : undefined;+ if (changed !== undefined) return changedRoutineRefusalText(changed, deviceWord(deps.lentPlatform()));const thread = store.messagesFor(capability.threadId);const others = thread.filter((line) => line.role === "user" && !deps.ownerPerson(line.peerAsk ? undefined : line.sender?.id));return cloudLendingRefusalText(conversation ?? "memory-changed", { room: Boolean(store.groupByThread(capability.threadId)), fromBefore: others.length > 0 && others.every(unstamped),@@ -126,6 +128,18 @@ export function createCloudLendingTurns(deps: CloudLendingTurnsDeps) {return conversation;}+ /** The name of the routine this turn runs, when the run is refused only because the routine is no longer the owner's+ * (someone else changed it); a webhook's or another app's run, or one somebody else started by hand, is not this. */+ function changedRoutine(capability: Turn): string | undefined {+ const turn = cloudLendingTurn(capability);+ const automation = turn.request?.automation;+ if (automation === undefined || automation === "webhook" || automation === "api") return undefined;+ const run = turn.routineRun();+ if (!run || run.ownerAuthored || run.triggerSource === "webhook" || run.triggerSource === "api" || (run.triggerSource === "manual" && !run.ownerStarted)) return undefined;+ const routineId = deps.activeRoutineRun(capability.threadId)?.routineId;+ return deps.routines()?.listRoutines().find((routine) => routine.id === routineId)?.name;+ }+/** A bot opened this conversation (a hand-off's work thread, one a bot started itself, a Chief's notices). */function cloudBotOpened(threadId: string): boolean {const bot = store.botByThread(threadId);