diff options
| author | Ben Sima <ben@bensima.com> | 2025-11-22 17:07:32 -0500 |
|---|---|---|
| committer | Ben Sima <ben@bensima.com> | 2025-11-22 17:08:11 -0500 |
| commit | 832a0a7d88d0553e7edf055addb2c3a6f9f492ab (patch) | |
| tree | 805e0aa0e174f2a3c61cb44fb3fe71562d77af2b /Omni/Agent/Worker.hs | |
| parent | 3e232940a6769cc2a238dc7b41b7c7b215295963 (diff) | |
| parent | bb15513a94140c22aa3aea510314f60c94df4d97 (diff) | |
task: complete t-1o2bxd11zv9 (Merge)
https: //ampcode.com/threads/T-ca3b086b-5a85-422a-b13d-256784c04221
Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID:
https://ampcode.com/threads/T-ca3b086b-5a85-422a-b13d-256784c04221
Diffstat (limited to 'Omni/Agent/Worker.hs')
| -rw-r--r-- | Omni/Agent/Worker.hs | 99 |
1 files changed, 47 insertions, 52 deletions
diff --git a/Omni/Agent/Worker.hs b/Omni/Agent/Worker.hs index 1cc0b8d..9f9e3bb 100644 --- a/Omni/Agent/Worker.hs +++ b/Omni/Agent/Worker.hs @@ -89,7 +89,7 @@ processTask worker task = do -- Run Amp AgentLog.updateActivity "Running Amp agent..." - (exitCode, output) <- runAmp repo task + exitCode <- runAmp repo task case exitCode of Exit.ExitSuccess -> do @@ -99,10 +99,8 @@ processTask worker task = do TaskCore.updateTaskStatus tid TaskCore.Review [] -- Commit changes - -- We use the agent's output as the extended commit description - let summary = Text.strip output - let commitMsg = "feat: implement " <> tid <> "\n\n" <> summary - Git.commit repo commitMsg + -- We should check if there are changes, but 'git add .' is safe. + Git.commit repo ("feat: implement " <> tid) -- Submit for review AgentLog.updateActivity "Submitting for review..." @@ -125,7 +123,7 @@ processTask worker task = do AgentLog.updateActivity "Agent failed, retrying..." threadDelay (10 * 1000000) -- Sleep 10s -runAmp :: FilePath -> TaskCore.Task -> IO (Exit.ExitCode, Text) +runAmp :: FilePath -> TaskCore.Task -> IO Exit.ExitCode runAmp repo task = do let prompt = "You are a Worker Agent.\n" @@ -137,8 +135,7 @@ runAmp repo task = do <> "3. Run tests to verify your work (e.g., 'bild --test Omni/Namespace').\n" <> "4. Fix any errors found during testing.\n" <> "5. Do NOT update the task status or manage git branches (the system handles that).\n" - <> "6. Do NOT run 'git commit'. The system will commit your changes automatically.\n" - <> "7. When finished and tested, exit.\n\n" + <> "6. When finished and tested, exit.\n\n" <> "Context:\n" <> "- You are working in '" <> Text.pack repo @@ -147,38 +144,25 @@ runAmp repo task = do <> fromMaybe "root" (TaskCore.taskNamespace task) <> "'.\n" - Directory.createDirectoryIfMissing True (repo </> "_/llm") - let logPath = repo </> "_/llm/amp.log" - - -- Ensure log file is empty/exists - IO.writeFile logPath "" - - -- Read AGENTS.md - agentsMd <- - fmap (fromMaybe "") <| do - exists <- Directory.doesFileExist (repo </> "AGENTS.md") - if exists - then Just </ readFile (repo </> "AGENTS.md") - else pure Nothing + let logFile = repo </> "_/llm/amp.log" - let fullPrompt = - prompt - <> "\n\nREPOSITORY GUIDELINES (AGENTS.md):\n" - <> agentsMd + -- Remove old log file + exists <- Directory.doesFileExist logFile + when exists (Directory.removeFile logFile) - -- Monitor log file - tidLog <- forkIO (monitorLog logPath) + Directory.createDirectoryIfMissing True (repo </> "_/llm") -- Assume amp is in PATH - let args = ["--log-level", "debug", "--log-file", "_/llm/amp.log", "--dangerously-allow-all", "-x", Text.unpack fullPrompt] + let args = ["--log-level", "debug", "--log-file", "_/llm/amp.log", "--dangerously-allow-all", "-x", Text.unpack prompt] let cp = (Process.proc "amp" args) {Process.cwd = Just repo} - (exitCode, out, _err) <- Process.readCreateProcessWithExitCode cp "" + (_, _, _, ph) <- Process.createProcess cp - -- Cleanup - killThread tidLog + tid <- forkIO <| monitorLog logFile ph - pure (exitCode, Text.pack out) + exitCode <- Process.waitForProcess ph + killThread tid + pure exitCode formatTask :: TaskCore.Task -> Text formatTask t = @@ -210,6 +194,37 @@ formatTask t = where formatDep dep = " - " <> TaskCore.depId dep <> " [" <> Text.pack (show (TaskCore.depType dep)) <> "]" +monitorLog :: FilePath -> Process.ProcessHandle -> IO () +monitorLog path ph = do + waitForFile path + IO.withFile path IO.ReadMode <| \h -> do + IO.hSetBuffering h IO.LineBuffering + go h + where + go h = do + eof <- IO.hIsEOF h + if eof + then do + mExit <- Process.getProcessExitCode ph + case mExit of + Nothing -> do + threadDelay 100000 -- 0.1s + go h + Just _ -> pure () + else do + line <- TIO.hGetLine h + AgentLog.processLogLine line + go h + +waitForFile :: FilePath -> IO () +waitForFile path = do + exists <- Directory.doesFileExist path + if exists + then pure () + else do + threadDelay 100000 + waitForFile path + findBaseBranch :: FilePath -> TaskCore.Task -> IO Text findBaseBranch repo task = do let deps = TaskCore.taskDependencies task @@ -230,23 +245,3 @@ findBaseBranch repo task = do case candidates of (candidate : _) -> pure ("task/" <> TaskCore.depId candidate) [] -> pure "live" - -monitorLog :: FilePath -> IO () -monitorLog path = do - -- Wait for file to exist - waitForFile path - - IO.withFile path IO.ReadMode <| \h -> do - IO.hSetBuffering h IO.LineBuffering - forever <| do - eof <- IO.hIsEOF h - if eof - then threadDelay 100000 -- 0.1s - else do - line <- TIO.hGetLine h - AgentLog.processLogLine line - -waitForFile :: FilePath -> IO () -waitForFile p = do - e <- Directory.doesFileExist p - if e then pure () else threadDelay 100000 >> waitForFile p |
