@@ -117,6 +117,8 @@ export function rewriteFileLinks(input: RewriteFileLinksInput): RewriteFileLinks
117117 }
118118 }
119119
120+ const api = pathApi ( input . projectRoot ) ;
121+
120122 if ( input . scope === 'global' ) {
121123 const tokenFwd = normalizeSeparators ( punctStripped ) ;
122124 const tokenReferencesMesh =
@@ -127,10 +129,24 @@ export function rewriteFileLinks(input: RewriteFileLinksInput): RewriteFileLinks
127129 resolvedBeforeTranslate !== null &&
128130 isUnderAgentsMesh ( input . projectRoot , resolvedBeforeTranslate ) ;
129131 const translatedIsMesh = isUnderAgentsMesh ( input . projectRoot , translatedPath ) ;
130- // No actual translation occurred — leave non-mesh links alone to avoid cross-surface rebasing.
132+ // No actual translation occurred — leave the link unchanged only when the resolved path
133+ // lives in the same top-level surface as the source file (e.g. both in .claude/).
134+ // This prevents cross-surface rebasing during import without suppressing project-root
135+ // normalization when generating from .agentsmesh/ to a tool file.
131136 const noTranslation = resolvedBeforeTranslate === translatedPath ;
132137 if ( noTranslation && ! translatedIsMesh && ! resolvedIsMesh && ! tokenReferencesMesh ) {
133- return match ;
138+ const sourceFromRoot = normalizeSeparators (
139+ api . relative ( input . projectRoot , normalizeForProject ( input . projectRoot , input . sourceFile ) ) ,
140+ ) ;
141+ const sourceTop = sourceFromRoot . split ( '/' ) . filter ( Boolean ) [ 0 ] ?? '' ;
142+ const resolvedFromRoot =
143+ resolvedBeforeTranslate !== null
144+ ? normalizeSeparators ( api . relative ( input . projectRoot , resolvedBeforeTranslate ) )
145+ : '' ;
146+ const resolvedTop = resolvedFromRoot . split ( '/' ) . filter ( Boolean ) [ 0 ] ?? '' ;
147+ if ( sourceTop . length > 0 && sourceTop === resolvedTop ) {
148+ return match ;
149+ }
134150 }
135151 if (
136152 ! tokenCanUseGlobalStandard &&
@@ -141,8 +157,6 @@ export function rewriteFileLinks(input: RewriteFileLinksInput): RewriteFileLinks
141157 return match ;
142158 }
143159 }
144-
145- const api = pathApi ( input . projectRoot ) ;
146160 const destAbsolute = normalizeForProject ( input . projectRoot , input . destinationFile ) ;
147161 const targetAbsolute = normalizeForProject ( input . projectRoot , translatedPath ) ;
148162 const destFromRoot = normalizeSeparators ( api . relative ( input . projectRoot , destAbsolute ) ) ;
0 commit comments