selfware 0.6.1

Your personal AI workshop — software you own, software that lasts
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
//! Browser benchmark runner — orchestrates web tasks with LLM-driven evaluation
//! and feeds interaction traces into the consolidation pipeline.

use std::path::PathBuf;
use std::time::Instant;

use anyhow::Result;
use serde::{Deserialize, Serialize};
use tracing::{info, warn};

use super::browser_executor::BrowserTaskExecutor;
use super::recorder::{InteractionTrace, TaskOutcome};
use super::tasks::{self, WebTask};

use crate::api::types::Message;
use crate::bench_harness::config::HarnessConfig;
use crate::bench_harness::report::HarnessReport;
use crate::bench_harness::runner::HarnessRunner;
use crate::bench_harness::task::{BenchTask, KeywordEvaluator};

/// Configuration for the browser benchmark.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrowserBenchConfig {
    /// LLM harness configuration.
    pub harness: HarnessConfig,
    /// Maximum concurrent browser sessions.
    pub max_browser_concurrent: usize,
    /// Directory for screenshots and traces.
    pub output_dir: PathBuf,
    /// Whether to run LLM analysis on captured pages.
    pub llm_analysis: bool,
    /// Whether to store traces for consolidation.
    pub store_traces: bool,
}

impl Default for BrowserBenchConfig {
    fn default() -> Self {
        Self {
            harness: HarnessConfig {
                endpoint: "http://localhost:8000/v1".into(),
                model: "qwen3.5-27b".into(),
                max_concurrent: 16,
                max_tokens: 1024,
                temperature: 0.3,
                timeout_secs: 120,
                output_dir: "bench_results/browser".into(),
                extra_body: serde_json::json!({
                    "chat_template_kwargs": {"enable_thinking": false}
                }),
                ..Default::default()
            },
            max_browser_concurrent: 4, // Browser sessions are heavier than LLM calls
            output_dir: "bench_results/browser".into(),
            llm_analysis: true,
            store_traces: true,
        }
    }
}

/// Results from a browser benchmark run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrowserBenchReport {
    /// Timestamp of the run.
    pub timestamp: String,
    /// Total web tasks executed.
    pub tasks_total: usize,
    /// Tasks that passed.
    pub tasks_passed: usize,
    /// Interaction traces from execution.
    pub traces: Vec<InteractionTrace>,
    /// LLM analysis report (if llm_analysis was enabled).
    pub llm_report: Option<HarnessReport>,
    /// Total duration in seconds.
    pub total_duration_secs: f64,
}

impl BrowserBenchReport {
    pub fn to_json(&self) -> serde_json::Result<String> {
        serde_json::to_string_pretty(self)
    }

    pub fn to_markdown(&self) -> String {
        let mut md = String::new();
        md.push_str("# Browser Benchmark Report\n\n");
        md.push_str(&format!(
            "**Date**: {} | **Duration**: {:.1}s\n\n",
            self.timestamp, self.total_duration_secs
        ));

        md.push_str("## Web Task Results\n\n");
        md.push_str("| Task | Status | Actions | Duration |\n");
        md.push_str("|------|--------|---------|----------|\n");
        for trace in &self.traces {
            let status = match &trace.final_outcome {
                TaskOutcome::Passed => "PASS",
                TaskOutcome::Failed { .. } => "FAIL",
                TaskOutcome::Timeout => "TIMEOUT",
                TaskOutcome::Error { .. } => "ERROR",
            };
            md.push_str(&format!(
                "| {} | {} | {} | {:.1}s |\n",
                trace.task_id,
                status,
                trace.actions.len(),
                trace.total_duration_ms as f64 / 1000.0,
            ));
        }

        if let Some(llm) = &self.llm_report {
            md.push_str("\n## LLM Analysis\n\n");
            md.push_str("| Metric | Value |\n|--------|-------|\n");
            md.push_str(&format!("| Tasks analyzed | {} |\n", llm.tasks_total));
            md.push_str(&format!("| Avg score | {:.0}% |\n", llm.avg_score * 100.0));
            md.push_str(&format!(
                "| Throughput | {:.0} tok/s |\n",
                llm.tokens_per_sec
            ));
        }

        md
    }

    pub fn write_to_dir(&self, dir: &std::path::Path) -> Result<()> {
        std::fs::create_dir_all(dir)?;
        std::fs::write(dir.join("browser_bench_report.json"), self.to_json()?)?;
        std::fs::write(dir.join("browser_bench_report.md"), self.to_markdown())?;

        // Write individual traces
        let traces_dir = dir.join("traces");
        std::fs::create_dir_all(&traces_dir)?;
        for trace in &self.traces {
            let path = traces_dir.join(format!("{}.json", trace.task_id));
            std::fs::write(&path, serde_json::to_string_pretty(trace)?)?;
        }

        Ok(())
    }
}

/// Runs browser benchmarks: executes web tasks, optionally analyzes with LLM.
pub struct BrowserBenchRunner {
    config: BrowserBenchConfig,
    executor: BrowserTaskExecutor,
}

impl BrowserBenchRunner {
    pub fn new(config: BrowserBenchConfig) -> Result<Self> {
        let screenshots = config.output_dir.join("screenshots");
        let executor = BrowserTaskExecutor::new(screenshots)?;
        Ok(Self { config, executor })
    }

    /// Run all predefined web task scenarios.
    pub async fn run_all(&self) -> Result<BrowserBenchReport> {
        let scenarios = tasks::all_scenarios();
        self.run_tasks(&scenarios).await
    }

    /// Run specific web tasks.
    pub async fn run_tasks(&self, tasks: &[WebTask]) -> Result<BrowserBenchReport> {
        let start = Instant::now();

        info!(
            "Starting browser benchmark: {} tasks, {} browser concurrent, {} LLM concurrent",
            tasks.len(),
            self.config.max_browser_concurrent,
            self.config.harness.max_concurrent,
        );

        // Phase 1: Execute web tasks and capture traces
        eprintln!("\n--- Phase 1: Executing web tasks ---");
        let traces = self
            .executor
            .execute_all(tasks, self.config.max_browser_concurrent)
            .await;

        let tasks_passed = traces
            .iter()
            .filter(|t| matches!(t.final_outcome, TaskOutcome::Passed))
            .count();

        for trace in &traces {
            let status = match &trace.final_outcome {
                TaskOutcome::Passed => "PASS",
                TaskOutcome::Failed { reasons } => &format!("FAIL: {}", reasons.join("; ")),
                TaskOutcome::Timeout => "TIMEOUT",
                TaskOutcome::Error { message } => message,
            };
            eprintln!(
                "  {}{} | {} actions | {:.1}s",
                trace.task_id,
                status,
                trace.actions.len(),
                trace.total_duration_ms as f64 / 1000.0,
            );
        }

        eprintln!("\nWeb tasks: {}/{} passed", tasks_passed, traces.len(),);

        // Phase 2: LLM analysis of captured page content
        let llm_report = if self.config.llm_analysis {
            eprintln!("\n--- Phase 2: LLM analysis of captured content ---");
            match self.run_llm_analysis(&traces).await {
                Ok(report) => {
                    eprintln!(
                        "LLM analysis: {}/{} passed | {:.0} tok/s | avg score {:.0}%",
                        report.tasks_passed,
                        report.tasks_total,
                        report.tokens_per_sec,
                        report.avg_score * 100.0,
                    );
                    Some(report)
                }
                Err(e) => {
                    warn!("LLM analysis failed: {e}");
                    None
                }
            }
        } else {
            None
        };

        // Phase 3: Store traces for consolidation
        if self.config.store_traces {
            let traces_dir = self.config.output_dir.join("traces");
            std::fs::create_dir_all(&traces_dir)?;
            for trace in &traces {
                let path = traces_dir.join(format!("{}.json", trace.task_id));
                std::fs::write(&path, serde_json::to_string_pretty(trace)?)?;
            }
            eprintln!("\nTraces saved to {}", traces_dir.display(),);
        }

        let report = BrowserBenchReport {
            timestamp: chrono::Utc::now().to_rfc3339(),
            tasks_total: traces.len(),
            tasks_passed,
            traces,
            llm_report,
            total_duration_secs: start.elapsed().as_secs_f64(),
        };

        info!(
            "Browser benchmark complete: {}/{} passed in {:.1}s",
            report.tasks_passed, report.tasks_total, report.total_duration_secs,
        );

        Ok(report)
    }

    /// Run LLM analysis on the captured page content from traces.
    ///
    /// Creates analysis tasks from the captured HTML content and sends them
    /// through the 16-concurrent-stream harness.
    async fn run_llm_analysis(&self, traces: &[InteractionTrace]) -> Result<HarnessReport> {
        let runner = HarnessRunner::new(self.config.harness.clone())?;

        // Build analysis tasks from traces
        let mut bench_tasks = Vec::new();
        for trace in traces {
            // Gather extracted content from successful actions
            let mut page_summaries = Vec::new();
            for action in &trace.actions {
                if let super::recorder::ActionOutcome::Success { output } = &action.outcome {
                    if output.len() > 20 {
                        page_summaries.push(output.clone());
                    }
                }
            }

            // Read any saved TEXT content from screenshots. Real screenshots are
            // now PNG images (browser backend); they are not text and must not be
            // fed to the text-based analyzer.
            for ss in &trace.screenshots {
                if is_image_path(&ss.path) {
                    continue;
                }
                if ss.path.exists() {
                    if let Ok(content) = std::fs::read_to_string(&ss.path) {
                        // Strip HTML tags and truncate to keep prompts small
                        let text = strip_html_tags(&content);
                        let truncated = if text.chars().count() > 3000 {
                            format!("{}...[truncated]", truncate_chars(&text, 3000))
                        } else {
                            text
                        };
                        page_summaries.push(truncated);
                    }
                }
            }

            if page_summaries.is_empty() {
                continue;
            }

            let content_summary = page_summaries.join("\n---\n");
            let prompt = format!(
                "Analyze this web page content captured during a browser benchmark task.\n\
                 Task: {} ({})\n\n\
                 Captured content:\n{}\n\n\
                 Provide a JSON response with:\n\
                 - \"summary\": What the page contains (1-2 sentences)\n\
                 - \"data_extracted\": Key data points found (array of strings)\n\
                 - \"navigation_quality\": Score 0-100 for how well the task navigated\n\
                 - \"content_relevance\": Score 0-100 for relevance to the task description\n\
                 \nRespond with only valid JSON.",
                trace.task_name,
                trace.task_id,
                truncate_chars(&content_summary, 6000),
            );

            bench_tasks.push(BenchTask {
                id: format!("llm-{}", trace.task_id),
                description: format!("Analyze {} content", trace.task_name),
                messages: vec![
                    Message::system("You are a web content analyst. Output only valid JSON."),
                    Message::user(prompt),
                ],
                evaluator: Box::new(KeywordEvaluator::new(vec![
                    "summary".into(),
                    "data_extracted".into(),
                ])),
            });
        }

        if bench_tasks.is_empty() {
            // Return empty report
            return Ok(HarnessReport::from_results(
                &self.config.harness,
                vec![],
                0.0,
            ));
        }

        runner.run(bench_tasks).await
    }
}

/// Truncate `s` to at most `max` CHARACTERS, returning a slice that ends on a
/// char boundary. A byte-index slice like `&s[..3000]` panics when the boundary
/// falls inside a multibyte UTF-8 character.
fn truncate_chars(s: &str, max: usize) -> &str {
    match s.char_indices().nth(max) {
        Some((idx, _)) => &s[..idx],
        None => s,
    }
}

/// Strip HTML tags from content, keeping only text.
fn strip_html_tags(html: &str) -> String {
    let mut result = String::with_capacity(html.len() / 2);
    let mut in_tag = false;
    let mut in_script = false;
    let mut in_style = false;
    let mut last_was_space = false;

    let lower = html.to_lowercase();
    let chars: Vec<char> = html.chars().collect();
    let lower_chars: Vec<char> = lower.chars().collect();

    let mut i = 0;
    while i < chars.len() {
        if !in_tag && chars[i] == '<' {
            in_tag = true;
            // Check for script/style tags
            let remaining: String = lower_chars[i..].iter().take(10).collect();
            if remaining.starts_with("<script") {
                in_script = true;
            } else if remaining.starts_with("<style") {
                in_style = true;
            } else if remaining.starts_with("</script") {
                in_script = false;
            } else if remaining.starts_with("</style") {
                in_style = false;
            }
        } else if in_tag && chars[i] == '>' {
            in_tag = false;
        } else if !in_tag && !in_script && !in_style {
            let ch = chars[i];
            if ch.is_whitespace() {
                if !last_was_space {
                    result.push(' ');
                    last_was_space = true;
                }
            } else {
                result.push(ch);
                last_was_space = false;
            }
        }
        i += 1;
    }

    result.trim().to_string()
}

/// Whether a path looks like a binary image (screenshot) that must not be read
/// as text for the text-based LLM analysis.
fn is_image_path(path: &std::path::Path) -> bool {
    matches!(
        path.extension()
            .and_then(|e| e.to_str())
            .map(|e| e.to_ascii_lowercase())
            .as_deref(),
        Some("png" | "jpg" | "jpeg" | "gif" | "webp" | "bmp")
    )
}

#[cfg(test)]
#[path = "../../../tests/unit/bench_harness/computer_control/bench_runner/bench_runner_test.rs"]
mod tests;