@@ -41,6 +41,7 @@ const DIVERGENCE_CHAIN_FILES = [
4141
4242type ImportSite = {
4343 file : string ;
44+ kind : 'import' | 'dynamic-import' | 'named-reexport' | 'all-reexport' ;
4445 /** Repo-root-relative path the specifier resolves to, or `null` for an unresolved/external specifier. */
4546 target : string | null ;
4647 /** A named import binding at this site: its exported name and whether IT is type-only. */
@@ -92,6 +93,7 @@ function collectImportSites(
9293 } ) ) ;
9394 sites . push ( {
9495 file,
96+ kind : 'import' ,
9597 target,
9698 bindings,
9799 declarationTypeOnly : node . importKind === 'type' ,
@@ -101,6 +103,7 @@ function collectImportSites(
101103 if ( node . type === 'ImportExpression' ) {
102104 sites . push ( {
103105 file,
106+ kind : 'dynamic-import' ,
104107 target : importSourceTarget ( file , node . source ) ,
105108 bindings : [ ] ,
106109 declarationTypeOnly : false ,
@@ -120,6 +123,7 @@ function collectImportSites(
120123 : [ ] ;
121124 sites . push ( {
122125 file,
126+ kind : node . type === 'ExportNamedDeclaration' ? 'named-reexport' : 'all-reexport' ,
123127 target : importSourceTarget ( file , node . source ) ,
124128 bindings,
125129 declarationTypeOnly : node . exportKind === 'type' ,
@@ -129,9 +133,54 @@ function collectImportSites(
129133}
130134
131135function importSourceTarget ( file : string , source : unknown ) : string | null {
132- if ( ! source || typeof source !== 'object' ) return null ;
133- const value = ( source as { value ?: unknown } ) . value ;
134- return typeof value === 'string' ? resolveRelativeTarget ( file , value ) : null ;
136+ const value = constantSpecifier ( source ) ;
137+ return value === undefined ? null : resolveRelativeTarget ( file , value ) ;
138+ }
139+
140+ function objectRecord ( value : unknown ) : Record < string , unknown > | undefined {
141+ return value && typeof value === 'object' ? ( value as Record < string , unknown > ) : undefined ;
142+ }
143+
144+ function templateSpecifier ( node : Record < string , unknown > ) : string | undefined {
145+ const expressions = node . expressions ;
146+ const quasis = node . quasis ;
147+ if ( ! Array . isArray ( expressions ) || expressions . length !== 0 || ! Array . isArray ( quasis ) ) {
148+ return undefined ;
149+ }
150+ const quasi = objectRecord ( quasis [ 0 ] ) ;
151+ const value = quasi ? objectRecord ( quasi . value ) : undefined ;
152+ return value && typeof value . cooked === 'string' ? value . cooked : undefined ;
153+ }
154+
155+ function wrappedSpecifier ( node : Record < string , unknown > ) : string | undefined {
156+ return constantSpecifier ( node . expression ) ;
157+ }
158+
159+ function binarySpecifier ( node : Record < string , unknown > ) : string | undefined {
160+ if ( node . operator !== '+' ) return undefined ;
161+ const left = constantSpecifier ( node . left ) ;
162+ const right = constantSpecifier ( node . right ) ;
163+ return left !== undefined && right !== undefined ? left + right : undefined ;
164+ }
165+
166+ function literalSpecifier ( node : Record < string , unknown > ) : string | undefined {
167+ return typeof node . value === 'string' ? node . value : undefined ;
168+ }
169+
170+ const SPECIFIER_READERS : Record < string , ( node : Record < string , unknown > ) => string | undefined > = {
171+ Literal : literalSpecifier ,
172+ TemplateLiteral : templateSpecifier ,
173+ TSAsExpression : wrappedSpecifier ,
174+ TSTypeAssertion : wrappedSpecifier ,
175+ ChainExpression : wrappedSpecifier ,
176+ ParenthesizedExpression : wrappedSpecifier ,
177+ BinaryExpression : binarySpecifier ,
178+ } ;
179+
180+ function constantSpecifier ( value : unknown ) : string | undefined {
181+ const node = objectRecord ( value ) ;
182+ if ( ! node ) return undefined ;
183+ return SPECIFIER_READERS [ String ( node . type ) ] ?.( node ) ;
135184}
136185
137186function importsValueBinding ( site : ImportSite , name : string ) : boolean {
@@ -196,9 +245,7 @@ function coordinatorImportBindings(
196245 const namespace = new Set < string > ( ) ;
197246 for ( const node of body ) {
198247 if ( node . type !== 'ImportDeclaration' ) continue ;
199- const source = node . source as { value ?: unknown } ;
200- if ( typeof source . value !== 'string' ) continue ;
201- if ( resolveRelativeTarget ( file , source . value ) !== COORDINATOR_MODULE ) continue ;
248+ if ( importSourceTarget ( file , node . source ) !== COORDINATOR_MODULE ) continue ;
202249 if ( node . importKind === 'type' ) continue ;
203250 for ( const specifier of node . specifiers as readonly Record < string , unknown > [ ] ) {
204251 addCoordinatorImportBinding ( specifier , direct , namespace ) ;
@@ -268,17 +315,36 @@ function dynamicCoordinatorImportSite(
268315 file : string ,
269316 node : Record < string , unknown > ,
270317) : CoordinatorConstructionSite | undefined {
271- const sourceNode = node . source as { type ?: unknown ; value ?: unknown } | undefined ;
272- if (
273- sourceNode ?. type !== 'Literal' ||
274- typeof sourceNode . value !== 'string' ||
275- resolveRelativeTarget ( file , sourceNode . value ) !== COORDINATOR_MODULE
276- ) {
318+ if ( importSourceTarget ( file , node . source ) !== COORDINATOR_MODULE ) {
277319 return undefined ;
278320 }
279321 return { file, kind : 'dynamic-import' , start : Number ( node . start ) } ;
280322}
281323
324+ function coordinatorReexportSites (
325+ files : readonly string [ ] ,
326+ sourceOverrides : ReadonlyMap < string , string > = new Map ( ) ,
327+ ) : ImportSite [ ] {
328+ return files . flatMap ( ( file ) =>
329+ collectImportSites ( file , sourceOverrides . get ( file ) ) . filter (
330+ ( site ) =>
331+ ( site . kind === 'named-reexport' || site . kind === 'all-reexport' ) &&
332+ site . target === COORDINATOR_MODULE ,
333+ ) ,
334+ ) ;
335+ }
336+
337+ function unresolvedDynamicImportSites (
338+ files : readonly string [ ] ,
339+ sourceOverrides : ReadonlyMap < string , string > = new Map ( ) ,
340+ ) : ImportSite [ ] {
341+ return files . flatMap ( ( file ) =>
342+ collectImportSites ( file , sourceOverrides . get ( file ) ) . filter (
343+ ( site ) => site . kind === 'dynamic-import' && site . target === null ,
344+ ) ,
345+ ) ;
346+ }
347+
282348const PRODUCTION_FILES = listProductionSourceFiles ( ) ;
283349
284350test ( 'createReplayCoordinator has exactly one production call site' , ( ) => {
@@ -292,6 +358,31 @@ test('createReplayCoordinator has exactly one production call site', () => {
292358 sites . map ( ( { file, kind } ) => `${ file } (${ kind } )` ) . join ( ', ' ) || '(none)'
293359 } `,
294360 ) ;
361+ assert . deepEqual (
362+ coordinatorReexportSites ( PRODUCTION_FILES ) ,
363+ [ ] ,
364+ 'createReplayCoordinator must not be re-exported from a production module' ,
365+ ) ;
366+ } ) ;
367+
368+ test ( 'coordinator ownership scanning catches re-exports and unresolved dynamic imports' , ( ) => {
369+ const probe = 'src/daemon/replay/internal/probe.ts' ;
370+ const source = `
371+ export { createReplayCoordinator } from '../../session-replay-coordinator.ts';
372+ export * from '../../session-replay-coordinator.ts';
373+ const target = '../../session-replay-coordinator.ts';
374+ void import(target);
375+ void import(
376+ \`../../session-replay-coordinator.ts\`
377+ );
378+ ` ;
379+ const overrides = new Map ( [ [ probe , source ] ] ) ;
380+ assert . equal ( coordinatorReexportSites ( [ probe ] , overrides ) . length , 2 ) ;
381+ assert . equal ( unresolvedDynamicImportSites ( [ probe ] , overrides ) . length , 1 ) ;
382+ assert . deepEqual (
383+ coordinatorConstructionSites ( probe , source ) . map ( ( { kind } ) => kind ) ,
384+ [ 'dynamic-import' ] ,
385+ ) ;
295386} ) ;
296387
297388test ( 'coordinator ownership scanning catches aliases, namespaces, and dynamic imports' , ( ) => {
@@ -365,6 +456,14 @@ test('the divergence-report chain never imports SessionStore', () => {
365456 }
366457} ) ;
367458
459+ test ( 'the divergence-report chain rejects unresolved dynamic imports' , ( ) => {
460+ assert . deepEqual (
461+ unresolvedDynamicImportSites ( DIVERGENCE_CHAIN_FILES ) ,
462+ [ ] ,
463+ 'protected replay imports must resolve their dynamic specifier or fail closed' ,
464+ ) ;
465+ } ) ;
466+
368467test ( 'SessionStore ownership scanning catches dynamic imports and re-exports' , ( ) => {
369468 const sites = collectImportSites (
370469 'src/daemon/replay/internal/probe.ts' ,
0 commit comments