samagotchi 0.181.0 → 0.183.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 +93 -1
- data/bin/chi +4 -1
- data/docs/architecture.md +17 -7
- data/docs/bundle-authoring.md +1 -1
- data/docs/bundles.md +103 -4
- data/docs/cli.md +58 -10
- data/docs/configuration.md +3 -1
- data/docs/guardrails.md +16 -1
- data/docs/internals/block-renderers.md +172 -3
- data/docs/internals/bridge.md +23 -5
- data/docs/internals/bundles.md +21 -4
- data/docs/internals/engine.md +29 -8
- data/docs/internals/inline-widgets.md +3 -1
- data/docs/internals/prompt-caching.md +9 -1
- data/docs/internals/remote-sessions.md +38 -7
- data/docs/internals/web-frontend.md +116 -26
- data/docs/internals/working-set.md +1 -1
- data/docs/mcp-server.md +80 -0
- data/docs/plugins.md +308 -21
- data/docs/sessions.md +7 -4
- data/docs/sub-agent.md +9 -0
- data/docs/testing.md +3 -1
- data/docs/thin-clients.md +146 -0
- data/lib/samagotchi/answer_display.rb +3 -2
- data/lib/samagotchi/block_fix/stamp.rb +49 -0
- data/lib/samagotchi/block_fix.rb +220 -0
- data/lib/samagotchi/bridge/block_fix_route.rb +30 -0
- data/lib/samagotchi/bridge/pending_card.rb +4 -3
- data/lib/samagotchi/bridge/sse_writer.rb +16 -15
- data/lib/samagotchi/bridge/turn_drawings.rb +19 -0
- data/lib/samagotchi/bridge.rb +69 -69
- data/lib/samagotchi/bridge_client.rb +9 -0
- data/lib/samagotchi/bundle_command/api_calls.rb +1 -1
- data/lib/samagotchi/bundle_command/check.rb +36 -19
- data/lib/samagotchi/bundle_command.rb +50 -21
- data/lib/samagotchi/bundle_manifest.rb +101 -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 +407 -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 +2 -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/config.rb +3 -0
- data/lib/samagotchi/continue_intent.rb +8 -0
- data/lib/samagotchi/continue_offer.rb +24 -15
- data/lib/samagotchi/engine/block_fix.rb +332 -0
- 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/side_requests.rb +8 -6
- data/lib/samagotchi/engine/sign_in.rb +142 -0
- data/lib/samagotchi/engine/turn_end.rb +3 -3
- data/lib/samagotchi/engine.rb +47 -27
- data/lib/samagotchi/engine_snapshots.rb +1 -1
- data/lib/samagotchi/events.rb +1 -0
- 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_client.rb +10 -6
- 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/inline_widgets.rb +12 -0
- 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/kernel_loop.rb +7 -5
- data/lib/samagotchi/llm/chat_loop.rb +2 -0
- data/lib/samagotchi/llm/http.rb +96 -5
- data/lib/samagotchi/markdown_parser.rb +28 -0
- data/lib/samagotchi/mcp_command.rb +110 -0
- data/lib/samagotchi/mcp_server/http.rb +112 -0
- data/lib/samagotchi/mcp_server/protocol.rb +244 -0
- data/lib/samagotchi/mcp_server/stdio.rb +92 -0
- data/lib/samagotchi/mcp_server/text.rb +155 -0
- data/lib/samagotchi/mcp_server/tools.rb +245 -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/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/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/run_options.rb +2 -1
- data/lib/samagotchi/send_command.rb +54 -41
- data/lib/samagotchi/session_access/ask.rb +244 -0
- data/lib/samagotchi/session_access/contract.rb +8 -0
- data/lib/samagotchi/session_access/delete_plan.rb +77 -0
- data/lib/samagotchi/session_access/exchange/token.rb +101 -0
- data/lib/samagotchi/session_access/exchange.rb +173 -0
- data/lib/samagotchi/session_access/local.rb +38 -6
- data/lib/samagotchi/session_access/remote.rb +23 -2
- data/lib/samagotchi/session_access.rb +1 -0
- data/lib/samagotchi/session_chain/delete.rb +39 -81
- data/lib/samagotchi/session_chain/delete_preview.rb +55 -19
- 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_delete_command.rb +129 -3
- data/lib/samagotchi/session_manager/delegate_rule.rb +86 -0
- data/lib/samagotchi/session_manager/listing.rb +4 -4
- data/lib/samagotchi/session_manager/removal.rb +38 -12
- data/lib/samagotchi/session_manager/running_tasks.rb +50 -23
- data/lib/samagotchi/session_manager.rb +1 -0
- data/lib/samagotchi/session_observer.rb +27 -0
- data/lib/samagotchi/steer_cut.rb +4 -3
- 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/tool_index.rb +14 -3
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +20 -6
- data/lib/samagotchi/web/ask_routes.rb +292 -0
- data/lib/samagotchi/web/block_fix_routes.rb +39 -0
- data/lib/samagotchi/web/markdown_renderer.rb +24 -9
- data/lib/samagotchi/web/mcp_route.rb +88 -0
- data/lib/samagotchi/web/message_display.rb +6 -1
- data/lib/samagotchi/web/participant_view.rb +1 -1
- data/lib/samagotchi/web/public/app.js +7 -8
- data/lib/samagotchi/web/public/block_fix.js +270 -0
- data/lib/samagotchi/web/public/block_fix_note.js +104 -0
- data/lib/samagotchi/web/public/block_frame.js +77 -13
- data/lib/samagotchi/web/public/block_host.js +45 -9
- data/lib/samagotchi/web/public/block_regions.js +126 -0
- data/lib/samagotchi/web/public/block_renderers.css +12 -0
- data/lib/samagotchi/web/public/block_watch.js +16 -2
- 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 +222 -162
- 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/inline_widgets.js +1 -1
- 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 +275 -0
- data/lib/samagotchi/web/public/running_tasks.js +6 -4
- data/lib/samagotchi/web/public/session_actions.js +105 -100
- data/lib/samagotchi/web/public/stream_handlers.js +5 -1
- data/lib/samagotchi/web/public/turn_block.js +5 -2
- data/lib/samagotchi/web/server.rb +6 -4
- data/lib/samagotchi/web/session_access_routes.rb +14 -11
- data/lib/samagotchi/web/session_control.rb +84 -30
- data/lib/samagotchi/web/session_create.rb +2 -2
- data/lib/samagotchi/worker/block_fixes.rb +22 -0
- 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 +42 -24
- 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_participant_gate.rb +9 -7
- data/lib/samagotchi/worker_task_wakes.rb +7 -5
- metadata +42 -1
|
@@ -13,7 +13,7 @@ module Samagotchi
|
|
|
13
13
|
# after-turn hooks, a stop before the model, the release, what an ending
|
|
14
14
|
# keeps (TurnEndings, forwarded), last_turn, the answer's display.
|
|
15
15
|
# Writes @last_context_status; reads @turn_endings, @turn_state,
|
|
16
|
-
# @steer_cut, @hooks, @
|
|
16
|
+
# @steer_cut, @hooks, @event_log, @client.
|
|
17
17
|
module TurnEndPart
|
|
18
18
|
# How the turn ended, on the session (Session#last_turn): the caller's
|
|
19
19
|
# save puts it in the file the session hub watches. Public for the
|
|
@@ -69,9 +69,9 @@ module Samagotchi
|
|
|
69
69
|
replace_session_messages(session, messages[0...-1] + [merged])
|
|
70
70
|
event = { type: :answer_display, display: changed ? display.text : nil }
|
|
71
71
|
event[:refs] = refs.count if added
|
|
72
|
-
@
|
|
72
|
+
@event_log.notify(event)
|
|
73
73
|
elsif pending
|
|
74
|
-
@
|
|
74
|
+
@event_log.notify({ type: :answer_display, display: nil })
|
|
75
75
|
end
|
|
76
76
|
end
|
|
77
77
|
end
|
data/lib/samagotchi/engine.rb
CHANGED
|
@@ -125,6 +125,7 @@ require_relative "engine/models"
|
|
|
125
125
|
require_relative "engine/model_settings"
|
|
126
126
|
require_relative "engine/events"
|
|
127
127
|
require_relative "engine/cards"
|
|
128
|
+
require_relative "engine/sign_in"
|
|
128
129
|
require_relative "engine/objectives"
|
|
129
130
|
require_relative "engine/messages"
|
|
130
131
|
require_relative "engine/plugins"
|
|
@@ -138,6 +139,7 @@ require_relative "engine/toolkit"
|
|
|
138
139
|
require_relative "engine/context_judge"
|
|
139
140
|
require_relative "engine/turn_helpers"
|
|
140
141
|
require_relative "engine/turn_end"
|
|
142
|
+
require_relative "engine/block_fix"
|
|
141
143
|
|
|
142
144
|
module Samagotchi
|
|
143
145
|
# Engine owns the core agent logic: system prompt construction, tool
|
|
@@ -161,6 +163,7 @@ module Samagotchi
|
|
|
161
163
|
include ModelSettingsPart
|
|
162
164
|
include EventsPart
|
|
163
165
|
include CardsPart
|
|
166
|
+
include SignInPart
|
|
164
167
|
include ObjectivesPart
|
|
165
168
|
include MessagesPart
|
|
166
169
|
include PluginsPart
|
|
@@ -174,6 +177,7 @@ module Samagotchi
|
|
|
174
177
|
include ContextJudgePart
|
|
175
178
|
include TurnHelpersPart
|
|
176
179
|
include TurnEndPart
|
|
180
|
+
include BlockFixPart
|
|
177
181
|
|
|
178
182
|
# Raised by #answer_question when the question it targets is no longer
|
|
179
183
|
# open (never asked, superseded, already answered or cancelled). A subclass
|
|
@@ -230,6 +234,7 @@ module Samagotchi
|
|
|
230
234
|
@chat_backend = nil
|
|
231
235
|
@chat_backend_mutex = Mutex.new
|
|
232
236
|
@side_judgments = SideJudgments.new
|
|
237
|
+
init_block_fix
|
|
233
238
|
@judgments = build_judgments
|
|
234
239
|
# Taken once: a running worker keeps the hosts it started with. A
|
|
235
240
|
# client (specs) goes on a registry of its own, never the shared one.
|
|
@@ -324,7 +329,7 @@ module Samagotchi
|
|
|
324
329
|
# Who is in the review now, and who came or went (#announce_participants).
|
|
325
330
|
@participants_watch = ParticipantsWatch.new(session_id: -> { @session&.id }, state_dir: -> { session_state_dir })
|
|
326
331
|
@session = nil
|
|
327
|
-
@
|
|
332
|
+
@event_log = SessionObserver.new
|
|
328
333
|
@metrics = SessionMetrics.new
|
|
329
334
|
@used_memories = UsedMemories.new
|
|
330
335
|
# Hydrate from resumed session if present
|
|
@@ -345,7 +350,7 @@ module Samagotchi
|
|
|
345
350
|
# recap). Both jobs read the activity seam above; the scheduler owns
|
|
346
351
|
# the single background thread and isolates per-job failures.
|
|
347
352
|
@idle_scheduler = build_idle_scheduler
|
|
348
|
-
|
|
353
|
+
subscribe_log_observers
|
|
349
354
|
# The UIs' read model (/state, /stats). Built last: nothing calls a
|
|
350
355
|
# snapshot while the Engine is built.
|
|
351
356
|
@engine_snapshots = build_engine_snapshots
|
|
@@ -378,6 +383,9 @@ module Samagotchi
|
|
|
378
383
|
# commands running now (#spawn_anytime), which it waits for.
|
|
379
384
|
@services = Plugin::Services.new
|
|
380
385
|
@anytime_threads = []
|
|
386
|
+
# ctx.sign_in's flows waiting for their callback (SignInPart), which
|
|
387
|
+
# #shutdown stops first.
|
|
388
|
+
@sign_in_flows = []
|
|
381
389
|
# chi.init tasks (#add_init_task), started by #start_init_tasks!, and
|
|
382
390
|
# plugins' tool sets from chi.replace_tools until the turn thread
|
|
383
391
|
# applies them (#apply_staged_tools!). Built before the plugins load.
|
|
@@ -423,19 +431,19 @@ module Samagotchi
|
|
|
423
431
|
private :restrict_scratch_tools!
|
|
424
432
|
|
|
425
433
|
# The persistent observers of every turn's events.
|
|
426
|
-
def
|
|
434
|
+
def subscribe_log_observers
|
|
427
435
|
# The metrics collector is a persistent observer so every run_turn event
|
|
428
436
|
# (a -p --non-interactive one-shot and SessionManager workers)
|
|
429
437
|
# feeds it automatically.
|
|
430
|
-
@
|
|
438
|
+
@event_log.subscribe(observer: @metrics)
|
|
431
439
|
# And the event trail in the debug log (`turn` records).
|
|
432
|
-
@
|
|
440
|
+
@event_log.subscribe(observer: LogSubscriber.new(session_id: -> { @session&.id }))
|
|
433
441
|
# The thinking of cut and capped generations, kept in the session dir.
|
|
434
|
-
@
|
|
442
|
+
@event_log.subscribe(observer: ThinkingTails.new(session_dir: lambda {
|
|
435
443
|
@session&.id && Session.session_dir(@session.id, state_dir: session_state_dir)
|
|
436
444
|
}))
|
|
437
445
|
end
|
|
438
|
-
private :
|
|
446
|
+
private :subscribe_log_observers
|
|
439
447
|
|
|
440
448
|
# The kernel and what it holds of the Engine for its life, one
|
|
441
449
|
# KernelLoop::Services (stores, hooks, tools, the mutes, the question
|
|
@@ -514,7 +522,7 @@ module Samagotchi
|
|
|
514
522
|
# The event fan-out and the stream handler (see #initialize for when).
|
|
515
523
|
def build_event_fanout
|
|
516
524
|
EventFanout.new(
|
|
517
|
-
observer: -> { @
|
|
525
|
+
observer: -> { @event_log },
|
|
518
526
|
used_memories: -> { @used_memories },
|
|
519
527
|
muted: -> { @muted_memory_names },
|
|
520
528
|
steer_cut: -> { @steer_cut },
|
|
@@ -544,7 +552,7 @@ module Samagotchi
|
|
|
544
552
|
EngineSnapshots.new(
|
|
545
553
|
session: -> { @session },
|
|
546
554
|
metrics: -> { @metrics },
|
|
547
|
-
observer: -> { @
|
|
555
|
+
observer: -> { @event_log },
|
|
548
556
|
question_desk: -> { @question_desk },
|
|
549
557
|
used_memories: -> { @used_memories },
|
|
550
558
|
muted: -> { @muted_memory_names },
|
|
@@ -625,7 +633,7 @@ module Samagotchi
|
|
|
625
633
|
|
|
626
634
|
# The idle layer's shared scheduler (see #initialize for when).
|
|
627
635
|
def build_idle_scheduler
|
|
628
|
-
IdleScheduler.new(
|
|
636
|
+
IdleScheduler.new(jobs: [@reminders, @recap].compact)
|
|
629
637
|
end
|
|
630
638
|
private :build_idle_scheduler
|
|
631
639
|
|
|
@@ -640,6 +648,22 @@ module Samagotchi
|
|
|
640
648
|
# @return [SessionMetrics] the per-session analytics collector
|
|
641
649
|
attr_reader :metrics
|
|
642
650
|
|
|
651
|
+
# @return [SessionObserver] the session's event log: the Worker hands it
|
|
652
|
+
# to the Bridge and its collaborators, which call it directly
|
|
653
|
+
attr_reader :event_log
|
|
654
|
+
|
|
655
|
+
# @return [TurnState] the turn as other threads see it: the Worker hands
|
|
656
|
+
# it to the Bridge and its collaborators, which call it directly
|
|
657
|
+
attr_reader :turn_state
|
|
658
|
+
|
|
659
|
+
# @return [QuestionDesk] the one open question: the Worker hands it to
|
|
660
|
+
# the Bridge and its collaborators, which call it directly
|
|
661
|
+
attr_reader :question_desk
|
|
662
|
+
|
|
663
|
+
# @return [SteerCut] the mid-turn steer cut: the Worker hands it to the
|
|
664
|
+
# Bridge, which calls it directly
|
|
665
|
+
attr_reader :steer_cut
|
|
666
|
+
|
|
643
667
|
attr_reader :default_model_name, :host_registry, :client
|
|
644
668
|
|
|
645
669
|
# @return [Commands::Registry] the commands this session runs: the
|
|
@@ -649,15 +673,6 @@ module Samagotchi
|
|
|
649
673
|
# @return [PeerSources] the sources the peer bundles declared
|
|
650
674
|
attr_reader :peer_sources
|
|
651
675
|
|
|
652
|
-
# Event types #announce accepts: facts about the session's input queue,
|
|
653
|
-
# a failed turn's prompt handed back and the continue offer, which live
|
|
654
|
-
# UIs need in the event log, emitted outside any turn's stream; a
|
|
655
|
-
# Verify fresh child's verdict landing on its card (:verdict_recorded).
|
|
656
|
-
ANNOUNCEABLE_EVENTS = %i[turn_enqueued turn_dequeued input_merged prompt_restored continue_offered continue_resolved
|
|
657
|
-
command_queued command_ran context_added card hook_notice guardrail_warning
|
|
658
|
-
plugin_init_started plugin_init_finished working_set_updated participants_changed
|
|
659
|
-
verdict_recorded objectives_updated].freeze
|
|
660
|
-
|
|
661
676
|
# ── Plugin init tasks (chi.init) ──────────────────────────────────────
|
|
662
677
|
|
|
663
678
|
# A plugin's slow setup (docs/plugins.md, Init tasks): PluginTasks.
|
|
@@ -673,7 +688,8 @@ module Samagotchi
|
|
|
673
688
|
SIDE_JUDGMENTS_JOIN_SECONDS = 1.0
|
|
674
689
|
|
|
675
690
|
# The session's worker (or a one-shot run) is leaving (/exit, an idle
|
|
676
|
-
# exit, a crash, TERM): stop the
|
|
691
|
+
# exit, a crash, TERM): stop the waiting sign-ins (their cards say
|
|
692
|
+
# cancelled) and the idle jobs, give the running anytime commands
|
|
677
693
|
# up to +join_timeout+ seconds to finish (their command_ran is
|
|
678
694
|
# announced), and settle already-reaped tasks under a shared deadline
|
|
679
695
|
# of at least TaskRuntime::SETTLE_ON_EXIT_SECONDS. Then stop the plugins'
|
|
@@ -687,6 +703,9 @@ module Samagotchi
|
|
|
687
703
|
# An init task's requests end (a server's boot), so it finishes.
|
|
688
704
|
@anytime_threads.dup + @plugin_tasks.shut_down!
|
|
689
705
|
end
|
|
706
|
+
# A waiting sign-in's card says cancelled while the Bridge still
|
|
707
|
+
# listens (its cards.json), and its port closes.
|
|
708
|
+
stop_sign_ins
|
|
690
709
|
# #stop_idle's work (the scheduler's stop is idempotent), where the
|
|
691
710
|
# callers haven't stopped it already.
|
|
692
711
|
@idle_scheduler&.stop
|
|
@@ -807,7 +826,9 @@ module Samagotchi
|
|
|
807
826
|
# @return [IdleRecap, nil]
|
|
808
827
|
def build_recap
|
|
809
828
|
@recap_built_for = session_recap_values
|
|
810
|
-
recap = RecapSetup.build(@recap_kwarg,
|
|
829
|
+
recap = RecapSetup.build(@recap_kwarg, turn_state: @turn_state, messages_json: -> { messages_json_for_recap },
|
|
830
|
+
emit_recap: ->(**fields) { emit_recap(**fields) },
|
|
831
|
+
on_usage: ->(usage) { note_side_usage(usage) }, host_registry: @host_registry,
|
|
811
832
|
session_target: -> { session_model_recap_target },
|
|
812
833
|
session_id: -> { @session&.id }, state_dir: -> { session_state_dir },
|
|
813
834
|
session_config: -> { @session&.settings&.core })
|
|
@@ -1153,6 +1174,7 @@ module Samagotchi
|
|
|
1153
1174
|
# so a Ctrl-C or a failure meanwhile keeps what is there.
|
|
1154
1175
|
def turn_messages(turn, image_refs)
|
|
1155
1176
|
session = turn.session
|
|
1177
|
+
note_block_fix_turn
|
|
1156
1178
|
turn.messages = session.messages.dup
|
|
1157
1179
|
# Built once per Engine (and again after a model switch) so the prompt
|
|
1158
1180
|
# prefix, and the model server's KV cache for it, stay stable.
|
|
@@ -1248,14 +1270,12 @@ module Samagotchi
|
|
|
1248
1270
|
|
|
1249
1271
|
def running_command_key = :"samagotchi_command_#{object_id}"
|
|
1250
1272
|
|
|
1251
|
-
# The one named seam for replacing a session's conversation (the
|
|
1252
|
-
# array is replaced, never mutated in place). No lock: a reader gets
|
|
1253
|
-
# the old array or the new one, and both are whole.
|
|
1254
|
-
# The turn as other threads see it (TurnState).
|
|
1255
|
-
attr_reader :turn_state
|
|
1256
1273
|
# The due reminders on their way into a turn (ReminderQueue).
|
|
1257
1274
|
attr_reader :reminder_queue
|
|
1258
1275
|
|
|
1276
|
+
# The one named seam for replacing a session's conversation (the
|
|
1277
|
+
# array is replaced, never mutated in place). No lock: a reader gets
|
|
1278
|
+
# the old array or the new one, and both are whole.
|
|
1259
1279
|
def replace_session_messages(session, messages)
|
|
1260
1280
|
session.messages = messages
|
|
1261
1281
|
end
|
|
@@ -1303,7 +1323,7 @@ module Samagotchi
|
|
|
1303
1323
|
def build_reminders(auto_turn_callback: nil)
|
|
1304
1324
|
@auto_turn_callback = auto_turn_callback
|
|
1305
1325
|
IdleReminders.new(
|
|
1306
|
-
|
|
1326
|
+
turn_state: @turn_state,
|
|
1307
1327
|
queue: @reminder_queue,
|
|
1308
1328
|
callback: @auto_turn_callback
|
|
1309
1329
|
)
|
|
@@ -118,7 +118,7 @@ module Samagotchi
|
|
|
118
118
|
# :status [String, nil] current session status
|
|
119
119
|
# :message_count [Integer] number of messages in the session
|
|
120
120
|
# :last_prompt [String, nil] the last user prompt (empty string if none)
|
|
121
|
-
# :event_seq [Integer] @
|
|
121
|
+
# :event_seq [Integer] @event_log.event_count
|
|
122
122
|
# :metrics [Hash] @metrics.snapshot (per-session analytics)
|
|
123
123
|
# :pending_question [Hash, nil] the pending question (PendingQuestion#to_h)
|
|
124
124
|
# :used_memory_names [Array<String>] deduped memory names active this session
|
data/lib/samagotchi/events.rb
CHANGED
|
@@ -271,6 +271,7 @@ module Samagotchi
|
|
|
271
271
|
require_relative "context_sources"
|
|
272
272
|
require_relative "chi_home"
|
|
273
273
|
require_relative "chi_shim"
|
|
274
|
+
require_relative "paths"
|
|
274
275
|
Guardrails::ProtectedPaths.new(
|
|
275
276
|
store_dir: File.dirname(@approvals.path),
|
|
276
277
|
bundles_dir: InstalledBundles.dir,
|
|
@@ -279,7 +280,8 @@ module Samagotchi
|
|
|
279
280
|
context_dir: ContextSources.root,
|
|
280
281
|
runs_dir: File.join(File.dirname(@approvals.path, 2), "system_one"),
|
|
281
282
|
home_path: ChiHome.path,
|
|
282
|
-
shim_path: ChiShim.path
|
|
283
|
+
shim_path: ChiShim.path,
|
|
284
|
+
plugins_dir: File.join(Paths.state_dir, "plugins")
|
|
283
285
|
)
|
|
284
286
|
end
|
|
285
287
|
end
|
|
@@ -25,8 +25,22 @@ module Samagotchi
|
|
|
25
25
|
# breaking it.
|
|
26
26
|
# execute can still write all of them (the guardrails bundle adds a
|
|
27
27
|
# text match for that).
|
|
28
|
+
#
|
|
29
|
+
# A bundle's secrets (<state dir>/plugins/<bundle>/secrets, ctx.secrets)
|
|
30
|
+
# are kept from chi's model whatever the call does: every file tool's
|
|
31
|
+
# path there is denied, read included, and a shell command that names
|
|
32
|
+
# the folder asks once (the user only). An accident guard, not a
|
|
33
|
+
# sandbox: a command that doesn't name it (cd there first, a variable,
|
|
34
|
+
# a script) still reads it.
|
|
28
35
|
class ProtectedPaths
|
|
29
36
|
SOURCE = "core"
|
|
37
|
+
SECRETS_RULE = "bundle-secrets"
|
|
38
|
+
SECRETS_SHELL_RULE = "bundle-secrets-shell"
|
|
39
|
+
SECRETS_REASON = "a bundle's secrets (tokens) are its own: chi's model doesn't read or change them"
|
|
40
|
+
SECRETS_SHELL_REASON = "the command names a bundle's secrets folder (its tokens)"
|
|
41
|
+
# A command naming a secrets folder under chi's state dir, as typed:
|
|
42
|
+
# the dir's name (samagotchi), then plugins/<bundle or a glob>/secrets.
|
|
43
|
+
SECRETS_TEXT = %r{samagotchi/plugins/[^/\s'"]+/secrets(?![\w.-])}
|
|
30
44
|
|
|
31
45
|
# @param store_dir [String] <state dir>/guardrails
|
|
32
46
|
# @param bundles_dir [String] <memories>/.bundles
|
|
@@ -36,7 +50,14 @@ module Samagotchi
|
|
|
36
50
|
# @param runs_dir [String, nil] <state dir>/system_one (SystemOne::Runs)
|
|
37
51
|
# @param home_path [String, nil] home.json (ChiHome.path)
|
|
38
52
|
# @param shim_path [String, nil] the chi shim (ChiShim.path)
|
|
39
|
-
|
|
53
|
+
# @param plugins_dir [String, nil] <state dir>/plugins (the bundles' data, their secrets/ in it)
|
|
54
|
+
def initialize(store_dir:, bundles_dir:, config_path:, hooks_dir:, context_dir: nil, runs_dir: nil, home_path: nil, shim_path: nil,
|
|
55
|
+
plugins_dir: nil)
|
|
56
|
+
@plugins_dir = plugins_dir
|
|
57
|
+
# The same, spelled out: this state dir's own plugins/<x>/secrets.
|
|
58
|
+
@secrets_text = plugins_dir && Regexp.union(SECRETS_TEXT, *([plugins_dir.to_s.chomp("/")] | PathResolve.reals(plugins_dir.to_s.chomp("/"))).map do |dir|
|
|
59
|
+
%r{#{Regexp.escape(dir)}/[^/\s'"]+/secrets(?![\w.-])}
|
|
60
|
+
end)
|
|
40
61
|
@deny = [
|
|
41
62
|
["guardrail-store", store_dir, "the approval store belongs to the user"],
|
|
42
63
|
["installed-bundles", bundles_dir, "installed bundles (hooks, rules, manifests) are changed by chi bundle install"],
|
|
@@ -56,7 +77,10 @@ module Samagotchi
|
|
|
56
77
|
# targets: writes: true) is judged on its paths.
|
|
57
78
|
def check(verdict)
|
|
58
79
|
targets = verdict.targets
|
|
59
|
-
return verdict unless targets
|
|
80
|
+
return verdict unless targets
|
|
81
|
+
|
|
82
|
+
check_secrets(verdict, targets)
|
|
83
|
+
return verdict unless targets.writes?
|
|
60
84
|
|
|
61
85
|
paths = targets.paths.flat_map { |p| PathResolve.reals(p) }
|
|
62
86
|
@deny.each do |rule, root, reason|
|
|
@@ -72,6 +96,16 @@ module Samagotchi
|
|
|
72
96
|
verdict
|
|
73
97
|
end
|
|
74
98
|
|
|
99
|
+
# Whether +path+ (absolute, resolved) is a bundle's secrets folder or
|
|
100
|
+
# in it: <plugins dir>/<bundle>/secrets[/…].
|
|
101
|
+
def secrets_path?(path)
|
|
102
|
+
return false unless @plugins_dir
|
|
103
|
+
|
|
104
|
+
PathResolve.reals(@plugins_dir.to_s.chomp("/")).any? do |dir|
|
|
105
|
+
PathResolve.under?(path, dir) && path[(dir.length + 1)..].to_s.split("/")[1]&.casecmp?("secrets")
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
75
109
|
# The path primitives (PathResolve) as this class has long named
|
|
76
110
|
# them; specs and older callers reach them here.
|
|
77
111
|
def self.expand(path, base = nil) = PathResolve.expand(path, base)
|
|
@@ -81,6 +115,21 @@ module Samagotchi
|
|
|
81
115
|
def self.within?(path, root) = PathResolve.within?(path, root)
|
|
82
116
|
def self.under?(path, dir) = PathResolve.under?(path, dir)
|
|
83
117
|
def self.folds_case?(path) = PathResolve.folds_case?(path)
|
|
118
|
+
|
|
119
|
+
private
|
|
120
|
+
|
|
121
|
+
def check_secrets(verdict, targets)
|
|
122
|
+
return unless @plugins_dir
|
|
123
|
+
|
|
124
|
+
paths = targets.paths.flat_map { |p| PathResolve.reals(p) }
|
|
125
|
+
if targets.shell?
|
|
126
|
+
return unless paths.any? { |p| secrets_path?(p) } || targets.command.to_s.match?(@secrets_text)
|
|
127
|
+
|
|
128
|
+
verdict.ask!(SECRETS_SHELL_REASON, scopes: %w[once], rule: SECRETS_SHELL_RULE, source: SOURCE, decided_by: "core")
|
|
129
|
+
elsif paths.any? { |p| secrets_path?(p) }
|
|
130
|
+
verdict.deny!(SECRETS_REASON, rule: SECRETS_RULE, source: SOURCE, decided_by: "core")
|
|
131
|
+
end
|
|
132
|
+
end
|
|
84
133
|
end
|
|
85
134
|
end
|
|
86
135
|
end
|
|
@@ -19,13 +19,14 @@ module Samagotchi
|
|
|
19
19
|
# that reaches the system prompt, or a model overlay, is written (a
|
|
20
20
|
# parent model would be writing a model's prompt); and the bundle's
|
|
21
21
|
# asks before answering chi's questions around chi answer (a parent
|
|
22
|
-
# allowing one would answer a grandchild's question as the user)
|
|
22
|
+
# allowing one would answer a grandchild's question as the user); and
|
|
23
|
+
# a shell command that names a bundle's secrets (ProtectedPaths).
|
|
23
24
|
# A parent agent may never allow them (ParentApprovals.protected?,
|
|
24
25
|
# ParentPolicy's user_only); System 1 may, only if the user lists the
|
|
25
26
|
# rule by name in guardrails.system_one.rules, and never for the
|
|
26
27
|
# chi-answer/chi-broadcast ones (SystemOne::SelfProtect::NEVER_AUTO).
|
|
27
28
|
PROTECTED_RULES = %w[chi-config chi-home chi-shim chi-hooks shell-touches-chi chi-context-cmd chi-broadcast prompt-memory-write
|
|
28
|
-
model-overlay-write chi-answer-piped chi-answer-http].freeze
|
|
29
|
+
model-overlay-write chi-answer-piped chi-answer-http bundle-secrets-shell].freeze
|
|
29
30
|
SCOPES = %w[once session repo rule].freeze
|
|
30
31
|
# Who answered an ask when it was a parent agent, not the user.
|
|
31
32
|
PARENT_AGENT = "parent agent"
|
data/lib/samagotchi/hold_stop.rb
CHANGED
|
@@ -24,13 +24,13 @@ module Samagotchi
|
|
|
24
24
|
def any? = reminders.any? || tasks.any? || delegates.any?
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
-
# @param
|
|
27
|
+
# @param reminder_store [ReminderStore, nil] the session's reminders
|
|
28
28
|
# @param task_watch [TaskWatch, nil] this session's finish-wake watch
|
|
29
29
|
# (nil with session.task_wakes: off)
|
|
30
30
|
# @param delegates [#call] the children to stop (ChildrenStatus::Child)
|
|
31
31
|
# @param state_dir [String] where the child sessions live
|
|
32
|
-
def initialize(
|
|
33
|
-
@
|
|
32
|
+
def initialize(reminder_store:, task_watch:, delegates:, state_dir:)
|
|
33
|
+
@reminder_store = reminder_store
|
|
34
34
|
@task_watch = task_watch
|
|
35
35
|
@delegates = delegates
|
|
36
36
|
@state_dir = state_dir
|
|
@@ -49,7 +49,7 @@ module Samagotchi
|
|
|
49
49
|
# Reminders live only in this process, so clearing the store ends them;
|
|
50
50
|
# the names go to the report before it is emptied.
|
|
51
51
|
def stop_reminders
|
|
52
|
-
store = @
|
|
52
|
+
store = @reminder_store
|
|
53
53
|
return [] unless store
|
|
54
54
|
|
|
55
55
|
names = store.reminders.values.map { |reminder| reminder[:name] }
|
|
@@ -29,9 +29,10 @@ module Samagotchi
|
|
|
29
29
|
# answered (the server's name for it; nil when the reply names none).
|
|
30
30
|
# +top_logprobs+: the first token's alternatives (LLM::TokenLogprob)
|
|
31
31
|
# when #ask asked for logprobs and the server sent them, else [].
|
|
32
|
-
#
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
# +cut_off+: the reply stopped at max_tokens (its finish reason is
|
|
33
|
+
# "length"). #to_s is the text.
|
|
34
|
+
Summary = Data.define(:text, :model, :top_logprobs, :cut_off) do
|
|
35
|
+
def initialize(top_logprobs: [].freeze, cut_off: false, **) = super
|
|
35
36
|
|
|
36
37
|
def to_s = text
|
|
37
38
|
end
|
|
@@ -139,7 +140,8 @@ module Samagotchi
|
|
|
139
140
|
def ask(messages, max_tokens: MAX_TOKENS, cancel_controller: nil, kind: "ask", options: {}, session_id: nil, on_usage: nil)
|
|
140
141
|
response = generate(messages, max_tokens: max_tokens, cancel_controller: cancel_controller, whole_sentences: false,
|
|
141
142
|
kind: kind, options: options, session_id: session_id, on_usage: on_usage)
|
|
142
|
-
Summary.new(text: response.text.to_s.strip, model: response.model, top_logprobs: response.top_logprobs
|
|
143
|
+
Summary.new(text: response.text.to_s.strip, model: response.model, top_logprobs: response.top_logprobs,
|
|
144
|
+
cut_off: response.cut_off)
|
|
143
145
|
rescue SummarizeError, LLM::RequestCancelled
|
|
144
146
|
raise
|
|
145
147
|
rescue StandardError => e
|
|
@@ -174,7 +176,9 @@ module Samagotchi
|
|
|
174
176
|
cut_off = response.finish_reason == "length"
|
|
175
177
|
# Reasoning cut off by max_tokens is the model's thinking, not a recap
|
|
176
178
|
# (a server that ignores the thinking switch thinks until the limit).
|
|
177
|
-
|
|
179
|
+
if content.empty? && cut_off
|
|
180
|
+
return Summary.new(text: "", model: response.model, top_logprobs: response.top_logprobs, cut_off: true)
|
|
181
|
+
end
|
|
178
182
|
|
|
179
183
|
# Prefer content, fall back to a finished reasoning_content (e.g.
|
|
180
184
|
# Qwen3.6), and strip thinking tokens some models (Qwen, Gemma) leave
|
|
@@ -182,7 +186,7 @@ module Samagotchi
|
|
|
182
186
|
text = self.class.strip_thinking(content.empty? ? reasoning : content)
|
|
183
187
|
# Cut off by max_tokens: keep the sentences that finished ("" if none).
|
|
184
188
|
text = whole_sentences ? self.class.full_sentences(text) : "#{text}…" if cut_off && !text.empty?
|
|
185
|
-
Summary.new(text: text, model: response.model, top_logprobs: response.top_logprobs)
|
|
189
|
+
Summary.new(text: text, model: response.model, top_logprobs: response.top_logprobs, cut_off: cut_off)
|
|
186
190
|
rescue LLM::ProtocolError => e
|
|
187
191
|
raise SummarizeError, "server returned no parseable assistant content (#{e.message})"
|
|
188
192
|
end
|
|
@@ -19,7 +19,7 @@ module Samagotchi
|
|
|
19
19
|
# IdleScheduler (one background thread for the whole idle layer).
|
|
20
20
|
#
|
|
21
21
|
# Owns the inactivity clock bookkeeping shared across UIs: it reads the
|
|
22
|
-
# Engine's `last_activity_at` / `activity_seq` / `
|
|
22
|
+
# Engine's TurnState (`last_activity_at` / `activity_seq` / `running?`, the
|
|
23
23
|
# single source of truth) rather than tracking its own timeline, so the
|
|
24
24
|
# clock behaves identically for any Engine-backed worker.
|
|
25
25
|
#
|
|
@@ -308,7 +308,13 @@ module Samagotchi
|
|
|
308
308
|
# @param base_url [String] the OpenAI API base the recap asks
|
|
309
309
|
# @param api_key_env [String, nil] the variable holding its key
|
|
310
310
|
# @param target [#call, nil] -> IdleTarget
|
|
311
|
-
|
|
311
|
+
# @param turn_state [TurnState] the Engine's (Engine#turn_state): whether a
|
|
312
|
+
# turn runs, the activity seq and the latest activity's time
|
|
313
|
+
# @param messages_json [#call] → the conversation to recap, as a JSON
|
|
314
|
+
# string (Engine#messages_json_for_recap)
|
|
315
|
+
# @param emit_recap [#call] (recap:, generation:, covered:, created_at:)
|
|
316
|
+
# announces a written recap (Engine#emit_recap)
|
|
317
|
+
def initialize(turn_state:, messages_json:, emit_recap:, model: nil, base_url: nil, api_key_env: nil, target: nil,
|
|
312
318
|
inactivity: DEFAULT_INACTIVITY_SECONDS,
|
|
313
319
|
min_user_turns: DEFAULT_MIN_USER_TURNS,
|
|
314
320
|
timeout: DEFAULT_TIMEOUT_SECONDS,
|
|
@@ -318,9 +324,11 @@ module Samagotchi
|
|
|
318
324
|
session_id: -> {},
|
|
319
325
|
on_usage: nil,
|
|
320
326
|
clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
|
|
321
|
-
raise ArgumentError, "IdleRecap requires
|
|
327
|
+
raise ArgumentError, "IdleRecap requires a turn state" unless turn_state
|
|
322
328
|
|
|
323
|
-
@
|
|
329
|
+
@turn_state = turn_state
|
|
330
|
+
@messages_json = messages_json
|
|
331
|
+
@emit_recap = emit_recap
|
|
324
332
|
# The recapped session's id, sent with the request (IdleClient#summarize).
|
|
325
333
|
@session_id = session_id
|
|
326
334
|
# Called with each recap request's SideUsage (IdleClient#summarize),
|
|
@@ -381,7 +389,7 @@ module Samagotchi
|
|
|
381
389
|
def request_now
|
|
382
390
|
@run_mutex.synchronize do
|
|
383
391
|
next :in_flight if in_flight?
|
|
384
|
-
next :busy if @
|
|
392
|
+
next :busy if @turn_state.running?
|
|
385
393
|
|
|
386
394
|
start
|
|
387
395
|
end
|
|
@@ -427,14 +435,14 @@ module Samagotchi
|
|
|
427
435
|
|
|
428
436
|
# @return [Boolean] true when a recap is eligible to fire right now.
|
|
429
437
|
def should_fire?
|
|
430
|
-
return false if @
|
|
438
|
+
return false if @turn_state.running?
|
|
431
439
|
return false unless last_idle_seconds >= @inactivity
|
|
432
440
|
# Fire at most once per idle window: require that activity advanced the
|
|
433
441
|
# shared seq since the last fire. Pure monotonic time passing does NOT
|
|
434
442
|
# bump the seq, so a long idle window only summarizes once.
|
|
435
443
|
return true if @last_fire_activity_seq.nil?
|
|
436
444
|
|
|
437
|
-
@
|
|
445
|
+
@turn_state.activity_seq > @last_fire_activity_seq
|
|
438
446
|
end
|
|
439
447
|
|
|
440
448
|
# SHA1 of one message's role and text: detects a rewrite a count alone
|
|
@@ -458,7 +466,7 @@ module Samagotchi
|
|
|
458
466
|
end
|
|
459
467
|
|
|
460
468
|
def last_idle_seconds
|
|
461
|
-
@clock.call - @
|
|
469
|
+
@clock.call - @turn_state.last_activity_at
|
|
462
470
|
end
|
|
463
471
|
|
|
464
472
|
# Start an attempt: snapshot, build the prompt, and spawn the summarize
|
|
@@ -469,8 +477,8 @@ module Samagotchi
|
|
|
469
477
|
# Latch the attempt, not the success: a short history, a failed or
|
|
470
478
|
# empty summary, or an invalidated run must not re-fire on every
|
|
471
479
|
# scheduler tick. The next recorded activity re-arms the window.
|
|
472
|
-
@last_fire_activity_seq = @
|
|
473
|
-
parsed = safe_parse(@
|
|
480
|
+
@last_fire_activity_seq = @turn_state.activity_seq
|
|
481
|
+
parsed = safe_parse(@messages_json.call)
|
|
474
482
|
return :too_short if parsed.nil?
|
|
475
483
|
|
|
476
484
|
# The turns as the page counts them (Steer.turn_opening?): not Qwen's
|
|
@@ -533,7 +541,7 @@ module Samagotchi
|
|
|
533
541
|
model: served || job[:model], created_at: Time.now.utc.iso8601 }.compact
|
|
534
542
|
@mutex.synchronize { @state = saved }
|
|
535
543
|
save(saved)
|
|
536
|
-
@
|
|
544
|
+
@emit_recap.call(recap: text, generation: job[:generation], covered: job[:covered], created_at: saved[:created_at])
|
|
537
545
|
rescue StandardError
|
|
538
546
|
@in_flight = nil
|
|
539
547
|
end
|
|
@@ -19,13 +19,15 @@ module Samagotchi
|
|
|
19
19
|
|
|
20
20
|
attr_reader :inactivity
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
# @param turn_state [TurnState] the Engine's (Engine#turn_state): whether
|
|
23
|
+
# a turn runs, and when the latest activity was
|
|
24
|
+
def initialize(turn_state:, inactivity: DEFAULT_MIN_INACTIVITY_SECONDS,
|
|
23
25
|
queue: nil,
|
|
24
26
|
clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) },
|
|
25
27
|
callback: nil)
|
|
26
|
-
raise ArgumentError, "IdleReminders requires
|
|
28
|
+
raise ArgumentError, "IdleReminders requires a turn state" unless turn_state
|
|
27
29
|
|
|
28
|
-
@
|
|
30
|
+
@turn_state = turn_state
|
|
29
31
|
@inactivity = inactivity
|
|
30
32
|
@queue = queue
|
|
31
33
|
@clock = clock
|
|
@@ -43,7 +45,7 @@ module Samagotchi
|
|
|
43
45
|
|
|
44
46
|
# @return [Boolean] true when it's time to check for reminders.
|
|
45
47
|
def should_check?
|
|
46
|
-
return false if @
|
|
48
|
+
return false if @turn_state.running?
|
|
47
49
|
return false unless last_idle_seconds >= @inactivity
|
|
48
50
|
|
|
49
51
|
true
|
|
@@ -52,7 +54,7 @@ module Samagotchi
|
|
|
52
54
|
private
|
|
53
55
|
|
|
54
56
|
def last_idle_seconds
|
|
55
|
-
@clock.call - @
|
|
57
|
+
@clock.call - @turn_state.last_activity_at
|
|
56
58
|
end
|
|
57
59
|
|
|
58
60
|
def check_due_reminders
|
|
@@ -4,16 +4,16 @@ require "monitor"
|
|
|
4
4
|
require_relative "log"
|
|
5
5
|
|
|
6
6
|
module Samagotchi
|
|
7
|
-
# Shared background poller for
|
|
8
|
-
#
|
|
7
|
+
# Shared background poller for idle subsystems (currently: IdleReminders
|
|
8
|
+
# and the optional IdleRecap).
|
|
9
9
|
#
|
|
10
10
|
# Previously each subsystem ran its own 0.5s polling thread against the
|
|
11
|
-
# Engine's `last_activity_at` / `activity_seq` / `
|
|
11
|
+
# Engine's TurnState (`last_activity_at` / `activity_seq` / `running?`).
|
|
12
12
|
# Now a single thread steps every registered job in turn, so the whole
|
|
13
|
-
# idle layer polls
|
|
13
|
+
# idle layer polls from one loop.
|
|
14
14
|
#
|
|
15
15
|
# Jobs must implement `tick`. Each job keeps its own eligibility guard
|
|
16
|
-
# (e.g. `should_fire?` / `should_check?`, including the `
|
|
16
|
+
# (e.g. `should_fire?` / `should_check?`, including the `running?`
|
|
17
17
|
# check) and its own internal state (generation ids, latches). A job that
|
|
18
18
|
# raises is logged and isolated — the remaining jobs keep polling.
|
|
19
19
|
class IdleScheduler
|
|
@@ -23,14 +23,11 @@ module Samagotchi
|
|
|
23
23
|
STOP_JOIN_SECONDS = 1.0
|
|
24
24
|
THREAD_NAME = "idle_scheduler"
|
|
25
25
|
|
|
26
|
-
attr_reader :
|
|
26
|
+
attr_reader :jobs, :interval
|
|
27
27
|
|
|
28
28
|
# +interval+: the pause between ticks (a spec passes a long one, so its
|
|
29
29
|
# own #tick is the only one after the first).
|
|
30
|
-
def initialize(
|
|
31
|
-
raise ArgumentError, "IdleScheduler requires an engine" unless engine
|
|
32
|
-
|
|
33
|
-
@engine = engine
|
|
30
|
+
def initialize(jobs: [], interval: POLL_INTERVAL_SECONDS)
|
|
34
31
|
@jobs = jobs.compact.freeze
|
|
35
32
|
@interval = interval
|
|
36
33
|
# The loop's pause: a pop with the interval as its timeout, which
|
|
@@ -51,6 +51,18 @@ module Samagotchi
|
|
|
51
51
|
COMMAND_LEAD = %r{\A[!/\s]+}
|
|
52
52
|
# How much of a bad type value an error repeats.
|
|
53
53
|
MAX_ECHO = 40
|
|
54
|
+
# The schemas (docs/internals/inline-widgets.md, The schemas) as a block
|
|
55
|
+
# fix's request states them (BlockFix.request): core can't read the
|
|
56
|
+
# inline-widgets bundle's prompt sections.
|
|
57
|
+
FIX_RULES = <<~RULES.chomp
|
|
58
|
+
A chi-ui block is YAML with `type:` first, picks or walk. Quote a value that holds a colon.
|
|
59
|
+
picks: `rows`, each with `id`, `text` and `options` (2 to 4 texts); optional `title`, and on a row
|
|
60
|
+
`recommended` (one of its options) and `why`. Row ids are unique, a row's options are unique.
|
|
61
|
+
walk: `title`, `actors` (2 to 5 unique texts) and `steps`, each with `id`, `actor` (one of the actors)
|
|
62
|
+
and `does` or `sees`; optional on a step `behind`, `next` (another step's id, or `end`) and `cases`
|
|
63
|
+
(each `when` and `goto`, a step's id). Step ids are unique and none is `end`.
|
|
64
|
+
No anchors, aliases or tags; at most #{MAX_ROWS} rows, #{MAX_STEPS} steps.
|
|
65
|
+
RULES
|
|
54
66
|
|
|
55
67
|
module_function
|
|
56
68
|
|