ironclad-schedule 0.9.7

Cron/heartbeat scheduler with DB-backed leases and wake signaling
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
//! # ironclad-schedule
//!
//! Unified cron/heartbeat scheduler for the Ironclad agent runtime. Jobs are
//! persisted in SQLite (`ironclad-db/cron.rs`) with lease-based mutual
//! exclusion to prevent duplicate execution across restarts.
//!
//! ## Key Types
//!
//! - [`HeartbeatDaemon`] -- Periodic tick loop driving registered heartbeat tasks
//! - [`DurableScheduler`] -- Cron expression and fixed-interval evaluation
//! - [`HeartbeatTask`] / [`TaskResult`] -- Pluggable task trait and outcome type
//!
//! ## Modules
//!
//! - `heartbeat` -- Heartbeat daemon loop with wallet and DB context
//! - `scheduler` -- Cron expression parsing (`evaluate_cron`) and interval checks
//! - `tasks` -- `HeartbeatTask` trait and built-in task implementations
//!
//! ## Entry Points
//!
//! - [`run_heartbeat()`] -- Start the heartbeat daemon
//! - [`run_cron_worker()`] -- Start the cron worker (lease, execute, record)

pub mod heartbeat;
pub mod scheduler;
pub mod tasks;

pub use heartbeat::run as run_heartbeat;
pub use heartbeat::{HeartbeatDaemon, TickContext};
pub use scheduler::DurableScheduler;
pub use tasks::{HeartbeatTask, TaskResult};

/// Cron worker loop: evaluates due jobs, acquires leases, executes, and records results.
pub async fn run_cron_worker(db: ironclad_db::Database, instance_id: String) {
    use std::time::Duration;

    let mut interval = tokio::time::interval(Duration::from_secs(60));
    tracing::info!("Cron worker started");

    loop {
        interval.tick().await;

        let jobs = match ironclad_db::cron::list_jobs(&db) {
            Ok(j) => j,
            Err(e) => {
                tracing::warn!(error = %e, "Failed to list cron jobs");
                continue;
            }
        };

        let now = chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true);

        for job in &jobs {
            if !job.enabled {
                continue;
            }

            let kind = normalize_schedule_kind(&job.schedule_kind);
            let due = match kind {
                "cron" => job
                    .schedule_expr
                    .as_deref()
                    .map(|expr| {
                        DurableScheduler::evaluate_cron(expr, job.last_run_at.as_deref(), &now)
                    })
                    .unwrap_or(false),
                "every" => {
                    let interval_ms = job
                        .schedule_every_ms
                        .or_else(|| {
                            job.schedule_expr
                                .as_deref()
                                .and_then(parse_interval_expr_to_ms)
                        })
                        .unwrap_or(60_000);
                    DurableScheduler::evaluate_interval(
                        job.last_run_at.as_deref(),
                        interval_ms,
                        &now,
                    )
                }
                _ => false,
            };

            if !due {
                continue;
            }

            match ironclad_db::cron::acquire_lease(&db, &job.id, &instance_id) {
                Ok(acquired) => {
                    if !acquired {
                        continue;
                    }
                }
                Err(e) => {
                    tracing::warn!(job_id = %job.id, error = %e, "failed to acquire cron lease");
                    continue;
                }
            }

            tracing::debug!(job = %job.name, "Executing cron job");
            let start = std::time::Instant::now();

            let (result_status, error_msg) = execute_cron_job(&db, job);
            let duration = start.elapsed().as_millis() as i64;

            if let Err(e) = ironclad_db::cron::record_run(
                &db,
                &job.id,
                result_status,
                Some(duration),
                error_msg.as_deref(),
                None,
            ) {
                tracing::warn!(job_id = %job.id, error = %e, "failed to record cron run");
            }
            // Persist next_run_at so the API can expose it
            let now_str = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%S").to_string();
            let next = DurableScheduler::calculate_next_run(
                kind,
                job.schedule_expr.as_deref(),
                job.schedule_every_ms,
                &now_str,
            );
            if let Err(e) = ironclad_db::cron::update_next_run_at(&db, &job.id, next.as_deref()) {
                tracing::warn!(job_id = %job.id, error = %e, "failed to update next_run_at");
            }
            if let Err(e) = ironclad_db::cron::release_lease(&db, &job.id, &instance_id) {
                tracing::warn!(job_id = %job.id, error = %e, "failed to release cron lease");
            }
        }
    }
}

/// Execute a cron job based on its payload. Returns (status, optional error message).
fn execute_cron_job(
    db: &ironclad_db::Database,
    job: &ironclad_db::cron::CronJob,
) -> (&'static str, Option<String>) {
    let payload: serde_json::Value = match serde_json::from_str(&job.payload_json) {
        Ok(v) => v,
        Err(e) => {
            tracing::warn!(job = %job.name, error = %e, "invalid job payload JSON");
            return ("error", Some(format!("invalid payload: {e}")));
        }
    };

    let action = payload
        .get("action")
        .and_then(|v| v.as_str())
        .unwrap_or("unknown");

    match action {
        "log" => {
            let message = payload
                .get("message")
                .and_then(|v| v.as_str())
                .unwrap_or("cron heartbeat");
            tracing::info!(job = %job.name, message, "cron job executed");
            ("success", None)
        }
        "metric_snapshot" => {
            let snapshot = serde_json::json!({
                "job_id": job.id,
                "job_name": job.name,
                "schedule_kind": job.schedule_kind,
                "timestamp": chrono::Utc::now().to_rfc3339(),
            });
            match ironclad_db::metrics::record_metric_snapshot(db, &snapshot.to_string()) {
                Ok(_) => ("success", None),
                Err(e) => ("error", Some(format!("metric_snapshot failed: {e}"))),
            }
        }
        "expire_sessions" => {
            let ttl_seconds = payload
                .get("ttl_seconds")
                .and_then(|v| v.as_u64())
                .unwrap_or(86_400);
            match ironclad_db::sessions::expire_stale_sessions(db, ttl_seconds) {
                Ok(expired) => {
                    tracing::info!(job = %job.name, expired, ttl_seconds, "expired stale sessions");
                    ("success", None)
                }
                Err(e) => ("error", Some(format!("expire_sessions failed: {e}"))),
            }
        }
        "record_transaction" => {
            let tx_type = payload
                .get("tx_type")
                .and_then(|v| v.as_str())
                .unwrap_or("cron");
            let amount = payload
                .get("amount")
                .and_then(|v| v.as_f64())
                .unwrap_or(0.0);
            let currency = payload
                .get("currency")
                .and_then(|v| v.as_str())
                .unwrap_or("USD");
            let counterparty = payload.get("counterparty").and_then(|v| v.as_str());
            let tx_hash = payload.get("tx_hash").and_then(|v| v.as_str());
            match ironclad_db::metrics::record_transaction(
                db,
                tx_type,
                amount,
                currency,
                counterparty,
                tx_hash,
            ) {
                Ok(_) => ("success", None),
                Err(e) => ("error", Some(format!("record_transaction failed: {e}"))),
            }
        }
        "noop" => {
            tracing::debug!(job = %job.name, "noop cron job");
            ("success", None)
        }
        other => {
            tracing::warn!(job = %job.name, action = other, "unknown cron action");
            ("error", Some(format!("unknown action: {other}")))
        }
    }
}

fn normalize_schedule_kind(kind: &str) -> &str {
    match kind {
        "interval" => "every",
        "every" | "cron" => kind,
        _ => kind,
    }
}

fn parse_interval_expr_to_ms(expr: &str) -> Option<i64> {
    if expr.is_empty() {
        return None;
    }
    let (unit_byte_offset, unit) = expr.char_indices().last()?;
    let qty = expr[..unit_byte_offset].parse::<i64>().ok()?;
    let ms = match unit {
        's' | 'S' => qty.saturating_mul(1_000),
        'm' | 'M' => qty.saturating_mul(60_000),
        'h' | 'H' => qty.saturating_mul(3_600_000),
        _ => return None,
    };
    if ms > 0 { Some(ms) } else { None }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ironclad_db::Database;

    fn test_db() -> Database {
        Database::new(":memory:").expect("in-memory db")
    }

    fn job_with_payload(
        db: &Database,
        name: &str,
        payload_json: &str,
    ) -> ironclad_db::cron::CronJob {
        let job_id = ironclad_db::cron::create_job(
            db,
            name,
            "test-agent",
            "every",
            Some("5m"),
            payload_json,
        )
        .expect("create job");
        ironclad_db::cron::get_job(db, &job_id)
            .expect("get job")
            .expect("job exists")
    }

    #[test]
    fn normalize_schedule_kind_maps_interval_to_every() {
        assert_eq!(normalize_schedule_kind("interval"), "every");
        assert_eq!(normalize_schedule_kind("every"), "every");
        assert_eq!(normalize_schedule_kind("cron"), "cron");
        assert_eq!(normalize_schedule_kind("custom"), "custom");
    }

    #[test]
    fn parse_interval_expr_to_ms_parses_supported_units() {
        assert_eq!(parse_interval_expr_to_ms("5s"), Some(5_000));
        assert_eq!(parse_interval_expr_to_ms("2m"), Some(120_000));
        assert_eq!(parse_interval_expr_to_ms("3h"), Some(10_800_000));
        assert_eq!(parse_interval_expr_to_ms("7S"), Some(7_000));
        assert_eq!(parse_interval_expr_to_ms("1M"), Some(60_000));
    }

    #[test]
    fn parse_interval_expr_to_ms_rejects_invalid_values() {
        assert_eq!(parse_interval_expr_to_ms(""), None);
        assert_eq!(parse_interval_expr_to_ms("10"), None);
        assert_eq!(parse_interval_expr_to_ms("xs"), None);
        assert_eq!(parse_interval_expr_to_ms("0s"), None);
        assert_eq!(parse_interval_expr_to_ms("-5m"), None);
    }

    #[test]
    fn execute_cron_job_rejects_invalid_payload_json() {
        let db = test_db();
        let job = job_with_payload(&db, "bad-json", "{not-json}");
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "error");
        assert!(error.unwrap_or_default().contains("invalid payload"));
    }

    #[test]
    fn execute_cron_job_handles_log_and_noop_actions() {
        let db = test_db();
        let log_job = job_with_payload(&db, "log-job", r#"{"action":"log","message":"hello"}"#);
        let (status, error) = execute_cron_job(&db, &log_job);
        assert_eq!(status, "success");
        assert!(error.is_none());

        let noop_job = job_with_payload(&db, "noop-job", r#"{"action":"noop"}"#);
        let (status, error) = execute_cron_job(&db, &noop_job);
        assert_eq!(status, "success");
        assert!(error.is_none());
    }

    #[test]
    fn execute_cron_job_records_metric_snapshot() {
        let db = test_db();
        let job = job_with_payload(&db, "metrics-job", r#"{"action":"metric_snapshot"}"#);
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "success");
        assert!(error.is_none());

        let count: i64 = db
            .conn()
            .query_row("SELECT COUNT(*) FROM metric_snapshots", [], |row| {
                row.get(0)
            })
            .expect("count snapshots");
        assert_eq!(count, 1);
    }

    #[test]
    fn execute_cron_job_expires_stale_sessions() {
        let db = test_db();
        let session_id = ironclad_db::sessions::find_or_create(&db, "expire-agent", None)
            .expect("create session");
        db.conn()
            .execute(
                "UPDATE sessions SET updated_at = datetime('now', '-2 days') WHERE id = ?1",
                [&session_id],
            )
            .expect("age session");

        let job = job_with_payload(
            &db,
            "expire-job",
            r#"{"action":"expire_sessions","ttl_seconds":60}"#,
        );
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "success");
        assert!(error.is_none());

        let status: String = db
            .conn()
            .query_row(
                "SELECT status FROM sessions WHERE id = ?1",
                [&session_id],
                |row| row.get(0),
            )
            .expect("session status");
        assert_eq!(status, "expired");
    }

    #[test]
    fn execute_cron_job_records_transaction() {
        let db = test_db();
        let job = job_with_payload(
            &db,
            "tx-job",
            r#"{"action":"record_transaction","tx_type":"ops","amount":1.25,"currency":"USD","counterparty":"scheduler"}"#,
        );
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "success");
        assert!(error.is_none());

        let txs = ironclad_db::metrics::query_transactions(&db, 24).expect("query txs");
        assert_eq!(txs.len(), 1);
        assert_eq!(txs[0].tx_type, "ops");
        assert_eq!(txs[0].currency, "USD");
    }

    #[test]
    fn execute_cron_job_rejects_unknown_action() {
        let db = test_db();
        let job = job_with_payload(&db, "unknown-job", r#"{"action":"mystery"}"#);
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "error");
        assert!(error.unwrap_or_default().contains("unknown action"));
    }

    #[test]
    fn execute_cron_job_rejects_legacy_agent_turn_kind() {
        let db = test_db();
        let job = job_with_payload(
            &db,
            "legacy-agent-turn",
            r#"{"kind":"agentTurn","message":"Do work"}"#,
        );
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "error");
        assert_eq!(error.as_deref(), Some("unknown action: unknown"));
    }

    #[test]
    fn execute_cron_job_rejects_legacy_metric_snapshot_kind() {
        let db = test_db();
        let job = job_with_payload(&db, "legacy-metrics", r#"{"kind":"metricSnapshot"}"#);
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "error");
        assert_eq!(error.as_deref(), Some("unknown action: unknown"));
    }

    // ── BUG-093: normalize_schedule_kind boundary cases ────────────────
    #[test]
    fn normalize_schedule_kind_pass_through_unknown_kinds() {
        assert_eq!(normalize_schedule_kind(""), "");
        assert_eq!(normalize_schedule_kind("once"), "once");
        assert_eq!(normalize_schedule_kind("at"), "at");
        assert_eq!(normalize_schedule_kind("weekly"), "weekly");
    }

    // ── BUG-094: parse_interval_expr_to_ms edge cases ──────────────────
    #[test]
    fn parse_interval_expr_to_ms_uppercase_h() {
        assert_eq!(parse_interval_expr_to_ms("1H"), Some(3_600_000));
        assert_eq!(parse_interval_expr_to_ms("2H"), Some(7_200_000));
    }

    #[test]
    fn parse_interval_expr_to_ms_single_char() {
        // Single character like "s" has no numeric part
        assert_eq!(parse_interval_expr_to_ms("s"), None);
        assert_eq!(parse_interval_expr_to_ms("m"), None);
        assert_eq!(parse_interval_expr_to_ms("h"), None);
    }

    #[test]
    fn parse_interval_expr_to_ms_large_values() {
        // 24h = 86_400_000
        assert_eq!(parse_interval_expr_to_ms("24h"), Some(86_400_000));
        // 1000s = 1_000_000
        assert_eq!(parse_interval_expr_to_ms("1000s"), Some(1_000_000));
    }

    #[test]
    fn parse_interval_expr_to_ms_unknown_unit() {
        assert_eq!(parse_interval_expr_to_ms("5d"), None); // days not supported
        assert_eq!(parse_interval_expr_to_ms("3w"), None); // weeks not supported
    }

    // ── execute_cron_job: log action with default message ──────────────
    #[test]
    fn execute_cron_job_log_action_with_default_message() {
        let db = test_db();
        // No "message" key in payload -> should use default "cron heartbeat"
        let job = job_with_payload(&db, "log-default", r#"{"action":"log"}"#);
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "success");
        assert!(error.is_none());
    }

    // ── execute_cron_job: expire_sessions with default TTL ─────────────
    #[test]
    fn execute_cron_job_expire_sessions_uses_default_ttl() {
        let db = test_db();
        // No "ttl_seconds" -> should use default 86_400
        let job = job_with_payload(&db, "expire-default", r#"{"action":"expire_sessions"}"#);
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "success");
        assert!(error.is_none());
    }

    // ── execute_cron_job: record_transaction with defaults ─────────────
    #[test]
    fn execute_cron_job_record_transaction_uses_defaults() {
        let db = test_db();
        // Minimal payload: no tx_type, amount, currency, counterparty, tx_hash
        let job = job_with_payload(&db, "tx-minimal", r#"{"action":"record_transaction"}"#);
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "success");
        assert!(error.is_none());

        let txs = ironclad_db::metrics::query_transactions(&db, 24).expect("query txs");
        assert_eq!(txs.len(), 1);
        assert_eq!(txs[0].tx_type, "cron"); // default tx_type
        assert_eq!(txs[0].currency, "USD"); // default currency
    }

    // ── execute_cron_job: record_transaction with tx_hash ──────────────
    #[test]
    fn execute_cron_job_record_transaction_with_tx_hash() {
        let db = test_db();
        let job = job_with_payload(
            &db,
            "tx-with-hash",
            r#"{"action":"record_transaction","tx_type":"payment","amount":42.0,"currency":"ETH","counterparty":"alice","tx_hash":"0xabc"}"#,
        );
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "success");
        assert!(error.is_none());

        let txs = ironclad_db::metrics::query_transactions(&db, 24).expect("query txs");
        assert_eq!(txs.len(), 1);
        assert_eq!(txs[0].tx_type, "payment");
        assert_eq!(txs[0].currency, "ETH");
    }

    // ── execute_cron_job: action-dispatch success paths ───────────────────
    #[test]
    fn execute_cron_job_expire_sessions_action() {
        let db = test_db();
        let job = job_with_payload(
            &db,
            "expire-action",
            r#"{"action":"expire_sessions","ttl_seconds":3600}"#,
        );
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "success");
        assert!(error.is_none());
    }

    #[test]
    fn execute_cron_job_record_transaction_action() {
        let db = test_db();
        let job = job_with_payload(
            &db,
            "tx-action",
            r#"{"action":"record_transaction","amount":5.0}"#,
        );
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "success");
        assert!(error.is_none());
    }

    #[test]
    fn execute_cron_job_log_action() {
        let db = test_db();
        let job = job_with_payload(&db, "log-action", r#"{"action":"log","message":"test"}"#);
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "success");
        assert!(error.is_none());
    }

    #[test]
    fn execute_cron_job_noop_action() {
        let db = test_db();
        let job = job_with_payload(&db, "noop-action", r#"{"action":"noop"}"#);
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "success");
        assert!(error.is_none());
    }

    // ── execute_cron_job: unknown action returns error ─────────────────────
    #[test]
    fn execute_cron_job_unknown_action() {
        let db = test_db();
        let job = job_with_payload(&db, "legacy-unknown", r#"{"kind":"foobar"}"#);
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "error");
        assert!(error.unwrap_or_default().contains("unknown action"));
    }

    // ── execute_cron_job: no action or kind -> "unknown" ───────────────
    #[test]
    fn execute_cron_job_no_action_or_kind_is_unknown() {
        let db = test_db();
        let job = job_with_payload(&db, "empty-payload", r#"{"data":"value"}"#);
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "error");
        assert!(error.unwrap_or_default().contains("unknown action"));
    }

    // ── execute_cron_job: empty object payload ─────────────────────────
    #[test]
    fn execute_cron_job_empty_object_payload() {
        let db = test_db();
        let job = job_with_payload(&db, "empty-obj", r#"{}"#);
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "error");
        assert!(error.unwrap_or_default().contains("unknown action"));
    }

    // ── execute_cron_job: action takes precedence over kind ────────────
    #[test]
    fn execute_cron_job_action_takes_precedence_over_kind() {
        let db = test_db();
        // Both action and kind are present; action should win
        let job = job_with_payload(&db, "precedence", r#"{"action":"noop","kind":"agentTurn"}"#);
        let (status, error) = execute_cron_job(&db, &job);
        assert_eq!(status, "success");
        assert!(error.is_none());
    }

    // ── run_cron_worker async integration tests ─────────────────────────
    // These tests exercise the async cron worker loop by spawning it with
    // tokio time control and aborting after one iteration completes.

    fn create_due_job(db: &Database, name: &str, payload: &str) -> String {
        let job_id =
            ironclad_db::cron::create_job(db, name, "test-agent", "every", Some("1s"), payload)
                .expect("create job");
        // Set schedule_every_ms to 1 so the job is immediately due
        db.conn()
            .execute(
                "UPDATE cron_jobs SET schedule_every_ms = 1 WHERE id = ?1",
                [&job_id],
            )
            .expect("update schedule_every_ms");
        job_id
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_executes_due_log_job() {
        let db = test_db();
        let _job_id = create_due_job(&db, "worker-log", r#"{"action":"log","message":"test"}"#);

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "test-instance".into()).await;
        });

        // Advance past the 60s interval to trigger one tick
        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        // Yield to let the worker process
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        // Verify the job was executed by checking cron_runs table
        let count: i64 = db
            .conn()
            .query_row("SELECT COUNT(*) FROM cron_runs", [], |row| row.get(0))
            .expect("count runs");
        assert!(count >= 1, "expected at least one cron run, got {count}");
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_executes_noop_job() {
        let db = test_db();
        let _job_id = create_due_job(&db, "worker-noop", r#"{"action":"noop"}"#);

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "noop-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        let count: i64 = db
            .conn()
            .query_row("SELECT COUNT(*) FROM cron_runs", [], |row| row.get(0))
            .expect("count runs");
        assert!(count >= 1, "expected at least one cron run");
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_executes_metric_snapshot_job() {
        let db = test_db();
        let _job_id = create_due_job(&db, "worker-metric", r#"{"action":"metric_snapshot"}"#);

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "metric-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        let snap_count: i64 = db
            .conn()
            .query_row("SELECT COUNT(*) FROM metric_snapshots", [], |row| {
                row.get(0)
            })
            .expect("count snapshots");
        assert!(snap_count >= 1, "expected at least one metric snapshot");
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_executes_expire_sessions_job() {
        let db = test_db();
        // Create a session that's old enough to expire
        let session_id = ironclad_db::sessions::find_or_create(&db, "cron-expire-agent", None)
            .expect("create session");
        db.conn()
            .execute(
                "UPDATE sessions SET updated_at = datetime('now', '-2 days') WHERE id = ?1",
                [&session_id],
            )
            .expect("age session");

        let _job_id = create_due_job(
            &db,
            "worker-expire",
            r#"{"action":"expire_sessions","ttl_seconds":60}"#,
        );

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "expire-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        let status: String = db
            .conn()
            .query_row(
                "SELECT status FROM sessions WHERE id = ?1",
                [&session_id],
                |row| row.get(0),
            )
            .expect("session status");
        assert_eq!(status, "expired");
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_executes_record_transaction_job() {
        let db = test_db();
        let _job_id = create_due_job(
            &db,
            "worker-tx",
            r#"{"action":"record_transaction","tx_type":"cron_test","amount":99.0,"currency":"USDC"}"#,
        );

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "tx-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        let txs = ironclad_db::metrics::query_transactions(&db, 24).expect("query txs");
        assert!(!txs.is_empty(), "expected at least one transaction");
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_skips_disabled_job() {
        let db = test_db();
        let job_id = create_due_job(
            &db,
            "worker-disabled",
            r#"{"action":"log","message":"skip"}"#,
        );
        // Disable the job
        db.conn()
            .execute("UPDATE cron_jobs SET enabled = 0 WHERE id = ?1", [&job_id])
            .expect("disable job");

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "disabled-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        // Disabled job should not produce any cron runs
        let count: i64 = db
            .conn()
            .query_row("SELECT COUNT(*) FROM cron_runs", [], |row| row.get(0))
            .expect("count runs");
        assert_eq!(count, 0, "disabled job should not be executed");
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_handles_unknown_action_job() {
        let db = test_db();
        let _job_id = create_due_job(&db, "worker-unknown", r#"{"action":"nonexistent"}"#);

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "unknown-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        // Should have recorded an error run
        let error_count: i64 = db
            .conn()
            .query_row(
                "SELECT COUNT(*) FROM cron_runs WHERE status = 'error'",
                [],
                |row| row.get(0),
            )
            .expect("count error runs");
        assert!(error_count >= 1, "expected at least one error run");
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_handles_invalid_json_job() {
        let db = test_db();
        let _job_id = create_due_job(&db, "worker-badjson", "{not-valid-json}");

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "badjson-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        let error_count: i64 = db
            .conn()
            .query_row(
                "SELECT COUNT(*) FROM cron_runs WHERE status = 'error'",
                [],
                |row| row.get(0),
            )
            .expect("count error runs");
        assert!(error_count >= 1, "expected error run for invalid JSON");
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_handles_legacy_agent_turn_job() {
        let db = test_db();
        let _job_id = create_due_job(
            &db,
            "worker-legacy-turn",
            r#"{"kind":"agentTurn","message":"hello"}"#,
        );

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "legacy-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        // Legacy "kind" payloads are no longer mapped — they resolve to "unknown" action
        // and produce an error run.
        let error_count: i64 = db
            .conn()
            .query_row(
                "SELECT COUNT(*) FROM cron_runs WHERE status = 'error'",
                [],
                |row| row.get(0),
            )
            .expect("count error runs");
        assert!(
            error_count >= 1,
            "expected error run for unmapped legacy agent turn kind"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_with_no_jobs_does_not_crash() {
        let db = test_db();

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "empty-instance".into()).await;
        });

        // Advance past a tick with no jobs
        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        // No crash, no runs
        let count: i64 = db
            .conn()
            .query_row("SELECT COUNT(*) FROM cron_runs", [], |row| row.get(0))
            .expect("count runs");
        assert_eq!(count, 0);
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_cron_schedule_kind_job() {
        let db = test_db();
        // Create a job with "cron" kind that uses a cron expression matching every minute
        let job_id = ironclad_db::cron::create_job(
            &db,
            "worker-cron-kind",
            "test-agent",
            "cron",
            Some("* * * * *"),
            r#"{"action":"log","message":"cron tick"}"#,
        )
        .expect("create cron job");

        // The cron expression "* * * * *" matches every minute.
        // We don't set last_run_at, so it should be evaluated as due.

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "cron-kind-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(100)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        // The cron evaluation depends on real wall-clock matching chrono::Utc::now().
        // In paused-time tests, Utc::now() still returns the real time, so the cron
        // expression "* * * * *" should match at most times.
        // We check that the job was at least attempted (run recorded).
        let count: i64 = db
            .conn()
            .query_row(
                &format!("SELECT COUNT(*) FROM cron_runs WHERE job_id = '{}'", job_id),
                [],
                |row| row.get(0),
            )
            .expect("count cron runs");
        // Cron jobs depend on wall time matching. If it happens to match, count >= 1.
        // We don't assert strictly because wall clock vs cron expression may not align
        // in CI, but the code path is still exercised.
        let _ = count;
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_unknown_schedule_kind_not_due() {
        let db = test_db();
        // Create a job with an unknown schedule kind -- should not be treated as due
        let job_id = ironclad_db::cron::create_job(
            &db,
            "worker-unknown-kind",
            "test-agent",
            "weekly",
            Some("*"),
            r#"{"action":"log","message":"weekly"}"#,
        )
        .expect("create job");

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "unknown-kind-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        // Unknown schedule_kind -> due = false -> no runs recorded for that job
        let count: i64 = db
            .conn()
            .query_row(
                &format!("SELECT COUNT(*) FROM cron_runs WHERE job_id = '{}'", job_id),
                [],
                |row| row.get(0),
            )
            .expect("count runs");
        assert_eq!(count, 0, "unknown schedule kind should not be executed");
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_interval_kind_job() {
        let db = test_db();
        // Create a job with "interval" schedule_kind (gets normalized to "every")
        let job_id = ironclad_db::cron::create_job(
            &db,
            "worker-interval-kind",
            "test-agent",
            "interval",
            Some("1s"),
            r#"{"action":"noop"}"#,
        )
        .expect("create job");
        // Set schedule_every_ms to 1 so it's immediately due
        db.conn()
            .execute(
                "UPDATE cron_jobs SET schedule_every_ms = 1 WHERE id = ?1",
                [&job_id],
            )
            .expect("update ms");

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "interval-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        let count: i64 = db
            .conn()
            .query_row(
                &format!("SELECT COUNT(*) FROM cron_runs WHERE job_id = '{}'", job_id),
                [],
                |row| row.get(0),
            )
            .expect("count runs");
        assert!(count >= 1, "interval job should have been executed");
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_every_kind_with_expr_fallback() {
        let db = test_db();
        // Create a job with "every" kind and schedule_expr but no schedule_every_ms
        // This tests the or_else fallback to parse_interval_expr_to_ms
        let job_id = ironclad_db::cron::create_job(
            &db,
            "worker-every-expr",
            "test-agent",
            "every",
            Some("1s"),
            r#"{"action":"noop"}"#,
        )
        .expect("create job");
        // Ensure schedule_every_ms is NULL so the expr fallback is used
        db.conn()
            .execute(
                "UPDATE cron_jobs SET schedule_every_ms = NULL WHERE id = ?1",
                [&job_id],
            )
            .expect("clear ms");

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "every-expr-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        // The expr "1s" = 1000ms, and with no last_run the job should be immediately due
        let count: i64 = db
            .conn()
            .query_row(
                &format!("SELECT COUNT(*) FROM cron_runs WHERE job_id = '{}'", job_id),
                [],
                |row| row.get(0),
            )
            .expect("count runs");
        assert!(
            count >= 1,
            "every-kind with expr fallback should have been executed"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn run_cron_worker_multiple_jobs_in_single_tick() {
        let db = test_db();
        let _id1 = create_due_job(&db, "multi-1", r#"{"action":"log","message":"first"}"#);
        let _id2 = create_due_job(&db, "multi-2", r#"{"action":"noop"}"#);
        let _id3 = create_due_job(&db, "multi-3", r#"{"action":"log","message":"third"}"#);

        let db_clone = db.clone();
        let handle = tokio::spawn(async move {
            run_cron_worker(db_clone, "multi-instance".into()).await;
        });

        tokio::time::advance(std::time::Duration::from_secs(61)).await;
        tokio::task::yield_now().await;
        tokio::time::advance(std::time::Duration::from_millis(10)).await;
        tokio::task::yield_now().await;

        handle.abort();
        let _ = handle.await;

        let count: i64 = db
            .conn()
            .query_row("SELECT COUNT(*) FROM cron_runs", [], |row| row.get(0))
            .expect("count runs");
        assert!(count >= 3, "expected at least 3 cron runs, got {count}");
    }
}