Skip to content

perf(desktop): reduce task runtime CPU and process leaks - #85349

Open
richardsolomou wants to merge 14 commits into
masterfrom
port-code-pr-3807
Open

perf(desktop): reduce task runtime CPU and process leaks#85349
richardsolomou wants to merge 14 commits into
masterfrom
port-code-pr-3807

Conversation

@richardsolomou

Copy link
Copy Markdown
Member

Problem

Desktop users can see rising CPU use during long task sessions, and cancelled tasks can leave descendant processes running.

PostHog/code#3807 was closed after PostHog Code moved into the monorepo.

Changes

  • Long-running desktop tasks now do less repeated work while keeping final messages and status changes immediate.
  • Streamed session updates batch for 50 ms, while completion, permission, and runtime state events remain immediate.
  • Conversation derivation reuses unchanged rows, so active turns rerender only where content changed.
  • Narrower subscriptions, slower watchers, cached cloud state, and capped diff workers reduce background work.
  • Process cleanup terminates nested process groups and validates process identity before delayed signals.
  • The port preserves the monorepo's newer chat grouping, session diagnostics, and callback interfaces.

Before:

flowchart LR
    Event[Stream event] --> Handle[Handle event]
    Handle --> Store[Write session]
    Store --> Render[Rebuild and render conversation]
    classDef phBlue fill:#1d4aff,stroke:#1d4aff,color:#fff;
    classDef phYellow fill:#f9bd2b,stroke:#f9bd2b,color:#000;
    classDef phGray fill:#e5e7eb,stroke:#c7ccd1,color:#000;
    class Event phYellow;
    class Handle phBlue;
    class Store,Render phGray;
Loading

After:

flowchart LR
    Stream[Stream event] --> Batch[50 ms batch]
    Batch --> Store[Write session once]
    Store --> Rows[Update changed rows]
    Immediate[Final or state event] --> Handle[Handle immediately]
    Handle --> Store
    classDef phBlue fill:#1d4aff,stroke:#1d4aff,color:#fff;
    classDef phYellow fill:#f9bd2b,stroke:#f9bd2b,color:#000;
    classDef phGray fill:#e5e7eb,stroke:#c7ccd1,color:#000;
    class Stream,Immediate phYellow;
    class Batch,Handle phBlue;
    class Store,Rows phGray;
Loading
  • The elapsed timer updates in whole seconds, and the pulse uses a stepped opacity change.
  • No screenshot is included because Electron could not launch after the local better-sqlite3 native build failed under Node 24.

How did you test this code?

  • pnpm --filter @posthog/core test
  • pnpm --filter @posthog/ui test
  • pnpm --filter @posthog/workspace-server test
  • pnpm typecheck
  • pnpm build:deps
  • hogli ci:preflight --strict
  • node scripts/check-host-boundaries.mjs
  • Not run: manual Electron verification because local native dependency setup failed before launch.

👉 Stay up-to-date with PostHog coding conventions for a smoother review.

Automatic notifications

  • Publish to changelog?

Docs update

N/A. This changes internal desktop runtime behavior and does not change a documented workflow.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Codex in Conductor ported PostHog/code#3807 after Richard requested the monorepo migration. No shareable session link is available.

Skills: posthog-desktop, writing-tests, rs-ship, rs-update-pr, writing-pr-descriptions, and rs-tone.

The requested porting-code-prs skill was unavailable, so the port used the repository layout and current desktop guidance.

Conflicts were resolved against the current chat grouping, session diagnostics, and callback interfaces. The source was public, and no private session material was used.

@trunk-io

trunk-io Bot commented Aug 19, 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

@richardsolomou richardsolomou added the skip-inkeep-docs Use this label to skip an Inkeep docs PR in posthog.com label Aug 19, 2026
@github-actions github-actions Bot added the feature/desktop Feature Tag: Desktop label Aug 19, 2026
@github-actions

github-actions Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

🤖 CI report

Trunk lane — non-backend lane

This PR is assigned to the non-backend lane. It does not run backend Python tests and may merge in parallel with PRs in other lanes.

@github-actions

github-actions Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

React Doctor found 3 issues in 2 files · 3 warnings.

3 warnings

packages/ui/src/features/sessions/components/GeneratingIndicator.tsx

packages/ui/src/features/sessions/components/session-update/StatusNotificationView.tsx

Reviewed by React Doctor for commit 14ad73a.

@hosthog

hosthog Bot commented Aug 19, 2026

Copy link
Copy Markdown

HostHog preview — posthog-desktop-web

Latest build (14ad73a): https://a37380da9d27484abebfdcdf8b370b46.hosthog.dev

Earlier builds of this PR, still serving:

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

@trunk-io

trunk-io Bot commented Aug 19, 2026

Copy link
Copy Markdown

Static BadgeStatic BadgeStatic Badge

View Full Report ↗︎Docs

@richardsolomou
richardsolomou requested a review from a team August 19, 2026 12:09
@richardsolomou
richardsolomou marked this pull request as ready for review August 19, 2026 13:29
@greptile-apps

greptile-apps Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Comments Outside Diff (1)

  1. products/desktop/packages/core/src/sessions/sessionEventBatching.test.ts, line 29 (link)

    P2 ACP fixtures bypass type validation

    The new ACP event helpers cast incomplete objects through unknown instead of supplying the required AcpMessage properties. This allows the fixtures to drift from the runtime protocol shape and prevents future required-field changes from producing type errors; the same pattern also appears in cloudToolChanges.test.ts.

    Rule Used: When creating mock objects for tests, include all ... (source)

    Learned From
    PostHog/posthog#32521

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: products/desktop/packages/core/src/sessions/sessionEventBatching.test.ts
    Line: 29
    
    Comment:
    **ACP fixtures bypass type validation**
    
    The new ACP event helpers cast incomplete objects through `unknown` instead of supplying the required `AcpMessage` properties. This allows the fixtures to drift from the runtime protocol shape and prevents future required-field changes from producing type errors; the same pattern also appears in `cloudToolChanges.test.ts`.
    
    **Rule Used:** When creating mock objects for tests, include all ... ([source](https://app.greptile.com/posthog-org-19734/-/custom-context?memory=693b8339-f300-4cf5-bacb-3f1630384594))
    
    **Learned From**
    [PostHog/posthog#32521](https://github.com/PostHog/posthog/pull/32521)
    
    ---
    
    For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
Prompt To Fix All With AI
### Issue 1
products/desktop/packages/core/src/sessions/sessionEventBatching.test.ts:29
**ACP fixtures bypass type validation**

The new ACP event helpers cast incomplete objects through `unknown` instead of supplying the required `AcpMessage` properties. This allows the fixtures to drift from the runtime protocol shape and prevents future required-field changes from producing type errors; the same pattern also appears in `cloudToolChanges.test.ts`.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "test(desktop): cover displaced transcrip..." | Re-trigger Greptile

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

Labels

feature/desktop Feature Tag: Desktop 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