Add workspaces, computers, bot memory and the bots queue
- Schema: workspaces (+ workspace_bots, workspace_computers), containers, computers, bot_computers, bots.memory, workspaces.system_prompt, and bots_queue as a bun-sqlite isQueue table - Main computer (and its container record) ensured with the coordinator on signup, seed and /admin/bots; the coordinator's access to it is locked - Effective computer access: direct grants plus workspace computers - Full bot prompt: core instructions, workspace prompts, then memory; bots can rewrite their memory and system prompt - Queue helpers on the native BunSQLite.queue API, plus a handler that heartbeats, serializes per computer, recovers after restarts, reports lost jobs, supports cancelling and pushes outcomes over the WebSocket - Enable SQLite foreign keys at startup so schema cascades fire - Prune finished jobs daily Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import assertPromptLength from "./assert-prompt-length";
|
||||
|
||||
describe("assertPromptLength", () => {
|
||||
test("allows text up to the cap", () => {
|
||||
expect(() =>
|
||||
assertPromptLength({
|
||||
label: "Memory",
|
||||
text: "a".repeat(AppData.PromptMaxLength),
|
||||
}),
|
||||
).not.toThrow();
|
||||
expect(() => assertPromptLength({ label: "Memory" })).not.toThrow();
|
||||
});
|
||||
|
||||
test("rejects text over the cap", () => {
|
||||
expect(() =>
|
||||
assertPromptLength({
|
||||
label: "Memory",
|
||||
text: "a".repeat(AppData.PromptMaxLength + 1),
|
||||
}),
|
||||
).toThrow("Memory is too long");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
import { AppData } from "@/src/data/app-data";
|
||||
import AppError from "./app-error";
|
||||
|
||||
type Params = {
|
||||
label: string;
|
||||
text?: string | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Throws if a prompt or memory is longer than `AppData.PromptMaxLength`.
|
||||
*/
|
||||
export default function assertPromptLength({ label, text }: Params) {
|
||||
if ((text?.length || 0) > AppData.PromptMaxLength) {
|
||||
throw new AppError(
|
||||
`${label} is too long (max ${AppData.PromptMaxLength} characters).`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import composeBotPrompt from "./compose-bot-prompt";
|
||||
|
||||
describe("composeBotPrompt", () => {
|
||||
test("orders core instructions, workspaces, then memory", () => {
|
||||
expect(
|
||||
composeBotPrompt({
|
||||
system_prompt: "# You are Scout\n\nFind things.",
|
||||
memory: "- The user prefers short replies.",
|
||||
workspaces: [
|
||||
{ name: "Launch", system_prompt: "Ship by Friday." },
|
||||
{ name: "Ops", system_prompt: "Servers are on Debian." },
|
||||
],
|
||||
}),
|
||||
).toBe(
|
||||
[
|
||||
"# You are Scout\n\nFind things.",
|
||||
"# Workspace: Launch\n\nShip by Friday.",
|
||||
"# Workspace: Ops\n\nServers are on Debian.",
|
||||
"# Memory\n\n- The user prefers short replies.",
|
||||
].join("\n\n"),
|
||||
);
|
||||
});
|
||||
|
||||
test("leaves out empty parts", () => {
|
||||
expect(
|
||||
composeBotPrompt({
|
||||
system_prompt: "Core.",
|
||||
memory: " ",
|
||||
workspaces: [
|
||||
{ name: "Empty", system_prompt: "" },
|
||||
{ name: null, system_prompt: "Shared." },
|
||||
],
|
||||
}),
|
||||
).toBe("Core.\n\n# Workspace: Untitled\n\nShared.");
|
||||
expect(composeBotPrompt({})).toBe("");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
type WorkspacePrompt = {
|
||||
name?: string | null;
|
||||
system_prompt?: string | null;
|
||||
};
|
||||
|
||||
type Params = {
|
||||
system_prompt?: string | null;
|
||||
memory?: string | null;
|
||||
workspaces?: WorkspacePrompt[];
|
||||
};
|
||||
|
||||
/**
|
||||
* The full prompt a bot runs with (markdown): its own core instructions,
|
||||
* then the shared context of each workspace it's in, then its memory.
|
||||
* Empty parts are left out.
|
||||
*/
|
||||
export default function composeBotPrompt({
|
||||
system_prompt,
|
||||
memory,
|
||||
workspaces = [],
|
||||
}: Params) {
|
||||
const parts: string[] = [];
|
||||
|
||||
if (system_prompt?.trim()) {
|
||||
parts.push(system_prompt.trim());
|
||||
}
|
||||
|
||||
for (const workspace of workspaces) {
|
||||
if (!workspace.system_prompt?.trim()) continue;
|
||||
|
||||
parts.push(
|
||||
`# Workspace: ${workspace.name?.trim() || "Untitled"}\n\n${workspace.system_prompt.trim()}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (memory?.trim()) {
|
||||
parts.push(`# Memory\n\n${memory.trim()}`);
|
||||
}
|
||||
|
||||
return parts.join("\n\n");
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import createKeyLock from "./create-key-lock";
|
||||
|
||||
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
describe("createKeyLock", () => {
|
||||
test("runs tasks with the same key one at a time, in order", async () => {
|
||||
const lock = createKeyLock();
|
||||
const events: string[] = [];
|
||||
|
||||
const job = (name: string, ms: number) =>
|
||||
lock.run({
|
||||
key: 1,
|
||||
task: async () => {
|
||||
events.push(`${name}:start`);
|
||||
await wait(ms);
|
||||
events.push(`${name}:end`);
|
||||
return name;
|
||||
},
|
||||
});
|
||||
|
||||
const results = await Promise.all([job("a", 20), job("b", 1)]);
|
||||
|
||||
expect(results).toEqual(["a", "b"]);
|
||||
expect(events).toEqual(["a:start", "a:end", "b:start", "b:end"]);
|
||||
expect(lock.size()).toBe(0);
|
||||
});
|
||||
|
||||
test("runs different keys side by side", async () => {
|
||||
const lock = createKeyLock();
|
||||
const events: string[] = [];
|
||||
|
||||
await Promise.all([
|
||||
lock.run({
|
||||
key: "x",
|
||||
task: async () => {
|
||||
events.push("x:start");
|
||||
await wait(20);
|
||||
events.push("x:end");
|
||||
},
|
||||
}),
|
||||
lock.run({
|
||||
key: "y",
|
||||
task: async () => {
|
||||
events.push("y:start");
|
||||
events.push("y:end");
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
expect(events.indexOf("y:end")).toBeLessThan(events.indexOf("x:end"));
|
||||
});
|
||||
|
||||
test("a failing task doesn't block the next one", async () => {
|
||||
const lock = createKeyLock();
|
||||
|
||||
const failed = lock.run({
|
||||
key: 1,
|
||||
task: async () => {
|
||||
throw new Error("boom");
|
||||
},
|
||||
});
|
||||
const next = lock.run({ key: 1, task: async () => "ok" });
|
||||
|
||||
expect(failed).rejects.toThrow("boom");
|
||||
expect(await next).toBe("ok");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
type RunParams<T> = {
|
||||
key: string | number;
|
||||
task: () => Promise<T>;
|
||||
};
|
||||
|
||||
/**
|
||||
* An in-process lock per key: tasks with the same key run one at a time in
|
||||
* call order, tasks with different keys run side by side. Used so two jobs
|
||||
* never drive the same computer at once.
|
||||
*/
|
||||
export default function createKeyLock() {
|
||||
const tails = new Map<string, Promise<unknown>>();
|
||||
|
||||
async function run<T>({ key, task }: RunParams<T>): Promise<T> {
|
||||
const id = String(key);
|
||||
const previous = tails.get(id) || Promise.resolve();
|
||||
const current = previous.catch(() => {}).then(task);
|
||||
const tail = current.catch(() => {});
|
||||
|
||||
tails.set(id, tail);
|
||||
|
||||
try {
|
||||
return await current;
|
||||
} finally {
|
||||
if (tails.get(id) === tail) {
|
||||
tails.delete(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { run, size: () => tails.size };
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import isLockedComputerAccess from "./is-locked-computer-access";
|
||||
|
||||
describe("isLockedComputerAccess", () => {
|
||||
test("locks the coordinator to the main computer", () => {
|
||||
expect(
|
||||
isLockedComputerAccess({
|
||||
bot_kind: "coordinator",
|
||||
computer_kind: "main",
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
test("leaves every other pair removable", () => {
|
||||
expect(
|
||||
isLockedComputerAccess({ bot_kind: "worker", computer_kind: "main" }),
|
||||
).toBe(false);
|
||||
expect(
|
||||
isLockedComputerAccess({
|
||||
bot_kind: "coordinator",
|
||||
computer_kind: "sandbox",
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(
|
||||
isLockedComputerAccess({
|
||||
bot_kind: "worker",
|
||||
computer_kind: "sandbox",
|
||||
}),
|
||||
).toBe(false);
|
||||
expect(isLockedComputerAccess({})).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
type Params = {
|
||||
bot_kind?: string | null;
|
||||
computer_kind?: string | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* The coordinator always has the main computer. That one access is granted
|
||||
* with the account and can never be removed; every other bot/computer pair
|
||||
* can be added and removed freely.
|
||||
*/
|
||||
export default function isLockedComputerAccess({
|
||||
bot_kind,
|
||||
computer_kind,
|
||||
}: Params) {
|
||||
return bot_kind == "coordinator" && computer_kind == "main";
|
||||
}
|
||||
Reference in New Issue
Block a user