Skip to content

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

fix(inspector): preserve live tail across stale refreshes

agoolla5f54a7d64e3
diff --git a/src/components/InspectorPanel.tsx b/src/components/InspectorPanel.tsx
index 22d911e754..203c4ea4be 100644
--- a/src/components/InspectorPanel.tsx
+++ b/src/components/InspectorPanel.tsx
@@ -11,7 +11,7 @@ import { timelineEvents } from "@/lib/taskTimeline";
import { activeLocale, t } from "@/lib/i18n";
import { capital } from "@/lib/automations";
import type { LocaleKey } from "@/locales";
-import { appendInspectorRuntime, boundInspectorEntries, INSPECTOR_WINDOW_LIMIT } from "@/lib/inspector-window";
+import { appendInspectorRuntime, mergeInspectorSnapshot, INSPECTOR_WINDOW_LIMIT } from "@/lib/inspector-window";
const tabLabel = (key: LocaleKey) => (activeLocale() === "en" ? t(key) : capital(t(key)));
@@ -41,7 +41,7 @@ export function InspectorPanel({ bot }: { bot: Bot }) {
// SAFETY: the same-version harness's typed inspector endpoint; malformed transport data is handled by catch.
const next = (await res.json()) as InspectorPage;
if (controller.signal.aborted) return;
- setPage({ ...next, entries: boundInspectorEntries(next.entries) });
+ setPage((previous) => mergeInspectorSnapshot(previous, next));
setExpanded((previous) => new Set([...previous].filter((key) => key.startsWith("runtime:"))));
setError(null);
} catch (e) {
diff --git a/src/components/live-panels.test.ts b/src/components/live-panels.test.ts
index 30b836d884..a9ba016ae8 100644
--- a/src/components/live-panels.test.ts
+++ b/src/components/live-panels.test.ts
@@ -70,6 +70,21 @@ afterEach(() => {
});
describe("Inspector on the app's live stream", () => {
+ it("keeps live events missing from a stale refresh snapshot, including an unflushed batch", async () => {
+ await mount(InspectorPanel);
+ const events = [...document.querySelectorAll<HTMLButtonElement>('[role="tab"]')].find((tab) => tab.textContent === "events")!;
+ flushSync(() => events.click());
+ frame(runtime("before-refresh", { type: "item.started", itemType: "tool", title: "not on disk yet" }));
+ missed();
+ await settle();
+ await vi.waitFor(() => expect(document.body.textContent).toContain("not on disk yet"));
+ expect(document.querySelectorAll('button .tabular-nums')).toHaveLength(1);
+ missed();
+ await settle();
+ expect(document.body.textContent).toContain("not on disk yet");
+ expect(document.querySelectorAll('button .tabular-nums')).toHaveLength(1);
+ });
+
it("does not expand a replacement native record after refresh, while keeping runtime expansion", async () => {
let nativeMessage = "first private record";
vi.stubGlobal("fetch", () => Promise.resolve(new Response(JSON.stringify({
@@ -178,6 +193,26 @@ describe("Inspector on the app's live stream", () => {
expect(document.body.textContent).toContain("71");
});
+ it("lets only the newest refresh merge its buffered tail", async () => {
+ await mount(InspectorPanel);
+ const events = [...document.querySelectorAll<HTMLButtonElement>('[role="tab"]')].find((tab) => tab.textContent === "events")!;
+ flushSync(() => events.click());
+ frame(runtime("before", { type: "item.started", itemType: "tool", title: "before either fetch" }));
+ const finishes: Array<(response: Response) => void> = [];
+ vi.stubGlobal("fetch", () => new Promise<Response>((resolve) => { finishes.push(resolve); }));
+ missed();
+ frame(runtime("during", { type: "item.started", itemType: "tool", title: "during both fetches" }));
+ missed();
+ const snapshot = { entries: [], total: { runtime: 0, native: 0 } };
+ finishes[1](new Response(JSON.stringify(snapshot)));
+ await vi.waitFor(() => expect(document.body.textContent).toContain("during both fetches"));
+ finishes[0](new Response(JSON.stringify(snapshot)));
+ await settle();
+ expect(document.body.textContent).toContain("before either fetch");
+ expect(document.body.textContent).toContain("during both fetches");
+ expect(document.querySelectorAll('button .tabular-nums')).toHaveLength(2);
+ });
+
it("cancels the old thread's queued batch when switching conversations", async () => {
const render = await mount(InspectorPanel);
frame(runtime("old", { type: "item.started", itemType: "tool", title: "old queued work" }));
diff --git a/src/lib/inspector-window.test.ts b/src/lib/inspector-window.test.ts
index 33257721f9..9f59f01776 100644
--- a/src/lib/inspector-window.test.ts
+++ b/src/lib/inspector-window.test.ts
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import type { RuntimeEvent } from "../../shared/runtime-events";
import type { InspectorEntry, InspectorPage } from "../../shared/inspector";
-import { appendInspectorRuntime, boundInspectorEntries } from "./inspector-window";
+import { appendInspectorRuntime, boundInspectorEntries, mergeInspectorSnapshot } from "./inspector-window";
const event = (i: number): RuntimeEvent => ({
eventId: `e${i}`, provider: "fixture", threadId: "t", createdAt: String(i), type: "turn.started",
@@ -9,6 +9,46 @@ 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("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 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 } });
+ expect(mergeInspectorSnapshot(merged, snapshot)).toEqual(merged);
+ expect(mergeInspectorSnapshot(merged, { ...snapshot, entries: previous.entries, total: { runtime: 53, native: 10 } })).toEqual(merged);
+ expect(snapshot.entries).toEqual([entry(1), entry(2)]);
+ expect(previous.total.native).toBe(12);
+ });
+
+ 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 snapshot: InspectorPage = { entries: entries.slice(0, 2), total: { runtime: 2, native: 0 } };
+ expect(mergeInspectorSnapshot(previous, snapshot)).toEqual(previous);
+ });
+
+ it("does not reintroduce old rows when the snapshot has advanced past the retained window", () => {
+ const previous: InspectorPage = { entries: [entry(1), entry(2)], total: { runtime: 2, native: 0 } };
+ const snapshot: InspectorPage = { entries: [entry(3), entry(4)], total: { runtime: 4, native: 0 } };
+ expect(mergeInspectorSnapshot(previous, snapshot)).toEqual(snapshot);
+ expect(mergeInspectorSnapshot(null, snapshot)).toEqual(snapshot);
+ });
+
+ it("bounds the merged runtime window while keeping fresh native records and cumulative totals", () => {
+ const entries = Array.from({ length: 401 }, (_, i) => {
+ const data = { ...event(i), createdAt: String(i).padStart(4, "0") };
+ return { kind: "runtime" as const, at: data.createdAt, data };
+ });
+ 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 } },
+ { entries: [...entries.slice(0, 400), native], total: { runtime: 500, native: 101 } },
+ );
+ expect(merged.entries.filter((item) => item.kind === "runtime")).toEqual(entries.slice(1));
+ expect(merged.entries).toContainEqual(native);
+ expect(merged.total).toEqual({ runtime: 501, native: 101 });
+ });
+
it("bounds each log separately while keeping the original interleaving and totals", () => {
const entries: InspectorEntry[] = [];
for (let i = 0; i < 500; i++) {
diff --git a/src/lib/inspector-window.ts b/src/lib/inspector-window.ts
index e3fe54b49b..1bfdff710a 100644
--- a/src/lib/inspector-window.ts
+++ b/src/lib/inspector-window.ts
@@ -29,3 +29,16 @@ export function appendInspectorRuntime(page: InspectorPage | null, events: Runti
total: { ...previous.total, runtime: previous.total.runtime + added.length },
};
}
+
+export function mergeInspectorSnapshot(previous: InspectorPage | null, snapshot: InspectorPage): InspectorPage {
+ const next = { ...snapshot, entries: boundInspectorEntries(snapshot.entries) };
+ if (!previous) return next;
+ const runtime = previous.entries.flatMap((entry) => entry.kind === "runtime" ? [entry.data] : []);
+ 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 merged = appendInspectorRuntime(next, tail)!;
+ return { ...merged, total: { ...merged.total, runtime: Math.max(previous.total.runtime, merged.total.runtime) } };
+}