samagotchi 0.190.0 → 0.191.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +38 -1
- data/docs/architecture.md +8 -7
- data/docs/cli.md +6 -3
- data/docs/context.md +9 -2
- data/docs/guardrails.md +5 -1
- data/docs/internals/attached-context.md +3 -1
- data/docs/internals/peers.md +15 -3
- data/docs/internals/remote-sessions.md +2 -2
- data/docs/internals/session-lifecycle.md +213 -12
- data/docs/peers.md +3 -1
- data/docs/plugins.md +2 -1
- data/docs/scheduling.md +31 -7
- data/docs/sessions.md +11 -11
- data/docs/sub-agent.md +1 -1
- data/lib/samagotchi/bridge.rb +1 -1
- data/lib/samagotchi/bundles/known-names/manifest.yml +2 -2
- data/lib/samagotchi/bundles/known-names/plugin.rb +17 -1
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/context_command.rb +5 -1
- data/lib/samagotchi/context_sources.rb +28 -0
- data/lib/samagotchi/continue_intent.rb +23 -43
- data/lib/samagotchi/hold_stop.rb +5 -2
- data/lib/samagotchi/participants.rb +13 -5
- data/lib/samagotchi/peer_say.rb +29 -14
- data/lib/samagotchi/plugin/sessions.rb +9 -5
- data/lib/samagotchi/schedule_command.rb +5 -1
- data/lib/samagotchi/schedules/prune_part.rb +138 -38
- data/lib/samagotchi/schedules.rb +29 -6
- data/lib/samagotchi/send_command.rb +6 -6
- data/lib/samagotchi/session_access/ask.rb +9 -3
- data/lib/samagotchi/session_access/exchange.rb +6 -2
- data/lib/samagotchi/session_access/local.rb +104 -31
- data/lib/samagotchi/session_access/remote.rb +10 -9
- data/lib/samagotchi/session_access/remote_peers.rb +2 -2
- data/lib/samagotchi/session_access.rb +17 -7
- data/lib/samagotchi/session_chain/delete.rb +100 -47
- data/lib/samagotchi/session_chain/delete_preview.rb +9 -5
- data/lib/samagotchi/session_chain/links.rb +68 -9
- data/lib/samagotchi/session_chain/successor.rb +78 -0
- data/lib/samagotchi/session_continue.rb +108 -40
- data/lib/samagotchi/session_inbox.rb +1 -1
- data/lib/samagotchi/session_inbox_delivery.rb +113 -26
- data/lib/samagotchi/session_lifecycle/allowed.rb +20 -1
- data/lib/samagotchi/session_lifecycle/reading.rb +9 -3
- data/lib/samagotchi/session_lifecycle.rb +13 -2
- data/lib/samagotchi/session_manager/delivery.rb +61 -23
- data/lib/samagotchi/session_manager/emptiness.rb +5 -2
- data/lib/samagotchi/session_manager/lifecycle.rb +50 -14
- data/lib/samagotchi/session_manager/removal.rb +65 -13
- data/lib/samagotchi/session_manager/spawn.rb +14 -6
- data/lib/samagotchi/session_retention.rb +28 -7
- data/lib/samagotchi/session_transition.rb +315 -0
- data/lib/samagotchi/sessions_command.rb +1 -1
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +10 -2
- data/lib/samagotchi/web/peer_routes.rb +5 -2
- data/lib/samagotchi/web/session_access_routes.rb +5 -2
- data/lib/samagotchi/web/session_control.rb +6 -1
- data/lib/samagotchi/web/session_create.rb +1 -1
- data/lib/samagotchi/worker_boot.rb +28 -6
- metadata +3 -1
|
@@ -21,6 +21,8 @@ require_relative "../peer_roster"
|
|
|
21
21
|
require_relative "../peer_say"
|
|
22
22
|
require_relative "../archive_store"
|
|
23
23
|
require_relative "../session_chain"
|
|
24
|
+
require_relative "../session_chain/successor"
|
|
25
|
+
require_relative "../session_transition"
|
|
24
26
|
require_relative "../send_command/busy"
|
|
25
27
|
require_relative "../chi_invocation"
|
|
26
28
|
require_relative "../worker_sidecar"
|
|
@@ -294,13 +296,14 @@ module Samagotchi
|
|
|
294
296
|
# @return [BridgeClient::Response, nil] the Bridge's reply; nil when
|
|
295
297
|
# no worker runs it
|
|
296
298
|
# @raise [SystemCallError, IOError, SocketError] the worker went away
|
|
299
|
+
# Transition hold: worker-side (Bridge-only: the live worker's own question).
|
|
297
300
|
def answer(id, question_id:, selected:, freeform:, client_id:, within: nil)
|
|
298
301
|
on_worker(id, within: within) do |client|
|
|
299
302
|
client.answer(id: question_id, selected: selected, freeform: freeform, client_id: client_id)
|
|
300
303
|
end
|
|
301
304
|
end
|
|
302
305
|
|
|
303
|
-
# The question dismissed on the session's worker; as #answer.
|
|
306
|
+
# The question dismissed on the session's worker; as #answer (worker-side, no hold).
|
|
304
307
|
def dismiss(id, question_id:)
|
|
305
308
|
on_worker(id) { |client| client.dismiss_question(id: question_id) }
|
|
306
309
|
end
|
|
@@ -363,24 +366,48 @@ module Samagotchi
|
|
|
363
366
|
|
|
364
367
|
# Attaches +source+ to the target's own sources.
|
|
365
368
|
# @return [ContextAdded]
|
|
369
|
+
# A session's own: under its transition lock (ContextSources.changing).
|
|
370
|
+
# @raise [ContextSources::Held] a transition holds the session's lock
|
|
366
371
|
def context_add(target, source)
|
|
367
372
|
location = context_location(target)
|
|
368
|
-
location.add(source.with(scope: location.scope))
|
|
373
|
+
own_change(target) { location.add(source.with(scope: location.scope)) }
|
|
369
374
|
ContextAdded.new(name: source.name)
|
|
370
375
|
end
|
|
371
376
|
|
|
372
377
|
# New text for the target's --push source +name+. Refused, nothing
|
|
373
378
|
# written, when the session was continued (its writer holds a stale
|
|
374
379
|
# id): a source of its own, or none of the name. A project's source
|
|
375
|
-
# takes it (the next link sees it too).
|
|
380
|
+
# takes it (the next link sees it too). The session's own source is
|
|
381
|
+
# checked again and written under a shared hold of its transition lock
|
|
382
|
+
# (SessionTransition.shared), so a continue (which moves its sources)
|
|
383
|
+
# can't start in between; one holding the lock now refuses it, naming
|
|
384
|
+
# its new link (:continued, in flight) or :busy.
|
|
376
385
|
# @param fetched [ContextSources::Fetched]
|
|
377
386
|
# @return [ContextPush]
|
|
378
387
|
def context_push(target, name, fetched)
|
|
379
388
|
attached = context_attached(target, name)
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
389
|
+
sid = target.session_id
|
|
390
|
+
own = !target.project? && (attached.nil? || attached.location.session?)
|
|
391
|
+
return context_push_to(target, name, fetched, attached) unless own
|
|
392
|
+
|
|
393
|
+
if (successor = continued_as(sid))
|
|
394
|
+
return ContextPush.new(status: :continued, next_id: successor.id, in_flight: successor.in_flight?)
|
|
395
|
+
end
|
|
396
|
+
# Its file is gone: no hold (it would make the folder again).
|
|
397
|
+
return context_push_to(target, name, fetched, attached) unless Session.exist?(sid, **dir)
|
|
398
|
+
|
|
399
|
+
SessionTransition.shared(sid, state_dir: state_dir!) do |free|
|
|
400
|
+
case (moved = moved_on(sid, free))
|
|
401
|
+
when :busy then next ContextPush.new(status: :busy)
|
|
402
|
+
when SessionChain::Successor
|
|
403
|
+
next ContextPush.new(status: :continued, next_id: moved.id, in_flight: moved.in_flight?)
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
context_push_to(target, name, fetched, attached)
|
|
383
407
|
end
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
private def context_push_to(target, name, fetched, attached)
|
|
384
411
|
return ContextPush.new(status: :missing) unless attached
|
|
385
412
|
return ContextPush.new(status: :not_push) unless attached.source.push?
|
|
386
413
|
|
|
@@ -437,9 +464,10 @@ module Samagotchi
|
|
|
437
464
|
# Detaches the target's own source +name+.
|
|
438
465
|
# @return [Symbol] :removed, :project (a session's project has it:
|
|
439
466
|
# rm --project, or mute it) or :missing
|
|
467
|
+
# @raise [ContextSources::Held] a transition holds the session's lock
|
|
440
468
|
def context_remove(target, name)
|
|
441
469
|
ContextSources.check_name!(name)
|
|
442
|
-
return :removed if context_location(target).remove(name)
|
|
470
|
+
return :removed if own_change(target) { context_location(target).remove(name) }
|
|
443
471
|
|
|
444
472
|
project = !target.project? && ContextSources.project_location_for(target.project_root,
|
|
445
473
|
project_id: target.project_id,
|
|
@@ -519,6 +547,7 @@ module Samagotchi
|
|
|
519
547
|
# @return [String, nil]
|
|
520
548
|
def identity_header = configured_public_url&.identity_header
|
|
521
549
|
|
|
550
|
+
# Transition hold: none (a removal; a continue's archive ends every grant).
|
|
522
551
|
# @return [Array<ParticipantView>] the ones it revoked
|
|
523
552
|
def revoke_participants(id = nil, participant: nil)
|
|
524
553
|
Participants.revoke(grants, session_id: id, id: participant, state_dir: state_dir!)
|
|
@@ -541,30 +570,41 @@ module Samagotchi
|
|
|
541
570
|
# Pairs +peer+ with session +id+ (link.json). Refused, nothing
|
|
542
571
|
# written, when the session was continued or a live worker of it
|
|
543
572
|
# doesn't take the peer mail (a pairing to it would look done and
|
|
544
|
-
# not work).
|
|
573
|
+
# not work). The pairing is written under a shared hold of the
|
|
574
|
+
# session's transition lock, checked again there (#moved_on): a
|
|
575
|
+
# continue holding it refuses the pair, naming its new link.
|
|
545
576
|
# @return [PeerPaired]
|
|
546
577
|
# @raise [PeerMailbox::Invalid] a bad name
|
|
547
578
|
# @raise [Session::NotFound] no such session
|
|
548
579
|
def peer_pair(id, peer, address:, from:)
|
|
549
580
|
PeerMailbox.check_name!(peer)
|
|
550
581
|
raise Session::NotFound, id unless Session.exist?(id, **dir)
|
|
551
|
-
if (
|
|
552
|
-
return PeerPaired.new(status: :continued, next_id:
|
|
582
|
+
if (successor = continued_as(id))
|
|
583
|
+
return PeerPaired.new(status: :continued, next_id: successor.id, in_flight: successor.in_flight?)
|
|
553
584
|
end
|
|
554
585
|
return PeerPaired.new(status: :old_worker) if old_peer_worker?(id)
|
|
555
586
|
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
587
|
+
SessionTransition.shared(id, state_dir: state_dir!) do |free|
|
|
588
|
+
case (moved = moved_on(id, free))
|
|
589
|
+
when :busy then next PeerPaired.new(status: :busy)
|
|
590
|
+
when SessionChain::Successor
|
|
591
|
+
next PeerPaired.new(status: :continued, next_id: moved.id, in_flight: moved.in_flight?)
|
|
592
|
+
end
|
|
593
|
+
|
|
594
|
+
link = PeerMailbox.pair(id, peer, address: address, from: from, state_dir: state_dir!)
|
|
595
|
+
location = ContextSources.session_location(id, state_dir: state_dir!)
|
|
596
|
+
# These are exactly the bundles' LEGACY_INBOX and LEGACY_OUTBOX names.
|
|
597
|
+
["#{peer}-inbox", "#{peer}-outbox"].each do |name|
|
|
598
|
+
location.remove(name)
|
|
599
|
+
rescue SystemCallError
|
|
600
|
+
nil
|
|
601
|
+
end
|
|
602
|
+
PeerPaired.new(status: :paired, link: link)
|
|
563
603
|
end
|
|
564
|
-
PeerPaired.new(status: :paired, link: link)
|
|
565
604
|
end
|
|
566
605
|
|
|
567
|
-
# Ends the pairing (link.json); the messages stay.
|
|
606
|
+
# Ends the pairing (link.json); the messages stay. Transition hold: none
|
|
607
|
+
# (a removal; the new link's pairing is the peer's, Plugin::Peer#tell_moved).
|
|
568
608
|
# @return [Boolean] whether there was one
|
|
569
609
|
def peer_unpair(id, peer) = PeerMailbox.unpair(id, peer, state_dir: state_dir!)
|
|
570
610
|
|
|
@@ -574,8 +614,11 @@ module Samagotchi
|
|
|
574
614
|
# A message from +peer+ into session +id+, as a delivery
|
|
575
615
|
# (SessionInbox.deliver :peer): refused before anything is written
|
|
576
616
|
# for a gone session, a continued one, no pairing, a live worker
|
|
577
|
-
# without the peer mail, UNTAKEN_MAX unread; else
|
|
578
|
-
#
|
|
617
|
+
# without the peer mail, UNTAKEN_MAX unread; else, under a shared hold
|
|
618
|
+
# of the session's transition lock (checked again there, #moved_on: a
|
|
619
|
+
# continue holding it refuses the say, naming its new link, so no
|
|
620
|
+
# message lands in a mailbox its carry already read), written inside
|
|
621
|
+
# the delivery's block (a session being deleted refuses first), which
|
|
579
622
|
# asks for a worker by the message's own +wake+ alone (no
|
|
580
623
|
# context.wake, no window). Then what chi did, at once when it can't
|
|
581
624
|
# answer yet, else the receipt within +wait+ seconds (a look every
|
|
@@ -587,8 +630,8 @@ module Samagotchi
|
|
|
587
630
|
PeerMailbox.check_name!(peer)
|
|
588
631
|
PeerMailbox.checked_text(text)
|
|
589
632
|
return PeerSay.new(status: :gone) unless Session.exist?(id, **dir)
|
|
590
|
-
if (
|
|
591
|
-
return PeerSay.new(status: :continued, next_id:
|
|
633
|
+
if (successor = continued_as(id))
|
|
634
|
+
return PeerSay.new(status: :continued, next_id: successor.id, in_flight: successor.in_flight?)
|
|
592
635
|
end
|
|
593
636
|
return PeerSay.new(status: :not_paired) unless PeerMailbox.link(id, peer, state_dir: state_dir!)
|
|
594
637
|
return PeerSay.new(status: :old_worker) if old_peer_worker?(id)
|
|
@@ -596,10 +639,18 @@ module Samagotchi
|
|
|
596
639
|
message = nil
|
|
597
640
|
start_failed = false
|
|
598
641
|
delivered = begin
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
642
|
+
SessionTransition.shared(id, state_dir: state_dir!) do |free|
|
|
643
|
+
case (moved = moved_on(id, free))
|
|
644
|
+
when :busy then return PeerSay.new(status: :busy)
|
|
645
|
+
when SessionChain::Successor
|
|
646
|
+
return PeerSay.new(status: :continued, next_id: moved.id, in_flight: moved.in_flight?)
|
|
647
|
+
end
|
|
648
|
+
|
|
649
|
+
SessionInbox.deliver(:peer, id, state_dir: state_dir!) do
|
|
650
|
+
message = PeerMailbox.post(id, peer, text: text, summary: summary, wake: wake, re: re, state_dir: state_dir!,
|
|
651
|
+
taken: PeerReceipts.taken(id, peer, state_dir: state_dir!))
|
|
652
|
+
message.wake
|
|
653
|
+
end
|
|
603
654
|
end
|
|
604
655
|
rescue PeerMailbox::Full
|
|
605
656
|
return PeerSay.new(status: :full)
|
|
@@ -751,13 +802,35 @@ module Samagotchi
|
|
|
751
802
|
|
|
752
803
|
def dir = @state_dir ? { state_dir: @state_dir } : {}
|
|
753
804
|
|
|
754
|
-
# The session that continues +id
|
|
755
|
-
#
|
|
756
|
-
#
|
|
805
|
+
# The session that continues +id+ (SessionChain::Successor.of: a saved
|
|
806
|
+
# one, or for a schedule's pruned anchor the schedule's head), or nil.
|
|
807
|
+
# The archive check first: a continued link is always archived, and the
|
|
808
|
+
# walk loads every session.
|
|
809
|
+
# @return [SessionChain::Successor, nil]
|
|
757
810
|
def continued_as(id)
|
|
758
811
|
return nil unless ArchiveStore.archived?(session_dir(id))
|
|
759
812
|
|
|
760
|
-
SessionChain.
|
|
813
|
+
SessionChain::Successor.of(id, state_dir: state_dir!)
|
|
814
|
+
end
|
|
815
|
+
|
|
816
|
+
# Inside SessionTransition.shared(+id+) (+free+: the shared hold was
|
|
817
|
+
# taken): why an arrival can't be written now — a Successor (a saved
|
|
818
|
+
# one; or, when a continue holds the lock, the one its intent names)
|
|
819
|
+
# or :busy (held, nobody named: another change, or a continue before
|
|
820
|
+
# its intent or after its undo). nil: it goes in.
|
|
821
|
+
# @return [SessionChain::Successor, :busy, nil]
|
|
822
|
+
def moved_on(id, free)
|
|
823
|
+
return continued_as(id) if free
|
|
824
|
+
|
|
825
|
+
SessionChain::Successor.of(id, state_dir: state_dir!, in_flight: true) || :busy
|
|
826
|
+
end
|
|
827
|
+
|
|
828
|
+
# The block, for a session target under its transition lock
|
|
829
|
+
# (ContextSources.changing); a project's as is.
|
|
830
|
+
def own_change(target, &)
|
|
831
|
+
return yield if target.project?
|
|
832
|
+
|
|
833
|
+
ContextSources.changing(target.session_id, state_dir: state_dir!, &)
|
|
761
834
|
end
|
|
762
835
|
|
|
763
836
|
def state_dir! = @state_dir || Session.default_state_dir
|
|
@@ -622,7 +622,8 @@ module Samagotchi
|
|
|
622
622
|
when 410
|
|
623
623
|
raise_refusal(reply) unless reply.error == "context_continued"
|
|
624
624
|
|
|
625
|
-
ContextPush.new(status: :continued, next_id: reply.json["next_id"].to_s)
|
|
625
|
+
ContextPush.new(status: :continued, next_id: reply.json["next_id"].to_s, in_flight: reply.json["in_flight"] == true)
|
|
626
|
+
when 423 then reply.error == "context_busy" ? ContextPush.new(status: :busy) : raise_refusal(reply)
|
|
626
627
|
when 400 then raise ContextSources::Invalid, reply.json["detail"].to_s
|
|
627
628
|
else raise_refusal(reply)
|
|
628
629
|
end
|
|
@@ -811,8 +812,7 @@ module Samagotchi
|
|
|
811
812
|
|
|
812
813
|
def no_provider?(reply) = reply.error == "no_provider"
|
|
813
814
|
|
|
814
|
-
# chi web's 404 not_found detail for a session
|
|
815
|
-
# "no session X" (Session::NotFound).
|
|
815
|
+
# chi web's 404 not_found detail for a missing session: "no session X" (Session::NotFound).
|
|
816
816
|
SESSION_NOT_FOUND = /\Ano session (.+)\z/m
|
|
817
817
|
|
|
818
818
|
# Session::NotFound for a missing session's 404, else an
|
|
@@ -841,14 +841,15 @@ module Samagotchi
|
|
|
841
841
|
|
|
842
842
|
raise SessionManager::OwnedByTUI, sid
|
|
843
843
|
when "worker_running", "still_stopping", "scheduled", "continuing", "delegate_unmovable", "chain_scheduled",
|
|
844
|
-
"protection_unreadable"
|
|
844
|
+
"protection_unreadable", "transition_held"
|
|
845
845
|
raise SessionManager::DeleteRefused.new(sid, reason || error.to_sym, message: message, ids: ids_of(json["ids"]),
|
|
846
846
|
schedule_id: json["schedule_id"])
|
|
847
847
|
when "tasks_running" then raise tasks_refusal(json, sid)
|
|
848
|
+
when "context_busy" then raise ContextSources::Held, message.to_s
|
|
848
849
|
when "busy", "scratch"
|
|
849
|
-
raise SessionManager::ArchiveRefused.new(sid, reason, busy_id: json["busy_id"]) if reason
|
|
850
|
-
when "continued", "open_children", "folder_gone"
|
|
851
|
-
raise SessionContinue::Refused.new(sid, reason, ids: Array(json["ids"]))
|
|
850
|
+
raise SessionManager::ArchiveRefused.new(sid, reason, busy_id: json["busy_id"], message: (message if reason == :transition_held)) if reason
|
|
851
|
+
when "continued", "open_children", "folder_gone", "transition_busy"
|
|
852
|
+
raise SessionContinue::Refused.new(sid, reason, ids: Array(json["ids"]), detail: (message if reason == :transition_busy))
|
|
852
853
|
when "held", "not_running", "unsupported", "failed"
|
|
853
854
|
if reason
|
|
854
855
|
raise SessionManager::RestartRefused.new(sid, reason, version: json["version"], holds: json["holds"],
|
|
@@ -856,9 +857,9 @@ module Samagotchi
|
|
|
856
857
|
end
|
|
857
858
|
when "parent_answers_refused", "parent_continue_refused", "no_vision"
|
|
858
859
|
raise Refused.new(message.to_s, code: error)
|
|
859
|
-
# No model configured there, or --model's host unknown there: what
|
|
860
|
-
# spawn_session raises here (chi send: could not start a session).
|
|
860
|
+
# No model configured there, or --model's host unknown there: what spawn_session raises here (chi send: could not start a session).
|
|
861
861
|
when "invalid_model" then raise ModelProfile::MissingModel, message.to_s
|
|
862
|
+
when "stop_held" then raise SessionTransition::Busy.new(sid, kind: json["kind"]&.to_sym, successor_id: json["successor_id"])
|
|
862
863
|
# A toolkit whose config kit's groups: is invalid there: the same.
|
|
863
864
|
when "toolkit_refused" then raise Toolkit::Refused, message.to_s
|
|
864
865
|
# The grants store doesn't read there: chi share says what it says here.
|
|
@@ -24,8 +24,8 @@ module Samagotchi
|
|
|
24
24
|
link = PeerMailbox::PeerLink.from_h(reply.json["link"]) or raise RemoteFailed, "#{@entry.label}: no link in its answer"
|
|
25
25
|
PeerPaired.new(status: :paired, link: link)
|
|
26
26
|
when 409
|
|
27
|
-
status = %w[continued old_worker].find { |known| known == reply.json["status"] } or raise_refusal(reply)
|
|
28
|
-
PeerPaired.new(status: status.to_sym, next_id: reply.json["next_id"]&.to_s)
|
|
27
|
+
status = %w[continued busy old_worker].find { |known| known == reply.json["status"] } or raise_refusal(reply)
|
|
28
|
+
PeerPaired.new(status: status.to_sym, next_id: reply.json["next_id"]&.to_s, in_flight: reply.json["in_flight"] == true)
|
|
29
29
|
else raise_refusal(reply)
|
|
30
30
|
end
|
|
31
31
|
end
|
|
@@ -269,13 +269,15 @@ module Samagotchi
|
|
|
269
269
|
|
|
270
270
|
# chi context push's outcome: :pushed (+before+ the revision it had,
|
|
271
271
|
# +revision+ the new one), :missing (no such source), :not_push (a
|
|
272
|
-
# command source, whose text is its command's)
|
|
273
|
-
# session was continued, as +next_id
|
|
272
|
+
# command source, whose text is its command's), :continued (the
|
|
273
|
+
# session was continued, as +next_id+, or is being continued, +in_flight+:
|
|
274
|
+
# nothing was written) or :busy (another change of the session holds
|
|
275
|
+
# its transition lock: nothing was written). +wake+:
|
|
274
276
|
# what a push that asked for a wake did (SessionInbox.deliver :push:
|
|
275
277
|
# :woken, :live, :stopped, :starting, :archived, :scratch, :owned,
|
|
276
278
|
# :gone), nil when it asked none (or an older chi web's answer has none).
|
|
277
|
-
ContextPush = Data.define(:status, :before, :revision, :next_id, :wake) do
|
|
278
|
-
def initialize(status:, before: nil, revision: nil, next_id: nil, wake: nil) = super
|
|
279
|
+
ContextPush = Data.define(:status, :before, :revision, :next_id, :wake, :in_flight) do
|
|
280
|
+
def initialize(status:, before: nil, revision: nil, next_id: nil, wake: nil, in_flight: false) = super
|
|
279
281
|
|
|
280
282
|
# What chi context push adds to its line for the wake, else nil.
|
|
281
283
|
def wake_line = ContextPush::WAKE_LINES[wake]
|
|
@@ -289,9 +291,17 @@ module Samagotchi
|
|
|
289
291
|
# writer holds the old id, and nothing reads a continued session.
|
|
290
292
|
# Generic (the CLI and chi web load no plugins): a peer's skill owns
|
|
291
293
|
# its re-pair.
|
|
292
|
-
def self.continued_message(name, id, next_id)
|
|
293
|
-
|
|
294
|
-
|
|
294
|
+
def self.continued_message(name, id, next_id, in_flight: false)
|
|
295
|
+
require_relative "session_chain/successor"
|
|
296
|
+
"#{SessionChain::Continued.new(id, next_id, in_flight: in_flight).message}: #{name} stays with the session it " \
|
|
297
|
+
"was attached to; attach it to #{next_id} (or pair again with #{next_id}) and push there" \
|
|
298
|
+
"#{" in a moment" if in_flight}"
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
# Why a push into session +id+ was refused as :busy.
|
|
302
|
+
def self.busy_message(id)
|
|
303
|
+
require_relative "session_transition"
|
|
304
|
+
SessionTransition::Busy.new(id).message
|
|
295
305
|
end
|
|
296
306
|
end
|
|
297
307
|
|
|
@@ -3,14 +3,15 @@
|
|
|
3
3
|
require "digest"
|
|
4
4
|
require "json"
|
|
5
5
|
require_relative "links"
|
|
6
|
+
require_relative "successor"
|
|
6
7
|
require_relative "../archive_store"
|
|
7
8
|
require_relative "../child_move"
|
|
8
|
-
require_relative "../continue_intent"
|
|
9
9
|
require_relative "../continued_from"
|
|
10
10
|
require_relative "../note_move"
|
|
11
11
|
require_relative "../owner_lock"
|
|
12
12
|
require_relative "../session"
|
|
13
13
|
require_relative "../session_protection"
|
|
14
|
+
require_relative "../session_transition"
|
|
14
15
|
require_relative "../starting_marker"
|
|
15
16
|
require_relative "../worker_presence"
|
|
16
17
|
|
|
@@ -40,6 +41,11 @@ module Samagotchi
|
|
|
40
41
|
# - Either way the link's own delegates follow SessionManager::DELEGATES
|
|
41
42
|
# (D7).
|
|
42
43
|
module Delete
|
|
44
|
+
# How long a delete waits for the transition locks of the sessions it
|
|
45
|
+
# takes (SessionTransition.exclusive, kind :delete): a send's shared
|
|
46
|
+
# hold is short, a continue's is not waited out (DeleteRefused.held).
|
|
47
|
+
LOCK_WAIT = 10
|
|
48
|
+
|
|
43
49
|
module_function
|
|
44
50
|
|
|
45
51
|
# What deleting the link +id+ alone does, read from one listing: the
|
|
@@ -97,41 +103,80 @@ module Samagotchi
|
|
|
97
103
|
def chained?(id, links) = links.live_links(id).size > 1
|
|
98
104
|
|
|
99
105
|
# Delete the link +id+ (a full id with a file or a folder; a session
|
|
100
|
-
# in no chain is a link with no previous one)
|
|
101
|
-
#
|
|
102
|
-
# back
|
|
106
|
+
# in no chain is a link with no previous one) under the exclusive
|
|
107
|
+
# transition locks (kind :delete) of it, the previous link when it
|
|
108
|
+
# would come back and the delegates that go with it, taken in sorted
|
|
109
|
+
# order before anything (the stop included) and held to the end: no
|
|
110
|
+
# continue of either starts meanwhile, and an arrival is refused as
|
|
111
|
+
# "being deleted". The plan is made again under them; an id it adds
|
|
112
|
+
# (late) is taken then, within the same LOCK_WAIT.
|
|
103
113
|
# @return [Hash] {id:, removed:, stopped:, unarchived:, moved_back:,
|
|
104
114
|
# deleted: (its delegates deleted and it), left_running:}
|
|
105
|
-
# @raise [SessionManager::DeleteRefused] :continuing
|
|
106
|
-
# :
|
|
115
|
+
# @raise [SessionManager::DeleteRefused] :continuing or
|
|
116
|
+
# :transition_held (a lock not free within LOCK_WAIT:
|
|
117
|
+
# DeleteRefused.held), :scheduled, :delegate_unmovable,
|
|
118
|
+
# :worker_running, :still_stopping
|
|
107
119
|
# @raise [SessionManager::OwnedByTUI]
|
|
120
|
+
# @raise [Session::NotFound] it went while the delete waited
|
|
108
121
|
# @param running_tasks [SessionManager::RunningTasks::Policy] for the
|
|
109
122
|
# link and the delegates that go with it (check_link!, then each
|
|
110
123
|
# one's removal under its lock)
|
|
111
|
-
def link(id, state_dir:, stop:, wait:, revive_previous:,
|
|
124
|
+
def link(id, state_dir:, stop:, wait:, revive_previous:, expect: nil,
|
|
112
125
|
running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
113
|
-
|
|
114
|
-
|
|
126
|
+
ends_at = monotonic + LOCK_WAIT
|
|
127
|
+
links = Links.new(state_dir: state_dir)
|
|
128
|
+
following = links.next_of(id)
|
|
129
|
+
first = link_plan(id, state_dir: state_dir, links: links, undo: revive_previous)
|
|
130
|
+
under_locks(plan_ids(first), state_dir, ends_at) do
|
|
131
|
+
raise Session::NotFound, id unless on_disk?(id, state_dir)
|
|
132
|
+
# Continued while the delete waited: it is no longer the link the
|
|
133
|
+
# caller meant (a newest link would now be a middle one).
|
|
134
|
+
if following.nil? && (successor = Links.new(state_dir: state_dir).next_of(id))
|
|
135
|
+
raise SessionManager::DeleteRefused.new(id, :continuing, message: SessionChain::Continued.new(id, successor).message)
|
|
136
|
+
end
|
|
115
137
|
|
|
116
138
|
plan = link_plan(id, state_dir: state_dir, links: Links.new(state_dir: state_dir), undo: revive_previous)
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
running_tasks: running_tasks)
|
|
139
|
+
under_locks(plan_ids(plan), state_dir, ends_at) do
|
|
140
|
+
delete_link(plan, state_dir: state_dir, stop: stop, wait: wait, expect: expect, running_tasks: running_tasks)
|
|
120
141
|
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# The sessions a plan takes or brings back: their transition locks.
|
|
146
|
+
def plan_ids(plan)
|
|
147
|
+
case plan
|
|
148
|
+
when LinkPlan then [plan.id, plan.previous, *plan.delegates.delete].compact
|
|
149
|
+
else [*plan.links, *plan.delegates.values.flat_map(&:delete)]
|
|
150
|
+
end
|
|
151
|
+
end
|
|
121
152
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
153
|
+
# The block under the exclusive transition locks (kind :delete) of
|
|
154
|
+
# +ids+ still on disk (a gone one's folder is never made again),
|
|
155
|
+
# tried until +ends_at+ (monotonic); ids this thread holds pass
|
|
156
|
+
# through, so a nested call takes only the late ones.
|
|
157
|
+
# @raise [SessionManager::DeleteRefused] a lock not free in time (DeleteRefused.held)
|
|
158
|
+
def under_locks(ids, state_dir, ends_at, &)
|
|
159
|
+
ids = ids.select { |sid| on_disk?(sid, state_dir) }
|
|
160
|
+
refuse_continuing_now!(ids, state_dir)
|
|
161
|
+
SessionTransition.exclusive(ids, kind: :delete, state_dir: state_dir, deadline: [ends_at - monotonic, 0].max, &)
|
|
162
|
+
rescue SessionTransition::Busy => e
|
|
163
|
+
raise SessionManager::DeleteRefused.held(e)
|
|
164
|
+
end
|
|
127
165
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
166
|
+
# A continue holding one of +ids+ now refuses at once, as before the
|
|
167
|
+
# exclusive hold (:continuing, DeleteRefused.held): its end would change
|
|
168
|
+
# what the delete takes. Ids this thread holds are its own.
|
|
169
|
+
def refuse_continuing_now!(ids, state_dir)
|
|
170
|
+
ids.each do |sid|
|
|
171
|
+
next if SessionTransition.holding?(sid, state_dir: state_dir) || !SessionTransition.held?(sid, state_dir: state_dir)
|
|
172
|
+
|
|
173
|
+
busy = SessionTransition::Busy.held(sid, state_dir: state_dir)
|
|
174
|
+
raise SessionManager::DeleteRefused.held(busy) if busy.kind == :continue
|
|
132
175
|
end
|
|
133
176
|
end
|
|
134
177
|
|
|
178
|
+
def monotonic = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
179
|
+
|
|
135
180
|
# D2's conditions: +id+ is the chain's end and not archived itself,
|
|
136
181
|
# and the link before it is there and archived.
|
|
137
182
|
def revivable?(id, previous, links)
|
|
@@ -330,26 +375,46 @@ module Samagotchi
|
|
|
330
375
|
# and again under each link's locks; a stop runs link by link
|
|
331
376
|
# @return also tasks_stopped:, tasks_not_stopped:, tasks_ended:
|
|
332
377
|
def chain(id, state_dir:, stop:, wait:, expect: nil, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
333
|
-
|
|
378
|
+
ends_at = monotonic + LOCK_WAIT
|
|
379
|
+
first = checked_chain_plan(id, state_dir, Links.new(state_dir: state_dir), stop: stop, expect: expect,
|
|
380
|
+
running_tasks: running_tasks)
|
|
381
|
+
under_locks(plan_ids(first), state_dir, ends_at) do
|
|
382
|
+
raise Session::NotFound, id unless Session.exist?(id, state_dir: state_dir)
|
|
383
|
+
|
|
384
|
+
# Made and checked again under the locks (a wait may have changed
|
|
385
|
+
# the chain); the late ids are taken within the same LOCK_WAIT.
|
|
386
|
+
links = Links.new(state_dir: state_dir)
|
|
387
|
+
plan = checked_chain_plan(id, state_dir, links, stop: stop, expect: expect, running_tasks: running_tasks)
|
|
388
|
+
under_locks(plan_ids(plan), state_dir, ends_at) do
|
|
389
|
+
delete_chain_locked(plan, links, state_dir, stop: stop, wait: wait, running_tasks: running_tasks)
|
|
390
|
+
end
|
|
391
|
+
end
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
# The chain's plan, its digest matched (+expect+) and every refusal
|
|
395
|
+
# made (check_chain!).
|
|
396
|
+
def checked_chain_plan(id, state_dir, links, stop:, expect:, running_tasks:)
|
|
334
397
|
plan = chain_plan(id, state_dir: state_dir, links: links)
|
|
335
398
|
raise SessionManager::DeleteRefused.new(plan.id, :plan_changed) if !expect.nil? && expect != digest(plan)
|
|
336
399
|
|
|
337
400
|
check_chain!(plan, state_dir, stop: stop, running_tasks: running_tasks)
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
SessionManager.release_for_delete!(link, state_dir: state_dir, stop: stop, wait: wait)
|
|
341
|
-
end
|
|
342
|
-
begin
|
|
343
|
-
delete_links(plan, links, state_dir, running_tasks: running_tasks)
|
|
344
|
-
.merge(stopped: stopped, cut_off: plan.cut_off, **running_tasks.result_fields)
|
|
345
|
-
rescue SessionManager::DeleteRefused => e
|
|
346
|
-
raise e unless e.reason == :tasks_running && !stopped.empty?
|
|
401
|
+
plan
|
|
402
|
+
end
|
|
347
403
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
404
|
+
# Every link's worker stopped, then the links deleted (#delete_links),
|
|
405
|
+
# under the chain's locks.
|
|
406
|
+
def delete_chain_locked(plan, links, state_dir, stop:, wait:, running_tasks:)
|
|
407
|
+
stopped = plan.links.select do |link|
|
|
408
|
+
SessionManager.release_for_delete!(link, state_dir: state_dir, stop: stop, wait: wait)
|
|
352
409
|
end
|
|
410
|
+
delete_links(plan, links, state_dir, running_tasks: running_tasks)
|
|
411
|
+
.merge(stopped: stopped, cut_off: plan.cut_off, **running_tasks.result_fields)
|
|
412
|
+
rescue SessionManager::DeleteRefused => e
|
|
413
|
+
raise e unless e.reason == :tasks_running && !stopped.to_a.empty?
|
|
414
|
+
|
|
415
|
+
# Nothing went (a part-way refusal is in the result): say whose
|
|
416
|
+
# worker was stopped, the refused link's own when it was.
|
|
417
|
+
raise e.with_worker_stopped(stopped.include?(e.session_id) ? e.session_id : stopped.first)
|
|
353
418
|
end
|
|
354
419
|
|
|
355
420
|
# The refusals a chain's delete makes before anything goes (the
|
|
@@ -373,18 +438,6 @@ module Samagotchi
|
|
|
373
438
|
end
|
|
374
439
|
end
|
|
375
440
|
|
|
376
|
-
# The block inside a shared continue hold of every id.
|
|
377
|
-
# @raise [SessionManager::DeleteRefused] :continuing, the first one a continue holds
|
|
378
|
-
def hold_all(ids, state_dir, &block)
|
|
379
|
-
return yield if ids.empty?
|
|
380
|
-
|
|
381
|
-
ContinueIntent.hold(ids.first, state_dir: state_dir) do |held|
|
|
382
|
-
raise SessionManager::DeleteRefused.new(ids.first, :continuing) unless held
|
|
383
|
-
|
|
384
|
-
hold_all(ids.drop(1), state_dir, &block)
|
|
385
|
-
end
|
|
386
|
-
end
|
|
387
|
-
|
|
388
441
|
# Each link with its delegates under their locks, newest first; a
|
|
389
442
|
# refused one keeps it and every earlier link whole.
|
|
390
443
|
def delete_links(plan, links, state_dir, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
@@ -90,10 +90,10 @@ module Samagotchi
|
|
|
90
90
|
refusal: refusal(state_dir) { chain_checks(plan, state_dir) } }
|
|
91
91
|
end
|
|
92
92
|
|
|
93
|
-
# The link delete's refusals in its order: a
|
|
93
|
+
# The link delete's refusals in its order: a transition of it (or of
|
|
94
94
|
# the previous link it undoes), a one-turn run, then check_link!.
|
|
95
95
|
def link_checks(plan, state_dir)
|
|
96
|
-
[plan.id, plan.previous].compact.each { |sid|
|
|
96
|
+
[plan.id, plan.previous].compact.each { |sid| refuse_held!(sid, state_dir) }
|
|
97
97
|
tui!(plan.id, state_dir)
|
|
98
98
|
Delete.check_link!(plan, state_dir)
|
|
99
99
|
end
|
|
@@ -102,11 +102,15 @@ module Samagotchi
|
|
|
102
102
|
# refusal here.
|
|
103
103
|
def chain_checks(plan, state_dir)
|
|
104
104
|
Delete.check_chain!(plan, state_dir, stop: true)
|
|
105
|
-
plan.links.each { |sid|
|
|
105
|
+
plan.links.each { |sid| refuse_held!(sid, state_dir) }
|
|
106
106
|
end
|
|
107
107
|
|
|
108
|
-
|
|
109
|
-
|
|
108
|
+
# A transition holding +id+'s lock now (a continue: :continuing,
|
|
109
|
+
# another delete: :transition_held; DeleteRefused.held).
|
|
110
|
+
def refuse_held!(id, state_dir)
|
|
111
|
+
return unless SessionTransition.held?(id, state_dir: state_dir)
|
|
112
|
+
|
|
113
|
+
raise SessionManager::DeleteRefused.held(SessionTransition::Busy.held(id, state_dir: state_dir))
|
|
110
114
|
end
|
|
111
115
|
|
|
112
116
|
def tui!(id, state_dir)
|