onlyne-proto 2.1.1

Onlyne wire protocol: envelopes, frames, events, ops, error codes, JSON Schema export
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! The total reducer: `apply` and the transition helpers behind it.

use super::event::{IgnoredReason, LifecycleEvent, RejectReason, Verdict, event_version};
use super::project::is_legal;
use super::state::{AgentPhase, DeliveryPhase, Observation, RecoveryPhase, ResourcePhase, Version};

/// Apply one event to one observation. Total function: no panics, and every
/// branch yields Applied/Ignored/Rejected.
// clippy::if_same_then_else: the version gate keeps the duplicated arms of the
// vendored reducer this item came from. Note that the item is no longer
// byte-comparable to that source: the tuple rebuild took the task-outcome arms
// out of it. The allowance stays because the shape that needs it stayed too.
#[allow(clippy::if_same_then_else)]
pub fn apply(obs: &Observation, event: &LifecycleEvent) -> Verdict {
    let v = event_version(event);

    // --- version gate (§2.3), uniform over every event kind ---
    if v.generation < obs.version.generation {
        return Verdict::Ignored(IgnoredReason::StaleGeneration);
    }
    let is_adoption = matches!(
        event,
        LifecycleEvent::AdoptNewGeneration { .. } | LifecycleEvent::Supersede { .. }
    );
    if v.generation == obs.version.generation {
        if v.seq <= obs.version.seq && !is_adoption {
            let reason = if v.seq == obs.version.seq {
                IgnoredReason::StaleOrDuplicateSeq
            } else {
                IgnoredReason::StaleOrDuplicateSeq
            };
            return Verdict::Ignored(reason);
        }
    } else if !is_adoption {
        return Verdict::Rejected(RejectReason::UnadoptedGeneration);
    }

    // --- generation gate: live duplicate adoption is rejected (§3.3) ---
    if let LifecycleEvent::AdoptNewGeneration { .. } = event {
        if obs.generation_live {
            return Verdict::Rejected(RejectReason::DuplicateLiveGeneration);
        }
    }
    if let LifecycleEvent::Supersede {
        old_generation_dead,
        ..
    } = event
    {
        if !old_generation_dead {
            return Verdict::Rejected(RejectReason::OldGenerationLive);
        }
    }

    // --- semantic rules ---
    match event {
        LifecycleEvent::Created { .. } | LifecycleEvent::Ready { .. } => {
            let agent = match event {
                LifecycleEvent::Created { .. } => AgentPhase::Booting,
                _ => AgentPhase::Ready,
            };
            step(obs, v, |o| transition(o, agent))
        }
        LifecycleEvent::TurnStarted { .. } => step(obs, v, |o| transition(o, AgentPhase::Running)),
        LifecycleEvent::TurnEnded { .. } => step(obs, v, |o| transition(o, AgentPhase::Idle)),
        LifecycleEvent::Heartbeat { body, .. } => {
            if !is_legal(body) {
                return Verdict::Rejected(RejectReason::IllegalObservation);
            }
            if body_is_no_op(obs, body) {
                return Verdict::Ignored(IgnoredReason::NoOp);
            }
            if v.generation != obs.version.generation {
                return Verdict::Rejected(RejectReason::UnadoptedGeneration);
            }
            let mut next = body.clone();
            next.version = v;
            next.generation_live = obs.generation_live;
            finish(obs, next)
        }
        LifecycleEvent::Complete { .. } => {
            let mut next = obs.advanced(v);
            next.delivery = DeliveryPhase::Pending;
            if obs.agent == AgentPhase::Idle {
                next.recovery = RecoveryPhase::Draining;
            }
            if next == *obs {
                return Verdict::Ignored(IgnoredReason::NoOp);
            }
            finish(obs, next)
        }
        LifecycleEvent::IntentPending { .. } => {
            let mut next = obs.advanced(v);
            match obs.delivery {
                DeliveryPhase::NoIntent | DeliveryPhase::Retrying | DeliveryPhase::Exhausted => {
                    next.delivery = DeliveryPhase::Pending;
                }
                DeliveryPhase::Pending | DeliveryPhase::Accepted => {
                    return Verdict::Ignored(IgnoredReason::NoOp);
                }
            }
            finish(obs, next)
        }
        LifecycleEvent::IntentRetry { .. } => {
            let mut next = obs.advanced(v);
            match obs.delivery {
                DeliveryPhase::Pending => {
                    next.delivery = DeliveryPhase::Retrying;
                    if obs.recovery == RecoveryPhase::IdleWaiting {
                        next.recovery = RecoveryPhase::NoRecovery;
                    }
                }
                DeliveryPhase::NoIntent
                | DeliveryPhase::Retrying
                | DeliveryPhase::Accepted
                | DeliveryPhase::Exhausted => {
                    return Verdict::Ignored(IgnoredReason::NoOp);
                }
            }
            finish(obs, next)
        }
        LifecycleEvent::IntentReceipt { .. } => {
            let mut next = obs.advanced(v);
            match obs.delivery {
                DeliveryPhase::Pending | DeliveryPhase::Retrying => {
                    next.delivery = DeliveryPhase::Accepted;
                    next.recovery = RecoveryPhase::NoRecovery;
                }
                DeliveryPhase::NoIntent | DeliveryPhase::Accepted | DeliveryPhase::Exhausted => {
                    return Verdict::Ignored(IgnoredReason::NoOp);
                }
            }
            finish(obs, next)
        }
        LifecycleEvent::IntentExhausted { .. } => {
            let mut next = obs.advanced(v);
            match obs.delivery {
                DeliveryPhase::Pending | DeliveryPhase::Retrying => {
                    next.delivery = DeliveryPhase::Exhausted;
                    if obs.recovery == RecoveryPhase::IdleWaiting {
                        next.recovery = RecoveryPhase::IdleFault;
                    }
                }
                DeliveryPhase::NoIntent | DeliveryPhase::Accepted | DeliveryPhase::Exhausted => {
                    return Verdict::Ignored(IgnoredReason::NoOp);
                }
            }
            finish(obs, next)
        }
        LifecycleEvent::ResourceAttach { .. } => {
            let mut next = obs.advanced(v);
            match obs.resource {
                ResourcePhase::Detached => next.resource = ResourcePhase::Attached,
                ResourcePhase::Attached => return Verdict::Ignored(IgnoredReason::NoOp),
                ResourcePhase::Closing | ResourcePhase::Closed => {
                    return Verdict::Rejected(RejectReason::UndefinedTransition);
                }
            }
            finish(obs, next)
        }
        LifecycleEvent::Suspend { .. } => {
            if obs.resource == ResourcePhase::Detached {
                return Verdict::Rejected(RejectReason::UndefinedTransition);
            }
            if obs.resource == ResourcePhase::Closed {
                return Verdict::Ignored(IgnoredReason::NoOp);
            }
            if matches!(
                obs.agent,
                AgentPhase::Booting | AgentPhase::Running | AgentPhase::Gone
            ) {
                return Verdict::Rejected(RejectReason::UndefinedTransition);
            }
            let mut next = obs.advanced(v);
            next.resource = ResourcePhase::Closed;
            finish(obs, next)
        }
        LifecycleEvent::Resume { .. } => {
            let mut next = obs.advanced(v);
            match obs.resource {
                ResourcePhase::Attached => return Verdict::Ignored(IgnoredReason::NoOp),
                ResourcePhase::Closed => next.resource = ResourcePhase::Attached,
                ResourcePhase::Detached | ResourcePhase::Closing => {
                    return Verdict::Rejected(RejectReason::UndefinedTransition);
                }
            }
            finish(obs, next)
        }
        LifecycleEvent::ResourceCloseRequested { .. } | LifecycleEvent::ResourceClosed { .. } => {
            let mut next = obs.advanced(v);
            match (event, obs.resource) {
                (LifecycleEvent::ResourceCloseRequested { .. }, ResourcePhase::Detached)
                | (LifecycleEvent::ResourceCloseRequested { .. }, ResourcePhase::Closed) => {
                    return Verdict::Ignored(IgnoredReason::NoOp);
                }
                (LifecycleEvent::ResourceCloseRequested { .. }, _) => {
                    next.resource = ResourcePhase::Closing;
                }
                (LifecycleEvent::ResourceClosed { .. }, ResourcePhase::Detached)
                | (LifecycleEvent::ResourceClosed { .. }, ResourcePhase::Closed) => {
                    return Verdict::Ignored(IgnoredReason::NoOp);
                }
                (LifecycleEvent::ResourceClosed { .. }, _) => {
                    next.resource = ResourcePhase::Closed;
                    // A closed resource kills the agent fact it hosted.
                    if obs.generation_live {
                        next.agent = AgentPhase::Gone;
                        next.generation_live = false;
                        next.recovery = RecoveryPhase::NoRecovery;
                    }
                }
                _ => return Verdict::Rejected(RejectReason::UndefinedTransition),
            }
            finish(obs, next)
        }
        LifecycleEvent::AgentGone { .. } => {
            let mut next = obs.advanced(v);
            if obs.agent == AgentPhase::Gone {
                return Verdict::Ignored(IgnoredReason::NoOp);
            }
            next.agent = AgentPhase::Gone;
            next.generation_live = false;
            next.recovery = RecoveryPhase::NoRecovery;
            next.resource = match obs.resource {
                ResourcePhase::Attached | ResourcePhase::Closing => ResourcePhase::Closing,
                r => r,
            };
            // What the task ended as is the ledger's fact, not this tuple's, so
            // an accepted receipt stays accepted: the drain closed before the
            // process went.
            finish(obs, next)
        }
        LifecycleEvent::Cancel { .. } => {
            // Cancelling settles the task's result, which is not a session
            // dimension. The exit still has to drain: the agent is alive, its
            // intent is where it was, and the reducer has nothing to write.
            Verdict::Ignored(IgnoredReason::NoOp)
        }
        LifecycleEvent::Fail { .. } => {
            let mut next = obs.advanced(v);
            // The session-side part of a fault is the open fault line; what the
            // work ended as belongs to the task ledger.
            if obs.agent == AgentPhase::Idle {
                next.recovery = RecoveryPhase::IdleFault;
            }
            finish(obs, next)
        }
        LifecycleEvent::ReconcileMismatch { .. } => {
            let count = obs.mismatch_count.saturating_add(1);
            if count > obs.terminate_after {
                return Verdict::Ignored(IgnoredReason::GenerationFinished);
            }
            if count >= obs.terminate_after {
                let mut next = obs.with_mismatch(count).advanced(v);
                next.generation_live = false;
                next.agent = AgentPhase::Gone;
                next.recovery = RecoveryPhase::NoRecovery;
                next.resource = match obs.resource {
                    ResourcePhase::Attached | ResourcePhase::Closing => ResourcePhase::Closing,
                    r => r,
                };
                return finish(obs, next);
            }
            if count >= obs.isolate_after {
                let mut next = obs.with_mismatch(count).advanced(v);
                if next.agent == AgentPhase::Idle {
                    next.recovery = RecoveryPhase::IdleFault;
                }
                return finish(obs, next);
            }
            Verdict::Applied(obs.with_mismatch(count).advanced(v))
        }
        LifecycleEvent::ReconcileOk { .. } => {
            let mut next = obs.advanced(v);
            next.mismatch_count = 0;
            if obs.recovery == RecoveryPhase::IdleFault {
                next.recovery = RecoveryPhase::NoRecovery;
            }
            // No early no-op here: the comparison tuple carries the four
            // dimensions, not the counter, and resetting the counter is often
            // this event's whole job. A mismatch counted while the agent was
            // running opens no fault line (`isolate` only marks an idle one), so
            // a later confirming probe moves nothing but the counter — which the
            // tuple test read as a replay and dropped. The ladder then only ever
            // climbed: a session could reach `terminate_after` on stale evidence
            // long after its last real disagreement. `finish` is the no-op gate
            // that sees the counter.
            finish(obs, next)
        }
        LifecycleEvent::AdoptNewGeneration { .. } => {
            // Content carries forward; the generation watermark moves. The
            // adopted shape is a live booting generation at the new version.
            let mut next = obs.adopted(v);
            next.agent = AgentPhase::Booting;
            next.delivery = DeliveryPhase::NoIntent;
            next.recovery = RecoveryPhase::NoRecovery;
            finish(obs, next)
        }
        LifecycleEvent::Supersede { body, .. } => {
            if !is_legal(body) {
                return Verdict::Rejected(RejectReason::IllegalObservation);
            }
            let mut next = body.clone();
            next.version = v;
            next.generation_live = true;
            finish(obs, next)
        }
    }
}

/// Apply an agent-state transition with the recovery/delivery coupling frozen
/// in §2.2. `None` means no rule covers the transition.
fn transition(obs: &Observation, agent: AgentPhase) -> Option<Observation> {
    let v = event_version(&LifecycleEvent::TurnStarted { v: obs.version });
    let _ = v;
    let mut next = obs.clone();
    match agent {
        AgentPhase::Booting => {
            if obs.agent == AgentPhase::Booting {
                return None;
            }
            next.agent = AgentPhase::Booting;
            next.delivery = DeliveryPhase::NoIntent;
            next.recovery = RecoveryPhase::NoRecovery;
        }
        AgentPhase::Ready => {
            if obs.agent == AgentPhase::Gone {
                return None;
            }
            next.agent = AgentPhase::Ready;
            if obs.recovery == RecoveryPhase::Draining {
                next.recovery = RecoveryPhase::NoRecovery;
            }
        }
        AgentPhase::Running => {
            if obs.agent == AgentPhase::Gone {
                return None;
            }
            // Turn start is the working evidence: it heals recovery substates.
            next.agent = AgentPhase::Running;
            next.recovery = RecoveryPhase::NoRecovery;
        }
        AgentPhase::Idle => {
            if obs.agent == AgentPhase::Gone {
                return None;
            }
            next.agent = AgentPhase::Idle;
            next.recovery = match obs.recovery {
                RecoveryPhase::Draining => RecoveryPhase::Draining,
                RecoveryPhase::IdleFault => RecoveryPhase::IdleFault,
                _ => match obs.delivery {
                    DeliveryPhase::Pending | DeliveryPhase::Retrying => RecoveryPhase::IdleWaiting,
                    _ => RecoveryPhase::NoRecovery,
                },
            };
        }
        AgentPhase::Gone => {
            if obs.agent == AgentPhase::Gone {
                return None;
            }
            next.agent = AgentPhase::Gone;
            next.generation_live = false;
            next.recovery = RecoveryPhase::NoRecovery;
            next.resource = match obs.resource {
                ResourcePhase::Attached | ResourcePhase::Closing => ResourcePhase::Closing,
                r => r,
            };
        }
    }
    Some(next)
}

/// Shared path for transitions produced by `transition`: no-op detection and
/// legality verification, with the version watermark advanced.
fn step(obs: &Observation, v: Version, f: impl Fn(&Observation) -> Option<Observation>) -> Verdict {
    let Some(mut next) = f(obs) else {
        if obs.agent == AgentPhase::Gone {
            return Verdict::Ignored(IgnoredReason::GenerationFinished);
        }
        return Verdict::Rejected(RejectReason::UndefinedTransition);
    };
    next.version = v;
    finish(obs, next)
}

/// Verify a produced tuple and compare it (ignoring the version) with the
/// current one for no-op detection. The tuple carries no derived view, so there
/// is nothing here to recompute before the legality check.
fn finish(obs: &Observation, next: Observation) -> Verdict {
    if !is_legal(&next) {
        return Verdict::Rejected(RejectReason::IllegalObservation);
    }
    if next.tuple() == obs.tuple() && next.mismatch_count == obs.mismatch_count {
        return Verdict::Ignored(IgnoredReason::NoOp);
    }
    Verdict::Applied(next)
}

/// True when a heartbeat body repeats the current tuple (idempotent replay).
fn body_is_no_op(obs: &Observation, body: &Observation) -> bool {
    obs.tuple() == body.tuple()
        && obs.mismatch_count == body.mismatch_count
        && obs.isolate_after == body.isolate_after
        && obs.terminate_after == body.terminate_after
}