@@ -208,19 +208,36 @@ export async function migrateLegacyRootMemoryFile(
208208 mergedLegacy : false ,
209209 } ;
210210 }
211+ // Preflight: read both memory files before archiving so oversized files
212+ // leave the legacy file in place rather than failing after archival.
213+ let canonicalText : string ;
214+ let legacyText : string ;
215+ try {
216+ [ canonicalText , legacyText ] = await Promise . all ( [
217+ readRegularFile ( {
218+ filePath : detection . canonicalPath ,
219+ maxBytes : WORKSPACE_DOCTOR_MAX_BYTES ,
220+ } ) . then ( ( r ) => r . buffer . toString ( "utf-8" ) ) ,
221+ readRegularFile ( {
222+ filePath : detection . legacyPath ,
223+ maxBytes : WORKSPACE_DOCTOR_MAX_BYTES ,
224+ } ) . then ( ( r ) => r . buffer . toString ( "utf-8" ) ) ,
225+ ] ) ;
226+ } catch {
227+ // File exceeds size cap; skip migration and keep source intact.
228+ return {
229+ changed : false ,
230+ canonicalPath : detection . canonicalPath ,
231+ legacyPath : detection . legacyPath ,
232+ removedLegacy : false ,
233+ mergedLegacy : false ,
234+ } ;
235+ }
236+
211237 const archivedLegacyPath = await moveLegacyRootMemoryFileToArchive ( {
212238 workspaceDir : detection . workspaceDir ,
213239 legacyPath : detection . legacyPath ,
214240 } ) ;
215- const [ canonicalText , legacyText ] = await Promise . all ( [
216- readRegularFile ( {
217- filePath : detection . canonicalPath ,
218- maxBytes : WORKSPACE_DOCTOR_MAX_BYTES ,
219- } ) . then ( ( r ) => r . buffer . toString ( "utf-8" ) ) ,
220- readRegularFile ( { filePath : archivedLegacyPath , maxBytes : WORKSPACE_DOCTOR_MAX_BYTES } ) . then (
221- ( r ) => r . buffer . toString ( "utf-8" ) ,
222- ) ,
223- ] ) ;
224241 if ( canonicalText !== legacyText ) {
225242 const merged = `${ canonicalText . trimEnd ( ) } \n${ buildMergedLegacyRootMemorySection ( {
226243 legacyText,
0 commit comments