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
23 changes: 22 additions & 1 deletion apps/vscode-extension/src/core/agent-flow/agentTraceSources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ export interface AgentTraceSource {
* Returns the stop function.
*/
follow(onEvents: (events: AgentTraceEvent[]) => void): () => void;
/**
* The agent's own id for this session, when it is known (an IDE chat
* running Claude Code over ACP has the id of the transcript Claude Code
* writes). A transcript with that id is the same session: the listing
* shows the live source only.
*/
agentSessionId?(): string | null;
}

/** A transcript written in the last few minutes is treated as a running session. */
Expand Down Expand Up @@ -90,6 +97,7 @@ export class PushAgentTraceSource implements AgentTraceSource {
private updated = Date.now();
private title: string;
private live = true;
private agentSession: string | null = null;
public readonly id: string;
public readonly kind: AgentTraceSourceKind;
public readonly agent: string;
Expand All @@ -107,6 +115,8 @@ export class PushAgentTraceSource implements AgentTraceSource {
public updatedAt(): number { return this.updated; }
public isLive(): boolean { return this.live; }
public setLive(live: boolean): void { this.live = live; }
public agentSessionId(): string | null { return this.agentSession; }
public setAgentSessionId(id: string | null | undefined): void { this.agentSession = id || null; }

public push(events: AgentTraceEvent[]): void {
if (events.length === 0) { return; }
Expand Down Expand Up @@ -399,10 +409,21 @@ export function discoverCodexSessions(root: string, options: DiscoveryOptions =
return sources;
}

/** Every source for a root: live ones first, then transcripts, newest first. */
/**
* True when a transcript id (`claude-code:<session>`, `codex:rollout-<date>-<session>`)
* names a session a live source already shows.
*/
export function coveredByLive(sourceId: string, agentSessionIds: readonly string[]): boolean {
const name = sourceId.slice(sourceId.indexOf(':') + 1);
return agentSessionIds.some((id) => name === id || name.endsWith(`-${id}`));
}

/** Every source for a root: live ones first, then transcripts, newest first (a transcript a live chat already shows is left out). */
export function listAgentTraceSources(root: string, options: DiscoveryOptions = {}): AgentTraceSource[] {
const live = [...liveSources.values()].filter((entry) => sameRoot(entry.root, root) || isWithinRoot(root, entry.root)).map((entry) => entry.source);
const covered = live.map((source) => source.agentSessionId?.() ?? null).filter((id): id is string => !!id);
const files = [...discoverClaudeCodeSessions(root, options), ...discoverCodexSessions(root, options)]
.filter((source) => !coveredByLive(source.id, covered))
.sort((a, b) => b.updatedAt() - a.updatedAt());
return [...live.sort((a, b) => b.updatedAt() - a.updatedAt()), ...files];
}
Expand Down
12 changes: 11 additions & 1 deletion apps/vscode-extension/test/analysis/agentFlowCore.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const { AcpSessionEventMapper } = out('acpSessionEvents.js');
const { layoutCity } = out('agentFlowLayout.js');
const { buildAgentFlowDataset, relativeToRoot } = out('agentFlowBuilder.js');
const { AgentFlowService, readAnalysisMetrics, readDependencyEdges, openAgentFlowInScenes, registerAgentFlowController } = out('agentFlowService.js');
const { PushAgentTraceSource, registerLiveAgentTraceSource, listAgentTraceSources, discoverClaudeCodeSessions, discoverCodexSessions, isWithinRoot } = out('agentTraceSources.js');
const { PushAgentTraceSource, registerLiveAgentTraceSource, listAgentTraceSources, discoverClaudeCodeSessions, discoverCodexSessions, isWithinRoot, coveredByLive } = out('agentTraceSources.js');
const { sampleEvents, sampleBaseMetrics, EXT_ROOT, T0 } = require('../manual/agent-flow/sampleSession.cjs');

const ROOT = '/work/demo';
Expand Down Expand Up @@ -389,6 +389,16 @@ test('Claude Code and Codex sessions of a folder are discovered from the local h
const codex = discoverCodexSessions(root, options);
assert.deepEqual(codex.map((s) => [s.id, s.label()]), [['codex:rollout-a', 'Write docs']]);
assert.equal(listAgentTraceSources(root, options).length, 2);
// An IDE chat running Claude Code over ACP writes that same transcript: listed once, as the live chat.
const chat = new PushAgentTraceSource('ide-chat:7', 'ide-acp', 'Claude Code', 'Failing tests');
const unregisterChat = registerLiveAgentTraceSource(root, chat);
assert.deepEqual(listAgentTraceSources(root, options).map((s) => s.id).sort(), ['claude-code:one', 'codex:rollout-a', 'ide-chat:7']);
chat.setAgentSessionId('one');
assert.deepEqual(listAgentTraceSources(root, options).map((s) => s.id).sort(), ['codex:rollout-a', 'ide-chat:7']);
unregisterChat();
assert.equal(listAgentTraceSources(root, options).length, 2);
assert.equal(coveredByLive('codex:rollout-2026-11-28T10-00-00-abc', ['abc']), true);
assert.equal(coveredByLive('claude-code:one', ['on']), false);
assert.equal(isWithinRoot(root, path.join(root, 'a')), true);
assert.equal(isWithinRoot(root, path.join(home, 'work')), false);
assert.deepEqual(discoverClaudeCodeSessions(path.join(home, 'nothing'), options), []);
Expand Down
Loading