command_tower 0.19.0 → 0.21.0

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 (29) hide show
  1. checksums.yaml +4 -4
  2. data/app/models/command_tower/messaging/delivery_attempt.rb +5 -0
  3. data/app/serializers/command_tower/serializers/messaging/preferences/notification_serializer.rb +109 -1
  4. data/app/services/command_tower/messaging/channels/definition.rb +3 -0
  5. data/app/services/command_tower/messaging/channels.rb +15 -0
  6. data/app/services/command_tower/messaging/delivery_retry_policy.rb +97 -0
  7. data/app/services/command_tower/messaging/execution/adapters/expo/adapter.rb +71 -23
  8. data/app/services/command_tower/messaging/execution/adapters/expo/http_client.rb +4 -0
  9. data/app/services/command_tower/messaging/execution/attempt_ledger.rb +42 -2
  10. data/app/services/command_tower/messaging/execution/operation_logger.rb +10 -0
  11. data/app/services/command_tower/messaging/execution/recovery.rb +64 -8
  12. data/app/services/command_tower/messaging/notification_types/declaration.rb +15 -1
  13. data/app/services/command_tower/messaging/notification_types/declaration_validator.rb +53 -0
  14. data/app/services/command_tower/messaging/preferences/catalog.rb +3 -32
  15. data/app/services/command_tower/messaging/preferences/catalog_notification_result.rb +35 -1
  16. data/app/services/command_tower/messaging/preferences/channel_catalog.rb +24 -0
  17. data/app/services/command_tower/messaging/preferences/corrections.rb +50 -0
  18. data/app/services/command_tower/messaging/preferences/destination_locks.rb +33 -0
  19. data/app/services/command_tower/messaging/preferences/destinations.rb +121 -0
  20. data/app/services/command_tower/messaging/preferences/notification_assembly.rb +77 -0
  21. data/app/services/command_tower/messaging/preferences/setting_controls.rb +25 -0
  22. data/app/services/command_tower/messaging/preferences/settings_projection.rb +126 -0
  23. data/app/services/command_tower/messaging/preferences/update.rb +57 -40
  24. data/app/workflows/command_tower/workflows/messaging/execution/deliver_workflow.rb +19 -0
  25. data/docs/upgrades/0.20.0.md +29 -0
  26. data/docs/upgrades/0.21.0.md +31 -0
  27. data/docs/upgrades/README.md +2 -0
  28. data/lib/command_tower/version.rb +1 -1
  29. metadata +11 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b932782bd089979dc908cc1865cdeefe8839fba2a4e5dbdb4956e00484345450
4
- data.tar.gz: c88d647eaa666f9593416acb3771e407f22d2ef090c7035f45b65b4cc2ff9265
3
+ metadata.gz: b21d84c5b214027953938804efd0dba2a7523229c33f073bb010c5db82128755
4
+ data.tar.gz: 9c74f1edd4ff8774167b3b40e2423fdd8c286107ac58940f88fcae3f9808b04e
5
5
  SHA512:
6
- metadata.gz: b3cff84a5ecf68c9929e4c9eca099c2f551a97a886e8c2882b6d4e411d064cb9df24fb64e626b35e08d53b72bcca68950c5e7cd4c197c482415f040e76f89be4
7
- data.tar.gz: aaa5bf71dadf4a4a8de13bcc25dd9c826a68c20c37cc014b86130b86d8ad7733d6a575608057a1f19c1d4e2435b79c5a2f679d7f8bb7d73c411886c9dfdfd712
6
+ metadata.gz: 948f30f811a880da5001b091360eefaed16cb6f1ad52bd93bd57d753ce6cef8bfb6f1d425cac0e299da772bdbd931e115b1c30e80a90376587d084f50630ead2
7
+ data.tar.gz: b578871e337d1291d18819c7a37e455b6646fdba6f2f33ffa67fb94fd415aee5a5a7d1b5c9e3c628772724552b764b9c9c6732b076e2e89bc2be4d741376996a
@@ -21,6 +21,11 @@ module CommandTower
21
21
  class_name: "CommandTower::Messaging::ChannelDelivery",
22
22
  inverse_of: :delivery_attempts
23
23
 
24
+ # MariaDB reports JSON columns as LONGTEXT. The text caster would write
25
+ # a Ruby hash string and fail `json_valid`. Forcing the JSON caster
26
+ # writes one JSON document on MySQL and on MariaDB.
27
+ attribute :provider_metadata, :json
28
+
24
29
  validates :status, presence: true, inclusion: { in: STATUSES }
25
30
  validates :started_at, presence: true
26
31
  end
@@ -6,7 +6,7 @@ module CommandTower
6
6
  module Preferences
7
7
  class NotificationSerializer
8
8
  def self.serialize(notification)
9
- {
9
+ payload = {
10
10
  key: notification.key,
11
11
  label: notification.label,
12
12
  description: notification.description,
@@ -16,6 +16,9 @@ module CommandTower
16
16
  inboxAvailable: notification.inbox_available,
17
17
  allowedChannels: notification.allowed_channels,
18
18
  availableChannels: notification.available_channels,
19
+ channelCatalog: serialize_channel_catalog(notification.channel_catalog),
20
+ destinations: serialize_destinations(notification.destinations),
21
+ settings: serialize_settings(notification.settings),
19
22
  urgency: notification.urgency,
20
23
  sound: notification.sound,
21
24
  preferences: {
@@ -24,7 +27,112 @@ module CommandTower
24
27
  storedOverridePresent: notification.stored_override_present,
25
28
  },
26
29
  }
30
+
31
+ corrections = serialize_corrections(notification.corrections)
32
+ payload[:corrections] = corrections unless corrections.nil?
33
+
34
+ payload
35
+ end
36
+
37
+ def self.serialize_channel_catalog(entries)
38
+ Array(entries).map do |entry|
39
+ {
40
+ key: entry[:key],
41
+ label: entry[:label],
42
+ description: entry[:description],
43
+ order: entry[:order],
44
+ glyphKey: entry[:glyph_key],
45
+ }
46
+ end
47
+ end
48
+ private_class_method :serialize_channel_catalog
49
+
50
+ def self.serialize_settings(settings)
51
+ Array(settings).map do |entry|
52
+ hash = entry.respond_to?(:to_h) ? entry.to_h : entry
53
+ {
54
+ key: entry_value(hash, :key),
55
+ channelKey: entry_value(hash, :channel_key) || entry_value(hash, :channelKey),
56
+ control: entry_value(hash, :control),
57
+ label: entry_value(hash, :label),
58
+ description: entry_value(hash, :description),
59
+ editable: entry_value(hash, :editable),
60
+ effective: entry_value(hash, :effective),
61
+ options: serialize_setting_options(entry_value(hash, :options)),
62
+ }
63
+ end
64
+ end
65
+ private_class_method :serialize_settings
66
+
67
+ def self.serialize_setting_options(options)
68
+ return nil if options.nil?
69
+
70
+ Array(options).map do |option|
71
+ option_hash = option.respond_to?(:to_h) ? option.to_h : option
72
+ {
73
+ value: entry_value(option_hash, :value),
74
+ label: entry_value(option_hash, :label),
75
+ description: entry_value(option_hash, :description),
76
+ }
77
+ end
78
+ end
79
+ private_class_method :serialize_setting_options
80
+
81
+ def self.serialize_destinations(destinations)
82
+ destinations.to_h.transform_values do |entry|
83
+ serialize_destination_entry(entry)
84
+ end
85
+ end
86
+ private_class_method :serialize_destinations
87
+
88
+ def self.serialize_destination_entry(entry)
89
+ hash =
90
+ if entry.respond_to?(:to_h)
91
+ entry.to_h
92
+ else
93
+ entry
94
+ end
95
+
96
+ {
97
+ state: entry_value(hash, :state),
98
+ effective: entry_value(hash, :effective),
99
+ reasonCode: entry_value(hash, :reason_code) || entry_value(hash, :reasonCode),
100
+ reason: entry_value(hash, :reason),
101
+ }
102
+ end
103
+
104
+ def self.entry_value(hash, key)
105
+ return hash[key] if hash.key?(key)
106
+
107
+ string_key = key.to_s
108
+ return hash[string_key] if hash.key?(string_key)
109
+
110
+ nil
111
+ end
112
+ private_class_method :entry_value
113
+ private_class_method :serialize_destination_entry
114
+
115
+ def self.serialize_corrections(corrections)
116
+ return nil if corrections.nil?
117
+
118
+ Array(corrections).map do |entry|
119
+ hash =
120
+ if entry.respond_to?(:to_h)
121
+ entry.to_h
122
+ else
123
+ entry
124
+ end
125
+
126
+ {
127
+ channelKey: hash[:channel_key] || hash["channelKey"],
128
+ submitted: hash[:submitted],
129
+ effective: hash[:effective],
130
+ reasonCode: hash[:reason_code] || hash["reasonCode"],
131
+ reason: hash[:reason],
132
+ }
133
+ end
27
134
  end
135
+ private_class_method :serialize_corrections
28
136
  end
29
137
  end
30
138
  end
@@ -6,6 +6,9 @@ module CommandTower
6
6
  Definition = Data.define(
7
7
  :key,
8
8
  :label,
9
+ :description,
10
+ :order,
11
+ :glyph_key,
9
12
  :kind,
10
13
  :supports_endpoint_records,
11
14
  :identity_backed,
@@ -12,6 +12,9 @@ module CommandTower
12
12
  Definition.new(
13
13
  key: "inbox",
14
14
  label: "Inbox",
15
+ description: "In-app messages",
16
+ order: 0,
17
+ glyph_key: "inbox",
15
18
  kind: :inbox,
16
19
  supports_endpoint_records: false,
17
20
  identity_backed: false,
@@ -20,6 +23,9 @@ module CommandTower
20
23
  Definition.new(
21
24
  key: "email",
22
25
  label: "Email",
26
+ description: "Email to your account address",
27
+ order: 1,
28
+ glyph_key: "mail",
23
29
  kind: :external,
24
30
  supports_endpoint_records: false,
25
31
  identity_backed: true,
@@ -28,6 +34,9 @@ module CommandTower
28
34
  Definition.new(
29
35
  key: "sms",
30
36
  label: "SMS",
37
+ description: "Text messages",
38
+ order: 2,
39
+ glyph_key: "message",
31
40
  kind: :external,
32
41
  supports_endpoint_records: false,
33
42
  identity_backed: true,
@@ -36,6 +45,9 @@ module CommandTower
36
45
  Definition.new(
37
46
  key: "push",
38
47
  label: "Push",
48
+ description: "Alerts on this device",
49
+ order: 3,
50
+ glyph_key: "bell",
39
51
  kind: :external,
40
52
  supports_endpoint_records: true,
41
53
  identity_backed: false,
@@ -44,6 +56,9 @@ module CommandTower
44
56
  Definition.new(
45
57
  key: "pushover",
46
58
  label: "Pushover",
59
+ description: "Pushover alerts",
60
+ order: 4,
61
+ glyph_key: "radio",
47
62
  kind: :external,
48
63
  supports_endpoint_records: true,
49
64
  identity_backed: false,
@@ -8,6 +8,10 @@ module CommandTower
8
8
  MAX_ATTEMPTS = 5
9
9
  GRACE_WINDOW = 1.minute
10
10
 
11
+ PUSH_RETRY_LIMIT = 2
12
+ SUBMISSION_NOT_SUBMITTED = "not_submitted"
13
+ SUBMISSION_UNCERTAIN = "submission_uncertain"
14
+
11
15
  class << self
12
16
  def retries_exhausted?(delivery)
13
17
  delivery.status == ChannelDelivery::STATUS_FAILED_RETRYABLE &&
@@ -24,6 +28,99 @@ module CommandTower
24
28
 
25
29
  delivery.updated_at + GRACE_WINDOW
26
30
  end
31
+
32
+ def push_channel?(delivery)
33
+ delivery.channel_key == "push"
34
+ end
35
+
36
+ def max_execution_attempts_for(delivery)
37
+ push_channel?(delivery) ? PUSH_RETRY_LIMIT : MAX_ATTEMPTS
38
+ end
39
+
40
+ def confirmed_not_submitted?(attempt)
41
+ attempt.provider_metadata.is_a?(Hash) &&
42
+ attempt.provider_metadata["submission"] == SUBMISSION_NOT_SUBMITTED
43
+ end
44
+
45
+ def accepted_ticket_in_attempt?(attempt)
46
+ return true if attempt.status == Messaging::DeliveryAttempt::STATUS_SUCCEEDED
47
+
48
+ metadata = attempt.provider_metadata
49
+ return false unless metadata.is_a?(Hash)
50
+
51
+ endpoints = metadata.dig("expo", "endpoints")
52
+ return false unless endpoints.is_a?(Array)
53
+
54
+ endpoints.any? { |entry| entry["ticket_status"] == "ok" }
55
+ end
56
+
57
+ def push_has_open_started_attempt?(delivery)
58
+ delivery.delivery_attempts
59
+ .where(status: Messaging::DeliveryAttempt::STATUS_STARTED, finished_at: nil)
60
+ .exists?
61
+ end
62
+
63
+ def latest_finished_attempt(delivery)
64
+ delivery.delivery_attempts
65
+ .where.not(finished_at: nil)
66
+ .order(id: :desc)
67
+ .first
68
+ end
69
+
70
+ # Persisted proof only — not tied to channel delivery status.
71
+ def push_has_not_submitted_evidence?(delivery)
72
+ attempt = latest_finished_attempt(delivery)
73
+ attempt.present? && confirmed_not_submitted?(attempt)
74
+ end
75
+
76
+ def push_retry_budget_remaining?(delivery)
77
+ delivery.execution_attempt_count < PUSH_RETRY_LIMIT
78
+ end
79
+
80
+ # recover_failed! — delivery is failed_retryable.
81
+ def push_redrive_eligible_from_failed_retryable?(delivery)
82
+ return false unless push_channel?(delivery)
83
+ return false unless delivery.status == ChannelDelivery::STATUS_FAILED_RETRYABLE
84
+ return false if push_has_open_started_attempt?(delivery)
85
+ return false unless push_has_not_submitted_evidence?(delivery)
86
+
87
+ push_retry_budget_remaining?(delivery)
88
+ end
89
+
90
+ # recover_queued! / recover_stale_executing! — delivery may already be queued.
91
+ def push_redrive_eligible_while_queued?(delivery)
92
+ return true unless push_channel?(delivery)
93
+ return false if push_has_open_started_attempt?(delivery)
94
+ return true if delivery.delivery_attempts.none?
95
+
96
+ push_has_not_submitted_evidence?(delivery) && push_retry_budget_remaining?(delivery)
97
+ end
98
+
99
+ # DeliverWorkflow — may this execution call Expo?
100
+ def push_may_submit_to_provider?(delivery, current_attempt_id:)
101
+ return true unless push_channel?(delivery)
102
+
103
+ prior_attempts = delivery.delivery_attempts.where.not(id: current_attempt_id)
104
+ return false if prior_attempts
105
+ .where(status: Messaging::DeliveryAttempt::STATUS_STARTED, finished_at: nil)
106
+ .exists?
107
+
108
+ finished = prior_attempts.where.not(finished_at: nil)
109
+ return true if finished.none?
110
+
111
+ return false if finished.any? { |attempt| accepted_ticket_in_attempt?(attempt) }
112
+
113
+ return false if finished.any? do |attempt|
114
+ attempt.error_code == SUBMISSION_UNCERTAIN ||
115
+ (
116
+ attempt.status == Messaging::DeliveryAttempt::STATUS_FAILED_TERMINAL &&
117
+ !confirmed_not_submitted?(attempt)
118
+ )
119
+ end
120
+
121
+ latest = finished.order(:id).last
122
+ confirmed_not_submitted?(latest) && delivery.execution_attempt_count <= PUSH_RETRY_LIMIT
123
+ end
27
124
  end
28
125
  end
29
126
  end
@@ -75,13 +75,10 @@ module CommandTower
75
75
  else
76
76
  AdapterResult.build(outcome: :terminal_failure, error_code: "adapter_unconfigured")
77
77
  end
78
+ rescue HttpClient::RequestNotSubmitted
79
+ not_submitted_result(attempts, history:)
78
80
  rescue StandardError
79
- # Keep the endpoint facts already recorded; the outcome stays today's rescue-all result.
80
- AdapterResult.build(
81
- outcome: :retryable_failure,
82
- error_code: "expo_transient",
83
- provider_metadata: provider_metadata_for(attempts),
84
- )
81
+ uncertain_terminal_result(attempts, history:)
85
82
  end
86
83
 
87
84
  private
@@ -110,8 +107,8 @@ module CommandTower
110
107
  sendable = attempts.select { |attempt| attempt.token.present? && attempt.outcome.nil? }
111
108
  messages = sendable.map { |attempt| expo_message(token: attempt.token, rendered:, communication:) }
112
109
 
113
- sendable.each { |attempt| attempt.submitted = true }
114
110
  send_result = @http_client.send_messages(messages)
111
+ sendable.each { |attempt| attempt.submitted = true }
115
112
  unless send_result[:ok]
116
113
  return map_http_failure(send_result[:status_code], attempts:, history:)
117
114
  end
@@ -125,7 +122,7 @@ module CommandTower
125
122
 
126
123
  ok_ticket_ids = sendable.filter_map { |attempt| attempt.ticket&.dig(:id) if attempt.ticket&.dig(:status) == "ok" }
127
124
  if ok_ticket_ids.any?
128
- receipts_result = @http_client.get_receipts(ok_ticket_ids)
125
+ receipts_result = fetch_receipts(ok_ticket_ids)
129
126
  if receipts_result[:ok]
130
127
  receipts = receipts_result[:receipts] || {}
131
128
  sendable.each do |attempt|
@@ -148,6 +145,12 @@ module CommandTower
148
145
  aggregate_result(attempts, history:)
149
146
  end
150
147
 
148
+ def fetch_receipts(ticket_ids)
149
+ @http_client.get_receipts(ticket_ids)
150
+ rescue StandardError
151
+ { ok: false, status_code: nil, receipts: {} }
152
+ end
153
+
151
154
  # Every eligible endpoint already holds an accepted ticket or a recorded terminal
152
155
  # ticket error from an earlier attempt, so there is nothing to send.
153
156
  def settled_only_result(history)
@@ -194,8 +197,8 @@ module CommandTower
194
197
  def apply_ticket_outcome!(attempt)
195
198
  ticket = attempt.ticket
196
199
  if ticket.nil?
197
- attempt.outcome = :retryable_failure
198
- attempt.error_code = "expo_transient"
200
+ attempt.outcome = :terminal_failure
201
+ attempt.error_code = submission_uncertain_code
199
202
  return
200
203
  end
201
204
 
@@ -211,8 +214,8 @@ module CommandTower
211
214
  attempt.error_code = "expo_rejected"
212
215
  end
213
216
  else
214
- attempt.outcome = :retryable_failure
215
- attempt.error_code = "expo_transient"
217
+ attempt.outcome = :terminal_failure
218
+ attempt.error_code = submission_uncertain_code
216
219
  end
217
220
  end
218
221
 
@@ -246,29 +249,49 @@ module CommandTower
246
249
  nil
247
250
  end
248
251
 
249
- # 5xx and 429 stay retryable. Any other non-success response is terminal for this
250
- # attempt even when an earlier attempt had accepted tickets; those tickets are carried
251
- # into the metadata so the accepted facts stay visible on this row.
252
+ # After the HTTP client returns, a non-success response is uncertain for duplicate policy
253
+ # (request may have been accepted). Terminal rejection (4xx except 429) maps to expo_rejected.
252
254
  def map_http_failure(status_code, attempts:, history:)
253
- retryable = status_code.to_i >= 500 || status_code.to_i == 429
254
- outcome = retryable ? :retryable_failure : :terminal_failure
255
- error_code = retryable ? "expo_transient" : "expo_rejected"
255
+ code = status_code.to_i
256
+ error_code =
257
+ if code >= 500 || code == 429
258
+ submission_uncertain_code
259
+ else
260
+ "expo_rejected"
261
+ end
256
262
  attempts.each { |attempt| attempt.error_code ||= error_code if attempt.submitted }
257
- carried = retryable ? [] : carried_accepted_entries(history)
263
+ carried = error_code == submission_uncertain_code ? [] : carried_accepted_entries(history)
258
264
 
259
- AdapterResult.build(outcome:, error_code:, provider_metadata: provider_metadata_for(attempts, carried:))
265
+ AdapterResult.build(
266
+ outcome: :terminal_failure,
267
+ error_code:,
268
+ provider_metadata: provider_metadata_for(attempts, carried:),
269
+ )
260
270
  end
261
271
 
262
272
  # Delivery-wide aggregation: this attempt's endpoints plus tickets recorded earlier.
263
273
  def aggregate_result(attempts, history:)
274
+ submitted = attempts.select(&:submitted)
264
275
  successes = attempts.select { |attempt| attempt.outcome == :success }
265
276
  first_ticket_id = successes.filter_map { |attempt| attempt.ticket&.dig(:id) }.first
266
277
  metadata = provider_metadata_for(attempts)
267
278
 
279
+ if submitted.any? { |attempt| attempt.outcome == :success } &&
280
+ submitted.any? do |attempt|
281
+ attempt.outcome == :terminal_failure && attempt.error_code == submission_uncertain_code
282
+ end
283
+ return AdapterResult.build(
284
+ outcome: :terminal_failure,
285
+ error_code: submission_uncertain_code,
286
+ provider_message_id: first_ticket_id,
287
+ provider_metadata: metadata,
288
+ )
289
+ end
290
+
268
291
  if attempts.any? { |attempt| attempt.outcome == :retryable_failure }
269
292
  return AdapterResult.build(
270
- outcome: :retryable_failure,
271
- error_code: "expo_transient",
293
+ outcome: :terminal_failure,
294
+ error_code: submission_uncertain_code,
272
295
  provider_message_id: first_ticket_id,
273
296
  provider_metadata: metadata,
274
297
  )
@@ -290,13 +313,38 @@ module CommandTower
290
313
  return AdapterResult.build(outcome: :terminal_failure, error_code:, provider_metadata: metadata)
291
314
  end
292
315
 
316
+ AdapterResult.build(
317
+ outcome: :terminal_failure,
318
+ error_code: submission_uncertain_code,
319
+ provider_metadata: metadata,
320
+ )
321
+ end
322
+
323
+ def not_submitted_result(_attempts, history: nil)
293
324
  AdapterResult.build(
294
325
  outcome: :retryable_failure,
295
- error_code: "expo_transient",
326
+ error_code: "expo_not_submitted",
327
+ provider_metadata: {
328
+ "submission" => Messaging::DeliveryRetryPolicy::SUBMISSION_NOT_SUBMITTED,
329
+ },
330
+ )
331
+ end
332
+
333
+ def uncertain_terminal_result(attempts, history:)
334
+ metadata = provider_metadata_for(attempts)
335
+ metadata = metadata.merge("submission" => submission_uncertain_code) if metadata.is_a?(Hash)
336
+
337
+ AdapterResult.build(
338
+ outcome: :terminal_failure,
339
+ error_code: submission_uncertain_code,
296
340
  provider_metadata: metadata,
297
341
  )
298
342
  end
299
343
 
344
+ def submission_uncertain_code
345
+ Messaging::DeliveryRetryPolicy::SUBMISSION_UNCERTAIN
346
+ end
347
+
300
348
  def carried_accepted_entries(history)
301
349
  history.accepted_ticket_ids.map do |endpoint_id, ticket_id|
302
350
  entry = { "endpoint_id" => endpoint_id, "ticket_status" => "ok" }
@@ -12,6 +12,8 @@ module CommandTower
12
12
  # Narrow Expo Push HTTP client for Messaging notification push.
13
13
  # Never logs tokens, Authorization headers, or raw bodies that may contain tokens.
14
14
  class HttpClient
15
+ RequestNotSubmitted = Class.new(StandardError)
16
+
15
17
  def initialize(configuration: nil)
16
18
  @configuration = configuration || Configuration.new
17
19
  end
@@ -65,6 +67,8 @@ module CommandTower
65
67
  ) do |http|
66
68
  http.request(request)
67
69
  end
70
+ rescue Net::OpenTimeout, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError => error
71
+ raise RequestNotSubmitted, error.message
68
72
  end
69
73
 
70
74
  def parse_send_response(response)
@@ -108,6 +108,31 @@ module CommandTower
108
108
  now = Time.current
109
109
  resolved_class = error_class || error&.class&.name || "UnknownError"
110
110
 
111
+ if Messaging::DeliveryRetryPolicy.push_channel?(delivery)
112
+ uncertain = Messaging::DeliveryRetryPolicy::SUBMISSION_UNCERTAIN
113
+ ActiveRecord::Base.transaction do
114
+ attempt.update!(
115
+ status: Messaging::DeliveryAttempt::STATUS_FAILED_TERMINAL,
116
+ finished_at: now,
117
+ error_code: uncertain,
118
+ error_class: resolved_class,
119
+ normalized_provider_status: nil,
120
+ provider_message_id: nil,
121
+ )
122
+ delivery.update!(status: Messaging::ChannelDelivery::STATUS_FAILED_TERMINAL)
123
+ end
124
+
125
+ delivery.reload
126
+ attempt.reload
127
+ OperationLogger.attempt_failed(channel_delivery: delivery, delivery_attempt: attempt)
128
+ OperationLogger.failed(
129
+ channel_delivery: delivery,
130
+ error_code: uncertain,
131
+ error_class: resolved_class,
132
+ )
133
+ return delivery
134
+ end
135
+
111
136
  ActiveRecord::Base.transaction do
112
137
  attempt.update!(
113
138
  status: Messaging::DeliveryAttempt::STATUS_FAILED_RETRYABLE,
@@ -128,8 +153,17 @@ module CommandTower
128
153
  error_code: "internal_adapter_error",
129
154
  error_class: resolved_class,
130
155
  )
131
- rescue StandardError
132
- mark_failed_retryable_best_effort!(delivery)
156
+ rescue StandardError => error
157
+ OperationLogger.attempt_finalize_unpersisted(
158
+ channel_delivery: delivery,
159
+ delivery_attempt: attempt,
160
+ error_class: error.class.name,
161
+ )
162
+ if Messaging::DeliveryRetryPolicy.push_channel?(delivery)
163
+ mark_failed_terminal_best_effort!(delivery)
164
+ else
165
+ mark_failed_retryable_best_effort!(delivery)
166
+ end
133
167
  end
134
168
 
135
169
  private
@@ -143,6 +177,12 @@ module CommandTower
143
177
  rescue StandardError
144
178
  nil
145
179
  end
180
+
181
+ def mark_failed_terminal_best_effort!(delivery)
182
+ delivery.update(status: Messaging::ChannelDelivery::STATUS_FAILED_TERMINAL)
183
+ rescue StandardError
184
+ nil
185
+ end
146
186
  end
147
187
  end
148
188
  end
@@ -103,6 +103,16 @@ module CommandTower
103
103
  )
104
104
  end
105
105
 
106
+ def attempt_finalize_unpersisted(channel_delivery:, delivery_attempt:, error_class:)
107
+ emit(
108
+ :error,
109
+ "messaging.execution.attempt.finalize_unpersisted",
110
+ channel_delivery:,
111
+ delivery_attempt_id: delivery_attempt.id,
112
+ error_class:,
113
+ )
114
+ end
115
+
106
116
  def render_started(channel_delivery:, delivery_attempt:)
107
117
  emit_render(
108
118
  :info,