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
6 changes: 5 additions & 1 deletion apps/vscode-extension/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
- Untouched floors keep the building's grey; a floor the agent read or edited keeps a soft tint of that action.
- The floors the current step reads or edits light up in the action's colour, with an outline: exactly the functions it works on, not the whole file.
- A function the agent writes from scratch gets a new floor, built in front of you when the step plays.
- Replaying the timeline animates the city as it changed: moving back shows the city as it was at that point of the chat, and playing forward grows new floors, raises and lowers buildings and fades neighbours step by step. Before, every growth and fade finished on the next frame.
- Resting the pointer on a floor names its function, its lines and how many times it was read and written.
- Line data comes from Claude Code's edit and read results and from the Code-XR IDE's agent locations; a step without it lights the whole building, as before.
- The timeline is time travel: at any step the city shows the project as it was then, and playing forward builds each change.
- Files created, deleted, moved or renamed appear and disappear in their step, whatever tool did it: shell commands (`rm`, `git rm`, `mv`, `git mv`, `cp`, `touch`, `> file`, `tee`, `sed -i` and their PowerShell and cmd forms), Codex patches (including moves) and the IDE's agents.
- A function the agent deletes keeps its floor until that step, then the floor comes down.
- Growth, fades and floors animate when a step plays; before, they finished on the next frame, so a replayed step showed its result already built.
- Deleting or moving a file no longer counts as a blind edit.
- Legends above the city: the building card, and the function card on top when you point at a floor, float above the tallest building with a line down to what they describe, so they no longer hide the buildings around them.
- Click a building to pin its card; it stays while you look at others and follows the playhead. Click it again to remove it.
- The label of the current step floats in the same band.
Expand Down
18 changes: 11 additions & 7 deletions apps/vscode-extension/src/core/agent-flow/acpSessionEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ function withLineTouches(step: StepEvent, locations: AgentTraceLocation[], creat
return step;
}

/** A terminal command read as the step it really was (a read, a search, a change to the tree). */
function applyShellCommand(command: string, kind: AgentStepKind, locations: AgentTraceLocation[], created: string[], deleted: string[]): AgentStepKind {
const classified = classifyShellCommand(command);
if (classified.kind === 'execute') { return kind; }
for (const path of classified.paths) { locations.push({ path }); }
created.push(...(classified.created ?? []));
deleted.push(...(classified.deleted ?? []));
return classified.kind;
}

/** A created diff's lines (every line new), when it has any. */
function createdTouch(path: string, newText: string, into: AgentTraceLineTouch[]): void {
const touch = touchFromCreatedContent(path, newText);
Expand Down Expand Up @@ -255,13 +265,7 @@ export class AcpSessionEventMapper {
let kind = content.kind;
const deleted: string[] = event.toolKind === 'delete' ? locations.map((loc) => loc.path) : [];
const command = kind === 'execute' ? parseRawCommand(event.rawInput) : undefined;
if (command) {
const classified = classifyShellCommand(command);
if (classified.kind !== 'execute') {
kind = classified.kind;
for (const path of classified.paths) { locations.push({ path }); }
}
}
if (command) { kind = applyShellCommand(command, kind, locations, content.created, deleted); }
this.kinds.set(event.id, kind);
if (locations.length > 0) { this.locations.set(event.id, locations); }
return withLineTouches(toolStep(event.id, kind, at, {
Expand Down
29 changes: 19 additions & 10 deletions apps/vscode-extension/src/core/agent-flow/agentFlowBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { type CityLayout, type LayoutRect, layoutCity, PLATE_HEIGHT } from './agentFlowLayout';
import { relativeToRoot } from './agentFlowPaths';
import { foldTraceEvents, type MutableStep } from './agentFlowSteps';
import { buildLifecycle, type LifecycleResult } from './agentFlowLifecycle';
import { buildFloors } from './agentFlowFloors';
import { applyMetricDeltas, computeBlastRadius, foldTouches, round, sessionMetrics, TOUCHING_KINDS } from './agentFlowMetrics';

Expand Down Expand Up @@ -196,7 +197,7 @@ function applyFloors(
input: AgentFlowBuildInput,
files: AgentFlowFile[],
steps: MutableStep[],
indexByPath: Map<string, number>,
lifecycle: LifecycleResult,
warnings: string[],
): Map<number, NonNullable<AgentFlowDataset['steps'][number]['floors']>> {
if (!input.functions || input.functions.length === 0) { return new Map(); }
Expand All @@ -206,13 +207,12 @@ function applyFloors(
functionsByPath.set(rel, entry.functions);
}
const createdBy = new Map<number, number>();
for (const step of steps) {
for (const path of step.createdPaths) {
const index = indexByPath.get(path);
if (index !== undefined && !createdBy.has(index)) { createdBy.set(index, step.seq); }
}
const movedTo = new Map<number, number>();
for (const [seq, change] of [...lifecycle.steps].sort((a, b) => a[0] - b[0])) {
for (const index of change.created) { if (!createdBy.has(index)) { createdBy.set(index, seq); } }
for (const [from, to] of change.moves) { movedTo.set(from, to); }
}
const result = buildFloors(files, steps, functionsByPath, createdBy);
const result = buildFloors(files, steps, functionsByPath, createdBy, movedTo);
if (result.tooMany > 0) {
warnings.push(`${result.tooMany} file(s) have too many functions to draw as floors and stay one block.`);
}
Expand Down Expand Up @@ -240,8 +240,12 @@ export function buildAgentFlowDataset(input: AgentFlowBuildInput): AgentFlowBuil
const totalDelta = applyMetricDeltas({ steps, turns, files, indexByPath, baseByPath, currentByPath, turnDeltas });
const { blastRadius, unreadDependents } = computeBlastRadius(files, indexByPath, input.dependencyEdges);

// ---- 7. Function floors -------------------------------------------------
const stepFloors = applyFloors(input, files, steps, indexByPath, warnings);
// ---- 7. Lifecycle: which files exist at every step ----------------------
const lifecycle = buildLifecycle(steps, files.length, indexByPath);
files.forEach((file) => { file.existed = lifecycle.existed[file.index]; });

// ---- 8. Function floors -------------------------------------------------
const stepFloors = applyFloors(input, files, steps, lifecycle, warnings);

const dataset: AgentFlowDataset = {
schemaVersion: AGENT_FLOW_SCHEMA_VERSION,
Expand All @@ -255,7 +259,12 @@ export function buildAgentFlowDataset(input: AgentFlowBuildInput): AgentFlowBuil
turns,
steps: steps.map(({ paths: _paths, createdPaths: _created, deletedPaths: _deleted, lineTouches: _lines, ...step }) => {
const floors = stepFloors.get(step.seq);
return floors ? { ...step, floors } : step;
const change = lifecycle.steps.get(step.seq);
return {
...step,
...(floors ? { floors } : {}),
...(change ? { created: change.created, deleted: change.deleted, ...(change.moves.length ? { moves: change.moves } : {}) } : {}),
};
}),
transitions: touches.transitions,
metrics: sessionMetrics({
Expand Down
67 changes: 62 additions & 5 deletions apps/vscode-extension/src/core/agent-flow/agentFlowFloors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
*
* A function is new (its floor is built during the playback) when its
* signature line was added by a write step whose removed lines did not
* mention its name; the earliest such step builds it. Pure: no fs.
* mention its name; the earliest such step builds it. A function a write
* removed (its definition among the removed lines) that the file no longer
* has gets a floor that stands until that step and comes down in it, so the
* timeline shows the file as it was at every step. Pure: no fs.
*/
import type {
AgentFlowFile,
Expand Down Expand Up @@ -190,6 +193,7 @@ function addedNow(touch: AgentTraceLineTouch, seq: number, edits: ReadonlyArray<

/** True when a write at `seq` added the floor's signature line without removing its name. */
function writeBuilds(floor: AgentFlowFloor, seq: number, added: LineRange[], removed: Set<string>): boolean {
if (floor.diedStep !== undefined) { return false; }
if (floor.bornStep >= 0 && floor.bornStep <= seq) { return false; }
const signatureAdded = added.some((range) => range.start <= floor.lineStart && range.end >= floor.lineStart);
return signatureAdded && !removed.has(functionIdentifier(floor.name));
Expand All @@ -211,6 +215,54 @@ function markBorn(floors: AgentFlowFloor[], touches: ReadonlyArray<FileTouch>, e
}
}

/**
* Floors for the functions the session removed: a write whose removed lines
* defined a function the file no longer has. The floor stands from when the
* function was written (a write that defined it, the file's creation, or
* before the session) until the step that removed it, at the point where it
* was. A name defined again later is a rewrite, not a removal.
*/
function removedFloors(
spans: ReadonlyArray<AgentFlowFunctionSpan>,
touches: ReadonlyArray<FileTouch>,
edits: ReadonlyArray<FileEdit>,
createdBy: number,
): AgentFlowFloor[] {
const alive = new Set(spans.map((fn) => functionIdentifier(fn.name)));
const definedAt = new Map<string, number>();
const out: AgentFlowFloor[] = [];
for (const { seq, kind, touch } of touches) {
if (kind !== 'write') { continue; }
const { readded, removals } = definitionChanges(touch, alive);
for (const [name, at] of removals) {
if (out.some((floor) => floor.name === name && floor.diedStep === seq)) { continue; }
out.push(dyingFloor(name, at, seq, edits, definedAt.get(name) ?? bornWithFile(createdBy, seq)));
definedAt.delete(name);
}
for (const name of readded) { if (!definedAt.has(name)) { definedAt.set(name, seq); } }
}
return out;
}

/** The functions a write re-adds, and the ones it removes for good (gone at the end, not re-added). */
function definitionChanges(touch: AgentTraceLineTouch, alive: ReadonlySet<string>): { readded: Set<string>; removals: Array<[string, number]> } {
const hunks = touch.hunks ?? [];
const readded = new Set(hunks.flatMap((hunk) => (hunk.addedDefs ?? []).map(([name]) => name)));
const removals = hunks.flatMap((hunk) => hunk.removedDefs ?? []).filter(([name]) => !alive.has(name) && !readded.has(name));
return { readded, removals };
}

/** A function with no recorded definition was born with its file, when the file was created earlier. */
function bornWithFile(createdBy: number, seq: number): number {
return createdBy >= 0 && createdBy < seq ? createdBy : -1;
}

/** The floor of a function removed at `seq`, at its removal point in today's numbering. */
function dyingFloor(name: string, at: number, seq: number, edits: ReadonlyArray<FileEdit>, born: number): AgentFlowFloor {
const point = forward({ start: at, end: at }, seq, edits).start;
return { name, lineStart: point, lineEnd: point, bornStep: born, diedStep: seq };
}

/** Records that step `seq` touched `floors` of `file` (merging touches of the same file). */
function addStepFloors(stepFloors: Map<number, AgentFlowStepFloors[]>, seq: number, file: number, floors: number[]): void {
const list = stepFloors.get(seq) ?? [];
Expand Down Expand Up @@ -242,13 +294,16 @@ export function buildFloors(
steps: ReadonlyArray<FloorStepInput>,
functionsByPath: ReadonlyMap<string, ReadonlyArray<AgentFlowFunctionSpan>>,
createdBy: ReadonlyMap<number, number> = new Map(),
movedTo: ReadonlyMap<number, number> = new Map(),
): FloorsResult {
const stepFloors = new Map<number, AgentFlowStepFloors[]>();
let floorCount = 0;
let tooMany = 0;
for (const file of files) {
const functions = functionsByPath.get(file.path);
const spans = functions && !file.deleted ? floorFunctions(functions) : [];
// A file moved away keeps, until the move, the functions its new path has now.
const moved = movedTo.get(file.index);
const functions = functionsByPath.get(file.path) ?? (moved !== undefined ? functionsByPath.get(files[moved]?.path ?? '') : undefined);
const spans = functions ? floorFunctions(functions) : [];
if (spans.length === 0) { continue; }
if (spans.length > MAX_FLOORS_PER_FILE || floorCount + spans.length > MAX_FLOORS_TOTAL) { tooMany += 1; continue; }
file.floors = fileFloors(file, spans, steps, createdBy.get(file.index) ?? -1, stepFloors);
Expand All @@ -265,16 +320,18 @@ function fileFloors(
createdBy: number,
stepFloors: Map<number, AgentFlowStepFloors[]>,
): AgentFlowFloor[] {
const floors: AgentFlowFloor[] = spans.map((fn) => ({ name: fn.name, lineStart: fn.lineStart, lineEnd: fn.lineEnd, bornStep: -1 }));
const touches = fileTouches(steps, file.index, file.path);
const edits: FileEdit[] = touches
.filter((entry) => entry.kind === 'write' && entry.touch.hunks?.length)
.map((entry) => ({ seq: entry.seq, hunks: sortedHunks(entry.touch.hunks ?? []) }));
const floors: AgentFlowFloor[] = spans.map((fn) => ({ name: fn.name, lineStart: fn.lineStart, lineEnd: fn.lineEnd, bornStep: -1 }));
markBorn(floors, touches, edits, createdBy);
floors.push(...removedFloors(spans, touches, edits, createdBy));
floors.sort((a, b) => a.lineStart - b.lineStart || (a.diedStep ?? Infinity) - (b.diedStep ?? Infinity));
for (const { seq, touch } of touches) {
const ranges = touchRanges(touch).map((range) => forward(range, seq, edits));
if (ranges.length === 0) { continue; }
const hit = overlapping(floors, ranges);
const hit = overlapping(floors, ranges).filter((index) => !(floors[index].diedStep !== undefined && (floors[index].diedStep as number) <= seq));
// The floors this step built are part of what it wrote.
floors.forEach((floor, index) => { if (floor.bornStep === seq && !hit.includes(index)) { hit.push(index); } });
addStepFloors(stepFloors, seq, file.index, hit);
Expand Down
79 changes: 79 additions & 0 deletions apps/vscode-extension/src/core/agent-flow/agentFlowLifecycle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Which files exist at every point of an agent session: the lifecycle the
* timeline replays. Every action that adds, removes or moves a file reaches
* the steps as `createdPaths` / `deletedPaths` (Write, apply_patch, ACP
* diffs and deletes, and the shell commands shellCommandClassifier reads),
* and this module turns them into per-step indexes, a per-file "existed
* before the session" flag and the moves (a delete and a create of the same
* name in one step). A new kind of action only has to fill those two lists.
* Pure: no fs.
*/

/** A step as the lifecycle needs it. */
export interface LifecycleStepInput {
seq: number;
files: number[];
createdPaths: string[];
deletedPaths: string[];
}

export interface StepLifecycle {
created: number[];
deleted: number[];
/** [from, to] file indexes moved by the step. */
moves: Array<[number, number]>;
}

export interface LifecycleResult {
/** Per step seq, the files it created, deleted and moved (only steps that change any). */
steps: Map<number, StepLifecycle>;
/** Per file index: it existed before the session's first step. */
existed: boolean[];
}

function indexesOf(paths: ReadonlyArray<string>, indexByPath: ReadonlyMap<string, number>): number[] {
const out: number[] = [];
for (const path of paths) {
const index = indexByPath.get(path);
if (index !== undefined && !out.includes(index)) { out.push(index); }
}
return out;
}

function baseName(path: string): string {
return path.slice(path.lastIndexOf('/') + 1);
}

/** A step that deletes `a/x.ts` and creates `b/x.ts` (or deletes one file and creates one) moved it. */
function pairMoves(created: number[], deleted: number[], pathOf: (index: number) => string): Array<[number, number]> {
if (created.length === 1 && deleted.length === 1) { return [[deleted[0], created[0]]]; }
const moves: Array<[number, number]> = [];
for (const from of deleted) {
const to = created.find((index) => baseName(pathOf(index)) === baseName(pathOf(from)) && !moves.some((move) => move[1] === index));
if (to !== undefined) { moves.push([from, to]); }
}
return moves;
}

export function buildLifecycle(
steps: ReadonlyArray<LifecycleStepInput>,
fileCount: number,
indexByPath: ReadonlyMap<string, number>,
): LifecycleResult {
const pathByIndex = new Map<number, string>();
for (const [path, index] of indexByPath) { pathByIndex.set(index, path); }
const result: LifecycleResult = { steps: new Map(), existed: new Array<boolean>(fileCount).fill(true) };
// A file's first event decides: created before anything else touched it -> it did not exist yet.
const decided = new Set<number>();
for (const step of [...steps].sort((a, b) => a.seq - b.seq)) {
const created = indexesOf(step.createdPaths, indexByPath);
const deleted = indexesOf(step.deletedPaths, indexByPath).filter((index) => !created.includes(index));
for (const index of created) {
if (!decided.has(index)) { result.existed[index] = false; }
}
for (const index of [...created, ...deleted, ...step.files]) { decided.add(index); }
if (created.length + deleted.length === 0) { continue; }
result.steps.set(step.seq, { created, deleted, moves: pairMoves(created, deleted, (index) => pathByIndex.get(index) ?? '') });
}
return result;
}
9 changes: 6 additions & 3 deletions apps/vscode-extension/src/core/agent-flow/agentFlowMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,16 @@ function markFiles(paths: string[], indexByPath: Map<string, number>, mark: (ind
}
}

function touchFile(state: TouchState, step: MutableStep, index: number): void {
function touchFile(state: TouchState, step: MutableStep, index: number, removes: ReadonlySet<number>): void {
const file = state.files[index];
if (step.kind === 'read') {
file.touches.reads += 1;
if (state.readSoFar.has(index)) { state.revisits += 1; }
state.readSoFar.add(index);
} else if (step.kind === 'write') {
file.touches.writes += 1;
if (!state.readSoFar.has(index) && !file.created) { state.writtenBeforeRead.add(index); }
// Creating, deleting or moving a file is not editing what it says: never blind.
if (!state.readSoFar.has(index) && !file.created && !removes.has(index)) { state.writtenBeforeRead.add(index); }
} else if (step.kind === 'search') {
file.touches.searches += 1;
} else {
Expand All @@ -106,9 +107,11 @@ function recordHop(state: TouchState, step: MutableStep): void {
function foldStepTouches(state: TouchState, step: MutableStep, indexByPath: Map<string, number>): void {
markFiles(step.createdPaths, indexByPath, (index) => { state.files[index].created = true; });
const set = state.turnFiles.get(step.turn) ?? new Set<number>();
const removes = new Set<number>();
markFiles(step.deletedPaths, indexByPath, (index) => { removes.add(index); });
for (const index of step.files) {
set.add(index);
touchFile(state, step, index);
touchFile(state, step, index, removes);
}
markFiles(step.deletedPaths, indexByPath, (index) => { state.files[index].deleted = true; });
state.turnFiles.set(step.turn, set);
Expand Down
Loading
Loading