@@ -420,6 +420,11 @@ export async function runAutoCommit(
420420 ] ;
421421
422422 const result = runSync ( tpsCmd , args , { cwd : repo , encoding : "utf-8" } ) ;
423+ const resultStdout = typeof result . stdout === "string" ? result . stdout . trim ( ) : "" ;
424+ const resultStderr = typeof result . stderr === "string" ? result . stderr . trim ( ) : "" ;
425+ console . log ( `[autoCommit] tps agent commit exit=${ result . status } push=${ push } branch=${ branchName } ` ) ;
426+ if ( resultStdout ) console . log ( `[autoCommit] stdout: ${ resultStdout . slice ( 0 , 200 ) } ` ) ;
427+ if ( resultStderr ) console . log ( `[autoCommit] stderr: ${ resultStderr . slice ( 0 , 200 ) } ` ) ;
423428 if ( result . status === 0 ) {
424429 if ( push && openPr && prRepo ) {
425430 const prArgs = [
@@ -434,11 +439,16 @@ export async function runAutoCommit(
434439 "--body" ,
435440 prBody ?? commitMessage ,
436441 ] ;
442+ console . log ( `[autoCommit] opening PR: gh-as ${ prArgs [ 0 ] } pr create --repo ${ prRepo } --head ${ branchName } ` ) ;
437443 const prResult = runSync ( "gh-as" , prArgs , { cwd : repo , encoding : "utf-8" } ) ;
444+ const prStdout2 = typeof prResult . stdout === "string" ? prResult . stdout . trim ( ) : "" ;
445+ const prStderr2 = typeof prResult . stderr === "string" ? prResult . stderr . trim ( ) : "" ;
446+ console . log ( `[autoCommit] gh-as pr create exit=${ prResult . status } stdout=${ prStdout2 . slice ( 0 , 200 ) } ` ) ;
447+ if ( prStderr2 ) console . log ( `[autoCommit] gh-as stderr: ${ prStderr2 . slice ( 0 , 200 ) } ` ) ;
438448 if ( ( prResult . status ?? 1 ) === 0 ) return ;
439449
440- const prStderr = typeof prResult . stderr === "string" ? prResult . stderr . trim ( ) : "" ;
441- const prStdout = typeof prResult . stdout === "string" ? prResult . stdout . trim ( ) : "" ;
450+ const prStderr = prStderr2 ;
451+ const prStdout = prStdout2 ;
442452 const prErrMsg = prStderr || prStdout || `exit ${ prResult . status ?? "unknown" } ` ;
443453 try {
444454 await flair . publishEvent ( {
@@ -517,6 +527,7 @@ async function _runAutoCommitLegacy(
517527 } catch ( e ) {
518528 const err = e as Error ;
519529 console . warn ( `[${ agentId } ] Auto-commit failed (non-fatal): ${ err . message } ` ) ;
530+ if ( err . stack ) console . warn ( `[${ agentId } ] Auto-commit stack: ${ err . stack . slice ( 0 , 400 ) } ` ) ;
520531 return null ;
521532 }
522533}
@@ -711,6 +722,30 @@ export async function runCodexRuntime(config: CodexRuntimeConfig): Promise<void>
711722 const flairPublisher = { publishEvent : async ( ev : Record < string , unknown > ) => {
712723 try { await ( flair as any ) . request ( "POST" , "/OrgEvent" , { ...ev , authorId : agentId } ) ; } catch { /* non-fatal */ }
713724 } } ;
725+ // Detect stale rebase state before attempting autoCommit
726+ const rebaseMergeDir = join ( config . workspace , ".git" , "rebase-merge" ) ;
727+ const rebaseApplyDir = join ( config . workspace , ".git" , "rebase-apply" ) ;
728+ // Also check worktree .git file reference
729+ // Resolve worktree git dir (workspace may use a .git file pointer)
730+ let wtGitDir : string | null = null ;
731+ try {
732+ const gitFile = join ( config . workspace , ".git" ) ;
733+ if ( existsSync ( gitFile ) ) {
734+ const stat = require ( "node:fs" ) . statSync ( gitFile ) ;
735+ if ( ! stat . isDirectory ( ) ) {
736+ const content2 = readFileSync ( gitFile , "utf-8" ) . trim ( ) ;
737+ const match = content2 . match ( / ^ g i t d i r : \s * ( .+ ) $ / ) ;
738+ if ( match ?. [ 1 ] ) wtGitDir = match [ 1 ] . trim ( ) ;
739+ }
740+ }
741+ } catch { /* ignore */ }
742+ const rebaseMergeDirWt = wtGitDir ? join ( wtGitDir , "rebase-merge" ) : null ;
743+ const hasStaleRebase = existsSync ( rebaseMergeDir ) || existsSync ( rebaseApplyDir ) ||
744+ ( rebaseMergeDirWt != null && existsSync ( rebaseMergeDirWt ) ) ;
745+ if ( hasStaleRebase ) {
746+ console . warn ( `[${ agentId } ] Stale rebase state detected before autoCommit — aborting rebase and proceeding` ) ;
747+ spawnSync ( GIT_BIN , [ "rebase" , "--abort" ] , { cwd : config . workspace , encoding : "utf-8" } ) ;
748+ }
714749 const mailSubject = msg . body . split ( "\n" ) [ 0 ] . slice ( 0 , 72 ) ;
715750 await _runAutoCommitLegacy ( agentId , config . workspace , msg . id , config . autoCommit , flairPublisher , mailSubject , msg . body ) ;
716751 }
0 commit comments