/** * Impact Analysis Service (Phase 4 D3) * * Given a git diff scope (unstaged / staged / committed-vs-base), compute the * set of symbols whose callers or dependents are affected by the change, and * rank them by a risk score derived from PageRank centrality (blast radius) * and graph distance (closer = riskier). * * Approach (ported-and-rewritten from codebase-memory-mcp's `detect_changes` * concept; that reference only mapped files→symbols — the reverse-traversal * and risk scoring here are fresh): * * 1. Run a SCOPED `git diff --name-only` to get changed files (never the * whole repo; the diff runner is injectable for tests). * 2. Map each changed file → symbols defined there (listDefinitions). * 3. REVERSE-traverse the dependency graph to find impacted consumers: * - File level: who imports the changed file? (symbol_imports reverse) * - Symbol level: who references the changed symbols? (get_references) * These two are the reliable CROSS-FILE signals. Typed CALL edges (D1) * are same-file only, so they are a secondary refinement, not the spine. * 4. Propagate impact by `depth` over the reverse file-import graph. * 5. Score each impacted symbol: risk = centralityWeight + proximityWeight * (centrality of the impacted file; closer hops weigh more). * 6. Sort descending, cap the result count. * * Cost is bounded by MAX_DEPTH, MAX_IMPACTED, and a visited set so a patho- * logical fan-out cannot exhaust memory/time. */ import { getSymbolRepository } from "../../data/symbol/symbol-repository-factory.js"; export type ImpactScope = "unstaged" | "staged" | "committed"; export interface ImpactAnalysisOptions { projectId: string; /** Absolute path to the project working tree (where `git` runs). */ projectPath: string; /** What diff to analyze. */ scope: ImpactScope; /** For committed scope: diff against this branch (default "main"). */ baseBranch?: string; /** For committed scope: commits since this date/ref (e.g. "2026-07-01" or a SHA). */ since?: string; /** How far to propagate impact through the reverse import graph. Default 2. */ depth?: number; /** Optional filter — only consider these changed paths (relative). */ paths?: string[]; /** Injectable diff runner (tests pass a stub; production runs real git). */ diffRunner?: (projectPath: string, scope: ImpactScope, baseBranch?: string, since?: string) => string[]; /** * Injectable symbol repository (tests pass an instrumented stub; production * resolves the singleton via {@link getSymbolRepository}). Using this seam * instead of `mock.module` on the factory avoids a process-wide mock that * leaks into concurrently-run sibling suites (which then crash the ETL * pipeline with "clearProject is not a function" on the real repo). */ repoOverride?: ReturnType; } export interface ChangedFile { /** Relative path inside the project (matches symbol_definitions.file_path). */ path: string; /** Symbols defined in this file. */ symbols: { fqn: string; name: string; kind: string; line: number; }[]; } export interface ImpactedSymbol { /** The impacted consumer symbol (a caller/dependent of the changed code). */ fqn: string; name: string; file: string; line: number; /** Hop distance from the nearest changed file (0 = directly imports it). */ depth: number; /** PageRank centrality of the impacted symbol's file (0–1). */ centrality: number; /** Computed risk score (higher = riskier). Sorted descending. */ risk: number; /** Human-readable reason this symbol is impacted. */ reason: string; /** Which changed file/symbol reaches it. */ via: { changedFile: string; changedSymbol?: string; edge: "import" | "reference"; }; } export interface ImpactAnalysisResult { projectId: string; scope: ImpactScope; baseBranch?: string; since?: string; depth: number; changedFiles: ChangedFile[]; impacted: ImpactedSymbol[]; truncated: boolean; /** Diagnostic when git produced no output (e.g. clean tree). */ note?: string; } export declare class ImpactAnalysisService { private static instance; private constructor(); static getInstance(): ImpactAnalysisService; /** * Run the impact analysis. See {@link ImpactAnalysisOptions}. */ analyze(opts: ImpactAnalysisOptions): Promise; /** All file paths indexed for the project (relative). */ private allIndexedFiles; /** Definitions in a single file (kind kept for filtering). */ private listDefinitionsByFile; /** * Reverse import graph: importerOf(file) = list of importer file paths for * every file that imports `file` (i.e. symbol_imports where to_file === file). * Built once from allImportEdges (client-side reverse; PG repo lacks * findImporters). PostgreSQL allImportEdges returns a minimal {from_file,to_file} * row (no specifier); PG returns full SymbolImport — we coerce to the minimal * shape so both backends unify. */ private buildReverseImportGraph; /** PageRank centrality map (file → score). */ private getCentralityMap; } /** * Run a SCOPED `git diff --name-only` in `projectPath`. Returns relative paths. * Scope: * - unstaged: `git diff --name-only` * - staged: `git diff --name-only --cached` * - committed: `git diff --name-only ...HEAD` (since wins if provided: * `git diff --name-only ...HEAD`) * * Never invoked on the whole repo by the tool — the scope is always bounded. * Throws on git failure so the tool can surface a clean error. */ export declare function defaultDiffRunner(projectPath: string, scope: ImpactScope, baseBranch?: string, since?: string): string[]; export declare const impactAnalysisService: ImpactAnalysisService; //# sourceMappingURL=impact-analysis.d.ts.map