ingraph 0.1.0

A declaration-driven indexing framework: records and nodes are written as Rust types, and the storage rows, the dataflow between them and the GraphQL query surface are emitted from the declaration.
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
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
//! THE SHUTDOWN REGISTRY: who will be running when the launch returns, counted exactly.
//!
//! It is built at WIRING and for one reason: that is the moment the set of running-to-be parties is
//! known. The create walk builds values and does not know which of them get an engine; the ignition
//! that follows starts them one at a time and would be counting as it went. The wiring walk stands
//! between the two holding the whole list, so `N` is a fact rather than a running total, and the
//! shutdown report can name exactly who did not answer.
//!
//! # Two handles per party, and which way each one points
//!
//! - the WIND-DOWN, said by the framework and heard by the party. One slot per party, capacity one:
//!   *stop taking new work*, which carries nothing beyond itself.
//! - the COMPLETION, fired by the party and collected by the framework. One oneshot per party: *I
//!   have settled my affairs*. A party that drops without firing has NOT settled, and the deadline
//!   report is what says so — the absence is the signal, so nothing here has to invent a timeout.
//!
//! [`Enrolled`] is a party's own pair and [`Party`] is the framework's; registering mints both at
//! once, which is what makes them pair by construction rather than by a caller keeping two lists
//! parallel.
//!
//! # The completion also carries the GOODBYE, and that is the whole of the teardown surgery
//!
//! A node's value is not the launch's to hold: it was moved onto an engine and it never migrates.
//! So the one moment the chain can reach it is the moment the body has finished with it — which is
//! this completion, and [`Settlement`] is what it carries. Two roads, and the ENGINE decides which:
//!
//! - a `Send` value on the async engine boxes its own goodbye and SHIPS it
//!   ([`Settlement::Boxed`]). The erasure happens at the only place that still knows the type, so
//!   the chain the handle walks carries no generics at all.
//! - a `!Send` value on the thread engine cannot ride a channel, and neither can its future. So it
//!   STAYS: the thread parks at settle ([`Enrolled::settle_parked`]), the chain says *your turn* in
//!   its own order, and only the OUTCOME — which is `Send` — comes back
//!   ([`Settlement::Parked`]).
//!
//! One chain, two execution sites. The cost of the second is one turn-signal round trip per parked
//! node on the shutdown path, and what it buys is the nodes teardown exists for: a model context, an
//! FFI session, a device handle — every one of them a value that cannot cross a thread.
//!
//! # Why the announcement is a slot per party rather than a broadcast
//!
//! The ruled shape is one broadcast plus a subscriber count known at launch. This crate's transport
//! is `flume`, which is MPMC — a message reaches ONE receiver — and the crate names no async runtime
//! to borrow a broadcast primitive from. So the announcement is one capacity-one slot per party and
//! the broadcast is the walk over them; the subscriber count the ruling asks for is [`Shutdown::len`],
//! exact by construction. Nothing about the semantics differs: every party hears it, once, and a
//! second announcement coalesces into the first.
//!
//! # The party also carries whether an ENGINE was ever put under it
//!
//! [`Party::ignited`] is the third fact, and it exists for the failed-launch UNWIND: winding down
//! *the already-ignited set* means the framework has to be able to NAME that set, and mid-walk the
//! registry holds parties in both states at once — the ones whose enrolment has reached an engine
//! and the ones whose enrolment is still in the caller's hands, waiting for a walk that will now
//! never reach them. Waiting for the second kind would be waiting for something that never began.
//!
//! It is a mark on the ENROLMENT rather than a count kept by the ignition, because both engines set
//! it and only one of them is a thing the ignition keeps: a thread body it holds a handle to, and an
//! async body it composed and handed to somebody else's runtime.
//!
//! # A party FINISHES once, and the registry is woken exactly once for it
//!
//! The two handles above are LOOKS, so a shutdown that wants to wait rather than spin needs
//! somewhere to park. That is the third channel: one shared bell every enrolment rings as it goes,
//! whichever way it went — [`settle`](Enrolled::settle) fires the completion and then the enrolment
//! drops, and a party that panicked drops without firing. So [`Shutdown::park`] wakes once per party
//! FINISHED and the per-party looks say which kind of finish it was; a ring carries no identity
//! because it does not need one, the looks being the answer to *who*.
//!
//! # What is NOT here
//!
//! The ORCHESTRATION. Announcing to every party, waiting for the N completions against a deadline,
//! aborting what did not answer and naming the unanswered in three classes — un-settled nodes,
//! un-flushed engines, un-torn resources — is the ignition round's, which is also where the parties
//! that are not nodes (a change intake, a query arm, a poll ticker) enrol through
//! [`register`](Shutdown::register). This module is the registry those steps spend.

use core::time::Duration;

use std::sync::{
  Arc,
  atomic::{AtomicBool, Ordering},
};

use smol_bytes::Utf8Bytes;

use crate::drive::{NodeName, lane::block_on};

use super::teardown::{BoxedError, Goodbye, Posted};

#[cfg(test)]
mod tests;

/// Every party that will be running when the launch returns.
///
/// Built by the wiring walk, one entry per party it wires, and handed on inside the assembly the
/// ignition starts. Its length is the ruled subscriber count: the walk knew who would run, so the
/// number of oneshots to wait for is not estimated anywhere.
#[derive(Debug, Default)]
pub struct Shutdown {
  parties: Vec<Party>,
  // Minted with the FIRST registration rather than with the registry, so an empty one costs no
  // channel and `new` stays `const`: a registry nobody enrolled in has nothing that could ring.
  bell: Option<Bell>,
}

impl Shutdown {
  /// Nobody registered yet.
  #[inline]
  #[must_use]
  pub const fn new() -> Self {
    Self {
      parties: Vec::new(),
      bell: None,
    }
  }

  /// Enrol a party called `name`, and hand back ITS half of the two channels.
  ///
  /// Appends, so registration order is the order the walk wired them — which is the order the
  /// ignition starts them in and, reversed, the order a teardown says goodbye in.
  ///
  /// The name is not checked for uniqueness. A node's name already is unique within an arrangement
  /// and a party that is not a node — an intake, a query arm — is named after the node it serves, so
  /// what the report gains from a duplicate is that both rows say which node they are about.
  pub fn register(&mut self, name: impl Into<Utf8Bytes>) -> Enrolled {
    let (wind, winding) = flume::bounded(1);
    let (settled, completion) = flume::bounded(1);
    let bell = self.bell.get_or_insert_with(Bell::new);
    let finished = Finished(bell.ring.clone());
    let ignited = Arc::new(AtomicBool::new(false));
    self.parties.push(Party {
      name: name.into(),
      wind,
      completion,
      answered: AtomicBool::new(false),
      ignited: Arc::clone(&ignited),
    });
    Enrolled {
      winding,
      settled,
      finished,
      ignited,
    }
  }

  /// A RINGER onto this registry's bell — what wakes a park that is not a party finishing.
  ///
  /// [`App::halt_on`](super::App::halt_on) is the one caller: an exit source firing has to end
  /// [`App::until_stopped`](super::App::until_stopped)'s park, and the park is a receive on this
  /// bell. So the registration is handed a sender onto the same line every enrolment rings as it
  /// goes, and a ring from it is indistinguishable from a party's — which is right, because the
  /// waiter's answer to either is the same: look again.
  ///
  /// It MINTS the bell where none exists yet, exactly as [`register`](Self::register) does. A
  /// registry nobody enrolled in has nothing that could ring, and a halt source registered against
  /// one is still owed a line to ring on.
  pub(super) fn ringer(&mut self) -> Ringer {
    Ringer(self.bell.get_or_insert_with(Bell::new).ring.clone())
  }

  /// Park until one more party has FINISHED — settled or vanished — for at most `within`, or for as
  /// long as it takes where none is given.
  ///
  /// `false` where the wait ran out with nothing more having finished, and where nobody is
  /// registered at all: an empty deployment has nothing to wait for, and a wait that could never end
  /// is not the honest answer to it.
  ///
  /// It is a WAKE rather than an answer, and the difference matters to a caller: a ring says
  /// something changed and the per-party looks say what, so a shutdown loop reads
  /// [`Party::finished`] before parking and again after. Nothing here counts, because counting in two
  /// places is what makes two places able to disagree.
  pub fn park(&self, within: Option<Duration>) -> bool {
    let Some(bell) = self.bell.as_ref() else {
      return false;
    };
    match within {
      Some(within) => bell.rung.recv_timeout(within).is_ok(),
      // The registry holds a ringer of its own, so this parks until a party finishes and not until
      // the last enrolment is dropped — which is what *park until the world ends of itself* means.
      None => bell.rung.recv().is_ok(),
    }
  }

  /// The registered parties, in registration order.
  #[inline]
  #[must_use]
  pub fn parties(&self) -> &[Party] {
    &self.parties
  }

  /// How many — the exact `N` the shutdown waits for.
  #[inline]
  #[must_use]
  pub fn len(&self) -> usize {
    self.parties.len()
  }

  /// Whether nobody is registered, which is what an empty deployment amounts to.
  #[inline]
  #[must_use]
  pub fn is_empty(&self) -> bool {
    self.parties.is_empty()
  }

  /// Whether every registered party has FINISHED — the fact a shutdown's wait is over.
  #[must_use]
  pub fn is_finished(&self) -> bool {
    self.parties.iter().all(Party::finished)
  }
}

/// The shared FINISH bell: one ring per party that finished, whichever way it finished.
///
/// UNBOUNDED, and that is the one thing it has to be: a ring happens while an enrolment is being
/// dropped, and a drop that could block would turn a party's own finish into a wait on whoever is
/// collecting. There is one ring per party ever, so the bound it declines is a bound on N.
#[derive(Debug)]
struct Bell {
  ring: flume::Sender<()>,
  rung: flume::Receiver<()>,
}

impl Bell {
  /// A bell nobody has rung.
  fn new() -> Self {
    let (ring, rung) = flume::unbounded();
    Self { ring, rung }
  }
}

/// A RING ON DEMAND, held by a party that is not an enrolment — see [`Shutdown::ringer`].
///
/// [`Finished`] one shape over, and the difference is WHEN: that one rings as it is dropped, which
/// is what makes a party that panicked wake the registry all the same; this one rings when it is
/// told to, because what it reports is not a finish.
#[derive(Debug, Clone)]
pub(super) struct Ringer(flume::Sender<()>);

impl Ringer {
  /// Wake whoever is parked on the registry. A bell nobody is listening to is not a failure.
  #[inline]
  pub(super) fn ring(&self) {
    let _ = self.0.send(());
  }
}

/// One enrolment's own ringer, which rings as the enrolment goes.
///
/// A `Drop` rather than a call, because the two ways a party can finish are a call and a drop:
/// [`Enrolled::settle`] fires the completion and then this rings as the enrolment falls out of
/// scope, and a party that panicked rings only this. So a waiter is woken exactly once per party
/// however that party ended, and [`Party::settled`] is what says which ending it was.
#[derive(Debug)]
struct Finished(flume::Sender<()>);

impl Drop for Finished {
  fn drop(&mut self) {
    // A registry that is already gone has nobody to wake, which is not a failure.
    let _ = self.0.send(());
  }
}

/// One registered party, from the FRAMEWORK's side: how to tell it to wind down, and how to learn
/// that it has.
#[derive(Debug)]
pub struct Party {
  name: Utf8Bytes,
  wind: flume::Sender<()>,
  completion: flume::Receiver<Settlement>,
  // Whether the completion has been TAKEN — see `goodbye`, which empties the slot the look below
  // reads. Without it a party would report itself settled until the chain collected its goodbye and
  // unsettled afterwards, which is the report saying two different things about one fact.
  answered: AtomicBool,
  ignited: Arc<AtomicBool>,
}

impl Party {
  /// Who it is — the node's own name, which is what a shutdown report prints.
  #[inline]
  #[must_use]
  pub fn name(&self) -> &NodeName {
    NodeName::new(&self.name)
  }

  /// Say *wind down* to it. `false` once the party is gone, which is not a failure: a party that has
  /// already finished has nothing left to be told.
  ///
  /// A second announcement coalesces into the first — the slot holds one and the message carries
  /// nothing beyond itself, exactly as a [`Nudge`](crate::Nudge) does.
  pub fn announce(&self) -> bool {
    match self.wind.try_send(()) {
      Ok(()) | Err(flume::TrySendError::Full(())) => true,
      Err(flume::TrySendError::Disconnected(())) => false,
    }
  }

  /// Whether its completion has arrived.
  ///
  /// A LOOK and not a take, so asking twice answers twice — the shutdown walks the registry more
  /// than once and a question that consumed its own answer would report the second pass wrong.
  ///
  /// A party that dropped without firing answers `false` forever, which is the honest reading: the
  /// oneshot is a positive signal, so a party that vanished is UNANSWERED rather than settled.
  #[inline]
  #[must_use]
  pub fn settled(&self) -> bool {
    self.answered.load(Ordering::Acquire) || !self.completion.is_empty()
  }

  /// TAKE what this party's completion carried — its goodbye, where it had one.
  ///
  /// The chain's own collection step, spent once per party after the wait is over, and the only
  /// thing that empties the slot [`settled`](Self::settled) looks at. It answers `None` for a party
  /// that never fired and for one whose settlement has already been taken, which are the same answer
  /// to the same question: there is nothing here for the chain.
  pub(super) fn goodbye(&self) -> Option<Settlement> {
    let settlement = self.completion.try_recv().ok()?;
    self.answered.store(true, Ordering::Release);
    Some(settlement)
  }

  /// Whether it is FINISHED — settled, or gone without settling.
  ///
  /// A party's own half is dropped either way, which is what this reads, so it is the fact a wait
  /// ends on and [`settled`](Self::settled) is the fact a REPORT is written from. Keeping them
  /// apart is what lets a shutdown stop waiting for a party that panicked at once rather than at its
  /// deadline: the party will never answer, and the registry can already tell.
  #[inline]
  #[must_use]
  pub fn finished(&self) -> bool {
    self.completion.is_disconnected()
  }

  /// Whether an ENGINE has been put under it.
  ///
  /// Set by the ignition step that hands this party's enrolment to an engine, and by both of them:
  /// the thread body it spawns and the async body it composes. So it says *this party has begun*,
  /// which is a different question from either of the two above — an ignited party may be running,
  /// settled or gone, and a party that is none of those has simply not been reached yet.
  ///
  /// What reads it is [`Ignition::unwind`](super::Ignition::unwind), which WAITS for the set this
  /// answers `true` for and reports on it: mid-walk the registry holds both kinds at once, and a
  /// party whose enrolment is still in the caller's hands is one that never started, so waiting for
  /// it would be waiting for something that will not happen. It is told to wind down all the same —
  /// an announcement costs nothing and a party that never began never reads one.
  #[inline]
  #[must_use]
  pub fn ignited(&self) -> bool {
    self.ignited.load(Ordering::Acquire)
  }
}

/// One registered party's OWN half: the announcement it watches, and the completion it fires.
///
/// Handed out by [`Shutdown::register`] and held by whoever runs the party — for a node, by the
/// ignition step that spawns its engine, which fires the completion when the loop returns.
#[derive(Debug)]
pub struct Enrolled {
  winding: flume::Receiver<()>,
  settled: flume::Sender<Settlement>,
  // Rung by its own `Drop`, so a party that settled and a party that vanished both wake the
  // registry — see `Finished`.
  #[expect(
    dead_code,
    reason = "the field IS the ring: it is held to be dropped, and dropping it is the wake"
  )]
  finished: Finished,
  ignited: Arc<AtomicBool>,
}

impl Enrolled {
  /// A handle onto this enrolment's IGNITED mark — see [`Party::ignited`].
  ///
  /// A separate handle rather than a call on the enrolment, because the enrolment MOVES onto the
  /// engine: the step that spawns has already let go of it by the time it learns whether the spawn
  /// succeeded, and a mark set before that answer would name a party that never ran.
  pub(super) fn mark(&self) -> Ignited {
    Ignited(Arc::clone(&self.ignited))
  }

  /// Whether the framework has said *wind down*.
  ///
  /// Non-blocking and repeatable, for [`Party::settled`]'s reason: a loop asks it once per pass, and
  /// an answer that consumed itself would be true exactly once.
  ///
  /// It also answers `true` once the framework's own half is gone, which is the same instruction
  /// arriving by a different road: nobody is left to ask this party for anything.
  #[inline]
  #[must_use]
  pub fn winding(&self) -> bool {
    !self.winding.is_empty() || self.winding.is_disconnected()
  }

  /// Fire the completion: this party has settled its affairs.
  ///
  /// It consumes, because settling happens once. Dropping an [`Enrolled`] without calling this is
  /// what leaves a party UNANSWERED in the shutdown report, and that is the intended reading of a
  /// party that panicked or was aborted.
  ///
  /// Either way the registry is WOKEN as the enrolment goes — see [`Shutdown::park`] — and the
  /// completion is queued first, so a waiter that this wakes already sees the positive answer.
  #[inline]
  pub fn settle(self) {
    self.answer(Settlement::Silent);
  }

  /// Settle, HANDING the chain the goodbye this party's value has to say — the `Send` road.
  ///
  /// `goodbye` was boxed by the body, at the one place that still knew the value's type, so what
  /// travels here is a future and never a value. The chain awaits it in reverse enrolment order,
  /// in its own context, long after this party has finished.
  ///
  /// It settles exactly as [`settle`](Self::settle) does: the completion is queued, the registry is
  /// woken, and the body is free to return. A goodbye nobody collects — an abort, a deadline that
  /// ran out — is dropped with the party's slot, which is a future that is never polled and
  /// therefore a goodbye that was never said.
  #[inline]
  pub fn settle_saying(self, goodbye: Posted) {
    self.answer(Settlement::Boxed(goodbye));
  }

  /// Settle, then PARK on this thread until the chain says it is this value's turn — the `!Send`
  /// road, and the one place a body outlives its own settlement.
  ///
  /// A `!Send` value cannot ride a channel and neither can its future, so the goodbye is driven
  /// HERE and only its outcome travels. The party is settled FIRST — the registry is told and woken
  /// before this parks — so the shutdown's own wait is over by the time the chain begins, and
  /// nothing about the ordering waits on anything this does.
  ///
  /// `say` is called exactly once, on this thread, when the turn signal arrives. Where the chain
  /// never gets there — an abort, a deadline that ran out, a caller that dropped the whole world —
  /// the signal's other half goes with it, this returns at once and the value is dropped unsaid.
  /// So a parked body cannot outlive the world it belongs to, and the report names it as a goodbye
  /// nobody told to be said.
  ///
  /// # It BLOCKS, so it belongs on a thread this party owns
  ///
  /// The whole point is that the value never moved, and what did not move cannot be awaited from
  /// somewhere else. It is the THREAD engine's road for that reason, and an async body has no use
  /// for it: a task that blocked here would be holding an executor thread hostage for the length of
  /// the entire chain.
  pub fn settle_parked<F>(self, say: F)
  where
    F: FnOnce() -> Goodbye,
  {
    let (turn, my_turn) = flume::bounded(1);
    let (said, answer) = flume::bounded(1);
    self.answer(Settlement::Parked(Parked { turn, said: answer }));
    // From here on this thread belongs to the chain rather than to the registry, which is why the
    // settlement went first: a party that had not settled would be one the shutdown is still
    // waiting for, and the chain does not begin until that wait is over.
    if my_turn.recv().is_err() {
      return;
    }
    let _ = said.send(block_on(say()));
  }

  /// Queue `settlement` and let the enrolment go, which is what rings the registry's bell.
  #[inline]
  fn answer(self, settlement: Settlement) {
    // The receiver may already be gone — a shutdown past its deadline stops collecting — and a
    // completion nobody is waiting for is not an error.
    let _ = self.settled.send(settlement);
  }
}

/// What a party's completion carries BESIDE the fact of it — the goodbye seat.
///
/// See the module note for the two roads and why the engine picks between them.
pub(super) enum Settlement {
  /// Nothing to say. The value was dropped where it lived, which is the shipped posture for every
  /// declaration that enrolled in no goodbye.
  Silent,
  /// The goodbye ITSELF, boxed where the type was still known — the `Send` road.
  Boxed(Posted),
  /// A turn signal onto the thread that is still holding the value — the `!Send` road.
  Parked(Parked),
}

impl core::fmt::Debug for Settlement {
  /// Which ROAD, and nothing else: the other two arms hold a future and a pair of channels, neither
  /// of which has anything a reader of a registry dump would want.
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    f.write_str(match self {
      Self::Silent => "Silent",
      Self::Boxed(_) => "Boxed",
      Self::Parked(_) => "Parked",
    })
  }
}

/// A goodbye that stayed on the thread that made it: the way to tell it to go, and the way to hear
/// how it went.
///
/// Minted by [`Enrolled::settle_parked`] and spent by the teardown chain. Dropping one without
/// spending it is what tells the parked thread that nobody is coming — the whole abandonment
/// protocol, and it costs no timeout anywhere.
pub(super) struct Parked {
  turn: flume::Sender<()>,
  said: flume::Receiver<Result<(), BoxedError>>,
}

impl Parked {
  /// Say *your turn*, and wait for the answer.
  ///
  /// `None` where the thread had already gone, which is the honest reading of a body that panicked
  /// between settling and its turn: there is nobody to say the goodbye, and there never will be.
  pub(super) async fn say(self) -> Option<Result<(), BoxedError>> {
    // Capacity one and nobody else sending, so this cannot block: the only way it answers `Err` is
    // a receiver that is gone, which is the vanished thread.
    self.turn.send(()).ok()?;
    self.said.recv_async().await.ok()
  }
}

impl core::fmt::Debug for Parked {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    f.debug_struct("Parked").finish()
  }
}

/// One enrolment's IGNITED mark, held by the ignition step that hands the enrolment to an engine.
///
/// Minted by [`Enrolled::mark`] and set once the engine exists.
#[derive(Debug)]
pub(super) struct Ignited(Arc<AtomicBool>);

impl Ignited {
  /// An engine is now running this party.
  #[inline]
  pub(super) fn ignite(&self) {
    self.0.store(true, Ordering::Release);
  }
}