Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Roll back on thrown skill writes; Undo re-checks teammate access
AdityaUmalea2b415fb2ab5
diff --git a/server/host/teammate-skills-host.test.ts b/server/host/teammate-skills-host.test.tsindex 61298e9bda..d953f7d023 100644--- a/server/host/teammate-skills-host.test.ts+++ b/server/host/teammate-skills-host.test.ts@@ -22,7 +22,7 @@ afterEach(async () => {type Skill = { kind: SwitchableSkill["kind"]; enabled: boolean; text: string };-function setup(options: { chief?: boolean; refuse?: string[]; failTurn?: string; failBack?: string; room?: boolean } = {}) {+function setup(options: { chief?: boolean; refuse?: string[]; failTurn?: string; throwTurn?: string; failBack?: string; room?: boolean } = {}) {const dataDir = mkdtempSync(join(tmpdir(), "teammate-skills-host-"));dirs.push(dataDir);const bots: Record<string, BotRecord> = {@@ -55,6 +55,7 @@ function setup(options: { chief?: boolean; refuse?: string[]; failTurn?: string;},turn: (botId, name, on) => {if (options.failTurn === `${botId}/${name}`) return { error: "disk full" };+ if (options.throwTurn === `${botId}/${name}`) throw Object.assign(new Error("ENOSPC: no space left on device, open '/data/skills/lint.md'"), { code: "ENOSPC" });if (options.failBack === `${botId}/${name}` && turns.includes(`${botId}/${name}=${!on}`)) return { error: "files changed" };skills.get(`${botId}/${name}`)!.enabled = on;turns.push(`${botId}/${name}=${on}`);@@ -143,6 +144,17 @@ describe("a Chief of Staff changing its teammates' skills", () => {expect(t.messages).toEqual([]);});+ it("puts back the ones already turned when a later one throws", () => {+ const t = setup({ throwTurn: "coder/lint" });+ const result = t.host.stage(t.bots.chief!, "t1", both, "self");+ expect(result).toMatchObject({ status: 422, body: { state: "failed" } });+ expect(result.body.error).toContain("could not save the change (ENOSPC)");+ expect(result.body.error).not.toContain("/data/");+ expect(result.body.error).toContain("Nothing was changed");+ expect(t.enabled("writer/research")).toBe(false);+ expect(t.records()).toHaveLength(0);+ });+it("names the skills it could not put back instead of saying nothing was changed", () => {const t = setup({ failTurn: "coder/lint", failBack: "writer/research" });const result = t.host.stage(t.bots.chief!, "t1", both, "self");@@ -228,6 +240,20 @@ describe("a Chief of Staff changing its teammates' skills", () => {expect(t.enabled("coder/lint")).toBe(false);});+ it("refuses Undo, changing none, when the Chief no longer manages a listed teammate", () => {+ const refuse: string[] = [];+ const t = setup({ refuse });+ t.host.stage(t.bots.chief!, "t1", both, "self");+ t.turns.length = 0;+ refuse.push("coder");+ const result = t.undo();+ expect(result).toMatchObject({ claimed: true, state: "invalid", status: 403 });+ expect("error" in result && result.error).toContain("Coder");+ expect(t.turns).toEqual([]);+ expect(t.enabled("writer/research")).toBe(true);+ expect(t.enabled("coder/lint")).toBe(false);+ });+it("keeps no record when applying fails", () => {const t = setup({ failTurn: "coder/lint" });expect(t.host.stage(t.bots.chief!, "t1", both, "self")).toMatchObject({ status: 422 });diff --git a/server/host/teammate-skills-host.ts b/server/host/teammate-skills-host.tsindex 6d3ba05bb8..f723216ed8 100644--- a/server/host/teammate-skills-host.ts+++ b/server/host/teammate-skills-host.ts@@ -58,9 +58,16 @@ export function createTeammateSkillsHost(deps: TeammateSkillsHostDeps) {};const dropRecord = (requestId: string) => rmSync(recordPath(requestId), { force: true });const botName = (id: string) => store.bot(id)?.name ?? id;- const turnOne = (step: Step) => {+ /** One step's result; a write that throws (a full disk) comes back as an error, so the batch can still be put back. */+ const turnOne = (step: Step): object => {const bot = store.bot(step.botId);- return bot ? deps.turn(step.botId, step.name, step.on, deps.skill(bot, step.name)?.kind) : { error: "that bot no longer exists" };+ if (!bot) return { error: "that bot no longer exists" };+ try {+ return deps.turn(step.botId, step.name, step.on, deps.skill(bot, step.name)?.kind);+ } catch (error) {+ const code = (error as NodeJS.ErrnoException | null)?.code;+ return { error: code ? `could not save the change (${code})` : "could not save the change" };+ }};/** Turns every step, or, when one fails, turns back those already turned so nothing is changed. */@@ -210,6 +217,11 @@ export function createTeammateSkillsHost(deps: TeammateSkillsHostDeps) {| { claimed: true; state: "invalid"; error: string; status: number; stale?: true } {const record = readRecord(request.requestId);const changes = record?.chiefId === botId ? record.changes : [];+ const refused = changes.flatMap((change) => {+ const why = deps.refusal(botId, change.botId);+ return why ? [`${botName(change.botId)}: ${why}`] : [];+ });+ if (refused.length) return { claimed: true, state: "invalid", error: `Nothing was changed. ${[...new Set(refused)].join("; ")}`, status: 403 };const moved = changes.some((change) => {const target = store.bot(change.botId);const current = target ? deps.skill(target, change.name) : undefined;