example.rs

  1use std::{
  2    error::Error,
  3    fmt::{self, Debug},
  4    path::Path,
  5    sync::{Arc, Mutex},
  6    time::Duration,
  7};
  8
  9use crate::{
 10    ToolMetrics,
 11    assertions::{AssertionsReport, RanAssertion, RanAssertionResult},
 12};
 13use agent::{ContextLoadResult, Thread, ThreadEvent};
 14use anyhow::{Result, anyhow};
 15use async_trait::async_trait;
 16use buffer_diff::DiffHunkStatus;
 17use collections::HashMap;
 18use futures::{FutureExt as _, StreamExt, channel::mpsc, select_biased};
 19use gpui::{App, AppContext, AsyncApp, Entity};
 20use language_model::{LanguageModel, Role, StopReason};
 21
 22pub const THREAD_EVENT_TIMEOUT: Duration = Duration::from_secs(60 * 2);
 23
 24#[async_trait(?Send)]
 25pub trait Example {
 26    fn meta(&self) -> ExampleMetadata;
 27    async fn conversation(&self, cx: &mut ExampleContext) -> Result<()>;
 28    fn diff_assertions(&self) -> Vec<JudgeAssertion> {
 29        Vec::new()
 30    }
 31    fn thread_assertions(&self) -> Vec<JudgeAssertion> {
 32        Vec::new()
 33    }
 34}
 35
 36#[derive(Clone, Debug)]
 37pub struct JudgeAssertion {
 38    pub id: String,
 39    pub description: String,
 40}
 41
 42#[derive(Clone, Debug)]
 43pub struct ExampleMetadata {
 44    pub name: String,
 45    pub url: String,
 46    pub revision: String,
 47    pub language_server: Option<LanguageServer>,
 48    pub max_assertions: Option<usize>,
 49}
 50
 51#[derive(Clone, Debug)]
 52pub struct LanguageServer {
 53    pub file_extension: String,
 54    pub allow_preexisting_diagnostics: bool,
 55}
 56
 57impl ExampleMetadata {
 58    pub fn repo_name(&self) -> String {
 59        self.url
 60            .split('/')
 61            .next_back()
 62            .unwrap_or(&"")
 63            .trim_end_matches(".git")
 64            .into()
 65    }
 66}
 67
 68pub struct FailedAssertion(pub String);
 69
 70impl fmt::Debug for FailedAssertion {
 71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 72        write!(f, "Assertion failure: {}", self.0)
 73    }
 74}
 75
 76impl fmt::Display for FailedAssertion {
 77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 78        write!(f, "{}", self.0)
 79    }
 80}
 81
 82impl Error for FailedAssertion {}
 83
 84pub struct ExampleContext {
 85    meta: ExampleMetadata,
 86    log_prefix: String,
 87    agent_thread: Entity<agent::Thread>,
 88    app: AsyncApp,
 89    model: Arc<dyn LanguageModel>,
 90    pub assertions: AssertionsReport,
 91    pub tool_metrics: Arc<Mutex<ToolMetrics>>,
 92}
 93
 94impl ExampleContext {
 95    pub fn new(
 96        meta: ExampleMetadata,
 97        log_prefix: String,
 98        agent_thread: Entity<agent::Thread>,
 99        model: Arc<dyn LanguageModel>,
100        app: AsyncApp,
101    ) -> Self {
102        let assertions = AssertionsReport::new(meta.max_assertions);
103
104        Self {
105            meta,
106            log_prefix,
107            agent_thread,
108            assertions,
109            model,
110            app,
111            tool_metrics: Arc::new(Mutex::new(ToolMetrics::default())),
112        }
113    }
114
115    pub fn push_user_message(&mut self, text: impl ToString) {
116        self.app
117            .update_entity(&self.agent_thread, |thread, cx| {
118                thread.insert_user_message(
119                    text.to_string(),
120                    ContextLoadResult::default(),
121                    None,
122                    cx,
123                );
124            })
125            .unwrap();
126    }
127
128    pub fn assert(&mut self, expected: bool, message: impl ToString) -> Result<()> {
129        let message = message.to_string();
130        self.log_assertion(
131            if expected {
132                Ok(())
133            } else {
134                Err(anyhow::Error::from(FailedAssertion(message.clone())))
135            },
136            message,
137        )
138    }
139
140    pub fn assert_some<T>(&mut self, option: Option<T>, message: impl ToString) -> Result<T> {
141        let message = message.to_string();
142        self.log_assertion(
143            match option {
144                Some(value) => Ok(value),
145                None => Err(anyhow::Error::from(FailedAssertion(message.clone()))),
146            },
147            message,
148        )
149    }
150
151    #[allow(dead_code)]
152    pub fn assert_eq<T: PartialEq + Debug>(
153        &mut self,
154        left: T,
155        right: T,
156        message: impl ToString,
157    ) -> Result<()> {
158        let message = message.to_string();
159        self.log_assertion(
160            if left == right {
161                Ok(())
162            } else {
163                println!("{}{:#?} != {:#?}", self.log_prefix, left, right);
164                Err(anyhow::Error::from(FailedAssertion(message.clone())))
165            },
166            message,
167        )
168    }
169
170    fn log_assertion<T>(&mut self, result: Result<T>, message: String) -> Result<T> {
171        if let Some(max) = self.meta.max_assertions {
172            if self.assertions.run_count() > max {
173                return Err(anyhow!(
174                    "More assertions were run than the stated max_assertions of {}",
175                    max
176                ));
177            }
178        }
179
180        self.assertions.ran.push(RanAssertion {
181            id: message.clone(),
182            result: Ok(RanAssertionResult {
183                analysis: None,
184                passed: result.is_ok(),
185            }),
186        });
187
188        if result.is_ok() {
189            println!("{}{}", self.log_prefix, message);
190        } else {
191            println!("{}{}", self.log_prefix, message);
192        }
193
194        result
195    }
196
197    pub async fn run_to_end(&mut self) -> Result<Response> {
198        self.run_turns(u32::MAX).await
199    }
200
201    pub async fn run_turn(&mut self) -> Result<Response> {
202        self.run_turns(1).await
203    }
204
205    pub async fn run_turns(&mut self, iterations: u32) -> Result<Response> {
206        let (mut tx, mut rx) = mpsc::channel(1);
207
208        let tool_metrics = self.tool_metrics.clone();
209        let log_prefix = self.log_prefix.clone();
210        let _subscription = self.app.subscribe(
211            &self.agent_thread,
212            move |thread, event: &ThreadEvent, cx| match event {
213                ThreadEvent::ShowError(thread_error) => {
214                    tx.try_send(Err(anyhow!(thread_error.clone()))).ok();
215                }
216                ThreadEvent::Stopped(reason) => match reason {
217                    Ok(StopReason::EndTurn) => {
218                        tx.close_channel();
219                    }
220                    Ok(StopReason::ToolUse) => {
221                        if thread.read(cx).remaining_turns() == 0 {
222                            tx.close_channel();
223                        }
224                    }
225                    Ok(StopReason::MaxTokens) => {
226                        tx.try_send(Err(anyhow!("Exceeded maximum tokens"))).ok();
227                    }
228                    Err(err) => {
229                        tx.try_send(Err(anyhow!(err.clone()))).ok();
230                    }
231                },
232                ThreadEvent::StreamedAssistantText(_, _)
233                | ThreadEvent::StreamedAssistantThinking(_, _)
234                | ThreadEvent::UsePendingTools { .. } => {}
235                ThreadEvent::ToolFinished {
236                    tool_use_id,
237                    pending_tool_use,
238                    ..
239                } => {
240                    thread.update(cx, |thread, _cx| {
241                        if let Some(tool_use) = pending_tool_use {
242                            let mut tool_metrics = tool_metrics.lock().unwrap();
243                            if let Some(tool_result) = thread.tool_result(&tool_use_id) {
244                                let message = if tool_result.is_error {
245                                    format!("✖︎ {}", tool_use.name)
246                                } else {
247                                    format!("✔︎ {}", tool_use.name)
248                                };
249                                println!("{log_prefix}{message}");
250                                tool_metrics
251                                    .insert(tool_result.tool_name.clone(), !tool_result.is_error);
252                            } else {
253                                let message =
254                                    format!("TOOL FINISHED WITHOUT RESULT: {}", tool_use.name);
255                                println!("{log_prefix}{message}");
256                                tool_metrics.insert(tool_use.name.clone(), true);
257                            }
258                        }
259                    });
260                }
261                ThreadEvent::InvalidToolInput { .. } => {
262                    println!("{log_prefix} invalid tool input");
263                }
264                ThreadEvent::ToolConfirmationNeeded => {
265                    panic!(
266                        "{}Bug: Tool confirmation should not be required in eval",
267                        log_prefix
268                    );
269                }
270                ThreadEvent::StreamedCompletion
271                | ThreadEvent::MessageAdded(_)
272                | ThreadEvent::MessageEdited(_)
273                | ThreadEvent::MessageDeleted(_)
274                | ThreadEvent::SummaryChanged
275                | ThreadEvent::SummaryGenerated
276                | ThreadEvent::ReceivedTextChunk
277                | ThreadEvent::StreamedToolUse { .. }
278                | ThreadEvent::CheckpointChanged
279                | ThreadEvent::UsageUpdated(_)
280                | ThreadEvent::CancelEditing => {
281                    tx.try_send(Ok(())).ok();
282                    if std::env::var("ZED_EVAL_DEBUG").is_ok() {
283                        println!("{}Event: {:#?}", log_prefix, event);
284                    }
285                }
286            },
287        );
288
289        let model = self.model.clone();
290
291        let message_count_before = self.app.update_entity(&self.agent_thread, |thread, cx| {
292            thread.set_remaining_turns(iterations);
293            thread.send_to_model(model, None, cx);
294            thread.messages().len()
295        })?;
296
297        loop {
298            select_biased! {
299                result = rx.next() => {
300                    if let Some(result) = result {
301                        result?;
302                    } else {
303                        break;
304                    }
305                }
306                _ = self.app.background_executor().timer(THREAD_EVENT_TIMEOUT).fuse() => {
307                    return Err(anyhow!("Agentic loop stalled - waited {:?} without any events", THREAD_EVENT_TIMEOUT));
308                }
309            }
310        }
311
312        let messages = self.app.read_entity(&self.agent_thread, |thread, cx| {
313            let mut messages = Vec::new();
314            for message in thread.messages().skip(message_count_before) {
315                messages.push(Message {
316                    _role: message.role,
317                    text: message.to_string(),
318                    tool_use: thread
319                        .tool_uses_for_message(message.id, cx)
320                        .into_iter()
321                        .map(|tool_use| ToolUse {
322                            name: tool_use.name.to_string(),
323                            value: tool_use.input,
324                        })
325                        .collect(),
326                });
327            }
328            messages
329        })?;
330
331        let response = Response::new(messages);
332
333        Ok(response)
334    }
335
336    pub fn edits(&self) -> HashMap<Arc<Path>, FileEdits> {
337        self.app
338            .read_entity(&self.agent_thread, |thread, cx| {
339                let action_log = thread.action_log().read(cx);
340                HashMap::from_iter(action_log.changed_buffers(cx).into_iter().map(
341                    |(buffer, diff)| {
342                        let snapshot = buffer.read(cx).snapshot();
343
344                        let file = snapshot.file().unwrap();
345                        let diff = diff.read(cx);
346                        let base_text = diff.base_text().text();
347
348                        let hunks = diff
349                            .hunks(&snapshot, cx)
350                            .map(|hunk| FileEditHunk {
351                                base_text: base_text[hunk.diff_base_byte_range.clone()].to_string(),
352                                text: snapshot
353                                    .text_for_range(hunk.range.clone())
354                                    .collect::<String>(),
355                                status: hunk.status(),
356                            })
357                            .collect();
358
359                        (file.path().clone(), FileEdits { hunks })
360                    },
361                ))
362            })
363            .unwrap()
364    }
365
366    pub fn agent_thread(&self) -> Entity<Thread> {
367        self.agent_thread.clone()
368    }
369}
370
371impl AppContext for ExampleContext {
372    type Result<T> = anyhow::Result<T>;
373
374    fn new<T: 'static>(
375        &mut self,
376        build_entity: impl FnOnce(&mut gpui::Context<T>) -> T,
377    ) -> Self::Result<Entity<T>> {
378        self.app.new(build_entity)
379    }
380
381    fn reserve_entity<T: 'static>(&mut self) -> Self::Result<gpui::Reservation<T>> {
382        self.app.reserve_entity()
383    }
384
385    fn insert_entity<T: 'static>(
386        &mut self,
387        reservation: gpui::Reservation<T>,
388        build_entity: impl FnOnce(&mut gpui::Context<T>) -> T,
389    ) -> Self::Result<Entity<T>> {
390        self.app.insert_entity(reservation, build_entity)
391    }
392
393    fn update_entity<T, R>(
394        &mut self,
395        handle: &Entity<T>,
396        update: impl FnOnce(&mut T, &mut gpui::Context<T>) -> R,
397    ) -> Self::Result<R>
398    where
399        T: 'static,
400    {
401        self.app.update_entity(handle, update)
402    }
403
404    fn read_entity<T, R>(
405        &self,
406        handle: &Entity<T>,
407        read: impl FnOnce(&T, &App) -> R,
408    ) -> Self::Result<R>
409    where
410        T: 'static,
411    {
412        self.app.read_entity(handle, read)
413    }
414
415    fn update_window<T, F>(&mut self, window: gpui::AnyWindowHandle, f: F) -> Result<T>
416    where
417        F: FnOnce(gpui::AnyView, &mut gpui::Window, &mut App) -> T,
418    {
419        self.app.update_window(window, f)
420    }
421
422    fn read_window<T, R>(
423        &self,
424        window: &gpui::WindowHandle<T>,
425        read: impl FnOnce(Entity<T>, &App) -> R,
426    ) -> Result<R>
427    where
428        T: 'static,
429    {
430        self.app.read_window(window, read)
431    }
432
433    fn background_spawn<R>(
434        &self,
435        future: impl std::future::Future<Output = R> + Send + 'static,
436    ) -> gpui::Task<R>
437    where
438        R: Send + 'static,
439    {
440        self.app.background_spawn(future)
441    }
442
443    fn read_global<G, R>(&self, callback: impl FnOnce(&G, &App) -> R) -> Self::Result<R>
444    where
445        G: gpui::Global,
446    {
447        self.app.read_global(callback)
448    }
449}
450
451#[derive(Debug)]
452pub struct Response {
453    messages: Vec<Message>,
454}
455
456impl Response {
457    pub fn new(messages: Vec<Message>) -> Self {
458        Self { messages }
459    }
460
461    pub fn expect_tool(
462        &self,
463        tool_name: &'static str,
464        cx: &mut ExampleContext,
465    ) -> Result<&ToolUse> {
466        let result = self.messages.iter().find_map(|msg| {
467            msg.tool_use
468                .iter()
469                .find(|tool_use| tool_use.name == tool_name)
470        });
471        cx.assert_some(result, format!("called `{}`", tool_name))
472    }
473
474    #[allow(dead_code)]
475    pub fn tool_uses(&self) -> impl Iterator<Item = &ToolUse> {
476        self.messages.iter().flat_map(|msg| &msg.tool_use)
477    }
478
479    pub fn texts(&self) -> impl Iterator<Item = String> {
480        self.messages.iter().map(|message| message.text.clone())
481    }
482}
483
484#[derive(Debug)]
485pub struct Message {
486    _role: Role,
487    text: String,
488    tool_use: Vec<ToolUse>,
489}
490
491#[derive(Debug)]
492pub struct ToolUse {
493    pub name: String,
494    value: serde_json::Value,
495}
496
497impl ToolUse {
498    pub fn parse_input<Input>(&self) -> Result<Input>
499    where
500        Input: for<'de> serde::Deserialize<'de>,
501    {
502        serde_json::from_value::<Input>(self.value.clone()).map_err(|err| anyhow!(err))
503    }
504}
505
506#[derive(Debug)]
507pub struct FileEdits {
508    hunks: Vec<FileEditHunk>,
509}
510
511#[derive(Debug)]
512struct FileEditHunk {
513    base_text: String,
514    text: String,
515    status: DiffHunkStatus,
516}
517
518impl FileEdits {
519    pub fn has_added_line(&self, line: &str) -> bool {
520        self.hunks.iter().any(|hunk| {
521            hunk.status == DiffHunkStatus::added_none()
522                && hunk.base_text.is_empty()
523                && hunk.text.contains(line)
524        })
525    }
526}