Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Tighten skill switch approval and clean up its digest pins
AdityaUmale490eb685d20d
diff --git a/server/host/skill-switch-host.test.ts b/server/host/skill-switch-host.test.tsindex b7cdd7c885..a1f33b183b 100644--- a/server/host/skill-switch-host.test.ts+++ b/server/host/skill-switch-host.test.ts@@ -6,6 +6,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import type { SkillRequestCardData, SkillSwitchAction } from "../../shared/skill-request.ts";import type { OptionCardData } from "../../shared/wire.ts";+import { flushDecisionLog } from "../decision-log.ts";import type { BotRecord } from "../store.ts";const mocks = vi.hoisted(() => ({ setImportedSkillEnabled: vi.fn() }));@@ -27,10 +28,11 @@ function setup() {const bot = { id: "b1", name: "Bot", color: "blue" } as BotRecord;const messages: Array<{ id: string; card: OptionCardData }> = [];let file = skillMd("A");+ let appendFails = false;const host = createSkillSwitchHost({store: {bot: () => bot,- appendMessage: ((_threadId: string, message: { card: OptionCardData }) => { messages.push({ id: `m${messages.length}`, card: message.card }); }) as never,+ appendMessage: ((_threadId: string, message: { card: OptionCardData }) => { if (appendFails) throw new Error("disk full"); messages.push({ id: `m${messages.length}`, card: message.card }); }) as never,patchMessage: ((_threadId: string, id: string, patch: { card: OptionCardData }) => { messages.find((message) => message.id === id)!.card = patch.card; }) as never,},dataDir,@@ -46,14 +48,17 @@ function setup() {return host.resolve({ botId: bot.id, threadId: "t1", requestId: request.requestId, behavior: "allow", reviewedSha256: request.sha256 }, id, shown, request);};const pins = () => readdirSync(join(dataDir, "skill-switch"));- return { bot, host, card, approve, pins, edit: (next: string) => { file = next; } };+ return { bot, host, card, approve, pins, edit: (next: string) => { file = next; }, failAppend: () => { appendFails = true; } };}beforeEach(() => {mocks.setImportedSkillEnabled.mockReset().mockReturnValue({ name: "lookup", enabled: false });});-afterEach(() => {- for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });+afterEach(async () => {+ for (const dir of dirs.splice(0)) {+ await flushDecisionLog(dir);+ rmSync(dir, { recursive: true, force: true });+ }});describe("skill switch cards", () => {@@ -82,4 +87,11 @@ describe("skill switch cards", () => {expect(card().held).toContain("changed after this card was shown");expect(mocks.setImportedSkillEnabled).not.toHaveBeenCalled();});++ it("leaves no digest pin behind when the card can't be added", () => {+ const { bot, host, pins, failAppend } = setup();+ failAppend();+ expect(() => host.stage(bot, "t1", "disable", { skill_name: "lookup" })).toThrow("disk full");+ expect(pins()).toHaveLength(0);+ });});diff --git a/server/host/skill-switch-host.ts b/server/host/skill-switch-host.tsindex 827bc80a99..730a0b8e1d 100644--- a/server/host/skill-switch-host.ts+++ b/server/host/skill-switch-host.ts@@ -100,7 +100,13 @@ export function createSkillSwitchHost(deps: SkillSwitchHostDeps) {const requestId = randomUUID();mkdirSync(dirname(pinPath(requestId)), { recursive: true, mode: 0o700 });writeFileAtomic(pinPath(requestId), target.contentSha256, { mode: 0o600 });- const summary = appendCard({ requestId, botId: from.id, threadId, action, name, target });+ let summary: string;+ try {+ summary = appendCard({ requestId, botId: from.id, threadId, action, name, target });+ } catch (error) {+ dropPin(requestId);+ throw error;+ }appendDecision(deps.dataDir, { threadId, requestId, botId: from.id, botName: from.name,tool: "stage_skill", summary, decision: "card-shown", source: "skill" });return { status: 201, body: { state: "pending", name, action, gist: target.gist, warnings: [], summary } };@@ -140,9 +146,9 @@ export function createSkillSwitchHost(deps: SkillSwitchHostDeps) {store.patchMessage(args.threadId, messageId, {card: { ...card, answered: "allow", held: undefined, title: skillSwitchCardCopy(request.action, request.name, "").done },});- dropPin(request.requestId);deps.noteSkillChange(args.botId, args.threadId);decided("user-approved");+ dropPin(request.requestId);return { claimed: true, outcome: "allowed-once" };}diff --git a/server/skill-switch.test.ts b/server/skill-switch.test.tsindex 85046c2e46..819c6e09e4 100644--- a/server/skill-switch.test.ts+++ b/server/skill-switch.test.ts@@ -7,10 +7,11 @@ import { skillSwitchCardCopy, skillSwitchStillMatches, skillSwitchTarget, type Sconst text = "---\nname: notes\ndescription: Keeps notes\n---\n# Notes\n";const own = (enabled: boolean): SwitchableSkill => ({ kind: "own", enabled, text, description: "Keeps notes" });const hash = (value: string) => createHash("sha256").update(value).digest("hex");+const pinned = (kind: SwitchableSkill["kind"], value = text) => hash(`${kind}\0${value}`);describe("skillSwitchTarget", () => {it("shows the current SKILL.md and binds the card to its hash", () => {- expect(skillSwitchTarget("notes", "disable", own(true))).toEqual({ preview: text, sha256: hash(text), contentSha256: hash(text), gist: "Keeps notes" });+ expect(skillSwitchTarget("notes", "disable", own(true))).toEqual({ preview: text, sha256: hash(text), contentSha256: pinned("own"), gist: "Keeps notes" });expect(skillSwitchTarget("notes", "enable", own(false))).toMatchObject({ sha256: hash(text) });expect(skillSwitchTarget("notes", "delete", own(false))).toMatchObject({ sha256: hash(text) });});@@ -35,10 +36,14 @@ describe("skillSwitchTarget", () => {describe("skillSwitchStillMatches", () => {it("passes only for the same skill in the same state", () => {- expect(skillSwitchStillMatches("notes", "disable", own(true), hash(text))).toEqual({ ok: true });- expect(skillSwitchStillMatches("notes", "disable", { ...own(true), text: `${text}more\n` }, hash(text))).toEqual({ error: expect.stringContaining("changed after this card was shown") });- expect(skillSwitchStillMatches("notes", "disable", own(false), hash(text))).toEqual({ error: "skill \"notes\" is already off" });- expect(skillSwitchStillMatches("notes", "delete", undefined, hash(text))).toHaveProperty("error");+ expect(skillSwitchStillMatches("notes", "disable", own(true), pinned("own"))).toEqual({ ok: true });+ expect(skillSwitchStillMatches("notes", "disable", { ...own(true), text: `${text}more\n` }, pinned("own"))).toEqual({ error: expect.stringContaining("changed after this card was shown") });+ expect(skillSwitchStillMatches("notes", "disable", own(false), pinned("own"))).toEqual({ error: "skill \"notes\" is already off" });+ expect(skillSwitchStillMatches("notes", "delete", undefined, pinned("own"))).toHaveProperty("error");+ });++ it("refuses an imported skill with the same SKILL.md as the own skill the card showed", () => {+ expect(skillSwitchStillMatches("notes", "disable", { ...own(true), kind: "imported" }, pinned("own"))).toEqual({ error: expect.stringContaining("changed after this card was shown") });});it("binds to the full SKILL.md, not only its redacted preview", () => {diff --git a/server/skill-switch.ts b/server/skill-switch.tsindex 5509b6594d..8e9e79a6a1 100644--- a/server/skill-switch.ts+++ b/server/skill-switch.ts@@ -21,7 +21,8 @@ export interface SkillSwitchTarget {/** The current SKILL.md as the card shows it; approval binds to its hash. */preview: string;sha256: string;- /** Digest of the full SKILL.md, secrets included. Approval binds to this one; it stays on the server, never on the card. */+ /** Digest of the skill's kind and full SKILL.md, secrets included, so an imported skill with the same text never stands in+ * for the bot's own. Approval binds to this one; it stays on the server, never on the card. */contentSha256: string;source?: string;gist: string;@@ -53,7 +54,7 @@ export function skillSwitchTarget(return {preview,sha256: createHash("sha256").update(preview).digest("hex"),- contentSha256: createHash("sha256").update(skill.text).digest("hex"),+ contentSha256: createHash("sha256").update(`${skill.kind}\0${skill.text}`).digest("hex"),...(skill.source ? { source: redactSecretsInText(skill.source) } : {}),gist: redactSecretsInText(skill.description ?? "").replace(/\s+/g, " ").trim().slice(0, STAGED_GIST_MAX),};diff --git a/src/components/CallView.tsx b/src/components/CallView.tsxindex 4484f6ec98..a5f8c7845f 100644--- a/src/components/CallView.tsx+++ b/src/components/CallView.tsx@@ -586,7 +586,7 @@ function Call({ bot }: { bot: Bot }) {const allow = YES.test(said);if (allow && open.skill) {setHeard("");- void sayThenListen("Open this chat to review the complete skill before enabling it. You can say no now to deny it.");+ void sayThenListen("Open this chat to review the complete skill before approving it. You can say no now to deny it.");return;}// Stay claimed until the server's card patch arrives, or a render in that gap submits the same approvaldiff --git a/src/components/GroupCallView.tsx b/src/components/GroupCallView.tsxindex 0e8db3aabc..aa456c4320 100644--- a/src/components/GroupCallView.tsx+++ b/src/components/GroupCallView.tsx@@ -240,7 +240,7 @@ function GroupCall({ group, members }: { group: Group; members: Bot[] }) {if (allow && openApproval.skill) {setHeard("");enqueueSpeech(- "Open the group thread to review the complete skill before enabling it. You can say no now to deny it.",+ "Open the group thread to review the complete skill before approving it. You can say no now to deny it.",openApproval.member,true,);