mirror of
https://github.com/callstack/agent-device.git
synced 2026-09-14 20:06:34 +08:00
cd19a73c55
Renders every production file under src/ as a pannable graph in one self-contained HTML file — no external requests, no runtime dependency, layouts precomputed at build time so the viewer never runs a physics simulation on a phone. pnpm depgraph # -> .tmp/depgraph/index.html (+ index.json) pnpm depgraph:test It reuses the layering gate's model (`listSourceFiles`, `resolveImportEdges`, `zoneRank`) rather than extracting its own graph. That matters more than it sounds: a separate extractor with its own resolution behaviour would draw a graph nobody enforces. Because the model is shared, its R6 count reproduces TYPE_INVERSION_BASELINE exactly, which doubles as a self-check. The README now documents WHEN it is productive, because the honest answer is "for three questions, and it misleads on a fourth": - what am I about to break (dependent counts, including the type-only and dynamic edges a grep for `from '...'` misses); - where is the debt concentrated (zone-level counts); - what is wrong that CI does not enforce — ~1300 transitively redundant value edges and 8 type-only/dynamic cycles, both outside the gate by design. The fourth: a cluster's SIZE IS NOT ITS DIFFICULTY. `commands -> client` looked like the obvious win at 28 edges into one file; moving that file down took the gate from 42 to 48, because the vocabulary it holds depends on commands/, metro/, core/ and remote/. The render shows an edge's weight, not whether it can be reversed — so the README pairs every visual question with the numeric query that answers "can this actually move?", verified against the real output rather than written from memory. Also states plainly that `pnpm check:layering` is authoritative and nothing here gates a merge: it is an instrument, not a rule. scripts/depgraph/** joins scripts/layering/**, scripts/perf/** and scripts/maestro-conformance/** in Fallow's ignorePatterns, which is how this repo already treats tooling trees. Worth knowing rather than discovering: that exempts viewer.js from the complexity gate, and its `draw` function would fail it. Two exports added to scripts/layering/model.ts: `zoneRank` (the viewer colours nodes by rank, so an inversion reads as an edge pointing the wrong way down the ramp) and `targetDagZone`, previously module-private. `pnpm check` green, 4488 unit tests. Verified against current main: 898 files, 4627 edges, 25 zones, R6 count matching the gate. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Bfu8HofkhybiAm5LECfqur
275 lines
8.6 KiB
TypeScript
275 lines
8.6 KiB
TypeScript
// Dependency-graph analysis model — pure functions over the layering gate's edge model.
|
||
//
|
||
// The graph is deliberately derived from `scripts/layering/model.ts` rather than a
|
||
// third-party extractor: the gate's file set (production `src/**/*.ts`, tests excluded),
|
||
// zone partition, edge kinds (value/type-only/dynamic), and cycle definition are already
|
||
// the repo's source of truth. A second extractor with its own resolution rules would
|
||
// visualize a graph the gate does not enforce.
|
||
|
||
import {
|
||
backEdgePair,
|
||
classifyZone,
|
||
findValueImportCycles,
|
||
targetDagZone,
|
||
typeInversionPair,
|
||
type ResolvedImportEdge,
|
||
} from '../layering/model.ts';
|
||
|
||
export type EdgeKind = 'value' | 'type' | 'dynamic';
|
||
|
||
export type GraphEdge = {
|
||
from: string;
|
||
to: string;
|
||
kind: EdgeKind;
|
||
line: number;
|
||
/** Set when this edge is a ranked-spine back-edge (`R5`), as `from-zone -> to-zone`. */
|
||
backEdge: string | null;
|
||
/** Set when this edge is a type-only spine inversion (`R6`), as `from-zone -> to-zone`. */
|
||
typeInversion: string | null;
|
||
/** True when the same pair is also reachable through a longer path of the same weight class. */
|
||
redundant: boolean;
|
||
};
|
||
|
||
export type GraphNode = {
|
||
id: string;
|
||
zone: string;
|
||
/** First two path segments — a finer cluster than the zone, used for layout gravity. */
|
||
group: string;
|
||
loc: number;
|
||
fanIn: number;
|
||
fanOut: number;
|
||
/** Index into `GraphData.cycles`, or -1. */
|
||
cycle: number;
|
||
};
|
||
|
||
export type ZoneEdge = {
|
||
from: string;
|
||
to: string;
|
||
count: number;
|
||
valueCount: number;
|
||
backEdge: boolean;
|
||
};
|
||
|
||
export type GraphCycle = {
|
||
path: string[];
|
||
/** `value` cycles are gate-rejected (R4); the others are gate-invisible by design. */
|
||
kind: EdgeKind;
|
||
};
|
||
|
||
export type GraphData = {
|
||
nodes: GraphNode[];
|
||
edges: GraphEdge[];
|
||
zones: { id: string; classification: string; files: number; loc: number }[];
|
||
zoneEdges: ZoneEdge[];
|
||
cycles: GraphCycle[];
|
||
};
|
||
|
||
export function fileGroup(file: string): string {
|
||
const match = /^src\/([^/]+)\/([^/]+)\//.exec(file);
|
||
if (match) return `${match[1]}/${match[2]}`;
|
||
return targetDagZone(file);
|
||
}
|
||
|
||
function countLines(source: string): number {
|
||
let lines = 1;
|
||
for (let index = 0; index < source.length; index++) {
|
||
if (source[index] === '\n') lines++;
|
||
}
|
||
return lines;
|
||
}
|
||
|
||
function edgeKind(edge: ResolvedImportEdge): EdgeKind {
|
||
if (edge.dynamic) return 'dynamic';
|
||
if (edge.typeOnly) return 'type';
|
||
return 'value';
|
||
}
|
||
|
||
/**
|
||
* Deduplicate parsed import edges down to one edge per (from, to) pair, keeping the
|
||
* strongest kind. A file that imports both a type and a value from the same module has one
|
||
* dependency on it, and the value import is what constrains layering and cold-start.
|
||
*/
|
||
export function collapseEdges(edges: readonly ResolvedImportEdge[]): GraphEdge[] {
|
||
const strength: Record<EdgeKind, number> = { type: 0, dynamic: 1, value: 2 };
|
||
const byPair = new Map<string, GraphEdge>();
|
||
for (const edge of edges) {
|
||
if (edge.file === edge.target) continue;
|
||
const key = `${edge.file} |