/
gall.hoon
3019 lines (3018 loc) · 87.8 KB
/
gall.hoon
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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!:
:: :: %gall, agent execution
!? 163
::
::::
|= our=ship
:: veb: verbosity flags
::
=/ veb-all-off
:: TODO: add more flags?
::
:* odd=`?`%.n :: unusual events
==
=, gall
=>
|%
+| %helpers
:: +trace: print if .verb is set and we're tracking .dude
::
++ trace
|= [verb=? =dude dudes=(set dude) print=tang]
^+ same
?. verb
same
?. => [dude=dude dudes=dudes in=in]
~+ |(=(~ dudes) (~(has in dudes) dude))
same
(slog print)
::
:: $bug: debug printing configuration
::
:: veb: verbosity toggles
:: dudes: app filter; if ~, print for all
::
+$ bug
$: veb=_veb-all-off
dudes=(set dude)
==
::
+| %main
::
:: $move: Arvo-level move
::
+$ move [=duct move=(wind note-arvo gift-arvo)]
:: $state-16: overall gall state, versioned
::
+$ state-16 [%16 state]
:: $state: overall gall state
::
:: system-duct: TODO document
:: outstanding: outstanding request queue
:: contacts: other ships we're in communication with
:: yokes: running agents
:: blocked: moves to agents that haven't been started yet
:: bug: debug printing configuration
:: leaves: retry nacked %leaves timer, if set
::
+$ state
$+ state
$: system-duct=duct
outstanding=(map [wire duct] (qeu remote-request))
contacts=(set ship)
yokes=(map term yoke)
blocked=(map term (qeu blocked-move))
=bug
leaves=(unit [=duct =wire date=@da])
==
:: $routes: new cuff; TODO: document
::
+$ routes
$: disclosing=(unit (set ship))
attributing=[=ship =path]
==
+$ brood [=coop =hutch]
:: $yoke: agent runner state
::
:: control-duct: TODO document
:: run-nonce: unique for each rebuild
:: sub-nonce: app-wide global %watch nonce
:: stats: TODO document
:: bitt: incoming subscriptions
:: boat: outgoing subscriptions
:: boar: and their nonces
:: code: most recently loaded code
:: agent: agent core
:: beak: compilation source
:: marks: mark conversion requests
:: sky: scry bindings
:: ken: open keen requests
::
+$ yoke
$% [%nuke sky=(map spur @ud) cop=(map coop hutch)]
$: %live
control-duct=duct
run-nonce=@t
sub-nonce=_1
=stats
=bitt
=boat
=boar
code=*
agent=(each agent vase)
=beak
marks=(map duct mark)
sky=farm
ken=(jug spar:ames wire)
pen=(jug spar:ames wire)
gem=(jug coop [path page])
== ==
::
++ of-farm
|_ =farm
++ key-coops
|= pos=path
^- (list coop)
=/ frm (get-farm pos)
?~ frm ~
=. farm u.frm
|-
?: ?=(%coop -.farm)
~[pos]
%- zing
%+ turn ~(tap by q.farm)
|= [seg=@ta f=^farm]
^- (list coop)
^$(pos (snoc pos seg), farm f)
::
++ match-coop
=| wer=path
|= =path
^- (unit coop)
?: ?=(%coop -.farm)
`(flop wer)
?~ path
~
?~ nex=(~(get by q.farm) i.path)
~
$(wer [i.path wer], path t.path, farm u.nex)
::
++ put
|= [=path =plot]
^- _farm
?: ?=(%coop -.farm)
farm(q (~(put by q.farm) path plot))
?~ path
farm(p `plot)
=/ nex (~(get by q.farm) i.path)
=/ res $(path t.path, farm ?~(nex *^farm u.nex))
farm(q (~(put by q.farm) i.path res))
::
++ put-grow
|= [=path =plot]
^- (unit _farm)
?: ?=(%coop -.farm)
~
?~ path
`farm(p `plot)
=/ nex (~(get by q.farm) i.path)
=/ res
$(path t.path, farm ?~(nex *^farm u.nex))
?~ res ~
`farm(q (~(put by q.farm) i.path u.res))
::
++ put-tend
|= [=path =plot]
^- (unit _farm)
?: ?=(%coop -.farm)
`farm(q (~(put by q.farm) path plot))
?~ path
`farm(p `plot)
?~ nex=(~(get by q.farm) i.path)
~
=/ res
$(path t.path, farm u.nex)
?~ res ~
`farm(q (~(put by q.farm) i.path u.res))
::
++ grow
|= [=spur now=@da =page]
=/ ski (gut spur)
%+ put spur
=- ski(fan (put:on-path fan.ski -< -> &/page))
?~ las=(ram:on-path fan.ski)
[?~(bob.ski 1 +(u.bob.ski)) now]
:_ (max now +(p.val.u.las))
?~(bob.ski +(key.u.las) +((max key.u.las u.bob.ski)))
::
++ germ
|= [=coop =hutch]
^- (unit _farm)
?~ coop
?. |(=(%coop -.farm) =([%page ~ ~] farm))
~
`[%coop hutch ~]
?: ?=(%coop -.farm)
~
?~ nex=(~(get by q.farm) i.coop)
~
$(coop t.coop, farm u.nex)
::
++ tend
|= [=coop =path =plot]
^- (unit _farm)
?~ coop
?. ?=(%coop -.farm)
~
`farm(q (~(put by q.farm) path plot))
?. ?=(%plot -.farm)
~
?~ nex=(~(get by q.farm) i.coop)
~
$(coop t.coop, farm u.nex)
::
++ del
|= =path
^+ farm
?: ?=(%coop -.farm)
farm(q (~(del by q.farm) path))
?~ path
farm(p ~)
?~ nex=(~(get by q.farm) i.path)
farm
$(path t.path, farm u.nex)
::
++ gut
|= =path
^- plot
(fall (get path) *plot)
::
++ put-hutch
|= [=path =hutch]
^- (unit _farm)
?~ path
?: ?=(%coop -.farm)
`farm(p hutch)
?. =([%plot ~ ~] farm)
~
`[%coop hutch ~]
?: ?=(%coop -.farm)
~
=/ nex (~(gut by q.farm) i.path *^farm)
=/ res $(path t.path, farm nex)
?~ res ~
`farm(q (~(put by q.farm) i.path u.res))
::
++ get-hutch
|= =path
^- (unit hutch)
?~ path
?. ?=(%coop -.farm)
~
`p.farm
?: ?=(%coop -.farm)
~
?~ nex=(~(get by q.farm) i.path)
~
$(path t.path, farm u.nex)
::
++ get-farm
|= =path
^- (unit ^farm)
?: ?=(%coop -.farm)
?~ (~(get by q.farm) path)
~
`farm
?~ path ~
?~ nex=(~(get by q.farm) i.path)
~
$(path t.path, farm u.nex)
::
++ get
|= =path
^- (unit plot)
?: ?=(%coop -.farm)
(~(get by q.farm) path)
?~ path
p.farm
?~ nex=(~(get by q.farm) i.path)
~
$(path t.path, farm u.nex)
::
++ tap-plot
=| wer=path
|- ^- (list [path plot])
=* tap-plot $
?: ?=(%coop -.farm)
%+ turn ~(tap by q.farm)
|= [=path =plot]
[(welp wer path) plot]
%+ welp ?~(p.farm ~ [wer u.p.farm]~)
%- zing
%+ turn ~(tap by q.farm)
|= [seg=@ta f=^farm]
^- (list [path plot])
tap-plot(wer (snoc wer seg), farm f)
::
++ run-plot
|* fun=gate
%- ~(gas by *(map path _(fun)))
%+ turn tap-plot
|= [=path =plot]
[path (fun plot)]
::
++ gas-hutch
|= =(list [=coop =hutch])
^- (unit _farm)
?~ list
`farm
=/ nex
(put-hutch i.list)
?~ nex ~
$(farm u.nex, list t.list)
::
++ tap-hutch
=| wer=path
%- ~(gas in *(set [=coop =hutch]))
|- ^- (list [=coop =hutch])
=* loop $
?: ?=(%coop -.farm)
[wer p.farm]~
%- zing
%+ turn ~(tap by q.farm)
|= [seg=@ta f=^farm]
^- (list [=coop =hutch])
loop(wer (snoc wer seg), farm f)
--
::
++ on-path ((on @ud (pair @da (each page @uvI))) lte)
:: $blocked-move: enqueued move to an agent
::
+$ blocked-move [=duct =routes move=(each deal unto)]
::
:: $fine-request: key exchange request for $coop
::
+$ fine-request
[%0 =path]
::
:: $fine-response: key exchange response for $coop
::
+$ fine-response
[%0 bod=(unit brood)]
::
+$ ames-response
$% [%d =mark noun=*]
[%x ~]
==
::
:: $ames-request: network request (%plea)
::
:: %m: poke
:: %l: watch-as
:: %s: watch
:: %u: leave
::
+$ ames-request-all
$% [%0 ames-request]
==
+$ ames-request
$% [%m =mark noun=*]
[%l =mark =path]
[%s =path]
[%u ~]
==
:: $remote-request: kinds of agent actions that can cross the network
::
:: Used in wires to identify the kind of remote request we made.
:: Bijective with the tags of $ames-request.
::
+$ remote-request
$? %watch
%watch-as
%poke
%leave
%missing
==
:: |migrate: data structures for upgrades
::
+| %migrate
::
:: $spore: structures for update, produced by +stay
::
:: remember to duplicate version tag changes here to $egg-any:gall in lull
::
+$ spore
$: system-duct=duct
outstanding=(map [wire duct] (qeu remote-request))
contacts=(set ship)
eggs=(map term egg)
blocked=(map term (qeu blocked-move))
=bug
leaves=(unit [=duct =wire date=@da])
==
+$ spore-16 [%16 spore]
--
:: adult gall vane interface, for type compatibility with pupa
::
=| state=state-16
|= [now=@da eny=@uvJ rof=roof]
=* gall-payload .
~% %gall-top ..part ~
|%
:: +mo: Arvo-level move handling
::
:: An outer core responsible for routing moves to and from Arvo; it calls
:: an inner core, +ap, to route internal moves to and from agents.
::
++ mo
~% %gall-mo +> ~
|_ [hen=duct moves=(list move)]
::
++ trace
|= [verb=? =dude print=tang]
^+ same
(^trace verb dude dudes.bug.state print)
::
:: +mo-abed: initialise state with the provided duct
:: +mo-abet: finalize, reversing moves
:: +mo-pass: prepend a standard %pass to the current list of moves
:: +mo-give: prepend a standard %give to the current list of moves
:: +mo-talk: build task to print config report or failure trace
::
++ mo-core .
++ mo-abed |=(hun=duct mo-core(hen hun))
++ mo-abet [(flop moves) gall-payload]
++ mo-emit |=(=move mo-core(moves [move moves]))
++ mo-give |=(=gift (mo-emit hen give+gift))
++ mo-talk
|= rup=(each suss tang)
^- [wire note-arvo]
:+ /sys/say %d
^- task:dill
?- -.rup
%& [%text "gall: {(t q)}ed %{(t p)}":[t=trip p.rup]]
%| [%talk leaf+"gall: failed" (flop p.rup)]
==
++ mo-pass |=(p=[wire note-arvo] (mo-emit hen pass+p))
++ mo-slip |=(p=note-arvo (mo-emit hen slip+p))
++ mo-past
|= =(list [wire note-arvo])
?~ list
mo-core
=. mo-core (mo-pass i.list)
$(list t.list)
:: +mo-jolt: (re)start agent
::
++ mo-jolt
|= [dap=term =ship =desk]
^+ mo-core
=/ =wire /sys/cor/[dap]/(scot %p ship)/[desk]
..mo-core
:: XX (mo-pass wire %c %jolt dap ship desk)
:: +mo-doff: kill all outgoing subscriptions
::
++ mo-doff
|= [prov=path dude=(unit dude) ship=(unit ship)]
^+ mo-core
=/ apps=(list (pair term yoke))
?~ dude ~(tap by yokes.state)
(drop (bind (~(get by yokes.state) u.dude) (lead u.dude)))
|- ^+ mo-core
?~ apps mo-core
?: ?=(%nuke -.q.i.apps) $(apps t.apps)
=/ ap-core (ap-yoke:ap p.i.apps [~ our prov] q.i.apps)
$(apps t.apps, mo-core ap-abet:(ap-doff:ap-core ship))
:: +mo-rake: send %cork's for old subscriptions if needed
::
++ mo-rake
|= [prov=path dude=(unit dude) all=?]
^+ mo-core
=/ apps=(list (pair term yoke))
?~ dude ~(tap by yokes.state)
(drop (bind (~(get by yokes.state) u.dude) (lead u.dude)))
|- ^+ mo-core
?~ apps mo-core
?: ?=(%nuke -.q.i.apps) $(apps t.apps)
=/ ap-core (ap-yoke:ap p.i.apps [~ our prov] q.i.apps)
$(apps t.apps, mo-core ap-abet:(ap-rake:ap-core all))
:: +mo-receive-core: receives an app core built by %ford.
::
:: Presuming we receive a good core, we first check to see if the agent
:: is already running. If so, we update its beak in %gall's state,
:: initialise an +ap core for the agent, install the core we got from
:: %ford, and then resolve any moves associated with it.
::
:: If we're dealing with a new agent, we create one using the result we
:: got from %ford, add it to the collection of agents %gall is keeping
:: track of, and then do more or less the same procedure as we did for the
:: running agent case.
::
++ mo-receive-core
~/ %mo-receive-core
|= [prov=path dap=term bek=beak =agent]
^+ mo-core
::
=/ yak (~(get by yokes.state) dap)
=/ tex=(unit tape)
?~ yak `"installing"
?: ?=(%nuke -.u.yak) `"unnuking" ::TODO good message here?
?- -.agent.u.yak
%| `"reviving"
%&
?: =(code.u.yak agent)
~
`"reloading"
==
=+ ?~ tex ~
~> %slog.[0 leaf+"gall: {u.tex} {<dap>}"] ~
::
?: ?=([~ %live *] yak)
?: &(=(q.beak.u.yak q.bek) =(code.u.yak agent) =(-.agent.u.yak &))
mo-core
::
=. yokes.state
(~(put by yokes.state) dap u.yak(beak bek, code agent))
=/ ap-core (ap-abed:ap dap [~ our prov])
=. ap-core (ap-reinstall:ap-core agent)
=. mo-core ap-abet:ap-core
(mo-clear-queue dap)
::
=. yokes.state
%+ ~(put by yokes.state) dap
%* . *$>(%live yoke)
control-duct hen
beak bek
code agent
agent &+agent
run-nonce (scot %uw (end 5 (shas %yoke-nonce eny)))
::
sky
?~ yak *farm
=| =farm
=. farm (need (~(gas-hutch of-farm farm) ~(tap by cop.u.yak)))
=/ sky=(list [=spur bob=@ud]) ~(tap by sky.u.yak)
|-
?~ sky farm
=. farm (need (~(put-grow of-farm farm) spur.i.sky [`bob.i.sky ~]))
$(sky t.sky)
==
::
=/ old mo-core
=/ wag
=/ ap-core (ap-abed:ap dap [~ our prov])
(ap-upgrade-state:ap-core ~)
::
=/ maybe-tang -.wag
=/ ap-core +.wag
?^ maybe-tang
=. mo-core old
(mo-pass (mo-talk %.n u.maybe-tang))
::
=. mo-core ap-abet:ap-core
=. mo-core (mo-clear-queue dap)
=/ =suss [dap %boot now]
(mo-pass (mo-talk %.y suss))
:: +mo-send-foreign-request: handle local request to .ship
::
++ mo-send-foreign-request
~/ %mo-send-foreign-request
|= [=ship foreign-agent=term =deal]
^+ mo-core
::
=. mo-core (mo-track-ship ship)
?< ?=(?(%raw-poke %poke-as) -.deal)
=/ =ames-request-all
:- %0
?- -.deal
%poke [%m p.cage.deal q.q.cage.deal]
%leave [%u ~]
%watch-as [%l [mark path]:deal]
%watch [%s path.deal]
==
::
=/ wire
/sys/way/(scot %p ship)/[foreign-agent]
::
=/ =note-arvo
=/ =path /ge/[foreign-agent]
[%a %plea ship %g path ames-request-all]
::
=. outstanding.state
=/ stand
(~(gut by outstanding.state) [wire hen] *(qeu remote-request))
(~(put by outstanding.state) [wire hen] (~(put to stand) -.deal))
(mo-pass wire note-arvo)
:: +mo-track-ship: subscribe to ames and jael for notices about .ship
::
++ mo-track-ship
|= =ship
^+ mo-core
:: if already contacted, no-op
::
?: (~(has in contacts.state) ship)
mo-core
:: first contact; update state and subscribe to notifications
::
=. contacts.state (~(put in contacts.state) ship)
:: ask ames to track .ship's connectivity
::
=. moves [[system-duct.state %pass /sys/lag %a %heed ship] moves]
:: ask jael to track .ship's breaches
::
=/ =note-arvo [%j %public-keys (silt ship ~)]
=. moves
[[system-duct.state %pass /sys/era note-arvo] moves]
mo-core
:: +mo-untrack-ship: cancel subscriptions to ames and jael for .ship
::
++ mo-untrack-ship
|= =ship
^+ mo-core
:: if already canceled, no-op
::
?. (~(has in contacts.state) ship)
mo-core
:: delete .ship from state and kill subscriptions
::
=. contacts.state (~(del in contacts.state) ship)
::
=. moves [[system-duct.state %pass /sys/lag %a %jilt ship] moves]
::
=/ =note-arvo [%j %nuke (silt ship ~)]
=. moves
[[system-duct.state %pass /sys/era note-arvo] moves]
mo-core
:: +mo-breach: ship breached, so forget about them
::
++ mo-breach
|= [prov=path =ship]
^+ mo-core
=. mo-core (mo-untrack-ship ship)
=. mo-core (mo-filter-queue ship)
=/ agents=(list [name=term =yoke]) ~(tap by yokes.state)
=. outstanding.state
%- malt
%+ skip ~(tap by outstanding.state)
|= [[=wire duct] (qeu remote-request)]
=(/sys/way/(scot %p ship) (scag 3 wire))
::
|- ^+ mo-core
?~ agents
mo-core
=? mo-core ?=(%live -.yoke.i.agents)
=/ =routes [disclosing=~ attributing=[ship prov]]
=/ app (ap-abed:ap name.i.agents routes)
ap-abet:(ap-breach:app ship)
$(agents t.agents)
:: +mo-handle-sys: handle a +sign incoming over /sys.
::
:: (Note that /sys implies the +sign should be routed to a vane.)
::
++ mo-handle-sys
~/ %mo-handle-sys
|= [=wire =sign-arvo]
^+ mo-core
::
?+ -.wire !!
%lyv ..mo-core :: vestigial
%cor ..mo-core :: vestigial
%era (mo-handle-sys-era wire sign-arvo)
%lag (mo-handle-sys-lag wire sign-arvo)
%req (mo-handle-sys-req wire sign-arvo)
%way (mo-handle-sys-way wire sign-arvo)
==
:: +mo-handle-sys-era: receive update about contact
::
++ mo-handle-sys-era
|= [=wire =sign-arvo]
^+ mo-core
?> ?=([%jael %public-keys *] sign-arvo)
?> ?=([%era ~] wire)
?. ?=(%breach -.public-keys-result.sign-arvo)
mo-core
(mo-breach /jael who.public-keys-result.sign-arvo)
:: +mo-handle-sys-lag: handle an ames %clog notification
::
++ mo-handle-sys-lag
|= [=wire =sign-arvo]
^+ mo-core
::
?> ?=([%lag ~] wire)
?> ?=([%ames %clog *] sign-arvo)
::
=/ agents=(list [=dude =yoke]) ~(tap by yokes.state)
|- ^+ mo-core
?~ agents mo-core
::
=? mo-core ?=(%live -.yoke.i.agents)
=/ app (ap-abed:ap dude.i.agents [~ our /ames])
ap-abet:(ap-clog:app ship.sign-arvo)
::
$(agents t.agents)
:: +mo-handle-sys-req: TODO description
::
:: TODO: what should we do if the remote nacks our %pull?
++ mo-handle-sys-req
|= [=wire =sign-arvo]
^+ mo-core
::
?> ?=([%req @ @ ~] wire)
=/ him (slav %p i.t.wire)
=/ dap i.t.t.wire
::
?> ?=([?(%gall %behn) %unto *] sign-arvo)
=/ =unto +>.sign-arvo
::
?- -.unto
%raw-fact ~|([%gall-raw-req wire] !!)
%poke-ack
=/ err=(unit error:ames)
?~ p.unto ~
`[%poke-ack u.p.unto]
(mo-give %done err)
::
%fact
=+ [mark noun]=[p q.q]:cage.unto
(mo-give %boon %d mark noun)
::
%kick
(mo-give %boon %x ~)
::
%watch-ack
=/ err=(unit error:ames)
?~ p.unto ~
`[%watch-ack u.p.unto]
(mo-give %done err)
==
:: +mo-handle-sys-way: handle response to outgoing remote request
::
++ mo-handle-sys-way
|= [=wire =sign-arvo]
^+ mo-core
?> ?=([%way @ @ $@(~ [@ ~])] wire)
=/ =ship (slav %p i.t.wire)
=/ foreign-agent i.t.t.wire
::
?+ sign-arvo !!
[%ames %done *]
=/ err=(unit tang)
?~ error=error.sign-arvo
~
`[[%leaf (trip tag.u.error)] tang.u.error]
=^ remote-request outstanding.state
?~ t.t.t.wire
=/ full-wire sys+wire
=/ stand
(~(gut by outstanding.state) [full-wire hen] ~)
::
:: default is to send both ack types; should only hit if
:: cleared queue in +load 3-to-4 or +load-4-to-5
::
=? stand ?=(~ stand)
~& [%gall-missing wire hen]
(~(put to *(qeu remote-request)) %missing)
~| [full-wire=full-wire hen=hen stand=stand]
=^ rr stand ~(get to stand)
:- rr
?: =(~ stand)
:: outstanding leaves are only deleted when acked
::
?: &(?=(^ err) ?=(%leave rr))
outstanding.state
(~(del by outstanding.state) [full-wire hen])
(~(put by outstanding.state) [full-wire hen] stand)
:: non-null case of wire is old, remove on next breach after
:: 2019/12
::
[;;(remote-request i.t.t.t.wire) outstanding.state]
:: send a %cork if we get a %nack upon initial subscription
::
=? mo-core
&(?=(^ err) |(?=(%watch-as remote-request) ?=(%watch remote-request)))
(mo-pass sys+wire %a %cork ship)
::
?- remote-request
%watch-as (mo-give %unto %watch-ack err)
%watch (mo-give %unto %watch-ack err)
%poke (mo-give %unto %poke-ack err)
%missing ~>(%slog.[3 'gall: missing'] mo-core)
::
%leave
:: if we get an %ack for a %leave, send %cork. otherwise,
:: the /nacked-leaves timer will re-send the %leave eventually.
::
?~ err
(mo-pass sys+wire %a %cork ship)
:: if first time hearing a %nack for a %leave, after upgrade
:: or if all outstanding %leaves have been handled, set up timer
::
=? mo-core ?=(~ leaves.state)
(mo-emit [/gall]~ %pass /nacked-leaves %b %wait `@da`(add now ~m2))
=? leaves.state ?=(~ leaves.state)
`[[/gall]~ /nacked-leaves `@da`(add now ~m2)]
mo-core
==
::
[%ames %boon *]
?^ t.t.t.wire
:: kill subscriptions which use the old wire format
::
!!
=/ =ames-response ;;(ames-response payload.sign-arvo)
:: %d: diff; ask clay to validate .noun as .mark
:: %x: kick; tell agent the publisher canceled the subscription, and
:: cork; tell ames to close the associated flow.
::
?- -.ames-response
%d (mo-give %unto %raw-fact mark.ames-response noun.ames-response)
%x =. mo-core (mo-give %unto %kick ~)
=/ key [[%sys wire] hen]
=? outstanding.state =(~ (~(gut by outstanding.state) key ~))
(~(del by outstanding.state) key)
(mo-pass [%sys wire] %a %cork ship)
==
::
[%ames %lost *]
:: note this should only happen on reverse bones, so only facts
:: and kicks
::
:: TODO: %drip %kick so app crash can't kill the remote %pull
::
=. mo-core (mo-send-foreign-request ship foreign-agent %leave ~)
=. mo-core (mo-give %unto %kick ~)
mo-core
==
++ mo-handle-key
~/ %mo-handle-stub
|= [=(pole knot) syn=sign-arvo]
?. ?=([agent=@ nonce=@ rest=*] pole)
~& [%mo-handle-key-bad-wire wire]
!!
=* dap agent.pole
=/ yoke (~(get by yokes.state) agent.pole)
?. ?=([~ %live *] yoke)
%- (slog leaf+"gall: {<`@t`dap>} dead, got %stub" ~)
mo-core
?. =(run-nonce.u.yoke nonce.pole)
%- (slog leaf+"gall: got old stub for {<dap>}" ~)
mo-core
=/ =routes [disclosing=~ attributing=[our /]]
=/ ap-core (ap-abed:ap agent.pole routes)
?+ rest.pole ~|(mo-handle-key-bad-wire/wire !!)
[%pug rest=*]
?> ?=([%ames %stub *] syn)
ap-abet:(ap-stub:ap-core rest.rest.pole [num key]:syn)
::
[%bod rest=*]
ap-abet:(ap-take-brood:ap-core rest.rest.pole syn)
==
:: +mo-handle-use: handle a typed +sign incoming on /use.
::
:: (Note that /use implies the +sign should be routed to an agent.)
::
:: Initialises the specified agent and then performs an agent-level
:: +take on the supplied +sign.
::
++ mo-handle-use
~/ %mo-handle-use
|= [=wire =sign-arvo]
^+ mo-core
::
?. ?=([@ @ @ *] wire)
~& [%mo-handle-use-bad-wire wire]
!!
::
=/ dap=term i.wire
=/ yoke (~(get by yokes.state) dap)
?. ?=([~ %live *] yoke)
%- (slog leaf+"gall: {<dap>} dead, got {<+<.sign-arvo>}" ~)
mo-core
?. =(run-nonce.u.yoke i.t.wire)
%- (slog leaf+"gall: got old {<+<.sign-arvo>} for {<dap>}" ~)
mo-core
::
?. ?=([?(%gall %behn) %unto *] sign-arvo)
?: ?=(%| -.agent.u.yoke)
%- (slog leaf+"gall: {<dap>} dozing, dropping {<+<.sign-arvo>}" ~)
mo-core
=/ app
=/ =ship (slav %p i.t.t.wire)
=/ =routes [disclosing=~ attributing=[ship /[-.sign-arvo]]]
(ap-abed:ap dap routes)
::
=. app (ap-generic-take:app t.t.t.wire sign-arvo)
ap-abet:app
?> ?=([%out @ @ *] t.t.wire)
=/ =ship (slav %p i.t.t.t.wire)
=/ other-agent i.t.t.t.t.wire
=/ prov=path ?.(=(ship our) *path /gall/[other-agent])
=/ =routes [disclosing=~ attributing=[ship prov]]
=/ =unto +>.sign-arvo
?: ?=(%| -.agent.u.yoke)
=/ blocked=(qeu blocked-move)
=/ waiting (~(get by blocked.state) dap)
=/ deals (fall waiting *(qeu blocked-move))
=/ deal [hen routes |+unto]
(~(put to deals) deal)
::
%- (slog leaf+"gall: {<dap>} dozing, got {<-.unto>}" ~)
%_ mo-core
blocked.state (~(put by blocked.state) dap blocked)
==
=/ app (ap-abed:ap dap routes)
=. app
(ap-specific-take:app t.t.wire unto)
ap-abet:app
:: +mo-clear-queue: clear blocked tasks from the specified running agent.
::
++ mo-clear-queue
|= dap=term
^+ mo-core
?. (~(has by yokes.state) dap)
mo-core
?~ maybe-blocked=(~(get by blocked.state) dap)
mo-core
=/ blocked=(qeu blocked-move) u.maybe-blocked
|- ^+ mo-core
?: =(~ blocked)
=. blocked.state (~(del by blocked.state) dap)
mo-core
=^ [=duct =routes blocker=(each deal unto)] blocked
~(get to blocked)
?: ?=(%| -.blocker) $
=/ =move
=/ =sack [ship.attributing.routes our path.attributing.routes]
=/ card [%slip %g %deal sack dap p.blocker]
[duct card]
$(moves [move moves])
:: +mo-filter-queue: remove all blocked tasks from ship.
::
++ mo-filter-queue
|= =ship
=/ agents=(list [name=term blocked=(qeu blocked-move)])
~(tap by blocked.state)
=| new-agents=(map term (qeu blocked-move))
|- ^+ mo-core
?~ agents
mo-core(blocked.state new-agents)
=| new-blocked=(qeu blocked-move)
|- ^+ mo-core
?: =(~ blocked.i.agents)
?~ new-blocked
^$(agents t.agents)
%= ^$
agents t.agents
new-agents (~(put by new-agents) name.i.agents new-blocked)
==
=^ mov=blocked-move blocked.i.agents ~(get to blocked.i.agents)
=? new-blocked !=(ship ship.attributing.routes.mov)
(~(put to new-blocked) mov)
$
:: +mo-idle: put agent to sleep
::
++ mo-idle
|= [prov=path dap=dude]
^+ mo-core
=/ yoke=(unit yoke) (~(get by yokes.state) dap)
?: |(?=(~ yoke) ?=(%nuke -.u.yoke))
~> %slog.0^leaf/"gall: ignoring %idle for {<dap>}, not running"
mo-core
ap-abet:ap-idle:(ap-abed:ap dap [~ our prov])
:: +mo-nuke: delete agent completely
::
++ mo-nuke
|= [prov=path dap=dude]
^+ mo-core
=/ yoke=(unit yoke) (~(get by yokes.state) dap)
?: |(?=(~ yoke) ?=(%nuke -.u.yoke))
~> %slog.0^leaf/"gall: ignoring %nuke for {<dap>}, not running"
mo-core
~> %slog.0^leaf/"gall: nuking {<dap>}"
=. mo-core ap-abet:ap-nuke:(ap-abed:ap dap [~ our prov])
=- mo-core(yokes.state -)
%+ ~(jab by yokes.state) dap
|= =^yoke
?: ?=(%nuke -.yoke) yoke
:+ %nuke
%- ~(run-plot of-farm sky.yoke)
|= plot
(fall (clap bob (bind (ram:on-path fan) head) max) 0)
~(tap-hutch of-farm sky.yoke)
:: +mo-load: install agents
::
++ mo-load
|= [prov=path agents=(list [=dude =beak =agent])]
=. mo-core
|- ^+ mo-core
?~ agents mo-core
=/ [=dude =desk] [dude q.beak]:i.agents
:: ~> %slog.0^leaf/"gall: starting {<dude>} on {<desk>}"
$(agents t.agents, mo-core (mo-receive-core prov i.agents))
::
=/ kil
=/ lol
(skim ~(tap by yokes.state) |=([* y=yoke] &(?=(%live -.y) -.agent.y)))
=/ mol (~(gas by *(map term yoke)) lol)
=/ sol ~(key by mol)
=/ new (silt (turn agents head))
~(tap in (~(dif in sol) new))
|- ^+ mo-core
?~ kil mo-core