tasker-orchestration 0.1.4

Orchestration system for tasker workflow coordination
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
//! # Batch Processing Service Integration Tests (TAS-63)
//!
//! Tests for BatchProcessingService using the large_dataset_processor template:
//! - NoBatches placeholder worker creation
//! - CreateBatches with multiple cursors
//! - Convergence step creation
//! - Edge wiring (batchable → workers → convergence)
//! - Idempotency on duplicate calls
//! - Worker naming conventions
//! - Integration with ViableStepDiscovery

use anyhow::Result;
use serde_json::json;
use sqlx::PgPool;
use std::sync::Arc;
use uuid::Uuid;

use tasker_orchestration::orchestration::lifecycle::step_enqueuer_services::StepEnqueuerService;
use tasker_orchestration::orchestration::lifecycle::task_initialization::TaskInitializer;
use tasker_orchestration::orchestration::lifecycle::{
    BatchProcessingError, BatchProcessingService,
};
use tasker_orchestration::orchestration::viable_step_discovery::ViableStepDiscovery;
use tasker_shared::messaging::execution_types::{
    BatchProcessingOutcome, CursorConfig, StepExecutionResult,
};
use tasker_shared::models::core::task_request::TaskRequest;
use tasker_shared::models::WorkflowStep;
use tasker_shared::registry::TaskHandlerRegistry;
use tasker_shared::state_machine::{StepEvent, StepStateMachine};
use tasker_shared::system_context::SystemContext;

/// Get the path to task template fixtures in the workspace root
fn fixture_path() -> String {
    let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
    std::path::Path::new(&manifest_dir)
        .parent()
        .unwrap_or(std::path::Path::new("."))
        .join("tests/fixtures/task_templates/rust")
        .to_string_lossy()
        .to_string()
}

/// Setup helper: creates BatchProcessingService with template registration
async fn setup_batch_service(
    pool: PgPool,
) -> Result<(
    BatchProcessingService,
    ViableStepDiscovery,
    TaskInitializer,
    Arc<SystemContext>,
)> {
    let registry = TaskHandlerRegistry::new(pool.clone());
    registry
        .discover_and_register_templates(&fixture_path())
        .await?;

    let system_context = Arc::new(SystemContext::with_pool(pool.clone()).await?);
    let step_enqueuer = Arc::new(StepEnqueuerService::new(system_context.clone()).await?);
    let task_initializer = TaskInitializer::new(system_context.clone(), step_enqueuer);
    let batch_service = BatchProcessingService::new(system_context.clone());
    let discovery = ViableStepDiscovery::new(system_context.clone());

    Ok((batch_service, discovery, task_initializer, system_context))
}

/// Create a task from the large_dataset_processor template
async fn create_batch_task(initializer: &TaskInitializer) -> Result<Uuid> {
    let request = TaskRequest {
        name: "large_dataset_processor".to_string(),
        namespace: "data_processing".to_string(),
        version: "1.0.0".to_string(),
        context: json!({
            "_test_run_id": Uuid::now_v7().to_string(),
            "dataset_size": 5000,
            "dataset_name": "users",
            "processing_mode": "parallel"
        }),
        correlation_id: Uuid::now_v7(),
        parent_correlation_id: None,
        initiator: "batch_test".to_string(),
        source_system: "integration_test".to_string(),
        reason: "Testing BatchProcessingService".to_string(),
        tags: vec!["test".to_string()],
        requested_at: chrono::Utc::now().naive_utc(),
        options: None,
        priority: Some(5),
        idempotency_key: None,
    };
    let result = initializer.create_task_from_request(request).await?;
    Ok(result.task_uuid)
}

/// Complete a step through the state machine lifecycle and return the step
async fn complete_step_and_get(
    pool: &PgPool,
    system_context: &Arc<SystemContext>,
    task_uuid: Uuid,
    step_name: &str,
    result_data: serde_json::Value,
) -> Result<WorkflowStep> {
    let step = WorkflowStep::find_step_by_name(pool, task_uuid, step_name)
        .await?
        .ok_or_else(|| anyhow::anyhow!("Step '{}' not found", step_name))?;

    let mut sm = StepStateMachine::new(step.clone(), system_context.clone());
    sm.transition(StepEvent::Enqueue).await?;
    sm.transition(StepEvent::Start).await?;
    sm.transition(StepEvent::EnqueueForOrchestration(Some(
        result_data.clone(),
    )))
    .await?;
    sm.transition(StepEvent::Complete(None)).await?;

    // Re-fetch and mark as processed so the step result is persisted
    let mut updated = WorkflowStep::find_by_id(pool, step.workflow_step_uuid)
        .await?
        .ok_or_else(|| anyhow::anyhow!("Step disappeared after completion"))?;
    updated.mark_processed(pool, Some(result_data)).await?;

    Ok(updated)
}

/// Build a StepExecutionResult with a BatchProcessingOutcome embedded
fn make_batch_result(step_uuid: Uuid, outcome: &BatchProcessingOutcome) -> StepExecutionResult {
    let outcome_json = serde_json::to_value(outcome).expect("outcome should serialize");
    StepExecutionResult {
        step_uuid,
        success: true,
        result: json!({"batch_processing_outcome": outcome_json}),
        status: "completed".to_string(),
        ..Default::default()
    }
}

/// Helper to build cursor configs for testing
fn make_cursor_configs(count: usize, batch_size: u32) -> Vec<CursorConfig> {
    (0..count)
        .map(|i| CursorConfig {
            batch_id: format!("{:03}", i + 1),
            start_cursor: json!(i as u32 * batch_size),
            end_cursor: json!((i as u32 + 1) * batch_size),
            batch_size,
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Tests: NoBatches outcome
// ---------------------------------------------------------------------------

/// NoBatches creates a placeholder worker
#[sqlx::test(migrator = "tasker_shared::database::migrator::MIGRATOR")]
async fn test_process_batchable_no_batches(pool: PgPool) -> Result<()> {
    let (service, _discovery, initializer, ctx) = setup_batch_service(pool.clone()).await?;
    let task_uuid = create_batch_task(&initializer).await?;

    // Complete analyze_dataset with NoBatches outcome
    let batchable_step = complete_step_and_get(
        &pool,
        &ctx,
        task_uuid,
        "analyze_dataset",
        json!({"batch_processing_outcome": {"type": "no_batches"}}),
    )
    .await?;

    let step_result = make_batch_result(
        batchable_step.workflow_step_uuid,
        &BatchProcessingOutcome::NoBatches,
    );

    let outcome = service
        .process_batchable_step(task_uuid, &batchable_step, &step_result)
        .await?;

    assert!(
        matches!(outcome, BatchProcessingOutcome::NoBatches),
        "Should return NoBatches"
    );

    // Verify a placeholder worker was created (should have batch_dependency edge)
    let worker_edges = sqlx::query_scalar!(
        r#"
        SELECT COUNT(*) AS "count!"
        FROM tasker.workflow_step_edges
        WHERE from_step_uuid = $1 AND name = 'batch_dependency'
        "#,
        batchable_step.workflow_step_uuid
    )
    .fetch_one(&pool)
    .await?;

    assert_eq!(worker_edges, 1, "One placeholder worker edge should exist");

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests: CreateBatches outcome
// ---------------------------------------------------------------------------

/// CreateBatches with 3 cursors creates 3 workers
#[sqlx::test(migrator = "tasker_shared::database::migrator::MIGRATOR")]
async fn test_process_batchable_create_batches(pool: PgPool) -> Result<()> {
    let (service, _discovery, initializer, ctx) = setup_batch_service(pool.clone()).await?;
    let task_uuid = create_batch_task(&initializer).await?;

    let cursors = make_cursor_configs(3, 1000);
    let batchable_step = complete_step_and_get(
        &pool,
        &ctx,
        task_uuid,
        "analyze_dataset",
        json!({"batch_processing_outcome": {
            "type": "create_batches",
            "worker_template_name": "process_batch",
            "worker_count": 3,
            "cursor_configs": cursors,
            "total_items": 3000
        }}),
    )
    .await?;

    let outcome_data = BatchProcessingOutcome::CreateBatches {
        worker_template_name: "process_batch".to_string(),
        worker_count: 3,
        cursor_configs: make_cursor_configs(3, 1000),
        total_items: 3000,
    };
    let step_result = make_batch_result(batchable_step.workflow_step_uuid, &outcome_data);

    let outcome = service
        .process_batchable_step(task_uuid, &batchable_step, &step_result)
        .await?;

    match outcome {
        BatchProcessingOutcome::CreateBatches { worker_count, .. } => {
            assert_eq!(worker_count, 3, "Should report 3 workers");
        }
        other => panic!("Expected CreateBatches, got {:?}", other),
    }

    // Verify 3 batch_dependency edges from analyze_dataset
    let worker_edges = sqlx::query_scalar!(
        r#"
        SELECT COUNT(*) AS "count!"
        FROM tasker.workflow_step_edges
        WHERE from_step_uuid = $1 AND name = 'batch_dependency'
        "#,
        batchable_step.workflow_step_uuid
    )
    .fetch_one(&pool)
    .await?;

    assert_eq!(worker_edges, 3, "Three worker edges should exist");

    Ok(())
}

/// Convergence step (aggregate_results) is created for batch workflows
#[sqlx::test(migrator = "tasker_shared::database::migrator::MIGRATOR")]
async fn test_batch_creates_convergence_step(pool: PgPool) -> Result<()> {
    let (service, _discovery, initializer, ctx) = setup_batch_service(pool.clone()).await?;
    let task_uuid = create_batch_task(&initializer).await?;

    let batchable_step = complete_step_and_get(
        &pool,
        &ctx,
        task_uuid,
        "analyze_dataset",
        json!({"batch_processing_outcome": {"type": "no_batches"}}),
    )
    .await?;

    let step_result = make_batch_result(
        batchable_step.workflow_step_uuid,
        &BatchProcessingOutcome::NoBatches,
    );

    service
        .process_batchable_step(task_uuid, &batchable_step, &step_result)
        .await?;

    // Verify aggregate_results was created as a convergence step
    let convergence_step =
        WorkflowStep::find_step_by_name(&pool, task_uuid, "aggregate_results").await?;

    assert!(
        convergence_step.is_some(),
        "aggregate_results convergence step should be created"
    );

    Ok(())
}

/// Worker-to-convergence edges are properly wired
#[sqlx::test(migrator = "tasker_shared::database::migrator::MIGRATOR")]
async fn test_batch_worker_edges_created(pool: PgPool) -> Result<()> {
    let (service, _discovery, initializer, ctx) = setup_batch_service(pool.clone()).await?;
    let task_uuid = create_batch_task(&initializer).await?;

    let cursors = make_cursor_configs(2, 1000);
    let batchable_step = complete_step_and_get(
        &pool,
        &ctx,
        task_uuid,
        "analyze_dataset",
        json!({"batch_processing_outcome": {
            "type": "create_batches",
            "worker_template_name": "process_batch",
            "worker_count": 2,
            "cursor_configs": cursors,
            "total_items": 2000
        }}),
    )
    .await?;

    let outcome_data = BatchProcessingOutcome::CreateBatches {
        worker_template_name: "process_batch".to_string(),
        worker_count: 2,
        cursor_configs: make_cursor_configs(2, 1000),
        total_items: 2000,
    };
    let step_result = make_batch_result(batchable_step.workflow_step_uuid, &outcome_data);

    service
        .process_batchable_step(task_uuid, &batchable_step, &step_result)
        .await?;

    // Verify worker_to_convergence edges exist
    let convergence_edges = sqlx::query_scalar!(
        r#"
        SELECT COUNT(*) AS "count!"
        FROM tasker.workflow_step_edges
        WHERE name = 'worker_to_convergence'
          AND to_step_uuid IN (
              SELECT ws.workflow_step_uuid
              FROM tasker.workflow_steps ws
              JOIN tasker.named_steps ns ON ws.named_step_uuid = ns.named_step_uuid
              WHERE ws.task_uuid = $1 AND ns.name = 'aggregate_results'
          )
        "#,
        task_uuid
    )
    .fetch_one(&pool)
    .await?;

    assert_eq!(
        convergence_edges, 2,
        "Two worker_to_convergence edges should point to aggregate_results"
    );

    Ok(())
}

/// Idempotency: second call returns same outcome without creating duplicates
#[sqlx::test(migrator = "tasker_shared::database::migrator::MIGRATOR")]
async fn test_batch_idempotency(pool: PgPool) -> Result<()> {
    let (service, _discovery, initializer, ctx) = setup_batch_service(pool.clone()).await?;
    let task_uuid = create_batch_task(&initializer).await?;

    let batchable_step = complete_step_and_get(
        &pool,
        &ctx,
        task_uuid,
        "analyze_dataset",
        json!({"batch_processing_outcome": {"type": "no_batches"}}),
    )
    .await?;

    let step_result = make_batch_result(
        batchable_step.workflow_step_uuid,
        &BatchProcessingOutcome::NoBatches,
    );

    // First call
    service
        .process_batchable_step(task_uuid, &batchable_step, &step_result)
        .await?;

    // Second call should succeed idempotently
    let result2 = service
        .process_batchable_step(task_uuid, &batchable_step, &step_result)
        .await?;

    assert!(
        matches!(result2, BatchProcessingOutcome::NoBatches),
        "Idempotent call should return same outcome"
    );

    // Verify still only 1 worker edge (not duplicated)
    let edge_count = sqlx::query_scalar!(
        r#"
        SELECT COUNT(*) AS "count!"
        FROM tasker.workflow_step_edges
        WHERE from_step_uuid = $1 AND name = 'batch_dependency'
        "#,
        batchable_step.workflow_step_uuid
    )
    .fetch_one(&pool)
    .await?;

    assert_eq!(
        edge_count, 1,
        "Idempotent call should not create duplicate workers"
    );

    Ok(())
}

/// Workers are named following the {template}_{batch_id} convention
#[sqlx::test(migrator = "tasker_shared::database::migrator::MIGRATOR")]
async fn test_batch_worker_names(pool: PgPool) -> Result<()> {
    let (service, _discovery, initializer, ctx) = setup_batch_service(pool.clone()).await?;
    let task_uuid = create_batch_task(&initializer).await?;

    let cursors = make_cursor_configs(2, 1000);
    let batchable_step = complete_step_and_get(
        &pool,
        &ctx,
        task_uuid,
        "analyze_dataset",
        json!({"batch_processing_outcome": {
            "type": "create_batches",
            "worker_template_name": "process_batch",
            "worker_count": 2,
            "cursor_configs": cursors,
            "total_items": 2000
        }}),
    )
    .await?;

    let outcome_data = BatchProcessingOutcome::CreateBatches {
        worker_template_name: "process_batch".to_string(),
        worker_count: 2,
        cursor_configs: make_cursor_configs(2, 1000),
        total_items: 2000,
    };
    let step_result = make_batch_result(batchable_step.workflow_step_uuid, &outcome_data);

    service
        .process_batchable_step(task_uuid, &batchable_step, &step_result)
        .await?;

    // Verify worker names follow convention: process_batch_{batch_id}
    let worker_step_1 =
        WorkflowStep::find_step_by_name(&pool, task_uuid, "process_batch_001").await?;
    let worker_step_2 =
        WorkflowStep::find_step_by_name(&pool, task_uuid, "process_batch_002").await?;

    assert!(
        worker_step_1.is_some(),
        "Worker process_batch_001 should exist"
    );
    assert!(
        worker_step_2.is_some(),
        "Worker process_batch_002 should exist"
    );

    Ok(())
}

/// After batch processing, workers become viable steps
#[sqlx::test(migrator = "tasker_shared::database::migrator::MIGRATOR")]
async fn test_batch_workers_become_viable(pool: PgPool) -> Result<()> {
    let (service, discovery, initializer, ctx) = setup_batch_service(pool.clone()).await?;
    let task_uuid = create_batch_task(&initializer).await?;

    let cursors = make_cursor_configs(2, 1000);
    let batchable_step = complete_step_and_get(
        &pool,
        &ctx,
        task_uuid,
        "analyze_dataset",
        json!({"batch_processing_outcome": {
            "type": "create_batches",
            "worker_template_name": "process_batch",
            "worker_count": 2,
            "cursor_configs": cursors,
            "total_items": 2000
        }}),
    )
    .await?;

    let outcome_data = BatchProcessingOutcome::CreateBatches {
        worker_template_name: "process_batch".to_string(),
        worker_count: 2,
        cursor_configs: make_cursor_configs(2, 1000),
        total_items: 2000,
    };
    let step_result = make_batch_result(batchable_step.workflow_step_uuid, &outcome_data);

    service
        .process_batchable_step(task_uuid, &batchable_step, &step_result)
        .await?;

    // Workers should be discoverable as viable steps
    let viable = discovery.find_viable_steps(task_uuid).await?;
    let viable_names: Vec<&str> = viable.iter().map(|s| s.name.as_str()).collect();

    assert!(
        viable_names.contains(&"process_batch_001"),
        "Worker process_batch_001 should be viable. Found: {:?}",
        viable_names
    );
    assert!(
        viable_names.contains(&"process_batch_002"),
        "Worker process_batch_002 should be viable. Found: {:?}",
        viable_names
    );

    Ok(())
}

/// Non-batchable step returns error
#[sqlx::test(migrator = "tasker_shared::database::migrator::MIGRATOR")]
async fn test_batch_processing_error_invalid_result(pool: PgPool) -> Result<()> {
    let (service, _discovery, initializer, ctx) = setup_batch_service(pool.clone()).await?;
    let task_uuid = create_batch_task(&initializer).await?;

    let batchable_step = complete_step_and_get(
        &pool,
        &ctx,
        task_uuid,
        "analyze_dataset",
        json!({"some_other_result": true}),
    )
    .await?;

    // Create a step result WITHOUT batch_processing_outcome
    let step_result = StepExecutionResult {
        step_uuid: batchable_step.workflow_step_uuid,
        success: true,
        result: json!({"no_batch_outcome_here": true}),
        status: "completed".to_string(),
        ..Default::default()
    };

    let result = service
        .process_batchable_step(task_uuid, &batchable_step, &step_result)
        .await;

    assert!(
        result.is_err(),
        "Should fail when result lacks batch_processing_outcome"
    );
    match result.unwrap_err() {
        BatchProcessingError::ResultParsing(msg) => {
            assert!(
                msg.contains("batch processing outcome"),
                "Error should mention missing outcome: {}",
                msg
            );
        }
        other => panic!("Expected ResultParsing, got {:?}", other),
    }

    Ok(())
}

/// Worker step initialization contains cursor data
#[sqlx::test(migrator = "tasker_shared::database::migrator::MIGRATOR")]
async fn test_batch_worker_inputs_contain_cursor(pool: PgPool) -> Result<()> {
    let (service, _discovery, initializer, ctx) = setup_batch_service(pool.clone()).await?;
    let task_uuid = create_batch_task(&initializer).await?;

    let cursors = make_cursor_configs(1, 500);
    let batchable_step = complete_step_and_get(
        &pool,
        &ctx,
        task_uuid,
        "analyze_dataset",
        json!({"batch_processing_outcome": {
            "type": "create_batches",
            "worker_template_name": "process_batch",
            "worker_count": 1,
            "cursor_configs": cursors,
            "total_items": 500
        }}),
    )
    .await?;

    let outcome_data = BatchProcessingOutcome::CreateBatches {
        worker_template_name: "process_batch".to_string(),
        worker_count: 1,
        cursor_configs: make_cursor_configs(1, 500),
        total_items: 500,
    };
    let step_result = make_batch_result(batchable_step.workflow_step_uuid, &outcome_data);

    service
        .process_batchable_step(task_uuid, &batchable_step, &step_result)
        .await?;

    // Fetch the created worker and check its inputs contain cursor data
    let worker = WorkflowStep::find_step_by_name(&pool, task_uuid, "process_batch_001")
        .await?
        .expect("Worker should exist");

    let inputs = worker.inputs.expect("Worker should have inputs");
    assert!(
        inputs.get("cursor").is_some(),
        "Worker inputs should contain cursor configuration. Got: {}",
        inputs
    );

    Ok(())
}