samagotchi 0.192.0 → 0.194.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 +128 -1
- data/docs/architecture.md +3 -3
- data/docs/bundle-authoring.md +1 -1
- data/docs/bundles.md +82 -40
- data/docs/cli.md +47 -17
- data/docs/configuration.md +75 -8
- data/docs/context.md +8 -5
- data/docs/desktop.md +3 -0
- data/docs/guardrails.md +20 -5
- data/docs/hooks.md +1 -0
- data/docs/internals/bridge.md +3 -1
- data/docs/internals/bundles.md +2 -2
- data/docs/internals/engine.md +46 -3
- data/docs/internals/pages.md +4 -4
- data/docs/internals/panes.md +66 -0
- data/docs/internals/participants.md +5 -3
- data/docs/internals/peers.md +176 -27
- data/docs/internals/prompt-caching.md +6 -1
- data/docs/internals/remote-sessions.md +6 -0
- data/docs/internals/session-lifecycle.md +12 -4
- data/docs/internals/web-frontend.md +106 -22
- data/docs/internals/working-set.md +5 -1
- data/docs/peers.md +150 -27
- data/docs/plugins.md +48 -7
- data/docs/releasing.md +30 -2
- data/docs/scheduling.md +3 -2
- data/docs/sessions.md +16 -7
- data/docs/sub-agent.md +6 -4
- data/docs/system-one.md +4 -0
- data/docs/testing.md +38 -3
- data/docs/thin-clients.md +27 -11
- data/lib/samagotchi/bridge/card_store.rb +1 -1
- data/lib/samagotchi/broadcast/triage_model.rb +17 -0
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +8 -5
- data/lib/samagotchi/bundles/claude-peer/plugin.rb +24 -5
- data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +61 -24
- data/lib/samagotchi/bundles/claude-peer/scripts/pair +31 -8
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +8 -5
- data/lib/samagotchi/bundles/codex-peer/plugin.rb +62 -6
- data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +57 -19
- data/lib/samagotchi/bundles/codex-peer/scripts/pair +28 -7
- data/lib/samagotchi/bundles/coordinator/manifest.yml +2 -2
- data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +31 -0
- data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/plugin.rb +0 -7
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/chi_invocation.rb +7 -0
- data/lib/samagotchi/client.rb +18 -0
- data/lib/samagotchi/codex_rollout.rb +93 -0
- data/lib/samagotchi/compat.rb +8 -1
- data/lib/samagotchi/config.rb +19 -6
- data/lib/samagotchi/continue_offer.rb +13 -1
- data/lib/samagotchi/delivery.rb +2 -2
- data/lib/samagotchi/effective_model.rb +28 -2
- data/lib/samagotchi/engine/cards.rb +9 -4
- data/lib/samagotchi/engine/hooks.rb +8 -0
- data/lib/samagotchi/engine/models.rb +197 -7
- data/lib/samagotchi/engine/plugins.rb +2 -0
- data/lib/samagotchi/engine/side_requests.rb +72 -13
- data/lib/samagotchi/engine/toolkit.rb +1 -1
- data/lib/samagotchi/engine/turn_helpers.rb +1 -1
- data/lib/samagotchi/engine.rb +11 -8
- data/lib/samagotchi/engine_snapshots.rb +16 -3
- data/lib/samagotchi/events.rb +1 -0
- data/lib/samagotchi/guardrails/pair_boundary.rb +49 -18
- data/lib/samagotchi/hooks.rb +3 -1
- data/lib/samagotchi/host_registry.rb +146 -16
- data/lib/samagotchi/idle_client.rb +13 -2
- data/lib/samagotchi/idle_recap.rb +33 -3
- data/lib/samagotchi/idle_target.rb +27 -7
- data/lib/samagotchi/jsonl_store.rb +13 -2
- data/lib/samagotchi/llm/adapter_common.rb +4 -2
- data/lib/samagotchi/llm/anthropic_messages.rb +14 -2
- data/lib/samagotchi/llm/errors.rb +35 -4
- data/lib/samagotchi/llm/model_listing.rb +19 -0
- data/lib/samagotchi/llm/openai_chat.rb +8 -2
- data/lib/samagotchi/llm/prompt_cache.rb +5 -0
- data/lib/samagotchi/model_binding.rb +46 -4
- data/lib/samagotchi/model_catalog.rb +68 -8
- data/lib/samagotchi/model_profile.rb +35 -0
- data/lib/samagotchi/models_command.rb +8 -4
- data/lib/samagotchi/pages/launch.rb +1 -1
- data/lib/samagotchi/panes/kitty.rb +122 -0
- data/lib/samagotchi/panes.rb +185 -0
- data/lib/samagotchi/peer_announce.rb +245 -0
- data/lib/samagotchi/peer_cold.rb +92 -0
- data/lib/samagotchi/peer_command.rb +121 -20
- data/lib/samagotchi/peer_continue.rb +42 -16
- data/lib/samagotchi/peer_exchanges.rb +7 -0
- data/lib/samagotchi/peer_mailbox.rb +50 -10
- data/lib/samagotchi/peer_pair_here.rb +149 -0
- data/lib/samagotchi/peer_presence.rb +434 -0
- data/lib/samagotchi/peer_receipts.rb +33 -6
- data/lib/samagotchi/peer_say.rb +46 -21
- data/lib/samagotchi/peer_wait_expiry.rb +47 -0
- data/lib/samagotchi/plugin/api.rb +13 -1
- data/lib/samagotchi/plugin/card.rb +68 -11
- data/lib/samagotchi/plugin/context.rb +33 -2
- data/lib/samagotchi/plugin/panes.rb +54 -0
- data/lib/samagotchi/plugin/peer.rb +170 -63
- data/lib/samagotchi/plugin/peer_pairing.rb +290 -0
- data/lib/samagotchi/plugin/peer_start.rb +301 -0
- data/lib/samagotchi/plugin/peer_start_hook.rb +207 -0
- data/lib/samagotchi/plugin/peers.rb +89 -9
- data/lib/samagotchi/recap_setup.rb +40 -3
- data/lib/samagotchi/self_report.rb +25 -6
- data/lib/samagotchi/send_command.rb +6 -6
- data/lib/samagotchi/served_lookup.rb +225 -0
- data/lib/samagotchi/served_ref.rb +41 -0
- data/lib/samagotchi/session_access/contract.rb +6 -0
- data/lib/samagotchi/session_access/local.rb +149 -40
- data/lib/samagotchi/session_access/remote.rb +4 -4
- data/lib/samagotchi/session_access/remote_peers.rb +8 -5
- data/lib/samagotchi/session_chain/links.rb +8 -3
- data/lib/samagotchi/session_commands.rb +40 -8
- data/lib/samagotchi/session_continue.rb +11 -6
- data/lib/samagotchi/session_lifecycle/allowed.rb +4 -2
- data/lib/samagotchi/session_manager/delivery.rb +5 -0
- data/lib/samagotchi/session_manager/lifecycle.rb +58 -7
- data/lib/samagotchi/session_manager/removal.rb +3 -0
- data/lib/samagotchi/session_manager/spawn.rb +6 -1
- data/lib/samagotchi/session_model_settings.rb +7 -3
- data/lib/samagotchi/session_stores.rb +25 -11
- data/lib/samagotchi/sessions_command.rb +16 -3
- data/lib/samagotchi/system_one/http_backend.rb +111 -10
- data/lib/samagotchi/system_one/runs.rb +5 -2
- data/lib/samagotchi/system_prompt.rb +11 -4
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -1
- data/lib/samagotchi/terminal_ui/formatting.rb +26 -12
- data/lib/samagotchi/terminal_ui/status_row.rb +20 -3
- data/lib/samagotchi/terminal_ui.rb +1 -1
- data/lib/samagotchi/testing/boundary.rb +0 -1
- data/lib/samagotchi/testing/bundle.rb +6 -2
- data/lib/samagotchi/testing/host.rb +1 -0
- data/lib/samagotchi/testing/recorder.rb +3 -0
- data/lib/samagotchi/turn_endings.rb +6 -2
- data/lib/samagotchi/user_typed.rb +29 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +8 -20
- data/lib/samagotchi/web/participant_review.rb +1 -1
- data/lib/samagotchi/web/participant_view.rb +1 -1
- data/lib/samagotchi/web/peer_routes.rb +40 -15
- data/lib/samagotchi/web/public/agent_rows.js +206 -0
- data/lib/samagotchi/web/public/agent_runs.js +595 -0
- data/lib/samagotchi/web/public/answer_fold.js +41 -44
- data/lib/samagotchi/web/public/app.js +4 -4
- data/lib/samagotchi/web/public/block_fix.js +1 -1
- data/lib/samagotchi/web/public/card.js +26 -10
- data/lib/samagotchi/web/public/cards_place.js +6 -4
- data/lib/samagotchi/web/public/context_chips.js +24 -2
- data/lib/samagotchi/web/public/copy.js +8 -2
- data/lib/samagotchi/web/public/data.js +2 -1
- data/lib/samagotchi/web/public/format.js +9 -1
- data/lib/samagotchi/web/public/history_reveal.js +5 -2
- data/lib/samagotchi/web/public/index.html +56 -2
- data/lib/samagotchi/web/public/info_bar_model.js +1 -1
- data/lib/samagotchi/web/public/model_pick.js +18 -5
- data/lib/samagotchi/web/public/model_picker.js +1 -1
- data/lib/samagotchi/web/public/page_view.js +1 -1
- data/lib/samagotchi/web/public/pages.css +1 -7
- data/lib/samagotchi/web/public/peer_chip.js +15 -5
- data/lib/samagotchi/web/public/peer_data.js +4 -2
- data/lib/samagotchi/web/public/peer_mailbox_api.js +2 -1
- data/lib/samagotchi/web/public/peer_note.js +4 -2
- data/lib/samagotchi/web/public/peer_row.js +37 -0
- data/lib/samagotchi/web/public/peer_wait.js +36 -0
- data/lib/samagotchi/web/public/quiet_notes.js +12 -1
- data/lib/samagotchi/web/public/revise_stack.js +5 -2
- data/lib/samagotchi/web/public/send_turn.js +11 -3
- data/lib/samagotchi/web/public/served_ref.js +9 -0
- data/lib/samagotchi/web/public/session_fields.js +1 -0
- data/lib/samagotchi/web/public/session_state.js +4 -0
- data/lib/samagotchi/web/public/stage_missing.js +23 -6
- data/lib/samagotchi/web/public/stream_handlers.js +16 -0
- data/lib/samagotchi/web/public/turn_block.js +4 -0
- data/lib/samagotchi/web/public/version_notices.js +7 -1
- data/lib/samagotchi/web/session_control.rb +19 -10
- data/lib/samagotchi/web/session_show.rb +20 -0
- data/lib/samagotchi/web/update_ops.rb +2 -2
- data/lib/samagotchi/worker/command_queue.rb +3 -0
- data/lib/samagotchi/worker/mail.rb +24 -1
- data/lib/samagotchi/worker.rb +8 -5
- data/lib/samagotchi/worker_inbound/mail.rb +34 -6
- data/lib/samagotchi/worker_inbound.rb +11 -3
- data/lib/samagotchi/worker_link.rb +13 -1
- data/lib/samagotchi/worker_wakes.rb +33 -5
- metadata +22 -2
- data/lib/samagotchi/web/public/pages_group.js +0 -67
|
@@ -16,6 +16,7 @@ require_relative "../worker_inbound"
|
|
|
16
16
|
require_relative "../context_sources"
|
|
17
17
|
require_relative "../peer_exchanges"
|
|
18
18
|
require_relative "../peer_mailbox"
|
|
19
|
+
require_relative "../peer_presence"
|
|
19
20
|
require_relative "../peer_receipts"
|
|
20
21
|
require_relative "../peer_roster"
|
|
21
22
|
require_relative "../peer_say"
|
|
@@ -314,7 +315,8 @@ module Samagotchi
|
|
|
314
315
|
def stop(id, wait:) = SessionManager.stop_session(id, wait: wait, **dir)
|
|
315
316
|
|
|
316
317
|
# @return [SessionManager::Restarted]
|
|
317
|
-
|
|
318
|
+
# @param start_stopped [Boolean] a person's restart: start a stopped session again
|
|
319
|
+
def restart(id, start_stopped: false) = SessionManager.restart_session(id, start_stopped: start_stopped, **dir)
|
|
318
320
|
|
|
319
321
|
# @return [SessionManager::ArchiveResult]
|
|
320
322
|
# @param stop_tasks [Boolean] stop the background tasks it or a
|
|
@@ -566,19 +568,131 @@ module Samagotchi
|
|
|
566
568
|
end
|
|
567
569
|
|
|
568
570
|
# --- peers (PeerMailbox: the peer's side; PeerReceipts: the worker's)
|
|
569
|
-
|
|
570
|
-
#
|
|
571
|
-
#
|
|
572
|
-
#
|
|
573
|
-
#
|
|
574
|
-
|
|
575
|
-
#
|
|
571
|
+
#
|
|
572
|
+
# The pairing follows the chain (a continue copies it, PeerContinue):
|
|
573
|
+
# a pair, unpair, list or say of a continued id runs on its chain's
|
|
574
|
+
# live link (#peer_head) whole, every step keyed on that link, and
|
|
575
|
+
# names it (+forwarded_to+).
|
|
576
|
+
|
|
577
|
+
# Pairs +peer+ with session +id+ (link.json), or with its chain's live
|
|
578
|
+
# link when +id+ was continued. Refused, nothing written, when the
|
|
579
|
+
# session is being continued or a live worker of it doesn't take the
|
|
580
|
+
# peer mail (a pairing to it would look done and not work). The
|
|
581
|
+
# pairing is written under a shared hold of the session's transition
|
|
582
|
+
# lock, checked again there (#moved_on): a continue holding it refuses
|
|
583
|
+
# the pair, naming its new link.
|
|
584
|
+
# +peer_session+, the peer's own session id when the pairing knows it
|
|
585
|
+
# (PeerPresence): a pairing that paired writes its file
|
|
586
|
+
# (paired/<peer session>/<link>, how a resume finds its pairings).
|
|
587
|
+
# Any pairing that paired ends the link's wait for +peer+.
|
|
576
588
|
# @return [PeerPaired]
|
|
577
589
|
# @raise [PeerMailbox::Invalid] a bad name
|
|
578
590
|
# @raise [Session::NotFound] no such session
|
|
579
|
-
def peer_pair(id, peer, address:, from:)
|
|
591
|
+
def peer_pair(id, peer, address:, from:, peer_session: nil)
|
|
580
592
|
PeerMailbox.check_name!(peer)
|
|
581
593
|
raise Session::NotFound, id unless Session.exist?(id, **dir)
|
|
594
|
+
|
|
595
|
+
head = peer_head(id)
|
|
596
|
+
paired = pair_on(head, peer, address: address, from: from, peer_session: peer_session)
|
|
597
|
+
head == id ? paired : paired.with(forwarded_to: head)
|
|
598
|
+
end
|
|
599
|
+
|
|
600
|
+
# Ends the pairing (link.json) of +id+ and, when it was continued, of
|
|
601
|
+
# every link after it to its chain's live link (the one chi sends
|
|
602
|
+
# from; a link between keeps a copy that deleting the live link would
|
|
603
|
+
# bring back with the link it un-archives); the messages stay.
|
|
604
|
+
# Transition hold: none (a removal; refusing it would only make the
|
|
605
|
+
# owner retry).
|
|
606
|
+
# Its wait for +peer+ ends too (the chip's Cancel is this route), and
|
|
607
|
+
# the pairing files of each link (PeerPresence).
|
|
608
|
+
# @return [PeerUnpaired]
|
|
609
|
+
def peer_unpair(id, peer)
|
|
610
|
+
path = Session.exist?(id, **dir) ? SessionChain::Links.new(state_dir: state_dir!).to_first_live(id) : [id]
|
|
611
|
+
ended = path.map { |link| PeerMailbox.unpair(link, peer, state_dir: state_dir!) }
|
|
612
|
+
waited = path.map { |link| PeerPresence.stop_waiting(peer, link, state_dir: state_dir!) }
|
|
613
|
+
path.each { |link| PeerPresence.unpaired!(peer, link, state_dir: state_dir!) }
|
|
614
|
+
PeerUnpaired.new(unpaired: ended.any?, forwarded_to: (path.last unless path.last == id), waited: waited.any?)
|
|
615
|
+
end
|
|
616
|
+
|
|
617
|
+
# A resume's refresh (chi peer announce): +peer+'s pairing of +id+'s
|
|
618
|
+
# chain's live link, paired again with +address+ when it is still this
|
|
619
|
+
# +peer_session+'s (#peer_pair: its busy, continue and worker-age
|
|
620
|
+
# rules), else the pairing's file dropped (an unpair or another peer
|
|
621
|
+
# session paired it since). nil: no pairing of this peer session's.
|
|
622
|
+
# @return [PeerPaired, nil] status :same when the address is the one paired
|
|
623
|
+
def peer_refresh(id, peer, address:, peer_session:)
|
|
624
|
+
PeerMailbox.check_name!(peer)
|
|
625
|
+
head = Session.exist?(id, **dir) ? peer_head(id) : nil
|
|
626
|
+
link = head && PeerMailbox.link(head, peer, state_dir: state_dir!)
|
|
627
|
+
unless link&.peer_session == peer_session
|
|
628
|
+
PeerPresence.drop_paired(peer, peer_session, id, state_dir: state_dir!)
|
|
629
|
+
return nil
|
|
630
|
+
end
|
|
631
|
+
return PeerPaired.new(status: :same, link: link, forwarded_to: (head unless head == id)) if link.address == address
|
|
632
|
+
|
|
633
|
+
paired = pair_on(head, peer, address: address, from: link.from, peer_session: peer_session)
|
|
634
|
+
head == id ? paired : paired.with(forwarded_to: head)
|
|
635
|
+
end
|
|
636
|
+
|
|
637
|
+
# A /clear's rename (chi peer announce): +id+'s chain's live link
|
|
638
|
+
# paired with +peer+'s session +from+ names +to+ instead; its address
|
|
639
|
+
# and when it paired stay.
|
|
640
|
+
# @return [Boolean] whether a link was renamed
|
|
641
|
+
def peer_retag(id, peer, from:, to:)
|
|
642
|
+
return false unless Session.exist?(id, **dir)
|
|
643
|
+
|
|
644
|
+
head = peer_head(id)
|
|
645
|
+
link = PeerMailbox.link(head, peer, state_dir: state_dir!)
|
|
646
|
+
return false unless link&.peer_session == from
|
|
647
|
+
|
|
648
|
+
PeerMailbox.write_link(head, link.with(peer_session: to), state_dir: state_dir!)
|
|
649
|
+
PeerPresence.paired!(peer, to, head, state_dir: state_dir!)
|
|
650
|
+
true
|
|
651
|
+
end
|
|
652
|
+
|
|
653
|
+
# The peers of +id+, or of its chain's live link when it was continued.
|
|
654
|
+
# @return [Array<PeerRoster::Peer>]
|
|
655
|
+
def peer_list(id) = PeerRoster.build(Session.exist?(id, **dir) ? peer_head(id) : id, state_dir: state_dir!)
|
|
656
|
+
|
|
657
|
+
# A message from +peer+ into session +id+, as a delivery
|
|
658
|
+
# (SessionInbox.deliver :peer), run on +id+'s chain's live link when
|
|
659
|
+
# +id+ was continued (#peer_head; the line names it). Refused before
|
|
660
|
+
# anything is written for a gone session, a continued one whose live
|
|
661
|
+
# link isn't paired (an older chi's continue: it names that link), no
|
|
662
|
+
# pairing, a live worker without the peer mail, UNTAKEN_MAX unread;
|
|
663
|
+
# else, under a shared hold of the link's transition lock (checked
|
|
664
|
+
# again there, #moved_on: a continue holding it refuses the say, naming
|
|
665
|
+
# its new link, so no message lands in a mailbox its carry already
|
|
666
|
+
# read; the pairing read again there too, as the continue copies it),
|
|
667
|
+
# written inside the delivery's block (a session being deleted refuses
|
|
668
|
+
# first), which asks for a worker by the message's own +wake+ alone (no
|
|
669
|
+
# context.wake, no window). Then what chi did, at once when it can't
|
|
670
|
+
# answer yet, else the receipt within +wait+ seconds (a look every
|
|
671
|
+
# PeerSay::POLL_SECONDS).
|
|
672
|
+
# @return [PeerSay]
|
|
673
|
+
# @raise [PeerMailbox::Invalid] a bad name, an empty or oversized text
|
|
674
|
+
def peer_say(id, peer, text:, summary:, wake:, re:, wait: 0, sleeper: ->(seconds) { sleep(seconds) },
|
|
675
|
+
clock: -> { monotonic })
|
|
676
|
+
PeerMailbox.check_name!(peer)
|
|
677
|
+
PeerMailbox.checked_text(text)
|
|
678
|
+
return PeerSay.new(status: :gone) unless Session.exist?(id, **dir)
|
|
679
|
+
|
|
680
|
+
head = peer_head(id)
|
|
681
|
+
said = say_on(head, id, peer, text: text, summary: summary, wake: wake, re: re, wait: wait, sleeper: sleeper,
|
|
682
|
+
clock: clock)
|
|
683
|
+
head == id || !said.received? ? said : said.with(forwarded_to: head)
|
|
684
|
+
end
|
|
685
|
+
|
|
686
|
+
private
|
|
687
|
+
|
|
688
|
+
# The link a peer command of +id+ runs on: +id+, or, when it is
|
|
689
|
+
# archived, its chain's first live link from it (SessionChain::Links
|
|
690
|
+
# #first_live, SessionInbox::Head's walk; the chain's end when every
|
|
691
|
+
# link after it is archived). Only an archived id lists the sessions.
|
|
692
|
+
def peer_head(id) = SessionChain::Links.new(state_dir: state_dir!).first_live(id)
|
|
693
|
+
|
|
694
|
+
# #peer_pair on the link +id+ (#peer_head's).
|
|
695
|
+
def pair_on(id, peer, address:, from:, peer_session: nil)
|
|
582
696
|
if (successor = continued_as(id))
|
|
583
697
|
return PeerPaired.new(status: :continued, next_id: successor.id, in_flight: successor.in_flight?)
|
|
584
698
|
end
|
|
@@ -591,7 +705,9 @@ module Samagotchi
|
|
|
591
705
|
next PeerPaired.new(status: :continued, next_id: moved.id, in_flight: moved.in_flight?)
|
|
592
706
|
end
|
|
593
707
|
|
|
594
|
-
link = PeerMailbox.pair(id, peer, address: address, from: from, state_dir: state_dir!)
|
|
708
|
+
link = PeerMailbox.pair(id, peer, address: address, from: from, peer_session: peer_session, state_dir: state_dir!)
|
|
709
|
+
PeerPresence.paired!(peer, peer_session, id, state_dir: state_dir!) if peer_session
|
|
710
|
+
PeerPresence.stop_waiting(peer, id, state_dir: state_dir!)
|
|
595
711
|
location = ContextSources.session_location(id, state_dir: state_dir!)
|
|
596
712
|
# These are exactly the bundles' LEGACY_INBOX and LEGACY_OUTBOX names.
|
|
597
713
|
["#{peer}-inbox", "#{peer}-outbox"].each do |name|
|
|
@@ -603,37 +719,17 @@ module Samagotchi
|
|
|
603
719
|
end
|
|
604
720
|
end
|
|
605
721
|
|
|
606
|
-
#
|
|
607
|
-
#
|
|
608
|
-
#
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
# @return [Array<PeerRoster::Peer>]
|
|
612
|
-
def peer_list(id) = PeerRoster.build(id, state_dir: state_dir!)
|
|
613
|
-
|
|
614
|
-
# A message from +peer+ into session +id+, as a delivery
|
|
615
|
-
# (SessionInbox.deliver :peer): refused before anything is written
|
|
616
|
-
# for a gone session, a continued one, no pairing, a live worker
|
|
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
|
|
622
|
-
# asks for a worker by the message's own +wake+ alone (no
|
|
623
|
-
# context.wake, no window). Then what chi did, at once when it can't
|
|
624
|
-
# answer yet, else the receipt within +wait+ seconds (a look every
|
|
625
|
-
# PeerSay::POLL_SECONDS).
|
|
626
|
-
# @return [PeerSay]
|
|
627
|
-
# @raise [PeerMailbox::Invalid] a bad name, an empty or oversized text
|
|
628
|
-
def peer_say(id, peer, text:, summary:, wake:, re:, wait: 0, sleeper: ->(seconds) { sleep(seconds) },
|
|
629
|
-
clock: -> { monotonic })
|
|
630
|
-
PeerMailbox.check_name!(peer)
|
|
631
|
-
PeerMailbox.checked_text(text)
|
|
632
|
-
return PeerSay.new(status: :gone) unless Session.exist?(id, **dir)
|
|
722
|
+
# #peer_say on the link +id+ (#peer_head's for +asked+). A live link
|
|
723
|
+
# with no pairing of a continued +asked+ (an older chi continued it,
|
|
724
|
+
# copying none) is refused as a continued link, naming it: the peer's
|
|
725
|
+
# skill pairs again there.
|
|
726
|
+
def say_on(id, asked, peer, text:, summary:, wake:, re:, wait:, sleeper:, clock:)
|
|
633
727
|
if (successor = continued_as(id))
|
|
634
728
|
return PeerSay.new(status: :continued, next_id: successor.id, in_flight: successor.in_flight?)
|
|
635
729
|
end
|
|
636
|
-
|
|
730
|
+
|
|
731
|
+
unpaired = id == asked ? PeerSay.new(status: :not_paired) : PeerSay.new(status: :continued, next_id: id)
|
|
732
|
+
return unpaired if id == asked && !PeerMailbox.link(id, peer, state_dir: state_dir!)
|
|
637
733
|
return PeerSay.new(status: :old_worker) if old_peer_worker?(id)
|
|
638
734
|
|
|
639
735
|
message = nil
|
|
@@ -645,6 +741,7 @@ module Samagotchi
|
|
|
645
741
|
when SessionChain::Successor
|
|
646
742
|
return PeerSay.new(status: :continued, next_id: moved.id, in_flight: moved.in_flight?)
|
|
647
743
|
end
|
|
744
|
+
return unpaired unless PeerMailbox.link(id, peer, state_dir: state_dir!)
|
|
648
745
|
|
|
649
746
|
SessionInbox.deliver(:peer, id, state_dir: state_dir!) do
|
|
650
747
|
message = PeerMailbox.post(id, peer, text: text, summary: summary, wake: wake, re: re, state_dir: state_dir!,
|
|
@@ -661,14 +758,26 @@ module Samagotchi
|
|
|
661
758
|
start_failed = true
|
|
662
759
|
nil
|
|
663
760
|
end
|
|
664
|
-
return
|
|
761
|
+
return refused_say(id, delivered.wake) if delivered&.refused?
|
|
665
762
|
|
|
666
763
|
at_once = start_failed ? :start_failed : peer_at_once(id, delivered.wake)
|
|
667
764
|
receipt = at_once ? nil : peer_receipt_within(id, peer, message.serial, wait, sleeper, clock)
|
|
668
765
|
peer_said(id, peer, message.serial, at_once, receipt)
|
|
669
766
|
end
|
|
670
767
|
|
|
671
|
-
|
|
768
|
+
# A say the delivery refused, nothing written: a transition's hold
|
|
769
|
+
# (Hold.under: a continue naming its new link, else busy), a delete,
|
|
770
|
+
# or the session gone.
|
|
771
|
+
def refused_say(id, why)
|
|
772
|
+
case why
|
|
773
|
+
when :deleting then PeerSay.new(status: :deleting)
|
|
774
|
+
when :busy then PeerSay.new(status: :busy)
|
|
775
|
+
when :continued
|
|
776
|
+
successor = SessionChain::Successor.of(id, state_dir: state_dir!, in_flight: true)
|
|
777
|
+
successor ? PeerSay.new(status: :continued, next_id: successor.id, in_flight: successor.in_flight?) : PeerSay.new(status: :busy)
|
|
778
|
+
else PeerSay.new(status: :gone)
|
|
779
|
+
end
|
|
780
|
+
end
|
|
672
781
|
|
|
673
782
|
# Whether +id+'s live worker doesn't take the peer mail (its Bridge
|
|
674
783
|
# features: a read-only look). No live worker: nil (the next one does).
|
|
@@ -485,13 +485,13 @@ module Samagotchi
|
|
|
485
485
|
reply.json["released"] != false
|
|
486
486
|
end
|
|
487
487
|
|
|
488
|
-
# @return [SessionManager::Restarted]
|
|
489
|
-
def restart(id)
|
|
490
|
-
reply = request(:post, "#{session_path(id)}/restart")
|
|
488
|
+
# @return [SessionManager::Restarted] +start_stopped+ is always sent (chi web takes none as a person's: the page's)
|
|
489
|
+
def restart(id, start_stopped: false)
|
|
490
|
+
reply = request(:post, "#{session_path(id)}/restart", body: { start_stopped: start_stopped == true })
|
|
491
491
|
raise_refusal(reply) unless reply.status == 200
|
|
492
492
|
|
|
493
493
|
SessionManager::Restarted.new(session_id: reply.json["session_id"] || id, from_version: reply.json["from_version"],
|
|
494
|
-
version: reply.json["version"])
|
|
494
|
+
version: reply.json["version"], was_stopped: (true if reply.json["was_stopped"] == true))
|
|
495
495
|
end
|
|
496
496
|
|
|
497
497
|
# @return [SessionManager::ArchiveResult] the reply's lists coerced to arrays
|
|
@@ -16,13 +16,15 @@ module Samagotchi
|
|
|
16
16
|
PEER_SAY_SLACK = 30
|
|
17
17
|
|
|
18
18
|
# @return [PeerPaired]
|
|
19
|
-
def peer_pair(id, peer, address:, from:)
|
|
19
|
+
def peer_pair(id, peer, address:, from:, peer_session: nil)
|
|
20
20
|
PeerMailbox.check_name!(peer)
|
|
21
|
-
|
|
21
|
+
body = { address: address, from: from }
|
|
22
|
+
body[:peer_session] = peer_session if peer_session
|
|
23
|
+
reply = peer_request(:post, "#{session_path(id)}/peers/#{peer}/pair", body: body)
|
|
22
24
|
case reply.status
|
|
23
25
|
when 200
|
|
24
26
|
link = PeerMailbox::PeerLink.from_h(reply.json["link"]) or raise RemoteFailed, "#{@entry.label}: no link in its answer"
|
|
25
|
-
PeerPaired.new(status: :paired, link: link)
|
|
27
|
+
PeerPaired.new(status: :paired, link: link, forwarded_to: PeerSay.id_or_nil(reply.json["forwarded_to"]))
|
|
26
28
|
when 409
|
|
27
29
|
status = %w[continued busy old_worker].find { |known| known == reply.json["status"] } or raise_refusal(reply)
|
|
28
30
|
PeerPaired.new(status: status.to_sym, next_id: reply.json["next_id"]&.to_s, in_flight: reply.json["in_flight"] == true)
|
|
@@ -30,13 +32,14 @@ module Samagotchi
|
|
|
30
32
|
end
|
|
31
33
|
end
|
|
32
34
|
|
|
33
|
-
# @return [
|
|
35
|
+
# @return [PeerUnpaired] (an older chi web names no +forwarded_to+ or +waited+)
|
|
34
36
|
def peer_unpair(id, peer)
|
|
35
37
|
PeerMailbox.check_name!(peer)
|
|
36
38
|
reply = peer_request(:delete, "#{session_path(id)}/peers/#{peer}")
|
|
37
39
|
raise_refusal(reply) unless reply.status == 200
|
|
38
40
|
|
|
39
|
-
reply.json["unpaired"] == true
|
|
41
|
+
PeerUnpaired.new(unpaired: reply.json["unpaired"] == true, forwarded_to: PeerSay.id_or_nil(reply.json["forwarded_to"]),
|
|
42
|
+
waited: reply.json["waited"] == true)
|
|
40
43
|
end
|
|
41
44
|
|
|
42
45
|
# @return [Array<PeerRoster::Peer>]
|
|
@@ -84,10 +84,15 @@ module Samagotchi
|
|
|
84
84
|
# From +id+ forward over archived links: +id+ when it is not archived,
|
|
85
85
|
# else the first link after it that is not, or the chain's end (over a
|
|
86
86
|
# pruned anchor's gap too). Only an archived +id+ makes the listing.
|
|
87
|
-
def first_live(id)
|
|
88
|
-
return id unless archived?(id)
|
|
87
|
+
def first_live(id) = to_first_live(id).last
|
|
89
88
|
|
|
90
|
-
|
|
89
|
+
# The links #first_live walks from +id+, +id+ first, the one it
|
|
90
|
+
# reaches last (a peer unpair ends the pairing on each).
|
|
91
|
+
# @return [Array<String>]
|
|
92
|
+
def to_first_live(id)
|
|
93
|
+
return [id] unless archived?(id)
|
|
94
|
+
|
|
95
|
+
walk(id, forward: true) { |link| !archived?(link) }
|
|
91
96
|
end
|
|
92
97
|
|
|
93
98
|
# The link after a pruned anchor's gap: the head of a schedule anchored
|
|
@@ -7,6 +7,7 @@ require_relative "served_model"
|
|
|
7
7
|
require_relative "prompt_note"
|
|
8
8
|
require_relative "turn_flow"
|
|
9
9
|
require_relative "cache_confirm"
|
|
10
|
+
require_relative "user_typed"
|
|
10
11
|
require_relative "tools/execute"
|
|
11
12
|
require_relative "tools/builtins"
|
|
12
13
|
require_relative "context_candidate_choice"
|
|
@@ -182,7 +183,7 @@ module Samagotchi
|
|
|
182
183
|
private
|
|
183
184
|
|
|
184
185
|
def dispatch(entry, text, confirm:, as_parent:, user_ui:)
|
|
185
|
-
return run_bundle_command(entry, text, confirm: confirm) unless entry.source == "core"
|
|
186
|
+
return run_bundle_command(entry, text, confirm: confirm, user_ui: user_ui) unless entry.source == "core"
|
|
186
187
|
|
|
187
188
|
# Only commands that may ask take the flag.
|
|
188
189
|
return send(entry.id, text, confirm: confirm) if CONFIRMING.include?(entry.id)
|
|
@@ -199,16 +200,24 @@ module Samagotchi
|
|
|
199
200
|
# The resolved ref (what the session stores and --alias/--default write).
|
|
200
201
|
def model_name = @engine.effective_model_ref
|
|
201
202
|
|
|
203
|
+
# The model as /model shows it: a served one with the id it runs on now
|
|
204
|
+
# ("main:served → q8"), else its ref.
|
|
205
|
+
def model_shown
|
|
206
|
+
id = Degrade.call(:model, "served_id_unreadable") { @engine.served_model_id }
|
|
207
|
+
id ? "#{model_name} → #{id}" : model_name
|
|
208
|
+
end
|
|
209
|
+
|
|
202
210
|
# A bundle plugin's command: its handler gets the text after the name
|
|
203
211
|
# (--yes kept); what it returns is the output (nil: nothing to show),
|
|
204
212
|
# or [output, CacheConfirm::Confirm or nil] for a change that asks
|
|
205
213
|
# first (ctx.cache_confirm), and a raise is an error result. The
|
|
206
214
|
# sender's +confirm+ (false once the line has --yes) reaches
|
|
207
|
-
# ctx.cache_confirm through the thread (CacheConfirm.asking)
|
|
208
|
-
|
|
215
|
+
# ctx.cache_confirm through the thread (CacheConfirm.asking), and
|
|
216
|
+
# +user_ui+ (a person at a UI typed it) ctx.user_typed? (UserTyped).
|
|
217
|
+
def run_bundle_command(entry, text, confirm: false, user_ui: false)
|
|
209
218
|
args = text.delete_prefix(entry.name).strip
|
|
210
219
|
_, yes = CacheConfirm.split(args)
|
|
211
|
-
output, ask = bundle_reply(CacheConfirm.asking(confirm && !yes) { entry.handler.call(args) })
|
|
220
|
+
output, ask = bundle_reply(UserTyped.as(user_ui) { CacheConfirm.asking(confirm && !yes) { entry.handler.call(args) } })
|
|
212
221
|
reply(output, confirm: ask)
|
|
213
222
|
rescue StandardError => e
|
|
214
223
|
reply("#{entry.name}: #{e.class}: #{e.message}", status: :error)
|
|
@@ -611,9 +620,9 @@ module Samagotchi
|
|
|
611
620
|
def model_command(input, confirm: false)
|
|
612
621
|
suffix = input.delete_prefix(MODEL_COMMAND).strip
|
|
613
622
|
if suffix.empty?
|
|
614
|
-
return ["runtime model: #{
|
|
623
|
+
return ["runtime model: #{model_shown}#{model_note}#{served_note}#{sampling_note}#{steer_cut_after_note}#{thinking_note}#{reasoning_history_note}#{notes_note}", false] if @default_model.nil? || model_name == @engine.model_ref_for(@default_model)
|
|
615
624
|
|
|
616
|
-
return ["runtime model: #{
|
|
625
|
+
return ["runtime model: #{model_shown}#{model_note("default: #{@default_model}")}#{served_note}#{sampling_note}#{steer_cut_after_note}#{thinking_note}#{reasoning_history_note}#{notes_note}", false]
|
|
617
626
|
end
|
|
618
627
|
|
|
619
628
|
# Parse flags: --default and --alias <name> / --alias=<name> (tolerant order)
|
|
@@ -864,9 +873,11 @@ module Samagotchi
|
|
|
864
873
|
|
|
865
874
|
(row.configured ? declared : shown) << [row, identifier]
|
|
866
875
|
end
|
|
867
|
-
|
|
876
|
+
served_line = served_models_line(hname, data, by_model, needle)
|
|
877
|
+
next if declared.empty? && shown.empty? && !served_line
|
|
868
878
|
|
|
869
879
|
lines << "#{host_label}:" unless data.error
|
|
880
|
+
lines << served_line if served_line
|
|
870
881
|
hidden = needle.empty? ? [shown.size - MODELS_PER_HOST, 0].max : 0
|
|
871
882
|
(declared + shown.first(shown.size - hidden)).each do |row, identifier|
|
|
872
883
|
raw = row.info&.raw || {}
|
|
@@ -889,7 +900,10 @@ module Samagotchi
|
|
|
889
900
|
end
|
|
890
901
|
end
|
|
891
902
|
# Warnings for unreachable hosts are already in lines; no failover
|
|
892
|
-
|
|
903
|
+
# A served target (main:served) is no orphan: it names no listed id.
|
|
904
|
+
orphans = aliases.reject do |_, model_id|
|
|
905
|
+
registry.served?(model_id) || seen.include?(model_id.downcase) || seen.include?(registry.parse_qualified_model(model_id).last.downcase)
|
|
906
|
+
end
|
|
893
907
|
unless orphans.empty? || !needle.empty?
|
|
894
908
|
lines << ""
|
|
895
909
|
lines << "orphan aliases (target not discovered):"
|
|
@@ -901,6 +915,24 @@ module Samagotchi
|
|
|
901
915
|
"unable to list models: #{e.message}"
|
|
902
916
|
end
|
|
903
917
|
|
|
918
|
+
# A host's served row for /models (" served → q8 (alias: splash)"):
|
|
919
|
+
# only when its own listing names exactly one id (ServedLookup.one_id),
|
|
920
|
+
# and +needle+ is blank or in `served` or the id; nil otherwise. An
|
|
921
|
+
# alias of host:served (or of a bare `served`, on the default host)
|
|
922
|
+
# shows on it.
|
|
923
|
+
def served_models_line(hname, data, by_model, needle)
|
|
924
|
+
# Here, not at load: the testing kit loads this file, not the registry's.
|
|
925
|
+
require_relative "served_lookup"
|
|
926
|
+
id = data.error ? nil : ServedLookup.one_id(data.models)
|
|
927
|
+
return nil unless id && (needle.empty? || ServedRef::WORD.include?(needle) || id.downcase.include?(needle))
|
|
928
|
+
|
|
929
|
+
# by_model's keys are downcased (the registry downcases host names too).
|
|
930
|
+
aliases = by_model.fetch("#{hname}:#{ServedRef::WORD}".downcase, [])
|
|
931
|
+
aliases += by_model.fetch(ServedRef::WORD, []) if hname == @engine.host_registry.default_entry&.name
|
|
932
|
+
base = " #{ServedRef::WORD} → #{id}"
|
|
933
|
+
aliases.empty? ? base : "#{base} (alias: #{aliases.uniq.join(", ")})"
|
|
934
|
+
end
|
|
935
|
+
|
|
904
936
|
# OpenRouter lists a "<id>:batch" variant next to many ids (71 of 457 on
|
|
905
937
|
# 2026-09-23), which only repeats the model. The ids of such variants
|
|
906
938
|
# whose plain id is listed too, unless the filter asks for batch.
|
|
@@ -128,12 +128,17 @@ module Samagotchi
|
|
|
128
128
|
# written into the link only (NoteMove::CONTINUE_SOURCES: never given
|
|
129
129
|
# back), which goes with its delete.
|
|
130
130
|
CarryStep.new(name: :objectives, store: :session_files, carry: :carry_objectives, undo: {}, files: []),
|
|
131
|
-
# The peers'
|
|
132
|
-
#
|
|
133
|
-
#
|
|
134
|
-
#
|
|
135
|
-
#
|
|
136
|
-
|
|
131
|
+
# The peers' pairings, copied (PeerContinue: the peer does nothing),
|
|
132
|
+
# then the messages it hadn't read, as one note; the messages stay with
|
|
133
|
+
# the previous link (their store rows say so). No undo: the copies are
|
|
134
|
+
# in the link's mailbox folder, removed with its delete (:peer_mailbox),
|
|
135
|
+
# and the note is in the link only, excluded from NoteMove's give-back;
|
|
136
|
+
# the previous link's mailbox and receipts are only read. A wait for a
|
|
137
|
+
# peer (PeerPresence) moves to the link, keeping when it started, so
|
|
138
|
+
# it shows, pairs, cancels and times out there; a failed continue's
|
|
139
|
+
# delete removes it with the link.
|
|
140
|
+
CarryStep.new(name: :peers, store: :peer_mailbox, carry: :carry_peers, undo: {},
|
|
141
|
+
files: ["link.json", "waiting/<id>.json"]),
|
|
137
142
|
# Its waiting notes last: the carry above read a pending /objectives
|
|
138
143
|
# edit from them first. They keep their names, so they still sort
|
|
139
144
|
# ahead of the link's own notes. A note sent to the previous link
|
|
@@ -32,8 +32,10 @@ module Samagotchi
|
|
|
32
32
|
# worker_newer is WorkerLink::Skew#short_message (Schedules::BusyPart's
|
|
33
33
|
# BUSY_WORDS; Schedules#busy_cell reads these two columns). Resume's live is "a
|
|
34
34
|
# worker holds it"; restart's fallback is its unresponsive-worker path,
|
|
35
|
-
# its stop_marked a stop marked
|
|
36
|
-
#
|
|
35
|
+
# its stop_marked a stop marked (refused, or with start_stopped, a
|
|
36
|
+
# person's restart, started again: restart_session's op-local branch)
|
|
37
|
+
# and its deleting a deleter holds the lock (it reads this table:
|
|
38
|
+
# restart_session's precheck); attach's gone_* are
|
|
37
39
|
# LocalLink's [:gone, why]. absent's delete and schedule cells are
|
|
38
40
|
# unreachable (a gone session is refused before the gate). Resolution (a
|
|
39
41
|
# chain's head, a continue's successor) stays outside: these are the
|
|
@@ -84,6 +84,11 @@ module Samagotchi
|
|
|
84
84
|
# (DeliverResult::STOPPED / ARCHIVED: only a person's message does).
|
|
85
85
|
def wake_refused? = DeliverResult::WAKE_REFUSALS.include?(error)
|
|
86
86
|
|
|
87
|
+
# A stopped session's refusal (DeliverResult::STOPPED): only ever a
|
|
88
|
+
# non-owner's, since the owner's message un-stops it. chi send adds
|
|
89
|
+
# how its user starts it again (ChiInvocation#start_stopped).
|
|
90
|
+
def stopped? = error == DeliverResult::STOPPED["error"]
|
|
91
|
+
|
|
87
92
|
# A session being deleted (DeliverResult::DELETING): nothing went in.
|
|
88
93
|
def deleting? = error == DeliverResult::DELETING["error"]
|
|
89
94
|
|
|
@@ -24,7 +24,10 @@ module Samagotchi
|
|
|
24
24
|
# :fallback_held (disk-visible work blocks an unresponsive-worker restart),
|
|
25
25
|
# :stopped (its worker, +pid+, is SIGSTOPped: neither the ask nor a TERM
|
|
26
26
|
# reaches it until it is continued), :stop_marked (the session is stopped,
|
|
27
|
-
# or being stopped: `chi sessions stop`, no successor would come
|
|
27
|
+
# or being stopped: `chi sessions stop`, no successor would come; only an
|
|
28
|
+
# agent's restart or chi web's after an update: a person's starts it
|
|
29
|
+
# again), :still_stopping (a person's restart waited STOP_WAIT for a
|
|
30
|
+
# stopping worker to leave),
|
|
28
31
|
# :deleting (a deleter holds the lock: the session is going), or
|
|
29
32
|
# :failed (the worker answered something else or didn't release).
|
|
30
33
|
class RestartRefused < StandardError
|
|
@@ -63,6 +66,8 @@ module Samagotchi
|
|
|
63
66
|
"a restart: continue it#{" with `kill -CONT #{pid}`" if pid}#{where}, then restart again"
|
|
64
67
|
when :stop_marked
|
|
65
68
|
"session #{session_id} can't restart: #{DeliverResult::STOPPED["detail"]} (on the newest chi)"
|
|
69
|
+
when :still_stopping
|
|
70
|
+
"session #{session_id} is still stopping; restart it again in a moment"
|
|
66
71
|
when :deleting
|
|
67
72
|
"session #{session_id} can't restart: #{DeliverResult::DELETING["detail"]}"
|
|
68
73
|
when :not_running
|
|
@@ -78,11 +83,28 @@ module Samagotchi
|
|
|
78
83
|
else "not now: #{HOLDS.fetch(reason, reason.to_s.tr("_", " "))}"
|
|
79
84
|
end
|
|
80
85
|
end
|
|
86
|
+
|
|
87
|
+
# What the caller adds after the words, by who reads them: a stop
|
|
88
|
+
# marked's way back ("start it yourself: …" to a person, "ask your
|
|
89
|
+
# user to start it: …" to an agent, whose own chi -p --resume would
|
|
90
|
+
# run as a terminal and un-stop it); nil for the other reasons.
|
|
91
|
+
def self.next_step(reason, session_id, agent:, via: ChiInvocation::HERE)
|
|
92
|
+
return nil unless reason == :stop_marked
|
|
93
|
+
|
|
94
|
+
"#{agent ? "ask your user to start it" : "start it yourself"}: #{via.start_stopped(session_id)}"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# The words and, after them, #next_step.
|
|
98
|
+
def words_for(agent:, via: ChiInvocation::HERE)
|
|
99
|
+
[message, RestartRefused.next_step(reason, session_id, agent: agent, via: via)].compact.join("; ")
|
|
100
|
+
end
|
|
81
101
|
end
|
|
82
102
|
|
|
83
103
|
# What a restart did: the worker's version before and after (nil when
|
|
84
|
-
# the new worker hadn't published its Bridge within the wait)
|
|
85
|
-
|
|
104
|
+
# the new worker hadn't published its Bridge within the wait);
|
|
105
|
+
# was_stopped true when it started a stopped session again (no worker
|
|
106
|
+
# before: from_version nil).
|
|
107
|
+
Restarted = Struct.new(:session_id, :from_version, :version, :was_stopped, keyword_init: true)
|
|
86
108
|
|
|
87
109
|
RESTART_WAIT = 10.0
|
|
88
110
|
# A TERMed worker writes its recap on leave (an LLM call) while it still
|
|
@@ -116,16 +138,23 @@ module Samagotchi
|
|
|
116
138
|
# worker (POST /exit restart: true), which leaves only when nothing would
|
|
117
139
|
# be lost and starts its successor. If the worker cannot be asked, stop
|
|
118
140
|
# it without a stop marker and resume only when disk shows no work to lose.
|
|
141
|
+
# A stopped session (stop marked) is refused :stop_marked, unless
|
|
142
|
+
# +start_stopped+ (a person's restart: the CLI with no agent, the web's
|
|
143
|
+
# Restart): then it is started again as `chi --resume` does (the stop
|
|
144
|
+
# cleared, a worker spawned; a stopping one once its worker has left,
|
|
145
|
+
# else :still_stopping). Its delegates the stop stopped stay stopped.
|
|
146
|
+
# @param start_stopped [Boolean] start a stopped session again
|
|
119
147
|
# @return [Restarted]
|
|
120
148
|
# @raise [OwnedByTUI] a one-turn chi run owns the session
|
|
121
149
|
# @raise [RestartRefused]
|
|
122
150
|
# @raise [ArgumentError] no such session
|
|
123
|
-
def restart_session(session_id, state_dir: nil, wait: RESTART_WAIT, client_id: ClientId::CLI_RESTART)
|
|
151
|
+
def restart_session(session_id, state_dir: nil, wait: RESTART_WAIT, client_id: ClientId::CLI_RESTART, start_stopped: false)
|
|
124
152
|
sd = state_dir || Session.default_state_dir
|
|
125
|
-
Session.load(session_id, state_dir: sd) # ArgumentError for none
|
|
153
|
+
session = Session.load(session_id, state_dir: sd) # ArgumentError for none
|
|
126
154
|
# The lifecycle precheck is ALLOWED[:restart] for the state read now
|
|
127
|
-
# (a stop marked refuses
|
|
128
|
-
#
|
|
155
|
+
# (a stop marked refuses, or with start_stopped starts it again, before
|
|
156
|
+
# any worker is asked); the op-local checks (SIGSTOP, start_stopped,
|
|
157
|
+
# features, the Bridge's reply, the fallback) are here.
|
|
129
158
|
reading = SessionLifecycle.read(session_id, state_dir: sd, owner: session_owner(session_id, state_dir: sd))
|
|
130
159
|
cell = SessionLifecycle.allowed(:restart, reading.state)
|
|
131
160
|
raise OwnedByTUI, session_id if cell == :owned_by_tui
|
|
@@ -135,6 +164,10 @@ module Samagotchi
|
|
|
135
164
|
# A stopped worker answers neither the ask (READ_TIMEOUT) nor the
|
|
136
165
|
# fallback's TERM (FALLBACK_STOP_WAIT): say so now, not after ~70 s.
|
|
137
166
|
raise RestartRefused.new(session_id, :stopped, pid: owner.pid) if owner&.worker? && ProcessGroup.stopped?(owner.pid)
|
|
167
|
+
# A scratch session's worker comes only from its terminal (ScratchSession).
|
|
168
|
+
if cell == :stop_marked && start_stopped && !session.scratch
|
|
169
|
+
return start_stopped_again(session_id, stopping: reading.state == :stopping, state_dir: sd, wait: wait)
|
|
170
|
+
end
|
|
138
171
|
raise RestartRefused.new(session_id, cell) if SessionLifecycle.verdict(:restart, cell) == :refused
|
|
139
172
|
|
|
140
173
|
link = reading.presence.link
|
|
@@ -170,6 +203,24 @@ module Samagotchi
|
|
|
170
203
|
Restarted.new(session_id: session_id, from_version: sidecar.version, version: version)
|
|
171
204
|
end
|
|
172
205
|
|
|
206
|
+
# A person's restart of a stopped session: what `chi --resume` does,
|
|
207
|
+
# without a terminal. A stopping one waits (STOP_WAIT) for its worker
|
|
208
|
+
# to leave first; resume_session clears the stop and spawns (or finds
|
|
209
|
+
# the worker another start brought), and any live Bridge within +wait+
|
|
210
|
+
# is the new worker's: there was none.
|
|
211
|
+
# @raise [RestartRefused] :still_stopping
|
|
212
|
+
private def start_stopped_again(session_id, stopping:, state_dir:, wait:)
|
|
213
|
+
if stopping && !wait_for_owner_release(session_id, timeout: STOP_WAIT, state_dir: state_dir)
|
|
214
|
+
raise RestartRefused.new(session_id, :still_stopping)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
resume_session(session_id, state_dir: state_dir)
|
|
218
|
+
Log.info(:worker, "restart_stopped", sid: session_id, stopping: stopping)
|
|
219
|
+
fresh = WorkerLink.await(session_id, session_dir: Session.session_dir(session_id, state_dir: state_dir),
|
|
220
|
+
timeout: wait, unlink: false, interval: 0.1)
|
|
221
|
+
Restarted.new(session_id: session_id, from_version: nil, version: (fresh.version if fresh.live?), was_stopped: true)
|
|
222
|
+
end
|
|
223
|
+
|
|
173
224
|
private def restart_fallback(session_id, owner:, sidecar:, state_dir:, wait:, why:, error: nil)
|
|
174
225
|
Log.info(:worker, "restart_fallback", sid: session_id, why: why, error: error&.class&.name, msg: error&.message)
|
|
175
226
|
dir = Session.session_dir(session_id, state_dir: state_dir)
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "../session_protection"
|
|
4
|
+
require_relative "../peer_presence"
|
|
4
5
|
require_relative "../session_stores"
|
|
5
6
|
require_relative "running_tasks"
|
|
6
7
|
|
|
@@ -478,6 +479,8 @@ module Samagotchi
|
|
|
478
479
|
removed.concat(ContextSources.remove_session(id, state_dir: state_dir))
|
|
479
480
|
when :peer_mailbox
|
|
480
481
|
removed.concat(PeerMailbox.remove_session(id, state_dir: state_dir))
|
|
482
|
+
when :peer_presence
|
|
483
|
+
removed.concat(PeerPresence.remove_session(id, state_dir: state_dir))
|
|
481
484
|
when :participants
|
|
482
485
|
Participants.end_sessions([id], by: "delete", state_dir: state_dir)
|
|
483
486
|
when :system_one_runs
|
|
@@ -110,7 +110,12 @@ module Samagotchi
|
|
|
110
110
|
# carry the id.
|
|
111
111
|
unless prompt.to_s.strip.empty?
|
|
112
112
|
session.first_turn_id = SecureRandom.uuid
|
|
113
|
-
|
|
113
|
+
# Who sent it when no person did (a delegating parent, a
|
|
114
|
+
# schedule), and a person's screen (a web tab, a TUI), so its
|
|
115
|
+
# first turn is interactive as their next one is
|
|
116
|
+
# (ctx.turn_interactive?: a dormant peer send asks there); chi
|
|
117
|
+
# send's and no sender's run as the owner's (ClientId::FIRST_PROMPT).
|
|
118
|
+
session.first_turn_client_id = client_id if !ClientId.human?(client_id) || ClientId.interactive?(client_id)
|
|
114
119
|
end
|
|
115
120
|
# With a prompt or without (chi send --new's idle create, its line
|
|
116
121
|
# sent next): its creator's first turn is the driver's too.
|