selfware 0.6.2

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
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
//! Smoke tests — quick validation that selfware works end-to-end on a live endpoint.
//!
//! These tests verify core agent capabilities against the actual model:
//! - Simple Q&A (no tools needed)
//! - File operations (create, read, edit)
//! - Project scaffolding (multi-step)
//! - Code generation + verification
//! - Context tools
//!
//! Run with:
//!   cargo test --features integration --test integration smoke_ -- --ignored
//!
//! Requires vLLM running on localhost:8000 with qwen3.5-27b.

use std::path::Path;
use tempfile::TempDir;

async fn smoke_config(work_dir: &Path) -> Option<selfware::config::Config> {
    let endpoint = std::env::var("SELFWARE_ENDPOINT")
        .unwrap_or_else(|_| "http://localhost:8000/v1".to_string());
    let model = std::env::var("SELFWARE_MODEL").unwrap_or_else(|_| "qwen3.5-27b".to_string());

    // Quick check: is the endpoint up and serving our model?
    let client = reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(5))
        .build()
        .ok()?;
    let resp = client
        .get(format!("{}/models", endpoint))
        .send()
        .await
        .ok()?;
    if !resp.status().is_success() {
        return None;
    }
    let body = resp.text().await.ok()?;
    if !body.contains(&model) {
        eprintln!("Model '{}' not found at {}", model, endpoint);
        return None;
    }

    Some(selfware::config::Config {
        endpoint,
        model,
        max_tokens: 65536,
        temperature: 0.7,
        agent: selfware::config::AgentConfig {
            max_iterations: 15,
            step_timeout_secs: 120,
            token_budget: 65536,
            streaming: true,
            native_function_calling: false,
            min_completion_steps: 0,
            require_verification_before_completion: false,
            ..Default::default()
        },
        safety: selfware::config::SafetyConfig {
            allowed_paths: vec![format!("{}/**", work_dir.display()), "./**".to_string()],
            ..Default::default()
        },
        execution_mode: selfware::config::ExecutionMode::Yolo,
        ..Default::default()
    })
}

// ─── Simple Q&A (no tools) ──────────────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires live endpoint"]
async fn smoke_simple_qa() {
    let tmp = TempDir::new().unwrap();
    let config = match smoke_config(tmp.path()).await {
        Some(c) => c,
        None => {
            eprintln!("SKIPPED: endpoint not available");
            return;
        }
    };

    let mut agent = selfware::agent::Agent::new(config).await.unwrap();
    let result = agent
        .run_task("What is 2 + 2? Just answer with the number.")
        .await;

    assert!(
        result.is_ok(),
        "Simple Q&A should complete: {:?}",
        result.err()
    );
}

// ─── File Creation ──────────────────────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires live endpoint"]
async fn smoke_create_file() {
    let tmp = TempDir::new().unwrap();
    let mut config = match smoke_config(tmp.path()).await {
        Some(c) => c,
        None => {
            eprintln!("SKIPPED: endpoint not available");
            return;
        }
    };
    // Allow writing to tmp dir
    config.safety.allowed_paths = vec![format!("{}/**", tmp.path().display()), "./**".to_string()];

    let mut agent = selfware::agent::Agent::new(config).await.unwrap();
    let task = format!(
        "Create a file at {}/hello.txt with the content 'Hello from selfware!'",
        tmp.path().display()
    );
    let result = agent.run_task(&task).await;

    assert!(
        result.is_ok(),
        "File creation should complete: {:?}",
        result.err()
    );

    let file_path = tmp.path().join("hello.txt");
    if file_path.exists() {
        let content = std::fs::read_to_string(&file_path).unwrap();
        assert!(
            content.contains("Hello") || content.contains("selfware"),
            "File should contain expected content, got: {}",
            content
        );
        println!("PASS: File created with content: {}", content.trim());
    } else {
        println!("WARN: File not created at expected path (model may have used different path)");
    }
}

// ─── Read Existing File ─────────────────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires live endpoint"]
async fn smoke_read_file() {
    let tmp = TempDir::new().unwrap();
    // Pre-create a file for the model to read
    let test_file = tmp.path().join("data.txt");
    std::fs::write(&test_file, "The secret number is 42.").unwrap();

    let config = match smoke_config(tmp.path()).await {
        Some(c) => c,
        None => {
            eprintln!("SKIPPED: endpoint not available");
            return;
        }
    };

    let mut agent = selfware::agent::Agent::new(config).await.unwrap();
    let task = format!(
        "Read the file at {} and tell me what the secret number is.",
        test_file.display()
    );
    let result = agent.run_task(&task).await;

    assert!(
        result.is_ok(),
        "File reading should complete: {:?}",
        result.err()
    );
    println!("PASS: Read file task completed");
}

// ─── Rust Project Scaffolding ───────────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires live endpoint"]
async fn smoke_create_rust_project() {
    let tmp = TempDir::new().unwrap();
    let project_dir = tmp.path().join("myapp");

    let mut config = match smoke_config(tmp.path()).await {
        Some(c) => c,
        None => {
            eprintln!("SKIPPED: endpoint not available");
            return;
        }
    };
    config.safety.allowed_paths = vec![format!("{}/**", tmp.path().display()), "./**".to_string()];
    config.agent.max_iterations = 20; // More room for multi-step

    let mut agent = selfware::agent::Agent::new(config).await.unwrap();
    let task = format!(
        "Create a new Rust project at {} using 'cargo init'. \
         Then add a function `fn greet(name: &str) -> String` that returns \
         \"Hello, {{name}}!\" and add a test for it. \
         Run cargo check to verify it compiles.",
        project_dir.display()
    );
    let result = agent.run_task(&task).await;

    // Check if project was created
    let cargo_toml = project_dir.join("Cargo.toml");
    let main_rs = project_dir.join("src/main.rs");

    if cargo_toml.exists() {
        println!("PASS: Cargo.toml created");
    } else {
        println!("WARN: Cargo.toml not found at {}", cargo_toml.display());
    }

    if main_rs.exists() {
        let content = std::fs::read_to_string(&main_rs).unwrap();
        if content.contains("greet") {
            println!("PASS: greet function found in main.rs");
        } else {
            println!("WARN: greet function not found in main.rs");
        }
        if content.contains("#[test]") || content.contains("mod tests") {
            println!("PASS: Tests found in main.rs");
        }
    }

    match result {
        Ok(()) => println!("PASS: Rust project scaffolding completed"),
        Err(e) => println!("PARTIAL: Task ended with error (may be partial): {}", e),
    }
}

// ─── Python Script Generation ───────────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires live endpoint"]
async fn smoke_create_python_script() {
    let tmp = TempDir::new().unwrap();

    let mut config = match smoke_config(tmp.path()).await {
        Some(c) => c,
        None => {
            eprintln!("SKIPPED: endpoint not available");
            return;
        }
    };
    config.safety.allowed_paths = vec![format!("{}/**", tmp.path().display()), "./**".to_string()];

    let mut agent = selfware::agent::Agent::new(config).await.unwrap();
    let task = format!(
        "Create a Python script at {}/fibonacci.py that:\n\
         1. Defines a function fibonacci(n) that returns the nth fibonacci number\n\
         2. Has a main block that prints fibonacci(10)\n\
         3. Includes a docstring",
        tmp.path().display()
    );
    let result = agent.run_task(&task).await;

    let script = tmp.path().join("fibonacci.py");
    if script.exists() {
        let content = std::fs::read_to_string(&script).unwrap();
        assert!(
            content.contains("fibonacci"),
            "Should contain fibonacci function"
        );
        assert!(
            content.contains("def "),
            "Should contain a function definition"
        );
        println!("PASS: Python script created ({} bytes)", content.len());
    } else {
        println!("WARN: Script not created at expected path");
    }

    match result {
        Ok(()) => println!("PASS: Python script generation completed"),
        Err(e) => println!("PARTIAL: {}", e),
    }
}

// ─── Multi-File Edit ────────────────────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires live endpoint"]
async fn smoke_multi_file_edit() {
    let tmp = TempDir::new().unwrap();

    // Pre-create files for the model to edit
    let config_file = tmp.path().join("config.json");
    std::fs::write(
        &config_file,
        r#"{"name": "myapp", "version": "1.0.0", "debug": true}"#,
    )
    .unwrap();

    let readme = tmp.path().join("README.md");
    std::fs::write(&readme, "# MyApp\n\nA simple app.\n").unwrap();

    let mut config = match smoke_config(tmp.path()).await {
        Some(c) => c,
        None => {
            eprintln!("SKIPPED: endpoint not available");
            return;
        }
    };
    config.safety.allowed_paths = vec![format!("{}/**", tmp.path().display()), "./**".to_string()];

    let mut agent = selfware::agent::Agent::new(config).await.unwrap();
    let task = format!(
        "In the directory {}, do these changes:\n\
         1. Read config.json and change \"debug\": true to \"debug\": false\n\
         2. Update README.md to add a '## Configuration' section that mentions the debug flag\n\
         Then verify both files look correct by reading them.",
        tmp.path().display()
    );
    let result = agent.run_task(&task).await;

    // Check config.json was modified
    if config_file.exists() {
        let content = std::fs::read_to_string(&config_file).unwrap();
        if content.contains("false") {
            println!("PASS: config.json debug flag updated");
        } else {
            println!("WARN: config.json not updated as expected: {}", content);
        }
    }

    // Check README was modified
    if readme.exists() {
        let content = std::fs::read_to_string(&readme).unwrap();
        if content.len() > 30 {
            println!("PASS: README.md expanded ({} bytes)", content.len());
        } else {
            println!("WARN: README.md not significantly changed");
        }
    }

    match result {
        Ok(()) => println!("PASS: Multi-file edit completed"),
        Err(e) => println!("PARTIAL: {}", e),
    }
}

// ─── Search and Analyze ─────────────────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires live endpoint"]
async fn smoke_search_codebase() {
    let tmp = TempDir::new().unwrap();
    let config = match smoke_config(tmp.path()).await {
        Some(c) => c,
        None => {
            eprintln!("SKIPPED: endpoint not available");
            return;
        }
    };

    let mut agent = selfware::agent::Agent::new(config).await.unwrap();
    // This task operates on the selfware codebase itself
    let result = agent
        .run_task("Search for 'strip_think_blocks' in the codebase and tell me which files contain it and what it does.")
        .await;

    assert!(
        result.is_ok(),
        "Search task should complete: {:?}",
        result.err()
    );
    println!("PASS: Search and analyze completed");
}

// ─── Shell Command Execution ────────────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires live endpoint"]
async fn smoke_shell_command() {
    let tmp = TempDir::new().unwrap();
    let config = match smoke_config(tmp.path()).await {
        Some(c) => c,
        None => {
            eprintln!("SKIPPED: endpoint not available");
            return;
        }
    };

    let mut agent = selfware::agent::Agent::new(config).await.unwrap();
    let result = agent
        .run_task("Run 'rustc --version' and 'cargo --version' and tell me the versions.")
        .await;

    assert!(
        result.is_ok(),
        "Shell command should complete: {:?}",
        result.err()
    );
    println!("PASS: Shell command execution completed");
}

// ─── Context Status Tool ────────────────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires live endpoint"]
async fn smoke_context_status() {
    let tmp = TempDir::new().unwrap();
    let config = match smoke_config(tmp.path()).await {
        Some(c) => c,
        None => {
            eprintln!("SKIPPED: endpoint not available");
            return;
        }
    };

    let mut agent = selfware::agent::Agent::new(config).await.unwrap();
    let result = agent
        .run_task("Use context_status to check your context window budget and report the numbers.")
        .await;

    match result {
        Ok(()) => println!("PASS: Context status tool worked"),
        Err(e) => println!("PARTIAL: Context status task: {}", e),
    }
}

// ─── Cargo Check on Selfware ────────────────────────────────────────────────

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
#[ignore = "requires live endpoint"]
async fn smoke_cargo_check_selfware() {
    let tmp = TempDir::new().unwrap();
    let config = match smoke_config(tmp.path()).await {
        Some(c) => c,
        None => {
            eprintln!("SKIPPED: endpoint not available");
            return;
        }
    };

    let mut agent = selfware::agent::Agent::new(config).await.unwrap();
    let result = agent
        .run_task("Run cargo check on this project and report if it compiles successfully.")
        .await;

    assert!(
        result.is_ok(),
        "Cargo check should complete: {:?}",
        result.err()
    );
    println!("PASS: Cargo check task completed");
}