Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(chat): page through the derived transcript instead of stopping at 500 items

Shanub83d1aef51d0
diff --git a/app/src/providers/__tests__/useCoreTranscriptProjection.test.tsx b/app/src/providers/__tests__/useCoreTranscriptProjection.test.tsx
new file mode 100644
index 0000000000..412e3da815
--- /dev/null
+++ b/app/src/providers/__tests__/useCoreTranscriptProjection.test.tsx
@@ -0,0 +1,79 @@
+import { renderHook, waitFor } from '@testing-library/react';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { threadApi } from '../../services/api/threadApi';
+import type { DerivedDisplayItem, DerivedTranscriptPage } from '../../types/derivedTranscript';
+import { useCoreTranscriptProjection } from '../useOpenHumanExternalStore';
+
+vi.mock('../../services/api/threadApi', () => ({ threadApi: { getDerivedTranscript: vi.fn() } }));
+
+const THREAD = 'thread-long';
+
+/** Newest-first items for one settled turn: a tool call after its boundary. */
+function turn(requestId: string, callId: string): DerivedDisplayItem[] {
+ return [
+ { kind: 'toolCall', callId, name: 'web_search_tool', status: 'success', result: 'ok' },
+ { kind: 'assistantMessage', content: `answer ${requestId}`, requestId, iteration: 1 },
+ { kind: 'turnBoundary', requestId },
+ ];
+}
+
+function page(over: Partial<DerivedTranscriptPage>): DerivedTranscriptPage {
+ return { threadId: THREAD, items: [], total: 0, hasMore: false, hasTranscript: true, ...over };
+}
+
+describe('useCoreTranscriptProjection paging', () => {
+ beforeEach(() => {
+ vi.mocked(threadApi.getDerivedTranscript).mockReset();
+ });
+
+ it('walks every older page and projects the whole history', async () => {
+ vi.mocked(threadApi.getDerivedTranscript)
+ .mockResolvedValueOnce(
+ page({ items: turn('r-new', 'call-new'), total: 6, hasMore: true, nextCursor: 'c1' })
+ )
+ .mockResolvedValueOnce(page({ items: turn('r-old', 'call-old'), total: 6, hasMore: false }));
+
+ const { result } = renderHook(() => useCoreTranscriptProjection(THREAD, 'rev-1', undefined));
+
+ await waitFor(() => expect(Object.keys(result.current.timelines)).toHaveLength(2));
+ expect(threadApi.getDerivedTranscript).toHaveBeenCalledTimes(2);
+ expect(threadApi.getDerivedTranscript).toHaveBeenLastCalledWith(THREAD, {
+ limit: 500,
+ cursor: 'c1',
+ });
+ expect(result.current.timelines['r-old']?.[0]).toMatchObject({ id: 'call-old' });
+ expect(result.current.timelines['r-new']?.[0]).toMatchObject({ id: 'call-new' });
+ });
+
+ it('stops when the core reports no more pages', async () => {
+ vi.mocked(threadApi.getDerivedTranscript).mockResolvedValueOnce(
+ page({ items: turn('r-only', 'call-only'), total: 3, hasMore: false })
+ );
+
+ const { result } = renderHook(() => useCoreTranscriptProjection(THREAD, 'rev-1', undefined));
+
+ await waitFor(() => expect(Object.keys(result.current.timelines)).toEqual(['r-only']));
+ expect(threadApi.getDerivedTranscript).toHaveBeenCalledTimes(1);
+ });
+
+ it('drops a page that lands after the thread changed', async () => {
+ const older = new Promise<DerivedTranscriptPage>(() => {
+ /* never resolves: the walk is still in flight when the thread switches */
+ });
+ vi.mocked(threadApi.getDerivedTranscript)
+ .mockResolvedValueOnce(
+ page({ items: turn('r-new', 'call-new'), total: 6, hasMore: true, nextCursor: 'c1' })
+ )
+ .mockReturnValueOnce(older);
+
+ const { result, rerender } = renderHook(
+ ({ thread }) => useCoreTranscriptProjection(thread, 'rev-1', undefined),
+ { initialProps: { thread: THREAD as string | null } }
+ );
+ await waitFor(() => expect(Object.keys(result.current.timelines)).toEqual(['r-new']));
+
+ rerender({ thread: null });
+ expect(result.current.timelines).toEqual({});
+ });
+});
diff --git a/app/src/providers/useOpenHumanExternalStore.ts b/app/src/providers/useOpenHumanExternalStore.ts
index 92c88f80f4..47b88c1840 100644
--- a/app/src/providers/useOpenHumanExternalStore.ts
+++ b/app/src/providers/useOpenHumanExternalStore.ts
@@ -4,6 +4,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
import { mapDisplayItems } from '../features/conversations/derived/mapDisplayItems';
import { threadApi } from '../services/api/threadApi';
import { useAppSelector } from '../store/hooks';
+import type { DerivedDisplayItem } from '../types/derivedTranscript';
import type { ThreadMessage } from '../types/thread';
import { buildRuntimeMessages } from './assistantUiMessages';
import { getChatSurface } from './chatSurfaceHandlers';
@@ -12,6 +13,15 @@ const EMPTY_MESSAGES: ThreadMessage[] = [];
const EMPTY_TIMELINE: never[] = [];
const EMPTY_TRANSCRIPT: never[] = [];
const EMPTY_TURN_MAP = {};
+/** Items per derived-transcript RPC page; the core caps a page at this size. */
+const DERIVED_TRANSCRIPT_PAGE_LIMIT = 500;
+/**
+ * Upper bound on pages walked for one thread (10k items). A thread longer than
+ * this is truncated at its oldest end rather than fetched without limit; the
+ * turn-bounded RPC contract that removes the ceiling altogether is tracked with
+ * the transcript RPC, not here.
+ */
+const DERIVED_TRANSCRIPT_MAX_PAGES = 20;
type CoreTranscriptProjection = {
threadId: string | null;
@@ -49,25 +59,51 @@ export function useCoreTranscriptProjection(
return;
}
let cancelled = false;
- void threadApi
- .getDerivedTranscript(threadId, { limit: 500 })
- .then(page => {
+ const skipRequestIds = liveRequestId ? new Set([liveRequestId]) : undefined;
+ const project = (items: DerivedDisplayItem[]) => {
+ const mapped = mapDisplayItems(items, { skipRequestIds });
+ setProjection({ threadId, timelines: mapped.timelines, transcripts: mapped.transcripts });
+ };
+ void (async () => {
+ try {
+ const first = await threadApi.getDerivedTranscript(threadId, {
+ limit: DERIVED_TRANSCRIPT_PAGE_LIMIT,
+ });
if (cancelled) return;
- if (!page.hasTranscript) {
+ if (!first.hasTranscript) {
setProjection({ threadId, timelines: EMPTY_TURN_MAP, transcripts: EMPTY_TURN_MAP });
return;
}
- const skipRequestIds = liveRequestId ? new Set([liveRequestId]) : undefined;
- const mapped = mapDisplayItems(page.items, { skipRequestIds });
- setProjection({ threadId, timelines: mapped.timelines, transcripts: mapped.transcripts });
- })
- .catch(() => {
+ // Paint the newest page immediately, then walk the older pages and
+ // re-project once with the whole history. A single page silently
+ // dropped everything older than 500 items on a long thread, and a
+ // page that begins mid-turn hides that turn's leading tool calls until
+ // its boundary is in view — both only resolve with the full list.
+ let items = first.items;
+ project(items);
+ let cursor = first.hasMore ? first.nextCursor : undefined;
+ let pages = 1;
+ while (cursor && pages < DERIVED_TRANSCRIPT_MAX_PAGES) {
+ const page = await threadApi.getDerivedTranscript(threadId, {
+ limit: DERIVED_TRANSCRIPT_PAGE_LIMIT,
+ cursor,
+ });
+ if (cancelled) return;
+ // Pages are newest-first and each next page is older, so appending
+ // keeps the newest-first order `mapDisplayItems` expects.
+ items = [...items, ...page.items];
+ pages += 1;
+ cursor = page.hasMore ? page.nextCursor : undefined;
+ }
+ if (pages > 1) project(items);
+ } catch {
// A missing/older core has no settled process trail; message text and
// the live socket projection remain usable. Navigation must not fail.
if (!cancelled) {
setProjection({ threadId, timelines: EMPTY_TURN_MAP, transcripts: EMPTY_TURN_MAP });
}
- });
+ }
+ })();
return () => {
cancelled = true;
};