samagotchi 0.164.0 → 0.166.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 (100) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +88 -1
  3. data/docs/architecture.md +7 -5
  4. data/docs/cli.md +23 -12
  5. data/docs/configuration.md +8 -2
  6. data/docs/guardrails.md +19 -18
  7. data/docs/internals/block-renderers.md +1 -1
  8. data/docs/internals/bridge.md +10 -3
  9. data/docs/internals/bundles.md +4 -2
  10. data/docs/internals/inline-widgets.md +8 -1
  11. data/docs/internals/remote-sessions.md +1 -1
  12. data/docs/internals/web-frontend.md +73 -13
  13. data/docs/memory.md +12 -7
  14. data/docs/plugins.md +6 -4
  15. data/docs/releasing.md +17 -4
  16. data/docs/scheduling.md +25 -3
  17. data/docs/sessions.md +12 -10
  18. data/docs/testing.md +37 -4
  19. data/lib/samagotchi/block_renderers.rb +3 -6
  20. data/lib/samagotchi/bridge/card_store.rb +2 -1
  21. data/lib/samagotchi/bridge/turn_accumulator.rb +7 -8
  22. data/lib/samagotchi/bridge.rb +45 -3
  23. data/lib/samagotchi/bundle_command/installed_check.rb +2 -1
  24. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  25. data/lib/samagotchi/chi_home.rb +44 -2
  26. data/lib/samagotchi/client_id.rb +34 -3
  27. data/lib/samagotchi/compat.rb +34 -0
  28. data/lib/samagotchi/context_command.rb +3 -0
  29. data/lib/samagotchi/context_providers.rb +5 -6
  30. data/lib/samagotchi/context_sources.rb +2 -2
  31. data/lib/samagotchi/continue_intent.rb +50 -5
  32. data/lib/samagotchi/events.rb +4 -4
  33. data/lib/samagotchi/extension_load.rb +2 -1
  34. data/lib/samagotchi/guardrails/approvals.rb +36 -9
  35. data/lib/samagotchi/guardrails/child_boundary.rb +7 -27
  36. data/lib/samagotchi/guardrails/rules.rb +11 -9
  37. data/lib/samagotchi/guardrails/targets.rb +3 -4
  38. data/lib/samagotchi/inline_widgets.rb +16 -1
  39. data/lib/samagotchi/memory_bundle/provenance.rb +46 -4
  40. data/lib/samagotchi/memory_bundle/status.rb +2 -1
  41. data/lib/samagotchi/memory_command.rb +3 -0
  42. data/lib/samagotchi/memory_orphans.rb +4 -7
  43. data/lib/samagotchi/memory_paths.rb +64 -7
  44. data/lib/samagotchi/memory_usage.rb +1 -1
  45. data/lib/samagotchi/plugin/sessions.rb +8 -3
  46. data/lib/samagotchi/project_command.rb +4 -1
  47. data/lib/samagotchi/project_id_clash.rb +37 -26
  48. data/lib/samagotchi/project_init.rb +24 -7
  49. data/lib/samagotchi/reply_wait/poll.rb +4 -14
  50. data/lib/samagotchi/reply_wait.rb +0 -23
  51. data/lib/samagotchi/run_options.rb +1 -1
  52. data/lib/samagotchi/schedules.rb +124 -106
  53. data/lib/samagotchi/send_command.rb +71 -18
  54. data/lib/samagotchi/session.rb +3 -2
  55. data/lib/samagotchi/session_access/local.rb +19 -2
  56. data/lib/samagotchi/session_access/remote.rb +13 -17
  57. data/lib/samagotchi/session_chain/links.rb +109 -0
  58. data/lib/samagotchi/session_chain.rb +7 -19
  59. data/lib/samagotchi/session_continue.rb +138 -54
  60. data/lib/samagotchi/session_inbox.rb +1 -1
  61. data/lib/samagotchi/session_inbox_delivery.rb +57 -38
  62. data/lib/samagotchi/session_manager/delivery.rb +104 -18
  63. data/lib/samagotchi/session_manager/removal.rb +13 -7
  64. data/lib/samagotchi/session_manager.rb +1 -0
  65. data/lib/samagotchi/session_retention.rb +20 -10
  66. data/lib/samagotchi/session_setup.rb +31 -1
  67. data/lib/samagotchi/state_format.rb +12 -8
  68. data/lib/samagotchi/terminal_ui/attach_launcher.rb +6 -7
  69. data/lib/samagotchi/terminal_ui.rb +20 -9
  70. data/lib/samagotchi/tools/delegate.rb +2 -3
  71. data/lib/samagotchi/verify_fresh.rb +4 -1
  72. data/lib/samagotchi/version.rb +1 -1
  73. data/lib/samagotchi/web/public/activity.js +2 -2
  74. data/lib/samagotchi/web/public/app.js +28 -21
  75. data/lib/samagotchi/web/public/auth_lost.js +3 -1
  76. data/lib/samagotchi/web/public/bar_fold.js +293 -0
  77. data/lib/samagotchi/web/public/card.js +1 -1
  78. data/lib/samagotchi/web/public/chip_popover.js +5 -1
  79. data/lib/samagotchi/web/public/composer_mode.js +5 -7
  80. data/lib/samagotchi/web/public/delivery_line.js +19 -4
  81. data/lib/samagotchi/web/public/index.html +242 -94
  82. data/lib/samagotchi/web/public/info_bar.js +28 -16
  83. data/lib/samagotchi/web/public/info_bar_model.js +76 -2
  84. data/lib/samagotchi/web/public/live_timing.js +3 -0
  85. data/lib/samagotchi/web/public/more_sheet.js +98 -40
  86. data/lib/samagotchi/web/public/resync_reach.js +59 -0
  87. data/lib/samagotchi/web/public/session_fields.js +1 -0
  88. data/lib/samagotchi/web/public/session_state.js +5 -0
  89. data/lib/samagotchi/web/public/stage_frame.js +4 -2
  90. data/lib/samagotchi/web/public/stage_stall.js +134 -0
  91. data/lib/samagotchi/web/public/stage_view.js +78 -7
  92. data/lib/samagotchi/web/public/stream_handlers.js +1 -1
  93. data/lib/samagotchi/web/public/turn_events.js +0 -122
  94. data/lib/samagotchi/web/public/turn_model.js +1 -1
  95. data/lib/samagotchi/web/public/widget_picks.js +18 -3
  96. data/lib/samagotchi/web/session_control.rb +15 -2
  97. data/lib/samagotchi/web/session_create.rb +4 -2
  98. data/lib/samagotchi/web/token.rb +2 -2
  99. data/lib/samagotchi/web/update_check.rb +27 -8
  100. metadata +5 -1
@@ -85,7 +85,8 @@ module Samagotchi
85
85
 
86
86
  # The class-file hooks the record still has (a 0.157 install's; chi
87
87
  # loads none, Manifest::HooksRetired), each "present" or "missing" in
88
- # hooks/, by name; {} for none.
88
+ # hooks/, by name; {} for none (compat shim retired_class_file_hooks:
89
+ # Compat::SHIMS).
89
90
  # @return [Hash{String => String}]
90
91
  def self.retired_hooks(provenance, bundle)
91
92
  bundle.hooks.keys.sort.to_h do |file|
@@ -96,6 +96,9 @@ module Samagotchi
96
96
  return usage_error("unknown scope #{scope.inspect}: use --scope project|system") if scope && !SCOPES.include?(scope)
97
97
 
98
98
  send(:"run_#{@sub}", parsed.options, parsed.args)
99
+ rescue MemoryPaths::ProjectIdConflict => e
100
+ error_line("#{command_name}: #{e.message}")
101
+ CLI::Exit::FAILED
99
102
  end
100
103
 
101
104
  # The memories of +scope+ (project first, then system, when nil), by
@@ -24,10 +24,6 @@ module Samagotchi
24
24
  # @!attribute computed_exists [Boolean] whether +computed+ is a folder (empty of memories)
25
25
  Found = Data.define(:computed, :candidates, :computed_exists)
26
26
 
27
- # A project keyed by its path is <name>_<8 hex>; a marked project's folder is
28
- # its id, <name>-<8 hex>.
29
- PATH_HASH = /_\h{8}\z/
30
-
31
27
  module_function
32
28
 
33
29
  # A candidate folder's key, in words: an earlier path's hash, or a chi project
@@ -37,8 +33,9 @@ module Samagotchi
37
33
  hash_keyed?(candidate) ? "another path's hash" : "project id #{File.basename(candidate)}"
38
34
  end
39
35
 
40
- # Internal: is this candidate keyed by its path rather than by a project id?
41
- def hash_keyed?(candidate) = File.basename(candidate).match?(PATH_HASH)
36
+ # Internal: is this candidate keyed by its path (<name>_<8 hex>) rather
37
+ # than by a project id (<name>-<8 hex>)?
38
+ def hash_keyed?(candidate) = MemoryPaths.path_key_shape?(File.basename(candidate))
42
39
  private_class_method :hash_keyed?
43
40
 
44
41
  # The orphan candidates for +cwd+'s project, or nil: only inside a
@@ -66,7 +63,7 @@ module Samagotchi
66
63
  return nil if candidates.empty?
67
64
 
68
65
  Found.new(computed: computed, candidates: candidates, computed_exists: File.directory?(computed))
69
- rescue SystemCallError
66
+ rescue MemoryPaths::ProjectIdConflict, SystemCallError
70
67
  nil
71
68
  end
72
69
 
@@ -19,6 +19,17 @@ module Samagotchi
19
19
  # The file that makes a folder in no git repository a chi project
20
20
  # (`chi project init`). The file, not the bare .chi/ dir, is the marker.
21
21
  MARKER = File.join(".chi", "project.yml").freeze
22
+ CLAIM = ".root"
23
+
24
+ class ProjectIdConflict < ArgumentError
25
+ attr_reader :folder
26
+
27
+ def initialize(folder)
28
+ @folder = folder
29
+ super("project memory folder #{folder} is already in use; run `chi project init --new-id` " \
30
+ "to give this project its own id")
31
+ end
32
+ end
22
33
 
23
34
  # How a folder's project was found (#lookup): +kind+ is :git, :marker or
24
35
  # :none; +root+ is what memories are keyed on (#project_root: git's
@@ -80,20 +91,59 @@ module Samagotchi
80
91
 
81
92
  # One folder per project: a marked project's id (ProjectId), else its
82
93
  # root's path key (#key_for_root).
83
- def project_key(cwd = Dir.pwd)
84
- found = lookup(cwd)
85
- key_for(root: found.root, id: found.id)
94
+ def project_key(cwd = Dir.pwd, env: ENV, home: default_home)
95
+ found = lookup(cwd, home: home)
96
+ key_for(root: found.root, id: found.id, env: env, home: home)
86
97
  end
87
98
 
88
99
  # The key of a project already resolved: +id+ (a ProjectLookup's, a
89
100
  # session's project_id) when there is one, else +root+'s path key. Every
90
101
  # caller holding a root keys here, with the id that came with it.
91
- def key_for(root:, id:) = id || key_for_root(root)
102
+ def key_for(root:, id:, env: ENV, home: default_home)
103
+ return key_for_root(root) unless id
104
+
105
+ conflict = project_id_conflict(root: root, id: id, env: env, home: home)
106
+ raise conflict if conflict
107
+
108
+ id
109
+ end
110
+
111
+ # The shape of a #key_for_root name (`<basename>_<8 lowercase hex>`);
112
+ # generated ids (ProjectId.generate) are `<name>-<8 hex>`.
113
+ PATH_KEY_SHAPE = /_[0-9a-f]{8}\z/
114
+ private_constant :PATH_KEY_SHAPE
115
+
116
+ # An existing id folder is safe only when it is claimed by this root, or
117
+ # the caller or its previous claimant no longer has this id. An unclaimed
118
+ # folder is taken only when its name has the path-key shape (another
119
+ # project's path-keyed folder); one of any other name is a legacy id
120
+ # folder (an older `chi project init` or chi wrote no claim):
121
+ # ProjectIdClash.check claims it at session start.
122
+ def project_id_conflict(root:, id:, env: ENV, home: default_home)
123
+ folder = File.join(projects_dir(env: env), id)
124
+ return nil unless File.exist?(folder) || File.symlink?(folder)
125
+ return ProjectIdConflict.new(folder) unless File.directory?(folder)
126
+
127
+ claim_path = File.join(folder, CLAIM)
128
+ other = File.file?(claim_path) ? File.read(claim_path, 4096).to_s.lines.first.to_s.strip : ""
129
+ if other.empty?
130
+ return path_key_shape?(id) ? ProjectIdConflict.new(folder) : nil
131
+ end
132
+ return nil if other == root || !same_project?(root, id, home)
133
+ return ProjectIdConflict.new(folder) if same_project?(other, id, home)
134
+
135
+ nil
136
+ rescue SystemCallError
137
+ ProjectIdConflict.new(folder)
138
+ end
92
139
 
93
140
  # The path key: basename + 8 hex chars of MD5(full root path). Pure: it
94
- # reads no file (#key_for adds the id).
141
+ # reads no file.
95
142
  def key_for_root(root) = "#{File.basename(root)}_#{Digest::MD5.hexdigest(root)[0..7]}"
96
143
 
144
+ # Whether +key+ looks like a #key_for_root name.
145
+ def path_key_shape?(key) = PATH_KEY_SHAPE.match?(key)
146
+
97
147
  # The id in the marker at +root+, or nil (none, or not a valid one: one
98
148
  # debug line says why).
99
149
  def marker_id(root)
@@ -227,10 +277,17 @@ module Samagotchi
227
277
  rescue SystemCallError
228
278
  gitdir
229
279
  end
230
- private_class_method :find_dot_git, :git_root, :gitdir_from_file, :common_dir
280
+
281
+ def same_project?(other, id, home)
282
+ return false if other.empty? || !File.directory?(other)
283
+
284
+ found = lookup(other, home: home)
285
+ found.marker? && found.root == File.realpath(other) && found.id == id
286
+ end
287
+ private_class_method :find_dot_git, :git_root, :gitdir_from_file, :common_dir, :same_project?
231
288
 
232
289
  def project_dir(env: ENV, cwd: Dir.pwd)
233
- File.join(projects_dir(env: env), project_key(cwd))
290
+ File.join(projects_dir(env: env), project_key(cwd, env: env))
234
291
  end
235
292
 
236
293
  # The folder of a memory scope: "system" (or blank) or "project"; nil for
@@ -351,7 +351,7 @@ module Samagotchi
351
351
 
352
352
  def project_dir_of(session)
353
353
  root, id = session_project(session)
354
- root ? File.join(MemoryPaths.projects_dir(env: @env), MemoryPaths.key_for(root: root, id: id)) : nil
354
+ root ? File.join(MemoryPaths.projects_dir(env: @env), MemoryPaths.key_for(root: root, id: id, env: @env)) : nil
355
355
  end
356
356
 
357
357
  # A session of this folder's project (worktrees of one repository share
@@ -112,7 +112,8 @@ module Samagotchi
112
112
  end
113
113
 
114
114
  # Send +text+ to session +id+ as a user message (it runs as a turn;
115
- # a stopped session is woken). Waits up to 5 s for its worker, so call
115
+ # an idle session is woken, but one its owner stopped or archived is
116
+ # not: refused). Waits up to 5 s for its worker, so call
116
117
  # it from an anytime command or your own thread, never a tool or hook
117
118
  # of a running turn. A running turn takes it at its next step; with
118
119
  # +delivery: :queue+ it runs as a turn of its own after that one
@@ -121,7 +122,8 @@ module Samagotchi
121
122
  # :queue
122
123
  # @return [String] the id of the session it went to
123
124
  # @raise [Error] no such session, a one-shot owns it, it wasn't taken
124
- # (a worker older than queueing refuses a queue), or delivery is
125
+ # (a worker older than queueing refuses a queue; a stopped or
126
+ # archived session: session_stopped / session_archived), or delivery is
125
127
  # :cut or unknown
126
128
  def send(id, text, delivery: nil)
127
129
  raise Error, "nothing to send" if text.to_s.strip.empty?
@@ -236,8 +238,11 @@ module Samagotchi
236
238
  "#{Tools::Delegate::MAX_CHILDREN_KEY}): #{running.map(&:short_id).join(", ")}"
237
239
  end
238
240
 
241
+ # The worker's turn snapshot (GET snapshot's "snapshot": messages,
242
+ # current_turn), nil with no worker or no reply.
239
243
  def live_snapshot(sid, state_dir)
240
- WorkerLink.client(sid, session_dir: Session.session_dir(sid, state_dir: state_dir))&.get_json("snapshot")
244
+ live = WorkerLink.client(sid, session_dir: Session.session_dir(sid, state_dir: state_dir))&.get_json("snapshot")
245
+ live.is_a?(Hash) && live["snapshot"].is_a?(Hash) ? live["snapshot"] : nil
241
246
  rescue StandardError
242
247
  nil
243
248
  end
@@ -86,6 +86,9 @@ module Samagotchi
86
86
  return run_merge(dir) if parsed.options[:merge_subfolders]
87
87
 
88
88
  run_init(dir)
89
+ rescue MemoryPaths::ProjectIdConflict => e
90
+ error_line("#{command_name}: #{e.message}")
91
+ CLI::Exit::FAILED
89
92
  end
90
93
 
91
94
  private
@@ -114,7 +117,7 @@ module Samagotchi
114
117
 
115
118
  # @param found [MemoryPaths::ProjectLookup] the project (its root and id key the folder)
116
119
  def memories_line(found)
117
- dir = File.join(MemoryPaths.projects_dir(env: @env), MemoryPaths.key_for(root: found.root, id: found.id))
120
+ dir = File.join(MemoryPaths.projects_dir(env: @env), MemoryPaths.key_for(root: found.root, id: found.id, env: @env))
118
121
  count = memory_count(dir)
119
122
  "#{Paths.tilde(dir)}#{" (#{count} #{count == 1 ? "memory" : "memories"})" if count.positive?}"
120
123
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "fileutils"
3
4
  require_relative "atomic_file"
4
5
  require_relative "memory_paths"
5
6
  require_relative "paths"
@@ -11,52 +12,62 @@ module Samagotchi
11
12
  # MemoryOrphans), at every session start while it lasts.
12
13
  #
13
14
  # The claim: an id-keyed memories folder's `.root` names the folder that
14
- # last started a session with it (one line; a dotfile, so no *.md glob
15
- # lists it). Written only here, at session start, never by the key
16
- # resolver: a read-only command or a memory call must not write.
17
- ProjectIdClash = Data.define(:root, :other, :id) do
15
+ # owns it (one line; a dotfile, so no *.md glob lists it). A new folder is
16
+ # claimed before use; init also claims folders it moves or copies.
17
+ ProjectIdClash = Data.define(:root, :other, :id, :folder) do
18
+ def initialize(root:, other:, id:, folder: File.join(MemoryPaths.projects_dir, id))
19
+ super
20
+ end
21
+
18
22
  # The one text for the condition.
19
23
  def message
20
- "#{Paths.tilde(root)} and #{Paths.tilde(other)} share chi project id #{id} (a copied folder?): they share " \
21
- "memories. Run `chi project init --new-id` in the copy to give it its own."
24
+ if other.empty?
25
+ "#{folder} already exists and is not claimed by #{Paths.tilde(root)}; run `chi project init --new-id` " \
26
+ "to give this project its own id"
27
+ else
28
+ "#{Paths.tilde(root)} and #{Paths.tilde(other)} share chi project id #{id} (a copied folder?): " \
29
+ "#{folder} is already in use. Run `chi project init --new-id` in the copy to give it its own."
30
+ end
22
31
  end
23
32
  end
24
33
 
25
34
  class ProjectIdClash
26
- CLAIM = ".root"
35
+ CLAIM = MemoryPaths::CLAIM
27
36
 
28
- # +cwd+'s project, when a marked one with an id: a clash when the claim
29
- # names another folder that still has a marker with this id; otherwise
30
- # the claim is set to this root (when it changes; a moved folder just
31
- # takes it over). Only once the id folder exists: nothing is made for a
32
- # project with no memories yet.
37
+ # +cwd+'s project, when a marked one with an id: a clash when the id
38
+ # folder is already taken; otherwise the folder is claimed for this root
39
+ # (a moved folder just takes it over).
33
40
  # @return [ProjectIdClash, nil]
34
41
  def self.check(env: ENV, cwd: Dir.pwd, home: MemoryPaths.default_home)
35
42
  found = MemoryPaths.lookup(cwd, home: home)
36
43
  return nil unless found.id
37
44
 
38
45
  dir = File.join(MemoryPaths.projects_dir(env: env), found.id)
39
- return nil unless File.directory?(dir)
46
+ unless File.exist?(dir) || File.symlink?(dir)
47
+ FileUtils.mkdir_p(File.dirname(dir))
48
+ created = begin
49
+ Dir.mkdir(dir)
50
+ true
51
+ rescue Errno::EEXIST
52
+ false
53
+ end
54
+ if created
55
+ AtomicFile.write(File.join(dir, CLAIM), "#{found.root}\n")
56
+ return nil
57
+ end
58
+ end
40
59
 
41
60
  claim = File.join(dir, CLAIM)
42
61
  other = File.file?(claim) ? File.read(claim, 4096).to_s.lines.first.to_s.strip : ""
43
- return nil if other == found.root
44
- return new(root: found.root, other: other, id: found.id) if same_project?(other, found.id, home)
62
+ conflict = MemoryPaths.project_id_conflict(root: found.root, id: found.id, env: env, home: home)
63
+ return new(root: found.root, other: other, id: found.id, folder: dir) if conflict
45
64
 
46
- AtomicFile.write(claim, "#{found.root}\n")
65
+ AtomicFile.write(claim, "#{found.root}\n") unless other == found.root
47
66
  nil
48
67
  rescue SystemCallError
68
+ # Session start goes on: a memories folder chi can't make or claim
69
+ # is no clash to warn about.
49
70
  nil
50
71
  end
51
-
52
- # Whether +other+ still is a marked folder with +id+ (not gone, not
53
- # given another id).
54
- def self.same_project?(other, id, home)
55
- return false if other.empty? || !File.directory?(other)
56
-
57
- found = MemoryPaths.lookup(other, home: home)
58
- found.marker? && found.root == File.realpath(other) && found.id == id
59
- end
60
- private_class_method :same_project?
61
72
  end
62
73
  end
@@ -126,16 +126,31 @@ module Samagotchi
126
126
  end
127
127
 
128
128
  root = found.root
129
- old_key = MemoryPaths.key_for(root: root, id: found.id)
129
+ conflict = nil
130
+ old_key = begin
131
+ MemoryPaths.key_for(root: root, id: found.id, env: env, home: home)
132
+ rescue MemoryPaths::ProjectIdConflict => e
133
+ conflict = e
134
+ nil
135
+ end
130
136
  id = ProjectId.generate(File.basename(root))
131
137
  lines = ["#{Paths.tilde(root)} has a chi project id of its own now: #{id} (was #{found.id || "none: keyed by its path"})"]
132
- places(env: env, state_dir: state_dir || SessionPaths.default_state_dir(env: env)).each do |place|
133
- from = place.path(old_key)
134
- next unless File.directory?(from)
138
+ if conflict
139
+ lines << "did not copy #{Paths.tilde(conflict.folder)} because it is already used by another project"
140
+ else
141
+ project_places = places(env: env, state_dir: state_dir || SessionPaths.default_state_dir(env: env))
142
+ project_places.each do |place|
143
+ from = place.path(old_key)
144
+ next unless File.directory?(from)
135
145
 
136
- to = place.path(id)
137
- copy_folder(File.realpath(from), to)
138
- lines << "copied #{Paths.tilde(from)} to #{Paths.tilde(to)}"
146
+ to = place.path(id)
147
+ copy_folder(File.realpath(from), to)
148
+ lines << "copied #{Paths.tilde(from)} to #{Paths.tilde(to)}"
149
+ end
150
+ memory_folder = project_places.first.path(id)
151
+ if File.directory?(memory_folder)
152
+ AtomicFile.write(File.join(memory_folder, MemoryPaths::CLAIM), "#{root}\n")
153
+ end
139
154
  end
140
155
  write_id(root, id)
141
156
  Outcome.new(kind: :new_id, reason: nil, root: root, top: root, nested_in: nil, message: lines.join("\n"), id: id)
@@ -181,6 +196,8 @@ module Samagotchi
181
196
  "with it when the folder moves or is renamed"]
182
197
  lines << "#{problem}: replaced" if problem
183
198
  list.each { |place| lines.concat(move_folder(place.path(path_key), place.path(id))) }
199
+ memory_folder = list.first.path(id)
200
+ AtomicFile.write(File.join(memory_folder, MemoryPaths::CLAIM), "#{root}\n") if File.directory?(memory_folder)
184
201
  write_id(root, id)
185
202
  if orphans
186
203
  keys = orphans.candidates.map { |candidate| "#{Paths.tilde(candidate)} (#{MemoryOrphans.key_note(candidate)})" }
@@ -126,21 +126,11 @@ module Samagotchi
126
126
 
127
127
  # Taken but never run as a turn of its own: a running turn's drain
128
128
  # merged it as a steer, so that turn's reply (or question) is its
129
- # answer. With the drain's merge record (SessionInbox.mark_merged)
130
- # the wait is that turn's from here on, as if it had been bound to it
131
- # (#follow_into). Without one (a 0.156 worker, a turn with no id, a
132
- # merge from before the update) the merge is inferred
133
- # (ReplyWait.merged_elsewhere?) and waited for as the next reply past
134
- # the cursor, as without turn:, never as a turn that will not come.
129
+ # answer. The drain's merge record (SessionInbox.mark_merged) says
130
+ # which turn: the wait is that turn's from here on, as if it had been
131
+ # bound to it (#follow_into).
135
132
  def follow_merge(session, merge)
136
- return unless may_follow?
137
-
138
- if merge
139
- follow_into(session, merge)
140
- elsif ReplyWait.merged_elsewhere?(session, @turn, @baseline, @session_dir)
141
- @turn = nil
142
- @seen_running = running?(session)
143
- end
133
+ follow_into(session, merge) if merge && may_follow?
144
134
  end
145
135
 
146
136
  def may_follow? = @turn && @claimed && !@seen_own && !@own_turn_input
@@ -259,29 +259,6 @@ module Samagotchi
259
259
  !baseline.ended_at?(session.last_turn&.ended_at)
260
260
  end
261
261
 
262
- # A turn: wait's taken input ran in another turn (a steer its drain
263
- # merged): that turn runs (its mark has another id, or none from a
264
- # worker that writes ids), or, idle, a turn not its ended since the
265
- # baseline. last_turn or a reply file being its own is never that, nor
266
- # an input with a tombstone (a removal's, written before its claim),
267
- # nor one whose claimed file is still there: a drain deletes the file
268
- # it merges as it reads it, while a turn of its own keeps it until the
269
- # turn ends (WorkerInbound#take_input), so a look between that claim
270
- # and the turn's running save (idle, an earlier turn's end since the
271
- # baseline) doesn't take the earlier turn's reply for it.
272
- def merged_elsewhere?(session, turn, baseline, session_dir)
273
- return false if session.last_turn&.enqueued_id == turn || SessionInbox.reply_for(session_dir, turn)
274
- return false if SessionInbox.removed?(session_dir, turn) || SessionInbox.claimed_input?(session_dir, turn)
275
-
276
- if session.status == Session::STATUS_RUNNING
277
- running_id = session.interrupted_turn&.enqueued_id
278
- return running_id != turn if running_id
279
-
280
- return turn_ids?(session_dir)
281
- end
282
- turn_ended_since?(session, baseline)
283
- end
284
-
285
262
  # A turn seen running (+own_base+: last_turn's ended_at then) is past:
286
263
  # last_turn isn't its, and another turn runs (a mark with another id)
287
264
  # or a turn ended since.
@@ -267,7 +267,7 @@ module Samagotchi
267
267
  opts.on("--no-default-input", "Skip the default input typed into a new session's first read (SAMAGOTCHI_DEFAULT_INPUT)") { options[:no_default_input] = true; cli_overrides["no_default_input"] = true }
268
268
  opts.on("--port PORT", "Port for `chi web` (default: 4567, env SAMAGOTCHI_WEB_PORT)") { |p| options[:web_port] = p.to_i; cli_overrides["web.port"] = p.to_s }
269
269
  opts.on("--open", "Open browser for `chi web`") { options[:web_open] = true }
270
- opts.on("--new-token", "`chi web`: replace the LAN access token (web.host: lan); links and QR codes made with the old one stop working") { options[:new_token] = true }
270
+ opts.on("--new-token", "`chi web`: replace its access token (LAN link, --tunnel tailscale, proxy); old links, QR codes and cookies stop working, participant grants stay") { options[:new_token] = true }
271
271
  opts.on("--tunnel PROVIDER", %w[tailscale],
272
272
  "`chi web`: reach it over https from your tailnet through Tailscale Serve (never Funnel), for this run; " \
273
273
  "turned off on exit") { |v| options[:tunnel] = v }