/** * Trace Path Service (Phase 4 D2) * * Graph traversal over the typed structural edges emitted by D1 * (CALLS / DATA_FLOWS / HTTP_CALLS / EMITS / LISTENS / IMPORTS). * * Given a seed symbol, performs a bounded, cycle-guarded BFS over the * edge graph filtered by `mode` (which edge types to follow) and * `direction` (outbound = what the seed reaches; inbound = what reaches * the seed; both = run each separately). * * Output is agent-consumable: a set of nodes (symbols with file:line) * and edges (type + route/event/param metadata), plus readable call * chains reconstructed from the BFS predecessor map. * * Cost is bounded by `maxDepth` (default 3, hard cap MAX_DEPTH) and a * visited set keyed on (FQN, depth) so cycles cannot loop forever. */ import type { EdgeType } from "./symbol-graph.service.js"; import { type DefinitionLookupResult } from "./definition-lookup.js"; export type TraceDirection = "outbound" | "inbound" | "both"; export type TraceMode = "calls" | "data_flow" | "cross_service" | "all"; export interface TracePathOptions { /** Seed symbol — a bare name resolved against definitions, OR a FQN * ('rel/path.ts#Name') used directly. */ symbol: string; /** Alias kept for clarity; merged with `symbol`. */ function_name?: string; /** Explicit FQN — when set, skips name resolution entirely. */ qualifiedName?: string; projectId: string; direction?: TraceDirection; mode?: TraceMode; /** Max BFS depth. Default 3, hard-capped at MAX_DEPTH. */ depth?: number; /** Include test files in the traversal. Default false. */ include_tests?: boolean; /** Explicit edge-type override; wins over `mode`. */ edge_types?: EdgeType[]; } export interface TraceNode { /** Stable symbol id: either the resolved FQN or, for unresolved callees, * the bare name as it appeared in source. */ fqn: string; name: string; file?: string; line?: number; /** Hop distance from the nearest seed (0 for seeds). */ depth: number; /** True when this node lives in a test file. */ isTest?: boolean; /** True when this node is one of the BFS seeds. */ isSeed?: boolean; } export interface TraceEdge { type: EdgeType; from: string; to: string; fromFile: string; fromLine: number; /** Typed-edge metadata (route, event, paramIndex, argName, callerFqn, method...). */ meta?: Record | null; } export interface TracePathResult { projectId: string; symbol: string; mode: TraceMode; direction: TraceDirection; /** Edge types actually used for the traversal (resolved mode/explicit). */ edgeTypes: EdgeType[]; /** Seeds the BFS started from (may be >1 for ambiguous name). */ seeds: string[]; nodes: TraceNode[]; edges: TraceEdge[]; /** Readable call chains (one per reached leaf), e.g. "a() -> b() -> c()". */ chains: string[]; truncated: boolean; /** Exact FQN lookup result; ambiguity is explicit and always has zero traversal. */ identityResolution?: Exclude; } export declare class TracePathService { private readonly identityLookup; private static instance; constructor(identityLookup?: import("./definition-lookup.js").DefinitionLookupService); static getInstance(): TracePathService; /** * Resolve the seed symbol to one or more FQNs. * * Precedence: * 1. Explicit `qualifiedName` (used verbatim — single seed). * 2. `symbol`/`function_name` that looks like a FQN (contains '#'). * 3. Name lookup against `findDefinitionsByName` → all matches (union BFS). * * Returns `[]` when nothing resolves; the caller surfaces a not-found hint. */ resolveSeeds(projectId: string, opts: TracePathOptions): Promise<{ fqn: string; name: string; file?: string; line?: number; }[]>; private resolveSeedResult; /** * Run the traversal. See {@link TracePathOptions} / {@link TracePathResult}. */ tracePath(opts: TracePathOptions): Promise; /** * Breadth-first traversal. `addNode`/`addEdge` mutators are injected so the * outbound and inbound passes share one node/edge set. The visited set is * keyed on FQN (not FQN+depth) so each symbol is expanded at most once — * depth is still tracked per-node for reporting. * * Outbound: follow edges where the seed-side symbol is the `fromSymbol` * (caller / source). The reached node is the edge's `targetFqn`. * Inbound: follow edges where the seed-side symbol is the `toSymbol` * (callee / target). The reached node is the edge's source FQN. */ private bfs; /** * For an inbound edge, the "other end" is the caller. The D1 extractor stamps * `meta.callerFqn` on CALL edges; fall back to the edge's symbol name when no * FQN is recorded (e.g. cross-file calls that did not resolve). */ private extractCallerFqn; /** * Build a synthetic leaf id for edges whose callee is not a project symbol * (HTTP routes, event names, external globals). Keeps the edge + its * meaningful metadata in the result instead of dropping it. */ private syntheticTarget; /** Derive a short display name from a FQN ('path/file.ts#Name' → 'Name'). */ private fqnToName; /** * Reconstruct readable call chains (root → leaf) from the edge set, scoped to * the seed roots. A leaf is any node that is never a `from` in the edge set. * * Bounded: the DFS carries a walk budget (MAX_CHAIN_WALKS) and stops as soon * as enough chains are collected, so a dense hub topology cannot explode the * recursion before the final `slice(0, 50)` cap. */ private buildChains; } export declare const tracePathService: TracePathService; //# sourceMappingURL=trace-path.d.ts.map