oharness-loop 0.1.0

Agent, Loop trait, ReactLoop, ConversationLoop, and run_reflexion for open-harness
Documentation
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
//! The default ReAct-style loop: thought → action → observation.
//!
//! Emits only lifecycle events (`meta`, `run.*`, `turn.*`, `budget.exceeded`).
//! `llm.*` and `tool.*` events are produced by `RequestTracer` and
//! `ToolTracer` in `oharness-trace`, wired in by `Agent::run`
//! (see docs/remaining-work.md §2.4 — M1b-δ refactor).

use crate::loop_trait::{Loop, LoopContext};
use async_trait::async_trait;
use oharness_core::event::{
    EventKind, MetaPayload, RunFinishedPayload, RunStartedPayload, TurnFinishedPayload,
    TurnPayload, TurnRevisedPayload,
};
use oharness_core::{
    AgentError, AssistantTurn, BudgetRequest, CompletionRequest, CompletionResponse, Content,
    ConversationView, Message, MetadataMap, ResourceUsage, RunError, RunErrorCategory, RunOutcome,
    StopReason, Task, Termination, TrajectoryHandle, TrajectoryView, TruncationLimit,
};
use oharness_critic::{AssessmentContext, Critic, CriticTrigger, CriticVerdict};
use oharness_llm::complete_from_stream;
use oharness_memory::policy::MemoryContext;
use oharness_tools::context::ToolContext;
use oharness_tools::toolset::ToolOutcome;
use oharness_trace::TOOL_USE_ID_KEY;
use serde_json::json;
use time::OffsetDateTime;

pub struct ReactLoop {
    system_prompt: Option<String>,
}

impl Default for ReactLoop {
    fn default() -> Self {
        Self {
            system_prompt: Some(DEFAULT_SYSTEM_PROMPT.to_string()),
        }
    }
}

impl ReactLoop {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_system_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.system_prompt = Some(prompt.into());
        self
    }

    pub fn without_system_prompt(mut self) -> Self {
        self.system_prompt = None;
        self
    }
}

#[async_trait]
impl Loop for ReactLoop {
    async fn run(&self, task: Task, ctx: &LoopContext) -> Result<RunOutcome, AgentError> {
        let started_at = OffsetDateTime::now_utc();
        let start_instant = std::time::Instant::now();

        // ---- meta event (always first) ----
        let capabilities = ctx.llm.capabilities();
        ctx.events.emit(
            "run-0",
            EventKind::Meta(MetaPayload {
                schema_version: oharness_core::event::SchemaVersion::CURRENT,
                harness_version: env!("CARGO_PKG_VERSION").to_string(),
                task_snapshot: task.clone(),
                llm_capabilities: capabilities.clone(),
            }),
            None,
        );

        let run_open_seq = ctx.events.emit(
            "run-0",
            EventKind::RunStarted(RunStartedPayload {
                extra: MetadataMap::new(),
            }),
            None,
        );

        // ---- initial conversation ----
        let mut messages: Vec<Message> = Vec::new();
        let user_text = build_user_text(&task);
        messages.push(Message::user_text(user_text));

        let tools_specs = ctx.tools.specs().to_vec();
        let mut usage_totals = ResourceUsage::default();
        let mut per_model: std::collections::HashMap<oharness_core::ModelId, ResourceUsage> =
            std::collections::HashMap::new();

        let mut termination: Option<Termination> = None;
        let mut turn_index: u32 = 0;
        while termination.is_none() {
            if turn_index >= ctx.max_turns {
                termination = Some(Termination::Truncated {
                    limit: TruncationLimit::MaxTurns(ctx.max_turns),
                });
                break;
            }
            if ctx.cancellation.is_cancelled() {
                termination = Some(Termination::Interrupted {
                    reason: oharness_core::InterruptionReason::Cancellation,
                });
                break;
            }

            let turn_span = format!("turn-{turn_index}");
            let turn_open_seq = ctx.events.emit(
                &turn_span,
                EventKind::TurnStarted(TurnPayload { turn_index }),
                Some(run_open_seq),
            );

            // ---- memory policy transform ----
            let mem_ctx = MemoryContext {
                events: ctx.events.clone(),
                token_budget: capabilities.max_context_tokens,
            };
            let transformed = match ctx
                .memory
                .transform(ConversationView::new(&messages), &mem_ctx)
                .await
            {
                Ok(m) => m,
                Err(e) => {
                    termination = Some(Termination::Failed {
                        error: RunError {
                            category: RunErrorCategory::Memory,
                            message: e.to_string(),
                        },
                        at_turn: turn_index,
                    });
                    break;
                }
            };

            // ---- budget pre-check ----
            let pre_budget = ctx
                .budget
                .check(BudgetRequest {
                    estimated_input_tokens: Some(
                        ConversationView::new(&transformed).token_estimate() as u64,
                    ),
                    ..Default::default()
                })
                .await;
            if let oharness_core::BudgetDecision::Deny { reason } = pre_budget {
                ctx.events.emit(
                    &turn_span,
                    EventKind::BudgetExceeded(json!({"reason": reason})),
                    Some(turn_open_seq),
                );
                termination = Some(Termination::Truncated {
                    limit: TruncationLimit::Budget(reason),
                });
                break;
            }

            // ---- build + issue LLM call (llm.* events are emitted by
            //      RequestTracer wrapping ctx.llm) ----
            let mut req = CompletionRequest::new(transformed);
            req.tools = tools_specs.clone();
            req.system = self.system_prompt.clone();

            let response =
                match complete_with_optional_streaming(ctx, req, capabilities.streaming).await {
                    Ok(r) => r,
                    Err(e) => {
                        termination = Some(Termination::Failed {
                            error: RunError {
                                category: RunErrorCategory::Llm,
                                message: e.to_string(),
                            },
                            at_turn: turn_index,
                        });
                        break;
                    }
                };

            // ---- consume budget + update totals ----
            usage_totals.add_usage(&response.usage);
            per_model
                .entry(response.model.clone())
                .or_default()
                .add_usage(&response.usage);

            ctx.budget
                .consume(oharness_core::BudgetAmount {
                    tokens_input: response.usage.tokens_input,
                    tokens_output: response.usage.tokens_output,
                    cost_usd: 0.0,
                    wall_clock: std::time::Duration::ZERO,
                    steps: 1,
                })
                .await;

            // ---- append assistant message ----
            let mut assistant_msg = Message::Assistant {
                content: response.content.clone(),
                stop_reason: Some(response.stop_reason.clone()),
                meta: MetadataMap::new(),
            };
            let mut effective_stop = response.stop_reason.clone();
            let mut effective_usage = response.usage.clone();
            messages.push(assistant_msg.clone());

            // ---- critic invocation (plan §11) ----
            // Only AfterAssistant is wired for M2 part 2; other triggers
            // (AfterToolResult, AfterEveryNTurns, OnDemand) are deferred.
            if matches!(ctx.critic_trigger, CriticTrigger::AfterAssistant) {
                if let Some(critic) = &ctx.critics {
                    match run_critic_after_assistant(
                        critic.as_ref(),
                        &task,
                        &messages,
                        turn_index,
                        &assistant_msg,
                        &effective_usage,
                        &effective_stop,
                        &turn_span,
                        turn_open_seq,
                        ctx,
                    )
                    .await
                    {
                        CriticOutcome::Continue {
                            effective_message,
                            effective_usage: new_usage,
                            effective_stop: new_stop,
                        } => {
                            // Swap the in-history assistant message with the
                            // (possibly revised) final version.
                            if let Some(last) = messages.last_mut() {
                                *last = effective_message.clone();
                            }
                            assistant_msg = effective_message;
                            effective_usage = new_usage;
                            effective_stop = new_stop;
                        }
                        CriticOutcome::Terminate { error } => {
                            termination = Some(Termination::Failed {
                                error,
                                at_turn: turn_index,
                            });
                            break;
                        }
                    }
                }
            }
            let _ = assistant_msg; // silence unused-assignment warning when no revision

            // ---- tool execution if any ----
            // Use the *effective* response — a revise verdict may have
            // swapped the tool_use blocks the agent actually intended.
            let effective_response = CompletionResponse {
                id: response.id.clone(),
                model: response.model.clone(),
                content: extract_content_from_message(messages.last()),
                stop_reason: effective_stop.clone(),
                usage: effective_usage.clone(),
            };
            let tool_calls_in_turn =
                execute_tool_calls(&effective_response, ctx, &mut messages).await;

            // ---- turn.finished ----
            ctx.events.emit(
                &turn_span,
                EventKind::TurnFinished(TurnFinishedPayload {
                    turn_index,
                    stop_reason: effective_stop.clone(),
                    usage: effective_usage.clone(),
                    tool_calls: tool_calls_in_turn,
                }),
                Some(turn_open_seq),
            );

            usage_totals.turns += 1;
            usage_totals.tool_calls += tool_calls_in_turn;

            // ---- termination decision ----
            match effective_stop {
                StopReason::EndTurn => {
                    termination = Some(Termination::Completed {
                        reason: oharness_core::CompletionReason::EndTurn,
                    });
                }
                StopReason::StopSequence(s) => {
                    termination = Some(Termination::Completed {
                        reason: oharness_core::CompletionReason::StopSequence(s),
                    });
                }
                StopReason::MaxTokens => {
                    termination = Some(Termination::Truncated {
                        limit: TruncationLimit::MaxTokens,
                    });
                }
                StopReason::Refusal => {
                    termination = Some(Termination::Completed {
                        reason: oharness_core::CompletionReason::EndTurn,
                    });
                }
                StopReason::ToolUse => {
                    // Continue to next turn; tool_results already appended.
                    turn_index += 1;
                    continue;
                }
                StopReason::Error(e) => {
                    termination = Some(Termination::Failed {
                        error: RunError {
                            category: RunErrorCategory::Llm,
                            message: e,
                        },
                        at_turn: turn_index,
                    });
                }
            }
        }

        let termination = termination.unwrap_or(Termination::Completed {
            reason: oharness_core::CompletionReason::EndTurn,
        });

        let finished_at = OffsetDateTime::now_utc();
        usage_totals.wall_clock = start_instant.elapsed();

        ctx.events.emit(
            "run-0",
            EventKind::RunFinished(RunFinishedPayload {
                termination: format!("{termination:?}"),
                turns: usage_totals.turns,
                tool_calls: usage_totals.tool_calls,
                extra: MetadataMap::new(),
            }),
            Some(run_open_seq),
        );

        Ok(RunOutcome {
            run_id: ctx.events.run_id(),
            task_id: task.id.clone(),
            termination,
            final_messages: messages,
            // Trajectory reference is supplied by the Agent wrapper; the loop itself
            // doesn't know where events were routed. Agent overwrites this with the
            // correct handle (file path or in-memory snapshot) before returning.
            trajectory: TrajectoryHandle::in_memory(Vec::new()),
            usage: usage_totals,
            per_model_usage: per_model,
            started_at,
            finished_at,
            agent_state: MetadataMap::new(),
        })
    }
}

async fn complete_with_optional_streaming(
    ctx: &LoopContext,
    req: CompletionRequest,
    streaming: bool,
) -> Result<CompletionResponse, oharness_llm::LlmError> {
    if streaming {
        let stream = ctx.llm.stream(req).await?;
        complete_from_stream(stream).await
    } else {
        ctx.llm.complete(req).await
    }
}

/// Result of a single critic cycle for one assistant turn. Produced by
/// [`run_critic_after_assistant`], consumed by the loop body.
///
/// `Continue` always carries the effective assistant state — the same
/// values that went in if the critic accepted first-pass, or the final
/// revision if the critic revised-then-accepted. This lets the loop
/// consume one canonical `(message, usage, stop)` triple regardless of
/// whether a revision happened.
enum CriticOutcome {
    Continue {
        effective_message: Message,
        effective_usage: oharness_core::Usage,
        effective_stop: StopReason,
    },
    Terminate {
        error: RunError,
    },
}

/// Invoke the critic on the freshly-appended assistant turn and resolve
/// the verdict. Re-invokes the critic on each revision up to
/// [`LoopContext::revision_depth_cap`] — once exceeded, the most recent
/// `Revise` verdict is converted to `Reject` per plan §11.1.
#[allow(clippy::too_many_arguments)]
async fn run_critic_after_assistant(
    critic: &oharness_critic::CompositeCritic,
    task: &Task,
    messages: &[Message],
    turn_index: u32,
    initial_message: &Message,
    initial_usage: &oharness_core::Usage,
    initial_stop: &StopReason,
    turn_span: &str,
    turn_open_seq: u64,
    ctx: &LoopContext,
) -> CriticOutcome {
    let mut current_message = initial_message.clone();
    let mut current_usage = initial_usage.clone();
    let mut current_stop = initial_stop.clone();

    for depth in 0..=ctx.revision_depth_cap {
        let original_seq = turn_open_seq;
        let trajectory_tail: Vec<oharness_core::Event> = Vec::new(); // loop does not re-read events
        let turn = AssistantTurn::new(
            turn_index,
            turn_span,
            current_message.clone(),
            current_usage.clone(),
            current_stop.clone(),
        );
        let assess_ctx = AssessmentContext::new(
            task,
            ConversationView::new(messages),
            &turn,
            TrajectoryView::new(&trajectory_tail),
        );

        let verdict = critic.assess(&assess_ctx).await;
        match verdict {
            CriticVerdict::Accept => {
                ctx.events.emit(
                    turn_span,
                    EventKind::CriticAssessed(json!({
                        "critic": critic.name(),
                        "verdict": "accept",
                        "revision_depth": depth,
                    })),
                    Some(turn_open_seq),
                );
                return CriticOutcome::Continue {
                    effective_message: current_message,
                    effective_usage: current_usage,
                    effective_stop: current_stop,
                };
            }
            CriticVerdict::AcceptWithNote(note) => {
                ctx.events.emit(
                    turn_span,
                    EventKind::CriticAssessed(json!({
                        "critic": critic.name(),
                        "verdict": "accept_with_note",
                        "note": note,
                        "revision_depth": depth,
                    })),
                    Some(turn_open_seq),
                );
                return CriticOutcome::Continue {
                    effective_message: current_message,
                    effective_usage: current_usage,
                    effective_stop: current_stop,
                };
            }
            CriticVerdict::Reject { reason } => {
                ctx.events.emit(
                    turn_span,
                    EventKind::CriticRejected(json!({
                        "critic": critic.name(),
                        "reason": reason,
                        "revision_depth": depth,
                    })),
                    Some(turn_open_seq),
                );
                return CriticOutcome::Terminate {
                    error: RunError {
                        category: RunErrorCategory::Critic,
                        message: format!("critic `{}` rejected turn: {reason}", critic.name()),
                    },
                };
            }
            CriticVerdict::Abort { reason } => {
                ctx.events.emit(
                    turn_span,
                    EventKind::CriticRejected(json!({
                        "critic": critic.name(),
                        "reason": reason,
                        "abort": true,
                        "revision_depth": depth,
                    })),
                    Some(turn_open_seq),
                );
                return CriticOutcome::Terminate {
                    error: RunError {
                        category: RunErrorCategory::Critic,
                        message: format!("critic `{}` aborted run: {reason}", critic.name()),
                    },
                };
            }
            CriticVerdict::Revise {
                replacement,
                reason,
            } => {
                if depth >= ctx.revision_depth_cap {
                    // Cap exceeded — convert to Reject per plan §11.1.
                    ctx.events.emit(
                        turn_span,
                        EventKind::CriticRejected(json!({
                            "critic": critic.name(),
                            "reason": format!(
                                "revision depth cap ({}) exceeded: {reason}",
                                ctx.revision_depth_cap
                            ),
                            "revision_depth": depth,
                        })),
                        Some(turn_open_seq),
                    );
                    return CriticOutcome::Terminate {
                        error: RunError {
                            category: RunErrorCategory::Critic,
                            message: format!(
                                "critic `{}`: revision depth cap ({}) exceeded",
                                critic.name(),
                                ctx.revision_depth_cap
                            ),
                        },
                    };
                }
                // Emit critic.revised (critic's declaration) + turn.revised
                // (loop's view of the swap), per plan §11.1.
                let critic_revised_seq = ctx.events.emit(
                    turn_span,
                    EventKind::CriticRevised(json!({
                        "critic": critic.name(),
                        "reason": reason,
                        "revision_depth": depth,
                    })),
                    Some(turn_open_seq),
                );
                ctx.events.emit(
                    turn_span,
                    EventKind::TurnRevised(TurnRevisedPayload {
                        original_seq,
                        replacement_seq: critic_revised_seq,
                        reason,
                    }),
                    Some(turn_open_seq),
                );
                current_message = replacement.message;
                current_usage = replacement.usage;
                current_stop = replacement.stop_reason;
                // Loop again — the revised turn is itself assessed.
                continue;
            }
        }
    }

    // Unreachable: loop returns in every branch except Revise, and Revise
    // checks `depth >= cap` to bail out. Defensive fallback:
    CriticOutcome::Continue {
        effective_message: current_message,
        effective_usage: current_usage,
        effective_stop: current_stop,
    }
}

/// Pull the `Content` vec out of an `Assistant` message. Used to build
/// the post-revision `CompletionResponse` fed into
/// `execute_tool_calls` — the critic may have changed which tool-use
/// blocks the agent issues.
fn extract_content_from_message(msg: Option<&Message>) -> Vec<Content> {
    match msg {
        Some(Message::Assistant { content, .. }) => content.clone(),
        _ => Vec::new(),
    }
}

async fn execute_tool_calls(
    response: &CompletionResponse,
    ctx: &LoopContext,
    messages: &mut Vec<Message>,
) -> u32 {
    let mut results: Vec<Content> = Vec::new();
    let mut count = 0u32;

    for block in &response.content {
        if let Content::ToolUse { id, name, input } = block {
            count += 1;

            // tool.call.* events are emitted by the ToolTracer wrapping
            // ctx.tools. Pass the tool_use_id via ctx.extensions so the
            // tracer can populate its payloads (see tracer::TOOL_USE_ID_KEY).
            let mut extensions = MetadataMap::new();
            extensions.insert(TOOL_USE_ID_KEY.to_string(), json!(id));
            let tool_ctx = ToolContext {
                events: ctx.events.sink().clone(),
                budget: ctx.budget.clone(),
                cancellation: ctx.cancellation.clone(),
                approval: ctx.approval.clone(),
                // Thread the loop's workspace into every tool call so
                // the shipped `fs` / `bash` tools scope to it instead
                // of cwd. Benchmark adapters populate this by building
                // the agent with `.with_workspace(loaded.workspace)`.
                workspace: ctx.workspace.clone(),
                extensions,
            };

            let outcome = ctx.tools.execute(name, input.clone(), &tool_ctx).await;
            match outcome {
                ToolOutcome::Success(output) => {
                    results.push(Content::ToolResult {
                        tool_use_id: id.clone(),
                        output,
                        is_error: false,
                    });
                }
                ToolOutcome::ExecutionError {
                    message,
                    recoverable: _,
                } => {
                    results.push(Content::ToolResult {
                        tool_use_id: id.clone(),
                        output: oharness_core::message::ToolOutput::text(format!(
                            "error: {message}"
                        )),
                        is_error: true,
                    });
                }
                ToolOutcome::Denied { reason } => {
                    results.push(Content::ToolResult {
                        tool_use_id: id.clone(),
                        output: oharness_core::message::ToolOutput::text(format!(
                            "denied: {reason}"
                        )),
                        is_error: true,
                    });
                }
                ToolOutcome::Cancelled => {
                    results.push(Content::ToolResult {
                        tool_use_id: id.clone(),
                        output: oharness_core::message::ToolOutput::text("cancelled"),
                        is_error: true,
                    });
                }
            }
        }
    }

    if !results.is_empty() {
        messages.push(Message::User {
            content: results,
            meta: MetadataMap::new(),
        });
    }
    count
}

fn build_user_text(task: &Task) -> String {
    let mut s = task.instruction.clone();
    for att in &task.attachments {
        s.push_str("\n\n");
        match att {
            oharness_core::Attachment::Text { name, content } => {
                s.push_str(&format!("# attachment: {name}\n{content}"));
            }
            oharness_core::Attachment::File { name, path } => {
                s.push_str(&format!("# attachment: {name} (file: {})", path.display()));
            }
            oharness_core::Attachment::Inline { name, mime, bytes } => {
                s.push_str(&format!(
                    "# attachment: {name} ({mime}, {} bytes)",
                    bytes.len()
                ));
            }
            oharness_core::Attachment::Url { url, .. } => {
                s.push_str(&format!("# attachment: {url}"));
            }
        }
    }
    s
}

const DEFAULT_SYSTEM_PROMPT: &str =
    "You are an agent running inside the open-harness research framework. You have \
     access to the tools listed in the `tools` field. Think step by step, call tools \
     to gather evidence and make changes, and respond with plain text when you've \
     completed the task. Stop calling tools once the task is done.";