Repository navigation
fix: cap agent session cache serialization to prevent perf regression (fixes #314404) - #314780
Draft
vs-code-engineering[bot] wants to merge 1 commit into
Draft
vs-code-engineering[bot] wants to merge 1 commit into
vs-code-engineering[bot] wants to merge 1 commit into
Conversation
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]>
vs-code-engineering
Bot
requested review from
Benjamin Pasero (bpasero) and
Copilot
and removed request for
Copilot
May 6, 2026 16:51
Contributor
Dmitriy Vasyura (dmitrivMS)
removed the request for review
from Benjamin Pasero (bpasero)
May 20, 2026 09:46
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
PerfSampleErroris thrown whensaveCachedSessions()callssafeStringify()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:
@bpaseroCulprit 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 firedAffected Files
src/vs/workbench/contrib/chat/browser/agentSessions/agentSessionsModel.tssaveCachedSessions()src/vs/base/common/objects.tssafeStringify/JSON.stringifysrc/vs/platform/storage/common/storage.tsflush()firesonWillSaveStateRepro Steps
PerfSampleErrorHow the Fix Works
Chosen approach (
src/vs/workbench/contrib/chat/browser/agentSessions/agentSessionsModel.ts):Added two caps to
saveCachedSessions():safeStringify).changesarray (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:
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 toagentSessionsModel.tsand agent session caching infrastructureerrors-fix-driver — cycle 22
Trigger: cron_merge_conflict · Head:
3a5a154dd346c27f9686006d1806d07dcb6d6d9c(3a5a154)agentSessionsModel.ts3a5a154— kept PR changes-per-session cap + main'slegacyResourcefieldPush: yes —
3a5a154· Copilot rerequested: okReady gate: CI pending (just pushed merge resolution) → not marking ready this cycle