Skip to content

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

Burst mode: a finished burst settles whatever became of its chat

Milind Sonia9edf41e25dc
diff --git a/server/burst/manager.test.ts b/server/burst/manager.test.ts
index e9bc2b466f..1646ba8132 100644
--- a/server/burst/manager.test.ts
+++ b/server/burst/manager.test.ts
@@ -16,6 +16,7 @@ function harness(input: { limits?: BurstLimits; hasRoom?: () => boolean; store?:
const posted: BurstChild[][] = [];
const drawn: BurstCardData[] = [];
let thread = 0, limits = input.limits ?? { parallel: 3, maxTasks: 50, smallPlan: false }, mergeBusy = false, room = input.hasRoom ?? (() => true);
+ let postParts = (parts: BurstChild[]) => { posted.push(parts); return "parts as they came"; };
const deps: BurstManagerDeps = {
store,
limits: () => limits,
@@ -25,7 +26,7 @@ function harness(input: { limits?: BurstLimits; hasRoom?: () => boolean; store?:
stopChild: (_burst, child) => { stopped.push(child.threadId!); },
startMerge: async (burst, text, done) => { if (mergeBusy) return false; merges.push({ burst, text: text(), done }); return true; },
draw: (burst, card) => { drawn.push(card); return burst.cardMessageId ?? "card-1"; },
- postParts: (_burst, parts) => { posted.push(parts); return "parts as they came"; },
+ postParts: (_burst, parts) => postParts(parts),
deliver: (_burst, text) => { delivered.push(text); },
now: () => 1_700_000_000_000,
log: () => {},
@@ -33,7 +34,7 @@ function harness(input: { limits?: BurstLimits; hasRoom?: () => boolean; store?:
const manager = createBurstManager(deps);
return {
store, manager, started, stopped, merges, delivered, posted, drawn,
- setLimits(next: BurstLimits) { limits = next; }, setRoom(next: () => boolean) { room = next; }, setMergeBusy(next: boolean) { mergeBusy = next; },
+ setLimits(next: BurstLimits) { limits = next; }, setRoom(next: () => boolean) { room = next; }, setMergeBusy(next: boolean) { mergeBusy = next; }, setPostParts(next: typeof postParts) { postParts = next; },
propose(items: readonly string[]) {
const proposal = manager.propose({ botId: "bot", threadId: "parent", task: "Research companies", instructions: "Find founders and funding.", items, model: { instanceId: "i", model: "fast-1" }, modelName: "fast-1" });
if ("refused" in proposal) throw new Error(proposal.refused);
@@ -282,6 +283,17 @@ describe("the merged result", () => {
expect(delivered).toEqual(["parts as they came"]);
});
+ it("ends even when the chat it was written for is gone", () => {
+ const one = fresh({ limits: { parallel: 2, maxTasks: 50, smallPlan: false } });
+ const { manager, started, merges, store } = one;
+ const { burstId } = one.propose(items(2));
+ manager.run(burstId);
+ started.forEach((run) => { run.done(ok("result")); });
+ one.setPostParts(() => { throw new Error("thread is gone"); });
+ merges[0]!.done({ ok: false, text: "" });
+ expect(store.get(burstId)!.phase).toBe("done");
+ });
+
it("keeps the merge prompt within budget for a large burst", () => {
const done: BurstChild[] = Array.from({ length: 200 }, (_, seq) => ({ burstId: "b", seq, item: `i${seq}`, title: `i${seq}`, status: "done", attempts: 1, result: "x".repeat(20_000) }));
expect(mergePrompt({ task: "t" }, done, [], 0).length).toBeLessThan(170_000);
diff --git a/server/burst/manager.ts b/server/burst/manager.ts
index 2a4021a244..63ba480a5c 100644
--- a/server/burst/manager.ts
+++ b/server/burst/manager.ts
@@ -11,6 +11,7 @@ import { burstRow, type BurstChild, type BurstRecord, type BurstStore } from "./
const MINUTES_PER_TASK = 3;
const TICK_MS = 3_000;
const START_ATTEMPTS = 5;
+const PRUNE_AFTER_MS = 30 * 24 * 60 * 60_000;
const REASON_MAX = 200;
const RESULT_MAX = 20_000;
const FAILURES_SHOWN = 10;
@@ -216,10 +217,15 @@ export function createBurstManager(deps: BurstManagerDeps) {
}
function finish(burst: BurstRecord, done: BurstChild[], failed: BurstChild[], outcome: BurstTurnOutcome): void {
- const text = outcome.ok && outcome.text.trim() ? outcome.text.trim() : deps.postParts(burst, [...done, ...failed]);
+ // Settled whatever happens to the chat around it: a burst must never sit in "merging" and keep the timer alive.
const current = store.update(burst.id, { phase: "done" }) ?? burst;
- draw(current, failed);
- deps.deliver(current, text);
+ try {
+ const text = outcome.ok && outcome.text.trim() ? outcome.text.trim() : deps.postParts(burst, [...done, ...failed]);
+ draw(current, failed);
+ deps.deliver(current, text);
+ } catch (error) {
+ log(`${burst.id} could not post its result: ${error instanceof Error ? error.message : String(error)}`);
+ }
stopIfIdle();
}
@@ -328,6 +334,7 @@ export function createBurstManager(deps: BurstManagerDeps) {
},
/** After a restart: a running turn is gone, so its part goes back in line, and a merge waiting on its turn tries again. */
recover(): void {
+ store.prune(now() - PRUNE_AFTER_MS);
for (const burst of store.active()) {
if (burst.phase === "running") { store.requeueRunning(burst.id); enlist(burst.id); }
ensureTimer();