samagotchi 0.191.0 → 0.192.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +40 -1
  3. data/docs/architecture.md +5 -4
  4. data/docs/bundles.md +1 -1
  5. data/docs/cli.md +15 -7
  6. data/docs/configuration.md +6 -1
  7. data/docs/internals/inline-widgets.md +2 -1
  8. data/docs/internals/participants.md +1 -5
  9. data/docs/internals/remote-sessions.md +3 -0
  10. data/docs/internals/session-lifecycle.md +33 -19
  11. data/docs/internals/web-frontend.md +107 -9
  12. data/docs/scheduling.md +7 -1
  13. data/docs/sessions.md +2 -2
  14. data/docs/testing.md +1 -2
  15. data/docs/thin-clients.md +98 -12
  16. data/lib/samagotchi/bridge.rb +2 -3
  17. data/lib/samagotchi/bundles/mcp/manifest.yml +4 -2
  18. data/lib/samagotchi/bundles/mcp/plugin.rb +7 -398
  19. data/lib/samagotchi/bundles/mcp/server_config.rb +158 -0
  20. data/lib/samagotchi/bundles/mcp/sign_in.rb +261 -0
  21. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  22. data/lib/samagotchi/client_id.rb +2 -19
  23. data/lib/samagotchi/compat.rb +1 -7
  24. data/lib/samagotchi/config.rb +3 -0
  25. data/lib/samagotchi/delivery.rb +1 -5
  26. data/lib/samagotchi/schedule_command.rb +1 -0
  27. data/lib/samagotchi/schedules/prune_part.rb +32 -0
  28. data/lib/samagotchi/schedules.rb +14 -9
  29. data/lib/samagotchi/session.rb +7 -0
  30. data/lib/samagotchi/session_access/remote.rb +1 -1
  31. data/lib/samagotchi/session_continue.rb +8 -1
  32. data/lib/samagotchi/session_lifecycle.rb +4 -0
  33. data/lib/samagotchi/session_manager/delivery.rb +0 -6
  34. data/lib/samagotchi/session_manager/lifecycle.rb +30 -17
  35. data/lib/samagotchi/session_manager/listing.rb +2 -1
  36. data/lib/samagotchi/session_manager/removal.rb +7 -5
  37. data/lib/samagotchi/session_manager/spawn.rb +2 -3
  38. data/lib/samagotchi/session_manager.rb +4 -0
  39. data/lib/samagotchi/session_row.rb +2 -0
  40. data/lib/samagotchi/session_transition.rb +61 -15
  41. data/lib/samagotchi/tools/cancel_schedule.rb +10 -6
  42. data/lib/samagotchi/version.rb +1 -1
  43. data/lib/samagotchi/web/app.rb +3 -1
  44. data/lib/samagotchi/web/ask_create.rb +138 -0
  45. data/lib/samagotchi/web/ask_routes.rb +56 -9
  46. data/lib/samagotchi/web/last_ask.rb +78 -0
  47. data/lib/samagotchi/web/public/answer_refs.js +3 -9
  48. data/lib/samagotchi/web/public/answer_write.js +28 -0
  49. data/lib/samagotchi/web/public/app.js +72 -96
  50. data/lib/samagotchi/web/public/block_fix.js +30 -12
  51. data/lib/samagotchi/web/public/chains.js +3 -1
  52. data/lib/samagotchi/web/public/command_list.js +5 -3
  53. data/lib/samagotchi/web/public/composer.js +9 -6
  54. data/lib/samagotchi/web/public/composer_mount.js +18 -11
  55. data/lib/samagotchi/web/public/draft_keys.js +10 -0
  56. data/lib/samagotchi/web/public/drafts.js +278 -0
  57. data/lib/samagotchi/web/public/owner_claim.js +7 -12
  58. data/lib/samagotchi/web/public/prompt_history.js +6 -0
  59. data/lib/samagotchi/web/public/send_turn.js +51 -20
  60. data/lib/samagotchi/web/public/session_actions.js +80 -16
  61. data/lib/samagotchi/web/public/session_prefs.js +9 -3
  62. data/lib/samagotchi/web/public/session_view.js +7 -2
  63. data/lib/samagotchi/web/public/turn_events.js +0 -2
  64. data/lib/samagotchi/web/public/unqueue.js +22 -9
  65. data/lib/samagotchi/web/public/viewer_self.js +1 -6
  66. data/lib/samagotchi/web/public/views.js +12 -1
  67. data/lib/samagotchi/web/session_access_routes.rb +6 -3
  68. data/lib/samagotchi/web/session_control.rb +4 -2
  69. data/lib/samagotchi/web/session_create.rb +50 -33
  70. data/lib/samagotchi/web/session_hub.rb +52 -36
  71. data/lib/samagotchi/web/session_summary.rb +45 -7
  72. data/lib/samagotchi/web/start_dir.rb +6 -4
  73. data/lib/samagotchi/worker_presence.rb +22 -6
  74. metadata +8 -1
@@ -147,8 +147,7 @@ module Samagotchi
147
147
  thread = @thread
148
148
  @thread = nil
149
149
  thread&.join(@scan_interval + 1.0)
150
- schedule_thread = @monitor.synchronize { @schedule_tick_thread }
151
- schedule_thread&.join(SCHEDULE_TICK_STOP_WAIT)
150
+ drain_schedule_tick(timeout: SCHEDULE_TICK_STOP_WAIT)
152
151
  sweep_thread = @monitor.synchronize { @sweep_thread }
153
152
  sweep_thread&.join(SWEEP_STOP_WAIT)
154
153
  nil
@@ -343,33 +342,6 @@ module Samagotchi
343
342
  @monitor.synchronize { !@subscribers.delete(handle).nil? }
344
343
  end
345
344
 
346
- private
347
-
348
- def run_loop
349
- until @stopped
350
- begin
351
- scan
352
- rescue StandardError => e
353
- Log.warn(:web, "hub_scan_failed", error: e.class.name, msg: e.message)
354
- end
355
- @wakeup.pop(timeout: @scan_interval)
356
- end
357
- end
358
-
359
- def full_probe_due?
360
- now = @now.call
361
- return false if @next_full_probe && now < @next_full_probe
362
-
363
- @next_full_probe = now + @full_probe_interval
364
- true
365
- end
366
-
367
- def sweep
368
- @manager.retention_sweep_if_due(state_dir: @state_dir)
369
- rescue StandardError => e
370
- Log.warn(:web, "retention_sweep_failed", error: e.class.name, msg: e.message)
371
- end
372
-
373
345
  # chi web can be the scheduler when the user opts in (schedule.auto_tick):
374
346
  # the same tick cron runs, gated by schedule.tick_interval_minutes and
375
347
  # sharing schedules.lock, so cron and the hub never both fire. The scan
@@ -378,18 +350,20 @@ module Samagotchi
378
350
  # auto_tick off as well (#claim_disabled_if_due). Only the scan(tick:
379
351
  # true) path gets here: server.rb's seeding scan(tick: false) runs before
380
352
  # the Notifier subscribes, and a notice claimed then would reach no one.
353
+ # @return [Thread, nil] the Thread it started, or nil when it started none.
381
354
  def schedule_tick
382
355
  claim_disabled_if_due
383
- return unless Config.get("schedule.auto_tick")
356
+ return nil unless Config.get("schedule.auto_tick")
384
357
 
385
358
  now = @now.call
386
359
  @monitor.synchronize do
387
- return if @schedule_tick_in_flight
388
- return if @next_schedule_tick && now < @next_schedule_tick
389
-
390
- @next_schedule_tick = now + (Samagotchi::Schedules.tick_interval_minutes * 60)
391
- @schedule_tick_in_flight = true
392
- @schedule_tick_thread = Thread.new { run_schedule_tick }
360
+ if @schedule_tick_in_flight || (@next_schedule_tick && now < @next_schedule_tick)
361
+ nil
362
+ else
363
+ @next_schedule_tick = now + (Samagotchi::Schedules.tick_interval_minutes * 60)
364
+ @schedule_tick_in_flight = true
365
+ @schedule_tick_thread = Thread.new { run_schedule_tick }
366
+ end
393
367
  end
394
368
  rescue StandardError => e
395
369
  @monitor.synchronize do
@@ -402,6 +376,8 @@ module Samagotchi
402
376
  nil
403
377
  end
404
378
 
379
+ # The tick's body on the caller's thread (the specs call it directly;
380
+ # #schedule_tick runs it on its own thread).
405
381
  def run_schedule_tick
406
382
  schedules = Samagotchi::Schedules.new(state_dir: @state_dir)
407
383
  # Stopping kills a condition's CMD (its process group) and runs no more.
@@ -417,6 +393,46 @@ module Samagotchi
417
393
  end
418
394
  end
419
395
 
396
+ # Whether a schedule tick is at work (started, not finished).
397
+ def schedule_tick_running?
398
+ @monitor.synchronize { @schedule_tick_in_flight }
399
+ end
400
+
401
+ # Waits for a running schedule tick to finish (it does not start one).
402
+ # @return [Boolean] true once no tick is running, else false.
403
+ def drain_schedule_tick(timeout:)
404
+ thread = @monitor.synchronize { @schedule_tick_thread }
405
+ thread&.join(timeout)
406
+ @monitor.synchronize { !@schedule_tick_in_flight }
407
+ end
408
+
409
+ private
410
+
411
+ def run_loop
412
+ until @stopped
413
+ begin
414
+ scan
415
+ rescue StandardError => e
416
+ Log.warn(:web, "hub_scan_failed", error: e.class.name, msg: e.message)
417
+ end
418
+ @wakeup.pop(timeout: @scan_interval)
419
+ end
420
+ end
421
+
422
+ def full_probe_due?
423
+ now = @now.call
424
+ return false if @next_full_probe && now < @next_full_probe
425
+
426
+ @next_full_probe = now + @full_probe_interval
427
+ true
428
+ end
429
+
430
+ def sweep
431
+ @manager.retention_sweep_if_due(state_dir: @state_dir)
432
+ rescue StandardError => e
433
+ Log.warn(:web, "retention_sweep_failed", error: e.class.name, msg: e.message)
434
+ end
435
+
420
436
  # Once per schedule.tick_interval_minutes: a lock-free read of
421
437
  # schedules.json (Schedules#claim_disabled_notices), so a cron tick's or
422
438
  # a run-now's turn-off reaches the phone within one interval. A claim
@@ -14,6 +14,8 @@ require_relative "../worker_link"
14
14
  require_relative "../compat"
15
15
  require_relative "../session_row"
16
16
  require_relative "../bridge/pending_card"
17
+ require_relative "../session_lifecycle"
18
+ require_relative "../worker_presence"
17
19
 
18
20
  module Samagotchi
19
21
  module Web
@@ -30,8 +32,27 @@ module Samagotchi
30
32
  archived project_root bridge_up worker_version worker_features worker_compat pending_question pending_card
31
33
  last_turn verdicts verify_fresh last_verdict].freeze
32
34
 
35
+ # The hub's answer for a session: its lifecycle state and whether its
36
+ # Bridge is up for it (the state, not the link alone).
37
+ Shown = Data.define(:state, :up)
38
+
33
39
  module_function
34
40
 
41
+ # What the hub should show for +session+ (its state and up), from the
42
+ # lifecycle facts WorkerPresence reads (its link passed in, or looked
43
+ # up here).
44
+ # @param session [Session]
45
+ # @param owner [OwnerLock::Owner, nil]
46
+ # @param session_dir [String] the session's folder
47
+ # @param link [WorkerLink outcome, nil] already resolved; nil: resolved here
48
+ # @return [Shown]
49
+ def shown(session, owner:, session_dir:, link: nil)
50
+ link ||= WorkerLink.resolve(session_dir, unlink: false, owner: owner)
51
+ presence = WorkerPresence.read(session.id, state_dir: File.dirname(session_dir), owner: owner, link: link)
52
+ state = SessionLifecycle::Reading.derive(presence).state
53
+ Shown.new(state: state, up: state == :live)
54
+ end
55
+
35
56
  # @param session [Session] a saved session (messages may be empty)
36
57
  # @param owner [OwnerLock::Owner, nil] SessionManager.session_owner's; its
37
58
  # kind is shown as `owner`
@@ -51,7 +72,8 @@ module Samagotchi
51
72
  def build(session, owner:, session_dir:, status: displayed_status(session, owner: owner), root_cache: nil,
52
73
  registry: nil, compat_version: VERSION)
53
74
  link = WorkerLink.resolve(session_dir, unlink: false, owner: owner)
54
- up = link.live?
75
+ shown = shown(session, owner: owner, session_dir: session_dir, link: link)
76
+ up = shown.up
55
77
  sidecar = up ? link.sidecar : nil
56
78
  saved = SessionMetrics.saved_summary(session_dir, budget_tokens: budget_resolver(session, registry))
57
79
  # The facts every list shares (SessionRow), from this one link,
@@ -60,7 +82,7 @@ module Samagotchi
60
82
  saved: saved, card: RecapStore.card(session_dir))
61
83
  values = {
62
84
  id: row.id,
63
- status: status,
85
+ status: unstopped(status, session, shown.state, owner),
64
86
  mode: session.mode,
65
87
  model_name: session.model_name,
66
88
  working_directory: row.cwd,
@@ -107,9 +129,9 @@ module Samagotchi
107
129
  # Hidden from the strip and the list unless the all view's "archived" toggle is on.
108
130
  archived: row.archived,
109
131
  project_root: row.project,
110
- # A worker is reachable: WorkerLink answers Live (the lock held by
111
- # a worker whose sidecar names its pid). A sidecar a dead worker
112
- # left is not up.
132
+ # A worker's Bridge answers and no stop is marked (the lifecycle
133
+ # state is live: a stopping worker, a worker on its way or with a
134
+ # newer protocol is not up).
113
135
  bridge_up: up,
114
136
  # The chi the live worker runs and what it can do (its sidecar;
115
137
  # nil and [] without one): the page's stale-worker badge.
@@ -119,8 +141,8 @@ module Samagotchi
119
141
  # For the tab's notifications (notify.js): an open question, the
120
142
  # running turn's open card with actions, and how the last turn
121
143
  # ended. A question saved by a worker that died is not open, nor a
122
- # one-turn chi run's (only a worker shares it): the row's live.
123
- pending_question: session.waiting_question(live: row.live),
144
+ # one-turn chi run's (only a worker shares it): the shown live.
145
+ pending_question: session.waiting_question(live: up),
124
146
  pending_card: up ? Bridge::PendingCard.read(session_dir) : nil,
125
147
  last_turn: session.last_turn&.to_file,
126
148
  # Its Verify verdicts, the fresh ones its worker hasn't taken yet
@@ -166,6 +188,22 @@ module Samagotchi
166
188
  session.displayed_status(owner: owner)
167
189
  end
168
190
 
191
+ # The status the page shows: a "stopped" overlay (or a pre-marker file
192
+ # that says stopped) is only true in a stopped state; everywhere else
193
+ # the session's own status is shown, as displayed_status would (a
194
+ # "running" with no owner is idle). A statuses== stopped file that
195
+ # predates the marker stays stopped.
196
+ def unstopped(status, session, state, owner)
197
+ return status unless status == Session::STATUS_STOPPED
198
+ return status if SessionLifecycle::STOPPED_STATES.include?(state)
199
+
200
+ loaded = session.loaded_status
201
+ return status if loaded.nil? || loaded == Session::STATUS_STOPPED
202
+
203
+ loaded == Session::STATUS_RUNNING && owner.nil? ? Session::STATUS_IDLE : loaded
204
+ end
205
+ private_class_method :unstopped
206
+
169
207
  def shown_status(session, state, turn_snapshot, owner:)
170
208
  status = displayed_status(session, state, owner: owner)
171
209
  return Session::STATUS_IDLE if status == Session::STATUS_RUNNING && turn_snapshot && !turn_snapshot["current_turn"]
@@ -10,17 +10,19 @@ module Samagotchi
10
10
  class Invalid < StandardError; end
11
11
 
12
12
  # Where +given+ came from, as its error names it.
13
- SOURCES = { flag: "--start-dir", config: "web.start_dir in config.yml" }.freeze
13
+ # web.ask_dir (a thin client's create, Web::AskCreate) may come from
14
+ # config.yml or SAMAGOTCHI_WEB_ASK_DIR: named alone.
15
+ SOURCES = { flag: "--start-dir", config: "web.start_dir in config.yml", ask_config: "web.ask_dir" }.freeze
14
16
 
15
17
  # +given+ as an absolute folder (~ expanded, a relative one against
16
- # +cwd+, or the home folder for a config value); nil or blank: +cwd+.
17
- # @param from [Symbol] :flag or :config (SOURCES)
18
+ # +cwd+, or the home folder for a setting's value); nil or blank: +cwd+.
19
+ # @param from [Symbol] :flag, :config or :ask_config (SOURCES)
18
20
  # @raise [Invalid] when it is no folder
19
21
  def self.resolve(given, cwd: Dir.pwd, from: :flag)
20
22
  return File.expand_path(cwd) if given.nil? || given.to_s.strip.empty?
21
23
 
22
24
  source = SOURCES.fetch(from)
23
- dir = File.expand_path(given.to_s.strip, from == :config ? Dir.home : cwd)
25
+ dir = File.expand_path(given.to_s.strip, from == :flag ? cwd : Dir.home)
24
26
  raise Invalid, "#{source} #{given}: no such folder" unless File.exist?(dir)
25
27
  raise Invalid, "#{source} #{given}: not a folder" unless File.directory?(dir)
26
28
 
@@ -38,9 +38,12 @@ module Samagotchi
38
38
  # the caller already read it (SessionManager.session_owner, a stand-in
39
39
  # manager's), or a reader of it called only for a valid id; :read reads
40
40
  # it here (OwnerLock.owner)
41
- # @param link [nil, :look, :unlink] also resolve the WorkerLink:
42
- # :look leaves a stale sidecar, :unlink removes it (WorkerLink.resolve,
43
- # given the owner read here or by the caller: one moment's snapshot)
41
+ # @param link [nil, :look, :unlink, WorkerLink outcome] also resolve the
42
+ # WorkerLink: :look leaves a stale sidecar, :unlink removes it (a
43
+ # WorkerLink.resolve, given the owner read here or by the caller: one
44
+ # moment's snapshot); an already resolved outcome (anything responding
45
+ # to #live?, e.g. a caller's own WorkerLink.resolve) is used as is.
46
+ # nil reads no link
44
47
  # @return [WorkerPresence]
45
48
  def self.read(session_id, state_dir: nil, owner: :read, link: nil)
46
49
  return absent(session_id, link: link) unless Session.valid_id?(session_id)
@@ -55,15 +58,28 @@ module Samagotchi
55
58
  stop_marked: Session.stopped_marker?(session_id, state_dir: sd),
56
59
  starting_marked: StartingMarker.fresh?(session_id, state_dir: sd),
57
60
  archived: ArchiveStore.archived?(dir),
58
- link: link && WorkerLink.resolve(dir, unlink: link == :unlink, owner: owner))
61
+ link: resolved_link(link, dir, owner))
59
62
  end
60
63
 
64
+ # The +link:+ argument as the outcome to carry: a resolved outcome
65
+ # (anything responding to #live?) is used as is; nil reads no link;
66
+ # :look or :unlink resolves the WorkerLink (WorkerLink.resolve).
67
+ # @return [WorkerLink outcome, nil]
68
+ def self.resolved_link(link, dir, owner)
69
+ return nil if link.nil?
70
+ return link if link.respond_to?(:live?)
71
+
72
+ WorkerLink.resolve(dir, unlink: link == :unlink, owner: owner)
73
+ end
74
+ private_class_method :resolved_link
75
+
61
76
  # An id that isn't one names no session: nothing on disk is read
62
- # (Session.session_dir would raise), and a link asked for is None.
77
+ # (Session.session_dir would raise), and a link asked for resolves as
78
+ # None unless it was already resolved.
63
79
  # @return [WorkerPresence]
64
80
  def self.absent(session_id, link: nil)
65
81
  new(session_id: session_id, exists: false, owner: nil, stop_marked: false, starting_marked: false,
66
- archived: false, link: link && WorkerLink::None.new)
82
+ archived: false, link: link.respond_to?(:live?) ? link : (link && WorkerLink::None.new))
67
83
  end
68
84
 
69
85
  def initialize(session_id:, exists:, owner:, stop_marked:, starting_marked:, archived:, link: nil) = super
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samagotchi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.191.0
4
+ version: 0.192.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Dedov
@@ -296,6 +296,8 @@ files:
296
296
  - lib/samagotchi/bundles/mcp/manifest.yml
297
297
  - lib/samagotchi/bundles/mcp/oauth.rb
298
298
  - lib/samagotchi/bundles/mcp/plugin.rb
299
+ - lib/samagotchi/bundles/mcp/server_config.rb
300
+ - lib/samagotchi/bundles/mcp/sign_in.rb
299
301
  - lib/samagotchi/bundles/mcp/tools.rb
300
302
  - lib/samagotchi/bundles/model-notes/manifest.yml
301
303
  - lib/samagotchi/bundles/model-notes/model_notes_deepseek.md
@@ -991,6 +993,7 @@ files:
991
993
  - lib/samagotchi/visitor.rb
992
994
  - lib/samagotchi/waiting_steer.rb
993
995
  - lib/samagotchi/web/app.rb
996
+ - lib/samagotchi/web/ask_create.rb
994
997
  - lib/samagotchi/web/ask_routes.rb
995
998
  - lib/samagotchi/web/auto_update.rb
996
999
  - lib/samagotchi/web/auto_update_record.rb
@@ -1000,6 +1003,7 @@ files:
1000
1003
  - lib/samagotchi/web/editor.rb
1001
1004
  - lib/samagotchi/web/info_probe.rb
1002
1005
  - lib/samagotchi/web/lan.rb
1006
+ - lib/samagotchi/web/last_ask.rb
1003
1007
  - lib/samagotchi/web/link_codes.rb
1004
1008
  - lib/samagotchi/web/markdown_renderer.rb
1005
1009
  - lib/samagotchi/web/mcp_route.rb
@@ -1025,6 +1029,7 @@ files:
1025
1029
  - lib/samagotchi/web/public/annotations.js
1026
1030
  - lib/samagotchi/web/public/answer_fold.js
1027
1031
  - lib/samagotchi/web/public/answer_refs.js
1032
+ - lib/samagotchi/web/public/answer_write.js
1028
1033
  - lib/samagotchi/web/public/app.js
1029
1034
  - lib/samagotchi/web/public/apple-touch-icon.png
1030
1035
  - lib/samagotchi/web/public/auth_lost.js
@@ -1071,6 +1076,8 @@ files:
1071
1076
  - lib/samagotchi/web/public/diagram_wiring.js
1072
1077
  - lib/samagotchi/web/public/diff_view.js
1073
1078
  - lib/samagotchi/web/public/dock_reserve.js
1079
+ - lib/samagotchi/web/public/draft_keys.js
1080
+ - lib/samagotchi/web/public/drafts.js
1074
1081
  - lib/samagotchi/web/public/families_view.js
1075
1082
  - lib/samagotchi/web/public/favicon.svg
1076
1083
  - lib/samagotchi/web/public/feature_gates.js