intent-engine 0.11.1

A command-line database service for tracking strategic intent, tasks, and events
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
// Performance tests for large datasets
// These tests are designed to validate performance and accuracy with 100k+ tasks

use intent_engine::db::models::SearchResult;
use intent_engine::db::{create_pool, run_migrations};
use intent_engine::events::EventManager;
use intent_engine::report::ReportManager;
use intent_engine::search::SearchManager;
use intent_engine::tasks::TaskManager;
use rand::Rng;
use std::time::Instant;
use tempfile::TempDir;

const TOTAL_TASKS_100K: usize = 100_000;
const TOTAL_TASKS_10K: usize = 10_000;

#[tokio::test]
#[ignore] // Run with: cargo test --test performance_large_dataset_tests test_large_dataset_report_and_search -- --ignored --nocapture
async fn test_large_dataset_report_and_search() {
    run_dataset_test(TOTAL_TASKS_100K).await;
}

#[tokio::test]
#[ignore] // Run with: cargo test --test performance_large_dataset_tests test_medium_dataset_report_and_search -- --ignored --nocapture
async fn test_medium_dataset_report_and_search() {
    run_dataset_test(TOTAL_TASKS_10K).await;
}

async fn run_dataset_test(total_tasks: usize) {
    println!(
        "\n🚀 Starting dataset performance test with {} tasks",
        total_tasks
    );

    // Setup
    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("large_test.db");
    let pool = create_pool(&db_path).await.unwrap();
    run_migrations(&pool).await.unwrap();

    let task_mgr = TaskManager::new(&pool);
    let search_mgr = SearchManager::new(&pool);
    let event_mgr = EventManager::new(&pool);
    let report_mgr = ReportManager::new(&pool);

    // Phase 1: Insert tasks
    println!("\n📝 Phase 1: Inserting {} tasks...", total_tasks);
    let start = Instant::now();

    let keywords = vec![
        "authentication",
        "database",
        "frontend",
        "backend",
        "api",
        "security",
        "optimization",
        "refactor",
        "feature",
        "bugfix",
        "documentation",
        "testing",
        "deployment",
        "migration",
        "performance",
    ];

    let mut task_ids = Vec::with_capacity(total_tasks);

    for i in 0..total_tasks {
        let keyword_idx = i % keywords.len();
        let keyword = keywords[keyword_idx];

        let name = format!("{} task #{}", keyword, i);
        let spec = format!("Implement {} functionality for module {}", keyword, i / 100);

        let task = task_mgr
            .add_task(
                name.to_string(),
                Some(spec.to_string()),
                None,
                None,
                None,
                None,
            )
            .await
            .unwrap();

        task_ids.push(task.id);

        if (i + 1) % 10000 == 0 {
            println!(
                "  ✓ Inserted {} tasks ({:.1}%)",
                i + 1,
                (i + 1) as f64 / total_tasks as f64 * 100.0
            );
        }
    }

    let insert_duration = start.elapsed();
    println!(
        "✅ Inserted {} tasks in {:.2}s ({:.0} tasks/sec)",
        total_tasks,
        insert_duration.as_secs_f64(),
        total_tasks as f64 / insert_duration.as_secs_f64()
    );

    // Phase 2: Randomly start and complete tasks
    println!("\n🎲 Phase 2: Randomly starting and completing tasks...");
    let start = Instant::now();

    #[allow(deprecated)]
    let mut rng = rand::thread_rng();
    let mut started_count = 0;
    let mut completed_count = 0;

    // Start 30% of tasks
    let tasks_to_start = total_tasks * 30 / 100;
    for _ in 0..tasks_to_start {
        #[allow(deprecated)]
        let random_idx = rng.gen_range(0..task_ids.len());
        let task_id = task_ids[random_idx];

        if task_mgr.start_task(task_id, false).await.is_ok() {
            started_count += 1;
        }
    }

    println!("  ✓ Started {} tasks", started_count);

    // Complete 50% of started tasks
    let tasks_to_complete = started_count / 2;
    for _ in 0..tasks_to_complete {
        #[allow(deprecated)]
        let random_idx = rng.gen_range(0..task_ids.len());
        let task_id = task_ids[random_idx];

        // Try to set as current and complete
        if sqlx::query(
            r#"
            INSERT INTO sessions (session_id, current_task_id, created_at, last_active_at)
            VALUES ('-1', ?, datetime('now'), datetime('now'))
            ON CONFLICT(session_id) DO UPDATE SET
                current_task_id = excluded.current_task_id,
                last_active_at = datetime('now')
            "#,
        )
        .bind(task_id)
        .execute(&pool)
        .await
        .is_ok()
        {
            // Check if task is in doing status
            let status: Option<String> =
                sqlx::query_scalar("SELECT status FROM tasks WHERE id = ?")
                    .bind(task_id)
                    .fetch_optional(&pool)
                    .await
                    .unwrap();

            if status == Some("doing".to_string()) {
                // Check if it has no uncompleted children
                let uncompleted_children: i64 = sqlx::query_scalar(
                    "SELECT COUNT(*) FROM tasks WHERE parent_id = ? AND status != 'done'",
                )
                .bind(task_id)
                .fetch_one(&pool)
                .await
                .unwrap();

                if uncompleted_children == 0 && task_mgr.done_task(false).await.is_ok() {
                    completed_count += 1;
                }
            }
        }
    }

    println!("  ✓ Completed {} tasks", completed_count);

    let state_change_duration = start.elapsed();
    println!(
        "✅ State changes completed in {:.2}s",
        state_change_duration.as_secs_f64()
    );

    // Add some events to random tasks
    println!("\n📋 Adding events to sample tasks...");
    let start = Instant::now();
    let events_count = 1000;

    for _ in 0..events_count {
        #[allow(deprecated)]
        let random_idx = rng.gen_range(0..task_ids.len());
        let task_id = task_ids[random_idx];

        let _ = event_mgr
            .add_event(
                task_id,
                "progress".to_string(),
                "Made significant progress on this task".to_string(),
            )
            .await;
    }

    let events_duration = start.elapsed();
    println!(
        "✅ Added {} events in {:.2}s",
        events_count,
        events_duration.as_secs_f64()
    );

    // Phase 3: Test Report Performance
    println!("\n📊 Phase 3: Testing Report Performance...");

    // Test 1: Summary-only report
    let start = Instant::now();
    let report = report_mgr
        .generate_report(None, None, None, None, true)
        .await
        .unwrap();
    let summary_duration = start.elapsed();

    println!("  Report Summary:");
    println!("    Total tasks: {}", report.summary.total_tasks);
    println!("    Todo: {}", report.summary.tasks_by_status.todo);
    println!("    Doing: {}", report.summary.tasks_by_status.doing);
    println!("    Done: {}", report.summary.tasks_by_status.done);
    println!("    Total events: {}", report.summary.total_events);
    println!(
        "  ⏱️  Summary report generated in {:.3}s",
        summary_duration.as_secs_f64()
    );

    // Verify counts
    assert_eq!(report.summary.total_tasks, total_tasks as i64);
    let total_status = report.summary.tasks_by_status.todo
        + report.summary.tasks_by_status.doing
        + report.summary.tasks_by_status.done;
    assert_eq!(total_status, total_tasks as i64);

    // Test 2: Status filter report
    let start = Instant::now();
    let done_report = report_mgr
        .generate_report(None, Some("done".to_string()), None, None, true)
        .await
        .unwrap();
    let status_filter_duration = start.elapsed();

    println!("  Status Filter Report (done only):");
    println!("    Total tasks: {}", done_report.summary.total_tasks);
    println!(
        "  ⏱️  Status filter report generated in {:.3}s",
        status_filter_duration.as_secs_f64()
    );

    assert_eq!(
        done_report.summary.total_tasks,
        report.summary.tasks_by_status.done
    );

    // Test 3: Time-based report
    let start = Instant::now();
    let time_report = report_mgr
        .generate_report(Some("24h".to_string()), None, None, None, true)
        .await
        .unwrap();
    let time_filter_duration = start.elapsed();

    println!("  Time Filter Report (last 24h):");
    println!("    Total tasks: {}", time_report.summary.total_tasks);
    println!(
        "  ⏱️  Time filter report generated in {:.3}s",
        time_filter_duration.as_secs_f64()
    );

    // Phase 4: Test Search Performance and Accuracy
    println!("\n🔍 Phase 4: Testing Search Performance and Accuracy...");

    // Test search for each keyword
    let mut total_search_duration = std::time::Duration::ZERO;
    for keyword in &keywords[0..5] {
        // Test first 5 keywords
        let start = Instant::now();
        let results = search_mgr
            .search(keyword, true, false, None, None, false)
            .await
            .unwrap()
            .results;
        let search_duration = start.elapsed();
        total_search_duration += search_duration;

        // Calculate expected count (approximate)
        let expected_min = total_tasks / keywords.len() - 100; // Allow some margin
        let expected_max = total_tasks / keywords.len() + 100;

        println!(
            "  Search '{}': {} results in {:.3}s",
            keyword,
            results.len(),
            search_duration.as_secs_f64()
        );

        // Verify accuracy - check that results actually contain the keyword
        let mut accurate = 0;
        for result in results.iter().take(100) {
            // Check first 100 results
            if let SearchResult::Task { task, .. } = result {
                if task.name.contains(keyword)
                    || task.spec.as_ref().is_some_and(|s| s.contains(keyword))
                {
                    accurate += 1;
                }
            }
        }

        let accuracy = accurate as f64 / results.len().min(100) as f64 * 100.0;
        println!(
            "    Accuracy: {:.1}% (checked {} results)",
            accuracy,
            results.len().min(100)
        );

        // Verify search returned reasonable number of results
        assert!(
            results.len() >= expected_min,
            "Search for '{}' returned too few results: {} < {}",
            keyword,
            results.len(),
            expected_min
        );
        assert!(
            results.len() <= expected_max,
            "Search for '{}' returned too many results: {} > {}",
            keyword,
            results.len(),
            expected_max
        );

        // Verify high accuracy
        assert!(
            accuracy >= 95.0,
            "Search accuracy too low: {:.1}% < 95%",
            accuracy
        );
    }

    // Test complex search with AND operator
    let start = Instant::now();
    let complex_results = search_mgr
        .search("authentication AND module", true, false, None, None, false)
        .await
        .unwrap()
        .results;
    let complex_duration = start.elapsed();

    println!(
        "  Complex search 'authentication AND module': {} results in {:.3}s",
        complex_results.len(),
        complex_duration.as_secs_f64()
    );

    // Verify complex search accuracy
    let mut accurate = 0;
    for result in complex_results.iter().take(100) {
        if let SearchResult::Task { task, .. } = result {
            let text = format!(
                "{} {}",
                task.name,
                task.spec.as_ref().unwrap_or(&String::new())
            );
            if text.contains("authentication") && text.contains("module") {
                accurate += 1;
            }
        }
    }

    if !complex_results.is_empty() {
        let accuracy = accurate as f64 / complex_results.len().min(100) as f64 * 100.0;
        println!("    Accuracy: {:.1}%", accuracy);
        assert!(accuracy >= 95.0);
    }

    // Phase 5: Test find_tasks performance
    println!("\n🔎 Phase 5: Testing find_tasks Performance...");

    let start = Instant::now();
    let result = task_mgr
        .find_tasks(Some("todo".to_string()), None, None, None, None)
        .await
        .unwrap();
    let find_duration = start.elapsed();

    println!(
        "  Find todo tasks: {} results in {:.3}s",
        result.tasks.len(),
        find_duration.as_secs_f64()
    );

    assert_eq!(
        result.tasks.len() as i64,
        report.summary.tasks_by_status.todo
    );

    let avg_search_duration = total_search_duration.as_secs_f64() / 5.0;

    // Summary
    println!("\n{}", "=".repeat(60));
    println!("📈 PERFORMANCE SUMMARY");
    println!("{}", "=".repeat(60));
    println!("Dataset: {} tasks, {} events", total_tasks, events_count);
    println!(
        "Insert:  {:.2}s ({:.0} tasks/sec)",
        insert_duration.as_secs_f64(),
        total_tasks as f64 / insert_duration.as_secs_f64()
    );
    println!("Report:  {:.3}s (summary)", summary_duration.as_secs_f64());
    println!(
        "Search:  ~{:.3}s (average per keyword)",
        avg_search_duration
    );
    println!(
        "Find:    {:.3}s (status filter)",
        find_duration.as_secs_f64()
    );
    println!("{}", "=".repeat(60));

    // Performance assertions
    assert!(
        summary_duration.as_secs_f64() < 5.0,
        "Summary report too slow: {:.2}s > 5s",
        summary_duration.as_secs_f64()
    );
    assert!(
        avg_search_duration < 1.0,
        "Search too slow: {:.2}s > 1s",
        avg_search_duration
    );
    assert!(
        find_duration.as_secs_f64() < 2.0,
        "Find tasks too slow: {:.2}s > 2s",
        find_duration.as_secs_f64()
    );

    println!("✅ All performance tests passed!");
}

#[tokio::test]
#[ignore] // Run with: cargo test --test performance_large_dataset_tests test_search_accuracy -- --ignored --nocapture
async fn test_search_accuracy_detailed() {
    println!("\n🎯 Testing search accuracy with controlled dataset...");

    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("accuracy_test.db");
    let pool = create_pool(&db_path).await.unwrap();
    run_migrations(&pool).await.unwrap();

    let task_mgr = TaskManager::new(&pool);
    let search_mgr = SearchManager::new(&pool);

    // Create controlled test dataset
    let test_cases = vec![
        ("JWT Authentication", "Implement JWT token authentication"),
        ("User Authentication", "Add user authentication with OAuth2"),
        (
            "Database Optimization",
            "Optimize database query performance",
        ),
        ("API Endpoint", "Create new API endpoint for authentication"),
        ("Frontend Component", "Build authentication form component"),
        ("Backend Service", "Authentication service implementation"),
        ("Security Audit", "Perform security audit on authentication"),
        ("Performance Testing", "Load test authentication endpoints"),
        ("Documentation", "Document authentication flow"),
        ("Migration Script", "Database migration for auth tables"),
    ];

    println!("Creating {} test tasks...", test_cases.len() * 100);

    for i in 0..100 {
        for (name_template, spec_template) in &test_cases {
            let name = format!("{} #{}", name_template, i);
            let spec = format!("{} - iteration {}", spec_template, i);
            task_mgr
                .add_task(
                    name.to_string(),
                    Some(spec.to_string()),
                    None,
                    None,
                    None,
                    None,
                )
                .await
                .unwrap();
        }
    }

    println!("Testing search accuracy...\n");

    // Test 1: Single keyword search
    let results = search_mgr
        .search("authentication", true, false, None, None, false)
        .await
        .unwrap()
        .results;
    println!("Search 'authentication': {} results", results.len());

    // Should find all tasks with "authentication" in name or spec
    let expected = test_cases
        .iter()
        .filter(|(n, s)| {
            n.to_lowercase().contains("authentication")
                || s.to_lowercase().contains("authentication")
        })
        .count()
        * 100;

    println!("  Expected: ~{} tasks", expected);
    println!("  Actual:   {} tasks", results.len());
    println!(
        "  Accuracy: {:.1}%",
        results.len() as f64 / expected as f64 * 100.0
    );

    assert!(
        results.len() >= expected - 10 && results.len() <= expected + 10,
        "Search returned {} tasks, expected ~{}",
        results.len(),
        expected
    );

    // Test 2: AND operator
    let results = search_mgr
        .search("authentication AND JWT", true, false, None, None, false)
        .await
        .unwrap()
        .results;
    println!(
        "\nSearch 'authentication AND JWT': {} results",
        results.len()
    );

    let expected = 100; // Only "JWT Authentication" tasks
    println!("  Expected: ~{} tasks", expected);
    println!("  Actual:   {} tasks", results.len());

    assert!(results.len() >= expected - 10 && results.len() <= expected + 10);

    // Test 3: OR operator
    let results = search_mgr
        .search("JWT OR OAuth2", true, false, None, None, false)
        .await
        .unwrap()
        .results;
    println!("\nSearch 'JWT OR OAuth2': {} results", results.len());

    let expected = 200; // "JWT Authentication" + "User Authentication"
    println!("  Expected: ~{} tasks", expected);
    println!("  Actual:   {} tasks", results.len());

    assert!(results.len() >= expected - 20 && results.len() <= expected + 20);

    // Test 4: NOT operator
    let results = search_mgr
        .search("authentication NOT JWT", true, false, None, None, false)
        .await
        .unwrap()
        .results;
    println!(
        "\nSearch 'authentication NOT JWT': {} results",
        results.len()
    );

    // Should exclude "JWT Authentication" tasks
    let expected = 600; // All auth tasks except JWT ones
    println!("  Expected: ~{} tasks", expected);
    println!("  Actual:   {} tasks", results.len());

    println!("\n✅ Search accuracy tests completed!");
}

#[tokio::test]
#[ignore]
async fn test_concurrent_search_performance() {
    println!("\n⚡ Testing concurrent search performance...");

    let temp_dir = TempDir::new().unwrap();
    let db_path = temp_dir.path().join("concurrent_test.db");
    let pool = create_pool(&db_path).await.unwrap();
    run_migrations(&pool).await.unwrap();

    let task_mgr = TaskManager::new(&pool);
    let _search_mgr = SearchManager::new(&pool);

    // Create dataset
    println!("Creating 10,000 tasks...");
    for i in 0..10000 {
        let keyword = match i % 5 {
            0 => "authentication",
            1 => "database",
            2 => "frontend",
            3 => "backend",
            _ => "api",
        };

        task_mgr
            .add_task(
                format!("{} task #{}", keyword, i),
                Some(format!("Implementation for {}", keyword)),
                None,
                None,
                None,
                None,
            )
            .await
            .unwrap();
    }

    println!("Running concurrent searches...");

    let start = Instant::now();
    let mut handles = vec![];

    // Spawn 10 concurrent search tasks
    for keyword in &["authentication", "database", "frontend", "backend", "api"] {
        for _ in 0..2 {
            let keyword = keyword.to_string();
            let temp_dir_path = temp_dir.path().to_path_buf();

            let handle = tokio::spawn(async move {
                let db_path = temp_dir_path.join("concurrent_test.db");
                let pool = create_pool(&db_path).await.unwrap();
                let _task_mgr = TaskManager::new(&pool);
                let search_mgr = SearchManager::new(&pool);

                let start = Instant::now();
                let results = search_mgr
                    .search(&keyword, true, false, None, None, false)
                    .await
                    .unwrap()
                    .results;
                let duration = start.elapsed();

                (keyword, results.len(), duration)
            });

            handles.push(handle);
        }
    }

    // Wait for all searches to complete
    let mut total_results = 0;
    for handle in handles {
        let (keyword, count, duration) = handle.await.unwrap();
        println!(
            "  '{}': {} results in {:.3}s",
            keyword,
            count,
            duration.as_secs_f64()
        );
        total_results += count;
    }

    let total_duration = start.elapsed();

    println!("\n✅ Concurrent searches completed:");
    println!("  Total time: {:.3}s", total_duration.as_secs_f64());
    println!("  Total results: {}", total_results);
    println!(
        "  Average time per search: {:.3}s",
        total_duration.as_secs_f64() / 10.0
    );

    // Should complete all searches within reasonable time
    assert!(
        total_duration.as_secs_f64() < 10.0,
        "Concurrent searches too slow: {:.2}s > 10s",
        total_duration.as_secs_f64()
    );
}