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
//! [`UserSimulator`] trait and shipped simulator implementations
//! (plan §12.3, §12.4).
//!
//! A simulator stands in for a human user during a [`ConversationLoop`]
//! run: it produces an initial message from the task, then responds to
//! each assistant turn with either [`UserAction::Say`] (send a follow-up)
//! or [`UserAction::EndConversation`] (terminate the loop).
//!
//! Simulators receive a [`oharness_core::ConversationView`] — not raw
//! messages — so they can call `.user_visible()` to strip internal
//! reasoning / tool calls / tool results and respond only to the
//! human-facing thread. Simulators that want stricter or looser views
//! can compose their own.

use async_trait::async_trait;
use oharness_core::{CompletionRequest, ConversationView, Message, Task};
use oharness_llm::Llm;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

#[async_trait]
pub trait UserSimulator: Send + Sync {
    fn name(&self) -> &str;

    /// Produce the first user message from the task. Called once at the
    /// start of a conversation loop.
    async fn initial_message(&self, task: &Task) -> Result<String, UserError>;

    /// Respond to the current conversation. Return
    /// [`UserAction::Say`] with the next user message, or
    /// [`UserAction::EndConversation`] to terminate the loop.
    async fn respond(
        &self,
        conversation: ConversationView<'_>,
        task: &Task,
    ) -> Result<UserAction, UserError>;
}

/// What the simulator wants to do next.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UserAction {
    Say(String),
    EndConversation,
}

/// Simulator-side errors. Any error here is promoted to
/// `Termination::Failed { reason: "user_simulator_error" }` by the
/// [`ConversationLoop`] — simulators cannot silently fall back to
/// [`UserAction::EndConversation`] since that would hide bugs.
#[derive(Debug, thiserror::Error)]
pub enum UserError {
    #[error("user simulator: {0}")]
    Other(String),
    #[error("user simulator llm: {0}")]
    Llm(#[from] oharness_llm::LlmError),
}

// ======================================================================
// ScriptedUserSimulator (plan §12.4)
// ======================================================================

/// A simulator that replays a fixed sequence of user utterances. The
/// first entry is returned from [`UserSimulator::initial_message`]; each
/// subsequent call to [`UserSimulator::respond`] returns the next entry,
/// [`UserAction::Say`]-wrapped, until the script is exhausted — at which
/// point the simulator returns [`UserAction::EndConversation`].
///
/// Useful for tests, reproducible evaluation runs, and
/// conversation-loop smoke paths where no LLM should be involved on the
/// user side.
pub struct ScriptedUserSimulator {
    script: Vec<String>,
    cursor: AtomicUsize,
    name: String,
}

impl ScriptedUserSimulator {
    pub fn new(script: impl IntoIterator<Item = impl Into<String>>) -> Self {
        let script = script.into_iter().map(Into::into).collect::<Vec<_>>();
        Self {
            script,
            cursor: AtomicUsize::new(0),
            name: "scripted-user".to_string(),
        }
    }

    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }
}

#[async_trait]
impl UserSimulator for ScriptedUserSimulator {
    fn name(&self) -> &str {
        &self.name
    }

    async fn initial_message(&self, task: &Task) -> Result<String, UserError> {
        let idx = self.cursor.fetch_add(1, Ordering::SeqCst);
        self.script
            .get(idx)
            .cloned()
            .ok_or_else(|| UserError::Other(format!("empty script (task={})", task.instruction)))
    }

    async fn respond(
        &self,
        _conversation: ConversationView<'_>,
        _task: &Task,
    ) -> Result<UserAction, UserError> {
        let idx = self.cursor.fetch_add(1, Ordering::SeqCst);
        match self.script.get(idx) {
            Some(msg) => Ok(UserAction::Say(msg.clone())),
            None => Ok(UserAction::EndConversation),
        }
    }
}

// ======================================================================
// LlmUserSimulator (plan §12.4)
// ======================================================================

/// A simulator that drives a user LLM with a persona + template. Each
/// `respond` call builds a [`CompletionRequest`] whose system prompt is
/// the rendered `prompt_template` (with `{persona}` and `{task}`
/// substituted) and whose user message is the serialized conversation
/// so far.
///
/// The simulator looks for a terminal sentinel — by default `<end>` —
/// in the LLM's response to decide whether to return
/// [`UserAction::EndConversation`]. The sentinel is matched
/// case-insensitively and stripped from the surrounding text.
pub struct LlmUserSimulator {
    llm: Arc<dyn Llm>,
    persona: String,
    prompt_template: String,
    end_sentinel: String,
    name: String,
}

impl LlmUserSimulator {
    /// A reasonable default template — users with opinionated
    /// simulators should supply their own.
    pub fn default_template() -> &'static str {
        "You are role-playing a user with this persona:\n\n{persona}\n\n\
         The user's underlying task is:\n\n{task}\n\n\
         Respond to the assistant's most recent turn as the user would. \
         Keep responses short. When the task is fully resolved, include \
         the literal token `<end>` anywhere in your reply to end the \
         conversation. Do not prefix your reply with `USER:` or any role \
         label."
    }

    pub fn new(
        llm: Arc<dyn Llm>,
        persona: impl Into<String>,
        prompt_template: impl Into<String>,
    ) -> Self {
        Self {
            llm,
            persona: persona.into(),
            prompt_template: prompt_template.into(),
            end_sentinel: "<end>".to_string(),
            name: "llm-user".to_string(),
        }
    }

    pub fn with_end_sentinel(mut self, sentinel: impl Into<String>) -> Self {
        self.end_sentinel = sentinel.into();
        self
    }

    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.name = name.into();
        self
    }

    fn render_system(&self, task: &Task) -> String {
        self.prompt_template
            .replace("{persona}", &self.persona)
            .replace("{task}", &task.instruction)
    }
}

#[async_trait]
impl UserSimulator for LlmUserSimulator {
    fn name(&self) -> &str {
        &self.name
    }

    async fn initial_message(&self, task: &Task) -> Result<String, UserError> {
        // Cheap: the task's instruction is already a user-shaped prompt.
        // Callers that want a more elaborate kickoff can subclass /
        // wrap — for the default, we just replay the instruction.
        Ok(task.instruction.clone())
    }

    async fn respond(
        &self,
        conversation: ConversationView<'_>,
        task: &Task,
    ) -> Result<UserAction, UserError> {
        let transcript = render_transcript(conversation);
        let mut req = CompletionRequest::new(vec![Message::user_text(transcript)]);
        req.system = Some(self.render_system(task));
        let res = self.llm.complete(req).await?;
        let text = res
            .content
            .iter()
            .filter_map(|c| match c {
                oharness_core::Content::Text { text } => Some(text.as_str()),
                _ => None,
            })
            .collect::<Vec<_>>()
            .join("\n");
        let text_lower = text.to_ascii_lowercase();
        let sentinel_lower = self.end_sentinel.to_ascii_lowercase();
        if text_lower.contains(&sentinel_lower) {
            // Sentinel present — end the conversation. Any surrounding
            // text is dropped; users who want a trailing message should
            // emit it on a previous turn rather than glue it to `<end>`.
            let _ = strip_case_insensitive(&text, &self.end_sentinel);
            Ok(UserAction::EndConversation)
        } else {
            Ok(UserAction::Say(text))
        }
    }
}

fn render_transcript(view: ConversationView<'_>) -> String {
    let mut out = String::new();
    for m in view.user_visible() {
        match m {
            Message::System { content, .. } => {
                out.push_str("SYSTEM: ");
                out.push_str(&content);
                out.push('\n');
            }
            Message::User { content, .. } => {
                out.push_str("USER: ");
                out.push_str(&flatten_text(&content));
                out.push('\n');
            }
            Message::Assistant { content, .. } => {
                out.push_str("ASSISTANT: ");
                out.push_str(&flatten_text(&content));
                out.push('\n');
            }
        }
    }
    out
}

fn flatten_text(content: &[oharness_core::Content]) -> String {
    content
        .iter()
        .filter_map(|c| match c {
            oharness_core::Content::Text { text } => Some(text.as_str()),
            _ => None,
        })
        .collect::<Vec<_>>()
        .join("\n")
}

fn strip_case_insensitive(haystack: &str, needle: &str) -> String {
    // Case-insensitive single-pass strip. Simple and correct for small
    // inputs; we don't expect the sentinel to appear many times.
    let hl = haystack.to_ascii_lowercase();
    let nl = needle.to_ascii_lowercase();
    let mut out = String::with_capacity(haystack.len());
    let mut i = 0;
    while i < haystack.len() {
        if hl[i..].starts_with(&nl) {
            i += needle.len();
        } else {
            let ch = haystack[i..].chars().next().unwrap();
            out.push(ch);
            i += ch.len_utf8();
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use oharness_core::{
        CompletionResponse, Content, LlmCapabilities, Message, ModelId, StopReason, Task, Usage,
    };
    use oharness_llm::{ChunkStream, LlmError};
    use std::sync::Mutex;

    // ---------- ScriptedUserSimulator ----------

    #[tokio::test]
    async fn scripted_returns_initial_then_sequenced_responses() {
        let sim = ScriptedUserSimulator::new(["hi", "more please", "thanks"]);
        let task = Task::new("chat");
        let first = sim.initial_message(&task).await.unwrap();
        assert_eq!(first, "hi");

        let empty: Vec<Message> = Vec::new();
        let v = ConversationView::new(&empty);
        match sim.respond(v, &task).await.unwrap() {
            UserAction::Say(s) => assert_eq!(s, "more please"),
            other => panic!("expected Say, got {other:?}"),
        }
        let v = ConversationView::new(&empty);
        match sim.respond(v, &task).await.unwrap() {
            UserAction::Say(s) => assert_eq!(s, "thanks"),
            other => panic!("expected Say, got {other:?}"),
        }
        // Exhausted -> EndConversation.
        let v = ConversationView::new(&empty);
        assert_eq!(
            sim.respond(v, &task).await.unwrap(),
            UserAction::EndConversation
        );
    }

    #[tokio::test]
    async fn scripted_empty_script_errors_on_initial_message() {
        let sim: ScriptedUserSimulator = ScriptedUserSimulator::new(std::iter::empty::<String>());
        match sim.initial_message(&Task::new("t")).await {
            Err(UserError::Other(msg)) => assert!(msg.contains("empty script")),
            other => panic!("expected Err(UserError::Other), got {other:?}"),
        }
    }

    // ---------- LlmUserSimulator ----------

    struct OneShot(Mutex<Option<CompletionResponse>>);
    #[async_trait]
    impl Llm for OneShot {
        fn name(&self) -> &str {
            "one-shot-user"
        }
        fn capabilities(&self) -> LlmCapabilities {
            LlmCapabilities::default()
        }
        async fn complete(&self, _req: CompletionRequest) -> Result<CompletionResponse, LlmError> {
            self.0
                .lock()
                .unwrap()
                .take()
                .ok_or(LlmError::Unsupported("one-shot"))
        }
        async fn stream(&self, _req: CompletionRequest) -> Result<ChunkStream, LlmError> {
            Err(LlmError::Unsupported("stream"))
        }
    }

    fn text_response(text: &str) -> CompletionResponse {
        CompletionResponse {
            id: "u".into(),
            model: ModelId::new("m"),
            content: vec![Content::text(text)],
            stop_reason: StopReason::EndTurn,
            usage: Usage::default(),
        }
    }

    #[tokio::test]
    async fn llm_user_initial_message_replays_task_instruction() {
        let llm: Arc<dyn Llm> = Arc::new(OneShot(Mutex::new(None)));
        let sim = LlmUserSimulator::new(llm, "friendly user", LlmUserSimulator::default_template());
        let task = Task::new("help me debug this bug");
        assert_eq!(
            sim.initial_message(&task).await.unwrap(),
            "help me debug this bug"
        );
    }

    #[tokio::test]
    async fn llm_user_respond_emits_say_on_plain_response() {
        let llm: Arc<dyn Llm> =
            Arc::new(OneShot(Mutex::new(Some(text_response("that's helpful")))));
        let sim = LlmUserSimulator::new(llm, "user", LlmUserSimulator::default_template());
        let empty: Vec<Message> = Vec::new();
        match sim
            .respond(ConversationView::new(&empty), &Task::new("t"))
            .await
            .unwrap()
        {
            UserAction::Say(s) => assert_eq!(s, "that's helpful"),
            other => panic!("expected Say, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn llm_user_respond_emits_end_on_sentinel() {
        let llm: Arc<dyn Llm> = Arc::new(OneShot(Mutex::new(Some(text_response("done <end>")))));
        let sim = LlmUserSimulator::new(llm, "user", LlmUserSimulator::default_template());
        let empty: Vec<Message> = Vec::new();
        assert_eq!(
            sim.respond(ConversationView::new(&empty), &Task::new("t"))
                .await
                .unwrap(),
            UserAction::EndConversation
        );
    }

    #[tokio::test]
    async fn llm_user_respond_is_case_insensitive_on_sentinel() {
        let llm: Arc<dyn Llm> = Arc::new(OneShot(Mutex::new(Some(text_response("<END>")))));
        let sim = LlmUserSimulator::new(llm, "user", LlmUserSimulator::default_template());
        let empty: Vec<Message> = Vec::new();
        assert_eq!(
            sim.respond(ConversationView::new(&empty), &Task::new("t"))
                .await
                .unwrap(),
            UserAction::EndConversation
        );
    }

    #[tokio::test]
    async fn llm_user_respond_errors_on_llm_error() {
        // One-shot with no response → first respond errors.
        let llm: Arc<dyn Llm> = Arc::new(OneShot(Mutex::new(None)));
        let sim = LlmUserSimulator::new(llm, "user", LlmUserSimulator::default_template());
        let empty: Vec<Message> = Vec::new();
        match sim
            .respond(ConversationView::new(&empty), &Task::new("t"))
            .await
        {
            Err(UserError::Llm(_)) => {}
            other => panic!("expected Err(UserError::Llm), got {other:?}"),
        }
    }
}