Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.
import { afterEach, expect, test } from "bun:test";
import {
mkdirSync,
mkdtempSync,
readdirSync,
readFileSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createControl } from "../src/control";
import { createControlStore } from "../src/control-store";
const directories: string[] = [];
function fixture() {
const directory = mkdtempSync(join(tmpdir(), "control-store-"));
directories.push(directory);
const store = createControlStore(directory, "bot-1");
const control = createControl(undefined, { store });
return { directory, store, control };
}
afterEach(() => {
for (const directory of directories.splice(0))
rmSync(directory, { recursive: true, force: true });
});
test("atomic persisted completion remains exact across reload and later request", async () => {
const { store, control, directory } = fixture();
const first = control.requestHelp("Sign in", "call-1").request!;
await control.take(first.id);
control.release(first.id);
control.requestSecret({ ref: "e1", label: "private label" });
const restored = createControl(undefined, { store });
expect(restored.get(first.id).request?.status).toBe("completed");
expect(restored.get().resumeSnapshotRequired).toBe(true);
expect(restored.requestHelp("Replay", "call-1").request?.id).toBe(first.id);
restored.requestHelp("Next", "call-2");
expect(restored.get(first.id).request?.status).toBe("completed");
expect(restored.pendingSecret()).toBeNull();
expect(
readFileSync(join(directory, ".control", "bot-1.json"), "utf8"),
).not.toContain("private label");
expect(readdirSync(join(directory, ".control"))).toEqual(["bot-1.json"]);
});
test("a save that cannot replace the state leaves no temporary copy behind", () => {
const { directory, store } = fixture();
// A directory where the state file goes makes the final rename fail.
mkdirSync(join(directory, ".control", "bot-1.json"), { recursive: true });
expect(() =>
store.save({
version: 1,
holder: "bot",
since: new Date().toISOString(),
resumeSnapshotRequired: false,
recoveryRequired: false,
requests: [],
aliases: {},
}),
).toThrow();
expect(readdirSync(join(directory, ".control"))).toEqual(["bot-1.json"]);
});
test("reload interrupts active requests and retains idempotent identity", async () => {
for (const take of [false, true]) {
const { store, control } = fixture();
const first = control.requestHelp("Sign in", "call-1").request!;
if (take) await control.take(first.id);
const restored = createControl(undefined, { store });
expect(restored.get(first.id).request?.status).toBe("interrupted");
expect(restored.get().holder).toBe("bot");
expect(restored.requestHelp("Replay", "call-1").request?.id).toBe(first.id);
expect(() => restored.admitBotAction(true)).toThrow();
expect(
createControl(undefined, { store }).get(first.id).request?.status,
).toBe("interrupted");
}
});
test("malformed store fails loudly; Bot ids cannot escape the control directory", () => {
const { directory, control, store } = fixture();
control.requestHelp("Sign in");
const path = join(directory, ".control", "bot-1.json");
for (const malformed of ["{", JSON.stringify({ version: 1, requests: [] })]) {
writeFileSync(path, malformed);
expect(() => createControl(undefined, { store })).toThrow();
}
expect(() => createControlStore(directory, "../other")).toThrow();
});
test("history remains bounded and keeps recent terminal requests", () => {
const { control, store } = fixture();
let latest = "";
for (let index = 0; index < 50; index++) {
latest = control.requestHelp("Sign in", `call-${index}`).request!.id;
control.cancel(latest);
}
expect(store.load()!.requests.length).toBeLessThanOrEqual(33);
expect(store.load()!.aliases["call-49"]).toBe(latest);
});
test("a write failure never becomes an in-memory success or reopens actions", () => {
const failure = new Error("disk full");
const control = createControl(undefined, {
store: {
load: () => undefined,
save: () => {
throw failure;
},
},
});
expect(() => control.requestHelp("Sign in")).toThrow(/could not be saved/);
expect(() => control.get()).toThrow(/could not be saved/);
expect(() => control.admitBotAction()).toThrow(/could not be saved/);
});
test("opaque tool call ids cannot collide with object prototype names", () => {
const { control } = fixture();
for (const toolCallId of ["__proto__", "constructor", "toString"]) {
const request = control.requestHelp("Sign in", toolCallId).request!;
control.cancel(request.id);
expect(control.requestHelp("Replay", toolCallId).request?.id).toBe(
request.id,
);
}
});
test("who holds the wheel is saved with it, a state saved before it loads, and a restart lets them go", async () => {
const { store, control, directory } = fixture();
const first = control.requestHelp("Take control", "call-1").request!;
await control.take(first.id, "user-1");
expect(store.load()?.heldBy).toBe("user-1");
// The process starting again interrupts the request, and with it the person's hold.
const restarted = createControl(undefined, { store });
expect(restarted.get().holder).toBe("bot");
expect(restarted.get().heldBy).toBeUndefined();
expect(store.load()?.heldBy).toBeUndefined();
// Saved by a computer from before `heldBy`: loads, and names nobody.
const path = join(directory, ".control", "bot-1.json");
const saved = JSON.parse(readFileSync(path, "utf8")) as Record<
string,
unknown
>;
delete saved.heldBy;
writeFileSync(path, JSON.stringify(saved));
expect(createControl(undefined, { store }).get().heldBy).toBeUndefined();
// A name with nobody holding the wheel is not a state this computer writes.
writeFileSync(path, JSON.stringify({ ...saved, heldBy: "user-1" }));
expect(() => createControl(undefined, { store })).toThrow();
});