Skip to main content

public_07_archive_dereference/
07_archive_dereference.rs

1#[path = "../support/utils.rs"]
2mod _utils;
3
4use _utils::{boxed_error, cleanup_run, create_client, new_run_id, print_summary, require};
5use mubit_sdk::{ArchiveOptions, DereferenceOptions, TransportMode};
6use serde_json::json;
7use std::error::Error;
8use std::time::Instant;
9
10#[tokio::main(flavor = "current_thread")]
11async fn main() -> Result<(), Box<dyn Error>> {
12    let name = "public_07_archive_dereference";
13    let started = Instant::now();
14    let client = create_client().await?;
15    let run_id = new_run_id("public_07_archive_dereference");
16    client.set_run_id(Some(run_id.clone()));
17    client.set_transport(TransportMode::Http);
18
19    let mut passed = true;
20    let mut detail = "validated helper archive + dereference flow".to_string();
21    let mut metrics = json!({});
22
23    let scenario = async {
24        let content =
25            "AssertionError: expected queryset ordering to preserve annotated alias.";
26        let mut archive = ArchiveOptions::new(content, "failing_test_summary");
27        archive.run_id = Some(run_id.clone());
28        archive.agent_id = Some("solver".to_string());
29        archive.source_attempt_id = Some("attempt-1".to_string());
30        archive.source_tool = Some("pytest".to_string());
31        archive.labels = vec!["django".to_string(), "query".to_string()];
32        archive.family = Some("patch-repair".to_string());
33        archive.metadata = Some(json!({ "source": "rust-helper-example" }));
34
35        let archived = client.archive(archive).await?;
36        let reference_id = archived
37            .get("reference_id")
38            .and_then(|value| value.as_str())
39            .ok_or_else(|| boxed_error(format!("archive reference missing: {archived}")))?
40            .to_string();
41
42        let mut dereference = DereferenceOptions::new(reference_id.clone());
43        dereference.run_id = Some(run_id.clone());
44        dereference.agent_id = Some("solver".to_string());
45        let exact = client.dereference(dereference).await?;
46
47        require(
48            archived
49                .get("success")
50                .and_then(|value| value.as_bool())
51                .unwrap_or(false),
52            format!("archive failed: {archived}"),
53        )?;
54        require(
55            exact.get("found")
56                .and_then(|value| value.as_bool())
57                .unwrap_or(false),
58            format!("dereference failed: {exact}"),
59        )?;
60        require(
61            exact.get("evidence")
62                .and_then(|value| value.get("content"))
63                .and_then(|value| value.as_str())
64                == Some(content),
65            format!("dereferenced content mismatch: {exact}"),
66        )?;
67
68        metrics = json!({
69            "run_id": run_id,
70            "reference_id": reference_id,
71            "entry_type": archived.get("entry_type").cloned().unwrap_or(serde_json::Value::Null),
72        });
73
74        Ok::<(), Box<dyn Error>>(())
75    }
76    .await;
77
78    if let Err(err) = scenario {
79        passed = false;
80        detail = err.to_string();
81    }
82
83    let cleanup_ok = cleanup_run(&client, &run_id).await;
84    if !cleanup_ok {
85        passed = false;
86        detail = format!("{detail} | cleanup failures");
87    }
88
89    print_summary(
90        name,
91        passed,
92        &detail,
93        &metrics,
94        started.elapsed().as_secs_f64(),
95        cleanup_ok,
96    );
97
98    if passed {
99        Ok(())
100    } else {
101        Err(boxed_error(detail))
102    }
103}