samagotchi 0.160.0 → 0.162.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 +110 -1
- data/docs/architecture.md +8 -4
- data/docs/cli.md +6 -4
- data/docs/context.md +10 -1
- data/docs/guardrails.md +12 -3
- data/docs/internals/block-renderers.md +3 -2
- data/docs/internals/bridge.md +82 -17
- data/docs/internals/bundles.md +24 -5
- data/docs/internals/inline-widgets.md +6 -3
- data/docs/internals/objectives.md +8 -2
- data/docs/internals/remote-sessions.md +2 -1
- data/docs/internals/web-frontend.md +1 -1
- data/docs/memory.md +4 -2
- data/docs/plugins.md +13 -4
- data/docs/releasing.md +7 -4
- data/docs/scheduling.md +37 -5
- data/docs/sessions.md +15 -12
- data/docs/testing.md +16 -2
- data/lib/samagotchi/bridge/card_store.rb +54 -16
- data/lib/samagotchi/bridge/ring_buffer.rb +11 -1
- data/lib/samagotchi/bridge/sse_writer.rb +8 -5
- data/lib/samagotchi/bridge/turn_accumulator.rb +7 -1
- data/lib/samagotchi/bridge/turn_drawings/synth.rb +555 -0
- data/lib/samagotchi/bridge/turn_drawings.rb +103 -7
- data/lib/samagotchi/bridge/turn_ranges.rb +175 -0
- data/lib/samagotchi/bridge.rb +127 -54
- data/lib/samagotchi/bridge_client.rb +9 -6
- data/lib/samagotchi/broadcast_command.rb +24 -8
- data/lib/samagotchi/bundle_command.rb +56 -6
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/child_reports.rb +29 -23
- data/lib/samagotchi/client_id.rb +2 -0
- data/lib/samagotchi/compat.rb +32 -0
- data/lib/samagotchi/context_candidates_log.rb +10 -1
- data/lib/samagotchi/context_command.rb +1 -1
- data/lib/samagotchi/context_move.rb +6 -1
- data/lib/samagotchi/engine/events.rb +12 -0
- data/lib/samagotchi/engine/plugins.rb +1 -1
- data/lib/samagotchi/engine/steering.rb +2 -2
- data/lib/samagotchi/guardrails/child_boundary.rb +37 -4
- data/lib/samagotchi/guardrails/protected_paths.rb +8 -5
- data/lib/samagotchi/guardrails/targets.rb +25 -6
- data/lib/samagotchi/live_versions.rb +7 -2
- data/lib/samagotchi/memory_bundle/asset_installer.rb +59 -12
- data/lib/samagotchi/memory_bundle/file_state.rb +11 -0
- data/lib/samagotchi/memory_bundle/index_updater.rb +7 -4
- data/lib/samagotchi/memory_bundle/installer.rb +79 -12
- data/lib/samagotchi/memory_bundle/provenance.rb +158 -6
- data/lib/samagotchi/memory_bundle/shipped_update.rb +2 -1
- data/lib/samagotchi/memory_bundle/status.rb +1 -1
- data/lib/samagotchi/memory_bundle/system_bundle.rb +2 -0
- data/lib/samagotchi/memory_bundle/uninstaller.rb +27 -13
- data/lib/samagotchi/memory_orphans.rb +40 -9
- data/lib/samagotchi/note_command.rb +9 -1
- data/lib/samagotchi/note_delivery.rb +34 -24
- data/lib/samagotchi/note_move.rb +205 -0
- data/lib/samagotchi/objectives_command.rb +3 -1
- data/lib/samagotchi/owner_lock.rb +7 -1
- data/lib/samagotchi/plugin/sessions.rb +8 -1
- data/lib/samagotchi/project_id_clash.rb +1 -1
- data/lib/samagotchi/project_init.rb +3 -3
- data/lib/samagotchi/review_start.rb +5 -3
- data/lib/samagotchi/schedule_command.rb +12 -0
- data/lib/samagotchi/schedules.rb +286 -48
- data/lib/samagotchi/send_command.rb +2 -15
- data/lib/samagotchi/session.rb +42 -5
- data/lib/samagotchi/session_access/local.rb +45 -17
- data/lib/samagotchi/session_access/remote.rb +11 -4
- data/lib/samagotchi/session_access/web_bridge_client.rb +2 -2
- data/lib/samagotchi/session_access.rb +16 -3
- data/lib/samagotchi/session_continue.rb +155 -21
- data/lib/samagotchi/session_delete_command.rb +3 -1
- data/lib/samagotchi/session_inbox.rb +41 -1
- data/lib/samagotchi/session_inbox_delivery.rb +254 -0
- data/lib/samagotchi/session_manager/delivery.rb +66 -12
- data/lib/samagotchi/session_manager/emptiness.rb +7 -2
- data/lib/samagotchi/session_manager/lifecycle.rb +2 -1
- data/lib/samagotchi/session_manager/listing.rb +4 -1
- data/lib/samagotchi/session_manager/removal.rb +130 -26
- data/lib/samagotchi/session_manager/spawn.rb +164 -104
- data/lib/samagotchi/session_manager.rb +1 -0
- data/lib/samagotchi/session_retention.rb +53 -23
- data/lib/samagotchi/session_setup.rb +27 -7
- data/lib/samagotchi/starting_marker.rb +93 -24
- data/lib/samagotchi/state_format.rb +18 -7
- data/lib/samagotchi/system_one/runs.rb +6 -1
- data/lib/samagotchi/system_one/runs_store.rb +18 -14
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +7 -4
- data/lib/samagotchi/terminal_ui/attached_loop/composer.rb +5 -2
- data/lib/samagotchi/terminal_ui/attached_loop.rb +5 -0
- data/lib/samagotchi/terminal_ui/join_replay.rb +34 -128
- data/lib/samagotchi/terminal_ui/version_lines.rb +4 -2
- data/lib/samagotchi/terminal_ui.rb +14 -4
- data/lib/samagotchi/tools/delegate.rb +9 -4
- data/lib/samagotchi/tools/schedule_task.rb +5 -3
- data/lib/samagotchi/tools/send_note.rb +10 -4
- data/lib/samagotchi/turn_state.rb +108 -4
- data/lib/samagotchi/update_command.rb +7 -2
- data/lib/samagotchi/verify_fresh.rb +27 -9
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +1 -1
- data/lib/samagotchi/web/message_display.rb +5 -1
- data/lib/samagotchi/web/public/app.js +2 -7
- data/lib/samagotchi/web/public/block_frame.js +5 -1
- data/lib/samagotchi/web/public/chains.js +4 -4
- data/lib/samagotchi/web/public/data.js +18 -2
- data/lib/samagotchi/web/public/info_bar_model.js +1 -1
- data/lib/samagotchi/web/public/session_fields.js +3 -2
- data/lib/samagotchi/web/public/session_state.js +2 -0
- data/lib/samagotchi/web/public/timing.js +17 -6
- data/lib/samagotchi/web/public/turn_block.js +6 -2
- data/lib/samagotchi/web/public/update_notice.js +2 -2
- data/lib/samagotchi/web/public/widget_walk.js +4 -2
- data/lib/samagotchi/web/session_access_routes.rb +12 -8
- data/lib/samagotchi/web/session_control.rb +9 -3
- data/lib/samagotchi/web/session_create.rb +8 -4
- data/lib/samagotchi/web/session_hub.rb +50 -5
- data/lib/samagotchi/web/session_show.rb +22 -11
- data/lib/samagotchi/web/session_summary.rb +5 -2
- data/lib/samagotchi/worker.rb +45 -7
- data/lib/samagotchi/worker_boot.rb +22 -11
- data/lib/samagotchi/worker_inbound.rb +41 -8
- data/lib/samagotchi/worker_link.rb +4 -1
- data/lib/samagotchi/worker_presence.rb +106 -0
- data/lib/samagotchi/worker_sidecar.rb +3 -4
- metadata +7 -1
|
@@ -4,7 +4,9 @@ require "json"
|
|
|
4
4
|
require "fileutils"
|
|
5
5
|
require_relative "../atomic_file"
|
|
6
6
|
require_relative "../events"
|
|
7
|
+
require_relative "../state_format"
|
|
7
8
|
require_relative "card_places"
|
|
9
|
+
require_relative "turn_notice"
|
|
8
10
|
|
|
9
11
|
module Samagotchi
|
|
10
12
|
class Bridge
|
|
@@ -62,15 +64,23 @@ module Samagotchi
|
|
|
62
64
|
# count becomes +base_since+, and new openings add to it (#seed_v1).
|
|
63
65
|
class CardStore
|
|
64
66
|
CAPACITY = 20
|
|
65
|
-
|
|
67
|
+
FORMAT_VERSION = 2
|
|
68
|
+
FORMAT = StateFormat.new(
|
|
69
|
+
version: FORMAT_VERSION,
|
|
70
|
+
members: [StateFormat.required(:type), StateFormat.extra(:extra, in_version: FORMAT_VERSION)]
|
|
71
|
+
)
|
|
72
|
+
# A question's events (TurnNotice's too): one :question entry, never
|
|
73
|
+
# pushed out while open.
|
|
74
|
+
QUESTION_EVENTS = %i[question_requested question_answered question_cancelled].freeze
|
|
66
75
|
# Capped apart from the notices.
|
|
67
76
|
LLM_CONTEXT_TYPES = %i[llm_context_edited].freeze
|
|
68
|
-
|
|
77
|
+
# A turn's rows (TurnNotice::FIELDS, the fields a join replays them
|
|
78
|
+
# with) other than questions and ✂ rows, plus the load warnings.
|
|
79
|
+
NOTICE_TYPES = (TurnNotice::FIELDS.keys - QUESTION_EVENTS - LLM_CONTEXT_TYPES + %i[guardrail_warning]).freeze
|
|
69
80
|
FILE = "cards.json"
|
|
70
81
|
# The events after which the file is written again: the ones that
|
|
71
82
|
# change an entry or the turn log.
|
|
72
|
-
SAVED_ON = (%i[turn_started card
|
|
73
|
-
question_cancelled question_relay] + Events::TURN_END).freeze
|
|
83
|
+
SAVED_ON = (%i[turn_started card question_relay] + TurnNotice::FIELDS.keys + Events::TURN_END).uniq.freeze
|
|
74
84
|
# What only the store reads: never listed.
|
|
75
85
|
INTERNAL_FIELDS = %i[turn_id continued base_since past_idless after_opening during after_failed command].freeze
|
|
76
86
|
|
|
@@ -98,6 +108,8 @@ module Samagotchi
|
|
|
98
108
|
@mutex = Mutex.new
|
|
99
109
|
@entries = []
|
|
100
110
|
@turn_log = []
|
|
111
|
+
@file_extra = {}
|
|
112
|
+
@format_read_only = false
|
|
101
113
|
@running_turn = nil
|
|
102
114
|
@continuing = false
|
|
103
115
|
@failed_turn = nil
|
|
@@ -180,13 +192,14 @@ module Samagotchi
|
|
|
180
192
|
|
|
181
193
|
def save
|
|
182
194
|
return unless @path
|
|
195
|
+
return if @format_read_only
|
|
183
196
|
|
|
184
197
|
entries = @entries.reject { |entry| entry[:type] == :guardrail_warning }
|
|
185
198
|
# A session that never showed one gets no file.
|
|
186
199
|
return if entries.empty? && !File.exist?(@path)
|
|
187
200
|
|
|
188
201
|
FileUtils.mkdir_p(File.dirname(@path))
|
|
189
|
-
saved =
|
|
202
|
+
saved = @file_extra.merge("version" => FORMAT_VERSION, "turn_log" => pruned_log(entries), "entries" => entries)
|
|
190
203
|
saved["failed_turn"] = @failed_turn if @failed_turn
|
|
191
204
|
AtomicFile.write(@path, JSON.generate(saved))
|
|
192
205
|
rescue StandardError
|
|
@@ -222,15 +235,34 @@ module Samagotchi
|
|
|
222
235
|
return unless @path && File.file?(@path)
|
|
223
236
|
|
|
224
237
|
data = JSON.parse(File.read(@path), symbolize_names: true)
|
|
225
|
-
return unless data.is_a?(Hash)
|
|
238
|
+
return unless data.is_a?(Hash)
|
|
239
|
+
|
|
240
|
+
format_data = data.transform_keys(&:to_s)
|
|
241
|
+
format_data["entries"] = if data[:entries].is_a?(Array)
|
|
242
|
+
data[:entries].map { |entry| entry.is_a?(Hash) ? entry.transform_keys(&:to_s) : entry }
|
|
243
|
+
else
|
|
244
|
+
data[:entries]
|
|
245
|
+
end
|
|
246
|
+
document = FORMAT.read(format_data, collection: "entries")
|
|
247
|
+
if document.version > FORMAT_VERSION
|
|
248
|
+
raise StateFormat::InvalidDocument,
|
|
249
|
+
"cards.json has format version #{document.version}, newer than supported #{FORMAT_VERSION}; update chi before writing it"
|
|
250
|
+
end
|
|
251
|
+
@file_extra = document.extra.except("version", "entries", "turns", "turn_log", "failed_turn")
|
|
252
|
+
return unless data[:entries].is_a?(Array)
|
|
226
253
|
|
|
227
254
|
if data[:turn_log].is_a?(Array)
|
|
228
255
|
@turn_log = data[:turn_log].grep(String)
|
|
229
256
|
@failed_turn = data[:failed_turn] if data[:failed_turn].is_a?(String)
|
|
230
|
-
@entries = seeded(
|
|
257
|
+
@entries = seeded(document.entries.map { |entry| entry.to_h.transform_keys(&:to_sym) })
|
|
231
258
|
elsif data[:turns].is_a?(Integer)
|
|
232
259
|
seed_v1(data)
|
|
233
260
|
end
|
|
261
|
+
rescue StateFormat::InvalidDocument => e
|
|
262
|
+
@format_read_only = true
|
|
263
|
+
@turn_log = []
|
|
264
|
+
@entries = []
|
|
265
|
+
Log.warn(:bridge, "card_store_unreadable", error: e.message)
|
|
234
266
|
rescue StandardError
|
|
235
267
|
@turn_log = []
|
|
236
268
|
@entries = []
|
|
@@ -292,11 +324,8 @@ module Samagotchi
|
|
|
292
324
|
# that turn: it anchors before the turn.
|
|
293
325
|
when :guardrail_warning then push({ type: :guardrail_warning, message: event[:message], label: event[:label] }
|
|
294
326
|
.compact.merge(in_turn: false, turn_id: @running_turn ? @turn_log[-2] : @turn_log.last))
|
|
295
|
-
when :empty_answer_retry
|
|
296
|
-
when :
|
|
297
|
-
when :llm_context_edited then add_turn_row(Marshal.load(Marshal.dump(event.slice(*LLM_CONTEXT_FIELDS)))) if @running_turn
|
|
298
|
-
when :question_requested
|
|
299
|
-
add_turn_row({ type: :question, pending_question: Marshal.load(Marshal.dump(event[:pending_question])) }) if @running_turn
|
|
327
|
+
when :empty_answer_retry, :steer_cut, :llm_context_edited then add_turn_row(notice_row(event)) if @running_turn
|
|
328
|
+
when :question_requested then add_turn_row(notice_row(event).merge(type: :question)) if @running_turn
|
|
300
329
|
when :question_answered then resolve_question(event[:id], answer: event[:answer])
|
|
301
330
|
when :question_cancelled then resolve_question(event[:id], cancelled: true, reason: event[:reason])
|
|
302
331
|
when :question_relay then mark_relay(event[:id], event[:relayed_to])
|
|
@@ -338,18 +367,27 @@ module Samagotchi
|
|
|
338
367
|
# (a command's answer from another thread; a turn's proposal its own
|
|
339
368
|
# button's command replaced, which nobody typed).
|
|
340
369
|
old = @entries[index]
|
|
341
|
-
|
|
342
|
-
|
|
370
|
+
# Its step too (iteration, calls): the first one's place, as live.
|
|
371
|
+
@entries[index] = card.except(:command).merge(updated: true, **old.slice(:in_turn, :turn_id, :iteration, :calls, :during, :continued,
|
|
372
|
+
:base_since, :past_idless, :after_opening, :after_failed,
|
|
373
|
+
:command))
|
|
343
374
|
else
|
|
344
375
|
entry = card.merge(in_turn: event[:in_turn] ? true : false, turn_id: anchor)
|
|
376
|
+
# The step it came in (a turn's row's place): where a drawing made
|
|
377
|
+
# from the turn's messages draws it (TurnDrawings::Synth).
|
|
378
|
+
entry.merge!({ iteration: @iteration, calls: @calls }.compact) if @running_turn && event[:in_turn]
|
|
345
379
|
entry[:during] = true if @running_turn && !event[:in_turn]
|
|
346
380
|
entry[:continued] = true if @running_turn && event[:in_turn] && @continuing
|
|
347
381
|
push(entry)
|
|
348
382
|
end
|
|
349
383
|
end
|
|
350
384
|
|
|
385
|
+
# A TurnNotice event as the store keeps it: its FIELDS, deep-copied,
|
|
386
|
+
# and its type as a Symbol.
|
|
387
|
+
def notice_row(event) = TurnNotice.slice(event).merge(type: event[:type])
|
|
388
|
+
|
|
351
389
|
def add_notice(event)
|
|
352
|
-
notice = event
|
|
390
|
+
notice = notice_row(event)
|
|
353
391
|
if event[:between_turns] || !@running_turn
|
|
354
392
|
push(notice.merge(in_turn: false, turn_id: anchor))
|
|
355
393
|
else
|
|
@@ -359,7 +397,7 @@ module Samagotchi
|
|
|
359
397
|
|
|
360
398
|
# A row of the running turn's current step.
|
|
361
399
|
def add_turn_row(row)
|
|
362
|
-
entry = row.merge(in_turn: true, turn_id: @running_turn, iteration: @iteration, calls: @calls).compact
|
|
400
|
+
entry = row.merge(in_turn: true, turn_id: @running_turn, iteration: @iteration || row[:iteration], calls: @calls).compact
|
|
363
401
|
entry[:continued] = true if @continuing
|
|
364
402
|
push(entry)
|
|
365
403
|
end
|
|
@@ -18,13 +18,17 @@ module Samagotchi
|
|
|
18
18
|
@capacity = [1, capacity].max
|
|
19
19
|
@mutex = Monitor.new
|
|
20
20
|
@events = [] # Array<{seq:, data:}>
|
|
21
|
+
@dropped = false
|
|
21
22
|
end
|
|
22
23
|
|
|
23
24
|
# Append +data+ keyed by +seq+ (drop oldest beyond capacity).
|
|
24
25
|
def push(seq:, data:)
|
|
25
26
|
@mutex.synchronize do
|
|
26
27
|
@events << { seq: seq, data: data }
|
|
27
|
-
|
|
28
|
+
while @events.size > @capacity
|
|
29
|
+
@events.shift
|
|
30
|
+
@dropped = true
|
|
31
|
+
end
|
|
28
32
|
nil
|
|
29
33
|
end
|
|
30
34
|
end
|
|
@@ -45,6 +49,12 @@ module Samagotchi
|
|
|
45
49
|
@mutex.synchronize { @events.first&.[](:seq) }
|
|
46
50
|
end
|
|
47
51
|
|
|
52
|
+
# @return [Boolean] whether a push has ever evicted an event: the window
|
|
53
|
+
# no longer starts where the capture did.
|
|
54
|
+
def dropped?
|
|
55
|
+
@mutex.synchronize { @dropped }
|
|
56
|
+
end
|
|
57
|
+
|
|
48
58
|
def empty?
|
|
49
59
|
@mutex.synchronize { @events.empty? }
|
|
50
60
|
end
|
|
@@ -161,16 +161,19 @@ module Samagotchi
|
|
|
161
161
|
def replay!(io, snapshot_seq)
|
|
162
162
|
from_seq = @high_water
|
|
163
163
|
oldest = @ring.oldest_seq
|
|
164
|
-
if @foreign_cursor || from_seq > snapshot_seq || (
|
|
164
|
+
if @foreign_cursor || from_seq > snapshot_seq || (oldest && from_seq < oldest - 1 && (from_seq.positive? || @ring.dropped?))
|
|
165
165
|
# Cursor is from another worker (epoch), ahead of the engine (a
|
|
166
166
|
# worker restarted, plain cursor) or behind the
|
|
167
167
|
# ring's buffered window (overflow): the server cannot replay the
|
|
168
168
|
# gap, so it emits a reset marker and the client re-syncs. A cursor
|
|
169
169
|
# at oldest - 1 has seen everything before the window, so it
|
|
170
|
-
# replays. A cold
|
|
171
|
-
#
|
|
172
|
-
#
|
|
173
|
-
#
|
|
170
|
+
# replays. A cold connect (from_seq == 0) is behind the window too
|
|
171
|
+
# once the ring has evicted an event (it would start mid-turn), so
|
|
172
|
+
# it resets; on a ring that never evicted (its first seq may be
|
|
173
|
+
# above 1: events before the Bridge started) it replays the whole
|
|
174
|
+
# window. A fully caught-up cursor (from_seq == snapshot_seq)
|
|
175
|
+
# is NOT a reset case — it gets an empty replay and holds for live
|
|
176
|
+
# events.
|
|
174
177
|
emit_reset(io)
|
|
175
178
|
else
|
|
176
179
|
@ring.events_in_range(after_seq: from_seq, to_seq: snapshot_seq).each { |record| write_event(io, record[:data]) }
|
|
@@ -30,8 +30,12 @@ module Samagotchi
|
|
|
30
30
|
# TurnDrawings#append). It runs under this accumulator's mutex and the
|
|
31
31
|
# SessionObserver's lock: it must never call back in here. An error is
|
|
32
32
|
# logged and the fold goes on.
|
|
33
|
+
# @param raise_errors [Boolean] a fold that raises raises out of #call
|
|
34
|
+
# (a drawing made from messages: TurnDrawings::Synth), never for a
|
|
35
|
+
# live turn's observer
|
|
33
36
|
def initialize(max_output_chars: ToolOutputCut::WEB_PREVIEW_CHARS,
|
|
34
|
-
clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }, on_turn_end: nil)
|
|
37
|
+
clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }, on_turn_end: nil, raise_errors: false)
|
|
38
|
+
@raise_errors = raise_errors
|
|
35
39
|
@max_output_chars = max_output_chars
|
|
36
40
|
@clock = clock
|
|
37
41
|
@on_turn_end = on_turn_end
|
|
@@ -50,6 +54,8 @@ module Samagotchi
|
|
|
50
54
|
def call(event)
|
|
51
55
|
@mutex.synchronize { fold(event) }
|
|
52
56
|
rescue StandardError
|
|
57
|
+
raise if @raise_errors
|
|
58
|
+
|
|
53
59
|
nil # never break the running turn
|
|
54
60
|
end
|
|
55
61
|
|