Skip to main content

aion_server/worker/
bridge.rs

1//! NIF bridge dispatcher that routes `run_activity` calls to connected workers.
2//!
3//! `WorkerActivityDispatcher` implements `aion::ActivityDispatcher` so the
4//! engine's activity NIFs can synchronously dispatch to a remote worker and
5//! block until the result comes back.
6//!
7//! # Threading contract
8//!
9//! The engine invokes [`aion::ActivityDispatcher::dispatch`] from two kinds of
10//! threads: beamr scheduler threads (concurrency combinators) and spawned
11//! tokio tasks (the two-phase `dispatch_activity` completion task). The task
12//! send uses `try_send()` (non-blocking channel push) and the response wait
13//! blocks on `std::sync::mpsc::Receiver::recv`.
14//!
15//! Blocking is harmless on a beamr thread, but on a tokio runtime worker it
16//! must be wrapped in `tokio::task::block_in_place`: the `try_send` wakes the
17//! per-worker gRPC stream forwarder task, and tokio schedules a task woken
18//! from task context into the *current* worker's LIFO slot, which no other
19//! runtime worker can steal. Without the `block_in_place` core handoff the
20//! forwarder sits trapped in that slot while this thread blocks, so the queued
21//! `ActivityTask` is never flushed to the worker even though the worker is
22//! healthy. `block_in_place` moves the worker's scheduler core (LIFO slot
23//! included) to another thread before the wait begins, so dispatch-to-delivery
24//! stays in the millisecond range and the runtime keeps full parallelism.
25//!
26//! # Wait termination
27//!
28//! The engine imposes no activity timeout of its own: agent-style activities
29//! legitimately run for over an hour, so the completion wait is unbounded.
30//! The blocking `recv` terminates on exactly one of:
31//!
32//! - **Completion** — the worker reports a result and the stream handler
33//!   delivers it through [`ActivityCompletionSink::complete_activity`].
34//! - **Worker loss** — the worker's gRPC stream ends (process death,
35//!   disconnect, expired token); the stream teardown sweeps the worker's
36//!   in-flight tasks through the same sink as every other TRANSPORT loss
37//!   ([`HeartbeatTracker::fail_disconnected_worker`]). A liminal-delivered
38//!   worker's loss is observed by its reply router thread instead (the
39//!   correlated-reply awaiter wakes the moment the connection closes) and
40//!   resolves the dispatch with the same transport-loss class. Both are
41//!   classified by [`transport_loss`](crate::worker::transport_loss): `lost:`
42//!   while the transport still has budget to deliver the activity (the engine
43//!   re-dispatches the SAME attempt, recording nothing), `transport-exhausted:`
44//!   once that budget is spent. Neither ever wears the action's retry
45//!   vocabulary — the activity never ran.
46//! - **Graceful-drain park (#207)** — during a drain, a worker stream ending
47//!   (or the drain-timeout backstop) PARKS the worker's in-flight tasks
48//!   through [`ActivityCompletionSink::park_activity`]: the waiter resolves
49//!   with the ephemeral parked sentinel
50//!   ([`aion::PARKED_ACTIVITY_REASON`]), nothing is recorded or delivered,
51//!   and restart recovery re-dispatches the dangling ordinal — kill -9
52//!   convergence.
53//! - **Drain timeout at shutdown** — the shutdown coordinator parks all
54//!   remaining in-flight tasks through the sink
55//!   (`HeartbeatTracker::park_all_in_flight_workers`).
56//! - **Channel teardown** — every sender for the pending entry is dropped
57//!   (a cleanup path removed the entry without completing it); surfaced as
58//!   a channel-closed dispatch error, never a hang.
59//!
60//! An activity's duration is bounded only by the workflow's own
61//! `timeout_seconds` and by worker liveness — never by an engine constant.
62
63use std::collections::BTreeMap;
64use std::sync::{Arc, OnceLock};
65use std::time::{Duration, Instant};
66
67use aion::{ActivityDispatch, ActivityDispatcher};
68use aion_core::{ActivityId, ContentType, Payload, RunId, WorkflowId};
69use aion_proto::{ProtoActivityId, ProtoActivityTask, ProtoPayload, ProtoWorkflowId};
70use dashmap::DashMap;
71
72use super::dispatch::{ActivityCompletion, ActivityCompletionOutcome, ActivityCompletionSink};
73use super::envelope::{CompletionFences, CompletionToken, idempotency_key};
74use super::heartbeat::{HeartbeatTracker, InFlightActivity};
75use super::queue_service::{
76    DeliveryRefusal, ExpiredClock, PARK_POLL_INTERVAL, PoolCensus, QueueDeclarationSource,
77    QueueServiceConfig, QueueServiceReason, QueueServiceState, SelectionRefusal, ServiceAddress,
78    ServiceWait, WorkerUnavailable, deliver_within_schedule_to_start, select_worker_or_refuse,
79};
80use super::registry::{
81    ConnectedWorkerRegistry, WorkerDelivery, WorkerHandle, WorkerId, WorkerMessage,
82};
83use crate::error::ServerError;
84use crate::shutdown::DrainState;
85use tracing::info_span;
86
87type SyncSender = std::sync::mpsc::SyncSender<Result<String, String>>;
88type SyncReceiver = std::sync::mpsc::Receiver<Result<String, String>>;
89
90/// Execution-scoped key for an in-flight activity dispatch.
91///
92/// The engine seam ([`ActivityDispatch`]) carries the *real* workflow id and
93/// the *real* per-workflow activity ordinal recorded in history, so this pair
94/// uniquely and stably identifies one execution. Keying by bare [`ActivityId`]
95/// would be unsafe across server restarts — a stale result re-reported from a
96/// worker's previous session could complete a *different* post-restart
97/// dispatch reusing the same ordinal — but pairing it with the real workflow
98/// id closes that race: two different workflow executions never share a
99/// workflow id, so a stale `(workflow_id, activity_id)` from a previous server
100/// life can only ever match the exact execution it belongs to.
101///
102/// The wire (`ActivityResult`) carries both ids, plus an attempt discriminator
103/// (`ActivityTask.attempt`). The pending key stays attempt-free for now: a
104/// retry re-dispatches under the same `(workflow_id, activity_id)` and the
105/// outstanding entry is the one awaiting completion. Redelivery bookkeeping
106/// can widen this key with the wire attempt later — no protocol change needed.
107type PendingActivityKey = (WorkflowId, ActivityId);
108
109/// Routes an unmatched durable-outbox completion into the live workflow.
110///
111/// When the outbox is ON a worker completion can arrive at the sink with no
112/// pending oneshot (the dispatch was non-blocking fan-out, or the original
113/// waiter was lost). Rather than dropping it, [`PendingActivities::complete`]
114/// hands it to this callback, which resolves the workflow to its live engine
115/// process and delivers the terminal into its mailbox. The callback is only
116/// installed when the outbox is enabled, so flag-off the unmatched branch
117/// stays a silent drop.
118pub trait OutboxDeliveryCallback: Send + Sync {
119    /// Deliver a successful completion to the live workflow.
120    ///
121    /// Returns `Ok(true)` when delivered to a live workflow and `Ok(false)`
122    /// when no run is currently live (the expected stale-completion case that
123    /// recovery re-arms).
124    ///
125    /// # Errors
126    ///
127    /// Returns [`ServerError`] when the engine rejects the delivery.
128    fn deliver_completion(
129        &self,
130        workflow_id: &WorkflowId,
131        activity_id: &ActivityId,
132        run_id: Option<&RunId>,
133        result: String,
134    ) -> Result<bool, ServerError>;
135
136    /// Deliver a failure to the live workflow. Same `bool`/error contract as
137    /// [`Self::deliver_completion`].
138    ///
139    /// # Errors
140    ///
141    /// Returns [`ServerError`] when the engine rejects the delivery.
142    fn deliver_failure(
143        &self,
144        workflow_id: &WorkflowId,
145        activity_id: &ActivityId,
146        run_id: Option<&RunId>,
147        reason: String,
148    ) -> Result<bool, ServerError>;
149}
150
151/// Tracks in-flight activity dispatches waiting for worker results.
152///
153/// When the server's worker stream handler receives an `ActivityResult`, it
154/// calls [`complete_activity`](ActivityCompletionSink::complete_activity) to
155/// deliver the result to the blocked NIF thread. Entries are keyed by
156/// [`PendingActivityKey`] so a stale result from a previous server life can
157/// never be matched to a different execution (#59).
158///
159/// Clones share both the pending map and the outbox-delivery callback through
160/// `Arc`, so [`set_outbox_delivery`](Self::set_outbox_delivery) called once on
161/// any clone after construction is visible to the clone the dispatcher holds.
162#[derive(Clone, Default)]
163pub struct PendingActivities {
164    pending: Arc<DashMap<PendingActivityKey, SyncSender>>,
165    completion_fences: CompletionFences,
166    outbox_delivery: Arc<OnceLock<Arc<dyn OutboxDeliveryCallback>>>,
167    /// The transport's OWN re-dispatch budget for activities whose worker died
168    /// before reporting (see [`transport_loss`](crate::worker::transport_loss)).
169    /// Shared across clones, so every loss for one execution site lands in one
170    /// budget. Zero-budget by default (a wiring that was never told the
171    /// operator's heartbeat window must not invent one).
172    transport_losses: super::transport_loss::TransportLossLedger,
173}
174
175impl std::fmt::Debug for PendingActivities {
176    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
177        formatter
178            .debug_struct("PendingActivities")
179            .field("pending", &self.pending.len())
180            .field("completion_fences", &self.completion_fences)
181            .field(
182                "outbox_delivery_installed",
183                &self.outbox_delivery.get().is_some(),
184            )
185            .field("transport_losses", &self.transport_losses)
186            .finish()
187    }
188}
189
190impl PendingActivities {
191    fn insert(
192        &self,
193        workflow_id: WorkflowId,
194        activity_id: ActivityId,
195    ) -> Result<(CompletionToken, SyncReceiver), ServerError> {
196        let completion_token = self.completion_fences.issue(&workflow_id, &activity_id)?;
197        let (tx, rx) = std::sync::mpsc::sync_channel(1);
198        self.pending.insert((workflow_id, activity_id), tx);
199        Ok((completion_token, rx))
200    }
201
202    /// Share the generation registry with non-blocking outbox dispatch.
203    #[must_use]
204    pub fn completion_fences(&self) -> CompletionFences {
205        self.completion_fences.clone()
206    }
207
208    /// Test seam: register a pending waiter exactly as a live dispatch does,
209    /// so crate-internal tests outside this module (the stream-teardown
210    /// park-vs-fail pins) can observe how a sweep resolves it.
211    #[cfg(test)]
212    pub(crate) fn insert_for_test(
213        &self,
214        workflow_id: WorkflowId,
215        activity_id: ActivityId,
216    ) -> Result<(CompletionToken, SyncReceiver), ServerError> {
217        self.insert(workflow_id, activity_id)
218    }
219
220    /// Install the unmatched-completion delivery callback (idempotent).
221    ///
222    /// Set once, after construction, when the durable outbox is enabled. A
223    /// second set is ignored and logged: the callback is process-wide and must
224    /// not silently change identity.
225    pub fn set_outbox_delivery(&self, callback: Arc<dyn OutboxDeliveryCallback>) {
226        if self.outbox_delivery.set(callback).is_err() {
227            tracing::warn!("outbox delivery callback already installed; ignoring duplicate set");
228        }
229    }
230
231    /// Complete a pending dispatch, or route an unmatched completion to the
232    /// outbox delivery callback when one is installed.
233    ///
234    /// A matched entry delivers to its waiting oneshot exactly as before. An
235    /// unmatched completion is dropped silently when no callback is installed
236    /// (outbox OFF — byte-identical to the prior behaviour); with a callback
237    /// installed (outbox ON) it is routed into the live workflow's mailbox.
238    fn complete(
239        &self,
240        workflow_id: &WorkflowId,
241        activity_id: &ActivityId,
242        run_id: Option<&RunId>,
243        result: Result<String, String>,
244    ) -> bool {
245        // Take and drop the DashMap guard before any callback runs: the engine
246        // delivery the callback invokes must never execute under a shard lock.
247        let matched = self
248            .pending
249            .remove(&(workflow_id.clone(), activity_id.clone()));
250        if let Some((_, sender)) = matched {
251            return sender.send(result).is_ok();
252        }
253        let Some(callback) = self.outbox_delivery.get() else {
254            // Outbox OFF: silent drop, byte-identical to the prior behaviour.
255            return false;
256        };
257        let outcome = match result {
258            Ok(payload) => callback.deliver_completion(workflow_id, activity_id, run_id, payload),
259            Err(reason) => callback.deliver_failure(workflow_id, activity_id, run_id, reason),
260        };
261        match outcome {
262            Ok(true) => true,
263            Ok(false) => {
264                // Not live: the expected stale-completion case recovery re-arms.
265                tracing::debug!(
266                    workflow_id = %workflow_id,
267                    activity_id = %activity_id,
268                    "unmatched outbox completion for a workflow that is not currently live; \
269                     recovery will re-arm it"
270                );
271                false
272            }
273            Err(error) => {
274                tracing::warn!(
275                    workflow_id = %workflow_id,
276                    activity_id = %activity_id,
277                    %error,
278                    "failed to deliver unmatched outbox completion to the live workflow"
279                );
280                false
281            }
282        }
283    }
284
285    /// Atomically consume `completion_token` and only then resolve its waiter or
286    /// durable-outbox callback.
287    fn complete_fenced(
288        &self,
289        workflow_id: &WorkflowId,
290        activity_id: &ActivityId,
291        run_id: Option<&RunId>,
292        completion_token: &CompletionToken,
293        result: Result<String, String>,
294    ) -> Result<bool, ServerError> {
295        self.completion_fences
296            .accept(workflow_id, activity_id, completion_token)
297            .inspect_err(|error| {
298                tracing::warn!(
299                    workflow_id = %workflow_id,
300                    activity_id = %activity_id,
301                    %error,
302                    "activity completion rejected by execution-generation fence"
303                );
304            })?;
305        // A resolution OUTSIDE the transport domain — a real result, or a real
306        // action failure — retires this execution site's transport-loss budget,
307        // so a run that survived one blip does not carry it forward. A
308        // transport-domain resolution deliberately does not: the running budget
309        // is exactly what bounds a flapping link.
310        let transport_domain = result
311            .as_ref()
312            .err()
313            .is_some_and(|reason| super::transport_loss::is_transport_domain_reason(reason));
314        if !transport_domain {
315            if let Err(error) = self.transport_losses.clear(workflow_id, activity_id) {
316                tracing::warn!(
317                    workflow_id = %workflow_id,
318                    activity_id = %activity_id,
319                    %error,
320                    "failed to retire the transport-loss budget for a resolved activity"
321                );
322            }
323        }
324        Ok(self.complete(workflow_id, activity_id, run_id, result))
325    }
326
327    /// Install the operator's heartbeat window so the transport-loss ledger
328    /// derives its budget from it (see
329    /// [`transport_loss`](crate::worker::transport_loss)).
330    ///
331    /// Called once at boot on the shared instance; clones made afterwards
332    /// observe it because the ledger's state is shared, and the builder replaces
333    /// the whole ledger before any dispatch runs.
334    #[must_use]
335    pub fn with_heartbeat_window(mut self, heartbeat_window: std::time::Duration) -> Self {
336        self.transport_losses = super::transport_loss::TransportLossLedger::new(heartbeat_window);
337        self
338    }
339
340    /// The transport-loss ledger this sink classifies worker deaths through.
341    #[must_use]
342    pub const fn transport_losses(&self) -> &super::transport_loss::TransportLossLedger {
343        &self.transport_losses
344    }
345
346    /// Classify one worker loss for `(workflow_id, activity_id)` into the
347    /// transport-domain reason the engine seam consumes.
348    ///
349    /// A ledger failure (poisoned lock) is reported as transport exhaustion
350    /// rather than as a re-dispatchable loss: with no trustworthy budget the
351    /// only safe answer is the one that terminates, because an unbounded
352    /// re-dispatch is the failure mode the budget exists to prevent.
353    fn classify_worker_loss(
354        &self,
355        workflow_id: &WorkflowId,
356        activity_id: &ActivityId,
357        worker_id: crate::worker::registry::WorkerId,
358    ) -> String {
359        let detail = super::transport_loss::worker_lost_detail(worker_id);
360        match self
361            .transport_losses
362            .record_loss(workflow_id, activity_id, &detail)
363        {
364            Ok(verdict) => {
365                if verdict.exhausted {
366                    tracing::error!(
367                        operation = "activity_complete",
368                        workflow_id = %workflow_id,
369                        activity_id = %activity_id,
370                        worker_id = ?worker_id,
371                        error_type = "TransportExhausted",
372                        losses = verdict.losses,
373                        budget_ms = self.transport_losses.budget().as_millis(),
374                        "activity abandoned: the transport kept losing its worker past the \
375                         transport-loss budget"
376                    );
377                } else {
378                    tracing::warn!(
379                        operation = "activity_complete",
380                        workflow_id = %workflow_id,
381                        activity_id = %activity_id,
382                        worker_id = ?worker_id,
383                        error_type = "WorkerLost",
384                        losses = verdict.losses,
385                        budget_ms = self.transport_losses.budget().as_millis(),
386                        "worker lost before reporting an activity result; the activity never ran \
387                         and will be re-dispatched attempt-neutrally"
388                    );
389                }
390                verdict.reason
391            }
392            Err(error) => {
393                tracing::error!(
394                    workflow_id = %workflow_id,
395                    activity_id = %activity_id,
396                    %error,
397                    "transport-loss ledger is unreadable; abandoning the activity rather than \
398                     re-dispatching it without a budget"
399                );
400                format!(
401                    "{}{detail} (transport-loss budget unreadable: {error})",
402                    super::transport_loss::TRANSPORT_EXHAUSTED_REASON_PREFIX
403                )
404            }
405        }
406    }
407}
408
409impl ActivityCompletionSink for PendingActivities {
410    /// Park one in-flight dispatch for restart recovery (#207): resolve the
411    /// matched waiter with the ephemeral parked sentinel, and nothing else.
412    ///
413    /// This resolution is MANDATORY, not an optimization: the default
414    /// `ActivityDispatcher::dispatch_async` runs the dispatcher's blocking
415    /// `std::sync::mpsc::recv()` on tokio's blocking pool, and tokio `Runtime`
416    /// drop joins blocking threads — an unresolved waiter would wedge process
417    /// exit indefinitely. An unmatched dispatch (already resolved by another
418    /// path) is a no-op: a park is NEVER routed to the outbox delivery
419    /// callback, because it is not a failure and must never reach a workflow.
420    fn park_activity(
421        &self,
422        workflow_id: &WorkflowId,
423        activity_id: &ActivityId,
424    ) -> Result<(), ServerError> {
425        self.completion_fences
426            .revoke_current(workflow_id, activity_id)?;
427        let matched = self
428            .pending
429            .remove(&(workflow_id.clone(), activity_id.clone()));
430        if let Some((_, sender)) = matched {
431            // A send failure means the waiter side already dropped its
432            // receiver (the dispatch is being cleaned up concurrently) —
433            // benign: there is no thread left to unblock.
434            let _ = sender.send(Err(aion::PARKED_ACTIVITY_REASON.to_owned()));
435        }
436        Ok(())
437    }
438
439    fn complete_activity(&self, completion: ActivityCompletion) -> Result<(), ServerError> {
440        let result = match completion.outcome {
441            ActivityCompletionOutcome::Succeeded(payload) => {
442                payload_to_string(&payload).map_err(|reason| {
443                    tracing::error!(
444                        operation = "activity_complete",
445                        workflow_id = %completion.workflow_id,
446                        activity_id = %completion.activity_id,
447                        error_type = "ActivityResultDecode",
448                        %reason,
449                        "activity completion failed"
450                    );
451                    ServerError::worker_dispatch("", "", format!("payload decode: {reason}"))
452                })?
453            }
454            ActivityCompletionOutcome::Failed(error) => {
455                let prefix = if error.is_retryable() {
456                    "retryable"
457                } else {
458                    "terminal"
459                };
460                tracing::error!(
461                    operation = "activity_complete",
462                    workflow_id = %completion.workflow_id,
463                    activity_id = %completion.activity_id,
464                    error_type = "ActivityFailed",
465                    error_kind = prefix,
466                    reason = %error.message,
467                    "activity completion failed"
468                );
469                Err(format!("{prefix}:{}", error.message))
470            }
471            // A TRANSPORT-domain loss: the activity never executed to a result,
472            // so it is classified by the transport's own ledger (re-dispatchable
473            // while its budget holds, transport-exhausted once it is spent) and
474            // NEVER by the action's retry vocabulary.
475            ActivityCompletionOutcome::WorkerLost { worker_id } => Err(self.classify_worker_loss(
476                &completion.workflow_id,
477                &completion.activity_id,
478                worker_id,
479            )),
480        };
481        self.complete_fenced(
482            &completion.workflow_id,
483            &completion.activity_id,
484            completion.run_id.as_ref(),
485            &completion.completion_token,
486            result,
487        )?;
488        Ok(())
489    }
490}
491
492fn payload_to_string(payload: &Payload) -> Result<Result<String, String>, String> {
493    match payload.content_type() {
494        ContentType::Json => String::from_utf8(payload.bytes().to_vec())
495            .map(Ok)
496            .map_err(|_| "activity result payload is not valid UTF-8".to_owned()),
497    }
498}
499
500/// Dispatcher that routes `run_activity` NIF calls to connected workers.
501///
502/// Synchronous interface — uses `try_send` for the task channel and
503/// `std::sync::mpsc::Receiver::recv` for the response. Callers on a
504/// multi-thread tokio runtime are detected and moved into
505/// `tokio::task::block_in_place` so the blocking wait never starves the
506/// runtime tasks that flush the worker stream (see the module docs).
507pub struct WorkerActivityDispatcher {
508    registry: ConnectedWorkerRegistry,
509    namespace: String,
510    pending: PendingActivities,
511    heartbeat_tracker: HeartbeatTracker,
512    drain_state: DrainState,
513    tokio_handle: Option<tokio::runtime::Handle>,
514    /// NOI-6 attempt→owner back-index. When installed, each liminal-delivered
515    /// dispatch binds its `(workflow, activity, attempt)` to the owning worker
516    /// for the dispatch's lifetime, so the intervention router (and the ops
517    /// console's live-attempts enumeration) can see and target it. `None`
518    /// (isolated tests) binds nothing.
519    attempt_owners: Option<super::intervention::AttemptOwnerIndex>,
520    /// R1 service policies and the two service clocks. Default: `strict` with
521    /// no clocks — refuse the structurally unservable, wait (loudly) for
522    /// everything else, and invent no deadline the operator never wrote.
523    queue_service: QueueServiceConfig,
524    /// Deployed queue declarations, installed once the engine exists. Answers
525    /// `Unknown` until then, and `Unknown` never refuses anything.
526    queue_declarations: QueueDeclarationSource,
527    /// Live unserved-queue state a parked dispatch publishes itself into.
528    queue_state: QueueServiceState,
529}
530
531impl std::fmt::Debug for WorkerActivityDispatcher {
532    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
533        f.debug_struct("WorkerActivityDispatcher")
534            .field("namespace", &self.namespace)
535            .finish_non_exhaustive()
536    }
537}
538
539impl WorkerActivityDispatcher {
540    /// Build a dispatcher for the given namespace, worker registry, and
541    /// liveness tracker.
542    ///
543    /// The tracker must be the same instance the worker stream handler and
544    /// shutdown coordinator share: the unbounded completion wait relies on
545    /// stream teardown sweeping this tracker's in-flight entries to fail
546    /// dispatches whose worker was lost.
547    #[must_use]
548    pub fn new(
549        registry: ConnectedWorkerRegistry,
550        namespace: impl Into<String>,
551        heartbeat_tracker: HeartbeatTracker,
552    ) -> Self {
553        Self {
554            registry,
555            namespace: namespace.into(),
556            pending: PendingActivities::default(),
557            heartbeat_tracker,
558            drain_state: DrainState::default(),
559            tokio_handle: None,
560            attempt_owners: None,
561            queue_service: QueueServiceConfig::default(),
562            queue_declarations: QueueDeclarationSource::default(),
563            queue_state: QueueServiceState::default(),
564        }
565    }
566
567    /// Share the operator's R1 queue-service settings: the default policy, the
568    /// written per-queue `durable_pending` opt-ins, and the two clocks.
569    #[must_use]
570    pub fn with_queue_service(mut self, queue_service: QueueServiceConfig) -> Self {
571        self.queue_service = queue_service;
572        self
573    }
574
575    /// Share the queue-declaration source the boot path fills in once the
576    /// engine exists (the handle is cloneable; installing on any clone is
577    /// visible here).
578    #[must_use]
579    pub fn with_queue_declarations(mut self, queue_declarations: QueueDeclarationSource) -> Self {
580        self.queue_declarations = queue_declarations;
581        self
582    }
583
584    /// Share the queue-service state so the server can read which addresses are
585    /// unserved and which runs are parked on them.
586    #[must_use]
587    pub fn with_queue_state(mut self, queue_state: QueueServiceState) -> Self {
588        self.queue_state = queue_state;
589        self
590    }
591
592    /// Share the server's NOI-6 attempt→owner back-index so liminal-delivered
593    /// dispatches are visible (and targetable) to the intervention router for
594    /// exactly as long as they are in flight. The production boot passes
595    /// `ServerState`'s index — the SAME instance `intervenable_attempts` and
596    /// `intervene` read — or the console's live-attempts list stays empty for
597    /// every bridge-dispatched agent step.
598    #[must_use]
599    pub fn with_attempt_owners(
600        mut self,
601        attempt_owners: super::intervention::AttemptOwnerIndex,
602    ) -> Self {
603        self.attempt_owners = Some(attempt_owners);
604        self
605    }
606
607    /// Share a caller-supplied pending-activities tracker.
608    #[must_use]
609    pub fn with_pending(mut self, pending: PendingActivities) -> Self {
610        self.pending = pending;
611        self
612    }
613
614    /// Share the server drain gate.
615    #[must_use]
616    pub fn with_drain_state(mut self, drain_state: DrainState) -> Self {
617        self.drain_state = drain_state;
618        self
619    }
620
621    /// Share the server runtime handle for sync history writes from dirty NIF threads.
622    #[must_use]
623    pub fn with_tokio_handle(mut self, tokio_handle: tokio::runtime::Handle) -> Self {
624        self.tokio_handle = Some(tokio_handle);
625        self
626    }
627}
628
629impl WorkerActivityDispatcher {
630    /// The drain gate for one dispatch, and the single place a draining server's
631    /// refusal is turned into a reason the engine acts on.
632    ///
633    /// The reason handed back is the #207 parked sentinel
634    /// ([`aion::PARKED_ACTIVITY_REASON`]), NOT the gate's own message. A
635    /// dispatch the drain refused never reached a worker and never ran, which is
636    /// precisely the state that sentinel was minted for: record nothing, deliver
637    /// nothing, and let post-restart replay re-dispatch the dangling ordinal —
638    /// the kill -9 convergence `shutdown::ShutdownOutcome::Parked` documents.
639    ///
640    /// Both alternatives are wrong here, and neither is a style preference:
641    ///
642    /// - the *unprefixed* gate message this used to return is classified as a
643    ///   terminal action failure, so a routine deploy records `ActivityFailed`
644    ///   (and, with no authored retry, `WorkflowFailed`) for work nobody
645    ///   attempted — the synthesized failure #207 exists to abolish;
646    /// - the TRANSPORT-loss class (`lost:`) is attempt-neutral but re-dispatches
647    ///   the SAME attempt live and in-process, which on a draining server means
648    ///   refuse → re-dispatch → refuse, spinning against the very gate that
649    ///   refused it while the process is trying to exit.
650    ///
651    /// The operator-facing detail is not lost: the gate's own message is what is
652    /// logged here. Only the engine-facing classification is the sentinel.
653    fn ensure_accepting(
654        &self,
655        namespace: &str,
656        activity_type: &str,
657        workflow_id: &WorkflowId,
658        activity_id: &ActivityId,
659        worker_id: Option<WorkerId>,
660    ) -> Result<(), String> {
661        self.drain_state
662            .ensure_accepting(namespace, activity_type)
663            .map_err(|error| {
664                log_worker_error(
665                    "WorkerDispatch",
666                    namespace,
667                    activity_type,
668                    workflow_id,
669                    activity_id,
670                    worker_id,
671                    &error.to_string(),
672                );
673                aion::PARKED_ACTIVITY_REASON.to_owned()
674            })
675    }
676
677    /// Select a worker for the address, or refuse with a typed R1 reason.
678    ///
679    /// The wait itself lives in [`super::queue_service::wait`]: every selection
680    /// miss is classified against the deployed contract records and the live
681    /// poller census, published to the queue-service state, and stated at WARN.
682    /// This method owns only the two things the seam cannot delegate — the
683    /// drain gate and the runtime plumbing of one bounded park.
684    fn select_worker_or_wait(
685        &self,
686        address: &ServiceAddress,
687        workflow_id: &WorkflowId,
688        activity_id: &ActivityId,
689    ) -> Result<WorkerHandle, String> {
690        let wait = ServiceWait {
691            registry: &self.registry,
692            declarations: &self.queue_declarations,
693            config: &self.queue_service,
694            state: &self.queue_state,
695            address,
696            workflow_id,
697            activity_id,
698        };
699        let mut accepting = || {
700            self.ensure_accepting(
701                &address.namespace,
702                &address.activity_type,
703                workflow_id,
704                activity_id,
705                None,
706            )
707        };
708        let mut park = |budget: Option<Duration>| self.park_for_worker(budget);
709        select_worker_or_refuse(&wait, &mut accepting, &mut park).map_err(|refusal| {
710            let reason = refusal.reason_string();
711            // The drain gate already logged its own refusal at its own site;
712            // logging it twice would double-count the incident.
713            if !matches!(refusal, SelectionRefusal::NotAccepting { .. }) {
714                let error_type = match refusal {
715                    SelectionRefusal::Unavailable(_) => "WorkerUnavailable",
716                    _ => "WorkerRegistry",
717                };
718                log_worker_error(
719                    error_type,
720                    &address.namespace,
721                    &address.activity_type,
722                    workflow_id,
723                    activity_id,
724                    None,
725                    &reason,
726                );
727            }
728            reason
729        })
730    }
731
732    /// Wait once for a worker arrival — bounded by `budget` when one is given,
733    /// and bounded in BOTH arms by the server's own shutdown.
734    ///
735    /// The arrival wait races the drain latch, and that race is the whole point.
736    /// Without it a `None` budget — the default, because no schedule-to-start
737    /// deadline is configured unless an operator writes one — parked this
738    /// dispatch on a `spawn_blocking` thread with nothing left that could ever
739    /// wake it: the drain gate is consulted once per selection iteration, and
740    /// the iteration was stuck inside this wait. Tokio's runtime `Drop` joins
741    /// the blocking pool, so `aion server` could not exit even after `main`
742    /// returned an exit code — it kept the store's writer lock and blocked the
743    /// next deploy into the same data directory. The captured stack of a real
744    /// one is `docs/evidence/samples/aion-server-hang-after-drain-15202.sample`.
745    /// The `Some(budget)` arm carried the same defect at the scale of the
746    /// budget: exit was delayed by up to the whole availability deadline.
747    ///
748    /// Racing the latch is the fix rather than capping the wait, because the
749    /// wait is legitimately unbounded while the server runs: a queue no worker
750    /// serves yet is a fleet condition an operator resolves by starting a
751    /// worker, and a deadline invented here would refuse work nobody asked to
752    /// have refused.
753    ///
754    /// Waking on the latch decides nothing by itself. The selection loop
755    /// re-selects, misses, and consults the SAME drain gate it always
756    /// consulted — [`Self::ensure_accepting`] — which is where the refusal and
757    /// its classification are produced. This seam gains no second opinion about
758    /// draining; it only stops being unwakeable.
759    fn park_for_worker(&self, budget: Option<Duration>) {
760        let handle = self
761            .tokio_handle
762            .clone()
763            .or_else(|| tokio::runtime::Handle::try_current().ok());
764        let Some(handle) = handle else {
765            // No runtime in reach (a plain OS thread in an isolated test):
766            // sleep the seam's park interval, clamped to the remaining budget.
767            // Bounded by construction, so the loop re-consults the drain gate
768            // within one interval and needs no signal to race.
769            std::thread::sleep(
770                budget.map_or(PARK_POLL_INTERVAL, |budget| budget.min(PARK_POLL_INTERVAL)),
771            );
772            return;
773        };
774        handle.block_on(async {
775            let arrival = async {
776                tokio::select! {
777                    () = self.registry.wait_for_worker() => {}
778                    () = self.drain_state.wait_for_drain() => {}
779                }
780            };
781            match budget {
782                None => arrival.await,
783                Some(budget) => {
784                    // Timeout is not failure here: the caller re-classifies and
785                    // decides whether the clock has run out.
786                    drop(tokio::time::timeout(budget, arrival).await);
787                }
788            }
789        });
790    }
791
792    fn track_worker_task(
793        &self,
794        worker_id: WorkerId,
795        activity_type: &str,
796        workflow_id: &WorkflowId,
797        activity_id: &ActivityId,
798        completion_token: CompletionToken,
799    ) -> Result<(), String> {
800        self.heartbeat_tracker
801            .track_task(
802                worker_id,
803                InFlightActivity {
804                    workflow_id: workflow_id.clone(),
805                    activity_id: activity_id.clone(),
806                    completion_token,
807                },
808                Instant::now(),
809            )
810            .map_err(|error| {
811                let reason = error.to_string();
812                log_worker_error(
813                    "WorkerHeartbeatTracker",
814                    &self.namespace,
815                    activity_type,
816                    workflow_id,
817                    activity_id,
818                    Some(worker_id),
819                    &reason,
820                );
821                reason
822            })
823    }
824
825    fn cleanup_activity(
826        &self,
827        worker_id: WorkerId,
828        workflow_id: &WorkflowId,
829        activity_id: &ActivityId,
830        completion_token: &CompletionToken,
831    ) {
832        self.pending
833            .pending
834            .remove(&(workflow_id.clone(), activity_id.clone()));
835        if let Err(error) =
836            self.pending
837                .completion_fences
838                .revoke(workflow_id, activity_id, completion_token)
839        {
840            tracing::error!(
841                workflow_id = %workflow_id,
842                activity_id = %activity_id,
843                %error,
844                "failed to revoke undelivered activity generation"
845            );
846        }
847        let _ = self
848            .heartbeat_tracker
849            .complete_task(worker_id, workflow_id, activity_id);
850        self.drain_state.notify_activity_drained();
851    }
852
853    /// Deliver one dispatched task to the selected worker over ITS transport.
854    ///
855    /// The bridge is transport-agnostic at this seam: selection
856    /// ([`Self::select_worker_or_wait`]) already treats every registry member
857    /// identically, and this match delivers on whichever [`WorkerDelivery`] leg
858    /// the worker registered with — the gRPC stream `mpsc` push, or the liminal
859    /// server-push on the worker's existing connection. Both legs resolve
860    /// through the SAME pending map, so `await_activity_result` is oblivious to
861    /// the transport.
862    fn send_activity_task(
863        &self,
864        worker: &WorkerHandle,
865        task: ProtoActivityTask,
866        address: &ServiceAddress,
867        workflow_id: &WorkflowId,
868        activity_id: &ActivityId,
869        completion_token: &CompletionToken,
870    ) -> Result<(), String> {
871        match worker.delivery() {
872            WorkerDelivery::Grpc(sender) => {
873                let worker_id = worker.id();
874                let mut accepting = || {
875                    self.ensure_accepting(
876                        &address.namespace,
877                        &address.activity_type,
878                        workflow_id,
879                        activity_id,
880                        Some(worker_id),
881                    )
882                };
883                let handed_over = deliver_within_schedule_to_start(
884                    sender,
885                    WorkerMessage::ActivityTask(Box::new(task)),
886                    self.queue_service.schedule_to_start_timeout,
887                    &mut accepting,
888                );
889                let Err(refusal) = handed_over else {
890                    return Ok(());
891                };
892                self.cleanup_activity(worker_id, workflow_id, activity_id, completion_token);
893                let (error_type, reason) = self.hand_off_failure(&refusal, address);
894                if !matches!(refusal, DeliveryRefusal::NotAccepting { .. }) {
895                    log_worker_error(
896                        error_type,
897                        &address.namespace,
898                        &address.activity_type,
899                        workflow_id,
900                        activity_id,
901                        Some(worker_id),
902                        &reason,
903                    );
904                }
905                Err(reason)
906            }
907            // The liminal leg has no bounded intake queue to saturate: the push
908            // either enqueues on the worker's live connection or fails
909            // synchronously because that connection is already gone. There is
910            // therefore no schedule-to-start window to apply here, and reporting
911            // `SATURATED` for a push failure would name a condition that did not
912            // happen.
913            #[cfg(feature = "liminal-transport")]
914            WorkerDelivery::Liminal(delivery) => self.send_liminal_activity_task(
915                worker.id(),
916                delivery,
917                task,
918                &address.activity_type,
919                workflow_id,
920                activity_id,
921            ),
922        }
923    }
924
925    /// Render one hand-off refusal into its log class and failure reason.
926    ///
927    /// `SATURATED` is the only one that becomes a typed [`WorkerUnavailable`]:
928    /// a compatible worker WAS live and would not take the task inside the
929    /// schedule-to-start clock. A full intake with no clock configured, and a
930    /// closed transport, keep the failure they always had.
931    fn hand_off_failure(
932        &self,
933        refusal: &DeliveryRefusal,
934        address: &ServiceAddress,
935    ) -> (&'static str, String) {
936        match refusal {
937            DeliveryRefusal::Saturated { waited } => {
938                // A census failure must not swallow the refusal: report the
939                // saturation with an empty census and say why it is empty.
940                let census = self
941                    .registry
942                    .pool_census(
943                        &address.namespace,
944                        &address.task_queue,
945                        &address.activity_type,
946                        address.node.as_deref(),
947                    )
948                    .unwrap_or_else(|error| {
949                        tracing::error!(
950                            namespace = %address.namespace,
951                            task_queue = %address.task_queue,
952                            activity_type = %address.activity_type,
953                            %error,
954                            "poller census failed while reporting a saturated queue; \
955                             the refusal carries an empty census"
956                        );
957                        PoolCensus::default()
958                    });
959                let unavailable = WorkerUnavailable {
960                    reason: QueueServiceReason::Saturated,
961                    clock: Some(ExpiredClock::ScheduleToStart),
962                    waited: *waited,
963                    address: address.clone(),
964                    census,
965                };
966                ("WorkerUnavailable", unavailable.reason_string())
967            }
968            DeliveryRefusal::Full => (
969                "WorkerChannelClosed",
970                "worker task channel full or closed: no available capacity".to_owned(),
971            ),
972            DeliveryRefusal::Closed => (
973                "WorkerChannelClosed",
974                "worker task channel full or closed: channel closed".to_owned(),
975            ),
976            DeliveryRefusal::NotAccepting { reason } => ("WorkerDispatch", reason.clone()),
977        }
978    }
979
980    /// Deliver one dispatched task to a liminal-connected worker: push the SAME
981    /// wire frame the outbox liminal path pushes (a
982    /// [`DispatchRequest`](super::liminal_transport::DispatchRequest) — the
983    /// worker's serve loop cannot tell a bridge dispatch from an outbox row) and
984    /// hand the correlated-reply awaiter to a dedicated router thread that
985    /// resolves this dispatch's pending entry exactly like a gRPC completion.
986    ///
987    /// The wire carries the SAME engine-provided `attempt` and `labels` the gRPC
988    /// arm's `ActivityTask` carries (a retry over liminal executes with the real
989    /// attempt, not a re-stamped first delivery), plus the server's heartbeat
990    /// window so the worker's automatic liveness pump keeps this TRACKED
991    /// dispatch alive under the #176 expiry sweeper. It carries the engine's
992    /// concrete run identity, generation proof, and stable effect key exactly
993    /// like the gRPC bridge task.
994    ///
995    /// A successful push also binds the attempt into the NOI-6 attempt→owner
996    /// back-index (when installed) with the SAME `(workflow, activity, attempt)`
997    /// key the worker stamps its intervention session with, exactly as the
998    /// outbox liminal arm binds each row dispatch — so the ops console can
999    /// enumerate this live attempt and route interventions to its worker. The
1000    /// binding is released when the reply router exits (reply, abandonment, or
1001    /// disconnect — every path). The gRPC arm carries no bind because the agent
1002    /// harness seam exists only on the liminal worker transport.
1003    #[cfg(feature = "liminal-transport")]
1004    fn send_liminal_activity_task(
1005        &self,
1006        worker_id: WorkerId,
1007        delivery: &super::liminal_transport::LiminalWorkerDelivery,
1008        task: ProtoActivityTask,
1009        activity_type: &str,
1010        workflow_id: &WorkflowId,
1011        activity_id: &ActivityId,
1012    ) -> Result<(), String> {
1013        let completion_token =
1014            CompletionToken::from_wire(workflow_id, activity_id, task.completion_token.clone())
1015                .map_err(|error| error.to_string())?;
1016        let heartbeat_window_ms =
1017            u64::try_from(self.heartbeat_tracker.heartbeat_window().as_millis())
1018                .unwrap_or(u64::MAX);
1019        let attempt = task.attempt;
1020        let request = super::liminal_transport::DispatchRequest {
1021            activity_type: activity_type.to_owned(),
1022            workflow_id: workflow_id.clone(),
1023            ordinal: activity_id.sequence_position(),
1024            run_id: task
1025                .run_id
1026                .map(RunId::try_from)
1027                .transpose()
1028                .map_err(|error| error.to_string())?,
1029            attempt,
1030            completion_token: task.completion_token,
1031            idempotency_key: task.idempotency_key,
1032            labels: task.labels.into_iter().collect(),
1033            heartbeat_window_ms,
1034            input: task.input.map(|payload| payload.bytes).unwrap_or_default(),
1035        };
1036        // A push-enqueue failure means the worker's connection was already gone
1037        // at push time — the same synchronous-failure contract as a closed gRPC
1038        // stream channel above.
1039        let awaiter = match delivery.push_dispatch(&request) {
1040            Ok(awaiter) => awaiter,
1041            Err(error) => {
1042                let reason = format!("worker liminal push failed: {error}");
1043                self.cleanup_activity(worker_id, workflow_id, activity_id, &completion_token);
1044                log_worker_error(
1045                    "WorkerChannelClosed",
1046                    &self.namespace,
1047                    activity_type,
1048                    workflow_id,
1049                    activity_id,
1050                    Some(worker_id),
1051                    &reason,
1052                );
1053                return Err(reason);
1054            }
1055        };
1056        // NOI-6: the attempt is live on `worker_id` from this push until the
1057        // router resolves it — bind it for exactly that window (the guard is
1058        // dropped when the router thread exits).
1059        let owner_binding = self.attempt_owners.as_ref().map(|owners| {
1060            super::liminal_transport::AttemptOwnerGuard::bind(
1061                owners.clone(),
1062                super::intervention::AttemptKey::new(
1063                    workflow_id.clone(),
1064                    activity_id.clone(),
1065                    attempt,
1066                ),
1067                worker_id,
1068            )
1069        });
1070        self.spawn_liminal_reply_router(
1071            worker_id,
1072            awaiter,
1073            workflow_id,
1074            activity_id,
1075            &completion_token,
1076            owner_binding,
1077        );
1078        Ok(())
1079    }
1080
1081    /// Waits (on a dedicated router thread, bounded by the dispatch's own
1082    /// lifetime) for the worker's correlated
1083    /// [`DispatchResponse`](super::liminal_transport::DispatchResponse) and
1084    /// re-enters it through the SAME completion bookkeeping the gRPC inbound
1085    /// stream applies (`process_inbound` in `worker_grpc.rs`): clear the
1086    /// in-flight liveness entry, wake any drain waiter, then resolve the
1087    /// bridge's pending map — result, failure, and retryable classification
1088    /// identical (the worker encodes the `retryable:`/`terminal:` reason
1089    /// vocabulary on the wire). An unmatched (already-resolved) REAL reply
1090    /// routes through the outbox delivery callback exactly like a late gRPC
1091    /// result.
1092    ///
1093    /// The #176 heartbeat sweeper covers this dispatch exactly as it covers a
1094    /// gRPC one: the dispatch is tracked in the shared [`HeartbeatTracker`] and
1095    /// the worker's runtime pumps automatic liveness beats over the reserved
1096    /// liminal channel (`WORKER_LIVENESS_CHANNEL`), so a healthy worker running
1097    /// an over-window activity is never falsely expired while a wedged one
1098    /// still is. Prompt worker-DEATH detection additionally rides the
1099    /// connection itself — the awaiter wakes with the typed Disconnected error
1100    /// the moment the connection closes, resolving the SAME retryable
1101    /// lost-worker failure the gRPC stream-teardown sweep reports.
1102    ///
1103    /// Two structural guards mirror the gRPC arm's tracker gating:
1104    ///
1105    /// - A SYNTHESIZED failure (disconnect / receive fault) is delivered only
1106    ///   when this router's own `complete_task` actually retired the tracked
1107    ///   entry — the same "fail only still-tracked tasks" gate
1108    ///   `remove_worker_tasks` gives the gRPC sweeps — so a dispatch already
1109    ///   resolved elsewhere (expiry sweep, shutdown drain, deregistered
1110    ///   fast path) never has a spurious failure injected for an ordinal whose
1111    ///   retry may be live on another worker.
1112    /// - The wait itself ends one reply-poll after the tracked entry
1113    ///   disappears, so an abandoned dispatch never parks this thread for the
1114    ///   remaining life of the worker's connection. A real reply arriving
1115    ///   AFTER that exit is dropped (the resolving path owns the ordinal — its
1116    ///   retry re-executes); this is the one deliberate divergence from the
1117    ///   gRPC arm, whose shared stream task routes any late result to the
1118    ///   outbox callback, and it is the safer half of the trade because a
1119    ///   stale attempt's result can never resolve a newer attempt's entry.
1120    #[cfg(feature = "liminal-transport")]
1121    fn spawn_liminal_reply_router(
1122        &self,
1123        worker_id: WorkerId,
1124        awaiter: liminal_server::server::connection::PushReplyAwaiter,
1125        workflow_id: &WorkflowId,
1126        activity_id: &ActivityId,
1127        completion_token: &CompletionToken,
1128        owner_binding: Option<super::liminal_transport::AttemptOwnerGuard>,
1129    ) {
1130        let pending = self.pending.clone();
1131        let heartbeat_tracker = self.heartbeat_tracker.clone();
1132        let drain_state = self.drain_state.clone();
1133        let workflow_id = workflow_id.clone();
1134        let activity_id = activity_id.clone();
1135        let completion_token = completion_token.clone();
1136        std::thread::spawn(move || {
1137            // Owns the NOI-6 attempt binding for the dispatch's lifetime: it
1138            // drops (releasing the back-index entry) when this router exits,
1139            // on every path — reply, abandonment, disconnect, or panic.
1140            let _owner_binding = owner_binding;
1141            route_liminal_reply(
1142                &pending,
1143                &heartbeat_tracker,
1144                &drain_state,
1145                &awaiter,
1146                (worker_id, &workflow_id, &activity_id, &completion_token),
1147            );
1148        });
1149    }
1150
1151    /// Block until the dispatch terminates (see the module docs for the
1152    /// exhaustive termination list). The wait is deliberately unbounded:
1153    /// the engine imposes no activity timeout of its own.
1154    fn await_activity_result(
1155        &self,
1156        context: &ActivityDispatchContext<'_>,
1157        rx: &SyncReceiver,
1158    ) -> Result<String, String> {
1159        // Close the dispatch/disconnect race before blocking. A worker whose
1160        // stream tore down *before* this dispatch tracked its task was swept
1161        // without this entry, so nothing would ever deliver through `rx`.
1162        // `fail_lost_worker` deregisters before it collects tasks, and this
1163        // dispatch tracked its task before sending, so: if the worker is
1164        // still registered here, any later sweep is guaranteed to include
1165        // this task and unblock the `recv` below.
1166        match self.registry.is_registered(context.worker_id) {
1167            Ok(true) => {}
1168            Ok(false) => {
1169                // A sweep that did include this task may have delivered
1170                // already; prefer its verdict (or a genuine result that
1171                // raced the disconnect) over fabricating one.
1172                if let Ok(result) = rx.try_recv() {
1173                    return self.deliver_result(context, result);
1174                }
1175                self.cleanup_activity(
1176                    context.worker_id,
1177                    context.workflow_id,
1178                    context.activity_id,
1179                    &context.completion_token,
1180                );
1181                // TRANSPORT domain, classified through the shared ledger: the
1182                // activity never executed, so the failure never wears the
1183                // action's retry vocabulary and is re-dispatched
1184                // attempt-neutrally until the transport's own budget is spent.
1185                let reason = self.pending.classify_worker_loss(
1186                    context.workflow_id,
1187                    context.activity_id,
1188                    context.worker_id,
1189                );
1190                log_worker_error(
1191                    "WorkerLost",
1192                    &self.namespace,
1193                    context.activity_type,
1194                    context.workflow_id,
1195                    context.activity_id,
1196                    Some(context.worker_id),
1197                    &reason,
1198                );
1199                return Err(reason);
1200            }
1201            Err(error) => {
1202                self.cleanup_activity(
1203                    context.worker_id,
1204                    context.workflow_id,
1205                    context.activity_id,
1206                    &context.completion_token,
1207                );
1208                let reason = format!("worker registry inspection failed: {error}");
1209                log_worker_error(
1210                    "WorkerRegistry",
1211                    &self.namespace,
1212                    context.activity_type,
1213                    context.workflow_id,
1214                    context.activity_id,
1215                    Some(context.worker_id),
1216                    &reason,
1217                );
1218                return Err(reason);
1219            }
1220        }
1221        if let Ok(result) = rx.recv() {
1222            return self.deliver_result(context, result);
1223        }
1224        // Every sender was dropped without completing: a cleanup path
1225        // removed the pending entry. Surface it instead of hanging.
1226        self.cleanup_activity(
1227            context.worker_id,
1228            context.workflow_id,
1229            context.activity_id,
1230            &context.completion_token,
1231        );
1232        let reason = "activity response channel dropped".to_owned();
1233        log_worker_error(
1234            "WorkerChannelClosed",
1235            &self.namespace,
1236            context.activity_type,
1237            context.workflow_id,
1238            context.activity_id,
1239            Some(context.worker_id),
1240            &reason,
1241        );
1242        Err(reason)
1243    }
1244
1245    fn deliver_result(
1246        &self,
1247        context: &ActivityDispatchContext<'_>,
1248        result: Result<String, String>,
1249    ) -> Result<String, String> {
1250        self.pending
1251            .pending
1252            .remove(&(context.workflow_id.clone(), context.activity_id.clone()));
1253        // A parked dispatch (#207) is not a failure: the server is draining and
1254        // restart recovery re-dispatches the ordinal. Info, never error — a
1255        // routine deploy must not emit an ActivityFailed log per in-flight
1256        // dispatch (the incident's alarm noise).
1257        if let Err(reason) = &result
1258            && aion::is_parked_reason(reason)
1259        {
1260            tracing::info!(
1261                operation = "activity_dispatch",
1262                namespace = %self.namespace,
1263                workflow_id = %context.workflow_id,
1264                activity_id = %context.activity_id,
1265                activity_type = context.activity_type,
1266                worker_id = ?context.worker_id,
1267                "activity parked for restart recovery"
1268            );
1269            return result;
1270        }
1271        log_activity_completion(context, result.is_ok());
1272        result.inspect_err(|reason| {
1273            log_worker_error(
1274                "ActivityFailed",
1275                &self.namespace,
1276                context.activity_type,
1277                context.workflow_id,
1278                context.activity_id,
1279                Some(context.worker_id),
1280                reason,
1281            );
1282        })
1283    }
1284}
1285
1286impl ActivityDispatcher for WorkerActivityDispatcher {
1287    fn dispatch(&self, request: ActivityDispatch) -> Result<String, String> {
1288        match tokio::runtime::Handle::try_current() {
1289            Ok(handle) => match handle.runtime_flavor() {
1290                tokio::runtime::RuntimeFlavor::MultiThread => {
1291                    // We are inside a tokio runtime (the engine spawns the
1292                    // sync dispatch onto its handle). Hand this worker's
1293                    // scheduler core to another thread before blocking so the
1294                    // stream forwarder woken by our `try_send` can actually
1295                    // run — otherwise it is trapped in this worker's
1296                    // non-stealable LIFO slot for as long as we block.
1297                    tokio::task::block_in_place(|| self.dispatch_blocking(request))
1298                }
1299                flavor => Err(format!(
1300                    "activity dispatch blocks the calling thread until the worker responds; \
1301                     a {flavor:?} tokio runtime cannot host that wait because the worker \
1302                     stream forwarder shares its only executor thread and the task could \
1303                     never be delivered — run the engine on a multi-thread tokio runtime"
1304                )),
1305            },
1306            // No tokio context: a beamr scheduler thread or other plain OS
1307            // thread. Blocking here is the designed contract and cannot starve
1308            // the server runtime.
1309            Err(_) => self.dispatch_blocking(request),
1310        }
1311    }
1312}
1313
1314impl WorkerActivityDispatcher {
1315    /// Dispatch the activity and block the calling thread until the worker
1316    /// responds, the worker is declared lost, or the server drains (see the
1317    /// module docs for the exhaustive termination list).
1318    ///
1319    /// The request carries the *real* workflow and activity ids the engine
1320    /// recorded in history, so the worker logs, the pending-completion key,
1321    /// and the heartbeat tracker all correlate directly against the event
1322    /// store. `config` is forwarded by the engine seam but not yet consumed
1323    /// here (the retry executor that reads it is unbuilt).
1324    ///
1325    /// Must never run while the calling thread still owns a tokio scheduler
1326    /// core: the response can only arrive after the runtime's stream
1327    /// forwarder flushes the queued [`WorkerMessage::ActivityTask`] to the
1328    /// worker, so the thread blocking here must not be the one responsible
1329    /// for polling that forwarder. [`ActivityDispatcher::dispatch`] enforces
1330    /// this with `tokio::task::block_in_place`.
1331    fn dispatch_blocking(&self, request: ActivityDispatch) -> Result<String, String> {
1332        let ActivityDispatch {
1333            namespace,
1334            task_queue,
1335            // OPTIONAL within-pool node affinity (NODE-4): `Some(n)` pins this
1336            // dispatch to workers advertising node `n` (require semantics);
1337            // `None` is unpinned and reaches any worker in the pool.
1338            node,
1339            workflow_id,
1340            run_id,
1341            activity_id,
1342            name,
1343            input,
1344            config: _,
1345            attempt,
1346            labels,
1347            // R5 advisory class: engine-side only. The wire carries no such
1348            // field and a worker's behaviour is identical either way — the
1349            // class governs how the ENGINE treats the failure, never how the
1350            // work is done.
1351            advisory: _,
1352        } = request;
1353        let started_at = Instant::now();
1354        self.ensure_accepting(&namespace, &name, &workflow_id, &activity_id, None)?;
1355        let address = ServiceAddress {
1356            namespace: namespace.clone(),
1357            task_queue: task_queue.clone(),
1358            activity_type: name.clone(),
1359            node: node.clone(),
1360        };
1361        let worker = self.select_worker_or_wait(&address, &workflow_id, &activity_id)?;
1362        let worker_id = worker.id();
1363        let span = info_span!(
1364            "activity_dispatch",
1365            operation = "activity_dispatch",
1366            namespace = %namespace,
1367            task_queue = %task_queue,
1368            node = node.as_deref(),
1369            workflow_id = %workflow_id,
1370            activity_id = %activity_id,
1371            activity_type = %name,
1372            worker_id = ?worker_id,
1373        );
1374        let _span_guard = span.enter();
1375        self.ensure_accepting(
1376            &namespace,
1377            &name,
1378            &workflow_id,
1379            &activity_id,
1380            Some(worker_id),
1381        )?;
1382
1383        let (completion_token, rx) = self
1384            .pending
1385            .insert(workflow_id.clone(), activity_id.clone())
1386            .map_err(|error| error.to_string())?;
1387        let task = activity_task(
1388            &name,
1389            &input,
1390            (&workflow_id, &run_id, &activity_id),
1391            attempt,
1392            labels,
1393            &completion_token,
1394        );
1395        if let Err(error) = self.track_worker_task(
1396            worker_id,
1397            &name,
1398            &workflow_id,
1399            &activity_id,
1400            completion_token.clone(),
1401        ) {
1402            self.cleanup_activity(worker_id, &workflow_id, &activity_id, &completion_token);
1403            return Err(error);
1404        }
1405        self.send_activity_task(
1406            &worker,
1407            task,
1408            &address,
1409            &workflow_id,
1410            &activity_id,
1411            &completion_token,
1412        )?;
1413        let context = ActivityDispatchContext {
1414            namespace: &namespace,
1415            activity_type: &name,
1416            worker_id,
1417            workflow_id: &workflow_id,
1418            activity_id: &activity_id,
1419            completion_token,
1420            started_at,
1421        };
1422        self.await_activity_result(&context, &rx)
1423    }
1424}
1425
1426/// Body of one liminal reply-router thread (see
1427/// [`WorkerActivityDispatcher::spawn_liminal_reply_router`] for the contract).
1428///
1429/// Resolves by the key THIS push dispatched (the awaiter is already
1430/// correlation-scoped to it), never by the reply's echoed ids: a buggy echo
1431/// must not cross executions.
1432#[cfg(feature = "liminal-transport")]
1433fn route_liminal_reply(
1434    pending: &PendingActivities,
1435    heartbeat_tracker: &HeartbeatTracker,
1436    drain_state: &DrainState,
1437    awaiter: &liminal_server::server::connection::PushReplyAwaiter,
1438    execution: (WorkerId, &WorkflowId, &ActivityId, &CompletionToken),
1439) {
1440    let (worker_id, workflow_id, activity_id, current_token) = execution;
1441    // The wait re-arms only while this dispatch is still tracked in-flight, so
1442    // a dispatch resolved elsewhere (expiry sweep, shutdown drain, cleanup)
1443    // releases this thread within one reply poll instead of parking it for the
1444    // remaining life of the worker's connection.
1445    let waited = super::liminal_transport::receive_bridge_reply(awaiter, || {
1446        heartbeat_tracker
1447            .is_tracked(worker_id, workflow_id, activity_id)
1448            .unwrap_or(false)
1449    });
1450    // `synthesized` marks a failure this router FABRICATED (disconnect or
1451    // receive fault) as opposed to a real worker reply: only fabricated
1452    // failures are gated on the tracker below.
1453    let (run_id, submitted_token, outcome, synthesized) = match waited {
1454        Ok(Some(response)) => {
1455            let submitted_token = match CompletionToken::from_wire(
1456                workflow_id,
1457                activity_id,
1458                response.completion_token,
1459            ) {
1460                Ok(token) => token,
1461                Err(error) => {
1462                    tracing::warn!(
1463                        worker_id = ?worker_id,
1464                        workflow_id = %workflow_id,
1465                        activity_id = %activity_id,
1466                        %error,
1467                        "liminal activity completion omitted its generation proof"
1468                    );
1469                    return;
1470                }
1471            };
1472            (response.run_id, submitted_token, response.outcome, false)
1473        }
1474        Ok(None) => {
1475            tracing::debug!(
1476                worker_id = ?worker_id,
1477                workflow_id = %workflow_id,
1478                activity_id = %activity_id,
1479                "liminal dispatch resolved by another path; abandoning reply wait"
1480            );
1481            return;
1482        }
1483        Err(error) if error.is_worker_connection_lost() => (
1484            None,
1485            current_token.clone(),
1486            // TRANSPORT domain (the worker's connection closed before it
1487            // replied): classified through the shared ledger, never as an
1488            // action failure.
1489            Err(pending.classify_worker_loss(workflow_id, activity_id, worker_id)),
1490            true,
1491        ),
1492        Err(error) => (
1493            None,
1494            current_token.clone(),
1495            Err(format!("retryable:worker liminal reply failed: {error}")),
1496            true,
1497        ),
1498    };
1499    // The gRPC sweeps fail only still-tracked tasks (`remove_worker_tasks`);
1500    // this is the same structural gate: `complete_task` reports whether THIS
1501    // call retired the tracked entry. A poisoned tracker fails open (deliver)
1502    // so the blocked dispatch thread is never left hanging on a broken lock.
1503    if synthesized {
1504        let was_tracked =
1505            complete_liminal_tracking(heartbeat_tracker, worker_id, workflow_id, activity_id);
1506        if !was_tracked {
1507            // Another path already resolved this dispatch (and notified drain):
1508            // injecting the fabricated lost-worker failure now could reach a retry.
1509            tracing::debug!(
1510                worker_id = ?worker_id,
1511                workflow_id = %workflow_id,
1512                activity_id = %activity_id,
1513                "liminal dispatch already resolved; dropping synthesized lost-worker failure"
1514            );
1515            return;
1516        }
1517    }
1518    if let Err(error) = pending.complete_fenced(
1519        workflow_id,
1520        activity_id,
1521        run_id.as_ref(),
1522        &submitted_token,
1523        outcome,
1524    ) {
1525        tracing::warn!(
1526            worker_id = ?worker_id,
1527            workflow_id = %workflow_id,
1528            activity_id = %activity_id,
1529            %error,
1530            "liminal activity completion handoff rejected"
1531        );
1532        return;
1533    }
1534    if !synthesized
1535        && let Err(error) = heartbeat_tracker.complete_task(worker_id, workflow_id, activity_id)
1536    {
1537        tracing::error!(
1538            worker_id = ?worker_id,
1539            workflow_id = %workflow_id,
1540            activity_id = %activity_id,
1541            %error,
1542            "failed to clear in-flight tracking for completed liminal activity"
1543        );
1544    }
1545    drain_state.notify_activity_drained();
1546}
1547
1548#[cfg(feature = "liminal-transport")]
1549fn complete_liminal_tracking(
1550    heartbeat_tracker: &HeartbeatTracker,
1551    worker_id: WorkerId,
1552    workflow_id: &WorkflowId,
1553    activity_id: &ActivityId,
1554) -> bool {
1555    heartbeat_tracker
1556        .complete_task(worker_id, workflow_id, activity_id)
1557        .unwrap_or_else(|error| {
1558            tracing::error!(
1559                worker_id = ?worker_id,
1560                workflow_id = %workflow_id,
1561                activity_id = %activity_id,
1562                %error,
1563                "failed to clear in-flight tracking for completed liminal activity"
1564            );
1565            true
1566        })
1567}
1568
1569struct ActivityDispatchContext<'a> {
1570    namespace: &'a str,
1571    activity_type: &'a str,
1572    worker_id: WorkerId,
1573    workflow_id: &'a WorkflowId,
1574    activity_id: &'a ActivityId,
1575    completion_token: CompletionToken,
1576    started_at: Instant,
1577}
1578
1579fn activity_task(
1580    activity_type: &str,
1581    input: &str,
1582    execution: (&WorkflowId, &RunId, &ActivityId),
1583    attempt: u32,
1584    labels: BTreeMap<String, String>,
1585    completion_token: &CompletionToken,
1586) -> ProtoActivityTask {
1587    let (workflow_id, run_id, activity_id) = execution;
1588    ProtoActivityTask {
1589        workflow_id: Some(ProtoWorkflowId::from(workflow_id.clone())),
1590        activity_id: Some(ProtoActivityId::from(activity_id.clone())),
1591        activity_type: activity_type.to_owned(),
1592        input: Some(ProtoPayload {
1593            content_type: String::from("application/json"),
1594            bytes: input.as_bytes().to_vec(),
1595        }),
1596        attempt,
1597        labels: labels.into_iter().collect(),
1598        run_id: Some(run_id.clone().into()),
1599        completion_token: completion_token.as_str().to_owned(),
1600        idempotency_key: idempotency_key(workflow_id, run_id, activity_id),
1601    }
1602}
1603
1604fn log_activity_completion(context: &ActivityDispatchContext<'_>, succeeded: bool) {
1605    let duration_ms = duration_ms(context.started_at.elapsed());
1606    tracing::info!(
1607        operation = "activity_complete",
1608        namespace = context.namespace,
1609        workflow_id = %context.workflow_id,
1610        activity_id = %context.activity_id,
1611        activity_type = context.activity_type,
1612        worker_id = ?context.worker_id,
1613        duration_ms,
1614        outcome = if succeeded { "succeeded" } else { "failed" },
1615        "activity completed"
1616    );
1617}
1618
1619fn duration_ms(duration: Duration) -> u64 {
1620    u64::try_from(duration.as_millis()).unwrap_or(u64::MAX)
1621}
1622
1623fn log_worker_error(
1624    error_type: &'static str,
1625    namespace: &str,
1626    activity_type: &str,
1627    workflow_id: &WorkflowId,
1628    activity_id: &ActivityId,
1629    worker_id: Option<super::registry::WorkerId>,
1630    reason: &str,
1631) {
1632    tracing::error!(
1633        operation = "activity_dispatch",
1634        namespace,
1635        workflow_id = %workflow_id,
1636        activity_id = %activity_id,
1637        activity_type,
1638        worker_id = ?worker_id,
1639        error_type,
1640        reason,
1641        "worker interaction failed"
1642    );
1643}
1644
1645#[cfg(test)]
1646mod tests {
1647    use std::sync::Mutex;
1648
1649    use aion_core::{ActivityError, ActivityErrorKind, ContentType, Payload};
1650
1651    use super::*;
1652
1653    fn activity_id(pos: u64) -> ActivityId {
1654        ActivityId::from_sequence_position(pos)
1655    }
1656
1657    #[test]
1658    fn pending_insert_and_complete_delivers_result() -> Result<(), ServerError> {
1659        let pending = PendingActivities::default();
1660        let workflow_id = WorkflowId::new_v4();
1661        let id = activity_id(1);
1662        let rx = pending.insert(workflow_id.clone(), id.clone())?.1;
1663
1664        assert!(pending.complete(&workflow_id, &id, None, Ok("done".to_owned())));
1665        assert_eq!(
1666            rx.recv_timeout(Duration::from_millis(50)),
1667            Ok(Ok("done".to_owned()))
1668        );
1669        Ok(())
1670    }
1671
1672    #[test]
1673    fn pending_complete_unknown_returns_false() {
1674        let pending = PendingActivities::default();
1675        assert!(!pending.complete(
1676            &WorkflowId::new_v4(),
1677            &activity_id(99),
1678            None,
1679            Ok("orphan".to_owned())
1680        ));
1681    }
1682
1683    #[derive(Default)]
1684    struct RecordingOutboxCallback {
1685        completions: Mutex<Vec<(WorkflowId, ActivityId, String)>>,
1686        failures: Mutex<Vec<(WorkflowId, ActivityId, String)>>,
1687        live: bool,
1688    }
1689
1690    impl OutboxDeliveryCallback for RecordingOutboxCallback {
1691        fn deliver_completion(
1692            &self,
1693            workflow_id: &WorkflowId,
1694            activity_id: &ActivityId,
1695            run_id: Option<&RunId>,
1696            result: String,
1697        ) -> Result<bool, ServerError> {
1698            let _ = run_id;
1699            self.completions
1700                .lock()
1701                .map_err(|_| ServerError::lock_poisoned("recording outbox callback"))?
1702                .push((workflow_id.clone(), activity_id.clone(), result));
1703            Ok(self.live)
1704        }
1705
1706        fn deliver_failure(
1707            &self,
1708            workflow_id: &WorkflowId,
1709            activity_id: &ActivityId,
1710            run_id: Option<&RunId>,
1711            reason: String,
1712        ) -> Result<bool, ServerError> {
1713            let _ = run_id;
1714            self.failures
1715                .lock()
1716                .map_err(|_| ServerError::lock_poisoned("recording outbox callback"))?
1717                .push((workflow_id.clone(), activity_id.clone(), reason));
1718            Ok(self.live)
1719        }
1720    }
1721
1722    #[test]
1723    fn unmatched_completion_routes_to_outbox_callback_when_installed() -> Result<(), ServerError> {
1724        let pending = PendingActivities::default();
1725        let callback = Arc::new(RecordingOutboxCallback {
1726            live: true,
1727            ..RecordingOutboxCallback::default()
1728        });
1729        // Install on one clone; the wiring must be visible to every clone.
1730        pending.clone().set_outbox_delivery(callback.clone());
1731
1732        let workflow_id = WorkflowId::new_v4();
1733        let id = activity_id(7);
1734
1735        // No pending entry: the completion is unmatched and must route to the
1736        // callback rather than being dropped. A live workflow reports true.
1737        assert!(pending.complete(&workflow_id, &id, None, Ok("done".to_owned())));
1738        let completions = callback
1739            .completions
1740            .lock()
1741            .map_err(|_| ServerError::lock_poisoned("recording outbox callback"))?;
1742        assert_eq!(completions.len(), 1);
1743        assert_eq!(completions[0].0, workflow_id);
1744        assert_eq!(completions[0].1, id);
1745        assert_eq!(completions[0].2, "done");
1746        Ok(())
1747    }
1748
1749    #[test]
1750    fn unmatched_failure_routes_to_outbox_callback_and_not_live_reports_false()
1751    -> Result<(), ServerError> {
1752        let pending = PendingActivities::default();
1753        // live = false models the expected stale-completion case.
1754        let callback = Arc::new(RecordingOutboxCallback::default());
1755        pending.set_outbox_delivery(callback.clone());
1756
1757        let workflow_id = WorkflowId::new_v4();
1758        let id = activity_id(8);
1759
1760        assert!(!pending.complete(&workflow_id, &id, None, Err("retryable:boom".to_owned())));
1761        let failures = callback
1762            .failures
1763            .lock()
1764            .map_err(|_| ServerError::lock_poisoned("recording outbox callback"))?;
1765        assert_eq!(failures.len(), 1);
1766        assert_eq!(failures[0].2, "retryable:boom");
1767        Ok(())
1768    }
1769
1770    #[test]
1771    fn unmatched_completion_is_silent_drop_when_no_callback_installed() {
1772        // Flag-off byte-identical behaviour: no callback, unmatched returns
1773        // false (silent drop) exactly as before.
1774        let pending = PendingActivities::default();
1775        assert!(!pending.complete(
1776            &WorkflowId::new_v4(),
1777            &activity_id(9),
1778            None,
1779            Ok("x".to_owned())
1780        ));
1781    }
1782
1783    #[test]
1784    fn matched_completion_never_reaches_outbox_callback() -> Result<(), ServerError> {
1785        let pending = PendingActivities::default();
1786        let callback = Arc::new(RecordingOutboxCallback {
1787            live: true,
1788            ..RecordingOutboxCallback::default()
1789        });
1790        pending.set_outbox_delivery(callback.clone());
1791
1792        let workflow_id = WorkflowId::new_v4();
1793        let id = activity_id(10);
1794        let rx = pending.insert(workflow_id.clone(), id.clone())?.1;
1795
1796        assert!(pending.complete(&workflow_id, &id, None, Ok("matched".to_owned())));
1797        assert_eq!(
1798            rx.recv_timeout(Duration::from_millis(50)),
1799            Ok(Ok("matched".to_owned()))
1800        );
1801        assert!(
1802            callback
1803                .completions
1804                .lock()
1805                .map_err(|_| ServerError::lock_poisoned("recording outbox callback"))?
1806                .is_empty(),
1807            "a matched completion must deliver to its waiter, not the outbox callback"
1808        );
1809        Ok(())
1810    }
1811
1812    /// #207: parking resolves the matched waiter with the ephemeral parked
1813    /// sentinel — the exact string the engine's retry loop classifies as
1814    /// `Parked` — and nothing else.
1815    #[test]
1816    fn park_activity_resolves_matched_waiter_with_the_parked_sentinel() -> Result<(), ServerError> {
1817        let pending = PendingActivities::default();
1818        let workflow_id = WorkflowId::new_v4();
1819        let id = activity_id(11);
1820        let rx = pending.insert(workflow_id.clone(), id.clone())?.1;
1821
1822        pending.park_activity(&workflow_id, &id)?;
1823        let result = rx
1824            .recv_timeout(Duration::from_millis(50))
1825            .map_err(|e| ServerError::worker_dispatch("", "", format!("channel: {e}")))?;
1826        assert_eq!(result, Err(aion::PARKED_ACTIVITY_REASON.to_owned()));
1827        Ok(())
1828    }
1829
1830    /// #207: an unmatched park is a no-op and is NEVER routed to the outbox
1831    /// delivery callback — a park is not a failure and must never reach a
1832    /// workflow.
1833    #[test]
1834    fn unmatched_park_is_a_noop_and_never_reaches_the_outbox_callback() -> Result<(), ServerError> {
1835        let pending = PendingActivities::default();
1836        let callback = Arc::new(RecordingOutboxCallback {
1837            live: true,
1838            ..RecordingOutboxCallback::default()
1839        });
1840        pending.set_outbox_delivery(callback.clone());
1841
1842        pending.park_activity(&WorkflowId::new_v4(), &activity_id(12))?;
1843
1844        assert!(
1845            callback
1846                .failures
1847                .lock()
1848                .map_err(|_| ServerError::lock_poisoned("recording outbox callback"))?
1849                .is_empty(),
1850            "a park must never be delivered as an outbox failure"
1851        );
1852        assert!(
1853            callback
1854                .completions
1855                .lock()
1856                .map_err(|_| ServerError::lock_poisoned("recording outbox callback"))?
1857                .is_empty(),
1858            "a park must never be delivered as an outbox completion"
1859        );
1860        Ok(())
1861    }
1862
1863    #[test]
1864    fn completion_sink_routes_success() -> Result<(), ServerError> {
1865        let pending = PendingActivities::default();
1866        let workflow_id = WorkflowId::new_v4();
1867        let id = activity_id(2);
1868        let (completion_token, rx) = pending.insert(workflow_id.clone(), id.clone())?;
1869        let payload = Payload::new(ContentType::Json, br#"{"greeting":"hi"}"#.to_vec());
1870
1871        pending.complete_activity(ActivityCompletion {
1872            workflow_id,
1873            activity_id: id,
1874            run_id: None,
1875            completion_token,
1876            outcome: ActivityCompletionOutcome::Succeeded(payload),
1877        })?;
1878
1879        let result = rx
1880            .recv_timeout(Duration::from_millis(50))
1881            .map_err(|e| ServerError::worker_dispatch("", "", format!("channel: {e}")))?;
1882        assert_eq!(result, Ok(r#"{"greeting":"hi"}"#.to_owned()));
1883        Ok(())
1884    }
1885
1886    #[test]
1887    fn malformed_payload_does_not_consume_the_current_generation() -> Result<(), ServerError> {
1888        let pending = PendingActivities::default();
1889        let workflow_id = WorkflowId::new_v4();
1890        let id = activity_id(12);
1891        let (completion_token, rx) = pending.insert(workflow_id.clone(), id.clone())?;
1892
1893        let malformed = pending.complete_activity(ActivityCompletion {
1894            workflow_id: workflow_id.clone(),
1895            activity_id: id.clone(),
1896            run_id: None,
1897            completion_token: completion_token.clone(),
1898            outcome: ActivityCompletionOutcome::Succeeded(Payload::new(
1899                ContentType::Json,
1900                vec![0xff],
1901            )),
1902        });
1903        assert!(matches!(malformed, Err(ServerError::WorkerDispatch { .. })));
1904        assert!(
1905            rx.try_recv().is_err(),
1906            "an invalid result must leave the waiter unresolved"
1907        );
1908
1909        pending.complete_activity(ActivityCompletion {
1910            workflow_id,
1911            activity_id: id,
1912            run_id: None,
1913            completion_token,
1914            outcome: ActivityCompletionOutcome::Succeeded(Payload::new(
1915                ContentType::Json,
1916                br#""valid""#.to_vec(),
1917            )),
1918        })?;
1919        let result = rx
1920            .recv_timeout(Duration::from_millis(50))
1921            .map_err(|error| ServerError::worker_dispatch("", "", format!("channel: {error}")))?;
1922        assert_eq!(result, Ok(r#""valid""#.to_owned()));
1923        Ok(())
1924    }
1925
1926    #[test]
1927    fn completion_sink_routes_retryable_error() -> Result<(), ServerError> {
1928        let pending = PendingActivities::default();
1929        let workflow_id = WorkflowId::new_v4();
1930        let id = activity_id(3);
1931        let (completion_token, rx) = pending.insert(workflow_id.clone(), id.clone())?;
1932
1933        pending.complete_activity(ActivityCompletion {
1934            workflow_id,
1935            activity_id: id,
1936            run_id: None,
1937            completion_token,
1938            outcome: ActivityCompletionOutcome::Failed(ActivityError {
1939                kind: ActivityErrorKind::Retryable,
1940                message: "temporary".to_owned(),
1941                details: None,
1942            }),
1943        })?;
1944
1945        let result = rx
1946            .recv_timeout(Duration::from_millis(50))
1947            .map_err(|e| ServerError::worker_dispatch("", "", format!("channel: {e}")))?;
1948        assert_eq!(result, Err("retryable:temporary".to_owned()));
1949        Ok(())
1950    }
1951
1952    /// Regression test (#59, brief D12): pending tracking must be keyed by
1953    /// the full `(WorkflowId, ActivityId)` pair. The dispatcher fabricates
1954    /// activity ids from a process-local counter that resets on server
1955    /// restart, so a stale result re-reported from a worker's previous
1956    /// session carries the same bare `ActivityId` as a fresh post-restart
1957    /// dispatch. Under bare-`ActivityId` keying the stale result completed
1958    /// the wrong execution; with pair keying it is dropped and the genuine
1959    /// result still completes.
1960    #[test]
1961    fn stale_result_for_other_workflow_does_not_complete_pending_dispatch()
1962    -> Result<(), ServerError> {
1963        let pending = PendingActivities::default();
1964        let post_restart_workflow = WorkflowId::new_v4();
1965        let pre_restart_workflow = WorkflowId::new_v4();
1966        // Counter resets to the same sequence position after restart.
1967        let id = activity_id(1);
1968        let (completion_token, rx) = pending.insert(post_restart_workflow.clone(), id.clone())?;
1969
1970        // Stale pre-restart result: same activity id, different workflow.
1971        let rejected = pending.complete_activity(ActivityCompletion {
1972            workflow_id: pre_restart_workflow,
1973            activity_id: id.clone(),
1974            run_id: None,
1975            completion_token: CompletionToken::for_test(),
1976            outcome: ActivityCompletionOutcome::Succeeded(Payload::new(
1977                ContentType::Json,
1978                br#""stale""#.to_vec(),
1979            )),
1980        });
1981        assert!(matches!(
1982            rejected,
1983            Err(ServerError::ActivityCompletionRejected { .. })
1984        ));
1985        assert!(
1986            rx.try_recv().is_err(),
1987            "stale result for a different workflow must not complete this dispatch"
1988        );
1989
1990        // The genuine result for the pending execution still completes.
1991        pending.complete_activity(ActivityCompletion {
1992            workflow_id: post_restart_workflow,
1993            activity_id: id,
1994            run_id: None,
1995            completion_token,
1996            outcome: ActivityCompletionOutcome::Succeeded(Payload::new(
1997                ContentType::Json,
1998                br#""fresh""#.to_vec(),
1999            )),
2000        })?;
2001        let result = rx
2002            .recv_timeout(Duration::from_millis(50))
2003            .map_err(|e| ServerError::worker_dispatch("", "", format!("channel: {e}")))?;
2004        assert_eq!(result, Ok(r#""fresh""#.to_owned()));
2005        Ok(())
2006    }
2007
2008    /// Liveness tracker for dispatcher unit tests; the window only matters
2009    /// to expiry checks, which nothing in these tests drives.
2010    fn test_tracker() -> HeartbeatTracker {
2011        HeartbeatTracker::new(Duration::from_secs(5))
2012    }
2013
2014    /// A `greet` dispatch request carrying real (test-synthesized) ids, the
2015    /// engine-seam shape `WorkerActivityDispatcher::dispatch` now consumes.
2016    fn greet_request() -> ActivityDispatch {
2017        ActivityDispatch {
2018            namespace: "default".to_owned(),
2019            task_queue: "default".to_owned(),
2020            node: None,
2021            workflow_id: WorkflowId::new_v4(),
2022            run_id: RunId::new_v4(),
2023            activity_id: ActivityId::from_sequence_position(0),
2024            name: "greet".to_owned(),
2025            input: "{}".to_owned(),
2026            config: "{}".to_owned(),
2027            attempt: 1,
2028            labels: std::collections::BTreeMap::new(),
2029            advisory: false,
2030        }
2031    }
2032
2033    #[test]
2034    fn dispatcher_fails_immediately_when_draining_without_workers() {
2035        let registry = ConnectedWorkerRegistry::default();
2036        let drain = DrainState::default();
2037        let dispatcher = WorkerActivityDispatcher::new(registry, "default", test_tracker())
2038            .with_drain_state(drain.clone());
2039
2040        let _ = drain.begin();
2041
2042        let result = dispatcher.dispatch(greet_request());
2043
2044        assert!(result.is_err());
2045        let err = result.err().unwrap_or_default();
2046        assert!(
2047            err.contains("drain"),
2048            "expected drain rejection, got: {err}"
2049        );
2050    }
2051
2052    /// Regression test for the production stall where every remote activity
2053    /// timed out: the engine invoked the sync `dispatch` from inside a
2054    /// spawned tokio task (`futures::future::lazy` polled on a runtime
2055    /// worker), and the woken stream-consumer task landed in that blocked
2056    /// worker's non-stealable LIFO slot, so the queued `ActivityTask` was
2057    /// only delivered when the then-extant 30s dispatch timeout fired (the
2058    /// dispatch wait is unbounded today; the stall would now be a hang).
2059    ///
2060    /// Mirrors the real wiring minus tonic: the real registry channel that
2061    /// the gRPC stream forwarder drains, a worker task awaiting that channel
2062    /// on the same runtime, completion through the production
2063    /// `ActivityCompletionSink`, and the sync dispatch invoked from a
2064    /// runtime worker task — the worst case the `block_in_place` guard in
2065    /// `dispatch` defends against (the engine itself now routes through
2066    /// `dispatch_async`, off the async workers).
2067    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2068    async fn dispatch_inside_runtime_task_delivers_promptly_and_round_trips()
2069    -> Result<(), Box<dyn std::error::Error>> {
2070        let registry = ConnectedWorkerRegistry::default();
2071        let pending = PendingActivities::default();
2072        let (worker_tx, mut worker_rx) = tokio::sync::mpsc::channel(32);
2073        let activity_types = [String::from("greet")];
2074        let registration = registry.register("default", activity_types.iter(), worker_tx)?;
2075
2076        let sink = pending.clone();
2077        let echo_worker = tokio::spawn(async move {
2078            let Some(WorkerMessage::ActivityTask(task)) = worker_rx.recv().await else {
2079                return Err("expected an activity task on the worker channel".to_owned());
2080            };
2081            let workflow_id = task
2082                .workflow_id
2083                .ok_or("task missing workflow id")
2084                .and_then(|id| WorkflowId::try_from(id).map_err(|_| "bad workflow id"))?;
2085            let activity_id = task
2086                .activity_id
2087                .map(ActivityId::from)
2088                .ok_or("task missing activity id")?;
2089            let completion_token =
2090                CompletionToken::from_wire(&workflow_id, &activity_id, task.completion_token)
2091                    .map_err(|error| error.to_string())?;
2092            sink.complete_activity(ActivityCompletion {
2093                workflow_id,
2094                activity_id,
2095                run_id: None,
2096                completion_token,
2097                outcome: ActivityCompletionOutcome::Succeeded(Payload::new(
2098                    ContentType::Json,
2099                    br#"{"greeting":"hello"}"#.to_vec(),
2100                )),
2101            })
2102            .map_err(|error| error.to_string())
2103        });
2104
2105        let dispatcher = Arc::new(
2106            WorkerActivityDispatcher::new(registry, "default", test_tracker())
2107                .with_pending(pending),
2108        );
2109        let started = Instant::now();
2110        // Invoke the sync dispatch inside the first poll of a spawned task:
2111        // the worst-case calling context for the `block_in_place` guard.
2112        let dispatch_task = tokio::spawn(futures::future::lazy(move |_| {
2113            dispatcher.dispatch(greet_request())
2114        }));
2115        let result = dispatch_task.await.map_err(|error| error.to_string())?;
2116        let elapsed = started.elapsed();
2117
2118        assert_eq!(result, Ok(r#"{"greeting":"hello"}"#.to_owned()));
2119        assert!(
2120            elapsed < Duration::from_secs(5),
2121            "dispatch round trip took {elapsed:?}; task delivery must not \
2122             depend on the blocked dispatch thread"
2123        );
2124        echo_worker.await.map_err(|error| error.to_string())??;
2125        registration.deregister()?;
2126        Ok(())
2127    }
2128
2129    /// A current-thread runtime cannot host the blocking wait (the stream
2130    /// forwarder would share its only executor thread), so dispatch must
2131    /// fail fast with a precise error instead of blocking forever.
2132    #[tokio::test]
2133    async fn dispatch_on_current_thread_runtime_fails_fast()
2134    -> Result<(), Box<dyn std::error::Error>> {
2135        let registry = ConnectedWorkerRegistry::default();
2136        let (worker_tx, _worker_rx) = tokio::sync::mpsc::channel(32);
2137        let activity_types = [String::from("greet")];
2138        let registration = registry.register("default", activity_types.iter(), worker_tx)?;
2139        let dispatcher = WorkerActivityDispatcher::new(registry, "default", test_tracker());
2140
2141        let started = Instant::now();
2142        let result = dispatcher.dispatch(greet_request());
2143        let elapsed = started.elapsed();
2144
2145        let err = result.err().ok_or("expected dispatch to fail")?;
2146        assert!(
2147            err.contains("multi-thread tokio runtime"),
2148            "unexpected error: {err}"
2149        );
2150        assert!(
2151            elapsed < Duration::from_secs(5),
2152            "fail-fast path took {elapsed:?}"
2153        );
2154        registration.deregister()?;
2155        Ok(())
2156    }
2157
2158    /// Bridge-level mirror of the e2e node-pin proof: two workers share the
2159    /// `(namespace, task_queue)` pool but advertise different nodes; an
2160    /// `ActivityDispatch` pinned to one node must reach ONLY the worker on that
2161    /// node through the live engine-seam `WorkerActivityDispatcher`. This is the
2162    /// regression guard for the bridge discarding the dispatch's `node`.
2163    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2164    async fn dispatch_pinned_to_node_reaches_only_that_node()
2165    -> Result<(), Box<dyn std::error::Error>> {
2166        let registry = ConnectedWorkerRegistry::default();
2167        let pending = PendingActivities::default();
2168        let activity_types = [String::from("greet")];
2169        let (n1_tx, mut n1_rx) = tokio::sync::mpsc::channel(32);
2170        let (n2_tx, mut n2_rx) = tokio::sync::mpsc::channel(32);
2171        // Register the DECOY (n2) FIRST so it owns the lowest worker id. The
2172        // bridge's `select_worker` picks the lowest-id matching worker, so a
2173        // bridge that DISCARDED the node would route to n2 (the decoy) here —
2174        // the n1 echo would never fire and the round trip would time out. With
2175        // the node threaded through, selection is filtered to n1.
2176        let on_n2 = registry.register_namespaces(
2177            [String::from("default")],
2178            "default",
2179            Some(String::from("n2")),
2180            activity_types.iter(),
2181            n2_tx,
2182        )?;
2183        let on_n1 = registry.register_namespaces(
2184            [String::from("default")],
2185            "default",
2186            Some(String::from("n1")),
2187            activity_types.iter(),
2188            n1_tx,
2189        )?;
2190
2191        // Echo only on the n1 channel: the dispatch can only complete if the
2192        // task was routed to n1. If it leaked to n2, the n1 wait would stall and
2193        // the round trip below would time out instead.
2194        let sink = pending.clone();
2195        let echo_n1 = tokio::spawn(async move {
2196            let Some(WorkerMessage::ActivityTask(task)) = n1_rx.recv().await else {
2197                return Err("expected an activity task on the n1 worker channel".to_owned());
2198            };
2199            let workflow_id = task
2200                .workflow_id
2201                .ok_or("task missing workflow id")
2202                .and_then(|id| WorkflowId::try_from(id).map_err(|_| "bad workflow id"))?;
2203            let activity_id = task
2204                .activity_id
2205                .map(ActivityId::from)
2206                .ok_or("task missing activity id")?;
2207            let completion_token =
2208                CompletionToken::from_wire(&workflow_id, &activity_id, task.completion_token)
2209                    .map_err(|error| error.to_string())?;
2210            sink.complete_activity(ActivityCompletion {
2211                workflow_id,
2212                activity_id,
2213                run_id: None,
2214                completion_token,
2215                outcome: ActivityCompletionOutcome::Succeeded(Payload::new(
2216                    ContentType::Json,
2217                    br#"{"greeting":"hello"}"#.to_vec(),
2218                )),
2219            })
2220            .map_err(|error| error.to_string())
2221        });
2222
2223        let dispatcher = Arc::new(
2224            WorkerActivityDispatcher::new(registry.clone(), "default", test_tracker())
2225                .with_pending(pending),
2226        );
2227
2228        let pinned = ActivityDispatch {
2229            node: Some(String::from("n1")),
2230            ..greet_request()
2231        };
2232        let started = Instant::now();
2233        let result = tokio::spawn(futures::future::lazy(move |_| dispatcher.dispatch(pinned)))
2234            .await
2235            .map_err(|error| error.to_string())?;
2236        let elapsed = started.elapsed();
2237
2238        assert_eq!(result, Ok(r#"{"greeting":"hello"}"#.to_owned()));
2239        assert!(
2240            elapsed < Duration::from_secs(5),
2241            "pinned dispatch round trip took {elapsed:?}; the task must route to n1"
2242        );
2243        echo_n1.await.map_err(|error| error.to_string())??;
2244
2245        // The n2 worker (wrong node) must never have been handed the task.
2246        assert!(
2247            n2_rx.try_recv().is_err(),
2248            "node=Some(\"n1\") dispatch must not reach the n2 worker"
2249        );
2250
2251        on_n1.deregister()?;
2252        on_n2.deregister()?;
2253        Ok(())
2254    }
2255
2256    /// An unpinned (`node = None`) dispatch is byte-identical to today: it
2257    /// reaches a worker in the pool regardless of the worker's advertised node.
2258    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2259    async fn unpinned_dispatch_reaches_a_pooled_worker_regardless_of_node()
2260    -> Result<(), Box<dyn std::error::Error>> {
2261        let registry = ConnectedWorkerRegistry::default();
2262        let pending = PendingActivities::default();
2263        let activity_types = [String::from("greet")];
2264        let (n1_tx, mut n1_rx) = tokio::sync::mpsc::channel(32);
2265        let on_n1 = registry.register_namespaces(
2266            [String::from("default")],
2267            "default",
2268            Some(String::from("n1")),
2269            activity_types.iter(),
2270            n1_tx,
2271        )?;
2272
2273        let sink = pending.clone();
2274        let echo = tokio::spawn(async move {
2275            let Some(WorkerMessage::ActivityTask(task)) = n1_rx.recv().await else {
2276                return Err("expected an activity task on the worker channel".to_owned());
2277            };
2278            let workflow_id = task
2279                .workflow_id
2280                .ok_or("task missing workflow id")
2281                .and_then(|id| WorkflowId::try_from(id).map_err(|_| "bad workflow id"))?;
2282            let activity_id = task
2283                .activity_id
2284                .map(ActivityId::from)
2285                .ok_or("task missing activity id")?;
2286            let completion_token =
2287                CompletionToken::from_wire(&workflow_id, &activity_id, task.completion_token)
2288                    .map_err(|error| error.to_string())?;
2289            sink.complete_activity(ActivityCompletion {
2290                workflow_id,
2291                activity_id,
2292                run_id: None,
2293                completion_token,
2294                outcome: ActivityCompletionOutcome::Succeeded(Payload::new(
2295                    ContentType::Json,
2296                    br#"{"greeting":"hello"}"#.to_vec(),
2297                )),
2298            })
2299            .map_err(|error| error.to_string())
2300        });
2301
2302        let dispatcher = Arc::new(
2303            WorkerActivityDispatcher::new(registry.clone(), "default", test_tracker())
2304                .with_pending(pending),
2305        );
2306
2307        // greet_request() carries node: None — the unpinned path.
2308        let result = tokio::spawn(futures::future::lazy(move |_| {
2309            dispatcher.dispatch(greet_request())
2310        }))
2311        .await
2312        .map_err(|error| error.to_string())?;
2313
2314        assert_eq!(result, Ok(r#"{"greeting":"hello"}"#.to_owned()));
2315        echo.await.map_err(|error| error.to_string())??;
2316        on_n1.deregister()?;
2317        Ok(())
2318    }
2319}