Skip to content

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

Harden event-log bounds, marker scrubbing and test isolation

erywim2e2cf69a14a4
diff --git a/server/cloud-home-start.ts b/server/cloud-home-start.ts
index 27ea1ff5c1..b4a0dadb6d 100644
--- a/server/cloud-home-start.ts
+++ b/server/cloud-home-start.ts
@@ -59,7 +59,7 @@ const SERVER_ENV_NAMES = new Set([
"OMB_IMAGE_ENGINES",
// The conversation logs' cap and age limit (server/host/thread-log-sweep.ts), off unless the Admin sets it.
"OMB_THREAD_LOG_SWEEP", "OMB_THREAD_LOG_CAP", "OMB_THREAD_LOG_CUT",
- // The canonical event log's bounds and drain concurrency (server/harness/bus.ts): memory traded for completeness.
+ // The canonical event log's bounds and drain concurrency: memory traded for completeness.
"OMB_EVENT_LOG_BATCH_LINES", "OMB_EVENT_LOG_QUEUE_BYTES", "OMB_EVENT_LOG_QUEUE_EVENTS", "OMB_EVENT_LOG_DRAIN_THREADS",
// An ended Cloud started only for its owner's export: the server runs frozen (server/move-freeze.ts).
"OMB_CLOUD_EXPORT_ONLY",
diff --git a/server/harness/bus.test.ts b/server/harness/bus.test.ts
index 597310f794..d3c36eca0f 100644
--- a/server/harness/bus.test.ts
+++ b/server/harness/bus.test.ts
@@ -32,13 +32,6 @@ async function liveInstance() {
return fake.created.get("inst-1")!;
}
-beforeEach(() => {
- // A developer's shell may carry the log's own settings; the default-bound tests need the defaults.
- for (const name of ["OMB_EVENT_LOG_BATCH_LINES", "OMB_EVENT_LOG_QUEUE_EVENTS", "OMB_EVENT_LOG_QUEUE_BYTES", "OMB_EVENT_LOG_DRAIN_THREADS"]) {
- delete process.env[name];
- }
-});
-
describe("EventBus", () => {
beforeEach(() => {
rmSync(EVENTS_DIR, { recursive: true, force: true });
@@ -396,6 +389,51 @@ describe("EventBus", () => {
}
});
+ it("counts a byte-bound drop and still logs a terminal event", async () => {
+ const writes: string[] = [];
+ const bus = new EventBus((_path, data) => { writes.push(data); }, undefined,
+ { batchLines: 64, queueEvents: 4096, queueBytes: 1, drainThreads: 8 });
+ bus.publish(testEvent({ eventId: "first" }));
+ bus.publish(testEvent({ eventId: "refused" }));
+ bus.publish(testEvent({ eventId: "done", type: "turn.completed", ok: true } as Partial<RuntimeEvent>));
+
+ expect(bus.dropStats().events).toBe(1);
+ expect(bus.dropStats().bytes).toBeGreaterThan(0);
+ await bus.flush();
+ const ids = writes.flatMap((chunk) => chunk.trim().split("\n").map((line) => JSON.parse(line) as { eventId: string })).map((line) => line.eventId);
+ expect(ids).toContain("first");
+ expect(ids).toContain("done");
+ expect(ids).not.toContain("refused");
+ });
+
+ it("keeps a batch and a worker when the limits say zero", async () => {
+ const writes: string[] = [];
+ const bus = new EventBus((_path, data) => { writes.push(data); }, undefined,
+ { batchLines: 0, queueEvents: 1, queueBytes: 1, drainThreads: 0 });
+ bus.publish(testEvent({ eventId: "first" }));
+ bus.publish(testEvent({ eventId: "refused" }));
+ await bus.flush();
+ const ids = writes.flatMap((chunk) => chunk.trim().split("\n").map((line) => JSON.parse(line) as { eventId: string })).map((line) => line.eventId);
+ expect(ids).toContain("first");
+ expect(ids).not.toContain("refused");
+ expect(bus.dropStats().events).toBe(1);
+ });
+
+ it("scrubs the write-failure marker it stores", async () => {
+ let failing = true;
+ const writes: string[] = [];
+ const bus = new EventBus((_path, data) => { if (failing) throw new Error("disk full"); writes.push(data); });
+ const threadId = "sk-ant-abcdefghijklmnop";
+ bus.publish(testEvent({ threadId }));
+ await bus.flush();
+ failing = false;
+ bus.publish(testEvent({ threadId, eventId: "after", type: "turn.completed", ok: true } as Partial<RuntimeEvent>));
+ await bus.flush();
+ const lines = writes.flatMap((chunk) => chunk.trim().split("\n"));
+ expect(lines.join("\n")).toContain("«redacted");
+ expect(lines.join("\n")).not.toContain("abcdefghijklmnop");
+ });
+
it("appends different threads at the same time, and one thread's lines in order", async () => {
let release!: () => void;
const pending = new Promise<void>((resolve) => { release = resolve; });
diff --git a/server/harness/bus.ts b/server/harness/bus.ts
index 1ccd3f035a..081b23fb05 100644
--- a/server/harness/bus.ts
+++ b/server/harness/bus.ts
@@ -108,7 +108,13 @@ export class EventBus {
constructor(appendLog: EventLogAppend = defaultAppend, gate?: (event: RuntimeEvent) => boolean, limits: EventLogLimits = eventLogLimits()) {
this.appendLog = appendLog;
this.gate = gate;
- this.limits = limits;
+ // A batch or worker count below one would leave drain() unable to empty the queue.
+ this.limits = {
+ batchLines: Math.max(1, Math.floor(limits.batchLines) || 1),
+ queueEvents: Math.max(1, Math.floor(limits.queueEvents) || 1),
+ queueBytes: Math.max(1, Math.floor(limits.queueBytes) || 1),
+ drainThreads: Math.max(1, Math.floor(limits.drainThreads) || 1),
+ };
}
/** Events the queue's bounds refused since this bus started, and their size. Never counted as disk failures. */
@@ -295,7 +301,7 @@ export class EventBus {
const warning = this.pendingLogWarnings.get(threadId);
const events = items.map((item) => item.event);
const lines = items.map((item) => item.line);
- if (warning) lines.unshift(JSON.stringify(warning));
+ if (warning) lines.unshift(JSON.stringify(redactSecrets(warning)));
const file = join(EVENTS_DIR, `${threadId}.ndjson`);
try {
await this.appendLog(file, lines.join("\n") + "\n", { mode: 0o600 });
diff --git a/server/testing/setup.ts b/server/testing/setup.ts
index 7c25eb6cf4..e8a2a75582 100644
--- a/server/testing/setup.ts
+++ b/server/testing/setup.ts
@@ -22,6 +22,8 @@ delete process.env.HERMES_HOME;
delete process.env.CODEX_HOME;
// Pi turns copy from the person's Pi agent folder ($PI_CODING_AGENT_DIR or ~/.pi/agent): keep it in the throwaway home.
delete process.env.PI_CODING_AGENT_DIR;
+// The canonical event log's bounds are server settings; a developer's shell must not shrink a test bus's queue.
+for (const name of ["OMB_EVENT_LOG_BATCH_LINES", "OMB_EVENT_LOG_QUEUE_BYTES", "OMB_EVENT_LOG_QUEUE_EVENTS", "OMB_EVENT_LOG_DRAIN_THREADS"]) delete process.env[name];
// No server in a test downloads the project's announcement cards; its tests hand the store a fake fetch.
process.env.OMB_DISABLE_ANNOUNCEMENTS = "1";
// Named explicitly although homedir() already covers it: the device tests delete this directory wholesale.