samagotchi 0.182.0 → 0.184.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 +107 -1
- data/bin/chi +3 -1
- data/docs/architecture.md +22 -11
- data/docs/bundle-authoring.md +1 -1
- data/docs/bundles.md +103 -4
- data/docs/cli.md +28 -6
- data/docs/configuration.md +4 -2
- data/docs/guardrails.md +16 -1
- data/docs/internals/block-renderers.md +58 -3
- data/docs/internals/bridge.md +14 -6
- data/docs/internals/bundles.md +26 -5
- data/docs/internals/engine.md +20 -9
- data/docs/internals/participants.md +1 -1
- data/docs/internals/prompt-caching.md +8 -1
- data/docs/internals/remote-sessions.md +40 -5
- data/docs/internals/web-frontend.md +96 -31
- data/docs/internals/web-push.md +11 -0
- data/docs/internals/working-set.md +239 -7
- data/docs/mcp-server.md +84 -0
- data/docs/memory.md +2 -1
- data/docs/plugins.md +309 -22
- data/docs/scheduling.md +14 -0
- data/docs/sessions.md +4 -4
- data/docs/sub-agent.md +9 -0
- data/docs/testing.md +3 -1
- data/docs/thin-clients.md +165 -0
- data/lib/samagotchi/bridge/pending_card.rb +4 -3
- data/lib/samagotchi/bridge/sse_writer.rb +16 -15
- data/lib/samagotchi/bridge.rb +59 -68
- data/lib/samagotchi/bridge_client.rb +12 -4
- data/lib/samagotchi/bundle_command/api_calls.rb +1 -1
- data/lib/samagotchi/bundle_command/check.rb +55 -20
- data/lib/samagotchi/bundle_command.rb +50 -21
- data/lib/samagotchi/bundle_manifest.rb +105 -4
- data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/client.rb +260 -0
- data/lib/samagotchi/bundles/mcp/content.rb +107 -0
- data/lib/samagotchi/bundles/mcp/http_client.rb +432 -0
- data/lib/samagotchi/bundles/mcp/manifest.yml +13 -5
- data/lib/samagotchi/bundles/mcp/oauth.rb +814 -0
- data/lib/samagotchi/bundles/mcp/plugin.rb +556 -574
- data/lib/samagotchi/bundles/mcp/tools.rb +232 -0
- data/lib/samagotchi/bundles/system/config_modification_protocol.md +10 -2
- data/lib/samagotchi/bundles/system/manifest.yml +3 -3
- data/lib/samagotchi/bundles/system/self_map.md +7 -2
- data/lib/samagotchi/cli/parent_wait.rb +5 -3
- data/lib/samagotchi/command_intake.rb +7 -0
- data/lib/samagotchi/commands/registry.rb +8 -3
- data/lib/samagotchi/continue_offer.rb +24 -15
- data/lib/samagotchi/engine/block_fix.rb +4 -4
- data/lib/samagotchi/engine/cards.rb +14 -5
- data/lib/samagotchi/engine/events.rb +10 -15
- data/lib/samagotchi/engine/models.rb +1 -1
- data/lib/samagotchi/engine/plugins.rb +3 -0
- data/lib/samagotchi/engine/recap.rb +1 -1
- data/lib/samagotchi/engine/session.rb +14 -6
- data/lib/samagotchi/engine/side_requests.rb +1 -1
- data/lib/samagotchi/engine/sign_in.rb +142 -0
- data/lib/samagotchi/engine/turn_end.rb +3 -3
- data/lib/samagotchi/engine/turn_helpers.rb +4 -3
- data/lib/samagotchi/engine.rb +43 -27
- data/lib/samagotchi/engine_snapshots.rb +1 -1
- data/lib/samagotchi/guardrail_wiring.rb +3 -1
- data/lib/samagotchi/guardrails/protected_paths.rb +51 -2
- data/lib/samagotchi/guardrails/verdict.rb +3 -2
- data/lib/samagotchi/hold_stop.rb +4 -4
- data/lib/samagotchi/idle_recap.rb +19 -11
- data/lib/samagotchi/idle_reminders.rb +7 -5
- data/lib/samagotchi/idle_scheduler.rb +7 -10
- data/lib/samagotchi/installed_bundles/dir.rb +37 -0
- data/lib/samagotchi/installed_bundles/manifest.rb +7 -1
- data/lib/samagotchi/installed_bundles/plugin_ref.rb +9 -5
- data/lib/samagotchi/installed_bundles/record.rb +3 -2
- data/lib/samagotchi/installed_bundles/standing.rb +3 -1
- data/lib/samagotchi/llm/http.rb +96 -5
- data/lib/samagotchi/mcp_command.rb +110 -0
- data/lib/samagotchi/mcp_server/http.rb +112 -0
- data/lib/samagotchi/mcp_server/protocol.rb +263 -0
- data/lib/samagotchi/mcp_server/stdio.rb +93 -0
- data/lib/samagotchi/mcp_server/text.rb +155 -0
- data/lib/samagotchi/mcp_server/tools.rb +246 -0
- data/lib/samagotchi/memory_bundle/asset_installer.rb +40 -12
- data/lib/samagotchi/memory_bundle/builder.rb +20 -2
- data/lib/samagotchi/memory_bundle/installed_copy.rb +1 -1
- data/lib/samagotchi/memory_bundle/installer.rb +16 -3
- data/lib/samagotchi/memory_bundle/provenance.rb +18 -1
- data/lib/samagotchi/memory_bundle/shas.rb +6 -4
- data/lib/samagotchi/memory_bundle/shipped_update.rb +7 -3
- data/lib/samagotchi/memory_bundle/status.rb +15 -6
- data/lib/samagotchi/memory_bundle/uninstaller.rb +9 -4
- data/lib/samagotchi/model_notes.rb +34 -12
- data/lib/samagotchi/plugin/api.rb +6 -1
- data/lib/samagotchi/plugin/card.rb +36 -11
- data/lib/samagotchi/plugin/context.rb +63 -6
- data/lib/samagotchi/plugin/http.rb +40 -15
- data/lib/samagotchi/plugin/http_errors.rb +15 -0
- data/lib/samagotchi/plugin/http_stream.rb +86 -0
- data/lib/samagotchi/plugin/http_transport.rb +58 -16
- data/lib/samagotchi/plugin/loader.rb +18 -11
- data/lib/samagotchi/plugin/secrets.rb +266 -0
- data/lib/samagotchi/plugin/sign_in.rb +392 -0
- data/lib/samagotchi/plugin/sse_reader.rb +103 -0
- data/lib/samagotchi/prompt_files.rb +11 -4
- data/lib/samagotchi/prompt_layout.rb +88 -0
- data/lib/samagotchi/prompt_part.rb +82 -20
- data/lib/samagotchi/queued_command.rb +28 -4
- data/lib/samagotchi/recap_setup.rb +13 -4
- data/lib/samagotchi/ref.rb +3 -2
- data/lib/samagotchi/relay_verifier.rb +8 -7
- data/lib/samagotchi/relay_watcher.rb +4 -4
- data/lib/samagotchi/reply_wait.rb +6 -3
- data/lib/samagotchi/run_options.rb +2 -1
- data/lib/samagotchi/schedule_command.rb +18 -4
- data/lib/samagotchi/schedules.rb +40 -8
- data/lib/samagotchi/send_command.rb +54 -41
- data/lib/samagotchi/session_access/ask.rb +285 -0
- data/lib/samagotchi/session_access/contract.rb +5 -0
- data/lib/samagotchi/session_access/exchange/token.rb +101 -0
- data/lib/samagotchi/session_access/exchange.rb +176 -0
- data/lib/samagotchi/session_access/local.rb +34 -6
- data/lib/samagotchi/session_access/remote.rb +4 -1
- data/lib/samagotchi/session_command_catalog.rb +10 -0
- data/lib/samagotchi/session_commands.rb +17 -0
- data/lib/samagotchi/session_config.rb +4 -4
- data/lib/samagotchi/session_inbox.rb +15 -0
- data/lib/samagotchi/session_manager/delivery.rb +8 -4
- data/lib/samagotchi/session_observer.rb +27 -0
- data/lib/samagotchi/steer_cut.rb +4 -3
- data/lib/samagotchi/system_prompt.rb +196 -161
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +4 -1
- data/lib/samagotchi/terminal_ui/formatting.rb +15 -0
- data/lib/samagotchi/testing/boundary.rb +4 -0
- data/lib/samagotchi/testing/bundle.rb +48 -2
- data/lib/samagotchi/testing/fake_http.rb +60 -15
- data/lib/samagotchi/testing/host.rb +4 -1
- data/lib/samagotchi/testing/recorder.rb +45 -5
- data/lib/samagotchi/tools/memory.rb +11 -3
- data/lib/samagotchi/tools/tool_index.rb +14 -3
- data/lib/samagotchi/turn_stages/warmup.rb +2 -2
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +9 -8
- data/lib/samagotchi/web/ask_routes.rb +362 -0
- data/lib/samagotchi/web/mcp_route.rb +91 -0
- data/lib/samagotchi/web/public/app.js +2 -3
- data/lib/samagotchi/web/public/block_frame.js +72 -11
- data/lib/samagotchi/web/public/block_host.js +44 -8
- data/lib/samagotchi/web/public/block_regions.js +126 -0
- data/lib/samagotchi/web/public/card.js +22 -9
- data/lib/samagotchi/web/public/card_actions.js +21 -0
- data/lib/samagotchi/web/public/card_kinds.js +1 -1
- data/lib/samagotchi/web/public/card_registry.js +3 -2
- data/lib/samagotchi/web/public/data.js +15 -12
- data/lib/samagotchi/web/public/delete_sheet.js +231 -169
- data/lib/samagotchi/web/public/diagram_wiring.js +28 -0
- data/lib/samagotchi/web/public/format.js +3 -0
- data/lib/samagotchi/web/public/index.html +52 -7
- data/lib/samagotchi/web/public/more_sheet.js +6 -4
- data/lib/samagotchi/web/public/picture_regions.js +243 -0
- data/lib/samagotchi/web/public/picture_viewer.js +113 -12
- data/lib/samagotchi/web/public/refs.js +5 -3
- data/lib/samagotchi/web/public/region_hit.js +144 -0
- data/lib/samagotchi/web/public/removal_sheet_model.js +274 -0
- data/lib/samagotchi/web/public/running_tasks.js +3 -17
- data/lib/samagotchi/web/public/session_actions.js +96 -100
- data/lib/samagotchi/web/public/working_set_view.js +167 -21
- data/lib/samagotchi/web/public/ws_part_peek.js +277 -0
- data/lib/samagotchi/web/public/ws_peek.js +3 -1
- data/lib/samagotchi/web/public/ws_preview.css +30 -0
- data/lib/samagotchi/web/public/ws_reader.js +7 -3
- data/lib/samagotchi/web/public/ws_sections.js +37 -7
- data/lib/samagotchi/web/push/notifier.rb +33 -6
- data/lib/samagotchi/web/server.rb +1 -1
- data/lib/samagotchi/web/session_access_routes.rb +14 -11
- data/lib/samagotchi/web/session_control.rb +31 -9
- data/lib/samagotchi/web/session_create.rb +2 -2
- data/lib/samagotchi/web/session_hub.rb +26 -4
- data/lib/samagotchi/web/working_set_routes.rb +93 -5
- data/lib/samagotchi/worker/child_side.rb +3 -2
- data/lib/samagotchi/worker/command_queue.rb +13 -13
- data/lib/samagotchi/worker/leave.rb +3 -3
- data/lib/samagotchi/worker/scratch.rb +6 -6
- data/lib/samagotchi/worker/signal_grace.rb +6 -5
- data/lib/samagotchi/worker/startup.rb +3 -3
- data/lib/samagotchi/worker/wake_turns.rb +4 -4
- data/lib/samagotchi/worker.rb +32 -21
- data/lib/samagotchi/worker_exit_claim.rb +1 -1
- data/lib/samagotchi/worker_idle_exit.rb +18 -9
- data/lib/samagotchi/worker_inbound.rb +12 -7
- data/lib/samagotchi/worker_link.rb +2 -2
- data/lib/samagotchi/worker_participant_gate.rb +9 -7
- data/lib/samagotchi/worker_task_wakes.rb +7 -5
- data/lib/samagotchi/working_set.rb +162 -90
- data/lib/samagotchi/working_set_locator.rb +221 -0
- data/lib/samagotchi/working_set_own_changes.rb +221 -0
- data/lib/samagotchi/working_set_part.rb +281 -0
- metadata +36 -1
data/lib/samagotchi/worker.rb
CHANGED
|
@@ -276,9 +276,16 @@ module Samagotchi
|
|
|
276
276
|
# runs from the leave) and restored into it after.
|
|
277
277
|
carried = take_carried_reminders
|
|
278
278
|
@engine = build_engine
|
|
279
|
+
# The Engine's event log, turn state, question desk and steer cut,
|
|
280
|
+
# handed to the Bridge and the collaborators (read here, not in
|
|
281
|
+
# #start_bridge, whose rescue would hide a missing one).
|
|
282
|
+
@event_log = @engine.event_log
|
|
283
|
+
@turn_state = @engine.turn_state
|
|
284
|
+
@question_desk = @engine.question_desk
|
|
285
|
+
@steer_cut = @engine.steer_cut
|
|
279
286
|
restore_carried_reminders(carried)
|
|
280
287
|
@config_restart_unsubscribe = ConfigFile.watch_restart_settings!(process: :worker) do |message|
|
|
281
|
-
@
|
|
288
|
+
@event_log.announce(type: :hook_notice, hook: "config", text: message, level: :warn, between_turns: true)
|
|
282
289
|
end
|
|
283
290
|
prune_tombstones
|
|
284
291
|
settle_interrupted_turn
|
|
@@ -289,7 +296,7 @@ module Samagotchi
|
|
|
289
296
|
@engine.session = @session
|
|
290
297
|
# May this participant act (loop thread only): a turn as it starts,
|
|
291
298
|
# the drain, the mid-turn stop, the Continue.
|
|
292
|
-
@participant_gate = WorkerParticipantGate.new(engine: @engine, session: @session, session_id: @session_id,
|
|
299
|
+
@participant_gate = WorkerParticipantGate.new(engine: @engine, event_log: @event_log, session: @session, session_id: @session_id,
|
|
293
300
|
state_dir: @state_dir, session_dir: @session_dir,
|
|
294
301
|
hook: Engine::PARTICIPANTS_HOOK)
|
|
295
302
|
# Who is in the review as the worker starts: no notice for them
|
|
@@ -304,7 +311,8 @@ module Samagotchi
|
|
|
304
311
|
@turn_flow = TurnFlow.new(engine: @engine)
|
|
305
312
|
# Its question's answer is queued as the matching /continue (the
|
|
306
313
|
# Bridge exists by the time anyone answers).
|
|
307
|
-
@continue_offer = ContinueOffer.new(engine: @engine,
|
|
314
|
+
@continue_offer = ContinueOffer.new(engine: @engine, event_log: @event_log, turn_state: @turn_state,
|
|
315
|
+
question_desk: @question_desk, turn_flow: @turn_flow, run_turn: method(:run_engine_turn),
|
|
308
316
|
max_iterations: method(:max_iterations),
|
|
309
317
|
participant_refused: @participant_gate.method(:refusal),
|
|
310
318
|
save: -> { save_or_log(:command) { save_session } },
|
|
@@ -319,11 +327,11 @@ module Samagotchi
|
|
|
319
327
|
@turn_end_seq = 0
|
|
320
328
|
# The background tasks this session started and hasn't seen finish:
|
|
321
329
|
# one that finishes unseen wakes the idle session (#run_task_wake).
|
|
322
|
-
@task_wakes = WorkerTaskWakes.new(engine: @engine, session: @session, wakes: @wakes,
|
|
330
|
+
@task_wakes = WorkerTaskWakes.new(engine: @engine, event_log: @event_log, session: @session, wakes: @wakes,
|
|
323
331
|
awaiting_continue: -> { @turn_flow.awaiting_continue? },
|
|
324
332
|
idle: -> { nothing_queued? && !@exit_claim.taken? },
|
|
325
333
|
save: -> { save_or_log(:task_notes) { save_session } })
|
|
326
|
-
@
|
|
334
|
+
@event_log.subscribe(observer: lambda { |event|
|
|
327
335
|
if Events::TURN_END.include?(event[:type])
|
|
328
336
|
@turn_end_seq = event[:event_seq]
|
|
329
337
|
@turn_end_type = event[:type]
|
|
@@ -345,7 +353,7 @@ module Samagotchi
|
|
|
345
353
|
peer_sources: @engine.peer_sources, peer_turns: -> { @wakes.peer_turns })
|
|
346
354
|
# What comes in between turns, and how each kind is saved.
|
|
347
355
|
@inbound = WorkerInbound.new(session: @session, state_dir: @state_dir, session_dir: @session_dir, engine: @engine,
|
|
348
|
-
context_absorber: @context_absorber, wakes: @wakes,
|
|
356
|
+
event_log: @event_log, turn_state: @turn_state, context_absorber: @context_absorber, wakes: @wakes,
|
|
349
357
|
awaiting_continue: -> { @turn_flow.awaiting_continue? },
|
|
350
358
|
stopped: -> { stopped_on_disk? },
|
|
351
359
|
queue_command: lambda { |line, client_id, after_file, enqueued_id, author: nil|
|
|
@@ -369,7 +377,8 @@ module Samagotchi
|
|
|
369
377
|
# The Bridge (UIs and clients reach the worker from here on), then the
|
|
370
378
|
# idle-exit policy.
|
|
371
379
|
def serve
|
|
372
|
-
@bridge = start_bridge(engine: @engine,
|
|
380
|
+
@bridge = start_bridge(engine: @engine, event_log: @event_log, turn_state: @turn_state, question_desk: @question_desk,
|
|
381
|
+
steer_cut: @steer_cut, state_dir: @state_dir, session_id: @session_id,
|
|
373
382
|
on_input: -> { @waker.wake },
|
|
374
383
|
on_command: method(:on_command),
|
|
375
384
|
on_exit_request: method(:exit_request),
|
|
@@ -388,7 +397,7 @@ module Samagotchi
|
|
|
388
397
|
@engine.announce_load_events!
|
|
389
398
|
announce_session_warnings
|
|
390
399
|
@idle_exit = WorkerIdleExit.new(
|
|
391
|
-
engine: @engine, bridge: @bridge,
|
|
400
|
+
engine: @engine, turn_state: @turn_state, question_desk: @question_desk, bridge: @bridge,
|
|
392
401
|
timeout_minutes: @idle_exit_minutes || config_idle_exit_minutes,
|
|
393
402
|
input_pending: method(:input_pending?),
|
|
394
403
|
awaiting_continue: -> { @turn_flow.awaiting_continue? },
|
|
@@ -403,8 +412,8 @@ module Samagotchi
|
|
|
403
412
|
# them so "stop them and exit" stops them too.
|
|
404
413
|
delegates: method(:live_delegates)
|
|
405
414
|
)
|
|
406
|
-
@hold_stop = HoldStop.new(
|
|
407
|
-
state_dir: @state_dir)
|
|
415
|
+
@hold_stop = HoldStop.new(reminder_store: @engine.reminder_store, task_watch: @task_wakes.watch,
|
|
416
|
+
delegates: method(:live_delegates), state_dir: @state_dir)
|
|
408
417
|
end
|
|
409
418
|
|
|
410
419
|
# Start the in-process Bridge transport for this worker. The bridge is
|
|
@@ -417,11 +426,13 @@ module Samagotchi
|
|
|
417
426
|
# (the worker degrades: turns still flow through the input-dir loop, but
|
|
418
427
|
# there is no live SSE or in-process cancel/answer).
|
|
419
428
|
# @param on_input [#call, nil] called after the Bridge queues a turn
|
|
420
|
-
def start_bridge(engine:,
|
|
421
|
-
|
|
429
|
+
def start_bridge(engine:, event_log:, turn_state:, question_desk:, steer_cut:, state_dir:, session_id:, on_input: nil,
|
|
430
|
+
on_command: nil, on_exit_request: nil, exit_discards: nil, exit_stopped: nil, on_holds: nil,
|
|
431
|
+
on_holds_stop: nil, on_activity: nil)
|
|
422
432
|
require_relative "bridge"
|
|
423
433
|
Samagotchi::Bridge.new(
|
|
424
|
-
engine: engine,
|
|
434
|
+
engine: engine, event_log: event_log, turn_state: turn_state, question_desk: question_desk, steer_cut: steer_cut,
|
|
435
|
+
state_dir: state_dir, session_id: session_id, input_format: SessionInbox::INPUT_FORMAT,
|
|
425
436
|
on_input: on_input, on_command: on_command, on_exit_request: on_exit_request, exit_discards: exit_discards,
|
|
426
437
|
exit_stopped: exit_stopped, on_holds: on_holds, on_holds_stop: on_holds_stop, on_activity: on_activity
|
|
427
438
|
).start
|
|
@@ -441,7 +452,7 @@ module Samagotchi
|
|
|
441
452
|
# @return [Hash]
|
|
442
453
|
def activity_snapshot
|
|
443
454
|
{
|
|
444
|
-
turn_running: @
|
|
455
|
+
turn_running: @turn_state.running?,
|
|
445
456
|
input_pending: input_pending?,
|
|
446
457
|
awaiting_continue: @turn_flow.awaiting_continue?,
|
|
447
458
|
open_streams: @bridge&.open_streams.to_i,
|
|
@@ -518,7 +529,7 @@ module Samagotchi
|
|
|
518
529
|
return if run_wake_turn
|
|
519
530
|
return if run_task_wake
|
|
520
531
|
return if run_due_reminders
|
|
521
|
-
return left(:scratch_left) if scratch_unattended? && !@
|
|
532
|
+
return left(:scratch_left) if scratch_unattended? && !@turn_state.running? && leave_scratch
|
|
522
533
|
return left(:idle_exit) if @idle_exit.due? && leave_idle
|
|
523
534
|
|
|
524
535
|
leaving = @exit_claim.taken? && leave_on_request
|
|
@@ -799,14 +810,14 @@ module Samagotchi
|
|
|
799
810
|
# conversation as a turn note
|
|
800
811
|
# Not saved here: run_engine_turn saves after its block.
|
|
801
812
|
def restore_failed_turn(prompts, error: nil)
|
|
802
|
-
@
|
|
813
|
+
@event_log.synchronize do
|
|
803
814
|
next if @turn_flow.prompt_turn_failed(error: error) == :kept
|
|
804
815
|
|
|
805
816
|
prompts.each do |prompt, origin, images, anchors|
|
|
806
817
|
restored = { type: :prompt_restored, prompt: prompt, origin: origin.wire }
|
|
807
818
|
restored[:images] = images unless Array(images).empty?
|
|
808
819
|
restored.merge!(anchors.to_h) if anchors
|
|
809
|
-
@
|
|
820
|
+
@event_log.announce(restored)
|
|
810
821
|
end
|
|
811
822
|
end
|
|
812
823
|
end
|
|
@@ -841,7 +852,7 @@ module Samagotchi
|
|
|
841
852
|
begin
|
|
842
853
|
input = SessionInbox.read_input(claimed_file)
|
|
843
854
|
enqueued_id = input.origin.enqueued_id
|
|
844
|
-
if SessionInbox.input_taken?(@session_dir, @session, enqueued_id) || !@
|
|
855
|
+
if SessionInbox.input_taken?(@session_dir, @session, enqueued_id) || !@turn_state.claim_enqueued_id(enqueued_id)
|
|
845
856
|
Log.info(:worker, "duplicate_input_dropped", enqueued_id: enqueued_id)
|
|
846
857
|
next
|
|
847
858
|
end
|
|
@@ -859,7 +870,7 @@ module Samagotchi
|
|
|
859
870
|
SessionInbox.mark_merged(@session_dir, enqueued_id, into: @turn_enqueued_id) unless prompt.empty?
|
|
860
871
|
prompt.empty? ? nil : [prompt, input.origin]
|
|
861
872
|
ensure
|
|
862
|
-
@
|
|
873
|
+
@turn_state.finish_enqueued_id(enqueued_id)
|
|
863
874
|
end
|
|
864
875
|
ensure
|
|
865
876
|
FileUtils.rm_f(claimed_file)
|
|
@@ -870,8 +881,8 @@ module Samagotchi
|
|
|
870
881
|
# (their rings stay for the next turn), so not in @merged_this_turn.
|
|
871
882
|
reports, mark = take_child_reports
|
|
872
883
|
unless merged.empty? && reports.empty?
|
|
873
|
-
@
|
|
874
|
-
|
|
884
|
+
@event_log.announce(type: :input_merged, count: merged.size + reports.size,
|
|
885
|
+
origins: (merged.map(&:last) + reports.map(&:origin)).filter_map(&:wire))
|
|
875
886
|
@merged_this_turn.concat(merged)
|
|
876
887
|
end
|
|
877
888
|
# Each line keeps its sender (Steer.source_for_client): a chi send or
|
|
@@ -12,7 +12,7 @@ module Samagotchi
|
|
|
12
12
|
# Threads: safe from any thread. Its fields change only under its own
|
|
13
13
|
# mutex, and the taken exit is one value replaced whole (#taken), so a
|
|
14
14
|
# reader never sees a kind with another request's flags. Today every
|
|
15
|
-
# writer also holds the event lock (
|
|
15
|
+
# writer also holds the event lock (SessionObserver#synchronize: the Bridge's
|
|
16
16
|
# /exit, the loop's leave); nothing here depends on it. Lock order: the
|
|
17
17
|
# event lock, then this one.
|
|
18
18
|
class WorkerExitClaim
|
|
@@ -66,7 +66,12 @@ module Samagotchi
|
|
|
66
66
|
# "2m", "running a turn").
|
|
67
67
|
HoldItem = Data.define(:id, :label, :detail)
|
|
68
68
|
|
|
69
|
-
# @param engine [Engine] #
|
|
69
|
+
# @param engine [Engine] #reminder_store, #sign_in_waiting?,
|
|
70
|
+
# #anytime_running?, #relay_desk
|
|
71
|
+
# @param turn_state [TurnState] the Engine's (Engine#turn_state):
|
|
72
|
+
# #running?, #last_activity_at
|
|
73
|
+
# @param question_desk [QuestionDesk] the Engine's
|
|
74
|
+
# (Engine#question_desk): #pending
|
|
70
75
|
# @param bridge [Bridge, nil] #open_streams, #last_client_activity_at; nil
|
|
71
76
|
# when the worker's Bridge failed to start
|
|
72
77
|
# @param timeout_minutes [Numeric, nil] 0 or nil: never exit
|
|
@@ -83,10 +88,12 @@ module Samagotchi
|
|
|
83
88
|
# or with a live worker (ChildrenStatus::Child): they never hold the
|
|
84
89
|
# exit, but the exit ask lists them so "stop them" stops them too
|
|
85
90
|
# @param clock [#call] monotonic seconds, the Engine's and Bridge's clock
|
|
86
|
-
def initialize(engine:, bridge:, timeout_minutes:, input_pending:, awaiting_continue: -> { false },
|
|
91
|
+
def initialize(engine:, turn_state:, question_desk:, bridge:, timeout_minutes:, input_pending:, awaiting_continue: -> { false },
|
|
87
92
|
running_tasks: -> { [] }, watched_tasks: -> { [] }, watched_running: -> { [] },
|
|
88
93
|
delegates: -> { [] }, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
|
|
89
94
|
@engine = engine
|
|
95
|
+
@turn_state = turn_state
|
|
96
|
+
@question_desk = question_desk
|
|
90
97
|
@bridge = bridge
|
|
91
98
|
@timeout = timeout_minutes.to_f * 60
|
|
92
99
|
@input_pending = input_pending
|
|
@@ -106,16 +113,18 @@ module Samagotchi
|
|
|
106
113
|
|
|
107
114
|
# What keeps the worker up, or nil when nothing does.
|
|
108
115
|
# @return [Symbol, nil] :disabled, :turn_running, :input_queued,
|
|
109
|
-
# :continue_offered, :client_connected, :reminders, :background_tasks
|
|
110
|
-
# or
|
|
116
|
+
# :continue_offered, :client_connected, :reminders, :background_tasks,
|
|
117
|
+
# :sign_in_waiting (ctx.sign_in's flow, until its own timeout) or
|
|
118
|
+
# :recent_activity
|
|
111
119
|
def hold
|
|
112
120
|
return :disabled unless @timeout.positive?
|
|
113
|
-
return :turn_running if @
|
|
121
|
+
return :turn_running if @turn_state.running?
|
|
114
122
|
return :input_queued if @input_pending.call
|
|
115
123
|
return :continue_offered if @awaiting_continue.call
|
|
116
124
|
return :client_connected if @bridge && @bridge.open_streams.positive?
|
|
117
125
|
return :reminders if @engine.reminder_store&.any?
|
|
118
126
|
return :background_tasks if @watched_tasks.call.any?
|
|
127
|
+
return :sign_in_waiting if @engine.sign_in_waiting?
|
|
119
128
|
return :recent_activity if idle_seconds < @timeout
|
|
120
129
|
|
|
121
130
|
nil
|
|
@@ -165,7 +174,7 @@ module Samagotchi
|
|
|
165
174
|
# leaving) ignores them.
|
|
166
175
|
# @return [Hold, nil]
|
|
167
176
|
def request_hold(requester:, streams: true, hint: nil, ask: false)
|
|
168
|
-
reason = if @
|
|
177
|
+
reason = if @turn_state.running? then :turn_running
|
|
169
178
|
elsif @input_pending.call then :input_queued
|
|
170
179
|
elsif @awaiting_continue.call then :continue_offered
|
|
171
180
|
elsif streams && @bridge && @bridge.open_streams_except(requester).positive? then :client_connected
|
|
@@ -193,10 +202,10 @@ module Samagotchi
|
|
|
193
202
|
# :question_pending, :command_running, :relays_open or
|
|
194
203
|
# :background_tasks
|
|
195
204
|
def hold_for_restart
|
|
196
|
-
return :turn_running if @
|
|
205
|
+
return :turn_running if @turn_state.running?
|
|
197
206
|
return :input_queued if @input_pending.call
|
|
198
207
|
return :continue_offered if @awaiting_continue.call
|
|
199
|
-
return :question_pending if @
|
|
208
|
+
return :question_pending if @question_desk.pending
|
|
200
209
|
return :command_running if @engine.anytime_running?
|
|
201
210
|
return :relays_open if @engine.relay_desk.active?
|
|
202
211
|
return :background_tasks if @running_tasks.call.any?
|
|
@@ -206,7 +215,7 @@ module Samagotchi
|
|
|
206
215
|
|
|
207
216
|
# @return [Float] seconds since the latest activity of any kind
|
|
208
217
|
def idle_seconds
|
|
209
|
-
last = [@started_at, @
|
|
218
|
+
last = [@started_at, @turn_state.last_activity_at, @bridge&.last_client_activity_at].compact.max
|
|
210
219
|
@clock.call - last
|
|
211
220
|
end
|
|
212
221
|
|
|
@@ -90,18 +90,23 @@ module Samagotchi
|
|
|
90
90
|
false
|
|
91
91
|
end
|
|
92
92
|
|
|
93
|
+
# @param event_log [SessionObserver] the Engine's event log (the context notice)
|
|
94
|
+
# @param turn_state [TurnState] the Engine's turn state (Engine#turn_state):
|
|
95
|
+
# the enqueued-id ledger a taken input claims and finishes on
|
|
93
96
|
# @param wakes [WorkerWakes] the wake budget a context wake asks
|
|
94
97
|
# @param awaiting_continue [#call] whether a continue offer waits
|
|
95
98
|
# @param stopped [#call] whether the session was stopped on disk
|
|
96
99
|
# @param queue_command [#call] (line, client_id, after_file, enqueued_id)
|
|
97
100
|
# queues a session command for the loop's next pass; after_file is the
|
|
98
101
|
# input file it came in, or nil
|
|
99
|
-
def initialize(session:, state_dir:, session_dir:, engine:, context_absorber:, wakes:,
|
|
100
|
-
stopped:, queue_command:)
|
|
102
|
+
def initialize(session:, state_dir:, session_dir:, engine:, event_log:, turn_state:, context_absorber:, wakes:,
|
|
103
|
+
awaiting_continue:, stopped:, queue_command:)
|
|
101
104
|
@session = session
|
|
102
105
|
@state_dir = state_dir
|
|
103
106
|
@session_dir = session_dir
|
|
104
107
|
@engine = engine
|
|
108
|
+
@event_log = event_log
|
|
109
|
+
@turn_state = turn_state
|
|
105
110
|
@context_absorber = context_absorber
|
|
106
111
|
@wakes = wakes
|
|
107
112
|
@awaiting_continue = awaiting_continue
|
|
@@ -271,7 +276,7 @@ module Samagotchi
|
|
|
271
276
|
begin
|
|
272
277
|
input = SessionInbox.read_input(claimed_file)
|
|
273
278
|
enqueued_id = input.origin.enqueued_id
|
|
274
|
-
if SessionInbox.input_taken?(@session_dir, @session, enqueued_id) || !@
|
|
279
|
+
if SessionInbox.input_taken?(@session_dir, @session, enqueued_id) || !@turn_state.claim_enqueued_id(enqueued_id)
|
|
275
280
|
Log.info(:worker, "duplicate_input_dropped", enqueued_id: enqueued_id)
|
|
276
281
|
return
|
|
277
282
|
end
|
|
@@ -282,7 +287,7 @@ module Samagotchi
|
|
|
282
287
|
yield Prompt.new(text: input.prompt, origin: input.origin, no_interrupt: input.no_interrupt, images: input.images,
|
|
283
288
|
delivery: input.delivery, anchors: input.anchors)
|
|
284
289
|
ensure
|
|
285
|
-
@
|
|
290
|
+
@turn_state.finish_enqueued_id(enqueued_id)
|
|
286
291
|
end
|
|
287
292
|
ensure
|
|
288
293
|
FileUtils.rm_f(claimed_file)
|
|
@@ -383,9 +388,9 @@ module Samagotchi
|
|
|
383
388
|
return unless delivery && @wakes.notice_peer_budget!
|
|
384
389
|
|
|
385
390
|
turns = key == ChildRing::MAX_WAKES_KEY ? @wakes.max : @wakes.peer_turns
|
|
386
|
-
@
|
|
387
|
-
|
|
388
|
-
|
|
391
|
+
@event_log.announce(type: :hook_notice, hook: "context", level: :info, between_turns: true,
|
|
392
|
+
text: "#{delivery.peer} answered again; it waits as a note for your next message " \
|
|
393
|
+
"(#{turns} wake turn#{"s" if turns != 1} in a row, #{key})")
|
|
389
394
|
end
|
|
390
395
|
|
|
391
396
|
def context_wakes_on? = Config.get("context.wake") != false
|
|
@@ -58,8 +58,8 @@ module Samagotchi
|
|
|
58
58
|
# The client for this worker (BridgeClient.for_sidecar: its port and its
|
|
59
59
|
# secret from this one read).
|
|
60
60
|
# @return [BridgeClient]
|
|
61
|
-
def client(session_id, host: BridgeClient::HOST, read_timeout: BridgeClient::READ_TIMEOUT)
|
|
62
|
-
BridgeClient.for_sidecar(session_id, sidecar, host: host, read_timeout: read_timeout)
|
|
61
|
+
def client(session_id, host: BridgeClient::HOST, read_timeout: BridgeClient::READ_TIMEOUT, deadline_margin: nil)
|
|
62
|
+
BridgeClient.for_sidecar(session_id, sidecar, host: host, read_timeout: read_timeout, deadline_margin: deadline_margin)
|
|
63
63
|
end
|
|
64
64
|
|
|
65
65
|
def port = sidecar.port
|
|
@@ -34,13 +34,15 @@ module Samagotchi
|
|
|
34
34
|
# (revoked, expired, or the session archived).
|
|
35
35
|
REVOKED_STOP = "%<name>s's link was revoked or expired"
|
|
36
36
|
|
|
37
|
-
# @param engine [Engine]
|
|
38
|
-
#
|
|
37
|
+
# @param engine [Engine] #watch's :participants_changed and its metrics' spend
|
|
38
|
+
# @param event_log [SessionObserver] the Engine's event log: announces the
|
|
39
|
+
# cap notice and :turn_dequeued
|
|
39
40
|
# @param session [Session] the worker's (its participants source, its
|
|
40
41
|
# Verify fresh)
|
|
41
42
|
# @param hook [String] the cap notice's hook name (Engine::PARTICIPANTS_HOOK)
|
|
42
|
-
def initialize(engine:, session:, session_id:, state_dir:, session_dir:, hook:)
|
|
43
|
+
def initialize(engine:, event_log:, session:, session_id:, state_dir:, session_dir:, hook:)
|
|
43
44
|
@engine = engine
|
|
45
|
+
@event_log = event_log
|
|
44
46
|
@session = session
|
|
45
47
|
@session_id = session_id
|
|
46
48
|
@state_dir = state_dir
|
|
@@ -99,7 +101,7 @@ module Samagotchi
|
|
|
99
101
|
TurnCheckpoint.record_refused(@session_dir, origin.enqueued_id, reason: reason, author: origin.participant_author)
|
|
100
102
|
Participants.log(source_id, :turn, state_dir: @state_dir, participant: origin.participant_id,
|
|
101
103
|
enqueued_id: origin.enqueued_id, status: "refused", reason: reason)
|
|
102
|
-
@
|
|
104
|
+
@event_log.announce(type: :turn_dequeued, enqueued_id: origin.enqueued_id, client_id: origin.client_id)
|
|
103
105
|
nil
|
|
104
106
|
end
|
|
105
107
|
|
|
@@ -148,9 +150,9 @@ module Samagotchi
|
|
|
148
150
|
|
|
149
151
|
@cap_noticed << key
|
|
150
152
|
id = Steer.participant_id(author)
|
|
151
|
-
@
|
|
152
|
-
|
|
153
|
-
|
|
153
|
+
@event_log.announce(type: :hook_notice, hook: @hook, level: :warn, between_turns: true,
|
|
154
|
+
text: format(CAP_NOTICE, name: label(author, entry.name), cap: cap,
|
|
155
|
+
session: source_id[0, 8], id: id))
|
|
154
156
|
end
|
|
155
157
|
end
|
|
156
158
|
end
|
|
@@ -22,13 +22,15 @@ module Samagotchi
|
|
|
22
22
|
# @return [TaskWatch]
|
|
23
23
|
attr_reader :watch
|
|
24
24
|
|
|
25
|
+
# @param event_log [SessionObserver] the Engine's event log (the task notice)
|
|
25
26
|
# @param wakes [WorkerWakes] the wake budget a task's wake asks
|
|
26
27
|
# @param awaiting_continue [#call] whether a continue offer waits
|
|
27
28
|
# @param idle [#call] nothing queued and no exit asked for: a finish
|
|
28
29
|
# may start a turn
|
|
29
30
|
# @param save [#call] saves the session (a failure is the callee's to log)
|
|
30
|
-
def initialize(engine:, session:, wakes:, awaiting_continue:, idle:, save:)
|
|
31
|
+
def initialize(engine:, event_log:, session:, wakes:, awaiting_continue:, idle:, save:)
|
|
31
32
|
@engine = engine
|
|
33
|
+
@event_log = event_log
|
|
32
34
|
@session = session
|
|
33
35
|
@wakes = wakes
|
|
34
36
|
@awaiting_continue = awaiting_continue
|
|
@@ -151,10 +153,10 @@ module Samagotchi
|
|
|
151
153
|
def budget_notice(count)
|
|
152
154
|
return unless count.positive? && @wakes.notice_budget!
|
|
153
155
|
|
|
154
|
-
@
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
156
|
+
@event_log.announce(type: :hook_notice, hook: "task", level: :info, between_turns: true,
|
|
157
|
+
text: "#{count} finished background task#{"s" if count != 1} noted; the model reads " \
|
|
158
|
+
"#{count == 1 ? "it" : "them"} with your next message " \
|
|
159
|
+
"(#{@wakes.max} wake turn#{"s" if @wakes.max != 1} in a row, #{ChildRing::MAX_WAKES_KEY})")
|
|
158
160
|
end
|
|
159
161
|
end
|
|
160
162
|
end
|