src/vs/platform/agentHost/node/claude/claudeFileEditObserver.ts
142 LOC · 138 covered · 4 uncovered · 23 ranges · 422 concepts · 13 introducers · 225 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.
/*---------------------------------------------------------------------------------------------
claudeFileEditObserver.ts ×4
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { SDKMessage } from '@anthropic-ai/claude-agent-sdk';
import { Disposable, IReference } from '../../../../base/common/lifecycle.js';
import { IInstantiationService } from '../../../instantiation/common/instantiation.js';
import { ILogService } from '../../../log/common/log.js';
import { ISessionDatabase } from '../../common/sessionDataService.js';
import { FileEditTracker } from '../shared/fileEditTracker.js';
import type { ClaudeMapperState } from './claudeMapSessionEvents.js';
import { getClaudeToolPath, isClaudeFileEditTool } from './claudeToolDisplay.js';
/**
* Phase 8 — file-edit observation off the SDK message stream.
*
* Owns the {@link FileEditTracker}, the in-flight `tool_use_id → path`
* map, and the dbRef whose lifetime gates persistence writes. Snapshots
* before-content when an assistant `tool_use` block arrives, snapshots
* after-content when the matching synthetic `tool_result` arrives, and
* stages a {@link ToolResultFileEditContent} on the session's
* {@link ClaudeMapperState} so the synchronous mapper can attach it to
* the `ChatToolCallComplete` action.
*
* Hooks (`Options.hooks.PreToolUse` / `Options.hooks.PostToolUse`) are
* deliberately NOT used: they are user-bypassable via settings, whereas
* the SDK message stream is the canonical, non-bypassable signal that
* a tool will run. Mirrors the production extension's dispatch-time
* observation (extensions/copilot/.../claudeMessageDispatch.ts:200) —
* see the comment there about `bypassPermissions` and internal SDK
* paths that skip `canUseTool`.
*
* Best-effort: the SDK proceeds to run the tool concurrently after
* yielding the `tool_use` block, so {@link observeAssistant}'s before-
* snapshot races against tool execution. Tool execution involves disk
* I/O before the write, which gives enough microtask headroom in
* practice; same guarantee the production extension relies on with
* `stream.externalEdit`.
*/
export class ClaudeFileEditObserver extends Disposable {
private readonly _editTracker: FileEditTracker;
/**
* Maps SDK `tool_use_id` → file path + raw tool input + model
* captured when the SDK yields the assistant `tool_use` block in
* {@link observeAssistant}. Consumed (and removed) by
* {@link observeUser} when the matching `tool_result` arrives.
* The raw input is forwarded to
* {@link FileEditTracker.takeCompletedEdit} so it can extract the
* AI-written text chunks for the edit-survival reporter. The
* model is read off the assistant message body and is naturally
* per-subagent: when a subagent emits the `tool_use`, its model
* (not the parent's) is what we record.
*/
private readonly _editToolPaths = new Map<string, { readonly filePath: string; readonly toolName: string; readonly toolInput: unknown; readonly modelId: string | undefined }>();
constructor(
dbRef: IReference<ISessionDatabase>,
@ILogService private readonly _logService: ILogService,
@IInstantiationService instantiationService: IInstantiationService,
) {
super();
// Own the DB reference for this observer's lifetime so
// {@link FileEditTracker.takeCompletedEdit}'s `storeFileEdit` write
// has a live database. Disposed first — ahead of any owning
// session's WarmQuery abort — so any in-flight write completes
// against an open DB.
this._register(dbRef);
this._editTracker = instantiationService.createInstance(
FileEditTracker,
sessionUri,
dbRef.object,
);
}
/**
* Snapshot before-content for any file-edit `tool_use` blocks
* carried by an SDK assistant message. Caller must invoke this when
* the SDK yields a canonical `'assistant'` message (full
* `tool_use.input` available).
*/
observeAssistant(message: Extract<SDKMessage, { type: 'assistant' }>): void {
if (!Array.isArray(content)) {
}
const modelId = typeof message.message.model === 'string' ? message.message.model : undefined;
claudeFileEditObserver.ts ×3
for (const block of content) {
if (block.type !== 'tool_use' || !isClaudeFileEditTool(block.name)) {
claudeFileEditObserver.ts ×1
}
if (!filePath) {
}
this._editToolPaths.set(block.id, { filePath, toolName: block.name, toolInput: block.input, modelId });
claudeFileEditObserver.ts ×2
void this._editTracker.trackEditStart(filePath).catch(err =>
this._logService.warn(`[ClaudeFileEditObserver] trackEditStart failed for ${filePath}: ${err}`));
}
/**
* Take after-content snapshots and stage
* {@link ToolResultFileEditContent} entries on `mapperState` for any
* `tool_result` blocks carried by an SDK user message. Caller MUST
* await this BEFORE invoking the synchronous mapper, so the cached
* file edit is already on `mapperState` when `mapUserMessage` calls
* `state.takeFileEdit`.
*/
async observeUser(
turnId: string,
mapperState: ClaudeMapperState,
): Promise<void> {
const content = message.message.content;
if (!Array.isArray(content)) {
}
if (block.type !== 'tool_result') {
continue;
}
if (!tracked) {
}
try {
await this._editTracker.completeEdit(tracked.filePath);
const fileEdit = await this._editTracker.takeCompletedEdit(turnId, block.tool_use_id, tracked.filePath, tracked.toolName, tracked.toolInput, tracked.modelId);
if (fileEdit) {
mapperState.cacheFileEdit(block.tool_use_id, fileEdit);
}
this._logService.warn(`[ClaudeFileEditObserver] file edit tracking failed for ${tracked.filePath}: ${err}`);
}