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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! `CoreMailbox` — the `Send + Sync` bridge between autonomous async
//! producers (timer tasks) and an owned, relocatable [`crate::node::Core`].
//!
//! # Why this exists (D223 / D225 / D227 / D230)
//!
//! Under the actor / work-stealing model (D221) a `Core` owns its state
//! cell **by value** and moves between workers — so a long-lived async
//! task (e.g. a `tokio::spawn`-ed timer loop) can no longer hold
//! `&Core` / `Arc<C>` / `Weak<C>` to call `Core::emit` directly (that
//! was the deleted `WeakCore` path; D223 forbids `Weak<C>` back-refs).
//!
//! Instead the task holds an `Arc<CoreMailbox>` (`Send + Sync`) and
//! **posts** a `(NodeId, HandleId)` emit request. The mailbox is drained
//! **owner-side** by the synchronous [`crate::node::Core::drain_mailbox`]
//! (applied via the existing sync `Core::emit` — *no async in Core*,
//! honoring the locked "Core never async" invariant). The drain point is
//! the embedder's existing advance/pump site (test harness `TestRuntime`
//! advance helper, napi pump): timer tasks already require the host
//! runtime to be advanced before they fire, so draining there is
//! **behaviour-identical** to the old autonomous `Weak::upgrade →
//! core.emit` (D230).
//!
//! # Shape (D227 full)
//!
//! - **Op queue** — FIFO of [`MailboxOp`]s, applied owner-side.
//! - **`closed`** — set when the owning `Core` drops; a timer task that
//! observes it releases its pending handle and bails (mirrors the old
//! `Weak::upgrade() == None` teardown path exactly).
//! - **`runnable`** — the per-group "this Core has work" wake bit. S2b
//! lands the field + sets/clears it (D227 builds the *full* mailbox
//! shape now); S4 wires it to wave drain-scoping + finalize, and the
//! host-executor (M6) reads it to schedule the owning worker. The
//! in-wave drain (`BatchGuard::drain_and_flush`) already gates on it
//! so the no-producer §7 floor pays one atomic load, not a mutex.
//!
//! # This is ONE mechanism (not six)
//!
//! The S2b design history records six producer "forks" (D227–D233);
//! that is *design-question* count, not runtime surface. What actually
//! exists is **one** owner-side re-entry mechanism:
//!
//! > A `Send + Sync` FIFO of deferred [`MailboxOp`]s, drained on the
//! > owner thread — **in-wave to quiescence** for producer sinks
//! > (`BatchGuard::drain_and_flush`) and at the embedder pump point for
//! > autonomous timer tasks ([`crate::node::Core::drain_mailbox`]) —
//! > each op applied via the one object-safe owner re-entry surface
//! > [`crate::node::CoreFull`].
//!
//! `MailboxOp` keeps a **typed fast path** (`Emit`/`Complete`/`Error`:
//! zero-alloc, and a deterministic `Core`-gone handle-release contract —
//! see the per-kind `post_*`) plus a **`Defer` escape hatch** (a boxed
//! `FnOnce(&dyn CoreFull)` for value-returning topology mutation:
//! windowing / higher-order inner subscribe). Collapsing the enum to
//! pure `Defer` was considered and rejected — it would heap-allocate per
//! timer/producer emit AND lose the typed `Core`-gone release affordance
//! (a dropped `FnOnce` can't run an else-branch). `ProducerEmitter`
//! (`graphrefly-operators`) is thin sugar over `post_*`; the producer
//! *build* closure isn't a mechanism at all — it uses the `&Core` its
//! `ProducerCtx` already lends it (D231). One queue, one drain loop, one
//! `match`, one re-entry trait.
use ;
use VecDeque;
use ;
use crate;
/// **`Send`** cross-thread deferred closure (D233; D249/S2c). Posted
/// by any cross-thread `Send` producer (canonically an autonomous
/// timer task — `temporal.rs` `window_time`/etc., a `tokio::spawn`ed
/// cross-thread task whose defer closure captures only `Send` state:
/// `Arc<Mutex<NodeId>>` / `Arc<dyn BindingBoundary>` / ids — but the
/// API admits any future cross-thread producer with the same `Send`
/// capture discipline, e.g. a napi/pyo3 binding-layer pump) and
/// applied owner-side. Rides the `Send + Sync` [`CoreMailbox`]
/// (cross-thread post side, drained owner-side via `drain_mailbox`).
pub type SendDeferFn = ;
/// **`!Send`** owner-side deferred closure (D248/D249/S2c). Posted by
/// an owner-side in-wave producer/graph sink whose closure captures
/// `!Send` state (a relaxed `Sink` / `Rc<RefCell<GraphInner>>` —
/// D248). Lives in the owner-only [`DeferQueue`], **never** the
/// cross-thread [`CoreMailbox`].
pub type DeferFn = ;
/// A re-entry request posted to the [`CoreMailbox`] by an autonomous
/// async producer (timer task → `Emit`) or by a producer-operator sink
/// (D232-AMEND/A′ → `Emit`/`Complete`/`Error`). Applied owner-side via
/// the sync `Core::{emit,complete,error}` by [`crate::node::Core::drain_mailbox`]
/// — drained **in-wave to quiescence** by the `BatchGuard` drain loop
/// for producer sinks (immediate, cascade-ordering-preserving), and at
/// the embedder pump point for timer tasks (D230).
/// `Send + Sync` mailbox bridging autonomous async producers to an owned,
/// relocatable [`crate::node::Core`]. Held behind an `Arc`; the `Core`
/// owns one clone, each timer task another. See the module docs.
// `CoreMailbox` is `Send + Sync` by construction (parking_lot::Mutex +
// atomics over the id-only `MailboxOp`). Asserted so a future field
// that breaks it fails here, not at the `Arc<CoreMailbox>` share site
// in `timer.rs`. D249/S2c: this MUST hold — the `!Send` owner-side
// `Defer` payload now lives in [`DeferQueue`], NOT here.
const _: fn = ;
/// Owner-only deferred-closure queue (D249/S2c — the minimal Defer
/// split pulled out of [`CoreMailbox`]; D254 (S5) relaxed off cross-
/// thread primitives).
///
/// Under D248 full single-owner the substrate `Sink`/`TopologySink`
/// dropped `Send + Sync`, so a [`DeferFn`] (which captures relaxed
/// `Sink`s / `Rc<RefCell<GraphInner>>`) is `!Send`. It cannot ride the
/// `Send + Sync` [`CoreMailbox`] (that bridges the `timer.rs`
/// cross-thread `Arc<CoreMailbox>` post side). This queue is therefore
/// **owner-only and `!Send`**: held behind an `Rc` shared between
/// [`crate::node::Core`] and `graphrefly-operators`' `ProducerEmitter`
/// on the one owner thread. Drained owner-side by
/// [`crate::node::Core::drain_mailbox`] (after the id-mailbox) and the
/// in-wave `BatchGuard` drain.
///
/// **Owner-thread-only by construction (D254 (S5), 2026-05-19).** The
/// `Rc<DeferQueue>` is never sent across threads (the closures it holds
/// are `!Send`), so the cross-thread primitives it used to carry —
/// `parking_lot::Mutex<VecDeque<DeferFn>>` for `q` and `AtomicBool` for
/// `runnable` — were unused capacity. D254 collapses them to
/// `RefCell<VecDeque<DeferFn>>` + `Cell<bool>`, dropping the
/// `parking_lot::Mutex::lock` acquire on every owner-side post/drain
/// (the hottest D251 path). `closed` similarly drops to `Cell<bool>` —
/// it is set by `Drop for Core` on the owner thread (the only `close()`
/// call site) and read by `post`/`drain_into` on the same thread.
///
/// S4 still does the per-group-wake + typed snapshot/prune `MailboxOp`
/// reshape (D246 rule 8) — D249 is the acknowledged minimal first
/// touch that lets the D248 single-owner Sink relaxation land in S2c.
// /qa M4 (2026-05-19): `DeferQueue` MUST stay `!Send + !Sync` by
// construction (owner-thread-only by D248/D249; the `Rc<DeferQueue>`
// is never sent across threads, the closures it holds are `!Send`).
// Lock the invariant in the type system so a future field that
// silently widens the trait bounds (e.g., swapping `RefCell` for
// `Mutex` "for safety") breaks the build instead of the actor-model
// invariant.
assert_not_impl_any!;
/// RAII guard that clears [`CoreMailbox::runnable`] on panic unwind if
/// the queue is empty at unwind time (/qa M3). Outside of unwind, the
/// explicit `runnable.store(false, Release)` on the empty-pop path of
/// [`CoreMailbox::drain_into`] handles the normal-exit clear under the
/// `ops` lock (QA F-#4 lost-wakeup discipline). On panic unwind from
/// inside `apply(f)`, this guard's `Drop` re-acquires the `ops` lock,
/// checks whether the queue is empty, and clears `runnable` if so —
/// if non-empty, leaves `runnable=true` so the next drain picks up the
/// remaining work, matching the QA F-#4 race discipline.
/// RAII guard that clears [`DeferQueue::runnable`] on panic unwind if
/// the queue is empty at unwind time (/qa M3). Owner-thread-only by
/// D248/D249/D254 construction — no lock to acquire; the cell-based
/// discipline lets the guard `Cell::set(false)` directly. Mirrors
/// [`MailboxRunnableClearOnPanic`]'s shape against the relaxed
/// `RefCell` + `Cell` primitives. If the queue is non-empty at unwind
/// time, leaves `runnable=true` so the next drain picks up the work.