telltale-machine 17.0.0

Protocol machine for choreographic session type protocols
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Coroutine scheduling: lane selection and policy dispatch.
impl Scheduler {
    /// Pick the next coroutine to execute, or `None` if none are ready.
    pub fn schedule(&mut self) -> Option<usize> {
        self.schedule_with(|_| false)
    }

    /// Pick the next coroutine using a progress predicate.
    pub fn schedule_with<F>(&mut self, has_progress: F) -> Option<usize>
    where
        F: Fn(usize) -> bool,
    {
        let lane = self.next_lane_with_ready()?;
        let policy = self.policy.clone();
        let picked = match policy {
            SchedPolicy::Priority(priority) => {
                self.pick_priority_candidate_in_lane(lane, &priority, has_progress)
            }
            SchedPolicy::ProgressAware => {
                if let Some(pos) = self.lane_queues.get(&lane).and_then(|queue| {
                    queue.iter().position(|id| {
                        self.lane_ready_set
                            .get(&lane)
                            .is_some_and(|ready| ready.contains(id))
                            && has_progress(*id)
                    })
                }) {
                    let picked = self
                        .lane_queues
                        .get_mut(&lane)
                        .and_then(|queue| queue.remove(pos));
                    if let Some(coro_id) = picked {
                        self.lane_queue_remove(lane, coro_id);
                        Some(coro_id)
                    } else {
                        None
                    }
                } else {
                    self.lane_queue_pop_front(lane)
                }
            }
            SchedPolicy::Cooperative | SchedPolicy::RoundRobin => self.lane_queue_pop_front(lane),
        };
        if let Some(coro_id) = picked {
            self.step_count += 1;
            self.remove_from_global_ready(coro_id);
            self.lane_eligible_queue_remove(lane, coro_id);
            Some(coro_id)
        } else {
            None
        }
    }

    /// Lean-aligned scheduler pick entrypoint.
    pub fn pick_runnable<F>(&mut self, has_progress: F) -> Option<usize>
    where
        F: Fn(usize) -> bool,
    {
        self.schedule_with(has_progress)
    }

    /// Pick the next runnable coroutine restricted to cached ready eligibility.
    pub fn pick_eligible_runnable<F>(&mut self, has_progress: F) -> Option<usize>
    where
        F: Fn(usize) -> bool + Copy,
    {
        let lane = self.next_lane_with_eligible_ready()?;
        let policy = self.policy.clone();
        let picked = match policy {
            SchedPolicy::Priority(priority) => {
                self.pick_priority_candidate_in_lane_eligible(lane, &priority, has_progress)
            }
            SchedPolicy::ProgressAware => {
                if let Some(pos) = self.lane_eligible_queues.get(&lane).and_then(|queue| {
                    queue.iter().position(|id| {
                        self.lane_eligible_set
                            .get(&lane)
                            .is_some_and(|eligible| eligible.contains(id))
                            && has_progress(*id)
                    })
                }) {
                    let picked = self
                        .lane_eligible_queues
                        .get_mut(&lane)
                        .and_then(|queue| queue.remove(pos));
                    if let Some(coro_id) = picked {
                        self.lane_eligible_queue_remove(lane, coro_id);
                        Some(coro_id)
                    } else {
                        None
                    }
                } else {
                    self.lane_eligible_queue_pop_front(lane)
                }
            }
            SchedPolicy::Cooperative | SchedPolicy::RoundRobin => {
                self.lane_eligible_queue_pop_front(lane)
            }
        };
        if let Some(coro_id) = picked {
            self.step_count += 1;
            self.remove_from_global_ready(coro_id);
            self.lane_queue_remove(lane, coro_id);
            Some(coro_id)
        } else {
            None
        }
    }

    /// Lean-aligned scheduler state transition helper.
    pub fn update_after_step(&mut self, coro_id: usize, update: StepUpdate) {
        match update {
            StepUpdate::Ready | StepUpdate::Yielded => self.reschedule(coro_id),
            StepUpdate::Blocked(reason) => self.mark_blocked(coro_id, reason),
            StepUpdate::Done => self.mark_done(coro_id),
        }
    }

    /// Re-enqueue a coroutine that yielded or completed an instruction.
    pub fn reschedule(&mut self, coro_id: usize) {
        if self.ready_set.insert(coro_id) {
            self.ready_queue.push_back(coro_id);
        }
        let lane = self.lane_for_or_default(coro_id);
        self.lane_queue_push(lane, coro_id);
    }

    /// Return whether a coroutine is currently in the global ready set.
    #[must_use]
    pub fn is_ready(&self, coro_id: usize) -> bool {
        self.ready_set.contains(&coro_id)
    }

    /// Number of ready coroutines.
    #[must_use]
    pub fn ready_count(&self) -> usize {
        self.ready_set.len()
    }

    /// Snapshot of the current global ready queue order.
    #[must_use]
    pub fn ready_snapshot(&self) -> Vec<usize> {
        let mut seen = BTreeSet::new();
        self.ready_queue
            .iter()
            .filter_map(|id| {
                if self.ready_set.contains(id) && seen.insert(*id) {
                    Some(*id)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Snapshot of current ready coroutine IDs as a set.
    #[must_use]
    pub fn ready_set_snapshot(&self) -> BTreeSet<usize> {
        self.ready_set.clone()
    }

    /// Return whether any ready coroutine satisfies `predicate`.
    pub fn any_ready<F>(&self, mut predicate: F) -> bool
    where
        F: FnMut(usize) -> bool,
    {
        let mut seen = BTreeSet::new();
        self.ready_queue.iter().copied().any(|coro_id| {
            self.ready_set.contains(&coro_id) && seen.insert(coro_id) && predicate(coro_id)
        })
    }

    /// Number of blocked coroutines.
    #[must_use]
    pub fn blocked_count(&self) -> usize {
        self.blocked_set.len()
    }

    /// Whether all coroutines are either done or blocked (no progress possible).
    #[must_use]
    pub fn is_stuck(&self) -> bool {
        self.ready_set.is_empty()
    }

    /// Total steps executed.
    #[must_use]
    pub fn step_count(&self) -> usize {
        self.step_count
    }

    /// Current policy.
    #[must_use]
    pub fn policy(&self) -> &SchedPolicy {
        &self.policy
    }

    /// Configured timeslice.
    #[must_use]
    pub fn timeslice(&self) -> usize {
        self.timeslice
    }

    /// Get the block reason for a coroutine, if blocked.
    #[must_use]
    pub fn block_reason(&self, coro_id: usize) -> Option<&BlockReason> {
        self.blocked_set.get(&coro_id)
    }

    /// All blocked coroutine IDs.
    #[must_use]
    pub fn blocked_ids(&self) -> Vec<usize> {
        self.blocked_set.keys().copied().collect()
    }

    /// Snapshot of blocked coroutine reasons.
    #[must_use]
    pub fn blocked_snapshot(&self) -> BTreeMap<usize, BlockReason> {
        self.blocked_set.clone()
    }

    /// Whether any coroutine is currently cached as both ready and eligible.
    #[must_use]
    pub fn has_eligible_ready(&self) -> bool {
        self.lane_eligible_set
            .values()
            .any(|eligible| !eligible.is_empty())
    }

    /// Update cached ready eligibility for a coroutine.
    pub fn set_ready_eligibility(&mut self, coro_id: usize, eligibility: ReadyEligibility) {
        let lane = self.lane_for_or_default(coro_id);
        if matches!(eligibility, ReadyEligibility::Eligible) && self.ready_set.contains(&coro_id) {
            self.lane_eligible_queue_push(lane, coro_id);
        } else {
            self.lane_eligible_queue_remove(lane, coro_id);
        }
    }

    /// Clear all cached ready eligibility state.
    pub fn clear_ready_eligibility(&mut self) {
        for queue in self.lane_eligible_queues.values_mut() {
            queue.clear();
        }
        for set in self.lane_eligible_set.values_mut() {
            set.clear();
        }
    }

    fn pick_priority_candidate_in_lane<F>(
        &mut self,
        lane: LaneId,
        policy: &PriorityPolicy,
        has_progress: F,
    ) -> Option<usize>
    where
        F: Fn(usize) -> bool,
    {
        let queue = self.lane_queues.get(&lane)?;
        let mut best: Option<(usize, usize)> = None;
        for (pos, id) in queue.iter().copied().enumerate() {
            if !self
                .lane_ready_set
                .get(&lane)
                .is_some_and(|ready| ready.contains(&id))
            {
                continue;
            }
            let score = match policy {
                PriorityPolicy::FixedMap(priorities) => priorities.get(&id).copied().unwrap_or(0),
                PriorityPolicy::Aging => queue.len().saturating_sub(pos),
                PriorityPolicy::TokenWeighted => {
                    let progress = usize::from(has_progress(id));
                    progress * queue.len().saturating_add(1) + queue.len().saturating_sub(pos)
                }
            };
            let replace = match best {
                None => true,
                Some((best_pos, best_score)) => {
                    score > best_score || (score == best_score && pos < best_pos)
                }
            };
            if replace {
                best = Some((pos, score));
            }
        }
        let pos = best.map(|(pos, _)| pos)?;
        let picked = self
            .lane_queues
            .get_mut(&lane)
            .and_then(|lane_queue| lane_queue.remove(pos));
        if let Some(coro_id) = picked {
            self.lane_queue_remove(lane, coro_id);
            Some(coro_id)
        } else {
            None
        }
    }

    fn pick_priority_candidate_in_lane_eligible<FProgress>(
        &mut self,
        lane: LaneId,
        policy: &PriorityPolicy,
        has_progress: FProgress,
    ) -> Option<usize>
    where
        FProgress: Fn(usize) -> bool,
    {
        let queue = self.lane_eligible_queues.get(&lane)?;
        let mut best: Option<(usize, usize)> = None;
        for (pos, id) in queue.iter().copied().enumerate() {
            if !self
                .lane_eligible_set
                .get(&lane)
                .is_some_and(|ready| ready.contains(&id))
            {
                continue;
            }
            let score = match policy {
                PriorityPolicy::FixedMap(priorities) => priorities.get(&id).copied().unwrap_or(0),
                PriorityPolicy::Aging => queue.len().saturating_sub(pos),
                PriorityPolicy::TokenWeighted => {
                    let progress = usize::from(has_progress(id));
                    progress * queue.len().saturating_add(1) + queue.len().saturating_sub(pos)
                }
            };
            let replace = match best {
                None => true,
                Some((best_pos, best_score)) => {
                    score > best_score || (score == best_score && pos < best_pos)
                }
            };
            if replace {
                best = Some((pos, score));
            }
        }
        let pos = best.map(|(pos, _)| pos)?;
        let picked = self
            .lane_eligible_queues
            .get_mut(&lane)
            .and_then(|lane_queue| lane_queue.remove(pos));
        if let Some(coro_id) = picked {
            self.lane_eligible_queue_remove(lane, coro_id);
            Some(coro_id)
        } else {
            None
        }
    }

    /// Assign a coroutine to a specific lane.
    pub fn assign_lane(&mut self, coro_id: usize, lane: LaneId) {
        self.register_lane(lane);
        let prior_lane = self.lane_of.insert(coro_id, lane).unwrap_or(0);
        if prior_lane != lane {
            self.lane_queue_remove(prior_lane, coro_id);
            let was_eligible = self
                .lane_eligible_set
                .get(&prior_lane)
                .is_some_and(|eligible| eligible.contains(&coro_id));
            self.lane_eligible_queue_remove(prior_lane, coro_id);
            if let Some(reason) = self.blocked_set.get(&coro_id).cloned() {
                self.lane_blocked
                    .entry(prior_lane)
                    .or_default()
                    .remove(&coro_id);
                self.lane_blocked
                    .entry(lane)
                    .or_default()
                    .insert(coro_id, reason);
            }
            if was_eligible && self.ready_set.contains(&coro_id) {
                self.lane_eligible_queue_push(lane, coro_id);
            }
        }
        if self.ready_set.contains(&coro_id) {
            self.lane_queue_push(lane, coro_id);
        }
    }

    /// Lane assignment for a coroutine.
    #[must_use]
    pub fn lane_of(&self, coro_id: usize) -> Option<LaneId> {
        self.lane_of.get(&coro_id).copied()
    }

    /// Snapshot of per-lane ready queues.
    #[must_use]
    pub fn lane_queues_snapshot(&self) -> BTreeMap<LaneId, Vec<usize>> {
        self.lane_queues
            .iter()
            .map(|(lane, queue)| (*lane, queue.iter().copied().collect()))
            .collect()
    }

    /// Snapshot of per-lane blocked coroutines.
    #[must_use]
    pub fn lane_blocked_snapshot(&self) -> BTreeMap<LaneId, BTreeMap<usize, BlockReason>> {
        self.lane_blocked.clone()
    }

    /// Record a cross-lane handoff.
    pub fn record_cross_lane_handoff(
        &mut self,
        from_coro: usize,
        to_coro: usize,
        reason: impl Into<String>,
    ) {
        let from_lane = self.lane_for_or_default(from_coro);
        let to_lane = self.lane_for_or_default(to_coro);
        if from_lane != to_lane {
            self.cross_lane_handoffs.push(CrossLaneHandoff {
                from_coro,
                to_coro,
                from_lane,
                to_lane,
                step: self.step_count,
                reason: reason.into(),
            });
        }
    }

    /// Cross-lane handoff log.
    #[must_use]
    pub fn cross_lane_handoffs(&self) -> &[CrossLaneHandoff] {
        &self.cross_lane_handoffs
    }
}