Skip to content

Commit 884ed9b

Browse files
npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7wpfleger96
andcommitted
fix(observer): eager archive hydration on panel open + 200-frame pages
On panel open the Activity feed showed only the live in-memory window (wiped on restart) because archived history requires manual scroll-up to page in — 50 frames at a time. A single code-review turn emits ~900 frames, so reaching session start required 18 scroll gestures nobody does, making archived history effectively invisible. Two changes fix this: A — eager initial hydration: add a hydration loop that fires automatically when the panel mounts (or channel switches) and calls fetchOlderArchived up to 10 pages = 2,000 frames before any user gesture. Reuses fetchOlderArchived's existing lock/cursor/backfill-await machinery; no parallel state machine. A new ps.initialHydrationDone flag (reset by applyChannelReset on channel switch) prevents duplicate concurrent loops. B — page size 50 → 200: observer frames are tiny; 50 was chat-message tuning. 200 frames/page means the 10-page budget covers 2,000 frames, more than enough for any single agent turn. The scroll sentinel in AgentSessionThreadPanel already works correctly (leave→enter gate, PRELOAD_MARGIN_PX=600) — scroll paging beyond the initial budget continues unchanged. Co-authored-by: Will Pfleger <[email protected]> Signed-off-by: Will Pfleger <[email protected]>
1 parent 659e6a8 commit 884ed9b

3 files changed

Lines changed: 213 additions & 6 deletions

File tree

‎desktop/src/features/agents/ingestArchivedObserverEvents.test.mjs‎

Lines changed: 159 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ describe("load-older cursor advance logic", () => {
354354

355355
it("test_short_page_signals_archive_exhausted", () => {
356356
// A page with fewer events than the limit signals end-of-archive.
357-
const PAGE_SIZE = 50;
357+
const PAGE_SIZE = 200;
358358
const page = Array.from({ length: 30 }, (_, i) => ({
359359
created_at: 1000 - i,
360360
}));
@@ -367,8 +367,8 @@ describe("load-older cursor advance logic", () => {
367367
});
368368

369369
it("test_full_page_signals_more_archive_available", () => {
370-
const PAGE_SIZE = 50;
371-
const page = Array.from({ length: 50 }, (_, i) => ({
370+
const PAGE_SIZE = 200;
371+
const page = Array.from({ length: 200 }, (_, i) => ({
372372
created_at: 1000 - i,
373373
}));
374374
const exhausted = page.length < PAGE_SIZE;
@@ -410,6 +410,11 @@ describe("archive paging state reset on channel change", () => {
410410
"backfillPromise is eagerly initialized",
411411
);
412412
assert.equal(ps.cursor, null, "cursor starts null");
413+
assert.equal(
414+
ps.initialHydrationDone,
415+
false,
416+
"initialHydrationDone starts false",
417+
);
413418
});
414419

415420
it("test_channel_switch_resets_cursor_exhaustion_and_fetch_lock", () => {
@@ -419,6 +424,7 @@ describe("archive paging state reset on channel change", () => {
419424
ps.cursor = { createdAt: 1000, id: "event-a5" };
420425
ps.hasOlderArchived = false; // channel A exhausted
421426
ps.isFetching = true; // mid-flight request (edge case)
427+
ps.initialHydrationDone = true; // hydration ran for channel A
422428
ps.backfillStatus = "done"; // backfill ran once already
423429
const originalPromise = ps.backfillPromise; // must survive reset
424430

@@ -436,6 +442,11 @@ describe("archive paging state reset on channel change", () => {
436442
false,
437443
"isFetching resets to false on channel switch",
438444
);
445+
assert.equal(
446+
ps.initialHydrationDone,
447+
false,
448+
"initialHydrationDone resets to false on channel switch so the new channel hydrates",
449+
);
439450

440451
// Backfill state must NOT be touched — it is identity-level and should
441452
// survive channel switches so the backfill only runs once per identity mount.
@@ -459,9 +470,10 @@ describe("archive paging state reset on channel change", () => {
459470
it("test_multiple_channel_switches_each_start_fresh", () => {
460471
const ps = createArchivePagingState();
461472

462-
// Switch to channel A: exhaust it.
473+
// Switch to channel A: exhaust it and complete hydration.
463474
ps.cursor = { createdAt: 500, id: "a-oldest" };
464475
ps.hasOlderArchived = false;
476+
ps.initialHydrationDone = true;
465477
applyChannelReset(ps);
466478

467479
assert.equal(ps.cursor, null, "switch A→B: cursor reset");
@@ -470,10 +482,16 @@ describe("archive paging state reset on channel change", () => {
470482
true,
471483
"switch A→B: hasOlderArchived reset",
472484
);
485+
assert.equal(
486+
ps.initialHydrationDone,
487+
false,
488+
"switch A→B: initialHydrationDone reset",
489+
);
473490

474-
// Simulate channel B also being paged.
491+
// Simulate channel B also being paged and hydrated.
475492
ps.cursor = { createdAt: 200, id: "b-oldest" };
476493
ps.hasOlderArchived = false;
494+
ps.initialHydrationDone = true;
477495
applyChannelReset(ps);
478496

479497
assert.equal(ps.cursor, null, "switch B→C: cursor reset again");
@@ -482,6 +500,142 @@ describe("archive paging state reset on channel change", () => {
482500
true,
483501
"switch B→C: hasOlderArchived reset again",
484502
);
503+
assert.equal(
504+
ps.initialHydrationDone,
505+
false,
506+
"switch B→C: initialHydrationDone reset again",
507+
);
508+
});
509+
});
510+
511+
// ── Eager initial hydration loop logic ───────────────────────────────────────
512+
//
513+
// The initial hydration loop in useLoadArchivedObserverEvents calls
514+
// fetchOlderArchived up to INITIAL_HYDRATION_BUDGET_PAGES times. The loop must:
515+
// - Stop at budget (10 pages) even if more archive exists.
516+
// - Stop early when the archive is exhausted (ps.hasOlderArchived → false).
517+
// - Respect channel-switch cancellation (cancelled flag).
518+
// - Set ps.initialHydrationDone immediately to prevent duplicate loops.
519+
//
520+
// We test the loop's control flow as pure logic using mock fetchOlderArchived
521+
// functions and ArchivePagingState directly — no React runtime needed.
522+
523+
describe("eager initial hydration loop control flow", () => {
524+
const BUDGET = 10; // mirrors INITIAL_HYDRATION_BUDGET_PAGES
525+
526+
// Simulate the hydration loop from the hook with injectable fetch + ps.
527+
async function runHydrationLoop(
528+
ps,
529+
fetchOlderArchived,
530+
cancelled = { value: false },
531+
) {
532+
ps.initialHydrationDone = true;
533+
for (let page = 0; page < BUDGET; page++) {
534+
if (cancelled.value || !ps.hasOlderArchived) {
535+
break;
536+
}
537+
await fetchOlderArchived();
538+
}
539+
}
540+
541+
it("test_hydration_stops_at_budget_when_archive_never_exhausted", async () => {
542+
const ps = createArchivePagingState();
543+
let fetchCount = 0;
544+
// fetchOlderArchived that never exhausts the archive
545+
const fetchOlderArchived = async () => {
546+
fetchCount++;
547+
// archive remains non-empty
548+
};
549+
await runHydrationLoop(ps, fetchOlderArchived);
550+
assert.equal(
551+
fetchCount,
552+
BUDGET,
553+
`hydration loop must stop after exactly ${BUDGET} pages (budget limit)`,
554+
);
555+
assert.equal(
556+
ps.initialHydrationDone,
557+
true,
558+
"initialHydrationDone must be true after budget reached",
559+
);
560+
});
561+
562+
it("test_hydration_stops_early_when_archive_exhausted", async () => {
563+
const ps = createArchivePagingState();
564+
let fetchCount = 0;
565+
// fetchOlderArchived that exhausts archive on page 3
566+
const fetchOlderArchived = async () => {
567+
fetchCount++;
568+
if (fetchCount >= 3) {
569+
ps.hasOlderArchived = false;
570+
}
571+
};
572+
await runHydrationLoop(ps, fetchOlderArchived);
573+
assert.equal(
574+
fetchCount,
575+
3,
576+
"hydration loop must stop as soon as archive is exhausted (before budget)",
577+
);
578+
assert.equal(
579+
ps.initialHydrationDone,
580+
true,
581+
"initialHydrationDone must be true after exhaustion",
582+
);
583+
assert.equal(
584+
ps.hasOlderArchived,
585+
false,
586+
"hasOlderArchived must be false after exhaustion",
587+
);
588+
});
589+
590+
it("test_hydration_respects_cancellation_on_channel_switch", async () => {
591+
const ps = createArchivePagingState();
592+
const cancelled = { value: false };
593+
let fetchCount = 0;
594+
// fetchOlderArchived that sets cancelled on page 2 (simulates channel switch mid-loop)
595+
const fetchOlderArchived = async () => {
596+
fetchCount++;
597+
if (fetchCount >= 2) {
598+
cancelled.value = true;
599+
}
600+
};
601+
await runHydrationLoop(ps, fetchOlderArchived, cancelled);
602+
assert.equal(
603+
fetchCount,
604+
2,
605+
"hydration loop must stop when cancelled (channel switch)",
606+
);
607+
// initialHydrationDone is true because it was set before the loop;
608+
// applyChannelReset will reset it for the new channel.
609+
assert.equal(
610+
ps.initialHydrationDone,
611+
true,
612+
"initialHydrationDone stays true (applyChannelReset will reset it for new channel)",
613+
);
614+
});
615+
616+
it("test_initialHydrationDone_set_before_loop_prevents_duplicate_loops", () => {
617+
const ps = createArchivePagingState();
618+
assert.equal(ps.initialHydrationDone, false, "starts false");
619+
// Simulate the synchronous guard at the top of the effect.
620+
ps.initialHydrationDone = true;
621+
// A second effect invocation would see initialHydrationDone=true and bail immediately.
622+
// Verify the guard value is set before any async work starts.
623+
assert.equal(
624+
ps.initialHydrationDone,
625+
true,
626+
"initialHydrationDone must be true immediately after loop starts (prevents re-entry)",
627+
);
628+
});
629+
630+
it("test_channel_reset_clears_hydration_flag_for_new_channel", () => {
631+
const ps = createArchivePagingState();
632+
ps.initialHydrationDone = true; // hydration ran for channel A
633+
applyChannelReset(ps);
634+
assert.equal(
635+
ps.initialHydrationDone,
636+
false,
637+
"applyChannelReset must clear initialHydrationDone so new channel gets fresh hydration pass",
638+
);
485639
});
486640
});
487641

‎desktop/src/features/agents/ui/archivePagingState.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export interface ArchivePagingState {
2828
* Mirrors SQL ORDER BY created_at DESC, id DESC so same-second siblings are
2929
* never skipped at a page boundary. */
3030
cursor: { createdAt: number; id: string } | null;
31+
/** True once the initial eager-hydration pass for the current channel has
32+
* completed (budget reached or archive exhausted). Resets on channel change
33+
* so switching channels triggers a fresh hydration pass. */
34+
initialHydrationDone: boolean;
3135
}
3236

3337
/**
@@ -43,6 +47,7 @@ export function createArchivePagingState(): ArchivePagingState {
4347
backfillPromise: null,
4448
backfillResolve: null,
4549
cursor: null,
50+
initialHydrationDone: false,
4651
};
4752
state.backfillPromise = new Promise<void>((resolve) => {
4853
state.backfillResolve = resolve;
@@ -64,4 +69,5 @@ export function applyChannelReset(state: ArchivePagingState): void {
6469
state.cursor = null;
6570
state.isFetching = false;
6671
state.hasOlderArchived = true;
72+
state.initialHydrationDone = false;
6773
}

‎desktop/src/features/agents/ui/useObserverEvents.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ export function useArchivedChannelEvents(
8888
return React.useSyncExternalStore(subscribeToStore, getSnapshot);
8989
}
9090

91-
const ARCHIVED_EVENTS_PAGE_SIZE = 50;
91+
const ARCHIVED_EVENTS_PAGE_SIZE = 200;
92+
93+
// Number of pages to load eagerly on panel open (before any scroll). Each page
94+
// is ARCHIVED_EVENTS_PAGE_SIZE frames; 10 pages = 2000 frames, which covers
95+
// agent turns that emit hundreds of frames (e.g. a full code-review turn ~900).
96+
const INITIAL_HYDRATION_BUDGET_PAGES = 10;
9297

9398
/**
9499
* Load-older-on-scroll for archived observer frames, scoped to a single channel.
@@ -318,5 +323,47 @@ export function useLoadArchivedObserverEvents(
318323
}
319324
}, [enabled, identityPubkey, hasSubscription, channelId, hasOlderArchived]);
320325

326+
// Eager initial hydration: on panel open (or channel switch), load archive
327+
// pages automatically until the budget is reached or the channel is exhausted.
328+
// This makes archived history visible immediately without any scrolling.
329+
//
330+
// Runs when: enabled + subscription confirmed + channelId resolved +
331+
// hydration not yet done for this channel. Respects `applyChannelReset`
332+
// (which resets initialHydrationDone) so channel switches trigger a fresh
333+
// pass. Uses fetchOlderArchived's existing lock/cursor/backfill-await
334+
// machinery — no parallel state machine.
335+
// biome-ignore lint/correctness/useExhaustiveDependencies: ps is a stable ref; initialHydrationDone from ps is read inside the effect via ps.initialHydrationDone (not as a reactive dep) to avoid triggering re-runs; the effect re-runs on the deps that actually indicate a new hydration is needed
336+
React.useEffect(() => {
337+
if (
338+
!enabled ||
339+
!identityPubkey ||
340+
!hasSubscription ||
341+
!channelId ||
342+
ps.initialHydrationDone
343+
) {
344+
return;
345+
}
346+
347+
let cancelled = false;
348+
// Mark running immediately to prevent concurrent hydration loops if
349+
// fetchOlderArchived identity changes mid-run (it changes when hasOlderArchived
350+
// flips). The loop completes asynchronously; cancelled handles cleanup.
351+
ps.initialHydrationDone = true;
352+
const runHydration = async () => {
353+
for (let page = 0; page < INITIAL_HYDRATION_BUDGET_PAGES; page++) {
354+
if (cancelled || !ps.hasOlderArchived) {
355+
break;
356+
}
357+
await fetchOlderArchived();
358+
// If fetchOlderArchived exhausted the channel, ps.hasOlderArchived is
359+
// now false — the loop will exit on the next iteration check.
360+
}
361+
};
362+
void runHydration();
363+
return () => {
364+
cancelled = true;
365+
};
366+
}, [enabled, identityPubkey, hasSubscription, channelId, fetchOlderArchived]);
367+
321368
return { fetchOlderArchived, hasOlderArchived };
322369
}

0 commit comments

Comments
 (0)