refactor(app): extract session prefetch controller from layout.tsx - #1058
Conversation
Move the session message prefetch queue (LRU eviction, bounded concurrency, priority warm) out of pages/layout.tsx into pages/layout/pawwork-session-prefetch.ts as createPawworkSessionPrefetch. Mechanical extraction: reactive dependencies (params, route, currentDir, visibleSessionDirs, navigation sessions, global SDK/sync) are passed as a named dependency object; effects run under the Layout owner unchanged. No user-visible behavior change. Refs #1056
📝 WalkthroughWalkthroughSession prefetch logic is extracted from layout.tsx into a new dedicated module with queued concurrent fetching, LRU-based cache eviction, and Solid store reconciliation. Layout refactored to delegate prefetch orchestration to the new factory. ChangesSession Prefetch Module Extraction
Sequence DiagramsequenceDiagram
participant Navigation as Navigation / Route Change
participant prefetchSession
participant PrefetchQueue as Prefetch Queue
participant PumpPrefetch as pumpPrefetch
participant prefetchMessages as prefetchMessages
participant SDK as SDK Client
participant SolidStore as Solid Store
Navigation->>prefetchSession: prefetch current session (high priority)
prefetchSession->>PrefetchQueue: enqueue session, apply LRU/dedupe
prefetchSession->>prefetchSession: reorder pending by priority
PumpPrefetch->>PumpPrefetch: check concurrency limit
PumpPrefetch->>prefetchMessages: start async fetch task
prefetchMessages->>SDK: fetchSessionMessages with retry
SDK-->>prefetchMessages: messages + parts
prefetchMessages->>prefetchMessages: merge by id, compute cursor
prefetchMessages->>SolidStore: reconcile message list
prefetchMessages->>SolidStore: reconcile per-message parts
prefetchMessages->>prefetchMessages: setSessionPrefetch metadata
prefetchMessages-->>PumpPrefetch: on complete
PumpPrefetch->>PumpPrefetch: decrement counters, continue pump
Navigation->>prefetchSession: warm neighboring sessions
prefetchSession->>PrefetchQueue: enqueue nearby in span window
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Suggested priority: P2 (includes user-path files (packages/app/src/pages/layout.tsx, packages/app/src/pages/layout/pawwork-session-prefetch.ts)).
P1/P0 are reserved for maintainer confirmation. Please relabel manually if this is a release blocker, security issue, data-loss risk, or updater/runtime failure.
There was a problem hiding this comment.
Code Review
This pull request refactors the session prefetching logic in the layout component by extracting it from packages/app/src/pages/layout.tsx into a new dedicated helper file packages/app/src/pages/layout/pawwork-session-prefetch.ts. This improves code modularity and readability by removing over 200 lines of inline prefetching logic and cleaning up unused imports. I have no feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/app/src/pages/layout/pawwork-session-prefetch.ts`:
- Around line 62-67: The per-directory maps are using raw directory strings
causing duplicate keys for equivalent workspaces; normalize keys by calling
workspaceKey(directory) before accessing or setting entries in prefetchedByDir
and prefetchQueues (e.g., in the lruFor helper and the other map access sites
around the prefetch queue logic). Update lruFor to compute const key =
workspaceKey(directory) and use prefetchedByDir.get(key)/set(key, created), and
make the same change where prefetchQueues is read/modified (the blocks
referenced around lines 83-89 and 100-121) so all lookups/sets use
workspaceKey(...) instead of the raw directory string.
- Around line 91-98: The effect that runs in createEffect currently increments
prefetchToken and clears inflight state and prefetchQueues but fails to reset
the per-directory LRU snapshot, so old entries in prefetchedByDir continue to
count against PREFETCH_MAX_SESSIONS_PER_DIR; update the effect (the createEffect
block that touches input.route(), input.globalSDK.url, prefetchToken.value,
clearSessionPrefetchInflight(), prefetchQueues.clear()) to also reset/clear
prefetchedByDir (or reinitialize its LRU data structure) so prefetchSession()
sees an empty per-directory budget after a route/SDK reset.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: ebdd0000-44bd-4608-8d8c-861baf6ffeec
📒 Files selected for processing (2)
packages/app/src/pages/layout.tsxpackages/app/src/pages/layout/pawwork-session-prefetch.ts
|
Re CodeRabbit's two findings (raw-directory map keys;
Keeping this slice behavior-identical per #1056. If either is worth changing, it should be a separate prefetch bug PR with its own review, not bundled into the governance extraction. |
Summary
Extract the session message prefetch controller out of
pages/layout.tsxinto a focused module. Slice 1 of the layout governance split tracked in #1056 (carries on the #875 follow-up sequence).packages/app/src/pages/layout/pawwork-session-prefetch.tsexportscreatePawworkSessionPrefetch, owning the prefetch queue: per-directory queues, bounded concurrency, pending limit, LRU eviction, priority warm of neighboring sessions, and the route/visibility-driven effects.pages/layout.tsxnow calls the controller with a named dependency object and consumes the returned{ prefetchSession, warm }.Mechanical extraction: the moved code is verbatim, with external references rewired to injected reactive accessors (
params,route,currentDir,visibleSessionDirs, navigation sessions,globalSDK,globalSync). Effects still register under the Layout owner. No change to user-visible behavior, DOM, copy, aria labels, command IDs, storage keys, or styling.layout.tsx: 2204 -> 1960 lines.Touched files
packages/app/src/pages/layout.tsx— remove inlined prefetch block + now-unused imports; wire controllerpackages/app/src/pages/layout/pawwork-session-prefetch.ts— new controllerReview focus
Messagetype,retry, session-prefetch fns,pickSessionCacheEvictionsremoved from layout.tsx;dropSessionCacheskept).Verification
bun turbo typecheck— green (8/8)eslinton both changed files — cleanbun testlayout suite (src/pages/layout/*.test.ts(x)) — 158 pass / 0 failTests
No new test added: this is a verbatim extraction, and the core decision logic it orchestrates (
shouldSkipSessionPrefetch,runSessionPrefetch,pickSessionCacheEvictions) already lives in and is tested under@/context/global-sync/session-prefetchandsession-cache. Directly unit-testing the controller's stateful queue would require mocking several modules (cf. known mock.module leakage in #957), not worth it for a no-behavior-change move. The existing 158 layout tests guard regressions.Risk
Low. Mechanical move behind a stable interface. Main risk is a reactivity regression (a dep captured as a value), mitigated by passing accessors and verified via typecheck + existing tests.
Refs #1056
Summary by CodeRabbit