1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//! # Task completion outcomes and the awaitable [`TaskWaiter`].
//!
//! [`TaskOutcome`] is the **final** result of a supervised task run: it is produced exactly once,
//! after the actor's retry loop has finished and its `JoinHandle` has been joined by the registry.
//!
//! [`TaskWaiter`] is the receiving half of a `oneshot` channel created at
//! registration time by [`SupervisorHandle::add_and_watch`](crate::SupervisorHandle::add_and_watch).
//!
//! ## Architecture: one sender, resolved at exactly one site
//!
//! The `oneshot` sender is minted at registration and travels with the task until a single site resolves it.
//! There are two birth points (direct vs. controller) and a closed set of resolution sites - **every** terminal path resolves
//! the sender - waiter can never hang:
//!
//! ```text
//! BIRTH TRAVELS IN RESOLVED AT (exactly one)
//! ───── ────────── ─────────────────────────
//! add_and_watch(spec) ───────────┐
//! oneshot::channel() ├─► RegistryCommand::Add ─► Registry Handle.done ─┐
//! returns (id, TaskWaiter{rx}) │ (mpsc, guaranteed) │
//! │ ▼
//! submit_and_watch(spec) ────────┘ ┌─ report_join (cooperative join)
//! oneshot::channel() │ Ok(Completed) → Completed
//! Controller.watchers[id]=tx ──► admitted ─► start_in_slot ───┤ Ok(Exhausted) → Failed
//! (parked until admitted or rejected) hands tx to Add │ Ok(Fatal) → Fatal
//! │ │ Ok(Canceled) → Canceled
//! │ │ Err(panic) → Panicked
//! └─ never admitted ─► finalize_rejected ─► Rejected └─ force-abort path → ForceAborted
//! ```
//!
//! ## Rules
//!
//! - The outcome is delivered via `oneshot` (guaranteed, not subject to bus `Lagged` loss).
//! - The channel is created **atomically with registration**: there is no window in which the task can finish before the waiter exists.
//! - **Exactly-once by construction**: the sender has a single owner (the registry `Handle`, or the controller `watchers` map before admission);
//! ownership transfers to exactly one resolution site, so the waiter is resolved once and never leaks.
//! - Dropping a [`TaskWaiter`] is always safe: the matching `send` becomes a no-op.
//! - The outcome reflects the **final** attempt only; per-attempt results are observable via [`Subscribe`](crate::Subscribe) events.
//! - `Rejected` is controller-only: a submission that is never admitted (slot busy, queue full, superseded, removed while queued, shutting down) resolves there.
use Arc;
use oneshot;
use crateRuntimeError;
use crateTaskId;
/// Final result of a supervised task run.
///
/// Delivered exactly once per task, after the actor has fully terminated (retry loop finished **and** the actor's `JoinHandle` joined).
///
/// ## `TaskOutcome` vs lifecycle events (one truth, two planes)
///
/// `TaskOutcome` is the **authoritative terminal classification** of a run; it is delivered on the guaranteed completion plane (a `oneshot`, immune to bus lag).
/// The [`EventKind`](crate::EventKind) events on the lossy observability bus are the per-attempt narration of the *same* run - they may be dropped under load and
/// a single `EventKind` (notably `ActorExhausted`) is intentionally reused for several terminal outcomes, discriminated only by its `reason` string.
///
/// When you need *the* final result, read the outcome; use events for live progress and metrics.
///
/// | `TaskOutcome` | Terminal event(s) on the bus | Notes |
/// |--------------------------------------|-----------------------------------------------------------------------|----------------------------------------------------------|
/// | [`Completed`](Self::Completed) | `ActorExhausted` (reason `policy_exhausted_success`) | success under `Never`/`OnFailure` |
/// | [`Failed`](Self::Failed) | `ActorExhausted` (reason = failure / `max_retries_exceeded(..)`) | `reason`/`exit_code` are **byte-identical** to the event |
/// | [`Fatal`](Self::Fatal) | `ActorDead` (reason = fatal message) | `reason`/`exit_code` byte-identical to the event |
/// | [`Canceled`](Self::Canceled) | `TaskCanceled`, or `ActorExhausted` (reason `task_returned_canceled`) | cooperative stop |
/// | [`ForceAborted`](Self::ForceAborted) | `TaskRemoved` (reason `force_terminated_after_grace`) | ignored cancellation |
/// | [`Panicked`](Self::Panicked) | `ActorDead` (reason `actor_panic`) | actor-level panic (not a task-body panic) |
/// | [`Rejected`](Self::Rejected) | `ControllerRejected` (reason = the same string) | never admitted; controller path only |
///
/// # Also
///
/// - [`SupervisorHandle::add_and_watch`](crate::SupervisorHandle::add_and_watch) - obtains a [`TaskWaiter`]
/// - [`SupervisorHandle::submit_and_watch`](crate::SupervisorHandle::submit_and_watch) - controller-path waiter
/// - [`RestartPolicy`](crate::RestartPolicy) / [`BackoffPolicy`](crate::BackoffPolicy) - decide how many attempts happen first
/// - [`EventKind`](crate::EventKind) - per-attempt observability on the event bus
/// Awaitable handle resolving to the [`TaskOutcome`] of a single task run.
///
/// Created by [`SupervisorHandle::add_and_watch`](crate::SupervisorHandle::add_and_watch).
/// Consumed by [`wait`](Self::wait) (a task terminates exactly once, so the outcome is delivered exactly once).
///
/// ## Example
/// ```rust,no_run
/// # use std::time::Duration;
/// # use taskvisor::prelude::*;
/// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let sup = Supervisor::new(SupervisorConfig::default(), vec![]);
/// # let handle = sup.serve();
/// let job: TaskRef = TaskFn::arc("job", |_ctx: CancellationToken| async { Ok(()) });
/// let (id, waiter) = handle
/// .add_and_watch(TaskSpec::once(job), Duration::from_secs(1))
/// .await?;
///
/// match waiter.wait().await? {
/// TaskOutcome::Completed => println!("{id} done"),
/// other => eprintln!("{id} ended with {other:?}"),
/// }
/// # Ok(()) }
/// ```