src/vs/platform/agentHost/node/agentHostLocalTurns.ts
161 LOC · 147 covered · 14 uncovered · 37 ranges · 1064 concepts · 15 introducers · 508 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.
/*---------------------------------------------------------------------------------------------
agentHostLocalTurns.ts ×12
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { IReference } from '../../../base/common/lifecycle.js';
import { URI } from '../../../base/common/uri.js';
import { ILogService } from '../../log/common/log.js';
import type { ILocalTurnRecord, ISessionDatabase, ISessionDataService } from '../common/sessionDataService.js';
import type { Turn } from '../common/state/sessionState.js';
/**
* Tracks host-injected ("local") turns — completed protocol turns the agent SDK
* never saw, such as the `/rename` acknowledgement or a `!command` terminal run.
*
* These turns exist only in the agent host: they are never forwarded to the
* agent SDK, so they are absent from the SDK transcript that
* {@link AgentService} replays on restore. This registry persists them (so they
* survive reload) and remembers, for each, the id of the preceding concrete
* (SDK-backed) turn — the *anchor* — so that fork/truncate operations targeting
* a local turn can be redirected to the concrete SDK message before it.
*
* Everything is scoped to a **chat** (its channel URI): a session's default
* chat and each of its peer chats are handled identically. Persistence lives in
* the owning session's database (one per session, shared across its chats),
* discriminated by {@link ILocalTurnRecord.chatUri}.
*/
export class AgentHostLocalTurns {
/** chat URI → (localTurnId → { anchorTurnId, seq }). */
private readonly _byChat = new Map<string, Map<string, { readonly anchorTurnId: string | undefined; readonly seq: number }>>();
/** session URI → highest `seq` assigned so far (seq is session-global for stable ordering). */
private readonly _seqBySession = new Map<string, number>();
constructor(
private readonly _logService: ILogService,
) { }
/** Whether `turnId` is a known host-injected local turn in `chat`. */
isLocal(chat: string, turnId: string): boolean {
}
/** All known local turn ids for `chat`. */
getLocalTurnIds(chat: string): string[] {
return map ? [...map.keys()] : [];
}
/**
* Resolves `turnId` to the concrete (SDK-backed) turn a fork/truncate should
* operate on within `chat`. For a local turn this is its anchor (the
* preceding real turn, or `undefined` when it precedes any real turn); for a
* concrete turn it is the turn itself.
*/
resolveConcreteTurnId(chat: string, turnId: string): string | undefined {
return entry ? entry.anchorTurnId : turnId;
}
/**
* Persist a local turn and remember it in memory. `anchorTurnId` is the id
* of the preceding concrete turn in `chat` (or `undefined` when there is
* none). `session` identifies the database to persist into.
*/
record(session: string, chat: string, turn: Turn, anchorTurnId: string | undefined): void {
this._noteInMemory(session, chat, turn.id, anchorTurnId, seq);
const record: ILocalTurnRecord = { turnId: turn.id, chatUri: chat, anchorTurnId, seq, payload: JSON.stringify(turn) };
let ref: IReference<ISessionDatabase>;
try {
ref = this._sessionDataService.openDatabase(URI.parse(session));
} catch (err) {
this._logService.warn(`[AgentHostLocalTurns] Failed to open database to persist local turn ${turn.id}`, err);
agentHostLocalTurns.ts ×1
return;
}
this._logService.warn(`[AgentHostLocalTurns] Failed to persist local turn ${turn.id}`, err);
/**
* Loads persisted local turns for `session`, populating the in-memory index
* (keyed by each record's chat), and returns the records for `chat` in
* `seq` order so the caller can interleave them into that chat's SDK-derived
* turns during restore.
*/
async loadForChat(session: string, chat: string): Promise<ILocalTurnRecord[]> {
return records.filter(r => r.chatUri === chat);
}
/** Note a local turn in memory only (used by fork seeding). */
noteInMemory(session: string, chat: string, turnId: string, anchorTurnId: string | undefined, seq: number): void {
this._noteInMemory(session, chat, turnId, anchorTurnId, seq);
}
/** Delete the given local turns from memory and the session database. */
deleteLocals(session: string, turnIds: readonly string[]): void {
}
for (const map of this._byChat.values()) {
for (const id of idSet) {
map.delete(id);
}
}
let ref: IReference<ISessionDatabase>;
try {
ref = this._sessionDataService.openDatabase(URI.parse(session));
} catch (err) {
this._logService.warn(`[AgentHostLocalTurns] Failed to open database to delete local turns for ${session}`, err);
return;
}
this._logService.warn(`[AgentHostLocalTurns] Failed to delete local turns for ${session}`, err);
/** Drop all in-memory state for a chat. */
forgetChat(chat: string): void {
this._byChat.delete(chat);
}
private async _load(session: string): Promise<ILocalTurnRecord[]> {
const ref = this._sessionDataService.tryOpenDatabase?.(URI.parse(session));
agentHostLocalTurns.ts ×5
if (!ref) {
return [];
}
const db = await ref;
if (!db) {
}
const records = await db.object.getLocalTurns();
for (const r of records) {
this._noteInMemory(session, r.chatUri, r.turnId, r.anchorTurnId, r.seq);
agentHostLocalTurns.ts ×1
}
} finally {
db.dispose();
}
this._logService.warn(`[AgentHostLocalTurns] Failed to load local turns for ${session}`, err);
return [];
}
private _noteInMemory(session: string, chat: string, turnId: string, anchorTurnId: string | undefined, seq: number): void {
if (!map) {
map = new Map();
this._byChat.set(chat, map);
}
map.set(turnId, { anchorTurnId, seq });
this._seqBySession.set(session, Math.max(this._seqBySession.get(session) ?? 0, seq));
}