samagotchi 0.60.0 → 0.62.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 (82) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +99 -1
  3. data/docs/architecture.md +10 -4
  4. data/docs/cli.md +22 -8
  5. data/docs/configuration.md +32 -19
  6. data/docs/context.md +8 -0
  7. data/docs/guardrails.md +2 -1
  8. data/docs/hooks.md +80 -1
  9. data/docs/internals/engine.md +57 -15
  10. data/docs/internals/prompt-caching.md +8 -1
  11. data/docs/plugins.md +49 -6
  12. data/docs/sessions.md +7 -3
  13. data/docs/testing.md +4 -1
  14. data/lib/samagotchi/answer_display.rb +4 -3
  15. data/lib/samagotchi/answer_refs.rb +210 -0
  16. data/lib/samagotchi/bridge/card_store.rb +6 -1
  17. data/lib/samagotchi/bundles/check-in/manifest.yml +2 -2
  18. data/lib/samagotchi/bundles/check-in/plugin.rb +27 -3
  19. data/lib/samagotchi/bundles/github-pr/manifest.yml +3 -3
  20. data/lib/samagotchi/bundles/github-pr/plugin.rb +70 -14
  21. data/lib/samagotchi/bundles/slow-commands/manifest.yml +2 -2
  22. data/lib/samagotchi/bundles/slow-commands/plugin.rb +5 -1
  23. data/lib/samagotchi/bundles/source-links/hooks/source_links.rb +58 -3
  24. data/lib/samagotchi/bundles/source-links/manifest.yml +4 -4
  25. data/lib/samagotchi/bundles/source-links/source_links.md +1 -1
  26. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  27. data/lib/samagotchi/engine.rb +47 -9
  28. data/lib/samagotchi/engine_peers.rb +1 -0
  29. data/lib/samagotchi/hooks/registry.rb +3 -1
  30. data/lib/samagotchi/interrupted_turn.rb +39 -0
  31. data/lib/samagotchi/kernel_loop.rb +13 -20
  32. data/lib/samagotchi/llm/chat_loop.rb +28 -17
  33. data/lib/samagotchi/log_subscriber.rb +6 -3
  34. data/lib/samagotchi/plugin/api.rb +36 -18
  35. data/lib/samagotchi/reasoning_history.rb +22 -4
  36. data/lib/samagotchi/ref.rb +25 -8
  37. data/lib/samagotchi/reply_wait.rb +6 -2
  38. data/lib/samagotchi/row_refs.rb +126 -0
  39. data/lib/samagotchi/session.rb +40 -4
  40. data/lib/samagotchi/session_manager.rb +64 -6
  41. data/lib/samagotchi/session_metrics.rb +24 -2
  42. data/lib/samagotchi/terminal_ui/formatting.rb +1 -1
  43. data/lib/samagotchi/terminal_ui.rb +30 -3
  44. data/lib/samagotchi/tool_activity.rb +8 -4
  45. data/lib/samagotchi/tool_response.rb +5 -4
  46. data/lib/samagotchi/tool_row_fields.rb +4 -2
  47. data/lib/samagotchi/tool_runner.rb +16 -3
  48. data/lib/samagotchi/tools/builtins.rb +9 -2
  49. data/lib/samagotchi/tools/execute.rb +15 -3
  50. data/lib/samagotchi/tools/peers.rb +3 -2
  51. data/lib/samagotchi/tools/registry.rb +5 -3
  52. data/lib/samagotchi/turn_checkpoint.rb +128 -0
  53. data/lib/samagotchi/version.rb +1 -1
  54. data/lib/samagotchi/web/app.rb +19 -1
  55. data/lib/samagotchi/web/message_parts.rb +17 -9
  56. data/lib/samagotchi/web/public/activity.js +4 -1
  57. data/lib/samagotchi/web/public/answer_refs.js +217 -0
  58. data/lib/samagotchi/web/public/app.js +74 -33
  59. data/lib/samagotchi/web/public/chains.js +18 -20
  60. data/lib/samagotchi/web/public/context_chips.js +92 -64
  61. data/lib/samagotchi/web/public/families_view.js +51 -39
  62. data/lib/samagotchi/web/public/index.html +66 -13
  63. data/lib/samagotchi/web/public/keep_focus.js +24 -0
  64. data/lib/samagotchi/web/public/lists_view.js +13 -13
  65. data/lib/samagotchi/web/public/llm_context_chip.js +55 -34
  66. data/lib/samagotchi/web/public/question_card.js +11 -0
  67. data/lib/samagotchi/web/public/question_cards.js +60 -5
  68. data/lib/samagotchi/web/public/ref_menu.js +24 -3
  69. data/lib/samagotchi/web/public/ref_scan.js +5 -3
  70. data/lib/samagotchi/web/public/refs.js +140 -20
  71. data/lib/samagotchi/web/public/scroll.js +42 -0
  72. data/lib/samagotchi/web/public/stage_model.js +22 -3
  73. data/lib/samagotchi/web/public/stage_view.js +299 -70
  74. data/lib/samagotchi/web/public/stream_handlers.js +5 -3
  75. data/lib/samagotchi/web/public/timing.js +1 -1
  76. data/lib/samagotchi/web/public/turn_html.js +5 -4
  77. data/lib/samagotchi/web/public/turn_model.js +49 -0
  78. data/lib/samagotchi/web/public/turn_view.js +121 -17
  79. data/lib/samagotchi/worker.rb +169 -21
  80. data/lib/samagotchi/worker_inbound.rb +5 -3
  81. data/lib/samagotchi/worker_signals.rb +81 -0
  82. metadata +8 -1
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Samagotchi
4
+ # TERM, HUP and INT in a worker process: the running turn stops as a Stop
5
+ # does (reason :signal), is saved, and the worker leaves (Worker#run
6
+ # returns :signal). SessionManager.run_session_loop installs the traps in
7
+ # a spawned worker only (WORKER_PROCESS_ENV), never in a process that runs
8
+ # the loop in-process (the specs, which fire #fire directly).
9
+ #
10
+ # A trap handler may take no lock (Mutex#lock raises ThreadError in trap
11
+ # context) and should do no IO: the trap only records the signal and
12
+ # starts a thread, which calls the worker's handler (#on_signal).
13
+ #
14
+ # Ruby's default for these signals raised SignalException in the main
15
+ # thread, mid-turn: the turn skipped every end-of-turn path and nothing of
16
+ # it reached the session file.
17
+ class WorkerSignals
18
+ SIGNALS = %w[TERM HUP INT].freeze
19
+
20
+ # Nothing trapped and nothing fires: Worker.new's default (specs, and
21
+ # any caller that isn't a spawned worker).
22
+ class None
23
+ def on_signal(&) = nil
24
+ def signal = nil
25
+ def count = 0
26
+ def hard_exit = nil
27
+ def restore! = nil
28
+ end
29
+ NONE = None.new.freeze
30
+
31
+ # @return [String, nil] the first signal received ("TERM"), nil before one
32
+ attr_reader :signal
33
+
34
+ # @param hard_exit [#call] the last resort when the worker hasn't left
35
+ # long after the signal (exit! in a real worker)
36
+ def initialize(hard_exit: -> { Process.exit!(1) })
37
+ @signal = nil
38
+ @count = 0
39
+ @handler = nil
40
+ @hard_exit = hard_exit
41
+ @previous = {}
42
+ end
43
+
44
+ # How many signals came (a second one skips the grace).
45
+ attr_reader :count
46
+
47
+ # The worker's handler, called on a thread of its own with the signal
48
+ # name and the count, once per signal.
49
+ def on_signal(&handler)
50
+ @handler = handler
51
+ end
52
+
53
+ # Trap SIGNALS in this process (the previous handlers kept for #restore!).
54
+ # @return [self]
55
+ def install!
56
+ SIGNALS.each { |name| @previous[name] = Signal.trap(name) { fire(name) } }
57
+ self
58
+ end
59
+
60
+ # The handlers from before #install!.
61
+ def restore!
62
+ @previous.each { |name, handler| Signal.trap(name, handler || "DEFAULT") }
63
+ @previous.clear
64
+ end
65
+
66
+ # What a trap does (and what specs call): record +name+ and run the
67
+ # handler on its own thread. Safe in trap context: no lock, no IO.
68
+ # @return [Thread, nil] the handler's thread
69
+ def fire(name)
70
+ @signal ||= name
71
+ @count += 1
72
+ handler = @handler
73
+ count = @count
74
+ return nil unless handler
75
+
76
+ Thread.new { handler.call(name, count) }
77
+ end
78
+
79
+ def hard_exit = @hard_exit.call
80
+ end
81
+ end
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.60.0
4
+ version: 0.62.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Dedov
@@ -150,6 +150,7 @@ files:
150
150
  - lib/samagotchi/analytics_file.rb
151
151
  - lib/samagotchi/answer_command.rb
152
152
  - lib/samagotchi/answer_display.rb
153
+ - lib/samagotchi/answer_refs.rb
153
154
  - lib/samagotchi/answer_tail.rb
154
155
  - lib/samagotchi/archive_store.rb
155
156
  - lib/samagotchi/atomic_file.rb
@@ -308,6 +309,7 @@ files:
308
309
  - lib/samagotchi/image_store.rb
309
310
  - lib/samagotchi/installed_gem.rb
310
311
  - lib/samagotchi/installed_versions.rb
312
+ - lib/samagotchi/interrupted_turn.rb
311
313
  - lib/samagotchi/iteration_limit.rb
312
314
  - lib/samagotchi/kernel_loop.rb
313
315
  - lib/samagotchi/last_turn.rb
@@ -414,6 +416,7 @@ files:
414
416
  - lib/samagotchi/reminder_queue.rb
415
417
  - lib/samagotchi/reminder_store.rb
416
418
  - lib/samagotchi/reply_wait.rb
419
+ - lib/samagotchi/row_refs.rb
417
420
  - lib/samagotchi/sampling_settings.rb
418
421
  - lib/samagotchi/self_report.rb
419
422
  - lib/samagotchi/send_command.rb
@@ -507,6 +510,7 @@ files:
507
510
  - lib/samagotchi/tools/tool_path.rb
508
511
  - lib/samagotchi/tools/web_fetch.rb
509
512
  - lib/samagotchi/tools/write.rb
513
+ - lib/samagotchi/turn_checkpoint.rb
510
514
  - lib/samagotchi/turn_flow.rb
511
515
  - lib/samagotchi/turn_note.rb
512
516
  - lib/samagotchi/turn_state.rb
@@ -530,6 +534,7 @@ files:
530
534
  - lib/samagotchi/web/public/annotate_bar.js
531
535
  - lib/samagotchi/web/public/annotate_presets.js
532
536
  - lib/samagotchi/web/public/annotations.js
537
+ - lib/samagotchi/web/public/answer_refs.js
533
538
  - lib/samagotchi/web/public/app.js
534
539
  - lib/samagotchi/web/public/card.js
535
540
  - lib/samagotchi/web/public/chains.js
@@ -553,6 +558,7 @@ files:
553
558
  - lib/samagotchi/web/public/index.html
554
559
  - lib/samagotchi/web/public/info_bar.js
555
560
  - lib/samagotchi/web/public/init_row.js
561
+ - lib/samagotchi/web/public/keep_focus.js
556
562
  - lib/samagotchi/web/public/kept_commands.js
557
563
  - lib/samagotchi/web/public/level_chip.js
558
564
  - lib/samagotchi/web/public/lists_view.js
@@ -604,6 +610,7 @@ files:
604
610
  - lib/samagotchi/worker_idle_exit.rb
605
611
  - lib/samagotchi/worker_inbound.rb
606
612
  - lib/samagotchi/worker_sidecar.rb
613
+ - lib/samagotchi/worker_signals.rb
607
614
  - lib/samagotchi/worker_wakes.rb
608
615
  - lib/samagotchi/yaml_lines.rb
609
616
  homepage: https://github.com/dm1try/samagotchi