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.
/*---------------------------------------------------------------------------------------------
agentHostGitStateService.ts ×7
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { equals as objectEquals } from '../../../base/common/objects.js';
import { URI } from '../../../base/common/uri.js';
import { Emitter } from '../../../base/common/event.js';
import { ILogService } from '../../log/common/log.js';
import { IAgentHostGitStateService, META_GIT_STATE, META_GITHUB_STATE } from '../common/agentHostGitStateService.js';
import { ISessionGitHubState, readSessionGitHubState, readSessionGitState, SessionLifecycle, withSessionGitHubState, withSessionGitState, type ISessionGitState } from '../common/state/sessionState.js';
import { IAgentHostGitService } from '../common/agentHostGitService.js';
import { AgentHostStateManager, IAgentHostStateManager } from './agentHostStateManager.js';
import { ISessionDataService } from '../common/sessionDataService.js';
import { IAgentHostOctoKitService } from './shared/agentHostOctoKitService.js';
import { IAgentService } from '../common/agentService.js';
import { IAgentHostGitHubEndpointService } from './agentHostGitHubEndpointService.js';
import { Disposable, toDisposable } from '../../../base/common/lifecycle.js';
import { CancellationTokenSource } from '../../../base/common/cancellation.js';
import { ThrottlerByKey, timeout } from '../../../base/common/async.js';
import { isCancellationError } from '../../../base/common/errors.js';
export class AgentHostGitStateService extends Disposable implements IAgentHostGitStateService {
declare readonly _serviceBrand: undefined;
private readonly _onDidRefreshSessionGitState = this._register(new Emitter<string>());
readonly onDidRefreshSessionGitState = this._onDidRefreshSessionGitState.event;
private readonly _gitStateRefreshThrottler = this._register(new ThrottlerByKey<string>());
private readonly _gitStateRefreshCancellationTokenSource = new CancellationTokenSource();
constructor(
@IAgentHostGitService private readonly _gitService: IAgentHostGitService,
@IAgentHostOctoKitService private readonly _octoKitService: IAgentHostOctoKitService,
@IAgentService private readonly _agentService: IAgentService,
@IAgentHostGitHubEndpointService private readonly _gitHubEndpointService: IAgentHostGitHubEndpointService,
@ILogService private readonly _logService: ILogService,
@ISessionDataService private readonly _sessionDataService: ISessionDataService,
) {
super();
this._register(toDisposable(() => this._gitStateRefreshCancellationTokenSource.dispose(true)));
}
async attachSessionGitHubPullRequest(sessionKey: string): Promise<void> {
if (!state) {
return;
}
// New session
if (state.lifecycle !== SessionLifecycle.Ready) {
return;
}
// GitHub state
const gitHubState = readSessionGitHubState(this._stateManager.getSessionState(sessionKey)?._meta);
if (!gitHubState?.owner || !gitHubState?.repo || gitHubState?.pullRequestUrl) {
return;
}
// Git state
const gitState = readSessionGitState(state._meta);
if (!gitState?.branchName || (gitState.branchName === gitState.baseBranchName)) {
agentService.ts ×13
return;
}
try {
const repoResource = this._gitHubEndpointService.getRepoResource();
const authToken = this._agentService.getAuthToken({
resource: repoResource.resource,
scopes: repoResource.scopes_supported,
});
if (!authToken) {
return;
}
const signal = new AbortController().signal;
const pr = await this._octoKitService.findPullRequestByHeadBranch(
gitHubState.owner, gitHubState.repo, gitState.branchName, authToken, signal);
return;
}
this.setSessionGitHubState(sessionKey, {
owner: gitHubState.owner,
repo: gitHubState.repo,
pullRequestUrl: pr.url
} satisfies ISessionGitHubState);
} catch (error) {
this._logService.warn(`[AgentHostGitStateService][attachSessionGitHubPullRequest] Failed to find pull request for ${sessionKey}`, error);
}
async refreshSessionGitState(sessionKey: string, workingDirectory: URI | undefined): Promise<void> {
const sessionState = this._stateManager.getSessionState(sessionKey);
agentHostGitStateService.ts ×4
if (sessionState?.lifecycle === SessionLifecycle.CreationFailed) {
return;
}
if (!workingDirectory) {
const workingDirectoryStr = sessionState?.workingDirectories?.[0];
agentHostGitStateService.ts ×2
if (workingDirectoryStr) {
}
if (!workingDirectory) {
}
await this._gitStateRefreshThrottler.queue(sessionKey, async () => {
try {
this._logService.trace(`[AgentHostGitStateService][refreshSessionGitState] Refreshing git state for ${sessionKey}, ${workingDirectory?.fsPath}`);
const gitState = await this._gitService.getSessionGitState(workingDirectory);
const currentMeta = this._stateManager.getSessionState(sessionKey)?._meta;
agentHostGitStateService.ts ×2
if (!objectEquals(readSessionGitState(currentMeta), gitState)) {
await this._setSessionGitState(sessionKey, gitState);
// Update the session's GitHub state
if (gitState.githubOwner && gitState.githubRepo) {
owner: gitState.githubOwner,
repo: gitState.githubRepo
} satisfies ISessionGitHubState);
}
this._onDidRefreshSessionGitState.fire(sessionKey);
// We want to ensure that we refresh the git state at
// most every 5 seconds in order to avoid excessive git
// operations and excessive traffic between the server
// and the client(s).
await timeout(5_000, this._gitStateRefreshCancellationTokenSource.token);
}
this._logService.warn(`[AgentHostGitStateService][refreshSessionGitState] Failed to compute git state for ${sessionKey}:`, error);
}
async setSessionGitHubState(sessionKey: string, state: ISessionGitHubState): Promise<void> {
const currentMeta = this._stateManager.getSessionState(sessionKey)?._meta;
agentHostGitStateService.ts ×3
const currentState = readSessionGitHubState(currentMeta);
const nextState = { ...(currentState ?? {}), ...state } satisfies ISessionGitHubState;
if (objectEquals(currentState, nextState)) {
return;
}
// Update session state manager
const nextMeta = withSessionGitHubState(currentMeta, nextState);
this._stateManager.setSessionMeta(sessionKey, nextMeta);
// Update session database
await this._saveSessionState(sessionKey, META_GITHUB_STATE, JSON.stringify(nextState));
}
private async _setSessionGitState(sessionKey: string, gitState: ISessionGitState): Promise<void> {
const currentMeta = this._stateManager.getSessionState(sessionKey)?._meta;
const nextMeta = withSessionGitState(currentMeta, gitState);
this._stateManager.setSessionMeta(sessionKey, nextMeta);
// Update session database
await this._saveSessionState(sessionKey, META_GIT_STATE, JSON.stringify(gitState));
}
private async _saveSessionState(sessionKey: string, key: string, value: string): Promise<void> {
// Skip saving session state if the session is not materialized
agentHostGitStateService.ts ×5
const state = this._stateManager.getSessionState(sessionKey);
if (state?.lifecycle === SessionLifecycle.Creating) {
}
let databaseRef;
try {
databaseRef = this._sessionDataService.openDatabase(URI.parse(sessionKey));
} catch (error) {
this._logService.warn(`[AgentHostGitStateService][_saveSessionState] Failed to open session database for ${sessionKey}`, error);
changesetUri.ts ×3
return;
}
try {
await databaseRef.object.setMetadata(key, value);
} catch (error) {
this._logService.warn(`[AgentHostGitStateService][_saveSessionState] Failed to persist ${key}`, error);
databaseRef.dispose();
}