Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

A newer licence is not held back by the write already failing

Sebastien Tardif1546432337a6
diff --git a/connector/src/licence.ts b/connector/src/licence.ts
index ef9d77ccc4..aa66ec28b9 100644
--- a/connector/src/licence.ts
+++ b/connector/src/licence.ts
@@ -74,11 +74,15 @@ export class LicenceDelivery {
if (!licence || this.now() < this.retryAt) return Promise.resolve();
const due = !this.settled || this.settled.licence !== licence || this.now() - this.settled.at >= (this.options.recheckMs ?? LICENCE_RECHECK_MS);
if (!due) return Promise.resolve();
- this.running = this.deliver(licence).finally(() => { this.running = null; });
+ this.running = this.deliver(licence).finally(() => {
+ this.running = null;
+ if (this.wanted !== licence) void this.tick();
+ });
return this.running;
}
- private failed(why: string): void {
+ private failed(licence: string, why: string): void {
+ if (this.wanted !== licence) return;
this.retryAt = this.now() + (this.options.retryMs ?? LICENCE_RETRY_MS);
this.options.log(`could not save the licence MausBot sent: ${why}; trying again in ${Math.round((this.options.retryMs ?? LICENCE_RETRY_MS) / 60_000)} minutes`);
}
@@ -101,21 +105,24 @@ export class LicenceDelivery {
try {
if (this.settle(licence, await this.options.home.licenceFile("check", licence))) return;
} catch (error) {
- return this.failed(errorText(error));
+ return this.failed(licence, errorText(error));
}
+ if (this.wanted !== licence) return;
// The audit line comes first: a licence the log could not record is not written.
try {
this.options.record({ event: "licence", what: "writing", by: "admin", fingerprint });
} catch (error) {
- return this.failed(`the audit log cannot be written (${errorText(error)})`);
+ return this.failed(licence, `the audit log cannot be written (${errorText(error)})`);
}
+ if (this.wanted !== licence) return;
let state: LicenceFileState;
try {
state = await this.options.home.licenceFile("write", licence);
} catch (error) {
try { this.options.record({ event: "licence", what: "failed", by: "admin", fingerprint }); } catch { /* the log said why already */ }
- return this.failed(errorText(error));
+ return this.failed(licence, errorText(error));
}
+ if (this.wanted !== licence) return;
if (this.settle(licence, state) && state === "written") this.options.log(`saved the licence MausBot sent (${fingerprint}) to the server's data folder`);
}
}
diff --git a/connector/test/customer-run.test.ts b/connector/test/customer-run.test.ts
index 4b655374ed..efea52badd 100644
--- a/connector/test/customer-run.test.ts
+++ b/connector/test/customer-run.test.ts
@@ -3,7 +3,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
-import { AdoptedHome, UPDATE_FILES, enrolmentRefusal, findInstall, standaloneServerPresent } from "../src/adopted-home.ts";
+import { AdoptedHome, UPDATE_FILES, enrolmentRefusal, findInstall, standaloneServerPresent, type LicenceFileState } from "../src/adopted-home.ts";
import { Agent, savedConfig, type Refusal, type Socket } from "../src/agent.ts";
import { AuditLog, type AuditEvent, type AuditLine } from "../src/audit.ts";
import { serveCustomerRunRequest } from "../src/customer-run-requests.ts";
@@ -456,6 +456,33 @@ describe("a customer-run server: its licence", () => {
expect(fixture.licenceExecs()).toBe(checks + 1);
});
+ it("writes a newer licence at once when the one already being written fails", async () => {
+ let clock = 1_800_000_000_000;
+ let failWrite: (() => void) | null = null;
+ const written: string[] = [];
+ const home = {
+ licenceProof: null as string | null,
+ licenceFile(mode: "check" | "write", licence: string): Promise<LicenceFileState> {
+ if (mode === "check") return Promise.resolve("differs");
+ if (licence === LICENCE) return new Promise((_resolve, reject) => { failWrite = () => reject(new Error("disk full")); });
+ written.push(licence);
+ return Promise.resolve("written");
+ },
+ };
+ const delivery = new LicenceDelivery({ home, log: () => {}, record: () => {}, now: () => clock, retryMs: 5 * 60_000 });
+ const first = delivery.offer(LICENCE);
+ for (let i = 0; i < 8 && !failWrite; i++) await Promise.resolve();
+ const renewed = delivery.offer(RENEWED);
+ failWrite!();
+ await first;
+ await renewed;
+ await delivery.tick();
+ expect(written).toEqual([RENEWED]);
+ clock += 60_000;
+ await delivery.tick();
+ expect(written).toEqual([RENEWED]);
+ });
+
it("saves it while remote management is paused: a licence is not a job on the server's bots", async () => {
const fixture = await delivering({ paused: true });
expect(fixture.state.paused()).toBe(true);