Skip to content

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

fix(inspector): buffer the initial snapshot live tail

agooll17cfc2d5589f
diff --git a/src/components/InspectorPanel.tsx b/src/components/InspectorPanel.tsx
index 203c4ea4be..6ccb4ceb43 100644
--- a/src/components/InspectorPanel.tsx
+++ b/src/components/InspectorPanel.tsx
@@ -17,6 +17,12 @@ const tabLabel = (key: LocaleKey) => (activeLocale() === "en" ? t(key) : capital
type Lens = "run" | "events" | "raw";
+/**
+ * Displays one thread's Inspector snapshot and live event window.
+ * Snapshot requests share the live subscription's buffering lifecycle, including
+ * the initial request, because disk persistence can lag behind live delivery.
+ * Native expansion resets on a loaded snapshot: its positional keys are not identities.
+ */
export function InspectorPanel({ bot }: { bot: Bot }) {
const { dispatch } = useStore();
const { dragProps } = useCaptionChrome();
@@ -56,7 +62,6 @@ export function InspectorPanel({ bot }: { bot: Bot }) {
setPage(null);
setExpanded(new Set());
stickToBottom.current = true;
- void load();
return () => loadAbort.current?.abort();
}, [load]);
@@ -113,6 +118,7 @@ export function InspectorPanel({ bot }: { bot: Bot }) {
}
},
});
+ requestRefresh();
return () => {
alive = false;
if (managedRefresh.current === requestRefresh) managedRefresh.current = () => {};
diff --git a/src/components/live-panels.test.ts b/src/components/live-panels.test.ts
index a9ba016ae8..7e8cc09032 100644
--- a/src/components/live-panels.test.ts
+++ b/src/components/live-panels.test.ts
@@ -70,6 +70,54 @@ afterEach(() => {
});
describe("Inspector on the app's live stream", () => {
+ it("buffers distinct equal-time live events during the initial snapshot load", async () => {
+ let finish!: (response: Response) => void;
+ vi.stubGlobal("fetch", () => new Promise<Response>((resolve) => { finish = resolve; }));
+ await mount(InspectorPanel);
+ const events = [...document.querySelectorAll<HTMLButtonElement>('[role="tab"]')].find((tab) => tab.textContent === "events")!;
+ flushSync(() => events.click());
+ frame(runtime("initial-live", { type: "item.started", itemType: "tool", title: "equal time live tail" }));
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ finish(new Response(JSON.stringify({
+ entries: [{ kind: "runtime", at: "2026-10-04T09:00:00.000Z", data: runtime("initial-disk", { type: "item.started", itemType: "tool", title: "equal time disk row" }) }],
+ total: { runtime: 1, native: 0 },
+ })));
+ await vi.waitFor(() => expect(document.body.textContent).toContain("equal time disk row"));
+ await vi.waitFor(() => expect(document.body.textContent).toContain("equal time live tail"));
+ expect(document.querySelectorAll('button .tabular-nums')).toHaveLength(2);
+ expect(document.body.textContent).toContain("2 entries");
+ });
+
+ it("deduplicates live events already present in the initial snapshot", async () => {
+ let finish!: (response: Response) => void;
+ vi.stubGlobal("fetch", () => new Promise<Response>((resolve) => { finish = resolve; }));
+ await mount(InspectorPanel);
+ const events = [...document.querySelectorAll<HTMLButtonElement>('[role="tab"]')].find((tab) => tab.textContent === "events")!;
+ flushSync(() => events.click());
+ const overlap = runtime("initial-overlap", { type: "item.started", itemType: "tool", title: "initial overlap" });
+ frame(overlap);
+ finish(new Response(JSON.stringify({
+ entries: [{ kind: "runtime", at: overlap.createdAt, data: overlap }], total: { runtime: 40, native: 0 },
+ })));
+ await vi.waitFor(() => expect(document.body.textContent).toContain("initial overlap"));
+ await new Promise((resolve) => setTimeout(resolve, 30));
+ expect(document.querySelectorAll('button .tabular-nums')).toHaveLength(1);
+ expect(document.body.textContent).toContain("last 1 of 40");
+ });
+
+ it("keeps the initial live tail when the first snapshot fails", async () => {
+ let finish!: (response: Response) => void;
+ vi.stubGlobal("fetch", () => new Promise<Response>((resolve) => { finish = resolve; }));
+ await mount(InspectorPanel);
+ const events = [...document.querySelectorAll<HTMLButtonElement>('[role="tab"]')].find((tab) => tab.textContent === "events")!;
+ flushSync(() => events.click());
+ frame(runtime("initial-failed", { type: "item.started", itemType: "tool", title: "live despite failure" }));
+ finish(new Response("", { status: 503 }));
+ await vi.waitFor(() => expect(document.body.textContent).toContain("live despite failure"));
+ expect(document.body.textContent).toContain("503");
+ expect(document.querySelectorAll('button .tabular-nums')).toHaveLength(1);
+ });
+
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")!;
diff --git a/src/lib/inspector-window.ts b/src/lib/inspector-window.ts
index 1bfdff710a..1bbb1744d6 100644
--- a/src/lib/inspector-window.ts
+++ b/src/lib/inspector-window.ts
@@ -3,6 +3,11 @@ import type { InspectorEntry, InspectorPage } from "../../shared/inspector";
export const INSPECTOR_WINDOW_LIMIT = 400;
+/**
+ * Retains the latest window per log kind without changing their relative order.
+ * Counts belong to InspectorPage.total and are deliberately independent of this
+ * display window. The record limit does not bound individual payload sizes.
+ */
export function boundInspectorEntries(entries: InspectorEntry[]): InspectorEntry[] {
const counts = { runtime: 0, native: 0 };
const kept: InspectorEntry[] = [];
@@ -13,6 +18,12 @@ export function boundInspectorEntries(entries: InspectorEntry[]): InspectorEntry
return kept.length === entries.length ? entries : kept.reverse();
}
+/**
+ * Appends a live batch without mutating the published page and advances its total.
+ * Event IDs deduplicate against the retained window and this batch only; an
+ * unbounded replay history would defeat the Inspector's retention limit.
+ * Empty or fully overlapping batches preserve the original page reference.
+ */
export function appendInspectorRuntime(page: InspectorPage | null, events: RuntimeEvent[]): InspectorPage | null {
if (!events.length) return page;
const previous = page ?? { entries: [], total: { runtime: 0, native: 0 } };
@@ -30,6 +41,14 @@ export function appendInspectorRuntime(page: InspectorPage | null, events: Runti
};
}
+/**
+ * Reconciles a disk snapshot with the retained runtime tail when persistence lags.
+ * A shared event ID anchors publication order even for equal timestamps; without
+ * an overlap, only a later timestamp establishes that a retained event is newer.
+ * Events arriving during the request must be buffered separately by the caller
+ * and appended afterward, including distinct events with equal timestamps.
+ * Native records come from the snapshot, while the runtime total never decreases.
+ */
export function mergeInspectorSnapshot(previous: InspectorPage | null, snapshot: InspectorPage): InspectorPage {
const next = { ...snapshot, entries: boundInspectorEntries(snapshot.entries) };
if (!previous) return next;