src/vs/server/node/serverAgentHostManager.ts
158 LOC · 149 covered · 9 uncovered · 17 ranges · 10 concepts · 7 introducers · 7 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.
/*---------------------------------------------------------------------------------------------
serverAgentHostManager.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 { Event } from '../../base/common/event.js';
import { Disposable, MutableDisposable, toDisposable } from '../../base/common/lifecycle.js';
import { ProxyChannel } from '../../base/parts/ipc/common/ipc.js';
import { IAgentHostConnection, IAgentHostStarter } from '../../platform/agentHost/common/agent.js';
import { reportAgentHostProcessError } from '../../platform/agentHost/common/agentHostProcessTelemetry.js';
import { AgentHostIpcChannels, IAgentService } from '../../platform/agentHost/common/agentService.js';
import { createDecorator } from '../../platform/instantiation/common/instantiation.js';
import { ILogService, ILoggerService } from '../../platform/log/common/log.js';
import { RemoteLoggerChannelClient } from '../../platform/log/common/logIpc.js';
import { ITelemetryService } from '../../platform/telemetry/common/telemetry.js';
import { IServerLifetimeService } from './serverLifetimeService.js';
export const IServerAgentHostManager = createDecorator<IServerAgentHostManager>('serverAgentHostManager');
/**
* Server-specific agent host manager. Eagerly starts the agent host process,
* handles crash recovery, and tracks active agent sessions plus incoming
* WebSocket clients to the spawned standalone agent host via
* {@link IServerLifetimeService}.
*
* Renderer-to-agent-host proxy connections handled by `AgentHostChannel`
* deliberately do not participate here.
*/
export interface IServerAgentHostManager {
readonly _serviceBrand: undefined;
}
/**
* Proxy interface for the connection tracker IPC channel exposed by the agent
* host process. This is NOT part of the agent host protocol -- it is a
* server-only process-management concern.
*/
interface IConnectionTrackerService {
readonly onDidChangeConnectionCount: Event<number>;
}
enum Constants {
MaxRestarts = 5,
}
export class ServerAgentHostManager extends Disposable implements IServerAgentHostManager {
declare readonly _serviceBrand: undefined;
private _restartCount = 0;
/** Lifetime token held while sessions are active or standalone WebSocket clients are connected. */
private readonly _lifetimeToken = this._register(new MutableDisposable());
private _hasActiveSessions = false;
private _connectionCount = 0;
constructor(
private readonly _starter: IAgentHostStarter,
@ILogService private readonly _logService: ILogService,
@ILoggerService private readonly _loggerService: ILoggerService,
@IServerLifetimeService private readonly _serverLifetimeService: IServerLifetimeService,
@ITelemetryService private readonly _telemetryService: ITelemetryService,
) {
super();
this._register(this._starter);
this._start();
}
private async _start(): Promise<void> {
try {
const connection = await this._starter.start();
if (this._store.isDisposed) {
connection.store.dispose();
return;
}
this._logService.info('ServerAgentHostManager: agent host started');
// Connect logger channel so agent host logs appear in the output channel
connection.store.add(new RemoteLoggerChannelClient(this._loggerService, connection.client.getChannel(AgentHostIpcChannels.Logger)));
this._trackActiveSessions(connection);
this._trackClientConnections(connection);
// Handle unexpected exit
connection.store.add(connection.onDidProcessExit(e => {
this._hasActiveSessions = false;
this._connectionCount = 0;
this._lifetimeToken.clear();
const willRestart = this._restartCount <= Constants.MaxRestarts;
reportAgentHostProcessError(this._telemetryService, {
kind: 'unexpectedExit',
code: e.code,
restartCount: this._restartCount,
willRestart,
});
if (willRestart) {
this._logService.error(`ServerAgentHostManager: agent host terminated unexpectedly with code ${e.code}`);
this._restartCount++;
connection.store.dispose();
this._start();
} else {
this._logService.error(`ServerAgentHostManager: agent host terminated with code ${e.code}, giving up after ${Constants.MaxRestarts} restarts`);
}
this._register(toDisposable(() => connection.store.dispose()));
} catch (error) {
return;
}
const willRestart = this._restartCount <= Constants.MaxRestarts;
reportAgentHostProcessError(this._telemetryService, {
kind: 'startFailed',
restartCount: this._restartCount,
willRestart,
}, error);
if (willRestart) {
this._logService.error('ServerAgentHostManager: agent host failed to start', error);
this._restartCount++;
this._start();
} else {
this._logService.error(`ServerAgentHostManager: agent host failed to start, giving up after ${Constants.MaxRestarts} restarts`, error);
}
private _trackActiveSessions(connection: IAgentHostConnection): void {
const agentService = ProxyChannel.toService<IAgentService>(connection.client.getChannel(AgentHostIpcChannels.AgentHost));
connection.store.add(agentService.onDidAction(envelope => {
this._hasActiveSessions = envelope.action.activeSessions > 0;
this._updateLifetimeToken();
}
}
private _trackClientConnections(connection: IAgentHostConnection): void {
const connectionTracker = ProxyChannel.toService<IConnectionTrackerService>(connection.client.getChannel(AgentHostIpcChannels.ConnectionTracker));
connection.store.add(connectionTracker.onDidChangeConnectionCount(count => {
this._updateLifetimeToken();
}
private _updateLifetimeToken(): void {
this._lifetimeToken.value ??= this._serverLifetimeService.active('AgentHost');
} else {
}