src/vs/platform/agentHost/common/pendingRequestRegistry.ts

136 LOC · 130 covered · 6 uncovered · 32 ranges · 1317 concepts · 16 introducers · 641 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 > /*--------------------------------------------------------------------------------------------- pendingRequestRegistry.ts ×9
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 { DeferredPromise } from '../../../base/common/async.js';
7 > import { CancellationError } from '../../../base/common/errors.js';
8 >
9 > /**
10 > * Registry of parked deferred promises keyed by string id. Used to
11 > * track request/response round-trips where a callback fires a signal
12 > * that an external responder may resolve synchronously.
13 > *
14 > * The atomic register-then-fire is enforced by {@link registerAndFire}
15 > * rather than by convention: a synchronous responder (e.g.
16 > * `agentSideEffects.ts:_handleToolReady` auto-approving writes inside
17 > * the listener for the fired signal) registered AFTER the fire would
18 > * miss its response and the awaited promise would deadlock — a
19 > * regression caught in Claude phase 7.
20 > */
21 > export class PendingRequestRegistry<T> {
22 > private readonly _entries = new Map<string, DeferredPromise<T>>(); pendingRequestRegistry.ts ×1
23 > /**
24 > * Results delivered via {@link respondOrBuffer} before any deferred was
25 > * parked under the same key. A subsequent {@link register} consumes the
26 > * buffered value and resolves immediately, tolerating a completion that
27 > * races ahead of the handler that awaits it.
28 > */
29 > private readonly _earlyResults = new Map<string, T>();
31 > registerAndFire(key: string, fire: () => void): Promise<T> {
32 > if (this._earlyResults.has(key)) { pendingRequestRegistry.ts ×2
33 const buffered = this._earlyResults.get(key) as T;
34 this._earlyResults.delete(key);
35 return Promise.resolve(buffered);
36 }
37 > const deferred = new DeferredPromise<T>(); pendingRequestRegistry.ts ×2
38 > this._entries.set(key, deferred);
39 > fire();
40 > return deferred.p;
41 > }
43 > /**
44 > * Park a deferred under `key` and return its promise. Use when there
45 > * is no synchronous responder to guard against — the request that
46 > * eventually feeds {@link respond} originates from a different code
47 > * path (e.g. an MCP handler invoked by the SDK whose completion
48 > * arrives via a workbench round-trip).
49 > *
50 > * If `key` is already registered (duplicate `tool_use_id` from the
51 > * SDK, retry, or logic bug), the previous deferred is rejected with
52 > * a {@link CancellationError} so its awaiter unwinds instead of
53 > * leaking forever.
54 > */
55 > register(key: string): Promise<T> {
56 > if (this._earlyResults.has(key)) { pendingRequestRegistry.ts ×3
57 > const buffered = this._earlyResults.get(key) as T; pendingRequestRegistry.ts ×1
58 > this._earlyResults.delete(key);
59 > return Promise.resolve(buffered);
60 > }
61 > const existing = this._entries.get(key); pendingRequestRegistry.ts ×2
62 > if (existing && !existing.isSettled) { pendingRequestRegistry.ts ×3
63 existing.error(new CancellationError());
64 }
65 > const deferred = new DeferredPromise<T>(); pendingRequestRegistry.ts ×2
66 > this._entries.set(key, deferred);
67 > return deferred.p;
70 > respond(key: string, value: T): boolean {
71 > const deferred = this._entries.get(key); pendingRequestRegistry.ts ×2
72 > if (!deferred) {
73 > return false; pendingRequestRegistry.ts ×1
74 > }
75 > this._entries.delete(key); pendingRequestRegistry.ts ×1
76 > deferred.complete(value);
77 > return true;
80 > /**
81 > * Like {@link respond}, but if no deferred is parked under `key`, buffer
82 > * the value so a subsequent {@link register} / {@link registerAndFire}
83 > * for the same key resolves immediately. Use when the completion may
84 > * legitimately arrive before the awaiting handler registers (the
85 > * Copilot client-tool round-trip, whose SDK handler and the workbench
86 > * completion race).
87 > */
88 > respondOrBuffer(key: string, value: T): void {
89 > if (!this.respond(key, value)) { pendingRequestRegistry.ts ×2
90 > this._earlyResults.set(key, value); pendingRequestRegistry.ts ×1
91 > }
94 > /** Whether a result arrived before a request registered under `key`. */
95 > hasBufferedResult(key: string): boolean {
96 > return this._earlyResults.has(key); pendingRequestRegistry.ts ×1
97 > }
99 > /**
100 > * Resolve every parked deferred with `denyValue` and clear the registry.
101 > *
102 > * Designed for the permission-deny path: a "deny" answer is itself a
103 > * successful round-trip result, so awaiting consumers receive `denyValue`
104 > * rather than an error. Use {@link rejectAll} when callers must observe
105 > * a thrown error instead (cancellation, dispose).
106 > */
107 > denyAll(denyValue: T): void {
108 > for (const [, deferred] of this._entries) { pendingRequestRegistry.ts ×2
109 > if (!deferred.isSettled) { pendingRequestRegistry.ts ×1
110 > deferred.complete(denyValue);
111 > }
112 > }
113 > this._entries.clear(); pendingRequestRegistry.ts ×2
114 > this._earlyResults.clear();
115 > }
117 > /**
118 > * Reject every parked deferred with `error` and clear the registry.
119 > *
120 > * Use this when in-flight requests must be cancelled rather than
121 > * answered (e.g. session dispose, `Query` rebind on tool-set change).
122 > * Compare with {@link denyAll}, which *resolves* every deferred with a
123 > * supplied value — that is right for the permission-deny path where a
124 > * "deny" is itself a successful answer, but wrong for cancellation
125 > * where the awaited consumer must observe an error to unwind.
126 > */
127 > rejectAll(error: Error): void {
128 > for (const [, deferred] of this._entries) { pendingRequestRegistry.ts ×2
129 > if (!deferred.isSettled) { pendingRequestRegistry.ts ×1
130 > deferred.error(error);
131 > }
132 > }
133 > this._entries.clear(); pendingRequestRegistry.ts ×2
134 > this._earlyResults.clear();
135 > }