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

202 LOC · 163 covered · 39 uncovered · 45 ranges · 939 concepts · 19 introducers · 492 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 > /*--------------------------------------------------------------------------------------------- agentHostGitStateService.ts ×7
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 { equals as objectEquals } from '../../../base/common/objects.js';
7 > import { URI } from '../../../base/common/uri.js';
8 > import { Emitter } from '../../../base/common/event.js';
9 > import { ILogService } from '../../log/common/log.js';
10 > import { IAgentHostGitStateService, META_GIT_STATE, META_GITHUB_STATE } from '../common/agentHostGitStateService.js';
11 > import { ISessionGitHubState, readSessionGitHubState, readSessionGitState, SessionLifecycle, withSessionGitHubState, withSessionGitState, type ISessionGitState } from '../common/state/sessionState.js';
12 > import { IAgentHostGitService } from '../common/agentHostGitService.js';
13 > import { AgentHostStateManager, IAgentHostStateManager } from './agentHostStateManager.js';
14 > import { ISessionDataService } from '../common/sessionDataService.js';
15 > import { IAgentHostOctoKitService } from './shared/agentHostOctoKitService.js';
16 > import { IAgentService } from '../common/agentService.js';
17 > import { IAgentHostGitHubEndpointService } from './agentHostGitHubEndpointService.js';
18 > import { Disposable, toDisposable } from '../../../base/common/lifecycle.js';
19 > import { CancellationTokenSource } from '../../../base/common/cancellation.js';
20 > import { ThrottlerByKey, timeout } from '../../../base/common/async.js';
21 > import { isCancellationError } from '../../../base/common/errors.js';
22 >
23 > export class AgentHostGitStateService extends Disposable implements IAgentHostGitStateService {
24 > declare readonly _serviceBrand: undefined;
25 >
26 > private readonly _onDidRefreshSessionGitState = this._register(new Emitter<string>());
27 > readonly onDidRefreshSessionGitState = this._onDidRefreshSessionGitState.event;
28 >
29 > private readonly _gitStateRefreshThrottler = this._register(new ThrottlerByKey<string>());
30 > private readonly _gitStateRefreshCancellationTokenSource = new CancellationTokenSource();
31 >
32 > constructor(
33 > @IAgentHostStateManager private readonly _stateManager: AgentHostStateManager, async.ts ×3
34 > @IAgentHostGitService private readonly _gitService: IAgentHostGitService,
35 > @IAgentHostOctoKitService private readonly _octoKitService: IAgentHostOctoKitService,
36 > @IAgentService private readonly _agentService: IAgentService,
37 > @IAgentHostGitHubEndpointService private readonly _gitHubEndpointService: IAgentHostGitHubEndpointService,
38 > @ILogService private readonly _logService: ILogService,
39 > @ISessionDataService private readonly _sessionDataService: ISessionDataService,
40 > ) {
41 > super();
42 >
43 > this._register(toDisposable(() => this._gitStateRefreshCancellationTokenSource.dispose(true)));
44 > }
46 > async attachSessionGitHubPullRequest(sessionKey: string): Promise<void> {
47 > const state = this._stateManager.getSessionState(sessionKey); agentService.ts ×13
48 > if (!state) {
49 return;
50 }
52 > // New session
53 > if (state.lifecycle !== SessionLifecycle.Ready) {
54 return;
55 }
57 > // GitHub state
58 > const gitHubState = readSessionGitHubState(this._stateManager.getSessionState(sessionKey)?._meta);
59 > if (!gitHubState?.owner || !gitHubState?.repo || gitHubState?.pullRequestUrl) {
60 > return;
61 > }
62
63 // Git state
64 const gitState = readSessionGitState(state._meta);
65 > if (!gitState?.branchName || (gitState.branchName === gitState.baseBranchName)) { agentService.ts ×13
66 return;
67 }
68
69 try {
70 const repoResource = this._gitHubEndpointService.getRepoResource();
71 const authToken = this._agentService.getAuthToken({
72 resource: repoResource.resource,
73 scopes: repoResource.scopes_supported,
74 });
75 if (!authToken) {
76 return;
77 }
78
79 const signal = new AbortController().signal;
80 const pr = await this._octoKitService.findPullRequestByHeadBranch(
81 gitHubState.owner, gitHubState.repo, gitState.branchName, authToken, signal);
82 > if (!pr?.url) { agentService.ts ×13
83 return;
84 }
85
86 this.setSessionGitHubState(sessionKey, {
87 owner: gitHubState.owner,
88 repo: gitHubState.repo,
89 pullRequestUrl: pr.url
90 } satisfies ISessionGitHubState);
91 } catch (error) {
92 this._logService.warn(`[AgentHostGitStateService][attachSessionGitHubPullRequest] Failed to find pull request for ${sessionKey}`, error);
93 }
96 > async refreshSessionGitState(sessionKey: string, workingDirectory: URI | undefined): Promise<void> {
97 > const sessionState = this._stateManager.getSessionState(sessionKey); agentHostGitStateService.ts ×4
98 > if (sessionState?.lifecycle === SessionLifecycle.CreationFailed) {
99 return;
100 }
102 > if (!workingDirectory) {
103 > const workingDirectoryStr = sessionState?.workingDirectories?.[0]; agentHostGitStateService.ts ×2
104 > if (workingDirectoryStr) {
105 > workingDirectory = URI.parse(workingDirectoryStr); agentHostGitStateService.ts ×1
106 > }
109 > if (!workingDirectory) {
111 > }
113 > await this._gitStateRefreshThrottler.queue(sessionKey, async () => {
114 > try {
115 > this._logService.trace(`[AgentHostGitStateService][refreshSessionGitState] Refreshing git state for ${sessionKey}, ${workingDirectory?.fsPath}`);
116 >
117 > const gitState = await this._gitService.getSessionGitState(workingDirectory);
118 > if (gitState) { agentHostGitStateService.ts ×2
119 > const currentMeta = this._stateManager.getSessionState(sessionKey)?._meta; agentHostGitStateService.ts ×2
120 > if (!objectEquals(readSessionGitState(currentMeta), gitState)) {
121 > // Update the session's git state agentHostGitStateService.ts ×5
122 > await this._setSessionGitState(sessionKey, gitState);
123 >
124 > // Update the session's GitHub state
125 > if (gitState.githubOwner && gitState.githubRepo) {
126 > await this.setSessionGitHubState(sessionKey, { agentHostGitStateService.ts ×3
127 > owner: gitState.githubOwner,
128 > repo: gitState.githubRepo
129 > } satisfies ISessionGitHubState);
130 > }
134 > this._onDidRefreshSessionGitState.fire(sessionKey);
135 >
136 > // We want to ensure that we refresh the git state at
137 > // most every 5 seconds in order to avoid excessive git
138 > // operations and excessive traffic between the server
139 > // and the client(s).
140 > await timeout(5_000, this._gitStateRefreshCancellationTokenSource.token);
141 > } catch (error) { agentHostGitStateService.ts ×3
142 > if (isCancellationError(error)) { agentHostGitStateService.ts ×1
143 > return; async.ts ×1
144 > }
146 > this._logService.warn(`[AgentHostGitStateService][refreshSessionGitState] Failed to compute git state for ${sessionKey}:`, error);
147 > }
151 > async setSessionGitHubState(sessionKey: string, state: ISessionGitHubState): Promise<void> {
152 > const currentMeta = this._stateManager.getSessionState(sessionKey)?._meta; agentHostGitStateService.ts ×3
153 >
154 > const currentState = readSessionGitHubState(currentMeta);
155 > const nextState = { ...(currentState ?? {}), ...state } satisfies ISessionGitHubState;
156 >
157 > if (objectEquals(currentState, nextState)) {
158 return;
159 }
161 > // Update session state manager
162 > const nextMeta = withSessionGitHubState(currentMeta, nextState);
163 > this._stateManager.setSessionMeta(sessionKey, nextMeta);
164 >
165 > // Update session database
166 > await this._saveSessionState(sessionKey, META_GITHUB_STATE, JSON.stringify(nextState));
167 > }
169 > private async _setSessionGitState(sessionKey: string, gitState: ISessionGitState): Promise<void> {
170 > // Update session state manager agentHostGitStateService.ts ×5
171 > const currentMeta = this._stateManager.getSessionState(sessionKey)?._meta;
172 > const nextMeta = withSessionGitState(currentMeta, gitState);
173 > this._stateManager.setSessionMeta(sessionKey, nextMeta);
174 >
175 > // Update session database
176 > await this._saveSessionState(sessionKey, META_GIT_STATE, JSON.stringify(gitState));
177 > }
179 > private async _saveSessionState(sessionKey: string, key: string, value: string): Promise<void> {
180 > // Skip saving session state if the session is not materialized agentHostGitStateService.ts ×5
181 > const state = this._stateManager.getSessionState(sessionKey);
182 > if (state?.lifecycle === SessionLifecycle.Creating) {
184 > }
186 > let databaseRef;
187 > try {
188 > databaseRef = this._sessionDataService.openDatabase(URI.parse(sessionKey));
189 > } catch (error) {
190 > this._logService.warn(`[AgentHostGitStateService][_saveSessionState] Failed to open session database for ${sessionKey}`, error); changesetUri.ts ×3
191 > return;
192 > }
194 > try {
195 > await databaseRef.object.setMetadata(key, value);
196 > } catch (error) {
197 this._logService.warn(`[AgentHostGitStateService][_saveSessionState] Failed to persist ${key}`, error);
199 > databaseRef.dispose();
200 > }