Skip to content

fix: cap agent session cache serialization to prevent perf regression (fixes #314404) - #314780

Draft
vs-code-engineering[bot] wants to merge 1 commit into
mainfrom
fix/agent-sessions-perf-serialization-011181442c387ee3
Draft

vs-code-engineering[bot] wants to merge 1 commit into
mainfrom
fix/agent-sessions-perf-serialization-011181442c387ee3

Conversation

@vs-code-engineering

@vs-code-engineering vs-code-engineering Bot commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The PerfSampleError is thrown when saveCachedSessions() calls safeStringify() to serialize all agent session data synchronously during a storage flush triggered by window focus. With growing agent session usage, the serialization payload becomes large enough to block the main thread and trigger the performance profiler. This is a 52.5x spike in 1.118.1 affecting 477 users across related buckets.

Fixes #314404
Recommended reviewer: @bpasero

Culprit Commit

This is a pre-existing performance issue that surfaced as a spike due to increased agent session adoption and larger data payloads. The bucket shows hits as far back as v1.111.0. No single commit introduced a logic regression — the spike correlates with usage growth, not a code change.

Code Flow

sequenceDiagram
    participant Focus as Window Focus
    participant Storage as StorageService.flush
    participant Model as AgentSessionsModel
    participant Cache as AgentSessionsCache
    participant Stringify as safeStringify

    Focus->>Storage: focus event triggers flush
    Storage->>Model: onWillSaveState fires
    Model->>Cache: saveCachedSessions(all sessions)
    Note over Cache: ⚠️ Root cause: no limit on<br/>sessions or changes serialized
    Cache->>Stringify: safeStringify(serialized)
    Note over Stringify: 💥 JSON.stringify blocks main thread<br/>PerfSampleError fired
Loading

Affected Files

File Role
src/vs/workbench/contrib/chat/browser/agentSessions/agentSessionsModel.ts Root cause — unbounded serialization in saveCachedSessions()
src/vs/base/common/objects.ts Crash site — safeStringify / JSON.stringify
src/vs/platform/storage/common/storage.ts Trigger — flush() fires onWillSaveState

Repro Steps

  1. Open VS Code with a workspace containing many agent sessions (coding agent sessions with file changes)
  2. Run several agent sessions to accumulate data in the sessions model
  3. Switch away from VS Code and switch back (triggering window focus → storage flush)
  4. With enough sessions/metadata, the synchronous serialization triggers PerfSampleError

How the Fix Works

Chosen approach (src/vs/workbench/contrib/chat/browser/agentSessions/agentSessionsModel.ts):

Added two caps to saveCachedSessions():

  1. Session limit (25): Sort sessions by creation time (most recent first) and only serialize the 25 most recent. This bounds the total payload size regardless of how many sessions accumulate. Fix at the data producer — the function that decides what to serialize — not at the crash site (safeStringify).
  2. Changes-per-session limit (100): Cap the changes array (file change entries) to 100 per session. Sessions with many file changes (e.g., large coding agent runs) contribute disproportionately to payload size.

These limits ensure serialization time stays bounded on the main thread during storage flush, preventing the performance profiler from firing.

Alternatives considered:

  • Moving serialization to a worker — correct long-term fix but requires significant refactoring of the storage flush path which is synchronous by design.
  • Adding try/catch around safeStringify — would silence the error without fixing the performance issue (violates the principle of not masking errors from telemetry).

Recommended Owner

@bpasero — assigned owner, primary contributor to agentSessionsModel.ts and agent session caching infrastructure

errors-fix-driver — cycle 22

Trigger: cron_merge_conflict · Head: 3a5a154dd346c27f9686006d1806d07dcb6d6d9c (3a5a154)

Item Action
Merge conflict in agentSessionsModel.ts Resolved in 3a5a154 — kept PR changes-per-session cap + main's legacyResource field

Push: yes — 3a5a154 · Copilot rerequested: ok

Ready gate: CI pending (just pushed merge resolution) → not marking ready this cycle

Generated by errors-fix-driver · ● 37.2M · ◷

Limit the number of sessions serialized in saveCachedSessions to 25
(most recent first) and cap file changes per session to 100 entries.
This prevents safeStringify from blocking the main thread during
storage flush on window focus.

Fixes #314404

Co-authored-by: Copilot <[email protected]>
Copilot AI review requested due to automatic review settings May 6, 2026 16:51
@vs-code-engineering
vs-code-engineering Bot requested review from Benjamin Pasero (bpasero) and Copilot and removed request for Copilot May 6, 2026 16:51
@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

Screenshot Changes

Base: 20544b72 Current: c67f1efa

Changed (1)

chat/widget/chatWidget/PendingToolApproval/Light
Before After
before after

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Error] unhandlederror-PerfSampleError: by <<renderer>> in (anonymous)#workbench.desktop.main.js:414:16444

2 participants