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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
//! SHAREABLE RESOURCES: the capability a type declares, the citizenship a macro emits, and the
//! handle a unit reaches one through.
//!
//! A model context, an engine client, a tokenizer — the things a unit loads once and several units
//! want. The framework's answer has THREE layers, and each is written by a different party:
//!
//! | layer | written by | what it says |
//! |---|---|---|
//! | [`AsyncResource`] | the resource's AUTHOR, by hand | how to build one, and that it crosses threads |
//! | [`DeclaredResource`] | `#[ingraph::resource]` | this type is a citizen of the walk |
//! | [`ResourceCatalog`] | the graph ROOT's own fields | which resources this deployment has, under which mount, from which SOURCE |
//! | [`Plan::validate`](crate::Plan::validate) | the deployment | every DEMANDED resource was declared |
//!
//! Only the author knows how to load a model, so the contract is hand-implemented; only a
//! declaration can say a type is meant to be shared, so citizenship is a separate act; only the ROOT
//! holds the catalog, its fields being the declared set; and only a plan knows which units are
//! enabled, so the cross-check is a start-up one.
//!
//! # RESOURCES ARE THE ENGINES, and one row of that table moves for a BUILTIN
//!
//! A resource is where a deployment's engines live, so `#[ingraph::resource]` takes an optional
//! BUILTIN KIND WORD — `#[ingraph::resource(tantivy)] pub struct Ft;` — naming one of the
//! framework's own. For those, the first row above is the MACRO's rather than the author's:
//! [`AsyncResource`] is emitted, its [`Options`](AsyncResource::Options) projected off the engine's
//! own store, and the engine's base and faces are emitted delegating to it. Everything else in the
//! table is unchanged, and the word rides its engine's cargo feature.
//!
//! The vocabulary is CLOSED and short, because a kind word is a promise that the macro can write the
//! whole binding — which it can only keep for an engine this crate ships. The open half of the axis
//! is the bare form, which is the table exactly as it reads: hand-written contract, hand-written
//! faces, and no vocabulary to be admitted to.
//!
//! # THE MODE a declaration records
//!
//! An engine's verbs come in pairs, and which half a FOREIGN resource's hand-written impls are is a
//! fact nothing outside its own declaration can see. `#[ingraph::resource(async)]` is what records
//! it, and [`DeclaredResource::SYNC`] is where a binding reads it back — a builtin states it without
//! the author writing anything, the kind carrying that knowledge.
//!
//! # THE GOODBYE is a third opt-in, on the ROOT
//!
//! [`AsyncResource::goodbye`] is defaulted to a drop, so every resource already written keeps the
//! posture it had. What calls it is `#[ingraph(resource(teardown))]` on the field that mounts the
//! resource — the graph root's word, composing with either factory form — and nothing else does. A
//! library resource that knows how to close itself politely therefore does not join a graph's
//! shutdown chain by accident: the deployment that mounts it is the party that decides.
//!
//! # Sharing is a DOUBLE opt-in, and the bare field is the default
//!
//! A unit field written as the plain type is EXCLUSIVE: the unit owns it, the framework knows
//! nothing about it, and a `!Send` value is perfectly legal there because a resident constructs its
//! operator on the engine that will run it and never migrates it. Nothing about that changes here.
//!
//! Sharing is what needs both parties to say so. The resource's author declares the type shareable
//! ([`AsyncResource`] plus the macro), and the unit reaches for it BY TYPE — `model: Shared<Egemma>`.
//! The string label a share once needed is dead: the TYPE is the rendezvous.
//!
//! # Where the teeth are, and which refusal a reader gets
//!
//! | mistake | refusal | where |
//! |---|---|---|
//! | `Shared<T>` for a `T` that is no resource | `E0277`, naming the macro | the unit's FIELD |
//! | `#[ingraph::resource]` on a type with no [`AsyncResource`] impl | `E0277` | the DECLARATION |
//! | a resource type that is not `Send`/`Sync` | `E0277`, through the supertraits | the DECLARATION |
//! | a demanded resource nothing declared | [`Refusal::UndeclaredResource`](crate::Refusal) | START-UP, by name |
//! | a door the declaration did not name | [`LaunchError::Loading`](crate::LaunchError) | THE CREATE WALK, naming the declared source |
//!
//! The last one is a start-up refusal and not a compile one on purpose: [`AsyncResource`] is
//! hand-implemented, so a type CAN satisfy [`Shared`]'s bound without ever having joined the walk.
//! That gap is exactly what [`ResourceCatalog`] and the plan's cross-check close, and closing it in the
//! type system would mean making the capability unimplementable without the macro — which is the
//! opposite of how every other declaration in this crate can be hand-written.
//!
//! # The walk joins BESIDE the type walk rather than through it
//!
//! [`ResourceVisitor`] is its own trait, in this module, exactly as
//! [`transform::visit`](crate::TypeVisitor)'s note says a second kind must join: a per-family face
//! exists to reach ONE family's fact, so folding several into one trait makes the bound set the
//! union of every family's, paid by every visitor. [`TypeVisitor::next_type`](crate::TypeVisitor)'s
//! signature is untouched.
use Future;
use ;
use crate::;
borrowed_name!
/// A type SEVERAL UNITS may hold at once — the capability contract, hand-implemented by the
/// resource's author.
///
/// # `Send + Sync` are the TEETH, and they are supertraits on purpose
///
/// A shared resource crosses residents, and residents are threads and tasks at once — so a type that
/// cannot cross is refused AT ITS DECLARATION, which is the earliest site there is, rather than at
/// whichever unit reached for it first. What the supertraits cannot promise is the other half:
/// `Sync` says a `&R` may be held by two threads, never that the PROTOCOL underneath is safe to
/// drive concurrently. That remains the author's asserted responsibility, and the declaration is
/// exactly the record of the assertion.
///
/// # `build` is ASYNC, and takes options the author shapes
///
/// Loading a model is I/O and compute, so construction is a future — the same async-factory posture
/// the rest of this crate's construction takes. [`Options`](Self::Options) is the AUTHOR's type
/// because only the author knows what one needs; the framework's job is to carry it to `build` once,
/// and to hand the built handle to every member that demanded it.
///
/// # Examples
///
/// ```
/// use ingraph::{Resources, AsyncResource, Shared};
///
/// struct Egemma {
/// dim: usize,
/// }
///
/// struct EgemmaOptions {
/// dim: usize,
/// }
///
/// impl AsyncResource for Egemma {
/// type Options = EgemmaOptions;
/// type Error = core::convert::Infallible;
///
/// async fn build(_ctx: &Resources, options: EgemmaOptions) -> Result<Self, Self::Error> {
/// Ok(Self { dim: options.dim })
/// }
/// }
///
/// // A handle is cloned, never rebuilt: one construction, N members.
/// let model = Shared::new(Egemma { dim: 768 });
/// let second = model.clone();
/// assert_eq!(second.dim, 768);
/// ```
/// A resource's CITIZENSHIP: it is in the walk, so a plan can enumerate it and a demand can be
/// checked against it.
///
/// WHAT A RESOURCE HOLDS — the two storage seams a declaration's leg may point at.
///
/// # A closed pair, and it is closed by the seams rather than by taste
///
/// A leg points at a resource and the resource is verified against a KIND TRAIT
/// ([`holds_documents`](crate::store::holds_documents) / [`holds_coordinates`](crate::store::holds_coordinates)),
/// and there are exactly two of those because there are exactly two retrieving families. A third
/// arrives as a third seam, at which point this enum grows with it — which is the framework-owned
/// vocabulary case a unit enum is for, and not the extension point an application reaches.
/// THE KINDS ONE RESOURCE ANSWERS FOR — a SET, because a store may hold both.
///
/// # Why a set and not a [`ResourceKind`]
///
/// Nothing stops one connection-manager from holding a document index and a vector index: a
/// database with both a full-text and a vector extension is one store answering two seams, and the
/// two mount traits are independent impls on one type. A single-valued seat would make such a
/// resource unrepresentable, or would make it lie about half of itself — and the whole point of the
/// facet is that *kind-compatible* is a question a launch asks of a CANDIDATE LIST.
///
/// # It is a defaulted fact of the DECLARATION, and it defaults to EMPTY
///
/// [`DeclaredResource::KINDS`] carries it. A BUILTIN kind word states it without the author
/// writing anything, the word being the knowledge; a FOREIGN resource states it in one line, which
/// is the open seam's usual price — and the default is EMPTY rather than a guess, because a
/// resource that has not said what it holds must not silently become the sole default for a leg it
/// cannot serve. Saying nothing costs exactly the sole-default convenience and nothing else: a leg
/// that POINTS at such a resource is verified by the kind trait as it always was.
;
/// `#[ingraph::resource]` emits `impl DeclaredResource for …`, and the SUPERTRAIT is what makes the
/// declaration assert the contract: a type declared without a [`AsyncResource`] impl is `E0277` AT THE
/// DECLARATION rather than at the first unit that reached for it.
///
/// The supertrait is the WHOLE assert, and that is measured rather than assumed. A separate audit
/// call beside the impl — the shape [`refers_to`](crate::refers_to) and its neighbours take — was
/// written and then removed: it produced a SECOND error with the same sentence at the same span, so
/// what it added was a duplicate. `ingraph-derive/tests/refusals/` holds the message the one impl
/// gives.
///
/// # It is a marker, and [`resource`](Self::resource) has no second body to write
///
/// The name is derived from the type, in the one place [`ResourceName::of`] derives it, so a
/// citizen states nothing a demand could disagree with. Overriding it would be stating the same
/// thing again, differently — which is the one way the cross-check could ever refuse a resource
/// that is really there.
///
/// A hand-written `impl DeclaredResource for Egemma {}` is the same declaration spelled out, and it
/// is legal: every declaration in this crate can be hand-written, and the macro is the ergonomic
/// spelling rather than a gate.
/// WHERE one resource's options come from — the ONE source its declaration named.
///
/// # It is a vocabulary, and the vocabulary is CLOSED
///
/// A resource field of a graph root is written in exactly three spellings, and each names one door:
/// there is no fourth for a deployment to invent, so this is an enum rather than a seam. What is
/// open is the resource itself — its type, its options and its `build` are all the author's.
///
/// The `teardown` word is beside this vocabulary rather than in it, and that is what makes it
/// compose: `#[ingraph(resource(factory, teardown))]` names this same PREPARE door and adds the
/// goodbye, because where a resource's options come from and whether it has something to say on the
/// way out are unrelated questions. See [`AsyncResource::goodbye`], which is what the word enrols.
///
/// # THE SINGLE-SOURCE LAW
///
/// No precedence chain. The declaration names THE source, and every other door is CLOSED for that
/// resource and refuses loudly rather than quietly losing to one — a conflict dies at launch instead
/// of in production, which is the immutable-configuration doctrine's direct descendant. Varying a
/// resource between deployments is a different DOCUMENT, never a second door.
/// A visitor applied once per resource a deployment declares — the RESOURCE half of the walk.
///
/// Beside [`TypeVisitor`](crate::TypeVisitor) rather than inside it, per that trait's own note: a
/// per-family face exists to reach ONE family's fact, so folding this into `next_type` would make
/// every node visitor pay a resource bound it never spends.
///
/// # What a visitor may assume
///
/// [`DeclaredResource`], which [`next_resource`](Self::next_resource) declares — so a resource that
/// never took the macro cannot be walked at all, and the refusal lands where the WALK names it.
/// That is the citizenship bound doing at the walk what
/// [`Describe`](crate::Describe) does for a node.
///
/// # The MOUNT and the SOURCE ride with the type
///
/// A resource is a FIELD of the graph root, so its declaration states three things at once: the type,
/// the name it is mounted under, and which door its options come through. All three are the root's
/// facts and none of them can be read off the type alone — so the walk carries them rather than
/// making a visitor derive two of them from somewhere they are not written.
///
/// # Examples
///
/// ```
/// use ingraph::{Resources, DeclaredResource, AsyncResource, ResourceSource, ResourceVisitor, ResourceCatalog};
///
/// struct Egemma;
/// struct Whisper;
///
/// impl AsyncResource for Egemma {
/// type Options = ();
/// type Error = core::convert::Infallible;
///
/// async fn build(_ctx: &Resources, _: ()) -> Result<Self, Self::Error> {
/// Ok(Self)
/// }
/// }
///
/// impl AsyncResource for Whisper {
/// type Options = ();
/// type Error = core::convert::Infallible;
///
/// async fn build(_ctx: &Resources, _: ()) -> Result<Self, Self::Error> {
/// Ok(Self)
/// }
/// }
///
/// // What `#[ingraph::resource]` emits.
/// impl DeclaredResource for Egemma {}
/// impl DeclaredResource for Whisper {}
///
/// // What `#[ingraph::graph]` emits — one call per resource field, in declaration order.
/// let mut declared = ResourceCatalog::new();
/// declared
/// .next_resource::<Egemma>("egemma", ResourceSource::Section)
/// .unwrap();
/// declared
/// .next_resource::<Whisper>("whisper", ResourceSource::Prepare)
/// .unwrap();
///
/// assert_eq!(declared.len(), 2);
/// assert!(declared.declares(<Egemma as DeclaredResource>::resource()));
/// assert_eq!(
/// declared.source(<Whisper as DeclaredResource>::resource()),
/// Some(ResourceSource::Prepare)
/// );
/// ```
/// THE ROOT CATALOG: every resource a deployment declares, under its mount, from its one source.
///
/// The graph root's fields ARE the declared set — nodes and resources in one namespace — so this is
/// what the root's own walk collects and what every later question is asked of: the validate walk
/// cross-checks a demand against it, and the create walk reads a resource's declared SOURCE out of
/// it before opening any door.
///
/// What it keeps is the FOUR facts a declaration states and nothing else — its name, its mount, its
/// source, and WHAT IT HOLDS. A resource's options and its built handle belong to the launch: the
/// options go through whichever door [`ResourceSource`] names, and the handle lives in the
/// [`Resources`](crate::Resources).
///
/// The fourth is the youngest and it is what the BINDING LAWS are resolved against: a leg that
/// names no resource takes the one kind-compatible resource a deployment declared, and zero or
/// several is a start-up refusal listing the candidates. See [`candidates`](Self::candidates) and
/// [`sole`](Self::sole).
/// ONE unit's demand for a resource — what a `Shared<R>` field says, as a plan records it.
///
/// It is minted from the TYPE and carries only the name, which is what keeps the demand recordable
/// for a resource that was never declared: that case is precisely the one the start-up cross-check
/// exists to refuse, so a door that could only be spelled for a citizen would make the refusal
/// unreachable.
/// A handle to a resource SEVERAL units hold at once — one construction, N clones.
///
/// # The bound on the STRUCT is the admission gate
///
/// `R: AsyncResource` is written on the type itself and not only on its impls, and that is deliberate
/// rather than an over-application: the whole job of this wrapper is that a unit's FIELD refuses an
/// undeclared type, and a bound that lived on the methods would let `model: Shared<Vec<u8>>` compile
/// and fail somewhere else. The refusal carries [`AsyncResource`]'s own
/// [`on_unimplemented`](diagnostic::on_unimplemented) sentence, so the message names the fix at the
/// site that needs it.
///
/// # `Arc` and nothing else
///
/// Sharing is construction-sharing: the framework builds one value and clones the handle. It adds no
/// lock and no cell — whether the resource's own protocol is safe to drive concurrently is the
/// author's assertion, recorded by the declaration, and a lock this layer imposed would be this
/// layer answering a question it cannot see.
///
/// # Examples
///
/// ```
/// use ingraph::{Resources, AsyncResource, Shared};
///
/// struct Tokenizer(usize);
///
/// impl AsyncResource for Tokenizer {
/// type Options = usize;
/// type Error = core::convert::Infallible;
///
/// async fn build(_ctx: &Resources, vocab: usize) -> Result<Self, Self::Error> {
/// Ok(Self(vocab))
/// }
/// }
///
/// # tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(async {
/// let shared = Shared::<Tokenizer>::build(&Resources::new(), 32_000).await.unwrap();
/// let held_elsewhere = shared.clone();
/// assert_eq!(held_elsewhere.0, 32_000);
/// assert_eq!(shared.holders(), 2);
/// # });
/// ```