Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(skills): refuse a teammate-skills Undo once a listed skill's content changed
AdityaUmaleec0620460724
diff --git a/server/host/teammate-skills-host.test.ts b/server/host/teammate-skills-host.test.tsindex 8c53e6c089..61298e9bda 100644--- a/server/host/teammate-skills-host.test.ts+++ b/server/host/teammate-skills-host.test.ts@@ -163,6 +163,11 @@ describe("a Chief of Staff changing its teammates' skills", () => {expect(t.enabled("writer/research")).toBe(true);expect(t.enabled("coder/lint")).toBe(false);expect(t.card()).toMatchObject({ answered: "allow", title: "Skills changed on your team (2)" });+ expect(t.records()).toHaveLength(1);++ expect(t.undo()).toEqual({ claimed: true, state: "undone" });+ expect(t.enabled("writer/research")).toBe(false);+ expect(t.enabled("coder/lint")).toBe(true);expect(t.records()).toHaveLength(0);});@@ -211,4 +216,21 @@ describe("a Chief of Staff changing its teammates' skills", () => {expect(t.turns).toEqual([]);expect(t.enabled("writer/research")).toBe(true);});++ it("refuses Undo, changing none, once a listed skill's content changed though it is still on", () => {+ const t = setup();+ t.host.stage(t.bots.chief!, "t1", both, "self");+ t.turns.length = 0;+ t.edit("writer/research", "---\nname: research\n---\nnew words\n");+ expect(t.undo()).toMatchObject({ claimed: true, state: "invalid", stale: true });+ 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 });+ expect(t.records()).toHaveLength(0);+ });});diff --git a/server/host/teammate-skills-host.ts b/server/host/teammate-skills-host.tsindex 3280d351cf..6d3ba05bb8 100644--- a/server/host/teammate-skills-host.ts+++ b/server/host/teammate-skills-host.ts@@ -1,7 +1,7 @@// A Chief of Staff turns named skills on or off for several of its teammates in one call. Each change passes the rules a// bot's own switch does (skill-switch.ts), and they apply together or not at all: where the Chief's own skill changes// apply directly, with one receipt and one Undo; otherwise on one card, approved only while every skill is still the one-// the card listed.+// the card listed. Its record stays until Undo, which turns them back only while every skill is still as it was applied.import { createHash, randomUUID } from "node:crypto";import { mkdirSync, readFileSync, rmSync } from "node:fs";import { dirname, join } from "node:path";@@ -14,7 +14,7 @@ import { chiefLevel } from "../chief-level.ts";import { appendDecision } from "../decision-log.ts";import { UNDO_STALE, type DirectApply } from "../direct-apply.ts";import { conversationLanguage, serverLine } from "../server-lines.ts";-import { skillSwitchStillMatches, skillSwitchTarget, type SwitchableSkill } from "../skill-switch.ts";+import { skillContentSha256, skillSwitchStillMatches, skillSwitchTarget, type SwitchableSkill } from "../skill-switch.ts";import type { BotRecord, Message, Store } from "../store.ts";export const TEAMMATE_SKILL_CHANGES_MAX = 40;@@ -52,6 +52,10 @@ export function createTeammateSkillsHost(deps: TeammateSkillsHostDeps) {return parsed.success ? parsed.data : null;} catch { return null; }};+ const writeRecord = (requestId: string, record: TeammateSkillsRecord) => {+ mkdirSync(dirname(recordPath(requestId)), { recursive: true, mode: 0o700 });+ writeFileAtomic(recordPath(requestId), `${JSON.stringify(record)}\n`, { mode: 0o600 });+ };const dropRecord = (requestId: string) => rmSync(recordPath(requestId), { force: true });const botName = (id: string) => store.bot(id)?.name ?? id;const turnOne = (step: Step) => {@@ -140,17 +144,19 @@ export function createTeammateSkillsHost(deps: TeammateSkillsHostDeps) {const persistence = deps.persistence(from.id, threadId, !applies);if (!persistence.ok) return { status: persistence.status, body: { error: persistence.error } };const requestId = randomUUID();+ const record: TeammateSkillsRecord = { chiefId: from.id, changes: asked.changes.map(({ botId, name, action, contentSha256 }) => ({ botId, name, action, contentSha256 })) };+ writeRecord(requestId, record);if (applies) {const turned = turnAll(steps(asked.changes));- if ("error" in turned) return { status: 422, body: { state: "failed", error: turned.error } };+ if ("error" in turned) {+ dropRecord(requestId);+ return { status: 422, body: { state: "failed", error: turned.error } };+ }const done = appendCard({ requestId, from, threadId, changes: asked.changes, applied: true });appendDecision(deps.dataDir, { threadId, requestId, botId: from.id, botName: from.name, tool: "stage_skill", summary: done, decision: "auto-approved", source: applies });deps.noteSkillChange(from.id, threadId);return { status: 201, body: { state: "applied", changes: asked.changes.length, summary: `${done}.` } };}- const record: TeammateSkillsRecord = { chiefId: from.id, changes: asked.changes.map(({ botId, name, action, contentSha256 }) => ({ botId, name, action, contentSha256 })) };- mkdirSync(dirname(recordPath(requestId)), { recursive: true, mode: 0o700 });- writeFileAtomic(recordPath(requestId), `${JSON.stringify(record)}\n`, { mode: 0o600 });let summary: string;try {summary = appendCard({ requestId, from, threadId, changes: asked.changes, applied: false });@@ -195,7 +201,6 @@ export function createTeammateSkillsHost(deps: TeammateSkillsHostDeps) {store.patchMessage(args.threadId, messageId, { card: { ...card, answered: "allow", held: undefined, title: done } });deps.noteSkillChange(args.botId, args.threadId);decided("user-approved");- dropRecord(request.requestId);return { claimed: true, outcome: "allowed-once" };}@@ -203,15 +208,18 @@ export function createTeammateSkillsHost(deps: TeammateSkillsHostDeps) {function undo(botId: string, threadId: string, message: Message, request: SkillRequestCardData):| { claimed: true; state: "undone" }| { claimed: true; state: "invalid"; error: string; status: number; stale?: true } {- const changes = request.teammates ?? [];+ const record = readRecord(request.requestId);+ const changes = record?.chiefId === botId ? record.changes : [];const moved = changes.some((change) => {const target = store.bot(change.botId);const current = target ? deps.skill(target, change.name) : undefined;- return !current || current.kind === "fixed" || current.enabled !== (change.action === "enable");+ return !current || current.kind === "fixed" || current.enabled !== (change.action === "enable") || current.text === null+ || skillContentSha256({ kind: current.kind, text: current.text }) !== change.contentSha256;});if (!changes.length || moved) return { claimed: true, state: "invalid", error: UNDO_STALE, status: 409, stale: true };const undone = turnAll(steps(changes, true));if ("error" in undone) return { claimed: true, state: "invalid", error: undone.error, status: 409 };+ dropRecord(request.requestId);if (message.card) store.patchMessage(threadId, message.id, { card: { ...message.card, undone: true } });deps.noteSkillChange(botId, threadId);return { claimed: true, state: "undone" };diff --git a/server/skill-switch.ts b/server/skill-switch.tsindex 96958e32ae..71d3ec4c11 100644--- a/server/skill-switch.ts+++ b/server/skill-switch.ts@@ -29,6 +29,11 @@ export interface SkillSwitchTarget {gist: string;}+/** Digest of a skill's kind and full SKILL.md: what an approval or an Undo binds to. */+export function skillContentSha256(skill: Pick<SwitchableSkill, "kind"> & { text: string }): string {+ return createHash("sha256").update(`${skill.kind}\0${skill.text}`).digest("hex");+}+/** Refuses what a bot may not change on its own card, before any card is shown. */export function skillSwitchTarget(name: string,@@ -55,7 +60,7 @@ export function skillSwitchTarget(return {preview,sha256: createHash("sha256").update(preview).digest("hex"),- contentSha256: createHash("sha256").update(`${skill.kind}\0${skill.text}`).digest("hex"),+ contentSha256: skillContentSha256({ kind: skill.kind, text: skill.text }),...(skill.source ? { source: redactSecretsInText(skill.source) } : {}),gist: redactSecretsInText(skill.description ?? "").replace(/\s+/g, " ").trim().slice(0, STAGED_GIST_MAX),};