Skip to content

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

fix(inspector): distinguish live tail from persisted records

agoollae65cb83332a
diff --git a/src/lib/inspector-window.test.ts b/src/lib/inspector-window.test.ts
index 4b01503cea..4bf98eeca0 100644
--- a/src/lib/inspector-window.test.ts
+++ b/src/lib/inspector-window.test.ts
@@ -9,10 +9,31 @@ const event = (i: number): RuntimeEvent => ({
const entry = (i: number): InspectorEntry => ({ kind: "runtime", at: String(i), data: event(i) });
describe("Inspector's recent event window", () => {
+ it("does not count displaced equal-time disk records as new live arrivals", () => {
+ const entries: InspectorEntry[] = Array.from({ length: 800 }, (_, i) => ({
+ kind: "runtime", at: "same", data: { ...event(i), createdAt: "same" },
+ }));
+ const previous = mergeInspectorSnapshot(null, { entries: entries.slice(0, 400), total: { runtime: 400, native: 0 } });
+ const snapshot = { entries: entries.slice(400), total: { runtime: 800, native: 0 } };
+ expect(mergeInspectorSnapshot(previous, snapshot)).toEqual(snapshot);
+ });
+
+ it("stops treating a live record as pending once a snapshot acknowledges it", () => {
+ const first = { ...event(1), createdAt: "same" };
+ const live = appendInspectorRuntime(null, [first])!;
+ const acknowledged = mergeInspectorSnapshot(live, {
+ entries: [{ kind: "runtime", at: "same", data: first }], total: { runtime: 1, native: 0 },
+ });
+ const advanced: InspectorPage = {
+ entries: [{ kind: "runtime", at: "same", data: { ...event(2), createdAt: "same" } }], total: { runtime: 2, native: 0 },
+ };
+ expect(mergeInspectorSnapshot(acknowledged, advanced)).toEqual(advanced);
+ });
+
it("preserves distinct equal-time records without an overlap and counts the union once", () => {
const first = { ...event(1), createdAt: "same" };
const second = { ...event(2), createdAt: "same" };
- const previous: InspectorPage = { entries: [{ kind: "runtime", at: "same", data: second }], total: { runtime: 1, native: 0 } };
+ const previous = appendInspectorRuntime(null, [second])!;
const snapshot: InspectorPage = { entries: [{ kind: "runtime", at: "same", data: first }], total: { runtime: 1, native: 0 } };
const merged = mergeInspectorSnapshot(previous, snapshot);
expect(merged.entries).toEqual([...snapshot.entries, ...previous.entries]);
@@ -22,7 +43,7 @@ describe("Inspector's recent event window", () => {
});
it("retains a live tail across stale snapshots and counts it once when disk catches up", () => {
- const previous: InspectorPage = { entries: [entry(1), entry(2), entry(3)], total: { runtime: 53, native: 12 } };
+ const previous = appendInspectorRuntime({ entries: [entry(1), entry(2)], total: { runtime: 52, native: 12 } }, [event(3)])!;
const snapshot: InspectorPage = { entries: [entry(1), entry(2)], total: { runtime: 52, native: 10 } };
const merged = mergeInspectorSnapshot(previous, snapshot);
expect(merged).toEqual({ entries: previous.entries, total: { runtime: 53, native: 10 } });
@@ -34,7 +55,7 @@ describe("Inspector's recent event window", () => {
it("preserves publication order for equal-time events using the overlap anchor", () => {
const entries: InspectorEntry[] = [1, 2, 3].map((i) => ({ kind: "runtime", at: "same", data: { ...event(i), createdAt: "same" } }));
- const previous: InspectorPage = { entries, total: { runtime: 3, native: 0 } };
+ const previous = appendInspectorRuntime(null, [1, 2, 3].map((i) => ({ ...event(i), createdAt: "same" })))!;
const snapshot: InspectorPage = { entries: entries.slice(0, 2), total: { runtime: 2, native: 0 } };
expect(mergeInspectorSnapshot(previous, snapshot)).toEqual(previous);
});
@@ -53,7 +74,7 @@ describe("Inspector's recent event window", () => {
});
const native: InspectorEntry = { kind: "native", at: "0001", data: { at: "0001", dir: "out", source: "fixture", msg: "fresh" } };
const merged = mergeInspectorSnapshot(
- { entries: entries.slice(1), total: { runtime: 501, native: 100 } },
+ appendInspectorRuntime({ entries: entries.slice(0, 400), total: { runtime: 500, native: 100 } }, [entries[400].data])!,
{ entries: [...entries.slice(0, 400), native], total: { runtime: 500, native: 101 } },
);
expect(merged.entries.filter((item) => item.kind === "runtime")).toEqual(entries.slice(1));
diff --git a/src/lib/inspector-window.ts b/src/lib/inspector-window.ts
index ef6e5abb4a..2c208caa13 100644
--- a/src/lib/inspector-window.ts
+++ b/src/lib/inspector-window.ts
@@ -2,6 +2,7 @@ import type { RuntimeEvent } from "../../shared/runtime-events";
import type { InspectorEntry, InspectorPage } from "../../shared/inspector";
export const INSPECTOR_WINDOW_LIMIT = 400;
+const unpersistedRuntime = new WeakMap<InspectorPage, ReadonlySet<string>>();
/**
* Retains the latest window per log kind without changing their relative order.
@@ -28,21 +29,27 @@ export function appendInspectorRuntime(page: InspectorPage | null, events: Runti
if (!events.length) return page;
const previous = page ?? { entries: [], total: { runtime: 0, native: 0 } };
const seen = new Set(previous.entries.flatMap((entry) => entry.kind === "runtime" ? [entry.data.eventId] : []));
- const added: InspectorEntry[] = [];
+ const added: Extract<InspectorEntry, { kind: "runtime" }>[] = [];
for (const event of events) {
if (seen.has(event.eventId)) continue;
seen.add(event.eventId);
added.push({ kind: "runtime", at: event.createdAt, data: event });
}
if (!added.length) return page;
- return {
- entries: boundInspectorEntries([...previous.entries, ...added]),
+ const entries = boundInspectorEntries([...previous.entries, ...added]);
+ const pending = new Set([...(unpersistedRuntime.get(previous) ?? []), ...added.map((entry) => entry.data.eventId)]);
+ const next = {
+ entries,
total: { ...previous.total, runtime: previous.total.runtime + added.length },
};
+ unpersistedRuntime.set(next, new Set(entries.flatMap((entry) => entry.kind === "runtime" && pending.has(entry.data.eventId) ? [entry.data.eventId] : [])));
+ return next;
}
/**
- * Reconciles a disk snapshot with the retained runtime tail when persistence lags.
+ * Reconciles a disk snapshot with live records not yet observed on disk.
+ * Provenance follows immutable page identities in a WeakMap, so displaced disk
+ * records cannot re-enter as live events and discarded pages can be collected.
* A shared event ID anchors publication order even for equal timestamps; without
* an overlap, equal-time records remain ambiguous and distinct IDs are retained
* alongside later records rather than risking the loss of a live event.
@@ -54,11 +61,15 @@ export function mergeInspectorSnapshot(previous: InspectorPage | null, snapshot:
const next = { ...snapshot, entries: boundInspectorEntries(snapshot.entries) };
if (!previous) return next;
const runtime = previous.entries.flatMap((entry) => entry.kind === "runtime" ? [entry.data] : []);
+ const pending = unpersistedRuntime.get(previous) ?? new Set<string>();
const snapshotRuntime = next.entries.filter((entry) => entry.kind === "runtime");
const ids = new Set(snapshotRuntime.map((entry) => entry.data.eventId));
const overlap = runtime.findLastIndex((event) => ids.has(event.eventId));
const latest = snapshotRuntime.at(-1)?.at;
- const tail = overlap >= 0 ? runtime.slice(overlap + 1) : runtime.filter((event) => latest === undefined || event.createdAt >= latest);
+ const tail = (overlap >= 0 ? runtime.slice(overlap + 1) : runtime.filter((event) => latest === undefined || event.createdAt >= latest))
+ .filter((event) => pending.has(event.eventId));
const merged = appendInspectorRuntime(next, tail)!;
- return { ...merged, total: { ...merged.total, runtime: Math.max(previous.total.runtime, merged.total.runtime) } };
+ const result = { ...merged, total: { ...merged.total, runtime: Math.max(previous.total.runtime, merged.total.runtime) } };
+ unpersistedRuntime.set(result, unpersistedRuntime.get(merged) ?? new Set());
+ return result;
}