/** * Workflow-run segmentation for the token-audit tool: split a main session into runs and * attach each subagent to the run that spawned it. * * A main session is cut at the user's workflow slash commands (`` tags) and at * `Skill` tool_use events naming a workflow; `/clear` ends the current run and returns to an * ad-hoc segment. * * D-AUDIT-SEGMENT-LINK: a subagent joins the segment that contains the main-session tool_use * which spawned it, found by the first rule that applies: * 1. `meta.toolUseId` -> the main session's `Agent`/`Task` tool_use id. * 2. The `subagents/workflows//` directory the transcript sits in -> the main * session's `Workflow` tool_use whose tool_result reports `toolUseResult.runId` equal to * that directory name. A workflow's subagents carry no `toolUseId` (the `Workflow` tool, not * the `Agent` tool, spawned them), so the run id is the only key; both sides must match * `WORKFLOW_RUN_ID_RE` and the id is only ever compared, never built into a path. * 3. `meta.parentAgentId` (a nested spawn inherits its parent's run). * Anything still unlinked lands in the `unattributed` segment with no main turns of its own. */ import type { CommandMention, SkillCall, SpawnCall, WorkflowRunCall } from './parse.js'; /** Workflow commands and skills that open a run. */ export const WORKFLOW_SKILLS: ReadonlySet = new Set([ 'devflow:implement', 'devflow:plan', 'devflow:code-review', 'devflow:resolve', 'devflow:release', 'devflow:debug', 'devflow:self-review', 'devflow:explore', ]); export const AD_HOC = 'adhoc'; export const UNATTRIBUTED = 'unattributed'; const CLEAR = 'clear'; /** Longest parentAgentId chain followed when linking a nested subagent. */ const MAX_PARENT_HOPS = 8; export interface Segment { readonly id: string; readonly sessionId: string; readonly label: string; /** Line index in the main transcript where the run starts; -1 for the implicit opening segment. */ readonly startOrder: number; } function segmentOf(sessionId: string, label: string, startOrder: number): Segment { return { id: `${sessionId}:${label}@${startOrder}`, sessionId, label, startOrder }; } /** The segment a subagent lands in when nothing links it to a spawn. */ export function unattributedSegment(sessionId: string): Segment { return { id: `${sessionId}:${UNATTRIBUTED}`, sessionId, label: UNATTRIBUTED, startOrder: -1 }; } /** Ordered segments of one main session; never empty (the first is the ad-hoc segment at -1). */ export type Segments = readonly [Segment, ...Segment[]]; /** * Ordered segments of one main session; the first is always the ad-hoc segment starting at -1. * Of two boundaries on the same line the later one wins. */ export function buildSegments( sessionId: string, commands: readonly CommandMention[], skillCalls: readonly SkillCall[], ): Segments { const boundaries: Array<{ readonly order: number; readonly label: string }> = []; for (const command of commands) { if (command.name === CLEAR) boundaries.push({ order: command.order, label: AD_HOC }); else if (WORKFLOW_SKILLS.has(command.name)) boundaries.push({ order: command.order, label: command.name }); } for (const call of skillCalls) { if (WORKFLOW_SKILLS.has(call.name)) boundaries.push({ order: call.order, label: call.name }); } boundaries.sort((a, b) => a.order - b.order); const rest: Segment[] = []; for (const boundary of boundaries) { const last = rest[rest.length - 1]; if (last !== undefined && last.startOrder === boundary.order) rest.pop(); rest.push(segmentOf(sessionId, boundary.label, boundary.order)); } return [segmentOf(sessionId, AD_HOC, -1), ...rest]; } /** The segment containing line `order`: the last one starting at or before it. */ export function segmentAt(segments: Segments, order: number): Segment { let low = 0; let high = segments.length - 1; while (low < high) { const mid = Math.ceil((low + high) / 2); const candidate = segments[mid]; if (candidate !== undefined && candidate.startOrder <= order) low = mid; else high = mid - 1; } return segments[low] ?? segments[0]; } export interface SubagentLinkInput { readonly agentId: string; readonly toolUseId: string | null; readonly parentAgentId: string | null; /** The `wf_*` directory of a workflow-run subagent; absent or null for any other. */ readonly workflowRunId?: string | null; } /** * Map each subagent id to the segment that spawned it (D-AUDIT-SEGMENT-LINK in the file header). * Joins are Map-based: one pass over the main session's spawns and workflow runs, then one * memoised resolution per subagent (a nested subagent follows `parentAgentId`, at most * `MAX_PARENT_HOPS` hops, so a cycle cannot loop). */ export function linkSubagents( sessionId: string, segments: Segments, spawns: readonly SpawnCall[], agents: readonly SubagentLinkInput[], workflowRuns: readonly WorkflowRunCall[] = [], ): ReadonlyMap { const spawnOrder = new Map(); for (const spawn of spawns) { if (!spawnOrder.has(spawn.toolUseId)) spawnOrder.set(spawn.toolUseId, spawn.order); } const runOrder = new Map(); for (const run of workflowRuns) { if (!runOrder.has(run.runId)) runOrder.set(run.runId, run.order); } const byId = new Map(); for (const agent of agents) byId.set(agent.agentId, agent); const orphan = unattributedSegment(sessionId); const resolved = new Map(); const resolve = (agentId: string, hops: number): Segment => { const memo = resolved.get(agentId); if (memo !== undefined) return memo; const agent = byId.get(agentId); let segment = orphan; if (agent !== undefined) { const spawnAt = agent.toolUseId === null ? undefined : spawnOrder.get(agent.toolUseId); const runKey = agent.workflowRunId ?? null; const runAt = runKey === null ? undefined : runOrder.get(runKey); const order = spawnAt ?? runAt; if (order !== undefined) { segment = segmentAt(segments, order); } else if (agent.parentAgentId !== null && hops < MAX_PARENT_HOPS && byId.has(agent.parentAgentId)) { segment = resolve(agent.parentAgentId, hops + 1); } } resolved.set(agentId, segment); return segment; }; for (const agent of agents) resolve(agent.agentId, 0); return resolved; }