@@ -108,13 +108,20 @@ const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Service | C
108108 )
109109 const snapshotGitEnv = ( env ?: Record < string , string > ) => ( { ...env , LC_ALL : "C" , LANG : "C" } )
110110
111+ // A corrupt index can silently produce the git empty tree for a non-empty worktree.
112+ const isSnapshotEmptyTree = ( hash : string ) =>
113+ hash === "4b825dc642cb6eb9a060e54bf8d69288fbee4904" ||
114+ hash === "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321"
115+
111116 const git = Effect . fnUntraced ( function * (
112117 cmd : string [ ] ,
113118 opts ?: { cwd ?: string ; env ?: Record < string , string > ; stdin ?: string } ,
114119 ) {
115- const result = Effect . suspend ( ( ) =>
120+ const writesIndex = [ "add" , "rm" , "read-tree" ] . some ( ( subcommand ) => cmd . includes ( subcommand ) )
121+ // Deleting a corrupt index is only safe ahead of commands that rebuild state from the worktree.
122+ const healsIndex = writesIndex || [ "diff-files" , "ls-files" ] . some ( ( subcommand ) => cmd . includes ( subcommand ) )
123+ const execute = Effect . suspend ( ( ) =>
116124 Effect . gen ( function * ( ) {
117- const writesIndex = [ "add" , "rm" , "read-tree" ] . some ( ( subcommand ) => cmd . includes ( subcommand ) )
118125 if ( writesIndex && ( yield * exists ( indexLock ) ) ) {
119126 return {
120127 code : ChildProcessSpawner . ExitCode ( 128 ) ,
@@ -149,41 +156,52 @@ const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Service | C
149156 } ) ,
150157 )
151158
152- return yield * result . pipe (
153- Effect . flatMap ( ( result ) =>
154- isIndexLockContention ( result ) ? Effect . fail ( result ) : Effect . succeed ( result ) ,
155- ) ,
156- Effect . retry ( { schedule : indexLockRetry , while : isIndexLockContention } ) ,
157- Effect . catch ( ( result ) =>
158- Effect . gen ( function * ( ) {
159- if ( isIndexLockContention ( result ) ) {
160- const ageMs = yield * indexLockAge ( )
161- yield * Effect . logError ( "snapshot_index_lock_stuck" , {
162- indexLock,
163- ageMs,
164- remedy : "remove the lock manually only after confirming no git process owns it" ,
165- } )
166- yield * Effect . sync ( ( ) =>
167- process . stderr . write (
168- `snapshot_index_lock_stuck index_lock=${ indexLock } age_ms=${ ageMs ?? "unknown" } remedy=manual-confirm-and-remove\n` ,
169- ) ,
170- )
171- }
172- return result
173- } ) ,
174- ) ,
159+ const settle = ( effect : typeof execute ) =>
160+ effect . pipe (
161+ Effect . flatMap ( ( result ) =>
162+ isIndexLockContention ( result ) ? Effect . fail ( result ) : Effect . succeed ( result ) ,
163+ ) ,
164+ Effect . retry ( { schedule : indexLockRetry , while : isIndexLockContention } ) ,
165+ Effect . catch ( ( result ) =>
166+ Effect . gen ( function * ( ) {
167+ if ( isIndexLockContention ( result ) ) {
168+ const ageMs = yield * indexLockAge ( )
169+ yield * Effect . logError ( "snapshot_index_lock_stuck" , {
170+ indexLock,
171+ ageMs,
172+ remedy : "remove the lock manually only after confirming no git process owns it" ,
173+ } )
174+ yield * Effect . sync ( ( ) =>
175+ process . stderr . write (
176+ `snapshot_index_lock_stuck index_lock=${ indexLock } age_ms=${ ageMs ?? "unknown" } remedy=manual-confirm-and-remove\n` ,
177+ ) ,
178+ )
179+ }
180+ return result
181+ } ) ,
182+ ) ,
183+ )
184+
185+ return yield * settle ( execute ) . pipe (
175186 Effect . flatMap ( ( outcome ) => {
176187 const corrupted =
177188 outcome . code !== 0 &&
178- / i n d e x f i l e c o r r u p t | i n d e x f i l e s m a l l e r t h a n e x p e c t e d | b a d i n d e x f i l e s h a 1 s i g n a t u r e / . test (
189+ healsIndex &&
190+ / i n d e x f i l e c o r r u p t | i n d e x f i l e s m a l l e r t h a n e x p e c t e d | b a d i n d e x f i l e s h a 1 s i g n a t u r e | u n e x p e c t e d d i f f s t a t u s | u n k n o w n f i l e m o d e f o r .* i n i n d e x / . test (
179191 outcome . stderr ,
180192 )
181193 if ( ! corrupted ) return Effect . succeed ( outcome )
182194 return Effect . gen ( function * ( ) {
183- // The snapshot gitdir is derived state; deleting a corrupt index only drops staged paths, which the retry restages.
195+ const removed = yield * fs . remove ( path . join ( state . gitdir , "index" ) ) . pipe (
196+ Effect . as ( true ) ,
197+ Effect . catch ( ( ) => Effect . succeed ( false ) ) ,
198+ )
199+ if ( ! removed ) {
200+ yield * Effect . logWarning ( "snapshot_index_corruption_detected" , { gitdir : state . gitdir } )
201+ return yield * settle ( execute )
202+ }
184203 yield * Effect . logWarning ( "snapshot_index_corruption_recovered" , { gitdir : state . gitdir } )
185- yield * fs . remove ( path . join ( state . gitdir , "index" ) ) . pipe ( Effect . ignore )
186- return yield * result
204+ return yield * settle ( execute )
187205 } )
188206 } ) ,
189207 )
@@ -569,23 +587,49 @@ const layer: Layer.Layer<Service, never, FSUtil.Service | AppProcess.Service | C
569587 if ( ! ( yield * setup ( existed ) ) ) return
570588 return yield * withTransactionToken (
571589 Effect . gen ( function * ( ) {
572- if ( ! ( yield * add ( ) ) ) return
573- const result = yield * git ( args ( [ "write-tree" ] ) , { cwd : state . directory } )
574- if ( result . code !== 0 ) {
575- yield * Effect . logError ( "failed to write snapshot tree" , {
576- exitCode : result . code ,
577- stderr : result . stderr ,
578- } )
579- return
580- }
581- const hash = result . text . trim ( )
582- if ( ! / ^ (?: [ 0 - 9 a - f ] { 40 } | [ 0 - 9 a - f ] { 64 } ) $ / . test ( hash ) ) {
583- yield * Effect . logError ( "snapshot git returned an invalid tree hash" , {
584- hash,
585- stderr : result . stderr ,
586- } )
587- return
588- }
590+ const capture = Effect . gen ( function * ( ) {
591+ if ( ! ( yield * add ( ) ) ) return
592+ const result = yield * git ( args ( [ "write-tree" ] ) , { cwd : state . directory } )
593+ if ( result . code !== 0 ) {
594+ yield * Effect . logError ( "failed to write snapshot tree" , {
595+ exitCode : result . code ,
596+ stderr : result . stderr ,
597+ } )
598+ return
599+ }
600+ const hash = result . text . trim ( )
601+ if ( ! / ^ (?: [ 0 - 9 a - f ] { 40 } | [ 0 - 9 a - f ] { 64 } ) $ / . test ( hash ) ) {
602+ yield * Effect . logError ( "snapshot git returned an invalid tree hash" , {
603+ hash,
604+ stderr : result . stderr ,
605+ } )
606+ return
607+ }
608+ return hash
609+ } )
610+
611+ // A corrupt index can silently produce the empty tree; rebuild it and recapture once before accepting the result.
612+ const hash = yield * capture . pipe (
613+ Effect . flatMap ( ( captured ) => {
614+ if ( captured === undefined || ! isSnapshotEmptyTree ( captured ) ) return Effect . succeed ( captured )
615+ return Effect . gen ( function * ( ) {
616+ yield * Effect . logWarning ( "snapshot_empty_tree_detected" , {
617+ hash : captured ,
618+ gitdir : state . gitdir ,
619+ } )
620+ yield * fs . remove ( path . join ( state . gitdir , "index" ) ) . pipe ( Effect . ignore )
621+ const retry = yield * capture
622+ if ( retry !== undefined && ! isSnapshotEmptyTree ( retry ) ) {
623+ yield * Effect . logWarning ( "snapshot_empty_tree_recovered" , {
624+ hash : retry ,
625+ gitdir : state . gitdir ,
626+ } )
627+ }
628+ return retry
629+ } )
630+ } ) ,
631+ )
632+ if ( hash === undefined ) return
589633 yield * Effect . logInfo ( "tracking" , { hash, cwd : state . directory , git : state . gitdir } )
590634 return hash
591635 } ) ,
0 commit comments