cloacina 0.6.1

A Rust library for resilient task execution and orchestration.
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
/*
 *  Copyright 2025-2026 Colliery Software
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

//! Unified database models using custom SQL types
//!
//! These models use the unified schema with DbUuid, DbTimestamp, DbBool custom
//! SQL types that work with both PostgreSQL and SQLite backends.

use crate::database::schema::unified::{
    accumulator_boundaries, accumulator_checkpoints, contexts, execution_events, key_trust_acls,
    package_signatures, reactor_state, recovery_events, schedule_executions, schedules,
    signing_keys, state_accumulator_buffers, task_execution_metadata, task_executions, task_outbox,
    trusted_keys, workflow_executions, workflow_packages, workflow_registry,
};
use crate::database::universal_types::{
    UniversalBinary, UniversalBool, UniversalTimestamp, UniversalUuid,
};
use diesel::prelude::*;

// ============================================================================
// Context Models
// ============================================================================

/// Unified context model that works with both PostgreSQL and SQLite.
#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = contexts)]
pub struct UnifiedDbContext {
    pub id: UniversalUuid,
    pub value: String,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

/// Insertable context with explicit ID and timestamps (for SQLite compatibility).
#[derive(Debug, Insertable)]
#[diesel(table_name = contexts)]
pub struct NewUnifiedDbContext {
    pub id: UniversalUuid,
    pub value: String,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

// ============================================================================
// Workflow Execution Models
// ============================================================================

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = workflow_executions)]
pub struct UnifiedWorkflowExecution {
    pub id: UniversalUuid,
    pub workflow_name: String,
    pub workflow_version: String,
    pub status: String,
    pub context_id: Option<UniversalUuid>,
    pub started_at: UniversalTimestamp,
    pub completed_at: Option<UniversalTimestamp>,
    pub error_details: Option<String>,
    pub recovery_attempts: i32,
    pub last_recovery_at: Option<UniversalTimestamp>,
    pub paused_at: Option<UniversalTimestamp>,
    pub pause_reason: Option<String>,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = workflow_executions)]
pub struct NewUnifiedWorkflowExecution {
    pub id: UniversalUuid,
    pub workflow_name: String,
    pub workflow_version: String,
    pub status: String,
    pub context_id: Option<UniversalUuid>,
    pub started_at: UniversalTimestamp,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

// ============================================================================
// Task Execution Models
// ============================================================================

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = task_executions)]
pub struct UnifiedTaskExecution {
    pub id: UniversalUuid,
    pub workflow_execution_id: UniversalUuid,
    pub task_name: String,
    pub status: String,
    pub started_at: Option<UniversalTimestamp>,
    pub completed_at: Option<UniversalTimestamp>,
    pub attempt: i32,
    pub max_attempts: i32,
    pub error_details: Option<String>,
    pub trigger_rules: String,
    pub task_configuration: String,
    pub retry_at: Option<UniversalTimestamp>,
    pub last_error: Option<String>,
    pub recovery_attempts: i32,
    pub last_recovery_at: Option<UniversalTimestamp>,
    pub sub_status: Option<String>,
    pub claimed_by: Option<UniversalUuid>,
    pub heartbeat_at: Option<UniversalTimestamp>,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = task_executions)]
pub struct NewUnifiedTaskExecution {
    pub id: UniversalUuid,
    pub workflow_execution_id: UniversalUuid,
    pub task_name: String,
    pub status: String,
    pub attempt: i32,
    pub max_attempts: i32,
    pub trigger_rules: String,
    pub task_configuration: String,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

// ============================================================================
// Task Execution Metadata Models
// ============================================================================

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = task_execution_metadata)]
pub struct UnifiedTaskExecutionMetadata {
    pub id: UniversalUuid,
    pub task_execution_id: UniversalUuid,
    pub workflow_execution_id: UniversalUuid,
    pub task_name: String,
    pub context_id: Option<UniversalUuid>,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = task_execution_metadata)]
pub struct NewUnifiedTaskExecutionMetadata {
    pub id: UniversalUuid,
    pub task_execution_id: UniversalUuid,
    pub workflow_execution_id: UniversalUuid,
    pub task_name: String,
    pub context_id: Option<UniversalUuid>,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

// ============================================================================
// Recovery Event Models
// ============================================================================

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = recovery_events)]
pub struct UnifiedRecoveryEvent {
    pub id: UniversalUuid,
    pub workflow_execution_id: UniversalUuid,
    pub task_execution_id: Option<UniversalUuid>,
    pub recovery_type: String,
    pub recovered_at: UniversalTimestamp,
    pub details: Option<String>,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = recovery_events)]
pub struct NewUnifiedRecoveryEvent {
    pub id: UniversalUuid,
    pub workflow_execution_id: UniversalUuid,
    pub task_execution_id: Option<UniversalUuid>,
    pub recovery_type: String,
    pub recovered_at: UniversalTimestamp,
    pub details: Option<String>,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

// ============================================================================
// Execution Event Models
// ============================================================================

/// Unified execution event model for audit trail of state transitions.
/// Append-only: events are never updated after creation.
#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = execution_events)]
pub struct UnifiedExecutionEvent {
    pub id: UniversalUuid,
    pub workflow_execution_id: UniversalUuid,
    pub task_execution_id: Option<UniversalUuid>,
    pub event_type: String,
    pub event_data: Option<String>,
    pub worker_id: Option<String>,
    pub created_at: UniversalTimestamp,
    pub sequence_num: i64,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = execution_events)]
pub struct NewUnifiedExecutionEvent {
    pub id: UniversalUuid,
    pub workflow_execution_id: UniversalUuid,
    pub task_execution_id: Option<UniversalUuid>,
    pub event_type: String,
    pub event_data: Option<String>,
    pub worker_id: Option<String>,
    pub created_at: UniversalTimestamp,
}

// ============================================================================
// Task Outbox Models
// ============================================================================

/// Unified task outbox model for work distribution.
/// Transient: rows are deleted immediately upon claiming.
#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = task_outbox)]
pub struct UnifiedTaskOutbox {
    pub id: i64,
    pub task_execution_id: UniversalUuid,
    pub created_at: UniversalTimestamp,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = task_outbox)]
pub struct NewUnifiedTaskOutbox {
    pub task_execution_id: UniversalUuid,
    pub created_at: UniversalTimestamp,
}

// ============================================================================
// Unified Schedule Models
// ============================================================================

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = schedules)]
pub struct UnifiedSchedule {
    pub id: UniversalUuid,
    pub schedule_type: String,
    pub workflow_name: String,
    pub enabled: UniversalBool,
    pub cron_expression: Option<String>,
    pub timezone: Option<String>,
    pub catchup_policy: Option<String>,
    pub start_date: Option<UniversalTimestamp>,
    pub end_date: Option<UniversalTimestamp>,
    pub trigger_name: Option<String>,
    pub poll_interval_ms: Option<i32>,
    pub allow_concurrent: Option<UniversalBool>,
    pub next_run_at: Option<UniversalTimestamp>,
    pub last_run_at: Option<UniversalTimestamp>,
    pub last_poll_at: Option<UniversalTimestamp>,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = schedules)]
pub struct NewUnifiedSchedule {
    pub id: UniversalUuid,
    pub schedule_type: String,
    pub workflow_name: String,
    pub enabled: UniversalBool,
    pub cron_expression: Option<String>,
    pub timezone: Option<String>,
    pub catchup_policy: Option<String>,
    pub start_date: Option<UniversalTimestamp>,
    pub end_date: Option<UniversalTimestamp>,
    pub trigger_name: Option<String>,
    pub poll_interval_ms: Option<i32>,
    pub allow_concurrent: Option<UniversalBool>,
    pub next_run_at: Option<UniversalTimestamp>,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

// ============================================================================
// Unified Schedule Execution Models
// ============================================================================

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = schedule_executions)]
pub struct UnifiedScheduleExecution {
    pub id: UniversalUuid,
    pub schedule_id: UniversalUuid,
    pub workflow_execution_id: Option<UniversalUuid>,
    pub scheduled_time: Option<UniversalTimestamp>,
    pub claimed_at: Option<UniversalTimestamp>,
    pub context_hash: Option<String>,
    pub started_at: UniversalTimestamp,
    pub completed_at: Option<UniversalTimestamp>,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = schedule_executions)]
pub struct NewUnifiedScheduleExecution {
    pub id: UniversalUuid,
    pub schedule_id: UniversalUuid,
    pub workflow_execution_id: Option<UniversalUuid>,
    pub scheduled_time: Option<UniversalTimestamp>,
    pub claimed_at: Option<UniversalTimestamp>,
    pub context_hash: Option<String>,
    pub started_at: UniversalTimestamp,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

// ============================================================================
// Workflow Registry Models
// ============================================================================

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = workflow_registry)]
pub struct UnifiedWorkflowRegistryEntry {
    pub id: UniversalUuid,
    pub created_at: UniversalTimestamp,
    pub data: UniversalBinary,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = workflow_registry)]
pub struct NewUnifiedWorkflowRegistryEntry {
    pub id: UniversalUuid,
    pub created_at: UniversalTimestamp,
    pub data: UniversalBinary,
}

// ============================================================================
// Workflow Packages Models
// ============================================================================

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = workflow_packages)]
pub struct UnifiedWorkflowPackage {
    pub id: UniversalUuid,
    pub registry_id: UniversalUuid,
    pub package_name: String,
    pub version: String,
    pub description: Option<String>,
    pub author: Option<String>,
    pub metadata: String,
    pub storage_type: String,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
    pub tenant_id: Option<String>,
    pub content_hash: String,
    pub superseded: UniversalBool,
    pub compiled_data: Option<UniversalBinary>,
    pub build_status: String,
    pub build_error: Option<String>,
    pub build_claimed_at: Option<UniversalTimestamp>,
    pub compiled_at: Option<UniversalTimestamp>,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = workflow_packages)]
pub struct NewUnifiedWorkflowPackage {
    pub id: UniversalUuid,
    pub registry_id: UniversalUuid,
    pub package_name: String,
    pub version: String,
    pub description: Option<String>,
    pub author: Option<String>,
    pub metadata: String,
    pub storage_type: String,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
    pub tenant_id: Option<String>,
    pub content_hash: String,
    pub superseded: UniversalBool,
    pub compiled_data: Option<UniversalBinary>,
    pub build_status: String,
    pub build_error: Option<String>,
    pub build_claimed_at: Option<UniversalTimestamp>,
    pub compiled_at: Option<UniversalTimestamp>,
}

// ============================================================================
// Signing Key Models
// ============================================================================

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = signing_keys)]
pub struct UnifiedSigningKey {
    pub id: UniversalUuid,
    pub org_id: UniversalUuid,
    pub key_name: String,
    pub encrypted_private_key: UniversalBinary,
    pub public_key: UniversalBinary,
    pub key_fingerprint: String,
    pub created_at: UniversalTimestamp,
    pub revoked_at: Option<UniversalTimestamp>,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = signing_keys)]
pub struct NewUnifiedSigningKey {
    pub id: UniversalUuid,
    pub org_id: UniversalUuid,
    pub key_name: String,
    pub encrypted_private_key: UniversalBinary,
    pub public_key: UniversalBinary,
    pub key_fingerprint: String,
    pub created_at: UniversalTimestamp,
}

// ============================================================================
// Trusted Key Models
// ============================================================================

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = trusted_keys)]
pub struct UnifiedTrustedKey {
    pub id: UniversalUuid,
    pub org_id: UniversalUuid,
    pub key_fingerprint: String,
    pub public_key: UniversalBinary,
    pub key_name: Option<String>,
    pub trusted_at: UniversalTimestamp,
    pub revoked_at: Option<UniversalTimestamp>,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = trusted_keys)]
pub struct NewUnifiedTrustedKey {
    pub id: UniversalUuid,
    pub org_id: UniversalUuid,
    pub key_fingerprint: String,
    pub public_key: UniversalBinary,
    pub key_name: Option<String>,
    pub trusted_at: UniversalTimestamp,
}

// ============================================================================
// Key Trust ACL Models
// ============================================================================

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = key_trust_acls)]
pub struct UnifiedKeyTrustAcl {
    pub id: UniversalUuid,
    pub parent_org_id: UniversalUuid,
    pub child_org_id: UniversalUuid,
    pub granted_at: UniversalTimestamp,
    pub revoked_at: Option<UniversalTimestamp>,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = key_trust_acls)]
pub struct NewUnifiedKeyTrustAcl {
    pub id: UniversalUuid,
    pub parent_org_id: UniversalUuid,
    pub child_org_id: UniversalUuid,
    pub granted_at: UniversalTimestamp,
}

// ============================================================================
// Package Signature Models
// ============================================================================

#[derive(Debug, Clone, Queryable, Selectable)]
#[diesel(table_name = package_signatures)]
pub struct UnifiedPackageSignature {
    pub id: UniversalUuid,
    pub package_hash: String,
    pub key_fingerprint: String,
    pub signature: UniversalBinary,
    pub signed_at: UniversalTimestamp,
    pub org_id: Option<UniversalUuid>,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = package_signatures)]
pub struct NewUnifiedPackageSignature {
    pub id: UniversalUuid,
    pub package_hash: String,
    pub key_fingerprint: String,
    pub signature: UniversalBinary,
    pub signed_at: UniversalTimestamp,
    pub org_id: Option<UniversalUuid>,
}

// ============================================================================
// Conversion to Domain Models
// ============================================================================
// Since unified models use Universal* types directly, conversion to domain
// models is straightforward - mostly just field-by-field mapping.

use crate::models::context::DbContext;
use crate::models::execution_event::ExecutionEvent;
use crate::models::key_trust_acl::KeyTrustAcl;
use crate::models::package_signature::PackageSignature;
use crate::models::recovery_event::RecoveryEvent;
use crate::models::schedule::{Schedule, ScheduleExecution};
use crate::models::signing_key::SigningKey;
use crate::models::task_execution::TaskExecution;
use crate::models::task_execution_metadata::TaskExecutionMetadata;
use crate::models::trusted_key::TrustedKey;
use crate::models::workflow_execution::WorkflowExecutionRecord;
use crate::models::workflow_packages::WorkflowPackage;
use crate::models::workflow_registry::WorkflowRegistryEntry;

impl From<UnifiedDbContext> for DbContext {
    fn from(u: UnifiedDbContext) -> Self {
        DbContext {
            id: u.id,
            value: u.value,
            created_at: u.created_at,
            updated_at: u.updated_at,
        }
    }
}

impl From<UnifiedWorkflowExecution> for WorkflowExecutionRecord {
    fn from(u: UnifiedWorkflowExecution) -> Self {
        WorkflowExecutionRecord {
            id: u.id,
            workflow_name: u.workflow_name,
            workflow_version: u.workflow_version,
            status: u.status,
            context_id: u.context_id,
            started_at: u.started_at,
            completed_at: u.completed_at,
            error_details: u.error_details,
            recovery_attempts: u.recovery_attempts,
            last_recovery_at: u.last_recovery_at,
            paused_at: u.paused_at,
            pause_reason: u.pause_reason,
            created_at: u.created_at,
            updated_at: u.updated_at,
        }
    }
}

impl From<UnifiedTaskExecution> for TaskExecution {
    fn from(u: UnifiedTaskExecution) -> Self {
        TaskExecution {
            id: u.id,
            workflow_execution_id: u.workflow_execution_id,
            task_name: u.task_name,
            status: u.status,
            started_at: u.started_at,
            completed_at: u.completed_at,
            attempt: u.attempt,
            max_attempts: u.max_attempts,
            error_details: u.error_details,
            trigger_rules: u.trigger_rules,
            task_configuration: u.task_configuration,
            retry_at: u.retry_at,
            last_error: u.last_error,
            recovery_attempts: u.recovery_attempts,
            last_recovery_at: u.last_recovery_at,
            sub_status: u.sub_status,
            claimed_by: u.claimed_by,
            heartbeat_at: u.heartbeat_at,
            created_at: u.created_at,
            updated_at: u.updated_at,
        }
    }
}

impl From<UnifiedTaskExecutionMetadata> for TaskExecutionMetadata {
    fn from(u: UnifiedTaskExecutionMetadata) -> Self {
        TaskExecutionMetadata {
            id: u.id,
            task_execution_id: u.task_execution_id,
            workflow_execution_id: u.workflow_execution_id,
            task_name: u.task_name,
            context_id: u.context_id,
            created_at: u.created_at,
            updated_at: u.updated_at,
        }
    }
}

impl From<UnifiedRecoveryEvent> for RecoveryEvent {
    fn from(u: UnifiedRecoveryEvent) -> Self {
        RecoveryEvent {
            id: u.id,
            workflow_execution_id: u.workflow_execution_id,
            task_execution_id: u.task_execution_id,
            recovery_type: u.recovery_type,
            recovered_at: u.recovered_at,
            details: u.details,
            created_at: u.created_at,
            updated_at: u.updated_at,
        }
    }
}

impl From<UnifiedExecutionEvent> for ExecutionEvent {
    fn from(u: UnifiedExecutionEvent) -> Self {
        ExecutionEvent {
            id: u.id,
            workflow_execution_id: u.workflow_execution_id,
            task_execution_id: u.task_execution_id,
            event_type: u.event_type,
            event_data: u.event_data,
            worker_id: u.worker_id,
            created_at: u.created_at,
            sequence_num: u.sequence_num,
        }
    }
}

impl From<UnifiedWorkflowRegistryEntry> for WorkflowRegistryEntry {
    fn from(u: UnifiedWorkflowRegistryEntry) -> Self {
        WorkflowRegistryEntry {
            id: u.id,
            created_at: u.created_at,
            data: u.data.into_inner(),
        }
    }
}

impl From<UnifiedWorkflowPackage> for WorkflowPackage {
    fn from(u: UnifiedWorkflowPackage) -> Self {
        WorkflowPackage {
            id: u.id,
            registry_id: u.registry_id,
            package_name: u.package_name,
            version: u.version,
            description: u.description,
            author: u.author,
            metadata: u.metadata,
            storage_type: u.storage_type.parse().unwrap(),
            created_at: u.created_at,
            updated_at: u.updated_at,
            tenant_id: u.tenant_id,
            content_hash: u.content_hash,
            superseded: u.superseded.0,
            compiled_data: u.compiled_data.map(|b| b.into_inner()),
            build_status: u.build_status,
            build_error: u.build_error,
            build_claimed_at: u.build_claimed_at,
            compiled_at: u.compiled_at,
        }
    }
}

impl From<UnifiedSigningKey> for SigningKey {
    fn from(u: UnifiedSigningKey) -> Self {
        SigningKey {
            id: u.id,
            org_id: u.org_id,
            key_name: u.key_name,
            encrypted_private_key: u.encrypted_private_key.into_inner(),
            public_key: u.public_key.into_inner(),
            key_fingerprint: u.key_fingerprint,
            created_at: u.created_at,
            revoked_at: u.revoked_at,
        }
    }
}

impl From<UnifiedTrustedKey> for TrustedKey {
    fn from(u: UnifiedTrustedKey) -> Self {
        TrustedKey {
            id: u.id,
            org_id: u.org_id,
            key_fingerprint: u.key_fingerprint,
            public_key: u.public_key.into_inner(),
            key_name: u.key_name,
            trusted_at: u.trusted_at,
            revoked_at: u.revoked_at,
        }
    }
}

impl From<UnifiedKeyTrustAcl> for KeyTrustAcl {
    fn from(u: UnifiedKeyTrustAcl) -> Self {
        KeyTrustAcl {
            id: u.id,
            parent_org_id: u.parent_org_id,
            child_org_id: u.child_org_id,
            granted_at: u.granted_at,
            revoked_at: u.revoked_at,
        }
    }
}

impl From<UnifiedPackageSignature> for PackageSignature {
    fn from(u: UnifiedPackageSignature) -> Self {
        PackageSignature {
            id: u.id,
            package_hash: u.package_hash,
            key_fingerprint: u.key_fingerprint,
            signature: u.signature.into_inner(),
            signed_at: u.signed_at,
            org_id: u.org_id,
        }
    }
}

impl From<UnifiedSchedule> for Schedule {
    fn from(u: UnifiedSchedule) -> Self {
        Schedule {
            id: u.id,
            schedule_type: u.schedule_type,
            workflow_name: u.workflow_name,
            enabled: u.enabled,
            cron_expression: u.cron_expression,
            timezone: u.timezone,
            catchup_policy: u.catchup_policy,
            start_date: u.start_date,
            end_date: u.end_date,
            trigger_name: u.trigger_name,
            poll_interval_ms: u.poll_interval_ms,
            allow_concurrent: u.allow_concurrent,
            next_run_at: u.next_run_at,
            last_run_at: u.last_run_at,
            last_poll_at: u.last_poll_at,
            created_at: u.created_at,
            updated_at: u.updated_at,
        }
    }
}

impl From<UnifiedScheduleExecution> for ScheduleExecution {
    fn from(u: UnifiedScheduleExecution) -> Self {
        ScheduleExecution {
            id: u.id,
            schedule_id: u.schedule_id,
            workflow_execution_id: u.workflow_execution_id,
            scheduled_time: u.scheduled_time,
            claimed_at: u.claimed_at,
            context_hash: u.context_hash,
            started_at: u.started_at,
            completed_at: u.completed_at,
            created_at: u.created_at,
            updated_at: u.updated_at,
        }
    }
}

// ============================================================================
// Computation Graph State Models
// ============================================================================

#[derive(Debug, Queryable, Selectable)]
#[diesel(table_name = accumulator_checkpoints)]
pub struct UnifiedAccumulatorCheckpoint {
    pub id: UniversalUuid,
    pub graph_name: String,
    pub accumulator_name: String,
    pub checkpoint_data: UniversalBinary,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = accumulator_checkpoints)]
pub struct NewUnifiedAccumulatorCheckpoint {
    pub id: UniversalUuid,
    pub graph_name: String,
    pub accumulator_name: String,
    pub checkpoint_data: UniversalBinary,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Queryable, Selectable)]
#[diesel(table_name = accumulator_boundaries)]
pub struct UnifiedAccumulatorBoundary {
    pub id: UniversalUuid,
    pub graph_name: String,
    pub accumulator_name: String,
    pub boundary_data: UniversalBinary,
    pub sequence_number: i64,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = accumulator_boundaries)]
pub struct NewUnifiedAccumulatorBoundary {
    pub id: UniversalUuid,
    pub graph_name: String,
    pub accumulator_name: String,
    pub boundary_data: UniversalBinary,
    pub sequence_number: i64,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Queryable, Selectable)]
#[diesel(table_name = reactor_state)]
pub struct UnifiedReactorState {
    pub id: UniversalUuid,
    pub graph_name: String,
    pub cache_data: UniversalBinary,
    pub dirty_flags: UniversalBinary,
    pub sequential_queue: Option<UniversalBinary>,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = reactor_state)]
pub struct NewUnifiedReactorState {
    pub id: UniversalUuid,
    pub graph_name: String,
    pub cache_data: UniversalBinary,
    pub dirty_flags: UniversalBinary,
    pub sequential_queue: Option<UniversalBinary>,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Queryable, Selectable)]
#[diesel(table_name = state_accumulator_buffers)]
pub struct UnifiedStateAccumulatorBuffer {
    pub id: UniversalUuid,
    pub graph_name: String,
    pub accumulator_name: String,
    pub buffer_data: UniversalBinary,
    pub capacity: i32,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}

#[derive(Debug, Insertable)]
#[diesel(table_name = state_accumulator_buffers)]
pub struct NewUnifiedStateAccumulatorBuffer {
    pub id: UniversalUuid,
    pub graph_name: String,
    pub accumulator_name: String,
    pub buffer_data: UniversalBinary,
    pub capacity: i32,
    pub created_at: UniversalTimestamp,
    pub updated_at: UniversalTimestamp,
}