samagotchi 0.163.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 +77 -1
- data/docs/architecture.md +7 -5
- data/docs/bundles.md +4 -2
- data/docs/cli.md +23 -11
- data/docs/configuration.md +5 -1
- data/docs/guardrails.md +13 -16
- data/docs/internals/block-renderers.md +3 -1
- data/docs/internals/bridge.md +4 -3
- data/docs/internals/bundles.md +4 -2
- data/docs/internals/inline-widgets.md +13 -3
- data/docs/internals/remote-sessions.md +7 -2
- 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 +9 -6
- data/docs/sub-agent.md +11 -0
- data/docs/testing.md +28 -4
- data/lib/samagotchi/block_renderers.rb +14 -7
- 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/inline-widgets/manifest.yml +2 -2
- data/lib/samagotchi/bundles/inline-widgets/plugin.rb +8 -4
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/chi_home.rb +44 -2
- data/lib/samagotchi/cli/parent_wait.rb +5 -1
- 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 +93 -0
- 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/message_parts.rb +13 -0
- data/lib/samagotchi/note_command.rb +3 -6
- data/lib/samagotchi/parent_report.rb +3 -2
- 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/parent_gate.rb +8 -5
- data/lib/samagotchi/send_command.rb +203 -45
- data/lib/samagotchi/session.rb +3 -2
- data/lib/samagotchi/session_access/local.rb +25 -2
- data/lib/samagotchi/session_access/remote.rb +40 -4
- data/lib/samagotchi/session_chain/links.rb +109 -0
- data/lib/samagotchi/session_chain.rb +7 -19
- data/lib/samagotchi/session_continue.rb +143 -57
- data/lib/samagotchi/session_inbox_delivery.rb +94 -28
- data/lib/samagotchi/session_manager/delivery.rb +106 -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/message_display.rb +1 -9
- data/lib/samagotchi/web/public/activity.js +2 -2
- data/lib/samagotchi/web/public/app.js +13 -13
- data/lib/samagotchi/web/public/apple-touch-icon.png +0 -0
- 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/favicon.svg +1 -1
- data/lib/samagotchi/web/public/icons/chi-180.png +0 -0
- data/lib/samagotchi/web/public/icons/chi-192.png +0 -0
- data/lib/samagotchi/web/public/icons/chi-512.png +0 -0
- 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/inline_widgets.js +4 -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/public/widget_walk.js +14 -4
- data/lib/samagotchi/web/session_access_routes.rb +8 -1
- 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 +4 -1
|
@@ -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.
|
|
@@ -14,6 +14,7 @@ require_relative "starting_marker"
|
|
|
14
14
|
require_relative "context_move"
|
|
15
15
|
require_relative "note_move"
|
|
16
16
|
require_relative "archive_store"
|
|
17
|
+
require_relative "continue_intent"
|
|
17
18
|
require_relative "recap_store"
|
|
18
19
|
require_relative "bridge_client"
|
|
19
20
|
require_relative "worker_link"
|
|
@@ -59,13 +60,75 @@ module Samagotchi
|
|
|
59
60
|
# recap (IdleRecap's own request timeout).
|
|
60
61
|
RECAP_WAIT = 30.0
|
|
61
62
|
# In the previous link's folder: one continue of a session at a time.
|
|
62
|
-
LOCK =
|
|
63
|
-
# A durable record in the previous link's folder, written before archive
|
|
64
|
-
|
|
63
|
+
LOCK = ContinueIntent::LOCK
|
|
64
|
+
# A durable record in the previous link's folder, written before archive
|
|
65
|
+
# (a send in flight reads it: ContinueIntent.in_flight).
|
|
66
|
+
CONTINUE_INTENT = ContinueIntent::FILE
|
|
65
67
|
# How often a last:<id> continue looks again when another continue
|
|
66
68
|
# moved the chain's end while it waited for the lock.
|
|
67
69
|
TRIES = 3
|
|
68
70
|
|
|
71
|
+
# One thing a continue carries from the previous link to the new one.
|
|
72
|
+
# +carry+ names the method run in spawn_session's before_spawn (once the
|
|
73
|
+
# link and its chain note are saved, before its worker spawns): it takes
|
|
74
|
+
# a CarryContext and returns what the step carried, kept in #run's
|
|
75
|
+
# +carried+ under +name+. +undo+ maps an UNDO_PHASES phase to the method
|
|
76
|
+
# (taking an UndoContext) that gives it back when the continue fails;
|
|
77
|
+
# empty when there is nothing to undo (CARRY's comment says why).
|
|
78
|
+
# +store+ is the SessionManager::SESSION_STATE_STORES store it carries.
|
|
79
|
+
CarryStep = Data.define(:name, :store, :carry, :undo)
|
|
80
|
+
# What a carry step works on: the previous link (a Session), the new
|
|
81
|
+
# link's id, the delegates moving (ChildMove::Plan#move).
|
|
82
|
+
CarryContext = Data.define(:previous, :to, :state_dir, :move)
|
|
83
|
+
# What an undo works on: the previous link's id, the failed link (nil in
|
|
84
|
+
# :moved_back, which looks it up itself), the delegates the plan moved,
|
|
85
|
+
# and what the forward carry kept (#run's +carried+, by step name).
|
|
86
|
+
UndoContext = Data.define(:from, :link, :state_dir, :moved, :carried)
|
|
87
|
+
|
|
88
|
+
# When a failed continue undoes each step, in time order: :moved_back
|
|
89
|
+
# while the failed link's worker may still run (before
|
|
90
|
+
# #discard_failed_link), :link_stopped once its worker is stopped,
|
|
91
|
+
# :link_unlinked once its session file is gone (out of the chain: a note
|
|
92
|
+
# to the previous link stays there), :link_deleted once its folder is
|
|
93
|
+
# gone. Within a phase, the steps undo in reverse CARRY order.
|
|
94
|
+
UNDO_PHASES = %i[moved_back link_stopped link_unlinked link_deleted].freeze
|
|
95
|
+
|
|
96
|
+
# What a continue carries, in order (the forward carry runs them first
|
|
97
|
+
# to last, in the new link's before_spawn). A new per-session store a
|
|
98
|
+
# continue should carry is added here, with its undo.
|
|
99
|
+
CARRY = [
|
|
100
|
+
# Its open delegates (ChildMove): given back to the previous link
|
|
101
|
+
# first, then the rings one rang the link with just before the undo.
|
|
102
|
+
CarryStep.new(name: :children, store: :session_files, carry: :carry_children,
|
|
103
|
+
undo: { moved_back: :undo_move, link_stopped: :return_rings }),
|
|
104
|
+
# Its movable context too, with a note saying what moved and stayed.
|
|
105
|
+
# No undo: ContextSources.move copies (the previous link keeps its
|
|
106
|
+
# sources), and the link's copies go with its delete (remove_files).
|
|
107
|
+
CarryStep.new(name: :context, store: :context_sources, carry: :carry_context, undo: {}),
|
|
108
|
+
# Its objectives: the list reads as the latest word. No undo: a note
|
|
109
|
+
# written into the link only (NoteMove::CONTINUE_SOURCES: never given
|
|
110
|
+
# back), which goes with its delete.
|
|
111
|
+
CarryStep.new(name: :objectives, store: :session_files, carry: :carry_objectives, undo: {}),
|
|
112
|
+
# Its waiting notes last: the carry above read a pending /objectives
|
|
113
|
+
# edit from them first. They keep their names, so they still sort
|
|
114
|
+
# ahead of the link's own notes. A note sent to the previous link
|
|
115
|
+
# from here on goes to this one (SessionInbox::Head), and a
|
|
116
|
+
# straggler is adopted by its worker (NoteMove.adoption). Given back
|
|
117
|
+
# once the link is out of the chain, and again for a note an older chi
|
|
118
|
+
# wrote after its folder went.
|
|
119
|
+
CarryStep.new(name: :notes, store: :session_files, carry: :carry_notes,
|
|
120
|
+
undo: { link_unlinked: :give_back_notes, link_deleted: :give_back_strays })
|
|
121
|
+
].freeze
|
|
122
|
+
|
|
123
|
+
# The SessionManager::SESSION_STATE_STORES a continue deliberately
|
|
124
|
+
# leaves with the previous link, and why: every store is in CARRY or here.
|
|
125
|
+
NOT_CARRIED = {
|
|
126
|
+
plugin_session_state: "a bundle's per-session state (ctx.session_dir) stays with the archived link",
|
|
127
|
+
participants: "the archive ends the previous link's review (D8); a continue copies no grants",
|
|
128
|
+
system_one_runs: "System 1's saved runs record the previous link's own turns",
|
|
129
|
+
context_candidates_log: "the candidates log records the previous link's own turns"
|
|
130
|
+
}.freeze
|
|
131
|
+
|
|
69
132
|
# The model a new link runs on (#model_for): the name to spawn it with,
|
|
70
133
|
# and why it isn't the previous link's (nil when it is, or was given).
|
|
71
134
|
ModelChoice = Data.define(:model, :warning)
|
|
@@ -172,24 +235,22 @@ module Samagotchi
|
|
|
172
235
|
end
|
|
173
236
|
successor_id = SecureRandom.uuid
|
|
174
237
|
intent_path = continue_intent_path(id, state_dir)
|
|
175
|
-
|
|
176
|
-
AtomicFile.write(intent_path, "#{JSON.generate(intent_data)}\n", perm: 0o600)
|
|
238
|
+
ContinueIntent.write(intent_path, successor_id: successor_id, previously_archived: already)
|
|
177
239
|
archive = SessionManager.archive_session(id, state_dir: state_dir, wait: archive_wait, except: plan.move)
|
|
178
240
|
archived = archive.archived - already
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
notes = []
|
|
241
|
+
ContinueIntent.write(intent_path, successor_id: successor_id, previously_archived: already, archived_ids: archived)
|
|
242
|
+
# What each CARRY step took to the new link, by name.
|
|
243
|
+
carried = {}
|
|
183
244
|
begin
|
|
184
245
|
link = spawn_continuation(previous, prompt: prompt, preview: preview, choice: checked.choice, state_dir: state_dir,
|
|
185
|
-
move: plan.move, client_id: client_id,
|
|
246
|
+
move: plan.move, client_id: client_id, carried: carried, session_id: successor_id)
|
|
186
247
|
rescue StandardError, Interrupt
|
|
187
248
|
# Nothing started: the delegates go back, what this archived goes
|
|
188
249
|
# back to the lists, and a new link saved before its worker failed
|
|
189
250
|
# to spawn goes (left, it would hold the chain: "continued already",
|
|
190
251
|
# last: to it), its notes given back first.
|
|
191
|
-
undo_failed_continue(id, state_dir, moved: plan.move, archived: archived) do
|
|
192
|
-
discard_failed_link(id, state_dir,
|
|
252
|
+
undo_failed_continue(id, state_dir, moved: plan.move, archived: archived, carried: carried) do
|
|
253
|
+
discard_failed_link(id, state_dir, moved: plan.move, carried: carried)
|
|
193
254
|
end
|
|
194
255
|
FileUtils.rm_f(intent_path)
|
|
195
256
|
raise
|
|
@@ -209,18 +270,11 @@ module Samagotchi
|
|
|
209
270
|
next unless lock.flock(File::LOCK_EX | File::LOCK_NB)
|
|
210
271
|
next unless File.file?(path)
|
|
211
272
|
|
|
212
|
-
intent =
|
|
213
|
-
|
|
214
|
-
successor_id = intent["successor_id"]
|
|
215
|
-
successor_path = Session.session_file(successor_id, state_dir: state_dir) if Session.valid_id?(successor_id)
|
|
273
|
+
intent = ContinueIntent.read(path)
|
|
274
|
+
successor_path = Session.session_file(intent.successor_id, state_dir: state_dir) if intent.successor_id
|
|
216
275
|
successor = JSON.parse(File.read(successor_path)) if successor_path && File.file?(successor_path)
|
|
217
276
|
unless successor.is_a?(Hash) && successor["continues"] == session.id
|
|
218
|
-
|
|
219
|
-
archived = if intent["archived_ids"].is_a?(Array)
|
|
220
|
-
intent["archived_ids"].select { |id| Session.valid_id?(id) }
|
|
221
|
-
else
|
|
222
|
-
archived_descendants(session.id, sessions, state_dir) - previously_archived
|
|
223
|
-
end
|
|
277
|
+
archived = intent.archived_ids || (archived_descendants(session.id, sessions, state_dir) - intent.previously_archived)
|
|
224
278
|
undo_failed_continue(session.id, state_dir, moved: [], archived: archived)
|
|
225
279
|
end
|
|
226
280
|
FileUtils.rm_f(path)
|
|
@@ -257,8 +311,8 @@ module Samagotchi
|
|
|
257
311
|
end
|
|
258
312
|
end
|
|
259
313
|
|
|
260
|
-
private_class_method def self.undo_failed_continue(id, state_dir, moved:, archived:)
|
|
261
|
-
|
|
314
|
+
private_class_method def self.undo_failed_continue(id, state_dir, moved:, archived:, carried: {})
|
|
315
|
+
undo_carry(:moved_back, UndoContext.new(from: id, link: nil, state_dir: state_dir, moved: moved, carried: carried))
|
|
262
316
|
begin
|
|
263
317
|
yield if block_given?
|
|
264
318
|
ensure
|
|
@@ -267,41 +321,81 @@ module Samagotchi
|
|
|
267
321
|
end
|
|
268
322
|
end
|
|
269
323
|
|
|
324
|
+
# The CARRY steps' undos for +phase+ (UNDO_PHASES), in reverse CARRY order.
|
|
325
|
+
private_class_method def self.undo_carry(phase, ctx)
|
|
326
|
+
CARRY.reverse_each do |step|
|
|
327
|
+
method = step.undo[phase]
|
|
328
|
+
send(method, ctx) if method
|
|
329
|
+
end
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
# CARRY's forward steps (CarryContext).
|
|
333
|
+
private_class_method def self.carry_children(ctx)
|
|
334
|
+
ChildMove.apply(ctx.move, from: ctx.previous.id, to: ctx.to, state_dir: ctx.state_dir)
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
private_class_method def self.carry_context(ctx)
|
|
338
|
+
ContextMove.call(from: ctx.previous.id, to: ctx.to, mode: :continue, state_dir: ctx.state_dir)
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
private_class_method def self.carry_objectives(ctx)
|
|
342
|
+
Objectives.carry(ctx.previous, to: ctx.to, state_dir: ctx.state_dir)
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
# @return [Array<String>] the names of the notes moved to the link
|
|
346
|
+
private_class_method def self.carry_notes(ctx)
|
|
347
|
+
NoteMove.move(from: ctx.previous.id, to: ctx.to, state_dir: ctx.state_dir)
|
|
348
|
+
end
|
|
349
|
+
|
|
270
350
|
# The delegates a failed #run moved, or began to (its
|
|
271
|
-
# intent names them), go back to
|
|
272
|
-
private_class_method def self.undo_move(
|
|
351
|
+
# intent names them), go back to the previous link.
|
|
352
|
+
private_class_method def self.undo_move(ctx)
|
|
353
|
+
id = ctx.from
|
|
354
|
+
state_dir = ctx.state_dir
|
|
273
355
|
link = SessionChain.next_of(id, state_dir: state_dir) or return
|
|
274
|
-
ids = ChildMove.read_intent(link, state_dir: state_dir)&.ids || moved
|
|
356
|
+
ids = ChildMove.read_intent(link, state_dir: state_dir)&.ids || ctx.moved
|
|
275
357
|
ChildMove.apply(ids, from: link, to: id, state_dir: state_dir, undo: true)
|
|
276
358
|
ChildMove.clear_intent(link, state_dir: state_dir)
|
|
277
359
|
rescue StandardError => e
|
|
278
360
|
Log.warn(:worker, "continue_undo_move_failed", sid: id, error: e.class.name, msg: e.message)
|
|
279
361
|
end
|
|
280
362
|
|
|
363
|
+
# The rings a moved delegate wrote into the failed link just before the
|
|
364
|
+
# undo go back to the previous link.
|
|
365
|
+
private_class_method def self.return_rings(ctx)
|
|
366
|
+
ChildMove.return_rings(from: ctx.link, to: ctx.from, state_dir: ctx.state_dir)
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
# The notes the link took (CARRY's :notes), with any sent to it meanwhile.
|
|
370
|
+
private_class_method def self.give_back_notes(ctx)
|
|
371
|
+
NoteMove.give_back(from: ctx.link, to: ctx.from, moved: Array(ctx.carried[:notes]), state_dir: ctx.state_dir)
|
|
372
|
+
end
|
|
373
|
+
|
|
281
374
|
# The link a failed #run saved (no other continues +id+:
|
|
282
375
|
# check! made sure under the lock): its worker stopped if one
|
|
283
376
|
# started, then the rings a moved delegate wrote into it just before
|
|
284
|
-
# the undo go back to +id
|
|
285
|
-
# link is out of the chain (a note to
|
|
286
|
-
# SessionInbox::Head) and a writer that resolved it
|
|
287
|
-
# then the notes it moved there
|
|
288
|
-
# meanwhile (NoteMove.give_back), then the
|
|
289
|
-
# last look gives back a note an older chi
|
|
290
|
-
#
|
|
291
|
-
#
|
|
292
|
-
# the caller hears it.
|
|
293
|
-
private_class_method def self.discard_failed_link(id, state_dir,
|
|
377
|
+
# the undo go back to +id+ (CARRY's :link_stopped undos). Then its
|
|
378
|
+
# session file goes first, so the link is out of the chain (a note to
|
|
379
|
+
# +id+ stays there: SessionInbox::Head) and a writer that resolved it
|
|
380
|
+
# before finds it gone, then the notes it moved there go back with any
|
|
381
|
+
# sent to it meanwhile (:link_unlinked: NoteMove.give_back), then the
|
|
382
|
+
# link's folder goes, and one last look gives back a note an older chi
|
|
383
|
+
# wrote after the delete (:link_deleted; its write_note recreates the
|
|
384
|
+
# folder). A worker that outlives the stop is raised
|
|
385
|
+
# (SessionManager::DeleteRefused): the link stays, and the caller hears it.
|
|
386
|
+
private_class_method def self.discard_failed_link(id, state_dir, moved:, carried:)
|
|
294
387
|
orphan = SessionChain.next_of(id, state_dir: state_dir) or return
|
|
295
388
|
if SessionManager.refuse_tui!(orphan, state_dir: state_dir) &&
|
|
296
389
|
!SessionManager.stop_session(orphan, state_dir: state_dir, wait: SessionManager::STOP_WAIT)
|
|
297
390
|
raise SessionManager::DeleteRefused.new(orphan, :still_stopping)
|
|
298
391
|
end
|
|
299
392
|
|
|
300
|
-
|
|
393
|
+
ctx = UndoContext.new(from: id, link: orphan, state_dir: state_dir, moved: moved, carried: carried)
|
|
394
|
+
undo_carry(:link_stopped, ctx)
|
|
301
395
|
FileUtils.rm_f(Session.session_file(orphan, state_dir: state_dir))
|
|
302
|
-
|
|
396
|
+
undo_carry(:link_unlinked, ctx)
|
|
303
397
|
SessionManager.delete_session(orphan, state_dir: state_dir, allow_scheduled: true)
|
|
304
|
-
|
|
398
|
+
undo_carry(:link_deleted, ctx)
|
|
305
399
|
rescue SessionManager::DeleteRefused
|
|
306
400
|
raise
|
|
307
401
|
rescue StandardError => e
|
|
@@ -310,11 +404,13 @@ module Samagotchi
|
|
|
310
404
|
|
|
311
405
|
# A failed link's folder an older chi's note brought back after its
|
|
312
406
|
# delete: the note goes back to +id+, the folder goes again.
|
|
313
|
-
private_class_method def self.give_back_strays(
|
|
407
|
+
private_class_method def self.give_back_strays(ctx)
|
|
408
|
+
orphan = ctx.link
|
|
409
|
+
state_dir = ctx.state_dir
|
|
314
410
|
dir = Session.session_dir(orphan, state_dir: state_dir)
|
|
315
411
|
return unless Dir.exist?(dir) && !Session.exist?(orphan, state_dir: state_dir)
|
|
316
412
|
|
|
317
|
-
NoteMove.give_back(from: orphan, to:
|
|
413
|
+
NoteMove.give_back(from: orphan, to: ctx.from, moved: [], state_dir: state_dir)
|
|
318
414
|
FileUtils.rm_rf(dir)
|
|
319
415
|
end
|
|
320
416
|
|
|
@@ -331,25 +427,15 @@ module Samagotchi
|
|
|
331
427
|
# The new link, holding off any worker but its own while +move+ goes to
|
|
332
428
|
# it (spawn_session marks it starting before its file is saved:
|
|
333
429
|
# StartingMarker.fresh?).
|
|
334
|
-
# @param
|
|
335
|
-
private_class_method def self.spawn_continuation(previous, prompt:, preview:, choice:, state_dir:, move:,
|
|
430
|
+
# @param carried [Hash] gets what each CARRY step took to it, by name
|
|
431
|
+
private_class_method def self.spawn_continuation(previous, prompt:, preview:, choice:, state_dir:, move:, carried:,
|
|
336
432
|
session_id:, client_id: nil)
|
|
337
433
|
recap = RecapStore.read(Session.session_dir(previous.id, state_dir: state_dir))
|
|
338
434
|
start = prompt.to_s.strip.empty? ? nil : prompt
|
|
339
435
|
preview = previous.first_preview if preview.to_s.strip.empty? && start.nil?
|
|
340
|
-
moved_context = nil
|
|
341
436
|
before_spawn = lambda do |link|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
moved_context = ContextMove.call(from: previous.id, to: link.id, mode: :continue, state_dir: state_dir)
|
|
345
|
-
# Its objectives: the list reads as the latest word.
|
|
346
|
-
Objectives.carry(previous, to: link.id, state_dir: state_dir)
|
|
347
|
-
# Its waiting notes last: the carry above read a pending /objectives
|
|
348
|
-
# edit from them first. They keep their names, so they still sort
|
|
349
|
-
# ahead of the link's own notes. A note sent to the previous link
|
|
350
|
-
# from here on goes to this one (SessionInbox::Head), and a
|
|
351
|
-
# straggler is adopted by its worker (NoteMove.adoption).
|
|
352
|
-
notes.concat(NoteMove.move(from: previous.id, to: link.id, state_dir: state_dir))
|
|
437
|
+
ctx = CarryContext.new(previous: previous, to: link.id, state_dir: state_dir, move: move)
|
|
438
|
+
CARRY.each { |step| carried[step.name] = send(step.carry, ctx) }
|
|
353
439
|
end
|
|
354
440
|
link = SessionManager.spawn_session(
|
|
355
441
|
prompt: start, preview: preview, title: previous.title, working_directory: previous.working_directory,
|
|
@@ -359,7 +445,7 @@ module Samagotchi
|
|
|
359
445
|
session_id: session_id
|
|
360
446
|
)
|
|
361
447
|
link.moved_children = move
|
|
362
|
-
link.moved_context =
|
|
448
|
+
link.moved_context = carried[:context]
|
|
363
449
|
link.opener = SessionChain.opener_for(previous)
|
|
364
450
|
# The fallback's warning first: spawn_session set its own.
|
|
365
451
|
warnings = [choice.warning, link.model_warning].compact
|
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
require_relative "session"
|
|
4
4
|
require_relative "session_inbox"
|
|
5
5
|
require_relative "archive_store"
|
|
6
|
+
require_relative "client_id"
|
|
6
7
|
require_relative "worker_presence"
|
|
8
|
+
require_relative "continue_intent"
|
|
9
|
+
require_relative "session_chain/links"
|
|
7
10
|
|
|
8
11
|
module Samagotchi
|
|
9
12
|
# Loaded on first use: this autoload keeps SessionManager and its ~80
|
|
@@ -18,9 +21,28 @@ module Samagotchi
|
|
|
18
21
|
# (spec/samagotchi/testing/sandbox_spec.rb) stays as it was.
|
|
19
22
|
module SessionInbox
|
|
20
23
|
# Where a delivery for +asked+ goes: +id+, reached +via+ :pinned (the
|
|
21
|
-
# kind never follows a chain), :same (followed, nothing to follow)
|
|
22
|
-
# :head (a later link of +asked+'s chain)
|
|
23
|
-
|
|
24
|
+
# kind never follows a chain), :same (followed, nothing to follow),
|
|
25
|
+
# :head (a later link of +asked+'s chain) or :continuing (a send whose
|
|
26
|
+
# link is being continued: +id+ is the successor, not saved yet; the
|
|
27
|
+
# sender asks again in a moment, Head.resolve).
|
|
28
|
+
Resolved = Data.define(:id, :asked, :via) do
|
|
29
|
+
# How a line names where it went: +asked+'s short id, or
|
|
30
|
+
# "OLD → NEW (continued)" when a later link took it (chi send's and
|
|
31
|
+
# chi note's one wording).
|
|
32
|
+
def self.label(asked, id)
|
|
33
|
+
return asked[0, 8] if asked == id
|
|
34
|
+
|
|
35
|
+
"#{asked[0, 8]} → #{id[0, 8]} (continued)"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def label = Resolved.label(asked, id)
|
|
39
|
+
|
|
40
|
+
def continuing? = via == :continuing
|
|
41
|
+
|
|
42
|
+
# The refusal of a send whose link is still being continued once the
|
|
43
|
+
# sender's wait is over.
|
|
44
|
+
def continuing_refusal = "being continued as #{id[0, 8]}; send again in a moment"
|
|
45
|
+
end
|
|
24
46
|
|
|
25
47
|
# A delivery's outcome.
|
|
26
48
|
# @!attribute kind [Symbol] the WakePolicy row
|
|
@@ -44,8 +66,10 @@ module Samagotchi
|
|
|
44
66
|
# What a delivery of one kind may do to a session's worker: one frozen
|
|
45
67
|
# row per kind (ROWS), decided on WorkerPresence's predicates alone, in
|
|
46
68
|
# one order for every kind (#decide).
|
|
47
|
-
# @!attribute kind [Symbol] :send, :note, :push, :ring, :fresh_verdict
|
|
48
|
-
# :leave (WorkerBoot's respawn, not a delivery)
|
|
69
|
+
# @!attribute kind [Symbol] :send, :note, :push, :ring, :fresh_verdict,
|
|
70
|
+
# :leave (WorkerBoot's respawn, not a delivery), or a turn delivery's
|
|
71
|
+
# unattended sender (deliver_turn, #for_sender): :schedule, :plugin,
|
|
72
|
+
# :delegate, :participant, :automatic
|
|
49
73
|
# @!attribute resolves [Symbol] which link it goes to (Head.resolve):
|
|
50
74
|
# :pinned the id named; :head the chain's newest link from it; :reader
|
|
51
75
|
# the link that reads the named one's file (a ring: the file stays with
|
|
@@ -53,9 +77,9 @@ module Samagotchi
|
|
|
53
77
|
# rings, so the wake goes one hop at most)
|
|
54
78
|
# @!attribute starts [Boolean] it may start a worker
|
|
55
79
|
# @!attribute unstops [Boolean] it starts a stopped session (a resume
|
|
56
|
-
# clears the stop): send only
|
|
80
|
+
# clears the stop): send only (a person's)
|
|
57
81
|
# @!attribute starts_archived [Boolean] it starts an archived link
|
|
58
|
-
# (input from a person un-archives it): send only
|
|
82
|
+
# (input from a person un-archives it): send (and leave) only
|
|
59
83
|
# @!attribute refuse_tui [Boolean] a one-turn chi run's session refuses it
|
|
60
84
|
# @!attribute refuse_scratch [Boolean] a chi scratch session refuses it
|
|
61
85
|
WakePolicy = Data.define(:kind, :resolves, :starts, :unstops, :starts_archived, :refuse_tui, :refuse_scratch) do
|
|
@@ -63,6 +87,11 @@ module Samagotchi
|
|
|
63
87
|
# @raise [ArgumentError] no such kind
|
|
64
88
|
def self.for(kind) = WakePolicy::ROWS.fetch(kind) { raise ArgumentError, "no inbox kind #{kind.inspect}" }
|
|
65
89
|
|
|
90
|
+
# The row a turn delivery from +client_id+ reads: its kind by the
|
|
91
|
+
# sender (ClientId.wake_kind), never a caller's flag.
|
|
92
|
+
# @return [WakePolicy]
|
|
93
|
+
def self.for_sender(client_id) = self.for(ClientId.wake_kind(client_id))
|
|
94
|
+
|
|
66
95
|
# Why the delivery is refused before anything is written, else nil:
|
|
67
96
|
# :gone (no session), :owned (a one-turn chi run's), :scratch.
|
|
68
97
|
# @param load [#call, nil] the session, read only when the scratch
|
|
@@ -105,8 +134,18 @@ module Samagotchi
|
|
|
105
134
|
private def scratch?(load) = load ? load.call.scratch == true : false
|
|
106
135
|
end
|
|
107
136
|
|
|
137
|
+
# A send follows a chain as a note does: input for a continued link
|
|
138
|
+
# goes to its chain's newest link (chi send resolves it up front,
|
|
139
|
+
# SessionAccess#head; SessionManager.deliver_turn resolves nothing, but
|
|
140
|
+
# refuses that followed send when a continue took the archived link
|
|
141
|
+
# since, and chi send resolves again: deliver_turn's follow:).
|
|
142
|
+
# What stays on the id it names doesn't ask Head: the web composer (a
|
|
143
|
+
# person on that page means that session), a participant's grant (one
|
|
144
|
+
# session), delegate follow-ups, Plugin::Sessions and Schedules (the
|
|
145
|
+
# chain's end, SessionChain::Links#latest: a firing continues the end,
|
|
146
|
+
# and never un-archives a retired chain).
|
|
108
147
|
WakePolicy::ROWS = {
|
|
109
|
-
send: WakePolicy.new(kind: :send, resolves: :
|
|
148
|
+
send: WakePolicy.new(kind: :send, resolves: :head, starts: true, unstops: true, starts_archived: true,
|
|
110
149
|
refuse_tui: true, refuse_scratch: true),
|
|
111
150
|
note: WakePolicy.new(kind: :note, resolves: :head, starts: false, unstops: false, starts_archived: false,
|
|
112
151
|
refuse_tui: true, refuse_scratch: true),
|
|
@@ -121,7 +160,16 @@ module Samagotchi
|
|
|
121
160
|
# lock's release). A stop wins; an archived session's input still
|
|
122
161
|
# starts it, as a send's would have.
|
|
123
162
|
leave: WakePolicy.new(kind: :leave, resolves: :pinned, starts: true, unstops: false, starts_archived: true,
|
|
124
|
-
refuse_tui: false, refuse_scratch: false)
|
|
163
|
+
refuse_tui: false, refuse_scratch: false),
|
|
164
|
+
# A turn delivery's unattended senders (deliver_turn reads the row by
|
|
165
|
+
# the sender, ClientId.wake_kind): they start an idle session as a send
|
|
166
|
+
# does, but a stop and an archive are the owner's (the sender is
|
|
167
|
+
# refused, SessionManager::DeliverResult::STOPPED / ARCHIVED). They
|
|
168
|
+
# stay on the id they name. :automatic is any sender chi doesn't name.
|
|
169
|
+
**%i[schedule plugin delegate participant automatic].to_h do |kind|
|
|
170
|
+
[kind, WakePolicy.new(kind: kind, resolves: :pinned, starts: true, unstops: false, starts_archived: false,
|
|
171
|
+
refuse_tui: true, refuse_scratch: true)]
|
|
172
|
+
end
|
|
125
173
|
}.freeze
|
|
126
174
|
|
|
127
175
|
# Which link of a chain a delivery goes to.
|
|
@@ -133,33 +181,51 @@ module Samagotchi
|
|
|
133
181
|
# Else the walk forward over archived links that have a successor:
|
|
134
182
|
# to the first link not archived, or the last (a loop a hand-edited
|
|
135
183
|
# file could make ends where it started); a :reader kind goes one hop
|
|
136
|
-
# at most.
|
|
184
|
+
# at most. Then, for a kind that would un-archive the link it reaches
|
|
185
|
+
# (a send: WakePolicy#starts_archived), a continue of that link in
|
|
186
|
+
# flight (ContinueIntent.in_flight) gives the successor's id: via
|
|
187
|
+
# :continuing while it is not saved yet, :head once it is
|
|
188
|
+
# (#continuing). A note needs no such check: written to the link
|
|
189
|
+
# before NoteMove.move it moves, after it the new link's worker adopts
|
|
190
|
+
# it (NoteMove.adoption).
|
|
137
191
|
# @return [Resolved]
|
|
138
192
|
def resolve(kind, id, state_dir:)
|
|
139
|
-
|
|
140
|
-
return Resolved.new(id: id, asked: id, via: :pinned) if
|
|
193
|
+
policy = WakePolicy.for(kind)
|
|
194
|
+
return Resolved.new(id: id, asked: id, via: :pinned) if policy.resolves == :pinned
|
|
141
195
|
|
|
142
|
-
|
|
143
|
-
|
|
196
|
+
resolved = walk(policy.resolves, id, state_dir)
|
|
197
|
+
(continuing(resolved, state_dir) if policy.starts_archived) || resolved
|
|
198
|
+
end
|
|
144
199
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
200
|
+
# The chain walk is SessionChain::Links': #first_live for :head (the
|
|
201
|
+
# send/note rule: the first live link, not the chain's end that a
|
|
202
|
+
# schedule fires at, Links#latest), its successor alone for :reader.
|
|
203
|
+
def walk(how, id, state_dir)
|
|
204
|
+
links = SessionChain::Links.new(state_dir: state_dir)
|
|
205
|
+
reached = how == :reader ? reader(links, id) : links.first_live(id)
|
|
206
|
+
reached == id ? Resolved.new(id: id, asked: id, via: :same) : Resolved.new(id: reached, asked: id, via: :head)
|
|
152
207
|
end
|
|
153
208
|
|
|
154
|
-
|
|
155
|
-
|
|
209
|
+
# The link that reads an archived +id+'s file: its successor, one hop.
|
|
210
|
+
def reader(links, id)
|
|
211
|
+
return id unless links.archived?(id)
|
|
212
|
+
|
|
213
|
+
links.next_of(id) || id
|
|
156
214
|
end
|
|
157
215
|
|
|
158
|
-
#
|
|
159
|
-
#
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
216
|
+
# +resolved+'s link being continued: its successor, via :continuing
|
|
217
|
+
# while it is not saved yet, via :head once it is (saved after the
|
|
218
|
+
# walk's listing: the walk still named the old link, which a send
|
|
219
|
+
# would un-archive beside it); else nil (none in flight). A continue
|
|
220
|
+
# that ends after this look is deliver_turn's to catch (a followed
|
|
221
|
+
# send to an archived link with a successor is refused, and chi send
|
|
222
|
+
# resolves again).
|
|
223
|
+
def continuing(resolved, state_dir)
|
|
224
|
+
successor = ContinueIntent.in_flight(resolved.id, state_dir: state_dir)
|
|
225
|
+
return nil if successor.nil?
|
|
226
|
+
|
|
227
|
+
via = Session.exist?(successor, state_dir: state_dir) ? :head : :continuing
|
|
228
|
+
Resolved.new(id: successor, asked: resolved.asked, via: via)
|
|
163
229
|
end
|
|
164
230
|
end
|
|
165
231
|
|