Skip to content

Commit b962458

Browse files
thymikeeclaude
andcommitted
refactor(layering): measure R83 against the merge-base and share one production predicate
R83's offender set is now a merge-base ratchet measured through mergeBaseRatchets (no hand-kept list to append to), wildcard exports are expanded against the tracked package files, and the consumer ban judges every export condition. isProductionSourceFile in tracked-sources owns production versus test support for the layering scans, R11's test-only import ban and R13. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4fc5d43 commit b962458

17 files changed

Lines changed: 460 additions & 400 deletions

‎scripts/__tests__/committed-source-tree.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,21 @@ function readCommittedBlobs(
157157

158158
/**
159159
* The same enumeration and blob read as `createCommittedSourceTree`, handed over as text: the
160-
* production sources and workspace manifests committed at `treeish`. A whole-tree measurement
160+
* production sources and workspace manifests committed at `treeish`, plus every committed path. A whole-tree measurement
161161
* (the layering ratchets) needs the corpus rather than a reader, and taking it from here is what
162162
* keeps its file set identical to the closure walker's.
163163
*/
164164
// fallow-ignore-next-line unused-export -- consumed by scripts/layering, outside fallow's scope
165165
export function readCommittedSources(
166166
repoRoot: string,
167167
treeish: string,
168-
): { sources: Map<string, string>; manifests: Map<string, string> } {
169-
const { sources, manifests } = committedSourceSet(listCommittedTree(repoRoot, treeish));
168+
): { sources: Map<string, string>; manifests: Map<string, string>; files: string[] } {
169+
const files = listCommittedTree(repoRoot, treeish);
170+
const { sources, manifests } = committedSourceSet(files);
170171
const blobs = readCommittedBlobs(repoRoot, treeish, [...sources, ...manifests]);
171172
const only = (files: readonly string[]) =>
172173
new Map(files.flatMap((file) => (blobs.has(file) ? [[file, blobs.get(file)!] as const] : [])));
173-
return { sources: only(sources), manifests: only(manifests) };
174+
return { sources: only(sources), manifests: only(manifests), files };
174175
}
175176

176177
/**

‎scripts/layering/check.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
// server (R81). The global spine
6262
// ranks all five layers at daemon-server's rank, so R5/R6/R80 are blind inside the zone.
6363
// - Over WORKSPACE PACKAGE EXPORTS: no package but test-kit publishes test support (fixtures,
64-
// fakes, mocks, harnesses, `__tests__/`, `./testing/*`); a shrink-only burn-down list holds
65-
// today's offenders, and production code may not import them or test-kit (R83).
64+
// fakes, mocks, harnesses, `__tests__/`, `./testing/*`); the offender set is a merge-base
65+
// ratchet, and production code may not import those exports or test-kit (R83).
6666
// - Over CONTRACTS PRODUCTION SOURCE: contracts owns vocabulary only — host, process, and timer
6767
// mechanics belong in capture-kit or an adapter (R18).
6868
// R6, R9, and the R10 R7 counts are ratchets with no written-down reference: each is the same
@@ -138,14 +138,17 @@ import { checkDaemonPlatformBoundary } from './daemon-platform-boundary.ts';
138138
import { checkDaemonPlatformRuntimeInventory } from './daemon-platform-runtime-inventory.ts';
139139
import { checkDaemonClientEntry } from './daemon-client-entry.ts';
140140
import { checkDaemonProcessReach } from './daemon-process-reach.ts';
141-
import { checkPackageExportSurface } from './package-export-surface.ts';
142-
import { TEST_ONLY_EXPORT_BURN_DOWN } from './package-export-surface-burn-down.ts';
141+
import {
142+
checkPackageExportSurface,
143+
packageExportSurfaceSummary,
144+
} from './package-export-surface.ts';
143145
import { checkDaemonLayers, DAEMON_ROOT } from './daemon-layers.ts';
144146
import { checkSessionAuthorityOverlay, handlerOwnedOverlay } from './session-authority-overlay.ts';
145147
import {
146148
DAEMON_SOURCE_ROOT,
147149
listTrackedPlatformZoneFiles,
148150
listTrackedProductionSources,
151+
listTrackedPackageFiles,
149152
listTrackedSrcUtilsFiles,
150153
listTrackedTypeScriptFiles,
151154
} from './tracked-sources.ts';
@@ -441,8 +444,9 @@ function report(
441444
`concrete platform imports in every executable and type-only form; ` +
442445
`no production daemon file reaches the root platform-runtime family or a hub that ` +
443446
`does (R76); the daemon process reaches no caller-side replay source module over ` +
444-
`static or dynamic edges (R82); package exports publish no new test support, with ` +
445-
`${TEST_ONLY_EXPORT_BURN_DOWN.length} burn-down entries left (R83); and the handler-owned SessionState/SessionStore ` +
447+
`static or dynamic edges (R82); ` +
448+
`${packageExportSurfaceSummary(ratchets.testOnlyExports, reference.testOnlyExports)}; and ` +
449+
`the handler-owned SessionState/SessionStore ` +
446450
`authority overlay holds at or under the merge-base (R75, ` +
447451
`${handlerOwnedShapeFiles} shape / ${handlerOwnedAuthorityFiles} authority files); and ` +
448452
`every production daemon file lives under a layer folder with no upward static edges ` +
@@ -603,7 +607,8 @@ export const LAYERING_RULES: Readonly<Record<LayeringRuleId, LayeringRule>> = {
603607
context.sourceFiles.filter((file) => file.startsWith(DAEMON_SOURCE_ROOT)),
604608
context.edges,
605609
),
606-
'package-export-surface': () => checkPackageExportSurface(readTrackedPackageManifests(repoRoot)),
610+
'package-export-surface': (context) =>
611+
checkPackageExportSurface(context.ratchets.testOnlyExports, context.reference.testOnlyExports),
607612
};
608613

609614
export function main(): number {
@@ -616,7 +621,10 @@ export function main(): number {
616621
const parse = memoizedImportParser();
617622
const edges = resolveImportEdges(sources, workspaceSpecifierTargets(repoRoot), parse);
618623
// Measured once and threaded: the rules and the success line must report the same numbers.
619-
const ratchets = measureRatchets(sources, edges);
624+
const ratchets = measureRatchets(sources, edges, {
625+
manifests: readTrackedPackageManifests(repoRoot),
626+
packageFiles: listTrackedPackageFiles(repoRoot),
627+
});
620628
const reference = mergeBaseRatchets(repoRoot, parse);
621629
const context: LayeringContext = {
622630
sourceFiles,

‎scripts/layering/daemon-platform-boundary.test.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
findDaemonPlatformDependencies,
77
} from './daemon-platform-boundary.ts';
88

9-
const daemonFile = 'packages/daemon/src/terminal-boundary-fixture.ts';
9+
const daemonFile = 'packages/daemon/src/terminal-boundary-case.ts';
1010

1111
function dependencies(source: string, file = daemonFile) {
1212
return findDaemonPlatformDependencies([{ path: file, source }]);
@@ -92,7 +92,7 @@ test('R65 recognizes side-effect and aliased imports, including nested daemon pa
9292
].join('\n');
9393

9494
assert.deepEqual(
95-
dependencies(source, 'packages/daemon/src/sessions/handlers/terminal-boundary-fixture.ts').map(
95+
dependencies(source, 'packages/daemon/src/sessions/handlers/terminal-boundary-case.ts').map(
9696
(found) => ({
9797
kind: found.kind,
9898
line: found.line,
@@ -269,13 +269,10 @@ test('R65 rejects platform selection inside cleanup orchestrators', () => {
269269

270270
test('R65 ignores non-daemon and test-shaped records even when their syntax is red', () => {
271271
const source = "import { platform } from '../../../src/platforms/android/runtime.ts';";
272-
assert.deepEqual(dependencies(source, 'src/core/terminal-boundary-fixture.ts'), []);
272+
assert.deepEqual(dependencies(source, 'src/core/terminal-boundary-case.ts'), []);
273+
assert.deepEqual(dependencies(source, 'packages/daemon/src/terminal-boundary-case.test.ts'), []);
273274
assert.deepEqual(
274-
dependencies(source, 'packages/daemon/src/terminal-boundary-fixture.test.ts'),
275-
[],
276-
);
277-
assert.deepEqual(
278-
dependencies(source, 'packages/daemon/src/__tests__/terminal-boundary-fixture.ts'),
275+
dependencies(source, 'packages/daemon/src/__tests__/terminal-boundary-case.ts'),
279276
[],
280277
);
281278
});

‎scripts/layering/daemon-platform-runtime-inventory.test.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@ for (const [file, target, symbol] of [
5959
test('R76 rejects an edge with the pair and its line', () => {
6060
const sources = {
6161
'src/platform-runtime-android-tool-host.ts': 'export function createAndroidToolHost() {}\n',
62-
'packages/daemon/src/fixture.ts':
62+
'packages/daemon/src/subject.ts':
6363
"import { createAndroidToolHost } from '../../../src/platform-runtime-android-tool-host.ts';\n" +
6464
'void createAndroidToolHost;\n',
6565
};
66-
const found = edgeViolations(sources, 'packages/daemon/src/fixture.ts');
66+
const found = edgeViolations(sources, 'packages/daemon/src/subject.ts');
6767
assert.equal(found.length, 1);
6868
assert.equal(found[0]!.rule, DAEMON_PLATFORM_RUNTIME_RULE);
6969
assert.equal(found[0]!.line, 1);
7070
assert.match(found[0]!.message, /daemon coupling to root platform mechanics is banned/);
7171
assert.match(
7272
found[0]!.message,
73-
/packages\/daemon\/src\/fixture\.ts -> src\/platform-runtime-android-tool-host\.ts/,
73+
/packages\/daemon\/src\/subject\.ts -> src\/platform-runtime-android-tool-host\.ts/,
7474
);
7575
});
7676

@@ -127,10 +127,10 @@ test('R76 ignores test-shaped and non-daemon importers', () => {
127127
test('R76 ignores retired-zone targets, which R65 owns', () => {
128128
const sources = {
129129
'src/platforms/android.ts': 'export const legacy = 1;\n',
130-
'packages/daemon/src/fixture.ts':
130+
'packages/daemon/src/subject.ts':
131131
'import { legacy } from "../../../src/platforms/android.ts";\nvoid legacy;\n',
132132
};
133-
assert.deepEqual(edgeViolations(sources, 'packages/daemon/src/fixture.ts'), []);
133+
assert.deepEqual(edgeViolations(sources, 'packages/daemon/src/subject.ts'), []);
134134
});
135135

136136
test('the root composition family is src/platform-runtime.ts plus src/platform-runtime-*.ts only', () => {
@@ -150,13 +150,13 @@ test('R76 catches a daemon import of a root hub that reaches the platform family
150150
'src/hub-runtime.ts':
151151
"import { gateway } from './platform-runtime.ts';\nvoid gateway;\n" +
152152
'export function hubFact() {}\n',
153-
'packages/daemon/src/fixture.ts':
153+
'packages/daemon/src/subject.ts':
154154
"import { hubFact } from '../../../src/hub-runtime.ts';\nvoid hubFact;\n",
155155
};
156-
const found = edgeViolations(sources, 'packages/daemon/src/fixture.ts');
156+
const found = edgeViolations(sources, 'packages/daemon/src/subject.ts');
157157
assert.equal(found.length, 1);
158158
assert.equal(found[0]!.rule, DAEMON_PLATFORM_RUNTIME_RULE);
159-
assert.match(found[0]!.message, /packages\/daemon\/src\/fixture\.ts -> src\/hub-runtime\.ts/);
159+
assert.match(found[0]!.message, /packages\/daemon\/src\/subject\.ts -> src\/hub-runtime\.ts/);
160160
});
161161

162162
test('R76 does not turn an intra-daemon hop into a hub edge', () => {

‎scripts/layering/model.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ export const UNRANKED_ZONES: ReadonlySet<string> = new Set([
108108
'host-kit',
109109
'capture-kit',
110110
'provision-kit',
111-
'test-kit',
112111
...PLATFORMS.map((family) => `platform-${family}`),
113112
'provider-webdriver',
114113
'provider-limrun',

‎scripts/layering/package-boundaries.ts‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@
3131
import fs from 'node:fs';
3232
import path from 'node:path';
3333
import { parseImports, resolveWorkspaceSpecifier } from './model.ts';
34-
import { PACKAGE_EXPORT_SURFACE_RULE, testOnlyImportMessage } from './package-export-surface.ts';
34+
import {
35+
PACKAGE_EXPORT_SURFACE_RULE,
36+
readExportManifests,
37+
resolveAllConditionTargets,
38+
testOnlyImportMessage,
39+
} from './package-export-surface.ts';
3540
import { listTrackedPackageManifests, listTrackedProductionSources } from './tracked-sources.ts';
3641

3742
export type PackageBoundaryViolation = {
@@ -47,6 +52,8 @@ export type WorkspacePackage = {
4752
name: string;
4853
/** Full import specifier -> repo-relative source target, from `exports`. */
4954
exportTargets: ReadonlyMap<string, string>;
55+
/** Export key -> every condition target (`types`, `import`, `default`, ...), repo-relative. */
56+
exportConditionTargets?: ReadonlyMap<string, readonly string[]>;
5057
/** Declared `workspace:*` dependencies on sibling internal packages. */
5158
workspaceDependencies: ReadonlySet<string>;
5259
/** Where siblings are declared: a published package bundles them, so it lists them as dev-only. */
@@ -140,6 +147,20 @@ export function workspacePackagesFromManifests(
140147
path.posix.join('packages', entry, path.posix.normalize(targetFile)),
141148
);
142149
}
150+
const conditionTargets = new Map(
151+
readExportManifests(new Map([[manifestFile, manifests.get(manifestFile)!]])).flatMap(
152+
(declared) =>
153+
declared.exports.map(
154+
({ key, targets }) =>
155+
[
156+
path.posix.join(manifest.name!, key),
157+
targets.map((target) =>
158+
path.posix.join('packages', entry, path.posix.normalize(target)),
159+
),
160+
] as const,
161+
),
162+
),
163+
);
143164
const workspaceDependencyField = manifest.private === true ? 'dependencies' : 'devDependencies';
144165
const workspaceDependencies = new Set(
145166
Object.entries(manifest[workspaceDependencyField] ?? {})
@@ -155,6 +176,7 @@ export function workspacePackagesFromManifests(
155176
dir: `packages/${entry}`,
156177
name: manifest.name,
157178
exportTargets,
179+
exportConditionTargets: conditionTargets,
158180
workspaceDependencies,
159181
workspaceDependencyField,
160182
externalDependencies,
@@ -186,14 +208,16 @@ function checkTestOnlyImportSite(
186208
site: SpecifierSite,
187209
target: WorkspacePackage,
188210
): PackageBoundaryViolation | undefined {
189-
const resolvedTarget = resolveWorkspaceSpecifier(site.specifier, target.exportTargets);
211+
const resolvedTargets = resolveAllConditionTargets(
212+
site.specifier,
213+
target.exportConditionTargets ??
214+
new Map([...target.exportTargets].map(([key, file]) => [key, [file]])),
215+
);
190216
const message = testOnlyImportMessage(
191217
site.file,
192218
site.specifier,
193219
target.name,
194-
resolvedTarget === undefined
195-
? undefined
196-
: { key: `.${site.specifier.slice(target.name.length)}`, targets: [resolvedTarget] },
220+
resolvedTargets.length > 0 ? resolvedTargets : undefined,
197221
);
198222
if (message === undefined) return undefined;
199223
return { rule: PACKAGE_EXPORT_SURFACE_RULE, file: site.file, line: site.line, message };

0 commit comments

Comments
 (0)