src/vs/platform/agentHost/node/agentHostChangesetStateCache.ts

126 LOC · 112 covered · 14 uncovered · 29 ranges · 3131 concepts · 10 introducers · 1399 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- agentHostStateManager.ts ×60
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { LinkedMap, Touch } from '../../../base/common/map.js';
7 > import { ChangesetStatus, type ChangesetState, type URI } from '../common/state/sessionState.js';
8 >
9 > /**
10 > * Default number of expanded changeset states kept hot in memory.
11 > *
12 > * This cache only stores the subscribable `ChangesetState` payloads. The
13 > * lightweight catalogue on `SessionSummary.changesets` remains on the session
14 > * summary, and static changesets can be rehydrated from persisted metadata or
15 > * recomputed on demand. The limit is intentionally a soft cap: subscribed or
16 > * actively-computing changesets may pin the cache above this value until they
17 > * become evictable.
18 > */
19 > const DEFAULT_CHANGESET_STATE_SOFT_LIMIT = 500;
20 >
21 > export interface IAgentHostChangesetStateRetentionOptions {
22 > /**
23 > * Number of expanded changeset states kept hot in memory. The limit is soft:
24 > * entries for which {@link canEvict} returns false may temporarily keep the
25 > * cache above this value.
26 > */
27 > readonly softLimit?: number;
28 >
29 > /**
30 > * Returns whether a changeset state can be silently evicted from the cache.
31 > * Production callers should provide this from `AgentService`, which owns
32 > * protocol subscription refcounts and can ask the changeset service about
33 > * active producers. Return false for changesets that are subscribed or have
34 > * an active producer that may still publish into the changeset URI.
35 > */
36 > readonly canEvict?: (changeset: URI) => boolean;
37 > }
38 >
39 > /**
40 > * Owns the memory policy for expanded changeset states.
41 > *
42 > * The state manager owns protocol sequencing and reducer application; this
43 > * helper owns the cache mechanics needed to keep dormant changesets bounded.
44 > * Eviction here is deliberately silent: protocol-visible teardown still goes
45 > * through `AgentHostStateManager.disposeChangeset`, which emits
46 > * `ChangesetCleared` before removing state.
47 > */
48 > export class AgentHostChangesetStateCache {
49 >
50 > private readonly _states = new Map<string, ChangesetState>();
51 > private readonly _lru = new LinkedMap<string, true>();
52 > private readonly _softLimit: number;
53 > private readonly _canEvict: (changeset: URI) => boolean;
54 >
55 > constructor(options: IAgentHostChangesetStateRetentionOptions = {}) {
56 > this._softLimit = Math.max(0, options.softLimit ?? DEFAULT_CHANGESET_STATE_SOFT_LIMIT); agentHostStateManager.ts ×4
57 > this._canEvict = options.canEvict ?? (() => true);
58 > }
60 > keys(): IterableIterator<string> {
61 > return this._states.keys(); agentHostStateManager.ts ×7
62 > }
64 > has(changeset: URI): boolean {
65 > return this._states.has(changeset); agentHostChangesetStateCache.ts ×2
66 > }
68 > get(changeset: URI): ChangesetState | undefined {
69 > this._touch(changeset); agentHostChangesetStateCache.ts ×1
70 > return this._states.get(changeset);
71 > }
73 > set(changeset: URI, state: ChangesetState): void {
74 > this._states.set(changeset, state); agentHostChangesetStateCache.ts ×6
75 > this._touch(changeset);
76 > this._evictIfOverLimit();
77 > }
79 > delete(changeset: URI): void {
80 > this._states.delete(changeset); agentHostChangesetStateCache.ts ×2
81 > this._lru.delete(changeset);
82 > }
84 > register(changeset: URI, initialStatus: ChangesetStatus = ChangesetStatus.Computing): void {
85 > if (this._states.has(changeset)) { agentHostChangesetStateCache.ts ×6
86 > this._touch(changeset); agentHostChangesetStateCache.ts ×1
87 > return;
88 > }
89 > this.set(changeset, { status: initialStatus, files: [] }); agentHostChangesetStateCache.ts ×6
90 > }
92 > /** Re-runs eviction after external liveness changes, such as unsubscribe or compute completion. */
93 > trimEvictableEntries(): void {
94 > this._evictIfOverLimit(); agentHostChangesetStateCache.ts ×1
95 > }
97 > private _touch(changeset: URI): void {
98 > if (this._states.has(changeset)) { agentHostChangesetStateCache.ts ×2
99 > this._lru.set(changeset, true, Touch.AsNew); agentHostChangesetStateCache.ts ×6
100 > }
103 > private _evictIfOverLimit(): void {
104 > if (this._softLimit === 0) { agentHostChangesetStateCache.ts ×3
105 for (const changeset of [...this._lru.keys()]) {
106 if (this._canEvict(changeset)) {
107 this.delete(changeset);
108 }
109 }
110 return;
111 }
113 > for (const changeset of [...this._lru.keys()]) {
114 > if (this._states.size <= this._softLimit) { agentHostChangesetStateCache.ts ×6
115 > return;
116 > }
117 if (!this._states.has(changeset)) {
118 this._lru.delete(changeset);
119 continue;
120 }
121 if (this._canEvict(changeset)) {
122 this.delete(changeset);
123 }