Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Task workspaces' dependency sweep: held off unless OMB_TASK_WORKSPACE_RETENTION is on (#7192)
Milind Soni40afb78d11e4
diff --git a/server/bot-box/env.test.ts b/server/bot-box/env.test.tsindex 6185f95570..c3b601b136 100644--- a/server/bot-box/env.test.ts+++ b/server/bot-box/env.test.ts@@ -8,7 +8,7 @@ import { BOX_ENV_NAMES, BOX_ENV_NEVER, BOX_ENV_OMB_NAMES, BOX_ENV_OMB_PREFIXES,* ending in `_` is a family (`OMB_CLOUD_*`). */const SERVER_ONLY_OMB: ReadonlySet<string> = new Set(["OMB_ACP_LIMIT_QUIET_MS", "OMB_ACP_NO_SIGNS_STOP_MS", "OMB_ACP_PLAN_LINES", "OMB_ACP_QUIET_NOTICE_MS",- "OMB_ACP_QUIET_TICK_MS", "OMB_ACP_SESSION_IDLE_", "OMB_ACP_SHARED_RECYCLE_MS", "OMB_ADMIN_", "OMB_ADMIN_MEMBERSHIP",+ "OMB_ACP_QUIET_TICK_MS", "OMB_TASK_WORKSPACE_RETENTION", "OMB_ACP_SESSION_IDLE_", "OMB_ACP_SHARED_RECYCLE_MS", "OMB_ADMIN_", "OMB_ADMIN_MEMBERSHIP","OMB_ADMIN_URL", "OMB_ADMIN_WORKSPACE", "OMB_AGENT_BROWSER_PATH", "OMB_AI_CHECKS", "OMB_ANTHROPIC_API_KEY", "OMB_ANTIGRAVITY_NATIVE_CLI","OMB_ANTHROPIC_API_URL", "OMB_APP_VERSION", "OMB_ATTACHMENTS_MAX_BYTES", "OMB_AZURE_OPENAI_LIVE_DEPLOYMENT","OMB_AZURE_OPENAI_LIVE_ENDPOINT", "OMB_BACKGROUND_TEXT_MAX", "OMB_BENCH", "OMB_BOAT_API", "OMB_BOTS_FULL_SAVE", "OMB_BOTS_SAVE_CHECK", "OMB_BOT_BOX", "OMB_BOT_MEMORY_MAX_MB", "OMB_BOX_CANARY", "OMB_BRAND_FILE",diff --git a/server/cloud-home-start.ts b/server/cloud-home-start.tsindex 805a957207..c04a6001d4 100644--- a/server/cloud-home-start.ts+++ b/server/cloud-home-start.ts@@ -44,6 +44,8 @@ const SERVER_ENV_NAMES = new Set(["OMB_BOT_BOX",// An ended Cloud started only for its owner's export: the server runs frozen (server/move-freeze.ts)."OMB_CLOUD_EXPORT_ONLY",+ // The idle task workspaces' dependency sweep (server/host/start-upkeep.ts), off unless set: the Admin may turn it on.+ "OMB_TASK_WORKSPACE_RETENTION",]);export function serverEnvironmentAllowed(name: string): boolean {return SERVER_ENV_NAMES.has(name) || name.startsWith("LC_");diff --git a/server/host/start-upkeep.test.ts b/server/host/start-upkeep.test.tsindex d961176766..a60d29adaa 100644--- a/server/host/start-upkeep.test.ts+++ b/server/host/start-upkeep.test.ts@@ -1,7 +1,7 @@import { afterEach, describe, expect, it, vi } from "vitest";import type { TaskRecord } from "../store.ts";-import { startUpkeep, THREAD_LOG_RETENTION_SWEEP_MS } from "./start-upkeep.ts";+import { startUpkeep, taskWorkspaceRetentionOn, THREAD_LOG_RETENTION_SWEEP_MS } from "./start-upkeep.ts";const task = (threadId: string, extra: Partial<TaskRecord> = {}) => ({ threadId, closedBy: { at: 1 }, archivedAt: null, unread: false, ...extra }) as TaskRecord;@@ -11,6 +11,30 @@ afterEach(() => {});describe("start-up upkeep", () => {+ it("holds the task workspaces' sweep off unless OMB_TASK_WORKSPACE_RETENTION is on", async () => {+ expect(taskWorkspaceRetentionOn({})).toBe(false);+ expect(taskWorkspaceRetentionOn({ OMB_TASK_WORKSPACE_RETENTION: "0" })).toBe(false);+ expect(taskWorkspaceRetentionOn({ OMB_TASK_WORKSPACE_RETENTION: "off" })).toBe(false);+ expect(taskWorkspaceRetentionOn({ OMB_TASK_WORKSPACE_RETENTION: "remove" })).toBe(false);+ for (const value of ["1", "on", " TRUE ", "yes"]) expect(taskWorkspaceRetentionOn({ OMB_TASK_WORKSPACE_RETENTION: value })).toBe(true);+ vi.useFakeTimers();+ vi.stubEnv("OMB_TASK_WORKSPACE_RETENTION", "");+ const calls: string[] = [];+ startUpkeep({+ bots: () => [{ id: "bot", tasks: [task("idle")] }],+ retentionDays: () => 30,+ busy: () => false,+ openDirectHandoff: () => false,+ cleanupPartials: () => 0,+ sweepLogs: () => { calls.push("logs"); return 0; },+ sweepWorkspaces: () => { calls.push("workspaces"); return []; },+ });+ await vi.advanceTimersByTimeAsync(0);+ await vi.advanceTimersByTimeAsync(THREAD_LOG_RETENTION_SWEEP_MS);+ expect(calls).toEqual(["logs", "logs"]);+ vi.unstubAllEnvs();+ });+it("runs its first pass once the server answers, not before, then the logs' sweep once a day", async () => {vi.useFakeTimers();const calls: string[] = [];@@ -20,6 +44,7 @@ describe("start-up upkeep", () => {busy: (_botId, threadId) => threadId === "working",openDirectHandoff: (threadId) => threadId === "quiet",cleanupPartials: () => { calls.push("partials"); return 0; },+ workspaceRetention: () => true,sweepWorkspaces: () => { calls.push("workspaces"); return []; },sweepLogs: (candidates, days) => {calls.push(`logs ${days}: ${[...candidates].map((c) => `${c.threadId}${c.busy ? " busy" : ""}${c.openDirectHandoff ? " handoff" : ""}${c.unread ? " unread" : ""}`).join(", ")}`);@@ -48,6 +73,7 @@ describe("start-up upkeep", () => {openDirectHandoff: () => false,cleanupPartials: () => { throw new Error("disk gone"); },sweepLogs: (_candidates, days) => { swept.push(days); return 0; },+ workspaceRetention: () => true,sweepWorkspaces: () => [],});await vi.advanceTimersByTimeAsync(0);@@ -58,7 +84,7 @@ describe("start-up upkeep", () => {expect(swept).toEqual([7]);});- it("sweeps idle task workspaces even without a log retention, and keeps going when the logs' sweep fails", async () => {+ it("with the switch on, sweeps idle task workspaces even without a log retention, and keeps going when the logs' sweep fails", async () => {vi.useFakeTimers();const warn = vi.spyOn(console, "warn").mockImplementation(() => {});const seen: string[] = [];@@ -69,6 +95,7 @@ describe("start-up upkeep", () => {openDirectHandoff: () => false,cleanupPartials: () => 0,sweepLogs: () => { throw new Error("logs gone"); },+ workspaceRetention: () => true,sweepWorkspaces: (candidates) => {seen.push(...[...candidates].map((c) => `${c.botId}/${c.threadId} ${c.lastActiveAt}${c.busy ? " busy" : ""}`));return [];diff --git a/server/host/start-upkeep.ts b/server/host/start-upkeep.tsindex e5941e4e83..7d34353c28 100644--- a/server/host/start-upkeep.ts+++ b/server/host/start-upkeep.ts@@ -1,12 +1,21 @@// The attachments', the thread logs' and the task workspaces' upkeep. All are safe beside requests: an upload in// flight keeps its partial and its reservation, and a busy, unread or handed-off thread keeps its log and its// workspace. So the first pass runs once the server answers, not before it listens, and the sweeps then run once a day.+// The task workspaces' sweep is held off unless OMB_TASK_WORKSPACE_RETENTION is on: it deletes any node_modules and+// any venv outright, without checking that a package manager made them, so a venv holding the person's own script or+// a hand-written node_modules would go, and on a desktop a task workspace can be a project the person also works in.import { cleanupStaleAttachmentPartials } from "../attachments.ts";import type { BotRecord } from "../store.ts";import { sweepTaskWorkspaceBuildFolders } from "../task-workspace-retention.ts";import { sweepThreadEventLogs } from "../thread-retention.ts";export const THREAD_LOG_RETENTION_SWEEP_MS = 24 * 60 * 60 * 1000;+export const TASK_WORKSPACE_RETENTION_ENV = "OMB_TASK_WORKSPACE_RETENTION";++/** The task workspaces' sweep runs only when OMB_TASK_WORKSPACE_RETENTION is 1, on, true or yes. */+export function taskWorkspaceRetentionOn(env: NodeJS.ProcessEnv = process.env): boolean {+ return ["1", "on", "true", "yes"].includes(env[TASK_WORKSPACE_RETENTION_ENV]?.trim().toLowerCase() ?? "");+}export interface StartUpkeepDeps {bots: () => ReadonlyArray<Pick<BotRecord, "id" | "tasks">>;@@ -16,6 +25,8 @@ export interface StartUpkeepDeps {cleanupPartials?: () => number;sweepLogs?: typeof sweepThreadEventLogs;sweepWorkspaces?: (candidates: Parameters<typeof sweepTaskWorkspaceBuildFolders>[0]) => string[] | Promise<string[]>;+ /** Whether the task workspaces' sweep runs; taskWorkspaceRetentionOn by default (off). */+ workspaceRetention?: () => boolean;}const reason = (error: unknown) => error instanceof Error ? error.message : String(error);@@ -52,7 +63,7 @@ export function startUpkeep(deps: StartUpkeepDeps): void {}let sweepingWorkspaces = false;const sweepWorkspacesNow = async () => {- if (sweepingWorkspaces) return;+ if (sweepingWorkspaces || !(deps.workspaceRetention ?? taskWorkspaceRetentionOn)()) return;sweepingWorkspaces = true;try {const removed = await (deps.sweepWorkspaces ?? ((c) => sweepTaskWorkspaceBuildFolders(c)))(workspaceCandidates());