summaryrefslogtreecommitdiff
path: root/Omni/Agent/Worker.hs
blob: d6afb735ca64824f1d9c3c03e0d055db6d64d902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Omni.Agent.Worker
  ( start,
    buildFullPrompt,
    selectModel,
    selectCostByComplexity,
  )
where

import Alpha
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Key as AesonKey
import qualified Data.ByteString.Lazy as BSL
import Data.IORef (modifyIORef', newIORef, readIORef)
import qualified Data.List as List
import qualified Data.Text as Text
import qualified Data.Text.Encoding as TE
import qualified Data.Time
import qualified Omni.Agent.Core as Core
import qualified Omni.Agent.Engine as Engine
import qualified Omni.Agent.Provider as Provider
import qualified Omni.Agent.Status as AgentStatus
import qualified Omni.Agent.Tools as Tools
import qualified Omni.Fact as Fact
import qualified Omni.Task.Core as TaskCore
import qualified System.Directory as Directory
import qualified System.Environment as Env
import qualified System.Exit as Exit
import System.FilePath ((</>))
import qualified System.Process as Process

start :: Core.Worker -> Maybe Text -> IO ()
start worker maybeTaskId = do
  if Core.workerQuiet worker
    then putText ("[worker] Starting for " <> Core.workerName worker)
    else do
      AgentStatus.init (Core.workerName worker)
      AgentStatus.log ("[worker] Starting for " <> Core.workerName worker)
  case maybeTaskId of
    Just tid -> logMsg worker ("[worker] Target task: " <> tid)
    Nothing -> logMsg worker "[worker] No specific task, will pick from ready queue"
  runOnce worker maybeTaskId

-- | Log message respecting quiet mode
logMsg :: Core.Worker -> Text -> IO ()
logMsg worker msg =
  if Core.workerQuiet worker
    then putText msg
    else AgentStatus.log msg

-- | Convert key-value pairs to JSON metadata string
toMetadata :: [(Text, Text)] -> Text
toMetadata pairs =
  let obj = Aeson.object [(AesonKey.fromText k, Aeson.String v) | (k, v) <- pairs]
   in TE.decodeUtf8 (BSL.toStrict (Aeson.encode obj))

-- | Format guardrail result for logging
formatGuardrailResult :: Engine.GuardrailResult -> Text
formatGuardrailResult Engine.GuardrailOk = "OK"
formatGuardrailResult (Engine.GuardrailCostExceeded actual limit) =
  "Cost exceeded: " <> tshow actual <> " cents (limit: " <> tshow limit <> ")"
formatGuardrailResult (Engine.GuardrailTokensExceeded actual limit) =
  "Tokens exceeded: " <> tshow actual <> " (limit: " <> tshow limit <> ")"
formatGuardrailResult (Engine.GuardrailDuplicateToolCalls tool count) =
  "Duplicate tool calls: " <> tool <> " called " <> tshow count <> " times"
formatGuardrailResult (Engine.GuardrailTestFailures count) =
  "Test failures: " <> tshow count <> " failures"
formatGuardrailResult (Engine.GuardrailEditFailures count) =
  "Edit failures: " <> tshow count <> " 'old_str not found' errors"

runOnce :: Core.Worker -> Maybe Text -> IO ()
runOnce worker maybeTaskId = do
  -- Find work
  targetTask <- case maybeTaskId of
    Just tid -> do
      TaskCore.findTask tid </ TaskCore.loadTasks
    Nothing -> do
      readyTasks <- TaskCore.getReadyTasks
      case readyTasks of
        [] -> pure Nothing
        (task : _) -> pure (Just task)

  case targetTask of
    Nothing -> do
      case maybeTaskId of
        Just tid -> do
          unless (Core.workerQuiet worker) <| AgentStatus.updateActivity ("Task " <> tid <> " not found.")
          logMsg worker ("[worker] Task " <> tid <> " not found.")
        Nothing -> do
          unless (Core.workerQuiet worker) <| AgentStatus.updateActivity "No work found."
          logMsg worker "[worker] No ready tasks found."
    Just task -> do
      processTask worker task

processTask :: Core.Worker -> TaskCore.Task -> IO ()
processTask worker task = do
  let repo = Core.workerPath worker
  let tid = TaskCore.taskId task
  let quiet = Core.workerQuiet worker
  let say = logMsg worker

  unless quiet <| AgentStatus.update (\s -> s {AgentStatus.statusTask = Just tid})
  say ("[worker] Claiming task " <> tid)

  -- Claim task
  TaskCore.logActivity tid TaskCore.Claiming Nothing
  TaskCore.updateTaskStatusWithActor tid TaskCore.InProgress [] TaskCore.Junior
  say "[worker] Status -> InProgress"

  -- Run agent with timing
  startTime <- Data.Time.getCurrentTime
  activityId <- TaskCore.logActivityWithMetrics tid TaskCore.Running Nothing Nothing (Just startTime) Nothing Nothing Nothing

  say "[worker] Starting engine..."
  engineResult <- runWithEngine worker repo task

  endTime <- Data.Time.getCurrentTime

  -- Update the activity record with metrics (convert Double to Int by rounding)
  let costCents = case engineResult of
        EngineSuccess _ c -> c
        EngineGuardrailViolation _ c -> c
        EngineError _ c -> c
  TaskCore.updateActivityMetrics activityId Nothing (Just endTime) (Just (round costCents)) Nothing

  case engineResult of
    EngineSuccess output _ -> do
      say "[worker] Agent completed successfully"
      TaskCore.logActivity tid TaskCore.Reviewing Nothing
      say "[worker] Running formatters..."
      _ <- runFormatters repo

      -- Try to commit (this runs git hooks which may fail)
      let commitMsg = formatCommitMessage task output
      say "[worker] Attempting commit..."
      commitResult <- tryCommit repo commitMsg

      case commitResult of
        CommitFailed commitErr -> do
          say ("[worker] Commit failed: " <> commitErr)

          -- 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
              say "[worker] Task failed 3 times, needs human intervention"
              TaskCore.logActivity tid TaskCore.Failed (Just (toMetadata [("reason", "max_retries_exceeded")]))
              TaskCore.updateTaskStatusWithActor tid TaskCore.Open [] TaskCore.Junior
            else do
              let currentReason = "attempt " <> tshow attempt <> ": commit_failed: " <> commitErr
              let accumulatedReason = case maybeCtx of
                    Nothing -> currentReason
                    Just ctx -> TaskCore.retryReason ctx <> "\n" <> currentReason
              TaskCore.setRetryContext
                TaskCore.RetryContext
                  { TaskCore.retryTaskId = tid,
                    TaskCore.retryOriginalCommit = "",
                    TaskCore.retryConflictFiles = [],
                    TaskCore.retryAttempt = attempt,
                    TaskCore.retryReason = accumulatedReason,
                    TaskCore.retryNotes = maybeCtx +> TaskCore.retryNotes
                  }
              TaskCore.logActivity tid TaskCore.Retrying (Just (toMetadata [("attempt", tshow attempt)]))
              TaskCore.updateTaskStatusWithActor tid TaskCore.Open [] TaskCore.Junior
              say ("[worker] Task reopened (attempt " <> tshow attempt <> "/3)")
        NoChanges -> do
          -- No changes = task already implemented, mark as Done
          say "[worker] No changes to commit - task already done"
          TaskCore.clearRetryContext tid
          TaskCore.logActivity tid TaskCore.Completed (Just (toMetadata [("result", "no_changes")]))
          TaskCore.updateTaskStatusWithActor tid TaskCore.Done [] TaskCore.Junior
          say ("[worker] ✓ Task " <> tid <> " -> Done (no changes)")
          unless quiet <| AgentStatus.update (\s -> s {AgentStatus.statusTask = Nothing})
        CommitSuccess -> do
          -- Commit succeeded, set to Review
          TaskCore.logActivity tid TaskCore.Completed (Just (toMetadata [("result", "committed")]))
          TaskCore.updateTaskStatusWithActor tid TaskCore.Review [] TaskCore.Junior
          say ("[worker] ✓ Task " <> tid <> " -> Review")
          unless quiet <| AgentStatus.update (\s -> s {AgentStatus.statusTask = Nothing})
    EngineGuardrailViolation errMsg _ -> do
      say ("[worker] Guardrail violation: " <> errMsg)
      TaskCore.logActivity tid TaskCore.Failed (Just (toMetadata [("reason", "guardrail_violation")]))
      -- Add comment with guardrail details
      _ <- TaskCore.addComment tid errMsg TaskCore.Junior
      -- Set to NeedsHelp so human can review
      TaskCore.updateTaskStatusWithActor tid TaskCore.NeedsHelp [] TaskCore.Junior
      say ("[worker] Task " <> tid <> " -> NeedsHelp (guardrail violation)")
      unless quiet <| AgentStatus.update (\s -> s {AgentStatus.statusTask = Nothing})
    EngineError errMsg _ -> do
      say ("[worker] Engine error: " <> errMsg)
      TaskCore.logActivity tid TaskCore.Failed (Just (toMetadata [("reason", "engine_error")]))
      -- Don't set back to Open here - leave in InProgress for debugging
      say "[worker] Task left in InProgress (engine failure)"

-- | Run lint --fix to format and fix lint issues
runFormatters :: FilePath -> IO (Either Text ())
runFormatters repo = do
  let cmd = (Process.proc "lint" ["--fix"]) {Process.cwd = Just repo}
  (code, _, _) <- Process.readCreateProcessWithExitCode cmd ""
  case code of
    Exit.ExitSuccess -> pure (Right ())
    Exit.ExitFailure _ -> pure (Right ()) -- lint --fix may exit non-zero but still fix things

data CommitResult = CommitSuccess | NoChanges | CommitFailed Text
  deriving (Show, Eq)

-- | Try to commit, returning result
tryCommit :: FilePath -> Text -> IO CommitResult
tryCommit repo msg = do
  -- Stage all changes
  let addCmd = (Process.proc "git" ["add", "."]) {Process.cwd = Just repo}
  (addCode, _, addErr) <- Process.readCreateProcessWithExitCode addCmd ""
  case addCode of
    Exit.ExitFailure _ -> pure <| CommitFailed (Text.pack addErr)
    Exit.ExitSuccess -> do
      -- Check for changes
      let checkCmd = (Process.proc "git" ["diff", "--cached", "--quiet"]) {Process.cwd = Just repo}
      (checkCode, _, _) <- Process.readCreateProcessWithExitCode checkCmd ""
      case checkCode of
        Exit.ExitSuccess -> pure NoChanges
        Exit.ExitFailure 1 -> do
          -- There are changes, commit them
          let commitCmd = (Process.proc "git" ["commit", "-m", Text.unpack msg]) {Process.cwd = Just repo}
          (commitCode, _, commitErr) <- Process.readCreateProcessWithExitCode commitCmd ""
          case commitCode of
            Exit.ExitSuccess -> pure CommitSuccess
            Exit.ExitFailure _ -> pure <| CommitFailed (Text.pack commitErr)
        Exit.ExitFailure c -> pure <| CommitFailed ("git diff failed with code " <> tshow c)

data EngineResult
  = EngineSuccess Text Double -- output, cost
  | EngineGuardrailViolation Text Double -- error message, cost
  | EngineError Text Double -- error message, cost

-- | Run task using native Engine
-- Returns engine result with output/error and cost
runWithEngine :: Core.Worker -> FilePath -> TaskCore.Task -> IO EngineResult
runWithEngine worker repo task = do
  -- Read API key from environment
  maybeApiKey <- Env.lookupEnv "OPENROUTER_API_KEY"
  case maybeApiKey of
    Nothing -> pure (EngineError "OPENROUTER_API_KEY not set" 0)
    Just apiKey -> do
      -- Check for retry context
      maybeRetry <- TaskCore.getRetryContext (TaskCore.taskId task)

      -- Get progress from database (checkpoint events from previous sessions)
      progressContent <- TaskCore.getProgressSummary (TaskCore.taskId task)

      -- Build the full prompt
      let ns = fromMaybe "." (TaskCore.taskNamespace task)
      let basePrompt = buildBasePrompt ns repo

      -- Add progress context if present
      let progressPrompt = buildProgressPrompt progressContent

      -- Add retry context if present
      let retryPrompt = buildRetryPrompt maybeRetry

      let prompt = basePrompt <> progressPrompt <> retryPrompt

      -- Read AGENTS.md
      agentsMd <-
        fmap (fromMaybe "") <| do
          exists <- Directory.doesFileExist (repo </> "AGENTS.md")
          if exists
            then Just </ readFile (repo </> "AGENTS.md")
            else pure Nothing

      -- Get relevant facts from the knowledge base
      relevantFacts <- getRelevantFacts task
      let factsSection = formatFacts relevantFacts

      -- Build system prompt
      let systemPrompt =
            prompt
              <> "\n\nREPOSITORY GUIDELINES (AGENTS.md):\n"
              <> agentsMd
              <> factsSection

      -- Build user prompt from task comments
      let userPrompt = formatTask task

      -- Select model based on task complexity (simple heuristic)
      let model = selectModel task

      -- Generate session ID for event logging
      sessionId <- TaskCore.generateSessionId
      let tid = TaskCore.taskId task

      -- Helper to log events to DB
      -- For text content, store as-is; for structured data, JSON-encode
      let logJuniorEvent eventType content = TaskCore.insertAgentEvent tid sessionId eventType content TaskCore.Junior
          logJuniorJson eventType value = do
            let contentJson = TE.decodeUtf8 (BSL.toStrict (Aeson.encode value))
            TaskCore.insertAgentEvent tid sessionId eventType contentJson TaskCore.Junior
          logSystemEvent eventType content = TaskCore.insertAgentEvent tid sessionId eventType content TaskCore.System

      -- Build Engine config with callbacks
      totalCostRef <- newIORef (0 :: Double)
      let quiet = Core.workerQuiet worker
          sayLog msg = if quiet then putText msg else AgentStatus.log msg
          engineCfg =
            Engine.EngineConfig
              { Engine.engineLLM =
                  Engine.defaultLLM
                    { Engine.llmApiKey = Text.pack apiKey
                    },
                Engine.engineOnCost = \tokens cost -> do
                  modifyIORef' totalCostRef (+ cost)
                  sayLog <| "Cost: " <> tshow cost <> " cents (" <> tshow tokens <> " tokens)"
                  logJuniorJson "Cost" (Aeson.object [("tokens", Aeson.toJSON tokens), ("cents", Aeson.toJSON cost)]),
                Engine.engineOnActivity = \activity -> do
                  sayLog <| "[engine] " <> activity,
                Engine.engineOnToolCall = \toolName args -> do
                  sayLog <| "[tool] " <> toolName
                  logJuniorEvent "ToolCall" (toolName <> ": " <> args),
                Engine.engineOnAssistant = \msg -> do
                  sayLog <| "[assistant] " <> Text.take 200 msg
                  logJuniorEvent "Assistant" msg,
                Engine.engineOnToolResult = \toolName success output -> do
                  let statusStr = if success then "ok" else "failed"
                  sayLog <| "[result] " <> toolName <> " (" <> statusStr <> "): " <> Text.take 100 output
                  logJuniorEvent "ToolResult" output,
                Engine.engineOnComplete = do
                  sayLog "[engine] Complete"
                  logJuniorEvent "Complete" "",
                Engine.engineOnError = \err -> do
                  sayLog <| "[error] " <> err
                  logJuniorEvent "Error" err,
                Engine.engineOnGuardrail = \guardrailResult -> do
                  let guardrailMsg = formatGuardrailResult guardrailResult
                      contentJson = TE.decodeUtf8 (BSL.toStrict (Aeson.encode guardrailResult))
                  sayLog <| "[guardrail] " <> guardrailMsg
                  logSystemEvent "Guardrail" contentJson
              }

      -- Build Agent config with guardrails (scale cost by complexity)
      let baseCost = selectCostByComplexity (TaskCore.taskComplexity task)
          guardrails =
            Engine.Guardrails
              { Engine.guardrailMaxCostCents = baseCost,
                Engine.guardrailMaxTokens = 2000000,
                Engine.guardrailMaxDuplicateToolCalls = 30,
                Engine.guardrailMaxTestFailures = 3,
                Engine.guardrailMaxEditFailures = 5
              }
          agentCfg =
            Engine.AgentConfig
              { Engine.agentModel = model,
                Engine.agentTools = Tools.allTools,
                Engine.agentSystemPrompt = systemPrompt,
                Engine.agentMaxIterations = 100,
                Engine.agentGuardrails = guardrails
              }

      -- Run the agent with appropriate provider
      result <- case Core.workerEngine worker of
        Core.EngineOpenRouter -> Engine.runAgent engineCfg agentCfg userPrompt
        Core.EngineOllama -> do
          ollamaModel <- fromMaybe "llama3.1:8b" </ Env.lookupEnv "OLLAMA_MODEL"
          let provider = Provider.defaultOllama (Text.pack ollamaModel)
          Engine.runAgentWithProvider engineCfg provider agentCfg userPrompt
        Core.EngineAmp -> pure (Left "Amp engine not yet implemented")
      totalCost <- readIORef totalCostRef

      case result of
        Left err ->
          if "Guardrail: " `Text.isPrefixOf` err
            then pure (EngineGuardrailViolation err totalCost)
            else pure (EngineError ("Engine error: " <> err) totalCost)
        Right agentResult -> do
          let output = Engine.resultFinalMessage agentResult
          pure (EngineSuccess output totalCost)

-- | Build the base prompt for the agent
buildBasePrompt :: Text -> FilePath -> Text
buildBasePrompt ns repo =
  "You are `jr`, an autonomous Senior Software Engineer. You are rigorous, efficient, and safety-conscious.\n"
    <> "Your Goal: Complete the assigned task with **zero regressions**.\n\n"
    <> "# The Workflow\n"
    <> "Follow this 4-phase loop. Do not skip phases.\n\n"
    <> "## Phase 1: Exploration (MANDATORY)\n"
    <> "- NEVER edit immediately. Explore first.\n"
    <> "- Use search_and_read to find code relevant to the task.\n"
    <> "- Read the imports. Read the tests that cover this code.\n"
    <> "- Understand the *callers* of a function before you modify it.\n\n"
    <> "## Phase 2: Planning (for multi-file changes)\n"
    <> "- If the task involves more than 2 files, plan the order of operations.\n"
    <> "- Identify potential breaking changes (API shifts, import cycles).\n"
    <> "- For refactors: copy code first, verify it works, then delete the original.\n\n"
    <> "## Phase 3: Execution\n"
    <> "- Make atomic changes. One logical edit per edit_file call.\n"
    <> "- Use edit_file with sufficient context (5+ lines) to match uniquely.\n"
    <> "- Do NOT update task status or manage git - the worker handles that.\n\n"
    <> "## Phase 4: Verification\n"
    <> "- Run 'bild --test "
    <> ns
    <> "' after your changes.\n"
    <> "- 'bild --test' tests ALL dependencies transitively - run it ONCE, not per-file.\n"
    <> "- Use 'lint --fix' to handle formatting (not hlint directly).\n"
    <> "- If tests pass, STOP. Do not verify again, do not double-check.\n\n"
    <> "# Tool Usage\n\n"
    <> "Your tools: read_file, write_file, edit_file, run_bash, search_codebase, search_and_read.\n\n"
    <> "## Efficient Reading (CRITICAL FOR BUDGET)\n"
    <> "- Read files ONCE with large ranges (500+ lines), not many small 100-line chunks.\n"
    <> "- WRONG: 10 separate read_file calls with 100-line ranges on the same file.\n"
    <> "- RIGHT: 1-2 read_file calls with 500-1000 line ranges to cover the file.\n"
    <> "- When you know the target file, use read_file directly with a path argument.\n"
    <> "- WRONG: search_and_read across the whole repo when you know the file is Worker.py.\n"
    <> "- RIGHT: read_file on Worker.py, or search_codebase with path='Worker.py'.\n"
    <> "- search_and_read is for discovery when you DON'T know which file to look in.\n\n"
    <> "## Efficient Editing\n"
    <> "- Include enough context in old_str to match uniquely (usually 5+ lines).\n"
    <> "- If edit_file fails with 'old_str not found', you are hallucinating the content.\n"
    <> "- STOP. Call read_file on those exact lines to get fresh content. Then retry.\n"
    <> "- After 3 failed edits on the same file, reconsider your approach.\n\n"
    <> "## Cost Awareness\n"
    <> "- Each tool call costs tokens. Large file writes are expensive.\n"
    <> "- For refactors: plan all new files first, then write them in order.\n"
    <> "- Don't write a file, then immediately read it back - you just wrote it!\n"
    <> "- Monitor your progress: if you're on tool call 30+ and not close to done, simplify.\n\n"
    <> "# Debugging\n"
    <> "If 'bild' fails, do NOT guess the fix.\n"
    <> "1. Read the error output carefully.\n"
    <> "2. For type errors: read the definition of the types involved.\n"
    <> "3. For import cycles: create a Types or Common module to break the cycle.\n"
    <> "4. If tests fail 3 times on the same issue, STOP - the task will be marked for human review.\n\n"
    <> "# Examples\n\n"
    <> "## Example: Splitting a Module\n"
    <> "1. search_and_read to understand the file structure\n"
    <> "2. write_file NewModule.py (with extracted code + proper imports)\n"
    <> "3. edit_file Original.py (remove moved code, add 'from NewModule import ...')\n"
    <> "4. run_bash: bild --test <namespace>\n"
    <> "5. Tests pass -> STOP\n\n"
    <> "## Example: Fixing a Type Error\n"
    <> "1. read_file Main.hs (lines around the error)\n"
    <> "2. Identify: function expects Text but got String\n"
    <> "3. edit_file Main.hs (add import, apply T.pack)\n"
    <> "4. run_bash: bild --test <namespace>\n"
    <> "5. Tests pass -> STOP\n\n"
    <> "# Constraints\n"
    <> "- You are autonomous. There is NO human to ask for clarification.\n"
    <> "- Make reasonable decisions. If ambiguous, implement the straightforward interpretation.\n"
    <> "- Aim to complete the task in under 50 tool calls.\n"
    <> "- Guardrails will stop you if you exceed cost/token limits or make repeated mistakes.\n\n"
    <> "# Context\n"
    <> "- Working directory: "
    <> Text.pack repo
    <> "\n"
    <> "- Namespace: "
    <> ns
    <> "\n"

-- | Build progress context prompt
buildProgressPrompt :: Maybe Text -> Text
buildProgressPrompt Nothing = ""
buildProgressPrompt (Just progress) =
  "\n\n## PROGRESS FROM PREVIOUS SESSIONS (from database)\n\n"
    <> "This task has been worked on before. Here are the checkpoint notes:\n\n"
    <> progress
    <> "\n\n"
    <> "IMPORTANT:\n"
    <> "- Review these checkpoints to understand what's already done\n"
    <> "- Do NOT repeat work that's already completed\n"
    <> "- If the task appears complete, verify tests pass and exit\n\n"

-- | Build retry context prompt
buildRetryPrompt :: Maybe TaskCore.RetryContext -> Text
buildRetryPrompt Nothing = ""
buildRetryPrompt (Just ctx) =
  "\n\n## RETRY CONTEXT (IMPORTANT)\n\n"
    <> "This task was previously attempted but failed. Attempt: "
    <> tshow (TaskCore.retryAttempt ctx)
    <> "/3\n"
    <> "Reason: "
    <> TaskCore.retryReason ctx
    <> "\n\n"
    <> ( if null (TaskCore.retryConflictFiles ctx)
           then ""
           else
             "Conflicting files from previous attempt:\n"
               <> Text.unlines (map ("  - " <>) (TaskCore.retryConflictFiles ctx))
               <> "\n"
       )
    <> "Original commit: "
    <> TaskCore.retryOriginalCommit ctx
    <> "\n\n"
    <> maybe "" (\notes -> "## HUMAN NOTES/GUIDANCE\n\n" <> notes <> "\n\n") (TaskCore.retryNotes ctx)
    <> "INSTRUCTIONS FOR RETRY:\n"
    <> "- The codebase has changed since your last attempt\n"
    <> "- Re-implement this task on top of the CURRENT codebase\n"
    <> "- If there were merge conflicts, the conflicting files may have been modified by others\n"
    <> "- Review the current state of those files before making changes\n"

-- | Select model based on task complexity (1-5 scale)
-- Uses OpenRouter model identifiers for Claude models
selectModel :: TaskCore.Task -> Text
selectModel task = selectModelByComplexity (TaskCore.taskComplexity task)

-- | Select model based on complexity level
selectModelByComplexity :: Maybe Int -> Text
selectModelByComplexity Nothing = "anthropic/claude-sonnet-4.5"
selectModelByComplexity (Just 1) = "anthropic/claude-haiku-4.5"
selectModelByComplexity (Just 2) = "anthropic/claude-haiku-4.5"
selectModelByComplexity (Just 3) = "anthropic/claude-sonnet-4.5"
selectModelByComplexity (Just 4) = "anthropic/claude-sonnet-4.5"
selectModelByComplexity (Just 5) = "anthropic/claude-opus-4.5"
selectModelByComplexity (Just _) = "anthropic/claude-sonnet-4.5"

-- | Select cost guardrail based on complexity level (in cents)
-- Lower complexity = lower budget, higher complexity = more room for iteration
selectCostByComplexity :: Maybe Int -> Double
selectCostByComplexity Nothing = 200.0
selectCostByComplexity (Just 1) = 50.0
selectCostByComplexity (Just 2) = 100.0
selectCostByComplexity (Just 3) = 200.0
selectCostByComplexity (Just 4) = 400.0
selectCostByComplexity (Just 5) = 600.0
selectCostByComplexity (Just _) = 200.0

formatTask :: TaskCore.Task -> Text
formatTask t =
  "Task: "
    <> TaskCore.taskId t
    <> "\n"
    <> "Title:      "
    <> TaskCore.taskTitle t
    <> "\n"
    <> "Type:       "
    <> Text.pack (show (TaskCore.taskType t))
    <> "\n"
    <> "Status:     "
    <> Text.pack (show (TaskCore.taskStatus t))
    <> "\n"
    <> "Priority:   "
    <> Text.pack (show (TaskCore.taskPriority t))
    <> "\n"
    <> maybe "" (\p -> "Parent:     " <> p <> "\n") (TaskCore.taskParent t)
    <> maybe "" (\ns -> "Namespace:  " <> ns <> "\n") (TaskCore.taskNamespace t)
    <> "Created:    "
    <> Text.pack (show (TaskCore.taskCreatedAt t))
    <> "\n"
    <> "Updated:    "
    <> Text.pack (show (TaskCore.taskUpdatedAt t))
    <> "\n"
    <> (if Text.null (TaskCore.taskDescription t) then "" else "Description:\n" <> TaskCore.taskDescription t <> "\n\n")
    <> formatDeps (TaskCore.taskDependencies t)
    <> formatComments (TaskCore.taskComments t)
  where
    formatDeps [] = ""
    formatDeps deps = "\nDependencies:\n" <> Text.unlines (map formatDep deps)
    formatDep dep = "  - " <> TaskCore.depId dep <> " [" <> Text.pack (show (TaskCore.depType dep)) <> "]"
    formatComments [] = ""
    formatComments cs = "\nComments/Notes:\n" <> Text.unlines (map formatComment cs)
    formatComment c = "  [" <> Text.pack (show (TaskCore.commentCreatedAt c)) <> "] " <> TaskCore.commentText c

formatCommitMessage :: TaskCore.Task -> Text -> Text
formatCommitMessage task agentOutput =
  let tid = TaskCore.taskId task
      subject = cleanSubject (TaskCore.taskTitle task)
      body = cleanBody agentOutput
   in if Text.null body
        then subject <> "\n\nTask-Id: " <> tid
        else subject <> "\n\n" <> body <> "\n\nTask-Id: " <> tid
  where
    cleanSubject s =
      let trailingPunct = ['.', ':', '!', '?', ',', ';', ' ', '-']
          stripped = Text.dropWhileEnd (`elem` trailingPunct) s
          truncated = Text.take 72 stripped
          noPunct = Text.dropWhileEnd (`elem` trailingPunct) truncated
          capitalized = case Text.uncons noPunct of
            Just (c, rest) -> Text.cons (toUpper c) rest
            Nothing -> noPunct
       in capitalized

    cleanBody :: Text -> Text
    cleanBody output =
      let stripped = Text.strip output
       in if Text.null stripped
            then ""
            else
              let lns = Text.lines stripped
                  cleaned = [Text.take 72 ln | ln <- lns]
               in Text.intercalate "\n" cleaned

-- | Get facts relevant to a task based on namespace/project
getRelevantFacts :: TaskCore.Task -> IO [TaskCore.Fact]
getRelevantFacts task = do
  let namespace = fromMaybe "Omni" (TaskCore.taskNamespace task)
  projectFacts <- Fact.getFactsByProject namespace
  let sorted = List.sortBy (comparing (Down <. TaskCore.factConfidence)) projectFacts
  pure (take 10 sorted)

-- | Format facts for inclusion in the prompt
formatFacts :: [TaskCore.Fact] -> Text
formatFacts [] = ""
formatFacts facts =
  Text.unlines
    [ "\n\nKNOWLEDGE BASE FACTS:",
      "(These are learned patterns/conventions from previous work)",
      ""
    ]
    <> Text.unlines (map formatFact facts)

-- | Format a single fact for the prompt
formatFact :: TaskCore.Fact -> Text
formatFact f =
  "- "
    <> TaskCore.factContent f
    <> ( if null (TaskCore.factRelatedFiles f)
           then ""
           else " [" <> Text.intercalate ", " (TaskCore.factRelatedFiles f) <> "]"
       )

-- | Build the full system prompt for a task without starting the agent.
-- This is useful for debugging/inspecting what the agent will be told.
buildFullPrompt :: TaskCore.Task -> IO Text
buildFullPrompt task = do
  repo <- Directory.getCurrentDirectory
  let ns = fromMaybe "." (TaskCore.taskNamespace task)
  let basePrompt = buildBasePrompt ns repo

  maybeRetry <- TaskCore.getRetryContext (TaskCore.taskId task)
  progressContent <- TaskCore.getProgressSummary (TaskCore.taskId task)

  let progressPrompt = buildProgressPrompt progressContent
  let retryPrompt = buildRetryPrompt maybeRetry
  let prompt = basePrompt <> progressPrompt <> retryPrompt

  agentsMd <-
    fmap (fromMaybe "") <| do
      exists <- Directory.doesFileExist (repo </> "AGENTS.md")
      if exists
        then Just </ readFile (repo </> "AGENTS.md")
        else pure Nothing

  relevantFacts <- getRelevantFacts task
  let factsSection = formatFacts relevantFacts

  let systemPrompt =
        prompt
          <> "\n\nREPOSITORY GUIDELINES (AGENTS.md):\n"
          <> agentsMd
          <> factsSection

  let model = selectModel task
  let costBudget = selectCostByComplexity (TaskCore.taskComplexity task)

  pure
    <| Text.unlines
      [ "=== AGENT CONFIGURATION ===",
        "Model: " <> model,
        "Cost budget: " <> tshow costBudget <> " cents",
        "",
        "=== SYSTEM PROMPT ===",
        systemPrompt,
        "",
        "=== USER PROMPT (task details) ===",
        formatTask task
      ]