samagotchi 0.191.0 → 0.193.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.
Files changed (141) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +91 -1
  3. data/docs/architecture.md +8 -7
  4. data/docs/bundles.md +46 -29
  5. data/docs/cli.md +37 -15
  6. data/docs/configuration.md +10 -4
  7. data/docs/context.md +8 -5
  8. data/docs/guardrails.md +9 -4
  9. data/docs/internals/bridge.md +3 -1
  10. data/docs/internals/bundles.md +2 -2
  11. data/docs/internals/inline-widgets.md +2 -1
  12. data/docs/internals/pages.md +4 -4
  13. data/docs/internals/participants.md +6 -8
  14. data/docs/internals/peers.md +75 -22
  15. data/docs/internals/prompt-caching.md +2 -1
  16. data/docs/internals/remote-sessions.md +9 -0
  17. data/docs/internals/session-lifecycle.md +36 -21
  18. data/docs/internals/web-frontend.md +195 -25
  19. data/docs/internals/working-set.md +5 -1
  20. data/docs/peers.md +71 -22
  21. data/docs/plugins.md +7 -4
  22. data/docs/releasing.md +30 -2
  23. data/docs/scheduling.md +10 -3
  24. data/docs/sessions.md +3 -3
  25. data/docs/testing.md +36 -5
  26. data/docs/thin-clients.md +114 -12
  27. data/lib/samagotchi/bridge.rb +2 -3
  28. data/lib/samagotchi/bundles/claude-peer/manifest.yml +2 -2
  29. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +33 -17
  30. data/lib/samagotchi/bundles/codex-peer/manifest.yml +2 -2
  31. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +34 -17
  32. data/lib/samagotchi/bundles/coordinator/manifest.yml +2 -2
  33. data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +31 -0
  34. data/lib/samagotchi/bundles/mcp/manifest.yml +4 -2
  35. data/lib/samagotchi/bundles/mcp/plugin.rb +7 -405
  36. data/lib/samagotchi/bundles/mcp/server_config.rb +158 -0
  37. data/lib/samagotchi/bundles/mcp/sign_in.rb +261 -0
  38. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  39. data/lib/samagotchi/client_id.rb +2 -19
  40. data/lib/samagotchi/compat.rb +6 -5
  41. data/lib/samagotchi/config.rb +7 -0
  42. data/lib/samagotchi/continue_offer.rb +13 -1
  43. data/lib/samagotchi/delivery.rb +3 -7
  44. data/lib/samagotchi/engine/models.rb +17 -0
  45. data/lib/samagotchi/engine/plugins.rb +2 -0
  46. data/lib/samagotchi/guardrails/pair_boundary.rb +49 -18
  47. data/lib/samagotchi/jsonl_store.rb +13 -2
  48. data/lib/samagotchi/llm/anthropic_messages.rb +11 -0
  49. data/lib/samagotchi/llm/openai_chat.rb +5 -0
  50. data/lib/samagotchi/llm/prompt_cache.rb +5 -0
  51. data/lib/samagotchi/pages/launch.rb +1 -1
  52. data/lib/samagotchi/peer_cold.rb +92 -0
  53. data/lib/samagotchi/peer_command.rb +24 -9
  54. data/lib/samagotchi/peer_continue.rb +32 -16
  55. data/lib/samagotchi/peer_exchanges.rb +7 -0
  56. data/lib/samagotchi/peer_mailbox.rb +21 -4
  57. data/lib/samagotchi/peer_receipts.rb +33 -6
  58. data/lib/samagotchi/peer_say.rb +34 -16
  59. data/lib/samagotchi/plugin/api.rb +2 -1
  60. data/lib/samagotchi/plugin/context.rb +13 -1
  61. data/lib/samagotchi/plugin/peer.rb +118 -53
  62. data/lib/samagotchi/plugin/peers.rb +13 -6
  63. data/lib/samagotchi/schedule_command.rb +1 -0
  64. data/lib/samagotchi/schedules/prune_part.rb +32 -0
  65. data/lib/samagotchi/schedules.rb +14 -9
  66. data/lib/samagotchi/session.rb +7 -0
  67. data/lib/samagotchi/session_access/local.rb +98 -37
  68. data/lib/samagotchi/session_access/remote.rb +1 -1
  69. data/lib/samagotchi/session_access/remote_peers.rb +3 -3
  70. data/lib/samagotchi/session_chain/links.rb +8 -3
  71. data/lib/samagotchi/session_continue.rb +15 -7
  72. data/lib/samagotchi/session_lifecycle.rb +4 -0
  73. data/lib/samagotchi/session_manager/delivery.rb +0 -6
  74. data/lib/samagotchi/session_manager/lifecycle.rb +30 -17
  75. data/lib/samagotchi/session_manager/listing.rb +2 -1
  76. data/lib/samagotchi/session_manager/removal.rb +7 -5
  77. data/lib/samagotchi/session_manager/spawn.rb +8 -4
  78. data/lib/samagotchi/session_manager.rb +4 -0
  79. data/lib/samagotchi/session_row.rb +2 -0
  80. data/lib/samagotchi/session_stores.rb +16 -11
  81. data/lib/samagotchi/session_transition.rb +61 -15
  82. data/lib/samagotchi/system_one/runs.rb +5 -2
  83. data/lib/samagotchi/testing/boundary.rb +0 -1
  84. data/lib/samagotchi/testing/host.rb +1 -0
  85. data/lib/samagotchi/testing/recorder.rb +3 -0
  86. data/lib/samagotchi/tools/cancel_schedule.rb +10 -6
  87. data/lib/samagotchi/version.rb +1 -1
  88. data/lib/samagotchi/web/app.rb +3 -1
  89. data/lib/samagotchi/web/ask_create.rb +138 -0
  90. data/lib/samagotchi/web/ask_routes.rb +56 -9
  91. data/lib/samagotchi/web/last_ask.rb +78 -0
  92. data/lib/samagotchi/web/peer_routes.rb +10 -5
  93. data/lib/samagotchi/web/public/agent_rows.js +206 -0
  94. data/lib/samagotchi/web/public/agent_runs.js +595 -0
  95. data/lib/samagotchi/web/public/answer_fold.js +41 -44
  96. data/lib/samagotchi/web/public/answer_refs.js +3 -9
  97. data/lib/samagotchi/web/public/answer_write.js +28 -0
  98. data/lib/samagotchi/web/public/app.js +75 -99
  99. data/lib/samagotchi/web/public/block_fix.js +30 -12
  100. data/lib/samagotchi/web/public/cards_place.js +6 -4
  101. data/lib/samagotchi/web/public/chains.js +3 -1
  102. data/lib/samagotchi/web/public/command_list.js +5 -3
  103. data/lib/samagotchi/web/public/composer.js +9 -6
  104. data/lib/samagotchi/web/public/composer_mount.js +18 -11
  105. data/lib/samagotchi/web/public/draft_keys.js +10 -0
  106. data/lib/samagotchi/web/public/drafts.js +278 -0
  107. data/lib/samagotchi/web/public/history_reveal.js +5 -2
  108. data/lib/samagotchi/web/public/index.html +38 -2
  109. data/lib/samagotchi/web/public/owner_claim.js +7 -12
  110. data/lib/samagotchi/web/public/page_view.js +1 -1
  111. data/lib/samagotchi/web/public/pages.css +1 -7
  112. data/lib/samagotchi/web/public/peer_mailbox_api.js +2 -1
  113. data/lib/samagotchi/web/public/peer_note.js +4 -2
  114. data/lib/samagotchi/web/public/peer_row.js +37 -0
  115. data/lib/samagotchi/web/public/prompt_history.js +6 -0
  116. data/lib/samagotchi/web/public/quiet_notes.js +12 -1
  117. data/lib/samagotchi/web/public/revise_stack.js +5 -2
  118. data/lib/samagotchi/web/public/send_turn.js +62 -23
  119. data/lib/samagotchi/web/public/session_actions.js +80 -16
  120. data/lib/samagotchi/web/public/session_prefs.js +9 -3
  121. data/lib/samagotchi/web/public/session_view.js +7 -2
  122. data/lib/samagotchi/web/public/turn_block.js +4 -0
  123. data/lib/samagotchi/web/public/turn_events.js +0 -2
  124. data/lib/samagotchi/web/public/unqueue.js +22 -9
  125. data/lib/samagotchi/web/public/viewer_self.js +1 -6
  126. data/lib/samagotchi/web/public/views.js +12 -1
  127. data/lib/samagotchi/web/session_access_routes.rb +6 -3
  128. data/lib/samagotchi/web/session_control.rb +4 -2
  129. data/lib/samagotchi/web/session_create.rb +50 -33
  130. data/lib/samagotchi/web/session_hub.rb +52 -36
  131. data/lib/samagotchi/web/session_summary.rb +45 -7
  132. data/lib/samagotchi/web/start_dir.rb +6 -4
  133. data/lib/samagotchi/worker/mail.rb +11 -1
  134. data/lib/samagotchi/worker.rb +7 -5
  135. data/lib/samagotchi/worker_inbound/mail.rb +34 -6
  136. data/lib/samagotchi/worker_inbound.rb +11 -3
  137. data/lib/samagotchi/worker_link.rb +13 -1
  138. data/lib/samagotchi/worker_presence.rb +22 -6
  139. data/lib/samagotchi/worker_wakes.rb +33 -5
  140. metadata +12 -2
  141. data/lib/samagotchi/web/public/pages_group.js +0 -67
@@ -566,19 +566,87 @@ module Samagotchi
566
566
  end
567
567
 
568
568
  # --- peers (PeerMailbox: the peer's side; PeerReceipts: the worker's)
569
-
570
- # Pairs +peer+ with session +id+ (link.json). Refused, nothing
571
- # written, when the session was continued or a live worker of it
572
- # doesn't take the peer mail (a pairing to it would look done and
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.
569
+ #
570
+ # The pairing follows the chain (a continue copies it, PeerContinue):
571
+ # a pair, unpair, list or say of a continued id runs on its chain's
572
+ # live link (#peer_head) whole, every step keyed on that link, and
573
+ # names it (+forwarded_to+).
574
+
575
+ # Pairs +peer+ with session +id+ (link.json), or with its chain's live
576
+ # link when +id+ was continued. Refused, nothing written, when the
577
+ # session is being continued or a live worker of it doesn't take the
578
+ # peer mail (a pairing to it would look done and not work). The
579
+ # pairing is written under a shared hold of the session's transition
580
+ # lock, checked again there (#moved_on): a continue holding it refuses
581
+ # the pair, naming its new link.
576
582
  # @return [PeerPaired]
577
583
  # @raise [PeerMailbox::Invalid] a bad name
578
584
  # @raise [Session::NotFound] no such session
579
585
  def peer_pair(id, peer, address:, from:)
580
586
  PeerMailbox.check_name!(peer)
581
587
  raise Session::NotFound, id unless Session.exist?(id, **dir)
588
+
589
+ head = peer_head(id)
590
+ paired = pair_on(head, peer, address: address, from: from)
591
+ head == id ? paired : paired.with(forwarded_to: head)
592
+ end
593
+
594
+ # Ends the pairing (link.json) of +id+ and, when it was continued, of
595
+ # every link after it to its chain's live link (the one chi sends
596
+ # from; a link between keeps a copy that deleting the live link would
597
+ # bring back with the link it un-archives); the messages stay.
598
+ # Transition hold: none (a removal; refusing it would only make the
599
+ # owner retry).
600
+ # @return [PeerUnpaired]
601
+ def peer_unpair(id, peer)
602
+ path = Session.exist?(id, **dir) ? SessionChain::Links.new(state_dir: state_dir!).to_first_live(id) : [id]
603
+ ended = path.map { |link| PeerMailbox.unpair(link, peer, state_dir: state_dir!) }
604
+ PeerUnpaired.new(unpaired: ended.any?, forwarded_to: (path.last unless path.last == id))
605
+ end
606
+
607
+ # The peers of +id+, or of its chain's live link when it was continued.
608
+ # @return [Array<PeerRoster::Peer>]
609
+ def peer_list(id) = PeerRoster.build(Session.exist?(id, **dir) ? peer_head(id) : id, state_dir: state_dir!)
610
+
611
+ # A message from +peer+ into session +id+, as a delivery
612
+ # (SessionInbox.deliver :peer), run on +id+'s chain's live link when
613
+ # +id+ was continued (#peer_head; the line names it). Refused before
614
+ # anything is written for a gone session, a continued one whose live
615
+ # link isn't paired (an older chi's continue: it names that link), no
616
+ # pairing, a live worker without the peer mail, UNTAKEN_MAX unread;
617
+ # else, under a shared hold of the link's transition lock (checked
618
+ # again there, #moved_on: a continue holding it refuses the say, naming
619
+ # its new link, so no message lands in a mailbox its carry already
620
+ # read; the pairing read again there too, as the continue copies it),
621
+ # written inside the delivery's block (a session being deleted refuses
622
+ # first), which 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)
633
+
634
+ head = peer_head(id)
635
+ said = say_on(head, id, peer, text: text, summary: summary, wake: wake, re: re, wait: wait, sleeper: sleeper,
636
+ clock: clock)
637
+ head == id || !said.received? ? said : said.with(forwarded_to: head)
638
+ end
639
+
640
+ private
641
+
642
+ # The link a peer command of +id+ runs on: +id+, or, when it is
643
+ # archived, its chain's first live link from it (SessionChain::Links
644
+ # #first_live, SessionInbox::Head's walk; the chain's end when every
645
+ # link after it is archived). Only an archived id lists the sessions.
646
+ def peer_head(id) = SessionChain::Links.new(state_dir: state_dir!).first_live(id)
647
+
648
+ # #peer_pair on the link +id+ (#peer_head's).
649
+ def pair_on(id, peer, address:, from:)
582
650
  if (successor = continued_as(id))
583
651
  return PeerPaired.new(status: :continued, next_id: successor.id, in_flight: successor.in_flight?)
584
652
  end
@@ -603,37 +671,17 @@ module Samagotchi
603
671
  end
604
672
  end
605
673
 
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).
608
- # @return [Boolean] whether there was one
609
- def peer_unpair(id, peer) = PeerMailbox.unpair(id, peer, state_dir: state_dir!)
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)
674
+ # #peer_say on the link +id+ (#peer_head's for +asked+). A live link
675
+ # with no pairing of a continued +asked+ (an older chi continued it,
676
+ # copying none) is refused as a continued link, naming it: the peer's
677
+ # skill pairs again there.
678
+ def say_on(id, asked, peer, text:, summary:, wake:, re:, wait:, sleeper:, clock:)
633
679
  if (successor = continued_as(id))
634
680
  return PeerSay.new(status: :continued, next_id: successor.id, in_flight: successor.in_flight?)
635
681
  end
636
- return PeerSay.new(status: :not_paired) unless PeerMailbox.link(id, peer, state_dir: state_dir!)
682
+
683
+ unpaired = id == asked ? PeerSay.new(status: :not_paired) : PeerSay.new(status: :continued, next_id: id)
684
+ return unpaired if id == asked && !PeerMailbox.link(id, peer, state_dir: state_dir!)
637
685
  return PeerSay.new(status: :old_worker) if old_peer_worker?(id)
638
686
 
639
687
  message = nil
@@ -645,6 +693,7 @@ module Samagotchi
645
693
  when SessionChain::Successor
646
694
  return PeerSay.new(status: :continued, next_id: moved.id, in_flight: moved.in_flight?)
647
695
  end
696
+ return unpaired unless PeerMailbox.link(id, peer, state_dir: state_dir!)
648
697
 
649
698
  SessionInbox.deliver(:peer, id, state_dir: state_dir!) do
650
699
  message = PeerMailbox.post(id, peer, text: text, summary: summary, wake: wake, re: re, state_dir: state_dir!,
@@ -661,14 +710,26 @@ module Samagotchi
661
710
  start_failed = true
662
711
  nil
663
712
  end
664
- return PeerSay.new(status: delivered.wake == :deleting ? :deleting : :gone) if delivered&.refused?
713
+ return refused_say(id, delivered.wake) if delivered&.refused?
665
714
 
666
715
  at_once = start_failed ? :start_failed : peer_at_once(id, delivered.wake)
667
716
  receipt = at_once ? nil : peer_receipt_within(id, peer, message.serial, wait, sleeper, clock)
668
717
  peer_said(id, peer, message.serial, at_once, receipt)
669
718
  end
670
719
 
671
- private
720
+ # A say the delivery refused, nothing written: a transition's hold
721
+ # (Hold.under: a continue naming its new link, else busy), a delete,
722
+ # or the session gone.
723
+ def refused_say(id, why)
724
+ case why
725
+ when :deleting then PeerSay.new(status: :deleting)
726
+ when :busy then PeerSay.new(status: :busy)
727
+ when :continued
728
+ successor = SessionChain::Successor.of(id, state_dir: state_dir!, in_flight: true)
729
+ successor ? PeerSay.new(status: :continued, next_id: successor.id, in_flight: successor.in_flight?) : PeerSay.new(status: :busy)
730
+ else PeerSay.new(status: :gone)
731
+ end
732
+ end
672
733
 
673
734
  # Whether +id+'s live worker doesn't take the peer mail (its Bridge
674
735
  # features: a read-only look). No live worker: nil (the next one does).
@@ -859,7 +859,7 @@ module Samagotchi
859
859
  raise Refused.new(message.to_s, code: error)
860
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
+ when "stop_held" then raise SessionTransition::Busy.new(sid, kind: json["kind"]&.to_sym, successor_id: json["successor_id"], taker: :stop)
863
863
  # A toolkit whose config kit's groups: is invalid there: the same.
864
864
  when "toolkit_refused" then raise Toolkit::Refused, message.to_s
865
865
  # The grants store doesn't read there: chi share says what it says here.
@@ -22,7 +22,7 @@ module Samagotchi
22
22
  case reply.status
23
23
  when 200
24
24
  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)
25
+ PeerPaired.new(status: :paired, link: link, forwarded_to: PeerSay.id_or_nil(reply.json["forwarded_to"]))
26
26
  when 409
27
27
  status = %w[continued busy old_worker].find { |known| known == reply.json["status"] } or raise_refusal(reply)
28
28
  PeerPaired.new(status: status.to_sym, next_id: reply.json["next_id"]&.to_s, in_flight: reply.json["in_flight"] == true)
@@ -30,13 +30,13 @@ module Samagotchi
30
30
  end
31
31
  end
32
32
 
33
- # @return [Boolean] whether there was a pairing
33
+ # @return [PeerUnpaired] (an older chi web names no +forwarded_to+)
34
34
  def peer_unpair(id, peer)
35
35
  PeerMailbox.check_name!(peer)
36
36
  reply = peer_request(:delete, "#{session_path(id)}/peers/#{peer}")
37
37
  raise_refusal(reply) unless reply.status == 200
38
38
 
39
- reply.json["unpaired"] == true
39
+ PeerUnpaired.new(unpaired: reply.json["unpaired"] == true, forwarded_to: PeerSay.id_or_nil(reply.json["forwarded_to"]))
40
40
  end
41
41
 
42
42
  # @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
- walk(id, forward: true) { |link| !archived?(link) }.last
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
@@ -128,12 +128,13 @@ 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' messages it hadn't read, as one note (PeerContinue): the
132
- # pairing and the mailbox stay with the previous link (its store rows
133
- # say so), the peer pairs again. No undo: a note written into the link
134
- # only, which is excluded from NoteMove's give-back and goes with its
135
- # delete; the previous link's mailbox and receipts are only read.
136
- CarryStep.new(name: :peers, store: :session_files, carry: :carry_peers, undo: {}, files: []),
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.
137
+ CarryStep.new(name: :peers, store: :peer_mailbox, carry: :carry_peers, undo: {}, files: ["link.json"]),
137
138
  # Its waiting notes last: the carry above read a pending /objectives
138
139
  # edit from them first. They keep their names, so they still sort
139
140
  # ahead of the link's own notes. A note sent to the previous link
@@ -247,7 +248,14 @@ module Samagotchi
247
248
  return :moved if follow && SessionChain.next_of(id, state_dir: state_dir)
248
249
 
249
250
  previous = Session.load(id, state_dir: state_dir)
250
- checked = check!(previous, state_dir, model: model)
251
+ # The link may be continued between the next_of look above and this check: move on, not refuse.
252
+ begin
253
+ checked = check!(previous, state_dir, model: model)
254
+ rescue Refused => e
255
+ raise unless follow && e.reason == :continued
256
+
257
+ return :moved
258
+ end
251
259
  # archive_session's refusals that can be known now, before the wait.
252
260
  SessionManager.archivable!(id, state_dir, except: checked.plan.move)
253
261
  recap_before_archive(id, state_dir, wait: recap_wait)
@@ -25,6 +25,10 @@ module Samagotchi
25
25
  STATES = %i[absent tui_owned deleting stopping skew leaving booting_stuck booting live
26
26
  stopped_starting stopped archived_starting starting archived idle].freeze
27
27
 
28
+ # The states whose session is shown "stopped" (the hub shows the stopped
29
+ # status only there; the file's own status is shown everywhere else).
30
+ STOPPED_STATES = %i[stopping stopped_starting stopped].freeze
31
+
28
32
  autoload :Reading, File.expand_path("session_lifecycle/reading", __dir__)
29
33
  autoload :CANONICAL, File.expand_path("session_lifecycle/canonical", __dir__)
30
34
  autoload :ALLOWED, File.expand_path("session_lifecycle/allowed", __dir__)
@@ -301,12 +301,6 @@ module Samagotchi
301
301
  return DeliverResult.new(status: :refused, code: 409, ack: ReviseAnchor::PARTICIPANT_UNAVAILABLE)
302
302
  end
303
303
 
304
- # compat shim review_client_id: a worker from before participant:
305
- # reads a participant's turn by review:<id> (dequeue is left out:
306
- # its client id is only echoed back).
307
- if ClientId.participant?(client_id) && !worker_has?(client, Delivery::PARTICIPANT_ID_FEATURE)
308
- client_id = ClientId.legacy_participant(client_id)
309
- end
310
304
  # compat shim agent_client_id: a worker from before agent: reads
311
305
  # an agent's line as chi send's (today's owner row).
312
306
  client_id = ClientId::CLI_SEND if ClientId.agent?(client_id) && !agent_reader
@@ -89,15 +89,19 @@ module Samagotchi
89
89
  # holds the lock: STOP_WAIT plus the recap's own wait (SessionContinue::RECAP_WAIT).
90
90
  FALLBACK_STOP_WAIT = 40
91
91
 
92
+ # How long a stop waits for its turn cancel: the Bridge's reply (its
93
+ # read timeout) and then the worker's save, each.
92
94
  STOP_CANCEL_WAIT = 3.0
93
95
  # How long a stop from the CLI (chi sessions stop, delete, a continue's
94
96
  # orphan) waits for the worker to let go of the session, here or
95
97
  # through chi web (POST …/stop's wait, capped at it).
96
98
  STOP_WAIT = 10
97
- # How long a stop waits for its session's transition lock (another
98
- # change holding it: a continue, a delete, a send's shared hold) before
99
- # it is refused (SessionTransition::Busy). An owner-side stop (a worker
100
- # stopping its delegates) tries once (0): it holds its own owner.lock.
99
+ # How long a stop waits for its session's transition lock (a short
100
+ # hold: a send's shared one, a holder that names nothing) before it is
101
+ # refused (SessionTransition::Busy). A continue naming its new link, a
102
+ # delete or an archive is refused at once (refuse_transitions). An
103
+ # owner-side stop (a worker stopping its delegates) tries once (0): it
104
+ # holds its own owner.lock.
101
105
  STOP_LOCK_WAIT = 10
102
106
  # The cancel reason of a turn a session stop cancels (Bridge::CANCEL_REASONS):
103
107
  # not the user's Stop, which only ends the turn ("user").
@@ -218,27 +222,32 @@ module Samagotchi
218
222
  # With +wait+ (seconds), also wait for the owner to let go of the session,
219
223
  # so a resume right after spawns a fresh worker instead of finding the
220
224
  # dying one.
221
- # A turn still running is canceled first (STOP_CANCEL_WAIT), as the
222
- # web's Cancel does, with reason STOP_CANCEL_REASON: its worker saves it. The TERM stops (and saves)
223
- # what still runs (WorkerSignals), with a short grace after the marker.
224
- # The mark and the TERM run under the session's transition lock, held
225
- # exclusively (kind :stop, +lock_wait+ seconds to take it; one this
225
+ # The cancel, the mark and the TERM run under the session's transition
226
+ # lock, held exclusively (kind :stop, +lock_wait+ seconds to take it,
227
+ # a continue naming its new link, a delete or an archive refused at
228
+ # once: SessionTransition.exclusive's refuse_transitions; one this
226
229
  # thread holds, a continue's or a delete's, passes through), never the
227
- # cancel's Bridge wait or the owner wait: a send, a continue or a delete
228
- # doesn't interleave with them, and a ring wake's start
229
- # (#wake_for_report) comes before the stop or after it, whole.
230
+ # owner wait: a send, a continue or a delete doesn't interleave with
231
+ # them, a refused stop leaves the running turn alone, and a ring
232
+ # wake's start (#wake_for_report) comes before the stop or after it,
233
+ # whole. A turn still running is canceled first, as the web's Cancel
234
+ # does, with reason STOP_CANCEL_REASON: its worker saves it. That is
235
+ # the one Bridge wait under the hold, bounded (STOP_CANCEL_WAIT for
236
+ # the reply, then as long for the save). The TERM stops (and saves)
237
+ # what still runs (WorkerSignals), with a short grace after the marker.
230
238
  # @param lock_wait [Numeric] 0 from a worker (it holds an owner.lock:
231
239
  # one try, never a wait)
232
240
  # @return [Boolean, nil] with +wait+: whether the owner was gone in time
233
241
  # @raise [OwnedByTUI] when the interactive TUI owns the session
234
242
  # @raise [SessionTransition::Busy] another change held the lock
235
- # past +lock_wait+ (its words name the holder); nothing was marked
243
+ # past +lock_wait+, or a transition held it (its words name the
244
+ # holder; a stop's: Busy#taker :stop); nothing was canceled or marked
236
245
  def stop_session(session_id, state_dir: nil, wait: nil, lock_wait: STOP_LOCK_WAIT)
237
246
  sd = state_dir || Session.default_state_dir
238
247
  refuse_tui!(session_id, state_dir: sd)
239
248
 
240
- cancel_running_turn(session_id, state_dir: sd)
241
249
  under_stop_lock(session_id, sd, lock_wait) do
250
+ cancel_running_turn(session_id, state_dir: sd)
242
251
  # The owner read again under the lock: a worker a ring wake started
243
252
  # while the stop waited for it is the one to TERM.
244
253
  owner = refuse_tui!(session_id, state_dir: sd)
@@ -268,7 +277,8 @@ module Samagotchi
268
277
  return yield unless Session.exist?(session_id, state_dir: state_dir)
269
278
 
270
279
  begin
271
- SessionTransition.exclusive([session_id], kind: :stop, state_dir: state_dir, deadline: lock_wait, &)
280
+ SessionTransition.exclusive([session_id], kind: :stop, state_dir: state_dir, deadline: lock_wait,
281
+ refuse_transitions: true, &)
272
282
  ensure
273
283
  SessionTransition.forget(session_id, state_dir: state_dir)
274
284
  end
@@ -286,11 +296,14 @@ module Samagotchi
286
296
 
287
297
  # Cancel the session's running turn over its Bridge and wait (up to
288
298
  # STOP_CANCEL_WAIT) for the worker to save it. Best effort: without a
289
- # live Bridge, or when it doesn't answer, the stop goes on as before.
299
+ # live Bridge, or when it doesn't answer within STOP_CANCEL_WAIT (its
300
+ # read timeout: the stop holds the transition lock), the stop goes on
301
+ # as before.
290
302
  private def cancel_running_turn(session_id, state_dir:)
291
303
  return unless Session.load(session_id, state_dir: state_dir).status == Session::STATUS_RUNNING
292
304
 
293
- client = WorkerLink.client(session_id, session_dir: Session.session_dir(session_id, state_dir: state_dir))
305
+ client = WorkerLink.client(session_id, session_dir: Session.session_dir(session_id, state_dir: state_dir),
306
+ read_timeout: STOP_CANCEL_WAIT)
294
307
  return unless client && client.cancel(reason: STOP_CANCEL_REASON).status == 202
295
308
 
296
309
  BridgeClient.poll(STOP_CANCEL_WAIT) do
@@ -39,7 +39,7 @@ module Samagotchi
39
39
  # @param include_archived [Boolean] archived sessions too
40
40
  # @return [Array<SessionRow>] see SessionRow for the fields
41
41
  def session_rows(live: false, cwd: nil, limit: nil, include_tests: true, exclude: nil, state_dir: nil,
42
- project_root: nil, include_archived: false, sort: nil, order: nil)
42
+ project_root: nil, include_archived: false, sort: nil, order: nil, exclude_delegates: false)
43
43
  sd = state_dir || Session.default_state_dir
44
44
  SessionContinue.repair_interrupted(state_dir: sd)
45
45
  root = cwd && folder_path(cwd)
@@ -51,6 +51,7 @@ module Samagotchi
51
51
  rows = Session.list(state_dir: sd, sort: sort || "updated_at", order: order || "desc", project_root: project_root,
52
52
  include_archived: include_archived).lazy
53
53
  .reject { |s| (!include_tests && s.test_run) || s.id == exclude }
54
+ .reject { |s| exclude_delegates && (s.delegate? || s.scratch) }
54
55
  .select { |s| root.nil? || in_folder?(s.working_directory, root) }
55
56
  .filter_map do |s|
56
57
  dir = Session.session_dir(s.id, state_dir: sd)
@@ -46,9 +46,10 @@ module Samagotchi
46
46
 
47
47
  # A delete's refusal for a transition lock not taken in time
48
48
  # (SessionTransition::Busy, which names the holder): :continuing when
49
- # a continue holds it, else :transition_held; Busy's words either way.
49
+ # a continue holds it, else :transition_held; Busy's words either way,
50
+ # with "try again" (Busy#retry_message: a stop's words name no delete).
50
51
  def self.held(busy)
51
- new(busy.session_id, busy.kind == :continue ? :continuing : :transition_held, message: busy.message)
52
+ new(busy.session_id, busy.kind == :continue ? :continuing : :transition_held, message: busy.retry_message)
52
53
  end
53
54
 
54
55
  # This refusal, saying the op stopped +worker_id+'s worker (this
@@ -104,11 +105,12 @@ module Samagotchi
104
105
 
105
106
  # An archive's refusal for a transition lock not taken in time (its
106
107
  # own, or a worker's stop's: SessionTransition::Busy, which names the
107
- # holder and the session held): :transition_held, Busy's words;
108
- # +stopped_workers+ the workers it stopped before.
108
+ # holder and the session held): :transition_held, Busy's words with
109
+ # "try again" (Busy#retry_message); +stopped_workers+ the workers it
110
+ # stopped before.
109
111
  def self.held(busy, stopped_workers: [])
110
112
  new(busy.session_id, :transition_held, busy_id: busy.session_id, stopped_workers: stopped_workers,
111
- message: busy.message)
113
+ message: busy.retry_message)
112
114
  end
113
115
 
114
116
  def worker_stopped = !@stopped_worker.nil?
@@ -69,9 +69,8 @@ module Samagotchi
69
69
  setup: SessionSetup.new, continues: nil, note: nil, note_source: "chi", before_spawn: nil,
70
70
  review: nil, verify_fresh: nil, scratch: false, session_id: nil, driver: :creator)
71
71
  sd = state_dir || Session.default_state_dir
72
- if session_id && (!Session.valid_id?(session_id) || Session.exist?(session_id, state_dir: sd))
73
- raise ArgumentError, "invalid or existing session id #{session_id.inspect}"
74
- end
72
+ raise ArgumentError, "invalid session id #{session_id.inspect}" if session_id && !Session.valid_id?(session_id)
73
+ raise IdTaken, "existing session id #{session_id.inspect}" if session_id && Session.exist?(session_id, state_dir: sd)
75
74
 
76
75
  # The resolved ref is stored (a resumed session keeps its model when an
77
76
  # alias is retargeted), with the name as typed beside it. A wrong host
@@ -111,7 +110,12 @@ module Samagotchi
111
110
  # carry the id.
112
111
  unless prompt.to_s.strip.empty?
113
112
  session.first_turn_id = SecureRandom.uuid
114
- session.first_turn_client_id = client_id unless ClientId.human?(client_id)
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)
115
119
  end
116
120
  # With a prompt or without (chi send --new's idle create, its line
117
121
  # sent next): its creator's first turn is the driver's too.
@@ -122,6 +122,10 @@ module Samagotchi
122
122
  end
123
123
  end
124
124
 
125
+ # spawn_session's session_id: is a session already (a retried create
126
+ # whose id its request_id names: Web::AskCreate goes on with that one).
127
+ class IdTaken < ArgumentError; end
128
+
125
129
  # A `chi scratch` session: its terminal's alone (Session::SCRATCH_REFUSED).
126
130
  # An OwnedByTUI, so every caller that refuses a terminal's session
127
131
  # (chi send, chi web's 409 owned_by_tui) refuses it the same way.
@@ -22,6 +22,8 @@ module Samagotchi
22
22
  # The liveness rule: +live+ is WorkerLink's worker_owned? (a worker holds
23
23
  # the lock, its Bridge up or on its way); +busy+ and +waiting+ need it,
24
24
  # +status+ is Session#displayed_status (a dead worker's "running" is idle).
25
+ # The session hub shows neither this live nor its question: it reads
26
+ # SessionSummary.shown (the lifecycle state).
25
27
  SessionRow = Data.define(:id, :desc, :preview, :title, :cwd, :project, :updated_at, :status, :saved_status, :live,
26
28
  :busy, :owner, :recap, :auto_title, :ctx_pct, :parent_id, :continues, :delegate, :archived,
27
29
  :scratch, :test_run, :stopped_by, :waiting, :waiting_id, :relayed_to)
@@ -2,8 +2,11 @@
2
2
 
3
3
  module Samagotchi
4
4
  # The files that hold state for a session, including stores outside its
5
- # sidecar directory. `corpus_dir` is relative to the compatibility corpus.
6
- # A `file` ending in "/" is a folder of files (PeerMailbox's in/).
5
+ # sidecar directory. `corpus_dir` is relative to the compatibility corpus;
6
+ # STATE is its root, the state dir beside `sessions/`, where the stores
7
+ # that keep every session's lines sit (SystemOne::RunsStore's
8
+ # system_one/runs/, ContextCandidatesLog's file).
9
+ # A `file` ending in "/" is a folder of files (PeerMailbox's in/, the runs).
7
10
  # A Symbol in `continue` names the carry step; a String explains why it stays.
8
11
  module SessionStores
9
12
  Store = Data.define(:file, :owner, :removal, :cross_version, :continue, :corpus_dir, :corpus_exception) do
@@ -19,11 +22,12 @@ module Samagotchi
19
22
  SIDE_CAR = "sessions/compat-session"
20
23
  CONTEXT = "context/sessions/compat-session"
21
24
  MAILBOX = "mailbox/compat-session/claude"
22
- # A continue leaves the mailbox with the previous link (the peer pairs
23
- # again with the next one, owner 2026-11-24); its :peers step only reads
24
- # it, for the one note.
25
- MAILBOX_STAYS = "the mailbox stays with the previous link (its unread messages reach the next one as one note, " \
26
- "SessionContinue :peers); a say there is refused with the next id"
25
+ STATE = "."
26
+ # A continue leaves the messages with the previous link: its :peers
27
+ # step only reads them, for the one note. The pairing (link.json) is
28
+ # carried: copied to the new link, so a say follows the chain.
29
+ MAILBOX_STAYS = "the messages stay with the previous link (its unread ones reach the next one as one note, " \
30
+ "SessionContinue :peers); a say there goes to the chain's live link"
27
31
 
28
32
  STORES = [
29
33
  Store.new(file: "archived", owner: "ArchiveStore", removal: :session_files, cross_version: true,
@@ -38,10 +42,10 @@ module Samagotchi
38
42
  continue: "a continue ends the previous link's participants", corpus_dir: SIDE_CAR),
39
43
  Store.new(file: "participant_prompts.json", owner: "ParticipantPrompts", removal: :session_files, cross_version: true,
40
44
  continue: "a continue ends the previous link's participants", corpus_dir: SIDE_CAR),
41
- Store.new(file: "judgments.jsonl", owner: "SystemOne::RunsStore", removal: :system_one_runs, cross_version: false,
42
- continue: "saved judgments belong to the previous link", corpus_dir: nil),
45
+ Store.new(file: "runs/", owner: "SystemOne::RunsStore", removal: :system_one_runs, cross_version: true,
46
+ continue: "saved judgments belong to the previous link", corpus_dir: "system_one"),
43
47
  Store.new(file: "context_candidates.jsonl", owner: "ContextCandidatesLog", removal: :context_candidates_log,
44
- cross_version: false, continue: "candidate records belong to the previous link", corpus_dir: nil),
48
+ cross_version: true, continue: "candidate records belong to the previous link", corpus_dir: STATE),
45
49
  Store.new(file: "bridge.json", owner: "WorkerSidecar", removal: :session_files, cross_version: true,
46
50
  continue: "the bridge endpoint belongs to the old worker", corpus_dir: SIDE_CAR),
47
51
  Store.new(file: "cards.json", owner: "Bridge::CardStore", removal: :session_files, cross_version: true,
@@ -68,8 +72,9 @@ module Samagotchi
68
72
  continue: "exchanges stay with the previous link; the new pairing starts empty", corpus_dir: nil,
69
73
  corpus_exception: "older readers do not read peer exchange metadata"),
70
74
  # A peer's side of the session (PeerMailbox): <state>/mailbox/<id>/<peer>/.
75
+ # The pairing is copied to the new link (SessionContinue :peers).
71
76
  Store.new(file: "link.json", owner: "PeerMailbox", removal: :peer_mailbox, cross_version: true,
72
- continue: MAILBOX_STAYS, corpus_dir: MAILBOX),
77
+ continue: :peers, corpus_dir: MAILBOX),
73
78
  Store.new(file: "in/", owner: "PeerMailbox", removal: :peer_mailbox, cross_version: true,
74
79
  continue: MAILBOX_STAYS, corpus_dir: MAILBOX),
75
80
  Store.new(file: "lock", owner: "PeerMailbox", removal: :peer_mailbox, cross_version: false,