Skip to content

perf(sessions): persist conversation build caches across task re-opens - #76788

Open
arnohillen wants to merge 3 commits into
masterfrom
posthog-code/persist-conversation-derived-cache
Open

perf(sessions): persist conversation build caches across task re-opens#76788
arnohillen wants to merge 3 commits into
masterfrom
posthog-code/persist-conversation-derived-cache

Conversation

@arnohillen

Copy link
Copy Markdown
Contributor

Ports PostHog/code#3976 onto products/desktop/ after the desktop import (the code repo's main is frozen). Original commits and authorship are preserved via git am.

Problem

Clicking a task in the sidebar takes seconds before the transcript renders, even when the session data is already warm in the store. The task detail route fully unmounts on navigation, and the incremental conversation builder (useConversationItems) and thread grouper both live in component-scoped refs, so every re-open falls back to a full O(n) re-parse of the transcript on the main thread. yieldToPaint already documents the mount blocking "for hundreds of ms"; with multi-MB transcripts it is seconds.

Changes

  • New conversationDerivedCache module: a small LRU cache (8 tasks per scope, backed by lru-cache, already a workspace dependency via harness) keyed per call site + task that keeps the incremental builder state and thread grouper alive across mounts.
  • useConversationItems accepts an optional persist key; without one, behavior is unchanged (per-component ref).
  • ConversationView and AcpChatThread opt in with distinct scopes; ChatThreadFooter now receives footerState and usage from AcpChatThread instead of running its own duplicate parse.
  • Entries are swept when the session residency system evicts a session's events (or the session is torn down), so the memory reclaim that eviction exists for still works. Entries for tasks that never had a session in the store (e.g. archive surfaces) are exempt from sweeping and reclaim via a 30 minute TTL instead.

Warm re-opens (session still in the store) now reuse already-built items: idle sessions return the memoized result identity, and streaming sessions take the append-only fast path because the store keeps appending while the view is unmounted and immer preserves element identity.

Compared to the source PR, the only monorepo-specific change is the lockfile: instead of carrying the source pnpm-lock.yaml hunk blindly, the products/desktop/pnpm-lock.yaml importer entry was regenerated for the new packages/ui dependency (same resolution, lru-cache@11.2.5).

How did you test this code?

  • Unit tests for the cache module: entry persistence, scope isolation, LRU bound, eviction sweep on evictEvents/removeSession, and the no-session exemption (guards rebuild churn on archive surfaces).
  • Hook tests: identical result identity across remount with a persist key (idle), completed-turn item reuse across remount while streaming, and unchanged rebuild behavior without a persist key.
  • Verified in the monorepo from products/desktop/: pnpm install --frozen-lockfile, pnpm typecheck (24/24 packages), and the full @posthog/ui vitest suite (305 files, 2519 tests passing).

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Docs update

Not applicable (desktop app internals), skip-inkeep-docs label applied.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

This PR is a port of PostHog/code#3976, recreated here by PostHog Code following the /porting-code-prs skill: patch series applied with git am --directory=products/desktop/ (original authorship and commit messages preserved), source lockfile hunk excluded and re-derived against the monorepo lockfile instead. Part 1 of 3 ported PRs on task re-open latency.


Created with PostHog Code

@arnohillen arnohillen added the skip-inkeep-docs Use this label to skip an Inkeep docs PR in posthog.com label Aug 3, 2026
@arnohillen arnohillen self-assigned this Aug 3, 2026
@arnohillen arnohillen added the skip-inkeep-docs Use this label to skip an Inkeep docs PR in posthog.com label Aug 3, 2026
@trunk-io

trunk-io Bot commented Aug 3, 2026

Copy link
Copy Markdown

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@greptile-apps

greptile-apps Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "chore(sessions): add lru-cache to the ui..." | Re-trigger Greptile

@trunk-io

trunk-io Bot commented Aug 3, 2026

Copy link
Copy Markdown

Static BadgeStatic BadgeStatic Badge

View Full Report ↗︎Docs

@scheduled-actions-posthog

Copy link
Copy Markdown
Contributor

This PR hasn't seen activity in a week! Should it be merged, closed, or further worked on? If you want to keep it open, please remove the stale label – otherwise this will be closed in another week. If you want to permanently keep it open, use the waiting label.

Generated-By: PostHog Desktop
Task-Id: 6ee186ae-f972-424a-9636-b747d12e9cc8
@arnohillen
arnohillen force-pushed the posthog-code/persist-conversation-derived-cache branch from feaf227 to 58d2773 Compare August 13, 2026 11:07
@github-actions

github-actions Bot commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

React Doctor found 2 issues in 2 files · 2 errors.

Errors

Reviewed by React Doctor for commit 730e2d0.

@hosthog

hosthog Bot commented Aug 16, 2026

Copy link
Copy Markdown

HostHog preview — posthog-desktop-web

Latest build (730e2d0): https://5991204f1a3e439ca7e0b4dc232613f8.hosthog.dev

Employee-gated; every push gets a fresh URL whose content never changes. All previews stop serving when the PR closes.

Keeps the persistent conversation build cache and master's transcript stall
logging: the stall-detection fields now live on ConversationBuildCache, so
they survive a task re-open along with the builder.

Generated-By: PostHog Desktop
Task-Id: 341e450d-132c-42dd-abf2-52b22f99c3f6
@github-actions

Copy link
Copy Markdown
Contributor

🤖 CI report

Trunk lane — does not run backend Python tests

This PR is assigned to a lane that does not run backend Python tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-inkeep-docs Use this label to skip an Inkeep docs PR in posthog.com

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant