zisk-cluster-common 1.3.0-alpha

Shared types and utilities for the ZisK distributed proving cluster
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
//! Core Domain Types for Distributed Proving System
//!
//! This module defines the fundamental domain types and business entities used throughout
//! the distributed proving system. These types form the core vocabulary of the system,
//! providing type safety, semantic clarity, and domain-driven design principles.

use borsh::{BorshDeserialize, BorshSerialize};
use chrono::{DateTime, Utc};
use proofman::{ContributionsInfo, ProvePhaseInputs, WitnessInfo};
use proofman_common::ProofOptions;
use serde::{Deserialize, Serialize};
use std::{
    collections::{BTreeMap, HashMap, VecDeque},
    fmt::{self, Debug, Display},
    ops::Range,
};
use tracing::error;
use zisk_common::{Proof, StatsCostPerType, ZiskExecutorTime};

use crate::{HintsModeDto, HintsSourceDto, InputSourceDto, InputsModeDto, ProofKind};

/// Job ID wrapper for type safety
#[derive(
    Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, BorshSerialize, BorshDeserialize,
)]
pub struct JobId(String);

impl Default for JobId {
    fn default() -> Self {
        Self::new()
    }
}

impl JobId {
    /// Generate a fresh random job id.
    pub fn new() -> Self {
        Self(uuid::Uuid::new_v4().to_string())
    }

    /// Borrow the id as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Clone the id into an owned `String`.
    pub fn as_string(&self) -> String {
        self.0.clone()
    }
}

impl From<String> for JobId {
    fn from(id: String) -> Self {
        Self(id)
    }
}

impl From<JobId> for String {
    fn from(job_id: JobId) -> Self {
        job_id.0
    }
}

impl std::fmt::Display for JobId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.0.len() > 8 {
            write!(f, "JobId({:.8}…)", self.0)
        } else {
            write!(f, "JobId({})", self.0)
        }
    }
}

/// Data ID wrapper for type safety
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct DataId(String);

impl Default for DataId {
    fn default() -> Self {
        Self::new()
    }
}

impl DataId {
    /// Generate a fresh random data id.
    pub fn new() -> Self {
        Self(uuid::Uuid::new_v4().to_string())
    }

    /// Borrow the id as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Clone the id into an owned `String`.
    pub fn as_string(&self) -> String {
        self.0.clone()
    }
}

impl From<String> for DataId {
    fn from(data_id: String) -> Self {
        Self(data_id)
    }
}

impl From<DataId> for String {
    fn from(data_id: DataId) -> Self {
        data_id.0
    }
}

impl std::fmt::Display for DataId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.0.len() > 8 {
            write!(f, "DataId({:.8}…)", self.0)
        } else {
            write!(f, "DataId({})", self.0)
        }
    }
}

/// Worker ID wrapper for type safety
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct WorkerId(String);

impl Default for WorkerId {
    fn default() -> Self {
        Self::new()
    }
}

impl WorkerId {
    /// Generate a fresh random worker id.
    pub fn new() -> Self {
        Self(uuid::Uuid::new_v4().to_string())
    }

    /// Borrow the id as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Clone the id into an owned `String`.
    pub fn as_string(&self) -> String {
        self.0.clone()
    }
}

impl From<String> for WorkerId {
    fn from(id: String) -> Self {
        Self(id)
    }
}

impl From<WorkerId> for String {
    fn from(worker_id: WorkerId) -> Self {
        worker_id.0
    }
}

impl std::fmt::Display for WorkerId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.0.len() > 8 {
            write!(f, "WorkerId({:.8}…)", self.0)
        } else {
            write!(f, "WorkerId({})", self.0)
        }
    }
}

/// Lifecycle state of a worker as tracked by the coordinator.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WorkerState {
    /// Not connected.
    Disconnected,
    /// Connection in progress.
    Connecting,
    /// Connected but no setup done yet. Not eligible for job assignment.
    Idle,
    /// Running setup (guest program load). Not eligible for job assignment.
    SettingUp,
    /// Setup complete. Eligible for job assignment.
    Ready,
    /// Computing the given phase of the given job.
    Computing((JobId, JobPhase)),
    /// In an error state.
    Error,
}

impl Display for WorkerState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let state_str = match self {
            WorkerState::Disconnected => "Disconnected",
            WorkerState::Connecting => "Connecting",
            WorkerState::Idle => "Idle",
            WorkerState::SettingUp => "SettingUp",
            WorkerState::Ready => "Ready",
            WorkerState::Computing(phase) => return write!(f, "Computing({})", phase.1),
            WorkerState::Error => "Error",
        };
        write!(f, "{}", state_str)
    }
}

/// Compute capacity
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
#[serde(deny_unknown_fields)]
pub struct ComputeCapacity {
    /// Number of compute units.
    pub compute_units: u32,
}

impl From<u32> for ComputeCapacity {
    fn from(units: u32) -> Self {
        Self { compute_units: units }
    }
}

impl std::fmt::Display for ComputeCapacity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}CU", self.compute_units)
    }
}

/// How a job is executed across workers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JobExecutionMode {
    /// Normal mode: real workers.
    Standard,
    /// Simulation mode: simulate N workers using only one physical worker.
    Simulating(u32),
}

impl JobExecutionMode {
    /// Whether this is [`Simulating`](Self::Simulating) mode.
    pub fn is_simulating(&self) -> bool {
        matches!(self, JobExecutionMode::Simulating(_))
    }
}

/// Policy applied when a worker fails or a phase times out.
///
/// Determines how the coordinator reacts to failures during job execution.
/// The policy is configured at the coordinator level and applies to all jobs.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[allow(dead_code)]
pub enum FailurePolicy {
    /// Abort the entire job immediately. All assigned workers are cancelled
    /// and the job is marked as failed.
    #[default]
    AbortJob,
    // /// Retry failed workers up to `max_retries` times before aborting.
    // /// If all retries are exhausted, the job is aborted.
    // RetryWorkers { max_retries: u32 },
}

impl Display for FailurePolicy {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FailurePolicy::AbortJob => write!(f, "AbortJob"),
            // FailurePolicy::RetryWorkers { max_retries } => write!(f, "RetryWorkers(max_retries={})", max_retries),
        }
    }
}

/// Per-phase timing data: tracks when a phase started and optionally when it ended.
#[derive(Clone)]
pub struct PhaseTimings {
    /// When the phase started.
    pub start_time: DateTime<Utc>,
    /// When the phase ended, if it has.
    pub end_time: Option<DateTime<Utc>>,
}

impl Debug for PhaseTimings {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl Display for PhaseTimings {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.end_time {
            Some(end) => {
                let duration = end.signed_duration_since(self.start_time);
                write!(f, "{}ms [{} - {}]", duration.num_milliseconds(), self.start_time, end)
            }
            None => write!(f, "in progress [started {}]", self.start_time),
        }
    }
}

/// The coordinator's full state for a single job across its lifecycle.
#[derive(Debug)]
pub struct Job {
    /// The job's id.
    pub job_id: JobId,
    /// Content hash of the guest program being proven.
    pub hash_id: String,
    /// Start/end timings per phase.
    pub phase_timings: HashMap<JobPhase, PhaseTimings>,
    /// When the job request was received.
    pub task_received_time: Option<DateTime<Utc>>,
    /// Total duration once terminal, in milliseconds.
    pub duration_ms: Option<u64>,
    /// When the job reached a terminal state.
    pub terminated_at: Option<DateTime<Utc>>,
    /// Current job state.
    pub state: JobState,
    /// Id of the input/hints data context.
    pub data_id: DataId,
    /// How inputs are supplied.
    pub inputs_mode: InputsModeDto,
    /// How hints are supplied.
    pub hints_mode: HintsModeDto,
    /// Requested compute capacity.
    pub compute_capacity: ComputeCapacity,
    /// Minimum acceptable compute capacity.
    pub minimal_compute_capacity: ComputeCapacity,
    /// Workers assigned to the job.
    pub workers: Vec<WorkerId>,
    /// Worker handling aggregation, if assigned.
    pub agg_worker_id: Option<WorkerId>,
    /// Per-worker compute-unit partitions.
    pub partitions: Vec<Vec<u32>>,
    /// Per-phase, per-worker results.
    pub results: HashMap<JobPhase, HashMap<WorkerId, JobResult>>,
    /// Collected contribution challenges, once available.
    pub challenges: Option<Vec<ContributionsInfo>>,
    /// Witness metadata, once available.
    pub witness_info: Option<WitnessInfo>,
    /// Execution mode (standard or simulating).
    pub execution_mode: JobExecutionMode,
    /// The final proof, once produced.
    pub proof: Option<Proof>,
    /// Total executed steps, once known.
    pub executed_steps: Option<u64>,
    /// Number of AIR instances, once known.
    pub instances: Option<u64>,
    /// Arbitrary client-supplied metadata.
    pub metadata: Option<BTreeMap<String, String>>,
    /// Whether this is an execute-only job (no proof).
    pub execution_only: bool,
    /// The kind of proof requested.
    pub proof_type: ProofKind,
    /// Aggregation task currently in-flight to the recurser (sent, not yet acked).
    /// Re-sent verbatim if the recurser reconnects before returning its result.
    pub agg_task_inflight: Option<PendingAggTask>,
    /// Queued aggregation tasks awaiting dispatch.
    pub agg_task_queue: VecDeque<PendingAggTask>,
}

impl Job {
    /// Create a new job in the `Created` state from its submission parameters.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        job_id: JobId,
        data_id: DataId,
        hash_id: String,
        inputs_mode: InputsModeDto,
        hints_mode: HintsModeDto,
        compute_capacity: ComputeCapacity,
        minimal_compute_capacity: ComputeCapacity,
        selected_workers: Vec<WorkerId>,
        partitions: Vec<Vec<u32>>,
        execution_mode: JobExecutionMode,
        metadata: Option<BTreeMap<String, String>>,
        execution_only: bool,
        proof_type: ProofKind,
    ) -> Self {
        Self {
            job_id,
            hash_id,
            phase_timings: HashMap::new(),
            duration_ms: None,
            terminated_at: None,
            state: JobState::Created,
            data_id,
            inputs_mode,
            hints_mode,
            compute_capacity,
            minimal_compute_capacity,
            workers: selected_workers,
            agg_worker_id: None,
            partitions,
            results: HashMap::new(),
            task_received_time: None,
            challenges: None,
            witness_info: None,
            execution_mode,
            proof: None,
            executed_steps: None,
            instances: None,
            metadata,
            execution_only,
            proof_type,
            agg_task_inflight: None,
            agg_task_queue: VecDeque::new(),
        }
    }

    /// The job's id.
    pub fn job_id(&self) -> &JobId {
        &self.job_id
    }

    /// Transition the job to `new_state`, validating the transition and
    /// recording phase timings and terminal duration. Invalid transitions are
    /// logged and ignored.
    pub fn change_state(&mut self, new_state: JobState) {
        // Validate transition. Failed and Cancelled are always reachable (abort from any state).
        let valid = matches!(
            (&self.state, &new_state),
            (_, JobState::Failed)
                | (_, JobState::Cancelled)
                | (JobState::Created, JobState::Running(_))
                | (JobState::Running(_), JobState::Running(_))
                | (JobState::Running(_), JobState::Completed)
        );

        if !valid {
            error!(
                "Invalid job state transition for {}: {} -> {}",
                self.job_id, self.state, new_state
            );
            return;
        }

        // Record end_time for the phase we're leaving
        if let JobState::Running(ref current_phase) = self.state {
            if let Some(timings) = self.phase_timings.get_mut(current_phase) {
                timings.end_time = Some(Utc::now());
            }
        }

        self.state = new_state.clone();

        match new_state {
            JobState::Running(phase) => {
                let previous = self
                    .phase_timings
                    .insert(phase.clone(), PhaseTimings { start_time: Utc::now(), end_time: None });
                if previous.is_some() {
                    error!("Start time for phase {:?} was already set", phase);
                }
            }
            JobState::Completed | JobState::Failed | JobState::Cancelled => {
                let now = Utc::now();
                let earliest_start = self.phase_timings.values().map(|t| t.start_time).min();
                if let Some(start_time) = earliest_start {
                    let duration = now.signed_duration_since(start_time);
                    self.duration_ms = Some(duration.num_milliseconds() as u64);
                }
                self.terminated_at = Some(now);
            }
            _ => {}
        }
    }

    /// Returns the start time for a given phase, if recorded.
    pub fn phase_start_time(&self, phase: &JobPhase) -> Option<DateTime<Utc>> {
        self.phase_timings.get(phase).map(|t| t.start_time)
    }

    /// The job's current state.
    pub fn state(&self) -> &JobState {
        &self.state
    }

    /// Release per-job working memory (partitions, results, timings, challenges,
    /// and aggregation queues) once the job is terminal.
    pub fn cleanup(&mut self) {
        self.partitions.clear();
        self.results.clear();
        self.phase_timings.clear();
        self.challenges = None;
        self.agg_task_inflight = None;
        self.agg_task_queue.clear();
    }
}

/// Coarse job state (the phase lives inside `Running`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum JobState {
    /// Created, not yet running.
    Created,
    /// Running the given phase.
    Running(JobPhase),
    /// Finished successfully.
    Completed,
    /// Finished with a failure.
    Failed,
    /// Cancelled.
    Cancelled,
}

impl JobState {
    /// Whether this is a resolved (terminal) state.
    pub fn is_resolved(&self) -> bool {
        matches!(self, JobState::Failed | JobState::Completed | JobState::Cancelled)
    }
}

impl fmt::Display for JobState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            JobState::Created => write!(f, "Created"),
            JobState::Running(phase) => write!(f, "Running ({:?})", phase),
            JobState::Completed => write!(f, "Completed"),
            JobState::Failed => write!(f, "Failed"),
            JobState::Cancelled => write!(f, "Cancelled"),
        }
    }
}

/// A single airgroup's partial proof produced by a worker.
#[derive(Debug, Clone)]
pub struct AggProofData {
    /// Index of the producing worker.
    pub worker_idx: u32,
    /// The airgroup this proof belongs to.
    pub airgroup_id: u64,
    /// The proof field-element values.
    pub values: Vec<u64>,
}

/// An aggregation task queued or in-flight to the recurser.
#[derive(Debug, Clone)]
pub struct PendingAggTask {
    /// The partial proofs to aggregate.
    pub proofs: Vec<AggProofData>,
    /// Whether this is the final aggregation step.
    pub all_done: bool,
    /// The kind of proof being produced.
    pub proof_type: ProofKind,
}

/// Result of the contribution phase for one worker.
#[derive(Debug, Clone)]
pub struct ContributionsResult {
    /// The contribution challenges.
    pub challenges: Vec<ContributionsInfo>,
    /// Witness metadata.
    pub witness_info: WitnessInfo,
    /// Executor timing.
    pub zisk_executor_time: ZiskExecutorTime,
    /// When the originating task was received.
    pub task_received_time: Option<chrono::DateTime<chrono::Utc>>,
    /// Number of AIR instances.
    pub instances: u64,
    /// Per-type cost breakdown.
    pub cost_per_type: StatsCostPerType,
}

/// Result of an execute-only run for one worker.
#[derive(Debug, Clone)]
pub struct ExecutionResult {
    /// Number of AIR instances.
    pub instances: u64,
    /// Steps executed.
    pub executed_steps: u64,
    /// Executor timing.
    pub zisk_executor_time: ZiskExecutorTime,
    /// When the originating task was received.
    pub task_received_time: Option<chrono::DateTime<chrono::Utc>>,
    /// The program's public outputs.
    pub public_outputs: Vec<u8>,
    /// Per-type cost breakdown.
    pub cost_per_type: StatsCostPerType,
    /// Per-AIR instance plan.
    pub plan: Vec<zisk_common::AirInstanceCount>,
}

/// The payload of a per-worker job result, by phase.
#[derive(Debug, Clone)]
pub enum JobResultData {
    /// Execute-only result.
    Execution(ExecutionResult),
    /// Contribution-phase result.
    Challenges(ContributionsResult),
    /// Partial proofs from the prove phase.
    AggProofs(Vec<AggProofData>),
}

/// A worker's result for one phase of a job.
#[derive(Debug, Clone)]
pub struct JobResult {
    /// Whether the phase succeeded for this worker.
    pub success: bool,
    /// The result payload.
    pub data: JobResultData,
    /// When the result was recorded.
    pub end_time: DateTime<Utc>,
}

/// The input/hints data context for a job.
#[derive(Debug, Clone)]
pub struct DataCtx {
    /// Id of this data context.
    pub data_id: DataId,
    /// Where the job's input comes from.
    pub input_source: InputSourceDto,
    /// Where the job's hints come from.
    pub hints_source: HintsSourceDto,
}

/// A phase of a job's execution. The `#[repr(u8)]` order is wire-significant
/// (used by [`TryFrom<u8>`]).
#[repr(u8)]
#[derive(Debug, Clone, Eq, PartialEq, Hash, BorshSerialize, BorshDeserialize)]
pub enum JobPhase {
    /// Execute-only phase.
    Execution,
    /// Contribution-gathering phase.
    Contributions,
    /// Proof-generation phase.
    Prove,
    /// Recursion/aggregation phase.
    Recurse,
    /// Streaming of contribution inputs.
    ContributionsInputsStream,
    /// Streaming of contribution hints.
    ContributionsHintsStream,
}

impl TryFrom<u8> for JobPhase {
    type Error = anyhow::Error;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(JobPhase::Execution),
            1 => Ok(JobPhase::Contributions),
            2 => Ok(JobPhase::Prove),
            3 => Ok(JobPhase::Recurse),
            4 => Ok(JobPhase::ContributionsInputsStream),
            5 => Ok(JobPhase::ContributionsHintsStream),
            _ => Err(anyhow::anyhow!("Invalid JobPhase byte: {}", value)),
        }
    }
}

impl fmt::Display for JobPhase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            JobPhase::Execution => write!(f, "Execution"),
            JobPhase::Contributions => write!(f, "Contributions"),
            JobPhase::Prove => write!(f, "Prove"),
            JobPhase::Recurse => write!(f, "Recurse"),
            JobPhase::ContributionsInputsStream => write!(f, "ContributionsInputsStream"),
            JobPhase::ContributionsHintsStream => write!(f, "ContributionsHintsStream"),
        }
    }
}

/// A worker's assigned compute-unit range within a job.
#[derive(Debug, Clone)]
pub struct WorkerAllocationDto {
    /// The compute-unit index range assigned to the worker.
    pub range: Range<u32>,
}

/// Parameters for an aggregation step.
#[derive(Debug, Clone)]
pub struct AggregationParams {
    /// Partial proofs to aggregate.
    pub agg_proofs: Vec<AggProofData>,
    /// Whether this is the last proof in the current round.
    pub last_proof: bool,
    /// Whether this produces the final job proof.
    pub final_proof: bool,
    /// The kind of proof being produced.
    pub proof_type: ProofKind,
}

/// A worker's partition of a job's total work.
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
pub struct PartitionInfo {
    /// Total compute units across the job.
    pub total_compute_units: usize,
    /// Per-worker compute-unit allocation.
    pub allocation: Vec<u32>,
    /// This worker's index into `allocation`.
    pub worker_idx: usize,
}

/// MPI broadcast message carrying a contribution/execution task to peer ranks.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct ContributionsMessage {
    /// The job id.
    pub job_id: JobId,
    /// Content hash of the guest program.
    pub hash_id: String,
    /// Proving-phase inputs.
    pub phase_inputs: ProvePhaseInputs,
    /// Proof options.
    pub options: ProofOptions,
    /// Where the input comes from.
    pub input_source: InputSourceDto,
    /// Where the hints come from.
    pub hints_source: HintsSourceDto,
    /// This rank's partition.
    pub partition_info: PartitionInfo,
}

/// MPI broadcast message carrying a prove task to peer ranks.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct ProveMessage {
    /// The job id.
    pub job_id: JobId,
    /// Proving-phase inputs (challenges).
    pub phase_inputs: ProvePhaseInputs,
    /// Proof options.
    pub options: ProofOptions,
}

/// MPI broadcast message carrying a chunk of streamed data.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct StreamMessage {
    /// The streamed words.
    pub data: Vec<u64>,
}

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

    fn make_job() -> Job {
        Job::new(
            JobId::new(),
            Default::default(),
            String::new(),
            crate::InputsModeDto::InputsNone,
            crate::HintsModeDto::HintsNone,
            ComputeCapacity::from(1u32),
            ComputeCapacity::from(1u32),
            vec![],
            vec![],
            JobExecutionMode::Standard,
            None,
            false,
            crate::ProofKind::VadcopFinal,
        )
    }

    #[test]
    fn test_valid_state_transitions() {
        let mut job = make_job();
        assert_eq!(job.state, JobState::Created);

        // Created → Running(Contributions)
        job.change_state(JobState::Running(JobPhase::Contributions));
        assert_eq!(job.state, JobState::Running(JobPhase::Contributions));

        // Running → Running (phase change)
        job.change_state(JobState::Running(JobPhase::Prove));
        assert_eq!(job.state, JobState::Running(JobPhase::Prove));

        // Running → Completed
        job.change_state(JobState::Completed);
        assert_eq!(job.state, JobState::Completed);
    }

    #[test]
    fn test_invalid_transition_created_to_completed() {
        let mut job = make_job();

        // Created → Completed is invalid
        job.change_state(JobState::Completed);
        assert_eq!(job.state, JobState::Created); // state unchanged
    }

    #[test]
    fn test_invalid_transition_completed_to_running() {
        let mut job = make_job();
        job.change_state(JobState::Running(JobPhase::Contributions));
        job.change_state(JobState::Completed);

        // Completed → Running is invalid
        job.change_state(JobState::Running(JobPhase::Prove));
        assert_eq!(job.state, JobState::Completed); // state unchanged
    }

    #[test]
    fn test_failed_always_reachable() {
        let mut job = make_job();

        // Created → Failed
        job.change_state(JobState::Failed);
        assert_eq!(job.state, JobState::Failed);

        // Another job: Running → Failed
        let mut job2 = make_job();
        job2.change_state(JobState::Running(JobPhase::Prove));
        job2.change_state(JobState::Failed);
        assert_eq!(job2.state, JobState::Failed);
    }

    #[test]
    fn test_duplicate_phase_start_time_does_not_crash() {
        let mut job = make_job();

        // First time: insert Contributions start time
        job.change_state(JobState::Running(JobPhase::Contributions));
        assert!(job.phase_start_time(&JobPhase::Contributions).is_some());

        // Manually re-insert to simulate the error path
        // (normally prevented by state machine, but we test the error! path)
        let original_time = job.phase_start_time(&JobPhase::Contributions).unwrap();
        job.phase_timings.insert(
            JobPhase::Contributions,
            PhaseTimings { start_time: Utc::now(), end_time: None },
        );

        // The job should still be functional — no panic
        job.change_state(JobState::Running(JobPhase::Prove));
        assert!(job.phase_start_time(&JobPhase::Prove).is_some());

        // Verify first phase was overwritten (not panicked)
        assert_ne!(job.phase_start_time(&JobPhase::Contributions).unwrap(), original_time);
    }

    #[test]
    fn test_duration_computed_on_completion() {
        let mut job = make_job();
        job.change_state(JobState::Running(JobPhase::Contributions));
        job.change_state(JobState::Completed);

        // duration_ms should be set (very small since no real work)
        assert!(job.duration_ms.is_some());
    }

    #[test]
    fn test_cleanup_clears_phase_timings() {
        let mut job = make_job();
        job.change_state(JobState::Running(JobPhase::Contributions));
        assert!(!job.phase_timings.is_empty());

        job.cleanup();
        assert!(job.phase_timings.is_empty());
    }

    #[test]
    fn test_phase_end_time_recorded_on_transition() {
        let mut job = make_job();
        job.change_state(JobState::Running(JobPhase::Contributions));

        // End time not set yet
        assert!(job.phase_timings.get(&JobPhase::Contributions).unwrap().end_time.is_none());

        // Transition to next phase records end_time on previous
        job.change_state(JobState::Running(JobPhase::Prove));
        assert!(job.phase_timings.get(&JobPhase::Contributions).unwrap().end_time.is_some());
    }
}