Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Doctor: a failed look after a fix is no repair; keep a temp beside an unreadable registry file
milind-soni0c276bcc3917
diff --git a/docs/self-hosting.md b/docs/self-hosting.mdindex f6125ead46..e56b34372a 100644--- a/docs/self-hosting.md+++ b/docs/self-hosting.md@@ -1864,9 +1864,10 @@ macOS).Before changing anything, a repair copies it into`<data>/doctor-backups/<time>/`, with a `manifest.json` that lists what changed-(for a file mode, the old mode). The ten newest runs' copies are kept, and the-older ones together stay under 2 GB; a run that kept a copy of `bots.json` or-`groups.json` is kept for at least 30 days either way. They stay on that+(for a file mode, the old mode). When a new run copies something, earlier runs+go: any beyond the ten newest, and, oldest first, as many as it takes to bring+the earlier runs together under 2 GB. A run that kept a copy of `bots.json` or+`groups.json` stays for at least 30 days either way. The copies stay on thatcomputer and are not part of a workspace backup.As a server starts, the doctor reads `messages.db` through and copies it beforediff --git a/server/doctor/checks/registry.test.ts b/server/doctor/checks/registry.test.tsindex 56faa9e7fd..d3c16ee0c1 100644--- a/server/doctor/checks/registry.test.ts+++ b/server/doctor/checks/registry.test.ts@@ -93,6 +93,10 @@ describe("registry check", () => {expect(report.checks[0]).toMatchObject({ status: "problem", fixable: false });expect(report.checks[0]!.fixed).toBeUndefined();expect(statSync(join(data, "bots.json")).isDirectory()).toBe(true);+ // Its whole copy stays beside it too, for when it can be read again.+ expect(report.checks[1]).toMatchObject({ id: "temp-files", status: "ok" });+ expect(readFileSync(join(data, `bots.json.4242.${UUID}.tmp`), "utf8")).toBe("[]");+ expect(report.backup).toBeNull();});it.skipIf(process.platform === "win32" || asRoot)("reports a bots.json this user may not read as not checked (EACCES), not damaged", () => {diff --git a/server/doctor/checks/registry.ts b/server/doctor/checks/registry.tsindex b33afd951e..7cf198019c 100644--- a/server/doctor/checks/registry.ts+++ b/server/doctor/checks/registry.ts@@ -13,28 +13,32 @@ import type { DoctorCheck, DoctorContext } from "../types.ts";export const REGISTRY_FILES = ["bots.json", "groups.json"] as const;type Damage = { file: string; path: string; copy: LeftoverTemp | null };-type Unchecked = { file: string; reason: string };+type Unchecked = { file: string; reason: string; copy: LeftoverTemp | null };++/** The newest whole save of `file` begun after `path` was last written: an older one would bring back a list from before. */+function newestCopy(ctx: DoctorContext, file: string, path: string): LeftoverTemp | null {+ let since = Infinity;+ try { since = statSync(path).mtimeMs; } catch {}+ const copies = leftoverTemps(ctx).filter((temp) => temp.of === file && temp.mtimeMs >= since && registryState(temp.path).state === "ok").sort((a, b) => b.mtimeMs - a.mtimeMs);+ return copies[0] ?? null;+}function look(ctx: DoctorContext): { damaged: Damage[]; unchecked: Unchecked[] } {const damaged: Damage[] = [], unchecked: Unchecked[] = [];for (const file of REGISTRY_FILES) {const path = join(ctx.dataDir, file);const state = registryState(path);- if (state.state === "unreadable") unchecked.push({ file, reason: state.reason });- if (state.state !== "damaged") continue;- // Only a save begun after the damaged file was written: an older one would bring back a list from before it.- let damagedAt = Infinity;- try { damagedAt = statSync(path).mtimeMs; } catch {}- const copies = leftoverTemps(ctx).filter((temp) => temp.of === file && temp.mtimeMs >= damagedAt && registryState(temp.path).state === "ok").sort((a, b) => b.mtimeMs - a.mtimeMs);- damaged.push({ file, path, copy: copies[0] ?? null });+ if (state.state === "unreadable") unchecked.push({ file, reason: state.reason, copy: newestCopy(ctx, file, path) });+ if (state.state === "damaged") damaged.push({ file, path, copy: newestCopy(ctx, file, path) });}return { damaged, unchecked };}-/** The temp files the registry would put in place of a damaged file: while one is still chosen (its fix has not run, or- * failed), the temp-file check leaves it where it is. */+/** The temp files the registry would put in place of a damaged file, or of one it could not read: while one is still+ * chosen (its fix has not run, or failed, or the file cannot be checked yet), the temp-file check leaves it where it is. */export function registryCopies(ctx: DoctorContext): Set<string> {- return new Set(look(ctx).damaged.flatMap((damage) => damage.copy ? [damage.copy.path] : []));+ const { damaged, unchecked } = look(ctx);+ return new Set([...damaged, ...unchecked].flatMap((item) => item.copy ? [item.copy.path] : []));}export function registryCheck(options: { rename?: (from: string, to: string) => void } = {}): DoctorCheck {diff --git a/server/doctor/run.test.ts b/server/doctor/run.test.tsindex 71fd88b0b5..f6d082a4fe 100644--- a/server/doctor/run.test.ts+++ b/server/doctor/run.test.ts@@ -29,6 +29,22 @@ describe("runDoctor", () => {expect(check.looks).toBe(3);});+ it("does not count a fix whose look after it failed as a repair", () => {+ let looks = 0;+ const check: DoctorCheck = {+ id: "breaks", title: "Breaks", looksAt: "nothing",+ check: () => {+ looks += 1;+ if (looks === 3) throw new Error("cannot look");+ return { status: "problem", fixable: true, summary: "Wrong." };+ },+ fix: () => "Done.",+ };+ const report = runDoctor({ dataDir: dataFolder(), version: "9.9.9", mode: "start", fix: true, now: () => NOW, checks: [check] });+ expect(report.checks[0]).toMatchObject({ status: "failed", repaired: false });+ expect(report.counts.fixed).toBe(0);+ });+it("hands the first look to the looks before and after a fix", () => {const earlier: unknown[] = [];const check: DoctorCheck = {diff --git a/server/doctor/run.ts b/server/doctor/run.tsindex 93a9f55fd8..00e3fa0bd7 100644--- a/server/doctor/run.ts+++ b/server/doctor/run.ts@@ -133,8 +133,8 @@ export function runDoctor(options: RunDoctorOptions): DoctorReport {const after = look(check, again);// A repair is counted only when the look after it finds the repairable part gone: all fine, or what is left is// not repairable (a registry file with no whole copy, beside one that was put back). A fix that changed nothing- // (a mode on a filesystem without modes) is not one.- const repaired = Boolean(fresh.fixable) && (after.status === "ok" || !after.fixable);+ // (a mode on a filesystem without modes) is not one, nor one whose look after it failed.+ const repaired = Boolean(fresh.fixable) && !after.failed && (after.status === "ok" || !after.fixable);results[at] = after.status === "ok"? { ...resultOf(check, fresh), status: "fixed", fixable: false, fixed: did, repaired }: { ...resultOf(check, after), fixed: did, repaired };