@temporalio/core-bridge 1.6.0 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/Cargo.lock +520 -456
  2. package/lib/index.d.ts +8 -6
  3. package/lib/index.js.map +1 -1
  4. package/package.json +8 -3
  5. package/releases/aarch64-apple-darwin/index.node +0 -0
  6. package/releases/aarch64-unknown-linux-gnu/index.node +0 -0
  7. package/releases/x86_64-apple-darwin/index.node +0 -0
  8. package/releases/x86_64-pc-windows-msvc/index.node +0 -0
  9. package/releases/x86_64-unknown-linux-gnu/index.node +0 -0
  10. package/sdk-core/.buildkite/docker/Dockerfile +2 -2
  11. package/sdk-core/.buildkite/docker/docker-compose.yaml +1 -1
  12. package/sdk-core/.buildkite/pipeline.yml +1 -1
  13. package/sdk-core/.github/workflows/heavy.yml +1 -0
  14. package/sdk-core/README.md +13 -7
  15. package/sdk-core/client/src/lib.rs +27 -9
  16. package/sdk-core/client/src/metrics.rs +17 -8
  17. package/sdk-core/client/src/raw.rs +3 -3
  18. package/sdk-core/core/Cargo.toml +3 -4
  19. package/sdk-core/core/src/abstractions/take_cell.rs +28 -0
  20. package/sdk-core/core/src/abstractions.rs +197 -18
  21. package/sdk-core/core/src/core_tests/activity_tasks.rs +137 -45
  22. package/sdk-core/core/src/core_tests/child_workflows.rs +6 -5
  23. package/sdk-core/core/src/core_tests/determinism.rs +212 -2
  24. package/sdk-core/core/src/core_tests/local_activities.rs +183 -36
  25. package/sdk-core/core/src/core_tests/queries.rs +32 -14
  26. package/sdk-core/core/src/core_tests/workers.rs +8 -5
  27. package/sdk-core/core/src/core_tests/workflow_tasks.rs +340 -51
  28. package/sdk-core/core/src/ephemeral_server/mod.rs +110 -8
  29. package/sdk-core/core/src/internal_flags.rs +141 -0
  30. package/sdk-core/core/src/lib.rs +14 -9
  31. package/sdk-core/core/src/replay/mod.rs +16 -27
  32. package/sdk-core/core/src/telemetry/metrics.rs +69 -35
  33. package/sdk-core/core/src/telemetry/mod.rs +38 -14
  34. package/sdk-core/core/src/telemetry/prometheus_server.rs +19 -13
  35. package/sdk-core/core/src/test_help/mod.rs +65 -13
  36. package/sdk-core/core/src/worker/activities/activity_heartbeat_manager.rs +119 -160
  37. package/sdk-core/core/src/worker/activities/activity_task_poller_stream.rs +89 -0
  38. package/sdk-core/core/src/worker/activities/local_activities.rs +122 -6
  39. package/sdk-core/core/src/worker/activities.rs +347 -173
  40. package/sdk-core/core/src/worker/client/mocks.rs +22 -2
  41. package/sdk-core/core/src/worker/client.rs +18 -2
  42. package/sdk-core/core/src/worker/mod.rs +137 -44
  43. package/sdk-core/core/src/worker/workflow/history_update.rs +132 -51
  44. package/sdk-core/core/src/worker/workflow/machines/activity_state_machine.rs +207 -166
  45. package/sdk-core/core/src/worker/workflow/machines/cancel_external_state_machine.rs +6 -7
  46. package/sdk-core/core/src/worker/workflow/machines/cancel_workflow_state_machine.rs +6 -7
  47. package/sdk-core/core/src/worker/workflow/machines/child_workflow_state_machine.rs +157 -82
  48. package/sdk-core/core/src/worker/workflow/machines/complete_workflow_state_machine.rs +12 -12
  49. package/sdk-core/core/src/worker/workflow/machines/continue_as_new_workflow_state_machine.rs +6 -7
  50. package/sdk-core/core/src/worker/workflow/machines/fail_workflow_state_machine.rs +13 -15
  51. package/sdk-core/core/src/worker/workflow/machines/local_activity_state_machine.rs +170 -60
  52. package/sdk-core/core/src/worker/workflow/machines/mod.rs +24 -16
  53. package/sdk-core/core/src/worker/workflow/machines/modify_workflow_properties_state_machine.rs +6 -8
  54. package/sdk-core/core/src/worker/workflow/machines/patch_state_machine.rs +320 -204
  55. package/sdk-core/core/src/worker/workflow/machines/signal_external_state_machine.rs +10 -13
  56. package/sdk-core/core/src/worker/workflow/machines/timer_state_machine.rs +15 -23
  57. package/sdk-core/core/src/worker/workflow/machines/upsert_search_attributes_state_machine.rs +187 -46
  58. package/sdk-core/core/src/worker/workflow/machines/workflow_machines.rs +237 -111
  59. package/sdk-core/core/src/worker/workflow/machines/workflow_task_state_machine.rs +13 -13
  60. package/sdk-core/core/src/worker/workflow/managed_run/managed_wf_test.rs +10 -6
  61. package/sdk-core/core/src/worker/workflow/managed_run.rs +81 -62
  62. package/sdk-core/core/src/worker/workflow/mod.rs +341 -79
  63. package/sdk-core/core/src/worker/workflow/run_cache.rs +18 -11
  64. package/sdk-core/core/src/worker/workflow/wft_extraction.rs +15 -3
  65. package/sdk-core/core/src/worker/workflow/workflow_stream/saved_wf_inputs.rs +2 -0
  66. package/sdk-core/core/src/worker/workflow/workflow_stream.rs +75 -52
  67. package/sdk-core/core-api/Cargo.toml +0 -1
  68. package/sdk-core/core-api/src/lib.rs +13 -7
  69. package/sdk-core/core-api/src/telemetry.rs +4 -6
  70. package/sdk-core/core-api/src/worker.rs +5 -0
  71. package/sdk-core/fsm/rustfsm_procmacro/src/lib.rs +80 -55
  72. package/sdk-core/fsm/rustfsm_trait/src/lib.rs +22 -68
  73. package/sdk-core/histories/ends_empty_wft_complete.bin +0 -0
  74. package/sdk-core/histories/old_change_marker_format.bin +0 -0
  75. package/sdk-core/protos/api_upstream/.github/CODEOWNERS +2 -1
  76. package/sdk-core/protos/api_upstream/Makefile +1 -1
  77. package/sdk-core/protos/api_upstream/temporal/api/command/v1/message.proto +5 -17
  78. package/sdk-core/protos/api_upstream/temporal/api/common/v1/message.proto +11 -0
  79. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/command_type.proto +1 -6
  80. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/event_type.proto +6 -6
  81. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/failed_cause.proto +5 -0
  82. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/update.proto +22 -6
  83. package/sdk-core/protos/api_upstream/temporal/api/history/v1/message.proto +48 -19
  84. package/sdk-core/protos/api_upstream/temporal/api/namespace/v1/message.proto +2 -0
  85. package/sdk-core/protos/api_upstream/temporal/api/operatorservice/v1/request_response.proto +3 -0
  86. package/sdk-core/protos/api_upstream/temporal/api/{enums/v1/interaction_type.proto → protocol/v1/message.proto} +29 -11
  87. package/sdk-core/protos/api_upstream/temporal/api/sdk/v1/task_complete_metadata.proto +63 -0
  88. package/sdk-core/protos/api_upstream/temporal/api/update/v1/message.proto +111 -0
  89. package/sdk-core/protos/api_upstream/temporal/api/workflowservice/v1/request_response.proto +59 -28
  90. package/sdk-core/protos/api_upstream/temporal/api/workflowservice/v1/service.proto +2 -2
  91. package/sdk-core/protos/local/temporal/sdk/core/activity_result/activity_result.proto +7 -8
  92. package/sdk-core/protos/local/temporal/sdk/core/activity_task/activity_task.proto +10 -7
  93. package/sdk-core/protos/local/temporal/sdk/core/child_workflow/child_workflow.proto +19 -30
  94. package/sdk-core/protos/local/temporal/sdk/core/common/common.proto +1 -0
  95. package/sdk-core/protos/local/temporal/sdk/core/core_interface.proto +1 -0
  96. package/sdk-core/protos/local/temporal/sdk/core/external_data/external_data.proto +8 -0
  97. package/sdk-core/protos/local/temporal/sdk/core/workflow_activation/workflow_activation.proto +65 -60
  98. package/sdk-core/protos/local/temporal/sdk/core/workflow_commands/workflow_commands.proto +85 -84
  99. package/sdk-core/protos/local/temporal/sdk/core/workflow_completion/workflow_completion.proto +9 -3
  100. package/sdk-core/sdk/Cargo.toml +1 -1
  101. package/sdk-core/sdk/src/lib.rs +21 -5
  102. package/sdk-core/sdk/src/workflow_context/options.rs +7 -1
  103. package/sdk-core/sdk/src/workflow_context.rs +24 -17
  104. package/sdk-core/sdk/src/workflow_future.rs +9 -3
  105. package/sdk-core/sdk-core-protos/src/history_builder.rs +114 -89
  106. package/sdk-core/sdk-core-protos/src/history_info.rs +6 -1
  107. package/sdk-core/sdk-core-protos/src/lib.rs +205 -64
  108. package/sdk-core/test-utils/src/canned_histories.rs +106 -296
  109. package/sdk-core/test-utils/src/lib.rs +32 -5
  110. package/sdk-core/tests/heavy_tests.rs +10 -43
  111. package/sdk-core/tests/integ_tests/ephemeral_server_tests.rs +25 -3
  112. package/sdk-core/tests/integ_tests/heartbeat_tests.rs +5 -3
  113. package/sdk-core/tests/integ_tests/metrics_tests.rs +218 -16
  114. package/sdk-core/tests/integ_tests/polling_tests.rs +3 -8
  115. package/sdk-core/tests/integ_tests/queries_tests.rs +4 -2
  116. package/sdk-core/tests/integ_tests/visibility_tests.rs +34 -23
  117. package/sdk-core/tests/integ_tests/workflow_tests/activities.rs +97 -81
  118. package/sdk-core/tests/integ_tests/workflow_tests/cancel_external.rs +1 -0
  119. package/sdk-core/tests/integ_tests/workflow_tests/cancel_wf.rs +1 -0
  120. package/sdk-core/tests/integ_tests/workflow_tests/child_workflows.rs +80 -3
  121. package/sdk-core/tests/integ_tests/workflow_tests/continue_as_new.rs +5 -1
  122. package/sdk-core/tests/integ_tests/workflow_tests/determinism.rs +1 -0
  123. package/sdk-core/tests/integ_tests/workflow_tests/local_activities.rs +25 -3
  124. package/sdk-core/tests/integ_tests/workflow_tests/modify_wf_properties.rs +2 -4
  125. package/sdk-core/tests/integ_tests/workflow_tests/patches.rs +30 -0
  126. package/sdk-core/tests/integ_tests/workflow_tests/replay.rs +64 -0
  127. package/sdk-core/tests/integ_tests/workflow_tests/resets.rs +1 -0
  128. package/sdk-core/tests/integ_tests/workflow_tests/signals.rs +4 -0
  129. package/sdk-core/tests/integ_tests/workflow_tests/stickyness.rs +3 -1
  130. package/sdk-core/tests/integ_tests/workflow_tests/timers.rs +7 -2
  131. package/sdk-core/tests/integ_tests/workflow_tests/upsert_search_attrs.rs +6 -7
  132. package/sdk-core/tests/integ_tests/workflow_tests.rs +8 -8
  133. package/sdk-core/tests/main.rs +16 -25
  134. package/sdk-core/tests/runner.rs +11 -9
  135. package/src/conversions.rs +14 -8
  136. package/src/runtime.rs +9 -8
  137. package/ts/index.ts +8 -6
  138. package/sdk-core/protos/api_upstream/temporal/api/interaction/v1/message.proto +0 -87
@@ -4,6 +4,10 @@ use super::{
4
4
  workflow_machines::MachineResponse, Cancellable, EventInfo, NewMachineWithCommand,
5
5
  OnEventWrapper, WFMachinesAdapter, WFMachinesError,
6
6
  };
7
+ use crate::{
8
+ internal_flags::CoreInternalFlags,
9
+ worker::workflow::{machines::HistEventData, InternalFlagsRef},
10
+ };
7
11
  use rustfsm::{fsm, MachineError, StateMachine, TransitionResult};
8
12
  use std::convert::{TryFrom, TryInto};
9
13
  use temporal_sdk_core_protos::{
@@ -16,10 +20,7 @@ use temporal_sdk_core_protos::{
16
20
  command::v1::{command, Command, RequestCancelActivityTaskCommandAttributes},
17
21
  common::v1::{ActivityType, Payload, Payloads},
18
22
  enums::v1::{CommandType, EventType, RetryState},
19
- failure::v1::{
20
- self as failure, failure::FailureInfo, ActivityFailureInfo, CanceledFailureInfo,
21
- Failure,
22
- },
23
+ failure::v1::{failure::FailureInfo, ActivityFailureInfo, CanceledFailureInfo, Failure},
23
24
  history::v1::{
24
25
  history_event, ActivityTaskCanceledEventAttributes,
25
26
  ActivityTaskCompletedEventAttributes, ActivityTaskFailedEventAttributes,
@@ -37,7 +38,7 @@ fsm! {
37
38
  Created --(Schedule, on_schedule)--> ScheduleCommandCreated;
38
39
 
39
40
  ScheduleCommandCreated --(CommandScheduleActivityTask) --> ScheduleCommandCreated;
40
- ScheduleCommandCreated --(ActivityTaskScheduled(i64),
41
+ ScheduleCommandCreated --(ActivityTaskScheduled(ActTaskScheduledData),
41
42
  shared on_activity_task_scheduled) --> ScheduledEventRecorded;
42
43
  ScheduleCommandCreated --(Cancel, shared on_canceled) --> Canceled;
43
44
 
@@ -45,7 +46,7 @@ fsm! {
45
46
  ScheduledEventRecorded --(ActivityTaskTimedOut(ActivityTaskTimedOutEventAttributes),
46
47
  shared on_task_timed_out) --> TimedOut;
47
48
  ScheduledEventRecorded --(Cancel, shared on_canceled) --> ScheduledActivityCancelCommandCreated;
48
- ScheduledEventRecorded --(Abandon, shared on_abandoned) --> Canceled;
49
+ ScheduledEventRecorded --(Abandon, on_abandoned) --> Canceled;
49
50
 
50
51
  Started --(ActivityTaskCompleted(ActivityTaskCompletedEventAttributes),
51
52
  on_activity_task_completed) --> Completed;
@@ -54,7 +55,7 @@ fsm! {
54
55
  Started --(ActivityTaskTimedOut(ActivityTaskTimedOutEventAttributes),
55
56
  shared on_activity_task_timed_out) --> TimedOut;
56
57
  Started --(Cancel, shared on_canceled) --> StartedActivityCancelCommandCreated;
57
- Started --(Abandon, shared on_abandoned) --> Canceled;
58
+ Started --(Abandon, on_abandoned) --> Canceled;
58
59
 
59
60
  ScheduledActivityCancelCommandCreated --(CommandRequestCancelActivityTask) --> ScheduledActivityCancelCommandCreated;
60
61
  ScheduledActivityCancelCommandCreated --(ActivityTaskCancelRequested) --> ScheduledActivityCancelEventRecorded;
@@ -89,39 +90,46 @@ pub(super) enum ActivityMachineCommand {
89
90
  #[display(fmt = "Fail")]
90
91
  Fail(Failure),
91
92
  #[display(fmt = "Cancel")]
92
- Cancel(Option<Payloads>),
93
+ Cancel(Option<ActivityTaskCanceledEventAttributes>),
93
94
  #[display(fmt = "RequestCancellation")]
94
95
  RequestCancellation(Command),
95
96
  }
96
97
 
97
- /// Creates a new activity state machine and a command to schedule it on the server.
98
- pub(super) fn new_activity(attribs: ScheduleActivity) -> NewMachineWithCommand {
99
- let (activity, add_cmd) = ActivityMachine::new_scheduled(attribs);
100
- NewMachineWithCommand {
101
- command: add_cmd,
102
- machine: activity.into(),
103
- }
98
+ pub(super) struct ActTaskScheduledData {
99
+ event_id: i64,
100
+ act_type: String,
101
+ act_id: String,
102
+ last_task_in_history: bool,
104
103
  }
105
104
 
106
105
  impl ActivityMachine {
107
106
  /// Create a new activity and immediately schedule it.
108
- fn new_scheduled(attribs: ScheduleActivity) -> (Self, Command) {
109
- let mut s = Self {
110
- state: Created {}.into(),
111
- shared_state: SharedState {
112
- cancellation_type: ActivityCancellationType::from_i32(attribs.cancellation_type)
107
+ pub(super) fn new_scheduled(
108
+ attrs: ScheduleActivity,
109
+ internal_flags: InternalFlagsRef,
110
+ ) -> NewMachineWithCommand {
111
+ let mut s = Self::from_parts(
112
+ Created {}.into(),
113
+ SharedState {
114
+ cancellation_type: ActivityCancellationType::from_i32(attrs.cancellation_type)
113
115
  .unwrap(),
114
- attrs: attribs,
115
- ..Default::default()
116
+ attrs,
117
+ internal_flags,
118
+ scheduled_event_id: 0,
119
+ started_event_id: 0,
120
+ cancelled_before_sent: false,
116
121
  },
117
- };
122
+ );
118
123
  OnEventWrapper::on_event_mut(&mut s, ActivityMachineEvents::Schedule)
119
124
  .expect("Scheduling activities doesn't fail");
120
- let cmd = Command {
125
+ let command = Command {
121
126
  command_type: CommandType::ScheduleActivityTask as i32,
122
127
  attributes: Some(s.shared_state().attrs.clone().into()),
123
128
  };
124
- (s, cmd)
129
+ NewMachineWithCommand {
130
+ command,
131
+ machine: s.into(),
132
+ }
125
133
  }
126
134
 
127
135
  fn machine_responses_from_cancel_request(&self, cancel_cmd: Command) -> Vec<MachineResponse> {
@@ -136,45 +144,46 @@ impl ActivityMachine {
136
144
  r
137
145
  }
138
146
 
139
- fn create_cancelation_resolve(&self, details: Option<Payload>) -> ResolveActivity {
147
+ fn create_cancelation_resolve(
148
+ &self,
149
+ attrs: Option<ActivityTaskCanceledEventAttributes>,
150
+ ) -> ResolveActivity {
151
+ let attrs = attrs.unwrap_or_default();
140
152
  ResolveActivity {
141
153
  seq: self.shared_state.attrs.seq,
142
154
  result: Some(ActivityResolution {
143
155
  status: Some(activity_resolution::Status::Cancelled(Cancellation {
144
- failure: Some(Failure {
145
- message: "Activity cancelled".to_string(),
146
- cause: Some(Box::from(Failure {
147
- failure_info: Some(FailureInfo::CanceledFailureInfo(
148
- CanceledFailureInfo {
149
- details: details.map(Into::into),
150
- },
151
- )),
152
- ..Default::default()
153
- })),
154
- failure_info: Some(FailureInfo::ActivityFailureInfo(ActivityFailureInfo {
155
- scheduled_event_id: self.shared_state.scheduled_event_id,
156
- started_event_id: self.shared_state.started_event_id,
157
- activity_type: Some(ActivityType {
158
- name: self.shared_state.attrs.activity_type.clone(),
159
- }),
160
- activity_id: self.shared_state.attrs.activity_id.clone(),
161
- retry_state: RetryState::CancelRequested as i32,
162
- ..Default::default()
163
- })),
164
- ..Default::default()
165
- }),
156
+ failure: Some(new_cancel_failure(&self.shared_state, attrs)),
166
157
  })),
167
158
  }),
168
159
  }
169
160
  }
170
161
  }
171
162
 
172
- impl TryFrom<HistoryEvent> for ActivityMachineEvents {
163
+ impl TryFrom<HistEventData> for ActivityMachineEvents {
173
164
  type Error = WFMachinesError;
174
165
 
175
- fn try_from(e: HistoryEvent) -> Result<Self, Self::Error> {
166
+ fn try_from(e: HistEventData) -> Result<Self, Self::Error> {
167
+ let last_task_in_history = e.current_task_is_last_in_history;
168
+ let e = e.event;
176
169
  Ok(match e.event_type() {
177
- EventType::ActivityTaskScheduled => Self::ActivityTaskScheduled(e.event_id),
170
+ EventType::ActivityTaskScheduled => {
171
+ if let Some(history_event::Attributes::ActivityTaskScheduledEventAttributes(
172
+ attrs,
173
+ )) = e.attributes
174
+ {
175
+ Self::ActivityTaskScheduled(ActTaskScheduledData {
176
+ event_id: e.event_id,
177
+ act_id: attrs.activity_id,
178
+ act_type: attrs.activity_type.unwrap_or_default().name,
179
+ last_task_in_history,
180
+ })
181
+ } else {
182
+ return Err(WFMachinesError::Fatal(format!(
183
+ "Activity scheduled attributes were unset: {e}"
184
+ )));
185
+ }
186
+ }
178
187
  EventType::ActivityTaskStarted => Self::ActivityTaskStarted(e.event_id),
179
188
  EventType::ActivityTaskCompleted => {
180
189
  if let Some(history_event::Attributes::ActivityTaskCompletedEventAttributes(
@@ -263,10 +272,8 @@ impl WFMachinesAdapter for ActivityMachine {
263
272
  ActivityMachineCommand::RequestCancellation(c) => {
264
273
  self.machine_responses_from_cancel_request(c)
265
274
  }
266
- ActivityMachineCommand::Cancel(details) => {
267
- vec![self
268
- .create_cancelation_resolve(convert_payloads(event_info, details)?)
269
- .into()]
275
+ ActivityMachineCommand::Cancel(attrs) => {
276
+ vec![self.create_cancelation_resolve(attrs).into()]
270
277
  }
271
278
  })
272
279
  }
@@ -325,14 +332,7 @@ impl Cancellable for ActivityMachine {
325
332
  self.machine_responses_from_cancel_request(cmd)
326
333
  }
327
334
  ActivityMachineCommand::Cancel(details) => {
328
- vec![self
329
- .create_cancelation_resolve(
330
- details
331
- .map(TryInto::try_into)
332
- .transpose()
333
- .unwrap_or_default(),
334
- )
335
- .into()]
335
+ vec![self.create_cancelation_resolve(details).into()]
336
336
  }
337
337
  x => panic!("Invalid cancel event response {x:?}"),
338
338
  })
@@ -345,13 +345,14 @@ impl Cancellable for ActivityMachine {
345
345
  }
346
346
  }
347
347
 
348
- #[derive(Default, Clone)]
348
+ #[derive(Clone)]
349
349
  pub(super) struct SharedState {
350
350
  scheduled_event_id: i64,
351
351
  started_event_id: i64,
352
352
  attrs: ScheduleActivity,
353
353
  cancellation_type: ActivityCancellationType,
354
354
  cancelled_before_sent: bool,
355
+ internal_flags: InternalFlagsRef,
355
356
  }
356
357
 
357
358
  #[derive(Default, Clone)]
@@ -370,28 +371,37 @@ pub(super) struct ScheduleCommandCreated {}
370
371
  impl ScheduleCommandCreated {
371
372
  pub(super) fn on_activity_task_scheduled(
372
373
  self,
373
- dat: SharedState,
374
- scheduled_event_id: i64,
374
+ dat: &mut SharedState,
375
+ sched_dat: ActTaskScheduledData,
375
376
  ) -> ActivityMachineTransition<ScheduledEventRecorded> {
376
- ActivityMachineTransition::ok_shared(
377
- vec![],
378
- ScheduledEventRecorded::default(),
379
- SharedState {
380
- scheduled_event_id,
381
- ..dat
382
- },
383
- )
377
+ if dat.internal_flags.borrow_mut().try_use(
378
+ CoreInternalFlags::IdAndTypeDeterminismChecks,
379
+ sched_dat.last_task_in_history,
380
+ ) {
381
+ if sched_dat.act_id != dat.attrs.activity_id {
382
+ return TransitionResult::Err(WFMachinesError::Nondeterminism(format!(
383
+ "Activity id of scheduled event '{}' does not \
384
+ match activity id of activity command '{}'",
385
+ sched_dat.act_id, dat.attrs.activity_id
386
+ )));
387
+ }
388
+ if sched_dat.act_type != dat.attrs.activity_type {
389
+ return TransitionResult::Err(WFMachinesError::Nondeterminism(format!(
390
+ "Activity type of scheduled event '{}' does not \
391
+ match activity type of activity command '{}'",
392
+ sched_dat.act_type, dat.attrs.activity_type
393
+ )));
394
+ }
395
+ }
396
+ dat.scheduled_event_id = sched_dat.event_id;
397
+ ActivityMachineTransition::default()
384
398
  }
385
- pub(super) fn on_canceled(self, dat: SharedState) -> ActivityMachineTransition<Canceled> {
386
- let canceled_state = SharedState {
387
- cancelled_before_sent: true,
388
- ..dat
389
- };
399
+
400
+ pub(super) fn on_canceled(self, dat: &mut SharedState) -> ActivityMachineTransition<Canceled> {
401
+ dat.cancelled_before_sent = true;
390
402
  match dat.cancellation_type {
391
- ActivityCancellationType::Abandon => {
392
- ActivityMachineTransition::ok_shared(vec![], Canceled::default(), canceled_state)
393
- }
394
- _ => notify_lang_activity_cancelled(canceled_state, None),
403
+ ActivityCancellationType::Abandon => ActivityMachineTransition::default(),
404
+ _ => notify_lang_activity_cancelled(None),
395
405
  }
396
406
  }
397
407
  }
@@ -402,21 +412,15 @@ pub(super) struct ScheduledEventRecorded {}
402
412
  impl ScheduledEventRecorded {
403
413
  pub(super) fn on_task_started(
404
414
  self,
405
- dat: SharedState,
415
+ dat: &mut SharedState,
406
416
  started_event_id: i64,
407
417
  ) -> ActivityMachineTransition<Started> {
408
- ActivityMachineTransition::ok_shared(
409
- vec![],
410
- Started::default(),
411
- SharedState {
412
- started_event_id,
413
- ..dat
414
- },
415
- )
418
+ dat.started_event_id = started_event_id;
419
+ ActivityMachineTransition::default()
416
420
  }
417
421
  pub(super) fn on_task_timed_out(
418
422
  self,
419
- dat: SharedState,
423
+ dat: &mut SharedState,
420
424
  attrs: ActivityTaskTimedOutEventAttributes,
421
425
  ) -> ActivityMachineTransition<TimedOut> {
422
426
  notify_lang_activity_timed_out(dat, attrs)
@@ -424,15 +428,15 @@ impl ScheduledEventRecorded {
424
428
 
425
429
  pub(super) fn on_canceled(
426
430
  self,
427
- dat: SharedState,
431
+ dat: &mut SharedState,
428
432
  ) -> ActivityMachineTransition<ScheduledActivityCancelCommandCreated> {
429
433
  create_request_cancel_activity_task_command(
430
434
  dat,
431
435
  ScheduledActivityCancelCommandCreated::default(),
432
436
  )
433
437
  }
434
- pub(super) fn on_abandoned(self, dat: SharedState) -> ActivityMachineTransition<Canceled> {
435
- notify_lang_activity_cancelled(dat, None)
438
+ pub(super) fn on_abandoned(self) -> ActivityMachineTransition<Canceled> {
439
+ notify_lang_activity_cancelled(None)
436
440
  }
437
441
  }
438
442
 
@@ -451,7 +455,7 @@ impl Started {
451
455
  }
452
456
  pub(super) fn on_activity_task_failed(
453
457
  self,
454
- dat: SharedState,
458
+ dat: &mut SharedState,
455
459
  attrs: ActivityTaskFailedEventAttributes,
456
460
  ) -> ActivityMachineTransition<Failed> {
457
461
  ActivityMachineTransition::ok(
@@ -462,7 +466,7 @@ impl Started {
462
466
 
463
467
  pub(super) fn on_activity_task_timed_out(
464
468
  self,
465
- dat: SharedState,
469
+ dat: &mut SharedState,
466
470
  attrs: ActivityTaskTimedOutEventAttributes,
467
471
  ) -> ActivityMachineTransition<TimedOut> {
468
472
  notify_lang_activity_timed_out(dat, attrs)
@@ -470,15 +474,15 @@ impl Started {
470
474
 
471
475
  pub(super) fn on_canceled(
472
476
  self,
473
- dat: SharedState,
477
+ dat: &mut SharedState,
474
478
  ) -> ActivityMachineTransition<StartedActivityCancelCommandCreated> {
475
479
  create_request_cancel_activity_task_command(
476
480
  dat,
477
481
  StartedActivityCancelCommandCreated::default(),
478
482
  )
479
483
  }
480
- pub(super) fn on_abandoned(self, dat: SharedState) -> ActivityMachineTransition<Canceled> {
481
- notify_lang_activity_cancelled(dat, None)
484
+ pub(super) fn on_abandoned(self) -> ActivityMachineTransition<Canceled> {
485
+ notify_lang_activity_cancelled(None)
482
486
  }
483
487
  }
484
488
 
@@ -491,15 +495,15 @@ pub(super) struct ScheduledActivityCancelEventRecorded {}
491
495
  impl ScheduledActivityCancelEventRecorded {
492
496
  pub(super) fn on_activity_task_canceled(
493
497
  self,
494
- dat: SharedState,
498
+ dat: &mut SharedState,
495
499
  attrs: ActivityTaskCanceledEventAttributes,
496
500
  ) -> ActivityMachineTransition<Canceled> {
497
- notify_if_not_already_cancelled(dat, |dat| notify_lang_activity_cancelled(dat, Some(attrs)))
501
+ notify_if_not_already_cancelled(dat, |_| notify_lang_activity_cancelled(Some(attrs)))
498
502
  }
499
503
 
500
504
  pub(super) fn on_activity_task_timed_out(
501
505
  self,
502
- dat: SharedState,
506
+ dat: &mut SharedState,
503
507
  attrs: ActivityTaskTimedOutEventAttributes,
504
508
  ) -> ActivityMachineTransition<TimedOut> {
505
509
  notify_if_not_already_cancelled(dat, |dat| notify_lang_activity_timed_out(dat, attrs))
@@ -521,7 +525,7 @@ pub(super) struct StartedActivityCancelEventRecorded {}
521
525
  impl StartedActivityCancelEventRecorded {
522
526
  pub(super) fn on_activity_task_completed(
523
527
  self,
524
- dat: SharedState,
528
+ dat: &mut SharedState,
525
529
  attrs: ActivityTaskCompletedEventAttributes,
526
530
  ) -> ActivityMachineTransition<Completed> {
527
531
  notify_if_not_already_cancelled(dat, |_| {
@@ -530,7 +534,7 @@ impl StartedActivityCancelEventRecorded {
530
534
  }
531
535
  pub(super) fn on_activity_task_failed(
532
536
  self,
533
- dat: SharedState,
537
+ dat: &mut SharedState,
534
538
  attrs: ActivityTaskFailedEventAttributes,
535
539
  ) -> ActivityMachineTransition<Failed> {
536
540
  notify_if_not_already_cancelled(dat, |dat| {
@@ -539,23 +543,23 @@ impl StartedActivityCancelEventRecorded {
539
543
  }
540
544
  pub(super) fn on_activity_task_timed_out(
541
545
  self,
542
- dat: SharedState,
546
+ dat: &mut SharedState,
543
547
  attrs: ActivityTaskTimedOutEventAttributes,
544
548
  ) -> ActivityMachineTransition<TimedOut> {
545
549
  notify_if_not_already_cancelled(dat, |dat| notify_lang_activity_timed_out(dat, attrs))
546
550
  }
547
551
  pub(super) fn on_activity_task_canceled(
548
552
  self,
549
- dat: SharedState,
553
+ dat: &mut SharedState,
550
554
  attrs: ActivityTaskCanceledEventAttributes,
551
555
  ) -> ActivityMachineTransition<Canceled> {
552
- notify_if_not_already_cancelled(dat, |dat| notify_lang_activity_cancelled(dat, Some(attrs)))
556
+ notify_if_not_already_cancelled(dat, |_| notify_lang_activity_cancelled(Some(attrs)))
553
557
  }
554
558
  }
555
559
 
556
560
  fn notify_if_not_already_cancelled<S>(
557
- dat: SharedState,
558
- notifier: impl FnOnce(SharedState) -> ActivityMachineTransition<S>,
561
+ dat: &mut SharedState,
562
+ notifier: impl FnOnce(&mut SharedState) -> ActivityMachineTransition<S>,
559
563
  ) -> ActivityMachineTransition<S>
560
564
  where
561
565
  S: Into<ActivityMachineState> + Default,
@@ -634,7 +638,7 @@ pub(super) struct Canceled {}
634
638
  impl Canceled {
635
639
  pub(super) fn on_activity_task_started(
636
640
  self,
637
- dat: SharedState,
641
+ dat: &mut SharedState,
638
642
  seq_num: i64,
639
643
  ) -> ActivityMachineTransition<Canceled> {
640
644
  // Abandoned activities might start anyway. Ignore the result.
@@ -649,7 +653,7 @@ impl Canceled {
649
653
  }
650
654
  pub(super) fn on_activity_task_completed(
651
655
  self,
652
- dat: SharedState,
656
+ dat: &mut SharedState,
653
657
  attrs: ActivityTaskCompletedEventAttributes,
654
658
  ) -> ActivityMachineTransition<Canceled> {
655
659
  // Abandoned activities might complete anyway. Ignore the result.
@@ -664,7 +668,7 @@ impl Canceled {
664
668
  }
665
669
 
666
670
  fn create_request_cancel_activity_task_command<S>(
667
- dat: SharedState,
671
+ dat: &mut SharedState,
668
672
  next_state: S,
669
673
  ) -> ActivityMachineTransition<S>
670
674
  where
@@ -689,73 +693,95 @@ where
689
693
  /// Notifies lang side that activity has timed out by sending a failure with timeout error as a cause.
690
694
  /// State machine will transition into the TimedOut state.
691
695
  fn notify_lang_activity_timed_out(
692
- dat: SharedState,
696
+ dat: &SharedState,
693
697
  attrs: ActivityTaskTimedOutEventAttributes,
694
698
  ) -> TransitionResult<ActivityMachine, TimedOut> {
695
- ActivityMachineTransition::ok_shared(
696
- vec![ActivityMachineCommand::Fail(new_timeout_failure(
697
- &dat, attrs,
698
- ))],
699
- TimedOut::default(),
700
- dat,
701
- )
699
+ ActivityMachineTransition::commands(vec![ActivityMachineCommand::Fail(new_timeout_failure(
700
+ dat, attrs,
701
+ ))])
702
702
  }
703
703
 
704
- /// Notifies lang side that activity has been cancelled by sending a failure with cancelled failure
705
- /// as a cause. Optional cancelled_event, if passed, is used to supply event IDs. State machine will
706
- /// transition into the `next_state` provided as a parameter.
704
+ /// Returns the transition to indicate activity has been cancelled by sending a failure with
705
+ /// cancelled failure as a cause. Optional cancelled_event, if passed, is used to supply event IDs.
707
706
  fn notify_lang_activity_cancelled(
708
- dat: SharedState,
709
707
  canceled_event: Option<ActivityTaskCanceledEventAttributes>,
710
708
  ) -> ActivityMachineTransition<Canceled> {
711
- ActivityMachineTransition::ok_shared(
712
- vec![ActivityMachineCommand::Cancel(
713
- canceled_event.and_then(|e| e.details),
714
- )],
715
- Canceled::default(),
716
- dat,
717
- )
709
+ ActivityMachineTransition::commands(vec![ActivityMachineCommand::Cancel(canceled_event)])
718
710
  }
719
711
 
720
- fn new_failure(dat: SharedState, attrs: ActivityTaskFailedEventAttributes) -> Failure {
712
+ fn new_failure(dat: &SharedState, attrs: ActivityTaskFailedEventAttributes) -> Failure {
713
+ let rs = attrs.retry_state();
721
714
  Failure {
722
715
  message: "Activity task failed".to_owned(),
723
716
  cause: attrs.failure.map(Box::new),
724
- failure_info: Some(FailureInfo::ActivityFailureInfo(
725
- failure::ActivityFailureInfo {
726
- identity: attrs.identity,
727
- activity_type: Some(ActivityType {
728
- name: dat.attrs.activity_type,
729
- }),
730
- activity_id: dat.attrs.activity_id,
731
- retry_state: attrs.retry_state,
732
- started_event_id: attrs.started_event_id,
733
- scheduled_event_id: attrs.scheduled_event_id,
734
- },
717
+ failure_info: Some(activity_fail_info(
718
+ dat.attrs.activity_type.clone(),
719
+ dat.attrs.activity_id.clone(),
720
+ Some(attrs.identity),
721
+ rs,
722
+ attrs.started_event_id,
723
+ attrs.scheduled_event_id,
735
724
  )),
736
725
  ..Default::default()
737
726
  }
738
727
  }
739
728
 
740
729
  fn new_timeout_failure(dat: &SharedState, attrs: ActivityTaskTimedOutEventAttributes) -> Failure {
741
- let failure_info = ActivityFailureInfo {
742
- activity_id: dat.attrs.activity_id.to_string(),
743
- activity_type: Some(ActivityType {
744
- name: dat.attrs.activity_type.to_string(),
745
- }),
746
- scheduled_event_id: attrs.scheduled_event_id,
747
- started_event_id: attrs.started_event_id,
748
- retry_state: attrs.retry_state,
749
- ..Default::default()
750
- };
730
+ let rs = attrs.retry_state();
751
731
  Failure {
752
732
  message: "Activity task timed out".to_string(),
753
733
  cause: attrs.failure.map(Box::new),
754
- failure_info: Some(FailureInfo::ActivityFailureInfo(failure_info)),
734
+ failure_info: Some(activity_fail_info(
735
+ dat.attrs.activity_type.clone(),
736
+ dat.attrs.activity_id.clone(),
737
+ None,
738
+ rs,
739
+ attrs.started_event_id,
740
+ attrs.scheduled_event_id,
741
+ )),
755
742
  ..Default::default()
756
743
  }
757
744
  }
758
745
 
746
+ fn new_cancel_failure(dat: &SharedState, attrs: ActivityTaskCanceledEventAttributes) -> Failure {
747
+ Failure {
748
+ message: "Activity cancelled".to_string(),
749
+ cause: Some(Box::from(Failure {
750
+ failure_info: Some(FailureInfo::CanceledFailureInfo(CanceledFailureInfo {
751
+ details: attrs.details.map(Into::into),
752
+ })),
753
+ ..Default::default()
754
+ })),
755
+ failure_info: Some(activity_fail_info(
756
+ dat.attrs.activity_type.clone(),
757
+ dat.attrs.activity_id.clone(),
758
+ Some(attrs.identity),
759
+ RetryState::CancelRequested,
760
+ attrs.started_event_id,
761
+ attrs.scheduled_event_id,
762
+ )),
763
+ ..Default::default()
764
+ }
765
+ }
766
+
767
+ pub fn activity_fail_info(
768
+ act_type: String,
769
+ act_id: String,
770
+ identity: Option<String>,
771
+ retry_state: RetryState,
772
+ started_event_id: i64,
773
+ scheduled_event_id: i64,
774
+ ) -> FailureInfo {
775
+ FailureInfo::ActivityFailureInfo(ActivityFailureInfo {
776
+ identity: identity.unwrap_or_default(),
777
+ activity_type: Some(ActivityType { name: act_type }),
778
+ activity_id: act_id,
779
+ retry_state: retry_state as i32,
780
+ started_event_id,
781
+ scheduled_event_id,
782
+ })
783
+ }
784
+
759
785
  fn convert_payloads(
760
786
  event_info: Option<EventInfo>,
761
787
  result: Option<Payloads>,
@@ -771,15 +797,17 @@ fn convert_payloads(
771
797
  mod test {
772
798
  use super::*;
773
799
  use crate::{
774
- replay::TestHistoryBuilder, test_help::canned_histories, worker::workflow::ManagedWFFunc,
800
+ internal_flags::InternalFlags, replay::TestHistoryBuilder, test_help::canned_histories,
801
+ worker::workflow::ManagedWFFunc,
775
802
  };
776
803
  use rstest::{fixture, rstest};
777
- use std::mem::discriminant;
804
+ use std::{cell::RefCell, mem::discriminant, rc::Rc};
778
805
  use temporal_sdk::{
779
806
  ActivityOptions, CancellableFuture, WfContext, WorkflowFunction, WorkflowResult,
780
807
  };
781
- use temporal_sdk_core_protos::coresdk::workflow_activation::{
782
- workflow_activation_job, WorkflowActivationJob,
808
+ use temporal_sdk_core_protos::{
809
+ coresdk::workflow_activation::{workflow_activation_job, WorkflowActivationJob},
810
+ DEFAULT_ACTIVITY_TYPE,
783
811
  };
784
812
 
785
813
  #[fixture]
@@ -799,7 +827,13 @@ mod test {
799
827
  }
800
828
 
801
829
  async fn activity_wf(command_sink: WfContext) -> WorkflowResult<()> {
802
- command_sink.activity(ActivityOptions::default()).await;
830
+ command_sink
831
+ .activity(ActivityOptions {
832
+ activity_id: Some("activity-id-1".to_string()),
833
+ activity_type: DEFAULT_ACTIVITY_TYPE.to_string(),
834
+ ..Default::default()
835
+ })
836
+ .await;
803
837
  Ok(().into())
804
838
  }
805
839
 
@@ -887,13 +921,20 @@ mod test {
887
921
  TimedOut {}.into(),
888
922
  Completed {}.into(),
889
923
  ] {
890
- let mut s = ActivityMachine {
891
- state: state.clone(),
892
- shared_state: Default::default(),
893
- };
924
+ let mut s = ActivityMachine::from_parts(
925
+ state.clone(),
926
+ SharedState {
927
+ scheduled_event_id: 0,
928
+ started_event_id: 0,
929
+ attrs: Default::default(),
930
+ cancellation_type: Default::default(),
931
+ cancelled_before_sent: false,
932
+ internal_flags: Rc::new(RefCell::new(InternalFlags::new(&Default::default()))),
933
+ },
934
+ );
894
935
  let cmds = s.cancel().unwrap();
895
936
  assert_eq!(cmds.len(), 0);
896
- assert_eq!(discriminant(&state), discriminant(&s.state));
937
+ assert_eq!(discriminant(&state), discriminant(s.state()));
897
938
  }
898
939
  }
899
940
  }