← Back to task

Commit 4602bc1b

commit 4602bc1b6617015189774cc5115bc8f4a7aa63ef
Author: Ben Sima <ben@bensima.com>
Date:   Wed Nov 26 06:38:31 2025

    Fix worker: only set Review after commit succeeds
    
    If commit fails (lint hooks, etc), save retry context and reopen task
    for another attempt. After 3 failures, mark for human intervention.
    
    Task-Id: t-1o2g8gugkr1

diff --git a/Omni/Agent/Worker.hs b/Omni/Agent/Worker.hs
index 9a6a86fd..f7324366 100644
--- a/Omni/Agent/Worker.hs
+++ b/Omni/Agent/Worker.hs
@@ -66,29 +66,43 @@ processTask worker task = do
 
       -- Run formatting and linting before commit
       AgentLog.updateActivity "Running formatters..."
-      formatResult <- runFormatters repo
-
-      case formatResult of
-        Left err -> do
-          AgentLog.log ("Formatting failed: " <> err)
-          AgentLog.updateActivity "Format failed, task incomplete"
-        -- Don't update status, leave as InProgress for retry
+      _ <- runFormatters repo
+
+      -- Try to commit (this runs git hooks which may fail)
+      let commitMsg = formatCommitMessage output tid
+      AgentLog.updateActivity "Committing..."
+      commitResult <- tryCommit repo commitMsg
+
+      case commitResult of
+        Left commitErr -> do
+          AgentLog.log ("Commit failed: " <> commitErr)
+          AgentLog.updateActivity "Commit failed, will retry"
+
+          -- Save failure context and reopen task for retry
+          maybeCtx <- TaskCore.getRetryContext tid
+          let attempt = maybe 1 (\c -> TaskCore.retryAttempt c + 1) maybeCtx
+
+          if attempt > 3
+            then do
+              AgentLog.log "Task failed 3 times, needs human intervention"
+              TaskCore.updateTaskStatus tid TaskCore.Open []
+            else do
+              TaskCore.setRetryContext
+                TaskCore.RetryContext
+                  { TaskCore.retryTaskId = tid,
+                    TaskCore.retryOriginalCommit = "",
+                    TaskCore.retryConflictFiles = [],
+                    TaskCore.retryAttempt = attempt,
+                    TaskCore.retryReason = "commit_failed: " <> commitErr
+                  }
+              TaskCore.updateTaskStatus tid TaskCore.Open []
+              AgentLog.log ("Task reopened (attempt " <> tshow attempt <> "/3)")
         Right () -> do
-          -- Update status to Review
+          -- Only set to Review after successful commit
           TaskCore.updateTaskStatus tid TaskCore.Review []
-
-          -- Commit changes using Amp output (Gerrit-style trailer)
-          let commitMsg = formatCommitMessage output tid
-          commitResult <- tryCommit repo commitMsg
-
-          case commitResult of
-            Left commitErr -> do
-              AgentLog.log ("Commit failed: " <> commitErr)
-              AgentLog.updateActivity "Commit failed"
-            Right () -> do
-              AgentLog.updateActivity "Task completed"
-              AgentLog.log ("[✓] Task " <> tid <> " completed")
-              AgentLog.update (\s -> s {AgentLog.statusTask = Nothing})
+          AgentLog.updateActivity "Task completed"
+          AgentLog.log ("[✓] Task " <> tid <> " completed")
+          AgentLog.update (\s -> s {AgentLog.statusTask = Nothing})
     Exit.ExitFailure code -> do
       AgentLog.log ("Agent failed with code " <> tshow code)
       AgentLog.updateActivity "Agent failed, retrying..."