Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions src/vs/platform/agentHost/node/agentHostChangesetCoordinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export const CHANGESET_DB_METADATA_KEYS: Record<string, true> = {
* {@link IAgentHostChangesetService} (which owns compute / publish /
* persist primitives).
*
* Owns the deferred uncommitted-refresh state machine — refreshes that
* fire before the session's working directory is known are queued and
* drained from {@link onSessionMaterialized} / {@link onSessionRestored}.
* Owns the deferred static-refresh state machine — refreshes that fire
* before the session's working directory is known are queued and drained
* from {@link onSessionMaterialized} / {@link onSessionRestored}.
*
* No per-session controllers — the cross-cutting concerns (listSessions
* overlay, subscribe URI routing) inherently span sessions, so a single
Expand All @@ -73,6 +73,12 @@ export class ChangesetSessionCoordinator extends Disposable {
* {@link onSessionRestored} once the working directory is set.
*/
private readonly _pendingUncommittedRefreshes = new Set<string>();
/**
* Sessions that subscribed to their session-wide branch changeset before
* the working directory was known. Drained alongside uncommitted refreshes
* once restore / materialization has populated the session summary.
*/
private readonly _pendingSessionRefreshes = new Set<string>();

/**
* Per-session set of turn ids that have at least one live subscriber to
Expand Down Expand Up @@ -148,7 +154,7 @@ export class ChangesetSessionCoordinator extends Disposable {

/**
* Called when a provisional session is materialized (working directory
* becomes known). Drains any uncommitted refresh that was deferred
* becomes known). Drains any static changeset refresh that was deferred
* because the working directory was not yet known.
*/
onSessionMaterialized(sessionStr: string): void {
Expand All @@ -162,6 +168,7 @@ export class ChangesetSessionCoordinator extends Disposable {
*/
onSessionDisposed(sessionStr: string): void {
this._pendingUncommittedRefreshes.delete(sessionStr);
this._pendingSessionRefreshes.delete(sessionStr);
this._subscribedTurns.delete(sessionStr);
this._changesetFileMonitor.onSessionDisposed(sessionStr);
}
Expand All @@ -173,9 +180,9 @@ export class ChangesetSessionCoordinator extends Disposable {
// ---- Subscription hooks -------------------------------------------------

/**
* Called on every `addSubscriber` 0→1 transition. When `resource` is
* the uncommitted changeset URI, triggers the first git-diff refresh
* (or queues it for later if the working directory is not yet known).
* Called on every `addSubscriber` 0→1 transition. When `resource` is a
* static changeset URI, triggers the first git-diff refresh (or queues
* it for later if the working directory is not yet known).
*
* Both {@link AgentService.subscribe} and the handshake fast-path
* (`ProtocolServerHandler.initialSubscriptions`) call into
Expand All @@ -190,10 +197,7 @@ export class ChangesetSessionCoordinator extends Disposable {
return;
}
if (parsed?.kind === ChangesetKind.Session) {
// Session-changeset compute uses git when a working dir is
// available and falls back to the SDK edit-tracker otherwise,
// so it doesn't need the same deferral as uncommitted.
this._changesets.refreshSessionChangeset(parsed.sessionUri);
this._triggerSessionRefresh(parsed.sessionUri);
this._changesetFileMonitor.trackSessionChanges(resourceStr, parsed.sessionUri);
return;
}
Expand All @@ -219,15 +223,15 @@ export class ChangesetSessionCoordinator extends Disposable {
// to the changeset URIs directly, and the user has been
// editing files manually in the working tree.
this._triggerUncommittedRefresh(resourceStr);
this._changesets.refreshSessionChangeset(resourceStr);
this._triggerSessionRefresh(resourceStr);
this._changesetFileMonitor.trackSessionChanges(resourceStr, resourceStr);
}
}

/**
* Called when a resource's last subscriber drops. Cleans up any
* deferred uncommitted refresh queued for that session — if no one is
* subscribed anymore, there's no point firing it on materialize.
* deferred refresh queued for that session — if no one is subscribed anymore,
* there's no point firing it on materialize.
*/
onLastSubscriber(resource: URI): void {
const resourceStr = resource.toString();
Expand All @@ -238,6 +242,7 @@ export class ChangesetSessionCoordinator extends Disposable {
return;
}
if (parsed?.kind === ChangesetKind.Session) {
this._pendingSessionRefreshes.delete(parsed.sessionUri);
this._changesetFileMonitor.untrackSessionChanges(resourceStr);
return;
}
Expand Down Expand Up @@ -432,9 +437,21 @@ export class ChangesetSessionCoordinator extends Disposable {
this._changesets.refreshUncommittedChangeset(sessionStr);
}

private _triggerSessionRefresh(sessionStr: string): void {
const wd = this._configurationService.getEffectiveWorkingDirectory(sessionStr);
if (!wd) {
this._pendingSessionRefreshes.add(sessionStr);
return;
}
this._changesets.refreshSessionChangeset(sessionStr);
}

private _drainPendingRefresh(sessionStr: string): void {
if (this._pendingUncommittedRefreshes.delete(sessionStr)) {
this._triggerUncommittedRefresh(sessionStr);
}
if (this._pendingSessionRefreshes.delete(sessionStr)) {
this._triggerSessionRefresh(sessionStr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { URI } from '../../../../base/common/uri.js';
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
import { NullLogService } from '../../../log/common/log.js';
import { AgentSession, IAgentSessionMetadata } from '../../common/agentService.js';
import { buildDefaultChangesetCatalogue, buildUncommittedChangesetUri } from '../../common/changesetUri.js';
import { buildDefaultChangesetCatalogue, buildSessionChangesetUri, buildUncommittedChangesetUri } from '../../common/changesetUri.js';
import { ActionType } from '../../common/state/sessionActions.js';
import { buildSubagentSessionUri, SessionStatus, type ISessionFileDiff } from '../../common/state/sessionState.js';
import { AgentConfigurationService } from '../../node/agentConfigurationService.js';
Expand Down Expand Up @@ -127,6 +127,38 @@ suite('ChangesetSessionCoordinator', () => {
});
});

test('defers session changeset refresh until the working directory is known', async () => {
const session = AgentSession.uri('mock', 'session-1').toString();
const environment = createEnvironment();
createSession(environment.stateManager, session, undefined, false);

environment.coordinator.onFirstSubscriber(URI.parse(buildSessionChangesetUri(session)));
await tick();

const summary = environment.stateManager.getSessionState(session)!.summary;
environment.stateManager.markSessionPersisted(session, { ...summary, workingDirectory: 'file:///repo/worktree' });
environment.coordinator.onSessionMaterialized(session);
await tick();

assert.deepStrictEqual(environment.changesets.sessionRefreshes, [session]);
});

test('drops pending session changeset refresh when the last subscriber leaves', async () => {
const session = AgentSession.uri('mock', 'session-1').toString();
const environment = createEnvironment();
const changeset = buildSessionChangesetUri(session);
createSession(environment.stateManager, session, undefined, false);

environment.coordinator.onFirstSubscriber(URI.parse(changeset));
environment.coordinator.onLastSubscriber(URI.parse(changeset));
const summary = environment.stateManager.getSessionState(session)!.summary;
environment.stateManager.markSessionPersisted(session, { ...summary, workingDirectory: 'file:///repo/worktree' });
environment.coordinator.onSessionMaterialized(session);
await tick();

assert.deepStrictEqual(environment.changesets.sessionRefreshes, []);
});

test('does not attach root state when watcher acquisition fails', async () => {
const session = AgentSession.uri('mock', 'session-1').toString();
const environment = createEnvironment();
Expand Down
Loading