samagotchi 0.164.0 → 0.165.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 +52 -1
- data/docs/architecture.md +7 -5
- data/docs/cli.md +20 -13
- data/docs/configuration.md +5 -1
- data/docs/guardrails.md +13 -16
- data/docs/internals/block-renderers.md +1 -1
- data/docs/internals/bridge.md +4 -3
- data/docs/internals/bundles.md +4 -2
- data/docs/internals/inline-widgets.md +8 -1
- data/docs/internals/web-frontend.md +36 -9
- data/docs/memory.md +12 -7
- data/docs/plugins.md +6 -4
- data/docs/releasing.md +6 -2
- data/docs/scheduling.md +25 -3
- data/docs/sessions.md +8 -6
- data/docs/testing.md +28 -4
- data/lib/samagotchi/block_renderers.rb +3 -6
- data/lib/samagotchi/bridge/card_store.rb +2 -1
- data/lib/samagotchi/bridge/turn_accumulator.rb +7 -8
- data/lib/samagotchi/bridge.rb +45 -3
- data/lib/samagotchi/bundle_command/installed_check.rb +2 -1
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/chi_home.rb +44 -2
- data/lib/samagotchi/client_id.rb +34 -3
- data/lib/samagotchi/compat.rb +36 -0
- data/lib/samagotchi/context_command.rb +3 -0
- data/lib/samagotchi/context_providers.rb +5 -6
- data/lib/samagotchi/context_sources.rb +2 -2
- data/lib/samagotchi/continue_intent.rb +50 -5
- data/lib/samagotchi/events.rb +4 -4
- data/lib/samagotchi/extension_load.rb +2 -1
- data/lib/samagotchi/guardrails/child_boundary.rb +7 -27
- data/lib/samagotchi/guardrails/rules.rb +11 -9
- data/lib/samagotchi/guardrails/targets.rb +3 -4
- data/lib/samagotchi/inline_widgets.rb +16 -1
- data/lib/samagotchi/memory_bundle/provenance.rb +46 -4
- data/lib/samagotchi/memory_bundle/status.rb +2 -1
- data/lib/samagotchi/memory_command.rb +3 -0
- data/lib/samagotchi/memory_orphans.rb +4 -7
- data/lib/samagotchi/memory_paths.rb +64 -7
- data/lib/samagotchi/memory_usage.rb +1 -1
- data/lib/samagotchi/plugin/sessions.rb +4 -2
- data/lib/samagotchi/project_command.rb +4 -1
- data/lib/samagotchi/project_id_clash.rb +37 -26
- data/lib/samagotchi/project_init.rb +24 -7
- data/lib/samagotchi/reply_wait/poll.rb +2 -1
- data/lib/samagotchi/schedules.rb +124 -106
- data/lib/samagotchi/send_command.rb +71 -18
- data/lib/samagotchi/session.rb +3 -2
- data/lib/samagotchi/session_access/local.rb +19 -2
- data/lib/samagotchi/session_access/remote.rb +15 -4
- data/lib/samagotchi/session_chain/links.rb +109 -0
- data/lib/samagotchi/session_chain.rb +7 -19
- data/lib/samagotchi/session_continue.rb +138 -54
- data/lib/samagotchi/session_inbox_delivery.rb +57 -38
- data/lib/samagotchi/session_manager/delivery.rb +104 -18
- data/lib/samagotchi/session_manager/removal.rb +13 -7
- data/lib/samagotchi/session_manager.rb +1 -0
- data/lib/samagotchi/session_retention.rb +20 -10
- data/lib/samagotchi/session_setup.rb +31 -1
- data/lib/samagotchi/state_format.rb +12 -8
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +6 -7
- data/lib/samagotchi/terminal_ui.rb +20 -9
- data/lib/samagotchi/tools/delegate.rb +2 -1
- data/lib/samagotchi/verify_fresh.rb +4 -1
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/public/activity.js +2 -2
- data/lib/samagotchi/web/public/app.js +13 -13
- data/lib/samagotchi/web/public/bar_fold.js +293 -0
- data/lib/samagotchi/web/public/card.js +1 -1
- data/lib/samagotchi/web/public/composer_mode.js +5 -7
- data/lib/samagotchi/web/public/index.html +132 -65
- data/lib/samagotchi/web/public/info_bar.js +28 -16
- data/lib/samagotchi/web/public/info_bar_model.js +70 -1
- data/lib/samagotchi/web/public/more_sheet.js +98 -40
- data/lib/samagotchi/web/public/stage_view.js +3 -1
- data/lib/samagotchi/web/public/stream_handlers.js +1 -1
- data/lib/samagotchi/web/public/turn_events.js +0 -122
- data/lib/samagotchi/web/public/turn_model.js +1 -1
- data/lib/samagotchi/web/public/widget_picks.js +18 -3
- data/lib/samagotchi/web/session_control.rb +15 -2
- data/lib/samagotchi/web/session_create.rb +4 -2
- data/lib/samagotchi/web/update_check.rb +27 -8
- metadata +3 -1
|
@@ -326,14 +326,18 @@ module Samagotchi
|
|
|
326
326
|
|
|
327
327
|
# One id at a time, so each line follows the order of the ids given;
|
|
328
328
|
# two that reach the same link (an old id and its newest link) send
|
|
329
|
-
# once
|
|
329
|
+
# once, also when an old id's send followed a continue (#deliver) to
|
|
330
|
+
# a link named after it: seen is where each one went.
|
|
330
331
|
seen = {}
|
|
331
332
|
results = targets(options).map do |target|
|
|
332
333
|
next unsent(target) unless target.is_a?(SessionInbox::Resolved)
|
|
333
334
|
next true if seen[target.id]
|
|
334
335
|
|
|
335
336
|
seen[target.id] = true
|
|
336
|
-
|
|
337
|
+
@sent_to = nil
|
|
338
|
+
sent = deliver(target, prompt)
|
|
339
|
+
seen[@sent_to.id] = true if sent && @sent_to
|
|
340
|
+
sent
|
|
337
341
|
end
|
|
338
342
|
results.all? ? 0 : 1
|
|
339
343
|
ensure
|
|
@@ -549,9 +553,11 @@ module Samagotchi
|
|
|
549
553
|
return SessionAccess::NewSession.new(**start, continues: options[:continues], model_name: options[:model])
|
|
550
554
|
end
|
|
551
555
|
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
556
|
+
# chi send takes no --memory, --mute or --append-system-prompt (a
|
|
557
|
+
# sender's flags, not a terminal's).
|
|
558
|
+
setup = options[:setup_words] || SessionSetup.from_request(llm_context: options[:llm_context], thinking: options[:thinking],
|
|
559
|
+
toolkit: options[:toolkit_value], settings: options[:settings],
|
|
560
|
+
parent_answers: options[:parent_answers])
|
|
555
561
|
SessionAccess::NewSession.new(**start, working_directory: options[:dir], model_name: options[:model], setup: setup)
|
|
556
562
|
end
|
|
557
563
|
|
|
@@ -596,19 +602,28 @@ module Samagotchi
|
|
|
596
602
|
# @return [Integer] the exit status
|
|
597
603
|
def run_wait(prompt, options)
|
|
598
604
|
target = wait_target(options) or return 1
|
|
599
|
-
id = target.id
|
|
600
605
|
return run_participant_wait(target, prompt, options) if @access.participant?
|
|
601
606
|
|
|
602
607
|
# Local: one read for the check and the baseline; remotely the
|
|
603
|
-
# server's wait prepare route reads both (Busy's words).
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
+
# server's wait prepare route reads both (Busy's words). Again for a
|
|
609
|
+
# link a continue moved the send to (#deliver).
|
|
610
|
+
start = nil
|
|
611
|
+
sent = deliver(target, prompt) do |at|
|
|
612
|
+
session = @access.show(at.id) unless @access.remote?
|
|
613
|
+
if (busy = @access.busy(at.id, queue: @queue, session: session))
|
|
614
|
+
@wait_target = at
|
|
615
|
+
@session_id = at.id
|
|
616
|
+
error_line("#{at.label} #{busy.message(via: @access.invocation)}")
|
|
617
|
+
next false
|
|
618
|
+
end
|
|
619
|
+
|
|
620
|
+
start = @access.prepare_wait(at.id, session: session)
|
|
621
|
+
true
|
|
608
622
|
end
|
|
623
|
+
return 1 unless sent
|
|
609
624
|
|
|
610
|
-
|
|
611
|
-
|
|
625
|
+
@wait_target = target = @sent_to
|
|
626
|
+
@session_id = id = target.id
|
|
612
627
|
return report_command(id, label: target.label) if @sent_command
|
|
613
628
|
|
|
614
629
|
target = ReplyWait::Target.message(start, @delivered, queue: @queue)
|
|
@@ -759,9 +774,17 @@ module Samagotchi
|
|
|
759
774
|
CLI::Exit::OK
|
|
760
775
|
end
|
|
761
776
|
|
|
777
|
+
# A followed send (a head #target resolved) is refused when a continue
|
|
778
|
+
# took its link since (DeliverResult#continued?): it resolves again
|
|
779
|
+
# (#follow_again) and goes to the new link, until CONTINUING_WAIT after
|
|
780
|
+
# the first refusal; the old link is never sent to.
|
|
762
781
|
# @param target [SessionInbox::Resolved] the session (#target)
|
|
763
|
-
# @
|
|
764
|
-
|
|
782
|
+
# @yieldparam at [SessionInbox::Resolved] each link about to be sent to
|
|
783
|
+
# @yieldreturn [Boolean] false: nothing is sent (its line printed)
|
|
784
|
+
# @return [Boolean] whether the message was queued (@sent_to: where)
|
|
785
|
+
def deliver(target, prompt, deadline: nil, &before)
|
|
786
|
+
return false if block_given? && !yield(target)
|
|
787
|
+
|
|
765
788
|
id = target.id
|
|
766
789
|
short = target.label
|
|
767
790
|
session = @access.show(id)
|
|
@@ -783,12 +806,22 @@ module Samagotchi
|
|
|
783
806
|
delivery = if @queue then Delivery::QUEUE
|
|
784
807
|
elsif @cut && running then Delivery::CUT
|
|
785
808
|
end
|
|
786
|
-
result = @access.deliver_turn(id, prompt: prompt, client_id: client_id_for(prompt), images: refs, delivery: delivery
|
|
809
|
+
result = @access.deliver_turn(id, prompt: prompt, client_id: client_id_for(prompt), images: refs, delivery: delivery,
|
|
810
|
+
follow: target.via != :pinned)
|
|
811
|
+
if result.continued?
|
|
812
|
+
# Copied in before the refusal: not the old link's to keep.
|
|
813
|
+
@access.discard_images(id, refs)
|
|
814
|
+
deadline ||= monotonic + self.class::CONTINUING_WAIT
|
|
815
|
+
again = follow_again(target, deadline)
|
|
816
|
+
return unsent(again) if again.is_a?(Unsent)
|
|
817
|
+
return deliver(again, prompt, deadline: deadline, &before) if again
|
|
818
|
+
end
|
|
787
819
|
unless result.accepted?
|
|
788
820
|
@info.puts("#{short} #{result.refusal_text}")
|
|
789
821
|
return false
|
|
790
822
|
end
|
|
791
823
|
|
|
824
|
+
@sent_to = target
|
|
792
825
|
@sent_command = result.command?
|
|
793
826
|
if @sent_command
|
|
794
827
|
@info.puts("#{short} #{SENT_COMMAND}")
|
|
@@ -937,9 +970,9 @@ module Samagotchi
|
|
|
937
970
|
def target(given)
|
|
938
971
|
id = @access.resolve(given)
|
|
939
972
|
head = @access.head(id)
|
|
940
|
-
deadline =
|
|
973
|
+
deadline = monotonic + self.class::CONTINUING_WAIT
|
|
941
974
|
waited = head.continuing?
|
|
942
|
-
while head.continuing? &&
|
|
975
|
+
while head.continuing? && monotonic < deadline
|
|
943
976
|
self.class.pause(self.class::CONTINUING_POLL)
|
|
944
977
|
head = @access.head(id)
|
|
945
978
|
end
|
|
@@ -951,6 +984,26 @@ module Samagotchi
|
|
|
951
984
|
Unsent.new(line: "#{command_name}: #{e.message}", error: true)
|
|
952
985
|
end
|
|
953
986
|
|
|
987
|
+
# +refused+'s id resolved again after a continue took its link
|
|
988
|
+
# (#deliver): #target, pausing between looks while it still names that
|
|
989
|
+
# link, until +deadline+. The new link's worker is the continue's spawn
|
|
990
|
+
# to start (#delivery_note).
|
|
991
|
+
# @return [SessionInbox::Resolved, Unsent, nil] nil: out of time
|
|
992
|
+
def follow_again(refused, deadline)
|
|
993
|
+
while monotonic < deadline
|
|
994
|
+
self.class.pause(self.class::CONTINUING_POLL)
|
|
995
|
+
again = target(refused.asked)
|
|
996
|
+
return again unless again.is_a?(SessionInbox::Resolved)
|
|
997
|
+
next if again.id == refused.id
|
|
998
|
+
|
|
999
|
+
(@continued_here ||= {})[again.id] = true
|
|
1000
|
+
return again
|
|
1001
|
+
end
|
|
1002
|
+
nil
|
|
1003
|
+
end
|
|
1004
|
+
|
|
1005
|
+
def monotonic = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
1006
|
+
|
|
954
1007
|
# Prints +unsent+'s line. @return [false]
|
|
955
1008
|
def unsent(unsent)
|
|
956
1009
|
unsent.error ? error_line(unsent.line) : @info.puts(unsent.line)
|
data/lib/samagotchi/session.rb
CHANGED
|
@@ -873,8 +873,9 @@ module Samagotchi
|
|
|
873
873
|
|
|
874
874
|
# Mark a session as stopped (STOPPED_FILE); its session file stays as
|
|
875
875
|
# it is.
|
|
876
|
+
# @param now [Time] the marker's at (a spec's, the corpus's clock)
|
|
876
877
|
# @raise [NotFound] when there is no such session
|
|
877
|
-
def self.mark_stopped(session_id, state_dir: default_state_dir)
|
|
878
|
+
def self.mark_stopped(session_id, state_dir: default_state_dir, now: Time.now)
|
|
878
879
|
raise NotFound, session_id unless exist?(session_id, state_dir: state_dir)
|
|
879
880
|
|
|
880
881
|
dir = session_dir(session_id, state_dir: state_dir)
|
|
@@ -882,7 +883,7 @@ module Samagotchi
|
|
|
882
883
|
# Its own at, on the clock the starting marker's at is written with
|
|
883
884
|
# (StartingMarker; resume_session compares them): a file's mtime may be
|
|
884
885
|
# coarse or skewed. Older chi readers ask only whether it exists.
|
|
885
|
-
AtomicFile.write(File.join(dir, STOPPED_FILE), JSON.generate({ "at" =>
|
|
886
|
+
AtomicFile.write(File.join(dir, STOPPED_FILE), JSON.generate({ "at" => now.iso8601(6) }))
|
|
886
887
|
end
|
|
887
888
|
|
|
888
889
|
# When the stop marker was written: its own at, or (an older chi's empty
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "fileutils"
|
|
3
4
|
require "socket"
|
|
4
5
|
|
|
5
6
|
require_relative "../session"
|
|
@@ -181,15 +182,31 @@ module Samagotchi
|
|
|
181
182
|
def upload_images(id, refs, from:)
|
|
182
183
|
to = session_dir(id)
|
|
183
184
|
refs.map do |ref|
|
|
185
|
+
target = File.join(to, ref[:file].to_s)
|
|
186
|
+
(@uploaded ||= Set.new) << target unless File.exist?(target)
|
|
184
187
|
ImageStore.copy_file(ref, from: from, to: to)
|
|
185
188
|
{ file: ref[:file], name: ref[:name] }
|
|
186
189
|
end
|
|
187
190
|
end
|
|
188
191
|
|
|
192
|
+
# Takes back the copies #upload_images made of +refs+ in the session
|
|
193
|
+
# (a send its delivery refused: chi send's link_continued): only the
|
|
194
|
+
# files it wrote, never one there before (a name is its content's
|
|
195
|
+
# hash, so an earlier turn's image may share it).
|
|
196
|
+
def discard_images(id, refs)
|
|
197
|
+
dir = session_dir(id)
|
|
198
|
+
refs.each do |ref|
|
|
199
|
+
target = File.join(dir, ref[:file].to_s)
|
|
200
|
+
FileUtils.rm_f(target) if @uploaded&.delete?(target)
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
189
204
|
# @return [SessionManager::DeliverResult]
|
|
190
205
|
# @raise [SessionManager::OwnedByTUI] a one-turn chi run owns it
|
|
191
|
-
|
|
192
|
-
|
|
206
|
+
# @param follow [Boolean] +id+ is the head #head resolved (SessionManager.deliver_turn's follow:)
|
|
207
|
+
def deliver_turn(id, prompt:, client_id:, images:, delivery: nil, follow: false)
|
|
208
|
+
SessionManager.deliver_turn(id, prompt: prompt, client_id: client_id, images: images, delivery: delivery,
|
|
209
|
+
follow: follow, **dir)
|
|
193
210
|
end
|
|
194
211
|
|
|
195
212
|
# @return [NoteDelivery::Result]
|
|
@@ -241,10 +241,18 @@ module Samagotchi
|
|
|
241
241
|
end
|
|
242
242
|
end
|
|
243
243
|
|
|
244
|
+
# Local#discard_images' counterpart: nothing. chi web has no route
|
|
245
|
+
# to take an upload back, so a refused send's copies stay there
|
|
246
|
+
# unused, as a web composer's removed upload does.
|
|
247
|
+
def discard_images(_id, _refs) = nil
|
|
248
|
+
|
|
244
249
|
# @return [SessionManager::DeliverResult]
|
|
245
250
|
# @raise [SessionManager::OwnedByTUI]
|
|
246
|
-
|
|
251
|
+
# @param follow [Boolean] +id+ is the head #head resolved: sent only
|
|
252
|
+
# when true (an older chi web ignores it and delivers as before)
|
|
253
|
+
def deliver_turn(id, prompt:, client_id:, images:, delivery: nil, follow: false)
|
|
247
254
|
body = { prompt: prompt, client_id: client_id, history: false }
|
|
255
|
+
body[:follow] = true if follow
|
|
248
256
|
body[:images] = images.map { |ref| { file: ref[:file], name: ref[:name] } } unless images.empty?
|
|
249
257
|
body[:check_vision] = true unless images.empty?
|
|
250
258
|
body[:delivery] = delivery if delivery
|
|
@@ -726,20 +734,23 @@ module Samagotchi
|
|
|
726
734
|
return false unless reply.status == 404
|
|
727
735
|
return true if reply.error == "context_missing"
|
|
728
736
|
|
|
729
|
-
# Older chi web used not_found for a missing context source
|
|
737
|
+
# Older chi web used not_found for a missing context source
|
|
738
|
+
# (compat shim remote_refusal_details: Compat::SHIMS).
|
|
730
739
|
reply.error == "not_found" && reply.json["detail"].to_s.start_with?("no source")
|
|
731
740
|
end
|
|
732
741
|
|
|
733
742
|
def no_provider?(reply)
|
|
734
743
|
return true if reply.error == "no_provider"
|
|
735
744
|
|
|
736
|
-
# Older chi web didn't give this refusal its own error code
|
|
745
|
+
# Older chi web didn't give this refusal its own error code
|
|
746
|
+
# (compat shim remote_refusal_details).
|
|
737
747
|
reply.json["detail"].to_s.start_with?("no installed bundle resolves")
|
|
738
748
|
end
|
|
739
749
|
|
|
740
750
|
# chi web's 404 not_found detail for a session it doesn't have:
|
|
741
751
|
# "no session X" (Session::NotFound), or "Session not found: X" from
|
|
742
|
-
# chi web 0.155.0 and older (compat N-1: the same line either way
|
|
752
|
+
# chi web 0.155.0 and older (compat N-1: the same line either way;
|
|
753
|
+
# compat shim session_not_found_words: Compat::SHIMS).
|
|
743
754
|
SESSION_NOT_FOUND = /\A(?:no session |Session not found: )(.+)\z/m
|
|
744
755
|
|
|
745
756
|
# Session::NotFound for a missing session's 404, else an
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../archive_store"
|
|
4
|
+
require_relative "../session"
|
|
5
|
+
|
|
6
|
+
module Samagotchi
|
|
7
|
+
module SessionChain
|
|
8
|
+
# The one walker of "who continues whom": every chain walk (SessionChain.latest
|
|
9
|
+
# and .next_of, SessionInbox::Head, Schedules' tick) goes through one of
|
|
10
|
+
# these. Built over one Session.list (archived ones too, created_at asc),
|
|
11
|
+
# made on the first question that needs it, so asking only #archived? of
|
|
12
|
+
# one id lists nothing. A link's successor is the earliest session that
|
|
13
|
+
# continues it (a fork a hand edit or an old race made keeps the first);
|
|
14
|
+
# a walk that comes back to a link it passed (a loop a hand-edited file
|
|
15
|
+
# could make) ends where it would repeat. A walk stops where no listed
|
|
16
|
+
# session continues the link: past a deleted middle link nothing is
|
|
17
|
+
# reached.
|
|
18
|
+
#
|
|
19
|
+
# The walks, and who uses which (not unified on purpose: a schedule
|
|
20
|
+
# firing must continue the chain's end, SessionContinue refuses a link
|
|
21
|
+
# already continued; input from a person goes to the first link still
|
|
22
|
+
# live, and a send un-archives an archived end, a schedule never does):
|
|
23
|
+
# - #latest: the chain's end (SessionChain.latest, `last:<id>`, Schedules)
|
|
24
|
+
# - #first_live: the first link not archived, or the end (Head: send, note)
|
|
25
|
+
# - #root: back to the first link (Schedules' one firing per chain)
|
|
26
|
+
#
|
|
27
|
+
# The listing is a snapshot: one Links per question, or per tick.
|
|
28
|
+
class Links
|
|
29
|
+
attr_reader :state_dir
|
|
30
|
+
|
|
31
|
+
def initialize(state_dir:)
|
|
32
|
+
@state_dir = state_dir
|
|
33
|
+
@by_id = nil
|
|
34
|
+
@nexts = nil
|
|
35
|
+
@archived = {}
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The listed session +id+ (a Session summary), or nil.
|
|
39
|
+
def session(id) = by_id[id]
|
|
40
|
+
|
|
41
|
+
# Not in the listing (pruned, deleted, never existed).
|
|
42
|
+
def gone?(id) = by_id[id].nil?
|
|
43
|
+
|
|
44
|
+
# Whether +id+ is archived: from the listing once it is made (it read
|
|
45
|
+
# the flags), else one ArchiveStore check, kept.
|
|
46
|
+
def archived?(id)
|
|
47
|
+
return @archived[id] if @archived.key?(id)
|
|
48
|
+
return @archived[id] = by_id[id]&.archived || false if @by_id
|
|
49
|
+
|
|
50
|
+
@archived[id] = Session.valid_id?(id) && ArchiveStore.archived?(Session.session_dir(id, state_dir: @state_dir))
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# The session +id+ continues, or nil (a first link, or not listed).
|
|
54
|
+
def continues(id) = by_id[id]&.continues
|
|
55
|
+
|
|
56
|
+
# The session that continues +id+, or nil. Archived ones count: an
|
|
57
|
+
# archived link is still a link.
|
|
58
|
+
def next_of(id) = nexts[id]
|
|
59
|
+
|
|
60
|
+
# The chain's end from +id+ on: +id+ itself when nobody continues it.
|
|
61
|
+
def latest(id) = walk(id).last
|
|
62
|
+
|
|
63
|
+
# From +id+ forward over archived links: +id+ when it is not archived,
|
|
64
|
+
# else the first link after it that is not, or the chain's end. Only an
|
|
65
|
+
# archived +id+ makes the listing.
|
|
66
|
+
def first_live(id)
|
|
67
|
+
return id unless archived?(id)
|
|
68
|
+
|
|
69
|
+
walk(id) { |link| !archived?(link) }.last
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# The chain's first link: back through `continues` to a link with no
|
|
73
|
+
# predecessor listed. A predecessor that is gone ends the walk on its
|
|
74
|
+
# id, so every link after it names the same root.
|
|
75
|
+
def root(id)
|
|
76
|
+
seen = [id]
|
|
77
|
+
while (previous = continues(seen.last)) && !seen.include?(previous)
|
|
78
|
+
seen << previous
|
|
79
|
+
end
|
|
80
|
+
seen.last
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
# +id+ and the links after it, in order, until none follows, a link
|
|
86
|
+
# would repeat, or the block says stop at the one just reached.
|
|
87
|
+
def walk(id)
|
|
88
|
+
seen = [id]
|
|
89
|
+
while (following = nexts[seen.last]) && !seen.include?(following)
|
|
90
|
+
seen << following
|
|
91
|
+
break if block_given? && yield(following)
|
|
92
|
+
end
|
|
93
|
+
seen
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def by_id
|
|
97
|
+
@by_id ||= Session.list(state_dir: @state_dir, sort: "created_at", order: "asc", include_archived: true)
|
|
98
|
+
.to_h { |session| [session.id, session] }
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# id → the session that continues it: the earliest created.
|
|
102
|
+
def nexts
|
|
103
|
+
@nexts ||= by_id.each_value.with_object({}) do |session, nexts|
|
|
104
|
+
nexts[session.continues] ||= session.id if session.continues
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
@@ -7,6 +7,7 @@ require_relative "config"
|
|
|
7
7
|
require_relative "log"
|
|
8
8
|
require_relative "session"
|
|
9
9
|
require_relative "session_inbox"
|
|
10
|
+
require_relative "session_chain/links"
|
|
10
11
|
require_relative "steer"
|
|
11
12
|
require_relative "recap_store"
|
|
12
13
|
|
|
@@ -16,7 +17,8 @@ module Samagotchi
|
|
|
16
17
|
# follow +continues+ back for the earlier links, forward (the session whose
|
|
17
18
|
# +continues+ names this one) for the later ones. A link is continued at
|
|
18
19
|
# most once (SessionContinue.run refuses a second), so the
|
|
19
|
-
# chain's latest link is the one nobody continues.
|
|
20
|
+
# chain's latest link is the one nobody continues. Every walk is
|
|
21
|
+
# SessionChain::Links'.
|
|
20
22
|
module SessionChain
|
|
21
23
|
# `--continues last:<id>`: the latest link of the chain <id> is in.
|
|
22
24
|
LAST_PREFIX = "last:"
|
|
@@ -36,28 +38,14 @@ module Samagotchi
|
|
|
36
38
|
module_function
|
|
37
39
|
|
|
38
40
|
# The session that continues +id+, else nil. Archived ones count: an
|
|
39
|
-
# archived link is still a link.
|
|
41
|
+
# archived link is still a link (Links#next_of).
|
|
40
42
|
# @return [String, nil]
|
|
41
|
-
def next_of(id, state_dir:)
|
|
42
|
-
Session.list(state_dir: state_dir, sort: "created_at", order: "asc", include_archived: true)
|
|
43
|
-
.find { |s| s.continues == id }&.id
|
|
44
|
-
end
|
|
43
|
+
def next_of(id, state_dir:) = Links.new(state_dir: state_dir).next_of(id)
|
|
45
44
|
|
|
46
45
|
# The chain's latest link from +id+ on: +id+ itself when nobody
|
|
47
|
-
# continues it
|
|
48
|
-
# could make ends where it started).
|
|
46
|
+
# continues it (Links#latest: one listing, walked forward).
|
|
49
47
|
# @return [String]
|
|
50
|
-
def latest(id, state_dir:)
|
|
51
|
-
nexts = {}
|
|
52
|
-
Session.list(state_dir: state_dir, sort: "created_at", order: "asc", include_archived: true).each do |s|
|
|
53
|
-
nexts[s.continues] ||= s.id if s.continues
|
|
54
|
-
end
|
|
55
|
-
seen = [id]
|
|
56
|
-
while (following = nexts[seen.last]) && !seen.include?(following)
|
|
57
|
-
seen << following
|
|
58
|
-
end
|
|
59
|
-
seen.last
|
|
60
|
-
end
|
|
48
|
+
def latest(id, state_dir:) = Links.new(state_dir: state_dir).latest(id)
|
|
61
49
|
|
|
62
50
|
# A session to continue as the CLI and the web name it: an id, a unique
|
|
63
51
|
# prefix, or `last:<id or prefix>` for that chain's latest link.
|