starweft-protocol 0.3.0

Core protocol definitions and message types for starweft
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
1137
1138
//! Protocol message types, envelopes, and signature verification for Starweft.
//!
//! Defines the complete set of typed message bodies exchanged between agents,
//! the signed envelope format, wire serialization, and domain status enums.

use std::collections::BTreeMap;
use std::str::FromStr;

use serde::{Deserialize, Serialize};
use serde_json::Value;
use starweft_crypto::{CryptoError, MessageSignature, StoredKeypair, canonical_json, verify_bytes};
use starweft_id::{ActorId, ArtifactId, MessageId, NodeId, ProjectId, StopId, TaskId, VisionId};
use thiserror::Error;
use time::OffsetDateTime;

/// Current protocol version string used in all envelopes.
pub const PROTOCOL_VERSION: &str = "starweft/0.1";

/// Errors that can occur during protocol-level operations.
#[derive(Debug, Error)]
pub enum ProtocolError {
    /// An underlying cryptographic operation failed.
    #[error("crypto error: {0}")]
    Crypto(#[from] CryptoError),
    /// JSON serialization or deserialization failed.
    #[error("serialization error: {0}")]
    Serialization(#[from] serde_json::Error),
    /// The signature uses an algorithm other than Ed25519.
    #[error("unsupported signature algorithm: {0}")]
    UnsupportedAlgorithm(String),
    /// The `msg_type` field does not match the body's declared type.
    #[error("message type/body mismatch")]
    MessageTypeMismatch,
    /// A status string could not be parsed into the expected enum.
    #[error("unknown status value: {0}")]
    UnknownStatus(String),
}

/// Discriminator for protocol message types.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum MsgType {
    /// A new vision intent from a principal.
    VisionIntent,
    /// A project charter establishing a project from a vision.
    ProjectCharter,
    /// Approval granted for a scope (project or task).
    ApprovalGranted,
    /// Confirmation that an approval has been applied.
    ApprovalApplied,
    /// Query for available capabilities from a node.
    CapabilityQuery,
    /// Advertisement of a node's capabilities.
    CapabilityAdvertisement,
    /// Offer to join a project with required capabilities.
    JoinOffer,
    /// Acceptance of a join offer.
    JoinAccept,
    /// Rejection of a join offer.
    JoinReject,
    /// A task delegated to a worker agent.
    TaskDelegated,
    /// Progress update for a running task.
    TaskProgress,
    /// Final result submitted for a completed/failed task.
    TaskResultSubmitted,
    /// Evaluation certificate issued for a task result.
    EvaluationIssued,
    /// Proposal to publish results to an external target.
    PublishIntentProposed,
    /// Notification that a publish intent was skipped.
    PublishIntentSkipped,
    /// Record of a completed publish operation.
    PublishResultRecorded,
    /// Request for a state snapshot.
    SnapshotRequest,
    /// Response containing a state snapshot.
    SnapshotResponse,
    /// Order to stop a project or task tree.
    StopOrder,
    /// Acknowledgment that a stop order was received.
    StopAck,
    /// Confirmation that a stop has fully completed.
    StopComplete,
}

/// Trait implemented by all message body types to declare their [`MsgType`].
pub trait RoutedBody {
    /// Returns the message type discriminator for this body.
    fn msg_type(&self) -> MsgType;
}

/// An envelope that has not yet been signed.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct UnsignedEnvelope<T> {
    /// Protocol version string.
    pub protocol: String,
    /// Unique message identifier.
    pub msg_id: MessageId,
    /// Type discriminator matching the body.
    pub msg_type: MsgType,
    /// Actor that created this message.
    pub from_actor_id: ActorId,
    /// Intended recipient actor, if directed.
    pub to_actor_id: Option<ActorId>,
    /// Associated vision, if any.
    pub vision_id: Option<VisionId>,
    /// Associated project, if any.
    pub project_id: Option<ProjectId>,
    /// Associated task, if any.
    pub task_id: Option<TaskId>,
    /// Lamport logical timestamp for causal ordering.
    pub lamport_ts: u64,
    /// Wall-clock creation time.
    pub created_at: OffsetDateTime,
    /// Optional expiration time for this message.
    pub expires_at: Option<OffsetDateTime>,
    /// The typed message body.
    pub body: T,
}

/// A signed envelope carrying a typed message body.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Envelope<T> {
    /// Protocol version string.
    pub protocol: String,
    /// Unique message identifier.
    pub msg_id: MessageId,
    /// Type discriminator matching the body.
    pub msg_type: MsgType,
    /// Actor that created this message.
    pub from_actor_id: ActorId,
    /// Intended recipient actor, if directed.
    pub to_actor_id: Option<ActorId>,
    /// Associated vision, if any.
    pub vision_id: Option<VisionId>,
    /// Associated project, if any.
    pub project_id: Option<ProjectId>,
    /// Associated task, if any.
    pub task_id: Option<TaskId>,
    /// Lamport logical timestamp for causal ordering.
    pub lamport_ts: u64,
    /// Wall-clock creation time.
    pub created_at: OffsetDateTime,
    /// Optional expiration time for this message.
    pub expires_at: Option<OffsetDateTime>,
    /// The typed message body.
    pub body: T,
    /// Ed25519 signature over the canonical envelope content.
    pub signature: MessageSignature,
}

/// A wire-format envelope where the body is untyped JSON for transport.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct WireEnvelope {
    /// Protocol version string.
    pub protocol: String,
    /// Unique message identifier.
    pub msg_id: MessageId,
    /// Type discriminator for the body.
    pub msg_type: MsgType,
    /// Actor that created this message.
    pub from_actor_id: ActorId,
    /// Intended recipient actor, if directed.
    pub to_actor_id: Option<ActorId>,
    /// Associated vision, if any.
    pub vision_id: Option<VisionId>,
    /// Associated project, if any.
    pub project_id: Option<ProjectId>,
    /// Associated task, if any.
    pub task_id: Option<TaskId>,
    /// Lamport logical timestamp for causal ordering.
    pub lamport_ts: u64,
    /// Wall-clock creation time.
    pub created_at: OffsetDateTime,
    /// Optional expiration time for this message.
    pub expires_at: Option<OffsetDateTime>,
    /// Untyped JSON body for wire transport.
    pub body: Value,
    /// Ed25519 signature over the canonical envelope content.
    pub signature: MessageSignature,
}

#[derive(Serialize)]
struct SignableEnvelope<'a, T> {
    protocol: &'a str,
    msg_id: &'a MessageId,
    msg_type: &'a MsgType,
    from_actor_id: &'a ActorId,
    to_actor_id: &'a Option<ActorId>,
    vision_id: &'a Option<VisionId>,
    project_id: &'a Option<ProjectId>,
    task_id: &'a Option<TaskId>,
    lamport_ts: u64,
    created_at: OffsetDateTime,
    expires_at: &'a Option<OffsetDateTime>,
    body: &'a T,
}

impl<T> UnsignedEnvelope<T>
where
    T: RoutedBody + Serialize,
{
    /// Creates a new unsigned envelope with auto-generated message ID and timestamp.
    #[must_use]
    pub fn new(from_actor_id: ActorId, to_actor_id: Option<ActorId>, body: T) -> Self {
        Self {
            protocol: PROTOCOL_VERSION.to_owned(),
            msg_id: MessageId::generate(),
            msg_type: body.msg_type(),
            from_actor_id,
            to_actor_id,
            vision_id: None,
            project_id: None,
            task_id: None,
            lamport_ts: 1,
            created_at: OffsetDateTime::now_utc(),
            expires_at: None,
            body,
        }
    }

    /// Sets the vision ID on this envelope.
    #[must_use]
    pub fn with_vision_id(mut self, vision_id: VisionId) -> Self {
        self.vision_id = Some(vision_id);
        self
    }

    /// Sets the project ID on this envelope.
    #[must_use]
    pub fn with_project_id(mut self, project_id: ProjectId) -> Self {
        self.project_id = Some(project_id);
        self
    }

    /// Sets the task ID on this envelope.
    #[must_use]
    pub fn with_task_id(mut self, task_id: TaskId) -> Self {
        self.task_id = Some(task_id);
        self
    }

    /// Sets the Lamport logical timestamp on this envelope.
    #[must_use]
    pub fn with_lamport_ts(mut self, lamport_ts: u64) -> Self {
        self.lamport_ts = lamport_ts;
        self
    }

    /// Sets the expiration time on this envelope.
    #[must_use]
    pub fn with_expires_at(mut self, expires_at: OffsetDateTime) -> Self {
        self.expires_at = Some(expires_at);
        self
    }

    /// Signs this envelope with the given keypair, producing a signed [`Envelope`].
    pub fn sign(self, keypair: &StoredKeypair) -> Result<Envelope<T>, ProtocolError> {
        if self.msg_type != self.body.msg_type() {
            return Err(ProtocolError::MessageTypeMismatch);
        }

        let signature = keypair.sign_bytes(&self.signable_bytes()?)?;

        Ok(Envelope {
            protocol: self.protocol,
            msg_id: self.msg_id,
            msg_type: self.msg_type,
            from_actor_id: self.from_actor_id,
            to_actor_id: self.to_actor_id,
            vision_id: self.vision_id,
            project_id: self.project_id,
            task_id: self.task_id,
            lamport_ts: self.lamport_ts,
            created_at: self.created_at,
            expires_at: self.expires_at,
            body: self.body,
            signature,
        })
    }

    fn signable_bytes(&self) -> Result<Vec<u8>, ProtocolError> {
        Ok(canonical_json(&SignableEnvelope {
            protocol: &self.protocol,
            msg_id: &self.msg_id,
            msg_type: &self.msg_type,
            from_actor_id: &self.from_actor_id,
            to_actor_id: &self.to_actor_id,
            vision_id: &self.vision_id,
            project_id: &self.project_id,
            task_id: &self.task_id,
            lamport_ts: self.lamport_ts,
            created_at: self.created_at,
            expires_at: &self.expires_at,
            body: &self.body,
        })?)
    }
}

impl<T> Envelope<T>
where
    T: RoutedBody + Serialize,
{
    /// Verifies the envelope's signature against the given public key.
    pub fn verify_with_key(
        &self,
        verifying_key: &ed25519_dalek::VerifyingKey,
    ) -> Result<(), ProtocolError> {
        if self.signature.alg != "ed25519" {
            return Err(ProtocolError::UnsupportedAlgorithm(
                self.signature.alg.clone(),
            ));
        }

        if self.msg_type != self.body.msg_type() {
            return Err(ProtocolError::MessageTypeMismatch);
        }

        let signable = SignableEnvelope {
            protocol: &self.protocol,
            msg_id: &self.msg_id,
            msg_type: &self.msg_type,
            from_actor_id: &self.from_actor_id,
            to_actor_id: &self.to_actor_id,
            vision_id: &self.vision_id,
            project_id: &self.project_id,
            task_id: &self.task_id,
            lamport_ts: self.lamport_ts,
            created_at: self.created_at,
            expires_at: &self.expires_at,
            body: &self.body,
        };
        verify_bytes(verifying_key, &canonical_json(&signable)?, &self.signature)?;
        Ok(())
    }

    /// Converts this typed envelope into a wire-format envelope with untyped JSON body.
    pub fn into_wire(self) -> Result<WireEnvelope, ProtocolError> {
        Ok(WireEnvelope {
            protocol: self.protocol,
            msg_id: self.msg_id,
            msg_type: self.msg_type,
            from_actor_id: self.from_actor_id,
            to_actor_id: self.to_actor_id,
            vision_id: self.vision_id,
            project_id: self.project_id,
            task_id: self.task_id,
            lamport_ts: self.lamport_ts,
            created_at: self.created_at,
            expires_at: self.expires_at,
            body: serde_json::to_value(self.body)?,
            signature: self.signature,
        })
    }
}

impl WireEnvelope {
    /// Decodes the wire envelope's JSON body into a typed [`Envelope`].
    pub fn decode<T>(self) -> Result<Envelope<T>, ProtocolError>
    where
        T: RoutedBody + for<'de> Deserialize<'de>,
    {
        let body: T = serde_json::from_value(self.body)?;
        if body.msg_type() != self.msg_type {
            return Err(ProtocolError::MessageTypeMismatch);
        }
        Ok(Envelope {
            protocol: self.protocol,
            msg_id: self.msg_id,
            msg_type: self.msg_type,
            from_actor_id: self.from_actor_id,
            to_actor_id: self.to_actor_id,
            vision_id: self.vision_id,
            project_id: self.project_id,
            task_id: self.task_id,
            lamport_ts: self.lamport_ts,
            created_at: self.created_at,
            expires_at: self.expires_at,
            body,
            signature: self.signature,
        })
    }

    /// Verifies the wire envelope's signature against the given public key.
    pub fn verify_with_key(
        &self,
        verifying_key: &ed25519_dalek::VerifyingKey,
    ) -> Result<(), ProtocolError> {
        if self.signature.alg != "ed25519" {
            return Err(ProtocolError::UnsupportedAlgorithm(
                self.signature.alg.clone(),
            ));
        }

        let signable = SignableEnvelope {
            protocol: &self.protocol,
            msg_id: &self.msg_id,
            msg_type: &self.msg_type,
            from_actor_id: &self.from_actor_id,
            to_actor_id: &self.to_actor_id,
            vision_id: &self.vision_id,
            project_id: &self.project_id,
            task_id: &self.task_id,
            lamport_ts: self.lamport_ts,
            created_at: self.created_at,
            expires_at: &self.expires_at,
            body: &self.body,
        };
        verify_bytes(verifying_key, &canonical_json(&signable)?, &self.signature)?;
        Ok(())
    }
}

/// Lifecycle status of a project.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProjectStatus {
    /// Project is being planned and tasks have not started.
    Planning,
    /// Project is actively executing tasks.
    Active,
    /// A stop order has been issued; tasks are draining.
    Stopping,
    /// All tasks have been stopped and the project is terminated.
    Stopped,
}

impl ProjectStatus {
    /// Returns the string representation of this status.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Planning => "planning",
            Self::Active => "active",
            Self::Stopping => "stopping",
            Self::Stopped => "stopped",
        }
    }
}

impl std::fmt::Display for ProjectStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for ProjectStatus {
    type Err = ProtocolError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "planning" => Ok(Self::Planning),
            "active" => Ok(Self::Active),
            "stopping" => Ok(Self::Stopping),
            "stopped" => Ok(Self::Stopped),
            other => Err(ProtocolError::UnknownStatus(other.to_owned())),
        }
    }
}

/// Lifecycle status of a task.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TaskStatus {
    /// Task is queued and awaiting assignment.
    Queued,
    /// Task has been offered to a worker.
    Offered,
    /// Worker accepted the task.
    Accepted,
    /// Task is actively being executed.
    Running,
    /// Worker submitted a result, pending evaluation.
    Submitted,
    /// Task completed successfully.
    Completed,
    /// Task execution failed.
    Failed,
    /// A stop order is being applied to this task.
    Stopping,
    /// Task was stopped before completion.
    Stopped,
}

impl TaskStatus {
    /// Returns the string representation of this status.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Queued => "queued",
            Self::Offered => "offered",
            Self::Accepted => "accepted",
            Self::Running => "running",
            Self::Submitted => "submitted",
            Self::Completed => "completed",
            Self::Failed => "failed",
            Self::Stopping => "stopping",
            Self::Stopped => "stopped",
        }
    }

    /// Returns `true` if the task is in a non-terminal, non-submitted state.
    #[must_use]
    pub fn is_active(&self) -> bool {
        matches!(
            self,
            Self::Queued | Self::Accepted | Self::Running | Self::Stopping
        )
    }

    /// Returns `true` if the task has reached a final state.
    #[must_use]
    pub fn is_terminal(&self) -> bool {
        matches!(self, Self::Completed | Self::Failed | Self::Stopped)
    }
}

impl std::fmt::Display for TaskStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for TaskStatus {
    type Err = ProtocolError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "queued" => Ok(Self::Queued),
            "offered" => Ok(Self::Offered),
            "accepted" => Ok(Self::Accepted),
            "running" => Ok(Self::Running),
            "submitted" => Ok(Self::Submitted),
            "completed" => Ok(Self::Completed),
            "failed" => Ok(Self::Failed),
            "stopping" => Ok(Self::Stopping),
            "stopped" => Ok(Self::Stopped),
            other => Err(ProtocolError::UnknownStatus(other.to_owned())),
        }
    }
}

/// Constraints applied to a vision that guide task planning and execution.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct VisionConstraints {
    /// Budget mode (e.g. `"minimal"`, `"standard"`).
    pub budget_mode: Option<String>,
    /// Whether external agents may participate.
    pub allow_external_agents: Option<bool>,
    /// Human intervention policy (e.g. `"required"`, `"none"`).
    pub human_intervention: Option<String>,
    /// Additional constraint key-value pairs.
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

/// A vision intent submitted by a principal to initiate work.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct VisionIntent {
    /// Short human-readable title for the vision.
    pub title: String,
    /// Raw free-form vision text to be decomposed into tasks.
    pub raw_vision_text: String,
    /// Constraints governing how this vision should be executed.
    pub constraints: VisionConstraints,
}

impl RoutedBody for VisionIntent {
    fn msg_type(&self) -> MsgType {
        MsgType::VisionIntent
    }
}

/// Policy controlling which agents may participate in a project.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ParticipantPolicy {
    /// Whether agents outside the local node may join.
    pub external_agents_allowed: bool,
}

/// Weights for multi-dimensional task evaluation scoring.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EvaluationPolicy {
    /// Weight for the quality dimension.
    pub quality_weight: f32,
    /// Weight for the speed dimension.
    pub speed_weight: f32,
    /// Weight for the reliability dimension.
    pub reliability_weight: f32,
    /// Weight for the alignment dimension.
    pub alignment_weight: f32,
}

/// A project charter establishing a new project from a vision.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ProjectCharter {
    /// Unique project identifier.
    pub project_id: ProjectId,
    /// The vision that spawned this project.
    pub vision_id: VisionId,
    /// The principal (human) who initiated the vision.
    pub principal_actor_id: ActorId,
    /// The agent that owns and orchestrates the project.
    pub owner_actor_id: ActorId,
    /// Short project title.
    pub title: String,
    /// High-level project objective.
    pub objective: String,
    /// Actor authorized to issue stop orders.
    pub stop_authority_actor_id: ActorId,
    /// Policy for agent participation.
    pub participant_policy: ParticipantPolicy,
    /// Policy for task result evaluation.
    pub evaluation_policy: EvaluationPolicy,
}

impl RoutedBody for ProjectCharter {
    fn msg_type(&self) -> MsgType {
        MsgType::ProjectCharter
    }
}

/// The scope an approval applies to.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ApprovalScopeType {
    /// Approval for an entire project.
    Project,
    /// Approval for a specific task.
    Task,
}

/// Notification that an approval has been granted.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ApprovalGranted {
    /// Whether this approval is for a project or task.
    pub scope_type: ApprovalScopeType,
    /// The ID of the approved project or task.
    pub scope_id: String,
    /// When the approval was granted.
    pub approved_at: OffsetDateTime,
}

impl RoutedBody for ApprovalGranted {
    fn msg_type(&self) -> MsgType {
        MsgType::ApprovalGranted
    }
}

/// Confirmation that an approval was applied and tasks may have been resumed.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ApprovalApplied {
    /// The scope this approval applies to.
    pub scope_type: ApprovalScopeType,
    /// The ID of the approved project or task.
    pub scope_id: String,
    /// Reference to the original approval message.
    pub approval_granted_msg_id: MessageId,
    /// Whether the approval state was updated (vs already applied).
    pub approval_updated: bool,
    /// Task IDs that were resumed as a result.
    pub resumed_task_ids: Vec<String>,
    /// Whether task dispatching occurred after this approval.
    pub dispatched: bool,
    /// When the approval was applied.
    pub applied_at: OffsetDateTime,
}

impl RoutedBody for ApprovalApplied {
    fn msg_type(&self) -> MsgType {
        MsgType::ApprovalApplied
    }
}

/// A query announcing a node's identity and requesting peer capabilities.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CapabilityQuery {
    /// The querying node's identifier.
    pub node_id: NodeId,
    /// Base64-encoded public key for message verification.
    pub public_key: String,
    /// Optional separate public key for stop order verification.
    pub stop_public_key: Option<String>,
    /// Capabilities this node offers.
    pub capabilities: Vec<String>,
    /// Network addresses where this node can be reached.
    pub listen_addresses: Vec<String>,
    /// When this query was created.
    pub requested_at: OffsetDateTime,
}

impl RoutedBody for CapabilityQuery {
    fn msg_type(&self) -> MsgType {
        MsgType::CapabilityQuery
    }
}

/// A node's response advertising its identity and capabilities.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CapabilityAdvertisement {
    /// The advertising node's identifier.
    pub node_id: NodeId,
    /// Base64-encoded public key for message verification.
    pub public_key: String,
    /// Optional separate public key for stop order verification.
    pub stop_public_key: Option<String>,
    /// Capabilities this node offers.
    pub capabilities: Vec<String>,
    /// Network addresses where this node can be reached.
    pub listen_addresses: Vec<String>,
    /// When this advertisement was created.
    pub advertised_at: OffsetDateTime,
}

impl RoutedBody for CapabilityAdvertisement {
    fn msg_type(&self) -> MsgType {
        MsgType::CapabilityAdvertisement
    }
}

/// An offer from a project owner to a worker to join and execute tasks.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct JoinOffer {
    /// Capabilities the worker must possess.
    pub required_capabilities: Vec<String>,
    /// Brief outline of the work to be done.
    pub task_outline: String,
    /// Estimated duration in seconds.
    pub expected_duration_sec: u64,
}

impl RoutedBody for JoinOffer {
    fn msg_type(&self) -> MsgType {
        MsgType::JoinOffer
    }
}

/// A worker's acceptance of a join offer.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct JoinAccept {
    /// Always `true` for an acceptance.
    pub accepted: bool,
    /// The capabilities the worker confirms it can provide.
    pub capabilities_confirmed: Vec<String>,
}

impl RoutedBody for JoinAccept {
    fn msg_type(&self) -> MsgType {
        MsgType::JoinAccept
    }
}

/// A worker's rejection of a join offer.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct JoinReject {
    /// Always `false` for a rejection.
    pub accepted: bool,
    /// Reason for rejecting the offer.
    pub reason: String,
}

impl RoutedBody for JoinReject {
    fn msg_type(&self) -> MsgType {
        MsgType::JoinReject
    }
}

/// A task delegated from the project owner to a worker agent.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TaskDelegated {
    /// Parent task ID if this is a sub-task.
    pub parent_task_id: Option<TaskId>,
    /// Short task title.
    pub title: String,
    /// Detailed task description.
    pub description: String,
    /// The objective this task must fulfill.
    pub objective: String,
    /// Capability required to execute this task.
    pub required_capability: String,
    /// Structured input data for the worker.
    pub input_payload: Value,
    /// JSON schema describing the expected output format.
    pub expected_output_schema: Value,
}

impl RoutedBody for TaskDelegated {
    fn msg_type(&self) -> MsgType {
        MsgType::TaskDelegated
    }
}

/// A progress update from a worker for a running task.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TaskProgress {
    /// Progress fraction (0.0 to 1.0).
    pub progress: f32,
    /// Human-readable progress message.
    pub message: String,
    /// When this update was recorded.
    pub updated_at: OffsetDateTime,
}

impl RoutedBody for TaskProgress {
    fn msg_type(&self) -> MsgType {
        MsgType::TaskProgress
    }
}

/// Encryption metadata for an artifact.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ArtifactEncryption {
    /// Encryption mode (e.g. `"aes-256-gcm"`).
    pub mode: String,
    /// Actors who can decrypt this artifact.
    pub recipients: Vec<ActorId>,
}

/// A reference to a stored artifact produced by a task.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ArtifactRef {
    /// Unique artifact identifier.
    pub artifact_id: ArtifactId,
    /// Storage scheme (e.g. `"file"`, `"s3"`).
    pub scheme: String,
    /// URI pointing to the artifact location.
    pub uri: String,
    /// SHA-256 hash of the artifact content.
    pub sha256: Option<String>,
    /// Size of the artifact in bytes.
    pub size: Option<u64>,
    /// Encryption metadata, if the artifact is encrypted.
    pub encryption: Option<ArtifactEncryption>,
}

/// Outcome status of a task execution.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TaskExecutionStatus {
    /// Task finished successfully.
    Completed,
    /// Task execution failed.
    Failed,
    /// Task was stopped before completion.
    Stopped,
}

/// A task result submitted by a worker after execution.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TaskResultSubmitted {
    /// Outcome status of the execution.
    pub status: TaskExecutionStatus,
    /// Human-readable summary of what was accomplished.
    pub summary: String,
    /// Structured output data.
    pub output_payload: Value,
    /// References to produced artifacts.
    pub artifact_refs: Vec<ArtifactRef>,
    /// When execution began.
    pub started_at: OffsetDateTime,
    /// When execution ended.
    pub finished_at: OffsetDateTime,
}

impl RoutedBody for TaskResultSubmitted {
    fn msg_type(&self) -> MsgType {
        MsgType::TaskResultSubmitted
    }
}

/// An evaluation certificate issued for a task result.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EvaluationIssued {
    /// The actor whose work is being evaluated.
    pub subject_actor_id: ActorId,
    /// Per-dimension scores (e.g. quality, speed, reliability, alignment).
    pub scores: BTreeMap<String, f32>,
    /// Evaluator's comment or summary.
    pub comment: String,
}

impl RoutedBody for EvaluationIssued {
    fn msg_type(&self) -> MsgType {
        MsgType::EvaluationIssued
    }
}

/// A proposal to publish results to an external target.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PublishIntentProposed {
    /// Scope type (e.g. `"project"`, `"task"`).
    pub scope_type: String,
    /// ID of the scoped entity.
    pub scope_id: String,
    /// Publish target identifier.
    pub target: String,
    /// Reason for publishing.
    pub reason: String,
    /// Summary of what will be published.
    pub summary: String,
    /// Additional context data.
    pub context: Value,
    /// When this proposal was created.
    pub proposed_at: OffsetDateTime,
}

impl RoutedBody for PublishIntentProposed {
    fn msg_type(&self) -> MsgType {
        MsgType::PublishIntentProposed
    }
}

/// Notification that a publish intent was skipped.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PublishIntentSkipped {
    /// Scope type (e.g. `"project"`, `"task"`).
    pub scope_type: String,
    /// ID of the scoped entity.
    pub scope_id: String,
    /// Publish target identifier.
    pub target: String,
    /// Reason for skipping.
    pub reason: String,
    /// Additional context data.
    pub context: Value,
    /// When this skip was recorded.
    pub skipped_at: OffsetDateTime,
}

impl RoutedBody for PublishIntentSkipped {
    fn msg_type(&self) -> MsgType {
        MsgType::PublishIntentSkipped
    }
}

/// Record of a completed publish operation.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PublishResultRecorded {
    /// Scope type (e.g. `"project"`, `"task"`).
    pub scope_type: String,
    /// ID of the scoped entity.
    pub scope_id: String,
    /// Publish target identifier.
    pub target: String,
    /// Outcome status of the publish operation.
    pub status: String,
    /// Published artifact location, if applicable.
    pub location: Option<String>,
    /// Detail about the publish result.
    pub detail: String,
    /// Structured result payload.
    pub result_payload: Value,
    /// When this result was recorded.
    pub recorded_at: OffsetDateTime,
}

impl RoutedBody for PublishResultRecorded {
    fn msg_type(&self) -> MsgType {
        MsgType::PublishResultRecorded
    }
}

/// Scope type for snapshot requests and responses.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SnapshotScopeType {
    /// Snapshot of a project.
    Project,
    /// Snapshot of a task.
    Task,
}

/// A request to take a state snapshot.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SnapshotRequest {
    /// Whether this is a project or task snapshot.
    pub scope_type: SnapshotScopeType,
    /// ID of the entity to snapshot.
    pub scope_id: String,
}

impl RoutedBody for SnapshotRequest {
    fn msg_type(&self) -> MsgType {
        MsgType::SnapshotRequest
    }
}

/// A response containing a state snapshot.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SnapshotResponse {
    /// Whether this is a project or task snapshot.
    pub scope_type: SnapshotScopeType,
    /// ID of the snapshotted entity.
    pub scope_id: String,
    /// The snapshot data.
    pub snapshot: Value,
}

impl RoutedBody for SnapshotResponse {
    fn msg_type(&self) -> MsgType {
        MsgType::SnapshotResponse
    }
}

/// Scope type for stop orders.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StopScopeType {
    /// Stop an entire project.
    Project,
    /// Stop a task and all its sub-tasks.
    TaskTree,
}

/// An order to stop a project or task tree.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StopOrder {
    /// Unique stop order identifier.
    pub stop_id: StopId,
    /// Whether this stops a project or a task tree.
    pub scope_type: StopScopeType,
    /// ID of the entity being stopped.
    pub scope_id: String,
    /// Machine-readable reason code.
    pub reason_code: String,
    /// Human-readable reason text.
    pub reason_text: String,
    /// When the stop order was issued.
    pub issued_at: OffsetDateTime,
}

impl RoutedBody for StopOrder {
    fn msg_type(&self) -> MsgType {
        MsgType::StopOrder
    }
}

/// State in a stop acknowledgment.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StopAckState {
    /// The actor is in the process of stopping.
    Stopping,
}

/// Acknowledgment that a stop order was received and is being processed.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StopAck {
    /// The stop order being acknowledged.
    pub stop_id: StopId,
    /// The actor sending this acknowledgment.
    pub actor_id: ActorId,
    /// Current stop state of the acknowledging actor.
    pub ack_state: StopAckState,
    /// When this acknowledgment was sent.
    pub acked_at: OffsetDateTime,
}

impl RoutedBody for StopAck {
    fn msg_type(&self) -> MsgType {
        MsgType::StopAck
    }
}

/// Final state in a stop completion message.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StopFinalState {
    /// The actor has fully stopped.
    Stopped,
}

/// Confirmation that all work has stopped for a given stop order.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct StopComplete {
    /// The stop order that has been completed.
    pub stop_id: StopId,
    /// The actor confirming completion.
    pub actor_id: ActorId,
    /// The final state (always `Stopped`).
    pub final_state: StopFinalState,
    /// When the stop was fully completed.
    pub completed_at: OffsetDateTime,
}

impl RoutedBody for StopComplete {
    fn msg_type(&self) -> MsgType {
        MsgType::StopComplete
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use starweft_crypto::StoredKeypair;
    use starweft_id::ActorId;

    #[test]
    fn signed_envelope_round_trip_verifies() {
        let keypair = StoredKeypair::generate();
        let actor_id = ActorId::generate();
        let envelope = UnsignedEnvelope::new(
            actor_id,
            None,
            VisionIntent {
                title: "vision".to_owned(),
                raw_vision_text: "build something".to_owned(),
                constraints: VisionConstraints::default(),
            },
        )
        .sign(&keypair)
        .expect("sign");

        envelope
            .verify_with_key(&keypair.verifying_key().expect("verifying key"))
            .expect("verify");
    }

    #[test]
    fn wire_envelope_round_trip_verifies() {
        let keypair = StoredKeypair::generate();
        let actor_id = ActorId::generate();
        let wire = UnsignedEnvelope::new(
            actor_id,
            None,
            VisionIntent {
                title: "vision".to_owned(),
                raw_vision_text: "build something".to_owned(),
                constraints: VisionConstraints::default(),
            },
        )
        .sign(&keypair)
        .expect("sign")
        .into_wire()
        .expect("wire");

        wire.verify_with_key(&keypair.verifying_key().expect("verifying key"))
            .expect("verify");
    }
}