selfware 0.2.2

Your personal AI workshop — software you own, software that lasts
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
use tracing::info;

use super::*;

impl Agent {
    pub(super) fn infer_task_type(task: &str) -> &'static str {
        let task_lower = task.to_lowercase();
        if task_lower.contains("review") {
            "code_review"
        } else if task_lower.contains("test") {
            "testing"
        } else if task_lower.contains("refactor") {
            "refactor"
        } else if task_lower.contains("fix") || task_lower.contains("bug") {
            "bug_fix"
        } else if task_lower.contains("document") || task_lower.contains("readme") {
            "documentation"
        } else {
            "general"
        }
    }

    pub(super) fn classify_error_type(error: &str) -> &'static str {
        let lower = error.to_lowercase();
        if lower.contains("timeout") || lower.contains("timed out") {
            "timeout"
        } else if lower.contains("permission") || lower.contains("denied") {
            "permission"
        } else if lower.contains("safety") || lower.contains("blocked") {
            "safety"
        } else if lower.contains("json") || lower.contains("parse") || lower.contains("invalid") {
            "parsing"
        } else if lower.contains("network") || lower.contains("connection") {
            "network"
        } else {
            "execution"
        }
    }

    pub(super) fn outcome_quality(outcome: Outcome) -> f32 {
        match outcome {
            Outcome::Success => 1.0,
            Outcome::Partial => 0.65,
            Outcome::Failure => 0.0,
            Outcome::Abandoned => 0.2,
        }
    }

    pub(super) fn learning_context(&self) -> &str {
        if self.current_task_context.is_empty() {
            "general"
        } else {
            &self.current_task_context
        }
    }

    pub(super) fn start_learning_session(&mut self, session_id: &str, task_context: &str) {
        self.current_task_context = task_context.to_string();
        self.self_improvement.start_session(session_id);
    }

    pub(super) fn record_task_outcome(
        &mut self,
        task_prompt: &str,
        outcome: Outcome,
        error: Option<&str>,
    ) {
        self.log_task_outcome_event(task_prompt, outcome, error);

        let task_type = Self::infer_task_type(task_prompt);
        self.self_improvement.record_prompt(
            task_prompt,
            task_type,
            outcome,
            Self::outcome_quality(outcome),
        );
        self.self_improvement.record_task(outcome.is_positive());

        if let Some(err) = error {
            self.self_improvement.record_error(
                err,
                Self::classify_error_type(err),
                self.learning_context(),
                "task_execution",
                None,
            );
        }

        self.self_improvement.end_session(None);
    }

    pub(super) fn build_learning_hint(&self, task_prompt: &str) -> Option<String> {
        if task_prompt.trim().is_empty() {
            return None;
        }

        let mut hints: Vec<String> = Vec::new();

        let preferred_tools: Vec<String> = self
            .self_improvement
            .best_tools_for(task_prompt)
            .into_iter()
            .filter(|(_, score)| *score >= 0.6)
            .take(3)
            .map(|(tool, score)| format!("{} ({:.0}% confidence)", tool, score * 100.0))
            .collect();
        if !preferred_tools.is_empty() {
            hints.push(format!(
                "Prefer previously effective tools: {}.",
                preferred_tools.join(", ")
            ));
        }

        let warnings = self
            .self_improvement
            .check_for_errors("task_execution", task_prompt);
        if let Some(warning) = warnings.into_iter().next().filter(|w| w.likelihood >= 0.6) {
            hints.push(format!(
                "Avoid recurring {} pattern (likelihood {:.0}%).",
                warning.error_type,
                warning.likelihood * 100.0
            ));
            if !warning.prevention.is_empty() {
                hints.push(format!(
                    "Prevention guidance: {}.",
                    warning
                        .prevention
                        .into_iter()
                        .take(2)
                        .collect::<Vec<_>>()
                        .join("; ")
                ));
            }
        }

        if hints.is_empty() {
            None
        } else {
            Some(format!(
                "Self-improvement guidance from prior outcomes:\n- {}",
                hints.join("\n- ")
            ))
        }
    }

    /// Reflect on a completed step: record lessons, update learner, inject hints
    pub(super) async fn reflect_on_step(&mut self, step: usize) {
        self.cognitive_state.set_phase(CyclePhase::Reflect);

        // 1. Check for verification failures in the last step and record lessons
        if let Some(ref checkpoint) = self.current_checkpoint {
            let step_errors: Vec<_> = checkpoint
                .errors
                .iter()
                .filter(|e| e.step == step)
                .collect();
            for error in &step_errors {
                if error.recovered {
                    self.cognitive_state.episodic_memory.what_worked(
                        "error_recovery",
                        &format!("Step {}: recovered from: {}", step, error.error),
                    );
                    // Record recovery strategy in improvement engine
                    self.self_improvement.record_error(
                        &error.error,
                        "step_error",
                        self.learning_context(),
                        &format!("step_{}", step),
                        Some("automatic_recovery".to_string()),
                    );
                } else {
                    self.cognitive_state.episodic_memory.what_failed(
                        "step_execution",
                        &format!("Step {}: unrecovered error: {}", step, error.error),
                    );
                }
            }
        }

        // 2. Query tool learner for recommendations and inject hint into working memory
        let context = self.current_task_context.clone();
        let best_tools = self.self_improvement.best_tools_for(&context);
        if let Some((tool, score)) = best_tools.first() {
            if *score >= 0.7 {
                let hint = format!(
                    "Based on learning: tool '{}' has {:.0}% effectiveness for this context",
                    tool,
                    score * 100.0
                );
                self.cognitive_state.working_memory.add_fact(&hint);
            }
        }

        // 3. LLM Functional Reflection (Every 5 steps)
        if step > 0 && step.is_multiple_of(5) {
            info!("Triggering functional reflection for step {}", step);
            let reflection_prompt = format!(
                "You have just completed step {}. Reflect on the last 5 steps.
                What did you learn? What would you do differently? What surprised you?
                Be concise. Output your reflection as a single paragraph.",
                step
            );

            let mut messages = self.messages.clone();
            messages.push(crate::api::types::Message::user(reflection_prompt));

            if let Ok(response) = self
                .client
                .chat(messages, None, crate::api::ThinkingMode::Disabled)
                .await
            {
                if let Some(choice) = response.choices.first() {
                    let text = choice.message.content.clone();
                    if !text.is_empty() {
                        let lesson = crate::cognitive::Lesson {
                            category: crate::cognitive::LessonCategory::Discovery,
                            content: format!("Reflection at step {}: {}", step, text),
                            context: "".to_string(),
                            tags: vec!["reflection".to_string()],
                            timestamp: chrono::Utc::now(),
                        };
                        self.cognitive_state.episodic_memory.record_lesson(lesson);
                        self.cognitive_state
                            .working_memory
                            .add_fact(&format!("Reflection (Step {}): {}", step, text));
                    }
                }
            }
        }

        // 4. Mark the plan step complete with notes
        let notes = format!("Step {} completed", step);
        self.cognitive_state
            .working_memory
            .complete_step(step, Some(notes));
        self.cognitive_state
            .complete_operational_step(step, Some(format!("Step {} completed", step)));

        self.cognitive_state.set_phase(CyclePhase::Do);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cognitive::self_improvement::Outcome;

    // =========================================================================
    // infer_task_type — exhaustive branch coverage + edge cases
    // =========================================================================

    #[test]
    fn test_infer_task_type_code_review() {
        assert_eq!(
            Agent::infer_task_type("Please review this PR"),
            "code_review"
        );
        assert_eq!(Agent::infer_task_type("Code review needed"), "code_review");
        assert_eq!(Agent::infer_task_type("REVIEW the changes"), "code_review");
    }

    #[test]
    fn test_infer_task_type_testing() {
        assert_eq!(Agent::infer_task_type("Write tests for module"), "testing");
        assert_eq!(Agent::infer_task_type("Add unit test coverage"), "testing");
        assert_eq!(Agent::infer_task_type("Run TEST suite"), "testing");
    }

    #[test]
    fn test_infer_task_type_refactor() {
        assert_eq!(Agent::infer_task_type("Refactor the parser"), "refactor");
        assert_eq!(Agent::infer_task_type("REFACTORING needed"), "refactor");
    }

    #[test]
    fn test_infer_task_type_bug_fix() {
        assert_eq!(Agent::infer_task_type("Fix this bug"), "bug_fix");
        assert_eq!(Agent::infer_task_type("There is a bug in login"), "bug_fix");
        assert_eq!(Agent::infer_task_type("FIX compilation error"), "bug_fix");
    }

    #[test]
    fn test_infer_task_type_documentation() {
        assert_eq!(Agent::infer_task_type("Document the API"), "documentation");
        assert_eq!(Agent::infer_task_type("Update README"), "documentation");
        assert_eq!(
            Agent::infer_task_type("Write documentation for new feature"),
            "documentation"
        );
        assert_eq!(
            Agent::infer_task_type("Update the readme file"),
            "documentation"
        );
    }

    #[test]
    fn test_infer_task_type_general_fallback() {
        assert_eq!(Agent::infer_task_type("Build the new feature"), "general");
        assert_eq!(Agent::infer_task_type("Deploy to production"), "general");
        assert_eq!(Agent::infer_task_type(""), "general");
    }

    #[test]
    fn test_infer_task_type_priority_order() {
        // "review" takes priority over "test" when both are present
        assert_eq!(
            Agent::infer_task_type("Review the test changes"),
            "code_review"
        );
        // "test" takes priority over "refactor"
        assert_eq!(Agent::infer_task_type("Test after refactor"), "testing");
        // "refactor" takes priority over "fix"
        assert_eq!(
            Agent::infer_task_type("Refactor to fix the issue"),
            "refactor"
        );
    }

    // =========================================================================
    // classify_error_type — exhaustive branch coverage + edge cases
    // =========================================================================

    #[test]
    fn test_classify_error_timeout() {
        assert_eq!(Agent::classify_error_type("request timed out"), "timeout");
        assert_eq!(
            Agent::classify_error_type("Connection timeout after 30s"),
            "timeout"
        );
        assert_eq!(Agent::classify_error_type("Operation TIMED OUT"), "timeout");
    }

    #[test]
    fn test_classify_error_permission() {
        assert_eq!(
            Agent::classify_error_type("permission denied"),
            "permission"
        );
        assert_eq!(
            Agent::classify_error_type("Access denied for path /root"),
            "permission"
        );
        assert_eq!(Agent::classify_error_type("PERMISSION error"), "permission");
    }

    #[test]
    fn test_classify_error_safety() {
        assert_eq!(Agent::classify_error_type("Safety check failed"), "safety");
        assert_eq!(
            Agent::classify_error_type("Operation blocked by policy"),
            "safety"
        );
        assert_eq!(Agent::classify_error_type("BLOCKED by firewall"), "safety");
    }

    #[test]
    fn test_classify_error_parsing() {
        assert_eq!(
            Agent::classify_error_type("Invalid JSON in response"),
            "parsing"
        );
        assert_eq!(
            Agent::classify_error_type("Failed to parse config"),
            "parsing"
        );
        assert_eq!(Agent::classify_error_type("JSON decode error"), "parsing");
        assert_eq!(
            Agent::classify_error_type("invalid syntax at line 5"),
            "parsing"
        );
    }

    #[test]
    fn test_classify_error_network() {
        assert_eq!(Agent::classify_error_type("Network unreachable"), "network");
        assert_eq!(Agent::classify_error_type("Connection refused"), "network");
        assert_eq!(Agent::classify_error_type("NETWORK error"), "network");
    }

    #[test]
    fn test_classify_error_execution_fallback() {
        assert_eq!(
            Agent::classify_error_type("unknown error occurred"),
            "execution"
        );
        assert_eq!(Agent::classify_error_type("segfault"), "execution");
        assert_eq!(Agent::classify_error_type(""), "execution");
    }

    #[test]
    fn test_classify_error_priority_order() {
        // "timeout" takes priority over "network" when both keywords present
        assert_eq!(
            Agent::classify_error_type("network connection timeout"),
            "timeout"
        );
        // "permission" takes priority over "safety"
        assert_eq!(
            Agent::classify_error_type("permission denied: safety policy"),
            "permission"
        );
    }

    // =========================================================================
    // outcome_quality — all variants
    // =========================================================================

    #[test]
    fn test_outcome_quality_all_variants() {
        assert_eq!(Agent::outcome_quality(Outcome::Success), 1.0);
        assert_eq!(Agent::outcome_quality(Outcome::Partial), 0.65);
        assert_eq!(Agent::outcome_quality(Outcome::Failure), 0.0);
        assert_eq!(Agent::outcome_quality(Outcome::Abandoned), 0.2);
    }

    #[test]
    fn test_outcome_quality_ordering() {
        assert!(
            Agent::outcome_quality(Outcome::Success) > Agent::outcome_quality(Outcome::Partial)
        );
        assert!(
            Agent::outcome_quality(Outcome::Partial) > Agent::outcome_quality(Outcome::Abandoned)
        );
        assert!(
            Agent::outcome_quality(Outcome::Abandoned) > Agent::outcome_quality(Outcome::Failure)
        );
    }

    // =========================================================================
    // Outcome enum methods
    // =========================================================================

    #[test]
    fn test_outcome_is_positive() {
        assert!(Outcome::Success.is_positive());
        assert!(Outcome::Partial.is_positive());
        assert!(!Outcome::Failure.is_positive());
        assert!(!Outcome::Abandoned.is_positive());
    }

    #[test]
    fn test_outcome_score() {
        assert_eq!(Outcome::Success.score(), 1.0);
        assert_eq!(Outcome::Partial.score(), 0.5);
        assert_eq!(Outcome::Failure.score(), 0.0);
        assert_eq!(Outcome::Abandoned.score(), 0.0);
    }

    // =========================================================================
    // build_learning_hint edge cases (via mock Agent)
    // =========================================================================

    #[tokio::test]
    async fn test_build_learning_hint_empty_prompt_returns_none() {
        let server = crate::testing::mock_api::MockLlmServer::builder()
            .with_response("done")
            .build()
            .await;

        let config = crate::config::Config {
            endpoint: format!("{}/v1", server.url()),
            model: "mock".to_string(),
            agent: crate::config::AgentConfig {
                max_iterations: 5,
                step_timeout_secs: 5,
                streaming: false,
                native_function_calling: false,
                ..Default::default()
            },
            ..Default::default()
        };
        let agent = Agent::new(config).await.unwrap();

        // Empty prompt should return None
        assert!(agent.build_learning_hint("").is_none());
        assert!(agent.build_learning_hint("   ").is_none());

        server.stop().await;
    }

    #[tokio::test]
    async fn test_build_learning_hint_returns_string_or_none() {
        let server = crate::testing::mock_api::MockLlmServer::builder()
            .with_response("done")
            .build()
            .await;

        let config = crate::config::Config {
            endpoint: format!("{}/v1", server.url()),
            model: "mock".to_string(),
            agent: crate::config::AgentConfig {
                max_iterations: 5,
                step_timeout_secs: 5,
                streaming: false,
                native_function_calling: false,
                ..Default::default()
            },
            ..Default::default()
        };
        let agent = Agent::new(config).await.unwrap();

        let result = agent.build_learning_hint("Write a new parser");
        // With no recorded data, result is None; with persisted data it contains guidance
        match result {
            None => {} // Fresh engine — no hints
            Some(ref hint) => {
                assert!(
                    hint.contains("Self-improvement guidance"),
                    "hint should contain expected prefix: {}",
                    hint
                );
            }
        }

        server.stop().await;
    }

    // =========================================================================
    // learning_context
    // =========================================================================

    #[tokio::test]
    async fn test_learning_context_defaults_to_general() {
        let server = crate::testing::mock_api::MockLlmServer::builder()
            .with_response("done")
            .build()
            .await;

        let config = crate::config::Config {
            endpoint: format!("{}/v1", server.url()),
            model: "mock".to_string(),
            agent: crate::config::AgentConfig {
                max_iterations: 5,
                step_timeout_secs: 5,
                streaming: false,
                native_function_calling: false,
                ..Default::default()
            },
            ..Default::default()
        };
        let agent = Agent::new(config).await.unwrap();

        // Default empty context returns "general"
        assert_eq!(agent.learning_context(), "general");

        server.stop().await;
    }
}