Skip to content

Commit 4a7c2b7

Browse files
committed
chore(gates): capture dynamic-import bindings in R74 so symbol drift cannot hide
1 parent 5529929 commit 4a7c2b7

5 files changed

Lines changed: 142 additions & 8 deletions

File tree

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,49 @@ test('R74 rejects new symbols on a classified edge', () => {
8080
assert.match(found[0]!.message, /ensureLocalPlatformDeviceReady, extraReadiness/);
8181
});
8282

83-
test('R74 matches a dynamic import by target with empty symbols', () => {
83+
test('R74 matches a destructured dynamic import by target with the recorded bindings', () => {
8484
const sources = {
8585
'src/platform-runtime-operation-host.ts':
8686
'export async function recoverLegacyAppLogMarkersAfterDaemonLock() { return {}; }\n',
8787
'src/daemon/server/daemon-runtime.ts':
88-
"const mod = await import('../../platform-runtime-operation-host.ts');\nvoid mod;\n",
88+
"const { recoverLegacyAppLogMarkersAfterDaemonLock } = await import('../../platform-runtime-operation-host.ts');\n" +
89+
'void recoverLegacyAppLogMarkersAfterDaemonLock;\n',
8990
};
9091
assert.deepEqual(edgeViolations(sources, 'src/daemon/server/daemon-runtime.ts'), []);
9192
});
9293

94+
test('R74 rejects an expanded destructured dynamic import on a classified edge', () => {
95+
const sources = {
96+
'src/platform-runtime-operation-host.ts':
97+
'export async function recoverLegacyAppLogMarkersAfterDaemonLock() { return {}; }\n' +
98+
'export async function extraLegacyMarkerSweep() { return {}; }\n',
99+
'src/daemon/server/daemon-runtime.ts':
100+
"const { recoverLegacyAppLogMarkersAfterDaemonLock, extraLegacyMarkerSweep } = await import('../../platform-runtime-operation-host.ts');\n" +
101+
'void [recoverLegacyAppLogMarkersAfterDaemonLock, extraLegacyMarkerSweep];\n',
102+
};
103+
const found = edgeViolations(sources, 'src/daemon/server/daemon-runtime.ts');
104+
assert.equal(found.length, 1);
105+
assert.equal(found[0]!.rule, DAEMON_PLATFORM_RUNTIME_RULE);
106+
assert.match(found[0]!.message, /classified symbols drifted/);
107+
assert.match(
108+
found[0]!.message,
109+
/extraLegacyMarkerSweep, recoverLegacyAppLogMarkersAfterDaemonLock/,
110+
);
111+
});
112+
113+
test('R74 rejects a namespace-form dynamic import that hides the recorded bindings', () => {
114+
const sources = {
115+
'src/platform-runtime-operation-host.ts':
116+
'export async function recoverLegacyAppLogMarkersAfterDaemonLock() { return {}; }\n',
117+
'src/daemon/server/daemon-runtime.ts':
118+
"const mod = await import('../../platform-runtime-operation-host.ts');\nvoid mod;\n",
119+
};
120+
const found = edgeViolations(sources, 'src/daemon/server/daemon-runtime.ts');
121+
assert.equal(found.length, 1);
122+
assert.equal(found[0]!.rule, DAEMON_PLATFORM_RUNTIME_RULE);
123+
assert.match(found[0]!.message, /classified symbols drifted/);
124+
});
125+
93126
test('R74 treats the import and re-export of one classified pair as one entry', () => {
94127
const sources = {
95128
'src/platform-runtime-open-target.ts':

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ export type DaemonPlatformRuntimeClassification =
1919
export type DaemonPlatformRuntimeEdge = Readonly<{
2020
file: string;
2121
target: string;
22-
/** Exact named symbols across every edge of the pair; empty for dynamic/side-effect imports. */
22+
/**
23+
* Exact named symbols across every edge of the pair; empty for side-effect imports and dynamic
24+
* imports that do not destructure named bindings (a destructured dynamic import records its
25+
* bindings, so widening the destructure is a drift, not a silent expansion).
26+
*/
2327
symbols: readonly string[];
2428
classification: DaemonPlatformRuntimeClassification;
2529
rationale: string;
@@ -95,7 +99,7 @@ export const DAEMON_PLATFORM_RUNTIME_EDGES: readonly DaemonPlatformRuntimeEdge[]
9599
{
96100
file: 'src/daemon/server/daemon-runtime.ts',
97101
target: 'src/platform-runtime-operation-host.ts',
98-
symbols: [],
102+
symbols: ['recoverLegacyAppLogMarkersAfterDaemonLock'],
99103
classification: 'leaked-platform-mechanics',
100104
rationale:
101105
'daemon startup names app-log legacy marker recovery, a platform process mechanic; ' +

‎scripts/layering/layering-ast.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,76 @@ export function visitAst(node: unknown, visitor: (node: Record<string, unknown>)
4242
visitor(record);
4343
for (const child of Object.values(record)) visitAst(child, visitor);
4444
}
45+
46+
const IMPORT_WRAPPER_TYPES = new Set([
47+
'AwaitExpression',
48+
'ParenthesizedExpression',
49+
'TSAsExpression',
50+
'TSSatisfiesExpression',
51+
'TSNonNullExpression',
52+
]);
53+
54+
function unwrapImportInit(node: unknown): Record<string, unknown> | null {
55+
let current = node;
56+
while (current !== null && typeof current === 'object') {
57+
const record = current as Record<string, unknown>;
58+
if (typeof record.type !== 'string' || !IMPORT_WRAPPER_TYPES.has(record.type)) {
59+
return record.type === 'ImportExpression' ? record : null;
60+
}
61+
current =
62+
record.type === 'AwaitExpression' || record.type === 'ParenthesizedExpression'
63+
? record.argument
64+
: record.expression;
65+
}
66+
return null;
67+
}
68+
69+
/**
70+
* Named bindings captured by destructuring a dynamic import
71+
* (`const { a, b: local } = await import('...')` captures `a` and `b`), keyed by the import
72+
* expression's source offset. Namespace-form and bare dynamic imports capture nothing.
73+
*/
74+
export function destructuredDynamicImportBindings(
75+
program: unknown,
76+
): ReadonlyMap<number, readonly string[]> {
77+
const bindings = new Map<number, readonly string[]>();
78+
const visit = (node: unknown): void => {
79+
if (node === null || typeof node !== 'object') return;
80+
if (Array.isArray(node)) {
81+
for (const child of node) visit(child);
82+
return;
83+
}
84+
const record = node as Record<string, unknown>;
85+
if (record.type === 'VariableDeclarator') {
86+
const init = unwrapImportInit(record.init);
87+
const id = record.id;
88+
if (
89+
init !== null &&
90+
typeof init.start === 'number' &&
91+
id !== null &&
92+
typeof id === 'object' &&
93+
(id as Record<string, unknown>).type === 'ObjectPattern'
94+
) {
95+
const names: string[] = [];
96+
const properties = (id as Record<string, unknown>).properties;
97+
if (Array.isArray(properties)) {
98+
for (const property of properties) {
99+
if (
100+
property !== null &&
101+
typeof property === 'object' &&
102+
(property as Record<string, unknown>).type === 'Property' &&
103+
(property as Record<string, unknown>).computed !== true
104+
) {
105+
const name = propertyName((property as Record<string, unknown>).key);
106+
if (name) names.push(name);
107+
}
108+
}
109+
}
110+
bindings.set(init.start, names);
111+
}
112+
}
113+
for (const child of Object.values(record)) visit(child);
114+
};
115+
visit(program);
116+
return bindings;
117+
}

‎scripts/layering/model.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,25 @@ test('parseImports resolves constant-template dynamic imports', () => {
6868
]);
6969
});
7070

71+
test('parseImports captures destructured named bindings of dynamic imports, keyed by export name', () => {
72+
const edges = parseImports(
73+
[
74+
"const { a, 'b': c } = await import('./dyn.ts');",
75+
"const mod = await import('./dyn.ts');",
76+
"const wrapped = (await import('./dyn.ts')) as Mod;",
77+
].join('\n'),
78+
);
79+
80+
assert.deepEqual(
81+
edges.map(({ spec, symbols }) => ({ spec, symbols })),
82+
[
83+
{ spec: './dyn.ts', symbols: ['a', 'b'] },
84+
{ spec: './dyn.ts', symbols: [] },
85+
{ spec: './dyn.ts', symbols: [] },
86+
],
87+
);
88+
});
89+
7190
test('parseImports retains named source symbols without changing edge-kind detection', () => {
7291
const edges = parseImports(
7392
[

‎scripts/layering/model.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import path from 'node:path';
22
import { PLATFORMS } from '@agent-device/kernel/device';
33
import { parseSync } from 'oxc-parser';
4-
import { visitAst } from './layering-ast.ts';
4+
import { destructuredDynamicImportBindings, visitAst } from './layering-ast.ts';
55

66
export type ImportEdge = {
77
spec: string;
88
dynamic: boolean;
99
typeOnly: boolean;
1010
line: number;
11-
/** Named symbols imported from the target; empty for side-effect, namespace, and dynamic imports. */
11+
/**
12+
* Named symbols imported from the target; empty for side-effect and namespace imports, and for
13+
* dynamic imports that do not destructure named bindings.
14+
*/
1215
symbols: readonly string[];
1316
};
1417

@@ -151,16 +154,18 @@ function literalSpecifier(node: unknown): string | undefined {
151154
function scanDynamicImports(source: string): ImportEdge[] {
152155
const edges: ImportEdge[] = [];
153156
const parsed = parseSync('layering-imports.ts', source);
157+
const destructured = destructuredDynamicImportBindings(parsed.program);
154158
visitAst(parsed.program, (node) => {
155159
if (node.type !== 'ImportExpression') return;
156160
const spec = literalSpecifier(node.source);
157161
if (spec === undefined) return;
162+
const start = node.start as number | undefined;
158163
edges.push({
159164
spec,
160165
dynamic: true,
161166
typeOnly: false,
162-
line: sourceLine(source, node.start as number | undefined),
163-
symbols: [],
167+
line: sourceLine(source, start),
168+
symbols: typeof start === 'number' ? [...(destructured.get(start) ?? [])] : [],
164169
});
165170
});
166171
return edges;

0 commit comments

Comments
 (0)