Skip to content

Commit b7621b2

Browse files
committed
refactor(depgraph): consolidate viewer data
1 parent 9881e9d commit b7621b2

20 files changed

Lines changed: 193 additions & 331 deletions

‎.gitignore‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,3 @@ docs/evidence/ios-simulator-ax-bridge-*.json.gz
5959

6060
# Release notes are generated from PR titles; a shared changelog conflicts on every PR.
6161
CHANGELOG.md
62-

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@
162162
"check:layering": "node --experimental-strip-types scripts/node-test-tmpdir.ts --experimental-strip-types --test scripts/layering/*.test.ts && node --experimental-strip-types scripts/layering/check.ts",
163163
"check:di-seams": "node --experimental-strip-types scripts/node-test-tmpdir.ts --experimental-strip-types --test scripts/di-seams/*.test.ts && node --experimental-strip-types scripts/di-seams/check.ts",
164164
"depgraph": "node --experimental-strip-types scripts/depgraph/build.ts",
165-
"depgraph:test": "node --experimental-strip-types scripts/node-test-tmpdir.ts --experimental-strip-types --test scripts/depgraph/model.test.ts scripts/depgraph/import-graph.test.ts scripts/depgraph/affected.test.ts scripts/depgraph/structure-summary.test.ts",
165+
"depgraph:test": "node --experimental-strip-types scripts/node-test-tmpdir.ts --experimental-strip-types --test scripts/depgraph/model.test.ts scripts/depgraph/entries.test.ts scripts/depgraph/import-graph.test.ts scripts/depgraph/affected.test.ts scripts/depgraph/structure-summary.test.ts tools/depgraph-view/*.test.mjs",
166166
"check:production-exports": "fallow dead-code --config fallow-production-exports.json --production --unused-exports --fail-on-issues",
167167
"check:bundle-owner-files": "node --experimental-strip-types scripts/check-bundle-owner-files.ts",
168168
"check:package": "node --experimental-strip-types scripts/check-package.ts",

‎scripts/build-entries.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export const buildEntries = {
2+
index: 'src/sdk/index.ts',
3+
io: 'src/sdk/io.ts',
4+
artifacts: 'src/sdk/artifacts.ts',
5+
batch: 'src/sdk/batch.ts',
6+
metro: 'src/sdk/metro.ts',
7+
'remote-config': 'src/sdk/remote-config.ts',
8+
'install-source': 'src/sdk/install-source.ts',
9+
'android-adb': 'src/sdk/android-adb.ts',
10+
limrun: 'src/sdk/limrun.ts',
11+
plugins: 'src/sdk/plugins.ts',
12+
'plugins/webdriver': 'src/sdk/plugin-webdriver.ts',
13+
contracts: 'src/sdk/contracts.ts',
14+
selectors: 'src/sdk/selectors.ts',
15+
finders: 'src/sdk/finders.ts',
16+
'ai-sdk': 'src/ai-sdk/index.ts',
17+
'internal/bin': 'src/bin.ts',
18+
'internal/companion-tunnel': 'packages/react-native/src/companion/companion-tunnel.ts',
19+
'internal/daemon': 'src/daemon.ts',
20+
'internal/run-script-http-child': 'packages/maestro/src/daemon-port/run-script-http-child.ts',
21+
'internal/png-worker': 'packages/capture-kit/src/png-worker.ts',
22+
'internal/update-check-entry': 'src/cli/update-check-entry.ts',
23+
} as const;

‎scripts/depgraph/README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ pnpm depgraph:test
77
```
88

99
Emits the dependency graph of every production file under `src/` (tests excluded) as JSON,
10-
plus a short summary of what the layering gate does not enforce. There is no renderer: the
11-
productive artifact is the JSON, queried directly.
10+
plus a short summary of what the layering gate does not enforce. The interactive viewer is at
11+
`/dependency-graph/` on the website. Run `pnpm --dir website build` and `pnpm --dir website preview`
12+
to view the generated graph locally.
1213

1314
## Blast radius of one file
1415

‎scripts/depgraph/build.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { pathToFileURL } from 'node:url';
1818
import { zoneRank } from '../layering/model.ts';
1919
import { runAffected } from './affected-run.ts';
2020
import { loadGraph } from './load.ts';
21+
import { graphEntries, type EntryManifest } from './entries.ts';
2122
import { computeLevels, markTransitivelyReachableEdges, type GraphData } from './model.ts';
2223
import {
2324
computeCohesionSummary,
@@ -67,6 +68,8 @@ type Payload = {
6768
* Report-only structural summaries (scripts/depgraph/structure-summary.ts): no layering or
6869
* check-affected gate reads these fields today.
6970
*/
71+
entries: EntryManifest;
72+
entrySummaries: Record<string, DominatorSummary>;
7073
dominatorSummary: DominatorSummary;
7174
zoneSccSummary: ZoneSccSummary;
7275
cohesionSummary: CohesionSummary;
@@ -108,11 +111,31 @@ function hasValueCycle(graph: Pick<GraphData, 'cycles'>): boolean {
108111

109112
function buildPayload(dominatorEntry: string): Payload {
110113
const graph = loadGraph(repoRoot);
114+
const dominatorSummary = computeDominatorSummary(dominatorEntry, graph);
111115
if (!hasValueCycle(graph)) markTransitivelyReachableEdges(graph.edges);
112116
const levels = computeLevels(graph.nodes, graph.edges);
113117

114118
const zoneIndex = new Map(graph.zones.map((zone, index) => [zone.id, index]));
115119
const nodeIndex = new Map(graph.nodes.map((node, index) => [node.id, index]));
120+
const entries = graphEntries(repoRoot, new Set(nodeIndex.keys()));
121+
const hot = graph.nodes
122+
.filter((n) => n.fanIn > 0)
123+
.sort((a, b) => b.fanIn + b.fanOut - (a.fanIn + a.fanOut))
124+
.slice(0, 300);
125+
const selected = new Set([
126+
...Object.values(entries)
127+
.flat()
128+
.map((e) => e.id),
129+
...hot.map((n) => n.id.replace(/^src\//, '')),
130+
dominatorEntry.replace(/^src\//, ''),
131+
]);
132+
const originalIds = new Map(graph.nodes.map((n) => [n.id.replace(/^src\//, ''), n.id]));
133+
const entrySummaries = Object.fromEntries(
134+
[...selected].map((id) => {
135+
const summary = computeDominatorSummary(originalIds.get(id)!, graph);
136+
return [id, { ...summary, bottlenecks: summary.bottlenecks.slice(0, 8) }];
137+
}),
138+
);
116139

117140
return {
118141
generated: { commit: headCommit(), files: graph.nodes.length, edges: graph.edges.length },
@@ -140,7 +163,9 @@ function buildPayload(dominatorEntry: string): Payload {
140163
path: cycle.path.map((file) => nodeIndex.get(file)!),
141164
})),
142165
typeInversions: graph.typeInversions,
143-
dominatorSummary: computeDominatorSummary(dominatorEntry, graph),
166+
entries,
167+
entrySummaries,
168+
dominatorSummary,
144169
zoneSccSummary: computeZoneSccSummary(graph),
145170
cohesionSummary: computeCohesionSummary(graph),
146171
};

‎scripts/depgraph/entries.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import assert from 'node:assert/strict';
2+
import { execFileSync } from 'node:child_process';
3+
import fs from 'node:fs';
4+
import os from 'node:os';
5+
import path from 'node:path';
6+
import { test } from 'node:test';
7+
import { graphEntries } from './entries.ts';
8+
9+
test('maps declared root and workspace exports to graph source files', () => {
10+
const repo = fs.mkdtempSync(path.join(os.tmpdir(), 'depgraph-entries-'));
11+
fs.mkdirSync(path.join(repo, 'packages/example'), { recursive: true });
12+
fs.writeFileSync(
13+
path.join(repo, 'package.json'),
14+
JSON.stringify({
15+
exports: {
16+
'.': { default: './dist/src/index.js' },
17+
'./io': { require: './dist/src/io.js' },
18+
},
19+
}),
20+
);
21+
fs.writeFileSync(
22+
path.join(repo, 'packages/example/package.json'),
23+
JSON.stringify({
24+
name: '@agent-device/example',
25+
exports: { '.': { require: './src/index.js' }, './alias': './src/index.ts' },
26+
}),
27+
);
28+
execFileSync('git', ['init', '-q'], { cwd: repo });
29+
execFileSync('git', ['add', 'package.json', 'packages/example/package.json'], { cwd: repo });
30+
const entries = graphEntries(
31+
repo,
32+
new Set(['src/sdk/index.ts', 'src/sdk/io.ts', 'packages/example/src/index.ts']),
33+
);
34+
assert.deepEqual(entries['src (agent-device)'], [
35+
{ id: 'sdk/index.ts', subpath: '.' },
36+
{ id: 'sdk/io.ts', subpath: './io' },
37+
]);
38+
assert.deepEqual(entries['packages/example'], [
39+
{ id: 'packages/example/src/index.ts', subpath: '.' },
40+
{ id: 'packages/example/src/index.ts', subpath: './alias' },
41+
]);
42+
});

‎scripts/depgraph/entries.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { buildEntries } from '../build-entries.ts';
4+
import { readWorkspacePackages } from '../layering/package-boundaries.ts';
5+
6+
export type EntryManifest = Record<string, { id: string; subpath: string }[]>;
7+
8+
export function graphEntries(repoRoot: string, ids: ReadonlySet<string>): EntryManifest {
9+
const entries: EntryManifest = {};
10+
const add = (pkg: string, id: string, subpath: string) => {
11+
const source = ids.has(id) ? id : id.replace(/\.js$/, '.ts');
12+
if (ids.has(source)) (entries[pkg] ??= []).push({ id: source.replace(/^src\//, ''), subpath });
13+
};
14+
const root = JSON.parse(fs.readFileSync(path.join(repoRoot, 'package.json'), 'utf8')) as {
15+
exports:
16+
| string
17+
| Record<string, { default?: string; import?: string; require?: string } | string>;
18+
};
19+
const rootExports = typeof root.exports === 'string' ? { '.': root.exports } : root.exports;
20+
for (const [subpath, value] of Object.entries(rootExports)) {
21+
const target =
22+
typeof value === 'string' ? value : (value.default ?? value.import ?? value.require);
23+
if (!target) continue;
24+
const name = target.replace(/^\.\/dist\/src\//, '').replace(/\.js$/, '');
25+
const source = (buildEntries as Record<string, string>)[name];
26+
if (source) add('src (agent-device)', source, subpath);
27+
}
28+
for (const pkg of readWorkspacePackages(repoRoot)) {
29+
for (const [specifier, target] of pkg.exportTargets) {
30+
add(pkg.dir, target, specifier === pkg.name ? '.' : `.${specifier.slice(pkg.name.length)}`);
31+
}
32+
}
33+
return entries;
34+
}

‎scripts/depgraph/model.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,8 @@ test('build.ts writes the default path and a summary consistent with the JSON',
531531
edgeAuthorities: string[][];
532532
authorityCounts: Record<string, number>;
533533
typeInversions: Record<string, number>;
534+
entries: Record<string, { id: string; subpath: string }[]>;
535+
entrySummaries: Record<string, { reachableFiles: number; reachableLoc: number }>;
534536
dominatorSummary: { entry: string; reachableFiles: number; totalFiles: number };
535537
zoneSccSummary: { zones: number; components: { zones: string[]; size: number }[] };
536538
cohesionSummary: {
@@ -549,6 +551,8 @@ test('build.ts writes the default path and a summary consistent with the JSON',
549551
'edges',
550552
'cycles',
551553
'typeInversions',
554+
'entries',
555+
'entrySummaries',
552556
'dominatorSummary',
553557
'zoneSccSummary',
554558
'cohesionSummary',
@@ -568,6 +572,15 @@ test('build.ts writes the default path and a summary consistent with the JSON',
568572

569573
// Report-only fields (#3283): no gate reads these, so this pins only the wire shape.
570574
assert.equal(payload.dominatorSummary.entry, 'src/daemon.ts');
575+
assert.ok(
576+
payload.entries['src (agent-device)'].some(
577+
(entry) => entry.subpath === '.' && entry.id === 'sdk/index.ts',
578+
),
579+
);
580+
assert.equal(
581+
payload.entrySummaries['daemon.ts'].reachableFiles,
582+
payload.dominatorSummary.reachableFiles,
583+
);
571584
assert.ok(payload.dominatorSummary.reachableFiles > 0);
572585
assert.ok(payload.dominatorSummary.reachableFiles <= payload.dominatorSummary.totalFiles);
573586
assert.ok(payload.zoneSccSummary.zones > 0);

‎scripts/layering/package-boundaries-dev-dependencies.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ function violations(owner: Record<string, unknown>, file: string) {
4141

4242
const DEV_ONLY = { private: true, devDependencies: { '@agent-device/helper': 'workspace:*' } };
4343

44+
test('a string export resolves the package root', () => {
45+
const packages = workspacePackagesFromManifests(manifests({ exports: './src/index.ts' }));
46+
const owner = packages.find(({ name }) => name === '@agent-device/owner')!;
47+
assert.equal(owner.exportTargets.get('@agent-device/owner'), 'packages/owner/src/index.ts');
48+
});
49+
50+
test('a require-only export resolves the package root', () => {
51+
const packages = workspacePackagesFromManifests(
52+
manifests({ exports: { '.': { require: './src/index.ts' } } }),
53+
);
54+
const owner = packages.find(({ name }) => name === '@agent-device/owner')!;
55+
assert.equal(owner.exportTargets.get('@agent-device/owner'), 'packages/owner/src/index.ts');
56+
});
57+
4458
test('a private package reads its devDependencies as test-only siblings', () => {
4559
const [, owner] = workspacePackagesFromManifests(manifests(DEV_ONLY));
4660
assert.deepEqual([...owner!.workspaceDependencies], []);

‎scripts/layering/package-boundaries.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,19 @@ export function workspacePackagesFromManifests(
140140
const manifest = JSON.parse(manifests.get(manifestFile)!) as {
141141
name?: string;
142142
private?: boolean;
143-
exports?: Record<string, { default?: string; import?: string } | string>;
143+
exports?:
144+
| string
145+
| Record<string, { default?: string; import?: string; require?: string } | string>;
144146
dependencies?: Record<string, string>;
145147
devDependencies?: Record<string, string>;
146148
};
147149
if (!manifest.name) continue;
148150
const exportTargets = new Map<string, string>();
149-
for (const [subpath, target] of Object.entries(manifest.exports ?? {})) {
150-
const targetFile = typeof target === 'string' ? target : (target.default ?? target.import);
151+
const exports =
152+
typeof manifest.exports === 'string' ? { '.': manifest.exports } : (manifest.exports ?? {});
153+
for (const [subpath, target] of Object.entries(exports)) {
154+
const targetFile =
155+
typeof target === 'string' ? target : (target.default ?? target.import ?? target.require);
151156
if (!targetFile) continue;
152157
exportTargets.set(
153158
path.posix.join(manifest.name, subpath),

0 commit comments

Comments
 (0)