lazily 0.35.0

Lazy reactive signals with dependency tracking and cache invalidation
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
//! Command / RPC message plane (`command-plane-v1`).
//!
//! An evented command message family that is an additive sibling to
//! `Snapshot` / `Delta` / `CrdtSync`. Editor and runtime integrations submit
//! commands ([`CommandSubmit`]), observe progress ([`CommandEvents`]), preempt
//! ([`CommandCancel`]), and resync after reconnect ([`CommandProjection`]).
//!
//! The one hard rule: **terminal authority is the causal receipt**, not the
//! event or the transport. `observed` / `accepted` / `started` events are
//! non-terminal progress; a command becomes terminal only when a terminal
//! [`CausalReceipt`](crate::receipt::CausalReceipt) folds in. The RPC facade
//! ([`CommandRpcClient`]) is derived behavior over the [`CommandProjection`]
//! reducer: a unary `call` resolves only on a terminal projection, never on an
//! ACK or an `accepted` event.
//!
//! Framing helpers are transport-agnostic; Unix-socket / WebSocket I/O stays
//! outside this module and is provided through the [`CommandTransport`] trait.

use std::collections::{BTreeMap, BTreeSet};

use crate::ipc::IpcValue;
use crate::receipt::{CausalReceipt, ReceiptOutcome};

/// How the admitter collapses concurrent / duplicate submits.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum DedupePolicy {
    /// No dedupe.
    None,
    /// Collapse submits sharing an idempotency key.
    SameIdempotencyKey,
    /// Collapse submits sharing a command id.
    SameCommandId,
}

/// Admission policy for a command.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CommandPolicy {
    /// Dedupe strategy.
    pub dedupe: DedupePolicy,
    /// Whether a newer submit with the same idempotency key supersedes an older
    /// non-terminal command.
    pub supersede: bool,
    /// Whether the admitter may cancel a non-terminal command on preemption.
    pub cancel_on_preempt: bool,
}

/// A command submission. lazily owns this envelope; the `namespace` owns the
/// `payload` body, which lazily never interprets.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CommandSubmit {
    /// Stable, replay-safe command id.
    pub command_id: String,
    /// Causal parent id (a command or event id); self-caused submits set this
    /// equal to `command_id`.
    pub causation_id: String,
    /// Submitter identity (e.g. `vscode-plugin`).
    pub source: String,
    /// Intended handler identity (e.g. `project-controller`).
    pub target: String,
    /// Domain namespace owning the payload schema (e.g. `agent-doc`).
    pub namespace: String,
    /// Command name within the namespace (e.g. `editor_route`).
    pub name: String,
    /// Authority/controller generation the command targets.
    pub authority_generation: u64,
    /// Dedupe / supersede key.
    pub idempotency_key: String,
    /// Deadline in milliseconds; `0` means no deadline.
    pub deadline_ms: u64,
    /// Admission policy.
    pub policy: CommandPolicy,
    /// Fully-qualified domain payload type (e.g. `agent-doc.editor_route.v1`).
    pub payload_type: String,
    /// Content hash of the payload body (e.g. `sha256:…`).
    pub payload_hash: String,
    /// Domain payload as inline bytes or a shared-memory blob reference.
    pub payload: IpcValue,
    /// Features the target must advertise or the submit fails closed.
    pub required_features: Vec<String>,
}

/// A cancel/preempt request for a still-non-terminal command.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CommandCancel {
    /// Command to cancel.
    pub command_id: String,
    /// Id of the cancel request itself (for its own receipt/replay).
    pub causation_id: String,
    /// Requester identity.
    pub source: String,
    /// Generation the cancel targets; a stale-generation cancel is ignored.
    pub authority_generation: u64,
    /// Optional cancel reason.
    pub reason: Option<String>,
}

/// Progress/detail event kind. UX and diagnostics only — never terminal proof.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum CommandEventKind {
    /// The target observed the command.
    Observed,
    /// The command was accepted/queued.
    Accepted,
    /// Execution started.
    Started,
    /// Mid-execution progress detail.
    Progress,
    /// A cancel was surfaced (terminal authority is a matching rejected receipt).
    Cancelled,
    /// The command was superseded by a newer submit.
    Superseded,
    /// The command deadline elapsed.
    TimedOut,
}

/// One progress/detail event keyed by `command_id`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CommandEvent {
    /// Idempotency key for this event.
    pub event_id: String,
    /// Command this event describes.
    pub command_id: String,
    /// Event kind.
    pub kind: CommandEventKind,
    /// Authority generation; events outside the current generation are ignored.
    pub generation: u64,
    /// Optional human/diagnostics detail. Not proof of effect.
    pub detail: Option<String>,
}

/// A batch of command events.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CommandEvents {
    /// Events in this batch.
    pub events: Vec<CommandEvent>,
}

/// Folded projection status for one command. `submitted` / `accepted` /
/// `running` are non-terminal; the rest are terminal and backed by a terminal
/// causal receipt.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum CommandStatus {
    /// Submitted, not yet acknowledged.
    Submitted,
    /// Accepted/queued by the target.
    Accepted,
    /// Execution started.
    Running,
    /// Terminal: applied.
    Applied,
    /// Terminal: rejected.
    Rejected,
    /// Terminal: cancelled (rejected receipt, reason `cancelled`).
    Cancelled,
    /// Terminal: superseded.
    Superseded,
    /// Terminal: timed out.
    TimedOut,
}

impl CommandStatus {
    /// Whether this status is terminal.
    #[must_use]
    pub const fn is_terminal(self) -> bool {
        matches!(
            self,
            Self::Applied | Self::Rejected | Self::Cancelled | Self::Superseded | Self::TimedOut
        )
    }
}

/// One command's folded projection entry.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CommandProjectionEntry {
    /// Command id.
    pub command_id: String,
    /// Folded status.
    pub status: CommandStatus,
    /// True iff a terminal causal receipt has folded in.
    pub terminal: bool,
    /// Current authority generation for the command.
    pub generation: u64,
    /// Terminal reason, or `null` while non-terminal / applied without reason.
    pub reason: Option<String>,
    /// Receipt id that made the command terminal, or `null`.
    pub terminal_receipt_id: Option<String>,
    /// Last folded event id, or `null`.
    pub last_event_id: Option<String>,
}

/// A queryable image of command state; also the reconnect resync frame.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CommandProjectionImage {
    /// Authority generation this image was taken at.
    pub generation: u64,
    /// Command entries, ordered by command id.
    pub commands: Vec<CommandProjectionEntry>,
}

/// Externally-tagged command-plane wire message. Sibling to `IpcMessage` /
/// `ReceiptMessage`, not a new state-plane variant.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CommandMessage {
    /// Submit a command. Boxed because the submit envelope is far larger than
    /// the other variants.
    CommandSubmit(Box<CommandSubmit>),
    /// Cancel a command.
    CommandCancel(CommandCancel),
    /// A batch of progress events.
    CommandEvents(CommandEvents),
    /// A projection resync image.
    CommandProjection(CommandProjectionImage),
}

/// Result of folding one command-plane input into a [`CommandProjection`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CommandApplyStatus {
    /// The input updated the projection.
    Recorded,
    /// The input id (command/event/receipt) was already seen; no change.
    Duplicate,
    /// The input targets a command not present in the projection; no change.
    Unknown,
    /// The input's generation does not match the command's current generation.
    StaleGeneration {
        /// Command's current generation.
        expected: u64,
        /// Generation carried by the input.
        actual: u64,
    },
    /// A different terminal outcome already exists for this command; fail closed.
    TerminalConflict {
        /// Command id with conflicting terminal outcomes.
        command_id: String,
        /// Existing terminal status.
        existing: CommandStatus,
        /// Incoming conflicting terminal status.
        incoming: CommandStatus,
    },
}

/// The folded command projection reducer.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CommandProjection {
    generation: u64,
    entries: BTreeMap<String, CommandProjectionEntry>,
    seen_event_ids: BTreeSet<String>,
    seen_receipt_ids: BTreeSet<String>,
    seen_cancel_ids: BTreeSet<String>,
    conflicts: BTreeSet<String>,
}

/// Map a terminal receipt outcome + reason to a folded [`CommandStatus`].
fn terminal_status_of(outcome: ReceiptOutcome, reason: Option<&str>) -> CommandStatus {
    match outcome {
        ReceiptOutcome::Applied => CommandStatus::Applied,
        ReceiptOutcome::Rejected => match reason {
            Some("cancelled") => CommandStatus::Cancelled,
            Some("superseded") => CommandStatus::Superseded,
            Some("timed_out") => CommandStatus::TimedOut,
            _ => CommandStatus::Rejected,
        },
        // Non-terminal outcomes never reach here (guarded by is_terminal).
        ReceiptOutcome::Observed | ReceiptOutcome::Accepted => CommandStatus::Accepted,
    }
}

/// The non-terminal status a progress event advances to, if any.
fn progress_status_of(kind: CommandEventKind) -> Option<CommandStatus> {
    match kind {
        CommandEventKind::Observed | CommandEventKind::Accepted => Some(CommandStatus::Accepted),
        CommandEventKind::Started | CommandEventKind::Progress => Some(CommandStatus::Running),
        // cancelled/superseded/timed_out events are UX only; status change waits
        // for the terminal receipt.
        CommandEventKind::Cancelled | CommandEventKind::Superseded | CommandEventKind::TimedOut => {
            None
        }
    }
}

/// Monotonic phase rank so progress never regresses status.
fn phase_rank(status: CommandStatus) -> u8 {
    match status {
        CommandStatus::Submitted => 0,
        CommandStatus::Accepted => 1,
        CommandStatus::Running => 2,
        _ => 3, // terminal
    }
}

impl CommandProjection {
    /// Create an empty projection.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// The current authority generation the projection has folded to.
    #[must_use]
    pub fn generation(&self) -> u64 {
        self.generation
    }

    /// Fold a command-plane message.
    pub fn apply_message(&mut self, message: &CommandMessage) -> CommandApplyStatus {
        match message {
            CommandMessage::CommandSubmit(s) => self.submit(s),
            CommandMessage::CommandCancel(c) => self.cancel(c),
            CommandMessage::CommandEvents(b) => {
                let mut last = CommandApplyStatus::Unknown;
                for event in &b.events {
                    last = self.event(event);
                }
                last
            }
            CommandMessage::CommandProjection(image) => self.apply_projection(image),
        }
    }

    /// Fold a command submission.
    pub fn submit(&mut self, submit: &CommandSubmit) -> CommandApplyStatus {
        if self.entries.contains_key(&submit.command_id) {
            return CommandApplyStatus::Duplicate;
        }
        self.generation = self.generation.max(submit.authority_generation);
        self.entries.insert(
            submit.command_id.clone(),
            CommandProjectionEntry {
                command_id: submit.command_id.clone(),
                status: CommandStatus::Submitted,
                terminal: false,
                generation: submit.authority_generation,
                reason: None,
                terminal_receipt_id: None,
                last_event_id: None,
            },
        );
        CommandApplyStatus::Recorded
    }

    /// Fold a progress event.
    pub fn event(&mut self, event: &CommandEvent) -> CommandApplyStatus {
        if self.seen_event_ids.contains(&event.event_id) {
            return CommandApplyStatus::Duplicate;
        }
        let Some(entry) = self.entries.get_mut(&event.command_id) else {
            return CommandApplyStatus::Unknown;
        };
        if event.generation != entry.generation {
            return CommandApplyStatus::StaleGeneration {
                expected: entry.generation,
                actual: event.generation,
            };
        }
        self.seen_event_ids.insert(event.event_id.clone());
        entry.last_event_id = Some(event.event_id.clone());
        if !entry.terminal
            && let Some(next) = progress_status_of(event.kind)
            && phase_rank(next) >= phase_rank(entry.status)
        {
            entry.status = next;
        }
        CommandApplyStatus::Recorded
    }

    /// Fold a cancel request. A cancel is non-terminal by itself; the terminal
    /// outcome folds through the matching rejected receipt.
    pub fn cancel(&mut self, cancel: &CommandCancel) -> CommandApplyStatus {
        if self.seen_cancel_ids.contains(&cancel.causation_id) {
            return CommandApplyStatus::Duplicate;
        }
        let Some(entry) = self.entries.get(&cancel.command_id) else {
            return CommandApplyStatus::Unknown;
        };
        if cancel.authority_generation != entry.generation {
            return CommandApplyStatus::StaleGeneration {
                expected: entry.generation,
                actual: cancel.authority_generation,
            };
        }
        self.seen_cancel_ids.insert(cancel.causation_id.clone());
        // A cancel after a terminal outcome is ignored (recorded but no change).
        CommandApplyStatus::Recorded
    }

    /// Fold a causal receipt (terminal authority) keyed by `causation_id` ==
    /// `command_id`.
    pub fn observe_receipt(&mut self, receipt: &CausalReceipt) -> CommandApplyStatus {
        if self.seen_receipt_ids.contains(&receipt.receipt_id) {
            return CommandApplyStatus::Duplicate;
        }
        let Some(entry) = self.entries.get(&receipt.causation_id) else {
            return CommandApplyStatus::Unknown;
        };
        if receipt.generation != entry.generation {
            return CommandApplyStatus::StaleGeneration {
                expected: entry.generation,
                actual: receipt.generation,
            };
        }
        if !receipt.outcome.is_terminal() {
            // Non-terminal receipt: record id, advance progress, keep non-terminal.
            self.seen_receipt_ids.insert(receipt.receipt_id.clone());
            let entry = self
                .entries
                .get_mut(&receipt.causation_id)
                .expect("present");
            if !entry.terminal && phase_rank(CommandStatus::Accepted) >= phase_rank(entry.status) {
                entry.status = CommandStatus::Accepted;
            }
            return CommandApplyStatus::Recorded;
        }
        let incoming = terminal_status_of(receipt.outcome, receipt.reason.as_deref());
        if entry.terminal {
            if entry.status == incoming {
                // Idempotent terminal.
                self.seen_receipt_ids.insert(receipt.receipt_id.clone());
                return CommandApplyStatus::Recorded;
            }
            let existing = entry.status;
            self.conflicts.insert(receipt.causation_id.clone());
            return CommandApplyStatus::TerminalConflict {
                command_id: receipt.causation_id.clone(),
                existing,
                incoming,
            };
        }
        self.seen_receipt_ids.insert(receipt.receipt_id.clone());
        let entry = self
            .entries
            .get_mut(&receipt.causation_id)
            .expect("present");
        entry.terminal = true;
        entry.status = incoming;
        entry.reason = receipt.reason.clone();
        entry.terminal_receipt_id = Some(receipt.receipt_id.clone());
        CommandApplyStatus::Recorded
    }

    /// Fold a projection resync image (reconnect / handoff).
    pub fn apply_projection(&mut self, image: &CommandProjectionImage) -> CommandApplyStatus {
        self.generation = self.generation.max(image.generation);
        for entry in &image.commands {
            self.entries.insert(entry.command_id.clone(), entry.clone());
            if let Some(ev) = &entry.last_event_id {
                self.seen_event_ids.insert(ev.clone());
            }
            if let Some(rc) = &entry.terminal_receipt_id {
                self.seen_receipt_ids.insert(rc.clone());
            }
        }
        CommandApplyStatus::Recorded
    }

    /// Look up a command's entry.
    #[must_use]
    pub fn entry(&self, command_id: &str) -> Option<&CommandProjectionEntry> {
        self.entries.get(command_id)
    }

    /// The terminal entry for a command, if it has reached a terminal outcome.
    #[must_use]
    pub fn terminal_for(&self, command_id: &str) -> Option<&CommandProjectionEntry> {
        self.entries.get(command_id).filter(|e| e.terminal)
    }

    /// Whether a command has recorded a terminal conflict (fail-closed).
    #[must_use]
    pub fn has_conflict(&self, command_id: &str) -> bool {
        self.conflicts.contains(command_id)
    }

    /// Snapshot the projection as a wire image, ordered by command id.
    #[must_use]
    pub fn to_image(&self) -> CommandProjectionImage {
        CommandProjectionImage {
            generation: self.generation,
            commands: self.entries.values().cloned().collect(),
        }
    }
}

/// Convenience: a terminal `applied` receipt keyed by a command id.
#[must_use]
pub fn applied_receipt(
    receipt_id: impl Into<String>,
    command_id: impl Into<String>,
    observer: impl Into<String>,
    generation: u64,
) -> CausalReceipt {
    CausalReceipt::applied(receipt_id, command_id, observer, generation)
}

/// Convenience: a terminal `rejected` receipt keyed by a command id, with reason.
#[must_use]
pub fn rejected_receipt(
    receipt_id: impl Into<String>,
    command_id: impl Into<String>,
    observer: impl Into<String>,
    generation: u64,
    reason: impl Into<String>,
) -> CausalReceipt {
    CausalReceipt::rejected(receipt_id, command_id, observer, generation).with_reason(reason)
}

/// Transport used by [`CommandRpcClient`] to emit command-plane frames. I/O
/// specifics (Unix socket, WebSocket) live behind this trait, outside the pure
/// type module.
pub trait CommandTransport {
    /// Transport error type.
    type Error;
    /// Send one command-plane message.
    ///
    /// # Errors
    /// Returns the transport's error if the frame could not be enqueued.
    fn send(&mut self, message: &CommandMessage) -> Result<(), Self::Error>;
}

/// Resolution state of an RPC `call`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CallState {
    /// The command has not reached a terminal outcome; `call` must keep waiting.
    Pending,
    /// The command reached a terminal outcome; `call` may resolve with it.
    Resolved(CommandProjectionEntry),
    /// The command failed closed on a terminal conflict.
    Conflict,
}

/// RPC facade over the command plane. `submit`/`call` build and send
/// `CommandSubmit`; incoming frames and receipts are folded via `ingest_*`; a
/// unary `call` resolves only when the projection reaches a terminal outcome.
#[derive(Debug)]
pub struct CommandRpcClient<T: CommandTransport> {
    transport: T,
    projection: CommandProjection,
}

impl<T: CommandTransport> CommandRpcClient<T> {
    /// Wrap a transport.
    pub fn new(transport: T) -> Self {
        Self {
            transport,
            projection: CommandProjection::new(),
        }
    }

    /// Borrow the folded projection.
    #[must_use]
    pub fn projection(&self) -> &CommandProjection {
        &self.projection
    }

    /// Submit a command: send `CommandSubmit`, fold it locally, return its id.
    ///
    /// # Errors
    /// Returns the transport error if the submit frame could not be sent.
    pub fn submit(&mut self, submit: CommandSubmit) -> Result<String, T::Error> {
        let command_id = submit.command_id.clone();
        let message = CommandMessage::CommandSubmit(Box::new(submit));
        self.transport.send(&message)?;
        self.projection.apply_message(&message);
        Ok(command_id)
    }

    /// Send a `CommandCancel` for a command.
    ///
    /// # Errors
    /// Returns the transport error if the cancel frame could not be sent.
    pub fn cancel(&mut self, cancel: CommandCancel) -> Result<(), T::Error> {
        let message = CommandMessage::CommandCancel(cancel);
        self.transport.send(&message)?;
        self.projection.apply_message(&message);
        Ok(())
    }

    /// Fold an incoming command-plane message (events / projection).
    pub fn ingest_command(&mut self, message: &CommandMessage) -> CommandApplyStatus {
        self.projection.apply_message(message)
    }

    /// Fold an incoming causal receipt (terminal authority).
    pub fn ingest_receipt(&mut self, receipt: &CausalReceipt) -> CommandApplyStatus {
        self.projection.observe_receipt(receipt)
    }

    /// Poll a unary `call`: `Pending` until a terminal causal receipt folds in.
    /// A transport ACK or `accepted`/`queued` event never resolves the call.
    #[must_use]
    pub fn poll_call(&self, command_id: &str) -> CallState {
        if self.projection.has_conflict(command_id) {
            return CallState::Conflict;
        }
        match self.projection.terminal_for(command_id) {
            Some(entry) => CallState::Resolved(entry.clone()),
            None => CallState::Pending,
        }
    }
}

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

    fn submit_fixture(command_id: &str, generation: u64) -> CommandSubmit {
        CommandSubmit {
            command_id: command_id.to_string(),
            causation_id: command_id.to_string(),
            source: "vscode-plugin".to_string(),
            target: "project-controller".to_string(),
            namespace: "agent-doc".to_string(),
            name: "editor_route".to_string(),
            authority_generation: generation,
            idempotency_key: "project-root:plan.md:run".to_string(),
            deadline_ms: 120_000,
            policy: CommandPolicy {
                dedupe: DedupePolicy::SameIdempotencyKey,
                supersede: false,
                cancel_on_preempt: true,
            },
            payload_type: "agent-doc.editor_route.v1".to_string(),
            payload_hash: "sha256:deadbeef".to_string(),
            payload: IpcValue::Inline(vec![1, 2, 3]),
            required_features: vec!["causal-receipts".to_string()],
        }
    }

    #[test]
    fn command_status_terminality_is_explicit() {
        assert!(!CommandStatus::Submitted.is_terminal());
        assert!(!CommandStatus::Accepted.is_terminal());
        assert!(!CommandStatus::Running.is_terminal());
        assert!(CommandStatus::Applied.is_terminal());
        assert!(CommandStatus::Cancelled.is_terminal());
        assert!(CommandStatus::TimedOut.is_terminal());
    }

    #[test]
    fn accepted_progress_is_not_terminal() {
        let mut p = CommandProjection::new();
        p.submit(&submit_fixture("cmd-1", 42));
        p.event(&CommandEvent {
            event_id: "ev-1".into(),
            command_id: "cmd-1".into(),
            kind: CommandEventKind::Accepted,
            generation: 42,
            detail: Some("queued".into()),
        });
        let entry = p.entry("cmd-1").unwrap();
        assert!(!entry.terminal);
        assert_eq!(entry.status, CommandStatus::Accepted);
        assert!(p.terminal_for("cmd-1").is_none());
    }

    #[test]
    fn applied_receipt_makes_command_terminal() {
        let mut p = CommandProjection::new();
        p.submit(&submit_fixture("cmd-1", 42));
        assert_eq!(
            p.observe_receipt(&applied_receipt(
                "rcpt-1",
                "cmd-1",
                "project-controller",
                42
            )),
            CommandApplyStatus::Recorded
        );
        let entry = p.terminal_for("cmd-1").unwrap();
        assert_eq!(entry.status, CommandStatus::Applied);
        assert_eq!(entry.terminal_receipt_id.as_deref(), Some("rcpt-1"));
    }

    #[test]
    fn stale_generation_receipt_is_ignored() {
        let mut p = CommandProjection::new();
        p.submit(&submit_fixture("cmd-1", 42));
        assert_eq!(
            p.observe_receipt(&applied_receipt(
                "rcpt-old",
                "cmd-1",
                "project-controller",
                41
            )),
            CommandApplyStatus::StaleGeneration {
                expected: 42,
                actual: 41
            }
        );
        assert!(p.terminal_for("cmd-1").is_none());
    }

    #[test]
    fn duplicate_submit_is_idempotent() {
        let mut p = CommandProjection::new();
        assert_eq!(
            p.submit(&submit_fixture("cmd-1", 42)),
            CommandApplyStatus::Recorded
        );
        assert_eq!(
            p.submit(&submit_fixture("cmd-1", 99)),
            CommandApplyStatus::Duplicate
        );
        assert_eq!(p.entry("cmd-1").unwrap().generation, 42);
    }

    #[test]
    fn cancel_before_terminal_then_rejected_receipt_is_cancelled() {
        let mut p = CommandProjection::new();
        p.submit(&submit_fixture("cmd-1", 42));
        p.event(&CommandEvent {
            event_id: "ev-1".into(),
            command_id: "cmd-1".into(),
            kind: CommandEventKind::Accepted,
            generation: 42,
            detail: None,
        });
        p.cancel(&CommandCancel {
            command_id: "cmd-1".into(),
            causation_id: "cancel-1".into(),
            source: "vscode-plugin".into(),
            authority_generation: 42,
            reason: Some("operator cleared run".into()),
        });
        p.observe_receipt(&rejected_receipt(
            "rcpt-cancel",
            "cmd-1",
            "project-controller",
            42,
            "cancelled",
        ));
        let entry = p.terminal_for("cmd-1").unwrap();
        assert_eq!(entry.status, CommandStatus::Cancelled);
        assert_eq!(entry.reason.as_deref(), Some("cancelled"));
    }

    #[test]
    fn cancel_after_applied_does_not_override() {
        let mut p = CommandProjection::new();
        p.submit(&submit_fixture("cmd-1", 42));
        p.observe_receipt(&applied_receipt(
            "rcpt-applied",
            "cmd-1",
            "project-controller",
            42,
        ));
        p.cancel(&CommandCancel {
            command_id: "cmd-1".into(),
            causation_id: "cancel-late".into(),
            source: "vscode-plugin".into(),
            authority_generation: 42,
            reason: Some("too late".into()),
        });
        assert_eq!(
            p.terminal_for("cmd-1").unwrap().status,
            CommandStatus::Applied
        );
    }

    #[test]
    fn conflicting_terminal_receipts_fail_closed() {
        let mut p = CommandProjection::new();
        p.submit(&submit_fixture("cmd-1", 42));
        p.observe_receipt(&applied_receipt(
            "rcpt-applied",
            "cmd-1",
            "project-controller",
            42,
        ));
        let status = p.observe_receipt(&rejected_receipt(
            "rcpt-rejected",
            "cmd-1",
            "project-controller",
            42,
            "conflicting terminal",
        ));
        assert!(matches!(
            status,
            CommandApplyStatus::TerminalConflict { .. }
        ));
        assert!(p.has_conflict("cmd-1"));
        // The applied outcome is not overwritten by winner selection.
        assert_eq!(p.entry("cmd-1").unwrap().status, CommandStatus::Applied);
    }

    #[test]
    fn reconnect_projection_is_fold_equivalent() {
        let mut source = CommandProjection::new();
        source.submit(&submit_fixture("cmd-1", 43));
        source.observe_receipt(&applied_receipt(
            "rcpt-1",
            "cmd-1",
            "project-controller",
            43,
        ));
        let image = source.to_image();

        let mut reconnected = CommandProjection::new();
        reconnected.apply_projection(&image);
        assert_eq!(reconnected.to_image(), image);
        assert_eq!(
            reconnected.terminal_for("cmd-1").unwrap().status,
            CommandStatus::Applied
        );
    }

    struct VecTransport {
        sent: Vec<CommandMessage>,
    }

    impl CommandTransport for VecTransport {
        type Error = ();
        fn send(&mut self, message: &CommandMessage) -> Result<(), ()> {
            self.sent.push(message.clone());
            Ok(())
        }
    }

    #[test]
    fn rpc_call_resolves_only_on_terminal_receipt() {
        let mut client = CommandRpcClient::new(VecTransport { sent: Vec::new() });
        let id = client.submit(submit_fixture("cmd-1", 42)).unwrap();

        // accepted / started progress must NOT resolve the call.
        client.ingest_command(&CommandMessage::CommandEvents(CommandEvents {
            events: vec![
                CommandEvent {
                    event_id: "ev-1".into(),
                    command_id: id.clone(),
                    kind: CommandEventKind::Accepted,
                    generation: 42,
                    detail: Some("queued".into()),
                },
                CommandEvent {
                    event_id: "ev-2".into(),
                    command_id: id.clone(),
                    kind: CommandEventKind::Started,
                    generation: 42,
                    detail: None,
                },
            ],
        }));
        assert_eq!(client.poll_call(&id), CallState::Pending);

        // The terminal receipt resolves it.
        client.ingest_receipt(&applied_receipt("rcpt-1", &id, "project-controller", 42));
        match client.poll_call(&id) {
            CallState::Resolved(entry) => assert_eq!(entry.status, CommandStatus::Applied),
            other => panic!("expected Resolved, got {other:?}"),
        }
    }
}