Skip to content

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

train: the acknowledgment batch lives in its own file, so the bot link stays under the size limit

milind-soni5c0ae8e24ea0
diff --git a/server/bot-link/ack-batch.ts b/server/bot-link/ack-batch.ts
new file mode 100644
index 0000000000..c5ab5ddb00
--- /dev/null
+++ b/server/bot-link/ack-batch.ts
@@ -0,0 +1,30 @@
+// A receive batch can carry hundreds of completed-result acknowledgments; each one used to rewrite and fsync the whole
+// link state. A run of them is now saved once, before any other frame is taken and before the batch returns.
+import { frameSchema, type Frame } from "./protocol.ts";
+
+/** Takes each frame that parses, in order. `take` gets a save for an acknowledgment that changed work: it is only
+ * counted, and the count is spent on save attempts (stopping at the first that works) before a frame that is not an
+ * acknowledgment, after a frame that throws, and at the end. */
+export function receiveFrames(frames: readonly unknown[], take: (frame: Frame, saveAcknowledgment: () => void) => void,
+ save: () => void, log: (line: string) => void): void {
+ let pending = 0;
+ const queue = () => { pending += 1; };
+ const flush = () => {
+ const attempts = pending;
+ pending = 0;
+ for (let attempt = 0; attempt < attempts; attempt += 1) {
+ try { save(); return; } catch (error) { log(`bot link: done failed (${String(error)})`); }
+ }
+ };
+ try {
+ for (const raw of frames) {
+ const parsed = frameSchema.safeParse(raw);
+ if (!parsed.success || parsed.data.t !== "done") flush();
+ if (!parsed.success) { log("bot link: a frame that did not parse was dropped"); continue; }
+ try { take(parsed.data, queue); } catch (error) {
+ flush();
+ log(`bot link: ${parsed.data.t} failed (${String(error)})`);
+ }
+ }
+ } finally { flush(); }
+}
diff --git a/server/bot-link/link.ts b/server/bot-link/link.ts
index dc3099521c..8495fce5f3 100644
--- a/server/bot-link/link.ts
+++ b/server/bot-link/link.ts
@@ -30,10 +30,10 @@ import type { PassedFile } from "../host/handoff-files.ts";
import { foldName, withTargetsHere, type CoordinateResult, type PeerDeliveryReceipt } from "../peer-delivery.ts";
import { isIdStart } from "../peer-roster.ts";
import type { Level } from "../runtime/types.ts";
-import { bothPlaces } from "./both-places.ts";
+import { receiveFrames } from "./ack-batch.ts"; import { bothPlaces } from "./both-places.ts";
import { armBackground, BACKGROUND_WAIT_MS, backgroundWaitNote, cloudUnreachableTooLong, neverSent } from "./away.ts";
import { endedSchema, stateSchema, type EndedLink, type LinkState, type Work } from "./link-state.ts";
-import { BRIEF_MAX, EXCHANGE_BYTES, FILES_MAX, LINK_EXPIRES_MS, PEERS_MAX, frameSchema, handoffOfRoot, isLinkedId, linkRoot, linkedId, parseLinked, peerKey,
+import { BRIEF_MAX, EXCHANGE_BYTES, FILES_MAX, LINK_EXPIRES_MS, PEERS_MAX, handoffOfRoot, isLinkedId, linkRoot, linkedId, parseLinked, peerKey,
type FileMeta, type FileWay, type Frame, type HandoffFrame, type LinkedBot, type Numbered, type Place, type ResultFrame } from "./protocol.ts";
const FILE = "bot-link.json";
@@ -961,28 +961,7 @@ export function createBotLink(deps: BotLinkDeps) {
return work?.peer === peer ? returned(work)[index] : undefined;
},
/** Frames from a place, each checked; one that does not parse is dropped. */
- receive(peer: string, frames: readonly unknown[]): void {
- let pendingAcknowledgments = 0;
- const queueAcknowledgmentSave = () => { pendingAcknowledgments += 1; };
- const flushAcknowledgments = () => {
- const attempts = pendingAcknowledgments;
- pendingAcknowledgments = 0;
- for (let attempt = 0; attempt < attempts; attempt += 1) {
- try { save(); return; } catch (error) { log(`bot link: done failed (${String(error)})`); }
- }
- };
- try {
- for (const raw of frames) {
- const parsed = frameSchema.safeParse(raw);
- if (!parsed.success || parsed.data.t !== "done") flushAcknowledgments();
- if (!parsed.success) { log("bot link: a frame that did not parse was dropped"); continue; }
- try { take(peer, parsed.data, queueAcknowledgmentSave); } catch (error) {
- flushAcknowledgments();
- log(`bot link: ${parsed.data.t} failed (${String(error)})`);
- }
- }
- } finally { flushAcknowledgments(); }
- },
+ receive(peer: string, frames: readonly unknown[]): void { receiveFrames(frames, (frame, saveAcknowledgment) => take(peer, frame, saveAcknowledgment), save, log); },
/** What waits to go to a place, after `ack` (frames up to it were taken and are dropped), as much as one exchange carries. */
outgoing(peer: string, ack: number): Numbered[] {
const one = live.get(peer);