@@ -217,6 +217,7 @@ export class ModelInstallService {
217217
218218 const actualSha = await this . computeSha256 ( verificationPath ) ;
219219 if ( actualSha !== artifact . expectedSha256 ) {
220+ this . cleanupCorruptedArtifact ( artifact ) ;
220221 throw new Error ( `Checksum mismatch for ${ artifact . fileName } ` ) ;
221222 }
222223
@@ -478,7 +479,12 @@ export class ModelInstallService {
478479 unlinkSync ( finalPath ) ;
479480 }
480481
481- const existingPartBytes = existsSync ( partPath ) ? statSync ( partPath ) . size : 0 ;
482+ let existingPartBytes = existsSync ( partPath ) ? statSync ( partPath ) . size : 0 ;
483+ if ( existingPartBytes >= artifact . expectedSizeBytes ) {
484+ this . safeDeleteFile ( partPath ) ;
485+ existingPartBytes = 0 ;
486+ }
487+
482488 const headers : Record < string , string > = { } ;
483489 if ( existingPartBytes > 0 ) {
484490 headers . Range = `bytes=${ existingPartBytes } -` ;
@@ -488,6 +494,10 @@ export class ModelInstallService {
488494 ...( Object . keys ( headers ) . length > 0 ? { headers } : { } ) ,
489495 signal,
490496 } ) ;
497+ if ( response . status === 416 ) {
498+ this . safeDeleteFile ( partPath ) ;
499+ throw new Error ( `Failed to resume download for ${ artifact . fileName } : HTTP 416` ) ;
500+ }
491501 if ( ! response . ok && response . status !== 206 ) {
492502 throw new Error ( `Failed to download ${ artifact . fileName } : HTTP ${ response . status } ` ) ;
493503 }
@@ -544,6 +554,23 @@ export class ModelInstallService {
544554 } ) ;
545555 }
546556
557+ private cleanupCorruptedArtifact ( artifact : DownloadArtifact ) : void {
558+ const partPath = this . getPartPath ( artifact . fileName ) ;
559+ this . safeDeleteFile ( partPath ) ;
560+ this . safeDeleteFile ( artifact . destinationPath ) ;
561+ }
562+
563+ private safeDeleteFile ( filePath : string ) : void {
564+ if ( ! existsSync ( filePath ) ) {
565+ return ;
566+ }
567+ try {
568+ unlinkSync ( filePath ) ;
569+ } catch ( error ) {
570+ this . logger . warn ( `Failed to delete file: ${ filePath } , ${ error } ` ) ;
571+ }
572+ }
573+
547574 private safeDeleteManagedFile ( filePath : string ) : void {
548575 if ( ! filePath . startsWith ( this . modelRootDir ) ) {
549576 return ;
0 commit comments