@@ -18,7 +18,7 @@ import { pathToFileURL } from 'node:url';
1818import { zoneRank } from '../layering/model.ts' ;
1919import { runAffected } from './affected-run.ts' ;
2020import { loadGraph } from './load.ts' ;
21- import { computeLevels , type GraphData } from './model.ts' ;
21+ import { computeLevels , markTransitivelyReachableEdges , type GraphData } from './model.ts' ;
2222
2323const repoRoot = execFileSync ( 'git' , [ 'rev-parse' , '--show-toplevel' ] , {
2424 encoding : 'utf8' ,
@@ -79,8 +79,14 @@ function edgeFlags(edge: GraphData['edges'][number]): number {
7979 ) ;
8080}
8181
82+ /** Transitive reachability is undefined on a value cycle; the report then lists the cycle instead. */
83+ function hasValueCycle ( graph : Pick < GraphData , 'cycles' > ) : boolean {
84+ return graph . cycles . some ( ( cycle ) => cycle . kind === 'value' ) ;
85+ }
86+
8287function buildPayload ( ) : Payload {
8388 const graph = loadGraph ( repoRoot ) ;
89+ if ( ! hasValueCycle ( graph ) ) markTransitivelyReachableEdges ( graph . edges ) ;
8490 const levels = computeLevels ( graph . nodes , graph . edges ) ;
8591
8692 const zoneIndex = new Map ( graph . zones . map ( ( zone , index ) => [ zone . id , index ] ) ) ;
@@ -115,6 +121,33 @@ function buildPayload(): Payload {
115121 } ;
116122}
117123
124+ /** The text summary printed after the JSON is written; every count is read back from `payload`. */
125+ function formatSummary ( payload : Payload , jsonPath : string ) : string {
126+ const valueCycles = payload . cycles . filter ( ( cycle ) => cycle . kind === 'value' ) . length ;
127+ const otherCycles = payload . cycles . length - valueCycles ;
128+ const backEdges = payload . edges . filter ( ( [ , , , flags ] ) => flags & 1 ) . length ;
129+ const transitivelyReachable =
130+ valueCycles > 0
131+ ? 'not computed while a value-import cycle exists'
132+ : payload . edges . filter ( ( [ , , , flags ] ) => flags & 2 ) . length ;
133+ const typeInversions = Object . values ( payload . typeInversions ) . reduce ( ( sum , n ) => sum + n , 0 ) ;
134+ const authorityCounts = Object . entries ( payload . authorityCounts )
135+ . map ( ( [ label , count ] ) => `${ label } =${ count } ` )
136+ . join ( ', ' ) ;
137+ return (
138+ `Dependency graph: ${ payload . generated . files } files, ${ payload . generated . edges } edges, ` +
139+ `${ payload . zones . length } zones\n` +
140+ ` value-import cycles (R4): ${ valueCycles } \n` +
141+ ` type-only/dynamic cycles (not gate-rejected): ${ otherCycles } \n` +
142+ ` spine back-edges (R5): ${ backEdges } \n` +
143+ ` type-only spine inversions (R6): ${ typeInversions } \n` +
144+ ` value edges whose target is also reachable at distance >= 2: ${ transitivelyReachable } \n` +
145+ ` declared-authority labels: ${ authorityCounts } \n` +
146+ ` (reachability only — not a removability claim, see scripts/depgraph/README.md)\n` +
147+ ` wrote ${ path . relative ( repoRoot , jsonPath ) } \n`
148+ ) ;
149+ }
150+
118151async function main ( argv : readonly string [ ] ) : Promise < number > {
119152 // `affected` is the query subcommand (scripts/depgraph/affected-run.ts); with no subcommand
120153 // this stays the whole-graph report.
@@ -130,26 +163,7 @@ async function main(argv: readonly string[]): Promise<number> {
130163 fs . mkdirSync ( path . dirname ( jsonPath ) , { recursive : true } ) ;
131164 fs . writeFileSync ( jsonPath , `${ JSON . stringify ( payload , null , 2 ) } \n` ) ;
132165
133- const valueCycles = payload . cycles . filter ( ( cycle ) => cycle . kind === 'value' ) . length ;
134- const otherCycles = payload . cycles . length - valueCycles ;
135- const backEdges = payload . edges . filter ( ( [ , , , flags ] ) => flags & 1 ) . length ;
136- const transitivelyReachable = payload . edges . filter ( ( [ , , , flags ] ) => flags & 2 ) . length ;
137- const typeInversions = Object . values ( payload . typeInversions ) . reduce ( ( sum , n ) => sum + n , 0 ) ;
138- const authorityCounts = Object . entries ( payload . authorityCounts )
139- . map ( ( [ label , count ] ) => `${ label } =${ count } ` )
140- . join ( ', ' ) ;
141- process . stdout . write (
142- `Dependency graph: ${ payload . generated . files } files, ${ payload . generated . edges } edges, ` +
143- `${ payload . zones . length } zones\n` +
144- ` value-import cycles (R4): ${ valueCycles } \n` +
145- ` type-only/dynamic cycles (not gate-rejected): ${ otherCycles } \n` +
146- ` spine back-edges (R5): ${ backEdges } \n` +
147- ` type-only spine inversions (R6): ${ typeInversions } \n` +
148- ` value edges whose target is also reachable at distance >= 2: ${ transitivelyReachable } \n` +
149- ` declared-authority labels: ${ authorityCounts } \n` +
150- ` (reachability only — not a removability claim, see scripts/depgraph/README.md)\n` +
151- ` wrote ${ path . relative ( repoRoot , jsonPath ) } \n` ,
152- ) ;
166+ process . stdout . write ( formatSummary ( payload , jsonPath ) ) ;
153167 return 0 ;
154168}
155169
0 commit comments