agnt5-sdk-core 0.2.0

High-performance Rust core for AGNT5 SDK
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
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
use crate::error::{Result, SdkError};
use crate::pb::{
    engine_service_client::EngineServiceClient,
    execution_engine_service_client::ExecutionEngineServiceClient,
    worker_coordinator_service_client::WorkerCoordinatorServiceClient, AppendBatchRequest,
    AppendBatchResponse, AppendRequest, BeginActivationRequest, BeginActivationResponse,
    CheckpointRequest, CheckpointType, CompleteActivationRequest, CompleteActivationResponse,
    CompleteJobRequest, CompleteJobResponse, DurableStepCheckpoint, EventStreamMessage,
    FailActivationRequest, FailActivationResponse, FindByStepKeyRequest, GetEntityStateRequest,
    GetEntityStateResponse, PollJobRequest, PollJobResponse, PutEntityStateRequest,
    PutEntityStateResponse, Record, RegisterService, RegisterWorkerSessionRequest,
    RegisterWorkerSessionResponse, RenewJobLeaseRequest, RenewJobLeaseResponse,
    ReportWorkerCapacityRequest, ReportWorkerCapacityResponse, RuntimeMessage, ServiceMessage,
    SuspendActivationRequest, SuspendActivationResponse,
};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::Duration;
use tonic::transport::Channel;
use tonic::Code;
use tracing::{debug, error};

pub const DURABLE_ACTIVATION_V1_CAPABILITY: &str = "durable_activation_v1";
pub const DURABLE_SUSPENSION_V1_CAPABILITY: &str = "durable_suspension_v1";

pub fn worker_protocol_capabilities() -> (Vec<String>, Vec<String>) {
    match std::env::var("AGNT5_DURABLE_ACTIVATION_MODE")
        .unwrap_or_else(|_| "preferred".to_string())
        .to_ascii_lowercase()
        .as_str()
    {
        "disabled" => (Vec::new(), Vec::new()),
        "required" => (
            vec![
                DURABLE_ACTIVATION_V1_CAPABILITY.to_string(),
                DURABLE_SUSPENSION_V1_CAPABILITY.to_string(),
            ],
            vec![DURABLE_ACTIVATION_V1_CAPABILITY.to_string()],
        ),
        _ => (
            vec![
                DURABLE_ACTIVATION_V1_CAPABILITY.to_string(),
                DURABLE_SUSPENSION_V1_CAPABILITY.to_string(),
            ],
            Vec::new(),
        ),
    }
}

pub(crate) fn validate_protocol_capabilities(
    worker_supported: &[String],
    worker_required: &[String],
    runtime_supported: &[String],
    runtime_required: &[String],
) -> Result<()> {
    if let Some(required) = worker_required
        .iter()
        .find(|required| !runtime_supported.contains(required))
    {
        return Err(SdkError::Activation {
            message: format!("runtime did not negotiate required protocol capability: {required}"),
            code: crate::error::ErrorCode::DurabilityUnavailable,
            activation_id: None,
            attempt: None,
        });
    }
    if let Some(required) = runtime_required
        .iter()
        .find(|required| !worker_supported.contains(required))
    {
        return Err(SdkError::Activation {
            message: format!("runtime requires unsupported worker protocol capability: {required}"),
            code: crate::error::ErrorCode::DurabilityUnavailable,
            activation_id: None,
            attempt: None,
        });
    }
    if worker_supported
        .iter()
        .any(|capability| capability == DURABLE_ACTIVATION_V1_CAPABILITY)
        && !worker_required
            .iter()
            .any(|capability| capability == DURABLE_ACTIVATION_V1_CAPABILITY)
        && !runtime_supported
            .iter()
            .any(|capability| capability == DURABLE_ACTIVATION_V1_CAPABILITY)
    {
        eprintln!(
            "[WARN] agnt5 durable activation degraded: runtime did not advertise durable_activation_v1; legacy checkpoints remain enabled"
        );
    }
    Ok(())
}

fn negotiated_protocol_capabilities(
    worker_supported: &[String],
    runtime_supported: &[String],
) -> Vec<String> {
    worker_supported
        .iter()
        .filter(|capability| runtime_supported.contains(capability))
        .fold(Vec::new(), |mut negotiated, capability| {
            if !negotiated.contains(capability) {
                negotiated.push(capability.clone());
            }
            negotiated
        })
}

fn activation_status(operation: &str, status: tonic::Status) -> SdkError {
    crate::runtime_adapter::activation_status_error(operation, status)
}

/// Simple client for communicating with the Worker Coordinator service.
///
/// Holds two gRPC clients multiplexed over the same `tonic::Channel`:
/// - `client`: WorkerCoordinatorService (worker registration, dispatch streaming)
/// - `engine_client`: EngineService (durable execution: checkpoint, event stream,
///   parked job polling/complete, memoization lookup via find_by_step_key)
///
/// The durable execution RPCs used to live on WorkerCoordinatorService and moved
/// to EngineService as part of the journal-owner consolidation. Both clients
/// share one HTTP/2 connection since `tonic::Channel` is cheap to clone and
/// multiplexes streams.
#[derive(Debug, Clone)]
pub struct WorkerCoordinatorClient {
    client: WorkerCoordinatorServiceClient<Channel>,
    engine_client: EngineServiceClient<Channel>,
    negotiated_protocol_capabilities: Arc<RwLock<Vec<String>>>,
}

const WORKER_COORDINATOR_RPC_TIMEOUT: Duration = Duration::from_secs(45);
const PARKED_POLL_CLIENT_GRACE: Duration = Duration::from_secs(5);
const MAX_PARKED_POLL_WAIT_MS: i64 = 30_000;

fn poll_job_deadline(req: &PollJobRequest) -> Duration {
    let wait_ms = req.wait_ms.clamp(0, MAX_PARKED_POLL_WAIT_MS) as u64;
    Duration::from_millis(wait_ms).saturating_add(PARKED_POLL_CLIENT_GRACE)
}

fn is_idle_poll_timeout(status: &tonic::Status) -> bool {
    matches!(status.code(), Code::Cancelled | Code::DeadlineExceeded)
        && status.message().to_ascii_lowercase().contains("timeout")
}

impl WorkerCoordinatorClient {
    /// Create a new client connected to the Worker Coordinator
    pub async fn connect(endpoint: String) -> Result<Self> {
        debug!("Connecting to Worker Coordinator at {}", endpoint);

        let channel = Channel::from_shared(endpoint.clone())
            .map_err(|e| SdkError::Connection {
                message: format!("Invalid endpoint {}: {}", endpoint, e),
                code: crate::error::ErrorCode::ConnectionFailed,
                source: None,
            })?
            .connect_timeout(Duration::from_secs(10))
            .timeout(WORKER_COORDINATOR_RPC_TIMEOUT)
            .http2_adaptive_window(true)
            .connect()
            .await
            .map_err(|e| {
                // Expected during reconnection — debug level to avoid noisy logs
                debug!("Connection to {} failed: {:?}", endpoint, e);
                e
            })?;

        let client = WorkerCoordinatorServiceClient::new(channel.clone());
        let engine_client = EngineServiceClient::new(channel);

        Ok(Self {
            client,
            engine_client,
            negotiated_protocol_capabilities: Arc::new(RwLock::new(Vec::new())),
        })
    }

    pub(crate) fn retain_negotiated_protocol_capabilities(
        &self,
        worker_supported: &[String],
        runtime_supported: &[String],
    ) {
        let negotiated = negotiated_protocol_capabilities(worker_supported, runtime_supported);
        let mut capabilities = self
            .negotiated_protocol_capabilities
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        *capabilities = negotiated;
    }

    /// Return whether this worker session actually negotiated a protocol
    /// capability with the runtime. Clones share the same session state.
    pub fn negotiated_protocol_capability(&self, capability: &str) -> bool {
        self.negotiated_protocol_capabilities
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .iter()
            .any(|negotiated| negotiated == capability)
    }

    /// Create a worker stream with immediate registration (based on working pattern)
    pub async fn create_worker_stream_with_registration(
        &mut self,
        worker_id: String,
        registration: RegisterService,
    ) -> Result<(
        flume::Sender<ServiceMessage>,
        flume::Receiver<RuntimeMessage>,
    )> {
        let worker_supported_protocols = registration.supported_protocol_capabilities.clone();
        let worker_required_protocols = registration.required_protocol_capabilities.clone();
        // Create the registration message first
        let registration_message = ServiceMessage {
            worker_id: worker_id.clone(),
            metadata: HashMap::new(),
            message_type: Some(crate::pb::service_message::MessageType::RegisterService(
                registration,
            )),
        };

        // Create bounded channels for ongoing communication (reasonable default capacity)
        let (outgoing_tx, outgoing_rx) = flume::bounded::<ServiceMessage>(1000);
        let (runtime_msg_tx, runtime_msg_rx) = flume::bounded::<RuntimeMessage>(1000);

        // Create stream that yields registration immediately, then handles ongoing messages
        let outgoing_stream = async_stream::stream! {
            // First, yield the registration message immediately
            yield registration_message;

            // Then, handle ongoing messages from the channel
            loop {
                match outgoing_rx.recv_async().await {
                    Ok(msg) => {
                        yield msg;
                    },
                    Err(_) => {
                        break;
                    }
                }
            }
        };

        // Expose the worker ID as gRPC metadata so L7 proxies can route
        // reconnects consistently before reading the protobuf stream body.
        let worker_id_header = tonic::metadata::MetadataValue::try_from(worker_id.as_str())
            .map_err(|e| SdkError::Connection {
                message: format!("Invalid worker_id for routing metadata: {}", e),
                code: crate::error::ErrorCode::ConnectionFailed,
                source: None,
            })?;
        let mut request = tonic::Request::new(outgoing_stream);
        request
            .metadata_mut()
            .insert("x-agnt5-worker-id", worker_id_header);

        // Establish the gRPC stream
        let mut response_stream = self
            .client
            .worker_stream(request)
            .await
            .map_err(|e| {
                debug!("Failed to create gRPC worker stream: {}", e);
                SdkError::Connection {
                    message: format!("gRPC stream failed: {}", e),
                    code: crate::error::ErrorCode::ConnectionFailed,
                    source: None,
                }
            })?
            .into_inner();

        let registration_response =
            tokio::time::timeout(Duration::from_secs(10), response_stream.message())
                .await
                .map_err(|_| {
                    error!("Timeout waiting for registration response");
                    SdkError::Connection {
                        message: "Registration timeout - no response from runtime".to_string(),
                        code: crate::error::ErrorCode::ConnectionTimeout,
                        source: None,
                    }
                })?
                .map_err(|e| {
                    debug!("Failed to receive registration response: {}", e);
                    SdkError::Connection {
                        message: format!("Stream error: {}", e),
                        code: crate::error::ErrorCode::ConnectionFailed,
                        source: None,
                    }
                })?;

        // Process registration response. Note: the runtime no longer
        // emits redirect NACKs (any serving coordinator accepts any
        // worker), so we only need to handle ack=true and outright
        // failures here.
        if let Some(runtime_message) = registration_response {
            match &runtime_message.message_data {
                Some(crate::pb::runtime_message::MessageData::RegisterServiceResponse(resp)) => {
                    if !resp.ack {
                        error!("Registration failed: {}", resp.error);
                        return Err(SdkError::Connection {
                            message: format!("Registration failed: {}", resp.error),
                            code: crate::error::ErrorCode::ConnectionFailed,
                            source: None,
                        });
                    }
                    validate_protocol_capabilities(
                        &worker_supported_protocols,
                        &worker_required_protocols,
                        &resp.supported_protocol_capabilities,
                        &resp.required_protocol_capabilities,
                    )?;
                    self.retain_negotiated_protocol_capabilities(
                        &worker_supported_protocols,
                        &resp.supported_protocol_capabilities,
                    );
                }
                _ => {
                    error!("Unexpected response type to registration");
                    return Err(SdkError::Connection {
                        message: "Unexpected response to registration".to_string(),
                        code: crate::error::ErrorCode::InvalidMessage,
                        source: None,
                    });
                }
            }
        } else {
            error!("No registration response received");
            return Err(SdkError::Connection {
                message: "No registration response received".to_string(),
                code: crate::error::ErrorCode::ConnectionFailed,
                source: None,
            });
        }

        // Spawn simple task to forward stream messages to runtime channel
        tokio::spawn(async move {
            while let Some(message_result) =
                tokio_stream::StreamExt::next(&mut response_stream).await
            {
                match message_result {
                    Ok(runtime_message) => {
                        if runtime_msg_tx.send_async(runtime_message).await.is_err() {
                            break;
                        }
                    }
                    Err(e) => {
                        debug!("Stream closed: {}", e);
                        break;
                    }
                }
            }
        });

        Ok((outgoing_tx, runtime_msg_rx))
    }

    /// Open an EventStream for sending ephemeral events (SSE-only: tokens, progress, logs, spans).
    ///
    /// Returns a sender for EventStreamMessage. Events sent through this channel are published
    /// to Centrifuge/Redis for real-time SSE delivery without journal persistence.
    /// Drop the sender to close the stream.
    pub async fn create_event_stream(
        &mut self,
        worker_id: String,
    ) -> Result<flume::Sender<EventStreamMessage>> {
        let (tx, rx) = flume::bounded::<EventStreamMessage>(1000);

        let stream = async_stream::stream! {
            loop {
                match rx.recv_async().await {
                    Ok(msg) => yield msg,
                    Err(_) => break, // Sender dropped, close stream
                }
            }
        };

        // EventStream now lives on EngineService (moved from WorkerCoordinatorService).
        let mut client = self.engine_client.clone();
        tokio::spawn(async move {
            match client.event_stream(stream).await {
                Ok(response) => {
                    let ack = response.into_inner();
                    debug!(
                        "EventStream closed: success={} events_received={}",
                        ack.success, ack.events_received
                    );
                }
                Err(e) => {
                    debug!("EventStream error: {}", e);
                }
            }
        });

        debug!("EventStream opened for worker {}", worker_id);
        Ok(tx)
    }

    /// Send a step checkpoint and check for memoized result
    ///
    /// This method sends a checkpoint for a workflow step and checks if the step
    /// result is already memoized. If memoized, returns the cached output.
    ///
    /// # Arguments
    ///
    /// * `tenant_id` - Tenant the run belongs to (required for engine lookups)
    /// * `run_id` - The workflow run ID
    /// * `step_key` - Unique key for this step (e.g., "step:greet:0")
    /// * `step_name` - Human-readable step name
    /// * `step_type` - Type of step (e.g., "function", "activity", "llm_call")
    /// * `checkpoint_type` - Type of checkpoint (started, completed, failed)
    /// * `payload` - Checkpoint payload (input for started, output for completed)
    /// * `error_message` - Error message (for failed checkpoints)
    /// * `error_type` - Error type (for failed checkpoints)
    /// * `latency_ms` - Step execution latency in milliseconds
    ///
    /// # Returns
    ///
    /// `CheckpointResult` containing memoization status and cached output if available
    pub async fn checkpoint(
        &mut self,
        tenant_id: String,
        run_id: String,
        step_key: String,
        step_name: String,
        step_type: String,
        checkpoint_type: CheckpointType,
        payload: Option<Vec<u8>>,
        error_message: Option<String>,
        error_type: Option<String>,
        latency_ms: Option<i64>,
    ) -> Result<CheckpointResult> {
        debug!(
            "Sending checkpoint: tenant_id={}, run_id={}, step_key={}, type={:?}",
            tenant_id, run_id, step_key, checkpoint_type
        );

        let checkpoint = DurableStepCheckpoint {
            run_id,
            step_key,
            step_name,
            step_type,
            r#type: checkpoint_type.into(),
            payload: payload.unwrap_or_default(),
            error_message: error_message.unwrap_or_default(),
            error_type: error_type.unwrap_or_default(),
            tokens_in: 0,
            tokens_out: 0,
            cost_usd: 0.0,
            latency_ms: latency_ms.unwrap_or(0),
            model_provider: String::new(),
            model_version: String::new(),
        };

        let request = CheckpointRequest {
            checkpoint: Some(checkpoint),
            project_id: tenant_id,
        };

        // Checkpoint moved from WorkerCoordinatorService → EngineService.
        let response = self
            .engine_client
            .checkpoint(request)
            .await
            .map_err(|e| {
                debug!("Checkpoint RPC failed: {}", e);
                SdkError::Connection {
                    message: format!("Checkpoint failed: {}", e),
                    code: crate::error::ErrorCode::ConnectionFailed,
                    source: None,
                }
            })?
            .into_inner();

        Ok(CheckpointResult {
            success: response.success,
            error_message: if response.error_message.is_empty() {
                None
            } else {
                Some(response.error_message)
            },
            memoized: response.memoized,
            cached_output: if response.cached_output.is_empty() {
                None
            } else {
                Some(response.cached_output)
            },
        })
    }

    /// Check if a step result is memoized without sending a full checkpoint.
    ///
    /// Uses `EngineService.FindByStepKey` as the canonical memoization lookup
    /// (replaces the legacy `WorkerCoordinatorService.GetMemoizedStep` RPC,
    /// which has been removed).
    ///
    /// # Arguments
    ///
    /// * `tenant_id` - The tenant that owns the run (required by the engine's
    ///   `(tenant_id, run_id)` cache key).
    /// * `run_id` - The workflow run ID.
    /// * `step_key` - Unique key for this step.
    ///
    /// # Returns
    ///
    /// `Some(output)` if the step is memoized, `None` otherwise. Returns the
    /// record's `data` field (the completed step's journal payload).
    pub async fn get_memoized_step(
        &mut self,
        tenant_id: String,
        run_id: String,
        step_key: String,
    ) -> Result<Option<Vec<u8>>> {
        debug!(
            "Checking memoization: tenant_id={}, run_id={}, step_key={}",
            tenant_id, run_id, step_key
        );

        let request = FindByStepKeyRequest {
            project_id: tenant_id,
            run_id,
            step_key,
        };

        let response = self
            .engine_client
            .find_by_step_key(request)
            .await
            .map_err(|e| {
                debug!("FindByStepKey RPC failed: {}", e);
                SdkError::Connection {
                    message: format!("FindByStepKey failed: {}", e),
                    code: crate::error::ErrorCode::ConnectionFailed,
                    source: None,
                }
            })?
            .into_inner();

        if response.found {
            if let Some(record) = response.record {
                if !record.data.is_empty() {
                    return Ok(Some(record.data));
                }
            }
        }
        Ok(None)
    }

    /// Register a parked-poll worker session with the Engine.
    pub async fn register_worker_session(
        &mut self,
        req: RegisterWorkerSessionRequest,
    ) -> Result<RegisterWorkerSessionResponse> {
        let response = self
            .engine_client
            .register_worker_session(req)
            .await
            .map_err(|e| {
                debug!("RegisterWorkerSession RPC failed: {}", e);
                SdkError::Connection {
                    message: format!("RegisterWorkerSession failed: {}", e),
                    code: crate::error::ErrorCode::ConnectionFailed,
                    source: None,
                }
            })?
            .into_inner();

        Ok(response)
    }

    /// Park one worker slot until a job is available or the Engine times out.
    pub async fn poll_job(&mut self, req: PollJobRequest) -> Result<PollJobResponse> {
        let timeout = poll_job_deadline(&req);
        let mut request = tonic::Request::new(req);
        request.set_timeout(timeout);
        let response = match self.engine_client.poll_job(request).await {
            Ok(response) => response,
            Err(e) => {
                if is_idle_poll_timeout(&e) {
                    debug!("PollJob idle timeout: {}", e);
                    return Ok(PollJobResponse { job: None });
                }
                debug!("PollJob RPC failed: {}", e);
                return Err(SdkError::Connection {
                    message: format!("PollJob failed: {}", e),
                    code: crate::error::ErrorCode::ConnectionFailed,
                    source: None,
                });
            }
        }
        .into_inner();

        Ok(response)
    }

    /// Read entity state through the same unary Engine service used by parked
    /// polling. This is request-scoped and does not require a worker stream.
    pub async fn get_entity_state(
        &mut self,
        req: GetEntityStateRequest,
    ) -> Result<GetEntityStateResponse> {
        self.engine_client
            .get_entity_state(req)
            .await
            .map(|response| response.into_inner())
            .map_err(|error| SdkError::Connection {
                message: format!("GetEntityState failed: {error}"),
                code: crate::error::ErrorCode::ConnectionFailed,
                source: None,
            })
    }

    /// Persist entity state through a unary, lease-fenced Engine request.
    pub async fn put_entity_state(
        &mut self,
        req: PutEntityStateRequest,
    ) -> Result<PutEntityStateResponse> {
        self.engine_client
            .put_entity_state(req)
            .await
            .map(|response| response.into_inner())
            .map_err(|error| SdkError::Connection {
                message: format!("PutEntityState failed: {error}"),
                code: crate::error::ErrorCode::ConnectionFailed,
                source: None,
            })
    }

    /// Renew an active push or pull execution lease.
    pub async fn renew_job_lease(
        &mut self,
        req: RenewJobLeaseRequest,
    ) -> Result<RenewJobLeaseResponse> {
        let response = self
            .engine_client
            .renew_job_lease(req)
            .await
            .map_err(|e| {
                debug!("RenewJobLease RPC failed: {}", e);
                SdkError::Connection {
                    message: format!("RenewJobLease failed: {}", e),
                    code: crate::error::ErrorCode::ConnectionFailed,
                    source: None,
                }
            })?
            .into_inner();

        Ok(response)
    }

    /// Report current parked-poll capacity and active slot usage.
    pub async fn report_worker_capacity(
        &mut self,
        req: ReportWorkerCapacityRequest,
    ) -> Result<ReportWorkerCapacityResponse> {
        let response = self
            .engine_client
            .report_worker_capacity(req)
            .await
            .map_err(|e| {
                debug!("ReportWorkerCapacity RPC failed: {}", e);
                SdkError::Connection {
                    message: format!("ReportWorkerCapacity failed: {}", e),
                    code: crate::error::ErrorCode::ConnectionFailed,
                    source: None,
                }
            })?
            .into_inner();

        Ok(response)
    }

    /// Report the result of a polled job back to the engine.
    /// Updates job_queue, run status, journal, and batch counters.
    ///
    /// CompleteJob now lives on EngineService (moved from WorkerCoordinatorService).
    pub async fn complete_job(&mut self, req: CompleteJobRequest) -> Result<CompleteJobResponse> {
        let response = self
            .engine_client
            .complete_job(req)
            .await
            .map_err(|e| {
                error!("CompleteJob RPC failed: {}", e);
                SdkError::Connection {
                    message: format!("CompleteJob failed: {}", e),
                    code: crate::error::ErrorCode::ConnectionFailed,
                    source: None,
                }
            })?
            .into_inner();

        Ok(response)
    }

    /// Suspend a polled job through the Engine RPC. Pull workers do not keep a
    /// bidirectional dispatch stream, so they cannot return this typed result
    /// through WorkerStream like push workers do.
    pub async fn suspend_activation(
        &mut self,
        req: SuspendActivationRequest,
    ) -> Result<SuspendActivationResponse> {
        self.engine_client
            .suspend_activation(req)
            .await
            .map(|response| response.into_inner())
            .map_err(|status| activation_status("SuspendActivation", status))
    }
}

/// Open an EventStream on the Execution Engine for sending ephemeral events (SSE-only).
///
/// Same pattern as WC's create_event_stream but routes to EE, which is the single
/// SSE publisher. Drop the sender to close the stream.
pub async fn create_ee_event_stream(
    ee_client: &mut ExecutionEngineServiceClient<Channel>,
    worker_id: String,
) -> Result<flume::Sender<EventStreamMessage>> {
    let (tx, rx) = flume::bounded::<EventStreamMessage>(1000);

    let stream = async_stream::stream! {
        loop {
            match rx.recv_async().await {
                Ok(msg) => yield msg,
                Err(_) => break, // Sender dropped, close stream
            }
        }
    };

    let mut client = ee_client.clone();
    tokio::spawn(async move {
        match client.event_stream(stream).await {
            Ok(response) => {
                let ack = response.into_inner();
                debug!(
                    "EE EventStream closed: success={} events_received={}",
                    ack.success, ack.events_received
                );
            }
            Err(e) => {
                debug!("EE EventStream error: {}", e);
            }
        }
    });

    debug!("EE EventStream opened for worker {}", worker_id);
    Ok(tx)
}

// =============================================================================
// Engine Client — routes events to the AGNT5 Rust engine (Append/AppendBatch)
// =============================================================================

/// Pool size for engine gRPC connections.
/// Each connection is an independent h2 session, distributing load to avoid
/// the h2 PoisonError that occurs when 100+ concurrent requests share one connection.
const ENGINE_POOL_SIZE: usize = 8;
const ENGINE_RPC_RETRY_ATTEMPTS: usize = 20;
const ENGINE_ACTIVATION_RPC_ATTEMPTS: usize = 6;
const ENGINE_RPC_RETRY_DELAY: Duration = Duration::from_millis(100);

fn is_retryable_engine_status(status: &tonic::Status) -> bool {
    let message = status.message().to_ascii_lowercase();
    matches!(
        status.code(),
        tonic::Code::Unavailable
            | tonic::Code::DeadlineExceeded
            | tonic::Code::Cancelled
            | tonic::Code::Unknown
    ) || is_retryable_engine_message(&message)
}

fn is_retryable_engine_message(message: &str) -> bool {
    message.contains("upstream connect error")
        || message.contains("disconnect/reset before headers")
        || message.contains("connection termination")
        || message.contains("connection refused")
        || message.contains("broken pipe")
        || message.contains("h2 protocol error")
        || message.contains("timeout expired")
        || message.contains("no sequencer available")
        || message.contains("this node is not the sequencer")
        || message.contains("stale epoch")
        || message.contains("future epoch")
        || message.contains("epoch ahead")
        || message.contains("not partition owner")
        || message.contains("partition not writable")
        || message.contains("quorum not reached")
        || message.contains("no connected peers for quorum replication")
        || message.contains("not connected for catch-up")
        || message.contains("catch-up failed")
}

fn validate_append_batch_records(records: &[Record]) -> Result<()> {
    let Some(first) = records.first() else {
        return Ok(());
    };
    if records
        .iter()
        .skip(1)
        .any(|record| record.run_id != first.run_id)
    {
        return Err(SdkError::InvalidArgument {
            message: "AppendBatch records must share one run_id so the request cannot span journal partitions".to_string(),
            argument: Some("records".to_string()),
        });
    }
    Ok(())
}

fn validate_append_batch_response(response: &AppendBatchResponse, expected: usize) -> Result<i32> {
    if response.offsets.len() != expected {
        return Err(SdkError::Internal(format!(
            "Engine AppendBatch response cardinality mismatch: expected {expected}, offsets={}",
            response.offsets.len()
        )));
    }
    if response.written_count < 0 || response.written_count as usize > expected {
        return Err(SdkError::Internal(format!(
            "Engine AppendBatch written_count out of range: expected 0..={expected}, got {}",
            response.written_count
        )));
    }
    Ok(response.written_count)
}

async fn sleep_engine_retry(attempt: usize) {
    let multiplier = (attempt + 1) as u32;
    tokio::time::sleep(ENGINE_RPC_RETRY_DELAY * multiplier).await;
}

fn should_retry_activation_status(status: &tonic::Status, attempt: usize) -> bool {
    attempt + 1 < ENGINE_ACTIVATION_RPC_ATTEMPTS
        && status.details().is_empty()
        && is_retryable_engine_status(status)
}

/// Client for communicating with the AGNT5 Engine.
///
/// Uses a pool of N independent gRPC connections with round-robin selection.
/// This prevents the h2 PoisonError that occurs when many concurrent checkpoint
/// events are routed through a single HTTP/2 connection.
#[derive(Debug, Clone)]
pub struct EngineClient {
    clients: Vec<EngineServiceClient<Channel>>,
    next: std::sync::Arc<std::sync::atomic::AtomicUsize>,
}

impl EngineClient {
    /// Connect to the engine at the given endpoint with a pool of connections.
    pub async fn connect(endpoint: &str) -> Result<Self> {
        debug!(
            "Connecting to Engine at {} (pool_size={})",
            endpoint, ENGINE_POOL_SIZE
        );

        let uri = if endpoint.contains("://") {
            endpoint.to_string()
        } else {
            format!("http://{}", endpoint)
        };

        let mut clients = Vec::with_capacity(ENGINE_POOL_SIZE);
        for i in 0..ENGINE_POOL_SIZE {
            let channel = Channel::from_shared(uri.clone())
                .map_err(|e| SdkError::Connection {
                    message: format!("Invalid engine endpoint {}: {}", endpoint, e),
                    code: crate::error::ErrorCode::ConnectionFailed,
                    source: None,
                })?
                .connect_timeout(Duration::from_secs(10))
                .timeout(Duration::from_secs(30))
                .http2_adaptive_window(true)
                .connect()
                .await
                .map_err(|e| {
                    debug!("Engine connection {} to {} failed: {:?}", i, endpoint, e);
                    SdkError::Connection {
                        message: format!("Engine connection failed: {}", e),
                        code: crate::error::ErrorCode::ConnectionFailed,
                        source: None,
                    }
                })?;
            clients.push(EngineServiceClient::new(channel));
        }

        debug!(
            "Engine client pool connected ({} connections)",
            ENGINE_POOL_SIZE
        );
        Ok(Self {
            clients,
            next: std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        })
    }

    /// Get the next client from the pool (round-robin).
    fn next_client(&mut self) -> &mut EngineServiceClient<Channel> {
        let idx = self.next.fetch_add(1, std::sync::atomic::Ordering::Relaxed) % self.clients.len();
        &mut self.clients[idx]
    }

    /// Admit one logical activation and return the journal-authoritative decision.
    pub async fn begin_activation(
        &mut self,
        request: BeginActivationRequest,
    ) -> Result<BeginActivationResponse> {
        for attempt in 0..ENGINE_ACTIVATION_RPC_ATTEMPTS {
            match self.next_client().begin_activation(request.clone()).await {
                Ok(response) => return Ok(response.into_inner()),
                Err(status) if should_retry_activation_status(&status, attempt) => {
                    debug!(
                        attempt = attempt + 1,
                        status = %status,
                        "BeginActivation hit a transient gRPC status; retrying the exact command"
                    );
                    sleep_engine_retry(attempt).await;
                }
                Err(status) => return Err(activation_status("BeginActivation", status)),
            }
        }
        unreachable!("activation retry loop always returns")
    }

    /// Commit one fenced activation completion and wait for its durability acknowledgement.
    pub async fn complete_activation(
        &mut self,
        request: CompleteActivationRequest,
    ) -> Result<CompleteActivationResponse> {
        for attempt in 0..ENGINE_ACTIVATION_RPC_ATTEMPTS {
            match self
                .next_client()
                .complete_activation(request.clone())
                .await
            {
                Ok(response) => return Ok(response.into_inner()),
                Err(status) if should_retry_activation_status(&status, attempt) => {
                    debug!(
                        attempt = attempt + 1,
                        status = %status,
                        "CompleteActivation hit a transient gRPC status; retrying the exact command"
                    );
                    sleep_engine_retry(attempt).await;
                }
                Err(status) => return Err(activation_status("CompleteActivation", status)),
            }
        }
        unreachable!("activation retry loop always returns")
    }

    /// Commit one fenced activation failure and wait for its durability acknowledgement.
    pub async fn fail_activation(
        &mut self,
        request: FailActivationRequest,
    ) -> Result<FailActivationResponse> {
        for attempt in 0..ENGINE_ACTIVATION_RPC_ATTEMPTS {
            match self.next_client().fail_activation(request.clone()).await {
                Ok(response) => return Ok(response.into_inner()),
                Err(status) if should_retry_activation_status(&status, attempt) => {
                    debug!(
                        attempt = attempt + 1,
                        status = %status,
                        "FailActivation hit a transient gRPC status; retrying the exact command"
                    );
                    sleep_engine_retry(attempt).await;
                }
                Err(status) => return Err(activation_status("FailActivation", status)),
            }
        }
        unreachable!("activation retry loop always returns")
    }

    /// Atomically park one fenced activation and its parent run until a
    /// durable timer generation is ready.
    pub async fn suspend_activation(
        &mut self,
        request: SuspendActivationRequest,
    ) -> Result<SuspendActivationResponse> {
        for attempt in 0..ENGINE_ACTIVATION_RPC_ATTEMPTS {
            match self.next_client().suspend_activation(request.clone()).await {
                Ok(response) => return Ok(response.into_inner()),
                Err(status) if should_retry_activation_status(&status, attempt) => {
                    debug!(
                        attempt = attempt + 1,
                        status = %status,
                        "SuspendActivation hit a transient gRPC status; retrying the exact command"
                    );
                    sleep_engine_retry(attempt).await;
                }
                Err(status) => return Err(activation_status("SuspendActivation", status)),
            }
        }
        unreachable!("activation retry loop always returns")
    }

    /// Append a single record to the engine.
    pub async fn append(&mut self, record: Record) -> Result<(u64, i64)> {
        for attempt in 0..ENGINE_RPC_RETRY_ATTEMPTS {
            match self
                .next_client()
                .append(AppendRequest {
                    record: Some(record.clone()),
                })
                .await
            {
                Ok(response) => {
                    let response = response.into_inner();
                    return Ok((response.offset, response.timestamp_ns));
                }
                Err(status)
                    if attempt + 1 < ENGINE_RPC_RETRY_ATTEMPTS
                        && is_retryable_engine_status(&status) =>
                {
                    debug!(
                        attempt = attempt + 1,
                        max = ENGINE_RPC_RETRY_ATTEMPTS,
                        "Engine Append hit retryable gRPC status: {}",
                        status
                    );
                    sleep_engine_retry(attempt).await;
                }
                Err(status) => {
                    debug!("Engine Append failed: {}", status);
                    return Err(SdkError::Connection {
                        message: format!("Engine Append failed: {}", status),
                        code: crate::error::ErrorCode::ConnectionFailed,
                        source: None,
                    });
                }
            }
        }

        unreachable!("engine append retry loop always returns")
    }

    /// Append a batch of records to the engine.
    pub async fn append_batch(&mut self, records: Vec<Record>) -> Result<i32> {
        validate_append_batch_records(&records)?;
        let expected = records.len();
        for attempt in 0..ENGINE_RPC_RETRY_ATTEMPTS {
            match self
                .next_client()
                .append_batch(AppendBatchRequest {
                    records: records.clone(),
                })
                .await
            {
                Ok(response) => {
                    let response = response.into_inner();
                    return validate_append_batch_response(&response, expected);
                }
                Err(status)
                    if attempt + 1 < ENGINE_RPC_RETRY_ATTEMPTS
                        && is_retryable_engine_status(&status) =>
                {
                    debug!(
                        attempt = attempt + 1,
                        max = ENGINE_RPC_RETRY_ATTEMPTS,
                        "Engine AppendBatch hit retryable gRPC status: {}",
                        status
                    );
                    sleep_engine_retry(attempt).await;
                }
                Err(status) => {
                    debug!("Engine AppendBatch failed: {}", status);
                    return Err(SdkError::Connection {
                        message: format!("Engine AppendBatch failed: {}", status),
                        code: crate::error::ErrorCode::ConnectionFailed,
                        source: None,
                    });
                }
            }
        }

        unreachable!("engine append batch retry loop always returns")
    }

    /// Publish a bounded batch of ephemeral events and wait until the runtime
    /// acknowledges every frame. Closing each batch supplies the ordering
    /// barrier needed before a durable terminal event is appended.
    pub async fn stream_events(&mut self, events: Vec<EventStreamMessage>) -> Result<i64> {
        if events.is_empty() {
            return Ok(0);
        }
        let expected = events.len() as i64;
        let response = self
            .next_client()
            .event_stream(tokio_stream::iter(events))
            .await
            .map_err(|status| SdkError::Connection {
                message: format!("Engine EventStream failed: {status}"),
                code: crate::error::ErrorCode::ConnectionFailed,
                source: None,
            })?
            .into_inner();
        if !response.success || response.events_received != expected {
            return Err(SdkError::Connection {
                message: format!(
                    "Engine EventStream acknowledged {} events, want {}",
                    response.events_received, expected
                ),
                code: crate::error::ErrorCode::ConnectionFailed,
                source: None,
            });
        }
        Ok(response.events_received)
    }
}

/// Build an engine `Record` from SDK event fields.
pub fn build_engine_record(
    tenant_id: String,
    run_id: String,
    event_type: String,
    data: Vec<u8>,
    timestamp_ns: i64,
    step_key: String,
    correlation_id: String,
    parent_event_id: String,
    metadata: HashMap<String, String>,
) -> Record {
    Record {
        offset: 0, // Assigned by engine
        project_id: tenant_id,
        run_id,
        event_type,
        data,
        timestamp_ns,
        step_key,
        correlation_id,
        parent_event_id,
        metadata,
        data_type: "json".to_string(),
        data_checksum: vec![],
        data_compressed: false,
    }
}

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

    #[test]
    fn retryable_engine_status_includes_partition_handoff_errors() {
        let status = tonic::Status::internal(
            "Engine AppendBatch failed: no sequencer available after retry",
        );
        assert!(is_retryable_engine_status(&status));

        let status = tonic::Status::failed_precondition("stale epoch on forward: 162 < 163");
        assert!(is_retryable_engine_status(&status));
    }

    #[test]
    fn retryable_engine_status_does_not_retry_plain_internal_errors() {
        let status = tonic::Status::internal("serialization failed");
        assert!(!is_retryable_engine_status(&status));
    }

    #[test]
    fn activation_rpc_retry_is_bounded_to_transient_statuses() {
        let lagging_replica =
            tonic::Status::unavailable("activation actv1_test is not yet visible on this replica");
        assert!(should_retry_activation_status(&lagging_replica, 0));
        assert!(!should_retry_activation_status(
            &lagging_replica,
            ENGINE_ACTIVATION_RPC_ATTEMPTS - 1
        ));

        let conflict = tonic::Status::already_exists("payload conflict");
        assert!(!should_retry_activation_status(&conflict, 0));

        let typed_conflict = tonic::Status::with_details(
            tonic::Code::Unavailable,
            "typed activation outcome",
            bytes::Bytes::from_static(b"activation-error-detail"),
        );
        assert!(!should_retry_activation_status(&typed_conflict, 0));
    }

    #[tokio::test]
    async fn append_batch_rejects_multi_run_before_retry_or_rpc() {
        let mut client = EngineClient {
            clients: Vec::new(),
            next: std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        };
        let records = vec![
            Record {
                run_id: "run-a".into(),
                event_type: "step.started".into(),
                ..Default::default()
            },
            Record {
                run_id: "run-b".into(),
                event_type: "step.completed".into(),
                ..Default::default()
            },
        ];

        let error = client.append_batch(records).await.unwrap_err();

        assert!(matches!(error, SdkError::InvalidArgument { .. }));
        assert_eq!(
            client.next.load(std::sync::atomic::Ordering::Relaxed),
            0,
            "preflight rejection must happen before the retry loop selects a connection"
        );
    }

    #[test]
    fn append_batch_rejects_malformed_response_counts() {
        for response in [
            AppendBatchResponse {
                offsets: vec![1],
                written_count: 1,
            },
            AppendBatchResponse {
                offsets: vec![1, 2],
                written_count: -1,
            },
            AppendBatchResponse {
                offsets: vec![1, 2],
                written_count: 3,
            },
        ] {
            let error = validate_append_batch_response(&response, 2).unwrap_err();
            assert!(matches!(error, SdkError::Internal(_)));
        }
    }

    #[test]
    fn protocol_capability_negotiation_fails_closed_when_required() {
        let durable = DURABLE_ACTIVATION_V1_CAPABILITY.to_string();
        let error = validate_protocol_capabilities(
            std::slice::from_ref(&durable),
            std::slice::from_ref(&durable),
            &[],
            &[],
        )
        .unwrap_err();
        assert_eq!(error.code(), crate::error::ErrorCode::DurabilityUnavailable);

        let error =
            validate_protocol_capabilities(&[], &[], &[], &["runtime_v2".into()]).unwrap_err();
        assert_eq!(error.code(), crate::error::ErrorCode::DurabilityUnavailable);

        validate_protocol_capabilities(
            std::slice::from_ref(&durable),
            std::slice::from_ref(&durable),
            std::slice::from_ref(&durable),
            &[],
        )
        .unwrap();
    }

    #[test]
    fn protocol_capability_negotiation_retains_only_the_intersection() {
        assert_eq!(
            negotiated_protocol_capabilities(
                &[
                    DURABLE_ACTIVATION_V1_CAPABILITY.into(),
                    "worker_only".into(),
                    DURABLE_ACTIVATION_V1_CAPABILITY.into(),
                ],
                &[
                    DURABLE_ACTIVATION_V1_CAPABILITY.into(),
                    "runtime_only".into(),
                ],
            ),
            vec![DURABLE_ACTIVATION_V1_CAPABILITY.to_string()]
        );
    }
}

/// Result of a checkpoint operation
#[derive(Debug, Clone)]
pub struct CheckpointResult {
    /// Whether the checkpoint was processed successfully
    pub success: bool,
    /// Error message if the checkpoint failed
    pub error_message: Option<String>,
    /// Whether the step was already memoized (for STEP_STARTED checkpoints)
    pub memoized: bool,
    /// Cached output if memoized
    pub cached_output: Option<Vec<u8>>,
}